xref: /freebsd/sys/dev/netmap/netmap.c (revision 8fd44c939522ba184d0d611b2ada7e027b1c63c9)
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()
296847bf383SLuigi Rizzo  *    - rx 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  *
316847bf383SLuigi Rizzo  *    na == NA(ifp) == generic_netmap_adapter created in generic_netmap_attach()
317847bf383SLuigi Rizzo  *
318847bf383SLuigi Rizzo  *    - tx from netmap userspace:
319847bf383SLuigi Rizzo  *       concurrently:
320847bf383SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
321847bf383SLuigi Rizzo  *               kring->nm_sync() == generic_netmap_txsync()
322847bf383SLuigi Rizzo  *                   linux:   dev_queue_xmit() with NM_MAGIC_PRIORITY_TX
323847bf383SLuigi Rizzo  *                       generic_ndo_start_xmit()
324847bf383SLuigi Rizzo  *                           orig. dev. start_xmit
325847bf383SLuigi Rizzo  *                   FreeBSD: na->if_transmit() == orig. dev if_transmit
326847bf383SLuigi Rizzo  *           2) generic_mbuf_destructor()
327847bf383SLuigi Rizzo  *                   na->nm_notify() == netmap_notify()
328847bf383SLuigi Rizzo  *    - rx from netmap userspace:
329847bf383SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
330847bf383SLuigi Rizzo  *               kring->nm_sync() == generic_netmap_rxsync()
331847bf383SLuigi Rizzo  *                   mbq_safe_dequeue()
332847bf383SLuigi Rizzo  *           2) device driver
333847bf383SLuigi Rizzo  *               generic_rx_handler()
334847bf383SLuigi Rizzo  *                   mbq_safe_enqueue()
335847bf383SLuigi Rizzo  *                   na->nm_notify() == netmap_notify()
336847bf383SLuigi Rizzo  *    - rx from host stack:
337847bf383SLuigi Rizzo  *        concurrently:
338847bf383SLuigi Rizzo  *           1) host stack
339847bf383SLuigi Rizzo  *               linux: generic_ndo_start_xmit()
340847bf383SLuigi Rizzo  *                   netmap_transmit()
341847bf383SLuigi Rizzo  *               FreeBSD: ifp->if_input() == netmap_transmit
342847bf383SLuigi Rizzo  *               both:
343847bf383SLuigi Rizzo  *                       na->nm_notify() == netmap_notify()
344847bf383SLuigi Rizzo  *           2) ioctl(NIOCRXSYNC)/netmap_poll() in process context
345847bf383SLuigi Rizzo  *                kring->nm_sync() == netmap_rxsync_from_host_compat
346847bf383SLuigi Rizzo  *                  netmap_rxsync_from_host(na, NULL, NULL)
347847bf383SLuigi Rizzo  *    - tx to host stack:
348847bf383SLuigi Rizzo  *           ioctl(NIOCTXSYNC)/netmap_poll() in process context
349847bf383SLuigi Rizzo  *             kring->nm_sync() == netmap_txsync_to_host_compat
350847bf383SLuigi Rizzo  *               netmap_txsync_to_host(na)
351847bf383SLuigi Rizzo  *                 NM_SEND_UP()
352847bf383SLuigi Rizzo  *                   FreeBSD: na->if_input() == ??? XXX
353847bf383SLuigi Rizzo  *                   linux: netif_rx() with NM_MAGIC_PRIORITY_RX
3544bf50f18SLuigi Rizzo  *
3554bf50f18SLuigi Rizzo  *
356847bf383SLuigi Rizzo  *                           -= VALE =-
3574bf50f18SLuigi Rizzo  *
358847bf383SLuigi Rizzo  *   INCOMING:
3594bf50f18SLuigi Rizzo  *
360847bf383SLuigi Rizzo  *      - VALE ports:
361847bf383SLuigi Rizzo  *          ioctl(NIOCTXSYNC)/netmap_poll() in process context
362847bf383SLuigi Rizzo  *              kring->nm_sync() == netmap_vp_txsync()
3634bf50f18SLuigi Rizzo  *
364847bf383SLuigi Rizzo  *      - system device with native support:
365847bf383SLuigi Rizzo  *         from cable:
366847bf383SLuigi Rizzo  *             interrupt
367847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr != host ring)
368847bf383SLuigi Rizzo  *                     kring->nm_sync() == DEVICE_netmap_rxsync()
369847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
370847bf383SLuigi Rizzo  *                     kring->nm_sync() == DEVICE_netmap_rxsync()
371847bf383SLuigi Rizzo  *         from host stack:
372847bf383SLuigi Rizzo  *             netmap_transmit()
373847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr == host ring)
374847bf383SLuigi Rizzo  *                     kring->nm_sync() == netmap_rxsync_from_host_compat()
375847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
3764bf50f18SLuigi Rizzo  *
377847bf383SLuigi Rizzo  *      - system device with generic support:
378847bf383SLuigi Rizzo  *         from device driver:
379847bf383SLuigi Rizzo  *            generic_rx_handler()
380847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr != host ring)
381847bf383SLuigi Rizzo  *                     kring->nm_sync() == generic_netmap_rxsync()
382847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
383847bf383SLuigi Rizzo  *                     kring->nm_sync() == generic_netmap_rxsync()
384847bf383SLuigi Rizzo  *         from host stack:
385847bf383SLuigi Rizzo  *            netmap_transmit()
386847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr == host ring)
387847bf383SLuigi Rizzo  *                     kring->nm_sync() == netmap_rxsync_from_host_compat()
388847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
3894bf50f18SLuigi Rizzo  *
390847bf383SLuigi Rizzo  *   (all cases) --> nm_bdg_flush()
391847bf383SLuigi Rizzo  *                      dest_na->nm_notify() == (see below)
3924bf50f18SLuigi Rizzo  *
393847bf383SLuigi Rizzo  *   OUTGOING:
3944bf50f18SLuigi Rizzo  *
395847bf383SLuigi Rizzo  *      - VALE ports:
396847bf383SLuigi Rizzo  *         concurrently:
397847bf383SLuigi Rizzo  *             1) ioctlNIOCRXSYNC)/netmap_poll() in process context
398847bf383SLuigi Rizzo  *                    kring->nm_sync() == netmap_vp_rxsync()
399847bf383SLuigi Rizzo  *             2) from nm_bdg_flush()
400847bf383SLuigi Rizzo  *                    na->nm_notify() == netmap_notify()
4014bf50f18SLuigi Rizzo  *
402847bf383SLuigi Rizzo  *      - system device with native support:
403847bf383SLuigi Rizzo  *          to cable:
404847bf383SLuigi Rizzo  *             na->nm_notify() == netmap_bwrap_notify()
405847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
406847bf383SLuigi Rizzo  *                 kring->nm_sync() == DEVICE_netmap_txsync()
407847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
408847bf383SLuigi Rizzo  *          to host stack:
409847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
410847bf383SLuigi Rizzo  *                 kring->nm_sync() == netmap_txsync_to_host_compat
411847bf383SLuigi Rizzo  *                 netmap_vp_rxsync_locked()
4124bf50f18SLuigi Rizzo  *
413847bf383SLuigi Rizzo  *      - system device with generic adapter:
414847bf383SLuigi Rizzo  *          to device driver:
415847bf383SLuigi Rizzo  *             na->nm_notify() == netmap_bwrap_notify()
416847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
417847bf383SLuigi Rizzo  *                 kring->nm_sync() == generic_netmap_txsync()
418847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
419847bf383SLuigi Rizzo  *          to host stack:
420847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
421847bf383SLuigi Rizzo  *                 kring->nm_sync() == netmap_txsync_to_host_compat
422847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
4234bf50f18SLuigi Rizzo  *
4244bf50f18SLuigi Rizzo  */
4254bf50f18SLuigi Rizzo 
426ce3ee1e7SLuigi Rizzo /*
427ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
428ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
429ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
430ce3ee1e7SLuigi Rizzo  */
43101c7d25fSLuigi Rizzo 
432ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
43368b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
43468b8534bSLuigi Rizzo #include <sys/types.h>
43568b8534bSLuigi Rizzo #include <sys/errno.h>
43668b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
43768b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
438f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
43989e3fd52SLuigi Rizzo #include <sys/filio.h>	/* FIONBIO */
44068b8534bSLuigi Rizzo #include <sys/sockio.h>
44168b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
44268b8534bSLuigi Rizzo #include <sys/malloc.h>
44368b8534bSLuigi Rizzo #include <sys/poll.h>
44489f6b863SAttilio Rao #include <sys/rwlock.h>
44568b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44668b8534bSLuigi Rizzo #include <sys/selinfo.h>
44768b8534bSLuigi Rizzo #include <sys/sysctl.h>
448339f59c0SGleb Smirnoff #include <sys/jail.h>
449339f59c0SGleb Smirnoff #include <net/vnet.h>
45068b8534bSLuigi Rizzo #include <net/if.h>
45176039bc8SGleb Smirnoff #include <net/if_var.h>
45268b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
45368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
454ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
455ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
45668b8534bSLuigi Rizzo 
45768b8534bSLuigi Rizzo 
458f9790aebSLuigi Rizzo /* reduce conditional code */
459f0ea3689SLuigi Rizzo // linux API, use for the knlist in FreeBSD
4600e73f29aSLuigi Rizzo /* use a private mutex for the knlist */
4610e73f29aSLuigi Rizzo #define init_waitqueue_head(x) do {			\
4620e73f29aSLuigi Rizzo 	struct mtx *m = &(x)->m;			\
4630e73f29aSLuigi Rizzo 	mtx_init(m, "nm_kn_lock", NULL, MTX_DEF);	\
4640e73f29aSLuigi Rizzo 	knlist_init_mtx(&(x)->si.si_note, m);		\
4650e73f29aSLuigi Rizzo     } while (0)
466ce3ee1e7SLuigi Rizzo 
4670e73f29aSLuigi Rizzo #define OS_selrecord(a, b)	selrecord(a, &((b)->si))
468f0ea3689SLuigi Rizzo #define OS_selwakeup(a, b)	freebsd_selwakeup(a, b)
469ce3ee1e7SLuigi Rizzo 
470ce3ee1e7SLuigi Rizzo #elif defined(linux)
471ce3ee1e7SLuigi Rizzo 
472ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
473ce3ee1e7SLuigi Rizzo 
474ce3ee1e7SLuigi Rizzo 
475ce3ee1e7SLuigi Rizzo 
476ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
477ce3ee1e7SLuigi Rizzo 
478ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
479ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
480ce3ee1e7SLuigi Rizzo 
481ce3ee1e7SLuigi Rizzo #else
482ce3ee1e7SLuigi Rizzo 
483ce3ee1e7SLuigi Rizzo #error	Unsupported platform
484ce3ee1e7SLuigi Rizzo 
485ce3ee1e7SLuigi Rizzo #endif /* unsupported */
486ce3ee1e7SLuigi Rizzo 
487ce3ee1e7SLuigi Rizzo /*
488ce3ee1e7SLuigi Rizzo  * common headers
489ce3ee1e7SLuigi Rizzo  */
4900b8ed8e0SLuigi Rizzo #include <net/netmap.h>
4910b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
492ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
4930b8ed8e0SLuigi Rizzo 
494ce3ee1e7SLuigi Rizzo 
495ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
496ce3ee1e7SLuigi Rizzo 
4975819da83SLuigi Rizzo /* user-controlled variables */
4985819da83SLuigi Rizzo int netmap_verbose;
4995819da83SLuigi Rizzo 
5005819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
5015819da83SLuigi Rizzo 
5025819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
5035819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
5045819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
5055819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
5065819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
5075819da83SLuigi Rizzo int netmap_mitigate = 1;
5085819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
509c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
5105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
5115819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
512f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
513f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
514f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
5155819da83SLuigi Rizzo 
5164bf50f18SLuigi Rizzo int netmap_adaptive_io = 0;
5174bf50f18SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, adaptive_io, CTLFLAG_RW,
5184bf50f18SLuigi Rizzo     &netmap_adaptive_io, 0 , "Adaptive I/O on paravirt");
5194bf50f18SLuigi Rizzo 
520f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
521091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
522f196ce38SLuigi Rizzo 
523f9790aebSLuigi Rizzo /*
524f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
525f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
526f9790aebSLuigi Rizzo  */
527f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
528f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
529f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
530f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
531f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
532f9790aebSLuigi Rizzo 
533f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
534f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
535f0ea3689SLuigi Rizzo int netmap_generic_rings = 1;   /* number of queues in generic. */
536f9790aebSLuigi Rizzo 
537f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
538091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
539f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
540f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
541f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
542f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW, &netmap_generic_rings, 0 , "");
543f196ce38SLuigi Rizzo 
544ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
545ce3ee1e7SLuigi Rizzo 
54617885a7bSLuigi Rizzo /*
54717885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
54817885a7bSLuigi Rizzo  * to make sure other users get to see it.
54917885a7bSLuigi Rizzo  */
5504bf50f18SLuigi Rizzo static void
551f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
552ce3ee1e7SLuigi Rizzo {
553ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
554ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
555ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
556ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
557ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
558ce3ee1e7SLuigi Rizzo }
559ce3ee1e7SLuigi Rizzo 
560847bf383SLuigi Rizzo /* stop or enable a single ring */
5614bf50f18SLuigi Rizzo void
562847bf383SLuigi Rizzo netmap_set_ring(struct netmap_adapter *na, u_int ring_id, enum txrx t, int stopped)
5634bf50f18SLuigi Rizzo {
5644bf50f18SLuigi Rizzo 	if (stopped)
565847bf383SLuigi Rizzo 		netmap_disable_ring(NMR(na, t) + ring_id);
5664bf50f18SLuigi Rizzo 	else
567847bf383SLuigi Rizzo 		NMR(na, t)[ring_id].nkr_stopped = 0;
5684bf50f18SLuigi Rizzo }
5694bf50f18SLuigi Rizzo 
570f9790aebSLuigi Rizzo 
57189cc2556SLuigi Rizzo /* stop or enable all the rings of na */
5724bf50f18SLuigi Rizzo void
5734bf50f18SLuigi Rizzo netmap_set_all_rings(struct netmap_adapter *na, int stopped)
574ce3ee1e7SLuigi Rizzo {
575ce3ee1e7SLuigi Rizzo 	int i;
576847bf383SLuigi Rizzo 	enum txrx t;
577ce3ee1e7SLuigi Rizzo 
5784bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
579ce3ee1e7SLuigi Rizzo 		return;
580ce3ee1e7SLuigi Rizzo 
581847bf383SLuigi Rizzo 	for_rx_tx(t) {
582847bf383SLuigi Rizzo 		for (i = 0; i < netmap_real_rings(na, t); i++) {
583847bf383SLuigi Rizzo 			netmap_set_ring(na, i, t, stopped);
584ce3ee1e7SLuigi Rizzo 		}
585ce3ee1e7SLuigi Rizzo 	}
586ce3ee1e7SLuigi Rizzo }
587ce3ee1e7SLuigi Rizzo 
58889cc2556SLuigi Rizzo /*
58989cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
59089cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
59189cc2556SLuigi Rizzo  * netmap mode off, or before removing the harware rings (e.g., on module
59289cc2556SLuigi Rizzo  * onload).  As a rule of thumb for linux drivers, this should be placed near
59389cc2556SLuigi Rizzo  * each napi_disable().
59489cc2556SLuigi Rizzo  */
595f9790aebSLuigi Rizzo void
596f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
597f9790aebSLuigi Rizzo {
5984bf50f18SLuigi Rizzo 	netmap_set_all_rings(NA(ifp), 1 /* stopped */);
599f9790aebSLuigi Rizzo }
600f9790aebSLuigi Rizzo 
60189cc2556SLuigi Rizzo /*
60289cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
60389cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
60489cc2556SLuigi Rizzo  * napi_enable().
60589cc2556SLuigi Rizzo  */
606f9790aebSLuigi Rizzo void
607f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
608f9790aebSLuigi Rizzo {
6094bf50f18SLuigi Rizzo 	netmap_set_all_rings(NA(ifp), 0 /* enabled */);
610f9790aebSLuigi Rizzo }
611f9790aebSLuigi Rizzo 
612f9790aebSLuigi Rizzo 
613ce3ee1e7SLuigi Rizzo /*
614ce3ee1e7SLuigi Rizzo  * generic bound_checking function
615ce3ee1e7SLuigi Rizzo  */
616ce3ee1e7SLuigi Rizzo u_int
617ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
618ce3ee1e7SLuigi Rizzo {
619ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
620ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
621ce3ee1e7SLuigi Rizzo 
622ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
623ce3ee1e7SLuigi Rizzo 		dflt = lo;
624ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
625ce3ee1e7SLuigi Rizzo 		dflt = hi;
626ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
627ce3ee1e7SLuigi Rizzo 		*v = dflt;
628ce3ee1e7SLuigi Rizzo 		op = "Bump";
629ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
630ce3ee1e7SLuigi Rizzo 		*v = hi;
631ce3ee1e7SLuigi Rizzo 		op = "Clamp";
632ce3ee1e7SLuigi Rizzo 	}
633ce3ee1e7SLuigi Rizzo 	if (op && msg)
634ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
635ce3ee1e7SLuigi Rizzo 	return *v;
636ce3ee1e7SLuigi Rizzo }
637ce3ee1e7SLuigi Rizzo 
638f9790aebSLuigi Rizzo 
639ce3ee1e7SLuigi Rizzo /*
640ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
641ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
642ce3ee1e7SLuigi Rizzo  */
643ce3ee1e7SLuigi Rizzo const char *
644ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
645ce3ee1e7SLuigi Rizzo {
646ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
647ce3ee1e7SLuigi Rizzo 	int i, j, i0;
648ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
649ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
650ce3ee1e7SLuigi Rizzo 
651ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
652ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
653ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
654ce3ee1e7SLuigi Rizzo 	if (!dst)
655ce3ee1e7SLuigi Rizzo 		dst = _dst;
656ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
657ce3ee1e7SLuigi Rizzo 		lim = len;
658ce3ee1e7SLuigi Rizzo 	o = dst;
659ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
660ce3ee1e7SLuigi Rizzo 	o += strlen(o);
661ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
662ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
663ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
664ce3ee1e7SLuigi Rizzo 		o += strlen(o);
665ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
666ce3ee1e7SLuigi Rizzo 		i0 = i;
667ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
668ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
669ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
670ce3ee1e7SLuigi Rizzo 		}
671ce3ee1e7SLuigi Rizzo 		i = i0;
672ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
673ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
674ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
675ce3ee1e7SLuigi Rizzo 		o += j+49;
676ce3ee1e7SLuigi Rizzo 	}
677ce3ee1e7SLuigi Rizzo 	*o = '\0';
678ce3ee1e7SLuigi Rizzo #undef P_HI
679ce3ee1e7SLuigi Rizzo #undef P_LO
680ce3ee1e7SLuigi Rizzo #undef P_C
681ce3ee1e7SLuigi Rizzo 	return dst;
682ce3ee1e7SLuigi Rizzo }
683f196ce38SLuigi Rizzo 
684f18be576SLuigi Rizzo 
685ae10d1afSLuigi Rizzo /*
686ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
687ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
688ae10d1afSLuigi Rizzo  */
68989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
690f9790aebSLuigi Rizzo int
691ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
692ae10d1afSLuigi Rizzo {
693ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
694ae10d1afSLuigi Rizzo 
695ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
6966641c68bSLuigi Rizzo 	if (na->nm_config == NULL ||
697847bf383SLuigi Rizzo 	    na->nm_config(na, &txr, &txd, &rxr, &rxd))
698847bf383SLuigi Rizzo 	{
699ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
700ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
701ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
702ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
703ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
704ae10d1afSLuigi Rizzo 	}
705ae10d1afSLuigi Rizzo 
706ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
707ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
708ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
709f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
710ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
7114bf50f18SLuigi Rizzo 			na->name,
712ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
713ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
714ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
7154bf50f18SLuigi Rizzo 			na->name, txr, txd, rxr, rxd);
716ae10d1afSLuigi Rizzo 	}
717f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
718ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
719ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
720ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
721ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
722ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
723ae10d1afSLuigi Rizzo 		return 0;
724ae10d1afSLuigi Rizzo 	}
725ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
726ae10d1afSLuigi Rizzo 	return 1;
727ae10d1afSLuigi Rizzo }
728ae10d1afSLuigi Rizzo 
729*8fd44c93SLuigi Rizzo static void netmap_txsync_to_host(struct netmap_adapter *na);
730*8fd44c93SLuigi Rizzo static int netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait);
731*8fd44c93SLuigi Rizzo 
73289cc2556SLuigi Rizzo /* kring->nm_sync callback for the host tx ring */
733f0ea3689SLuigi Rizzo static int
734f0ea3689SLuigi Rizzo netmap_txsync_to_host_compat(struct netmap_kring *kring, int flags)
735f0ea3689SLuigi Rizzo {
73689cc2556SLuigi Rizzo 	(void)flags; /* unused */
737f0ea3689SLuigi Rizzo 	netmap_txsync_to_host(kring->na);
738f0ea3689SLuigi Rizzo 	return 0;
739f0ea3689SLuigi Rizzo }
740f0ea3689SLuigi Rizzo 
74189cc2556SLuigi Rizzo /* kring->nm_sync callback for the host rx ring */
742f0ea3689SLuigi Rizzo static int
743f0ea3689SLuigi Rizzo netmap_rxsync_from_host_compat(struct netmap_kring *kring, int flags)
744f0ea3689SLuigi Rizzo {
74589cc2556SLuigi Rizzo 	(void)flags; /* unused */
746f0ea3689SLuigi Rizzo 	netmap_rxsync_from_host(kring->na, NULL, NULL);
747f0ea3689SLuigi Rizzo 	return 0;
748f0ea3689SLuigi Rizzo }
749f0ea3689SLuigi Rizzo 
750f0ea3689SLuigi Rizzo 
751f0ea3689SLuigi Rizzo 
752f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
753f0ea3689SLuigi Rizzo  * The array layout is this:
754f0ea3689SLuigi Rizzo  *
755f0ea3689SLuigi Rizzo  *                    +----------+
756f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
757f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
758f0ea3689SLuigi Rizzo  *                    |          | /
759f0ea3689SLuigi Rizzo  *                    +----------+
760f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
761f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
762f0ea3689SLuigi Rizzo  *                    |          | \
763f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
764f0ea3689SLuigi Rizzo  *                    |          | /
765f0ea3689SLuigi Rizzo  *                    +----------+
766f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
767f0ea3689SLuigi Rizzo  *                    +----------+
768f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
769f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
770f0ea3689SLuigi Rizzo  *                    |          | /
771f0ea3689SLuigi Rizzo  *                    +----------+
772f0ea3689SLuigi Rizzo  *
773f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
774f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
775f0ea3689SLuigi Rizzo  */
77689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
777f9790aebSLuigi Rizzo int
778f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
779f9790aebSLuigi Rizzo {
780f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
781f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
782847bf383SLuigi Rizzo 	u_int n[NR_TXRX];
783847bf383SLuigi Rizzo 	enum txrx t;
784f9790aebSLuigi Rizzo 
785f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
786847bf383SLuigi Rizzo 	n[NR_TX] = na->num_tx_rings + 1;
787847bf383SLuigi Rizzo 	n[NR_RX] = na->num_rx_rings + 1;
788f0ea3689SLuigi Rizzo 
789847bf383SLuigi Rizzo 	len = (n[NR_TX] + n[NR_RX]) * sizeof(struct netmap_kring) + tailroom;
790f9790aebSLuigi Rizzo 
791f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
792f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
793f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
794f9790aebSLuigi Rizzo 		return ENOMEM;
795f9790aebSLuigi Rizzo 	}
796847bf383SLuigi Rizzo 	na->rx_rings = na->tx_rings + n[NR_TX];
797f9790aebSLuigi Rizzo 
79817885a7bSLuigi Rizzo 	/*
79917885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
80017885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
80117885a7bSLuigi Rizzo 	 */
802847bf383SLuigi Rizzo 	for_rx_tx(t) {
803847bf383SLuigi Rizzo 		ndesc = nma_get_ndesc(na, t);
804847bf383SLuigi Rizzo 		for (i = 0; i < n[t]; i++) {
805847bf383SLuigi Rizzo 			kring = &NMR(na, t)[i];
806f9790aebSLuigi Rizzo 			bzero(kring, sizeof(*kring));
807f9790aebSLuigi Rizzo 			kring->na = na;
80817885a7bSLuigi Rizzo 			kring->ring_id = i;
809847bf383SLuigi Rizzo 			kring->tx = t;
810f9790aebSLuigi Rizzo 			kring->nkr_num_slots = ndesc;
811847bf383SLuigi Rizzo 			if (i < nma_get_nrings(na, t)) {
812847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ? na->nm_txsync : na->nm_rxsync);
813f0ea3689SLuigi Rizzo 			} else if (i == na->num_tx_rings) {
814847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ?
815847bf383SLuigi Rizzo 						netmap_txsync_to_host_compat :
816847bf383SLuigi Rizzo 						netmap_rxsync_from_host_compat);
817f0ea3689SLuigi Rizzo 			}
818847bf383SLuigi Rizzo 			kring->nm_notify = na->nm_notify;
819847bf383SLuigi Rizzo 			kring->rhead = kring->rcur = kring->nr_hwcur = 0;
820f9790aebSLuigi Rizzo 			/*
82117885a7bSLuigi Rizzo 			 * IMPORTANT: Always keep one slot empty.
822f9790aebSLuigi Rizzo 			 */
823847bf383SLuigi Rizzo 			kring->rtail = kring->nr_hwtail = (t == NR_TX ? ndesc - 1 : 0);
824847bf383SLuigi Rizzo 			snprintf(kring->name, sizeof(kring->name) - 1, "%s %s%d", na->name,
825847bf383SLuigi Rizzo 					nm_txrx2str(t), i);
826f0ea3689SLuigi Rizzo 			ND("ktx %s h %d c %d t %d",
827f0ea3689SLuigi Rizzo 				kring->name, kring->rhead, kring->rcur, kring->rtail);
828847bf383SLuigi Rizzo 			mtx_init(&kring->q_lock, (t == NR_TX ? "nm_txq_lock" : "nm_rxq_lock"), NULL, MTX_DEF);
829f9790aebSLuigi Rizzo 			init_waitqueue_head(&kring->si);
830f9790aebSLuigi Rizzo 		}
831847bf383SLuigi Rizzo 		init_waitqueue_head(&na->si[t]);
832f0ea3689SLuigi Rizzo 	}
833f9790aebSLuigi Rizzo 
834847bf383SLuigi Rizzo 	na->tailroom = na->rx_rings + n[NR_RX];
835f9790aebSLuigi Rizzo 
836f9790aebSLuigi Rizzo 	return 0;
837f9790aebSLuigi Rizzo }
838f9790aebSLuigi Rizzo 
839f9790aebSLuigi Rizzo 
8400e73f29aSLuigi Rizzo #ifdef __FreeBSD__
8410e73f29aSLuigi Rizzo static void
8420e73f29aSLuigi Rizzo netmap_knlist_destroy(NM_SELINFO_T *si)
8430e73f29aSLuigi Rizzo {
8440e73f29aSLuigi Rizzo 	/* XXX kqueue(9) needed; these will mirror knlist_init. */
8450e73f29aSLuigi Rizzo 	knlist_delete(&si->si.si_note, curthread, 0 /* not locked */ );
8460e73f29aSLuigi Rizzo 	knlist_destroy(&si->si.si_note);
8470e73f29aSLuigi Rizzo 	/* now we don't need the mutex anymore */
8480e73f29aSLuigi Rizzo 	mtx_destroy(&si->m);
8490e73f29aSLuigi Rizzo }
8500e73f29aSLuigi Rizzo #endif /* __FreeBSD__ */
8510e73f29aSLuigi Rizzo 
8520e73f29aSLuigi Rizzo 
853f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
85489cc2556SLuigi Rizzo /* call with NMG_LOCK held */
855f9790aebSLuigi Rizzo void
856f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
857f9790aebSLuigi Rizzo {
858f0ea3689SLuigi Rizzo 	struct netmap_kring *kring = na->tx_rings;
859847bf383SLuigi Rizzo 	enum txrx t;
860847bf383SLuigi Rizzo 
861847bf383SLuigi Rizzo 	for_rx_tx(t)
862847bf383SLuigi Rizzo 		netmap_knlist_destroy(&na->si[t]);
863f9790aebSLuigi Rizzo 
864f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
865f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
866f0ea3689SLuigi Rizzo 		mtx_destroy(&kring->q_lock);
8670e73f29aSLuigi Rizzo 		netmap_knlist_destroy(&kring->si);
868f9790aebSLuigi Rizzo 	}
869f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
870f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
871f9790aebSLuigi Rizzo }
872f9790aebSLuigi Rizzo 
873f9790aebSLuigi Rizzo 
87417885a7bSLuigi Rizzo /*
87517885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
87617885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
87717885a7bSLuigi Rizzo  * them first.
87817885a7bSLuigi Rizzo  */
87989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
88017885a7bSLuigi Rizzo static void
88117885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
88217885a7bSLuigi Rizzo {
88317885a7bSLuigi Rizzo 	struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue;
88417885a7bSLuigi Rizzo 
88517885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
88617885a7bSLuigi Rizzo 	mbq_purge(q);
88717885a7bSLuigi Rizzo 	mbq_safe_destroy(q);
88817885a7bSLuigi Rizzo 	netmap_krings_delete(na);
88917885a7bSLuigi Rizzo }
89017885a7bSLuigi Rizzo 
89117885a7bSLuigi Rizzo 
892f9790aebSLuigi Rizzo 
89368b8534bSLuigi Rizzo /*
894847bf383SLuigi Rizzo  * Undo everything that was done in netmap_do_regif(). In particular,
895847bf383SLuigi Rizzo  * call nm_register(ifp,0) to stop netmap mode on the interface and
8964bf50f18SLuigi Rizzo  * revert to normal operation.
89768b8534bSLuigi Rizzo  */
898ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
899847bf383SLuigi Rizzo static void netmap_unset_ringid(struct netmap_priv_d *);
900847bf383SLuigi Rizzo static void netmap_rel_exclusive(struct netmap_priv_d *);
90168b8534bSLuigi Rizzo static void
902847bf383SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv)
90368b8534bSLuigi Rizzo {
904f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
90568b8534bSLuigi Rizzo 
906ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
907f9790aebSLuigi Rizzo 	na->active_fds--;
908847bf383SLuigi Rizzo 	/* release exclusive use if it was requested on regif */
909847bf383SLuigi Rizzo 	netmap_rel_exclusive(priv);
910f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
91168b8534bSLuigi Rizzo 
912ae10d1afSLuigi Rizzo 		if (netmap_verbose)
9134bf50f18SLuigi Rizzo 			D("deleting last instance for %s", na->name);
914847bf383SLuigi Rizzo 
915847bf383SLuigi Rizzo #ifdef	WITH_MONITOR
916847bf383SLuigi Rizzo 		/* walk through all the rings and tell any monitor
917847bf383SLuigi Rizzo 		 * that the port is going to exit netmap mode
918847bf383SLuigi Rizzo 		 */
919847bf383SLuigi Rizzo 		netmap_monitor_stop(na);
920847bf383SLuigi Rizzo #endif
92168b8534bSLuigi Rizzo 		/*
922f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
923f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
924f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
925f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
926ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
927ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
928ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
929ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
930ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
931ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
932ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
933ce3ee1e7SLuigi Rizzo 		 * syscall is running.
93468b8534bSLuigi Rizzo 		 */
935f9790aebSLuigi Rizzo 		na->nm_register(na, 0); /* off, clear flags */
93668b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
93768b8534bSLuigi Rizzo 		 * then return POLLERR
938ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
939ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
94068b8534bSLuigi Rizzo 		 */
941f9790aebSLuigi Rizzo 		/* delete rings and buffers */
942f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
943f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
94468b8534bSLuigi Rizzo 	}
945847bf383SLuigi Rizzo 	/* possibily decrement counter of tx_si/rx_si users */
946847bf383SLuigi Rizzo 	netmap_unset_ringid(priv);
947f9790aebSLuigi Rizzo 	/* delete the nifp */
948847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, priv->np_nifp);
949847bf383SLuigi Rizzo 	/* drop the allocator */
950847bf383SLuigi Rizzo 	netmap_mem_deref(na->nm_mem, na);
951847bf383SLuigi Rizzo 	/* mark the priv as unregistered */
952847bf383SLuigi Rizzo 	priv->np_na = NULL;
953847bf383SLuigi Rizzo 	priv->np_nifp = NULL;
9545819da83SLuigi Rizzo }
95568b8534bSLuigi Rizzo 
95689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
957f0ea3689SLuigi Rizzo static __inline int
958847bf383SLuigi Rizzo nm_si_user(struct netmap_priv_d *priv, enum txrx t)
959f0ea3689SLuigi Rizzo {
960f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
961847bf383SLuigi Rizzo 		(priv->np_qlast[t] - priv->np_qfirst[t] > 1));
962f0ea3689SLuigi Rizzo }
963f0ea3689SLuigi Rizzo 
964ce3ee1e7SLuigi Rizzo /*
965*8fd44c93SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd is closed
966*8fd44c93SLuigi Rizzo  * Action: undo all the things done by NIOCREGIF,
967*8fd44c93SLuigi Rizzo  * On FreeBSD we need to track whether there are active mmap()s,
968*8fd44c93SLuigi Rizzo  * and we use np_active_mmaps for that. On linux, the field is always 0.
969*8fd44c93SLuigi Rizzo  * Return: 1 if we can free priv, 0 otherwise.
97089cc2556SLuigi Rizzo  *
971ce3ee1e7SLuigi Rizzo  */
97289cc2556SLuigi Rizzo /* call with NMG_LOCK held */
973f9790aebSLuigi Rizzo int
974ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
975ce3ee1e7SLuigi Rizzo {
976f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
977ce3ee1e7SLuigi Rizzo 
978*8fd44c93SLuigi Rizzo 	/* number of active mmaps on this fd (FreeBSD only) */
979*8fd44c93SLuigi Rizzo 	if (--priv->np_refs > 0) {
980ce3ee1e7SLuigi Rizzo 		return 0;
981ce3ee1e7SLuigi Rizzo 	}
982*8fd44c93SLuigi Rizzo 
983f9790aebSLuigi Rizzo 	if (!na) {
984f9790aebSLuigi Rizzo 		return 1; //XXX is it correct?
985ce3ee1e7SLuigi Rizzo 	}
986847bf383SLuigi Rizzo 	netmap_do_unregif(priv);
987f9790aebSLuigi Rizzo 	netmap_adapter_put(na);
988ce3ee1e7SLuigi Rizzo 	return 1;
989f196ce38SLuigi Rizzo }
9905819da83SLuigi Rizzo 
991f9790aebSLuigi Rizzo 
99289cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
993f9790aebSLuigi Rizzo void
9945819da83SLuigi Rizzo netmap_dtor(void *data)
9955819da83SLuigi Rizzo {
9965819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
997ce3ee1e7SLuigi Rizzo 	int last_instance;
9985819da83SLuigi Rizzo 
999ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
1000ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
1001ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1002ce3ee1e7SLuigi Rizzo 	if (last_instance) {
1003ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
100468b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
100568b8534bSLuigi Rizzo 	}
1006ce3ee1e7SLuigi Rizzo }
100768b8534bSLuigi Rizzo 
1008f18be576SLuigi Rizzo 
100968b8534bSLuigi Rizzo 
101068b8534bSLuigi Rizzo 
101168b8534bSLuigi Rizzo /*
101202ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
1013091fd0abSLuigi Rizzo  * Netmap has two operating modes:
1014091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
1015091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
1016091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
1017091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
1018091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
1019091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
1020091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
1021091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
1022091fd0abSLuigi Rizzo  *   to the other side.
1023091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1024091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1025091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1026091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1027091fd0abSLuigi Rizzo  */
1028091fd0abSLuigi Rizzo 
1029f18be576SLuigi Rizzo 
1030091fd0abSLuigi Rizzo /*
1031091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
103217885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1033091fd0abSLuigi Rizzo  */
1034091fd0abSLuigi Rizzo static void
1035f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1036091fd0abSLuigi Rizzo {
1037091fd0abSLuigi Rizzo 	struct mbuf *m;
1038091fd0abSLuigi Rizzo 
1039091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
1040f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1041091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
1042091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
1043091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
1044091fd0abSLuigi Rizzo 	}
1045f9790aebSLuigi Rizzo 	mbq_destroy(q);
1046091fd0abSLuigi Rizzo }
1047091fd0abSLuigi Rizzo 
1048f18be576SLuigi Rizzo 
1049091fd0abSLuigi Rizzo /*
1050091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
105117885a7bSLuigi Rizzo  * Take packets from hwcur to ring->head marked NS_FORWARD (or forced)
105217885a7bSLuigi Rizzo  * and pass them up. Drop remaining packets in the unlikely event
105317885a7bSLuigi Rizzo  * of an mbuf shortage.
1054091fd0abSLuigi Rizzo  */
1055091fd0abSLuigi Rizzo static void
1056091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1057091fd0abSLuigi Rizzo {
105817885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1059847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
106017885a7bSLuigi Rizzo 	u_int n;
1061f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1062091fd0abSLuigi Rizzo 
106317885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
106417885a7bSLuigi Rizzo 		struct mbuf *m;
1065091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1066091fd0abSLuigi Rizzo 
1067091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1068091fd0abSLuigi Rizzo 			continue;
10694bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
107017885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
1071091fd0abSLuigi Rizzo 			continue;
1072091fd0abSLuigi Rizzo 		}
1073091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
107417885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
10754bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1076091fd0abSLuigi Rizzo 
1077091fd0abSLuigi Rizzo 		if (m == NULL)
1078091fd0abSLuigi Rizzo 			break;
1079f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1080091fd0abSLuigi Rizzo 	}
1081091fd0abSLuigi Rizzo }
1082091fd0abSLuigi Rizzo 
1083f18be576SLuigi Rizzo 
1084091fd0abSLuigi Rizzo /*
108517885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
108617885a7bSLuigi Rizzo  * kring->nr_hwcur and kring->rhead
108717885a7bSLuigi Rizzo  * Called under kring->rx_queue.lock on the sw rx ring,
1088091fd0abSLuigi Rizzo  */
108917885a7bSLuigi Rizzo static u_int
1090091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1091091fd0abSLuigi Rizzo {
1092091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
109317885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
109417885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
109517885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
109617885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
109717885a7bSLuigi Rizzo 	u_int sent = 0;
1098ce3ee1e7SLuigi Rizzo 
109917885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
110017885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
110117885a7bSLuigi Rizzo 		struct netmap_kring *kdst = &na->tx_rings[i];
110217885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
110317885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1104ce3ee1e7SLuigi Rizzo 
110517885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
110617885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
110717885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1108091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
110917885a7bSLuigi Rizzo 			u_int dst_cur = rdst->cur;
111017885a7bSLuigi Rizzo 
111117885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
111217885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
111317885a7bSLuigi Rizzo 				continue;
111417885a7bSLuigi Rizzo 
111517885a7bSLuigi Rizzo 			sent++;
111617885a7bSLuigi Rizzo 
111717885a7bSLuigi Rizzo 			dst = &rdst->slot[dst_cur];
111817885a7bSLuigi Rizzo 
1119091fd0abSLuigi Rizzo 			tmp = *src;
112017885a7bSLuigi Rizzo 
1121091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1122091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1123091fd0abSLuigi Rizzo 
1124091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1125091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1126091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1127091fd0abSLuigi Rizzo 
11284bf50f18SLuigi Rizzo 			rdst->cur = nm_next(dst_cur, dst_lim);
1129091fd0abSLuigi Rizzo 		}
113017885a7bSLuigi Rizzo 		/* if (sent) XXX txsync ? */
1131091fd0abSLuigi Rizzo 	}
113217885a7bSLuigi Rizzo 	return sent;
1133091fd0abSLuigi Rizzo }
1134091fd0abSLuigi Rizzo 
1135f18be576SLuigi Rizzo 
1136091fd0abSLuigi Rizzo /*
1137ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
113802ad4083SLuigi Rizzo  * system call in user process context, and the only contention
113902ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1140091fd0abSLuigi Rizzo  * this routine concurrently.
114168b8534bSLuigi Rizzo  */
1142*8fd44c93SLuigi Rizzo static void
1143ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
114468b8534bSLuigi Rizzo {
1145d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
114617885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1147f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1148f9790aebSLuigi Rizzo 	struct mbq q;
114968b8534bSLuigi Rizzo 
115017885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
115117885a7bSLuigi Rizzo 	 * force head = cur since netmap_grab_packets() stops at head
115268b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
115368b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
115468b8534bSLuigi Rizzo 	 */
1155f9790aebSLuigi Rizzo 	mbq_init(&q);
115617885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
115717885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
115817885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
115917885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
116017885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
116117885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
116268b8534bSLuigi Rizzo 
1163f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
1164f18be576SLuigi Rizzo }
1165f18be576SLuigi Rizzo 
1166f18be576SLuigi Rizzo 
116768b8534bSLuigi Rizzo /*
116802ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
116917885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
117017885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
117102ad4083SLuigi Rizzo  *
11724bf50f18SLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
11734bf50f18SLuigi Rizzo  * (we know because td != NULL).
11744bf50f18SLuigi Rizzo  *
11754bf50f18SLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
11764bf50f18SLuigi Rizzo  *     as an additional hidden argument.
117717885a7bSLuigi Rizzo  * returns the number of packets delivered to tx queues in
117817885a7bSLuigi Rizzo  * transparent mode, or a negative value if error
117968b8534bSLuigi Rizzo  */
1180*8fd44c93SLuigi Rizzo static int
1181ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
118268b8534bSLuigi Rizzo {
1183d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
118468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
118517885a7bSLuigi Rizzo 	u_int nm_i, n;
118617885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1187f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
118817885a7bSLuigi Rizzo 	int ret = 0;
1189847bf383SLuigi Rizzo 	struct mbq *q = &kring->rx_queue, fq;
119068b8534bSLuigi Rizzo 
119101c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
1192f0ea3689SLuigi Rizzo 	(void)td;
119317885a7bSLuigi Rizzo 
1194847bf383SLuigi Rizzo 	mbq_init(&fq); /* fq holds packets to be freed */
1195847bf383SLuigi Rizzo 
1196997b054cSLuigi Rizzo 	mbq_lock(q);
119717885a7bSLuigi Rizzo 
119817885a7bSLuigi Rizzo 	/* First part: import newly received packets */
119917885a7bSLuigi Rizzo 	n = mbq_len(q);
120017885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
120117885a7bSLuigi Rizzo 		struct mbuf *m;
120217885a7bSLuigi Rizzo 		uint32_t stop_i;
120317885a7bSLuigi Rizzo 
120417885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
120517885a7bSLuigi Rizzo 		stop_i = nm_prev(nm_i, lim);
120617885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
120717885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
120817885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
120917885a7bSLuigi Rizzo 
12104bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
121117885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
121217885a7bSLuigi Rizzo 			if (netmap_verbose)
12134bf50f18SLuigi Rizzo                                 D("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
121417885a7bSLuigi Rizzo 
121517885a7bSLuigi Rizzo 			slot->len = len;
121617885a7bSLuigi Rizzo 			slot->flags = kring->nkr_slot_flags;
121717885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
1218847bf383SLuigi Rizzo 			mbq_enqueue(&fq, m);
121964ae02c3SLuigi Rizzo 		}
122017885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
122164ae02c3SLuigi Rizzo 	}
122217885a7bSLuigi Rizzo 
122317885a7bSLuigi Rizzo 	/*
122417885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
122517885a7bSLuigi Rizzo 	 */
122617885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
122717885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
122817885a7bSLuigi Rizzo 		if (netmap_fwd || kring->ring->flags & NR_FORWARD)
122917885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
123017885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
123164ae02c3SLuigi Rizzo 	}
123217885a7bSLuigi Rizzo 
12334bf50f18SLuigi Rizzo 	/* access copies of cur,tail in the kring */
12344bf50f18SLuigi Rizzo 	if (kring->rcur == kring->rtail && td) /* no bufs available */
12350e73f29aSLuigi Rizzo 		OS_selrecord(td, &kring->si);
12364bf50f18SLuigi Rizzo 
1237997b054cSLuigi Rizzo 	mbq_unlock(q);
1238847bf383SLuigi Rizzo 
1239847bf383SLuigi Rizzo 	mbq_purge(&fq);
1240847bf383SLuigi Rizzo 	mbq_destroy(&fq);
1241847bf383SLuigi Rizzo 
124217885a7bSLuigi Rizzo 	return ret;
124368b8534bSLuigi Rizzo }
124468b8534bSLuigi Rizzo 
124568b8534bSLuigi Rizzo 
1246f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1247f9790aebSLuigi Rizzo  *
1248f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1249f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1250f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1251f9790aebSLuigi Rizzo  *
1252f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1253f9790aebSLuigi Rizzo  * EBUSY.
1254f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1255f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1256f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1257f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1258f9790aebSLuigi Rizzo  *
1259f9790aebSLuigi Rizzo  *					native_support
1260f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1261f9790aebSLuigi Rizzo  * -------------------------------------------------------
1262f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1263f9790aebSLuigi Rizzo  *
1264f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1265f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1266f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1267f9790aebSLuigi Rizzo  *
1268f9790aebSLuigi Rizzo  */
1269f9790aebSLuigi Rizzo 
1270f9790aebSLuigi Rizzo int
1271f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
1272f9790aebSLuigi Rizzo {
1273f9790aebSLuigi Rizzo 	/* generic support */
1274f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1275f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1276847bf383SLuigi Rizzo #ifdef WITH_GENERIC
1277f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1278847bf383SLuigi Rizzo 	int error = 0;
1279847bf383SLuigi Rizzo #endif
1280f9790aebSLuigi Rizzo 
1281f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1282f9790aebSLuigi Rizzo 
1283f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1284f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1285f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1286f9790aebSLuigi Rizzo 
1287f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
12884bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1289f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1290f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1291f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1292f9790aebSLuigi Rizzo 		 * adapters.
1293f9790aebSLuigi Rizzo 		 */
12944bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
12954bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
12964bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
12974bf50f18SLuigi Rizzo #ifdef WITH_PIPES
12984bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
12994bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
13004bf50f18SLuigi Rizzo 			 */
13014bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
13024bf50f18SLuigi Rizzo #endif
13034bf50f18SLuigi Rizzo 		) {
13044bf50f18SLuigi Rizzo 			*na = prev_na;
1305f9790aebSLuigi Rizzo 			return 0;
1306f9790aebSLuigi Rizzo 		}
1307f9790aebSLuigi Rizzo 	}
1308f9790aebSLuigi Rizzo 
1309f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1310f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1311f9790aebSLuigi Rizzo 	 */
1312f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1313f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1314f9790aebSLuigi Rizzo 
1315847bf383SLuigi Rizzo #ifdef WITH_GENERIC
1316f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1317f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1318f9790aebSLuigi Rizzo 	 *
1319f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1320f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1321f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1322f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1323f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1324f9790aebSLuigi Rizzo 	 * must be that
1325f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1326f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1327f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1328f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1329f9790aebSLuigi Rizzo 	 */
1330f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1331f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1332f9790aebSLuigi Rizzo 	if (error)
1333f9790aebSLuigi Rizzo 		return error;
1334f9790aebSLuigi Rizzo 
1335f9790aebSLuigi Rizzo 	*na = NA(ifp);
1336f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1337f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1338f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1339f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1340f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1341f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1342f9790aebSLuigi Rizzo 	}
134317885a7bSLuigi Rizzo 	ND("Created generic NA %p (prev %p)", gna, gna->prev);
1344f9790aebSLuigi Rizzo 
1345f9790aebSLuigi Rizzo 	return 0;
1346847bf383SLuigi Rizzo #else /* !WITH_GENERIC */
1347847bf383SLuigi Rizzo 	return EOPNOTSUPP;
1348847bf383SLuigi Rizzo #endif
1349f9790aebSLuigi Rizzo }
1350f9790aebSLuigi Rizzo 
1351f9790aebSLuigi Rizzo 
135268b8534bSLuigi Rizzo /*
1353ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1354ce3ee1e7SLuigi Rizzo  *
1355f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
1356f2637526SLuigi Rizzo  * to the interface specified by nmr.
1357ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1358ce3ee1e7SLuigi Rizzo  *
1359f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1360f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1361f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1362f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1363f2637526SLuigi Rizzo  * could not be allocated.
1364f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1365f18be576SLuigi Rizzo  *
1366f2637526SLuigi Rizzo  * No reference is kept on the real interface, which may then
1367f2637526SLuigi Rizzo  * disappear at any time.
136868b8534bSLuigi Rizzo  */
1369f9790aebSLuigi Rizzo int
1370f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
137168b8534bSLuigi Rizzo {
1372f0ea3689SLuigi Rizzo 	struct ifnet *ifp = NULL;
1373f9790aebSLuigi Rizzo 	int error = 0;
1374f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1375f9790aebSLuigi Rizzo 
1376f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1377f196ce38SLuigi Rizzo 
1378ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1379ce3ee1e7SLuigi Rizzo 
13804bf50f18SLuigi Rizzo 	/* we cascade through all possibile types of netmap adapter.
13814bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
13824bf50f18SLuigi Rizzo 	 * with the following combinations:
13834bf50f18SLuigi Rizzo 	 *
13844bf50f18SLuigi Rizzo 	 * error    na
13854bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
13864bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
13874bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
13884bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
13894bf50f18SLuigi Rizzo 	 */
13904bf50f18SLuigi Rizzo 
13914bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
13924bf50f18SLuigi Rizzo 	error = netmap_get_monitor_na(nmr, na, create);
13934bf50f18SLuigi Rizzo 	if (error || *na != NULL)
13944bf50f18SLuigi Rizzo 		return error;
13954bf50f18SLuigi Rizzo 
13964bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
1397f0ea3689SLuigi Rizzo 	error = netmap_get_pipe_na(nmr, na, create);
1398f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1399f9790aebSLuigi Rizzo 		return error;
1400ce3ee1e7SLuigi Rizzo 
14014bf50f18SLuigi Rizzo 	/* try to see if this is a bridge port */
1402f0ea3689SLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1403f0ea3689SLuigi Rizzo 	if (error)
1404f0ea3689SLuigi Rizzo 		return error;
1405f0ea3689SLuigi Rizzo 
1406f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1407847bf383SLuigi Rizzo 		goto out;
1408f0ea3689SLuigi Rizzo 
140989cc2556SLuigi Rizzo 	/*
141089cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
141189cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
141289cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
141389cc2556SLuigi Rizzo 	 * persistent VALE port.
141489cc2556SLuigi Rizzo 	 */
1415f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1416f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1417ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1418f196ce38SLuigi Rizzo 	}
1419ce3ee1e7SLuigi Rizzo 
1420f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1421f9790aebSLuigi Rizzo 	if (error)
1422f9790aebSLuigi Rizzo 		goto out;
1423f18be576SLuigi Rizzo 
1424f9790aebSLuigi Rizzo 	*na = ret;
1425f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1426f0ea3689SLuigi Rizzo 
1427f9790aebSLuigi Rizzo out:
1428f0ea3689SLuigi Rizzo 	if (error && ret != NULL)
1429f0ea3689SLuigi Rizzo 		netmap_adapter_put(ret);
1430f0ea3689SLuigi Rizzo 
1431f0ea3689SLuigi Rizzo 	if (ifp)
143289cc2556SLuigi Rizzo 		if_rele(ifp); /* allow live unloading of drivers modules */
1433f18be576SLuigi Rizzo 
14345ab0d24dSLuigi Rizzo 	return error;
14355ab0d24dSLuigi Rizzo }
1436ce3ee1e7SLuigi Rizzo 
1437ce3ee1e7SLuigi Rizzo 
1438f9790aebSLuigi Rizzo /*
1439f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1440f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
144117885a7bSLuigi Rizzo  * in case of error.
1442f9790aebSLuigi Rizzo  *
144317885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
144417885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1445f9790aebSLuigi Rizzo  *
144617885a7bSLuigi Rizzo  * We want
144717885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1448f9790aebSLuigi Rizzo  *
144917885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1450f9790aebSLuigi Rizzo  */
1451847bf383SLuigi Rizzo static u_int
145217885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring)
1453f9790aebSLuigi Rizzo {
1454847bf383SLuigi Rizzo #define NM_ASSERT(t) if (t) { D("fail " #t); goto error; }
1455f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
145617885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1457f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1458f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1459ce3ee1e7SLuigi Rizzo 
146017885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
146117885a7bSLuigi Rizzo 		kring->name,
146217885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
146317885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
146417885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
146517885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->rhead >= n ||
146617885a7bSLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n)
1467f9790aebSLuigi Rizzo 		goto error;
1468f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
146917885a7bSLuigi Rizzo 	/*
147017885a7bSLuigi Rizzo 	 * user sanity checks. We only use 'cur',
147117885a7bSLuigi Rizzo 	 * A, B, ... are possible positions for cur:
147217885a7bSLuigi Rizzo 	 *
147317885a7bSLuigi Rizzo 	 *  0    A  cur   B  tail  C  n-1
147417885a7bSLuigi Rizzo 	 *  0    D  tail  E  cur   F  n-1
147517885a7bSLuigi Rizzo 	 *
147617885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
147717885a7bSLuigi Rizzo 	 */
147817885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
147917885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
1480847bf383SLuigi Rizzo 		NM_ASSERT(head < kring->rhead || head > kring->rtail);
148117885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
1482847bf383SLuigi Rizzo 		NM_ASSERT(cur < head || cur > kring->rtail);
148317885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
148417885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
1485847bf383SLuigi Rizzo 		NM_ASSERT(head > kring->rtail && head < kring->rhead);
148617885a7bSLuigi Rizzo 
148717885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
148817885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
148917885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
1490847bf383SLuigi Rizzo 			NM_ASSERT(cur < head || cur > kring->rtail);
149117885a7bSLuigi Rizzo 		} else { /* head >= rhead */
149217885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
1493847bf383SLuigi Rizzo 			NM_ASSERT(cur > kring->rtail && cur < head);
1494f18be576SLuigi Rizzo 		}
1495f9790aebSLuigi Rizzo 	}
149617885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
149717885a7bSLuigi Rizzo 		RD(5, "tail overwritten was %d need %d",
149817885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
149917885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
150017885a7bSLuigi Rizzo 	}
150117885a7bSLuigi Rizzo 	kring->rhead = head;
150217885a7bSLuigi Rizzo 	kring->rcur = cur;
150317885a7bSLuigi Rizzo 	return head;
1504f9790aebSLuigi Rizzo 
1505f9790aebSLuigi Rizzo error:
1506847bf383SLuigi Rizzo 	RD(5, "%s kring error: head %d cur %d tail %d rhead %d rcur %d rtail %d hwcur %d hwtail %d",
150717885a7bSLuigi Rizzo 		kring->name,
1508847bf383SLuigi Rizzo 		head, cur, ring->tail,
1509847bf383SLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail,
1510847bf383SLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail);
1511f9790aebSLuigi Rizzo 	return n;
1512847bf383SLuigi Rizzo #undef NM_ASSERT
151368b8534bSLuigi Rizzo }
151468b8534bSLuigi Rizzo 
151568b8534bSLuigi Rizzo 
151668b8534bSLuigi Rizzo /*
1517f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
151817885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1519f9790aebSLuigi Rizzo  *
152017885a7bSLuigi Rizzo  * For a valid configuration,
152117885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1522f9790aebSLuigi Rizzo  *
152317885a7bSLuigi Rizzo  * We only consider head and cur.
152417885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1525f9790aebSLuigi Rizzo  *
1526f9790aebSLuigi Rizzo  */
1527847bf383SLuigi Rizzo static u_int
152817885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring)
1529f9790aebSLuigi Rizzo {
1530f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
153117885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
153217885a7bSLuigi Rizzo 	uint32_t head, cur;
1533f9790aebSLuigi Rizzo 
1534847bf383SLuigi Rizzo 	ND(5,"%s kc %d kt %d h %d c %d t %d",
153517885a7bSLuigi Rizzo 		kring->name,
153617885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
153717885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
153817885a7bSLuigi Rizzo 	/*
153917885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
154017885a7bSLuigi Rizzo 	 * move backwards. However:
154117885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
154217885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
154317885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
154417885a7bSLuigi Rizzo 	 */
154517885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
154617885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1547f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
154817885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwtail >= n)
1549f9790aebSLuigi Rizzo 		goto error;
1550f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1551f9790aebSLuigi Rizzo 	/* user sanity checks */
155217885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
155317885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
155417885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur || head > kring->nr_hwtail)
1555f9790aebSLuigi Rizzo 			goto error;
155617885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
155717885a7bSLuigi Rizzo 		if (cur < head || cur > kring->nr_hwtail)
1558f9790aebSLuigi Rizzo 			goto error;
1559f9790aebSLuigi Rizzo 	} else {
156017885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
156117885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur && head > kring->nr_hwtail)
1562f9790aebSLuigi Rizzo 			goto error;
156317885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
156417885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
156517885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
156617885a7bSLuigi Rizzo 			if (cur < head || cur > kring->nr_hwtail)
1567f9790aebSLuigi Rizzo 				goto error;
156817885a7bSLuigi Rizzo 		} else {
156917885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
157017885a7bSLuigi Rizzo 			if (cur < head && cur > kring->nr_hwtail)
1571f9790aebSLuigi Rizzo 				goto error;
1572f9790aebSLuigi Rizzo 		}
1573f9790aebSLuigi Rizzo 	}
157417885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
157517885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
157617885a7bSLuigi Rizzo 			kring->name,
157717885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
157817885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
157917885a7bSLuigi Rizzo 	}
158017885a7bSLuigi Rizzo 	return head;
1581f9790aebSLuigi Rizzo 
1582f9790aebSLuigi Rizzo error:
158317885a7bSLuigi Rizzo 	RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d",
1584f9790aebSLuigi Rizzo 		kring->nr_hwcur,
158517885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
158617885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, ring->tail);
1587f9790aebSLuigi Rizzo 	return n;
1588f9790aebSLuigi Rizzo }
1589f9790aebSLuigi Rizzo 
159017885a7bSLuigi Rizzo 
1591f9790aebSLuigi Rizzo /*
159268b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
159317885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
159468b8534bSLuigi Rizzo  * Return 1 on reinit.
1595506cc70cSLuigi Rizzo  *
1596506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1597506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
159817885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1599506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1600506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1601506cc70cSLuigi Rizzo  * it under lock.
160268b8534bSLuigi Rizzo  */
160368b8534bSLuigi Rizzo int
160468b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
160568b8534bSLuigi Rizzo {
160668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
160768b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
160868b8534bSLuigi Rizzo 	int errors = 0;
160968b8534bSLuigi Rizzo 
1610ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
16114bf50f18SLuigi Rizzo 	RD(10, "called for %s", kring->name);
161217885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
161317885a7bSLuigi Rizzo 	kring->rhead = ring->head;
161417885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
161517885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
161617885a7bSLuigi Rizzo 
161768b8534bSLuigi Rizzo 	if (ring->cur > lim)
161868b8534bSLuigi Rizzo 		errors++;
161917885a7bSLuigi Rizzo 	if (ring->head > lim)
162017885a7bSLuigi Rizzo 		errors++;
162117885a7bSLuigi Rizzo 	if (ring->tail > lim)
162217885a7bSLuigi Rizzo 		errors++;
162368b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
162468b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
162568b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
1626847bf383SLuigi Rizzo 		if (idx < 2 || idx >= kring->na->na_lut.objtotal) {
162717885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
162868b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
162968b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
16304bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
163168b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
163217885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
163368b8534bSLuigi Rizzo 		}
163468b8534bSLuigi Rizzo 	}
163568b8534bSLuigi Rizzo 	if (errors) {
16368241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
163717885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
163817885a7bSLuigi Rizzo 			kring->name,
163968b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
164017885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
164117885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
164217885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
164317885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
164468b8534bSLuigi Rizzo 	}
164568b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
164668b8534bSLuigi Rizzo }
164768b8534bSLuigi Rizzo 
16484bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
16494bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
16504bf50f18SLuigi Rizzo  *
16514bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
16524bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
16534bf50f18SLuigi Rizzo  *
165468b8534bSLuigi Rizzo  */
16554bf50f18SLuigi Rizzo int
16564bf50f18SLuigi Rizzo netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
165768b8534bSLuigi Rizzo {
1658f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1659f0ea3689SLuigi Rizzo 	u_int j, i = ringid & NETMAP_RING_MASK;
1660f0ea3689SLuigi Rizzo 	u_int reg = flags & NR_REG_MASK;
1661847bf383SLuigi Rizzo 	enum txrx t;
166268b8534bSLuigi Rizzo 
1663f0ea3689SLuigi Rizzo 	if (reg == NR_REG_DEFAULT) {
1664f0ea3689SLuigi Rizzo 		/* convert from old ringid to flags */
166568b8534bSLuigi Rizzo 		if (ringid & NETMAP_SW_RING) {
1666f0ea3689SLuigi Rizzo 			reg = NR_REG_SW;
166768b8534bSLuigi Rizzo 		} else if (ringid & NETMAP_HW_RING) {
1668f0ea3689SLuigi Rizzo 			reg = NR_REG_ONE_NIC;
166968b8534bSLuigi Rizzo 		} else {
1670f0ea3689SLuigi Rizzo 			reg = NR_REG_ALL_NIC;
1671f0ea3689SLuigi Rizzo 		}
1672f0ea3689SLuigi Rizzo 		D("deprecated API, old ringid 0x%x -> ringid %x reg %d", ringid, i, reg);
1673f0ea3689SLuigi Rizzo 	}
1674f0ea3689SLuigi Rizzo 	switch (reg) {
1675f0ea3689SLuigi Rizzo 	case NR_REG_ALL_NIC:
1676f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_MASTER:
1677f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_SLAVE:
1678847bf383SLuigi Rizzo 		for_rx_tx(t) {
1679847bf383SLuigi Rizzo 			priv->np_qfirst[t] = 0;
1680847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t);
1681847bf383SLuigi Rizzo 		}
1682f0ea3689SLuigi Rizzo 		ND("%s %d %d", "ALL/PIPE",
1683847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX], priv->np_qlast[NR_RX]);
1684f0ea3689SLuigi Rizzo 		break;
1685f0ea3689SLuigi Rizzo 	case NR_REG_SW:
1686f0ea3689SLuigi Rizzo 	case NR_REG_NIC_SW:
1687f0ea3689SLuigi Rizzo 		if (!(na->na_flags & NAF_HOST_RINGS)) {
1688f0ea3689SLuigi Rizzo 			D("host rings not supported");
1689f0ea3689SLuigi Rizzo 			return EINVAL;
1690f0ea3689SLuigi Rizzo 		}
1691847bf383SLuigi Rizzo 		for_rx_tx(t) {
1692847bf383SLuigi Rizzo 			priv->np_qfirst[t] = (reg == NR_REG_SW ?
1693847bf383SLuigi Rizzo 				nma_get_nrings(na, t) : 0);
1694847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t) + 1;
1695847bf383SLuigi Rizzo 		}
1696f0ea3689SLuigi Rizzo 		ND("%s %d %d", reg == NR_REG_SW ? "SW" : "NIC+SW",
1697847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX], priv->np_qlast[NR_RX]);
1698f0ea3689SLuigi Rizzo 		break;
1699f0ea3689SLuigi Rizzo 	case NR_REG_ONE_NIC:
1700f0ea3689SLuigi Rizzo 		if (i >= na->num_tx_rings && i >= na->num_rx_rings) {
1701f0ea3689SLuigi Rizzo 			D("invalid ring id %d", i);
1702f0ea3689SLuigi Rizzo 			return EINVAL;
1703f0ea3689SLuigi Rizzo 		}
1704847bf383SLuigi Rizzo 		for_rx_tx(t) {
1705f0ea3689SLuigi Rizzo 			/* if not enough rings, use the first one */
1706f0ea3689SLuigi Rizzo 			j = i;
1707847bf383SLuigi Rizzo 			if (j >= nma_get_nrings(na, t))
1708f0ea3689SLuigi Rizzo 				j = 0;
1709847bf383SLuigi Rizzo 			priv->np_qfirst[t] = j;
1710847bf383SLuigi Rizzo 			priv->np_qlast[t] = j + 1;
1711847bf383SLuigi Rizzo 		}
1712f0ea3689SLuigi Rizzo 		break;
1713f0ea3689SLuigi Rizzo 	default:
1714f0ea3689SLuigi Rizzo 		D("invalid regif type %d", reg);
1715f0ea3689SLuigi Rizzo 		return EINVAL;
171668b8534bSLuigi Rizzo 	}
1717f0ea3689SLuigi Rizzo 	priv->np_flags = (flags & ~NR_REG_MASK) | reg;
17184bf50f18SLuigi Rizzo 
1719ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1720f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
17214bf50f18SLuigi Rizzo 			na->name,
1722847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1723847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1724847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1725847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX],
1726f0ea3689SLuigi Rizzo 			i);
1727ae10d1afSLuigi Rizzo 	}
172868b8534bSLuigi Rizzo 	return 0;
172968b8534bSLuigi Rizzo }
173068b8534bSLuigi Rizzo 
17314bf50f18SLuigi Rizzo 
17324bf50f18SLuigi Rizzo /*
17334bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
17344bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
17354bf50f18SLuigi Rizzo  */
17364bf50f18SLuigi Rizzo static int
17374bf50f18SLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
17384bf50f18SLuigi Rizzo {
17394bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
17404bf50f18SLuigi Rizzo 	int error;
1741847bf383SLuigi Rizzo 	enum txrx t;
17424bf50f18SLuigi Rizzo 
17434bf50f18SLuigi Rizzo 	error = netmap_interp_ringid(priv, ringid, flags);
17444bf50f18SLuigi Rizzo 	if (error) {
17454bf50f18SLuigi Rizzo 		return error;
17464bf50f18SLuigi Rizzo 	}
17474bf50f18SLuigi Rizzo 
17484bf50f18SLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
17494bf50f18SLuigi Rizzo 
17504bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
17514bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
17524bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
17534bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
17544bf50f18SLuigi Rizzo 	 */
1755847bf383SLuigi Rizzo 	for_rx_tx(t) {
1756847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1757847bf383SLuigi Rizzo 			na->si_users[t]++;
1758847bf383SLuigi Rizzo 	}
17594bf50f18SLuigi Rizzo 	return 0;
17604bf50f18SLuigi Rizzo }
17614bf50f18SLuigi Rizzo 
1762847bf383SLuigi Rizzo static void
1763847bf383SLuigi Rizzo netmap_unset_ringid(struct netmap_priv_d *priv)
1764847bf383SLuigi Rizzo {
1765847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1766847bf383SLuigi Rizzo 	enum txrx t;
1767847bf383SLuigi Rizzo 
1768847bf383SLuigi Rizzo 	for_rx_tx(t) {
1769847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1770847bf383SLuigi Rizzo 			na->si_users[t]--;
1771847bf383SLuigi Rizzo 		priv->np_qfirst[t] = priv->np_qlast[t] = 0;
1772847bf383SLuigi Rizzo 	}
1773847bf383SLuigi Rizzo 	priv->np_flags = 0;
1774847bf383SLuigi Rizzo 	priv->np_txpoll = 0;
1775847bf383SLuigi Rizzo }
1776847bf383SLuigi Rizzo 
1777847bf383SLuigi Rizzo 
1778847bf383SLuigi Rizzo /* check that the rings we want to bind are not exclusively owned by a previous
1779847bf383SLuigi Rizzo  * bind.  If exclusive ownership has been requested, we also mark the rings.
1780847bf383SLuigi Rizzo  */
1781847bf383SLuigi Rizzo static int
1782847bf383SLuigi Rizzo netmap_get_exclusive(struct netmap_priv_d *priv)
1783847bf383SLuigi Rizzo {
1784847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1785847bf383SLuigi Rizzo 	u_int i;
1786847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1787847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
1788847bf383SLuigi Rizzo 	enum txrx t;
1789847bf383SLuigi Rizzo 
1790847bf383SLuigi Rizzo 	ND("%s: grabbing tx [%d, %d) rx [%d, %d)",
1791847bf383SLuigi Rizzo 			na->name,
1792847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1793847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1794847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1795847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX]);
1796847bf383SLuigi Rizzo 
1797847bf383SLuigi Rizzo 	/* first round: check that all the requested rings
1798847bf383SLuigi Rizzo 	 * are neither alread exclusively owned, nor we
1799847bf383SLuigi Rizzo 	 * want exclusive ownership when they are already in use
1800847bf383SLuigi Rizzo 	 */
1801847bf383SLuigi Rizzo 	for_rx_tx(t) {
1802847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
1803847bf383SLuigi Rizzo 			kring = &NMR(na, t)[i];
1804847bf383SLuigi Rizzo 			if ((kring->nr_kflags & NKR_EXCLUSIVE) ||
1805847bf383SLuigi Rizzo 			    (kring->users && excl))
1806847bf383SLuigi Rizzo 			{
1807847bf383SLuigi Rizzo 				ND("ring %s busy", kring->name);
1808847bf383SLuigi Rizzo 				return EBUSY;
1809847bf383SLuigi Rizzo 			}
1810847bf383SLuigi Rizzo 		}
1811847bf383SLuigi Rizzo 	}
1812847bf383SLuigi Rizzo 
1813847bf383SLuigi Rizzo 	/* second round: increment usage cound and possibly
1814847bf383SLuigi Rizzo 	 * mark as exclusive
1815847bf383SLuigi Rizzo 	 */
1816847bf383SLuigi Rizzo 
1817847bf383SLuigi Rizzo 	for_rx_tx(t) {
1818847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
1819847bf383SLuigi Rizzo 			kring = &NMR(na, t)[i];
1820847bf383SLuigi Rizzo 			kring->users++;
1821847bf383SLuigi Rizzo 			if (excl)
1822847bf383SLuigi Rizzo 				kring->nr_kflags |= NKR_EXCLUSIVE;
1823847bf383SLuigi Rizzo 		}
1824847bf383SLuigi Rizzo 	}
1825847bf383SLuigi Rizzo 
1826847bf383SLuigi Rizzo 	return 0;
1827847bf383SLuigi Rizzo 
1828847bf383SLuigi Rizzo }
1829847bf383SLuigi Rizzo 
1830847bf383SLuigi Rizzo /* undo netmap_get_ownership() */
1831847bf383SLuigi Rizzo static void
1832847bf383SLuigi Rizzo netmap_rel_exclusive(struct netmap_priv_d *priv)
1833847bf383SLuigi Rizzo {
1834847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1835847bf383SLuigi Rizzo 	u_int i;
1836847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1837847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
1838847bf383SLuigi Rizzo 	enum txrx t;
1839847bf383SLuigi Rizzo 
1840847bf383SLuigi Rizzo 	ND("%s: releasing tx [%d, %d) rx [%d, %d)",
1841847bf383SLuigi Rizzo 			na->name,
1842847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1843847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1844847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1845847bf383SLuigi Rizzo 			priv->np_qlast[MR_RX]);
1846847bf383SLuigi Rizzo 
1847847bf383SLuigi Rizzo 
1848847bf383SLuigi Rizzo 	for_rx_tx(t) {
1849847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
1850847bf383SLuigi Rizzo 			kring = &NMR(na, t)[i];
1851847bf383SLuigi Rizzo 			if (excl)
1852847bf383SLuigi Rizzo 				kring->nr_kflags &= ~NKR_EXCLUSIVE;
1853847bf383SLuigi Rizzo 			kring->users--;
1854847bf383SLuigi Rizzo 		}
1855847bf383SLuigi Rizzo 	}
1856847bf383SLuigi Rizzo }
1857847bf383SLuigi Rizzo 
1858f18be576SLuigi Rizzo /*
1859f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1860f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1861ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
18624bf50f18SLuigi Rizzo  *
18634bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
18644bf50f18SLuigi Rizzo  *
18654bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
18664bf50f18SLuigi Rizzo  * (get current number and size of rings)
18674bf50f18SLuigi Rizzo  *
18684bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
18694bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
18704bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
18714bf50f18SLuigi Rizzo  *
18724bf50f18SLuigi Rizzo  *
1873847bf383SLuigi Rizzo  * na->nm_krings_create()
18744bf50f18SLuigi Rizzo  * (create and init the krings array)
18754bf50f18SLuigi Rizzo  *
18764bf50f18SLuigi Rizzo  * 	One of the following:
18774bf50f18SLuigi Rizzo  *
18784bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
18794bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
18804bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
18814bf50f18SLuigi Rizzo  *
18824bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
18834bf50f18SLuigi Rizzo  * 		add leases and scratchpads
18844bf50f18SLuigi Rizzo  *
18854bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
18864bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
18874bf50f18SLuigi Rizzo  * 		cross-link them
18884bf50f18SLuigi Rizzo  *
18894bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
18904bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
18914bf50f18SLuigi Rizzo  *
18924bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
18934bf50f18SLuigi Rizzo  *      	create both the brap krings array,
18944bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
18954bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
18964bf50f18SLuigi Rizzo  *
18974bf50f18SLuigi Rizzo  * na->nm_register(, 1)
18984bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
18994bf50f18SLuigi Rizzo  *
19004bf50f18SLuigi Rizzo  * 	This may be one of the following:
19014bf50f18SLuigi Rizzo  * 	(XXX these should be either all *_register or all *_reg 2014-03-15)
19024bf50f18SLuigi Rizzo  *
19034bf50f18SLuigi Rizzo  * 	* netmap_hw_register				(hw ports)
19044bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
19054bf50f18SLuigi Rizzo  * 		the hardware specific callback;
19064bf50f18SLuigi Rizzo  *
19074bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
19084bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
19094bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
19104bf50f18SLuigi Rizzo  *		bridge write lock.
19114bf50f18SLuigi Rizzo  *
19124bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
19134bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
19144bf50f18SLuigi Rizzo  *		longer responsibile for the lifetime of this
19154bf50f18SLuigi Rizzo  *		pipe end
19164bf50f18SLuigi Rizzo  *
19174bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
19184bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
19194bf50f18SLuigi Rizzo  *		rings
19204bf50f18SLuigi Rizzo  *
19214bf50f18SLuigi Rizzo  *	* netmap_bwrap_register				(bwraps)
19224bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
19234bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
19244bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
19254bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
19264bf50f18SLuigi Rizzo  *
19274bf50f18SLuigi Rizzo  *
1928f18be576SLuigi Rizzo  */
1929847bf383SLuigi Rizzo int
1930f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1931847bf383SLuigi Rizzo 	uint16_t ringid, uint32_t flags)
1932f18be576SLuigi Rizzo {
1933f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1934847bf383SLuigi Rizzo 	int error;
1935f18be576SLuigi Rizzo 
1936ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1937f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1938f18be576SLuigi Rizzo 	netmap_update_config(na);
1939f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1940f0ea3689SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid, flags);
1941f18be576SLuigi Rizzo 	if (error)
1942847bf383SLuigi Rizzo 		goto err;
1943847bf383SLuigi Rizzo 	error = netmap_mem_finalize(na->nm_mem, na);
1944ce3ee1e7SLuigi Rizzo 	if (error)
1945847bf383SLuigi Rizzo 		goto err;
1946847bf383SLuigi Rizzo 
1947847bf383SLuigi Rizzo 	if (na->active_fds == 0) {
1948847bf383SLuigi Rizzo 		/*
1949847bf383SLuigi Rizzo 		 * If this is the first registration of the adapter,
1950847bf383SLuigi Rizzo 		 * also create the netmap rings and their in-kernel view,
1951847bf383SLuigi Rizzo 		 * the netmap krings.
1952847bf383SLuigi Rizzo 		 */
1953847bf383SLuigi Rizzo 
1954847bf383SLuigi Rizzo 		/*
1955847bf383SLuigi Rizzo 		 * Depending on the adapter, this may also create
1956847bf383SLuigi Rizzo 		 * the netmap rings themselves
1957847bf383SLuigi Rizzo 		 */
1958847bf383SLuigi Rizzo 		error = na->nm_krings_create(na);
1959847bf383SLuigi Rizzo 		if (error)
1960847bf383SLuigi Rizzo 			goto err_drop_mem;
1961847bf383SLuigi Rizzo 
1962847bf383SLuigi Rizzo 		/* create all missing netmap rings */
1963847bf383SLuigi Rizzo 		error = netmap_mem_rings_create(na);
1964847bf383SLuigi Rizzo 		if (error)
1965847bf383SLuigi Rizzo 			goto err_del_krings;
1966ce3ee1e7SLuigi Rizzo 	}
1967847bf383SLuigi Rizzo 
1968847bf383SLuigi Rizzo 	/* now the kring must exist and we can check whether some
1969847bf383SLuigi Rizzo 	 * previous bind has exclusive ownership on them
1970847bf383SLuigi Rizzo 	 */
1971847bf383SLuigi Rizzo 	error = netmap_get_exclusive(priv);
1972847bf383SLuigi Rizzo 	if (error)
1973847bf383SLuigi Rizzo 		goto err_del_rings;
1974847bf383SLuigi Rizzo 
1975847bf383SLuigi Rizzo 	/* in all cases, create a new netmap if */
1976847bf383SLuigi Rizzo 	nifp = netmap_mem_if_new(na);
1977847bf383SLuigi Rizzo 	if (nifp == NULL) {
1978f18be576SLuigi Rizzo 		error = ENOMEM;
1979847bf383SLuigi Rizzo 		goto err_rel_excl;
1980ce3ee1e7SLuigi Rizzo 	}
1981847bf383SLuigi Rizzo 
1982f9790aebSLuigi Rizzo 	na->active_fds++;
19834bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
19844bf50f18SLuigi Rizzo 		/* Netmap not active, set the card in netmap mode
1985f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1986ce3ee1e7SLuigi Rizzo 		 */
198789cc2556SLuigi Rizzo 		/* cache the allocator info in the na */
1988847bf383SLuigi Rizzo 		netmap_mem_get_lut(na->nm_mem, &na->na_lut);
1989847bf383SLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut.lut);
1990f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1991847bf383SLuigi Rizzo 		if (error)
1992847bf383SLuigi Rizzo 			goto err_del_if;
1993f18be576SLuigi Rizzo 	}
1994847bf383SLuigi Rizzo 
1995ce3ee1e7SLuigi Rizzo 	/*
1996847bf383SLuigi Rizzo 	 * advertise that the interface is ready by setting np_nifp.
1997847bf383SLuigi Rizzo 	 * The barrier is needed because readers (poll, *SYNC and mmap)
1998ce3ee1e7SLuigi Rizzo 	 * check for priv->np_nifp != NULL without locking
1999ce3ee1e7SLuigi Rizzo 	 */
2000847bf383SLuigi Rizzo 	mb(); /* make sure previous writes are visible to all CPUs */
2001ce3ee1e7SLuigi Rizzo 	priv->np_nifp = nifp;
2002847bf383SLuigi Rizzo 
2003847bf383SLuigi Rizzo 	return 0;
2004847bf383SLuigi Rizzo 
2005847bf383SLuigi Rizzo err_del_if:
2006847bf383SLuigi Rizzo 	memset(&na->na_lut, 0, sizeof(na->na_lut));
2007847bf383SLuigi Rizzo 	na->active_fds--;
2008847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
2009847bf383SLuigi Rizzo err_rel_excl:
2010847bf383SLuigi Rizzo 	netmap_rel_exclusive(priv);
2011847bf383SLuigi Rizzo err_del_rings:
2012847bf383SLuigi Rizzo 	if (na->active_fds == 0)
2013847bf383SLuigi Rizzo 		netmap_mem_rings_delete(na);
2014847bf383SLuigi Rizzo err_del_krings:
2015847bf383SLuigi Rizzo 	if (na->active_fds == 0)
2016847bf383SLuigi Rizzo 		na->nm_krings_delete(na);
2017847bf383SLuigi Rizzo err_drop_mem:
2018847bf383SLuigi Rizzo 	netmap_mem_deref(na->nm_mem, na);
2019847bf383SLuigi Rizzo err:
2020847bf383SLuigi Rizzo 	priv->np_na = NULL;
2021847bf383SLuigi Rizzo 	return error;
2022ce3ee1e7SLuigi Rizzo }
2023847bf383SLuigi Rizzo 
2024847bf383SLuigi Rizzo 
2025847bf383SLuigi Rizzo /*
2026847bf383SLuigi Rizzo  * update kring and ring at the end of txsync.
2027847bf383SLuigi Rizzo  */
2028847bf383SLuigi Rizzo static inline void
2029847bf383SLuigi Rizzo nm_txsync_finalize(struct netmap_kring *kring)
2030847bf383SLuigi Rizzo {
2031847bf383SLuigi Rizzo 	/* update ring tail to what the kernel knows */
2032847bf383SLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
2033847bf383SLuigi Rizzo 
2034847bf383SLuigi Rizzo 	/* note, head/rhead/hwcur might be behind cur/rcur
2035847bf383SLuigi Rizzo 	 * if no carrier
2036847bf383SLuigi Rizzo 	 */
2037847bf383SLuigi Rizzo 	ND(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
2038847bf383SLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
2039847bf383SLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
2040847bf383SLuigi Rizzo }
2041847bf383SLuigi Rizzo 
2042847bf383SLuigi Rizzo 
2043847bf383SLuigi Rizzo /*
2044847bf383SLuigi Rizzo  * update kring and ring at the end of rxsync
2045847bf383SLuigi Rizzo  */
2046847bf383SLuigi Rizzo static inline void
2047847bf383SLuigi Rizzo nm_rxsync_finalize(struct netmap_kring *kring)
2048847bf383SLuigi Rizzo {
2049847bf383SLuigi Rizzo 	/* tell userspace that there might be new packets */
2050847bf383SLuigi Rizzo 	//struct netmap_ring *ring = kring->ring;
2051847bf383SLuigi Rizzo 	ND("head %d cur %d tail %d -> %d", ring->head, ring->cur, ring->tail,
2052847bf383SLuigi Rizzo 		kring->nr_hwtail);
2053847bf383SLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
2054847bf383SLuigi Rizzo 	/* make a copy of the state for next round */
2055847bf383SLuigi Rizzo 	kring->rhead = kring->ring->head;
2056847bf383SLuigi Rizzo 	kring->rcur = kring->ring->cur;
2057f18be576SLuigi Rizzo }
2058f18be576SLuigi Rizzo 
2059f18be576SLuigi Rizzo 
2060f18be576SLuigi Rizzo 
206168b8534bSLuigi Rizzo /*
206268b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
206368b8534bSLuigi Rizzo  *
206468b8534bSLuigi Rizzo  * Following a list of accepted commands:
206568b8534bSLuigi Rizzo  * - NIOCGINFO
206668b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
206768b8534bSLuigi Rizzo  * - NIOCREGIF
206868b8534bSLuigi Rizzo  * - NIOCTXSYNC
206968b8534bSLuigi Rizzo  * - NIOCRXSYNC
207068b8534bSLuigi Rizzo  *
207168b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
207268b8534bSLuigi Rizzo  */
2073f9790aebSLuigi Rizzo int
20740b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
20750b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
207668b8534bSLuigi Rizzo {
207768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
207868b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
2079ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
208068b8534bSLuigi Rizzo 	int error;
2081f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
208268b8534bSLuigi Rizzo 	struct netmap_if *nifp;
2083ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
2084847bf383SLuigi Rizzo 	enum txrx t;
208568b8534bSLuigi Rizzo 
20860b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
20870b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
2088f196ce38SLuigi Rizzo 
208917885a7bSLuigi Rizzo 	if (cmd == NIOCGINFO || cmd == NIOCREGIF) {
209017885a7bSLuigi Rizzo 		/* truncate name */
209117885a7bSLuigi Rizzo 		nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';
209217885a7bSLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
209317885a7bSLuigi Rizzo 			D("API mismatch for %s got %d need %d",
209417885a7bSLuigi Rizzo 				nmr->nr_name,
209517885a7bSLuigi Rizzo 				nmr->nr_version, NETMAP_API);
209617885a7bSLuigi Rizzo 			nmr->nr_version = NETMAP_API;
2097f0ea3689SLuigi Rizzo 		}
2098f0ea3689SLuigi Rizzo 		if (nmr->nr_version < NETMAP_MIN_API ||
2099f0ea3689SLuigi Rizzo 		    nmr->nr_version > NETMAP_MAX_API) {
210017885a7bSLuigi Rizzo 			return EINVAL;
210117885a7bSLuigi Rizzo 		}
210217885a7bSLuigi Rizzo 	}
2103506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
2104506cc70cSLuigi Rizzo 
210568b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
21068241616dSLuigi Rizzo 	if (error) {
2107506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
21088241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
21098241616dSLuigi Rizzo 		 * is now created in the open */
21108241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
2111506cc70cSLuigi Rizzo 	}
211268b8534bSLuigi Rizzo 
211368b8534bSLuigi Rizzo 	switch (cmd) {
211468b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
2115f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
2116f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
2117f18be576SLuigi Rizzo 			break;
2118f18be576SLuigi Rizzo 		}
2119ce3ee1e7SLuigi Rizzo 
2120ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2121ce3ee1e7SLuigi Rizzo 		do {
2122ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
2123ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
2124ce3ee1e7SLuigi Rizzo 			u_int memflags;
2125ce3ee1e7SLuigi Rizzo 
2126ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
2127ce3ee1e7SLuigi Rizzo 				/* get a refcount */
2128f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
21298241616dSLuigi Rizzo 				if (error)
21308241616dSLuigi Rizzo 					break;
2131f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
2132ce3ee1e7SLuigi Rizzo 			}
2133ce3ee1e7SLuigi Rizzo 
2134f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags,
2135f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
2136ce3ee1e7SLuigi Rizzo 			if (error)
2137ce3ee1e7SLuigi Rizzo 				break;
2138ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
2139ce3ee1e7SLuigi Rizzo 				break;
21408241616dSLuigi Rizzo 			nmr->nr_offset = 0;
21418241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
2142ae10d1afSLuigi Rizzo 			netmap_update_config(na);
2143d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
2144d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
214564ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
214664ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
2147f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
2148ce3ee1e7SLuigi Rizzo 		} while (0);
2149ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
215068b8534bSLuigi Rizzo 		break;
215168b8534bSLuigi Rizzo 
215268b8534bSLuigi Rizzo 	case NIOCREGIF:
2153f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
2154f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
2155f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
21564bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_VNET_HDR
21574bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_NEWIF
21584bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_DELIF) {
2159f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
2160f18be576SLuigi Rizzo 			break;
2161f18be576SLuigi Rizzo 		} else if (i != 0) {
2162f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
2163f18be576SLuigi Rizzo 			error = EINVAL;
2164f18be576SLuigi Rizzo 			break;
2165f18be576SLuigi Rizzo 		}
2166f18be576SLuigi Rizzo 
21678241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
2168ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2169ce3ee1e7SLuigi Rizzo 		do {
2170ce3ee1e7SLuigi Rizzo 			u_int memflags;
2171ce3ee1e7SLuigi Rizzo 
2172847bf383SLuigi Rizzo 			if (priv->np_nifp != NULL) {	/* thread already registered */
2173f0ea3689SLuigi Rizzo 				error = EBUSY;
2174506cc70cSLuigi Rizzo 				break;
2175506cc70cSLuigi Rizzo 			}
217668b8534bSLuigi Rizzo 			/* find the interface and a reference */
2177f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
217868b8534bSLuigi Rizzo 			if (error)
2179ce3ee1e7SLuigi Rizzo 				break;
2180f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
2181f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2182ce3ee1e7SLuigi Rizzo 				error = EBUSY;
2183ce3ee1e7SLuigi Rizzo 				break;
2184f196ce38SLuigi Rizzo 			}
2185847bf383SLuigi Rizzo 			error = netmap_do_regif(priv, na, nmr->nr_ringid, nmr->nr_flags);
2186847bf383SLuigi Rizzo 			if (error) {    /* reg. failed, release priv and ref */
2187f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2188ce3ee1e7SLuigi Rizzo 				break;
218968b8534bSLuigi Rizzo 			}
2190847bf383SLuigi Rizzo 			nifp = priv->np_nifp;
2191f0ea3689SLuigi Rizzo 			priv->np_td = td; // XXX kqueue, debugging only
219268b8534bSLuigi Rizzo 
219368b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
2194d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
2195d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
219664ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
219764ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
2198f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags,
2199f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
2200ce3ee1e7SLuigi Rizzo 			if (error) {
2201847bf383SLuigi Rizzo 				netmap_do_unregif(priv);
2202f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2203ce3ee1e7SLuigi Rizzo 				break;
2204ce3ee1e7SLuigi Rizzo 			}
2205ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
22063d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2207ce3ee1e7SLuigi Rizzo 			}
2208847bf383SLuigi Rizzo 			for_rx_tx(t) {
2209847bf383SLuigi Rizzo 				priv->np_si[t] = nm_si_user(priv, t) ?
2210847bf383SLuigi Rizzo 					&na->si[t] : &NMR(na, t)[priv->np_qfirst[t]].si;
2211847bf383SLuigi Rizzo 			}
2212f0ea3689SLuigi Rizzo 
2213f0ea3689SLuigi Rizzo 			if (nmr->nr_arg3) {
2214f0ea3689SLuigi Rizzo 				D("requested %d extra buffers", nmr->nr_arg3);
2215f0ea3689SLuigi Rizzo 				nmr->nr_arg3 = netmap_extra_alloc(na,
2216f0ea3689SLuigi Rizzo 					&nifp->ni_bufs_head, nmr->nr_arg3);
2217f0ea3689SLuigi Rizzo 				D("got %d extra buffers", nmr->nr_arg3);
2218f0ea3689SLuigi Rizzo 			}
2219ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
2220ce3ee1e7SLuigi Rizzo 		} while (0);
2221ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
222268b8534bSLuigi Rizzo 		break;
222368b8534bSLuigi Rizzo 
222468b8534bSLuigi Rizzo 	case NIOCTXSYNC:
222568b8534bSLuigi Rizzo 	case NIOCRXSYNC:
22268241616dSLuigi Rizzo 		nifp = priv->np_nifp;
22278241616dSLuigi Rizzo 
22288241616dSLuigi Rizzo 		if (nifp == NULL) {
2229506cc70cSLuigi Rizzo 			error = ENXIO;
2230506cc70cSLuigi Rizzo 			break;
2231506cc70cSLuigi Rizzo 		}
22326641c68bSLuigi Rizzo 		mb(); /* make sure following reads are not from cache */
22338241616dSLuigi Rizzo 
2234f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
22358241616dSLuigi Rizzo 
2236f9790aebSLuigi Rizzo 		if (na == NULL) {
2237f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
22388241616dSLuigi Rizzo 			error = ENXIO;
22398241616dSLuigi Rizzo 			break;
22408241616dSLuigi Rizzo 		}
22418241616dSLuigi Rizzo 
22424bf50f18SLuigi Rizzo 		if (!nm_netmap_on(na)) {
2243f9790aebSLuigi Rizzo 			error = ENXIO;
2244f9790aebSLuigi Rizzo 			break;
2245f9790aebSLuigi Rizzo 		}
2246f9790aebSLuigi Rizzo 
2247847bf383SLuigi Rizzo 		t = (cmd == NIOCTXSYNC ? NR_TX : NR_RX);
2248847bf383SLuigi Rizzo 		krings = NMR(na, t);
2249847bf383SLuigi Rizzo 		qfirst = priv->np_qfirst[t];
2250847bf383SLuigi Rizzo 		qlast = priv->np_qlast[t];
225168b8534bSLuigi Rizzo 
2252f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
2253ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
2254ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
2255ce3ee1e7SLuigi Rizzo 				error = EBUSY;
2256ce3ee1e7SLuigi Rizzo 				goto out;
2257ce3ee1e7SLuigi Rizzo 			}
225868b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
225968b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
22603c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
226168b8534bSLuigi Rizzo 					    i, kring->ring->cur,
226268b8534bSLuigi Rizzo 					    kring->nr_hwcur);
226317885a7bSLuigi Rizzo 				if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
226417885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
2265847bf383SLuigi Rizzo 				} else if (kring->nm_sync(kring, NAF_FORCE_RECLAIM) == 0) {
2266847bf383SLuigi Rizzo 					nm_txsync_finalize(kring);
226717885a7bSLuigi Rizzo 				}
226868b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
22693c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
227068b8534bSLuigi Rizzo 					    i, kring->ring->cur,
227168b8534bSLuigi Rizzo 					    kring->nr_hwcur);
227268b8534bSLuigi Rizzo 			} else {
2273847bf383SLuigi Rizzo 				if (nm_rxsync_prologue(kring) >= kring->nkr_num_slots) {
2274847bf383SLuigi Rizzo 					netmap_ring_reinit(kring);
2275847bf383SLuigi Rizzo 				} else if (kring->nm_sync(kring, NAF_FORCE_READ) == 0) {
2276847bf383SLuigi Rizzo 					nm_rxsync_finalize(kring);
2277847bf383SLuigi Rizzo 				}
227868b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
227968b8534bSLuigi Rizzo 			}
2280ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
228168b8534bSLuigi Rizzo 		}
228268b8534bSLuigi Rizzo 
228368b8534bSLuigi Rizzo 		break;
228468b8534bSLuigi Rizzo 
2285847bf383SLuigi Rizzo #ifdef WITH_VALE
22864bf50f18SLuigi Rizzo 	case NIOCCONFIG:
22874bf50f18SLuigi Rizzo 		error = netmap_bdg_config(nmr);
22884bf50f18SLuigi Rizzo 		break;
2289847bf383SLuigi Rizzo #endif
2290f196ce38SLuigi Rizzo #ifdef __FreeBSD__
229189e3fd52SLuigi Rizzo 	case FIONBIO:
229289e3fd52SLuigi Rizzo 	case FIOASYNC:
229389e3fd52SLuigi Rizzo 		ND("FIONBIO/FIOASYNC are no-ops");
229489e3fd52SLuigi Rizzo 		break;
229589e3fd52SLuigi Rizzo 
229668b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
229768b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
229868b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
229968b8534bSLuigi Rizzo 	case BIOCSSEESENT:
230068b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
230168b8534bSLuigi Rizzo 		break;
230268b8534bSLuigi Rizzo 
2303babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
230468b8534bSLuigi Rizzo 	    {
2305b3d37588SLuigi Rizzo 		struct ifnet *ifp = ifunit_ref(nmr->nr_name);
2306b3d37588SLuigi Rizzo 		if (ifp == NULL) {
2307b3d37588SLuigi Rizzo 			error = ENXIO;
2308b3d37588SLuigi Rizzo 		} else {
230968b8534bSLuigi Rizzo 			struct socket so;
2310ce3ee1e7SLuigi Rizzo 
231168b8534bSLuigi Rizzo 			bzero(&so, sizeof(so));
231268b8534bSLuigi Rizzo 			so.so_vnet = ifp->if_vnet;
231368b8534bSLuigi Rizzo 			// so->so_proto not null.
231468b8534bSLuigi Rizzo 			error = ifioctl(&so, cmd, data, td);
2315b3d37588SLuigi Rizzo 			if_rele(ifp);
2316b3d37588SLuigi Rizzo 		}
2317babc7c12SLuigi Rizzo 		break;
231868b8534bSLuigi Rizzo 	    }
2319f196ce38SLuigi Rizzo 
2320f196ce38SLuigi Rizzo #else /* linux */
2321f196ce38SLuigi Rizzo 	default:
2322f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
2323f196ce38SLuigi Rizzo #endif /* linux */
232468b8534bSLuigi Rizzo 	}
2325ce3ee1e7SLuigi Rizzo out:
232668b8534bSLuigi Rizzo 
2327506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
232868b8534bSLuigi Rizzo 	return (error);
232968b8534bSLuigi Rizzo }
233068b8534bSLuigi Rizzo 
233168b8534bSLuigi Rizzo 
233268b8534bSLuigi Rizzo /*
233368b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
233468b8534bSLuigi Rizzo  *
233568b8534bSLuigi Rizzo  * Can be called for one or more queues.
233668b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
233768b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
2338ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
233968b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
234068b8534bSLuigi Rizzo  * are done through callbacks.
2341f196ce38SLuigi Rizzo  *
234201c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
234301c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
234401c7d25fSLuigi Rizzo  * hidden argument.
234568b8534bSLuigi Rizzo  */
2346f9790aebSLuigi Rizzo int
234701c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
234868b8534bSLuigi Rizzo {
234968b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
235068b8534bSLuigi Rizzo 	struct netmap_adapter *na;
235168b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2352847bf383SLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want[NR_TXRX], revents = 0;
2353847bf383SLuigi Rizzo #define want_tx want[NR_TX]
2354847bf383SLuigi Rizzo #define want_rx want[NR_RX]
235517885a7bSLuigi Rizzo 	struct mbq q;		/* packets from hw queues to host stack */
235601c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
2357f0ea3689SLuigi Rizzo 	int is_kevent = 0;
2358847bf383SLuigi Rizzo 	enum txrx t;
235901c7d25fSLuigi Rizzo 
2360f9790aebSLuigi Rizzo 	/*
2361f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
2362f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
2363f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
2364f9790aebSLuigi Rizzo 	 */
236517885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
2366ce3ee1e7SLuigi Rizzo 
236701c7d25fSLuigi Rizzo 	(void)pwait;
2368f9790aebSLuigi Rizzo 	mbq_init(&q);
236968b8534bSLuigi Rizzo 
2370f0ea3689SLuigi Rizzo 	/*
2371f0ea3689SLuigi Rizzo 	 * XXX kevent has curthread->tp_fop == NULL,
2372f0ea3689SLuigi Rizzo 	 * so devfs_get_cdevpriv() fails. We circumvent this by passing
2373f0ea3689SLuigi Rizzo 	 * priv as the first argument, which is also useful to avoid
2374f0ea3689SLuigi Rizzo 	 * the selrecord() which are not necessary in that case.
2375f0ea3689SLuigi Rizzo 	 */
2376f0ea3689SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0) {
2377f0ea3689SLuigi Rizzo 		is_kevent = 1;
2378f0ea3689SLuigi Rizzo 		if (netmap_verbose)
2379f0ea3689SLuigi Rizzo 			D("called from kevent");
2380f0ea3689SLuigi Rizzo 		priv = (struct netmap_priv_d *)dev;
2381f0ea3689SLuigi Rizzo 	}
2382f0ea3689SLuigi Rizzo 	if (priv == NULL)
238368b8534bSLuigi Rizzo 		return POLLERR;
238468b8534bSLuigi Rizzo 
23858241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
23868241616dSLuigi Rizzo 		D("No if registered");
23878241616dSLuigi Rizzo 		return POLLERR;
23888241616dSLuigi Rizzo 	}
2389847bf383SLuigi Rizzo 	mb(); /* make sure following reads are not from cache */
23908241616dSLuigi Rizzo 
2391f9790aebSLuigi Rizzo 	na = priv->np_na;
2392f9790aebSLuigi Rizzo 
23934bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
239468b8534bSLuigi Rizzo 		return POLLERR;
239568b8534bSLuigi Rizzo 
239668b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
23974bf50f18SLuigi Rizzo 		D("device %s events 0x%x", na->name, events);
239868b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
239968b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
240068b8534bSLuigi Rizzo 
2401091fd0abSLuigi Rizzo 
240268b8534bSLuigi Rizzo 	/*
2403f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
2404f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
2405ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
2406ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
2407ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
2408ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
240968b8534bSLuigi Rizzo 	 *
241068b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
241168b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
241268b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
241368b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
241468b8534bSLuigi Rizzo 	 */
2415847bf383SLuigi Rizzo 	check_all_tx = nm_si_user(priv, NR_TX);
2416847bf383SLuigi Rizzo 	check_all_rx = nm_si_user(priv, NR_RX);
241764ae02c3SLuigi Rizzo 
241868b8534bSLuigi Rizzo 	/*
2419f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
2420f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
242168b8534bSLuigi Rizzo 	 * routines.
242268b8534bSLuigi Rizzo 	 */
2423847bf383SLuigi Rizzo 	for_rx_tx(t) {
2424847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; want[t] && i < priv->np_qlast[t]; i++) {
2425847bf383SLuigi Rizzo 			kring = &NMR(na, t)[i];
242617885a7bSLuigi Rizzo 			/* XXX compare ring->cur and kring->tail */
242717885a7bSLuigi Rizzo 			if (!nm_ring_empty(kring->ring)) {
2428847bf383SLuigi Rizzo 				revents |= want[t];
2429847bf383SLuigi Rizzo 				want[t] = 0;	/* also breaks the loop */
243068b8534bSLuigi Rizzo 			}
243168b8534bSLuigi Rizzo 		}
243268b8534bSLuigi Rizzo 	}
243368b8534bSLuigi Rizzo 
243468b8534bSLuigi Rizzo 	/*
243517885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
243617885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
243717885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
2438f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
2439f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
244068b8534bSLuigi Rizzo 	 */
244168b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
244217885a7bSLuigi Rizzo 		/*
244317885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
244417885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
244517885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
244617885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
2447ce3ee1e7SLuigi Rizzo 		 */
2448091fd0abSLuigi Rizzo flush_tx:
2449847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[NR_TX]; i < priv->np_qlast[NR_RX]; i++) {
245017885a7bSLuigi Rizzo 			int found = 0;
245117885a7bSLuigi Rizzo 
245268b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
245368b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
245468b8534bSLuigi Rizzo 				continue;
245517885a7bSLuigi Rizzo 			/* only one thread does txsync */
2456ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
245789cc2556SLuigi Rizzo 				/* either busy or stopped
245889cc2556SLuigi Rizzo 				 * XXX if the ring is stopped, sleeping would
245989cc2556SLuigi Rizzo 				 * be better. In current code, however, we only
246089cc2556SLuigi Rizzo 				 * stop the rings for brief intervals (2014-03-14)
246189cc2556SLuigi Rizzo 				 */
246289e3fd52SLuigi Rizzo 				if (netmap_verbose)
246389e3fd52SLuigi Rizzo 					RD(2, "%p lost race on txring %d, ok",
246489e3fd52SLuigi Rizzo 					    priv, i);
246517885a7bSLuigi Rizzo 				continue;
246668b8534bSLuigi Rizzo 			}
246717885a7bSLuigi Rizzo 			if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
246817885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
246917885a7bSLuigi Rizzo 				revents |= POLLERR;
247017885a7bSLuigi Rizzo 			} else {
2471f0ea3689SLuigi Rizzo 				if (kring->nm_sync(kring, 0))
247268b8534bSLuigi Rizzo 					revents |= POLLERR;
2473847bf383SLuigi Rizzo 				else
2474847bf383SLuigi Rizzo 					nm_txsync_finalize(kring);
247517885a7bSLuigi Rizzo 			}
247668b8534bSLuigi Rizzo 
247717885a7bSLuigi Rizzo 			/*
247817885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
247917885a7bSLuigi Rizzo 			 * listeners on the same ring.
248017885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
248117885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
2482f9790aebSLuigi Rizzo 			 */
248317885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
248417885a7bSLuigi Rizzo 			nm_kr_put(kring);
248517885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
248668b8534bSLuigi Rizzo 				revents |= want_tx;
248768b8534bSLuigi Rizzo 				want_tx = 0;
2488847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
248968b8534bSLuigi Rizzo 			}
2490ce3ee1e7SLuigi Rizzo 		}
2491f0ea3689SLuigi Rizzo 		if (want_tx && retry_tx && !is_kevent) {
24920e73f29aSLuigi Rizzo 			OS_selrecord(td, check_all_tx ?
2493847bf383SLuigi Rizzo 			    &na->si[NR_TX] : &na->tx_rings[priv->np_qfirst[NR_TX]].si);
2494ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
2495ce3ee1e7SLuigi Rizzo 			goto flush_tx;
249668b8534bSLuigi Rizzo 		}
249768b8534bSLuigi Rizzo 	}
249868b8534bSLuigi Rizzo 
249968b8534bSLuigi Rizzo 	/*
250017885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
250168b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
250268b8534bSLuigi Rizzo 	 */
250368b8534bSLuigi Rizzo 	if (want_rx) {
250417885a7bSLuigi Rizzo 		int send_down = 0; /* transparent mode */
250589cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
2506ce3ee1e7SLuigi Rizzo do_retry_rx:
2507847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[NR_RX]; i < priv->np_qlast[NR_RX]; i++) {
250817885a7bSLuigi Rizzo 			int found = 0;
250917885a7bSLuigi Rizzo 
251068b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
2511ce3ee1e7SLuigi Rizzo 
2512ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
251389e3fd52SLuigi Rizzo 				if (netmap_verbose)
251489e3fd52SLuigi Rizzo 					RD(2, "%p lost race on rxring %d, ok",
251589e3fd52SLuigi Rizzo 					    priv, i);
251617885a7bSLuigi Rizzo 				continue;
251768b8534bSLuigi Rizzo 			}
2518ce3ee1e7SLuigi Rizzo 
2519847bf383SLuigi Rizzo 			if (nm_rxsync_prologue(kring) >= kring->nkr_num_slots) {
2520847bf383SLuigi Rizzo 				netmap_ring_reinit(kring);
2521847bf383SLuigi Rizzo 				revents |= POLLERR;
2522847bf383SLuigi Rizzo 			}
2523847bf383SLuigi Rizzo 			/* now we can use kring->rcur, rtail */
2524847bf383SLuigi Rizzo 
252517885a7bSLuigi Rizzo 			/*
252617885a7bSLuigi Rizzo 			 * transparent mode support: collect packets
252717885a7bSLuigi Rizzo 			 * from the rxring(s).
25284bf50f18SLuigi Rizzo 			 * XXX NR_FORWARD should only be read on
25294bf50f18SLuigi Rizzo 			 * physical or NIC ports
2530ce3ee1e7SLuigi Rizzo 			 */
2531091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
2532091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
2533091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
2534091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
2535091fd0abSLuigi Rizzo 			}
253668b8534bSLuigi Rizzo 
2537f0ea3689SLuigi Rizzo 			if (kring->nm_sync(kring, 0))
253868b8534bSLuigi Rizzo 				revents |= POLLERR;
2539847bf383SLuigi Rizzo 			else
2540847bf383SLuigi Rizzo 				nm_rxsync_finalize(kring);
25415819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
25425819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
254368b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
25445819da83SLuigi Rizzo 			}
254517885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
254617885a7bSLuigi Rizzo 			nm_kr_put(kring);
254717885a7bSLuigi Rizzo 			if (found) {
254868b8534bSLuigi Rizzo 				revents |= want_rx;
2549ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
2550847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
255168b8534bSLuigi Rizzo 			}
255268b8534bSLuigi Rizzo 		}
255317885a7bSLuigi Rizzo 
255417885a7bSLuigi Rizzo 		/* transparent mode XXX only during first pass ? */
2555f0ea3689SLuigi Rizzo 		if (na->na_flags & NAF_HOST_RINGS) {
2556f0ea3689SLuigi Rizzo 			kring = &na->rx_rings[na->num_rx_rings];
25574bf50f18SLuigi Rizzo 			if (check_all_rx
25584bf50f18SLuigi Rizzo 			    && (netmap_fwd || kring->ring->flags & NR_FORWARD)) {
25594bf50f18SLuigi Rizzo 				/* XXX fix to use kring fields */
25604bf50f18SLuigi Rizzo 				if (nm_ring_empty(kring->ring))
256117885a7bSLuigi Rizzo 					send_down = netmap_rxsync_from_host(na, td, dev);
25624bf50f18SLuigi Rizzo 				if (!nm_ring_empty(kring->ring))
25634bf50f18SLuigi Rizzo 					revents |= want_rx;
256417885a7bSLuigi Rizzo 			}
2565f0ea3689SLuigi Rizzo 		}
256617885a7bSLuigi Rizzo 
2567f0ea3689SLuigi Rizzo 		if (retry_rx && !is_kevent)
25680e73f29aSLuigi Rizzo 			OS_selrecord(td, check_all_rx ?
2569847bf383SLuigi Rizzo 			    &na->si[NR_RX] : &na->rx_rings[priv->np_qfirst[NR_RX]].si);
257017885a7bSLuigi Rizzo 		if (send_down > 0 || retry_rx) {
257117885a7bSLuigi Rizzo 			retry_rx = 0;
257217885a7bSLuigi Rizzo 			if (send_down)
257317885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
257417885a7bSLuigi Rizzo 			else
2575ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
2576ce3ee1e7SLuigi Rizzo 		}
257768b8534bSLuigi Rizzo 	}
2578091fd0abSLuigi Rizzo 
257917885a7bSLuigi Rizzo 	/*
258017885a7bSLuigi Rizzo 	 * Transparent mode: marked bufs on rx rings between
258117885a7bSLuigi Rizzo 	 * kring->nr_hwcur and ring->head
258217885a7bSLuigi Rizzo 	 * are passed to the other endpoint.
258317885a7bSLuigi Rizzo 	 *
258417885a7bSLuigi Rizzo 	 * In this mode we also scan the sw rxring, which in
258517885a7bSLuigi Rizzo 	 * turn passes packets up.
258617885a7bSLuigi Rizzo 	 *
258717885a7bSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
258817885a7bSLuigi Rizzo  	 * rings to a single file descriptor.
2589ce3ee1e7SLuigi Rizzo 	 */
2590091fd0abSLuigi Rizzo 
25914bf50f18SLuigi Rizzo 	if (q.head && na->ifp != NULL)
2592f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
259368b8534bSLuigi Rizzo 
259468b8534bSLuigi Rizzo 	return (revents);
2595847bf383SLuigi Rizzo #undef want_tx
2596847bf383SLuigi Rizzo #undef want_rx
259768b8534bSLuigi Rizzo }
259868b8534bSLuigi Rizzo 
259917885a7bSLuigi Rizzo 
260017885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
260168b8534bSLuigi Rizzo 
2602f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
2603f9790aebSLuigi Rizzo 
260489cc2556SLuigi Rizzo /* default notify callback */
2605f9790aebSLuigi Rizzo static int
2606847bf383SLuigi Rizzo netmap_notify(struct netmap_kring *kring, int flags)
2607f9790aebSLuigi Rizzo {
2608847bf383SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
2609847bf383SLuigi Rizzo 	enum txrx t = kring->tx;
2610f9790aebSLuigi Rizzo 
2611f0ea3689SLuigi Rizzo 	OS_selwakeup(&kring->si, PI_NET);
261289cc2556SLuigi Rizzo 	/* optimization: avoid a wake up on the global
261389cc2556SLuigi Rizzo 	 * queue if nobody has registered for more
261489cc2556SLuigi Rizzo 	 * than one ring
261589cc2556SLuigi Rizzo 	 */
2616847bf383SLuigi Rizzo 	if (na->si_users[t] > 0)
2617847bf383SLuigi Rizzo 		OS_selwakeup(&na->si[t], PI_NET);
2618847bf383SLuigi Rizzo 
2619f9790aebSLuigi Rizzo 	return 0;
2620f9790aebSLuigi Rizzo }
2621f9790aebSLuigi Rizzo 
2622f9790aebSLuigi Rizzo 
262389cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
262489cc2556SLuigi Rizzo  * Attach na to the ifp (if any) and provide defaults
262589cc2556SLuigi Rizzo  * for optional callbacks. Defaults assume that we
262689cc2556SLuigi Rizzo  * are creating an hardware netmap_adapter.
262789cc2556SLuigi Rizzo  */
2628f9790aebSLuigi Rizzo int
2629f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2630f9790aebSLuigi Rizzo {
2631f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2632f9790aebSLuigi Rizzo 
2633f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2634f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
26354bf50f18SLuigi Rizzo 			na->name, na->num_tx_rings, na->num_rx_rings);
2636f9790aebSLuigi Rizzo 		return EINVAL;
2637f9790aebSLuigi Rizzo 	}
26384bf50f18SLuigi Rizzo 	/* ifp is NULL for virtual adapters (bwrap, non-persistent VALE ports,
26394bf50f18SLuigi Rizzo 	 * pipes, monitors). For bwrap we actually have a non-null ifp for
26404bf50f18SLuigi Rizzo 	 * use by the external modules, but that is set after this
26414bf50f18SLuigi Rizzo 	 * function has been called.
26424bf50f18SLuigi Rizzo 	 * XXX this is ugly, maybe split this function in two (2014-03-14)
26434bf50f18SLuigi Rizzo 	 */
26444bf50f18SLuigi Rizzo 	if (ifp != NULL) {
2645f9790aebSLuigi Rizzo 		WNA(ifp) = na;
264617885a7bSLuigi Rizzo 
264717885a7bSLuigi Rizzo 	/* the following is only needed for na that use the host port.
264817885a7bSLuigi Rizzo 	 * XXX do we have something similar for linux ?
264917885a7bSLuigi Rizzo 	 */
265017885a7bSLuigi Rizzo #ifdef __FreeBSD__
265117885a7bSLuigi Rizzo 		na->if_input = ifp->if_input; /* for netmap_send_up */
265217885a7bSLuigi Rizzo #endif /* __FreeBSD__ */
265317885a7bSLuigi Rizzo 
2654f9790aebSLuigi Rizzo 		NETMAP_SET_CAPABLE(ifp);
26554bf50f18SLuigi Rizzo 	}
2656f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
265789cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
265889cc2556SLuigi Rizzo 		 * since other port types all provide their own
265989cc2556SLuigi Rizzo 		 * nm_krings_create
266089cc2556SLuigi Rizzo 		 */
2661f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
266217885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
2663f9790aebSLuigi Rizzo 	}
2664f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2665f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2666f9790aebSLuigi Rizzo 	na->active_fds = 0;
2667f9790aebSLuigi Rizzo 
2668f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
26694bf50f18SLuigi Rizzo 		/* use the global allocator */
2670f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
2671847bf383SLuigi Rizzo 	netmap_mem_get(na->nm_mem);
2672847bf383SLuigi Rizzo #ifdef WITH_VALE
26734bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
26744bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
26754bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
26764bf50f18SLuigi Rizzo 		 */
26774bf50f18SLuigi Rizzo 		na->nm_bdg_attach = netmap_bwrap_attach;
2678847bf383SLuigi Rizzo #endif
2679f9790aebSLuigi Rizzo 	return 0;
2680f9790aebSLuigi Rizzo }
2681f9790aebSLuigi Rizzo 
2682f9790aebSLuigi Rizzo 
268389cc2556SLuigi Rizzo /* standard cleanup, called by all destructors */
2684f9790aebSLuigi Rizzo void
2685f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2686f9790aebSLuigi Rizzo {
268789cc2556SLuigi Rizzo 	if (na->ifp != NULL)
2688f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2689f9790aebSLuigi Rizzo 
2690f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2691f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2692f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2693f9790aebSLuigi Rizzo 	}
2694f0ea3689SLuigi Rizzo 	netmap_pipe_dealloc(na);
2695847bf383SLuigi Rizzo 	if (na->nm_mem)
2696847bf383SLuigi Rizzo 		netmap_mem_put(na->nm_mem);
2697f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2698f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2699f9790aebSLuigi Rizzo }
2700f9790aebSLuigi Rizzo 
27014bf50f18SLuigi Rizzo /* Wrapper for the register callback provided hardware drivers.
27024bf50f18SLuigi Rizzo  * na->ifp == NULL means the the driver module has been
27034bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
27044bf50f18SLuigi Rizzo  * Note that module unloading, in our patched linux drivers,
27054bf50f18SLuigi Rizzo  * happens under NMG_LOCK and after having stopped all the
27064bf50f18SLuigi Rizzo  * nic rings (see netmap_detach). This provides sufficient
27074bf50f18SLuigi Rizzo  * protection for the other driver-provied callbacks
27084bf50f18SLuigi Rizzo  * (i.e., nm_config and nm_*xsync), that therefore don't need
27094bf50f18SLuigi Rizzo  * to wrapped.
27104bf50f18SLuigi Rizzo  */
27114bf50f18SLuigi Rizzo static int
27124bf50f18SLuigi Rizzo netmap_hw_register(struct netmap_adapter *na, int onoff)
27134bf50f18SLuigi Rizzo {
27144bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
27154bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
27164bf50f18SLuigi Rizzo 
27174bf50f18SLuigi Rizzo 	if (na->ifp == NULL)
27184bf50f18SLuigi Rizzo 		return onoff ? ENXIO : 0;
27194bf50f18SLuigi Rizzo 
27204bf50f18SLuigi Rizzo 	return hwna->nm_hw_register(na, onoff);
27214bf50f18SLuigi Rizzo }
27224bf50f18SLuigi Rizzo 
2723f18be576SLuigi Rizzo 
272468b8534bSLuigi Rizzo /*
272568b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
272668b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
272768b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
272868b8534bSLuigi Rizzo  * of hardware rings):
272968b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
273068b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
273117885a7bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues. // XXX still true ?
273268b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
273368b8534bSLuigi Rizzo  */
273468b8534bSLuigi Rizzo int
2735f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
273668b8534bSLuigi Rizzo {
2737f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2738f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2739ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
274068b8534bSLuigi Rizzo 
2741ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2742ae10d1afSLuigi Rizzo 		goto fail;
2743f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2744f9790aebSLuigi Rizzo 	if (hwna == NULL)
2745ae10d1afSLuigi Rizzo 		goto fail;
2746f9790aebSLuigi Rizzo 	hwna->up = *arg;
2747847bf383SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS | NAF_NATIVE;
27484bf50f18SLuigi Rizzo 	strncpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
27494bf50f18SLuigi Rizzo 	hwna->nm_hw_register = hwna->up.nm_register;
27504bf50f18SLuigi Rizzo 	hwna->up.nm_register = netmap_hw_register;
2751f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2752f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2753f9790aebSLuigi Rizzo 		goto fail;
2754f9790aebSLuigi Rizzo 	}
2755f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2756f9790aebSLuigi Rizzo 
275764ae02c3SLuigi Rizzo #ifdef linux
2758f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2759f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2760847bf383SLuigi Rizzo #ifndef NETMAP_LINUX_HAVE_NETDEV_OPS
2761f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2762f18be576SLuigi Rizzo #else
2763f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2764f18be576SLuigi Rizzo #endif
2765f18be576SLuigi Rizzo 	}
2766f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
27674bf50f18SLuigi Rizzo 	if (ifp->ethtool_ops) {
27684bf50f18SLuigi Rizzo 		hwna->nm_eto = *ifp->ethtool_ops;
27694bf50f18SLuigi Rizzo 	}
27704bf50f18SLuigi Rizzo 	hwna->nm_eto.set_ringparam = linux_netmap_set_ringparam;
2771847bf383SLuigi Rizzo #ifdef NETMAP_LINUX_HAVE_SET_CHANNELS
27724bf50f18SLuigi Rizzo 	hwna->nm_eto.set_channels = linux_netmap_set_channels;
27734bf50f18SLuigi Rizzo #endif
27744bf50f18SLuigi Rizzo 	if (arg->nm_config == NULL) {
27754bf50f18SLuigi Rizzo 		hwna->up.nm_config = netmap_linux_config;
27764bf50f18SLuigi Rizzo 	}
2777ce3ee1e7SLuigi Rizzo #endif /* linux */
2778f9790aebSLuigi Rizzo 
2779d82f9014SRui Paulo 	if_printf(ifp, "netmap queues/slots: TX %d/%d, RX %d/%d\n",
2780d82f9014SRui Paulo 	    hwna->up.num_tx_rings, hwna->up.num_tx_desc,
2781d82f9014SRui Paulo 	    hwna->up.num_rx_rings, hwna->up.num_rx_desc);
2782ae10d1afSLuigi Rizzo 	return 0;
278368b8534bSLuigi Rizzo 
2784ae10d1afSLuigi Rizzo fail:
2785f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2786441ab64fSLuigi Rizzo 	if (ifp)
2787849bec0eSLuigi Rizzo 		netmap_detach(ifp);
2788f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
278968b8534bSLuigi Rizzo }
279068b8534bSLuigi Rizzo 
279168b8534bSLuigi Rizzo 
2792f9790aebSLuigi Rizzo void
2793f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2794f9790aebSLuigi Rizzo {
2795f9790aebSLuigi Rizzo 	if (!na) {
2796f9790aebSLuigi Rizzo 		return;
2797f9790aebSLuigi Rizzo 	}
2798f9790aebSLuigi Rizzo 
2799f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2800f9790aebSLuigi Rizzo }
2801f9790aebSLuigi Rizzo 
2802f9790aebSLuigi Rizzo 
2803f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2804f9790aebSLuigi Rizzo int
2805f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2806f9790aebSLuigi Rizzo {
2807f9790aebSLuigi Rizzo 	if (!na)
2808f9790aebSLuigi Rizzo 		return 1;
2809f9790aebSLuigi Rizzo 
2810f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2811f9790aebSLuigi Rizzo 		return 0;
2812f9790aebSLuigi Rizzo 
2813f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2814f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2815f9790aebSLuigi Rizzo 
2816f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2817f9790aebSLuigi Rizzo 
2818f9790aebSLuigi Rizzo 	return 1;
2819f9790aebSLuigi Rizzo }
2820f9790aebSLuigi Rizzo 
282189cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
2822f9790aebSLuigi Rizzo int
2823f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2824f9790aebSLuigi Rizzo {
2825f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
282617885a7bSLuigi Rizzo 	if (ret == 0) {
282717885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
282817885a7bSLuigi Rizzo 		mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue);
282917885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
283017885a7bSLuigi Rizzo 	}
283117885a7bSLuigi Rizzo 	return ret;
2832f9790aebSLuigi Rizzo }
2833f9790aebSLuigi Rizzo 
2834f9790aebSLuigi Rizzo 
2835f9790aebSLuigi Rizzo 
283668b8534bSLuigi Rizzo /*
283789cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
283868b8534bSLuigi Rizzo  */
283968b8534bSLuigi Rizzo void
284068b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
284168b8534bSLuigi Rizzo {
284268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
284368b8534bSLuigi Rizzo 
284468b8534bSLuigi Rizzo 	if (!na)
284568b8534bSLuigi Rizzo 		return;
284668b8534bSLuigi Rizzo 
2847f9790aebSLuigi Rizzo 	NMG_LOCK();
2848f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2849f9790aebSLuigi Rizzo 	na->ifp = NULL;
28504bf50f18SLuigi Rizzo 	na->na_flags &= ~NAF_NETMAP_ON;
2851847bf383SLuigi Rizzo 	/*
2852847bf383SLuigi Rizzo 	 * if the netmap adapter is not native, somebody
2853847bf383SLuigi Rizzo 	 * changed it, so we can not release it here.
2854847bf383SLuigi Rizzo 	 * The NULL na->ifp will notify the new owner that
2855847bf383SLuigi Rizzo 	 * the driver is gone.
2856847bf383SLuigi Rizzo 	 */
2857847bf383SLuigi Rizzo 	if (na->na_flags & NAF_NATIVE) {
2858847bf383SLuigi Rizzo 	        netmap_adapter_put(na);
2859847bf383SLuigi Rizzo 	}
2860fb25194fSLuigi Rizzo 	/* give them a chance to notice */
2861f9790aebSLuigi Rizzo 	netmap_enable_all_rings(ifp);
2862f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2863ae10d1afSLuigi Rizzo }
2864f18be576SLuigi Rizzo 
2865f18be576SLuigi Rizzo 
286668b8534bSLuigi Rizzo /*
286702ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
286802ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
286917885a7bSLuigi Rizzo  *
287017885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
287117885a7bSLuigi Rizzo  * in the relevant rxsync routine.
287217885a7bSLuigi Rizzo  *
2873ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2874ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2875ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2876f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
287768b8534bSLuigi Rizzo  */
287868b8534bSLuigi Rizzo int
2879ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
288068b8534bSLuigi Rizzo {
288168b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2882ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
288317885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
288417885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
288517885a7bSLuigi Rizzo 	struct mbq *q;
288617885a7bSLuigi Rizzo 	int space;
288768b8534bSLuigi Rizzo 
2888847bf383SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
2889ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2890ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2891ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
289217885a7bSLuigi Rizzo 
28934bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
28944bf50f18SLuigi Rizzo 		D("%s not in netmap mode anymore", na->name);
2895ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2896ce3ee1e7SLuigi Rizzo 		goto done;
2897ce3ee1e7SLuigi Rizzo 	}
2898ce3ee1e7SLuigi Rizzo 
289917885a7bSLuigi Rizzo 	q = &kring->rx_queue;
290017885a7bSLuigi Rizzo 
2901ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
29024bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
29034bf50f18SLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", na->name,
29044bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
2905ce3ee1e7SLuigi Rizzo 		goto done;
2906849bec0eSLuigi Rizzo 	}
290717885a7bSLuigi Rizzo 
290817885a7bSLuigi Rizzo 	/* protect against rxsync_from_host(), netmap_sw_to_nic()
290917885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
291017885a7bSLuigi Rizzo 	 * not possible on Linux).
291117885a7bSLuigi Rizzo 	 * Also avoid overflowing the queue.
2912ce3ee1e7SLuigi Rizzo 	 */
2913997b054cSLuigi Rizzo 	mbq_lock(q);
291417885a7bSLuigi Rizzo 
291517885a7bSLuigi Rizzo         space = kring->nr_hwtail - kring->nr_hwcur;
291617885a7bSLuigi Rizzo         if (space < 0)
291717885a7bSLuigi Rizzo                 space += kring->nkr_num_slots;
291817885a7bSLuigi Rizzo 	if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX
291917885a7bSLuigi Rizzo 		RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p",
29204bf50f18SLuigi Rizzo 			na->name, kring->nr_hwcur, kring->nr_hwtail, mbq_len(q),
292117885a7bSLuigi Rizzo 			len, m);
2922ce3ee1e7SLuigi Rizzo 	} else {
292317885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
292417885a7bSLuigi Rizzo 		ND(10, "%s %d bufs in queue len %d m %p",
29254bf50f18SLuigi Rizzo 			na->name, mbq_len(q), len, m);
292617885a7bSLuigi Rizzo 		/* notify outside the lock */
292717885a7bSLuigi Rizzo 		m = NULL;
292868b8534bSLuigi Rizzo 		error = 0;
2929ce3ee1e7SLuigi Rizzo 	}
2930997b054cSLuigi Rizzo 	mbq_unlock(q);
2931ce3ee1e7SLuigi Rizzo 
293268b8534bSLuigi Rizzo done:
293317885a7bSLuigi Rizzo 	if (m)
293468b8534bSLuigi Rizzo 		m_freem(m);
293517885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
2936847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
293789cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
293889cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
293989cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
294089cc2556SLuigi Rizzo 	 */
294168b8534bSLuigi Rizzo 
294268b8534bSLuigi Rizzo 	return (error);
294368b8534bSLuigi Rizzo }
294468b8534bSLuigi Rizzo 
294568b8534bSLuigi Rizzo 
294668b8534bSLuigi Rizzo /*
294768b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
294868b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2949f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
295068b8534bSLuigi Rizzo  */
295168b8534bSLuigi Rizzo struct netmap_slot *
2952ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
295368b8534bSLuigi Rizzo 	u_int new_cur)
295468b8534bSLuigi Rizzo {
295568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2956506cc70cSLuigi Rizzo 	int new_hwofs, lim;
295768b8534bSLuigi Rizzo 
29584bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
29594bf50f18SLuigi Rizzo 		ND("interface not in native netmap mode");
296068b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2961ce3ee1e7SLuigi Rizzo 	}
296268b8534bSLuigi Rizzo 
2963ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2964ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2965ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2966ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2967ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2968ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2969ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2970ce3ee1e7SLuigi Rizzo 	 */
297164ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
29728241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
29738241616dSLuigi Rizzo 			return NULL;
297464ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
297517885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
2976506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
297764ae02c3SLuigi Rizzo 	} else {
29788241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
29798241616dSLuigi Rizzo 			return NULL;
298064ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
298117885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
298264ae02c3SLuigi Rizzo 	}
298364ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2984506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2985506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2986506cc70cSLuigi Rizzo 
2987ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
298817885a7bSLuigi Rizzo 	if (netmap_verbose)
298917885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
29904bf50f18SLuigi Rizzo 		na->name,
299117885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
2992ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
299317885a7bSLuigi Rizzo 		kring->nr_hwtail,
299417885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
2995506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
299617885a7bSLuigi Rizzo 	if (tx == NR_TX) {
299717885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
299817885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
299917885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
300017885a7bSLuigi Rizzo 	}
3001506cc70cSLuigi Rizzo 
3002f196ce38SLuigi Rizzo #if 0 // def linux
3003f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
3004f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
3005f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
3006f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
3007f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
3008f196ce38SLuigi Rizzo 		// XXX fix error handling
3009f196ce38SLuigi Rizzo 	}
3010f196ce38SLuigi Rizzo 
3011f196ce38SLuigi Rizzo #endif /* linux */
301268b8534bSLuigi Rizzo 	/*
3013ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
3014506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
3015506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
301668b8534bSLuigi Rizzo 	 */
3017847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
301868b8534bSLuigi Rizzo 	return kring->ring->slot;
301968b8534bSLuigi Rizzo }
302068b8534bSLuigi Rizzo 
302168b8534bSLuigi Rizzo 
3022ce3ee1e7SLuigi Rizzo /*
3023f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
3024ce3ee1e7SLuigi Rizzo  *
3025ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3026ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
3027ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
3028849bec0eSLuigi Rizzo  *
3029f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
3030f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
3031f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
30324bf50f18SLuigi Rizzo  *   (see netmap_notify)
30334bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
30344bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
3035f9790aebSLuigi Rizzo  */
3036f9790aebSLuigi Rizzo void
3037f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
3038f9790aebSLuigi Rizzo {
3039f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
3040f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
3041847bf383SLuigi Rizzo 	enum txrx t = (work_done ? NR_RX : NR_TX);
3042f9790aebSLuigi Rizzo 
3043f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
3044f9790aebSLuigi Rizzo 
3045f9790aebSLuigi Rizzo 	if (netmap_verbose) {
3046f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
3047f9790aebSLuigi Rizzo 	}
3048f9790aebSLuigi Rizzo 
3049847bf383SLuigi Rizzo 	if (q >= nma_get_nrings(na, t))
3050f9790aebSLuigi Rizzo 		return;	// not a physical queue
3051847bf383SLuigi Rizzo 
3052847bf383SLuigi Rizzo 	kring = NMR(na, t) + q;
3053847bf383SLuigi Rizzo 
3054847bf383SLuigi Rizzo 	if (t == NR_RX) {
3055f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
3056f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
3057f9790aebSLuigi Rizzo 	}
3058847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
3059f9790aebSLuigi Rizzo }
3060f9790aebSLuigi Rizzo 
306117885a7bSLuigi Rizzo 
3062f9790aebSLuigi Rizzo /*
3063f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
3064f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3065f9790aebSLuigi Rizzo  *
3066ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
3067ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
3068f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
3069ce3ee1e7SLuigi Rizzo  *
3070ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
3071ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
3072ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
3073ce3ee1e7SLuigi Rizzo  * and return 1.
3074ce3ee1e7SLuigi Rizzo  *
3075ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
3076ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
30771a26580eSLuigi Rizzo  */
3078babc7c12SLuigi Rizzo int
3079ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
30801a26580eSLuigi Rizzo {
30814bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
30824bf50f18SLuigi Rizzo 
30834bf50f18SLuigi Rizzo 	/*
30844bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
30854bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
30864bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
30874bf50f18SLuigi Rizzo 	 * nm_native_on() here.
30884bf50f18SLuigi Rizzo 	 */
30894bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
30901a26580eSLuigi Rizzo 		return 0;
3091849bec0eSLuigi Rizzo 
30924bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
30938241616dSLuigi Rizzo 		ND("use regular interrupt");
30948241616dSLuigi Rizzo 		return 0;
30958241616dSLuigi Rizzo 	}
30968241616dSLuigi Rizzo 
3097f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
30981a26580eSLuigi Rizzo 	return 1;
30991a26580eSLuigi Rizzo }
31001a26580eSLuigi Rizzo 
310164ae02c3SLuigi Rizzo 
310201c7d25fSLuigi Rizzo /*
3103f9790aebSLuigi Rizzo  * Module loader and unloader
3104f196ce38SLuigi Rizzo  *
3105f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
3106f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
3107f9790aebSLuigi Rizzo  * (but there is no chance)
3108f9790aebSLuigi Rizzo  *
3109f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
3110f196ce38SLuigi Rizzo  */
3111babc7c12SLuigi Rizzo 
3112babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
3113f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
3114babc7c12SLuigi Rizzo 
311517885a7bSLuigi Rizzo 
3116f9790aebSLuigi Rizzo void
311768b8534bSLuigi Rizzo netmap_fini(void)
311868b8534bSLuigi Rizzo {
3119847bf383SLuigi Rizzo 	netmap_uninit_bridges();
3120f9790aebSLuigi Rizzo 	if (netmap_dev)
312168b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
3122ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
3123ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
312468b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
312568b8534bSLuigi Rizzo }
312668b8534bSLuigi Rizzo 
312717885a7bSLuigi Rizzo 
3128f9790aebSLuigi Rizzo int
3129f9790aebSLuigi Rizzo netmap_init(void)
313068b8534bSLuigi Rizzo {
3131f9790aebSLuigi Rizzo 	int error;
313268b8534bSLuigi Rizzo 
3133f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
313468b8534bSLuigi Rizzo 
3135f9790aebSLuigi Rizzo 	error = netmap_mem_init();
3136f9790aebSLuigi Rizzo 	if (error != 0)
3137f9790aebSLuigi Rizzo 		goto fail;
3138c929ca72SLuigi Rizzo 	/*
3139c929ca72SLuigi Rizzo 	 * MAKEDEV_ETERNAL_KLD avoids an expensive check on syscalls
3140c929ca72SLuigi Rizzo 	 * when the module is compiled in.
3141c929ca72SLuigi Rizzo 	 * XXX could use make_dev_credv() to get error number
3142c929ca72SLuigi Rizzo 	 */
31430e73f29aSLuigi Rizzo 	netmap_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
314411c0b69cSAdrian Chadd 		&netmap_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0600,
31450e73f29aSLuigi Rizzo 			      "netmap");
3146f9790aebSLuigi Rizzo 	if (!netmap_dev)
3147f9790aebSLuigi Rizzo 		goto fail;
3148f9790aebSLuigi Rizzo 
3149847bf383SLuigi Rizzo 	error = netmap_init_bridges();
3150847bf383SLuigi Rizzo 	if (error)
3151847bf383SLuigi Rizzo 		goto fail;
3152847bf383SLuigi Rizzo 
31534bf50f18SLuigi Rizzo #ifdef __FreeBSD__
31544bf50f18SLuigi Rizzo 	nm_vi_init_index();
31554bf50f18SLuigi Rizzo #endif
3156847bf383SLuigi Rizzo 
3157f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
3158f9790aebSLuigi Rizzo 	return (0);
3159f9790aebSLuigi Rizzo fail:
316068b8534bSLuigi Rizzo 	netmap_fini();
3161f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
316268b8534bSLuigi Rizzo }
3163