xref: /freebsd/sys/dev/netmap/netmap.c (revision c929ca72c93bc519ac32790778525394ee4bb446)
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 
1274bf50f18SLuigi Rizzo 
1284bf50f18SLuigi Rizzo /* --- internals ----
1294bf50f18SLuigi Rizzo  *
1304bf50f18SLuigi Rizzo  * Roadmap to the code that implements the above.
1314bf50f18SLuigi Rizzo  *
1324bf50f18SLuigi Rizzo  * > 1. a process/thread issues one or more open() on /dev/netmap, to create
1334bf50f18SLuigi Rizzo  * >    select()able file descriptor on which events are reported.
1344bf50f18SLuigi Rizzo  *
1354bf50f18SLuigi Rizzo  *  	Internally, we allocate a netmap_priv_d structure, that will be
1364bf50f18SLuigi Rizzo  *  	initialized on ioctl(NIOCREGIF).
1374bf50f18SLuigi Rizzo  *
1384bf50f18SLuigi Rizzo  *      os-specific:
1394bf50f18SLuigi Rizzo  *  	    FreeBSD: netmap_open (netmap_freebsd.c). The priv is
1404bf50f18SLuigi Rizzo  *  		     per-thread.
1414bf50f18SLuigi Rizzo  *  	    linux:   linux_netmap_open (netmap_linux.c). The priv is
1424bf50f18SLuigi Rizzo  *  		     per-open.
1434bf50f18SLuigi Rizzo  *
1444bf50f18SLuigi Rizzo  * > 2. on each descriptor, the process issues an ioctl() to identify
1454bf50f18SLuigi Rizzo  * >    the interface that should report events to the file descriptor.
1464bf50f18SLuigi Rizzo  *
1474bf50f18SLuigi Rizzo  * 	Implemented by netmap_ioctl(), NIOCREGIF case, with nmr->nr_cmd==0.
1484bf50f18SLuigi Rizzo  * 	Most important things happen in netmap_get_na() and
1494bf50f18SLuigi Rizzo  * 	netmap_do_regif(), called from there. Additional details can be
1504bf50f18SLuigi Rizzo  * 	found in the comments above those functions.
1514bf50f18SLuigi Rizzo  *
1524bf50f18SLuigi Rizzo  * 	In all cases, this action creates/takes-a-reference-to a
1534bf50f18SLuigi Rizzo  * 	netmap_*_adapter describing the port, and allocates a netmap_if
1544bf50f18SLuigi Rizzo  * 	and all necessary netmap rings, filling them with netmap buffers.
1554bf50f18SLuigi Rizzo  *
1564bf50f18SLuigi Rizzo  *      In this phase, the sync callbacks for each ring are set (these are used
1574bf50f18SLuigi Rizzo  *      in steps 5 and 6 below).  The callbacks depend on the type of adapter.
1584bf50f18SLuigi Rizzo  *      The adapter creation/initialization code puts them in the
1594bf50f18SLuigi Rizzo  * 	netmap_adapter (fields na->nm_txsync and na->nm_rxsync).  Then, they
1604bf50f18SLuigi Rizzo  * 	are copied from there to the netmap_kring's during netmap_do_regif(), by
1614bf50f18SLuigi Rizzo  * 	the nm_krings_create() callback.  All the nm_krings_create callbacks
1624bf50f18SLuigi Rizzo  * 	actually call netmap_krings_create() to perform this and the other
1634bf50f18SLuigi Rizzo  * 	common stuff. netmap_krings_create() also takes care of the host rings,
1644bf50f18SLuigi Rizzo  * 	if needed, by setting their sync callbacks appropriately.
1654bf50f18SLuigi Rizzo  *
1664bf50f18SLuigi Rizzo  * 	Additional actions depend on the kind of netmap_adapter that has been
1674bf50f18SLuigi Rizzo  * 	registered:
1684bf50f18SLuigi Rizzo  *
1694bf50f18SLuigi Rizzo  * 	- netmap_hw_adapter:  	     [netmap.c]
1704bf50f18SLuigi Rizzo  * 	     This is a system netdev/ifp with native netmap support.
1714bf50f18SLuigi Rizzo  * 	     The ifp is detached from the host stack by redirecting:
1724bf50f18SLuigi Rizzo  * 	       - transmissions (from the network stack) to netmap_transmit()
1734bf50f18SLuigi Rizzo  * 	       - receive notifications to the nm_notify() callback for
1744bf50f18SLuigi Rizzo  * 	         this adapter. The callback is normally netmap_notify(), unless
1754bf50f18SLuigi Rizzo  * 	         the ifp is attached to a bridge using bwrap, in which case it
1764bf50f18SLuigi Rizzo  * 	         is netmap_bwrap_intr_notify().
1774bf50f18SLuigi Rizzo  *
1784bf50f18SLuigi Rizzo  * 	- netmap_generic_adapter:      [netmap_generic.c]
1794bf50f18SLuigi Rizzo  * 	      A system netdev/ifp without native netmap support.
1804bf50f18SLuigi Rizzo  *
1814bf50f18SLuigi Rizzo  * 	(the decision about native/non native support is taken in
1824bf50f18SLuigi Rizzo  * 	 netmap_get_hw_na(), called by netmap_get_na())
1834bf50f18SLuigi Rizzo  *
1844bf50f18SLuigi Rizzo  * 	- netmap_vp_adapter 		[netmap_vale.c]
1854bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_bdg_na().
1864bf50f18SLuigi Rizzo  * 	      This is a persistent or ephemeral VALE port. Ephemeral ports
1874bf50f18SLuigi Rizzo  * 	      are created on the fly if they don't already exist, and are
1884bf50f18SLuigi Rizzo  * 	      always attached to a bridge.
1894bf50f18SLuigi Rizzo  * 	      Persistent VALE ports must must be created seperately, and i
1904bf50f18SLuigi Rizzo  * 	      then attached like normal NICs. The NIOCREGIF we are examining
1914bf50f18SLuigi Rizzo  * 	      will find them only if they had previosly been created and
1924bf50f18SLuigi Rizzo  * 	      attached (see VALE_CTL below).
1934bf50f18SLuigi Rizzo  *
1944bf50f18SLuigi Rizzo  * 	- netmap_pipe_adapter 	      [netmap_pipe.c]
1954bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_pipe_na().
1964bf50f18SLuigi Rizzo  * 	      Both pipe ends are created, if they didn't already exist.
1974bf50f18SLuigi Rizzo  *
1984bf50f18SLuigi Rizzo  * 	- netmap_monitor_adapter      [netmap_monitor.c]
1994bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_monitor_na().
2004bf50f18SLuigi Rizzo  * 	      If successful, the nm_sync callbacks of the monitored adapter
2014bf50f18SLuigi Rizzo  * 	      will be intercepted by the returned monitor.
2024bf50f18SLuigi Rizzo  *
2034bf50f18SLuigi Rizzo  * 	- netmap_bwrap_adapter	      [netmap_vale.c]
2044bf50f18SLuigi Rizzo  * 	      Cannot be obtained in this way, see VALE_CTL below
2054bf50f18SLuigi Rizzo  *
2064bf50f18SLuigi Rizzo  *
2074bf50f18SLuigi Rizzo  * 	os-specific:
2084bf50f18SLuigi Rizzo  * 	    linux: we first go through linux_netmap_ioctl() to
2094bf50f18SLuigi Rizzo  * 	           adapt the FreeBSD interface to the linux one.
2104bf50f18SLuigi Rizzo  *
2114bf50f18SLuigi Rizzo  *
2124bf50f18SLuigi Rizzo  * > 3. on each descriptor, the process issues an mmap() request to
2134bf50f18SLuigi Rizzo  * >    map the shared memory region within the process' address space.
2144bf50f18SLuigi Rizzo  * >    The list of interesting queues is indicated by a location in
2154bf50f18SLuigi Rizzo  * >    the shared memory region.
2164bf50f18SLuigi Rizzo  *
2174bf50f18SLuigi Rizzo  *      os-specific:
2184bf50f18SLuigi Rizzo  *  	    FreeBSD: netmap_mmap_single (netmap_freebsd.c).
2194bf50f18SLuigi Rizzo  *  	    linux:   linux_netmap_mmap (netmap_linux.c).
2204bf50f18SLuigi Rizzo  *
2214bf50f18SLuigi Rizzo  * > 4. using the functions in the netmap(4) userspace API, a process
2224bf50f18SLuigi Rizzo  * >    can look up the occupation state of a queue, access memory buffers,
2234bf50f18SLuigi Rizzo  * >    and retrieve received packets or enqueue packets to transmit.
2244bf50f18SLuigi Rizzo  *
2254bf50f18SLuigi Rizzo  * 	these actions do not involve the kernel.
2264bf50f18SLuigi Rizzo  *
2274bf50f18SLuigi Rizzo  * > 5. using some ioctl()s the process can synchronize the userspace view
2284bf50f18SLuigi Rizzo  * >    of the queue with the actual status in the kernel. This includes both
2294bf50f18SLuigi Rizzo  * >    receiving the notification of new packets, and transmitting new
2304bf50f18SLuigi Rizzo  * >    packets on the output interface.
2314bf50f18SLuigi Rizzo  *
2324bf50f18SLuigi Rizzo  * 	These are implemented in netmap_ioctl(), NIOCTXSYNC and NIOCRXSYNC
2334bf50f18SLuigi Rizzo  * 	cases. They invoke the nm_sync callbacks on the netmap_kring
2344bf50f18SLuigi Rizzo  * 	structures, as initialized in step 2 and maybe later modified
2354bf50f18SLuigi Rizzo  * 	by a monitor. Monitors, however, will always call the original
2364bf50f18SLuigi Rizzo  * 	callback before doing anything else.
2374bf50f18SLuigi Rizzo  *
2384bf50f18SLuigi Rizzo  *
2394bf50f18SLuigi Rizzo  * > 6. select() or poll() can be used to wait for events on individual
2404bf50f18SLuigi Rizzo  * >    transmit or receive queues (or all queues for a given interface).
2414bf50f18SLuigi Rizzo  *
2424bf50f18SLuigi Rizzo  * 	Implemented in netmap_poll(). This will call the same nm_sync()
2434bf50f18SLuigi Rizzo  * 	callbacks as in step 5 above.
2444bf50f18SLuigi Rizzo  *
2454bf50f18SLuigi Rizzo  * 	os-specific:
2464bf50f18SLuigi Rizzo  * 		linux: we first go through linux_netmap_poll() to adapt
2474bf50f18SLuigi Rizzo  * 		       the FreeBSD interface to the linux one.
2484bf50f18SLuigi Rizzo  *
2494bf50f18SLuigi Rizzo  *
2504bf50f18SLuigi Rizzo  *  ----  VALE_CTL -----
2514bf50f18SLuigi Rizzo  *
2524bf50f18SLuigi Rizzo  *  VALE switches are controlled by issuing a NIOCREGIF with a non-null
2534bf50f18SLuigi Rizzo  *  nr_cmd in the nmreq structure. These subcommands are handled by
2544bf50f18SLuigi Rizzo  *  netmap_bdg_ctl() in netmap_vale.c. Persistent VALE ports are created
2554bf50f18SLuigi Rizzo  *  and destroyed by issuing the NETMAP_BDG_NEWIF and NETMAP_BDG_DELIF
2564bf50f18SLuigi Rizzo  *  subcommands, respectively.
2574bf50f18SLuigi Rizzo  *
2584bf50f18SLuigi Rizzo  *  Any network interface known to the system (including a persistent VALE
2594bf50f18SLuigi Rizzo  *  port) can be attached to a VALE switch by issuing the
2604bf50f18SLuigi Rizzo  *  NETMAP_BDG_ATTACH subcommand. After the attachment, persistent VALE ports
2614bf50f18SLuigi Rizzo  *  look exactly like ephemeral VALE ports (as created in step 2 above).  The
2624bf50f18SLuigi Rizzo  *  attachment of other interfaces, instead, requires the creation of a
2634bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  Moreover, the attached interface must be put in
2644bf50f18SLuigi Rizzo  *  netmap mode. This may require the creation of a netmap_generic_adapter if
2654bf50f18SLuigi Rizzo  *  we have no native support for the interface, or if generic adapters have
2664bf50f18SLuigi Rizzo  *  been forced by sysctl.
2674bf50f18SLuigi Rizzo  *
2684bf50f18SLuigi Rizzo  *  Both persistent VALE ports and bwraps are handled by netmap_get_bdg_na(),
2694bf50f18SLuigi Rizzo  *  called by nm_bdg_ctl_attach(), and discriminated by the nm_bdg_attach()
2704bf50f18SLuigi Rizzo  *  callback.  In the case of the bwrap, the callback creates the
2714bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  The initialization of the bwrap is then
2724bf50f18SLuigi Rizzo  *  completed by calling netmap_do_regif() on it, in the nm_bdg_ctl()
2734bf50f18SLuigi Rizzo  *  callback (netmap_bwrap_bdg_ctl in netmap_vale.c).
2744bf50f18SLuigi Rizzo  *  A generic adapter for the wrapped ifp will be created if needed, when
2754bf50f18SLuigi Rizzo  *  netmap_get_bdg_na() calls netmap_get_hw_na().
2764bf50f18SLuigi Rizzo  *
2774bf50f18SLuigi Rizzo  *
2784bf50f18SLuigi Rizzo  *  ---- DATAPATHS -----
2794bf50f18SLuigi Rizzo  *
2804bf50f18SLuigi Rizzo  *              -= SYSTEM DEVICE WITH NATIVE SUPPORT =-
2814bf50f18SLuigi Rizzo  *
2824bf50f18SLuigi Rizzo  *    na == NA(ifp) == netmap_hw_adapter created in DEVICE_netmap_attach()
2834bf50f18SLuigi Rizzo  *
2844bf50f18SLuigi Rizzo  *    - tx from netmap userspace:
2854bf50f18SLuigi Rizzo  *	 concurrently:
2864bf50f18SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
2874bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_txsync()
2884bf50f18SLuigi Rizzo  *           2) device interrupt handler
2894bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
2904bf50f18SLuigi Rizzo  *    - rx from netmap userspace:
2914bf50f18SLuigi Rizzo  *       concurrently:
2924bf50f18SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
2934bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_rxsync()
2944bf50f18SLuigi Rizzo  *           2) device interrupt handler
2954bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
2964bf50f18SLuigi Rizzo  *    - tx from host stack
2974bf50f18SLuigi Rizzo  *       concurrently:
2984bf50f18SLuigi Rizzo  *           1) host stack
2994bf50f18SLuigi Rizzo  *                netmap_transmit()
3004bf50f18SLuigi Rizzo  *                  na->nm_notify  == netmap_notify()
3014bf50f18SLuigi Rizzo  *           2) ioctl(NIOCRXSYNC)/netmap_poll() in process context
3024bf50f18SLuigi Rizzo  *                kring->nm_sync() == netmap_rxsync_from_host_compat
3034bf50f18SLuigi Rizzo  *                  netmap_rxsync_from_host(na, NULL, NULL)
3044bf50f18SLuigi Rizzo  *    - tx to host stack
3054bf50f18SLuigi Rizzo  *           ioctl(NIOCTXSYNC)/netmap_poll() in process context
3064bf50f18SLuigi Rizzo  *             kring->nm_sync() == netmap_txsync_to_host_compat
3074bf50f18SLuigi Rizzo  *               netmap_txsync_to_host(na)
3084bf50f18SLuigi Rizzo  *                 NM_SEND_UP()
3094bf50f18SLuigi Rizzo  *                   FreeBSD: na->if_input() == ?? XXX
3104bf50f18SLuigi Rizzo  *                   linux: netif_rx() with NM_MAGIC_PRIORITY_RX
3114bf50f18SLuigi Rizzo  *
3124bf50f18SLuigi Rizzo  *
3134bf50f18SLuigi Rizzo  *
3144bf50f18SLuigi Rizzo  *               -= SYSTEM DEVICE WITH GENERIC SUPPORT =-
3154bf50f18SLuigi Rizzo  *
3164bf50f18SLuigi Rizzo  *
3174bf50f18SLuigi Rizzo  *
3184bf50f18SLuigi Rizzo  *                           -= VALE PORT =-
3194bf50f18SLuigi Rizzo  *
3204bf50f18SLuigi Rizzo  *
3214bf50f18SLuigi Rizzo  *
3224bf50f18SLuigi Rizzo  *                           -= NETMAP PIPE =-
3234bf50f18SLuigi Rizzo  *
3244bf50f18SLuigi Rizzo  *
3254bf50f18SLuigi Rizzo  *
3264bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH NATIVE SUPPORT, CONNECTED TO VALE, NO HOST RINGS =-
3274bf50f18SLuigi Rizzo  *
3284bf50f18SLuigi Rizzo  *
3294bf50f18SLuigi Rizzo  *
3304bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH NATIVE SUPPORT, CONNECTED TO VALE, WITH HOST RINGS =-
3314bf50f18SLuigi Rizzo  *
3324bf50f18SLuigi Rizzo  *
3334bf50f18SLuigi Rizzo  *
3344bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH GENERIC SUPPORT, CONNECTED TO VALE, NO HOST RINGS =-
3354bf50f18SLuigi Rizzo  *
3364bf50f18SLuigi Rizzo  *
3374bf50f18SLuigi Rizzo  *
3384bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH GENERIC SUPPORT, CONNECTED TO VALE, WITH HOST RINGS =-
3394bf50f18SLuigi Rizzo  *
3404bf50f18SLuigi Rizzo  *
3414bf50f18SLuigi Rizzo  *
3424bf50f18SLuigi Rizzo  */
3434bf50f18SLuigi Rizzo 
344ce3ee1e7SLuigi Rizzo /*
345ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
346ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
347ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
348ce3ee1e7SLuigi Rizzo  */
34901c7d25fSLuigi Rizzo 
350ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
35168b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
35268b8534bSLuigi Rizzo #include <sys/types.h>
35368b8534bSLuigi Rizzo #include <sys/errno.h>
35468b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
35568b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
356f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
35789e3fd52SLuigi Rizzo #include <sys/filio.h>	/* FIONBIO */
35868b8534bSLuigi Rizzo #include <sys/sockio.h>
35968b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
36068b8534bSLuigi Rizzo #include <sys/malloc.h>
36168b8534bSLuigi Rizzo #include <sys/poll.h>
36289f6b863SAttilio Rao #include <sys/rwlock.h>
36368b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
36468b8534bSLuigi Rizzo #include <sys/selinfo.h>
36568b8534bSLuigi Rizzo #include <sys/sysctl.h>
366339f59c0SGleb Smirnoff #include <sys/jail.h>
367339f59c0SGleb Smirnoff #include <net/vnet.h>
36868b8534bSLuigi Rizzo #include <net/if.h>
36976039bc8SGleb Smirnoff #include <net/if_var.h>
37068b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
37168b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
372ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
373ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
37468b8534bSLuigi Rizzo 
37568b8534bSLuigi Rizzo 
376f9790aebSLuigi Rizzo /* reduce conditional code */
377f0ea3689SLuigi Rizzo // linux API, use for the knlist in FreeBSD
3780e73f29aSLuigi Rizzo /* use a private mutex for the knlist */
3790e73f29aSLuigi Rizzo #define init_waitqueue_head(x) do {			\
3800e73f29aSLuigi Rizzo 	struct mtx *m = &(x)->m;			\
3810e73f29aSLuigi Rizzo 	mtx_init(m, "nm_kn_lock", NULL, MTX_DEF);	\
3820e73f29aSLuigi Rizzo 	knlist_init_mtx(&(x)->si.si_note, m);		\
3830e73f29aSLuigi Rizzo     } while (0)
384ce3ee1e7SLuigi Rizzo 
3850e73f29aSLuigi Rizzo #define OS_selrecord(a, b)	selrecord(a, &((b)->si))
386f0ea3689SLuigi Rizzo #define OS_selwakeup(a, b)	freebsd_selwakeup(a, b)
387ce3ee1e7SLuigi Rizzo 
388ce3ee1e7SLuigi Rizzo #elif defined(linux)
389ce3ee1e7SLuigi Rizzo 
390ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
391ce3ee1e7SLuigi Rizzo 
392ce3ee1e7SLuigi Rizzo 
393ce3ee1e7SLuigi Rizzo 
394ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
395ce3ee1e7SLuigi Rizzo 
396ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
397ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
398ce3ee1e7SLuigi Rizzo 
399ce3ee1e7SLuigi Rizzo #else
400ce3ee1e7SLuigi Rizzo 
401ce3ee1e7SLuigi Rizzo #error	Unsupported platform
402ce3ee1e7SLuigi Rizzo 
403ce3ee1e7SLuigi Rizzo #endif /* unsupported */
404ce3ee1e7SLuigi Rizzo 
405ce3ee1e7SLuigi Rizzo /*
406ce3ee1e7SLuigi Rizzo  * common headers
407ce3ee1e7SLuigi Rizzo  */
4080b8ed8e0SLuigi Rizzo #include <net/netmap.h>
4090b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
410ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
4110b8ed8e0SLuigi Rizzo 
412ce3ee1e7SLuigi Rizzo 
413ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
414ce3ee1e7SLuigi Rizzo 
415ce3ee1e7SLuigi Rizzo /*
416ce3ee1e7SLuigi Rizzo  * The following variables are used by the drivers and replicate
417ce3ee1e7SLuigi Rizzo  * fields in the global memory pool. They only refer to buffers
418ce3ee1e7SLuigi Rizzo  * used by physical interfaces.
419ce3ee1e7SLuigi Rizzo  */
4205819da83SLuigi Rizzo u_int netmap_total_buffers;
4218241616dSLuigi Rizzo u_int netmap_buf_size;
422ce3ee1e7SLuigi Rizzo char *netmap_buffer_base;	/* also address of an invalid buffer */
4235819da83SLuigi Rizzo 
4245819da83SLuigi Rizzo /* user-controlled variables */
4255819da83SLuigi Rizzo int netmap_verbose;
4265819da83SLuigi Rizzo 
4275819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
4285819da83SLuigi Rizzo 
4295819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
4305819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
4315819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
4325819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
4335819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
4345819da83SLuigi Rizzo int netmap_mitigate = 1;
4355819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
436c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
4375819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
4385819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
439f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
440f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
441f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
4425819da83SLuigi Rizzo 
4434bf50f18SLuigi Rizzo int netmap_adaptive_io = 0;
4444bf50f18SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, adaptive_io, CTLFLAG_RW,
4454bf50f18SLuigi Rizzo     &netmap_adaptive_io, 0 , "Adaptive I/O on paravirt");
4464bf50f18SLuigi Rizzo 
447f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
448091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
449ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */
450f196ce38SLuigi Rizzo 
451f9790aebSLuigi Rizzo /*
452f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
453f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
454f9790aebSLuigi Rizzo  */
455f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
456f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
457f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
458f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
459f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
460f9790aebSLuigi Rizzo 
461f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
462f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
463f0ea3689SLuigi Rizzo int netmap_generic_rings = 1;   /* number of queues in generic. */
464f9790aebSLuigi Rizzo 
465f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
466091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
467ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, "");
468f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
469f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
470f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
471f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW, &netmap_generic_rings, 0 , "");
472f196ce38SLuigi Rizzo 
473ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
474ce3ee1e7SLuigi Rizzo 
475ce3ee1e7SLuigi Rizzo 
476f9790aebSLuigi Rizzo static void
477f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr)
478ce3ee1e7SLuigi Rizzo {
479ce3ee1e7SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
480ce3ee1e7SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
481ce3ee1e7SLuigi Rizzo }
482ce3ee1e7SLuigi Rizzo 
483f9790aebSLuigi Rizzo 
48417885a7bSLuigi Rizzo /*
48517885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
48617885a7bSLuigi Rizzo  * to make sure other users get to see it.
48717885a7bSLuigi Rizzo  */
4884bf50f18SLuigi Rizzo static void
489f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
490ce3ee1e7SLuigi Rizzo {
491ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
492ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
493ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
494ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
495ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
496ce3ee1e7SLuigi Rizzo }
497ce3ee1e7SLuigi Rizzo 
4984bf50f18SLuigi Rizzo /* stop or enable a single tx ring */
4994bf50f18SLuigi Rizzo void
5004bf50f18SLuigi Rizzo netmap_set_txring(struct netmap_adapter *na, u_int ring_id, int stopped)
5014bf50f18SLuigi Rizzo {
5024bf50f18SLuigi Rizzo 	if (stopped)
5034bf50f18SLuigi Rizzo 		netmap_disable_ring(na->tx_rings + ring_id);
5044bf50f18SLuigi Rizzo 	else
5054bf50f18SLuigi Rizzo 		na->tx_rings[ring_id].nkr_stopped = 0;
5064bf50f18SLuigi Rizzo 	/* nofify that the stopped state has changed. This is currently
5074bf50f18SLuigi Rizzo 	 *only used by bwrap to propagate the state to its own krings.
5084bf50f18SLuigi Rizzo 	 * (see netmap_bwrap_intr_notify).
5094bf50f18SLuigi Rizzo 	 */
5104bf50f18SLuigi Rizzo 	na->nm_notify(na, ring_id, NR_TX, NAF_DISABLE_NOTIFY);
5114bf50f18SLuigi Rizzo }
5124bf50f18SLuigi Rizzo 
5134bf50f18SLuigi Rizzo /* stop or enable a single rx ring */
5144bf50f18SLuigi Rizzo void
5154bf50f18SLuigi Rizzo netmap_set_rxring(struct netmap_adapter *na, u_int ring_id, int stopped)
5164bf50f18SLuigi Rizzo {
5174bf50f18SLuigi Rizzo 	if (stopped)
5184bf50f18SLuigi Rizzo 		netmap_disable_ring(na->rx_rings + ring_id);
5194bf50f18SLuigi Rizzo 	else
5204bf50f18SLuigi Rizzo 		na->rx_rings[ring_id].nkr_stopped = 0;
5214bf50f18SLuigi Rizzo 	/* nofify that the stopped state has changed. This is currently
5224bf50f18SLuigi Rizzo 	 *only used by bwrap to propagate the state to its own krings.
5234bf50f18SLuigi Rizzo 	 * (see netmap_bwrap_intr_notify).
5244bf50f18SLuigi Rizzo 	 */
5254bf50f18SLuigi Rizzo 	na->nm_notify(na, ring_id, NR_RX, NAF_DISABLE_NOTIFY);
5264bf50f18SLuigi Rizzo }
5274bf50f18SLuigi Rizzo 
528f9790aebSLuigi Rizzo 
52989cc2556SLuigi Rizzo /* stop or enable all the rings of na */
5304bf50f18SLuigi Rizzo void
5314bf50f18SLuigi Rizzo netmap_set_all_rings(struct netmap_adapter *na, int stopped)
532ce3ee1e7SLuigi Rizzo {
533ce3ee1e7SLuigi Rizzo 	int i;
534f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
535ce3ee1e7SLuigi Rizzo 
5364bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
537ce3ee1e7SLuigi Rizzo 		return;
538ce3ee1e7SLuigi Rizzo 
539f0ea3689SLuigi Rizzo 	ntx = netmap_real_tx_rings(na);
540f0ea3689SLuigi Rizzo 	nrx = netmap_real_rx_rings(na);
541f0ea3689SLuigi Rizzo 
542f0ea3689SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
5434bf50f18SLuigi Rizzo 		netmap_set_txring(na, i, stopped);
544ce3ee1e7SLuigi Rizzo 	}
545f9790aebSLuigi Rizzo 
546f0ea3689SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
5474bf50f18SLuigi Rizzo 		netmap_set_rxring(na, i, stopped);
548ce3ee1e7SLuigi Rizzo 	}
549ce3ee1e7SLuigi Rizzo }
550ce3ee1e7SLuigi Rizzo 
55189cc2556SLuigi Rizzo /*
55289cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
55389cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
55489cc2556SLuigi Rizzo  * netmap mode off, or before removing the harware rings (e.g., on module
55589cc2556SLuigi Rizzo  * onload).  As a rule of thumb for linux drivers, this should be placed near
55689cc2556SLuigi Rizzo  * each napi_disable().
55789cc2556SLuigi Rizzo  */
558f9790aebSLuigi Rizzo void
559f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
560f9790aebSLuigi Rizzo {
5614bf50f18SLuigi Rizzo 	netmap_set_all_rings(NA(ifp), 1 /* stopped */);
562f9790aebSLuigi Rizzo }
563f9790aebSLuigi Rizzo 
56489cc2556SLuigi Rizzo /*
56589cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
56689cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
56789cc2556SLuigi Rizzo  * napi_enable().
56889cc2556SLuigi Rizzo  */
569f9790aebSLuigi Rizzo void
570f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
571f9790aebSLuigi Rizzo {
5724bf50f18SLuigi Rizzo 	netmap_set_all_rings(NA(ifp), 0 /* enabled */);
573f9790aebSLuigi Rizzo }
574f9790aebSLuigi Rizzo 
575f9790aebSLuigi Rizzo 
576ce3ee1e7SLuigi Rizzo /*
577ce3ee1e7SLuigi Rizzo  * generic bound_checking function
578ce3ee1e7SLuigi Rizzo  */
579ce3ee1e7SLuigi Rizzo u_int
580ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
581ce3ee1e7SLuigi Rizzo {
582ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
583ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
584ce3ee1e7SLuigi Rizzo 
585ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
586ce3ee1e7SLuigi Rizzo 		dflt = lo;
587ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
588ce3ee1e7SLuigi Rizzo 		dflt = hi;
589ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
590ce3ee1e7SLuigi Rizzo 		*v = dflt;
591ce3ee1e7SLuigi Rizzo 		op = "Bump";
592ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
593ce3ee1e7SLuigi Rizzo 		*v = hi;
594ce3ee1e7SLuigi Rizzo 		op = "Clamp";
595ce3ee1e7SLuigi Rizzo 	}
596ce3ee1e7SLuigi Rizzo 	if (op && msg)
597ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
598ce3ee1e7SLuigi Rizzo 	return *v;
599ce3ee1e7SLuigi Rizzo }
600ce3ee1e7SLuigi Rizzo 
601f9790aebSLuigi Rizzo 
602ce3ee1e7SLuigi Rizzo /*
603ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
604ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
605ce3ee1e7SLuigi Rizzo  */
606ce3ee1e7SLuigi Rizzo const char *
607ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
608ce3ee1e7SLuigi Rizzo {
609ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
610ce3ee1e7SLuigi Rizzo 	int i, j, i0;
611ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
612ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
613ce3ee1e7SLuigi Rizzo 
614ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
615ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
616ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
617ce3ee1e7SLuigi Rizzo 	if (!dst)
618ce3ee1e7SLuigi Rizzo 		dst = _dst;
619ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
620ce3ee1e7SLuigi Rizzo 		lim = len;
621ce3ee1e7SLuigi Rizzo 	o = dst;
622ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
623ce3ee1e7SLuigi Rizzo 	o += strlen(o);
624ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
625ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
626ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
627ce3ee1e7SLuigi Rizzo 		o += strlen(o);
628ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
629ce3ee1e7SLuigi Rizzo 		i0 = i;
630ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
631ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
632ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
633ce3ee1e7SLuigi Rizzo 		}
634ce3ee1e7SLuigi Rizzo 		i = i0;
635ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
636ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
637ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
638ce3ee1e7SLuigi Rizzo 		o += j+49;
639ce3ee1e7SLuigi Rizzo 	}
640ce3ee1e7SLuigi Rizzo 	*o = '\0';
641ce3ee1e7SLuigi Rizzo #undef P_HI
642ce3ee1e7SLuigi Rizzo #undef P_LO
643ce3ee1e7SLuigi Rizzo #undef P_C
644ce3ee1e7SLuigi Rizzo 	return dst;
645ce3ee1e7SLuigi Rizzo }
646f196ce38SLuigi Rizzo 
647f18be576SLuigi Rizzo 
648ae10d1afSLuigi Rizzo /*
649ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
650ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
651ae10d1afSLuigi Rizzo  */
65289cc2556SLuigi Rizzo /* call with NMG_LOCK held */
653f9790aebSLuigi Rizzo int
654ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
655ae10d1afSLuigi Rizzo {
656ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
657ae10d1afSLuigi Rizzo 
658ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
659ae10d1afSLuigi Rizzo 	if (na->nm_config) {
660f9790aebSLuigi Rizzo 		na->nm_config(na, &txr, &txd, &rxr, &rxd);
661ae10d1afSLuigi Rizzo 	} else {
662ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
663ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
664ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
665ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
666ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
667ae10d1afSLuigi Rizzo 	}
668ae10d1afSLuigi Rizzo 
669ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
670ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
671ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
672f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
673ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
6744bf50f18SLuigi Rizzo 			na->name,
675ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
676ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
677ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
6784bf50f18SLuigi Rizzo 			na->name, txr, txd, rxr, rxd);
679ae10d1afSLuigi Rizzo 	}
680f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
681ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
682ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
683ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
684ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
685ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
686ae10d1afSLuigi Rizzo 		return 0;
687ae10d1afSLuigi Rizzo 	}
688ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
689ae10d1afSLuigi Rizzo 	return 1;
690ae10d1afSLuigi Rizzo }
691ae10d1afSLuigi Rizzo 
69289cc2556SLuigi Rizzo /* kring->nm_sync callback for the host tx ring */
693f0ea3689SLuigi Rizzo static int
694f0ea3689SLuigi Rizzo netmap_txsync_to_host_compat(struct netmap_kring *kring, int flags)
695f0ea3689SLuigi Rizzo {
69689cc2556SLuigi Rizzo 	(void)flags; /* unused */
697f0ea3689SLuigi Rizzo 	netmap_txsync_to_host(kring->na);
698f0ea3689SLuigi Rizzo 	return 0;
699f0ea3689SLuigi Rizzo }
700f0ea3689SLuigi Rizzo 
70189cc2556SLuigi Rizzo /* kring->nm_sync callback for the host rx ring */
702f0ea3689SLuigi Rizzo static int
703f0ea3689SLuigi Rizzo netmap_rxsync_from_host_compat(struct netmap_kring *kring, int flags)
704f0ea3689SLuigi Rizzo {
70589cc2556SLuigi Rizzo 	(void)flags; /* unused */
706f0ea3689SLuigi Rizzo 	netmap_rxsync_from_host(kring->na, NULL, NULL);
707f0ea3689SLuigi Rizzo 	return 0;
708f0ea3689SLuigi Rizzo }
709f0ea3689SLuigi Rizzo 
710f0ea3689SLuigi Rizzo 
711f0ea3689SLuigi Rizzo 
712f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
713f0ea3689SLuigi Rizzo  * The array layout is this:
714f0ea3689SLuigi Rizzo  *
715f0ea3689SLuigi Rizzo  *                    +----------+
716f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
717f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
718f0ea3689SLuigi Rizzo  *                    |          | /
719f0ea3689SLuigi Rizzo  *                    +----------+
720f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
721f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
722f0ea3689SLuigi Rizzo  *                    |          | \
723f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
724f0ea3689SLuigi Rizzo  *                    |          | /
725f0ea3689SLuigi Rizzo  *                    +----------+
726f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
727f0ea3689SLuigi Rizzo  *                    +----------+
728f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
729f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
730f0ea3689SLuigi Rizzo  *                    |          | /
731f0ea3689SLuigi Rizzo  *                    +----------+
732f0ea3689SLuigi Rizzo  *
733f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
734f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
735f0ea3689SLuigi Rizzo  */
73689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
737f9790aebSLuigi Rizzo int
738f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
739f9790aebSLuigi Rizzo {
740f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
741f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
742f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
743f9790aebSLuigi Rizzo 
744f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
745f0ea3689SLuigi Rizzo 	ntx = na->num_tx_rings + 1;
746f0ea3689SLuigi Rizzo 	nrx = na->num_rx_rings + 1;
747f0ea3689SLuigi Rizzo 
748f9790aebSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom;
749f9790aebSLuigi Rizzo 
750f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
751f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
752f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
753f9790aebSLuigi Rizzo 		return ENOMEM;
754f9790aebSLuigi Rizzo 	}
755f9790aebSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
756f9790aebSLuigi Rizzo 
75717885a7bSLuigi Rizzo 	/*
75817885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
75917885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
76017885a7bSLuigi Rizzo 	 */
761f9790aebSLuigi Rizzo 	ndesc = na->num_tx_desc;
762f9790aebSLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
763f9790aebSLuigi Rizzo 		kring = &na->tx_rings[i];
764f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
765f9790aebSLuigi Rizzo 		kring->na = na;
76617885a7bSLuigi Rizzo 		kring->ring_id = i;
767f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
768f0ea3689SLuigi Rizzo 		if (i < na->num_tx_rings) {
7694bf50f18SLuigi Rizzo 			kring->nm_sync = na->nm_txsync;
770f0ea3689SLuigi Rizzo 		} else if (i == na->num_tx_rings) {
771f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_txsync_to_host_compat;
772f0ea3689SLuigi Rizzo 		}
773f9790aebSLuigi Rizzo 		/*
77417885a7bSLuigi Rizzo 		 * IMPORTANT: Always keep one slot empty.
775f9790aebSLuigi Rizzo 		 */
77617885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
77717885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = ndesc - 1;
7784bf50f18SLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", na->name, i);
779f0ea3689SLuigi Rizzo 		ND("ktx %s h %d c %d t %d",
780f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
781f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF);
782f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
783f9790aebSLuigi Rizzo 	}
784f9790aebSLuigi Rizzo 
785f9790aebSLuigi Rizzo 	ndesc = na->num_rx_desc;
786f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
787f9790aebSLuigi Rizzo 		kring = &na->rx_rings[i];
788f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
789f9790aebSLuigi Rizzo 		kring->na = na;
79017885a7bSLuigi Rizzo 		kring->ring_id = i;
791f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
792f0ea3689SLuigi Rizzo 		if (i < na->num_rx_rings) {
7934bf50f18SLuigi Rizzo 			kring->nm_sync = na->nm_rxsync;
794f0ea3689SLuigi Rizzo 		} else if (i == na->num_rx_rings) {
795f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_rxsync_from_host_compat;
796f0ea3689SLuigi Rizzo 		}
79717885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
79817885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = 0;
7994bf50f18SLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", na->name, i);
800f0ea3689SLuigi Rizzo 		ND("krx %s h %d c %d t %d",
801f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
802f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF);
803f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
804f9790aebSLuigi Rizzo 	}
805f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
806f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
807f9790aebSLuigi Rizzo 
808f9790aebSLuigi Rizzo 	na->tailroom = na->rx_rings + nrx;
809f9790aebSLuigi Rizzo 
810f9790aebSLuigi Rizzo 	return 0;
811f9790aebSLuigi Rizzo }
812f9790aebSLuigi Rizzo 
813f9790aebSLuigi Rizzo 
8140e73f29aSLuigi Rizzo #ifdef __FreeBSD__
8150e73f29aSLuigi Rizzo static void
8160e73f29aSLuigi Rizzo netmap_knlist_destroy(NM_SELINFO_T *si)
8170e73f29aSLuigi Rizzo {
8180e73f29aSLuigi Rizzo 	/* XXX kqueue(9) needed; these will mirror knlist_init. */
8190e73f29aSLuigi Rizzo 	knlist_delete(&si->si.si_note, curthread, 0 /* not locked */ );
8200e73f29aSLuigi Rizzo 	knlist_destroy(&si->si.si_note);
8210e73f29aSLuigi Rizzo 	/* now we don't need the mutex anymore */
8220e73f29aSLuigi Rizzo 	mtx_destroy(&si->m);
8230e73f29aSLuigi Rizzo }
8240e73f29aSLuigi Rizzo #endif /* __FreeBSD__ */
8250e73f29aSLuigi Rizzo 
8260e73f29aSLuigi Rizzo 
827f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
82889cc2556SLuigi Rizzo /* call with NMG_LOCK held */
829f9790aebSLuigi Rizzo void
830f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
831f9790aebSLuigi Rizzo {
832f0ea3689SLuigi Rizzo 	struct netmap_kring *kring = na->tx_rings;
833f9790aebSLuigi Rizzo 
834f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
835f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
836f0ea3689SLuigi Rizzo 		mtx_destroy(&kring->q_lock);
8370e73f29aSLuigi Rizzo 		netmap_knlist_destroy(&kring->si);
838f9790aebSLuigi Rizzo 	}
839f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
840f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
841f9790aebSLuigi Rizzo }
842f9790aebSLuigi Rizzo 
843f9790aebSLuigi Rizzo 
84417885a7bSLuigi Rizzo /*
84517885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
84617885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
84717885a7bSLuigi Rizzo  * them first.
84817885a7bSLuigi Rizzo  */
84989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
85017885a7bSLuigi Rizzo static void
85117885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
85217885a7bSLuigi Rizzo {
85317885a7bSLuigi Rizzo 	struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue;
85417885a7bSLuigi Rizzo 
85517885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
85617885a7bSLuigi Rizzo 	mbq_purge(q);
85717885a7bSLuigi Rizzo 	mbq_safe_destroy(q);
85817885a7bSLuigi Rizzo 	netmap_krings_delete(na);
85917885a7bSLuigi Rizzo }
86017885a7bSLuigi Rizzo 
86117885a7bSLuigi Rizzo 
86289cc2556SLuigi Rizzo /* create a new netmap_if for a newly registered fd.
86389cc2556SLuigi Rizzo  * If this is the first registration of the adapter,
86489cc2556SLuigi Rizzo  * also create the netmap rings and their in-kernel view,
86589cc2556SLuigi Rizzo  * the netmap krings.
86689cc2556SLuigi Rizzo  */
86789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
868ce3ee1e7SLuigi Rizzo static struct netmap_if*
8694bf50f18SLuigi Rizzo netmap_if_new(struct netmap_adapter *na)
870ce3ee1e7SLuigi Rizzo {
871f9790aebSLuigi Rizzo 	struct netmap_if *nifp;
872f9790aebSLuigi Rizzo 
873ce3ee1e7SLuigi Rizzo 	if (netmap_update_config(na)) {
874ce3ee1e7SLuigi Rizzo 		/* configuration mismatch, report and fail */
875ce3ee1e7SLuigi Rizzo 		return NULL;
876ce3ee1e7SLuigi Rizzo 	}
877f9790aebSLuigi Rizzo 
87889cc2556SLuigi Rizzo 	if (na->active_fds)	/* already registered */
879f9790aebSLuigi Rizzo 		goto final;
880f9790aebSLuigi Rizzo 
88189cc2556SLuigi Rizzo 	/* create and init the krings arrays.
88289cc2556SLuigi Rizzo 	 * Depending on the adapter, this may also create
88389cc2556SLuigi Rizzo 	 * the netmap rings themselves
88489cc2556SLuigi Rizzo 	 */
885f9790aebSLuigi Rizzo 	if (na->nm_krings_create(na))
8864bf50f18SLuigi Rizzo 		return NULL;
887f9790aebSLuigi Rizzo 
88889cc2556SLuigi Rizzo 	/* create all missing netmap rings */
889f9790aebSLuigi Rizzo 	if (netmap_mem_rings_create(na))
890f9790aebSLuigi Rizzo 		goto cleanup;
891f9790aebSLuigi Rizzo 
892f9790aebSLuigi Rizzo final:
893f9790aebSLuigi Rizzo 
89489cc2556SLuigi Rizzo 	/* in all cases, create a new netmap if */
8954bf50f18SLuigi Rizzo 	nifp = netmap_mem_if_new(na);
896f9790aebSLuigi Rizzo 	if (nifp == NULL)
897f9790aebSLuigi Rizzo 		goto cleanup;
898f9790aebSLuigi Rizzo 
899f9790aebSLuigi Rizzo 	return (nifp);
900f9790aebSLuigi Rizzo 
901f9790aebSLuigi Rizzo cleanup:
902f9790aebSLuigi Rizzo 
903f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
904f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
905f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
906ce3ee1e7SLuigi Rizzo 	}
90768b8534bSLuigi Rizzo 
908f9790aebSLuigi Rizzo 	return NULL;
909f9790aebSLuigi Rizzo }
9108241616dSLuigi Rizzo 
91168b8534bSLuigi Rizzo 
912ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already.  The
913ce3ee1e7SLuigi Rizzo  * reference is taken from the netmap_adapter registered with the priv.
914ce3ee1e7SLuigi Rizzo  */
91589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
916ce3ee1e7SLuigi Rizzo static int
917ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p)
918ce3ee1e7SLuigi Rizzo {
919ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *nmd;
920ce3ee1e7SLuigi Rizzo 	int error = 0;
921ce3ee1e7SLuigi Rizzo 
922f9790aebSLuigi Rizzo 	if (p->np_na == NULL) {
923ce3ee1e7SLuigi Rizzo 		if (!netmap_mmap_unreg)
924ce3ee1e7SLuigi Rizzo 			return ENODEV;
925ce3ee1e7SLuigi Rizzo 		/* for compatibility with older versions of the API
926ce3ee1e7SLuigi Rizzo  		 * we use the global allocator when no interface has been
927ce3ee1e7SLuigi Rizzo  		 * registered
928ce3ee1e7SLuigi Rizzo  		 */
929ce3ee1e7SLuigi Rizzo 		nmd = &nm_mem;
930ce3ee1e7SLuigi Rizzo 	} else {
931f9790aebSLuigi Rizzo 		nmd = p->np_na->nm_mem;
932ce3ee1e7SLuigi Rizzo 	}
933ce3ee1e7SLuigi Rizzo 	if (p->np_mref == NULL) {
9344bf50f18SLuigi Rizzo 		error = netmap_mem_finalize(nmd, p->np_na);
935ce3ee1e7SLuigi Rizzo 		if (!error)
936ce3ee1e7SLuigi Rizzo 			p->np_mref = nmd;
937ce3ee1e7SLuigi Rizzo 	} else if (p->np_mref != nmd) {
938ce3ee1e7SLuigi Rizzo 		/* a virtual port has been registered, but previous
939ce3ee1e7SLuigi Rizzo  		 * syscalls already used the global allocator.
940ce3ee1e7SLuigi Rizzo  		 * We cannot continue
941ce3ee1e7SLuigi Rizzo  		 */
942ce3ee1e7SLuigi Rizzo 		error = ENODEV;
943ce3ee1e7SLuigi Rizzo 	}
944ce3ee1e7SLuigi Rizzo 	return error;
945ce3ee1e7SLuigi Rizzo }
94668b8534bSLuigi Rizzo 
947f9790aebSLuigi Rizzo 
94889cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
949f9790aebSLuigi Rizzo int
9508241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
9518241616dSLuigi Rizzo {
952ce3ee1e7SLuigi Rizzo 	int error;
953ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
954ce3ee1e7SLuigi Rizzo 	error = netmap_get_memory_locked(p);
955ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
9568241616dSLuigi Rizzo 	return error;
9578241616dSLuigi Rizzo }
9588241616dSLuigi Rizzo 
959f9790aebSLuigi Rizzo 
96089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
961ce3ee1e7SLuigi Rizzo static int
962ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p)
963ce3ee1e7SLuigi Rizzo {
964ce3ee1e7SLuigi Rizzo 	return p->np_mref != NULL;
965ce3ee1e7SLuigi Rizzo }
966ce3ee1e7SLuigi Rizzo 
967f9790aebSLuigi Rizzo 
96889cc2556SLuigi Rizzo /* call with NMG_LOCK held */
969ce3ee1e7SLuigi Rizzo static void
970ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p)
971ce3ee1e7SLuigi Rizzo {
972ce3ee1e7SLuigi Rizzo 	if (p->np_mref) {
9734bf50f18SLuigi Rizzo 		netmap_mem_deref(p->np_mref, p->np_na);
974ce3ee1e7SLuigi Rizzo 		p->np_mref = NULL;
975ce3ee1e7SLuigi Rizzo 	}
976ce3ee1e7SLuigi Rizzo }
977ce3ee1e7SLuigi Rizzo 
978f9790aebSLuigi Rizzo 
97968b8534bSLuigi Rizzo /*
98068b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
9814bf50f18SLuigi Rizzo  * revert to normal operation.
982ce3ee1e7SLuigi Rizzo  * The second argument is the nifp to work on. In some cases it is
983ce3ee1e7SLuigi Rizzo  * not attached yet to the netmap_priv_d so we need to pass it as
984ce3ee1e7SLuigi Rizzo  * a separate argument.
98568b8534bSLuigi Rizzo  */
986ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
98768b8534bSLuigi Rizzo static void
988ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp)
98968b8534bSLuigi Rizzo {
990f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
99168b8534bSLuigi Rizzo 
992ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
993f9790aebSLuigi Rizzo 	na->active_fds--;
994f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
99568b8534bSLuigi Rizzo 
996ae10d1afSLuigi Rizzo 		if (netmap_verbose)
9974bf50f18SLuigi Rizzo 			D("deleting last instance for %s", na->name);
99868b8534bSLuigi Rizzo 		/*
999f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
1000f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
1001f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
1002f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
1003ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
1004ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
1005ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
1006ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
1007ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
1008ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
1009ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
1010ce3ee1e7SLuigi Rizzo 		 * syscall is running.
101168b8534bSLuigi Rizzo 		 */
1012f9790aebSLuigi Rizzo 		na->nm_register(na, 0); /* off, clear flags */
101368b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
101468b8534bSLuigi Rizzo 		 * then return POLLERR
1015ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
1016ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
101768b8534bSLuigi Rizzo 		 */
10180e73f29aSLuigi Rizzo 		netmap_knlist_destroy(&na->tx_si);
10190e73f29aSLuigi Rizzo 		netmap_knlist_destroy(&na->rx_si);
1020f9790aebSLuigi Rizzo 
1021f9790aebSLuigi Rizzo 		/* delete rings and buffers */
1022f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
1023f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
102468b8534bSLuigi Rizzo 	}
1025f9790aebSLuigi Rizzo 	/* delete the nifp */
1026ce3ee1e7SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
10275819da83SLuigi Rizzo }
102868b8534bSLuigi Rizzo 
102989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1030f0ea3689SLuigi Rizzo static __inline int
1031f0ea3689SLuigi Rizzo nm_tx_si_user(struct netmap_priv_d *priv)
1032f0ea3689SLuigi Rizzo {
1033f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
1034f0ea3689SLuigi Rizzo 		(priv->np_txqlast - priv->np_txqfirst > 1));
1035f0ea3689SLuigi Rizzo }
1036f0ea3689SLuigi Rizzo 
103789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1038f0ea3689SLuigi Rizzo static __inline int
1039f0ea3689SLuigi Rizzo nm_rx_si_user(struct netmap_priv_d *priv)
1040f0ea3689SLuigi Rizzo {
1041f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
1042f0ea3689SLuigi Rizzo 		(priv->np_rxqlast - priv->np_rxqfirst > 1));
1043f0ea3689SLuigi Rizzo }
1044f0ea3689SLuigi Rizzo 
1045f18be576SLuigi Rizzo 
1046ce3ee1e7SLuigi Rizzo /*
104789cc2556SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd has
104889cc2556SLuigi Rizzo  * no active open() and mmap(). Also called in error paths.
104989cc2556SLuigi Rizzo  *
1050ce3ee1e7SLuigi Rizzo  * returns 1 if this is the last instance and we can free priv
1051ce3ee1e7SLuigi Rizzo  */
105289cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1053f9790aebSLuigi Rizzo int
1054ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
1055ce3ee1e7SLuigi Rizzo {
1056f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1057ce3ee1e7SLuigi Rizzo 
1058ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
1059ce3ee1e7SLuigi Rizzo 	/*
1060ce3ee1e7SLuigi Rizzo 	 * np_refcount is the number of active mmaps on
1061ce3ee1e7SLuigi Rizzo 	 * this file descriptor
1062ce3ee1e7SLuigi Rizzo 	 */
1063ce3ee1e7SLuigi Rizzo 	if (--priv->np_refcount > 0) {
1064ce3ee1e7SLuigi Rizzo 		return 0;
1065ce3ee1e7SLuigi Rizzo 	}
1066ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
1067f9790aebSLuigi Rizzo 	if (!na) {
1068f9790aebSLuigi Rizzo 	    return 1; //XXX is it correct?
1069ce3ee1e7SLuigi Rizzo 	}
1070f9790aebSLuigi Rizzo 	netmap_do_unregif(priv, priv->np_nifp);
1071f9790aebSLuigi Rizzo 	priv->np_nifp = NULL;
1072ce3ee1e7SLuigi Rizzo 	netmap_drop_memory_locked(priv);
1073f9790aebSLuigi Rizzo 	if (priv->np_na) {
1074f0ea3689SLuigi Rizzo 		if (nm_tx_si_user(priv))
1075f0ea3689SLuigi Rizzo 			na->tx_si_users--;
1076f0ea3689SLuigi Rizzo 		if (nm_rx_si_user(priv))
1077f0ea3689SLuigi Rizzo 			na->rx_si_users--;
1078f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
1079f9790aebSLuigi Rizzo 		priv->np_na = NULL;
1080ce3ee1e7SLuigi Rizzo 	}
1081ce3ee1e7SLuigi Rizzo 	return 1;
1082f196ce38SLuigi Rizzo }
10835819da83SLuigi Rizzo 
1084f9790aebSLuigi Rizzo 
108589cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
1086f9790aebSLuigi Rizzo void
10875819da83SLuigi Rizzo netmap_dtor(void *data)
10885819da83SLuigi Rizzo {
10895819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
1090ce3ee1e7SLuigi Rizzo 	int last_instance;
10915819da83SLuigi Rizzo 
1092ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
1093ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
1094ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1095ce3ee1e7SLuigi Rizzo 	if (last_instance) {
1096ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
109768b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
109868b8534bSLuigi Rizzo 	}
1099ce3ee1e7SLuigi Rizzo }
110068b8534bSLuigi Rizzo 
1101f18be576SLuigi Rizzo 
110268b8534bSLuigi Rizzo 
110368b8534bSLuigi Rizzo 
110468b8534bSLuigi Rizzo /*
110502ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
1106091fd0abSLuigi Rizzo  * Netmap has two operating modes:
1107091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
1108091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
1109091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
1110091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
1111091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
1112091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
1113091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
1114091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
1115091fd0abSLuigi Rizzo  *   to the other side.
1116091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1117091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1118091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1119091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1120091fd0abSLuigi Rizzo  */
1121091fd0abSLuigi Rizzo 
1122f18be576SLuigi Rizzo 
1123091fd0abSLuigi Rizzo /*
1124091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
112517885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1126091fd0abSLuigi Rizzo  */
1127091fd0abSLuigi Rizzo static void
1128f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1129091fd0abSLuigi Rizzo {
1130091fd0abSLuigi Rizzo 	struct mbuf *m;
1131091fd0abSLuigi Rizzo 
1132091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
1133f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1134091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
1135091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
1136091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
1137091fd0abSLuigi Rizzo 	}
1138f9790aebSLuigi Rizzo 	mbq_destroy(q);
1139091fd0abSLuigi Rizzo }
1140091fd0abSLuigi Rizzo 
1141f18be576SLuigi Rizzo 
1142091fd0abSLuigi Rizzo /*
1143091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
114417885a7bSLuigi Rizzo  * Take packets from hwcur to ring->head marked NS_FORWARD (or forced)
114517885a7bSLuigi Rizzo  * and pass them up. Drop remaining packets in the unlikely event
114617885a7bSLuigi Rizzo  * of an mbuf shortage.
1147091fd0abSLuigi Rizzo  */
1148091fd0abSLuigi Rizzo static void
1149091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1150091fd0abSLuigi Rizzo {
115117885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
115217885a7bSLuigi Rizzo 	u_int const head = kring->ring->head;
115317885a7bSLuigi Rizzo 	u_int n;
1154f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1155091fd0abSLuigi Rizzo 
115617885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
115717885a7bSLuigi Rizzo 		struct mbuf *m;
1158091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1159091fd0abSLuigi Rizzo 
1160091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1161091fd0abSLuigi Rizzo 			continue;
11624bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
116317885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
1164091fd0abSLuigi Rizzo 			continue;
1165091fd0abSLuigi Rizzo 		}
1166091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
116717885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
11684bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1169091fd0abSLuigi Rizzo 
1170091fd0abSLuigi Rizzo 		if (m == NULL)
1171091fd0abSLuigi Rizzo 			break;
1172f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1173091fd0abSLuigi Rizzo 	}
1174091fd0abSLuigi Rizzo }
1175091fd0abSLuigi Rizzo 
1176f18be576SLuigi Rizzo 
1177091fd0abSLuigi Rizzo /*
117817885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
117917885a7bSLuigi Rizzo  * kring->nr_hwcur and kring->rhead
118017885a7bSLuigi Rizzo  * Called under kring->rx_queue.lock on the sw rx ring,
1181091fd0abSLuigi Rizzo  */
118217885a7bSLuigi Rizzo static u_int
1183091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1184091fd0abSLuigi Rizzo {
1185091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
118617885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
118717885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
118817885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
118917885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
119017885a7bSLuigi Rizzo 	u_int sent = 0;
1191ce3ee1e7SLuigi Rizzo 
119217885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
119317885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
119417885a7bSLuigi Rizzo 		struct netmap_kring *kdst = &na->tx_rings[i];
119517885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
119617885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1197ce3ee1e7SLuigi Rizzo 
119817885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
119917885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
120017885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1201091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
120217885a7bSLuigi Rizzo 			u_int dst_cur = rdst->cur;
120317885a7bSLuigi Rizzo 
120417885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
120517885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
120617885a7bSLuigi Rizzo 				continue;
120717885a7bSLuigi Rizzo 
120817885a7bSLuigi Rizzo 			sent++;
120917885a7bSLuigi Rizzo 
121017885a7bSLuigi Rizzo 			dst = &rdst->slot[dst_cur];
121117885a7bSLuigi Rizzo 
1212091fd0abSLuigi Rizzo 			tmp = *src;
121317885a7bSLuigi Rizzo 
1214091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1215091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1216091fd0abSLuigi Rizzo 
1217091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1218091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1219091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1220091fd0abSLuigi Rizzo 
12214bf50f18SLuigi Rizzo 			rdst->cur = nm_next(dst_cur, dst_lim);
1222091fd0abSLuigi Rizzo 		}
122317885a7bSLuigi Rizzo 		/* if (sent) XXX txsync ? */
1224091fd0abSLuigi Rizzo 	}
122517885a7bSLuigi Rizzo 	return sent;
1226091fd0abSLuigi Rizzo }
1227091fd0abSLuigi Rizzo 
1228f18be576SLuigi Rizzo 
1229091fd0abSLuigi Rizzo /*
1230ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
123102ad4083SLuigi Rizzo  * system call in user process context, and the only contention
123202ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1233091fd0abSLuigi Rizzo  * this routine concurrently.
123468b8534bSLuigi Rizzo  */
1235f9790aebSLuigi Rizzo void
1236ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
123768b8534bSLuigi Rizzo {
1238d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
123968b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
124017885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1241f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1242f9790aebSLuigi Rizzo 	struct mbq q;
124368b8534bSLuigi Rizzo 
124417885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
124517885a7bSLuigi Rizzo 	 * force head = cur since netmap_grab_packets() stops at head
124668b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
124768b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
124868b8534bSLuigi Rizzo 	 */
1249f9790aebSLuigi Rizzo 	mbq_init(&q);
125017885a7bSLuigi Rizzo 	ring->cur = head;
125117885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
125217885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
125317885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
125417885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
125517885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
125617885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
125717885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
125868b8534bSLuigi Rizzo 
1259f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
1260f18be576SLuigi Rizzo }
1261f18be576SLuigi Rizzo 
1262f18be576SLuigi Rizzo 
126368b8534bSLuigi Rizzo /*
126402ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
126517885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
126617885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
126702ad4083SLuigi Rizzo  *
12684bf50f18SLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
12694bf50f18SLuigi Rizzo  * (we know because td != NULL).
12704bf50f18SLuigi Rizzo  *
12714bf50f18SLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
12724bf50f18SLuigi Rizzo  *     as an additional hidden argument.
127317885a7bSLuigi Rizzo  * returns the number of packets delivered to tx queues in
127417885a7bSLuigi Rizzo  * transparent mode, or a negative value if error
127568b8534bSLuigi Rizzo  */
127617885a7bSLuigi Rizzo int
1277ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
127868b8534bSLuigi Rizzo {
1279d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
128068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
128117885a7bSLuigi Rizzo 	u_int nm_i, n;
128217885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1283f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
128417885a7bSLuigi Rizzo 	int ret = 0;
128517885a7bSLuigi Rizzo 	struct mbq *q = &kring->rx_queue;
128668b8534bSLuigi Rizzo 
128701c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
1288f0ea3689SLuigi Rizzo 	(void)td;
128917885a7bSLuigi Rizzo 
1290997b054cSLuigi Rizzo 	mbq_lock(q);
129117885a7bSLuigi Rizzo 
129217885a7bSLuigi Rizzo 	/* First part: import newly received packets */
129317885a7bSLuigi Rizzo 	n = mbq_len(q);
129417885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
129517885a7bSLuigi Rizzo 		struct mbuf *m;
129617885a7bSLuigi Rizzo 		uint32_t stop_i;
129717885a7bSLuigi Rizzo 
129817885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
129917885a7bSLuigi Rizzo 		stop_i = nm_prev(nm_i, lim);
130017885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
130117885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
130217885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
130317885a7bSLuigi Rizzo 
13044bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
130517885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
130617885a7bSLuigi Rizzo 			if (netmap_verbose)
13074bf50f18SLuigi Rizzo                                 D("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
130817885a7bSLuigi Rizzo 
130917885a7bSLuigi Rizzo 			slot->len = len;
131017885a7bSLuigi Rizzo 			slot->flags = kring->nkr_slot_flags;
131117885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
13124bf50f18SLuigi Rizzo 			m_freem(m);
131364ae02c3SLuigi Rizzo 		}
131417885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
131564ae02c3SLuigi Rizzo 	}
131617885a7bSLuigi Rizzo 
131717885a7bSLuigi Rizzo 	/*
131817885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
131917885a7bSLuigi Rizzo 	 */
132017885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
132117885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
132217885a7bSLuigi Rizzo 		if (netmap_fwd || kring->ring->flags & NR_FORWARD)
132317885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
132417885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
132564ae02c3SLuigi Rizzo 	}
132617885a7bSLuigi Rizzo 
132717885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
132817885a7bSLuigi Rizzo 
13294bf50f18SLuigi Rizzo 	/* access copies of cur,tail in the kring */
13304bf50f18SLuigi Rizzo 	if (kring->rcur == kring->rtail && td) /* no bufs available */
13310e73f29aSLuigi Rizzo 		OS_selrecord(td, &kring->si);
13324bf50f18SLuigi Rizzo 
1333997b054cSLuigi Rizzo 	mbq_unlock(q);
133417885a7bSLuigi Rizzo 	return ret;
133568b8534bSLuigi Rizzo }
133668b8534bSLuigi Rizzo 
133768b8534bSLuigi Rizzo 
1338f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1339f9790aebSLuigi Rizzo  *
1340f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1341f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1342f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1343f9790aebSLuigi Rizzo  *
1344f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1345f9790aebSLuigi Rizzo  * EBUSY.
1346f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1347f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1348f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1349f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1350f9790aebSLuigi Rizzo  *
1351f9790aebSLuigi Rizzo  *					native_support
1352f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1353f9790aebSLuigi Rizzo  * -------------------------------------------------------
1354f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1355f9790aebSLuigi Rizzo  *
1356f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1357f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1358f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1359f9790aebSLuigi Rizzo  *
1360f9790aebSLuigi Rizzo  */
1361f9790aebSLuigi Rizzo 
1362f9790aebSLuigi Rizzo int
1363f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
1364f9790aebSLuigi Rizzo {
1365f9790aebSLuigi Rizzo 	/* generic support */
1366f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1367f9790aebSLuigi Rizzo 	int error = 0;
1368f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1369f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1370f9790aebSLuigi Rizzo 
1371f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1372f9790aebSLuigi Rizzo 
1373f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1374f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1375f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1376f9790aebSLuigi Rizzo 
1377f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
13784bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1379f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1380f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1381f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1382f9790aebSLuigi Rizzo 		 * adapters.
1383f9790aebSLuigi Rizzo 		 */
13844bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
13854bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
13864bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
13874bf50f18SLuigi Rizzo #ifdef WITH_PIPES
13884bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
13894bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
13904bf50f18SLuigi Rizzo 			 */
13914bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
13924bf50f18SLuigi Rizzo #endif
13934bf50f18SLuigi Rizzo 		) {
13944bf50f18SLuigi Rizzo 			*na = prev_na;
1395f9790aebSLuigi Rizzo 			return 0;
1396f9790aebSLuigi Rizzo 		}
1397f9790aebSLuigi Rizzo 	}
1398f9790aebSLuigi Rizzo 
1399f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1400f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1401f9790aebSLuigi Rizzo 	 */
1402f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1403f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1404f9790aebSLuigi Rizzo 
1405f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1406f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1407f9790aebSLuigi Rizzo 	 *
1408f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1409f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1410f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1411f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1412f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1413f9790aebSLuigi Rizzo 	 * must be that
1414f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1415f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1416f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1417f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1418f9790aebSLuigi Rizzo 	 */
1419f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1420f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1421f9790aebSLuigi Rizzo 	if (error)
1422f9790aebSLuigi Rizzo 		return error;
1423f9790aebSLuigi Rizzo 
1424f9790aebSLuigi Rizzo 	*na = NA(ifp);
1425f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1426f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1427f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1428f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1429f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1430f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1431f9790aebSLuigi Rizzo 	}
143217885a7bSLuigi Rizzo 	ND("Created generic NA %p (prev %p)", gna, gna->prev);
1433f9790aebSLuigi Rizzo 
1434f9790aebSLuigi Rizzo 	return 0;
1435f9790aebSLuigi Rizzo }
1436f9790aebSLuigi Rizzo 
1437f9790aebSLuigi Rizzo 
143868b8534bSLuigi Rizzo /*
1439ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1440ce3ee1e7SLuigi Rizzo  *
1441f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
1442f2637526SLuigi Rizzo  * to the interface specified by nmr.
1443ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1444ce3ee1e7SLuigi Rizzo  *
1445f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1446f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1447f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1448f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1449f2637526SLuigi Rizzo  * could not be allocated.
1450f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1451f18be576SLuigi Rizzo  *
1452f2637526SLuigi Rizzo  * No reference is kept on the real interface, which may then
1453f2637526SLuigi Rizzo  * disappear at any time.
145468b8534bSLuigi Rizzo  */
1455f9790aebSLuigi Rizzo int
1456f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
145768b8534bSLuigi Rizzo {
1458f0ea3689SLuigi Rizzo 	struct ifnet *ifp = NULL;
1459f9790aebSLuigi Rizzo 	int error = 0;
1460f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1461f9790aebSLuigi Rizzo 
1462f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1463f196ce38SLuigi Rizzo 
1464ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1465ce3ee1e7SLuigi Rizzo 
14664bf50f18SLuigi Rizzo 	/* we cascade through all possibile types of netmap adapter.
14674bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
14684bf50f18SLuigi Rizzo 	 * with the following combinations:
14694bf50f18SLuigi Rizzo 	 *
14704bf50f18SLuigi Rizzo 	 * error    na
14714bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
14724bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
14734bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
14744bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
14754bf50f18SLuigi Rizzo 	 */
14764bf50f18SLuigi Rizzo 
14774bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
14784bf50f18SLuigi Rizzo 	error = netmap_get_monitor_na(nmr, na, create);
14794bf50f18SLuigi Rizzo 	if (error || *na != NULL)
14804bf50f18SLuigi Rizzo 		return error;
14814bf50f18SLuigi Rizzo 
14824bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
1483f0ea3689SLuigi Rizzo 	error = netmap_get_pipe_na(nmr, na, create);
1484f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1485f9790aebSLuigi Rizzo 		return error;
1486ce3ee1e7SLuigi Rizzo 
14874bf50f18SLuigi Rizzo 	/* try to see if this is a bridge port */
1488f0ea3689SLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1489f0ea3689SLuigi Rizzo 	if (error)
1490f0ea3689SLuigi Rizzo 		return error;
1491f0ea3689SLuigi Rizzo 
1492f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1493f0ea3689SLuigi Rizzo 		goto pipes;
1494f0ea3689SLuigi Rizzo 
149589cc2556SLuigi Rizzo 	/*
149689cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
149789cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
149889cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
149989cc2556SLuigi Rizzo 	 * persistent VALE port.
150089cc2556SLuigi Rizzo 	 */
1501f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1502f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1503ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1504f196ce38SLuigi Rizzo 	}
1505ce3ee1e7SLuigi Rizzo 
1506f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1507f9790aebSLuigi Rizzo 	if (error)
1508f9790aebSLuigi Rizzo 		goto out;
1509f18be576SLuigi Rizzo 
1510f9790aebSLuigi Rizzo 	*na = ret;
1511f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1512f0ea3689SLuigi Rizzo 
1513f0ea3689SLuigi Rizzo pipes:
151489cc2556SLuigi Rizzo 	/*
151589cc2556SLuigi Rizzo 	 * If we are opening a pipe whose parent was not in netmap mode,
151689cc2556SLuigi Rizzo 	 * we have to allocate the pipe array now.
151789cc2556SLuigi Rizzo 	 * XXX get rid of this clumsiness (2014-03-15)
151889cc2556SLuigi Rizzo 	 */
1519f0ea3689SLuigi Rizzo 	error = netmap_pipe_alloc(*na, nmr);
1520f0ea3689SLuigi Rizzo 
1521f9790aebSLuigi Rizzo out:
1522f0ea3689SLuigi Rizzo 	if (error && ret != NULL)
1523f0ea3689SLuigi Rizzo 		netmap_adapter_put(ret);
1524f0ea3689SLuigi Rizzo 
1525f0ea3689SLuigi Rizzo 	if (ifp)
152689cc2556SLuigi Rizzo 		if_rele(ifp); /* allow live unloading of drivers modules */
1527f18be576SLuigi Rizzo 
15285ab0d24dSLuigi Rizzo 	return error;
15295ab0d24dSLuigi Rizzo }
1530ce3ee1e7SLuigi Rizzo 
1531ce3ee1e7SLuigi Rizzo 
1532f9790aebSLuigi Rizzo /*
1533f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1534f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
153517885a7bSLuigi Rizzo  * in case of error.
1536f9790aebSLuigi Rizzo  *
153717885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
153817885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1539f9790aebSLuigi Rizzo  *
154017885a7bSLuigi Rizzo  * We want
154117885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1542f9790aebSLuigi Rizzo  *
154317885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1544f9790aebSLuigi Rizzo  */
1545f9790aebSLuigi Rizzo u_int
154617885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring)
1547f9790aebSLuigi Rizzo {
1548f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
154917885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1550f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1551f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1552ce3ee1e7SLuigi Rizzo 
155317885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
155417885a7bSLuigi Rizzo 		kring->name,
155517885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
155617885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
155717885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
155817885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->rhead >= n ||
155917885a7bSLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n)
1560f9790aebSLuigi Rizzo 		goto error;
1561f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
156217885a7bSLuigi Rizzo 	/*
156317885a7bSLuigi Rizzo 	 * user sanity checks. We only use 'cur',
156417885a7bSLuigi Rizzo 	 * A, B, ... are possible positions for cur:
156517885a7bSLuigi Rizzo 	 *
156617885a7bSLuigi Rizzo 	 *  0    A  cur   B  tail  C  n-1
156717885a7bSLuigi Rizzo 	 *  0    D  tail  E  cur   F  n-1
156817885a7bSLuigi Rizzo 	 *
156917885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
157017885a7bSLuigi Rizzo 	 */
157117885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
157217885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
157317885a7bSLuigi Rizzo 		if (head < kring->rhead || head > kring->rtail)
1574f9790aebSLuigi Rizzo 			goto error;
157517885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
157617885a7bSLuigi Rizzo 		if (cur < head || cur > kring->rtail)
1577f9790aebSLuigi Rizzo 			goto error;
157817885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
157917885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
158017885a7bSLuigi Rizzo 		if (head > kring->rtail && head < kring->rhead)
158117885a7bSLuigi Rizzo 			goto error;
158217885a7bSLuigi Rizzo 
158317885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
158417885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
158517885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
158617885a7bSLuigi Rizzo 			if (cur < head || cur > kring->rtail)
158717885a7bSLuigi Rizzo 				goto error;
158817885a7bSLuigi Rizzo 		} else { /* head >= rhead */
158917885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
159017885a7bSLuigi Rizzo 			if (cur > kring->rtail && cur < head)
159117885a7bSLuigi Rizzo 				goto error;
1592f18be576SLuigi Rizzo 		}
1593f9790aebSLuigi Rizzo 	}
159417885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
159517885a7bSLuigi Rizzo 		RD(5, "tail overwritten was %d need %d",
159617885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
159717885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
159817885a7bSLuigi Rizzo 	}
159917885a7bSLuigi Rizzo 	kring->rhead = head;
160017885a7bSLuigi Rizzo 	kring->rcur = cur;
160117885a7bSLuigi Rizzo 	return head;
1602f9790aebSLuigi Rizzo 
1603f9790aebSLuigi Rizzo error:
160417885a7bSLuigi Rizzo 	RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d",
160517885a7bSLuigi Rizzo 		kring->name,
1606f9790aebSLuigi Rizzo 		kring->nr_hwcur,
160717885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
160817885a7bSLuigi Rizzo 		cur, ring->tail);
1609f9790aebSLuigi Rizzo 	return n;
161068b8534bSLuigi Rizzo }
161168b8534bSLuigi Rizzo 
161268b8534bSLuigi Rizzo 
161368b8534bSLuigi Rizzo /*
1614f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
161517885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1616f9790aebSLuigi Rizzo  *
161717885a7bSLuigi Rizzo  * For a valid configuration,
161817885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1619f9790aebSLuigi Rizzo  *
162017885a7bSLuigi Rizzo  * We only consider head and cur.
162117885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1622f9790aebSLuigi Rizzo  *
1623f9790aebSLuigi Rizzo  */
1624f9790aebSLuigi Rizzo u_int
162517885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring)
1626f9790aebSLuigi Rizzo {
1627f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
162817885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
162917885a7bSLuigi Rizzo 	uint32_t head, cur;
1630f9790aebSLuigi Rizzo 
163117885a7bSLuigi Rizzo 	ND("%s kc %d kt %d h %d c %d t %d",
163217885a7bSLuigi Rizzo 		kring->name,
163317885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
163417885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
163517885a7bSLuigi Rizzo 	/*
163617885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
163717885a7bSLuigi Rizzo 	 * move backwards. However:
163817885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
163917885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
164017885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
164117885a7bSLuigi Rizzo 	 */
164217885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
164317885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1644f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
164517885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwtail >= n)
1646f9790aebSLuigi Rizzo 		goto error;
1647f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1648f9790aebSLuigi Rizzo 	/* user sanity checks */
164917885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
165017885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
165117885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur || head > kring->nr_hwtail)
1652f9790aebSLuigi Rizzo 			goto error;
165317885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
165417885a7bSLuigi Rizzo 		if (cur < head || cur > kring->nr_hwtail)
1655f9790aebSLuigi Rizzo 			goto error;
1656f9790aebSLuigi Rizzo 	} else {
165717885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
165817885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur && head > kring->nr_hwtail)
1659f9790aebSLuigi Rizzo 			goto error;
166017885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
166117885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
166217885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
166317885a7bSLuigi Rizzo 			if (cur < head || cur > kring->nr_hwtail)
1664f9790aebSLuigi Rizzo 				goto error;
166517885a7bSLuigi Rizzo 		} else {
166617885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
166717885a7bSLuigi Rizzo 			if (cur < head && cur > kring->nr_hwtail)
1668f9790aebSLuigi Rizzo 				goto error;
1669f9790aebSLuigi Rizzo 		}
1670f9790aebSLuigi Rizzo 	}
167117885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
167217885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
167317885a7bSLuigi Rizzo 			kring->name,
167417885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
167517885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
167617885a7bSLuigi Rizzo 	}
167717885a7bSLuigi Rizzo 	return head;
1678f9790aebSLuigi Rizzo 
1679f9790aebSLuigi Rizzo error:
168017885a7bSLuigi Rizzo 	RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d",
1681f9790aebSLuigi Rizzo 		kring->nr_hwcur,
168217885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
168317885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, ring->tail);
1684f9790aebSLuigi Rizzo 	return n;
1685f9790aebSLuigi Rizzo }
1686f9790aebSLuigi Rizzo 
168717885a7bSLuigi Rizzo 
1688f9790aebSLuigi Rizzo /*
168968b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
169017885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
169168b8534bSLuigi Rizzo  * Return 1 on reinit.
1692506cc70cSLuigi Rizzo  *
1693506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1694506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
169517885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1696506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1697506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1698506cc70cSLuigi Rizzo  * it under lock.
169968b8534bSLuigi Rizzo  */
170068b8534bSLuigi Rizzo int
170168b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
170268b8534bSLuigi Rizzo {
170368b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
170468b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
170568b8534bSLuigi Rizzo 	int errors = 0;
170668b8534bSLuigi Rizzo 
1707ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
17084bf50f18SLuigi Rizzo 	RD(10, "called for %s", kring->name);
170917885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
171017885a7bSLuigi Rizzo 	kring->rhead = ring->head;
171117885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
171217885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
171317885a7bSLuigi Rizzo 
171468b8534bSLuigi Rizzo 	if (ring->cur > lim)
171568b8534bSLuigi Rizzo 		errors++;
171617885a7bSLuigi Rizzo 	if (ring->head > lim)
171717885a7bSLuigi Rizzo 		errors++;
171817885a7bSLuigi Rizzo 	if (ring->tail > lim)
171917885a7bSLuigi Rizzo 		errors++;
172068b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
172168b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
172268b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
172368b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
172417885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
172568b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
172668b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
17274bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
172868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
172917885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
173068b8534bSLuigi Rizzo 		}
173168b8534bSLuigi Rizzo 	}
173268b8534bSLuigi Rizzo 	if (errors) {
17338241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
173417885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
173517885a7bSLuigi Rizzo 			kring->name,
173668b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
173717885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
173817885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
173917885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
174017885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
174168b8534bSLuigi Rizzo 	}
174268b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
174368b8534bSLuigi Rizzo }
174468b8534bSLuigi Rizzo 
17454bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
17464bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
17474bf50f18SLuigi Rizzo  *
17484bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
17494bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
17504bf50f18SLuigi Rizzo  *
175168b8534bSLuigi Rizzo  */
17524bf50f18SLuigi Rizzo int
17534bf50f18SLuigi Rizzo netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
175468b8534bSLuigi Rizzo {
1755f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1756f0ea3689SLuigi Rizzo 	u_int j, i = ringid & NETMAP_RING_MASK;
1757f0ea3689SLuigi Rizzo 	u_int reg = flags & NR_REG_MASK;
175868b8534bSLuigi Rizzo 
1759f0ea3689SLuigi Rizzo 	if (reg == NR_REG_DEFAULT) {
1760f0ea3689SLuigi Rizzo 		/* convert from old ringid to flags */
176168b8534bSLuigi Rizzo 		if (ringid & NETMAP_SW_RING) {
1762f0ea3689SLuigi Rizzo 			reg = NR_REG_SW;
176368b8534bSLuigi Rizzo 		} else if (ringid & NETMAP_HW_RING) {
1764f0ea3689SLuigi Rizzo 			reg = NR_REG_ONE_NIC;
176568b8534bSLuigi Rizzo 		} else {
1766f0ea3689SLuigi Rizzo 			reg = NR_REG_ALL_NIC;
1767f0ea3689SLuigi Rizzo 		}
1768f0ea3689SLuigi Rizzo 		D("deprecated API, old ringid 0x%x -> ringid %x reg %d", ringid, i, reg);
1769f0ea3689SLuigi Rizzo 	}
1770f0ea3689SLuigi Rizzo 	switch (reg) {
1771f0ea3689SLuigi Rizzo 	case NR_REG_ALL_NIC:
1772f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_MASTER:
1773f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_SLAVE:
1774f0ea3689SLuigi Rizzo 		priv->np_txqfirst = 0;
1775f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings;
1776f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = 0;
1777f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings;
1778f0ea3689SLuigi Rizzo 		ND("%s %d %d", "ALL/PIPE",
1779f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1780f0ea3689SLuigi Rizzo 		break;
1781f0ea3689SLuigi Rizzo 	case NR_REG_SW:
1782f0ea3689SLuigi Rizzo 	case NR_REG_NIC_SW:
1783f0ea3689SLuigi Rizzo 		if (!(na->na_flags & NAF_HOST_RINGS)) {
1784f0ea3689SLuigi Rizzo 			D("host rings not supported");
1785f0ea3689SLuigi Rizzo 			return EINVAL;
1786f0ea3689SLuigi Rizzo 		}
1787f0ea3689SLuigi Rizzo 		priv->np_txqfirst = (reg == NR_REG_SW ?
1788f0ea3689SLuigi Rizzo 			na->num_tx_rings : 0);
1789f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings + 1;
1790f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = (reg == NR_REG_SW ?
1791f0ea3689SLuigi Rizzo 			na->num_rx_rings : 0);
1792f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings + 1;
1793f0ea3689SLuigi Rizzo 		ND("%s %d %d", reg == NR_REG_SW ? "SW" : "NIC+SW",
1794f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1795f0ea3689SLuigi Rizzo 		break;
1796f0ea3689SLuigi Rizzo 	case NR_REG_ONE_NIC:
1797f0ea3689SLuigi Rizzo 		if (i >= na->num_tx_rings && i >= na->num_rx_rings) {
1798f0ea3689SLuigi Rizzo 			D("invalid ring id %d", i);
1799f0ea3689SLuigi Rizzo 			return EINVAL;
1800f0ea3689SLuigi Rizzo 		}
1801f0ea3689SLuigi Rizzo 		/* if not enough rings, use the first one */
1802f0ea3689SLuigi Rizzo 		j = i;
1803f0ea3689SLuigi Rizzo 		if (j >= na->num_tx_rings)
1804f0ea3689SLuigi Rizzo 			j = 0;
1805f0ea3689SLuigi Rizzo 		priv->np_txqfirst = j;
1806f0ea3689SLuigi Rizzo 		priv->np_txqlast = j + 1;
1807f0ea3689SLuigi Rizzo 		j = i;
1808f0ea3689SLuigi Rizzo 		if (j >= na->num_rx_rings)
1809f0ea3689SLuigi Rizzo 			j = 0;
1810f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = j;
1811f0ea3689SLuigi Rizzo 		priv->np_rxqlast = j + 1;
1812f0ea3689SLuigi Rizzo 		break;
1813f0ea3689SLuigi Rizzo 	default:
1814f0ea3689SLuigi Rizzo 		D("invalid regif type %d", reg);
1815f0ea3689SLuigi Rizzo 		return EINVAL;
181668b8534bSLuigi Rizzo 	}
1817f0ea3689SLuigi Rizzo 	priv->np_flags = (flags & ~NR_REG_MASK) | reg;
18184bf50f18SLuigi Rizzo 
1819ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1820f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
18214bf50f18SLuigi Rizzo 			na->name,
1822f0ea3689SLuigi Rizzo 			priv->np_txqfirst,
1823f0ea3689SLuigi Rizzo 			priv->np_txqlast,
1824f0ea3689SLuigi Rizzo 			priv->np_rxqfirst,
1825f0ea3689SLuigi Rizzo 			priv->np_rxqlast,
1826f0ea3689SLuigi Rizzo 			i);
1827ae10d1afSLuigi Rizzo 	}
182868b8534bSLuigi Rizzo 	return 0;
182968b8534bSLuigi Rizzo }
183068b8534bSLuigi Rizzo 
18314bf50f18SLuigi Rizzo 
18324bf50f18SLuigi Rizzo /*
18334bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
18344bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
18354bf50f18SLuigi Rizzo  */
18364bf50f18SLuigi Rizzo static int
18374bf50f18SLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
18384bf50f18SLuigi Rizzo {
18394bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
18404bf50f18SLuigi Rizzo 	int error;
18414bf50f18SLuigi Rizzo 
18424bf50f18SLuigi Rizzo 	error = netmap_interp_ringid(priv, ringid, flags);
18434bf50f18SLuigi Rizzo 	if (error) {
18444bf50f18SLuigi Rizzo 		return error;
18454bf50f18SLuigi Rizzo 	}
18464bf50f18SLuigi Rizzo 
18474bf50f18SLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
18484bf50f18SLuigi Rizzo 
18494bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
18504bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
18514bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
18524bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
18534bf50f18SLuigi Rizzo 	 */
18544bf50f18SLuigi Rizzo 	if (nm_tx_si_user(priv))
18554bf50f18SLuigi Rizzo 		na->tx_si_users++;
18564bf50f18SLuigi Rizzo 	if (nm_rx_si_user(priv))
18574bf50f18SLuigi Rizzo 		na->rx_si_users++;
18584bf50f18SLuigi Rizzo 	return 0;
18594bf50f18SLuigi Rizzo }
18604bf50f18SLuigi Rizzo 
1861f18be576SLuigi Rizzo /*
1862f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1863f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1864ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
18654bf50f18SLuigi Rizzo  *
18664bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
18674bf50f18SLuigi Rizzo  *
18684bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
18694bf50f18SLuigi Rizzo  * (get current number and size of rings)
18704bf50f18SLuigi Rizzo  *
18714bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
18724bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
18734bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
18744bf50f18SLuigi Rizzo  *
18754bf50f18SLuigi Rizzo  *    	XXX netmap_if_new calls this again (2014-03-15)
18764bf50f18SLuigi Rizzo  *
18774bf50f18SLuigi Rizzo  * na->nm_krings_create()		[by netmap_if_new]
18784bf50f18SLuigi Rizzo  * (create and init the krings array)
18794bf50f18SLuigi Rizzo  *
18804bf50f18SLuigi Rizzo  * 	One of the following:
18814bf50f18SLuigi Rizzo  *
18824bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
18834bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
18844bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
18854bf50f18SLuigi Rizzo  *
18864bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
18874bf50f18SLuigi Rizzo  * 		add leases and scratchpads
18884bf50f18SLuigi Rizzo  *
18894bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
18904bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
18914bf50f18SLuigi Rizzo  * 		cross-link them
18924bf50f18SLuigi Rizzo  *
18934bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
18944bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
18954bf50f18SLuigi Rizzo  *
18964bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
18974bf50f18SLuigi Rizzo  *      	create both the brap krings array,
18984bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
18994bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
19004bf50f18SLuigi Rizzo  *
19014bf50f18SLuigi Rizzo  * na->nm_register(, 1)
19024bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
19034bf50f18SLuigi Rizzo  *
19044bf50f18SLuigi Rizzo  * 	This may be one of the following:
19054bf50f18SLuigi Rizzo  * 	(XXX these should be either all *_register or all *_reg 2014-03-15)
19064bf50f18SLuigi Rizzo  *
19074bf50f18SLuigi Rizzo  * 	* netmap_hw_register				(hw ports)
19084bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
19094bf50f18SLuigi Rizzo  * 		the hardware specific callback;
19104bf50f18SLuigi Rizzo  *
19114bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
19124bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
19134bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
19144bf50f18SLuigi Rizzo  *		bridge write lock.
19154bf50f18SLuigi Rizzo  *
19164bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
19174bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
19184bf50f18SLuigi Rizzo  *		longer responsibile for the lifetime of this
19194bf50f18SLuigi Rizzo  *		pipe end
19204bf50f18SLuigi Rizzo  *
19214bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
19224bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
19234bf50f18SLuigi Rizzo  *		rings
19244bf50f18SLuigi Rizzo  *
19254bf50f18SLuigi Rizzo  *	* netmap_bwrap_register				(bwraps)
19264bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
19274bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
19284bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
19294bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
19304bf50f18SLuigi Rizzo  *
19314bf50f18SLuigi Rizzo  * XXX maybe netmap_if_new() should be merged with this (2014-03-15).
19324bf50f18SLuigi Rizzo  *
1933f18be576SLuigi Rizzo  */
1934f9790aebSLuigi Rizzo struct netmap_if *
1935f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1936f0ea3689SLuigi Rizzo 	uint16_t ringid, uint32_t flags, int *err)
1937f18be576SLuigi Rizzo {
1938f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1939f9790aebSLuigi Rizzo 	int error, need_mem = 0;
1940f18be576SLuigi Rizzo 
1941ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1942f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1943f18be576SLuigi Rizzo 	netmap_update_config(na);
1944f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1945f0ea3689SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid, flags);
1946f18be576SLuigi Rizzo 	if (error)
1947f18be576SLuigi Rizzo 		goto out;
1948ce3ee1e7SLuigi Rizzo 	/* ensure allocators are ready */
1949ce3ee1e7SLuigi Rizzo 	need_mem = !netmap_have_memory_locked(priv);
1950ce3ee1e7SLuigi Rizzo 	if (need_mem) {
1951ce3ee1e7SLuigi Rizzo 		error = netmap_get_memory_locked(priv);
1952ce3ee1e7SLuigi Rizzo 		ND("get_memory returned %d", error);
1953ce3ee1e7SLuigi Rizzo 		if (error)
1954ce3ee1e7SLuigi Rizzo 			goto out;
1955ce3ee1e7SLuigi Rizzo 	}
195689cc2556SLuigi Rizzo 	/* Allocate a netmap_if and, if necessary, all the netmap_ring's */
19574bf50f18SLuigi Rizzo 	nifp = netmap_if_new(na);
1958f18be576SLuigi Rizzo 	if (nifp == NULL) { /* allocation failed */
1959f18be576SLuigi Rizzo 		error = ENOMEM;
1960ce3ee1e7SLuigi Rizzo 		goto out;
1961ce3ee1e7SLuigi Rizzo 	}
1962f9790aebSLuigi Rizzo 	na->active_fds++;
19634bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
19644bf50f18SLuigi Rizzo 		/* Netmap not active, set the card in netmap mode
1965f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1966ce3ee1e7SLuigi Rizzo 		 */
196789cc2556SLuigi Rizzo 		/* cache the allocator info in the na */
19684bf50f18SLuigi Rizzo 		na->na_lut = netmap_mem_get_lut(na->nm_mem);
1969f9790aebSLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut);
19704bf50f18SLuigi Rizzo 		na->na_lut_objtotal = netmap_mem_get_buftotal(na->nm_mem);
19714bf50f18SLuigi Rizzo 		na->na_lut_objsize = netmap_mem_get_bufsize(na->nm_mem);
1972f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1973f18be576SLuigi Rizzo 		if (error) {
1974ce3ee1e7SLuigi Rizzo 			netmap_do_unregif(priv, nifp);
1975f18be576SLuigi Rizzo 			nifp = NULL;
1976f18be576SLuigi Rizzo 		}
1977f18be576SLuigi Rizzo 	}
1978f18be576SLuigi Rizzo out:
1979f18be576SLuigi Rizzo 	*err = error;
1980f9790aebSLuigi Rizzo 	if (error) {
198189cc2556SLuigi Rizzo 		/* we should drop the allocator, but only
198289cc2556SLuigi Rizzo 		 * if we were the ones who grabbed it
198389cc2556SLuigi Rizzo 		 */
1984f9790aebSLuigi Rizzo 		if (need_mem)
1985f9790aebSLuigi Rizzo 			netmap_drop_memory_locked(priv);
19864bf50f18SLuigi Rizzo 		priv->np_na = NULL;
1987f9790aebSLuigi Rizzo 	}
1988ce3ee1e7SLuigi Rizzo 	if (nifp != NULL) {
1989ce3ee1e7SLuigi Rizzo 		/*
1990ce3ee1e7SLuigi Rizzo 		 * advertise that the interface is ready bt setting ni_nifp.
1991ce3ee1e7SLuigi Rizzo 		 * The barrier is needed because readers (poll and *SYNC)
1992ce3ee1e7SLuigi Rizzo 		 * check for priv->np_nifp != NULL without locking
1993ce3ee1e7SLuigi Rizzo 		 */
1994ce3ee1e7SLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1995ce3ee1e7SLuigi Rizzo 		priv->np_nifp = nifp;
1996ce3ee1e7SLuigi Rizzo 	}
1997f18be576SLuigi Rizzo 	return nifp;
1998f18be576SLuigi Rizzo }
1999f18be576SLuigi Rizzo 
2000f18be576SLuigi Rizzo 
2001f18be576SLuigi Rizzo 
200268b8534bSLuigi Rizzo /*
200368b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
200468b8534bSLuigi Rizzo  *
200568b8534bSLuigi Rizzo  * Following a list of accepted commands:
200668b8534bSLuigi Rizzo  * - NIOCGINFO
200768b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
200868b8534bSLuigi Rizzo  * - NIOCREGIF
200968b8534bSLuigi Rizzo  * - NIOCTXSYNC
201068b8534bSLuigi Rizzo  * - NIOCRXSYNC
201168b8534bSLuigi Rizzo  *
201268b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
201368b8534bSLuigi Rizzo  */
2014f9790aebSLuigi Rizzo int
20150b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
20160b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
201768b8534bSLuigi Rizzo {
201868b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
201968b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
2020ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
202168b8534bSLuigi Rizzo 	int error;
2022f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
202368b8534bSLuigi Rizzo 	struct netmap_if *nifp;
2024ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
202568b8534bSLuigi Rizzo 
20260b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
20270b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
2028f196ce38SLuigi Rizzo 
202917885a7bSLuigi Rizzo 	if (cmd == NIOCGINFO || cmd == NIOCREGIF) {
203017885a7bSLuigi Rizzo 		/* truncate name */
203117885a7bSLuigi Rizzo 		nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';
203217885a7bSLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
203317885a7bSLuigi Rizzo 			D("API mismatch for %s got %d need %d",
203417885a7bSLuigi Rizzo 				nmr->nr_name,
203517885a7bSLuigi Rizzo 				nmr->nr_version, NETMAP_API);
203617885a7bSLuigi Rizzo 			nmr->nr_version = NETMAP_API;
2037f0ea3689SLuigi Rizzo 		}
2038f0ea3689SLuigi Rizzo 		if (nmr->nr_version < NETMAP_MIN_API ||
2039f0ea3689SLuigi Rizzo 		    nmr->nr_version > NETMAP_MAX_API) {
204017885a7bSLuigi Rizzo 			return EINVAL;
204117885a7bSLuigi Rizzo 		}
204217885a7bSLuigi Rizzo 	}
2043506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
2044506cc70cSLuigi Rizzo 
204568b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
20468241616dSLuigi Rizzo 	if (error) {
2047506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
20488241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
20498241616dSLuigi Rizzo 		 * is now created in the open */
20508241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
2051506cc70cSLuigi Rizzo 	}
205268b8534bSLuigi Rizzo 
205368b8534bSLuigi Rizzo 	switch (cmd) {
205468b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
2055f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
2056f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
2057f18be576SLuigi Rizzo 			break;
2058f18be576SLuigi Rizzo 		}
2059ce3ee1e7SLuigi Rizzo 
2060ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2061ce3ee1e7SLuigi Rizzo 		do {
2062ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
2063ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
2064ce3ee1e7SLuigi Rizzo 			u_int memflags;
2065ce3ee1e7SLuigi Rizzo 
2066ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
2067ce3ee1e7SLuigi Rizzo 				/* get a refcount */
2068f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
20698241616dSLuigi Rizzo 				if (error)
20708241616dSLuigi Rizzo 					break;
2071f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
2072ce3ee1e7SLuigi Rizzo 			}
2073ce3ee1e7SLuigi Rizzo 
2074f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags,
2075f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
2076ce3ee1e7SLuigi Rizzo 			if (error)
2077ce3ee1e7SLuigi Rizzo 				break;
2078ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
2079ce3ee1e7SLuigi Rizzo 				break;
20808241616dSLuigi Rizzo 			nmr->nr_offset = 0;
20818241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
2082ae10d1afSLuigi Rizzo 			netmap_update_config(na);
2083d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
2084d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
208564ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
208664ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
2087f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
2088ce3ee1e7SLuigi Rizzo 		} while (0);
2089ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
209068b8534bSLuigi Rizzo 		break;
209168b8534bSLuigi Rizzo 
209268b8534bSLuigi Rizzo 	case NIOCREGIF:
2093f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
2094f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
2095f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
20964bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_VNET_HDR
20974bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_NEWIF
20984bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_DELIF) {
2099f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
2100f18be576SLuigi Rizzo 			break;
2101f18be576SLuigi Rizzo 		} else if (i != 0) {
2102f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
2103f18be576SLuigi Rizzo 			error = EINVAL;
2104f18be576SLuigi Rizzo 			break;
2105f18be576SLuigi Rizzo 		}
2106f18be576SLuigi Rizzo 
21078241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
2108ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2109ce3ee1e7SLuigi Rizzo 		do {
2110ce3ee1e7SLuigi Rizzo 			u_int memflags;
2111ce3ee1e7SLuigi Rizzo 
2112f9790aebSLuigi Rizzo 			if (priv->np_na != NULL) {	/* thread already registered */
2113f0ea3689SLuigi Rizzo 				error = EBUSY;
2114506cc70cSLuigi Rizzo 				break;
2115506cc70cSLuigi Rizzo 			}
211668b8534bSLuigi Rizzo 			/* find the interface and a reference */
2117f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
211868b8534bSLuigi Rizzo 			if (error)
2119ce3ee1e7SLuigi Rizzo 				break;
2120f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
2121f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2122ce3ee1e7SLuigi Rizzo 				error = EBUSY;
2123ce3ee1e7SLuigi Rizzo 				break;
2124f196ce38SLuigi Rizzo 			}
2125f0ea3689SLuigi Rizzo 			nifp = netmap_do_regif(priv, na, nmr->nr_ringid, nmr->nr_flags, &error);
2126f18be576SLuigi Rizzo 			if (!nifp) {    /* reg. failed, release priv and ref */
2127f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
21288241616dSLuigi Rizzo 				priv->np_nifp = NULL;
2129ce3ee1e7SLuigi Rizzo 				break;
213068b8534bSLuigi Rizzo 			}
2131f0ea3689SLuigi Rizzo 			priv->np_td = td; // XXX kqueue, debugging only
213268b8534bSLuigi Rizzo 
213368b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
2134d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
2135d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
213664ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
213764ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
2138f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags,
2139f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
2140ce3ee1e7SLuigi Rizzo 			if (error) {
2141f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2142ce3ee1e7SLuigi Rizzo 				break;
2143ce3ee1e7SLuigi Rizzo 			}
2144ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
21453d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2146ce3ee1e7SLuigi Rizzo 			}
2147f0ea3689SLuigi Rizzo 			priv->np_txsi = (priv->np_txqlast - priv->np_txqfirst > 1) ?
2148f0ea3689SLuigi Rizzo 				&na->tx_si : &na->tx_rings[priv->np_txqfirst].si;
2149f0ea3689SLuigi Rizzo 			priv->np_rxsi = (priv->np_rxqlast - priv->np_rxqfirst > 1) ?
2150f0ea3689SLuigi Rizzo 				&na->rx_si : &na->rx_rings[priv->np_rxqfirst].si;
2151f0ea3689SLuigi Rizzo 
2152f0ea3689SLuigi Rizzo 			if (nmr->nr_arg3) {
2153f0ea3689SLuigi Rizzo 				D("requested %d extra buffers", nmr->nr_arg3);
2154f0ea3689SLuigi Rizzo 				nmr->nr_arg3 = netmap_extra_alloc(na,
2155f0ea3689SLuigi Rizzo 					&nifp->ni_bufs_head, nmr->nr_arg3);
2156f0ea3689SLuigi Rizzo 				D("got %d extra buffers", nmr->nr_arg3);
2157f0ea3689SLuigi Rizzo 			}
2158ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
2159ce3ee1e7SLuigi Rizzo 		} while (0);
2160ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
216168b8534bSLuigi Rizzo 		break;
216268b8534bSLuigi Rizzo 
216368b8534bSLuigi Rizzo 	case NIOCTXSYNC:
216468b8534bSLuigi Rizzo 	case NIOCRXSYNC:
21658241616dSLuigi Rizzo 		nifp = priv->np_nifp;
21668241616dSLuigi Rizzo 
21678241616dSLuigi Rizzo 		if (nifp == NULL) {
2168506cc70cSLuigi Rizzo 			error = ENXIO;
2169506cc70cSLuigi Rizzo 			break;
2170506cc70cSLuigi Rizzo 		}
21718241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
21728241616dSLuigi Rizzo 
2173f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
21748241616dSLuigi Rizzo 
2175f9790aebSLuigi Rizzo 		if (na == NULL) {
2176f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
21778241616dSLuigi Rizzo 			error = ENXIO;
21788241616dSLuigi Rizzo 			break;
21798241616dSLuigi Rizzo 		}
21808241616dSLuigi Rizzo 
21814bf50f18SLuigi Rizzo 		if (!nm_netmap_on(na)) {
2182f9790aebSLuigi Rizzo 			error = ENXIO;
2183f9790aebSLuigi Rizzo 			break;
2184f9790aebSLuigi Rizzo 		}
2185f9790aebSLuigi Rizzo 
2186f0ea3689SLuigi Rizzo 		if (cmd == NIOCTXSYNC) {
2187f0ea3689SLuigi Rizzo 			krings = na->tx_rings;
2188f0ea3689SLuigi Rizzo 			qfirst = priv->np_txqfirst;
2189f0ea3689SLuigi Rizzo 			qlast = priv->np_txqlast;
2190f0ea3689SLuigi Rizzo 		} else {
2191f0ea3689SLuigi Rizzo 			krings = na->rx_rings;
2192f0ea3689SLuigi Rizzo 			qfirst = priv->np_rxqfirst;
2193f0ea3689SLuigi Rizzo 			qlast = priv->np_rxqlast;
219468b8534bSLuigi Rizzo 		}
219568b8534bSLuigi Rizzo 
2196f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
2197ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
2198ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
2199ce3ee1e7SLuigi Rizzo 				error = EBUSY;
2200ce3ee1e7SLuigi Rizzo 				goto out;
2201ce3ee1e7SLuigi Rizzo 			}
220268b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
220368b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
22043c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
220568b8534bSLuigi Rizzo 					    i, kring->ring->cur,
220668b8534bSLuigi Rizzo 					    kring->nr_hwcur);
220717885a7bSLuigi Rizzo 				if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
220817885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
220917885a7bSLuigi Rizzo 				} else {
2210f0ea3689SLuigi Rizzo 					kring->nm_sync(kring, NAF_FORCE_RECLAIM);
221117885a7bSLuigi Rizzo 				}
221268b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
22133c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
221468b8534bSLuigi Rizzo 					    i, kring->ring->cur,
221568b8534bSLuigi Rizzo 					    kring->nr_hwcur);
221668b8534bSLuigi Rizzo 			} else {
2217f0ea3689SLuigi Rizzo 				kring->nm_sync(kring, NAF_FORCE_READ);
221868b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
221968b8534bSLuigi Rizzo 			}
2220ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
222168b8534bSLuigi Rizzo 		}
222268b8534bSLuigi Rizzo 
222368b8534bSLuigi Rizzo 		break;
222468b8534bSLuigi Rizzo 
22254bf50f18SLuigi Rizzo 	case NIOCCONFIG:
22264bf50f18SLuigi Rizzo 		error = netmap_bdg_config(nmr);
22274bf50f18SLuigi Rizzo 		break;
2228f196ce38SLuigi Rizzo #ifdef __FreeBSD__
222989e3fd52SLuigi Rizzo 	case FIONBIO:
223089e3fd52SLuigi Rizzo 	case FIOASYNC:
223189e3fd52SLuigi Rizzo 		ND("FIONBIO/FIOASYNC are no-ops");
223289e3fd52SLuigi Rizzo 		break;
223389e3fd52SLuigi Rizzo 
223468b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
223568b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
223668b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
223768b8534bSLuigi Rizzo 	case BIOCSSEESENT:
223868b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
223968b8534bSLuigi Rizzo 		break;
224068b8534bSLuigi Rizzo 
2241babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
224268b8534bSLuigi Rizzo 	    {
2243b3d37588SLuigi Rizzo 		struct ifnet *ifp = ifunit_ref(nmr->nr_name);
2244b3d37588SLuigi Rizzo 		if (ifp == NULL) {
2245b3d37588SLuigi Rizzo 			error = ENXIO;
2246b3d37588SLuigi Rizzo 		} else {
224768b8534bSLuigi Rizzo 			struct socket so;
2248ce3ee1e7SLuigi Rizzo 
224968b8534bSLuigi Rizzo 			bzero(&so, sizeof(so));
225068b8534bSLuigi Rizzo 			so.so_vnet = ifp->if_vnet;
225168b8534bSLuigi Rizzo 			// so->so_proto not null.
225268b8534bSLuigi Rizzo 			error = ifioctl(&so, cmd, data, td);
2253b3d37588SLuigi Rizzo 			if_rele(ifp);
2254b3d37588SLuigi Rizzo 		}
2255babc7c12SLuigi Rizzo 		break;
225668b8534bSLuigi Rizzo 	    }
2257f196ce38SLuigi Rizzo 
2258f196ce38SLuigi Rizzo #else /* linux */
2259f196ce38SLuigi Rizzo 	default:
2260f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
2261f196ce38SLuigi Rizzo #endif /* linux */
226268b8534bSLuigi Rizzo 	}
2263ce3ee1e7SLuigi Rizzo out:
226468b8534bSLuigi Rizzo 
2265506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
226668b8534bSLuigi Rizzo 	return (error);
226768b8534bSLuigi Rizzo }
226868b8534bSLuigi Rizzo 
226968b8534bSLuigi Rizzo 
227068b8534bSLuigi Rizzo /*
227168b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
227268b8534bSLuigi Rizzo  *
227368b8534bSLuigi Rizzo  * Can be called for one or more queues.
227468b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
227568b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
2276ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
227768b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
227868b8534bSLuigi Rizzo  * are done through callbacks.
2279f196ce38SLuigi Rizzo  *
228001c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
228101c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
228201c7d25fSLuigi Rizzo  * hidden argument.
228368b8534bSLuigi Rizzo  */
2284f9790aebSLuigi Rizzo int
228501c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
228668b8534bSLuigi Rizzo {
228768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
228868b8534bSLuigi Rizzo 	struct netmap_adapter *na;
228968b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2290954dca4cSLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0;
229117885a7bSLuigi Rizzo 	struct mbq q;		/* packets from hw queues to host stack */
229201c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
2293f0ea3689SLuigi Rizzo 	int is_kevent = 0;
229401c7d25fSLuigi Rizzo 
2295f9790aebSLuigi Rizzo 	/*
2296f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
2297f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
2298f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
2299f9790aebSLuigi Rizzo 	 */
230017885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
2301ce3ee1e7SLuigi Rizzo 
230201c7d25fSLuigi Rizzo 	(void)pwait;
2303f9790aebSLuigi Rizzo 	mbq_init(&q);
230468b8534bSLuigi Rizzo 
2305f0ea3689SLuigi Rizzo 	/*
2306f0ea3689SLuigi Rizzo 	 * XXX kevent has curthread->tp_fop == NULL,
2307f0ea3689SLuigi Rizzo 	 * so devfs_get_cdevpriv() fails. We circumvent this by passing
2308f0ea3689SLuigi Rizzo 	 * priv as the first argument, which is also useful to avoid
2309f0ea3689SLuigi Rizzo 	 * the selrecord() which are not necessary in that case.
2310f0ea3689SLuigi Rizzo 	 */
2311f0ea3689SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0) {
2312f0ea3689SLuigi Rizzo 		is_kevent = 1;
2313f0ea3689SLuigi Rizzo 		if (netmap_verbose)
2314f0ea3689SLuigi Rizzo 			D("called from kevent");
2315f0ea3689SLuigi Rizzo 		priv = (struct netmap_priv_d *)dev;
2316f0ea3689SLuigi Rizzo 	}
2317f0ea3689SLuigi Rizzo 	if (priv == NULL)
231868b8534bSLuigi Rizzo 		return POLLERR;
231968b8534bSLuigi Rizzo 
23208241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
23218241616dSLuigi Rizzo 		D("No if registered");
23228241616dSLuigi Rizzo 		return POLLERR;
23238241616dSLuigi Rizzo 	}
23248241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
23258241616dSLuigi Rizzo 
2326f9790aebSLuigi Rizzo 	na = priv->np_na;
2327f9790aebSLuigi Rizzo 
23284bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
232968b8534bSLuigi Rizzo 		return POLLERR;
233068b8534bSLuigi Rizzo 
233168b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
23324bf50f18SLuigi Rizzo 		D("device %s events 0x%x", na->name, events);
233368b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
233468b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
233568b8534bSLuigi Rizzo 
2336091fd0abSLuigi Rizzo 
233768b8534bSLuigi Rizzo 	/*
2338f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
2339f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
2340ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
2341ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
2342ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
2343ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
234468b8534bSLuigi Rizzo 	 *
234568b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
234668b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
234768b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
234868b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
234968b8534bSLuigi Rizzo 	 */
2350f0ea3689SLuigi Rizzo 	check_all_tx = nm_tx_si_user(priv);
2351f0ea3689SLuigi Rizzo 	check_all_rx = nm_rx_si_user(priv);
235264ae02c3SLuigi Rizzo 
235368b8534bSLuigi Rizzo 	/*
2354f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
2355f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
235668b8534bSLuigi Rizzo 	 * routines.
235768b8534bSLuigi Rizzo 	 */
2358f0ea3689SLuigi Rizzo 	for (i = priv->np_rxqfirst; want_rx && i < priv->np_rxqlast; i++) {
235968b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
236017885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
236117885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
236268b8534bSLuigi Rizzo 			revents |= want_rx;
236368b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
236468b8534bSLuigi Rizzo 		}
236568b8534bSLuigi Rizzo 	}
2366f0ea3689SLuigi Rizzo 	for (i = priv->np_txqfirst; want_tx && i < priv->np_txqlast; i++) {
236768b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
236817885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
236917885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
237068b8534bSLuigi Rizzo 			revents |= want_tx;
237168b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
237268b8534bSLuigi Rizzo 		}
237368b8534bSLuigi Rizzo 	}
237468b8534bSLuigi Rizzo 
237568b8534bSLuigi Rizzo 	/*
237617885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
237717885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
237817885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
2379f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
2380f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
238168b8534bSLuigi Rizzo 	 */
238268b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
238317885a7bSLuigi Rizzo 		/*
238417885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
238517885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
238617885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
238717885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
2388ce3ee1e7SLuigi Rizzo 		 */
2389091fd0abSLuigi Rizzo flush_tx:
2390f0ea3689SLuigi Rizzo 		for (i = priv->np_txqfirst; i < priv->np_txqlast; i++) {
239117885a7bSLuigi Rizzo 			int found = 0;
239217885a7bSLuigi Rizzo 
239368b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
239468b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
239568b8534bSLuigi Rizzo 				continue;
239617885a7bSLuigi Rizzo 			/* only one thread does txsync */
2397ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
239889cc2556SLuigi Rizzo 				/* either busy or stopped
239989cc2556SLuigi Rizzo 				 * XXX if the ring is stopped, sleeping would
240089cc2556SLuigi Rizzo 				 * be better. In current code, however, we only
240189cc2556SLuigi Rizzo 				 * stop the rings for brief intervals (2014-03-14)
240289cc2556SLuigi Rizzo 				 */
240389e3fd52SLuigi Rizzo 				if (netmap_verbose)
240489e3fd52SLuigi Rizzo 					RD(2, "%p lost race on txring %d, ok",
240589e3fd52SLuigi Rizzo 					    priv, i);
240617885a7bSLuigi Rizzo 				continue;
240768b8534bSLuigi Rizzo 			}
240817885a7bSLuigi Rizzo 			if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
240917885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
241017885a7bSLuigi Rizzo 				revents |= POLLERR;
241117885a7bSLuigi Rizzo 			} else {
2412f0ea3689SLuigi Rizzo 				if (kring->nm_sync(kring, 0))
241368b8534bSLuigi Rizzo 					revents |= POLLERR;
241417885a7bSLuigi Rizzo 			}
241568b8534bSLuigi Rizzo 
241617885a7bSLuigi Rizzo 			/*
241717885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
241817885a7bSLuigi Rizzo 			 * listeners on the same ring.
241917885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
242017885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
2421f9790aebSLuigi Rizzo 			 */
242217885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
242317885a7bSLuigi Rizzo 			nm_kr_put(kring);
242417885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
242568b8534bSLuigi Rizzo 				revents |= want_tx;
242668b8534bSLuigi Rizzo 				want_tx = 0;
2427f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_TX, 0);
242868b8534bSLuigi Rizzo 			}
2429ce3ee1e7SLuigi Rizzo 		}
2430f0ea3689SLuigi Rizzo 		if (want_tx && retry_tx && !is_kevent) {
24310e73f29aSLuigi Rizzo 			OS_selrecord(td, check_all_tx ?
2432f0ea3689SLuigi Rizzo 			    &na->tx_si : &na->tx_rings[priv->np_txqfirst].si);
2433ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
2434ce3ee1e7SLuigi Rizzo 			goto flush_tx;
243568b8534bSLuigi Rizzo 		}
243668b8534bSLuigi Rizzo 	}
243768b8534bSLuigi Rizzo 
243868b8534bSLuigi Rizzo 	/*
243917885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
244068b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
244168b8534bSLuigi Rizzo 	 */
244268b8534bSLuigi Rizzo 	if (want_rx) {
244317885a7bSLuigi Rizzo 		int send_down = 0; /* transparent mode */
244489cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
2445ce3ee1e7SLuigi Rizzo do_retry_rx:
2446f0ea3689SLuigi Rizzo 		for (i = priv->np_rxqfirst; i < priv->np_rxqlast; i++) {
244717885a7bSLuigi Rizzo 			int found = 0;
244817885a7bSLuigi Rizzo 
244968b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
2450ce3ee1e7SLuigi Rizzo 
2451ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
245289e3fd52SLuigi Rizzo 				if (netmap_verbose)
245389e3fd52SLuigi Rizzo 					RD(2, "%p lost race on rxring %d, ok",
245489e3fd52SLuigi Rizzo 					    priv, i);
245517885a7bSLuigi Rizzo 				continue;
245668b8534bSLuigi Rizzo 			}
2457ce3ee1e7SLuigi Rizzo 
245817885a7bSLuigi Rizzo 			/*
245917885a7bSLuigi Rizzo 			 * transparent mode support: collect packets
246017885a7bSLuigi Rizzo 			 * from the rxring(s).
24614bf50f18SLuigi Rizzo 			 * XXX NR_FORWARD should only be read on
24624bf50f18SLuigi Rizzo 			 * physical or NIC ports
2463ce3ee1e7SLuigi Rizzo 			 */
2464091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
2465091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
2466091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
2467091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
2468091fd0abSLuigi Rizzo 			}
246968b8534bSLuigi Rizzo 
2470f0ea3689SLuigi Rizzo 			if (kring->nm_sync(kring, 0))
247168b8534bSLuigi Rizzo 				revents |= POLLERR;
24725819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
24735819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
247468b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
24755819da83SLuigi Rizzo 			}
247617885a7bSLuigi Rizzo 			/* after an rxsync we can use kring->rcur, rtail */
247717885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
247817885a7bSLuigi Rizzo 			nm_kr_put(kring);
247917885a7bSLuigi Rizzo 			if (found) {
248068b8534bSLuigi Rizzo 				revents |= want_rx;
2481ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
2482f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_RX, 0);
248368b8534bSLuigi Rizzo 			}
248468b8534bSLuigi Rizzo 		}
248517885a7bSLuigi Rizzo 
248617885a7bSLuigi Rizzo 		/* transparent mode XXX only during first pass ? */
2487f0ea3689SLuigi Rizzo 		if (na->na_flags & NAF_HOST_RINGS) {
2488f0ea3689SLuigi Rizzo 			kring = &na->rx_rings[na->num_rx_rings];
24894bf50f18SLuigi Rizzo 			if (check_all_rx
24904bf50f18SLuigi Rizzo 			    && (netmap_fwd || kring->ring->flags & NR_FORWARD)) {
24914bf50f18SLuigi Rizzo 				/* XXX fix to use kring fields */
24924bf50f18SLuigi Rizzo 				if (nm_ring_empty(kring->ring))
249317885a7bSLuigi Rizzo 					send_down = netmap_rxsync_from_host(na, td, dev);
24944bf50f18SLuigi Rizzo 				if (!nm_ring_empty(kring->ring))
24954bf50f18SLuigi Rizzo 					revents |= want_rx;
249617885a7bSLuigi Rizzo 			}
2497f0ea3689SLuigi Rizzo 		}
249817885a7bSLuigi Rizzo 
2499f0ea3689SLuigi Rizzo 		if (retry_rx && !is_kevent)
25000e73f29aSLuigi Rizzo 			OS_selrecord(td, check_all_rx ?
2501f0ea3689SLuigi Rizzo 			    &na->rx_si : &na->rx_rings[priv->np_rxqfirst].si);
250217885a7bSLuigi Rizzo 		if (send_down > 0 || retry_rx) {
250317885a7bSLuigi Rizzo 			retry_rx = 0;
250417885a7bSLuigi Rizzo 			if (send_down)
250517885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
250617885a7bSLuigi Rizzo 			else
2507ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
2508ce3ee1e7SLuigi Rizzo 		}
250968b8534bSLuigi Rizzo 	}
2510091fd0abSLuigi Rizzo 
251117885a7bSLuigi Rizzo 	/*
251217885a7bSLuigi Rizzo 	 * Transparent mode: marked bufs on rx rings between
251317885a7bSLuigi Rizzo 	 * kring->nr_hwcur and ring->head
251417885a7bSLuigi Rizzo 	 * are passed to the other endpoint.
251517885a7bSLuigi Rizzo 	 *
251617885a7bSLuigi Rizzo 	 * In this mode we also scan the sw rxring, which in
251717885a7bSLuigi Rizzo 	 * turn passes packets up.
251817885a7bSLuigi Rizzo 	 *
251917885a7bSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
252017885a7bSLuigi Rizzo  	 * rings to a single file descriptor.
2521ce3ee1e7SLuigi Rizzo 	 */
2522091fd0abSLuigi Rizzo 
25234bf50f18SLuigi Rizzo 	if (q.head && na->ifp != NULL)
2524f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
252568b8534bSLuigi Rizzo 
252668b8534bSLuigi Rizzo 	return (revents);
252768b8534bSLuigi Rizzo }
252868b8534bSLuigi Rizzo 
252917885a7bSLuigi Rizzo 
253017885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
253168b8534bSLuigi Rizzo 
2532f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
2533f9790aebSLuigi Rizzo 
253489cc2556SLuigi Rizzo /* default notify callback */
2535f9790aebSLuigi Rizzo static int
253617885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring,
253717885a7bSLuigi Rizzo 	enum txrx tx, int flags)
2538f9790aebSLuigi Rizzo {
2539f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2540f9790aebSLuigi Rizzo 
2541f9790aebSLuigi Rizzo 	if (tx == NR_TX) {
2542f9790aebSLuigi Rizzo 		kring = na->tx_rings + n_ring;
2543f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
254489cc2556SLuigi Rizzo 		/* optimization: avoid a wake up on the global
254589cc2556SLuigi Rizzo 		 * queue if nobody has registered for more
254689cc2556SLuigi Rizzo 		 * than one ring
254789cc2556SLuigi Rizzo 		 */
2548f0ea3689SLuigi Rizzo 		if (na->tx_si_users > 0)
2549f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->tx_si, PI_NET);
2550f9790aebSLuigi Rizzo 	} else {
2551f9790aebSLuigi Rizzo 		kring = na->rx_rings + n_ring;
2552f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
255389cc2556SLuigi Rizzo 		/* optimization: same as above */
2554f0ea3689SLuigi Rizzo 		if (na->rx_si_users > 0)
2555f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->rx_si, PI_NET);
2556f9790aebSLuigi Rizzo 	}
2557f9790aebSLuigi Rizzo 	return 0;
2558f9790aebSLuigi Rizzo }
2559f9790aebSLuigi Rizzo 
2560f9790aebSLuigi Rizzo 
256189cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
256289cc2556SLuigi Rizzo  * Attach na to the ifp (if any) and provide defaults
256389cc2556SLuigi Rizzo  * for optional callbacks. Defaults assume that we
256489cc2556SLuigi Rizzo  * are creating an hardware netmap_adapter.
256589cc2556SLuigi Rizzo  */
2566f9790aebSLuigi Rizzo int
2567f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2568f9790aebSLuigi Rizzo {
2569f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2570f9790aebSLuigi Rizzo 
2571f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2572f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
25734bf50f18SLuigi Rizzo 			na->name, na->num_tx_rings, na->num_rx_rings);
2574f9790aebSLuigi Rizzo 		return EINVAL;
2575f9790aebSLuigi Rizzo 	}
25764bf50f18SLuigi Rizzo 	/* ifp is NULL for virtual adapters (bwrap, non-persistent VALE ports,
25774bf50f18SLuigi Rizzo 	 * pipes, monitors). For bwrap we actually have a non-null ifp for
25784bf50f18SLuigi Rizzo 	 * use by the external modules, but that is set after this
25794bf50f18SLuigi Rizzo 	 * function has been called.
25804bf50f18SLuigi Rizzo 	 * XXX this is ugly, maybe split this function in two (2014-03-14)
25814bf50f18SLuigi Rizzo 	 */
25824bf50f18SLuigi Rizzo 	if (ifp != NULL) {
2583f9790aebSLuigi Rizzo 		WNA(ifp) = na;
258417885a7bSLuigi Rizzo 
258517885a7bSLuigi Rizzo 	/* the following is only needed for na that use the host port.
258617885a7bSLuigi Rizzo 	 * XXX do we have something similar for linux ?
258717885a7bSLuigi Rizzo 	 */
258817885a7bSLuigi Rizzo #ifdef __FreeBSD__
258917885a7bSLuigi Rizzo 		na->if_input = ifp->if_input; /* for netmap_send_up */
259017885a7bSLuigi Rizzo #endif /* __FreeBSD__ */
259117885a7bSLuigi Rizzo 
2592f9790aebSLuigi Rizzo 		NETMAP_SET_CAPABLE(ifp);
25934bf50f18SLuigi Rizzo 	}
2594f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
259589cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
259689cc2556SLuigi Rizzo 		 * since other port types all provide their own
259789cc2556SLuigi Rizzo 		 * nm_krings_create
259889cc2556SLuigi Rizzo 		 */
2599f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
260017885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
2601f9790aebSLuigi Rizzo 	}
2602f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2603f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2604f9790aebSLuigi Rizzo 	na->active_fds = 0;
2605f9790aebSLuigi Rizzo 
2606f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
26074bf50f18SLuigi Rizzo 		/* use the global allocator */
2608f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
26094bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
26104bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
26114bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
26124bf50f18SLuigi Rizzo 		 */
26134bf50f18SLuigi Rizzo 		na->nm_bdg_attach = netmap_bwrap_attach;
2614f9790aebSLuigi Rizzo 	return 0;
2615f9790aebSLuigi Rizzo }
2616f9790aebSLuigi Rizzo 
2617f9790aebSLuigi Rizzo 
261889cc2556SLuigi Rizzo /* standard cleanup, called by all destructors */
2619f9790aebSLuigi Rizzo void
2620f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2621f9790aebSLuigi Rizzo {
262289cc2556SLuigi Rizzo 	if (na->ifp != NULL)
2623f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2624f9790aebSLuigi Rizzo 
2625f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2626f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2627f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2628f9790aebSLuigi Rizzo 	}
2629f0ea3689SLuigi Rizzo 	netmap_pipe_dealloc(na);
2630f9790aebSLuigi Rizzo 	if (na->na_flags & NAF_MEM_OWNER)
2631f9790aebSLuigi Rizzo 		netmap_mem_private_delete(na->nm_mem);
2632f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2633f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2634f9790aebSLuigi Rizzo }
2635f9790aebSLuigi Rizzo 
26364bf50f18SLuigi Rizzo /* Wrapper for the register callback provided hardware drivers.
26374bf50f18SLuigi Rizzo  * na->ifp == NULL means the the driver module has been
26384bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
26394bf50f18SLuigi Rizzo  * Note that module unloading, in our patched linux drivers,
26404bf50f18SLuigi Rizzo  * happens under NMG_LOCK and after having stopped all the
26414bf50f18SLuigi Rizzo  * nic rings (see netmap_detach). This provides sufficient
26424bf50f18SLuigi Rizzo  * protection for the other driver-provied callbacks
26434bf50f18SLuigi Rizzo  * (i.e., nm_config and nm_*xsync), that therefore don't need
26444bf50f18SLuigi Rizzo  * to wrapped.
26454bf50f18SLuigi Rizzo  */
26464bf50f18SLuigi Rizzo static int
26474bf50f18SLuigi Rizzo netmap_hw_register(struct netmap_adapter *na, int onoff)
26484bf50f18SLuigi Rizzo {
26494bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
26504bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
26514bf50f18SLuigi Rizzo 
26524bf50f18SLuigi Rizzo 	if (na->ifp == NULL)
26534bf50f18SLuigi Rizzo 		return onoff ? ENXIO : 0;
26544bf50f18SLuigi Rizzo 
26554bf50f18SLuigi Rizzo 	return hwna->nm_hw_register(na, onoff);
26564bf50f18SLuigi Rizzo }
26574bf50f18SLuigi Rizzo 
2658f18be576SLuigi Rizzo 
265968b8534bSLuigi Rizzo /*
266068b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
266168b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
266268b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
266368b8534bSLuigi Rizzo  * of hardware rings):
266468b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
266568b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
266617885a7bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues. // XXX still true ?
266768b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
266868b8534bSLuigi Rizzo  */
266968b8534bSLuigi Rizzo int
2670f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
267168b8534bSLuigi Rizzo {
2672f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2673f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2674ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
267568b8534bSLuigi Rizzo 
2676ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2677ae10d1afSLuigi Rizzo 		goto fail;
2678f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2679f9790aebSLuigi Rizzo 	if (hwna == NULL)
2680ae10d1afSLuigi Rizzo 		goto fail;
2681f9790aebSLuigi Rizzo 	hwna->up = *arg;
2682f0ea3689SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS;
26834bf50f18SLuigi Rizzo 	strncpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
26844bf50f18SLuigi Rizzo 	hwna->nm_hw_register = hwna->up.nm_register;
26854bf50f18SLuigi Rizzo 	hwna->up.nm_register = netmap_hw_register;
2686f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2687f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2688f9790aebSLuigi Rizzo 		goto fail;
2689f9790aebSLuigi Rizzo 	}
2690f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2691f9790aebSLuigi Rizzo 
269264ae02c3SLuigi Rizzo #ifdef linux
2693f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2694f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2695f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2696f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2697f18be576SLuigi Rizzo #else
2698f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2699f18be576SLuigi Rizzo #endif
2700f18be576SLuigi Rizzo 	}
2701f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
27024bf50f18SLuigi Rizzo 	if (ifp->ethtool_ops) {
27034bf50f18SLuigi Rizzo 		hwna->nm_eto = *ifp->ethtool_ops;
27044bf50f18SLuigi Rizzo 	}
27054bf50f18SLuigi Rizzo 	hwna->nm_eto.set_ringparam = linux_netmap_set_ringparam;
27064bf50f18SLuigi Rizzo #ifdef ETHTOOL_SCHANNELS
27074bf50f18SLuigi Rizzo 	hwna->nm_eto.set_channels = linux_netmap_set_channels;
27084bf50f18SLuigi Rizzo #endif
27094bf50f18SLuigi Rizzo 	if (arg->nm_config == NULL) {
27104bf50f18SLuigi Rizzo 		hwna->up.nm_config = netmap_linux_config;
27114bf50f18SLuigi Rizzo 	}
2712ce3ee1e7SLuigi Rizzo #endif /* linux */
2713f9790aebSLuigi Rizzo 
271489cc2556SLuigi Rizzo 	D("success for %s tx %d/%d rx %d/%d queues/slots",
27154bf50f18SLuigi Rizzo 		hwna->up.name,
271689cc2556SLuigi Rizzo 		hwna->up.num_tx_rings, hwna->up.num_tx_desc,
271789cc2556SLuigi Rizzo 		hwna->up.num_rx_rings, hwna->up.num_rx_desc
271889cc2556SLuigi Rizzo 		);
2719ae10d1afSLuigi Rizzo 	return 0;
272068b8534bSLuigi Rizzo 
2721ae10d1afSLuigi Rizzo fail:
2722f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2723441ab64fSLuigi Rizzo 	if (ifp)
2724849bec0eSLuigi Rizzo 		netmap_detach(ifp);
2725f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
272668b8534bSLuigi Rizzo }
272768b8534bSLuigi Rizzo 
272868b8534bSLuigi Rizzo 
2729f9790aebSLuigi Rizzo void
2730f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2731f9790aebSLuigi Rizzo {
2732f9790aebSLuigi Rizzo 	if (!na) {
2733f9790aebSLuigi Rizzo 		return;
2734f9790aebSLuigi Rizzo 	}
2735f9790aebSLuigi Rizzo 
2736f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2737f9790aebSLuigi Rizzo }
2738f9790aebSLuigi Rizzo 
2739f9790aebSLuigi Rizzo 
2740f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2741f9790aebSLuigi Rizzo int
2742f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2743f9790aebSLuigi Rizzo {
2744f9790aebSLuigi Rizzo 	if (!na)
2745f9790aebSLuigi Rizzo 		return 1;
2746f9790aebSLuigi Rizzo 
2747f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2748f9790aebSLuigi Rizzo 		return 0;
2749f9790aebSLuigi Rizzo 
2750f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2751f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2752f9790aebSLuigi Rizzo 
2753f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2754f9790aebSLuigi Rizzo 
2755f9790aebSLuigi Rizzo 	return 1;
2756f9790aebSLuigi Rizzo }
2757f9790aebSLuigi Rizzo 
275889cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
2759f9790aebSLuigi Rizzo int
2760f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2761f9790aebSLuigi Rizzo {
2762f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
276317885a7bSLuigi Rizzo 	if (ret == 0) {
276417885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
276517885a7bSLuigi Rizzo 		mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue);
276617885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
276717885a7bSLuigi Rizzo 	}
276817885a7bSLuigi Rizzo 	return ret;
2769f9790aebSLuigi Rizzo }
2770f9790aebSLuigi Rizzo 
2771f9790aebSLuigi Rizzo 
2772f9790aebSLuigi Rizzo 
277368b8534bSLuigi Rizzo /*
277489cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
277568b8534bSLuigi Rizzo  */
277668b8534bSLuigi Rizzo void
277768b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
277868b8534bSLuigi Rizzo {
277968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
278068b8534bSLuigi Rizzo 
278168b8534bSLuigi Rizzo 	if (!na)
278268b8534bSLuigi Rizzo 		return;
278368b8534bSLuigi Rizzo 
2784f9790aebSLuigi Rizzo 	NMG_LOCK();
2785f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2786fb25194fSLuigi Rizzo 	if (!netmap_adapter_put(na)) {
2787fb25194fSLuigi Rizzo 		/* someone is still using the adapter,
2788fb25194fSLuigi Rizzo 		 * tell them that the interface is gone
2789fb25194fSLuigi Rizzo 		 */
2790f9790aebSLuigi Rizzo 		na->ifp = NULL;
27914bf50f18SLuigi Rizzo 		// XXX also clear NAF_NATIVE_ON ?
27924bf50f18SLuigi Rizzo 		na->na_flags &= ~NAF_NETMAP_ON;
2793fb25194fSLuigi Rizzo 		/* give them a chance to notice */
2794f9790aebSLuigi Rizzo 		netmap_enable_all_rings(ifp);
2795fb25194fSLuigi Rizzo 	}
2796f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2797ae10d1afSLuigi Rizzo }
2798f18be576SLuigi Rizzo 
2799f18be576SLuigi Rizzo 
280068b8534bSLuigi Rizzo /*
280102ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
280202ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
280317885a7bSLuigi Rizzo  *
280417885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
280517885a7bSLuigi Rizzo  * in the relevant rxsync routine.
280617885a7bSLuigi Rizzo  *
2807ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2808ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2809ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2810f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
281168b8534bSLuigi Rizzo  */
281268b8534bSLuigi Rizzo int
2813ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
281468b8534bSLuigi Rizzo {
281568b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2816ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
281717885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
281817885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
281917885a7bSLuigi Rizzo 	struct mbq *q;
282017885a7bSLuigi Rizzo 	int space;
282168b8534bSLuigi Rizzo 
2822ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2823ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2824ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
282517885a7bSLuigi Rizzo 
28264bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
28274bf50f18SLuigi Rizzo 		D("%s not in netmap mode anymore", na->name);
2828ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2829ce3ee1e7SLuigi Rizzo 		goto done;
2830ce3ee1e7SLuigi Rizzo 	}
2831ce3ee1e7SLuigi Rizzo 
2832ce3ee1e7SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
283317885a7bSLuigi Rizzo 	q = &kring->rx_queue;
283417885a7bSLuigi Rizzo 
2835ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
28364bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
28374bf50f18SLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", na->name,
28384bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
2839ce3ee1e7SLuigi Rizzo 		goto done;
2840849bec0eSLuigi Rizzo 	}
284117885a7bSLuigi Rizzo 
284217885a7bSLuigi Rizzo 	/* protect against rxsync_from_host(), netmap_sw_to_nic()
284317885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
284417885a7bSLuigi Rizzo 	 * not possible on Linux).
284517885a7bSLuigi Rizzo 	 * Also avoid overflowing the queue.
2846ce3ee1e7SLuigi Rizzo 	 */
2847997b054cSLuigi Rizzo 	mbq_lock(q);
284817885a7bSLuigi Rizzo 
284917885a7bSLuigi Rizzo         space = kring->nr_hwtail - kring->nr_hwcur;
285017885a7bSLuigi Rizzo         if (space < 0)
285117885a7bSLuigi Rizzo                 space += kring->nkr_num_slots;
285217885a7bSLuigi Rizzo 	if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX
285317885a7bSLuigi Rizzo 		RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p",
28544bf50f18SLuigi Rizzo 			na->name, kring->nr_hwcur, kring->nr_hwtail, mbq_len(q),
285517885a7bSLuigi Rizzo 			len, m);
2856ce3ee1e7SLuigi Rizzo 	} else {
285717885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
285817885a7bSLuigi Rizzo 		ND(10, "%s %d bufs in queue len %d m %p",
28594bf50f18SLuigi Rizzo 			na->name, mbq_len(q), len, m);
286017885a7bSLuigi Rizzo 		/* notify outside the lock */
286117885a7bSLuigi Rizzo 		m = NULL;
286268b8534bSLuigi Rizzo 		error = 0;
2863ce3ee1e7SLuigi Rizzo 	}
2864997b054cSLuigi Rizzo 	mbq_unlock(q);
2865ce3ee1e7SLuigi Rizzo 
286668b8534bSLuigi Rizzo done:
286717885a7bSLuigi Rizzo 	if (m)
286868b8534bSLuigi Rizzo 		m_freem(m);
286917885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
287017885a7bSLuigi Rizzo 	na->nm_notify(na, na->num_rx_rings, NR_RX, 0);
287189cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
287289cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
287389cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
287489cc2556SLuigi Rizzo 	 */
287568b8534bSLuigi Rizzo 
287668b8534bSLuigi Rizzo 	return (error);
287768b8534bSLuigi Rizzo }
287868b8534bSLuigi Rizzo 
287968b8534bSLuigi Rizzo 
288068b8534bSLuigi Rizzo /*
288168b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
288268b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2883f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
288468b8534bSLuigi Rizzo  */
288568b8534bSLuigi Rizzo struct netmap_slot *
2886ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
288768b8534bSLuigi Rizzo 	u_int new_cur)
288868b8534bSLuigi Rizzo {
288968b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2890506cc70cSLuigi Rizzo 	int new_hwofs, lim;
289168b8534bSLuigi Rizzo 
28924bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
28934bf50f18SLuigi Rizzo 		ND("interface not in native netmap mode");
289468b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2895ce3ee1e7SLuigi Rizzo 	}
289668b8534bSLuigi Rizzo 
2897ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2898ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2899ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2900ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2901ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2902ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2903ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2904ce3ee1e7SLuigi Rizzo 	 */
290564ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
29068241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
29078241616dSLuigi Rizzo 			return NULL;
290864ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
290917885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
2910506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
291164ae02c3SLuigi Rizzo 	} else {
29128241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
29138241616dSLuigi Rizzo 			return NULL;
291464ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
291517885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
291664ae02c3SLuigi Rizzo 	}
291764ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2918506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2919506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2920506cc70cSLuigi Rizzo 
2921ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
292217885a7bSLuigi Rizzo 	if (netmap_verbose)
292317885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
29244bf50f18SLuigi Rizzo 		na->name,
292517885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
2926ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
292717885a7bSLuigi Rizzo 		kring->nr_hwtail,
292817885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
2929506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
293017885a7bSLuigi Rizzo 	if (tx == NR_TX) {
293117885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
293217885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
293317885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
293417885a7bSLuigi Rizzo 	}
2935506cc70cSLuigi Rizzo 
2936f196ce38SLuigi Rizzo #if 0 // def linux
2937f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
2938f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
2939f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2940f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2941f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
2942f196ce38SLuigi Rizzo 		// XXX fix error handling
2943f196ce38SLuigi Rizzo 	}
2944f196ce38SLuigi Rizzo 
2945f196ce38SLuigi Rizzo #endif /* linux */
294668b8534bSLuigi Rizzo 	/*
2947ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
2948506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
2949506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
295068b8534bSLuigi Rizzo 	 */
2951f0ea3689SLuigi Rizzo 	na->nm_notify(na, n, tx, 0);
295268b8534bSLuigi Rizzo 	return kring->ring->slot;
295368b8534bSLuigi Rizzo }
295468b8534bSLuigi Rizzo 
295568b8534bSLuigi Rizzo 
2956ce3ee1e7SLuigi Rizzo /*
2957f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
2958ce3ee1e7SLuigi Rizzo  *
2959ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2960ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
2961ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
2962849bec0eSLuigi Rizzo  *
2963f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
2964f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
2965f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
29664bf50f18SLuigi Rizzo  *   (see netmap_notify)
29674bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
29684bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
2969f9790aebSLuigi Rizzo  */
2970f9790aebSLuigi Rizzo void
2971f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
2972f9790aebSLuigi Rizzo {
2973f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2974f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2975f9790aebSLuigi Rizzo 
2976f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
2977f9790aebSLuigi Rizzo 
2978f9790aebSLuigi Rizzo 	if (netmap_verbose) {
2979f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2980f9790aebSLuigi Rizzo 	}
2981f9790aebSLuigi Rizzo 
2982f9790aebSLuigi Rizzo 	if (work_done) { /* RX path */
2983f9790aebSLuigi Rizzo 		if (q >= na->num_rx_rings)
2984f9790aebSLuigi Rizzo 			return;	// not a physical queue
2985f9790aebSLuigi Rizzo 		kring = na->rx_rings + q;
2986f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
2987f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_RX, 0);
2988f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
2989f9790aebSLuigi Rizzo 	} else { /* TX path */
2990f9790aebSLuigi Rizzo 		if (q >= na->num_tx_rings)
2991f9790aebSLuigi Rizzo 			return;	// not a physical queue
2992f9790aebSLuigi Rizzo 		kring = na->tx_rings + q;
2993f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_TX, 0);
2994f9790aebSLuigi Rizzo 	}
2995f9790aebSLuigi Rizzo }
2996f9790aebSLuigi Rizzo 
299717885a7bSLuigi Rizzo 
2998f9790aebSLuigi Rizzo /*
2999f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
3000f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3001f9790aebSLuigi Rizzo  *
3002ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
3003ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
3004f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
3005ce3ee1e7SLuigi Rizzo  *
3006ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
3007ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
3008ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
3009ce3ee1e7SLuigi Rizzo  * and return 1.
3010ce3ee1e7SLuigi Rizzo  *
3011ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
3012ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
30131a26580eSLuigi Rizzo  */
3014babc7c12SLuigi Rizzo int
3015ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
30161a26580eSLuigi Rizzo {
30174bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
30184bf50f18SLuigi Rizzo 
30194bf50f18SLuigi Rizzo 	/*
30204bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
30214bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
30224bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
30234bf50f18SLuigi Rizzo 	 * nm_native_on() here.
30244bf50f18SLuigi Rizzo 	 */
30254bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
30261a26580eSLuigi Rizzo 		return 0;
3027849bec0eSLuigi Rizzo 
30284bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
30298241616dSLuigi Rizzo 		ND("use regular interrupt");
30308241616dSLuigi Rizzo 		return 0;
30318241616dSLuigi Rizzo 	}
30328241616dSLuigi Rizzo 
3033f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
30341a26580eSLuigi Rizzo 	return 1;
30351a26580eSLuigi Rizzo }
30361a26580eSLuigi Rizzo 
303764ae02c3SLuigi Rizzo 
303801c7d25fSLuigi Rizzo /*
3039f9790aebSLuigi Rizzo  * Module loader and unloader
3040f196ce38SLuigi Rizzo  *
3041f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
3042f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
3043f9790aebSLuigi Rizzo  * (but there is no chance)
3044f9790aebSLuigi Rizzo  *
3045f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
3046f196ce38SLuigi Rizzo  */
3047babc7c12SLuigi Rizzo 
3048babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
3049f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
3050babc7c12SLuigi Rizzo 
305117885a7bSLuigi Rizzo 
3052f9790aebSLuigi Rizzo void
305368b8534bSLuigi Rizzo netmap_fini(void)
305468b8534bSLuigi Rizzo {
3055f9790aebSLuigi Rizzo 	// XXX destroy_bridges() ?
3056f9790aebSLuigi Rizzo 	if (netmap_dev)
305768b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
3058ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
3059ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
306068b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
306168b8534bSLuigi Rizzo }
306268b8534bSLuigi Rizzo 
306317885a7bSLuigi Rizzo 
3064f9790aebSLuigi Rizzo int
3065f9790aebSLuigi Rizzo netmap_init(void)
306668b8534bSLuigi Rizzo {
3067f9790aebSLuigi Rizzo 	int error;
306868b8534bSLuigi Rizzo 
3069f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
307068b8534bSLuigi Rizzo 
3071f9790aebSLuigi Rizzo 	error = netmap_mem_init();
3072f9790aebSLuigi Rizzo 	if (error != 0)
3073f9790aebSLuigi Rizzo 		goto fail;
3074*c929ca72SLuigi Rizzo 	/*
3075*c929ca72SLuigi Rizzo 	 * MAKEDEV_ETERNAL_KLD avoids an expensive check on syscalls
3076*c929ca72SLuigi Rizzo 	 * when the module is compiled in.
3077*c929ca72SLuigi Rizzo 	 * XXX could use make_dev_credv() to get error number
3078*c929ca72SLuigi Rizzo 	 */
30790e73f29aSLuigi Rizzo 	netmap_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
308011c0b69cSAdrian Chadd 		&netmap_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0600,
30810e73f29aSLuigi Rizzo 			      "netmap");
3082f9790aebSLuigi Rizzo 	if (!netmap_dev)
3083f9790aebSLuigi Rizzo 		goto fail;
3084f9790aebSLuigi Rizzo 
3085f9790aebSLuigi Rizzo 	netmap_init_bridges();
30864bf50f18SLuigi Rizzo #ifdef __FreeBSD__
30874bf50f18SLuigi Rizzo 	nm_vi_init_index();
30884bf50f18SLuigi Rizzo #endif
3089f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
3090f9790aebSLuigi Rizzo 	return (0);
3091f9790aebSLuigi Rizzo fail:
309268b8534bSLuigi Rizzo 	netmap_fini();
3093f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
309468b8534bSLuigi Rizzo }
3095