xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision cfa866f6)
1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi
537e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Luigi Rizzo
637e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Giuseppe Lettieri
737e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Vincenzo Maffione
837e3a6d3SLuigi Rizzo  * All rights reserved.
968b8534bSLuigi Rizzo  *
1068b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
1168b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
1268b8534bSLuigi Rizzo  * are met:
1368b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
1468b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1568b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1668b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1768b8534bSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1868b8534bSLuigi Rizzo  *
1968b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2068b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2168b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2268b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2368b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2468b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2568b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2668b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2768b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2868b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2968b8534bSLuigi Rizzo  * SUCH DAMAGE.
3068b8534bSLuigi Rizzo  */
3168b8534bSLuigi Rizzo 
32ce3ee1e7SLuigi Rizzo 
3368b8534bSLuigi Rizzo /*
34f9790aebSLuigi Rizzo  * $FreeBSD$
35f9790aebSLuigi Rizzo  *
3668b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3768b8534bSLuigi Rizzo  * see netmap(4).
3868b8534bSLuigi Rizzo  *
3968b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
4068b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
4168b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
4268b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
4368b8534bSLuigi Rizzo  *
4468b8534bSLuigi Rizzo  * Access to the network card works like this:
4568b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
4668b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4768b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4868b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4968b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
5068b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
5168b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
5268b8534bSLuigi Rizzo  *    the shared memory region.
5368b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
5468b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
5568b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
5668b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5768b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5868b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5968b8534bSLuigi Rizzo  *    packets on the output interface.
6068b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
6168b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
62ce3ee1e7SLuigi Rizzo  *
63ce3ee1e7SLuigi Rizzo 
64ce3ee1e7SLuigi Rizzo 		SYNCHRONIZATION (USER)
65ce3ee1e7SLuigi Rizzo 
66ce3ee1e7SLuigi Rizzo The netmap rings and data structures may be shared among multiple
67ce3ee1e7SLuigi Rizzo user threads or even independent processes.
68ce3ee1e7SLuigi Rizzo Any synchronization among those threads/processes is delegated
69ce3ee1e7SLuigi Rizzo to the threads themselves. Only one thread at a time can be in
70ce3ee1e7SLuigi Rizzo a system call on the same netmap ring. The OS does not enforce
71ce3ee1e7SLuigi Rizzo this and only guarantees against system crashes in case of
72ce3ee1e7SLuigi Rizzo invalid usage.
73ce3ee1e7SLuigi Rizzo 
74ce3ee1e7SLuigi Rizzo 		LOCKING (INTERNAL)
75ce3ee1e7SLuigi Rizzo 
76ce3ee1e7SLuigi Rizzo Within the kernel, access to the netmap rings is protected as follows:
77ce3ee1e7SLuigi Rizzo 
78ce3ee1e7SLuigi Rizzo - a spinlock on each ring, to handle producer/consumer races on
79ce3ee1e7SLuigi Rizzo   RX rings attached to the host stack (against multiple host
80ce3ee1e7SLuigi Rizzo   threads writing from the host stack to the same ring),
81ce3ee1e7SLuigi Rizzo   and on 'destination' rings attached to a VALE switch
82ce3ee1e7SLuigi Rizzo   (i.e. RX rings in VALE ports, and TX rings in NIC/host ports)
83ce3ee1e7SLuigi Rizzo   protecting multiple active senders for the same destination)
84ce3ee1e7SLuigi Rizzo 
85ce3ee1e7SLuigi Rizzo - an atomic variable to guarantee that there is at most one
86ce3ee1e7SLuigi Rizzo   instance of *_*xsync() on the ring at any time.
87ce3ee1e7SLuigi Rizzo   For rings connected to user file
88ce3ee1e7SLuigi Rizzo   descriptors, an atomic_test_and_set() protects this, and the
89ce3ee1e7SLuigi Rizzo   lock on the ring is not actually used.
90ce3ee1e7SLuigi Rizzo   For NIC RX rings connected to a VALE switch, an atomic_test_and_set()
91ce3ee1e7SLuigi Rizzo   is also used to prevent multiple executions (the driver might indeed
92ce3ee1e7SLuigi Rizzo   already guarantee this).
93ce3ee1e7SLuigi Rizzo   For NIC TX rings connected to a VALE switch, the lock arbitrates
94ce3ee1e7SLuigi Rizzo   access to the queue (both when allocating buffers and when pushing
95ce3ee1e7SLuigi Rizzo   them out).
96ce3ee1e7SLuigi Rizzo 
97ce3ee1e7SLuigi Rizzo - *xsync() should be protected against initializations of the card.
98ce3ee1e7SLuigi Rizzo   On FreeBSD most devices have the reset routine protected by
99ce3ee1e7SLuigi Rizzo   a RING lock (ixgbe, igb, em) or core lock (re). lem is missing
100ce3ee1e7SLuigi Rizzo   the RING protection on rx_reset(), this should be added.
101ce3ee1e7SLuigi Rizzo 
102ce3ee1e7SLuigi Rizzo   On linux there is an external lock on the tx path, which probably
103ce3ee1e7SLuigi Rizzo   also arbitrates access to the reset routine. XXX to be revised
104ce3ee1e7SLuigi Rizzo 
105ce3ee1e7SLuigi Rizzo - a per-interface core_lock protecting access from the host stack
106ce3ee1e7SLuigi Rizzo   while interfaces may be detached from netmap mode.
107ce3ee1e7SLuigi Rizzo   XXX there should be no need for this lock if we detach the interfaces
108ce3ee1e7SLuigi Rizzo   only while they are down.
109ce3ee1e7SLuigi Rizzo 
110ce3ee1e7SLuigi Rizzo 
111ce3ee1e7SLuigi Rizzo --- VALE SWITCH ---
112ce3ee1e7SLuigi Rizzo 
113ce3ee1e7SLuigi Rizzo NMG_LOCK() serializes all modifications to switches and ports.
114ce3ee1e7SLuigi Rizzo A switch cannot be deleted until all ports are gone.
115ce3ee1e7SLuigi Rizzo 
116ce3ee1e7SLuigi Rizzo For each switch, an SX lock (RWlock on linux) protects
117ce3ee1e7SLuigi Rizzo deletion of ports. When configuring or deleting a new port, the
118ce3ee1e7SLuigi Rizzo lock is acquired in exclusive mode (after holding NMG_LOCK).
119ce3ee1e7SLuigi Rizzo When forwarding, the lock is acquired in shared mode (without NMG_LOCK).
120ce3ee1e7SLuigi Rizzo The lock is held throughout the entire forwarding cycle,
121ce3ee1e7SLuigi Rizzo during which the thread may incur in a page fault.
122ce3ee1e7SLuigi Rizzo Hence it is important that sleepable shared locks are used.
123ce3ee1e7SLuigi Rizzo 
124ce3ee1e7SLuigi Rizzo On the rx ring, the per-port lock is grabbed initially to reserve
125ce3ee1e7SLuigi Rizzo a number of slot in the ring, then the lock is released,
126ce3ee1e7SLuigi Rizzo packets are copied from source to destination, and then
127ce3ee1e7SLuigi Rizzo the lock is acquired again and the receive ring is updated.
128ce3ee1e7SLuigi Rizzo (A similar thing is done on the tx ring for NIC and host stack
129ce3ee1e7SLuigi Rizzo ports attached to the switch)
130ce3ee1e7SLuigi Rizzo 
13168b8534bSLuigi Rizzo  */
13268b8534bSLuigi Rizzo 
1334bf50f18SLuigi Rizzo 
1344bf50f18SLuigi Rizzo /* --- internals ----
1354bf50f18SLuigi Rizzo  *
1364bf50f18SLuigi Rizzo  * Roadmap to the code that implements the above.
1374bf50f18SLuigi Rizzo  *
1384bf50f18SLuigi Rizzo  * > 1. a process/thread issues one or more open() on /dev/netmap, to create
1394bf50f18SLuigi Rizzo  * >    select()able file descriptor on which events are reported.
1404bf50f18SLuigi Rizzo  *
1414bf50f18SLuigi Rizzo  *  	Internally, we allocate a netmap_priv_d structure, that will be
14237e3a6d3SLuigi Rizzo  *  	initialized on ioctl(NIOCREGIF). There is one netmap_priv_d
14337e3a6d3SLuigi Rizzo  *  	structure for each open().
1444bf50f18SLuigi Rizzo  *
1454bf50f18SLuigi Rizzo  *      os-specific:
14637e3a6d3SLuigi Rizzo  *  	    FreeBSD: see netmap_open() (netmap_freebsd.c)
14737e3a6d3SLuigi Rizzo  *  	    linux:   see linux_netmap_open() (netmap_linux.c)
1484bf50f18SLuigi Rizzo  *
1494bf50f18SLuigi Rizzo  * > 2. on each descriptor, the process issues an ioctl() to identify
1504bf50f18SLuigi Rizzo  * >    the interface that should report events to the file descriptor.
1514bf50f18SLuigi Rizzo  *
1524bf50f18SLuigi Rizzo  * 	Implemented by netmap_ioctl(), NIOCREGIF case, with nmr->nr_cmd==0.
1534bf50f18SLuigi Rizzo  * 	Most important things happen in netmap_get_na() and
1544bf50f18SLuigi Rizzo  * 	netmap_do_regif(), called from there. Additional details can be
1554bf50f18SLuigi Rizzo  * 	found in the comments above those functions.
1564bf50f18SLuigi Rizzo  *
1574bf50f18SLuigi Rizzo  * 	In all cases, this action creates/takes-a-reference-to a
1584bf50f18SLuigi Rizzo  * 	netmap_*_adapter describing the port, and allocates a netmap_if
1594bf50f18SLuigi Rizzo  * 	and all necessary netmap rings, filling them with netmap buffers.
1604bf50f18SLuigi Rizzo  *
1614bf50f18SLuigi Rizzo  *      In this phase, the sync callbacks for each ring are set (these are used
1624bf50f18SLuigi Rizzo  *      in steps 5 and 6 below).  The callbacks depend on the type of adapter.
1634bf50f18SLuigi Rizzo  *      The adapter creation/initialization code puts them in the
1644bf50f18SLuigi Rizzo  * 	netmap_adapter (fields na->nm_txsync and na->nm_rxsync).  Then, they
1654bf50f18SLuigi Rizzo  * 	are copied from there to the netmap_kring's during netmap_do_regif(), by
1664bf50f18SLuigi Rizzo  * 	the nm_krings_create() callback.  All the nm_krings_create callbacks
1674bf50f18SLuigi Rizzo  * 	actually call netmap_krings_create() to perform this and the other
1684bf50f18SLuigi Rizzo  * 	common stuff. netmap_krings_create() also takes care of the host rings,
1694bf50f18SLuigi Rizzo  * 	if needed, by setting their sync callbacks appropriately.
1704bf50f18SLuigi Rizzo  *
1714bf50f18SLuigi Rizzo  * 	Additional actions depend on the kind of netmap_adapter that has been
1724bf50f18SLuigi Rizzo  * 	registered:
1734bf50f18SLuigi Rizzo  *
1744bf50f18SLuigi Rizzo  * 	- netmap_hw_adapter:  	     [netmap.c]
1754bf50f18SLuigi Rizzo  * 	     This is a system netdev/ifp with native netmap support.
1764bf50f18SLuigi Rizzo  * 	     The ifp is detached from the host stack by redirecting:
1774bf50f18SLuigi Rizzo  * 	       - transmissions (from the network stack) to netmap_transmit()
1784bf50f18SLuigi Rizzo  * 	       - receive notifications to the nm_notify() callback for
1794bf50f18SLuigi Rizzo  * 	         this adapter. The callback is normally netmap_notify(), unless
1804bf50f18SLuigi Rizzo  * 	         the ifp is attached to a bridge using bwrap, in which case it
1814bf50f18SLuigi Rizzo  * 	         is netmap_bwrap_intr_notify().
1824bf50f18SLuigi Rizzo  *
1834bf50f18SLuigi Rizzo  * 	- netmap_generic_adapter:      [netmap_generic.c]
1844bf50f18SLuigi Rizzo  * 	      A system netdev/ifp without native netmap support.
1854bf50f18SLuigi Rizzo  *
1864bf50f18SLuigi Rizzo  * 	(the decision about native/non native support is taken in
1874bf50f18SLuigi Rizzo  * 	 netmap_get_hw_na(), called by netmap_get_na())
1884bf50f18SLuigi Rizzo  *
1894bf50f18SLuigi Rizzo  * 	- netmap_vp_adapter 		[netmap_vale.c]
1904bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_bdg_na().
1914bf50f18SLuigi Rizzo  * 	      This is a persistent or ephemeral VALE port. Ephemeral ports
1924bf50f18SLuigi Rizzo  * 	      are created on the fly if they don't already exist, and are
1934bf50f18SLuigi Rizzo  * 	      always attached to a bridge.
194453130d9SPedro F. Giffuni  * 	      Persistent VALE ports must must be created separately, and i
1954bf50f18SLuigi Rizzo  * 	      then attached like normal NICs. The NIOCREGIF we are examining
1964bf50f18SLuigi Rizzo  * 	      will find them only if they had previosly been created and
1974bf50f18SLuigi Rizzo  * 	      attached (see VALE_CTL below).
1984bf50f18SLuigi Rizzo  *
1994bf50f18SLuigi Rizzo  * 	- netmap_pipe_adapter 	      [netmap_pipe.c]
2004bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_pipe_na().
2014bf50f18SLuigi Rizzo  * 	      Both pipe ends are created, if they didn't already exist.
2024bf50f18SLuigi Rizzo  *
2034bf50f18SLuigi Rizzo  * 	- netmap_monitor_adapter      [netmap_monitor.c]
2044bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_monitor_na().
2054bf50f18SLuigi Rizzo  * 	      If successful, the nm_sync callbacks of the monitored adapter
2064bf50f18SLuigi Rizzo  * 	      will be intercepted by the returned monitor.
2074bf50f18SLuigi Rizzo  *
2084bf50f18SLuigi Rizzo  * 	- netmap_bwrap_adapter	      [netmap_vale.c]
2094bf50f18SLuigi Rizzo  * 	      Cannot be obtained in this way, see VALE_CTL below
2104bf50f18SLuigi Rizzo  *
2114bf50f18SLuigi Rizzo  *
2124bf50f18SLuigi Rizzo  * 	os-specific:
2134bf50f18SLuigi Rizzo  * 	    linux: we first go through linux_netmap_ioctl() to
2144bf50f18SLuigi Rizzo  * 	           adapt the FreeBSD interface to the linux one.
2154bf50f18SLuigi Rizzo  *
2164bf50f18SLuigi Rizzo  *
2174bf50f18SLuigi Rizzo  * > 3. on each descriptor, the process issues an mmap() request to
2184bf50f18SLuigi Rizzo  * >    map the shared memory region within the process' address space.
2194bf50f18SLuigi Rizzo  * >    The list of interesting queues is indicated by a location in
2204bf50f18SLuigi Rizzo  * >    the shared memory region.
2214bf50f18SLuigi Rizzo  *
2224bf50f18SLuigi Rizzo  *      os-specific:
2234bf50f18SLuigi Rizzo  *  	    FreeBSD: netmap_mmap_single (netmap_freebsd.c).
2244bf50f18SLuigi Rizzo  *  	    linux:   linux_netmap_mmap (netmap_linux.c).
2254bf50f18SLuigi Rizzo  *
2264bf50f18SLuigi Rizzo  * > 4. using the functions in the netmap(4) userspace API, a process
2274bf50f18SLuigi Rizzo  * >    can look up the occupation state of a queue, access memory buffers,
2284bf50f18SLuigi Rizzo  * >    and retrieve received packets or enqueue packets to transmit.
2294bf50f18SLuigi Rizzo  *
2304bf50f18SLuigi Rizzo  * 	these actions do not involve the kernel.
2314bf50f18SLuigi Rizzo  *
2324bf50f18SLuigi Rizzo  * > 5. using some ioctl()s the process can synchronize the userspace view
2334bf50f18SLuigi Rizzo  * >    of the queue with the actual status in the kernel. This includes both
2344bf50f18SLuigi Rizzo  * >    receiving the notification of new packets, and transmitting new
2354bf50f18SLuigi Rizzo  * >    packets on the output interface.
2364bf50f18SLuigi Rizzo  *
2374bf50f18SLuigi Rizzo  * 	These are implemented in netmap_ioctl(), NIOCTXSYNC and NIOCRXSYNC
2384bf50f18SLuigi Rizzo  * 	cases. They invoke the nm_sync callbacks on the netmap_kring
2394bf50f18SLuigi Rizzo  * 	structures, as initialized in step 2 and maybe later modified
2404bf50f18SLuigi Rizzo  * 	by a monitor. Monitors, however, will always call the original
2414bf50f18SLuigi Rizzo  * 	callback before doing anything else.
2424bf50f18SLuigi Rizzo  *
2434bf50f18SLuigi Rizzo  *
2444bf50f18SLuigi Rizzo  * > 6. select() or poll() can be used to wait for events on individual
2454bf50f18SLuigi Rizzo  * >    transmit or receive queues (or all queues for a given interface).
2464bf50f18SLuigi Rizzo  *
2474bf50f18SLuigi Rizzo  * 	Implemented in netmap_poll(). This will call the same nm_sync()
2484bf50f18SLuigi Rizzo  * 	callbacks as in step 5 above.
2494bf50f18SLuigi Rizzo  *
2504bf50f18SLuigi Rizzo  * 	os-specific:
2514bf50f18SLuigi Rizzo  * 		linux: we first go through linux_netmap_poll() to adapt
2524bf50f18SLuigi Rizzo  * 		       the FreeBSD interface to the linux one.
2534bf50f18SLuigi Rizzo  *
2544bf50f18SLuigi Rizzo  *
2554bf50f18SLuigi Rizzo  *  ----  VALE_CTL -----
2564bf50f18SLuigi Rizzo  *
2574bf50f18SLuigi Rizzo  *  VALE switches are controlled by issuing a NIOCREGIF with a non-null
2584bf50f18SLuigi Rizzo  *  nr_cmd in the nmreq structure. These subcommands are handled by
2594bf50f18SLuigi Rizzo  *  netmap_bdg_ctl() in netmap_vale.c. Persistent VALE ports are created
2604bf50f18SLuigi Rizzo  *  and destroyed by issuing the NETMAP_BDG_NEWIF and NETMAP_BDG_DELIF
2614bf50f18SLuigi Rizzo  *  subcommands, respectively.
2624bf50f18SLuigi Rizzo  *
2634bf50f18SLuigi Rizzo  *  Any network interface known to the system (including a persistent VALE
2644bf50f18SLuigi Rizzo  *  port) can be attached to a VALE switch by issuing the
2652ff91c17SVincenzo Maffione  *  NETMAP_REQ_VALE_ATTACH command. After the attachment, persistent VALE ports
2664bf50f18SLuigi Rizzo  *  look exactly like ephemeral VALE ports (as created in step 2 above).  The
2674bf50f18SLuigi Rizzo  *  attachment of other interfaces, instead, requires the creation of a
2684bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  Moreover, the attached interface must be put in
2694bf50f18SLuigi Rizzo  *  netmap mode. This may require the creation of a netmap_generic_adapter if
2704bf50f18SLuigi Rizzo  *  we have no native support for the interface, or if generic adapters have
2714bf50f18SLuigi Rizzo  *  been forced by sysctl.
2724bf50f18SLuigi Rizzo  *
2734bf50f18SLuigi Rizzo  *  Both persistent VALE ports and bwraps are handled by netmap_get_bdg_na(),
2744bf50f18SLuigi Rizzo  *  called by nm_bdg_ctl_attach(), and discriminated by the nm_bdg_attach()
2754bf50f18SLuigi Rizzo  *  callback.  In the case of the bwrap, the callback creates the
2764bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  The initialization of the bwrap is then
2774bf50f18SLuigi Rizzo  *  completed by calling netmap_do_regif() on it, in the nm_bdg_ctl()
2784bf50f18SLuigi Rizzo  *  callback (netmap_bwrap_bdg_ctl in netmap_vale.c).
2794bf50f18SLuigi Rizzo  *  A generic adapter for the wrapped ifp will be created if needed, when
2804bf50f18SLuigi Rizzo  *  netmap_get_bdg_na() calls netmap_get_hw_na().
2814bf50f18SLuigi Rizzo  *
2824bf50f18SLuigi Rizzo  *
2834bf50f18SLuigi Rizzo  *  ---- DATAPATHS -----
2844bf50f18SLuigi Rizzo  *
2854bf50f18SLuigi Rizzo  *              -= SYSTEM DEVICE WITH NATIVE SUPPORT =-
2864bf50f18SLuigi Rizzo  *
2874bf50f18SLuigi Rizzo  *    na == NA(ifp) == netmap_hw_adapter created in DEVICE_netmap_attach()
2884bf50f18SLuigi Rizzo  *
2894bf50f18SLuigi Rizzo  *    - tx from netmap userspace:
2904bf50f18SLuigi Rizzo  *	 concurrently:
2914bf50f18SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
2924bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_txsync()
2934bf50f18SLuigi Rizzo  *           2) device interrupt handler
2944bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
2954bf50f18SLuigi Rizzo  *    - rx from netmap userspace:
2964bf50f18SLuigi Rizzo  *       concurrently:
2974bf50f18SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
2984bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_rxsync()
2994bf50f18SLuigi Rizzo  *           2) device interrupt handler
3004bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
301847bf383SLuigi Rizzo  *    - rx from host stack
3024bf50f18SLuigi Rizzo  *       concurrently:
3034bf50f18SLuigi Rizzo  *           1) host stack
3044bf50f18SLuigi Rizzo  *                netmap_transmit()
3054bf50f18SLuigi Rizzo  *                  na->nm_notify  == netmap_notify()
3064bf50f18SLuigi Rizzo  *           2) ioctl(NIOCRXSYNC)/netmap_poll() in process context
30737e3a6d3SLuigi Rizzo  *                kring->nm_sync() == netmap_rxsync_from_host
3084bf50f18SLuigi Rizzo  *                  netmap_rxsync_from_host(na, NULL, NULL)
3094bf50f18SLuigi Rizzo  *    - tx to host stack
3104bf50f18SLuigi Rizzo  *           ioctl(NIOCTXSYNC)/netmap_poll() in process context
31137e3a6d3SLuigi Rizzo  *             kring->nm_sync() == netmap_txsync_to_host
3124bf50f18SLuigi Rizzo  *               netmap_txsync_to_host(na)
31337e3a6d3SLuigi Rizzo  *                 nm_os_send_up()
31437e3a6d3SLuigi Rizzo  *                   FreeBSD: na->if_input() == ether_input()
3154bf50f18SLuigi Rizzo  *                   linux: netif_rx() with NM_MAGIC_PRIORITY_RX
3164bf50f18SLuigi Rizzo  *
3174bf50f18SLuigi Rizzo  *
3184bf50f18SLuigi Rizzo  *               -= SYSTEM DEVICE WITH GENERIC SUPPORT =-
3194bf50f18SLuigi Rizzo  *
320847bf383SLuigi Rizzo  *    na == NA(ifp) == generic_netmap_adapter created in generic_netmap_attach()
321847bf383SLuigi Rizzo  *
322847bf383SLuigi Rizzo  *    - tx from netmap userspace:
323847bf383SLuigi Rizzo  *       concurrently:
324847bf383SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
325847bf383SLuigi Rizzo  *               kring->nm_sync() == generic_netmap_txsync()
32637e3a6d3SLuigi Rizzo  *                   nm_os_generic_xmit_frame()
327847bf383SLuigi Rizzo  *                       linux:   dev_queue_xmit() with NM_MAGIC_PRIORITY_TX
32837e3a6d3SLuigi Rizzo  *                           ifp->ndo_start_xmit == generic_ndo_start_xmit()
32937e3a6d3SLuigi Rizzo  *                               gna->save_start_xmit == orig. dev. start_xmit
330847bf383SLuigi Rizzo  *                       FreeBSD: na->if_transmit() == orig. dev if_transmit
331847bf383SLuigi Rizzo  *           2) generic_mbuf_destructor()
332847bf383SLuigi Rizzo  *                   na->nm_notify() == netmap_notify()
333847bf383SLuigi Rizzo  *    - rx from netmap userspace:
334847bf383SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
335847bf383SLuigi Rizzo  *               kring->nm_sync() == generic_netmap_rxsync()
336847bf383SLuigi Rizzo  *                   mbq_safe_dequeue()
337847bf383SLuigi Rizzo  *           2) device driver
338847bf383SLuigi Rizzo  *               generic_rx_handler()
339847bf383SLuigi Rizzo  *                   mbq_safe_enqueue()
340847bf383SLuigi Rizzo  *                   na->nm_notify() == netmap_notify()
34137e3a6d3SLuigi Rizzo  *    - rx from host stack
34237e3a6d3SLuigi Rizzo  *        FreeBSD: same as native
34337e3a6d3SLuigi Rizzo  *        Linux: same as native except:
344847bf383SLuigi Rizzo  *           1) host stack
34537e3a6d3SLuigi Rizzo  *               dev_queue_xmit() without NM_MAGIC_PRIORITY_TX
34637e3a6d3SLuigi Rizzo  *                   ifp->ndo_start_xmit == generic_ndo_start_xmit()
347847bf383SLuigi Rizzo  *                       netmap_transmit()
348847bf383SLuigi Rizzo  *                           na->nm_notify() == netmap_notify()
34937e3a6d3SLuigi Rizzo  *    - tx to host stack (same as native):
3504bf50f18SLuigi Rizzo  *
3514bf50f18SLuigi Rizzo  *
352847bf383SLuigi Rizzo  *                           -= VALE =-
3534bf50f18SLuigi Rizzo  *
354847bf383SLuigi Rizzo  *   INCOMING:
3554bf50f18SLuigi Rizzo  *
356847bf383SLuigi Rizzo  *      - VALE ports:
357847bf383SLuigi Rizzo  *          ioctl(NIOCTXSYNC)/netmap_poll() in process context
358847bf383SLuigi Rizzo  *              kring->nm_sync() == netmap_vp_txsync()
3594bf50f18SLuigi Rizzo  *
360847bf383SLuigi Rizzo  *      - system device with native support:
361847bf383SLuigi Rizzo  *         from cable:
362847bf383SLuigi Rizzo  *             interrupt
363847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr != host ring)
364847bf383SLuigi Rizzo  *                     kring->nm_sync() == DEVICE_netmap_rxsync()
365847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
366847bf383SLuigi Rizzo  *                     kring->nm_sync() == DEVICE_netmap_rxsync()
367847bf383SLuigi Rizzo  *         from host stack:
368847bf383SLuigi Rizzo  *             netmap_transmit()
369847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr == host ring)
37037e3a6d3SLuigi Rizzo  *                     kring->nm_sync() == netmap_rxsync_from_host()
371847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
3724bf50f18SLuigi Rizzo  *
373847bf383SLuigi Rizzo  *      - system device with generic support:
374847bf383SLuigi Rizzo  *         from device driver:
375847bf383SLuigi Rizzo  *            generic_rx_handler()
376847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr != host ring)
377847bf383SLuigi Rizzo  *                     kring->nm_sync() == generic_netmap_rxsync()
378847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
379847bf383SLuigi Rizzo  *                     kring->nm_sync() == generic_netmap_rxsync()
380847bf383SLuigi Rizzo  *         from host stack:
381847bf383SLuigi Rizzo  *            netmap_transmit()
382847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr == host ring)
38337e3a6d3SLuigi Rizzo  *                     kring->nm_sync() == netmap_rxsync_from_host()
384847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
3854bf50f18SLuigi Rizzo  *
386847bf383SLuigi Rizzo  *   (all cases) --> nm_bdg_flush()
387847bf383SLuigi Rizzo  *                      dest_na->nm_notify() == (see below)
3884bf50f18SLuigi Rizzo  *
389847bf383SLuigi Rizzo  *   OUTGOING:
3904bf50f18SLuigi Rizzo  *
391847bf383SLuigi Rizzo  *      - VALE ports:
392847bf383SLuigi Rizzo  *         concurrently:
393c3e9b4dbSLuiz Otavio O Souza  *             1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
394847bf383SLuigi Rizzo  *                    kring->nm_sync() == netmap_vp_rxsync()
395847bf383SLuigi Rizzo  *             2) from nm_bdg_flush()
396847bf383SLuigi Rizzo  *                    na->nm_notify() == netmap_notify()
3974bf50f18SLuigi Rizzo  *
398847bf383SLuigi Rizzo  *      - system device with native support:
399847bf383SLuigi Rizzo  *          to cable:
400847bf383SLuigi Rizzo  *             na->nm_notify() == netmap_bwrap_notify()
401847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
402847bf383SLuigi Rizzo  *                 kring->nm_sync() == DEVICE_netmap_txsync()
403847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
404847bf383SLuigi Rizzo  *          to host stack:
405847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
40637e3a6d3SLuigi Rizzo  *                 kring->nm_sync() == netmap_txsync_to_host
407847bf383SLuigi Rizzo  *                 netmap_vp_rxsync_locked()
4084bf50f18SLuigi Rizzo  *
409847bf383SLuigi Rizzo  *      - system device with generic adapter:
410847bf383SLuigi Rizzo  *          to device driver:
411847bf383SLuigi Rizzo  *             na->nm_notify() == netmap_bwrap_notify()
412847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
413847bf383SLuigi Rizzo  *                 kring->nm_sync() == generic_netmap_txsync()
414847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
415847bf383SLuigi Rizzo  *          to host stack:
416847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
41737e3a6d3SLuigi Rizzo  *                 kring->nm_sync() == netmap_txsync_to_host
418847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
4194bf50f18SLuigi Rizzo  *
4204bf50f18SLuigi Rizzo  */
4214bf50f18SLuigi Rizzo 
422ce3ee1e7SLuigi Rizzo /*
423ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
424ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
425ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
426ce3ee1e7SLuigi Rizzo  */
42701c7d25fSLuigi Rizzo 
428ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
42968b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
43068b8534bSLuigi Rizzo #include <sys/types.h>
43168b8534bSLuigi Rizzo #include <sys/errno.h>
43268b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
43368b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
434f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
43589e3fd52SLuigi Rizzo #include <sys/filio.h>	/* FIONBIO */
43668b8534bSLuigi Rizzo #include <sys/sockio.h>
43768b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
43868b8534bSLuigi Rizzo #include <sys/malloc.h>
43968b8534bSLuigi Rizzo #include <sys/poll.h>
44089f6b863SAttilio Rao #include <sys/rwlock.h>
44168b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44268b8534bSLuigi Rizzo #include <sys/selinfo.h>
44368b8534bSLuigi Rizzo #include <sys/sysctl.h>
444339f59c0SGleb Smirnoff #include <sys/jail.h>
445339f59c0SGleb Smirnoff #include <net/vnet.h>
44668b8534bSLuigi Rizzo #include <net/if.h>
44776039bc8SGleb Smirnoff #include <net/if_var.h>
44868b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
44968b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
450ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
451ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
45268b8534bSLuigi Rizzo 
45368b8534bSLuigi Rizzo 
454ce3ee1e7SLuigi Rizzo #elif defined(linux)
455ce3ee1e7SLuigi Rizzo 
456ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
457ce3ee1e7SLuigi Rizzo 
458ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
459ce3ee1e7SLuigi Rizzo 
460ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
461ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
462ce3ee1e7SLuigi Rizzo 
46337e3a6d3SLuigi Rizzo #elif defined (_WIN32)
46437e3a6d3SLuigi Rizzo 
46537e3a6d3SLuigi Rizzo #include "win_glue.h"
46637e3a6d3SLuigi Rizzo 
467ce3ee1e7SLuigi Rizzo #else
468ce3ee1e7SLuigi Rizzo 
469ce3ee1e7SLuigi Rizzo #error	Unsupported platform
470ce3ee1e7SLuigi Rizzo 
471ce3ee1e7SLuigi Rizzo #endif /* unsupported */
472ce3ee1e7SLuigi Rizzo 
473ce3ee1e7SLuigi Rizzo /*
474ce3ee1e7SLuigi Rizzo  * common headers
475ce3ee1e7SLuigi Rizzo  */
4760b8ed8e0SLuigi Rizzo #include <net/netmap.h>
4770b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
478ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
4790b8ed8e0SLuigi Rizzo 
480ce3ee1e7SLuigi Rizzo 
4815819da83SLuigi Rizzo /* user-controlled variables */
4825819da83SLuigi Rizzo int netmap_verbose;
4835819da83SLuigi Rizzo 
4845819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
485c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
486f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
487c3e9b4dbSLuiz Otavio O Souza static int netmap_fwd = 0;	/* force transparent forwarding */
488f196ce38SLuigi Rizzo 
489f9790aebSLuigi Rizzo /*
490f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
491f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
492f9790aebSLuigi Rizzo  */
493f9790aebSLuigi Rizzo enum {	NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
494f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
495f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
496f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
497f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
498f9790aebSLuigi Rizzo 
49937e3a6d3SLuigi Rizzo /* netmap_generic_mit controls mitigation of RX notifications for
50037e3a6d3SLuigi Rizzo  * the generic netmap adapter. The value is a time interval in
50137e3a6d3SLuigi Rizzo  * nanoseconds. */
50237e3a6d3SLuigi Rizzo int netmap_generic_mit = 100*1000;
50337e3a6d3SLuigi Rizzo 
50437e3a6d3SLuigi Rizzo /* We use by default netmap-aware qdiscs with generic netmap adapters,
50537e3a6d3SLuigi Rizzo  * even if there can be a little performance hit with hardware NICs.
50637e3a6d3SLuigi Rizzo  * However, using the qdisc is the safer approach, for two reasons:
50737e3a6d3SLuigi Rizzo  * 1) it prevents non-fifo qdiscs to break the TX notification
50837e3a6d3SLuigi Rizzo  *    scheme, which is based on mbuf destructors when txqdisc is
50937e3a6d3SLuigi Rizzo  *    not used.
51037e3a6d3SLuigi Rizzo  * 2) it makes it possible to transmit over software devices that
51137e3a6d3SLuigi Rizzo  *    change skb->dev, like bridge, veth, ...
51237e3a6d3SLuigi Rizzo  *
51337e3a6d3SLuigi Rizzo  * Anyway users looking for the best performance should
51437e3a6d3SLuigi Rizzo  * use native adapters.
51537e3a6d3SLuigi Rizzo  */
5164f80b14cSVincenzo Maffione #ifdef linux
51737e3a6d3SLuigi Rizzo int netmap_generic_txqdisc = 1;
5184f80b14cSVincenzo Maffione #endif
51937e3a6d3SLuigi Rizzo 
52037e3a6d3SLuigi Rizzo /* Default number of slots and queues for generic adapters. */
52137e3a6d3SLuigi Rizzo int netmap_generic_ringsize = 1024;
52237e3a6d3SLuigi Rizzo int netmap_generic_rings = 1;
52337e3a6d3SLuigi Rizzo 
52437e3a6d3SLuigi Rizzo /* Non-zero if ptnet devices are allowed to use virtio-net headers. */
52537e3a6d3SLuigi Rizzo int ptnet_vnet_hdr = 1;
52637e3a6d3SLuigi Rizzo 
527c3e9b4dbSLuiz Otavio O Souza /* 0 if ptnetmap should not use worker threads for TX processing */
528c3e9b4dbSLuiz Otavio O Souza int ptnetmap_tx_workers = 1;
529c3e9b4dbSLuiz Otavio O Souza 
53037e3a6d3SLuigi Rizzo /*
53137e3a6d3SLuigi Rizzo  * SYSCTL calls are grouped between SYSBEGIN and SYSEND to be emulated
53237e3a6d3SLuigi Rizzo  * in some other operating systems
53337e3a6d3SLuigi Rizzo  */
53437e3a6d3SLuigi Rizzo SYSBEGIN(main_init);
53537e3a6d3SLuigi Rizzo 
53637e3a6d3SLuigi Rizzo SYSCTL_DECL(_dev_netmap);
53737e3a6d3SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
53837e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
53937e3a6d3SLuigi Rizzo 		CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
54037e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
54137e3a6d3SLuigi Rizzo 		CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
5424f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, CTLFLAG_RW, &netmap_no_pendintr,
5434f80b14cSVincenzo Maffione 		0, "Always look for new received packets.");
54437e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
54537e3a6d3SLuigi Rizzo 		&netmap_txsync_retry, 0, "Number of txsync loops in bridge's flush.");
546f9790aebSLuigi Rizzo 
5474f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0,
5484f80b14cSVincenzo Maffione 		"Force NR_FORWARD mode");
5494f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0,
5504f80b14cSVincenzo Maffione 		"Adapter mode. 0 selects the best option available,"
5514f80b14cSVincenzo Maffione 		"1 forces native adapter, 2 forces emulated adapter");
5524f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit,
5534f80b14cSVincenzo Maffione 		0, "RX notification interval in nanoseconds");
5544f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW,
5554f80b14cSVincenzo Maffione 		&netmap_generic_ringsize, 0,
5564f80b14cSVincenzo Maffione 		"Number of per-ring slots for emulated netmap mode");
5574f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW,
5584f80b14cSVincenzo Maffione 		&netmap_generic_rings, 0,
5594f80b14cSVincenzo Maffione 		"Number of TX/RX queues for emulated netmap adapters");
5604f80b14cSVincenzo Maffione #ifdef linux
5614f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_txqdisc, CTLFLAG_RW,
5624f80b14cSVincenzo Maffione 		&netmap_generic_txqdisc, 0, "Use qdisc for generic adapters");
5634f80b14cSVincenzo Maffione #endif
5644f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, ptnet_vnet_hdr, CTLFLAG_RW, &ptnet_vnet_hdr,
5654f80b14cSVincenzo Maffione 		0, "Allow ptnet devices to use virtio-net headers");
5664f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, ptnetmap_tx_workers, CTLFLAG_RW,
5674f80b14cSVincenzo Maffione 		&ptnetmap_tx_workers, 0, "Use worker threads for pnetmap TX processing");
56837e3a6d3SLuigi Rizzo 
56937e3a6d3SLuigi Rizzo SYSEND;
570f196ce38SLuigi Rizzo 
571ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
572ce3ee1e7SLuigi Rizzo 
57317885a7bSLuigi Rizzo /*
57417885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
57517885a7bSLuigi Rizzo  * to make sure other users get to see it.
57637e3a6d3SLuigi Rizzo  * stopped must be either NR_KR_STOPPED (for unbounded stop)
57737e3a6d3SLuigi Rizzo  * of NR_KR_LOCKED (brief stop for mutual exclusion purposes)
57817885a7bSLuigi Rizzo  */
5794bf50f18SLuigi Rizzo static void
58037e3a6d3SLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr, int stopped)
581ce3ee1e7SLuigi Rizzo {
58237e3a6d3SLuigi Rizzo 	nm_kr_stop(kr, stopped);
58337e3a6d3SLuigi Rizzo 	// XXX check if nm_kr_stop is sufficient
584ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
585ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
586ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
587ce3ee1e7SLuigi Rizzo }
588ce3ee1e7SLuigi Rizzo 
589847bf383SLuigi Rizzo /* stop or enable a single ring */
5904bf50f18SLuigi Rizzo void
591847bf383SLuigi Rizzo netmap_set_ring(struct netmap_adapter *na, u_int ring_id, enum txrx t, int stopped)
5924bf50f18SLuigi Rizzo {
5934bf50f18SLuigi Rizzo 	if (stopped)
5942ff91c17SVincenzo Maffione 		netmap_disable_ring(NMR(na, t)[ring_id], stopped);
5954bf50f18SLuigi Rizzo 	else
5962ff91c17SVincenzo Maffione 		NMR(na, t)[ring_id]->nkr_stopped = 0;
5974bf50f18SLuigi Rizzo }
5984bf50f18SLuigi Rizzo 
599f9790aebSLuigi Rizzo 
60089cc2556SLuigi Rizzo /* stop or enable all the rings of na */
6014bf50f18SLuigi Rizzo void
6024bf50f18SLuigi Rizzo netmap_set_all_rings(struct netmap_adapter *na, int stopped)
603ce3ee1e7SLuigi Rizzo {
604ce3ee1e7SLuigi Rizzo 	int i;
605847bf383SLuigi Rizzo 	enum txrx t;
606ce3ee1e7SLuigi Rizzo 
6074bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
608ce3ee1e7SLuigi Rizzo 		return;
609ce3ee1e7SLuigi Rizzo 
610847bf383SLuigi Rizzo 	for_rx_tx(t) {
611847bf383SLuigi Rizzo 		for (i = 0; i < netmap_real_rings(na, t); i++) {
612847bf383SLuigi Rizzo 			netmap_set_ring(na, i, t, stopped);
613ce3ee1e7SLuigi Rizzo 		}
614ce3ee1e7SLuigi Rizzo 	}
615ce3ee1e7SLuigi Rizzo }
616ce3ee1e7SLuigi Rizzo 
61789cc2556SLuigi Rizzo /*
61889cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
61989cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
620ddb13598SKevin Lo  * netmap mode off, or before removing the hardware rings (e.g., on module
62137e3a6d3SLuigi Rizzo  * onload).
62289cc2556SLuigi Rizzo  */
623f9790aebSLuigi Rizzo void
624f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
625f9790aebSLuigi Rizzo {
62637e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
62737e3a6d3SLuigi Rizzo 		netmap_set_all_rings(NA(ifp), NM_KR_STOPPED);
62837e3a6d3SLuigi Rizzo 	}
629f9790aebSLuigi Rizzo }
630f9790aebSLuigi Rizzo 
63189cc2556SLuigi Rizzo /*
63289cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
63389cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
63489cc2556SLuigi Rizzo  * napi_enable().
63589cc2556SLuigi Rizzo  */
636f9790aebSLuigi Rizzo void
637f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
638f9790aebSLuigi Rizzo {
63937e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
6404bf50f18SLuigi Rizzo 		netmap_set_all_rings(NA(ifp), 0 /* enabled */);
641f9790aebSLuigi Rizzo 	}
64237e3a6d3SLuigi Rizzo }
643f9790aebSLuigi Rizzo 
64437e3a6d3SLuigi Rizzo void
64537e3a6d3SLuigi Rizzo netmap_make_zombie(struct ifnet *ifp)
64637e3a6d3SLuigi Rizzo {
64737e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
64837e3a6d3SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
64937e3a6d3SLuigi Rizzo 		netmap_set_all_rings(na, NM_KR_LOCKED);
65037e3a6d3SLuigi Rizzo 		na->na_flags |= NAF_ZOMBIE;
65137e3a6d3SLuigi Rizzo 		netmap_set_all_rings(na, 0);
65237e3a6d3SLuigi Rizzo 	}
65337e3a6d3SLuigi Rizzo }
65437e3a6d3SLuigi Rizzo 
65537e3a6d3SLuigi Rizzo void
65637e3a6d3SLuigi Rizzo netmap_undo_zombie(struct ifnet *ifp)
65737e3a6d3SLuigi Rizzo {
65837e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
65937e3a6d3SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
66037e3a6d3SLuigi Rizzo 		if (na->na_flags & NAF_ZOMBIE) {
66137e3a6d3SLuigi Rizzo 			netmap_set_all_rings(na, NM_KR_LOCKED);
66237e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_ZOMBIE;
66337e3a6d3SLuigi Rizzo 			netmap_set_all_rings(na, 0);
66437e3a6d3SLuigi Rizzo 		}
66537e3a6d3SLuigi Rizzo 	}
66637e3a6d3SLuigi Rizzo }
667f9790aebSLuigi Rizzo 
668ce3ee1e7SLuigi Rizzo /*
669ce3ee1e7SLuigi Rizzo  * generic bound_checking function
670ce3ee1e7SLuigi Rizzo  */
671ce3ee1e7SLuigi Rizzo u_int
672ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
673ce3ee1e7SLuigi Rizzo {
674ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
675ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
676ce3ee1e7SLuigi Rizzo 
677ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
678ce3ee1e7SLuigi Rizzo 		dflt = lo;
679ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
680ce3ee1e7SLuigi Rizzo 		dflt = hi;
681ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
682ce3ee1e7SLuigi Rizzo 		*v = dflt;
683ce3ee1e7SLuigi Rizzo 		op = "Bump";
684ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
685ce3ee1e7SLuigi Rizzo 		*v = hi;
686ce3ee1e7SLuigi Rizzo 		op = "Clamp";
687ce3ee1e7SLuigi Rizzo 	}
688ce3ee1e7SLuigi Rizzo 	if (op && msg)
689c3e9b4dbSLuiz Otavio O Souza 		nm_prinf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
690ce3ee1e7SLuigi Rizzo 	return *v;
691ce3ee1e7SLuigi Rizzo }
692ce3ee1e7SLuigi Rizzo 
693f9790aebSLuigi Rizzo 
694ce3ee1e7SLuigi Rizzo /*
695ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
696ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
697ce3ee1e7SLuigi Rizzo  */
698ce3ee1e7SLuigi Rizzo const char *
699ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
700ce3ee1e7SLuigi Rizzo {
701ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
702ce3ee1e7SLuigi Rizzo 	int i, j, i0;
703ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
704ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
705ce3ee1e7SLuigi Rizzo 
706ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
707ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
708ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
709ce3ee1e7SLuigi Rizzo 	if (!dst)
710ce3ee1e7SLuigi Rizzo 		dst = _dst;
711ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
712ce3ee1e7SLuigi Rizzo 		lim = len;
713ce3ee1e7SLuigi Rizzo 	o = dst;
714ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
715ce3ee1e7SLuigi Rizzo 	o += strlen(o);
716ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
717ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
718ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
719ce3ee1e7SLuigi Rizzo 		o += strlen(o);
720ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
721ce3ee1e7SLuigi Rizzo 		i0 = i;
722ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
723ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
724ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
725ce3ee1e7SLuigi Rizzo 		}
726ce3ee1e7SLuigi Rizzo 		i = i0;
727ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
728ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
729ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
730ce3ee1e7SLuigi Rizzo 		o += j+49;
731ce3ee1e7SLuigi Rizzo 	}
732ce3ee1e7SLuigi Rizzo 	*o = '\0';
733ce3ee1e7SLuigi Rizzo #undef P_HI
734ce3ee1e7SLuigi Rizzo #undef P_LO
735ce3ee1e7SLuigi Rizzo #undef P_C
736ce3ee1e7SLuigi Rizzo 	return dst;
737ce3ee1e7SLuigi Rizzo }
738f196ce38SLuigi Rizzo 
739f18be576SLuigi Rizzo 
740ae10d1afSLuigi Rizzo /*
741ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
742ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
743ae10d1afSLuigi Rizzo  */
74489cc2556SLuigi Rizzo /* call with NMG_LOCK held */
745f9790aebSLuigi Rizzo int
746ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
747ae10d1afSLuigi Rizzo {
7482ff91c17SVincenzo Maffione 	struct nm_config_info info;
749ae10d1afSLuigi Rizzo 
7502ff91c17SVincenzo Maffione 	bzero(&info, sizeof(info));
7516641c68bSLuigi Rizzo 	if (na->nm_config == NULL ||
7522ff91c17SVincenzo Maffione 	    na->nm_config(na, &info)) {
753ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
7542ff91c17SVincenzo Maffione 		info.num_tx_rings = na->num_tx_rings;
7552ff91c17SVincenzo Maffione 		info.num_tx_descs = na->num_tx_desc;
7562ff91c17SVincenzo Maffione 		info.num_rx_rings = na->num_rx_rings;
7572ff91c17SVincenzo Maffione 		info.num_rx_descs = na->num_rx_desc;
7582ff91c17SVincenzo Maffione 		info.rx_buf_maxsize = na->rx_buf_maxsize;
759ae10d1afSLuigi Rizzo 	}
760ae10d1afSLuigi Rizzo 
7612ff91c17SVincenzo Maffione 	if (na->num_tx_rings == info.num_tx_rings &&
7622ff91c17SVincenzo Maffione 	    na->num_tx_desc == info.num_tx_descs &&
7632ff91c17SVincenzo Maffione 	    na->num_rx_rings == info.num_rx_rings &&
7642ff91c17SVincenzo Maffione 	    na->num_rx_desc == info.num_rx_descs &&
7652ff91c17SVincenzo Maffione 	    na->rx_buf_maxsize == info.rx_buf_maxsize)
766ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
767f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
7682ff91c17SVincenzo Maffione 		na->num_tx_rings = info.num_tx_rings;
7692ff91c17SVincenzo Maffione 		na->num_tx_desc = info.num_tx_descs;
7702ff91c17SVincenzo Maffione 		na->num_rx_rings = info.num_rx_rings;
7712ff91c17SVincenzo Maffione 		na->num_rx_desc = info.num_rx_descs;
7722ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = info.rx_buf_maxsize;
773*cfa866f6SMatt Macy 		D("configuration changed for %s: txring %d x %d, "
774*cfa866f6SMatt Macy 			"rxring %d x %d, rxbufsz %d",
775*cfa866f6SMatt Macy 			na->name, na->num_tx_rings, na->num_tx_desc,
776*cfa866f6SMatt Macy 			na->num_rx_rings, na->num_rx_desc, na->rx_buf_maxsize);
777ae10d1afSLuigi Rizzo 		return 0;
778ae10d1afSLuigi Rizzo 	}
7792ff91c17SVincenzo Maffione 	D("WARNING: configuration changed for %s while active: "
7802ff91c17SVincenzo Maffione 		"txring %d x %d, rxring %d x %d, rxbufsz %d",
7812ff91c17SVincenzo Maffione 		na->name, info.num_tx_rings, info.num_tx_descs,
7822ff91c17SVincenzo Maffione 		info.num_rx_rings, info.num_rx_descs,
7832ff91c17SVincenzo Maffione 		info.rx_buf_maxsize);
784ae10d1afSLuigi Rizzo 	return 1;
785ae10d1afSLuigi Rizzo }
786ae10d1afSLuigi Rizzo 
78737e3a6d3SLuigi Rizzo /* nm_sync callbacks for the host rings */
78837e3a6d3SLuigi Rizzo static int netmap_txsync_to_host(struct netmap_kring *kring, int flags);
78937e3a6d3SLuigi Rizzo static int netmap_rxsync_from_host(struct netmap_kring *kring, int flags);
790f0ea3689SLuigi Rizzo 
791f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
792f0ea3689SLuigi Rizzo  * The array layout is this:
793f0ea3689SLuigi Rizzo  *
794f0ea3689SLuigi Rizzo  *                    +----------+
795f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
796f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
797f0ea3689SLuigi Rizzo  *                    |          | /
798f0ea3689SLuigi Rizzo  *                    +----------+
799f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
800f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
801f0ea3689SLuigi Rizzo  *                    |          | \
802f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
803f0ea3689SLuigi Rizzo  *                    |          | /
804f0ea3689SLuigi Rizzo  *                    +----------+
805f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
806f0ea3689SLuigi Rizzo  *                    +----------+
807f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
808f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
809f0ea3689SLuigi Rizzo  *                    |          | /
810f0ea3689SLuigi Rizzo  *                    +----------+
811f0ea3689SLuigi Rizzo  *
812f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
813f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
814f0ea3689SLuigi Rizzo  */
81589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
816f9790aebSLuigi Rizzo int
817f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
818f9790aebSLuigi Rizzo {
819f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
820f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
821847bf383SLuigi Rizzo 	u_int n[NR_TXRX];
822847bf383SLuigi Rizzo 	enum txrx t;
823f9790aebSLuigi Rizzo 
824c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings != NULL) {
825c3e9b4dbSLuiz Otavio O Souza 		D("warning: krings were already created");
826c3e9b4dbSLuiz Otavio O Souza 		return 0;
827c3e9b4dbSLuiz Otavio O Souza 	}
828c3e9b4dbSLuiz Otavio O Souza 
829f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
830847bf383SLuigi Rizzo 	n[NR_TX] = na->num_tx_rings + 1;
831847bf383SLuigi Rizzo 	n[NR_RX] = na->num_rx_rings + 1;
832f0ea3689SLuigi Rizzo 
8332ff91c17SVincenzo Maffione 	len = (n[NR_TX] + n[NR_RX]) *
8342ff91c17SVincenzo Maffione 		(sizeof(struct netmap_kring) + sizeof(struct netmap_kring *))
8352ff91c17SVincenzo Maffione 		+ tailroom;
836f9790aebSLuigi Rizzo 
837c3e9b4dbSLuiz Otavio O Souza 	na->tx_rings = nm_os_malloc((size_t)len);
838f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
839f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
840f9790aebSLuigi Rizzo 		return ENOMEM;
841f9790aebSLuigi Rizzo 	}
842847bf383SLuigi Rizzo 	na->rx_rings = na->tx_rings + n[NR_TX];
8432ff91c17SVincenzo Maffione 	na->tailroom = na->rx_rings + n[NR_RX];
8442ff91c17SVincenzo Maffione 
8452ff91c17SVincenzo Maffione 	/* link the krings in the krings array */
8462ff91c17SVincenzo Maffione 	kring = (struct netmap_kring *)((char *)na->tailroom + tailroom);
8472ff91c17SVincenzo Maffione 	for (i = 0; i < n[NR_TX] + n[NR_RX]; i++) {
8482ff91c17SVincenzo Maffione 		na->tx_rings[i] = kring;
8492ff91c17SVincenzo Maffione 		kring++;
8502ff91c17SVincenzo Maffione 	}
851f9790aebSLuigi Rizzo 
85217885a7bSLuigi Rizzo 	/*
85317885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
85417885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
85517885a7bSLuigi Rizzo 	 */
856847bf383SLuigi Rizzo 	for_rx_tx(t) {
857847bf383SLuigi Rizzo 		ndesc = nma_get_ndesc(na, t);
858847bf383SLuigi Rizzo 		for (i = 0; i < n[t]; i++) {
8592ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
860f9790aebSLuigi Rizzo 			bzero(kring, sizeof(*kring));
861f9790aebSLuigi Rizzo 			kring->na = na;
8622ff91c17SVincenzo Maffione 			kring->notify_na = na;
86317885a7bSLuigi Rizzo 			kring->ring_id = i;
864847bf383SLuigi Rizzo 			kring->tx = t;
865f9790aebSLuigi Rizzo 			kring->nkr_num_slots = ndesc;
86637e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
86737e3a6d3SLuigi Rizzo 			kring->nr_pending_mode = NKR_NETMAP_OFF;
868847bf383SLuigi Rizzo 			if (i < nma_get_nrings(na, t)) {
869847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ? na->nm_txsync : na->nm_rxsync);
87037e3a6d3SLuigi Rizzo 			} else {
8712ff91c17SVincenzo Maffione 				if (!(na->na_flags & NAF_HOST_RINGS))
8722ff91c17SVincenzo Maffione 					kring->nr_kflags |= NKR_FAKERING;
873847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ?
87437e3a6d3SLuigi Rizzo 						netmap_txsync_to_host:
87537e3a6d3SLuigi Rizzo 						netmap_rxsync_from_host);
876f0ea3689SLuigi Rizzo 			}
877847bf383SLuigi Rizzo 			kring->nm_notify = na->nm_notify;
878847bf383SLuigi Rizzo 			kring->rhead = kring->rcur = kring->nr_hwcur = 0;
879f9790aebSLuigi Rizzo 			/*
88017885a7bSLuigi Rizzo 			 * IMPORTANT: Always keep one slot empty.
881f9790aebSLuigi Rizzo 			 */
882847bf383SLuigi Rizzo 			kring->rtail = kring->nr_hwtail = (t == NR_TX ? ndesc - 1 : 0);
883847bf383SLuigi Rizzo 			snprintf(kring->name, sizeof(kring->name) - 1, "%s %s%d", na->name,
884847bf383SLuigi Rizzo 					nm_txrx2str(t), i);
885f0ea3689SLuigi Rizzo 			ND("ktx %s h %d c %d t %d",
886f0ea3689SLuigi Rizzo 				kring->name, kring->rhead, kring->rcur, kring->rtail);
887847bf383SLuigi Rizzo 			mtx_init(&kring->q_lock, (t == NR_TX ? "nm_txq_lock" : "nm_rxq_lock"), NULL, MTX_DEF);
88837e3a6d3SLuigi Rizzo 			nm_os_selinfo_init(&kring->si);
889f9790aebSLuigi Rizzo 		}
89037e3a6d3SLuigi Rizzo 		nm_os_selinfo_init(&na->si[t]);
891f0ea3689SLuigi Rizzo 	}
892f9790aebSLuigi Rizzo 
893f9790aebSLuigi Rizzo 
894f9790aebSLuigi Rizzo 	return 0;
895f9790aebSLuigi Rizzo }
896f9790aebSLuigi Rizzo 
897f9790aebSLuigi Rizzo 
898f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
89989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
900f9790aebSLuigi Rizzo void
901f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
902f9790aebSLuigi Rizzo {
9032ff91c17SVincenzo Maffione 	struct netmap_kring **kring = na->tx_rings;
904847bf383SLuigi Rizzo 	enum txrx t;
905847bf383SLuigi Rizzo 
906c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings == NULL) {
907c3e9b4dbSLuiz Otavio O Souza 		D("warning: krings were already deleted");
908c3e9b4dbSLuiz Otavio O Souza 		return;
909c3e9b4dbSLuiz Otavio O Souza 	}
910c3e9b4dbSLuiz Otavio O Souza 
911847bf383SLuigi Rizzo 	for_rx_tx(t)
91237e3a6d3SLuigi Rizzo 		nm_os_selinfo_uninit(&na->si[t]);
913f9790aebSLuigi Rizzo 
914f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
915f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
9162ff91c17SVincenzo Maffione 		mtx_destroy(&(*kring)->q_lock);
9172ff91c17SVincenzo Maffione 		nm_os_selinfo_uninit(&(*kring)->si);
918f9790aebSLuigi Rizzo 	}
919c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(na->tx_rings);
920f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
921f9790aebSLuigi Rizzo }
922f9790aebSLuigi Rizzo 
923f9790aebSLuigi Rizzo 
92417885a7bSLuigi Rizzo /*
92517885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
92617885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
92717885a7bSLuigi Rizzo  * them first.
92817885a7bSLuigi Rizzo  */
92989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
93037e3a6d3SLuigi Rizzo void
93117885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
93217885a7bSLuigi Rizzo {
9332ff91c17SVincenzo Maffione 	struct mbq *q = &na->rx_rings[na->num_rx_rings]->rx_queue;
93417885a7bSLuigi Rizzo 
93517885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
93617885a7bSLuigi Rizzo 	mbq_purge(q);
93737e3a6d3SLuigi Rizzo 	mbq_safe_fini(q);
93817885a7bSLuigi Rizzo 	netmap_krings_delete(na);
93917885a7bSLuigi Rizzo }
94017885a7bSLuigi Rizzo 
9414f80b14cSVincenzo Maffione static void
9424f80b14cSVincenzo Maffione netmap_mem_drop(struct netmap_adapter *na)
9434f80b14cSVincenzo Maffione {
9444f80b14cSVincenzo Maffione 	int last = netmap_mem_deref(na->nm_mem, na);
9454f80b14cSVincenzo Maffione 	/* if the native allocator had been overrided on regif,
9464f80b14cSVincenzo Maffione 	 * restore it now and drop the temporary one
9474f80b14cSVincenzo Maffione 	 */
9484f80b14cSVincenzo Maffione 	if (last && na->nm_mem_prev) {
9494f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
9504f80b14cSVincenzo Maffione 		na->nm_mem = na->nm_mem_prev;
9514f80b14cSVincenzo Maffione 		na->nm_mem_prev = NULL;
9524f80b14cSVincenzo Maffione 	}
9534f80b14cSVincenzo Maffione }
954f9790aebSLuigi Rizzo 
95568b8534bSLuigi Rizzo /*
956847bf383SLuigi Rizzo  * Undo everything that was done in netmap_do_regif(). In particular,
957847bf383SLuigi Rizzo  * call nm_register(ifp,0) to stop netmap mode on the interface and
9584bf50f18SLuigi Rizzo  * revert to normal operation.
95968b8534bSLuigi Rizzo  */
960ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
961847bf383SLuigi Rizzo static void netmap_unset_ringid(struct netmap_priv_d *);
96237e3a6d3SLuigi Rizzo static void netmap_krings_put(struct netmap_priv_d *);
96337e3a6d3SLuigi Rizzo void
964847bf383SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv)
96568b8534bSLuigi Rizzo {
966f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
96768b8534bSLuigi Rizzo 
968ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
969f9790aebSLuigi Rizzo 	na->active_fds--;
97037e3a6d3SLuigi Rizzo 	/* unset nr_pending_mode and possibly release exclusive mode */
97137e3a6d3SLuigi Rizzo 	netmap_krings_put(priv);
972847bf383SLuigi Rizzo 
973847bf383SLuigi Rizzo #ifdef	WITH_MONITOR
97437e3a6d3SLuigi Rizzo 	/* XXX check whether we have to do something with monitor
97537e3a6d3SLuigi Rizzo 	 * when rings change nr_mode. */
97637e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {
977847bf383SLuigi Rizzo 		/* walk through all the rings and tell any monitor
978847bf383SLuigi Rizzo 		 * that the port is going to exit netmap mode
979847bf383SLuigi Rizzo 		 */
980847bf383SLuigi Rizzo 		netmap_monitor_stop(na);
98137e3a6d3SLuigi Rizzo 	}
982847bf383SLuigi Rizzo #endif
98337e3a6d3SLuigi Rizzo 
98437e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0 || nm_kring_pending(priv)) {
98537e3a6d3SLuigi Rizzo 		na->nm_register(na, 0);
98637e3a6d3SLuigi Rizzo 	}
98737e3a6d3SLuigi Rizzo 
98837e3a6d3SLuigi Rizzo 	/* delete rings and buffers that are no longer needed */
98937e3a6d3SLuigi Rizzo 	netmap_mem_rings_delete(na);
99037e3a6d3SLuigi Rizzo 
99137e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
99268b8534bSLuigi Rizzo 		/*
99337e3a6d3SLuigi Rizzo 		 * (TO CHECK) We enter here
994f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
995f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
996f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
997ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
998ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
999ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
1000ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
1001ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
1002ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
1003ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
1004ce3ee1e7SLuigi Rizzo 		 * syscall is running.
100568b8534bSLuigi Rizzo 		 */
100637e3a6d3SLuigi Rizzo 		if (netmap_verbose)
100737e3a6d3SLuigi Rizzo 			D("deleting last instance for %s", na->name);
100837e3a6d3SLuigi Rizzo 
100937e3a6d3SLuigi Rizzo 		if (nm_netmap_on(na)) {
101037e3a6d3SLuigi Rizzo 			D("BUG: netmap on while going to delete the krings");
101137e3a6d3SLuigi Rizzo 		}
101237e3a6d3SLuigi Rizzo 
1013f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
101468b8534bSLuigi Rizzo 	}
101537e3a6d3SLuigi Rizzo 
1016847bf383SLuigi Rizzo 	/* possibily decrement counter of tx_si/rx_si users */
1017847bf383SLuigi Rizzo 	netmap_unset_ringid(priv);
1018f9790aebSLuigi Rizzo 	/* delete the nifp */
1019847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, priv->np_nifp);
1020847bf383SLuigi Rizzo 	/* drop the allocator */
10214f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
1022847bf383SLuigi Rizzo 	/* mark the priv as unregistered */
1023847bf383SLuigi Rizzo 	priv->np_na = NULL;
1024847bf383SLuigi Rizzo 	priv->np_nifp = NULL;
10255819da83SLuigi Rizzo }
102668b8534bSLuigi Rizzo 
102789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1028f0ea3689SLuigi Rizzo static __inline int
1029847bf383SLuigi Rizzo nm_si_user(struct netmap_priv_d *priv, enum txrx t)
1030f0ea3689SLuigi Rizzo {
1031f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
1032847bf383SLuigi Rizzo 		(priv->np_qlast[t] - priv->np_qfirst[t] > 1));
1033f0ea3689SLuigi Rizzo }
1034f0ea3689SLuigi Rizzo 
103537e3a6d3SLuigi Rizzo struct netmap_priv_d*
103637e3a6d3SLuigi Rizzo netmap_priv_new(void)
103737e3a6d3SLuigi Rizzo {
103837e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
103937e3a6d3SLuigi Rizzo 
1040c3e9b4dbSLuiz Otavio O Souza 	priv = nm_os_malloc(sizeof(struct netmap_priv_d));
104137e3a6d3SLuigi Rizzo 	if (priv == NULL)
104237e3a6d3SLuigi Rizzo 		return NULL;
104337e3a6d3SLuigi Rizzo 	priv->np_refs = 1;
104437e3a6d3SLuigi Rizzo 	nm_os_get_module();
104537e3a6d3SLuigi Rizzo 	return priv;
104637e3a6d3SLuigi Rizzo }
104737e3a6d3SLuigi Rizzo 
1048ce3ee1e7SLuigi Rizzo /*
10498fd44c93SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd is closed
10508fd44c93SLuigi Rizzo  * Action: undo all the things done by NIOCREGIF,
10518fd44c93SLuigi Rizzo  * On FreeBSD we need to track whether there are active mmap()s,
10528fd44c93SLuigi Rizzo  * and we use np_active_mmaps for that. On linux, the field is always 0.
10538fd44c93SLuigi Rizzo  * Return: 1 if we can free priv, 0 otherwise.
105489cc2556SLuigi Rizzo  *
1055ce3ee1e7SLuigi Rizzo  */
105689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
105737e3a6d3SLuigi Rizzo void
105837e3a6d3SLuigi Rizzo netmap_priv_delete(struct netmap_priv_d *priv)
1059ce3ee1e7SLuigi Rizzo {
1060f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1061ce3ee1e7SLuigi Rizzo 
1062847adfb7SLuigi Rizzo 	/* number of active references to this fd */
10638fd44c93SLuigi Rizzo 	if (--priv->np_refs > 0) {
106437e3a6d3SLuigi Rizzo 		return;
1065ce3ee1e7SLuigi Rizzo 	}
106637e3a6d3SLuigi Rizzo 	nm_os_put_module();
106737e3a6d3SLuigi Rizzo 	if (na) {
1068847bf383SLuigi Rizzo 		netmap_do_unregif(priv);
106937e3a6d3SLuigi Rizzo 	}
107037e3a6d3SLuigi Rizzo 	netmap_unget_na(na, priv->np_ifp);
107137e3a6d3SLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* for safety */
1072c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(priv);
1073f196ce38SLuigi Rizzo }
10745819da83SLuigi Rizzo 
1075f9790aebSLuigi Rizzo 
107689cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
1077f9790aebSLuigi Rizzo void
10785819da83SLuigi Rizzo netmap_dtor(void *data)
10795819da83SLuigi Rizzo {
10805819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
10815819da83SLuigi Rizzo 
1082ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
108337e3a6d3SLuigi Rizzo 	netmap_priv_delete(priv);
1084ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1085ce3ee1e7SLuigi Rizzo }
108668b8534bSLuigi Rizzo 
1087f18be576SLuigi Rizzo 
108868b8534bSLuigi Rizzo /*
1089c3e9b4dbSLuiz Otavio O Souza  * Handlers for synchronization of the rings from/to the host stack.
1090c3e9b4dbSLuiz Otavio O Souza  * These are associated to a network interface and are just another
1091c3e9b4dbSLuiz Otavio O Souza  * ring pair managed by userspace.
1092c3e9b4dbSLuiz Otavio O Souza  *
1093c3e9b4dbSLuiz Otavio O Souza  * Netmap also supports transparent forwarding (NS_FORWARD and NR_FORWARD
1094c3e9b4dbSLuiz Otavio O Souza  * flags):
1095c3e9b4dbSLuiz Otavio O Souza  *
1096c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on hw RX rings, the application can mark
1097c3e9b4dbSLuiz Otavio O Souza  *   them with the NS_FORWARD flag. During the next RXSYNC or poll(), they
1098c3e9b4dbSLuiz Otavio O Souza  *   will be forwarded to the host stack, similarly to what happened if
1099c3e9b4dbSLuiz Otavio O Souza  *   the application moved them to the host TX ring.
1100c3e9b4dbSLuiz Otavio O Souza  *
1101c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on the host RX ring, the application can
1102c3e9b4dbSLuiz Otavio O Souza  *   mark them with the NS_FORWARD flag. During the next RXSYNC or poll(),
1103c3e9b4dbSLuiz Otavio O Souza  *   they will be forwarded to the hw TX rings, saving the application
1104c3e9b4dbSLuiz Otavio O Souza  *   from doing the same task in user-space.
1105c3e9b4dbSLuiz Otavio O Souza  *
1106c3e9b4dbSLuiz Otavio O Souza  * Transparent fowarding can be enabled per-ring, by setting the NR_FORWARD
1107c3e9b4dbSLuiz Otavio O Souza  * flag, or globally with the netmap_fwd sysctl.
1108c3e9b4dbSLuiz Otavio O Souza  *
1109091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1110091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1111091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1112091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1113091fd0abSLuigi Rizzo  */
1114091fd0abSLuigi Rizzo 
1115f18be576SLuigi Rizzo 
1116091fd0abSLuigi Rizzo /*
1117c3e9b4dbSLuiz Otavio O Souza  * Pass a whole queue of mbufs to the host stack as coming from 'dst'
111817885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1119c3e9b4dbSLuiz Otavio O Souza  * After this call the queue is empty.
1120091fd0abSLuigi Rizzo  */
1121091fd0abSLuigi Rizzo static void
1122f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1123091fd0abSLuigi Rizzo {
1124091fd0abSLuigi Rizzo 	struct mbuf *m;
112537e3a6d3SLuigi Rizzo 	struct mbuf *head = NULL, *prev = NULL;
1126091fd0abSLuigi Rizzo 
1127c3e9b4dbSLuiz Otavio O Souza 	/* Send packets up, outside the lock; head/prev machinery
1128c3e9b4dbSLuiz Otavio O Souza 	 * is only useful for Windows. */
1129f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1130091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
1131091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
113237e3a6d3SLuigi Rizzo 		prev = nm_os_send_up(dst, m, prev);
113337e3a6d3SLuigi Rizzo 		if (head == NULL)
113437e3a6d3SLuigi Rizzo 			head = prev;
1135091fd0abSLuigi Rizzo 	}
113637e3a6d3SLuigi Rizzo 	if (head)
113737e3a6d3SLuigi Rizzo 		nm_os_send_up(dst, NULL, head);
113837e3a6d3SLuigi Rizzo 	mbq_fini(q);
1139091fd0abSLuigi Rizzo }
1140091fd0abSLuigi Rizzo 
1141f18be576SLuigi Rizzo 
1142091fd0abSLuigi Rizzo /*
1143c3e9b4dbSLuiz Otavio O Souza  * Scan the buffers from hwcur to ring->head, and put a copy of those
1144c3e9b4dbSLuiz Otavio O Souza  * marked NS_FORWARD (or all of them if forced) into a queue of mbufs.
1145c3e9b4dbSLuiz Otavio O Souza  * Drop remaining packets in the unlikely event
114617885a7bSLuigi Rizzo  * of an mbuf shortage.
1147091fd0abSLuigi Rizzo  */
1148091fd0abSLuigi Rizzo static void
1149091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1150091fd0abSLuigi Rizzo {
115117885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1152847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
115317885a7bSLuigi Rizzo 	u_int n;
1154f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1155091fd0abSLuigi Rizzo 
115617885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
115717885a7bSLuigi Rizzo 		struct mbuf *m;
1158091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1159091fd0abSLuigi Rizzo 
1160091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1161091fd0abSLuigi Rizzo 			continue;
11624bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
116317885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
1164091fd0abSLuigi Rizzo 			continue;
1165091fd0abSLuigi Rizzo 		}
1166091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
116717885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
11684bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1169091fd0abSLuigi Rizzo 
1170091fd0abSLuigi Rizzo 		if (m == NULL)
1171091fd0abSLuigi Rizzo 			break;
1172f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1173091fd0abSLuigi Rizzo 	}
1174091fd0abSLuigi Rizzo }
1175091fd0abSLuigi Rizzo 
117637e3a6d3SLuigi Rizzo static inline int
117737e3a6d3SLuigi Rizzo _nm_may_forward(struct netmap_kring *kring)
117837e3a6d3SLuigi Rizzo {
117937e3a6d3SLuigi Rizzo 	return	((netmap_fwd || kring->ring->flags & NR_FORWARD) &&
118037e3a6d3SLuigi Rizzo 		 kring->na->na_flags & NAF_HOST_RINGS &&
118137e3a6d3SLuigi Rizzo 		 kring->tx == NR_RX);
118237e3a6d3SLuigi Rizzo }
118337e3a6d3SLuigi Rizzo 
118437e3a6d3SLuigi Rizzo static inline int
118537e3a6d3SLuigi Rizzo nm_may_forward_up(struct netmap_kring *kring)
118637e3a6d3SLuigi Rizzo {
118737e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
118837e3a6d3SLuigi Rizzo 		 kring->ring_id != kring->na->num_rx_rings;
118937e3a6d3SLuigi Rizzo }
119037e3a6d3SLuigi Rizzo 
119137e3a6d3SLuigi Rizzo static inline int
1192c3e9b4dbSLuiz Otavio O Souza nm_may_forward_down(struct netmap_kring *kring, int sync_flags)
119337e3a6d3SLuigi Rizzo {
119437e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
1195c3e9b4dbSLuiz Otavio O Souza 		 (sync_flags & NAF_CAN_FORWARD_DOWN) &&
119637e3a6d3SLuigi Rizzo 		 kring->ring_id == kring->na->num_rx_rings;
119737e3a6d3SLuigi Rizzo }
1198f18be576SLuigi Rizzo 
1199091fd0abSLuigi Rizzo /*
120017885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
1201c3e9b4dbSLuiz Otavio O Souza  * kring->nr_hwcur and kring->rhead.
1202c3e9b4dbSLuiz Otavio O Souza  * Called under kring->rx_queue.lock on the sw rx ring.
1203c3e9b4dbSLuiz Otavio O Souza  *
1204c3e9b4dbSLuiz Otavio O Souza  * It can only be called if the user opened all the TX hw rings,
1205c3e9b4dbSLuiz Otavio O Souza  * see NAF_CAN_FORWARD_DOWN flag.
1206c3e9b4dbSLuiz Otavio O Souza  * We can touch the TX netmap rings (slots, head and cur) since
1207c3e9b4dbSLuiz Otavio O Souza  * we are in poll/ioctl system call context, and the application
1208c3e9b4dbSLuiz Otavio O Souza  * is not supposed to touch the ring (using a different thread)
1209c3e9b4dbSLuiz Otavio O Souza  * during the execution of the system call.
1210091fd0abSLuigi Rizzo  */
121117885a7bSLuigi Rizzo static u_int
1212091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1213091fd0abSLuigi Rizzo {
12142ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[na->num_rx_rings];
121517885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
121617885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
121717885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
121817885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
121917885a7bSLuigi Rizzo 	u_int sent = 0;
1220ce3ee1e7SLuigi Rizzo 
122117885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
122217885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
12232ff91c17SVincenzo Maffione 		struct netmap_kring *kdst = na->tx_rings[i];
122417885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
122517885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1226ce3ee1e7SLuigi Rizzo 
122717885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
122817885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
122917885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1230091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
123137e3a6d3SLuigi Rizzo 			u_int dst_head = rdst->head;
123217885a7bSLuigi Rizzo 
123317885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
123417885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
123517885a7bSLuigi Rizzo 				continue;
123617885a7bSLuigi Rizzo 
123717885a7bSLuigi Rizzo 			sent++;
123817885a7bSLuigi Rizzo 
123937e3a6d3SLuigi Rizzo 			dst = &rdst->slot[dst_head];
124017885a7bSLuigi Rizzo 
1241091fd0abSLuigi Rizzo 			tmp = *src;
124217885a7bSLuigi Rizzo 
1243091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1244091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1245091fd0abSLuigi Rizzo 
1246091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1247091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1248091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1249091fd0abSLuigi Rizzo 
125037e3a6d3SLuigi Rizzo 			rdst->head = rdst->cur = nm_next(dst_head, dst_lim);
1251091fd0abSLuigi Rizzo 		}
1252c3e9b4dbSLuiz Otavio O Souza 		/* if (sent) XXX txsync ? it would be just an optimization */
1253091fd0abSLuigi Rizzo 	}
125417885a7bSLuigi Rizzo 	return sent;
1255091fd0abSLuigi Rizzo }
1256091fd0abSLuigi Rizzo 
1257f18be576SLuigi Rizzo 
1258091fd0abSLuigi Rizzo /*
1259ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
126002ad4083SLuigi Rizzo  * system call in user process context, and the only contention
126102ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1262091fd0abSLuigi Rizzo  * this routine concurrently.
126368b8534bSLuigi Rizzo  */
126437e3a6d3SLuigi Rizzo static int
126537e3a6d3SLuigi Rizzo netmap_txsync_to_host(struct netmap_kring *kring, int flags)
126668b8534bSLuigi Rizzo {
126737e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
126817885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1269f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1270f9790aebSLuigi Rizzo 	struct mbq q;
127168b8534bSLuigi Rizzo 
127217885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
1273c3e9b4dbSLuiz Otavio O Souza 	 * Force hwcur = head since netmap_grab_packets() stops at head
127468b8534bSLuigi Rizzo 	 */
1275f9790aebSLuigi Rizzo 	mbq_init(&q);
127617885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
127717885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
127817885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
127917885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
128017885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
128117885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
128268b8534bSLuigi Rizzo 
1283f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
128437e3a6d3SLuigi Rizzo 	return 0;
1285f18be576SLuigi Rizzo }
1286f18be576SLuigi Rizzo 
1287f18be576SLuigi Rizzo 
128868b8534bSLuigi Rizzo /*
128902ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
129017885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
129117885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
129202ad4083SLuigi Rizzo  *
1293c3e9b4dbSLuiz Otavio O Souza  * also moves to the nic hw rings any packet the user has marked
1294c3e9b4dbSLuiz Otavio O Souza  * for transparent-mode forwarding, then sets the NR_FORWARD
1295c3e9b4dbSLuiz Otavio O Souza  * flag in the kring to let the caller push them out
129668b8534bSLuigi Rizzo  */
12978fd44c93SLuigi Rizzo static int
129837e3a6d3SLuigi Rizzo netmap_rxsync_from_host(struct netmap_kring *kring, int flags)
129968b8534bSLuigi Rizzo {
130037e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
130168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
130217885a7bSLuigi Rizzo 	u_int nm_i, n;
130317885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1304f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
130517885a7bSLuigi Rizzo 	int ret = 0;
1306847bf383SLuigi Rizzo 	struct mbq *q = &kring->rx_queue, fq;
130768b8534bSLuigi Rizzo 
1308847bf383SLuigi Rizzo 	mbq_init(&fq); /* fq holds packets to be freed */
1309847bf383SLuigi Rizzo 
1310997b054cSLuigi Rizzo 	mbq_lock(q);
131117885a7bSLuigi Rizzo 
131217885a7bSLuigi Rizzo 	/* First part: import newly received packets */
131317885a7bSLuigi Rizzo 	n = mbq_len(q);
131417885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
131517885a7bSLuigi Rizzo 		struct mbuf *m;
131617885a7bSLuigi Rizzo 		uint32_t stop_i;
131717885a7bSLuigi Rizzo 
131817885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
1319c3e9b4dbSLuiz Otavio O Souza 		stop_i = nm_prev(kring->nr_hwcur, lim);
132017885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
132117885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
132217885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
132317885a7bSLuigi Rizzo 
13244bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
132517885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
132617885a7bSLuigi Rizzo 			if (netmap_verbose)
13274bf50f18SLuigi Rizzo 				D("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
132817885a7bSLuigi Rizzo 
132917885a7bSLuigi Rizzo 			slot->len = len;
13304f80b14cSVincenzo Maffione 			slot->flags = 0;
133117885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
1332847bf383SLuigi Rizzo 			mbq_enqueue(&fq, m);
133364ae02c3SLuigi Rizzo 		}
133417885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
133564ae02c3SLuigi Rizzo 	}
133617885a7bSLuigi Rizzo 
133717885a7bSLuigi Rizzo 	/*
133817885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
133917885a7bSLuigi Rizzo 	 */
134017885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
134117885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
1342c3e9b4dbSLuiz Otavio O Souza 		if (nm_may_forward_down(kring, flags)) {
134317885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
134437e3a6d3SLuigi Rizzo 			if (ret > 0) {
134537e3a6d3SLuigi Rizzo 				kring->nr_kflags |= NR_FORWARD;
134637e3a6d3SLuigi Rizzo 				ret = 0;
134737e3a6d3SLuigi Rizzo 			}
134837e3a6d3SLuigi Rizzo 		}
134917885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
135064ae02c3SLuigi Rizzo 	}
135117885a7bSLuigi Rizzo 
1352997b054cSLuigi Rizzo 	mbq_unlock(q);
1353847bf383SLuigi Rizzo 
1354847bf383SLuigi Rizzo 	mbq_purge(&fq);
135537e3a6d3SLuigi Rizzo 	mbq_fini(&fq);
1356847bf383SLuigi Rizzo 
135717885a7bSLuigi Rizzo 	return ret;
135868b8534bSLuigi Rizzo }
135968b8534bSLuigi Rizzo 
136068b8534bSLuigi Rizzo 
1361f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1362f9790aebSLuigi Rizzo  *
1363f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1364f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1365f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1366f9790aebSLuigi Rizzo  *
1367f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1368f9790aebSLuigi Rizzo  * EBUSY.
1369f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1370f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1371f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1372f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1373f9790aebSLuigi Rizzo  *
1374f9790aebSLuigi Rizzo  *					native_support
1375f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1376f9790aebSLuigi Rizzo  * -------------------------------------------------------
1377f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1378f9790aebSLuigi Rizzo  *
1379f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1380f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1381f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1382f9790aebSLuigi Rizzo  *
1383f9790aebSLuigi Rizzo  */
138437e3a6d3SLuigi Rizzo static void netmap_hw_dtor(struct netmap_adapter *); /* needed by NM_IS_NATIVE() */
1385f9790aebSLuigi Rizzo int
1386c3e9b4dbSLuiz Otavio O Souza netmap_get_hw_na(struct ifnet *ifp, struct netmap_mem_d *nmd, struct netmap_adapter **na)
1387f9790aebSLuigi Rizzo {
1388f9790aebSLuigi Rizzo 	/* generic support */
1389f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1390f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1391847bf383SLuigi Rizzo 	int error = 0;
1392f9790aebSLuigi Rizzo 
1393f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1394f9790aebSLuigi Rizzo 
1395f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1396f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1397f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1398f9790aebSLuigi Rizzo 
139937e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
14004bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1401f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1402f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1403f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1404f9790aebSLuigi Rizzo 		 * adapters.
1405f9790aebSLuigi Rizzo 		 */
14064bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
14074bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
14084bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
14094bf50f18SLuigi Rizzo #ifdef WITH_PIPES
14104bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
14114bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
14124bf50f18SLuigi Rizzo 			 */
14134bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
14144bf50f18SLuigi Rizzo #endif
14154bf50f18SLuigi Rizzo 		) {
14164bf50f18SLuigi Rizzo 			*na = prev_na;
1417c3e9b4dbSLuiz Otavio O Souza 			goto assign_mem;
1418f9790aebSLuigi Rizzo 		}
1419f9790aebSLuigi Rizzo 	}
1420f9790aebSLuigi Rizzo 
1421f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1422f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1423f9790aebSLuigi Rizzo 	 */
142437e3a6d3SLuigi Rizzo 	if (!NM_IS_NATIVE(ifp) && i == NETMAP_ADMODE_NATIVE)
1425f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1426f9790aebSLuigi Rizzo 
1427f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1428f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1429f9790aebSLuigi Rizzo 	 *
1430f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1431f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1432f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1433f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1434f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1435f9790aebSLuigi Rizzo 	 * must be that
1436f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1437f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1438f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1439f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1440f9790aebSLuigi Rizzo 	 */
1441f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1442f9790aebSLuigi Rizzo 	if (error)
1443f9790aebSLuigi Rizzo 		return error;
1444f9790aebSLuigi Rizzo 
1445f9790aebSLuigi Rizzo 	*na = NA(ifp);
1446c3e9b4dbSLuiz Otavio O Souza 
1447c3e9b4dbSLuiz Otavio O Souza assign_mem:
1448c3e9b4dbSLuiz Otavio O Souza 	if (nmd != NULL && !((*na)->na_flags & NAF_MEM_OWNER) &&
1449c3e9b4dbSLuiz Otavio O Souza 	    (*na)->active_fds == 0 && ((*na)->nm_mem != nmd)) {
14504f80b14cSVincenzo Maffione 		(*na)->nm_mem_prev = (*na)->nm_mem;
1451c3e9b4dbSLuiz Otavio O Souza 		(*na)->nm_mem = netmap_mem_get(nmd);
1452f9790aebSLuigi Rizzo 	}
1453f9790aebSLuigi Rizzo 
1454c3e9b4dbSLuiz Otavio O Souza 	return 0;
1455c3e9b4dbSLuiz Otavio O Souza }
1456f9790aebSLuigi Rizzo 
145768b8534bSLuigi Rizzo /*
1458ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1459ce3ee1e7SLuigi Rizzo  *
1460f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
14612ff91c17SVincenzo Maffione  * to the interface specified by req.
1462ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1463ce3ee1e7SLuigi Rizzo  *
1464f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1465f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1466f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1467f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1468f2637526SLuigi Rizzo  * could not be allocated.
1469f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1470f18be576SLuigi Rizzo  *
14712ff91c17SVincenzo Maffione  * If the interface specified by req is a system one, also keep
147237e3a6d3SLuigi Rizzo  * a reference to it and return a valid *ifp.
147368b8534bSLuigi Rizzo  */
1474f9790aebSLuigi Rizzo int
14752ff91c17SVincenzo Maffione netmap_get_na(struct nmreq_header *hdr,
14762ff91c17SVincenzo Maffione 	      struct netmap_adapter **na, struct ifnet **ifp,
14772ff91c17SVincenzo Maffione 	      struct netmap_mem_d *nmd, int create)
147868b8534bSLuigi Rizzo {
1479*cfa866f6SMatt Macy 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
1480f9790aebSLuigi Rizzo 	int error = 0;
1481f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1482c3e9b4dbSLuiz Otavio O Souza 	int nmd_ref = 0;
1483f9790aebSLuigi Rizzo 
1484f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
148537e3a6d3SLuigi Rizzo 	*ifp = NULL;
1486f196ce38SLuigi Rizzo 
14872ff91c17SVincenzo Maffione 	if (hdr->nr_reqtype != NETMAP_REQ_REGISTER) {
14882ff91c17SVincenzo Maffione 		return EINVAL;
14892ff91c17SVincenzo Maffione 	}
14902ff91c17SVincenzo Maffione 
14912ff91c17SVincenzo Maffione 	if (req->nr_mode == NR_REG_PIPE_MASTER ||
14922ff91c17SVincenzo Maffione 			req->nr_mode == NR_REG_PIPE_SLAVE) {
14932ff91c17SVincenzo Maffione 		/* Do not accept deprecated pipe modes. */
14942ff91c17SVincenzo Maffione 		D("Deprecated pipe nr_mode, use xx{yy or xx}yy syntax");
14952ff91c17SVincenzo Maffione 		return EINVAL;
14962ff91c17SVincenzo Maffione 	}
14972ff91c17SVincenzo Maffione 
1498ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1499ce3ee1e7SLuigi Rizzo 
1500c3e9b4dbSLuiz Otavio O Souza 	/* if the request contain a memid, try to find the
1501c3e9b4dbSLuiz Otavio O Souza 	 * corresponding memory region
1502c3e9b4dbSLuiz Otavio O Souza 	 */
15032ff91c17SVincenzo Maffione 	if (nmd == NULL && req->nr_mem_id) {
15042ff91c17SVincenzo Maffione 		nmd = netmap_mem_find(req->nr_mem_id);
1505c3e9b4dbSLuiz Otavio O Souza 		if (nmd == NULL)
1506c3e9b4dbSLuiz Otavio O Souza 			return EINVAL;
1507c3e9b4dbSLuiz Otavio O Souza 		/* keep the rereference */
1508c3e9b4dbSLuiz Otavio O Souza 		nmd_ref = 1;
1509c3e9b4dbSLuiz Otavio O Souza 	}
1510c3e9b4dbSLuiz Otavio O Souza 
151137e3a6d3SLuigi Rizzo 	/* We cascade through all possible types of netmap adapter.
15124bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
15134bf50f18SLuigi Rizzo 	 * with the following combinations:
15144bf50f18SLuigi Rizzo 	 *
15154bf50f18SLuigi Rizzo 	 * error    na
15164bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
15174bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
15184bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
15194bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
15204bf50f18SLuigi Rizzo 	 */
15214bf50f18SLuigi Rizzo 
152237e3a6d3SLuigi Rizzo 	/* try to see if this is a ptnetmap port */
15232ff91c17SVincenzo Maffione 	error = netmap_get_pt_host_na(hdr, na, nmd, create);
152437e3a6d3SLuigi Rizzo 	if (error || *na != NULL)
1525c3e9b4dbSLuiz Otavio O Souza 		goto out;
152637e3a6d3SLuigi Rizzo 
15274bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
15282ff91c17SVincenzo Maffione 	error = netmap_get_monitor_na(hdr, na, nmd, create);
15294bf50f18SLuigi Rizzo 	if (error || *na != NULL)
1530c3e9b4dbSLuiz Otavio O Souza 		goto out;
15314bf50f18SLuigi Rizzo 
15324bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
15332ff91c17SVincenzo Maffione 	error = netmap_get_pipe_na(hdr, na, nmd, create);
1534f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1535c3e9b4dbSLuiz Otavio O Souza 		goto out;
1536ce3ee1e7SLuigi Rizzo 
15374bf50f18SLuigi Rizzo 	/* try to see if this is a bridge port */
15382ff91c17SVincenzo Maffione 	error = netmap_get_bdg_na(hdr, na, nmd, create);
1539f0ea3689SLuigi Rizzo 	if (error)
1540c3e9b4dbSLuiz Otavio O Souza 		goto out;
1541f0ea3689SLuigi Rizzo 
1542f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1543847bf383SLuigi Rizzo 		goto out;
1544f0ea3689SLuigi Rizzo 
154589cc2556SLuigi Rizzo 	/*
154689cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
154789cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
154889cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
154989cc2556SLuigi Rizzo 	 * persistent VALE port.
155089cc2556SLuigi Rizzo 	 */
15512ff91c17SVincenzo Maffione 	*ifp = ifunit_ref(hdr->nr_name);
155237e3a6d3SLuigi Rizzo 	if (*ifp == NULL) {
1553c3e9b4dbSLuiz Otavio O Souza 		error = ENXIO;
1554c3e9b4dbSLuiz Otavio O Souza 		goto out;
1555f196ce38SLuigi Rizzo 	}
1556ce3ee1e7SLuigi Rizzo 
1557c3e9b4dbSLuiz Otavio O Souza 	error = netmap_get_hw_na(*ifp, nmd, &ret);
1558f9790aebSLuigi Rizzo 	if (error)
1559f9790aebSLuigi Rizzo 		goto out;
1560f18be576SLuigi Rizzo 
1561f9790aebSLuigi Rizzo 	*na = ret;
1562f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1563f0ea3689SLuigi Rizzo 
1564f9790aebSLuigi Rizzo out:
156537e3a6d3SLuigi Rizzo 	if (error) {
156637e3a6d3SLuigi Rizzo 		if (ret)
1567f0ea3689SLuigi Rizzo 			netmap_adapter_put(ret);
156837e3a6d3SLuigi Rizzo 		if (*ifp) {
156937e3a6d3SLuigi Rizzo 			if_rele(*ifp);
157037e3a6d3SLuigi Rizzo 			*ifp = NULL;
157137e3a6d3SLuigi Rizzo 		}
157237e3a6d3SLuigi Rizzo 	}
1573c3e9b4dbSLuiz Otavio O Souza 	if (nmd_ref)
1574c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_put(nmd);
1575f18be576SLuigi Rizzo 
15765ab0d24dSLuigi Rizzo 	return error;
15775ab0d24dSLuigi Rizzo }
1578ce3ee1e7SLuigi Rizzo 
157937e3a6d3SLuigi Rizzo /* undo netmap_get_na() */
158037e3a6d3SLuigi Rizzo void
158137e3a6d3SLuigi Rizzo netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp)
158237e3a6d3SLuigi Rizzo {
158337e3a6d3SLuigi Rizzo 	if (ifp)
158437e3a6d3SLuigi Rizzo 		if_rele(ifp);
158537e3a6d3SLuigi Rizzo 	if (na)
158637e3a6d3SLuigi Rizzo 		netmap_adapter_put(na);
158737e3a6d3SLuigi Rizzo }
158837e3a6d3SLuigi Rizzo 
158937e3a6d3SLuigi Rizzo 
159037e3a6d3SLuigi Rizzo #define NM_FAIL_ON(t) do {						\
159137e3a6d3SLuigi Rizzo 	if (unlikely(t)) {						\
159237e3a6d3SLuigi Rizzo 		RD(5, "%s: fail '" #t "' "				\
159337e3a6d3SLuigi Rizzo 			"h %d c %d t %d "				\
159437e3a6d3SLuigi Rizzo 			"rh %d rc %d rt %d "				\
159537e3a6d3SLuigi Rizzo 			"hc %d ht %d",					\
159637e3a6d3SLuigi Rizzo 			kring->name,					\
159737e3a6d3SLuigi Rizzo 			head, cur, ring->tail,				\
159837e3a6d3SLuigi Rizzo 			kring->rhead, kring->rcur, kring->rtail,	\
159937e3a6d3SLuigi Rizzo 			kring->nr_hwcur, kring->nr_hwtail);		\
160037e3a6d3SLuigi Rizzo 		return kring->nkr_num_slots;				\
160137e3a6d3SLuigi Rizzo 	}								\
160237e3a6d3SLuigi Rizzo } while (0)
1603ce3ee1e7SLuigi Rizzo 
1604f9790aebSLuigi Rizzo /*
1605f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1606f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
160717885a7bSLuigi Rizzo  * in case of error.
1608f9790aebSLuigi Rizzo  *
160917885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
161017885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1611f9790aebSLuigi Rizzo  *
161217885a7bSLuigi Rizzo  * We want
161317885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1614f9790aebSLuigi Rizzo  *
161517885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1616f9790aebSLuigi Rizzo  */
161737e3a6d3SLuigi Rizzo u_int
161837e3a6d3SLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1619f9790aebSLuigi Rizzo {
162017885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1621f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1622f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1623ce3ee1e7SLuigi Rizzo 
162417885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
162517885a7bSLuigi Rizzo 		kring->name,
162617885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
162717885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
162817885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
162937e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->rhead >= n ||
163037e3a6d3SLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n);
1631f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
163217885a7bSLuigi Rizzo 	/*
163337e3a6d3SLuigi Rizzo 	 * user sanity checks. We only use head,
163437e3a6d3SLuigi Rizzo 	 * A, B, ... are possible positions for head:
163517885a7bSLuigi Rizzo 	 *
163637e3a6d3SLuigi Rizzo 	 *  0    A  rhead   B  rtail   C  n-1
163737e3a6d3SLuigi Rizzo 	 *  0    D  rtail   E  rhead   F  n-1
163817885a7bSLuigi Rizzo 	 *
163917885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
164017885a7bSLuigi Rizzo 	 */
164117885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
164217885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
164337e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->rhead || head > kring->rtail);
164417885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
164537e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->rtail);
164617885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
164717885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
164837e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head > kring->rtail && head < kring->rhead);
164917885a7bSLuigi Rizzo 
165017885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
165117885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
165217885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
165337e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->rtail);
165417885a7bSLuigi Rizzo 		} else { /* head >= rhead */
165517885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
165637e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur > kring->rtail && cur < head);
1657f18be576SLuigi Rizzo 		}
1658f9790aebSLuigi Rizzo 	}
165917885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
1660a2a74091SLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d", kring->name,
166117885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
166217885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
166317885a7bSLuigi Rizzo 	}
166417885a7bSLuigi Rizzo 	kring->rhead = head;
166517885a7bSLuigi Rizzo 	kring->rcur = cur;
166617885a7bSLuigi Rizzo 	return head;
166768b8534bSLuigi Rizzo }
166868b8534bSLuigi Rizzo 
166968b8534bSLuigi Rizzo 
167068b8534bSLuigi Rizzo /*
1671f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
167217885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1673f9790aebSLuigi Rizzo  *
167417885a7bSLuigi Rizzo  * For a valid configuration,
167517885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1676f9790aebSLuigi Rizzo  *
167717885a7bSLuigi Rizzo  * We only consider head and cur.
167817885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1679f9790aebSLuigi Rizzo  *
1680f9790aebSLuigi Rizzo  */
168137e3a6d3SLuigi Rizzo u_int
168237e3a6d3SLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1683f9790aebSLuigi Rizzo {
168417885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
168517885a7bSLuigi Rizzo 	uint32_t head, cur;
1686f9790aebSLuigi Rizzo 
1687847bf383SLuigi Rizzo 	ND(5,"%s kc %d kt %d h %d c %d t %d",
168817885a7bSLuigi Rizzo 		kring->name,
168917885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
169017885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
169117885a7bSLuigi Rizzo 	/*
169217885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
169317885a7bSLuigi Rizzo 	 * move backwards. However:
169417885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
169517885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
169617885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
169717885a7bSLuigi Rizzo 	 */
169817885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
169917885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1700f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
170137e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->nr_hwtail >= n);
1702f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1703f9790aebSLuigi Rizzo 	/* user sanity checks */
170417885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
170517885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
170637e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur || head > kring->nr_hwtail);
170717885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
170837e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
1709f9790aebSLuigi Rizzo 	} else {
171017885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
171137e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur && head > kring->nr_hwtail);
171217885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
171317885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
171417885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
171537e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
171617885a7bSLuigi Rizzo 		} else {
171717885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
171837e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head && cur > kring->nr_hwtail);
1719f9790aebSLuigi Rizzo 		}
1720f9790aebSLuigi Rizzo 	}
172117885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
172217885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
172317885a7bSLuigi Rizzo 			kring->name,
172417885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
172517885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
172617885a7bSLuigi Rizzo 	}
172717885a7bSLuigi Rizzo 	return head;
1728f9790aebSLuigi Rizzo }
1729f9790aebSLuigi Rizzo 
173017885a7bSLuigi Rizzo 
1731f9790aebSLuigi Rizzo /*
173268b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
173317885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
173468b8534bSLuigi Rizzo  * Return 1 on reinit.
1735506cc70cSLuigi Rizzo  *
1736506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1737506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
173817885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1739506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1740506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1741506cc70cSLuigi Rizzo  * it under lock.
174268b8534bSLuigi Rizzo  */
174368b8534bSLuigi Rizzo int
174468b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
174568b8534bSLuigi Rizzo {
174668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
174768b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
174868b8534bSLuigi Rizzo 	int errors = 0;
174968b8534bSLuigi Rizzo 
1750ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
17514bf50f18SLuigi Rizzo 	RD(10, "called for %s", kring->name);
175217885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
175317885a7bSLuigi Rizzo 	kring->rhead = ring->head;
175417885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
175517885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
175617885a7bSLuigi Rizzo 
175768b8534bSLuigi Rizzo 	if (ring->cur > lim)
175868b8534bSLuigi Rizzo 		errors++;
175917885a7bSLuigi Rizzo 	if (ring->head > lim)
176017885a7bSLuigi Rizzo 		errors++;
176117885a7bSLuigi Rizzo 	if (ring->tail > lim)
176217885a7bSLuigi Rizzo 		errors++;
176368b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
176468b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
176568b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
1766847bf383SLuigi Rizzo 		if (idx < 2 || idx >= kring->na->na_lut.objtotal) {
176717885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
176868b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
176968b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
17704bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
177168b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
177217885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
177368b8534bSLuigi Rizzo 		}
177468b8534bSLuigi Rizzo 	}
177568b8534bSLuigi Rizzo 	if (errors) {
17768241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
177717885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
177817885a7bSLuigi Rizzo 			kring->name,
177968b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
178017885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
178117885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
178217885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
178317885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
178468b8534bSLuigi Rizzo 	}
178568b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
178668b8534bSLuigi Rizzo }
178768b8534bSLuigi Rizzo 
17884bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
17894bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
17904bf50f18SLuigi Rizzo  *
17914bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
17924bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
17934bf50f18SLuigi Rizzo  *
179468b8534bSLuigi Rizzo  */
17954bf50f18SLuigi Rizzo int
17962ff91c17SVincenzo Maffione netmap_interp_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
17972ff91c17SVincenzo Maffione 			uint16_t nr_ringid, uint64_t nr_flags)
179868b8534bSLuigi Rizzo {
1799f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
180037e3a6d3SLuigi Rizzo 	int excluded_direction[] = { NR_TX_RINGS_ONLY, NR_RX_RINGS_ONLY };
1801847bf383SLuigi Rizzo 	enum txrx t;
18022ff91c17SVincenzo Maffione 	u_int j;
180368b8534bSLuigi Rizzo 
18042ff91c17SVincenzo Maffione 	if ((nr_flags & NR_PTNETMAP_HOST) && ((nr_mode != NR_REG_ALL_NIC) ||
18052ff91c17SVincenzo Maffione 			nr_flags & (NR_RX_RINGS_ONLY|NR_TX_RINGS_ONLY))) {
180637e3a6d3SLuigi Rizzo 		D("Error: only NR_REG_ALL_NIC supported with netmap passthrough");
180737e3a6d3SLuigi Rizzo 		return EINVAL;
180837e3a6d3SLuigi Rizzo 	}
180937e3a6d3SLuigi Rizzo 
181037e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
18112ff91c17SVincenzo Maffione 		if (nr_flags & excluded_direction[t]) {
181237e3a6d3SLuigi Rizzo 			priv->np_qfirst[t] = priv->np_qlast[t] = 0;
181337e3a6d3SLuigi Rizzo 			continue;
181437e3a6d3SLuigi Rizzo 		}
18152ff91c17SVincenzo Maffione 		switch (nr_mode) {
1816f0ea3689SLuigi Rizzo 		case NR_REG_ALL_NIC:
1817847bf383SLuigi Rizzo 			priv->np_qfirst[t] = 0;
1818847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t);
181937e3a6d3SLuigi Rizzo 			ND("ALL/PIPE: %s %d %d", nm_txrx2str(t),
182037e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1821f0ea3689SLuigi Rizzo 			break;
1822f0ea3689SLuigi Rizzo 		case NR_REG_SW:
1823f0ea3689SLuigi Rizzo 		case NR_REG_NIC_SW:
1824f0ea3689SLuigi Rizzo 			if (!(na->na_flags & NAF_HOST_RINGS)) {
1825f0ea3689SLuigi Rizzo 				D("host rings not supported");
1826f0ea3689SLuigi Rizzo 				return EINVAL;
1827f0ea3689SLuigi Rizzo 			}
18282ff91c17SVincenzo Maffione 			priv->np_qfirst[t] = (nr_mode == NR_REG_SW ?
1829847bf383SLuigi Rizzo 				nma_get_nrings(na, t) : 0);
1830847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t) + 1;
18312ff91c17SVincenzo Maffione 			ND("%s: %s %d %d", nr_mode == NR_REG_SW ? "SW" : "NIC+SW",
183237e3a6d3SLuigi Rizzo 				nm_txrx2str(t),
183337e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1834f0ea3689SLuigi Rizzo 			break;
1835f0ea3689SLuigi Rizzo 		case NR_REG_ONE_NIC:
18362ff91c17SVincenzo Maffione 			if (nr_ringid >= na->num_tx_rings &&
18372ff91c17SVincenzo Maffione 					nr_ringid >= na->num_rx_rings) {
18382ff91c17SVincenzo Maffione 				D("invalid ring id %d", nr_ringid);
1839f0ea3689SLuigi Rizzo 				return EINVAL;
1840f0ea3689SLuigi Rizzo 			}
1841f0ea3689SLuigi Rizzo 			/* if not enough rings, use the first one */
18422ff91c17SVincenzo Maffione 			j = nr_ringid;
1843847bf383SLuigi Rizzo 			if (j >= nma_get_nrings(na, t))
1844f0ea3689SLuigi Rizzo 				j = 0;
1845847bf383SLuigi Rizzo 			priv->np_qfirst[t] = j;
1846847bf383SLuigi Rizzo 			priv->np_qlast[t] = j + 1;
184737e3a6d3SLuigi Rizzo 			ND("ONE_NIC: %s %d %d", nm_txrx2str(t),
184837e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1849f0ea3689SLuigi Rizzo 			break;
1850f0ea3689SLuigi Rizzo 		default:
18512ff91c17SVincenzo Maffione 			D("invalid regif type %d", nr_mode);
1852f0ea3689SLuigi Rizzo 			return EINVAL;
185368b8534bSLuigi Rizzo 		}
185437e3a6d3SLuigi Rizzo 	}
18552ff91c17SVincenzo Maffione 	priv->np_flags = nr_flags | nr_mode; // TODO
18564bf50f18SLuigi Rizzo 
1857c3e9b4dbSLuiz Otavio O Souza 	/* Allow transparent forwarding mode in the host --> nic
1858c3e9b4dbSLuiz Otavio O Souza 	 * direction only if all the TX hw rings have been opened. */
1859c3e9b4dbSLuiz Otavio O Souza 	if (priv->np_qfirst[NR_TX] == 0 &&
1860c3e9b4dbSLuiz Otavio O Souza 			priv->np_qlast[NR_TX] >= na->num_tx_rings) {
1861c3e9b4dbSLuiz Otavio O Souza 		priv->np_sync_flags |= NAF_CAN_FORWARD_DOWN;
1862c3e9b4dbSLuiz Otavio O Souza 	}
1863c3e9b4dbSLuiz Otavio O Souza 
1864ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1865f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
18664bf50f18SLuigi Rizzo 			na->name,
1867847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1868847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1869847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1870847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX],
18712ff91c17SVincenzo Maffione 			nr_ringid);
1872ae10d1afSLuigi Rizzo 	}
187368b8534bSLuigi Rizzo 	return 0;
187468b8534bSLuigi Rizzo }
187568b8534bSLuigi Rizzo 
18764bf50f18SLuigi Rizzo 
18774bf50f18SLuigi Rizzo /*
18784bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
18794bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
18804bf50f18SLuigi Rizzo  */
18814bf50f18SLuigi Rizzo static int
18822ff91c17SVincenzo Maffione netmap_set_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
18832ff91c17SVincenzo Maffione 		uint16_t nr_ringid, uint64_t nr_flags)
18844bf50f18SLuigi Rizzo {
18854bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
18864bf50f18SLuigi Rizzo 	int error;
1887847bf383SLuigi Rizzo 	enum txrx t;
18884bf50f18SLuigi Rizzo 
18892ff91c17SVincenzo Maffione 	error = netmap_interp_ringid(priv, nr_mode, nr_ringid, nr_flags);
18904bf50f18SLuigi Rizzo 	if (error) {
18914bf50f18SLuigi Rizzo 		return error;
18924bf50f18SLuigi Rizzo 	}
18934bf50f18SLuigi Rizzo 
18942ff91c17SVincenzo Maffione 	priv->np_txpoll = (nr_flags & NR_NO_TX_POLL) ? 0 : 1;
18954bf50f18SLuigi Rizzo 
18964bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
18974bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
18984bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
18994bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
19004bf50f18SLuigi Rizzo 	 */
1901847bf383SLuigi Rizzo 	for_rx_tx(t) {
1902847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1903847bf383SLuigi Rizzo 			na->si_users[t]++;
1904847bf383SLuigi Rizzo 	}
19054bf50f18SLuigi Rizzo 	return 0;
19064bf50f18SLuigi Rizzo }
19074bf50f18SLuigi Rizzo 
1908847bf383SLuigi Rizzo static void
1909847bf383SLuigi Rizzo netmap_unset_ringid(struct netmap_priv_d *priv)
1910847bf383SLuigi Rizzo {
1911847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1912847bf383SLuigi Rizzo 	enum txrx t;
1913847bf383SLuigi Rizzo 
1914847bf383SLuigi Rizzo 	for_rx_tx(t) {
1915847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1916847bf383SLuigi Rizzo 			na->si_users[t]--;
1917847bf383SLuigi Rizzo 		priv->np_qfirst[t] = priv->np_qlast[t] = 0;
1918847bf383SLuigi Rizzo 	}
1919847bf383SLuigi Rizzo 	priv->np_flags = 0;
1920847bf383SLuigi Rizzo 	priv->np_txpoll = 0;
1921847bf383SLuigi Rizzo }
1922847bf383SLuigi Rizzo 
1923847bf383SLuigi Rizzo 
192437e3a6d3SLuigi Rizzo /* Set the nr_pending_mode for the requested rings.
192537e3a6d3SLuigi Rizzo  * If requested, also try to get exclusive access to the rings, provided
192637e3a6d3SLuigi Rizzo  * the rings we want to bind are not exclusively owned by a previous bind.
1927847bf383SLuigi Rizzo  */
1928847bf383SLuigi Rizzo static int
192937e3a6d3SLuigi Rizzo netmap_krings_get(struct netmap_priv_d *priv)
1930847bf383SLuigi Rizzo {
1931847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1932847bf383SLuigi Rizzo 	u_int i;
1933847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1934847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
1935847bf383SLuigi Rizzo 	enum txrx t;
1936847bf383SLuigi Rizzo 
19374f80b14cSVincenzo Maffione 	if (netmap_verbose)
19384f80b14cSVincenzo Maffione 		D("%s: grabbing tx [%d, %d) rx [%d, %d)",
1939847bf383SLuigi Rizzo 			na->name,
1940847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1941847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1942847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1943847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX]);
1944847bf383SLuigi Rizzo 
1945847bf383SLuigi Rizzo 	/* first round: check that all the requested rings
1946847bf383SLuigi Rizzo 	 * are neither alread exclusively owned, nor we
1947847bf383SLuigi Rizzo 	 * want exclusive ownership when they are already in use
1948847bf383SLuigi Rizzo 	 */
1949847bf383SLuigi Rizzo 	for_rx_tx(t) {
1950847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
19512ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
1952847bf383SLuigi Rizzo 			if ((kring->nr_kflags & NKR_EXCLUSIVE) ||
1953847bf383SLuigi Rizzo 			    (kring->users && excl))
1954847bf383SLuigi Rizzo 			{
1955847bf383SLuigi Rizzo 				ND("ring %s busy", kring->name);
1956847bf383SLuigi Rizzo 				return EBUSY;
1957847bf383SLuigi Rizzo 			}
1958847bf383SLuigi Rizzo 		}
1959847bf383SLuigi Rizzo 	}
1960847bf383SLuigi Rizzo 
196137e3a6d3SLuigi Rizzo 	/* second round: increment usage count (possibly marking them
196237e3a6d3SLuigi Rizzo 	 * as exclusive) and set the nr_pending_mode
1963847bf383SLuigi Rizzo 	 */
1964847bf383SLuigi Rizzo 	for_rx_tx(t) {
1965847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
19662ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
1967847bf383SLuigi Rizzo 			kring->users++;
1968847bf383SLuigi Rizzo 			if (excl)
1969847bf383SLuigi Rizzo 				kring->nr_kflags |= NKR_EXCLUSIVE;
197037e3a6d3SLuigi Rizzo 	                kring->nr_pending_mode = NKR_NETMAP_ON;
1971847bf383SLuigi Rizzo 		}
1972847bf383SLuigi Rizzo 	}
1973847bf383SLuigi Rizzo 
1974847bf383SLuigi Rizzo 	return 0;
1975847bf383SLuigi Rizzo 
1976847bf383SLuigi Rizzo }
1977847bf383SLuigi Rizzo 
197837e3a6d3SLuigi Rizzo /* Undo netmap_krings_get(). This is done by clearing the exclusive mode
197937e3a6d3SLuigi Rizzo  * if was asked on regif, and unset the nr_pending_mode if we are the
198037e3a6d3SLuigi Rizzo  * last users of the involved rings. */
1981847bf383SLuigi Rizzo static void
198237e3a6d3SLuigi Rizzo netmap_krings_put(struct netmap_priv_d *priv)
1983847bf383SLuigi Rizzo {
1984847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1985847bf383SLuigi Rizzo 	u_int i;
1986847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1987847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
1988847bf383SLuigi Rizzo 	enum txrx t;
1989847bf383SLuigi Rizzo 
1990847bf383SLuigi Rizzo 	ND("%s: releasing tx [%d, %d) rx [%d, %d)",
1991847bf383SLuigi Rizzo 			na->name,
1992847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1993847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1994847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1995847bf383SLuigi Rizzo 			priv->np_qlast[MR_RX]);
1996847bf383SLuigi Rizzo 
1997847bf383SLuigi Rizzo 	for_rx_tx(t) {
1998847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
19992ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
2000847bf383SLuigi Rizzo 			if (excl)
2001847bf383SLuigi Rizzo 				kring->nr_kflags &= ~NKR_EXCLUSIVE;
2002847bf383SLuigi Rizzo 			kring->users--;
200337e3a6d3SLuigi Rizzo 			if (kring->users == 0)
200437e3a6d3SLuigi Rizzo 				kring->nr_pending_mode = NKR_NETMAP_OFF;
2005847bf383SLuigi Rizzo 		}
2006847bf383SLuigi Rizzo 	}
2007847bf383SLuigi Rizzo }
2008847bf383SLuigi Rizzo 
20092ff91c17SVincenzo Maffione static int
20102ff91c17SVincenzo Maffione nm_priv_rx_enabled(struct netmap_priv_d *priv)
20112ff91c17SVincenzo Maffione {
20122ff91c17SVincenzo Maffione 	return (priv->np_qfirst[NR_RX] != priv->np_qlast[NR_RX]);
20132ff91c17SVincenzo Maffione }
20142ff91c17SVincenzo Maffione 
2015f18be576SLuigi Rizzo /*
2016f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
2017f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
2018ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
20194bf50f18SLuigi Rizzo  *
20204bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
20214bf50f18SLuigi Rizzo  *
20224bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
20234bf50f18SLuigi Rizzo  * (get current number and size of rings)
20244bf50f18SLuigi Rizzo  *
20254bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
20264bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
20274bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
20284bf50f18SLuigi Rizzo  *
20294bf50f18SLuigi Rizzo  *
2030847bf383SLuigi Rizzo  * na->nm_krings_create()
20314bf50f18SLuigi Rizzo  * (create and init the krings array)
20324bf50f18SLuigi Rizzo  *
20334bf50f18SLuigi Rizzo  * 	One of the following:
20344bf50f18SLuigi Rizzo  *
20354bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
20364bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
20374bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
20384bf50f18SLuigi Rizzo  *
20394bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
20404bf50f18SLuigi Rizzo  * 		add leases and scratchpads
20414bf50f18SLuigi Rizzo  *
20424bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
20434bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
20444bf50f18SLuigi Rizzo  * 		cross-link them
20454bf50f18SLuigi Rizzo  *
20464bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
20474bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
20484bf50f18SLuigi Rizzo  *
20494bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
20504bf50f18SLuigi Rizzo  *      	create both the brap krings array,
20514bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
20524bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
20534bf50f18SLuigi Rizzo  *
20544bf50f18SLuigi Rizzo  * na->nm_register(, 1)
20554bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
20564bf50f18SLuigi Rizzo  *
20574bf50f18SLuigi Rizzo  * 	This may be one of the following:
20584bf50f18SLuigi Rizzo  *
205937e3a6d3SLuigi Rizzo  * 	* netmap_hw_reg				        (hw ports)
20604bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
20614bf50f18SLuigi Rizzo  * 		the hardware specific callback;
20624bf50f18SLuigi Rizzo  *
20634bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
20644bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
20654bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
20664bf50f18SLuigi Rizzo  *		bridge write lock.
20674bf50f18SLuigi Rizzo  *
20684bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
20694bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
2070453130d9SPedro F. Giffuni  *		longer responsible for the lifetime of this
20714bf50f18SLuigi Rizzo  *		pipe end
20724bf50f18SLuigi Rizzo  *
20734bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
20744bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
20754bf50f18SLuigi Rizzo  *		rings
20764bf50f18SLuigi Rizzo  *
207737e3a6d3SLuigi Rizzo  *	* netmap_bwrap_reg				(bwraps)
20784bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
20794bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
20804bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
20814bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
20824bf50f18SLuigi Rizzo  *
20834bf50f18SLuigi Rizzo  *
2084f18be576SLuigi Rizzo  */
2085847bf383SLuigi Rizzo int
2086f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
20872ff91c17SVincenzo Maffione 	uint32_t nr_mode, uint16_t nr_ringid, uint64_t nr_flags)
2088f18be576SLuigi Rizzo {
2089f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
2090847bf383SLuigi Rizzo 	int error;
2091f18be576SLuigi Rizzo 
2092ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
2093f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
2094847bf383SLuigi Rizzo 	error = netmap_mem_finalize(na->nm_mem, na);
2095ce3ee1e7SLuigi Rizzo 	if (error)
2096847bf383SLuigi Rizzo 		goto err;
2097847bf383SLuigi Rizzo 
2098847bf383SLuigi Rizzo 	if (na->active_fds == 0) {
20992ff91c17SVincenzo Maffione 
21002ff91c17SVincenzo Maffione 		/* cache the allocator info in the na */
21012ff91c17SVincenzo Maffione 		error = netmap_mem_get_lut(na->nm_mem, &na->na_lut);
21022ff91c17SVincenzo Maffione 		if (error)
21032ff91c17SVincenzo Maffione 			goto err_drop_mem;
21042ff91c17SVincenzo Maffione 		ND("lut %p bufs %u size %u", na->na_lut.lut, na->na_lut.objtotal,
21052ff91c17SVincenzo Maffione 					    na->na_lut.objsize);
21062ff91c17SVincenzo Maffione 
21072ff91c17SVincenzo Maffione 		/* ring configuration may have changed, fetch from the card */
21082ff91c17SVincenzo Maffione 		netmap_update_config(na);
2109*cfa866f6SMatt Macy 	}
21102ff91c17SVincenzo Maffione 
2111*cfa866f6SMatt Macy 	/* compute the range of tx and rx rings to monitor */
2112*cfa866f6SMatt Macy 	error = netmap_set_ringid(priv, nr_mode, nr_ringid, nr_flags);
2113*cfa866f6SMatt Macy 	if (error)
2114*cfa866f6SMatt Macy 		goto err_put_lut;
2115*cfa866f6SMatt Macy 
2116*cfa866f6SMatt Macy 	if (na->active_fds == 0) {
2117847bf383SLuigi Rizzo 		/*
2118847bf383SLuigi Rizzo 		 * If this is the first registration of the adapter,
21194f80b14cSVincenzo Maffione 		 * perform sanity checks and create the in-kernel view
21204f80b14cSVincenzo Maffione 		 * of the netmap rings (the netmap krings).
2121847bf383SLuigi Rizzo 		 */
21222ff91c17SVincenzo Maffione 		if (na->ifp && nm_priv_rx_enabled(priv)) {
21234f80b14cSVincenzo Maffione 			/* This netmap adapter is attached to an ifnet. */
2124*cfa866f6SMatt Macy 			unsigned nbs = NETMAP_BUF_SIZE(na);
21254f80b14cSVincenzo Maffione 			unsigned mtu = nm_os_ifnet_mtu(na->ifp);
21264f80b14cSVincenzo Maffione 
2127*cfa866f6SMatt Macy 			ND("%s: mtu %d rx_buf_maxsize %d netmap_buf_size %d",
2128*cfa866f6SMatt Macy 					na->name, mtu, na->rx_buf_maxsize, nbs);
2129*cfa866f6SMatt Macy 
2130*cfa866f6SMatt Macy 			if (na->rx_buf_maxsize == 0) {
2131*cfa866f6SMatt Macy 				D("%s: error: rx_buf_maxsize == 0", na->name);
2132*cfa866f6SMatt Macy 				error = EIO;
2133*cfa866f6SMatt Macy 				goto err_drop_mem;
2134*cfa866f6SMatt Macy 			}
21352ff91c17SVincenzo Maffione 
21362ff91c17SVincenzo Maffione 			if (mtu <= na->rx_buf_maxsize) {
21374f80b14cSVincenzo Maffione 				/* The MTU fits a single NIC slot. We only
21384f80b14cSVincenzo Maffione 				 * Need to check that netmap buffers are
21394f80b14cSVincenzo Maffione 				 * large enough to hold an MTU. NS_MOREFRAG
21404f80b14cSVincenzo Maffione 				 * cannot be used in this case. */
21414f80b14cSVincenzo Maffione 				if (nbs < mtu) {
21424f80b14cSVincenzo Maffione 					nm_prerr("error: netmap buf size (%u) "
21432ff91c17SVincenzo Maffione 						"< device MTU (%u)\n", nbs, mtu);
21444f80b14cSVincenzo Maffione 					error = EINVAL;
21454f80b14cSVincenzo Maffione 					goto err_drop_mem;
21464f80b14cSVincenzo Maffione 				}
21474f80b14cSVincenzo Maffione 			} else {
21484f80b14cSVincenzo Maffione 				/* More NIC slots may be needed to receive
21494f80b14cSVincenzo Maffione 				 * or transmit a single packet. Check that
21504f80b14cSVincenzo Maffione 				 * the adapter supports NS_MOREFRAG and that
21514f80b14cSVincenzo Maffione 				 * netmap buffers are large enough to hold
21524f80b14cSVincenzo Maffione 				 * the maximum per-slot size. */
21534f80b14cSVincenzo Maffione 				if (!(na->na_flags & NAF_MOREFRAG)) {
21544f80b14cSVincenzo Maffione 					nm_prerr("error: large MTU (%d) needed "
21554f80b14cSVincenzo Maffione 						"but %s does not support "
21562ff91c17SVincenzo Maffione 						"NS_MOREFRAG\n", mtu,
21574f80b14cSVincenzo Maffione 						na->ifp->if_xname);
21584f80b14cSVincenzo Maffione 					error = EINVAL;
21594f80b14cSVincenzo Maffione 					goto err_drop_mem;
21602ff91c17SVincenzo Maffione 				} else if (nbs < na->rx_buf_maxsize) {
21614f80b14cSVincenzo Maffione 					nm_prerr("error: using NS_MOREFRAG on "
21624f80b14cSVincenzo Maffione 						"%s requires netmap buf size "
21632ff91c17SVincenzo Maffione 						">= %u\n", na->ifp->if_xname,
21642ff91c17SVincenzo Maffione 						na->rx_buf_maxsize);
21654f80b14cSVincenzo Maffione 					error = EINVAL;
21664f80b14cSVincenzo Maffione 					goto err_drop_mem;
21674f80b14cSVincenzo Maffione 				} else {
21684f80b14cSVincenzo Maffione 					nm_prinf("info: netmap application on "
21694f80b14cSVincenzo Maffione 						"%s needs to support "
21704f80b14cSVincenzo Maffione 						"NS_MOREFRAG "
21712ff91c17SVincenzo Maffione 						"(MTU=%u,netmap_buf_size=%u)\n",
21724f80b14cSVincenzo Maffione 						na->ifp->if_xname, mtu, nbs);
21734f80b14cSVincenzo Maffione 				}
21744f80b14cSVincenzo Maffione 			}
21754f80b14cSVincenzo Maffione 		}
2176847bf383SLuigi Rizzo 
2177847bf383SLuigi Rizzo 		/*
2178847bf383SLuigi Rizzo 		 * Depending on the adapter, this may also create
2179847bf383SLuigi Rizzo 		 * the netmap rings themselves
2180847bf383SLuigi Rizzo 		 */
2181847bf383SLuigi Rizzo 		error = na->nm_krings_create(na);
2182847bf383SLuigi Rizzo 		if (error)
21832ff91c17SVincenzo Maffione 			goto err_put_lut;
2184847bf383SLuigi Rizzo 
2185ce3ee1e7SLuigi Rizzo 	}
2186847bf383SLuigi Rizzo 
218737e3a6d3SLuigi Rizzo 	/* now the krings must exist and we can check whether some
218837e3a6d3SLuigi Rizzo 	 * previous bind has exclusive ownership on them, and set
218937e3a6d3SLuigi Rizzo 	 * nr_pending_mode
2190847bf383SLuigi Rizzo 	 */
219137e3a6d3SLuigi Rizzo 	error = netmap_krings_get(priv);
2192847bf383SLuigi Rizzo 	if (error)
219337e3a6d3SLuigi Rizzo 		goto err_del_krings;
219437e3a6d3SLuigi Rizzo 
219537e3a6d3SLuigi Rizzo 	/* create all needed missing netmap rings */
219637e3a6d3SLuigi Rizzo 	error = netmap_mem_rings_create(na);
219737e3a6d3SLuigi Rizzo 	if (error)
219837e3a6d3SLuigi Rizzo 		goto err_rel_excl;
2199847bf383SLuigi Rizzo 
2200847bf383SLuigi Rizzo 	/* in all cases, create a new netmap if */
2201c3e9b4dbSLuiz Otavio O Souza 	nifp = netmap_mem_if_new(na, priv);
2202847bf383SLuigi Rizzo 	if (nifp == NULL) {
2203f18be576SLuigi Rizzo 		error = ENOMEM;
2204*cfa866f6SMatt Macy 		goto err_rel_excl;
2205ce3ee1e7SLuigi Rizzo 	}
2206847bf383SLuigi Rizzo 
220737e3a6d3SLuigi Rizzo 	if (nm_kring_pending(priv)) {
220837e3a6d3SLuigi Rizzo 		/* Some kring is switching mode, tell the adapter to
220937e3a6d3SLuigi Rizzo 		 * react on this. */
221037e3a6d3SLuigi Rizzo 		error = na->nm_register(na, 1);
221137e3a6d3SLuigi Rizzo 		if (error)
22122ff91c17SVincenzo Maffione 			goto err_del_if;
221337e3a6d3SLuigi Rizzo 	}
221437e3a6d3SLuigi Rizzo 
221537e3a6d3SLuigi Rizzo 	/* Commit the reference. */
221637e3a6d3SLuigi Rizzo 	na->active_fds++;
221737e3a6d3SLuigi Rizzo 
2218ce3ee1e7SLuigi Rizzo 	/*
2219847bf383SLuigi Rizzo 	 * advertise that the interface is ready by setting np_nifp.
2220847bf383SLuigi Rizzo 	 * The barrier is needed because readers (poll, *SYNC and mmap)
2221ce3ee1e7SLuigi Rizzo 	 * check for priv->np_nifp != NULL without locking
2222ce3ee1e7SLuigi Rizzo 	 */
2223847bf383SLuigi Rizzo 	mb(); /* make sure previous writes are visible to all CPUs */
2224ce3ee1e7SLuigi Rizzo 	priv->np_nifp = nifp;
2225847bf383SLuigi Rizzo 
2226847bf383SLuigi Rizzo 	return 0;
2227847bf383SLuigi Rizzo 
222837e3a6d3SLuigi Rizzo err_del_if:
2229847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
22304f80b14cSVincenzo Maffione err_rel_excl:
22314f80b14cSVincenzo Maffione 	netmap_krings_put(priv);
2232*cfa866f6SMatt Macy 	netmap_mem_rings_delete(na);
2233847bf383SLuigi Rizzo err_del_krings:
2234847bf383SLuigi Rizzo 	if (na->active_fds == 0)
2235847bf383SLuigi Rizzo 		na->nm_krings_delete(na);
22362ff91c17SVincenzo Maffione err_put_lut:
22372ff91c17SVincenzo Maffione 	if (na->active_fds == 0)
22382ff91c17SVincenzo Maffione 		memset(&na->na_lut, 0, sizeof(na->na_lut));
2239847bf383SLuigi Rizzo err_drop_mem:
22404f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
2241847bf383SLuigi Rizzo err:
2242847bf383SLuigi Rizzo 	priv->np_na = NULL;
2243847bf383SLuigi Rizzo 	return error;
2244ce3ee1e7SLuigi Rizzo }
2245847bf383SLuigi Rizzo 
2246847bf383SLuigi Rizzo 
2247847bf383SLuigi Rizzo /*
224837e3a6d3SLuigi Rizzo  * update kring and ring at the end of rxsync/txsync.
2249847bf383SLuigi Rizzo  */
2250847bf383SLuigi Rizzo static inline void
225137e3a6d3SLuigi Rizzo nm_sync_finalize(struct netmap_kring *kring)
2252847bf383SLuigi Rizzo {
225337e3a6d3SLuigi Rizzo 	/*
225437e3a6d3SLuigi Rizzo 	 * Update ring tail to what the kernel knows
225537e3a6d3SLuigi Rizzo 	 * After txsync: head/rhead/hwcur might be behind cur/rcur
225637e3a6d3SLuigi Rizzo 	 * if no carrier.
225737e3a6d3SLuigi Rizzo 	 */
2258847bf383SLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
2259847bf383SLuigi Rizzo 
2260847bf383SLuigi Rizzo 	ND(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
2261847bf383SLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
2262847bf383SLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
2263847bf383SLuigi Rizzo }
2264847bf383SLuigi Rizzo 
2265c3e9b4dbSLuiz Otavio O Souza /* set ring timestamp */
2266c3e9b4dbSLuiz Otavio O Souza static inline void
2267c3e9b4dbSLuiz Otavio O Souza ring_timestamp_set(struct netmap_ring *ring)
2268c3e9b4dbSLuiz Otavio O Souza {
2269c3e9b4dbSLuiz Otavio O Souza 	if (netmap_no_timestamp == 0 || ring->flags & NR_TIMESTAMP) {
2270c3e9b4dbSLuiz Otavio O Souza 		microtime(&ring->ts);
2271c3e9b4dbSLuiz Otavio O Souza 	}
2272c3e9b4dbSLuiz Otavio O Souza }
2273c3e9b4dbSLuiz Otavio O Souza 
22742ff91c17SVincenzo Maffione static int nmreq_copyin(struct nmreq_header *, int);
22752ff91c17SVincenzo Maffione static int nmreq_copyout(struct nmreq_header *, int);
22762ff91c17SVincenzo Maffione static int nmreq_checkoptions(struct nmreq_header *);
2277c3e9b4dbSLuiz Otavio O Souza 
227868b8534bSLuigi Rizzo /*
227968b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
228068b8534bSLuigi Rizzo  *
228168b8534bSLuigi Rizzo  * Following a list of accepted commands:
22822ff91c17SVincenzo Maffione  * - NIOCCTRL		device control API
22832ff91c17SVincenzo Maffione  * - NIOCTXSYNC		sync TX rings
22842ff91c17SVincenzo Maffione  * - NIOCRXSYNC		sync RX rings
228568b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
22862ff91c17SVincenzo Maffione  * - NIOCGINFO		deprecated (legacy API)
22872ff91c17SVincenzo Maffione  * - NIOCREGIF		deprecated (legacy API)
228868b8534bSLuigi Rizzo  *
228968b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
229068b8534bSLuigi Rizzo  */
2291f9790aebSLuigi Rizzo int
22922ff91c17SVincenzo Maffione netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
22932ff91c17SVincenzo Maffione 		struct thread *td, int nr_body_is_user)
229468b8534bSLuigi Rizzo {
2295c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
2296ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
2297c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd = NULL;
229837e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
229937e3a6d3SLuigi Rizzo 	int error = 0;
2300f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
230168b8534bSLuigi Rizzo 	struct netmap_if *nifp;
23022ff91c17SVincenzo Maffione 	struct netmap_kring **krings;
2303c3e9b4dbSLuiz Otavio O Souza 	int sync_flags;
2304847bf383SLuigi Rizzo 	enum txrx t;
230568b8534bSLuigi Rizzo 
23062ff91c17SVincenzo Maffione 	switch (cmd) {
23072ff91c17SVincenzo Maffione 	case NIOCCTRL: {
23082ff91c17SVincenzo Maffione 		struct nmreq_header *hdr = (struct nmreq_header *)data;
23092ff91c17SVincenzo Maffione 
23102ff91c17SVincenzo Maffione 		if (hdr->nr_version != NETMAP_API) {
23112ff91c17SVincenzo Maffione 			D("API mismatch for reqtype %d: got %d need %d",
23122ff91c17SVincenzo Maffione 				hdr->nr_version,
23132ff91c17SVincenzo Maffione 				hdr->nr_version, NETMAP_API);
23142ff91c17SVincenzo Maffione 			hdr->nr_version = NETMAP_API;
2315f0ea3689SLuigi Rizzo 		}
23162ff91c17SVincenzo Maffione 		if (hdr->nr_version < NETMAP_MIN_API ||
23172ff91c17SVincenzo Maffione 		    hdr->nr_version > NETMAP_MAX_API) {
231817885a7bSLuigi Rizzo 			return EINVAL;
231917885a7bSLuigi Rizzo 		}
232068b8534bSLuigi Rizzo 
23212ff91c17SVincenzo Maffione 		/* Make a kernel-space copy of the user-space nr_body.
23222ff91c17SVincenzo Maffione 		 * For convenince, the nr_body pointer and the pointers
23232ff91c17SVincenzo Maffione 		 * in the options list will be replaced with their
23242ff91c17SVincenzo Maffione 		 * kernel-space counterparts. The original pointers are
23252ff91c17SVincenzo Maffione 		 * saved internally and later restored by nmreq_copyout
23262ff91c17SVincenzo Maffione 		 */
23272ff91c17SVincenzo Maffione 		error = nmreq_copyin(hdr, nr_body_is_user);
232837e3a6d3SLuigi Rizzo 		if (error) {
23292ff91c17SVincenzo Maffione 			return error;
2330ce3ee1e7SLuigi Rizzo 		}
2331ce3ee1e7SLuigi Rizzo 
23322ff91c17SVincenzo Maffione 		/* Sanitize hdr->nr_name. */
23332ff91c17SVincenzo Maffione 		hdr->nr_name[sizeof(hdr->nr_name) - 1] = '\0';
233468b8534bSLuigi Rizzo 
23352ff91c17SVincenzo Maffione 		switch (hdr->nr_reqtype) {
23362ff91c17SVincenzo Maffione 		case NETMAP_REQ_REGISTER: {
23372ff91c17SVincenzo Maffione 			struct nmreq_register *req =
2338*cfa866f6SMatt Macy 				(struct nmreq_register *)(uintptr_t)hdr->nr_body;
23392ff91c17SVincenzo Maffione 			/* Protect access to priv from concurrent requests. */
2340ce3ee1e7SLuigi Rizzo 			NMG_LOCK();
2341ce3ee1e7SLuigi Rizzo 			do {
2342ce3ee1e7SLuigi Rizzo 				u_int memflags;
23432ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
23442ff91c17SVincenzo Maffione 				struct nmreq_option *opt;
23452ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
2346ce3ee1e7SLuigi Rizzo 
2347847bf383SLuigi Rizzo 				if (priv->np_nifp != NULL) {	/* thread already registered */
2348f0ea3689SLuigi Rizzo 					error = EBUSY;
2349506cc70cSLuigi Rizzo 					break;
2350506cc70cSLuigi Rizzo 				}
2351c3e9b4dbSLuiz Otavio O Souza 
23522ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
2353*cfa866f6SMatt Macy 				opt = nmreq_findoption((struct nmreq_option *)(uintptr_t)hdr->nr_options,
23542ff91c17SVincenzo Maffione 						NETMAP_REQ_OPT_EXTMEM);
23552ff91c17SVincenzo Maffione 				if (opt != NULL) {
23562ff91c17SVincenzo Maffione 					struct nmreq_opt_extmem *e =
23572ff91c17SVincenzo Maffione 						(struct nmreq_opt_extmem *)opt;
23582ff91c17SVincenzo Maffione 
23592ff91c17SVincenzo Maffione 					error = nmreq_checkduplicate(opt);
23602ff91c17SVincenzo Maffione 					if (error) {
23612ff91c17SVincenzo Maffione 						opt->nro_status = error;
23622ff91c17SVincenzo Maffione 						break;
23632ff91c17SVincenzo Maffione 					}
23642ff91c17SVincenzo Maffione 					nmd = netmap_mem_ext_create(e->nro_usrptr,
23652ff91c17SVincenzo Maffione 							&e->nro_info, &error);
23662ff91c17SVincenzo Maffione 					opt->nro_status = error;
23672ff91c17SVincenzo Maffione 					if (nmd == NULL)
23682ff91c17SVincenzo Maffione 						break;
23692ff91c17SVincenzo Maffione 				}
23702ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
23712ff91c17SVincenzo Maffione 
23722ff91c17SVincenzo Maffione 				if (nmd == NULL && req->nr_mem_id) {
2373c3e9b4dbSLuiz Otavio O Souza 					/* find the allocator and get a reference */
23742ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id);
2375c3e9b4dbSLuiz Otavio O Souza 					if (nmd == NULL) {
2376c3e9b4dbSLuiz Otavio O Souza 						error = EINVAL;
2377c3e9b4dbSLuiz Otavio O Souza 						break;
2378c3e9b4dbSLuiz Otavio O Souza 					}
2379c3e9b4dbSLuiz Otavio O Souza 				}
238068b8534bSLuigi Rizzo 				/* find the interface and a reference */
23812ff91c17SVincenzo Maffione 				error = netmap_get_na(hdr, &na, &ifp, nmd,
238237e3a6d3SLuigi Rizzo 						      1 /* create */); /* keep reference */
238368b8534bSLuigi Rizzo 				if (error)
2384ce3ee1e7SLuigi Rizzo 					break;
2385f9790aebSLuigi Rizzo 				if (NETMAP_OWNED_BY_KERN(na)) {
2386ce3ee1e7SLuigi Rizzo 					error = EBUSY;
2387ce3ee1e7SLuigi Rizzo 					break;
2388f196ce38SLuigi Rizzo 				}
238937e3a6d3SLuigi Rizzo 
23902ff91c17SVincenzo Maffione 				if (na->virt_hdr_len && !(req->nr_flags & NR_ACCEPT_VNET_HDR)) {
239137e3a6d3SLuigi Rizzo 					error = EIO;
239237e3a6d3SLuigi Rizzo 					break;
239337e3a6d3SLuigi Rizzo 				}
239437e3a6d3SLuigi Rizzo 
23952ff91c17SVincenzo Maffione 				error = netmap_do_regif(priv, na, req->nr_mode,
23962ff91c17SVincenzo Maffione 							req->nr_ringid, req->nr_flags);
2397847bf383SLuigi Rizzo 				if (error) {    /* reg. failed, release priv and ref */
2398ce3ee1e7SLuigi Rizzo 					break;
239968b8534bSLuigi Rizzo 				}
2400847bf383SLuigi Rizzo 				nifp = priv->np_nifp;
24012ff91c17SVincenzo Maffione 				priv->np_td = td; /* for debugging purposes */
240268b8534bSLuigi Rizzo 
240368b8534bSLuigi Rizzo 				/* return the offset of the netmap_if object */
24042ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
24052ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
24062ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
24072ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
24082ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(na->nm_mem, &req->nr_memsize, &memflags,
24092ff91c17SVincenzo Maffione 					&req->nr_mem_id);
2410ce3ee1e7SLuigi Rizzo 				if (error) {
2411847bf383SLuigi Rizzo 					netmap_do_unregif(priv);
2412ce3ee1e7SLuigi Rizzo 					break;
2413ce3ee1e7SLuigi Rizzo 				}
2414ce3ee1e7SLuigi Rizzo 				if (memflags & NETMAP_MEM_PRIVATE) {
24153d819cb6SLuigi Rizzo 					*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2416ce3ee1e7SLuigi Rizzo 				}
2417847bf383SLuigi Rizzo 				for_rx_tx(t) {
2418847bf383SLuigi Rizzo 					priv->np_si[t] = nm_si_user(priv, t) ?
24192ff91c17SVincenzo Maffione 						&na->si[t] : &NMR(na, t)[priv->np_qfirst[t]]->si;
2420847bf383SLuigi Rizzo 				}
2421f0ea3689SLuigi Rizzo 
24222ff91c17SVincenzo Maffione 				if (req->nr_extra_bufs) {
242337e3a6d3SLuigi Rizzo 					if (netmap_verbose)
24242ff91c17SVincenzo Maffione 						D("requested %d extra buffers",
24252ff91c17SVincenzo Maffione 							req->nr_extra_bufs);
24262ff91c17SVincenzo Maffione 					req->nr_extra_bufs = netmap_extra_alloc(na,
24272ff91c17SVincenzo Maffione 						&nifp->ni_bufs_head, req->nr_extra_bufs);
242837e3a6d3SLuigi Rizzo 					if (netmap_verbose)
24292ff91c17SVincenzo Maffione 						D("got %d extra buffers", req->nr_extra_bufs);
2430f0ea3689SLuigi Rizzo 				}
24312ff91c17SVincenzo Maffione 				req->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
24322ff91c17SVincenzo Maffione 
24332ff91c17SVincenzo Maffione 				error = nmreq_checkoptions(hdr);
24342ff91c17SVincenzo Maffione 				if (error) {
24352ff91c17SVincenzo Maffione 					netmap_do_unregif(priv);
24362ff91c17SVincenzo Maffione 					break;
24372ff91c17SVincenzo Maffione 				}
243837e3a6d3SLuigi Rizzo 
243937e3a6d3SLuigi Rizzo 				/* store ifp reference so that priv destructor may release it */
244037e3a6d3SLuigi Rizzo 				priv->np_ifp = ifp;
2441ce3ee1e7SLuigi Rizzo 			} while (0);
2442c3e9b4dbSLuiz Otavio O Souza 			if (error) {
2443c3e9b4dbSLuiz Otavio O Souza 				netmap_unget_na(na, ifp);
2444c3e9b4dbSLuiz Otavio O Souza 			}
2445c3e9b4dbSLuiz Otavio O Souza 			/* release the reference from netmap_mem_find() or
2446c3e9b4dbSLuiz Otavio O Souza 			 * netmap_mem_ext_create()
2447c3e9b4dbSLuiz Otavio O Souza 			 */
2448c3e9b4dbSLuiz Otavio O Souza 			if (nmd)
2449c3e9b4dbSLuiz Otavio O Souza 				netmap_mem_put(nmd);
2450ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
245168b8534bSLuigi Rizzo 			break;
24522ff91c17SVincenzo Maffione 		}
24532ff91c17SVincenzo Maffione 
24542ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_INFO_GET: {
24552ff91c17SVincenzo Maffione 			struct nmreq_port_info_get *req =
2456*cfa866f6SMatt Macy 				(struct nmreq_port_info_get *)(uintptr_t)hdr->nr_body;
24572ff91c17SVincenzo Maffione 
24582ff91c17SVincenzo Maffione 			NMG_LOCK();
24592ff91c17SVincenzo Maffione 			do {
24602ff91c17SVincenzo Maffione 				u_int memflags;
24612ff91c17SVincenzo Maffione 
24622ff91c17SVincenzo Maffione 				if (hdr->nr_name[0] != '\0') {
24632ff91c17SVincenzo Maffione 					/* Build a nmreq_register out of the nmreq_port_info_get,
24642ff91c17SVincenzo Maffione 					 * so that we can call netmap_get_na(). */
24652ff91c17SVincenzo Maffione 					struct nmreq_register regreq;
24662ff91c17SVincenzo Maffione 					bzero(&regreq, sizeof(regreq));
24672ff91c17SVincenzo Maffione 					regreq.nr_tx_slots = req->nr_tx_slots;
24682ff91c17SVincenzo Maffione 					regreq.nr_rx_slots = req->nr_rx_slots;
24692ff91c17SVincenzo Maffione 					regreq.nr_tx_rings = req->nr_tx_rings;
24702ff91c17SVincenzo Maffione 					regreq.nr_rx_rings = req->nr_rx_rings;
24712ff91c17SVincenzo Maffione 					regreq.nr_mem_id = req->nr_mem_id;
24722ff91c17SVincenzo Maffione 
24732ff91c17SVincenzo Maffione 					/* get a refcount */
24742ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2475*cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)&regreq;
24762ff91c17SVincenzo Maffione 					error = netmap_get_na(hdr, &na, &ifp, NULL, 1 /* create */);
24772ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_PORT_INFO_GET; /* reset type */
2478*cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)req; /* reset nr_body */
24792ff91c17SVincenzo Maffione 					if (error) {
24802ff91c17SVincenzo Maffione 						na = NULL;
24812ff91c17SVincenzo Maffione 						ifp = NULL;
24822ff91c17SVincenzo Maffione 						break;
24832ff91c17SVincenzo Maffione 					}
24842ff91c17SVincenzo Maffione 					nmd = na->nm_mem; /* get memory allocator */
24852ff91c17SVincenzo Maffione 				} else {
24862ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id ? req->nr_mem_id : 1);
24872ff91c17SVincenzo Maffione 					if (nmd == NULL) {
24882ff91c17SVincenzo Maffione 						error = EINVAL;
24892ff91c17SVincenzo Maffione 						break;
24902ff91c17SVincenzo Maffione 					}
24912ff91c17SVincenzo Maffione 				}
24922ff91c17SVincenzo Maffione 
24932ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(nmd, &req->nr_memsize, &memflags,
24942ff91c17SVincenzo Maffione 					&req->nr_mem_id);
24952ff91c17SVincenzo Maffione 				if (error)
24962ff91c17SVincenzo Maffione 					break;
24972ff91c17SVincenzo Maffione 				if (na == NULL) /* only memory info */
24982ff91c17SVincenzo Maffione 					break;
24992ff91c17SVincenzo Maffione 				req->nr_offset = 0;
25002ff91c17SVincenzo Maffione 				req->nr_rx_slots = req->nr_tx_slots = 0;
25012ff91c17SVincenzo Maffione 				netmap_update_config(na);
25022ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
25032ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
25042ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
25052ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
25062ff91c17SVincenzo Maffione 			} while (0);
25072ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
25082ff91c17SVincenzo Maffione 			NMG_UNLOCK();
25092ff91c17SVincenzo Maffione 			break;
25102ff91c17SVincenzo Maffione 		}
25112ff91c17SVincenzo Maffione #ifdef WITH_VALE
25122ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_ATTACH: {
25132ff91c17SVincenzo Maffione 			error = nm_bdg_ctl_attach(hdr, NULL /* userspace request */);
25142ff91c17SVincenzo Maffione 			break;
25152ff91c17SVincenzo Maffione 		}
25162ff91c17SVincenzo Maffione 
25172ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DETACH: {
25182ff91c17SVincenzo Maffione 			error = nm_bdg_ctl_detach(hdr, NULL /* userspace request */);
25192ff91c17SVincenzo Maffione 			break;
25202ff91c17SVincenzo Maffione 		}
25212ff91c17SVincenzo Maffione 
25222ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_LIST: {
25232ff91c17SVincenzo Maffione 			error = netmap_bdg_list(hdr);
25242ff91c17SVincenzo Maffione 			break;
25252ff91c17SVincenzo Maffione 		}
25262ff91c17SVincenzo Maffione 
25272ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_SET: {
25282ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2529*cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
25302ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
25312ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
25322ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
25332ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
25342ff91c17SVincenzo Maffione 			/* For now we only support virtio-net headers, and only for
25352ff91c17SVincenzo Maffione 			 * VALE ports, but this may change in future. Valid lengths
25362ff91c17SVincenzo Maffione 			 * for the virtio-net header are 0 (no header), 10 and 12. */
25372ff91c17SVincenzo Maffione 			if (req->nr_hdr_len != 0 &&
25382ff91c17SVincenzo Maffione 				req->nr_hdr_len != sizeof(struct nm_vnet_hdr) &&
25392ff91c17SVincenzo Maffione 					req->nr_hdr_len != 12) {
25402ff91c17SVincenzo Maffione 				error = EINVAL;
25412ff91c17SVincenzo Maffione 				break;
25422ff91c17SVincenzo Maffione 			}
25432ff91c17SVincenzo Maffione 			NMG_LOCK();
25442ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2545*cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
25462ff91c17SVincenzo Maffione 			error = netmap_get_bdg_na(hdr, &na, NULL, 0);
25472ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_SET;
2548*cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
25492ff91c17SVincenzo Maffione 			if (na && !error) {
25502ff91c17SVincenzo Maffione 				struct netmap_vp_adapter *vpna =
25512ff91c17SVincenzo Maffione 					(struct netmap_vp_adapter *)na;
25522ff91c17SVincenzo Maffione 				na->virt_hdr_len = req->nr_hdr_len;
25532ff91c17SVincenzo Maffione 				if (na->virt_hdr_len) {
25542ff91c17SVincenzo Maffione 					vpna->mfs = NETMAP_BUF_SIZE(na);
25552ff91c17SVincenzo Maffione 				}
25562ff91c17SVincenzo Maffione 				D("Using vnet_hdr_len %d for %p", na->virt_hdr_len, na);
25572ff91c17SVincenzo Maffione 				netmap_adapter_put(na);
25582ff91c17SVincenzo Maffione 			} else if (!na) {
25592ff91c17SVincenzo Maffione 				error = ENXIO;
25602ff91c17SVincenzo Maffione 			}
25612ff91c17SVincenzo Maffione 			NMG_UNLOCK();
25622ff91c17SVincenzo Maffione 			break;
25632ff91c17SVincenzo Maffione 		}
25642ff91c17SVincenzo Maffione 
25652ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_GET: {
25662ff91c17SVincenzo Maffione 			/* Get vnet-header length for this netmap port */
25672ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2568*cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
25692ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
25702ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
25712ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
25722ff91c17SVincenzo Maffione 			struct ifnet *ifp;
25732ff91c17SVincenzo Maffione 
25742ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
25752ff91c17SVincenzo Maffione 			NMG_LOCK();
25762ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2577*cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
25782ff91c17SVincenzo Maffione 			error = netmap_get_na(hdr, &na, &ifp, NULL, 0);
25792ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_GET;
2580*cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
25812ff91c17SVincenzo Maffione 			if (na && !error) {
25822ff91c17SVincenzo Maffione 				req->nr_hdr_len = na->virt_hdr_len;
25832ff91c17SVincenzo Maffione 			}
25842ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
25852ff91c17SVincenzo Maffione 			NMG_UNLOCK();
25862ff91c17SVincenzo Maffione 			break;
25872ff91c17SVincenzo Maffione 		}
25882ff91c17SVincenzo Maffione 
25892ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_NEWIF: {
25902ff91c17SVincenzo Maffione 			error = nm_vi_create(hdr);
25912ff91c17SVincenzo Maffione 			break;
25922ff91c17SVincenzo Maffione 		}
25932ff91c17SVincenzo Maffione 
25942ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DELIF: {
25952ff91c17SVincenzo Maffione 			error = nm_vi_destroy(hdr->nr_name);
25962ff91c17SVincenzo Maffione 			break;
25972ff91c17SVincenzo Maffione 		}
25982ff91c17SVincenzo Maffione 
25992ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_ENABLE:
26002ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_DISABLE: {
26012ff91c17SVincenzo Maffione 			error = nm_bdg_polling(hdr);
26022ff91c17SVincenzo Maffione 			break;
26032ff91c17SVincenzo Maffione 		}
26042ff91c17SVincenzo Maffione #endif  /* WITH_VALE */
26052ff91c17SVincenzo Maffione 		case NETMAP_REQ_POOLS_INFO_GET: {
26062ff91c17SVincenzo Maffione 			struct nmreq_pools_info *req =
2607*cfa866f6SMatt Macy 				(struct nmreq_pools_info *)(uintptr_t)hdr->nr_body;
26082ff91c17SVincenzo Maffione 			/* Get information from the memory allocator. This
26092ff91c17SVincenzo Maffione 			 * netmap device must already be bound to a port.
26102ff91c17SVincenzo Maffione 			 * Note that hdr->nr_name is ignored. */
26112ff91c17SVincenzo Maffione 			NMG_LOCK();
26122ff91c17SVincenzo Maffione 			if (priv->np_na && priv->np_na->nm_mem) {
26132ff91c17SVincenzo Maffione 				struct netmap_mem_d *nmd = priv->np_na->nm_mem;
26142ff91c17SVincenzo Maffione 				error = netmap_mem_pools_info_get(req, nmd);
26152ff91c17SVincenzo Maffione 			} else {
26162ff91c17SVincenzo Maffione 				error = EINVAL;
26172ff91c17SVincenzo Maffione 			}
26182ff91c17SVincenzo Maffione 			NMG_UNLOCK();
26192ff91c17SVincenzo Maffione 			break;
26202ff91c17SVincenzo Maffione 		}
26212ff91c17SVincenzo Maffione 
26222ff91c17SVincenzo Maffione 		default: {
26232ff91c17SVincenzo Maffione 			error = EINVAL;
26242ff91c17SVincenzo Maffione 			break;
26252ff91c17SVincenzo Maffione 		}
26262ff91c17SVincenzo Maffione 		}
26272ff91c17SVincenzo Maffione 		/* Write back request body to userspace and reset the
26282ff91c17SVincenzo Maffione 		 * user-space pointer. */
26292ff91c17SVincenzo Maffione 		error = nmreq_copyout(hdr, error);
26302ff91c17SVincenzo Maffione 		break;
26312ff91c17SVincenzo Maffione 	}
263268b8534bSLuigi Rizzo 
263368b8534bSLuigi Rizzo 	case NIOCTXSYNC:
26342ff91c17SVincenzo Maffione 	case NIOCRXSYNC: {
26358241616dSLuigi Rizzo 		nifp = priv->np_nifp;
26368241616dSLuigi Rizzo 
26378241616dSLuigi Rizzo 		if (nifp == NULL) {
2638506cc70cSLuigi Rizzo 			error = ENXIO;
2639506cc70cSLuigi Rizzo 			break;
2640506cc70cSLuigi Rizzo 		}
26416641c68bSLuigi Rizzo 		mb(); /* make sure following reads are not from cache */
26428241616dSLuigi Rizzo 
2643f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
26448241616dSLuigi Rizzo 
2645f9790aebSLuigi Rizzo 		if (na == NULL) {
2646f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
26478241616dSLuigi Rizzo 			error = ENXIO;
26488241616dSLuigi Rizzo 			break;
26498241616dSLuigi Rizzo 		}
26508241616dSLuigi Rizzo 
2651c3e9b4dbSLuiz Otavio O Souza 		mbq_init(&q);
2652847bf383SLuigi Rizzo 		t = (cmd == NIOCTXSYNC ? NR_TX : NR_RX);
2653847bf383SLuigi Rizzo 		krings = NMR(na, t);
2654847bf383SLuigi Rizzo 		qfirst = priv->np_qfirst[t];
2655847bf383SLuigi Rizzo 		qlast = priv->np_qlast[t];
2656c3e9b4dbSLuiz Otavio O Souza 		sync_flags = priv->np_sync_flags;
265768b8534bSLuigi Rizzo 
2658f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
26592ff91c17SVincenzo Maffione 			struct netmap_kring *kring = krings[i];
266037e3a6d3SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
266137e3a6d3SLuigi Rizzo 
266237e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &error))) {
266337e3a6d3SLuigi Rizzo 				error = (error ? EIO : 0);
266437e3a6d3SLuigi Rizzo 				continue;
2665ce3ee1e7SLuigi Rizzo 			}
266637e3a6d3SLuigi Rizzo 
266768b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
266868b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
26693c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
267037e3a6d3SLuigi Rizzo 					    i, ring->cur,
267168b8534bSLuigi Rizzo 					    kring->nr_hwcur);
267237e3a6d3SLuigi Rizzo 				if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
267317885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
2674c3e9b4dbSLuiz Otavio O Souza 				} else if (kring->nm_sync(kring, sync_flags | NAF_FORCE_RECLAIM) == 0) {
267537e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
267617885a7bSLuigi Rizzo 				}
267768b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
26783c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
267937e3a6d3SLuigi Rizzo 					    i, ring->cur,
268068b8534bSLuigi Rizzo 					    kring->nr_hwcur);
268168b8534bSLuigi Rizzo 			} else {
268237e3a6d3SLuigi Rizzo 				if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
2683847bf383SLuigi Rizzo 					netmap_ring_reinit(kring);
2684c3e9b4dbSLuiz Otavio O Souza 				}
2685c3e9b4dbSLuiz Otavio O Souza 				if (nm_may_forward_up(kring)) {
2686c3e9b4dbSLuiz Otavio O Souza 					/* transparent forwarding, see netmap_poll() */
2687c3e9b4dbSLuiz Otavio O Souza 					netmap_grab_packets(kring, &q, netmap_fwd);
2688c3e9b4dbSLuiz Otavio O Souza 				}
2689c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags | NAF_FORCE_READ) == 0) {
269037e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
2691847bf383SLuigi Rizzo 				}
2692c3e9b4dbSLuiz Otavio O Souza 				ring_timestamp_set(ring);
269368b8534bSLuigi Rizzo 			}
2694ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
269568b8534bSLuigi Rizzo 		}
269668b8534bSLuigi Rizzo 
2697c3e9b4dbSLuiz Otavio O Souza 		if (mbq_peek(&q)) {
2698c3e9b4dbSLuiz Otavio O Souza 			netmap_send_up(na->ifp, &q);
2699c3e9b4dbSLuiz Otavio O Souza 		}
2700c3e9b4dbSLuiz Otavio O Souza 
270168b8534bSLuigi Rizzo 		break;
270268b8534bSLuigi Rizzo 	}
2703f196ce38SLuigi Rizzo 
27042ff91c17SVincenzo Maffione 	default: {
27052ff91c17SVincenzo Maffione 		return netmap_ioctl_legacy(priv, cmd, data, td);
27062ff91c17SVincenzo Maffione 		break;
27072ff91c17SVincenzo Maffione 	}
270868b8534bSLuigi Rizzo 	}
270968b8534bSLuigi Rizzo 
271068b8534bSLuigi Rizzo 	return (error);
271168b8534bSLuigi Rizzo }
271268b8534bSLuigi Rizzo 
27132ff91c17SVincenzo Maffione size_t
27142ff91c17SVincenzo Maffione nmreq_size_by_type(uint16_t nr_reqtype)
27152ff91c17SVincenzo Maffione {
27162ff91c17SVincenzo Maffione 	switch (nr_reqtype) {
27172ff91c17SVincenzo Maffione 	case NETMAP_REQ_REGISTER:
27182ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_register);
27192ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_INFO_GET:
27202ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_info_get);
27212ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_ATTACH:
27222ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_attach);
27232ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DETACH:
27242ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_detach);
27252ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_LIST:
27262ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_list);
27272ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_SET:
27282ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_GET:
27292ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_hdr);
27302ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_NEWIF:
27312ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_newif);
27322ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DELIF:
27332ff91c17SVincenzo Maffione 		return 0;
27342ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_ENABLE:
27352ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_DISABLE:
27362ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_polling);
27372ff91c17SVincenzo Maffione 	case NETMAP_REQ_POOLS_INFO_GET:
27382ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_pools_info);
27392ff91c17SVincenzo Maffione 	}
27402ff91c17SVincenzo Maffione 	return 0;
27412ff91c17SVincenzo Maffione }
27422ff91c17SVincenzo Maffione 
27432ff91c17SVincenzo Maffione static size_t
27442ff91c17SVincenzo Maffione nmreq_opt_size_by_type(uint16_t nro_reqtype)
27452ff91c17SVincenzo Maffione {
27462ff91c17SVincenzo Maffione 	size_t rv = sizeof(struct nmreq_option);
27472ff91c17SVincenzo Maffione #ifdef NETMAP_REQ_OPT_DEBUG
27482ff91c17SVincenzo Maffione 	if (nro_reqtype & NETMAP_REQ_OPT_DEBUG)
27492ff91c17SVincenzo Maffione 		return (nro_reqtype & ~NETMAP_REQ_OPT_DEBUG);
27502ff91c17SVincenzo Maffione #endif /* NETMAP_REQ_OPT_DEBUG */
27512ff91c17SVincenzo Maffione 	switch (nro_reqtype) {
27522ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
27532ff91c17SVincenzo Maffione 	case NETMAP_REQ_OPT_EXTMEM:
27542ff91c17SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_extmem);
27552ff91c17SVincenzo Maffione 		break;
27562ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
27572ff91c17SVincenzo Maffione 	}
27582ff91c17SVincenzo Maffione 	/* subtract the common header */
27592ff91c17SVincenzo Maffione 	return rv - sizeof(struct nmreq_option);
27602ff91c17SVincenzo Maffione }
27612ff91c17SVincenzo Maffione 
27622ff91c17SVincenzo Maffione int
27632ff91c17SVincenzo Maffione nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user)
27642ff91c17SVincenzo Maffione {
27652ff91c17SVincenzo Maffione 	size_t rqsz, optsz, bufsz;
27662ff91c17SVincenzo Maffione 	int error;
27672ff91c17SVincenzo Maffione 	char *ker = NULL, *p;
27682ff91c17SVincenzo Maffione 	struct nmreq_option **next, *src;
27692ff91c17SVincenzo Maffione 	struct nmreq_option buf;
27702ff91c17SVincenzo Maffione 	uint64_t *ptrs;
27712ff91c17SVincenzo Maffione 
27722ff91c17SVincenzo Maffione 	if (hdr->nr_reserved)
27732ff91c17SVincenzo Maffione 		return EINVAL;
27742ff91c17SVincenzo Maffione 
27752ff91c17SVincenzo Maffione 	if (!nr_body_is_user)
27762ff91c17SVincenzo Maffione 		return 0;
27772ff91c17SVincenzo Maffione 
27782ff91c17SVincenzo Maffione 	hdr->nr_reserved = nr_body_is_user;
27792ff91c17SVincenzo Maffione 
27802ff91c17SVincenzo Maffione 	/* compute the total size of the buffer */
27812ff91c17SVincenzo Maffione 	rqsz = nmreq_size_by_type(hdr->nr_reqtype);
27822ff91c17SVincenzo Maffione 	if (rqsz > NETMAP_REQ_MAXSIZE) {
27832ff91c17SVincenzo Maffione 		error = EMSGSIZE;
27842ff91c17SVincenzo Maffione 		goto out_err;
27852ff91c17SVincenzo Maffione 	}
2786*cfa866f6SMatt Macy 	if ((rqsz && hdr->nr_body == (uintptr_t)NULL) ||
2787*cfa866f6SMatt Macy 		(!rqsz && hdr->nr_body != (uintptr_t)NULL)) {
27882ff91c17SVincenzo Maffione 		/* Request body expected, but not found; or
27892ff91c17SVincenzo Maffione 		 * request body found but unexpected. */
27902ff91c17SVincenzo Maffione 		error = EINVAL;
27912ff91c17SVincenzo Maffione 		goto out_err;
27922ff91c17SVincenzo Maffione 	}
27932ff91c17SVincenzo Maffione 
27942ff91c17SVincenzo Maffione 	bufsz = 2 * sizeof(void *) + rqsz;
27952ff91c17SVincenzo Maffione 	optsz = 0;
2796*cfa866f6SMatt Macy 	for (src = (struct nmreq_option *)(uintptr_t)hdr->nr_options; src;
2797*cfa866f6SMatt Macy 	     src = (struct nmreq_option *)(uintptr_t)buf.nro_next)
27982ff91c17SVincenzo Maffione 	{
27992ff91c17SVincenzo Maffione 		error = copyin(src, &buf, sizeof(*src));
28002ff91c17SVincenzo Maffione 		if (error)
28012ff91c17SVincenzo Maffione 			goto out_err;
28022ff91c17SVincenzo Maffione 		optsz += sizeof(*src);
28032ff91c17SVincenzo Maffione 		optsz += nmreq_opt_size_by_type(buf.nro_reqtype);
28042ff91c17SVincenzo Maffione 		if (rqsz + optsz > NETMAP_REQ_MAXSIZE) {
28052ff91c17SVincenzo Maffione 			error = EMSGSIZE;
28062ff91c17SVincenzo Maffione 			goto out_err;
28072ff91c17SVincenzo Maffione 		}
28082ff91c17SVincenzo Maffione 		bufsz += optsz + sizeof(void *);
28092ff91c17SVincenzo Maffione 	}
28102ff91c17SVincenzo Maffione 
28112ff91c17SVincenzo Maffione 	ker = nm_os_malloc(bufsz);
28122ff91c17SVincenzo Maffione 	if (ker == NULL) {
28132ff91c17SVincenzo Maffione 		error = ENOMEM;
28142ff91c17SVincenzo Maffione 		goto out_err;
28152ff91c17SVincenzo Maffione 	}
28162ff91c17SVincenzo Maffione 	p = ker;
28172ff91c17SVincenzo Maffione 
28182ff91c17SVincenzo Maffione 	/* make a copy of the user pointers */
28192ff91c17SVincenzo Maffione 	ptrs = (uint64_t*)p;
28202ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_body;
28212ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_options;
28222ff91c17SVincenzo Maffione 	p = (char *)ptrs;
28232ff91c17SVincenzo Maffione 
28242ff91c17SVincenzo Maffione 	/* copy the body */
2825*cfa866f6SMatt Macy 	error = copyin((void *)(uintptr_t)hdr->nr_body, p, rqsz);
28262ff91c17SVincenzo Maffione 	if (error)
28272ff91c17SVincenzo Maffione 		goto out_restore;
28282ff91c17SVincenzo Maffione 	/* overwrite the user pointer with the in-kernel one */
2829*cfa866f6SMatt Macy 	hdr->nr_body = (uintptr_t)p;
28302ff91c17SVincenzo Maffione 	p += rqsz;
28312ff91c17SVincenzo Maffione 
28322ff91c17SVincenzo Maffione 	/* copy the options */
28332ff91c17SVincenzo Maffione 	next = (struct nmreq_option **)&hdr->nr_options;
28342ff91c17SVincenzo Maffione 	src = *next;
28352ff91c17SVincenzo Maffione 	while (src) {
28362ff91c17SVincenzo Maffione 		struct nmreq_option *opt;
28372ff91c17SVincenzo Maffione 
28382ff91c17SVincenzo Maffione 		/* copy the option header */
28392ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)p;
28402ff91c17SVincenzo Maffione 		opt = (struct nmreq_option *)(ptrs + 1);
28412ff91c17SVincenzo Maffione 		error = copyin(src, opt, sizeof(*src));
28422ff91c17SVincenzo Maffione 		if (error)
28432ff91c17SVincenzo Maffione 			goto out_restore;
28442ff91c17SVincenzo Maffione 		/* make a copy of the user next pointer */
28452ff91c17SVincenzo Maffione 		*ptrs = opt->nro_next;
28462ff91c17SVincenzo Maffione 		/* overwrite the user pointer with the in-kernel one */
28472ff91c17SVincenzo Maffione 		*next = opt;
28482ff91c17SVincenzo Maffione 
28492ff91c17SVincenzo Maffione 		/* initialize the option as not supported.
28502ff91c17SVincenzo Maffione 		 * Recognized options will update this field.
28512ff91c17SVincenzo Maffione 		 */
28522ff91c17SVincenzo Maffione 		opt->nro_status = EOPNOTSUPP;
28532ff91c17SVincenzo Maffione 
28542ff91c17SVincenzo Maffione 		p = (char *)(opt + 1);
28552ff91c17SVincenzo Maffione 
28562ff91c17SVincenzo Maffione 		/* copy the option body */
28572ff91c17SVincenzo Maffione 		optsz = nmreq_opt_size_by_type(opt->nro_reqtype);
28582ff91c17SVincenzo Maffione 		if (optsz) {
28592ff91c17SVincenzo Maffione 			/* the option body follows the option header */
28602ff91c17SVincenzo Maffione 			error = copyin(src + 1, p, optsz);
28612ff91c17SVincenzo Maffione 			if (error)
28622ff91c17SVincenzo Maffione 				goto out_restore;
28632ff91c17SVincenzo Maffione 			p += optsz;
28642ff91c17SVincenzo Maffione 		}
28652ff91c17SVincenzo Maffione 
28662ff91c17SVincenzo Maffione 		/* move to next option */
28672ff91c17SVincenzo Maffione 		next = (struct nmreq_option **)&opt->nro_next;
28682ff91c17SVincenzo Maffione 		src = *next;
28692ff91c17SVincenzo Maffione 	}
28702ff91c17SVincenzo Maffione 	return 0;
28712ff91c17SVincenzo Maffione 
28722ff91c17SVincenzo Maffione out_restore:
28732ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker;
28742ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
28752ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs++;
28762ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
28772ff91c17SVincenzo Maffione 	nm_os_free(ker);
28782ff91c17SVincenzo Maffione out_err:
28792ff91c17SVincenzo Maffione 	return error;
28802ff91c17SVincenzo Maffione }
28812ff91c17SVincenzo Maffione 
28822ff91c17SVincenzo Maffione static int
28832ff91c17SVincenzo Maffione nmreq_copyout(struct nmreq_header *hdr, int rerror)
28842ff91c17SVincenzo Maffione {
28852ff91c17SVincenzo Maffione 	struct nmreq_option *src, *dst;
2886*cfa866f6SMatt Macy 	void *ker = (void *)(uintptr_t)hdr->nr_body, *bufstart;
28872ff91c17SVincenzo Maffione 	uint64_t *ptrs;
28882ff91c17SVincenzo Maffione 	size_t bodysz;
28892ff91c17SVincenzo Maffione 	int error;
28902ff91c17SVincenzo Maffione 
28912ff91c17SVincenzo Maffione 	if (!hdr->nr_reserved)
28922ff91c17SVincenzo Maffione 		return rerror;
28932ff91c17SVincenzo Maffione 
28942ff91c17SVincenzo Maffione 	/* restore the user pointers in the header */
28952ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker - 2;
28962ff91c17SVincenzo Maffione 	bufstart = ptrs;
28972ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
2898*cfa866f6SMatt Macy 	src = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
28992ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs;
29002ff91c17SVincenzo Maffione 
29012ff91c17SVincenzo Maffione 	if (!rerror) {
29022ff91c17SVincenzo Maffione 		/* copy the body */
29032ff91c17SVincenzo Maffione 		bodysz = nmreq_size_by_type(hdr->nr_reqtype);
2904*cfa866f6SMatt Macy 		error = copyout(ker, (void *)(uintptr_t)hdr->nr_body, bodysz);
29052ff91c17SVincenzo Maffione 		if (error) {
29062ff91c17SVincenzo Maffione 			rerror = error;
29072ff91c17SVincenzo Maffione 			goto out;
29082ff91c17SVincenzo Maffione 		}
29092ff91c17SVincenzo Maffione 	}
29102ff91c17SVincenzo Maffione 
29112ff91c17SVincenzo Maffione 	/* copy the options */
2912*cfa866f6SMatt Macy 	dst = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
29132ff91c17SVincenzo Maffione 	while (src) {
29142ff91c17SVincenzo Maffione 		size_t optsz;
29152ff91c17SVincenzo Maffione 		uint64_t next;
29162ff91c17SVincenzo Maffione 
29172ff91c17SVincenzo Maffione 		/* restore the user pointer */
29182ff91c17SVincenzo Maffione 		next = src->nro_next;
29192ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)src - 1;
29202ff91c17SVincenzo Maffione 		src->nro_next = *ptrs;
29212ff91c17SVincenzo Maffione 
29222ff91c17SVincenzo Maffione 		/* always copy the option header */
29232ff91c17SVincenzo Maffione 		error = copyout(src, dst, sizeof(*src));
29242ff91c17SVincenzo Maffione 		if (error) {
29252ff91c17SVincenzo Maffione 			rerror = error;
29262ff91c17SVincenzo Maffione 			goto out;
29272ff91c17SVincenzo Maffione 		}
29282ff91c17SVincenzo Maffione 
29292ff91c17SVincenzo Maffione 		/* copy the option body only if there was no error */
29302ff91c17SVincenzo Maffione 		if (!rerror && !src->nro_status) {
29312ff91c17SVincenzo Maffione 			optsz = nmreq_opt_size_by_type(src->nro_reqtype);
29322ff91c17SVincenzo Maffione 			if (optsz) {
29332ff91c17SVincenzo Maffione 				error = copyout(src + 1, dst + 1, optsz);
29342ff91c17SVincenzo Maffione 				if (error) {
29352ff91c17SVincenzo Maffione 					rerror = error;
29362ff91c17SVincenzo Maffione 					goto out;
29372ff91c17SVincenzo Maffione 				}
29382ff91c17SVincenzo Maffione 			}
29392ff91c17SVincenzo Maffione 		}
2940*cfa866f6SMatt Macy 		src = (struct nmreq_option *)(uintptr_t)next;
2941*cfa866f6SMatt Macy 		dst = (struct nmreq_option *)(uintptr_t)*ptrs;
29422ff91c17SVincenzo Maffione 	}
29432ff91c17SVincenzo Maffione 
29442ff91c17SVincenzo Maffione 
29452ff91c17SVincenzo Maffione out:
29462ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
29472ff91c17SVincenzo Maffione 	nm_os_free(bufstart);
29482ff91c17SVincenzo Maffione 	return rerror;
29492ff91c17SVincenzo Maffione }
29502ff91c17SVincenzo Maffione 
29512ff91c17SVincenzo Maffione struct nmreq_option *
29522ff91c17SVincenzo Maffione nmreq_findoption(struct nmreq_option *opt, uint16_t reqtype)
29532ff91c17SVincenzo Maffione {
2954*cfa866f6SMatt Macy 	for ( ; opt; opt = (struct nmreq_option *)(uintptr_t)opt->nro_next)
29552ff91c17SVincenzo Maffione 		if (opt->nro_reqtype == reqtype)
29562ff91c17SVincenzo Maffione 			return opt;
29572ff91c17SVincenzo Maffione 	return NULL;
29582ff91c17SVincenzo Maffione }
29592ff91c17SVincenzo Maffione 
29602ff91c17SVincenzo Maffione int
29612ff91c17SVincenzo Maffione nmreq_checkduplicate(struct nmreq_option *opt) {
29622ff91c17SVincenzo Maffione 	uint16_t type = opt->nro_reqtype;
29632ff91c17SVincenzo Maffione 	int dup = 0;
29642ff91c17SVincenzo Maffione 
2965*cfa866f6SMatt Macy 	while ((opt = nmreq_findoption((struct nmreq_option *)(uintptr_t)opt->nro_next,
29662ff91c17SVincenzo Maffione 			type))) {
29672ff91c17SVincenzo Maffione 		dup++;
29682ff91c17SVincenzo Maffione 		opt->nro_status = EINVAL;
29692ff91c17SVincenzo Maffione 	}
29702ff91c17SVincenzo Maffione 	return (dup ? EINVAL : 0);
29712ff91c17SVincenzo Maffione }
29722ff91c17SVincenzo Maffione 
29732ff91c17SVincenzo Maffione static int
29742ff91c17SVincenzo Maffione nmreq_checkoptions(struct nmreq_header *hdr)
29752ff91c17SVincenzo Maffione {
29762ff91c17SVincenzo Maffione 	struct nmreq_option *opt;
29772ff91c17SVincenzo Maffione 	/* return error if there is still any option
29782ff91c17SVincenzo Maffione 	 * marked as not supported
29792ff91c17SVincenzo Maffione 	 */
29802ff91c17SVincenzo Maffione 
2981*cfa866f6SMatt Macy 	for (opt = (struct nmreq_option *)(uintptr_t)hdr->nr_options; opt;
2982*cfa866f6SMatt Macy 	     opt = (struct nmreq_option *)(uintptr_t)opt->nro_next)
29832ff91c17SVincenzo Maffione 		if (opt->nro_status == EOPNOTSUPP)
29842ff91c17SVincenzo Maffione 			return EOPNOTSUPP;
29852ff91c17SVincenzo Maffione 
29862ff91c17SVincenzo Maffione 	return 0;
29872ff91c17SVincenzo Maffione }
298868b8534bSLuigi Rizzo 
298968b8534bSLuigi Rizzo /*
299068b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
299168b8534bSLuigi Rizzo  *
299268b8534bSLuigi Rizzo  * Can be called for one or more queues.
299368b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
299468b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
2995ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
299668b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
299768b8534bSLuigi Rizzo  * are done through callbacks.
2998f196ce38SLuigi Rizzo  *
299901c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
300001c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
300101c7d25fSLuigi Rizzo  * hidden argument.
300268b8534bSLuigi Rizzo  */
3003f9790aebSLuigi Rizzo int
300437e3a6d3SLuigi Rizzo netmap_poll(struct netmap_priv_d *priv, int events, NM_SELRECORD_T *sr)
300568b8534bSLuigi Rizzo {
300668b8534bSLuigi Rizzo 	struct netmap_adapter *na;
300768b8534bSLuigi Rizzo 	struct netmap_kring *kring;
300837e3a6d3SLuigi Rizzo 	struct netmap_ring *ring;
3009847bf383SLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want[NR_TXRX], revents = 0;
3010847bf383SLuigi Rizzo #define want_tx want[NR_TX]
3011847bf383SLuigi Rizzo #define want_rx want[NR_RX]
3012c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
301301c7d25fSLuigi Rizzo 
3014f9790aebSLuigi Rizzo 	/*
3015f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
3016f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
3017f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
3018f9790aebSLuigi Rizzo 	 */
301917885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
3020ce3ee1e7SLuigi Rizzo 
3021c3e9b4dbSLuiz Otavio O Souza 	/* Transparent mode: send_down is 1 if we have found some
3022c3e9b4dbSLuiz Otavio O Souza 	 * packets to forward (host RX ring --> NIC) during the rx
3023c3e9b4dbSLuiz Otavio O Souza 	 * scan and we have not sent them down to the NIC yet.
3024c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode requires to bind all rings to a single
3025c3e9b4dbSLuiz Otavio O Souza 	 * file descriptor.
3026f0ea3689SLuigi Rizzo 	 */
302737e3a6d3SLuigi Rizzo 	int send_down = 0;
3028c3e9b4dbSLuiz Otavio O Souza 	int sync_flags = priv->np_sync_flags;
302937e3a6d3SLuigi Rizzo 
303037e3a6d3SLuigi Rizzo 	mbq_init(&q);
303168b8534bSLuigi Rizzo 
30328241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
30338241616dSLuigi Rizzo 		D("No if registered");
30348241616dSLuigi Rizzo 		return POLLERR;
30358241616dSLuigi Rizzo 	}
3036847bf383SLuigi Rizzo 	mb(); /* make sure following reads are not from cache */
30378241616dSLuigi Rizzo 
3038f9790aebSLuigi Rizzo 	na = priv->np_na;
3039f9790aebSLuigi Rizzo 
30404bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
304168b8534bSLuigi Rizzo 		return POLLERR;
304268b8534bSLuigi Rizzo 
304368b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
30444bf50f18SLuigi Rizzo 		D("device %s events 0x%x", na->name, events);
304568b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
304668b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
304768b8534bSLuigi Rizzo 
304868b8534bSLuigi Rizzo 	/*
3049f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
3050f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
3051ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
3052ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
3053ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
3054ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
305568b8534bSLuigi Rizzo 	 *
305668b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
305768b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
305868b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
305968b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
306068b8534bSLuigi Rizzo 	 */
3061847bf383SLuigi Rizzo 	check_all_tx = nm_si_user(priv, NR_TX);
3062847bf383SLuigi Rizzo 	check_all_rx = nm_si_user(priv, NR_RX);
306364ae02c3SLuigi Rizzo 
30644f80b14cSVincenzo Maffione #ifdef __FreeBSD__
306568b8534bSLuigi Rizzo 	/*
3066f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
3067f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
30684f80b14cSVincenzo Maffione 	 * routines. We can't do this on Linux, as the contract says
30694f80b14cSVincenzo Maffione 	 * that we must call nm_os_selrecord() unconditionally.
307068b8534bSLuigi Rizzo 	 */
307137e3a6d3SLuigi Rizzo 	if (want_tx) {
30724f80b14cSVincenzo Maffione 		enum txrx t = NR_TX;
307337e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[t]; want[t] && i < priv->np_qlast[t]; i++) {
30742ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
307537e3a6d3SLuigi Rizzo 			/* XXX compare ring->cur and kring->tail */
307637e3a6d3SLuigi Rizzo 			if (!nm_ring_empty(kring->ring)) {
307737e3a6d3SLuigi Rizzo 				revents |= want[t];
307837e3a6d3SLuigi Rizzo 				want[t] = 0;	/* also breaks the loop */
307937e3a6d3SLuigi Rizzo 			}
308037e3a6d3SLuigi Rizzo 		}
308137e3a6d3SLuigi Rizzo 	}
308237e3a6d3SLuigi Rizzo 	if (want_rx) {
30834f80b14cSVincenzo Maffione 		enum txrx t = NR_RX;
308437e3a6d3SLuigi Rizzo 		want_rx = 0; /* look for a reason to run the handlers */
308537e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
30862ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
308737e3a6d3SLuigi Rizzo 			if (kring->ring->cur == kring->ring->tail /* try fetch new buffers */
308837e3a6d3SLuigi Rizzo 			    || kring->rhead != kring->ring->head /* release buffers */) {
308937e3a6d3SLuigi Rizzo 				want_rx = 1;
309037e3a6d3SLuigi Rizzo 			}
309137e3a6d3SLuigi Rizzo 		}
309237e3a6d3SLuigi Rizzo 		if (!want_rx)
309337e3a6d3SLuigi Rizzo 			revents |= events & (POLLIN | POLLRDNORM); /* we have data */
309437e3a6d3SLuigi Rizzo 	}
30954f80b14cSVincenzo Maffione #endif
30964f80b14cSVincenzo Maffione 
30974f80b14cSVincenzo Maffione #ifdef linux
30984f80b14cSVincenzo Maffione 	/* The selrecord must be unconditional on linux. */
30994f80b14cSVincenzo Maffione 	nm_os_selrecord(sr, check_all_tx ?
31002ff91c17SVincenzo Maffione 	    &na->si[NR_TX] : &na->tx_rings[priv->np_qfirst[NR_TX]]->si);
31014f80b14cSVincenzo Maffione 	nm_os_selrecord(sr, check_all_rx ?
31022ff91c17SVincenzo Maffione 		&na->si[NR_RX] : &na->rx_rings[priv->np_qfirst[NR_RX]]->si);
31034f80b14cSVincenzo Maffione #endif /* linux */
310468b8534bSLuigi Rizzo 
310568b8534bSLuigi Rizzo 	/*
310617885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
310717885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
310817885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
3109f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
311068b8534bSLuigi Rizzo 	 */
311168b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
311217885a7bSLuigi Rizzo 		/*
311317885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
311417885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
311517885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
311617885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
3117ce3ee1e7SLuigi Rizzo 		 */
3118091fd0abSLuigi Rizzo flush_tx:
311937e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[NR_TX]; i < priv->np_qlast[NR_TX]; i++) {
312017885a7bSLuigi Rizzo 			int found = 0;
312117885a7bSLuigi Rizzo 
31222ff91c17SVincenzo Maffione 			kring = na->tx_rings[i];
312337e3a6d3SLuigi Rizzo 			ring = kring->ring;
312437e3a6d3SLuigi Rizzo 
31254f80b14cSVincenzo Maffione 			/*
31264f80b14cSVincenzo Maffione 			 * Don't try to txsync this TX ring if we already found some
31274f80b14cSVincenzo Maffione 			 * space in some of the TX rings (want_tx == 0) and there are no
31284f80b14cSVincenzo Maffione 			 * TX slots in this ring that need to be flushed to the NIC
31292ff91c17SVincenzo Maffione 			 * (head == hwcur).
31304f80b14cSVincenzo Maffione 			 */
31312ff91c17SVincenzo Maffione 			if (!send_down && !want_tx && ring->head == kring->nr_hwcur)
313268b8534bSLuigi Rizzo 				continue;
313337e3a6d3SLuigi Rizzo 
313437e3a6d3SLuigi Rizzo 			if (nm_kr_tryget(kring, 1, &revents))
313517885a7bSLuigi Rizzo 				continue;
313637e3a6d3SLuigi Rizzo 
313737e3a6d3SLuigi Rizzo 			if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
313817885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
313917885a7bSLuigi Rizzo 				revents |= POLLERR;
314017885a7bSLuigi Rizzo 			} else {
3141c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags))
314268b8534bSLuigi Rizzo 					revents |= POLLERR;
3143847bf383SLuigi Rizzo 				else
314437e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
314517885a7bSLuigi Rizzo 			}
314668b8534bSLuigi Rizzo 
314717885a7bSLuigi Rizzo 			/*
314817885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
314917885a7bSLuigi Rizzo 			 * listeners on the same ring.
315017885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
315117885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
3152f9790aebSLuigi Rizzo 			 */
315317885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
315417885a7bSLuigi Rizzo 			nm_kr_put(kring);
315517885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
315668b8534bSLuigi Rizzo 				revents |= want_tx;
315768b8534bSLuigi Rizzo 				want_tx = 0;
31584f80b14cSVincenzo Maffione #ifndef linux
3159847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
31604f80b14cSVincenzo Maffione #endif /* linux */
316168b8534bSLuigi Rizzo 			}
3162ce3ee1e7SLuigi Rizzo 		}
316337e3a6d3SLuigi Rizzo 		/* if there were any packet to forward we must have handled them by now */
316437e3a6d3SLuigi Rizzo 		send_down = 0;
316537e3a6d3SLuigi Rizzo 		if (want_tx && retry_tx && sr) {
31664f80b14cSVincenzo Maffione #ifndef linux
316737e3a6d3SLuigi Rizzo 			nm_os_selrecord(sr, check_all_tx ?
31682ff91c17SVincenzo Maffione 			    &na->si[NR_TX] : &na->tx_rings[priv->np_qfirst[NR_TX]]->si);
31694f80b14cSVincenzo Maffione #endif /* !linux */
3170ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
3171ce3ee1e7SLuigi Rizzo 			goto flush_tx;
317268b8534bSLuigi Rizzo 		}
317368b8534bSLuigi Rizzo 	}
317468b8534bSLuigi Rizzo 
317568b8534bSLuigi Rizzo 	/*
317617885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
317768b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
317868b8534bSLuigi Rizzo 	 */
317968b8534bSLuigi Rizzo 	if (want_rx) {
318089cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
3181ce3ee1e7SLuigi Rizzo do_retry_rx:
3182847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[NR_RX]; i < priv->np_qlast[NR_RX]; i++) {
318317885a7bSLuigi Rizzo 			int found = 0;
318417885a7bSLuigi Rizzo 
31852ff91c17SVincenzo Maffione 			kring = na->rx_rings[i];
318637e3a6d3SLuigi Rizzo 			ring = kring->ring;
3187ce3ee1e7SLuigi Rizzo 
318837e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &revents)))
318917885a7bSLuigi Rizzo 				continue;
3190ce3ee1e7SLuigi Rizzo 
319137e3a6d3SLuigi Rizzo 			if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
3192847bf383SLuigi Rizzo 				netmap_ring_reinit(kring);
3193847bf383SLuigi Rizzo 				revents |= POLLERR;
3194847bf383SLuigi Rizzo 			}
3195847bf383SLuigi Rizzo 			/* now we can use kring->rcur, rtail */
3196847bf383SLuigi Rizzo 
319717885a7bSLuigi Rizzo 			/*
3198c3e9b4dbSLuiz Otavio O Souza 			 * transparent mode support: collect packets from
3199c3e9b4dbSLuiz Otavio O Souza 			 * hw rxring(s) that have been released by the user
3200ce3ee1e7SLuigi Rizzo 			 */
320137e3a6d3SLuigi Rizzo 			if (nm_may_forward_up(kring)) {
3202091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
3203091fd0abSLuigi Rizzo 			}
320468b8534bSLuigi Rizzo 
3205c3e9b4dbSLuiz Otavio O Souza 			/* Clear the NR_FORWARD flag anyway, it may be set by
3206c3e9b4dbSLuiz Otavio O Souza 			 * the nm_sync() below only on for the host RX ring (see
3207c3e9b4dbSLuiz Otavio O Souza 			 * netmap_rxsync_from_host()). */
320837e3a6d3SLuigi Rizzo 			kring->nr_kflags &= ~NR_FORWARD;
3209c3e9b4dbSLuiz Otavio O Souza 			if (kring->nm_sync(kring, sync_flags))
321068b8534bSLuigi Rizzo 				revents |= POLLERR;
3211847bf383SLuigi Rizzo 			else
321237e3a6d3SLuigi Rizzo 				nm_sync_finalize(kring);
3213c3e9b4dbSLuiz Otavio O Souza 			send_down |= (kring->nr_kflags & NR_FORWARD);
3214c3e9b4dbSLuiz Otavio O Souza 			ring_timestamp_set(ring);
321517885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
321617885a7bSLuigi Rizzo 			nm_kr_put(kring);
321717885a7bSLuigi Rizzo 			if (found) {
321868b8534bSLuigi Rizzo 				revents |= want_rx;
3219ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
32204f80b14cSVincenzo Maffione #ifndef linux
3221847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
32224f80b14cSVincenzo Maffione #endif /* linux */
322368b8534bSLuigi Rizzo 			}
322468b8534bSLuigi Rizzo 		}
322517885a7bSLuigi Rizzo 
32264f80b14cSVincenzo Maffione #ifndef linux
322737e3a6d3SLuigi Rizzo 		if (retry_rx && sr) {
322837e3a6d3SLuigi Rizzo 			nm_os_selrecord(sr, check_all_rx ?
32292ff91c17SVincenzo Maffione 			    &na->si[NR_RX] : &na->rx_rings[priv->np_qfirst[NR_RX]]->si);
323037e3a6d3SLuigi Rizzo 		}
32314f80b14cSVincenzo Maffione #endif /* !linux */
3232c3e9b4dbSLuiz Otavio O Souza 		if (send_down || retry_rx) {
323317885a7bSLuigi Rizzo 			retry_rx = 0;
323417885a7bSLuigi Rizzo 			if (send_down)
323517885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
323617885a7bSLuigi Rizzo 			else
3237ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
3238ce3ee1e7SLuigi Rizzo 		}
323968b8534bSLuigi Rizzo 	}
3240091fd0abSLuigi Rizzo 
324117885a7bSLuigi Rizzo 	/*
3242c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode: released bufs (i.e. between kring->nr_hwcur and
3243c3e9b4dbSLuiz Otavio O Souza 	 * ring->head) marked with NS_FORWARD on hw rx rings are passed up
3244c3e9b4dbSLuiz Otavio O Souza 	 * to the host stack.
3245ce3ee1e7SLuigi Rizzo 	 */
3246091fd0abSLuigi Rizzo 
3247c3e9b4dbSLuiz Otavio O Souza 	if (mbq_peek(&q)) {
3248f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
324937e3a6d3SLuigi Rizzo 	}
325068b8534bSLuigi Rizzo 
325168b8534bSLuigi Rizzo 	return (revents);
3252847bf383SLuigi Rizzo #undef want_tx
3253847bf383SLuigi Rizzo #undef want_rx
325468b8534bSLuigi Rizzo }
325568b8534bSLuigi Rizzo 
32564f80b14cSVincenzo Maffione int
32574f80b14cSVincenzo Maffione nma_intr_enable(struct netmap_adapter *na, int onoff)
32584f80b14cSVincenzo Maffione {
32594f80b14cSVincenzo Maffione 	bool changed = false;
32604f80b14cSVincenzo Maffione 	enum txrx t;
32614f80b14cSVincenzo Maffione 	int i;
32624f80b14cSVincenzo Maffione 
32634f80b14cSVincenzo Maffione 	for_rx_tx(t) {
32644f80b14cSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t); i++) {
32652ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
32664f80b14cSVincenzo Maffione 			int on = !(kring->nr_kflags & NKR_NOINTR);
32674f80b14cSVincenzo Maffione 
32684f80b14cSVincenzo Maffione 			if (!!onoff != !!on) {
32694f80b14cSVincenzo Maffione 				changed = true;
32704f80b14cSVincenzo Maffione 			}
32714f80b14cSVincenzo Maffione 			if (onoff) {
32724f80b14cSVincenzo Maffione 				kring->nr_kflags &= ~NKR_NOINTR;
32734f80b14cSVincenzo Maffione 			} else {
32744f80b14cSVincenzo Maffione 				kring->nr_kflags |= NKR_NOINTR;
32754f80b14cSVincenzo Maffione 			}
32764f80b14cSVincenzo Maffione 		}
32774f80b14cSVincenzo Maffione 	}
32784f80b14cSVincenzo Maffione 
32794f80b14cSVincenzo Maffione 	if (!changed) {
32804f80b14cSVincenzo Maffione 		return 0; /* nothing to do */
32814f80b14cSVincenzo Maffione 	}
32824f80b14cSVincenzo Maffione 
32834f80b14cSVincenzo Maffione 	if (!na->nm_intr) {
32844f80b14cSVincenzo Maffione 		D("Cannot %s interrupts for %s", onoff ? "enable" : "disable",
32854f80b14cSVincenzo Maffione 		  na->name);
32864f80b14cSVincenzo Maffione 		return -1;
32874f80b14cSVincenzo Maffione 	}
32884f80b14cSVincenzo Maffione 
32894f80b14cSVincenzo Maffione 	na->nm_intr(na, onoff);
32904f80b14cSVincenzo Maffione 
32914f80b14cSVincenzo Maffione 	return 0;
32924f80b14cSVincenzo Maffione }
32934f80b14cSVincenzo Maffione 
329417885a7bSLuigi Rizzo 
329517885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
329668b8534bSLuigi Rizzo 
329789cc2556SLuigi Rizzo /* default notify callback */
3298f9790aebSLuigi Rizzo static int
3299847bf383SLuigi Rizzo netmap_notify(struct netmap_kring *kring, int flags)
3300f9790aebSLuigi Rizzo {
33012ff91c17SVincenzo Maffione 	struct netmap_adapter *na = kring->notify_na;
3302847bf383SLuigi Rizzo 	enum txrx t = kring->tx;
3303f9790aebSLuigi Rizzo 
330437e3a6d3SLuigi Rizzo 	nm_os_selwakeup(&kring->si);
330589cc2556SLuigi Rizzo 	/* optimization: avoid a wake up on the global
330689cc2556SLuigi Rizzo 	 * queue if nobody has registered for more
330789cc2556SLuigi Rizzo 	 * than one ring
330889cc2556SLuigi Rizzo 	 */
3309847bf383SLuigi Rizzo 	if (na->si_users[t] > 0)
331037e3a6d3SLuigi Rizzo 		nm_os_selwakeup(&na->si[t]);
3311847bf383SLuigi Rizzo 
331237e3a6d3SLuigi Rizzo 	return NM_IRQ_COMPLETED;
3313f9790aebSLuigi Rizzo }
3314f9790aebSLuigi Rizzo 
331589cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
331637e3a6d3SLuigi Rizzo  * provide some defaults and get a reference to the
331737e3a6d3SLuigi Rizzo  * memory allocator
331889cc2556SLuigi Rizzo  */
3319f9790aebSLuigi Rizzo int
3320f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
3321f9790aebSLuigi Rizzo {
3322f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
3323f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
33244bf50f18SLuigi Rizzo 			na->name, na->num_tx_rings, na->num_rx_rings);
3325f9790aebSLuigi Rizzo 		return EINVAL;
3326f9790aebSLuigi Rizzo 	}
332717885a7bSLuigi Rizzo 
33282ff91c17SVincenzo Maffione 	if (!na->rx_buf_maxsize) {
33292ff91c17SVincenzo Maffione 		/* Set a conservative default (larger is safer). */
33302ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = PAGE_SIZE;
33312ff91c17SVincenzo Maffione 	}
33322ff91c17SVincenzo Maffione 
333317885a7bSLuigi Rizzo #ifdef __FreeBSD__
333437e3a6d3SLuigi Rizzo 	if (na->na_flags & NAF_HOST_RINGS && na->ifp) {
333537e3a6d3SLuigi Rizzo 		na->if_input = na->ifp->if_input; /* for netmap_send_up */
33364bf50f18SLuigi Rizzo 	}
33374f80b14cSVincenzo Maffione 	na->pdev = na; /* make sure netmap_mem_map() is called */
333837e3a6d3SLuigi Rizzo #endif /* __FreeBSD__ */
3339f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
334089cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
334189cc2556SLuigi Rizzo 		 * since other port types all provide their own
334289cc2556SLuigi Rizzo 		 * nm_krings_create
334389cc2556SLuigi Rizzo 		 */
3344f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
334517885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
3346f9790aebSLuigi Rizzo 	}
3347f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
3348f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
3349f9790aebSLuigi Rizzo 	na->active_fds = 0;
3350f9790aebSLuigi Rizzo 
3351c3e9b4dbSLuiz Otavio O Souza 	if (na->nm_mem == NULL) {
33524bf50f18SLuigi Rizzo 		/* use the global allocator */
3353c3e9b4dbSLuiz Otavio O Souza 		na->nm_mem = netmap_mem_get(&nm_mem);
3354c3e9b4dbSLuiz Otavio O Souza 	}
3355847bf383SLuigi Rizzo #ifdef WITH_VALE
33564bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
33574bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
33584bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
33594bf50f18SLuigi Rizzo 		 */
33604bf50f18SLuigi Rizzo 		na->nm_bdg_attach = netmap_bwrap_attach;
3361847bf383SLuigi Rizzo #endif
336237e3a6d3SLuigi Rizzo 
3363f9790aebSLuigi Rizzo 	return 0;
3364f9790aebSLuigi Rizzo }
3365f9790aebSLuigi Rizzo 
336637e3a6d3SLuigi Rizzo /* Wrapper for the register callback provided netmap-enabled
336737e3a6d3SLuigi Rizzo  * hardware drivers.
336837e3a6d3SLuigi Rizzo  * nm_iszombie(na) means that the driver module has been
33694bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
337037e3a6d3SLuigi Rizzo  * nm_os_ifnet_lock() must guarantee mutual exclusion with
337137e3a6d3SLuigi Rizzo  * module unloading.
33724bf50f18SLuigi Rizzo  */
33734bf50f18SLuigi Rizzo static int
337437e3a6d3SLuigi Rizzo netmap_hw_reg(struct netmap_adapter *na, int onoff)
33754bf50f18SLuigi Rizzo {
33764bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
33774bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
337837e3a6d3SLuigi Rizzo 	int error = 0;
33794bf50f18SLuigi Rizzo 
338037e3a6d3SLuigi Rizzo 	nm_os_ifnet_lock();
33814bf50f18SLuigi Rizzo 
338237e3a6d3SLuigi Rizzo 	if (nm_iszombie(na)) {
338337e3a6d3SLuigi Rizzo 		if (onoff) {
338437e3a6d3SLuigi Rizzo 			error = ENXIO;
338537e3a6d3SLuigi Rizzo 		} else if (na != NULL) {
338637e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_NETMAP_ON;
338737e3a6d3SLuigi Rizzo 		}
338837e3a6d3SLuigi Rizzo 		goto out;
338937e3a6d3SLuigi Rizzo 	}
339037e3a6d3SLuigi Rizzo 
339137e3a6d3SLuigi Rizzo 	error = hwna->nm_hw_register(na, onoff);
339237e3a6d3SLuigi Rizzo 
339337e3a6d3SLuigi Rizzo out:
339437e3a6d3SLuigi Rizzo 	nm_os_ifnet_unlock();
339537e3a6d3SLuigi Rizzo 
339637e3a6d3SLuigi Rizzo 	return error;
339737e3a6d3SLuigi Rizzo }
339837e3a6d3SLuigi Rizzo 
339937e3a6d3SLuigi Rizzo static void
340037e3a6d3SLuigi Rizzo netmap_hw_dtor(struct netmap_adapter *na)
340137e3a6d3SLuigi Rizzo {
340237e3a6d3SLuigi Rizzo 	if (nm_iszombie(na) || na->ifp == NULL)
340337e3a6d3SLuigi Rizzo 		return;
340437e3a6d3SLuigi Rizzo 
340537e3a6d3SLuigi Rizzo 	WNA(na->ifp) = NULL;
34064bf50f18SLuigi Rizzo }
34074bf50f18SLuigi Rizzo 
3408f18be576SLuigi Rizzo 
340968b8534bSLuigi Rizzo /*
3410c3e9b4dbSLuiz Otavio O Souza  * Allocate a netmap_adapter object, and initialize it from the
341137e3a6d3SLuigi Rizzo  * 'arg' passed by the driver on attach.
3412c3e9b4dbSLuiz Otavio O Souza  * We allocate a block of memory of 'size' bytes, which has room
3413c3e9b4dbSLuiz Otavio O Souza  * for struct netmap_adapter plus additional room private to
3414c3e9b4dbSLuiz Otavio O Souza  * the caller.
341568b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
341668b8534bSLuigi Rizzo  */
3417c3e9b4dbSLuiz Otavio O Souza int
34184f80b14cSVincenzo Maffione netmap_attach_ext(struct netmap_adapter *arg, size_t size, int override_reg)
341968b8534bSLuigi Rizzo {
3420f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
342137e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
342268b8534bSLuigi Rizzo 
3423c3e9b4dbSLuiz Otavio O Souza 	if (size < sizeof(struct netmap_hw_adapter)) {
3424c3e9b4dbSLuiz Otavio O Souza 		D("Invalid netmap adapter size %d", (int)size);
3425c3e9b4dbSLuiz Otavio O Souza 		return EINVAL;
3426c3e9b4dbSLuiz Otavio O Souza 	}
3427c3e9b4dbSLuiz Otavio O Souza 
342837e3a6d3SLuigi Rizzo 	if (arg == NULL || arg->ifp == NULL)
3429ae10d1afSLuigi Rizzo 		goto fail;
34304f80b14cSVincenzo Maffione 
343137e3a6d3SLuigi Rizzo 	ifp = arg->ifp;
34324f80b14cSVincenzo Maffione 	if (NA(ifp) && !NM_NA_VALID(ifp)) {
34334f80b14cSVincenzo Maffione 		/* If NA(ifp) is not null but there is no valid netmap
34344f80b14cSVincenzo Maffione 		 * adapter it means that someone else is using the same
34354f80b14cSVincenzo Maffione 		 * pointer (e.g. ax25_ptr on linux). This happens for
34364f80b14cSVincenzo Maffione 		 * instance when also PF_RING is in use. */
34374f80b14cSVincenzo Maffione 		D("Error: netmap adapter hook is busy");
34384f80b14cSVincenzo Maffione 		return EBUSY;
34394f80b14cSVincenzo Maffione 	}
34404f80b14cSVincenzo Maffione 
3441c3e9b4dbSLuiz Otavio O Souza 	hwna = nm_os_malloc(size);
3442f9790aebSLuigi Rizzo 	if (hwna == NULL)
3443ae10d1afSLuigi Rizzo 		goto fail;
3444f9790aebSLuigi Rizzo 	hwna->up = *arg;
3445847bf383SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS | NAF_NATIVE;
34464bf50f18SLuigi Rizzo 	strncpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
34474f80b14cSVincenzo Maffione 	if (override_reg) {
34484bf50f18SLuigi Rizzo 		hwna->nm_hw_register = hwna->up.nm_register;
344937e3a6d3SLuigi Rizzo 		hwna->up.nm_register = netmap_hw_reg;
34504f80b14cSVincenzo Maffione 	}
3451f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
3452c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(hwna);
3453f9790aebSLuigi Rizzo 		goto fail;
3454f9790aebSLuigi Rizzo 	}
3455f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
3456f9790aebSLuigi Rizzo 
345737e3a6d3SLuigi Rizzo 	NM_ATTACH_NA(ifp, &hwna->up);
345837e3a6d3SLuigi Rizzo 
345964ae02c3SLuigi Rizzo #ifdef linux
3460f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
3461f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
3462847bf383SLuigi Rizzo #ifndef NETMAP_LINUX_HAVE_NETDEV_OPS
3463f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
3464f18be576SLuigi Rizzo #else
3465f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
346637e3a6d3SLuigi Rizzo #endif /* NETMAP_LINUX_HAVE_NETDEV_OPS */
3467f18be576SLuigi Rizzo 	}
3468f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
34694f80b14cSVincenzo Maffione 	hwna->nm_ndo.ndo_change_mtu = linux_netmap_change_mtu;
34704bf50f18SLuigi Rizzo 	if (ifp->ethtool_ops) {
34714bf50f18SLuigi Rizzo 		hwna->nm_eto = *ifp->ethtool_ops;
34724bf50f18SLuigi Rizzo 	}
34734bf50f18SLuigi Rizzo 	hwna->nm_eto.set_ringparam = linux_netmap_set_ringparam;
3474847bf383SLuigi Rizzo #ifdef NETMAP_LINUX_HAVE_SET_CHANNELS
34754bf50f18SLuigi Rizzo 	hwna->nm_eto.set_channels = linux_netmap_set_channels;
347637e3a6d3SLuigi Rizzo #endif /* NETMAP_LINUX_HAVE_SET_CHANNELS */
34774bf50f18SLuigi Rizzo 	if (arg->nm_config == NULL) {
34784bf50f18SLuigi Rizzo 		hwna->up.nm_config = netmap_linux_config;
34794bf50f18SLuigi Rizzo 	}
3480ce3ee1e7SLuigi Rizzo #endif /* linux */
348137e3a6d3SLuigi Rizzo 	if (arg->nm_dtor == NULL) {
348237e3a6d3SLuigi Rizzo 		hwna->up.nm_dtor = netmap_hw_dtor;
348337e3a6d3SLuigi Rizzo 	}
3484f9790aebSLuigi Rizzo 
3485d82f9014SRui Paulo 	if_printf(ifp, "netmap queues/slots: TX %d/%d, RX %d/%d\n",
3486d82f9014SRui Paulo 	    hwna->up.num_tx_rings, hwna->up.num_tx_desc,
3487d82f9014SRui Paulo 	    hwna->up.num_rx_rings, hwna->up.num_rx_desc);
3488ae10d1afSLuigi Rizzo 	return 0;
348968b8534bSLuigi Rizzo 
3490ae10d1afSLuigi Rizzo fail:
3491f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
3492f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
349368b8534bSLuigi Rizzo }
349468b8534bSLuigi Rizzo 
349568b8534bSLuigi Rizzo 
349637e3a6d3SLuigi Rizzo int
349737e3a6d3SLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
349837e3a6d3SLuigi Rizzo {
34994f80b14cSVincenzo Maffione 	return netmap_attach_ext(arg, sizeof(struct netmap_hw_adapter),
35004f80b14cSVincenzo Maffione 			1 /* override nm_reg */);
350137e3a6d3SLuigi Rizzo }
350237e3a6d3SLuigi Rizzo 
350337e3a6d3SLuigi Rizzo 
3504f9790aebSLuigi Rizzo void
3505f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
3506f9790aebSLuigi Rizzo {
3507f9790aebSLuigi Rizzo 	if (!na) {
3508f9790aebSLuigi Rizzo 		return;
3509f9790aebSLuigi Rizzo 	}
3510f9790aebSLuigi Rizzo 
3511f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
3512f9790aebSLuigi Rizzo }
3513f9790aebSLuigi Rizzo 
3514f9790aebSLuigi Rizzo 
3515f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
3516f9790aebSLuigi Rizzo int
3517f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
3518f9790aebSLuigi Rizzo {
3519f9790aebSLuigi Rizzo 	if (!na)
3520f9790aebSLuigi Rizzo 		return 1;
3521f9790aebSLuigi Rizzo 
3522f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
3523f9790aebSLuigi Rizzo 		return 0;
3524f9790aebSLuigi Rizzo 
3525f9790aebSLuigi Rizzo 	if (na->nm_dtor)
3526f9790aebSLuigi Rizzo 		na->nm_dtor(na);
3527f9790aebSLuigi Rizzo 
35284f80b14cSVincenzo Maffione 	if (na->tx_rings) { /* XXX should not happen */
35294f80b14cSVincenzo Maffione 		D("freeing leftover tx_rings");
35304f80b14cSVincenzo Maffione 		na->nm_krings_delete(na);
35314f80b14cSVincenzo Maffione 	}
35324f80b14cSVincenzo Maffione 	netmap_pipe_dealloc(na);
35334f80b14cSVincenzo Maffione 	if (na->nm_mem)
35344f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
35354f80b14cSVincenzo Maffione 	bzero(na, sizeof(*na));
35364f80b14cSVincenzo Maffione 	nm_os_free(na);
3537f9790aebSLuigi Rizzo 
3538f9790aebSLuigi Rizzo 	return 1;
3539f9790aebSLuigi Rizzo }
3540f9790aebSLuigi Rizzo 
354189cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
3542f9790aebSLuigi Rizzo int
3543f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
3544f9790aebSLuigi Rizzo {
3545f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
354617885a7bSLuigi Rizzo 	if (ret == 0) {
354717885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
35482ff91c17SVincenzo Maffione 		mbq_safe_init(&na->rx_rings[na->num_rx_rings]->rx_queue);
354917885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
355017885a7bSLuigi Rizzo 	}
355117885a7bSLuigi Rizzo 	return ret;
3552f9790aebSLuigi Rizzo }
3553f9790aebSLuigi Rizzo 
3554f9790aebSLuigi Rizzo 
3555f9790aebSLuigi Rizzo 
355668b8534bSLuigi Rizzo /*
355789cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
355868b8534bSLuigi Rizzo  */
355968b8534bSLuigi Rizzo void
356068b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
356168b8534bSLuigi Rizzo {
356268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
356368b8534bSLuigi Rizzo 
356468b8534bSLuigi Rizzo 	if (!na)
356568b8534bSLuigi Rizzo 		return;
356668b8534bSLuigi Rizzo 
3567f9790aebSLuigi Rizzo 	NMG_LOCK();
356837e3a6d3SLuigi Rizzo 	netmap_set_all_rings(na, NM_KR_LOCKED);
3569847bf383SLuigi Rizzo 	/*
3570847bf383SLuigi Rizzo 	 * if the netmap adapter is not native, somebody
3571847bf383SLuigi Rizzo 	 * changed it, so we can not release it here.
357237e3a6d3SLuigi Rizzo 	 * The NAF_ZOMBIE flag will notify the new owner that
3573847bf383SLuigi Rizzo 	 * the driver is gone.
3574847bf383SLuigi Rizzo 	 */
35754f80b14cSVincenzo Maffione 	if (!(na->na_flags & NAF_NATIVE) || !netmap_adapter_put(na)) {
35764f80b14cSVincenzo Maffione 		na->na_flags |= NAF_ZOMBIE;
3577847bf383SLuigi Rizzo 	}
357837e3a6d3SLuigi Rizzo 	/* give active users a chance to notice that NAF_ZOMBIE has been
357937e3a6d3SLuigi Rizzo 	 * turned on, so that they can stop and return an error to userspace.
358037e3a6d3SLuigi Rizzo 	 * Note that this becomes a NOP if there are no active users and,
358137e3a6d3SLuigi Rizzo 	 * therefore, the put() above has deleted the na, since now NA(ifp) is
358237e3a6d3SLuigi Rizzo 	 * NULL.
358337e3a6d3SLuigi Rizzo 	 */
3584f9790aebSLuigi Rizzo 	netmap_enable_all_rings(ifp);
3585f9790aebSLuigi Rizzo 	NMG_UNLOCK();
3586ae10d1afSLuigi Rizzo }
3587f18be576SLuigi Rizzo 
3588f18be576SLuigi Rizzo 
358968b8534bSLuigi Rizzo /*
359002ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
359102ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
359217885a7bSLuigi Rizzo  *
359317885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
359417885a7bSLuigi Rizzo  * in the relevant rxsync routine.
359517885a7bSLuigi Rizzo  *
3596ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
3597ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
3598ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
3599f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
360068b8534bSLuigi Rizzo  */
360168b8534bSLuigi Rizzo int
3602ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
360368b8534bSLuigi Rizzo {
360468b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
360537e3a6d3SLuigi Rizzo 	struct netmap_kring *kring, *tx_kring;
360617885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
360717885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
360837e3a6d3SLuigi Rizzo 	unsigned int txr;
360917885a7bSLuigi Rizzo 	struct mbq *q;
3610c3e9b4dbSLuiz Otavio O Souza 	int busy;
361168b8534bSLuigi Rizzo 
36122ff91c17SVincenzo Maffione 	kring = na->rx_rings[na->num_rx_rings];
3613ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
3614ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
3615ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
361617885a7bSLuigi Rizzo 
36174bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
36184bf50f18SLuigi Rizzo 		D("%s not in netmap mode anymore", na->name);
3619ce3ee1e7SLuigi Rizzo 		error = ENXIO;
3620ce3ee1e7SLuigi Rizzo 		goto done;
3621ce3ee1e7SLuigi Rizzo 	}
3622ce3ee1e7SLuigi Rizzo 
362337e3a6d3SLuigi Rizzo 	txr = MBUF_TXQ(m);
362437e3a6d3SLuigi Rizzo 	if (txr >= na->num_tx_rings) {
362537e3a6d3SLuigi Rizzo 		txr %= na->num_tx_rings;
362637e3a6d3SLuigi Rizzo 	}
36272ff91c17SVincenzo Maffione 	tx_kring = NMR(na, NR_TX)[txr];
362837e3a6d3SLuigi Rizzo 
362937e3a6d3SLuigi Rizzo 	if (tx_kring->nr_mode == NKR_NETMAP_OFF) {
363037e3a6d3SLuigi Rizzo 		return MBUF_TRANSMIT(na, ifp, m);
363137e3a6d3SLuigi Rizzo 	}
363237e3a6d3SLuigi Rizzo 
363317885a7bSLuigi Rizzo 	q = &kring->rx_queue;
363417885a7bSLuigi Rizzo 
3635ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
36364bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
36374bf50f18SLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", na->name,
36384bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
3639ce3ee1e7SLuigi Rizzo 		goto done;
3640849bec0eSLuigi Rizzo 	}
364117885a7bSLuigi Rizzo 
364237e3a6d3SLuigi Rizzo 	if (nm_os_mbuf_has_offld(m)) {
3643c3e9b4dbSLuiz Otavio O Souza 		RD(1, "%s drop mbuf that needs offloadings", na->name);
364437e3a6d3SLuigi Rizzo 		goto done;
364537e3a6d3SLuigi Rizzo 	}
364637e3a6d3SLuigi Rizzo 
3647c3e9b4dbSLuiz Otavio O Souza 	/* protect against netmap_rxsync_from_host(), netmap_sw_to_nic()
364817885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
364917885a7bSLuigi Rizzo 	 * not possible on Linux).
3650c3e9b4dbSLuiz Otavio O Souza 	 * We enqueue the mbuf only if we are sure there is going to be
3651c3e9b4dbSLuiz Otavio O Souza 	 * enough room in the host RX ring, otherwise we drop it.
3652ce3ee1e7SLuigi Rizzo 	 */
3653997b054cSLuigi Rizzo 	mbq_lock(q);
365417885a7bSLuigi Rizzo 
3655c3e9b4dbSLuiz Otavio O Souza 	busy = kring->nr_hwtail - kring->nr_hwcur;
3656c3e9b4dbSLuiz Otavio O Souza 	if (busy < 0)
3657c3e9b4dbSLuiz Otavio O Souza 		busy += kring->nkr_num_slots;
3658c3e9b4dbSLuiz Otavio O Souza 	if (busy + mbq_len(q) >= kring->nkr_num_slots - 1) {
3659c3e9b4dbSLuiz Otavio O Souza 		RD(2, "%s full hwcur %d hwtail %d qlen %d", na->name,
3660c3e9b4dbSLuiz Otavio O Souza 			kring->nr_hwcur, kring->nr_hwtail, mbq_len(q));
3661ce3ee1e7SLuigi Rizzo 	} else {
366217885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
3663c3e9b4dbSLuiz Otavio O Souza 		ND(2, "%s %d bufs in queue", na->name, mbq_len(q));
366417885a7bSLuigi Rizzo 		/* notify outside the lock */
366517885a7bSLuigi Rizzo 		m = NULL;
366668b8534bSLuigi Rizzo 		error = 0;
3667ce3ee1e7SLuigi Rizzo 	}
3668997b054cSLuigi Rizzo 	mbq_unlock(q);
3669ce3ee1e7SLuigi Rizzo 
367068b8534bSLuigi Rizzo done:
367117885a7bSLuigi Rizzo 	if (m)
367268b8534bSLuigi Rizzo 		m_freem(m);
367317885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
3674847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
367589cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
367689cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
367789cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
367889cc2556SLuigi Rizzo 	 */
367968b8534bSLuigi Rizzo 
368068b8534bSLuigi Rizzo 	return (error);
368168b8534bSLuigi Rizzo }
368268b8534bSLuigi Rizzo 
368368b8534bSLuigi Rizzo 
368468b8534bSLuigi Rizzo /*
368568b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
368668b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
3687f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
368837e3a6d3SLuigi Rizzo  * If native netmap mode is set, in particular, we have to set nr_mode to
368937e3a6d3SLuigi Rizzo  * NKR_NETMAP_ON.
369068b8534bSLuigi Rizzo  */
369168b8534bSLuigi Rizzo struct netmap_slot *
3692ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
369368b8534bSLuigi Rizzo 	u_int new_cur)
369468b8534bSLuigi Rizzo {
369568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
3696506cc70cSLuigi Rizzo 	int new_hwofs, lim;
369768b8534bSLuigi Rizzo 
36984bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
36994bf50f18SLuigi Rizzo 		ND("interface not in native netmap mode");
370068b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
3701ce3ee1e7SLuigi Rizzo 	}
370268b8534bSLuigi Rizzo 
3703ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
3704ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
3705ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
3706ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
3707ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
3708ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
3709ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
3710ce3ee1e7SLuigi Rizzo 	 */
371164ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
37128241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
37138241616dSLuigi Rizzo 			return NULL;
371437e3a6d3SLuigi Rizzo 
37152ff91c17SVincenzo Maffione 		kring = na->tx_rings[n];
371637e3a6d3SLuigi Rizzo 
371737e3a6d3SLuigi Rizzo 		if (kring->nr_pending_mode == NKR_NETMAP_OFF) {
371837e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
371937e3a6d3SLuigi Rizzo 			return NULL;
372037e3a6d3SLuigi Rizzo 		}
372137e3a6d3SLuigi Rizzo 
372217885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
3723506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
372464ae02c3SLuigi Rizzo 	} else {
37258241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
37268241616dSLuigi Rizzo 			return NULL;
37272ff91c17SVincenzo Maffione 		kring = na->rx_rings[n];
372837e3a6d3SLuigi Rizzo 
372937e3a6d3SLuigi Rizzo 		if (kring->nr_pending_mode == NKR_NETMAP_OFF) {
373037e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
373137e3a6d3SLuigi Rizzo 			return NULL;
373237e3a6d3SLuigi Rizzo 		}
373337e3a6d3SLuigi Rizzo 
373417885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
373564ae02c3SLuigi Rizzo 	}
373664ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
3737506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
3738506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
3739506cc70cSLuigi Rizzo 
3740ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
374117885a7bSLuigi Rizzo 	if (netmap_verbose)
374217885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
37434bf50f18SLuigi Rizzo 		na->name,
374417885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
3745ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
374617885a7bSLuigi Rizzo 		kring->nr_hwtail,
374717885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
3748506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
374917885a7bSLuigi Rizzo 	if (tx == NR_TX) {
375017885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
375117885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
375217885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
375317885a7bSLuigi Rizzo 	}
3754506cc70cSLuigi Rizzo 
375568b8534bSLuigi Rizzo 	/*
3756ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
3757506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
3758506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
375968b8534bSLuigi Rizzo 	 */
376037e3a6d3SLuigi Rizzo 	kring->nr_mode = NKR_NETMAP_ON;
3761847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
376268b8534bSLuigi Rizzo 	return kring->ring->slot;
376368b8534bSLuigi Rizzo }
376468b8534bSLuigi Rizzo 
376568b8534bSLuigi Rizzo 
3766ce3ee1e7SLuigi Rizzo /*
3767f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
3768ce3ee1e7SLuigi Rizzo  *
3769ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3770ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
3771ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
3772849bec0eSLuigi Rizzo  *
3773f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
3774f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
3775f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
37764bf50f18SLuigi Rizzo  *   (see netmap_notify)
37774bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
37784bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
3779f9790aebSLuigi Rizzo  */
378037e3a6d3SLuigi Rizzo int
378137e3a6d3SLuigi Rizzo netmap_common_irq(struct netmap_adapter *na, u_int q, u_int *work_done)
3782f9790aebSLuigi Rizzo {
3783f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
3784847bf383SLuigi Rizzo 	enum txrx t = (work_done ? NR_RX : NR_TX);
3785f9790aebSLuigi Rizzo 
3786f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
3787f9790aebSLuigi Rizzo 
3788f9790aebSLuigi Rizzo 	if (netmap_verbose) {
3789f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
3790f9790aebSLuigi Rizzo 	}
3791f9790aebSLuigi Rizzo 
3792847bf383SLuigi Rizzo 	if (q >= nma_get_nrings(na, t))
379337e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS; // not a physical queue
3794847bf383SLuigi Rizzo 
37952ff91c17SVincenzo Maffione 	kring = NMR(na, t)[q];
3796847bf383SLuigi Rizzo 
379737e3a6d3SLuigi Rizzo 	if (kring->nr_mode == NKR_NETMAP_OFF) {
379837e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
379937e3a6d3SLuigi Rizzo 	}
380037e3a6d3SLuigi Rizzo 
3801847bf383SLuigi Rizzo 	if (t == NR_RX) {
3802f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
3803f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
3804f9790aebSLuigi Rizzo 	}
380537e3a6d3SLuigi Rizzo 
380637e3a6d3SLuigi Rizzo 	return kring->nm_notify(kring, 0);
3807f9790aebSLuigi Rizzo }
3808f9790aebSLuigi Rizzo 
380917885a7bSLuigi Rizzo 
3810f9790aebSLuigi Rizzo /*
3811f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
3812f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3813f9790aebSLuigi Rizzo  *
381437e3a6d3SLuigi Rizzo  * If the card is not in netmap mode, simply return NM_IRQ_PASS,
3815ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
381637e3a6d3SLuigi Rizzo  * Otherwise call netmap_common_irq().
3817ce3ee1e7SLuigi Rizzo  *
3818ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
3819ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
3820ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
382137e3a6d3SLuigi Rizzo  * and return NR_IRQ_COMPLETED.
3822ce3ee1e7SLuigi Rizzo  *
3823ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
382437e3a6d3SLuigi Rizzo  * calls the proper forwarding routine.
38251a26580eSLuigi Rizzo  */
3826babc7c12SLuigi Rizzo int
3827ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
38281a26580eSLuigi Rizzo {
38294bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
38304bf50f18SLuigi Rizzo 
38314bf50f18SLuigi Rizzo 	/*
38324bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
38334bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
38344bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
38354bf50f18SLuigi Rizzo 	 * nm_native_on() here.
38364bf50f18SLuigi Rizzo 	 */
38374bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
383837e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
3839849bec0eSLuigi Rizzo 
38404bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
38418241616dSLuigi Rizzo 		ND("use regular interrupt");
384237e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
38438241616dSLuigi Rizzo 	}
38448241616dSLuigi Rizzo 
384537e3a6d3SLuigi Rizzo 	return netmap_common_irq(na, q, work_done);
38461a26580eSLuigi Rizzo }
38471a26580eSLuigi Rizzo 
384864ae02c3SLuigi Rizzo 
384901c7d25fSLuigi Rizzo /*
3850f9790aebSLuigi Rizzo  * Module loader and unloader
3851f196ce38SLuigi Rizzo  *
3852f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
3853f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
3854f9790aebSLuigi Rizzo  * (but there is no chance)
3855f9790aebSLuigi Rizzo  *
3856f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
3857f196ce38SLuigi Rizzo  */
3858babc7c12SLuigi Rizzo 
3859babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
3860f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
3861babc7c12SLuigi Rizzo 
386217885a7bSLuigi Rizzo 
3863f9790aebSLuigi Rizzo void
386468b8534bSLuigi Rizzo netmap_fini(void)
386568b8534bSLuigi Rizzo {
3866f9790aebSLuigi Rizzo 	if (netmap_dev)
386768b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
386837e3a6d3SLuigi Rizzo 	/* we assume that there are no longer netmap users */
386937e3a6d3SLuigi Rizzo 	nm_os_ifnet_fini();
387037e3a6d3SLuigi Rizzo 	netmap_uninit_bridges();
3871ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
3872ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
3873c3e9b4dbSLuiz Otavio O Souza 	nm_prinf("netmap: unloaded module.\n");
387468b8534bSLuigi Rizzo }
387568b8534bSLuigi Rizzo 
387617885a7bSLuigi Rizzo 
3877f9790aebSLuigi Rizzo int
3878f9790aebSLuigi Rizzo netmap_init(void)
387968b8534bSLuigi Rizzo {
3880f9790aebSLuigi Rizzo 	int error;
388168b8534bSLuigi Rizzo 
3882f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
388368b8534bSLuigi Rizzo 
3884f9790aebSLuigi Rizzo 	error = netmap_mem_init();
3885f9790aebSLuigi Rizzo 	if (error != 0)
3886f9790aebSLuigi Rizzo 		goto fail;
3887c929ca72SLuigi Rizzo 	/*
3888c929ca72SLuigi Rizzo 	 * MAKEDEV_ETERNAL_KLD avoids an expensive check on syscalls
3889c929ca72SLuigi Rizzo 	 * when the module is compiled in.
3890c929ca72SLuigi Rizzo 	 * XXX could use make_dev_credv() to get error number
3891c929ca72SLuigi Rizzo 	 */
38920e73f29aSLuigi Rizzo 	netmap_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
389311c0b69cSAdrian Chadd 		&netmap_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0600,
38940e73f29aSLuigi Rizzo 			      "netmap");
3895f9790aebSLuigi Rizzo 	if (!netmap_dev)
3896f9790aebSLuigi Rizzo 		goto fail;
3897f9790aebSLuigi Rizzo 
3898847bf383SLuigi Rizzo 	error = netmap_init_bridges();
3899847bf383SLuigi Rizzo 	if (error)
3900847bf383SLuigi Rizzo 		goto fail;
3901847bf383SLuigi Rizzo 
39024bf50f18SLuigi Rizzo #ifdef __FreeBSD__
390337e3a6d3SLuigi Rizzo 	nm_os_vi_init_index();
39044bf50f18SLuigi Rizzo #endif
3905847bf383SLuigi Rizzo 
390637e3a6d3SLuigi Rizzo 	error = nm_os_ifnet_init();
390737e3a6d3SLuigi Rizzo 	if (error)
390837e3a6d3SLuigi Rizzo 		goto fail;
390937e3a6d3SLuigi Rizzo 
3910c3e9b4dbSLuiz Otavio O Souza 	nm_prinf("netmap: loaded module\n");
3911f9790aebSLuigi Rizzo 	return (0);
3912f9790aebSLuigi Rizzo fail:
391368b8534bSLuigi Rizzo 	netmap_fini();
3914f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
391568b8534bSLuigi Rizzo }
3916