xref: /freebsd/sys/dev/netmap/netmap.c (revision bb714db6d39583a9fbf5d11849c5e2365e7c0d80)
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>
440a4470078SGleb Smirnoff #include <sys/proc.h>
44189f6b863SAttilio Rao #include <sys/rwlock.h>
44268b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44368b8534bSLuigi Rizzo #include <sys/selinfo.h>
44468b8534bSLuigi Rizzo #include <sys/sysctl.h>
445339f59c0SGleb Smirnoff #include <sys/jail.h>
446a4470078SGleb Smirnoff #include <sys/epoch.h>
447339f59c0SGleb Smirnoff #include <net/vnet.h>
44868b8534bSLuigi Rizzo #include <net/if.h>
44976039bc8SGleb Smirnoff #include <net/if_var.h>
45068b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
45168b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
452ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
453ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
45489a9a5b5SVincenzo Maffione #include <net/ethernet.h>	/* ETHER_BPF_MTAP */
45568b8534bSLuigi Rizzo 
45668b8534bSLuigi Rizzo 
457ce3ee1e7SLuigi Rizzo #elif defined(linux)
458ce3ee1e7SLuigi Rizzo 
459ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
460ce3ee1e7SLuigi Rizzo 
461ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
462ce3ee1e7SLuigi Rizzo 
463ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
464ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
465ce3ee1e7SLuigi Rizzo 
46637e3a6d3SLuigi Rizzo #elif defined (_WIN32)
46737e3a6d3SLuigi Rizzo 
46837e3a6d3SLuigi Rizzo #include "win_glue.h"
46937e3a6d3SLuigi Rizzo 
470ce3ee1e7SLuigi Rizzo #else
471ce3ee1e7SLuigi Rizzo 
472ce3ee1e7SLuigi Rizzo #error	Unsupported platform
473ce3ee1e7SLuigi Rizzo 
474ce3ee1e7SLuigi Rizzo #endif /* unsupported */
475ce3ee1e7SLuigi Rizzo 
476ce3ee1e7SLuigi Rizzo /*
477ce3ee1e7SLuigi Rizzo  * common headers
478ce3ee1e7SLuigi Rizzo  */
4790b8ed8e0SLuigi Rizzo #include <net/netmap.h>
4800b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
481ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
4820b8ed8e0SLuigi Rizzo 
483ce3ee1e7SLuigi Rizzo 
4845819da83SLuigi Rizzo /* user-controlled variables */
4855819da83SLuigi Rizzo int netmap_verbose;
486b6e66be2SVincenzo Maffione #ifdef CONFIG_NETMAP_DEBUG
487b6e66be2SVincenzo Maffione int netmap_debug;
488b6e66be2SVincenzo Maffione #endif /* CONFIG_NETMAP_DEBUG */
4895819da83SLuigi Rizzo 
4905819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
491c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
492f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
493c3e9b4dbSLuiz Otavio O Souza static int netmap_fwd = 0;	/* force transparent forwarding */
494f196ce38SLuigi Rizzo 
495f9790aebSLuigi Rizzo /*
496f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
497f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
498f9790aebSLuigi Rizzo  */
499f9790aebSLuigi Rizzo enum {	NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
500f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
501f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
502f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
503f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
504f9790aebSLuigi Rizzo 
50537e3a6d3SLuigi Rizzo /* netmap_generic_mit controls mitigation of RX notifications for
50637e3a6d3SLuigi Rizzo  * the generic netmap adapter. The value is a time interval in
50737e3a6d3SLuigi Rizzo  * nanoseconds. */
50837e3a6d3SLuigi Rizzo int netmap_generic_mit = 100*1000;
50937e3a6d3SLuigi Rizzo 
51037e3a6d3SLuigi Rizzo /* We use by default netmap-aware qdiscs with generic netmap adapters,
51137e3a6d3SLuigi Rizzo  * even if there can be a little performance hit with hardware NICs.
51237e3a6d3SLuigi Rizzo  * However, using the qdisc is the safer approach, for two reasons:
51337e3a6d3SLuigi Rizzo  * 1) it prevents non-fifo qdiscs to break the TX notification
51437e3a6d3SLuigi Rizzo  *    scheme, which is based on mbuf destructors when txqdisc is
51537e3a6d3SLuigi Rizzo  *    not used.
51637e3a6d3SLuigi Rizzo  * 2) it makes it possible to transmit over software devices that
51737e3a6d3SLuigi Rizzo  *    change skb->dev, like bridge, veth, ...
51837e3a6d3SLuigi Rizzo  *
51937e3a6d3SLuigi Rizzo  * Anyway users looking for the best performance should
52037e3a6d3SLuigi Rizzo  * use native adapters.
52137e3a6d3SLuigi Rizzo  */
5224f80b14cSVincenzo Maffione #ifdef linux
52337e3a6d3SLuigi Rizzo int netmap_generic_txqdisc = 1;
5244f80b14cSVincenzo Maffione #endif
52537e3a6d3SLuigi Rizzo 
52637e3a6d3SLuigi Rizzo /* Default number of slots and queues for generic adapters. */
52737e3a6d3SLuigi Rizzo int netmap_generic_ringsize = 1024;
52837e3a6d3SLuigi Rizzo int netmap_generic_rings = 1;
52937e3a6d3SLuigi Rizzo 
5302a7db7a6SVincenzo Maffione /* Non-zero to enable checksum offloading in NIC drivers */
5312a7db7a6SVincenzo Maffione int netmap_generic_hwcsum = 0;
5322a7db7a6SVincenzo Maffione 
53337e3a6d3SLuigi Rizzo /* Non-zero if ptnet devices are allowed to use virtio-net headers. */
53437e3a6d3SLuigi Rizzo int ptnet_vnet_hdr = 1;
53537e3a6d3SLuigi Rizzo 
53637e3a6d3SLuigi Rizzo /*
53737e3a6d3SLuigi Rizzo  * SYSCTL calls are grouped between SYSBEGIN and SYSEND to be emulated
53837e3a6d3SLuigi Rizzo  * in some other operating systems
53937e3a6d3SLuigi Rizzo  */
54037e3a6d3SLuigi Rizzo SYSBEGIN(main_init);
54137e3a6d3SLuigi Rizzo 
54237e3a6d3SLuigi Rizzo SYSCTL_DECL(_dev_netmap);
5437029da5cSPawel Biernacki SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
5447029da5cSPawel Biernacki     "Netmap args");
54537e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
54637e3a6d3SLuigi Rizzo 		CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
547b6e66be2SVincenzo Maffione #ifdef CONFIG_NETMAP_DEBUG
548b6e66be2SVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, debug,
549b6e66be2SVincenzo Maffione 		CTLFLAG_RW, &netmap_debug, 0, "Debug messages");
550b6e66be2SVincenzo Maffione #endif /* CONFIG_NETMAP_DEBUG */
55137e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
55237e3a6d3SLuigi Rizzo 		CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
5534f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, CTLFLAG_RW, &netmap_no_pendintr,
5544f80b14cSVincenzo Maffione 		0, "Always look for new received packets.");
55537e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
55637e3a6d3SLuigi Rizzo 		&netmap_txsync_retry, 0, "Number of txsync loops in bridge's flush.");
557f9790aebSLuigi Rizzo 
5584f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0,
5594f80b14cSVincenzo Maffione 		"Force NR_FORWARD mode");
5604f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0,
5614f80b14cSVincenzo Maffione 		"Adapter mode. 0 selects the best option available,"
5624f80b14cSVincenzo Maffione 		"1 forces native adapter, 2 forces emulated adapter");
5632a7db7a6SVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_hwcsum, CTLFLAG_RW, &netmap_generic_hwcsum,
5642a7db7a6SVincenzo Maffione 		0, "Hardware checksums. 0 to disable checksum generation by the NIC (default),"
5652a7db7a6SVincenzo Maffione 		"1 to enable checksum generation by the NIC");
5664f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit,
5674f80b14cSVincenzo Maffione 		0, "RX notification interval in nanoseconds");
5684f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW,
5694f80b14cSVincenzo Maffione 		&netmap_generic_ringsize, 0,
5704f80b14cSVincenzo Maffione 		"Number of per-ring slots for emulated netmap mode");
5714f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW,
5724f80b14cSVincenzo Maffione 		&netmap_generic_rings, 0,
5734f80b14cSVincenzo Maffione 		"Number of TX/RX queues for emulated netmap adapters");
5744f80b14cSVincenzo Maffione #ifdef linux
5754f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_txqdisc, CTLFLAG_RW,
5764f80b14cSVincenzo Maffione 		&netmap_generic_txqdisc, 0, "Use qdisc for generic adapters");
5774f80b14cSVincenzo Maffione #endif
5784f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, ptnet_vnet_hdr, CTLFLAG_RW, &ptnet_vnet_hdr,
5794f80b14cSVincenzo Maffione 		0, "Allow ptnet devices to use virtio-net headers");
58037e3a6d3SLuigi Rizzo 
58137e3a6d3SLuigi Rizzo SYSEND;
582f196ce38SLuigi Rizzo 
583ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
584ce3ee1e7SLuigi Rizzo 
58517885a7bSLuigi Rizzo /*
58617885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
58717885a7bSLuigi Rizzo  * to make sure other users get to see it.
58837e3a6d3SLuigi Rizzo  * stopped must be either NR_KR_STOPPED (for unbounded stop)
58937e3a6d3SLuigi Rizzo  * of NR_KR_LOCKED (brief stop for mutual exclusion purposes)
59017885a7bSLuigi Rizzo  */
5914bf50f18SLuigi Rizzo static void
59237e3a6d3SLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr, int stopped)
593ce3ee1e7SLuigi Rizzo {
59437e3a6d3SLuigi Rizzo 	nm_kr_stop(kr, stopped);
59537e3a6d3SLuigi Rizzo 	// XXX check if nm_kr_stop is sufficient
596ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
597ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
598ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
599ce3ee1e7SLuigi Rizzo }
600ce3ee1e7SLuigi Rizzo 
601847bf383SLuigi Rizzo /* stop or enable a single ring */
6024bf50f18SLuigi Rizzo void
603847bf383SLuigi Rizzo netmap_set_ring(struct netmap_adapter *na, u_int ring_id, enum txrx t, int stopped)
6044bf50f18SLuigi Rizzo {
6054bf50f18SLuigi Rizzo 	if (stopped)
6062ff91c17SVincenzo Maffione 		netmap_disable_ring(NMR(na, t)[ring_id], stopped);
6074bf50f18SLuigi Rizzo 	else
6082ff91c17SVincenzo Maffione 		NMR(na, t)[ring_id]->nkr_stopped = 0;
6094bf50f18SLuigi Rizzo }
6104bf50f18SLuigi Rizzo 
611f9790aebSLuigi Rizzo 
61289cc2556SLuigi Rizzo /* stop or enable all the rings of na */
6134bf50f18SLuigi Rizzo void
6144bf50f18SLuigi Rizzo netmap_set_all_rings(struct netmap_adapter *na, int stopped)
615ce3ee1e7SLuigi Rizzo {
616ce3ee1e7SLuigi Rizzo 	int i;
617847bf383SLuigi Rizzo 	enum txrx t;
618ce3ee1e7SLuigi Rizzo 
6194bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
620ce3ee1e7SLuigi Rizzo 		return;
621ce3ee1e7SLuigi Rizzo 
622*bb714db6SVincenzo Maffione 	if (netmap_verbose) {
623*bb714db6SVincenzo Maffione 		nm_prinf("%s: %sable all rings", na->name,
624*bb714db6SVincenzo Maffione 		    (stopped ? "dis" : "en"));
625*bb714db6SVincenzo Maffione 	}
626847bf383SLuigi Rizzo 	for_rx_tx(t) {
627847bf383SLuigi Rizzo 		for (i = 0; i < netmap_real_rings(na, t); i++) {
628847bf383SLuigi Rizzo 			netmap_set_ring(na, i, t, stopped);
629ce3ee1e7SLuigi Rizzo 		}
630ce3ee1e7SLuigi Rizzo 	}
631ce3ee1e7SLuigi Rizzo }
632ce3ee1e7SLuigi Rizzo 
63389cc2556SLuigi Rizzo /*
63489cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
63589cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
636ddb13598SKevin Lo  * netmap mode off, or before removing the hardware rings (e.g., on module
63737e3a6d3SLuigi Rizzo  * onload).
63889cc2556SLuigi Rizzo  */
639f9790aebSLuigi Rizzo void
640f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
641f9790aebSLuigi Rizzo {
64237e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
64337e3a6d3SLuigi Rizzo 		netmap_set_all_rings(NA(ifp), NM_KR_STOPPED);
64437e3a6d3SLuigi Rizzo 	}
645f9790aebSLuigi Rizzo }
646f9790aebSLuigi Rizzo 
64789cc2556SLuigi Rizzo /*
64889cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
64989cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
65089cc2556SLuigi Rizzo  * napi_enable().
65189cc2556SLuigi Rizzo  */
652f9790aebSLuigi Rizzo void
653f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
654f9790aebSLuigi Rizzo {
65537e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
6564bf50f18SLuigi Rizzo 		netmap_set_all_rings(NA(ifp), 0 /* enabled */);
657f9790aebSLuigi Rizzo 	}
65837e3a6d3SLuigi Rizzo }
659f9790aebSLuigi Rizzo 
66037e3a6d3SLuigi Rizzo void
66137e3a6d3SLuigi Rizzo netmap_make_zombie(struct ifnet *ifp)
66237e3a6d3SLuigi Rizzo {
66337e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
66437e3a6d3SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
66537e3a6d3SLuigi Rizzo 		netmap_set_all_rings(na, NM_KR_LOCKED);
66637e3a6d3SLuigi Rizzo 		na->na_flags |= NAF_ZOMBIE;
66737e3a6d3SLuigi Rizzo 		netmap_set_all_rings(na, 0);
66837e3a6d3SLuigi Rizzo 	}
66937e3a6d3SLuigi Rizzo }
67037e3a6d3SLuigi Rizzo 
67137e3a6d3SLuigi Rizzo void
67237e3a6d3SLuigi Rizzo netmap_undo_zombie(struct ifnet *ifp)
67337e3a6d3SLuigi Rizzo {
67437e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
67537e3a6d3SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
67637e3a6d3SLuigi Rizzo 		if (na->na_flags & NAF_ZOMBIE) {
67737e3a6d3SLuigi Rizzo 			netmap_set_all_rings(na, NM_KR_LOCKED);
67837e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_ZOMBIE;
67937e3a6d3SLuigi Rizzo 			netmap_set_all_rings(na, 0);
68037e3a6d3SLuigi Rizzo 		}
68137e3a6d3SLuigi Rizzo 	}
68237e3a6d3SLuigi Rizzo }
683f9790aebSLuigi Rizzo 
684ce3ee1e7SLuigi Rizzo /*
685ce3ee1e7SLuigi Rizzo  * generic bound_checking function
686ce3ee1e7SLuigi Rizzo  */
687ce3ee1e7SLuigi Rizzo u_int
688ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
689ce3ee1e7SLuigi Rizzo {
690ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
691ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
692ce3ee1e7SLuigi Rizzo 
693ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
694ce3ee1e7SLuigi Rizzo 		dflt = lo;
695ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
696ce3ee1e7SLuigi Rizzo 		dflt = hi;
697ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
698ce3ee1e7SLuigi Rizzo 		*v = dflt;
699ce3ee1e7SLuigi Rizzo 		op = "Bump";
700ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
701ce3ee1e7SLuigi Rizzo 		*v = hi;
702ce3ee1e7SLuigi Rizzo 		op = "Clamp";
703ce3ee1e7SLuigi Rizzo 	}
704ce3ee1e7SLuigi Rizzo 	if (op && msg)
705b6e66be2SVincenzo Maffione 		nm_prinf("%s %s to %d (was %d)", op, msg, *v, oldv);
706ce3ee1e7SLuigi Rizzo 	return *v;
707ce3ee1e7SLuigi Rizzo }
708ce3ee1e7SLuigi Rizzo 
709f9790aebSLuigi Rizzo 
710ce3ee1e7SLuigi Rizzo /*
711ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
712ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
713ce3ee1e7SLuigi Rizzo  */
714ce3ee1e7SLuigi Rizzo const char *
715ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
716ce3ee1e7SLuigi Rizzo {
717ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
718ce3ee1e7SLuigi Rizzo 	int i, j, i0;
719ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
720ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
721ce3ee1e7SLuigi Rizzo 
722ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
723ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
724ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
725ce3ee1e7SLuigi Rizzo 	if (!dst)
726ce3ee1e7SLuigi Rizzo 		dst = _dst;
727ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
728ce3ee1e7SLuigi Rizzo 		lim = len;
729ce3ee1e7SLuigi Rizzo 	o = dst;
730ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
731ce3ee1e7SLuigi Rizzo 	o += strlen(o);
732ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
733ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
734ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
735ce3ee1e7SLuigi Rizzo 		o += strlen(o);
736ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
737ce3ee1e7SLuigi Rizzo 		i0 = i;
738ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
739ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
740ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
741ce3ee1e7SLuigi Rizzo 		}
742ce3ee1e7SLuigi Rizzo 		i = i0;
743ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
744ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
745ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
746ce3ee1e7SLuigi Rizzo 		o += j+49;
747ce3ee1e7SLuigi Rizzo 	}
748ce3ee1e7SLuigi Rizzo 	*o = '\0';
749ce3ee1e7SLuigi Rizzo #undef P_HI
750ce3ee1e7SLuigi Rizzo #undef P_LO
751ce3ee1e7SLuigi Rizzo #undef P_C
752ce3ee1e7SLuigi Rizzo 	return dst;
753ce3ee1e7SLuigi Rizzo }
754f196ce38SLuigi Rizzo 
755f18be576SLuigi Rizzo 
756ae10d1afSLuigi Rizzo /*
757ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
758ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
759ae10d1afSLuigi Rizzo  */
76089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
761f9790aebSLuigi Rizzo int
762ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
763ae10d1afSLuigi Rizzo {
7642ff91c17SVincenzo Maffione 	struct nm_config_info info;
765ae10d1afSLuigi Rizzo 
7662ff91c17SVincenzo Maffione 	bzero(&info, sizeof(info));
7676641c68bSLuigi Rizzo 	if (na->nm_config == NULL ||
7682ff91c17SVincenzo Maffione 	    na->nm_config(na, &info)) {
769ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
7702ff91c17SVincenzo Maffione 		info.num_tx_rings = na->num_tx_rings;
7712ff91c17SVincenzo Maffione 		info.num_tx_descs = na->num_tx_desc;
7722ff91c17SVincenzo Maffione 		info.num_rx_rings = na->num_rx_rings;
7732ff91c17SVincenzo Maffione 		info.num_rx_descs = na->num_rx_desc;
7742ff91c17SVincenzo Maffione 		info.rx_buf_maxsize = na->rx_buf_maxsize;
775ae10d1afSLuigi Rizzo 	}
776ae10d1afSLuigi Rizzo 
7772ff91c17SVincenzo Maffione 	if (na->num_tx_rings == info.num_tx_rings &&
7782ff91c17SVincenzo Maffione 	    na->num_tx_desc == info.num_tx_descs &&
7792ff91c17SVincenzo Maffione 	    na->num_rx_rings == info.num_rx_rings &&
7802ff91c17SVincenzo Maffione 	    na->num_rx_desc == info.num_rx_descs &&
7812ff91c17SVincenzo Maffione 	    na->rx_buf_maxsize == info.rx_buf_maxsize)
782ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
783f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
7842ff91c17SVincenzo Maffione 		na->num_tx_rings = info.num_tx_rings;
7852ff91c17SVincenzo Maffione 		na->num_tx_desc = info.num_tx_descs;
7862ff91c17SVincenzo Maffione 		na->num_rx_rings = info.num_rx_rings;
7872ff91c17SVincenzo Maffione 		na->num_rx_desc = info.num_rx_descs;
7882ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = info.rx_buf_maxsize;
789b6e66be2SVincenzo Maffione 		if (netmap_verbose)
790b6e66be2SVincenzo Maffione 			nm_prinf("configuration changed for %s: txring %d x %d, "
791cfa866f6SMatt Macy 				"rxring %d x %d, rxbufsz %d",
792cfa866f6SMatt Macy 				na->name, na->num_tx_rings, na->num_tx_desc,
793cfa866f6SMatt Macy 				na->num_rx_rings, na->num_rx_desc, na->rx_buf_maxsize);
794ae10d1afSLuigi Rizzo 		return 0;
795ae10d1afSLuigi Rizzo 	}
796b6e66be2SVincenzo Maffione 	nm_prerr("WARNING: configuration changed for %s while active: "
7972ff91c17SVincenzo Maffione 		"txring %d x %d, rxring %d x %d, rxbufsz %d",
7982ff91c17SVincenzo Maffione 		na->name, info.num_tx_rings, info.num_tx_descs,
7992ff91c17SVincenzo Maffione 		info.num_rx_rings, info.num_rx_descs,
8002ff91c17SVincenzo Maffione 		info.rx_buf_maxsize);
801ae10d1afSLuigi Rizzo 	return 1;
802ae10d1afSLuigi Rizzo }
803ae10d1afSLuigi Rizzo 
80437e3a6d3SLuigi Rizzo /* nm_sync callbacks for the host rings */
80537e3a6d3SLuigi Rizzo static int netmap_txsync_to_host(struct netmap_kring *kring, int flags);
80637e3a6d3SLuigi Rizzo static int netmap_rxsync_from_host(struct netmap_kring *kring, int flags);
807f0ea3689SLuigi Rizzo 
808f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
809f0ea3689SLuigi Rizzo  * The array layout is this:
810f0ea3689SLuigi Rizzo  *
811f0ea3689SLuigi Rizzo  *                    +----------+
812f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
813f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
814f0ea3689SLuigi Rizzo  *                    |          | /
815f0ea3689SLuigi Rizzo  *                    +----------+
816f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
817f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
818f0ea3689SLuigi Rizzo  *                    |          | \
819f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
820f0ea3689SLuigi Rizzo  *                    |          | /
821f0ea3689SLuigi Rizzo  *                    +----------+
822f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
823f0ea3689SLuigi Rizzo  *                    +----------+
824f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
825f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
826f0ea3689SLuigi Rizzo  *                    |          | /
827f0ea3689SLuigi Rizzo  *                    +----------+
828f0ea3689SLuigi Rizzo  *
829f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
830f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
831f0ea3689SLuigi Rizzo  */
83289cc2556SLuigi Rizzo /* call with NMG_LOCK held */
833f9790aebSLuigi Rizzo int
834f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
835f9790aebSLuigi Rizzo {
836f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
837f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
838847bf383SLuigi Rizzo 	u_int n[NR_TXRX];
839847bf383SLuigi Rizzo 	enum txrx t;
84019c4ec08SVincenzo Maffione 	int err = 0;
841f9790aebSLuigi Rizzo 
842c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings != NULL) {
843b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
844b6e66be2SVincenzo Maffione 			nm_prerr("warning: krings were already created");
845c3e9b4dbSLuiz Otavio O Souza 		return 0;
846c3e9b4dbSLuiz Otavio O Souza 	}
847c3e9b4dbSLuiz Otavio O Souza 
848f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
8492a7db7a6SVincenzo Maffione 	n[NR_TX] = netmap_all_rings(na, NR_TX);
8502a7db7a6SVincenzo Maffione 	n[NR_RX] = netmap_all_rings(na, NR_RX);
851f0ea3689SLuigi Rizzo 
8522ff91c17SVincenzo Maffione 	len = (n[NR_TX] + n[NR_RX]) *
8532ff91c17SVincenzo Maffione 		(sizeof(struct netmap_kring) + sizeof(struct netmap_kring *))
8542ff91c17SVincenzo Maffione 		+ tailroom;
855f9790aebSLuigi Rizzo 
856c3e9b4dbSLuiz Otavio O Souza 	na->tx_rings = nm_os_malloc((size_t)len);
857f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
858b6e66be2SVincenzo Maffione 		nm_prerr("Cannot allocate krings");
859f9790aebSLuigi Rizzo 		return ENOMEM;
860f9790aebSLuigi Rizzo 	}
861847bf383SLuigi Rizzo 	na->rx_rings = na->tx_rings + n[NR_TX];
8622ff91c17SVincenzo Maffione 	na->tailroom = na->rx_rings + n[NR_RX];
8632ff91c17SVincenzo Maffione 
8642ff91c17SVincenzo Maffione 	/* link the krings in the krings array */
8652ff91c17SVincenzo Maffione 	kring = (struct netmap_kring *)((char *)na->tailroom + tailroom);
8662ff91c17SVincenzo Maffione 	for (i = 0; i < n[NR_TX] + n[NR_RX]; i++) {
8672ff91c17SVincenzo Maffione 		na->tx_rings[i] = kring;
8682ff91c17SVincenzo Maffione 		kring++;
8692ff91c17SVincenzo Maffione 	}
870f9790aebSLuigi Rizzo 
87117885a7bSLuigi Rizzo 	/*
87217885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
87317885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
87417885a7bSLuigi Rizzo 	 */
875847bf383SLuigi Rizzo 	for_rx_tx(t) {
876847bf383SLuigi Rizzo 		ndesc = nma_get_ndesc(na, t);
877847bf383SLuigi Rizzo 		for (i = 0; i < n[t]; i++) {
8782ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
879f9790aebSLuigi Rizzo 			bzero(kring, sizeof(*kring));
8802ff91c17SVincenzo Maffione 			kring->notify_na = na;
88117885a7bSLuigi Rizzo 			kring->ring_id = i;
882847bf383SLuigi Rizzo 			kring->tx = t;
883f9790aebSLuigi Rizzo 			kring->nkr_num_slots = ndesc;
88437e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
88537e3a6d3SLuigi Rizzo 			kring->nr_pending_mode = NKR_NETMAP_OFF;
886847bf383SLuigi Rizzo 			if (i < nma_get_nrings(na, t)) {
887847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ? na->nm_txsync : na->nm_rxsync);
88837e3a6d3SLuigi Rizzo 			} else {
8892ff91c17SVincenzo Maffione 				if (!(na->na_flags & NAF_HOST_RINGS))
8902ff91c17SVincenzo Maffione 					kring->nr_kflags |= NKR_FAKERING;
891847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ?
89237e3a6d3SLuigi Rizzo 						netmap_txsync_to_host:
89337e3a6d3SLuigi Rizzo 						netmap_rxsync_from_host);
894f0ea3689SLuigi Rizzo 			}
895847bf383SLuigi Rizzo 			kring->nm_notify = na->nm_notify;
896847bf383SLuigi Rizzo 			kring->rhead = kring->rcur = kring->nr_hwcur = 0;
897f9790aebSLuigi Rizzo 			/*
89817885a7bSLuigi Rizzo 			 * IMPORTANT: Always keep one slot empty.
899f9790aebSLuigi Rizzo 			 */
900847bf383SLuigi Rizzo 			kring->rtail = kring->nr_hwtail = (t == NR_TX ? ndesc - 1 : 0);
901847bf383SLuigi Rizzo 			snprintf(kring->name, sizeof(kring->name) - 1, "%s %s%d", na->name,
902847bf383SLuigi Rizzo 					nm_txrx2str(t), i);
90375f4f3edSVincenzo Maffione 			nm_prdis("ktx %s h %d c %d t %d",
904f0ea3689SLuigi Rizzo 				kring->name, kring->rhead, kring->rcur, kring->rtail);
90519c4ec08SVincenzo Maffione 			err = nm_os_selinfo_init(&kring->si, kring->name);
90619c4ec08SVincenzo Maffione 			if (err) {
90719c4ec08SVincenzo Maffione 				netmap_krings_delete(na);
90819c4ec08SVincenzo Maffione 				return err;
90919c4ec08SVincenzo Maffione 			}
910847bf383SLuigi Rizzo 			mtx_init(&kring->q_lock, (t == NR_TX ? "nm_txq_lock" : "nm_rxq_lock"), NULL, MTX_DEF);
91119c4ec08SVincenzo Maffione 			kring->na = na;	/* setting this field marks the mutex as initialized */
912f9790aebSLuigi Rizzo 		}
91319c4ec08SVincenzo Maffione 		err = nm_os_selinfo_init(&na->si[t], na->name);
91419c4ec08SVincenzo Maffione 		if (err) {
91519c4ec08SVincenzo Maffione 			netmap_krings_delete(na);
91619c4ec08SVincenzo Maffione 			return err;
917f0ea3689SLuigi Rizzo 		}
91819c4ec08SVincenzo Maffione 	}
919f9790aebSLuigi Rizzo 
920f9790aebSLuigi Rizzo 	return 0;
921f9790aebSLuigi Rizzo }
922f9790aebSLuigi Rizzo 
923f9790aebSLuigi Rizzo 
924f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
92589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
926f9790aebSLuigi Rizzo void
927f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
928f9790aebSLuigi Rizzo {
9292ff91c17SVincenzo Maffione 	struct netmap_kring **kring = na->tx_rings;
930847bf383SLuigi Rizzo 	enum txrx t;
931847bf383SLuigi Rizzo 
932c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings == NULL) {
933b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
934b6e66be2SVincenzo Maffione 			nm_prerr("warning: krings were already deleted");
935c3e9b4dbSLuiz Otavio O Souza 		return;
936c3e9b4dbSLuiz Otavio O Souza 	}
937c3e9b4dbSLuiz Otavio O Souza 
938847bf383SLuigi Rizzo 	for_rx_tx(t)
93937e3a6d3SLuigi Rizzo 		nm_os_selinfo_uninit(&na->si[t]);
940f9790aebSLuigi Rizzo 
941f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
942f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
94319c4ec08SVincenzo Maffione 		if ((*kring)->na != NULL)
9442ff91c17SVincenzo Maffione 			mtx_destroy(&(*kring)->q_lock);
9452ff91c17SVincenzo Maffione 		nm_os_selinfo_uninit(&(*kring)->si);
946f9790aebSLuigi Rizzo 	}
947c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(na->tx_rings);
948f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
949f9790aebSLuigi Rizzo }
950f9790aebSLuigi Rizzo 
951f9790aebSLuigi Rizzo 
95217885a7bSLuigi Rizzo /*
95317885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
95417885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
95517885a7bSLuigi Rizzo  * them first.
95617885a7bSLuigi Rizzo  */
95789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
95837e3a6d3SLuigi Rizzo void
95917885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
96017885a7bSLuigi Rizzo {
9612a7db7a6SVincenzo Maffione 	u_int lim = netmap_real_rings(na, NR_RX), i;
96217885a7bSLuigi Rizzo 
9632a7db7a6SVincenzo Maffione 	for (i = nma_get_nrings(na, NR_RX); i < lim; i++) {
9642a7db7a6SVincenzo Maffione 		struct mbq *q = &NMR(na, NR_RX)[i]->rx_queue;
96575f4f3edSVincenzo Maffione 		nm_prdis("destroy sw mbq with len %d", mbq_len(q));
96617885a7bSLuigi Rizzo 		mbq_purge(q);
96737e3a6d3SLuigi Rizzo 		mbq_safe_fini(q);
9682a7db7a6SVincenzo Maffione 	}
96917885a7bSLuigi Rizzo 	netmap_krings_delete(na);
97017885a7bSLuigi Rizzo }
97117885a7bSLuigi Rizzo 
9724f80b14cSVincenzo Maffione static void
9734f80b14cSVincenzo Maffione netmap_mem_drop(struct netmap_adapter *na)
9744f80b14cSVincenzo Maffione {
9754f80b14cSVincenzo Maffione 	int last = netmap_mem_deref(na->nm_mem, na);
9764f80b14cSVincenzo Maffione 	/* if the native allocator had been overrided on regif,
9774f80b14cSVincenzo Maffione 	 * restore it now and drop the temporary one
9784f80b14cSVincenzo Maffione 	 */
9794f80b14cSVincenzo Maffione 	if (last && na->nm_mem_prev) {
9804f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
9814f80b14cSVincenzo Maffione 		na->nm_mem = na->nm_mem_prev;
9824f80b14cSVincenzo Maffione 		na->nm_mem_prev = NULL;
9834f80b14cSVincenzo Maffione 	}
9844f80b14cSVincenzo Maffione }
985f9790aebSLuigi Rizzo 
98668b8534bSLuigi Rizzo /*
987847bf383SLuigi Rizzo  * Undo everything that was done in netmap_do_regif(). In particular,
988847bf383SLuigi Rizzo  * call nm_register(ifp,0) to stop netmap mode on the interface and
9894bf50f18SLuigi Rizzo  * revert to normal operation.
99068b8534bSLuigi Rizzo  */
991ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
992847bf383SLuigi Rizzo static void netmap_unset_ringid(struct netmap_priv_d *);
99337e3a6d3SLuigi Rizzo static void netmap_krings_put(struct netmap_priv_d *);
99437e3a6d3SLuigi Rizzo void
995847bf383SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv)
99668b8534bSLuigi Rizzo {
997f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
99868b8534bSLuigi Rizzo 
999ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1000f9790aebSLuigi Rizzo 	na->active_fds--;
100137e3a6d3SLuigi Rizzo 	/* unset nr_pending_mode and possibly release exclusive mode */
100237e3a6d3SLuigi Rizzo 	netmap_krings_put(priv);
1003847bf383SLuigi Rizzo 
1004847bf383SLuigi Rizzo #ifdef	WITH_MONITOR
100537e3a6d3SLuigi Rizzo 	/* XXX check whether we have to do something with monitor
100637e3a6d3SLuigi Rizzo 	 * when rings change nr_mode. */
100737e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {
1008847bf383SLuigi Rizzo 		/* walk through all the rings and tell any monitor
1009847bf383SLuigi Rizzo 		 * that the port is going to exit netmap mode
1010847bf383SLuigi Rizzo 		 */
1011847bf383SLuigi Rizzo 		netmap_monitor_stop(na);
101237e3a6d3SLuigi Rizzo 	}
1013847bf383SLuigi Rizzo #endif
101437e3a6d3SLuigi Rizzo 
101537e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0 || nm_kring_pending(priv)) {
101637e3a6d3SLuigi Rizzo 		na->nm_register(na, 0);
101737e3a6d3SLuigi Rizzo 	}
101837e3a6d3SLuigi Rizzo 
101937e3a6d3SLuigi Rizzo 	/* delete rings and buffers that are no longer needed */
102037e3a6d3SLuigi Rizzo 	netmap_mem_rings_delete(na);
102137e3a6d3SLuigi Rizzo 
102237e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
102368b8534bSLuigi Rizzo 		/*
102437e3a6d3SLuigi Rizzo 		 * (TO CHECK) We enter here
1025f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
1026f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
1027f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
1028ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
1029ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
1030ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
1031ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
1032ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
1033ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
1034ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
1035ce3ee1e7SLuigi Rizzo 		 * syscall is running.
103668b8534bSLuigi Rizzo 		 */
1037b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
1038b6e66be2SVincenzo Maffione 			nm_prinf("deleting last instance for %s", na->name);
103937e3a6d3SLuigi Rizzo 
104037e3a6d3SLuigi Rizzo 		if (nm_netmap_on(na)) {
1041b6e66be2SVincenzo Maffione 			nm_prerr("BUG: netmap on while going to delete the krings");
104237e3a6d3SLuigi Rizzo 		}
104337e3a6d3SLuigi Rizzo 
1044f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
1045d12354a5SVincenzo Maffione 
1046d12354a5SVincenzo Maffione 		/* restore the default number of host tx and rx rings */
1047253b2ec1SVincenzo Maffione 		if (na->na_flags & NAF_HOST_RINGS) {
1048d12354a5SVincenzo Maffione 			na->num_host_tx_rings = 1;
1049d12354a5SVincenzo Maffione 			na->num_host_rx_rings = 1;
1050253b2ec1SVincenzo Maffione 		} else {
1051253b2ec1SVincenzo Maffione 			na->num_host_tx_rings = 0;
1052253b2ec1SVincenzo Maffione 			na->num_host_rx_rings = 0;
1053253b2ec1SVincenzo Maffione 		}
105468b8534bSLuigi Rizzo 	}
105537e3a6d3SLuigi Rizzo 
1056847bf383SLuigi Rizzo 	/* possibily decrement counter of tx_si/rx_si users */
1057847bf383SLuigi Rizzo 	netmap_unset_ringid(priv);
1058f9790aebSLuigi Rizzo 	/* delete the nifp */
1059847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, priv->np_nifp);
1060847bf383SLuigi Rizzo 	/* drop the allocator */
10614f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
1062847bf383SLuigi Rizzo 	/* mark the priv as unregistered */
1063847bf383SLuigi Rizzo 	priv->np_na = NULL;
1064847bf383SLuigi Rizzo 	priv->np_nifp = NULL;
10655819da83SLuigi Rizzo }
106668b8534bSLuigi Rizzo 
106737e3a6d3SLuigi Rizzo struct netmap_priv_d*
106837e3a6d3SLuigi Rizzo netmap_priv_new(void)
106937e3a6d3SLuigi Rizzo {
107037e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
107137e3a6d3SLuigi Rizzo 
1072c3e9b4dbSLuiz Otavio O Souza 	priv = nm_os_malloc(sizeof(struct netmap_priv_d));
107337e3a6d3SLuigi Rizzo 	if (priv == NULL)
107437e3a6d3SLuigi Rizzo 		return NULL;
107537e3a6d3SLuigi Rizzo 	priv->np_refs = 1;
107637e3a6d3SLuigi Rizzo 	nm_os_get_module();
107737e3a6d3SLuigi Rizzo 	return priv;
107837e3a6d3SLuigi Rizzo }
107937e3a6d3SLuigi Rizzo 
1080ce3ee1e7SLuigi Rizzo /*
10818fd44c93SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd is closed
10828fd44c93SLuigi Rizzo  * Action: undo all the things done by NIOCREGIF,
10838fd44c93SLuigi Rizzo  * On FreeBSD we need to track whether there are active mmap()s,
10848fd44c93SLuigi Rizzo  * and we use np_active_mmaps for that. On linux, the field is always 0.
10858fd44c93SLuigi Rizzo  * Return: 1 if we can free priv, 0 otherwise.
108689cc2556SLuigi Rizzo  *
1087ce3ee1e7SLuigi Rizzo  */
108889cc2556SLuigi Rizzo /* call with NMG_LOCK held */
108937e3a6d3SLuigi Rizzo void
109037e3a6d3SLuigi Rizzo netmap_priv_delete(struct netmap_priv_d *priv)
1091ce3ee1e7SLuigi Rizzo {
1092f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1093ce3ee1e7SLuigi Rizzo 
1094847adfb7SLuigi Rizzo 	/* number of active references to this fd */
10958fd44c93SLuigi Rizzo 	if (--priv->np_refs > 0) {
109637e3a6d3SLuigi Rizzo 		return;
1097ce3ee1e7SLuigi Rizzo 	}
109837e3a6d3SLuigi Rizzo 	nm_os_put_module();
109937e3a6d3SLuigi Rizzo 	if (na) {
1100847bf383SLuigi Rizzo 		netmap_do_unregif(priv);
110137e3a6d3SLuigi Rizzo 	}
110237e3a6d3SLuigi Rizzo 	netmap_unget_na(na, priv->np_ifp);
110337e3a6d3SLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* for safety */
1104c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(priv);
1105f196ce38SLuigi Rizzo }
11065819da83SLuigi Rizzo 
1107f9790aebSLuigi Rizzo 
110889cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
1109f9790aebSLuigi Rizzo void
11105819da83SLuigi Rizzo netmap_dtor(void *data)
11115819da83SLuigi Rizzo {
11125819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
11135819da83SLuigi Rizzo 
1114ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
111537e3a6d3SLuigi Rizzo 	netmap_priv_delete(priv);
1116ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1117ce3ee1e7SLuigi Rizzo }
111868b8534bSLuigi Rizzo 
1119f18be576SLuigi Rizzo 
112068b8534bSLuigi Rizzo /*
1121c3e9b4dbSLuiz Otavio O Souza  * Handlers for synchronization of the rings from/to the host stack.
1122c3e9b4dbSLuiz Otavio O Souza  * These are associated to a network interface and are just another
1123c3e9b4dbSLuiz Otavio O Souza  * ring pair managed by userspace.
1124c3e9b4dbSLuiz Otavio O Souza  *
1125c3e9b4dbSLuiz Otavio O Souza  * Netmap also supports transparent forwarding (NS_FORWARD and NR_FORWARD
1126c3e9b4dbSLuiz Otavio O Souza  * flags):
1127c3e9b4dbSLuiz Otavio O Souza  *
1128c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on hw RX rings, the application can mark
1129c3e9b4dbSLuiz Otavio O Souza  *   them with the NS_FORWARD flag. During the next RXSYNC or poll(), they
1130c3e9b4dbSLuiz Otavio O Souza  *   will be forwarded to the host stack, similarly to what happened if
1131c3e9b4dbSLuiz Otavio O Souza  *   the application moved them to the host TX ring.
1132c3e9b4dbSLuiz Otavio O Souza  *
1133c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on the host RX ring, the application can
1134c3e9b4dbSLuiz Otavio O Souza  *   mark them with the NS_FORWARD flag. During the next RXSYNC or poll(),
1135c3e9b4dbSLuiz Otavio O Souza  *   they will be forwarded to the hw TX rings, saving the application
1136c3e9b4dbSLuiz Otavio O Souza  *   from doing the same task in user-space.
1137c3e9b4dbSLuiz Otavio O Souza  *
1138c3e9b4dbSLuiz Otavio O Souza  * Transparent fowarding can be enabled per-ring, by setting the NR_FORWARD
1139c3e9b4dbSLuiz Otavio O Souza  * flag, or globally with the netmap_fwd sysctl.
1140c3e9b4dbSLuiz Otavio O Souza  *
1141091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1142091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1143091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1144091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1145091fd0abSLuigi Rizzo  */
1146091fd0abSLuigi Rizzo 
1147f18be576SLuigi Rizzo 
1148091fd0abSLuigi Rizzo /*
1149c3e9b4dbSLuiz Otavio O Souza  * Pass a whole queue of mbufs to the host stack as coming from 'dst'
115017885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1151c3e9b4dbSLuiz Otavio O Souza  * After this call the queue is empty.
1152091fd0abSLuigi Rizzo  */
1153091fd0abSLuigi Rizzo static void
1154f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1155091fd0abSLuigi Rizzo {
1156091fd0abSLuigi Rizzo 	struct mbuf *m;
115737e3a6d3SLuigi Rizzo 	struct mbuf *head = NULL, *prev = NULL;
1158b7d69138SVincenzo Maffione #ifdef __FreeBSD__
1159b7d69138SVincenzo Maffione 	struct epoch_tracker et;
1160091fd0abSLuigi Rizzo 
1161a4470078SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1162b7d69138SVincenzo Maffione #endif /* __FreeBSD__ */
1163c3e9b4dbSLuiz Otavio O Souza 	/* Send packets up, outside the lock; head/prev machinery
1164c3e9b4dbSLuiz Otavio O Souza 	 * is only useful for Windows. */
1165f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1166b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_HOST)
1167b6e66be2SVincenzo Maffione 			nm_prinf("sending up pkt %p size %d", m, MBUF_LEN(m));
116837e3a6d3SLuigi Rizzo 		prev = nm_os_send_up(dst, m, prev);
116937e3a6d3SLuigi Rizzo 		if (head == NULL)
117037e3a6d3SLuigi Rizzo 			head = prev;
1171091fd0abSLuigi Rizzo 	}
117237e3a6d3SLuigi Rizzo 	if (head)
117337e3a6d3SLuigi Rizzo 		nm_os_send_up(dst, NULL, head);
1174b7d69138SVincenzo Maffione #ifdef __FreeBSD__
1175a4470078SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1176b7d69138SVincenzo Maffione #endif /* __FreeBSD__ */
117737e3a6d3SLuigi Rizzo 	mbq_fini(q);
1178091fd0abSLuigi Rizzo }
1179091fd0abSLuigi Rizzo 
1180f18be576SLuigi Rizzo 
1181091fd0abSLuigi Rizzo /*
1182c3e9b4dbSLuiz Otavio O Souza  * Scan the buffers from hwcur to ring->head, and put a copy of those
1183c3e9b4dbSLuiz Otavio O Souza  * marked NS_FORWARD (or all of them if forced) into a queue of mbufs.
1184c3e9b4dbSLuiz Otavio O Souza  * Drop remaining packets in the unlikely event
118517885a7bSLuigi Rizzo  * of an mbuf shortage.
1186091fd0abSLuigi Rizzo  */
1187091fd0abSLuigi Rizzo static void
1188091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1189091fd0abSLuigi Rizzo {
119017885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1191847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
119217885a7bSLuigi Rizzo 	u_int n;
1193f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1194091fd0abSLuigi Rizzo 
119517885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
119617885a7bSLuigi Rizzo 		struct mbuf *m;
1197091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1198091fd0abSLuigi Rizzo 
1199091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1200091fd0abSLuigi Rizzo 			continue;
12014bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
120275f4f3edSVincenzo Maffione 			nm_prlim(5, "bad pkt at %d len %d", n, slot->len);
1203091fd0abSLuigi Rizzo 			continue;
1204091fd0abSLuigi Rizzo 		}
1205091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
120617885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
12074bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1208091fd0abSLuigi Rizzo 
1209091fd0abSLuigi Rizzo 		if (m == NULL)
1210091fd0abSLuigi Rizzo 			break;
1211f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1212091fd0abSLuigi Rizzo 	}
1213091fd0abSLuigi Rizzo }
1214091fd0abSLuigi Rizzo 
121537e3a6d3SLuigi Rizzo static inline int
121637e3a6d3SLuigi Rizzo _nm_may_forward(struct netmap_kring *kring)
121737e3a6d3SLuigi Rizzo {
121837e3a6d3SLuigi Rizzo 	return	((netmap_fwd || kring->ring->flags & NR_FORWARD) &&
121937e3a6d3SLuigi Rizzo 		 kring->na->na_flags & NAF_HOST_RINGS &&
122037e3a6d3SLuigi Rizzo 		 kring->tx == NR_RX);
122137e3a6d3SLuigi Rizzo }
122237e3a6d3SLuigi Rizzo 
122337e3a6d3SLuigi Rizzo static inline int
122437e3a6d3SLuigi Rizzo nm_may_forward_up(struct netmap_kring *kring)
122537e3a6d3SLuigi Rizzo {
122637e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
122737e3a6d3SLuigi Rizzo 		 kring->ring_id != kring->na->num_rx_rings;
122837e3a6d3SLuigi Rizzo }
122937e3a6d3SLuigi Rizzo 
123037e3a6d3SLuigi Rizzo static inline int
1231c3e9b4dbSLuiz Otavio O Souza nm_may_forward_down(struct netmap_kring *kring, int sync_flags)
123237e3a6d3SLuigi Rizzo {
123337e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
1234c3e9b4dbSLuiz Otavio O Souza 		 (sync_flags & NAF_CAN_FORWARD_DOWN) &&
123537e3a6d3SLuigi Rizzo 		 kring->ring_id == kring->na->num_rx_rings;
123637e3a6d3SLuigi Rizzo }
1237f18be576SLuigi Rizzo 
1238091fd0abSLuigi Rizzo /*
123917885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
1240c3e9b4dbSLuiz Otavio O Souza  * kring->nr_hwcur and kring->rhead.
1241c3e9b4dbSLuiz Otavio O Souza  * Called under kring->rx_queue.lock on the sw rx ring.
1242c3e9b4dbSLuiz Otavio O Souza  *
1243c3e9b4dbSLuiz Otavio O Souza  * It can only be called if the user opened all the TX hw rings,
1244c3e9b4dbSLuiz Otavio O Souza  * see NAF_CAN_FORWARD_DOWN flag.
1245c3e9b4dbSLuiz Otavio O Souza  * We can touch the TX netmap rings (slots, head and cur) since
1246c3e9b4dbSLuiz Otavio O Souza  * we are in poll/ioctl system call context, and the application
1247c3e9b4dbSLuiz Otavio O Souza  * is not supposed to touch the ring (using a different thread)
1248c3e9b4dbSLuiz Otavio O Souza  * during the execution of the system call.
1249091fd0abSLuigi Rizzo  */
125017885a7bSLuigi Rizzo static u_int
1251091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1252091fd0abSLuigi Rizzo {
12532ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[na->num_rx_rings];
125417885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
125517885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
125617885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
125717885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
125817885a7bSLuigi Rizzo 	u_int sent = 0;
1259ce3ee1e7SLuigi Rizzo 
126017885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
126117885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
12622ff91c17SVincenzo Maffione 		struct netmap_kring *kdst = na->tx_rings[i];
126317885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
126417885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1265ce3ee1e7SLuigi Rizzo 
126617885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
126717885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
126817885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1269091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
127037e3a6d3SLuigi Rizzo 			u_int dst_head = rdst->head;
127117885a7bSLuigi Rizzo 
127217885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
127317885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
127417885a7bSLuigi Rizzo 				continue;
127517885a7bSLuigi Rizzo 
127617885a7bSLuigi Rizzo 			sent++;
127717885a7bSLuigi Rizzo 
127837e3a6d3SLuigi Rizzo 			dst = &rdst->slot[dst_head];
127917885a7bSLuigi Rizzo 
1280091fd0abSLuigi Rizzo 			tmp = *src;
128117885a7bSLuigi Rizzo 
1282091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1283091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1284091fd0abSLuigi Rizzo 
1285091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1286091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1287091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1288091fd0abSLuigi Rizzo 
128937e3a6d3SLuigi Rizzo 			rdst->head = rdst->cur = nm_next(dst_head, dst_lim);
1290091fd0abSLuigi Rizzo 		}
1291c3e9b4dbSLuiz Otavio O Souza 		/* if (sent) XXX txsync ? it would be just an optimization */
1292091fd0abSLuigi Rizzo 	}
129317885a7bSLuigi Rizzo 	return sent;
1294091fd0abSLuigi Rizzo }
1295091fd0abSLuigi Rizzo 
1296f18be576SLuigi Rizzo 
1297091fd0abSLuigi Rizzo /*
1298ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
129902ad4083SLuigi Rizzo  * system call in user process context, and the only contention
130002ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1301091fd0abSLuigi Rizzo  * this routine concurrently.
130268b8534bSLuigi Rizzo  */
130337e3a6d3SLuigi Rizzo static int
130437e3a6d3SLuigi Rizzo netmap_txsync_to_host(struct netmap_kring *kring, int flags)
130568b8534bSLuigi Rizzo {
130637e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
130717885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1308f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1309f9790aebSLuigi Rizzo 	struct mbq q;
131068b8534bSLuigi Rizzo 
131117885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
1312c3e9b4dbSLuiz Otavio O Souza 	 * Force hwcur = head since netmap_grab_packets() stops at head
131368b8534bSLuigi Rizzo 	 */
1314f9790aebSLuigi Rizzo 	mbq_init(&q);
131517885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
131675f4f3edSVincenzo Maffione 	nm_prdis("have %d pkts in queue", mbq_len(&q));
131717885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
131817885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
131917885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
132017885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
132168b8534bSLuigi Rizzo 
1322f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
132337e3a6d3SLuigi Rizzo 	return 0;
1324f18be576SLuigi Rizzo }
1325f18be576SLuigi Rizzo 
1326f18be576SLuigi Rizzo 
132768b8534bSLuigi Rizzo /*
132802ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
132917885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
133017885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
133102ad4083SLuigi Rizzo  *
1332c3e9b4dbSLuiz Otavio O Souza  * also moves to the nic hw rings any packet the user has marked
1333c3e9b4dbSLuiz Otavio O Souza  * for transparent-mode forwarding, then sets the NR_FORWARD
1334c3e9b4dbSLuiz Otavio O Souza  * flag in the kring to let the caller push them out
133568b8534bSLuigi Rizzo  */
13368fd44c93SLuigi Rizzo static int
133737e3a6d3SLuigi Rizzo netmap_rxsync_from_host(struct netmap_kring *kring, int flags)
133868b8534bSLuigi Rizzo {
133937e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
134068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
134117885a7bSLuigi Rizzo 	u_int nm_i, n;
134217885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1343f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
134417885a7bSLuigi Rizzo 	int ret = 0;
1345847bf383SLuigi Rizzo 	struct mbq *q = &kring->rx_queue, fq;
134668b8534bSLuigi Rizzo 
1347847bf383SLuigi Rizzo 	mbq_init(&fq); /* fq holds packets to be freed */
1348847bf383SLuigi Rizzo 
1349997b054cSLuigi Rizzo 	mbq_lock(q);
135017885a7bSLuigi Rizzo 
135117885a7bSLuigi Rizzo 	/* First part: import newly received packets */
135217885a7bSLuigi Rizzo 	n = mbq_len(q);
135317885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
135417885a7bSLuigi Rizzo 		struct mbuf *m;
135517885a7bSLuigi Rizzo 		uint32_t stop_i;
135617885a7bSLuigi Rizzo 
135717885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
1358c3e9b4dbSLuiz Otavio O Souza 		stop_i = nm_prev(kring->nr_hwcur, lim);
135917885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
136017885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
136117885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
136217885a7bSLuigi Rizzo 
13634bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
136475f4f3edSVincenzo Maffione 			nm_prdis("nm %d len %d", nm_i, len);
1365b6e66be2SVincenzo Maffione 			if (netmap_debug & NM_DEBUG_HOST)
1366b6e66be2SVincenzo Maffione 				nm_prinf("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
136717885a7bSLuigi Rizzo 
136817885a7bSLuigi Rizzo 			slot->len = len;
13694f80b14cSVincenzo Maffione 			slot->flags = 0;
137017885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
1371847bf383SLuigi Rizzo 			mbq_enqueue(&fq, m);
137264ae02c3SLuigi Rizzo 		}
137317885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
137464ae02c3SLuigi Rizzo 	}
137517885a7bSLuigi Rizzo 
137617885a7bSLuigi Rizzo 	/*
137717885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
137817885a7bSLuigi Rizzo 	 */
137917885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
138017885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
1381c3e9b4dbSLuiz Otavio O Souza 		if (nm_may_forward_down(kring, flags)) {
138217885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
138337e3a6d3SLuigi Rizzo 			if (ret > 0) {
138437e3a6d3SLuigi Rizzo 				kring->nr_kflags |= NR_FORWARD;
138537e3a6d3SLuigi Rizzo 				ret = 0;
138637e3a6d3SLuigi Rizzo 			}
138737e3a6d3SLuigi Rizzo 		}
138817885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
138964ae02c3SLuigi Rizzo 	}
139017885a7bSLuigi Rizzo 
1391997b054cSLuigi Rizzo 	mbq_unlock(q);
1392847bf383SLuigi Rizzo 
1393847bf383SLuigi Rizzo 	mbq_purge(&fq);
139437e3a6d3SLuigi Rizzo 	mbq_fini(&fq);
1395847bf383SLuigi Rizzo 
139617885a7bSLuigi Rizzo 	return ret;
139768b8534bSLuigi Rizzo }
139868b8534bSLuigi Rizzo 
139968b8534bSLuigi Rizzo 
1400f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1401f9790aebSLuigi Rizzo  *
1402f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1403f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1404f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1405f9790aebSLuigi Rizzo  *
1406f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1407f9790aebSLuigi Rizzo  * EBUSY.
1408f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1409f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1410f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1411f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1412f9790aebSLuigi Rizzo  *
1413f9790aebSLuigi Rizzo  *					native_support
1414f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1415f9790aebSLuigi Rizzo  * -------------------------------------------------------
1416f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1417f9790aebSLuigi Rizzo  *
1418f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1419f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1420f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1421f9790aebSLuigi Rizzo  *
1422f9790aebSLuigi Rizzo  */
142337e3a6d3SLuigi Rizzo static void netmap_hw_dtor(struct netmap_adapter *); /* needed by NM_IS_NATIVE() */
1424f9790aebSLuigi Rizzo int
1425c3e9b4dbSLuiz Otavio O Souza netmap_get_hw_na(struct ifnet *ifp, struct netmap_mem_d *nmd, struct netmap_adapter **na)
1426f9790aebSLuigi Rizzo {
1427f9790aebSLuigi Rizzo 	/* generic support */
1428f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1429f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1430847bf383SLuigi Rizzo 	int error = 0;
1431f9790aebSLuigi Rizzo 
1432f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1433f9790aebSLuigi Rizzo 
1434f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1435f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1436f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1437f9790aebSLuigi Rizzo 
143837e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
14394bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1440f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1441f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1442f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1443f9790aebSLuigi Rizzo 		 * adapters.
1444f9790aebSLuigi Rizzo 		 */
14454bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
14464bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
14474bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
14484bf50f18SLuigi Rizzo #ifdef WITH_PIPES
14494bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
14504bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
14514bf50f18SLuigi Rizzo 			 */
14524bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
14534bf50f18SLuigi Rizzo #endif
14544bf50f18SLuigi Rizzo 		) {
14554bf50f18SLuigi Rizzo 			*na = prev_na;
1456c3e9b4dbSLuiz Otavio O Souza 			goto assign_mem;
1457f9790aebSLuigi Rizzo 		}
1458f9790aebSLuigi Rizzo 	}
1459f9790aebSLuigi Rizzo 
1460f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1461f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1462f9790aebSLuigi Rizzo 	 */
146337e3a6d3SLuigi Rizzo 	if (!NM_IS_NATIVE(ifp) && i == NETMAP_ADMODE_NATIVE)
1464f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1465f9790aebSLuigi Rizzo 
1466f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1467f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1468f9790aebSLuigi Rizzo 	 *
1469f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1470f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1471f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1472f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1473f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1474f9790aebSLuigi Rizzo 	 * must be that
1475f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1476f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1477f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1478f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1479f9790aebSLuigi Rizzo 	 */
1480f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1481f9790aebSLuigi Rizzo 	if (error)
1482f9790aebSLuigi Rizzo 		return error;
1483f9790aebSLuigi Rizzo 
1484f9790aebSLuigi Rizzo 	*na = NA(ifp);
1485c3e9b4dbSLuiz Otavio O Souza 
1486c3e9b4dbSLuiz Otavio O Souza assign_mem:
1487c3e9b4dbSLuiz Otavio O Souza 	if (nmd != NULL && !((*na)->na_flags & NAF_MEM_OWNER) &&
1488c3e9b4dbSLuiz Otavio O Souza 	    (*na)->active_fds == 0 && ((*na)->nm_mem != nmd)) {
14894f80b14cSVincenzo Maffione 		(*na)->nm_mem_prev = (*na)->nm_mem;
1490c3e9b4dbSLuiz Otavio O Souza 		(*na)->nm_mem = netmap_mem_get(nmd);
1491f9790aebSLuigi Rizzo 	}
1492f9790aebSLuigi Rizzo 
1493c3e9b4dbSLuiz Otavio O Souza 	return 0;
1494c3e9b4dbSLuiz Otavio O Souza }
1495f9790aebSLuigi Rizzo 
149668b8534bSLuigi Rizzo /*
1497ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1498ce3ee1e7SLuigi Rizzo  *
1499f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
15002ff91c17SVincenzo Maffione  * to the interface specified by req.
1501ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1502ce3ee1e7SLuigi Rizzo  *
1503f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1504f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1505f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1506f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1507f2637526SLuigi Rizzo  * could not be allocated.
1508f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1509f18be576SLuigi Rizzo  *
15102ff91c17SVincenzo Maffione  * If the interface specified by req is a system one, also keep
151137e3a6d3SLuigi Rizzo  * a reference to it and return a valid *ifp.
151268b8534bSLuigi Rizzo  */
1513f9790aebSLuigi Rizzo int
15142ff91c17SVincenzo Maffione netmap_get_na(struct nmreq_header *hdr,
15152ff91c17SVincenzo Maffione 	      struct netmap_adapter **na, struct ifnet **ifp,
15162ff91c17SVincenzo Maffione 	      struct netmap_mem_d *nmd, int create)
151768b8534bSLuigi Rizzo {
1518cfa866f6SMatt Macy 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
1519f9790aebSLuigi Rizzo 	int error = 0;
1520f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1521c3e9b4dbSLuiz Otavio O Souza 	int nmd_ref = 0;
1522f9790aebSLuigi Rizzo 
1523f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
152437e3a6d3SLuigi Rizzo 	*ifp = NULL;
1525f196ce38SLuigi Rizzo 
15262ff91c17SVincenzo Maffione 	if (hdr->nr_reqtype != NETMAP_REQ_REGISTER) {
15272ff91c17SVincenzo Maffione 		return EINVAL;
15282ff91c17SVincenzo Maffione 	}
15292ff91c17SVincenzo Maffione 
15302ff91c17SVincenzo Maffione 	if (req->nr_mode == NR_REG_PIPE_MASTER ||
15312ff91c17SVincenzo Maffione 			req->nr_mode == NR_REG_PIPE_SLAVE) {
15322ff91c17SVincenzo Maffione 		/* Do not accept deprecated pipe modes. */
1533b6e66be2SVincenzo Maffione 		nm_prerr("Deprecated pipe nr_mode, use xx{yy or xx}yy syntax");
15342ff91c17SVincenzo Maffione 		return EINVAL;
15352ff91c17SVincenzo Maffione 	}
15362ff91c17SVincenzo Maffione 
1537ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1538ce3ee1e7SLuigi Rizzo 
1539c3e9b4dbSLuiz Otavio O Souza 	/* if the request contain a memid, try to find the
1540c3e9b4dbSLuiz Otavio O Souza 	 * corresponding memory region
1541c3e9b4dbSLuiz Otavio O Souza 	 */
15422ff91c17SVincenzo Maffione 	if (nmd == NULL && req->nr_mem_id) {
15432ff91c17SVincenzo Maffione 		nmd = netmap_mem_find(req->nr_mem_id);
1544c3e9b4dbSLuiz Otavio O Souza 		if (nmd == NULL)
1545c3e9b4dbSLuiz Otavio O Souza 			return EINVAL;
1546c3e9b4dbSLuiz Otavio O Souza 		/* keep the rereference */
1547c3e9b4dbSLuiz Otavio O Souza 		nmd_ref = 1;
1548c3e9b4dbSLuiz Otavio O Souza 	}
1549c3e9b4dbSLuiz Otavio O Souza 
155037e3a6d3SLuigi Rizzo 	/* We cascade through all possible types of netmap adapter.
15514bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
15524bf50f18SLuigi Rizzo 	 * with the following combinations:
15534bf50f18SLuigi Rizzo 	 *
15544bf50f18SLuigi Rizzo 	 * error    na
15554bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
15564bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
15574bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
15584bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
15594bf50f18SLuigi Rizzo 	 */
1560b6e66be2SVincenzo Maffione 	error = netmap_get_null_na(hdr, na, nmd, create);
156137e3a6d3SLuigi Rizzo 	if (error || *na != NULL)
1562c3e9b4dbSLuiz Otavio O Souza 		goto out;
156337e3a6d3SLuigi Rizzo 
15644bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
15652ff91c17SVincenzo Maffione 	error = netmap_get_monitor_na(hdr, na, nmd, create);
15664bf50f18SLuigi Rizzo 	if (error || *na != NULL)
1567c3e9b4dbSLuiz Otavio O Souza 		goto out;
15684bf50f18SLuigi Rizzo 
15694bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
15702ff91c17SVincenzo Maffione 	error = netmap_get_pipe_na(hdr, na, nmd, create);
1571f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1572c3e9b4dbSLuiz Otavio O Souza 		goto out;
1573ce3ee1e7SLuigi Rizzo 
15744bf50f18SLuigi Rizzo 	/* try to see if this is a bridge port */
15752a7db7a6SVincenzo Maffione 	error = netmap_get_vale_na(hdr, na, nmd, create);
1576f0ea3689SLuigi Rizzo 	if (error)
1577c3e9b4dbSLuiz Otavio O Souza 		goto out;
1578f0ea3689SLuigi Rizzo 
1579f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1580847bf383SLuigi Rizzo 		goto out;
1581f0ea3689SLuigi Rizzo 
158289cc2556SLuigi Rizzo 	/*
158389cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
158489cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
158589cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
158689cc2556SLuigi Rizzo 	 * persistent VALE port.
158789cc2556SLuigi Rizzo 	 */
15882ff91c17SVincenzo Maffione 	*ifp = ifunit_ref(hdr->nr_name);
158937e3a6d3SLuigi Rizzo 	if (*ifp == NULL) {
1590c3e9b4dbSLuiz Otavio O Souza 		error = ENXIO;
1591c3e9b4dbSLuiz Otavio O Souza 		goto out;
1592f196ce38SLuigi Rizzo 	}
1593ce3ee1e7SLuigi Rizzo 
1594c3e9b4dbSLuiz Otavio O Souza 	error = netmap_get_hw_na(*ifp, nmd, &ret);
1595f9790aebSLuigi Rizzo 	if (error)
1596f9790aebSLuigi Rizzo 		goto out;
1597f18be576SLuigi Rizzo 
1598f9790aebSLuigi Rizzo 	*na = ret;
1599f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1600f0ea3689SLuigi Rizzo 
1601d12354a5SVincenzo Maffione 	/*
1602d12354a5SVincenzo Maffione 	 * if the adapter supports the host rings and it is not alread open,
1603d12354a5SVincenzo Maffione 	 * try to set the number of host rings as requested by the user
1604d12354a5SVincenzo Maffione 	 */
1605d12354a5SVincenzo Maffione 	if (((*na)->na_flags & NAF_HOST_RINGS) && (*na)->active_fds == 0) {
1606d12354a5SVincenzo Maffione 		if (req->nr_host_tx_rings)
1607d12354a5SVincenzo Maffione 			(*na)->num_host_tx_rings = req->nr_host_tx_rings;
1608d12354a5SVincenzo Maffione 		if (req->nr_host_rx_rings)
1609d12354a5SVincenzo Maffione 			(*na)->num_host_rx_rings = req->nr_host_rx_rings;
1610d12354a5SVincenzo Maffione 	}
1611d12354a5SVincenzo Maffione 	nm_prdis("%s: host tx %d rx %u", (*na)->name, (*na)->num_host_tx_rings,
1612d12354a5SVincenzo Maffione 			(*na)->num_host_rx_rings);
1613d12354a5SVincenzo Maffione 
1614f9790aebSLuigi Rizzo out:
161537e3a6d3SLuigi Rizzo 	if (error) {
161637e3a6d3SLuigi Rizzo 		if (ret)
1617f0ea3689SLuigi Rizzo 			netmap_adapter_put(ret);
161837e3a6d3SLuigi Rizzo 		if (*ifp) {
161937e3a6d3SLuigi Rizzo 			if_rele(*ifp);
162037e3a6d3SLuigi Rizzo 			*ifp = NULL;
162137e3a6d3SLuigi Rizzo 		}
162237e3a6d3SLuigi Rizzo 	}
1623c3e9b4dbSLuiz Otavio O Souza 	if (nmd_ref)
1624c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_put(nmd);
1625f18be576SLuigi Rizzo 
16265ab0d24dSLuigi Rizzo 	return error;
16275ab0d24dSLuigi Rizzo }
1628ce3ee1e7SLuigi Rizzo 
162937e3a6d3SLuigi Rizzo /* undo netmap_get_na() */
163037e3a6d3SLuigi Rizzo void
163137e3a6d3SLuigi Rizzo netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp)
163237e3a6d3SLuigi Rizzo {
163337e3a6d3SLuigi Rizzo 	if (ifp)
163437e3a6d3SLuigi Rizzo 		if_rele(ifp);
163537e3a6d3SLuigi Rizzo 	if (na)
163637e3a6d3SLuigi Rizzo 		netmap_adapter_put(na);
163737e3a6d3SLuigi Rizzo }
163837e3a6d3SLuigi Rizzo 
163937e3a6d3SLuigi Rizzo 
164037e3a6d3SLuigi Rizzo #define NM_FAIL_ON(t) do {						\
164137e3a6d3SLuigi Rizzo 	if (unlikely(t)) {						\
164275f4f3edSVincenzo Maffione 		nm_prlim(5, "%s: fail '" #t "' "				\
164337e3a6d3SLuigi Rizzo 			"h %d c %d t %d "				\
164437e3a6d3SLuigi Rizzo 			"rh %d rc %d rt %d "				\
164537e3a6d3SLuigi Rizzo 			"hc %d ht %d",					\
164637e3a6d3SLuigi Rizzo 			kring->name,					\
164737e3a6d3SLuigi Rizzo 			head, cur, ring->tail,				\
164837e3a6d3SLuigi Rizzo 			kring->rhead, kring->rcur, kring->rtail,	\
164937e3a6d3SLuigi Rizzo 			kring->nr_hwcur, kring->nr_hwtail);		\
165037e3a6d3SLuigi Rizzo 		return kring->nkr_num_slots;				\
165137e3a6d3SLuigi Rizzo 	}								\
165237e3a6d3SLuigi Rizzo } while (0)
1653ce3ee1e7SLuigi Rizzo 
1654f9790aebSLuigi Rizzo /*
1655f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1656f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
165717885a7bSLuigi Rizzo  * in case of error.
1658f9790aebSLuigi Rizzo  *
165917885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
166017885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1661f9790aebSLuigi Rizzo  *
166217885a7bSLuigi Rizzo  * We want
166317885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1664f9790aebSLuigi Rizzo  *
166517885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1666f9790aebSLuigi Rizzo  */
166737e3a6d3SLuigi Rizzo u_int
166837e3a6d3SLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1669f9790aebSLuigi Rizzo {
167017885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1671f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1672f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1673ce3ee1e7SLuigi Rizzo 
167475f4f3edSVincenzo Maffione 	nm_prdis(5, "%s kcur %d ktail %d head %d cur %d tail %d",
167517885a7bSLuigi Rizzo 		kring->name,
167617885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
167717885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
167817885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
167937e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->rhead >= n ||
168037e3a6d3SLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n);
1681f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
168217885a7bSLuigi Rizzo 	/*
168337e3a6d3SLuigi Rizzo 	 * user sanity checks. We only use head,
168437e3a6d3SLuigi Rizzo 	 * A, B, ... are possible positions for head:
168517885a7bSLuigi Rizzo 	 *
168637e3a6d3SLuigi Rizzo 	 *  0    A  rhead   B  rtail   C  n-1
168737e3a6d3SLuigi Rizzo 	 *  0    D  rtail   E  rhead   F  n-1
168817885a7bSLuigi Rizzo 	 *
168917885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
169017885a7bSLuigi Rizzo 	 */
169117885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
169217885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
169337e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->rhead || head > kring->rtail);
169417885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
169537e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->rtail);
169617885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
169717885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
169837e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head > kring->rtail && head < kring->rhead);
169917885a7bSLuigi Rizzo 
170017885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
170117885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
170217885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
170337e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->rtail);
170417885a7bSLuigi Rizzo 		} else { /* head >= rhead */
170517885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
170637e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur > kring->rtail && cur < head);
1707f18be576SLuigi Rizzo 		}
1708f9790aebSLuigi Rizzo 	}
170917885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
171075f4f3edSVincenzo Maffione 		nm_prlim(5, "%s tail overwritten was %d need %d", kring->name,
171117885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
171217885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
171317885a7bSLuigi Rizzo 	}
171417885a7bSLuigi Rizzo 	kring->rhead = head;
171517885a7bSLuigi Rizzo 	kring->rcur = cur;
171617885a7bSLuigi Rizzo 	return head;
171768b8534bSLuigi Rizzo }
171868b8534bSLuigi Rizzo 
171968b8534bSLuigi Rizzo 
172068b8534bSLuigi Rizzo /*
1721f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
172217885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1723f9790aebSLuigi Rizzo  *
172417885a7bSLuigi Rizzo  * For a valid configuration,
172517885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1726f9790aebSLuigi Rizzo  *
172717885a7bSLuigi Rizzo  * We only consider head and cur.
172817885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1729f9790aebSLuigi Rizzo  *
1730f9790aebSLuigi Rizzo  */
173137e3a6d3SLuigi Rizzo u_int
173237e3a6d3SLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1733f9790aebSLuigi Rizzo {
173417885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
173517885a7bSLuigi Rizzo 	uint32_t head, cur;
1736f9790aebSLuigi Rizzo 
173775f4f3edSVincenzo Maffione 	nm_prdis(5,"%s kc %d kt %d h %d c %d t %d",
173817885a7bSLuigi Rizzo 		kring->name,
173917885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
174017885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
174117885a7bSLuigi Rizzo 	/*
174217885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
174317885a7bSLuigi Rizzo 	 * move backwards. However:
174417885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
174517885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
174617885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
174717885a7bSLuigi Rizzo 	 */
174817885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
174917885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1750f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
175137e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->nr_hwtail >= n);
1752f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1753f9790aebSLuigi Rizzo 	/* user sanity checks */
175417885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
175517885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
175637e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur || head > kring->nr_hwtail);
175717885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
175837e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
1759f9790aebSLuigi Rizzo 	} else {
176017885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
176137e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur && head > kring->nr_hwtail);
176217885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
176317885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
176417885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
176537e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
176617885a7bSLuigi Rizzo 		} else {
176717885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
176837e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head && cur > kring->nr_hwtail);
1769f9790aebSLuigi Rizzo 		}
1770f9790aebSLuigi Rizzo 	}
177117885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
177275f4f3edSVincenzo Maffione 		nm_prlim(5, "%s tail overwritten was %d need %d",
177317885a7bSLuigi Rizzo 			kring->name,
177417885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
177517885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
177617885a7bSLuigi Rizzo 	}
177717885a7bSLuigi Rizzo 	return head;
1778f9790aebSLuigi Rizzo }
1779f9790aebSLuigi Rizzo 
178017885a7bSLuigi Rizzo 
1781f9790aebSLuigi Rizzo /*
178268b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
178317885a7bSLuigi Rizzo  * Can't do much more than resetting head = cur = hwcur, tail = hwtail
178468b8534bSLuigi Rizzo  * Return 1 on reinit.
1785506cc70cSLuigi Rizzo  *
1786506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1787506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
178817885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1789506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1790506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1791506cc70cSLuigi Rizzo  * it under lock.
179268b8534bSLuigi Rizzo  */
179368b8534bSLuigi Rizzo int
179468b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
179568b8534bSLuigi Rizzo {
179668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
179768b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
179868b8534bSLuigi Rizzo 	int errors = 0;
179968b8534bSLuigi Rizzo 
1800ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
180175f4f3edSVincenzo Maffione 	nm_prlim(10, "called for %s", kring->name);
180217885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
180317885a7bSLuigi Rizzo 	kring->rhead = ring->head;
180417885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
180517885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
180617885a7bSLuigi Rizzo 
180768b8534bSLuigi Rizzo 	if (ring->cur > lim)
180868b8534bSLuigi Rizzo 		errors++;
180917885a7bSLuigi Rizzo 	if (ring->head > lim)
181017885a7bSLuigi Rizzo 		errors++;
181117885a7bSLuigi Rizzo 	if (ring->tail > lim)
181217885a7bSLuigi Rizzo 		errors++;
181368b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
181468b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
181568b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
1816847bf383SLuigi Rizzo 		if (idx < 2 || idx >= kring->na->na_lut.objtotal) {
181775f4f3edSVincenzo Maffione 			nm_prlim(5, "bad index at slot %d idx %d len %d ", i, idx, len);
181868b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
181968b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
18204bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
182168b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
182275f4f3edSVincenzo Maffione 			nm_prlim(5, "bad len at slot %d idx %d len %d", i, idx, len);
182368b8534bSLuigi Rizzo 		}
182468b8534bSLuigi Rizzo 	}
182568b8534bSLuigi Rizzo 	if (errors) {
182675f4f3edSVincenzo Maffione 		nm_prlim(10, "total %d errors", errors);
182775f4f3edSVincenzo Maffione 		nm_prlim(10, "%s reinit, cur %d -> %d tail %d -> %d",
182817885a7bSLuigi Rizzo 			kring->name,
182968b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
183017885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
183117885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
183217885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
183317885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
183468b8534bSLuigi Rizzo 	}
183568b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
183668b8534bSLuigi Rizzo }
183768b8534bSLuigi Rizzo 
18384bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
18394bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
18404bf50f18SLuigi Rizzo  *
18414bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
18424bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
18434bf50f18SLuigi Rizzo  *
184468b8534bSLuigi Rizzo  */
18454bf50f18SLuigi Rizzo int
18462ff91c17SVincenzo Maffione netmap_interp_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
18472ff91c17SVincenzo Maffione 			uint16_t nr_ringid, uint64_t nr_flags)
184868b8534bSLuigi Rizzo {
1849f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
185037e3a6d3SLuigi Rizzo 	int excluded_direction[] = { NR_TX_RINGS_ONLY, NR_RX_RINGS_ONLY };
1851847bf383SLuigi Rizzo 	enum txrx t;
18522ff91c17SVincenzo Maffione 	u_int j;
185368b8534bSLuigi Rizzo 
185437e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
18552ff91c17SVincenzo Maffione 		if (nr_flags & excluded_direction[t]) {
185637e3a6d3SLuigi Rizzo 			priv->np_qfirst[t] = priv->np_qlast[t] = 0;
185737e3a6d3SLuigi Rizzo 			continue;
185837e3a6d3SLuigi Rizzo 		}
18592ff91c17SVincenzo Maffione 		switch (nr_mode) {
1860f0ea3689SLuigi Rizzo 		case NR_REG_ALL_NIC:
1861b6e66be2SVincenzo Maffione 		case NR_REG_NULL:
1862847bf383SLuigi Rizzo 			priv->np_qfirst[t] = 0;
1863847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t);
186475f4f3edSVincenzo Maffione 			nm_prdis("ALL/PIPE: %s %d %d", nm_txrx2str(t),
186537e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1866f0ea3689SLuigi Rizzo 			break;
1867f0ea3689SLuigi Rizzo 		case NR_REG_SW:
1868f0ea3689SLuigi Rizzo 		case NR_REG_NIC_SW:
1869f0ea3689SLuigi Rizzo 			if (!(na->na_flags & NAF_HOST_RINGS)) {
1870b6e66be2SVincenzo Maffione 				nm_prerr("host rings not supported");
1871f0ea3689SLuigi Rizzo 				return EINVAL;
1872f0ea3689SLuigi Rizzo 			}
18732ff91c17SVincenzo Maffione 			priv->np_qfirst[t] = (nr_mode == NR_REG_SW ?
1874847bf383SLuigi Rizzo 				nma_get_nrings(na, t) : 0);
18752a7db7a6SVincenzo Maffione 			priv->np_qlast[t] = netmap_all_rings(na, t);
187675f4f3edSVincenzo Maffione 			nm_prdis("%s: %s %d %d", nr_mode == NR_REG_SW ? "SW" : "NIC+SW",
187737e3a6d3SLuigi Rizzo 				nm_txrx2str(t),
187837e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1879f0ea3689SLuigi Rizzo 			break;
1880f0ea3689SLuigi Rizzo 		case NR_REG_ONE_NIC:
18812ff91c17SVincenzo Maffione 			if (nr_ringid >= na->num_tx_rings &&
18822ff91c17SVincenzo Maffione 					nr_ringid >= na->num_rx_rings) {
1883b6e66be2SVincenzo Maffione 				nm_prerr("invalid ring id %d", nr_ringid);
1884f0ea3689SLuigi Rizzo 				return EINVAL;
1885f0ea3689SLuigi Rizzo 			}
1886f0ea3689SLuigi Rizzo 			/* if not enough rings, use the first one */
18872ff91c17SVincenzo Maffione 			j = nr_ringid;
1888847bf383SLuigi Rizzo 			if (j >= nma_get_nrings(na, t))
1889f0ea3689SLuigi Rizzo 				j = 0;
1890847bf383SLuigi Rizzo 			priv->np_qfirst[t] = j;
1891847bf383SLuigi Rizzo 			priv->np_qlast[t] = j + 1;
189275f4f3edSVincenzo Maffione 			nm_prdis("ONE_NIC: %s %d %d", nm_txrx2str(t),
189337e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1894f0ea3689SLuigi Rizzo 			break;
1895d12354a5SVincenzo Maffione 		case NR_REG_ONE_SW:
1896d12354a5SVincenzo Maffione 			if (!(na->na_flags & NAF_HOST_RINGS)) {
1897d12354a5SVincenzo Maffione 				nm_prerr("host rings not supported");
1898d12354a5SVincenzo Maffione 				return EINVAL;
1899d12354a5SVincenzo Maffione 			}
1900d12354a5SVincenzo Maffione 			if (nr_ringid >= na->num_host_tx_rings &&
1901d12354a5SVincenzo Maffione 					nr_ringid >= na->num_host_rx_rings) {
1902d12354a5SVincenzo Maffione 				nm_prerr("invalid ring id %d", nr_ringid);
1903d12354a5SVincenzo Maffione 				return EINVAL;
1904d12354a5SVincenzo Maffione 			}
1905d12354a5SVincenzo Maffione 			/* if not enough rings, use the first one */
1906d12354a5SVincenzo Maffione 			j = nr_ringid;
1907d12354a5SVincenzo Maffione 			if (j >= nma_get_host_nrings(na, t))
1908d12354a5SVincenzo Maffione 				j = 0;
1909d12354a5SVincenzo Maffione 			priv->np_qfirst[t] = nma_get_nrings(na, t) + j;
1910d12354a5SVincenzo Maffione 			priv->np_qlast[t] = nma_get_nrings(na, t) + j + 1;
1911d12354a5SVincenzo Maffione 			nm_prdis("ONE_SW: %s %d %d", nm_txrx2str(t),
1912d12354a5SVincenzo Maffione 				priv->np_qfirst[t], priv->np_qlast[t]);
1913d12354a5SVincenzo Maffione 			break;
1914f0ea3689SLuigi Rizzo 		default:
1915b6e66be2SVincenzo Maffione 			nm_prerr("invalid regif type %d", nr_mode);
1916f0ea3689SLuigi Rizzo 			return EINVAL;
191768b8534bSLuigi Rizzo 		}
191837e3a6d3SLuigi Rizzo 	}
1919b6e66be2SVincenzo Maffione 	priv->np_flags = nr_flags;
19204bf50f18SLuigi Rizzo 
1921c3e9b4dbSLuiz Otavio O Souza 	/* Allow transparent forwarding mode in the host --> nic
1922c3e9b4dbSLuiz Otavio O Souza 	 * direction only if all the TX hw rings have been opened. */
1923c3e9b4dbSLuiz Otavio O Souza 	if (priv->np_qfirst[NR_TX] == 0 &&
1924c3e9b4dbSLuiz Otavio O Souza 			priv->np_qlast[NR_TX] >= na->num_tx_rings) {
1925c3e9b4dbSLuiz Otavio O Souza 		priv->np_sync_flags |= NAF_CAN_FORWARD_DOWN;
1926c3e9b4dbSLuiz Otavio O Souza 	}
1927c3e9b4dbSLuiz Otavio O Souza 
1928ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1929b6e66be2SVincenzo Maffione 		nm_prinf("%s: tx [%d,%d) rx [%d,%d) id %d",
19304bf50f18SLuigi Rizzo 			na->name,
1931847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1932847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1933847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1934847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX],
19352ff91c17SVincenzo Maffione 			nr_ringid);
1936ae10d1afSLuigi Rizzo 	}
193768b8534bSLuigi Rizzo 	return 0;
193868b8534bSLuigi Rizzo }
193968b8534bSLuigi Rizzo 
19404bf50f18SLuigi Rizzo 
19414bf50f18SLuigi Rizzo /*
19424bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
19434bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
19444bf50f18SLuigi Rizzo  */
19454bf50f18SLuigi Rizzo static int
19462ff91c17SVincenzo Maffione netmap_set_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
19472ff91c17SVincenzo Maffione 		uint16_t nr_ringid, uint64_t nr_flags)
19484bf50f18SLuigi Rizzo {
19494bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
19504bf50f18SLuigi Rizzo 	int error;
1951847bf383SLuigi Rizzo 	enum txrx t;
19524bf50f18SLuigi Rizzo 
19532ff91c17SVincenzo Maffione 	error = netmap_interp_ringid(priv, nr_mode, nr_ringid, nr_flags);
19544bf50f18SLuigi Rizzo 	if (error) {
19554bf50f18SLuigi Rizzo 		return error;
19564bf50f18SLuigi Rizzo 	}
19574bf50f18SLuigi Rizzo 
19582ff91c17SVincenzo Maffione 	priv->np_txpoll = (nr_flags & NR_NO_TX_POLL) ? 0 : 1;
19594bf50f18SLuigi Rizzo 
19604bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
19614bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
19624bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
19634bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
19644bf50f18SLuigi Rizzo 	 */
1965847bf383SLuigi Rizzo 	for_rx_tx(t) {
1966847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1967847bf383SLuigi Rizzo 			na->si_users[t]++;
1968847bf383SLuigi Rizzo 	}
19694bf50f18SLuigi Rizzo 	return 0;
19704bf50f18SLuigi Rizzo }
19714bf50f18SLuigi Rizzo 
1972847bf383SLuigi Rizzo static void
1973847bf383SLuigi Rizzo netmap_unset_ringid(struct netmap_priv_d *priv)
1974847bf383SLuigi Rizzo {
1975847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1976847bf383SLuigi Rizzo 	enum txrx t;
1977847bf383SLuigi Rizzo 
1978847bf383SLuigi Rizzo 	for_rx_tx(t) {
1979847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1980847bf383SLuigi Rizzo 			na->si_users[t]--;
1981847bf383SLuigi Rizzo 		priv->np_qfirst[t] = priv->np_qlast[t] = 0;
1982847bf383SLuigi Rizzo 	}
1983847bf383SLuigi Rizzo 	priv->np_flags = 0;
1984847bf383SLuigi Rizzo 	priv->np_txpoll = 0;
1985b6e66be2SVincenzo Maffione 	priv->np_kloop_state = 0;
1986847bf383SLuigi Rizzo }
1987847bf383SLuigi Rizzo 
1988847bf383SLuigi Rizzo 
198937e3a6d3SLuigi Rizzo /* Set the nr_pending_mode for the requested rings.
199037e3a6d3SLuigi Rizzo  * If requested, also try to get exclusive access to the rings, provided
199137e3a6d3SLuigi Rizzo  * the rings we want to bind are not exclusively owned by a previous bind.
1992847bf383SLuigi Rizzo  */
1993847bf383SLuigi Rizzo static int
199437e3a6d3SLuigi Rizzo netmap_krings_get(struct netmap_priv_d *priv)
1995847bf383SLuigi Rizzo {
1996847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1997847bf383SLuigi Rizzo 	u_int i;
1998847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1999847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
2000847bf383SLuigi Rizzo 	enum txrx t;
2001847bf383SLuigi Rizzo 
2002b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_ON)
2003b6e66be2SVincenzo Maffione 		nm_prinf("%s: grabbing tx [%d, %d) rx [%d, %d)",
2004847bf383SLuigi Rizzo 			na->name,
2005847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
2006847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
2007847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
2008847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX]);
2009847bf383SLuigi Rizzo 
2010847bf383SLuigi Rizzo 	/* first round: check that all the requested rings
2011847bf383SLuigi Rizzo 	 * are neither alread exclusively owned, nor we
2012847bf383SLuigi Rizzo 	 * want exclusive ownership when they are already in use
2013847bf383SLuigi Rizzo 	 */
2014847bf383SLuigi Rizzo 	for_rx_tx(t) {
2015847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
20162ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
2017847bf383SLuigi Rizzo 			if ((kring->nr_kflags & NKR_EXCLUSIVE) ||
2018847bf383SLuigi Rizzo 			    (kring->users && excl))
2019847bf383SLuigi Rizzo 			{
202075f4f3edSVincenzo Maffione 				nm_prdis("ring %s busy", kring->name);
2021847bf383SLuigi Rizzo 				return EBUSY;
2022847bf383SLuigi Rizzo 			}
2023847bf383SLuigi Rizzo 		}
2024847bf383SLuigi Rizzo 	}
2025847bf383SLuigi Rizzo 
202637e3a6d3SLuigi Rizzo 	/* second round: increment usage count (possibly marking them
202737e3a6d3SLuigi Rizzo 	 * as exclusive) and set the nr_pending_mode
2028847bf383SLuigi Rizzo 	 */
2029847bf383SLuigi Rizzo 	for_rx_tx(t) {
2030847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
20312ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
2032847bf383SLuigi Rizzo 			kring->users++;
2033847bf383SLuigi Rizzo 			if (excl)
2034847bf383SLuigi Rizzo 				kring->nr_kflags |= NKR_EXCLUSIVE;
203537e3a6d3SLuigi Rizzo 	                kring->nr_pending_mode = NKR_NETMAP_ON;
2036847bf383SLuigi Rizzo 		}
2037847bf383SLuigi Rizzo 	}
2038847bf383SLuigi Rizzo 
2039847bf383SLuigi Rizzo 	return 0;
2040847bf383SLuigi Rizzo 
2041847bf383SLuigi Rizzo }
2042847bf383SLuigi Rizzo 
204337e3a6d3SLuigi Rizzo /* Undo netmap_krings_get(). This is done by clearing the exclusive mode
204437e3a6d3SLuigi Rizzo  * if was asked on regif, and unset the nr_pending_mode if we are the
204537e3a6d3SLuigi Rizzo  * last users of the involved rings. */
2046847bf383SLuigi Rizzo static void
204737e3a6d3SLuigi Rizzo netmap_krings_put(struct netmap_priv_d *priv)
2048847bf383SLuigi Rizzo {
2049847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
2050847bf383SLuigi Rizzo 	u_int i;
2051847bf383SLuigi Rizzo 	struct netmap_kring *kring;
2052847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
2053847bf383SLuigi Rizzo 	enum txrx t;
2054847bf383SLuigi Rizzo 
205575f4f3edSVincenzo Maffione 	nm_prdis("%s: releasing tx [%d, %d) rx [%d, %d)",
2056847bf383SLuigi Rizzo 			na->name,
2057847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
2058847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
2059847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
2060847bf383SLuigi Rizzo 			priv->np_qlast[MR_RX]);
2061847bf383SLuigi Rizzo 
2062847bf383SLuigi Rizzo 	for_rx_tx(t) {
2063847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
20642ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
2065847bf383SLuigi Rizzo 			if (excl)
2066847bf383SLuigi Rizzo 				kring->nr_kflags &= ~NKR_EXCLUSIVE;
2067847bf383SLuigi Rizzo 			kring->users--;
206837e3a6d3SLuigi Rizzo 			if (kring->users == 0)
206937e3a6d3SLuigi Rizzo 				kring->nr_pending_mode = NKR_NETMAP_OFF;
2070847bf383SLuigi Rizzo 		}
2071847bf383SLuigi Rizzo 	}
2072847bf383SLuigi Rizzo }
2073847bf383SLuigi Rizzo 
20742ff91c17SVincenzo Maffione static int
20752ff91c17SVincenzo Maffione nm_priv_rx_enabled(struct netmap_priv_d *priv)
20762ff91c17SVincenzo Maffione {
20772ff91c17SVincenzo Maffione 	return (priv->np_qfirst[NR_RX] != priv->np_qlast[NR_RX]);
20782ff91c17SVincenzo Maffione }
20792ff91c17SVincenzo Maffione 
2080b6e66be2SVincenzo Maffione /* Validate the CSB entries for both directions (atok and ktoa).
2081b6e66be2SVincenzo Maffione  * To be called under NMG_LOCK(). */
2082b6e66be2SVincenzo Maffione static int
2083b6e66be2SVincenzo Maffione netmap_csb_validate(struct netmap_priv_d *priv, struct nmreq_opt_csb *csbo)
2084b6e66be2SVincenzo Maffione {
2085b6e66be2SVincenzo Maffione 	struct nm_csb_atok *csb_atok_base =
2086b6e66be2SVincenzo Maffione 		(struct nm_csb_atok *)(uintptr_t)csbo->csb_atok;
2087b6e66be2SVincenzo Maffione 	struct nm_csb_ktoa *csb_ktoa_base =
2088b6e66be2SVincenzo Maffione 		(struct nm_csb_ktoa *)(uintptr_t)csbo->csb_ktoa;
2089b6e66be2SVincenzo Maffione 	enum txrx t;
2090b6e66be2SVincenzo Maffione 	int num_rings[NR_TXRX], tot_rings;
2091b6e66be2SVincenzo Maffione 	size_t entry_size[2];
2092b6e66be2SVincenzo Maffione 	void *csb_start[2];
2093b6e66be2SVincenzo Maffione 	int i;
2094b6e66be2SVincenzo Maffione 
2095b6e66be2SVincenzo Maffione 	if (priv->np_kloop_state & NM_SYNC_KLOOP_RUNNING) {
2096b6e66be2SVincenzo Maffione 		nm_prerr("Cannot update CSB while kloop is running");
2097b6e66be2SVincenzo Maffione 		return EBUSY;
2098b6e66be2SVincenzo Maffione 	}
2099b6e66be2SVincenzo Maffione 
2100b6e66be2SVincenzo Maffione 	tot_rings = 0;
2101b6e66be2SVincenzo Maffione 	for_rx_tx(t) {
2102b6e66be2SVincenzo Maffione 		num_rings[t] = priv->np_qlast[t] - priv->np_qfirst[t];
2103b6e66be2SVincenzo Maffione 		tot_rings += num_rings[t];
2104b6e66be2SVincenzo Maffione 	}
2105b6e66be2SVincenzo Maffione 	if (tot_rings <= 0)
2106b6e66be2SVincenzo Maffione 		return 0;
2107b6e66be2SVincenzo Maffione 
2108b6e66be2SVincenzo Maffione 	if (!(priv->np_flags & NR_EXCLUSIVE)) {
2109b6e66be2SVincenzo Maffione 		nm_prerr("CSB mode requires NR_EXCLUSIVE");
2110b6e66be2SVincenzo Maffione 		return EINVAL;
2111b6e66be2SVincenzo Maffione 	}
2112b6e66be2SVincenzo Maffione 
2113b6e66be2SVincenzo Maffione 	entry_size[0] = sizeof(*csb_atok_base);
2114b6e66be2SVincenzo Maffione 	entry_size[1] = sizeof(*csb_ktoa_base);
2115b6e66be2SVincenzo Maffione 	csb_start[0] = (void *)csb_atok_base;
2116b6e66be2SVincenzo Maffione 	csb_start[1] = (void *)csb_ktoa_base;
2117b6e66be2SVincenzo Maffione 
2118b6e66be2SVincenzo Maffione 	for (i = 0; i < 2; i++) {
2119b6e66be2SVincenzo Maffione 		/* On Linux we could use access_ok() to simplify
2120b6e66be2SVincenzo Maffione 		 * the validation. However, the advantage of
2121b6e66be2SVincenzo Maffione 		 * this approach is that it works also on
2122b6e66be2SVincenzo Maffione 		 * FreeBSD. */
2123b6e66be2SVincenzo Maffione 		size_t csb_size = tot_rings * entry_size[i];
2124b6e66be2SVincenzo Maffione 		void *tmp;
2125b6e66be2SVincenzo Maffione 		int err;
2126b6e66be2SVincenzo Maffione 
2127b6e66be2SVincenzo Maffione 		if ((uintptr_t)csb_start[i] & (entry_size[i]-1)) {
2128b6e66be2SVincenzo Maffione 			nm_prerr("Unaligned CSB address");
2129b6e66be2SVincenzo Maffione 			return EINVAL;
2130b6e66be2SVincenzo Maffione 		}
2131b6e66be2SVincenzo Maffione 
2132b6e66be2SVincenzo Maffione 		tmp = nm_os_malloc(csb_size);
2133b6e66be2SVincenzo Maffione 		if (!tmp)
2134b6e66be2SVincenzo Maffione 			return ENOMEM;
2135b6e66be2SVincenzo Maffione 		if (i == 0) {
2136b6e66be2SVincenzo Maffione 			/* Application --> kernel direction. */
2137b6e66be2SVincenzo Maffione 			err = copyin(csb_start[i], tmp, csb_size);
2138b6e66be2SVincenzo Maffione 		} else {
2139b6e66be2SVincenzo Maffione 			/* Kernel --> application direction. */
2140b6e66be2SVincenzo Maffione 			memset(tmp, 0, csb_size);
2141b6e66be2SVincenzo Maffione 			err = copyout(tmp, csb_start[i], csb_size);
2142b6e66be2SVincenzo Maffione 		}
2143b6e66be2SVincenzo Maffione 		nm_os_free(tmp);
2144b6e66be2SVincenzo Maffione 		if (err) {
2145b6e66be2SVincenzo Maffione 			nm_prerr("Invalid CSB address");
2146b6e66be2SVincenzo Maffione 			return err;
2147b6e66be2SVincenzo Maffione 		}
2148b6e66be2SVincenzo Maffione 	}
2149b6e66be2SVincenzo Maffione 
2150b6e66be2SVincenzo Maffione 	priv->np_csb_atok_base = csb_atok_base;
2151b6e66be2SVincenzo Maffione 	priv->np_csb_ktoa_base = csb_ktoa_base;
2152b6e66be2SVincenzo Maffione 
2153b6e66be2SVincenzo Maffione 	/* Initialize the CSB. */
2154b6e66be2SVincenzo Maffione 	for_rx_tx(t) {
2155b6e66be2SVincenzo Maffione 		for (i = 0; i < num_rings[t]; i++) {
2156b6e66be2SVincenzo Maffione 			struct netmap_kring *kring =
2157b6e66be2SVincenzo Maffione 				NMR(priv->np_na, t)[i + priv->np_qfirst[t]];
2158b6e66be2SVincenzo Maffione 			struct nm_csb_atok *csb_atok = csb_atok_base + i;
2159b6e66be2SVincenzo Maffione 			struct nm_csb_ktoa *csb_ktoa = csb_ktoa_base + i;
2160b6e66be2SVincenzo Maffione 
2161b6e66be2SVincenzo Maffione 			if (t == NR_RX) {
2162b6e66be2SVincenzo Maffione 				csb_atok += num_rings[NR_TX];
2163b6e66be2SVincenzo Maffione 				csb_ktoa += num_rings[NR_TX];
2164b6e66be2SVincenzo Maffione 			}
2165b6e66be2SVincenzo Maffione 
2166b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, head, kring->rhead);
2167b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, cur, kring->rcur);
2168b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, appl_need_kick, 1);
2169b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, sync_flags, 1);
2170b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_ktoa, hwcur, kring->nr_hwcur);
2171b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_ktoa, hwtail, kring->nr_hwtail);
2172b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_ktoa, kern_need_kick, 1);
2173b6e66be2SVincenzo Maffione 
2174b6e66be2SVincenzo Maffione 			nm_prinf("csb_init for kring %s: head %u, cur %u, "
2175b6e66be2SVincenzo Maffione 				"hwcur %u, hwtail %u", kring->name,
2176b6e66be2SVincenzo Maffione 				kring->rhead, kring->rcur, kring->nr_hwcur,
2177b6e66be2SVincenzo Maffione 				kring->nr_hwtail);
2178b6e66be2SVincenzo Maffione 		}
2179b6e66be2SVincenzo Maffione 	}
2180b6e66be2SVincenzo Maffione 
2181b6e66be2SVincenzo Maffione 	return 0;
2182b6e66be2SVincenzo Maffione }
2183b6e66be2SVincenzo Maffione 
218477a2baf5SVincenzo Maffione /* Ensure that the netmap adapter can support the given MTU.
218577a2baf5SVincenzo Maffione  * @return EINVAL if the na cannot be set to mtu, 0 otherwise.
218677a2baf5SVincenzo Maffione  */
218777a2baf5SVincenzo Maffione int
218877a2baf5SVincenzo Maffione netmap_buf_size_validate(const struct netmap_adapter *na, unsigned mtu) {
218977a2baf5SVincenzo Maffione 	unsigned nbs = NETMAP_BUF_SIZE(na);
219077a2baf5SVincenzo Maffione 
219177a2baf5SVincenzo Maffione 	if (mtu <= na->rx_buf_maxsize) {
219277a2baf5SVincenzo Maffione 		/* The MTU fits a single NIC slot. We only
219377a2baf5SVincenzo Maffione 		 * Need to check that netmap buffers are
219477a2baf5SVincenzo Maffione 		 * large enough to hold an MTU. NS_MOREFRAG
219577a2baf5SVincenzo Maffione 		 * cannot be used in this case. */
219677a2baf5SVincenzo Maffione 		if (nbs < mtu) {
219777a2baf5SVincenzo Maffione 			nm_prerr("error: netmap buf size (%u) "
219877a2baf5SVincenzo Maffione 				 "< device MTU (%u)", nbs, mtu);
219977a2baf5SVincenzo Maffione 			return EINVAL;
220077a2baf5SVincenzo Maffione 		}
220177a2baf5SVincenzo Maffione 	} else {
220277a2baf5SVincenzo Maffione 		/* More NIC slots may be needed to receive
220377a2baf5SVincenzo Maffione 		 * or transmit a single packet. Check that
220477a2baf5SVincenzo Maffione 		 * the adapter supports NS_MOREFRAG and that
220577a2baf5SVincenzo Maffione 		 * netmap buffers are large enough to hold
220677a2baf5SVincenzo Maffione 		 * the maximum per-slot size. */
220777a2baf5SVincenzo Maffione 		if (!(na->na_flags & NAF_MOREFRAG)) {
220877a2baf5SVincenzo Maffione 			nm_prerr("error: large MTU (%d) needed "
220977a2baf5SVincenzo Maffione 				 "but %s does not support "
221077a2baf5SVincenzo Maffione 				 "NS_MOREFRAG", mtu,
221177a2baf5SVincenzo Maffione 				 na->ifp->if_xname);
221277a2baf5SVincenzo Maffione 			return EINVAL;
221377a2baf5SVincenzo Maffione 		} else if (nbs < na->rx_buf_maxsize) {
221477a2baf5SVincenzo Maffione 			nm_prerr("error: using NS_MOREFRAG on "
221577a2baf5SVincenzo Maffione 				 "%s requires netmap buf size "
221677a2baf5SVincenzo Maffione 				 ">= %u", na->ifp->if_xname,
221777a2baf5SVincenzo Maffione 				 na->rx_buf_maxsize);
221877a2baf5SVincenzo Maffione 			return EINVAL;
221977a2baf5SVincenzo Maffione 		} else {
222077a2baf5SVincenzo Maffione 			nm_prinf("info: netmap application on "
222177a2baf5SVincenzo Maffione 				 "%s needs to support "
222277a2baf5SVincenzo Maffione 				 "NS_MOREFRAG "
222377a2baf5SVincenzo Maffione 				 "(MTU=%u,netmap_buf_size=%u)",
222477a2baf5SVincenzo Maffione 				 na->ifp->if_xname, mtu, nbs);
222577a2baf5SVincenzo Maffione 		}
222677a2baf5SVincenzo Maffione 	}
222777a2baf5SVincenzo Maffione 	return 0;
222877a2baf5SVincenzo Maffione }
222977a2baf5SVincenzo Maffione 
223077a2baf5SVincenzo Maffione 
2231f18be576SLuigi Rizzo /*
2232f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
2233f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
2234ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
22354bf50f18SLuigi Rizzo  *
22364bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
22374bf50f18SLuigi Rizzo  *
22384bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
22394bf50f18SLuigi Rizzo  * (get current number and size of rings)
22404bf50f18SLuigi Rizzo  *
22414bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
22424bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
22434bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
22444bf50f18SLuigi Rizzo  *
22454bf50f18SLuigi Rizzo  *
2246847bf383SLuigi Rizzo  * na->nm_krings_create()
22474bf50f18SLuigi Rizzo  * (create and init the krings array)
22484bf50f18SLuigi Rizzo  *
22494bf50f18SLuigi Rizzo  * 	One of the following:
22504bf50f18SLuigi Rizzo  *
22514bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
22524bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
22534bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
22544bf50f18SLuigi Rizzo  *
22554bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
22564bf50f18SLuigi Rizzo  * 		add leases and scratchpads
22574bf50f18SLuigi Rizzo  *
22584bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
22594bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
22604bf50f18SLuigi Rizzo  * 		cross-link them
22614bf50f18SLuigi Rizzo  *
22624bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
22634bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
22644bf50f18SLuigi Rizzo  *
22654bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
22664bf50f18SLuigi Rizzo  *      	create both the brap krings array,
22674bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
22684bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
22694bf50f18SLuigi Rizzo  *
22704bf50f18SLuigi Rizzo  * na->nm_register(, 1)
22714bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
22724bf50f18SLuigi Rizzo  *
22734bf50f18SLuigi Rizzo  * 	This may be one of the following:
22744bf50f18SLuigi Rizzo  *
227537e3a6d3SLuigi Rizzo  * 	* netmap_hw_reg				        (hw ports)
22764bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
22774bf50f18SLuigi Rizzo  * 		the hardware specific callback;
22784bf50f18SLuigi Rizzo  *
22794bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
22804bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
22814bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
22824bf50f18SLuigi Rizzo  *		bridge write lock.
22834bf50f18SLuigi Rizzo  *
22844bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
22854bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
2286453130d9SPedro F. Giffuni  *		longer responsible for the lifetime of this
22874bf50f18SLuigi Rizzo  *		pipe end
22884bf50f18SLuigi Rizzo  *
22894bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
22904bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
22914bf50f18SLuigi Rizzo  *		rings
22924bf50f18SLuigi Rizzo  *
229337e3a6d3SLuigi Rizzo  *	* netmap_bwrap_reg				(bwraps)
22944bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
22954bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
22964bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
22974bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
22984bf50f18SLuigi Rizzo  *
22994bf50f18SLuigi Rizzo  *
2300f18be576SLuigi Rizzo  */
2301847bf383SLuigi Rizzo int
2302f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
23032ff91c17SVincenzo Maffione 	uint32_t nr_mode, uint16_t nr_ringid, uint64_t nr_flags)
2304f18be576SLuigi Rizzo {
2305f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
2306847bf383SLuigi Rizzo 	int error;
2307f18be576SLuigi Rizzo 
2308ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
2309f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
2310847bf383SLuigi Rizzo 	error = netmap_mem_finalize(na->nm_mem, na);
2311ce3ee1e7SLuigi Rizzo 	if (error)
2312847bf383SLuigi Rizzo 		goto err;
2313847bf383SLuigi Rizzo 
2314847bf383SLuigi Rizzo 	if (na->active_fds == 0) {
23152ff91c17SVincenzo Maffione 
23162ff91c17SVincenzo Maffione 		/* cache the allocator info in the na */
23172ff91c17SVincenzo Maffione 		error = netmap_mem_get_lut(na->nm_mem, &na->na_lut);
23182ff91c17SVincenzo Maffione 		if (error)
23192ff91c17SVincenzo Maffione 			goto err_drop_mem;
232075f4f3edSVincenzo Maffione 		nm_prdis("lut %p bufs %u size %u", na->na_lut.lut, na->na_lut.objtotal,
23212ff91c17SVincenzo Maffione 					    na->na_lut.objsize);
23222ff91c17SVincenzo Maffione 
23232ff91c17SVincenzo Maffione 		/* ring configuration may have changed, fetch from the card */
23242ff91c17SVincenzo Maffione 		netmap_update_config(na);
2325cfa866f6SMatt Macy 	}
23262ff91c17SVincenzo Maffione 
2327cfa866f6SMatt Macy 	/* compute the range of tx and rx rings to monitor */
2328cfa866f6SMatt Macy 	error = netmap_set_ringid(priv, nr_mode, nr_ringid, nr_flags);
2329cfa866f6SMatt Macy 	if (error)
2330cfa866f6SMatt Macy 		goto err_put_lut;
2331cfa866f6SMatt Macy 
2332cfa866f6SMatt Macy 	if (na->active_fds == 0) {
2333847bf383SLuigi Rizzo 		/*
2334847bf383SLuigi Rizzo 		 * If this is the first registration of the adapter,
23354f80b14cSVincenzo Maffione 		 * perform sanity checks and create the in-kernel view
23364f80b14cSVincenzo Maffione 		 * of the netmap rings (the netmap krings).
2337847bf383SLuigi Rizzo 		 */
23382ff91c17SVincenzo Maffione 		if (na->ifp && nm_priv_rx_enabled(priv)) {
23394f80b14cSVincenzo Maffione 			/* This netmap adapter is attached to an ifnet. */
23404f80b14cSVincenzo Maffione 			unsigned mtu = nm_os_ifnet_mtu(na->ifp);
23414f80b14cSVincenzo Maffione 
234275f4f3edSVincenzo Maffione 			nm_prdis("%s: mtu %d rx_buf_maxsize %d netmap_buf_size %d",
234377a2baf5SVincenzo Maffione 				na->name, mtu, na->rx_buf_maxsize, NETMAP_BUF_SIZE(na));
2344cfa866f6SMatt Macy 
2345cfa866f6SMatt Macy 			if (na->rx_buf_maxsize == 0) {
2346b6e66be2SVincenzo Maffione 				nm_prerr("%s: error: rx_buf_maxsize == 0", na->name);
2347cfa866f6SMatt Macy 				error = EIO;
2348cfa866f6SMatt Macy 				goto err_drop_mem;
2349cfa866f6SMatt Macy 			}
23502ff91c17SVincenzo Maffione 
235177a2baf5SVincenzo Maffione 			error = netmap_buf_size_validate(na, mtu);
235277a2baf5SVincenzo Maffione 			if (error)
23534f80b14cSVincenzo Maffione 				goto err_drop_mem;
23544f80b14cSVincenzo Maffione 		}
2355847bf383SLuigi Rizzo 
2356847bf383SLuigi Rizzo 		/*
2357847bf383SLuigi Rizzo 		 * Depending on the adapter, this may also create
2358847bf383SLuigi Rizzo 		 * the netmap rings themselves
2359847bf383SLuigi Rizzo 		 */
2360847bf383SLuigi Rizzo 		error = na->nm_krings_create(na);
2361847bf383SLuigi Rizzo 		if (error)
23622ff91c17SVincenzo Maffione 			goto err_put_lut;
2363847bf383SLuigi Rizzo 
2364ce3ee1e7SLuigi Rizzo 	}
2365847bf383SLuigi Rizzo 
236637e3a6d3SLuigi Rizzo 	/* now the krings must exist and we can check whether some
236737e3a6d3SLuigi Rizzo 	 * previous bind has exclusive ownership on them, and set
236837e3a6d3SLuigi Rizzo 	 * nr_pending_mode
2369847bf383SLuigi Rizzo 	 */
237037e3a6d3SLuigi Rizzo 	error = netmap_krings_get(priv);
2371847bf383SLuigi Rizzo 	if (error)
237237e3a6d3SLuigi Rizzo 		goto err_del_krings;
237337e3a6d3SLuigi Rizzo 
237437e3a6d3SLuigi Rizzo 	/* create all needed missing netmap rings */
237537e3a6d3SLuigi Rizzo 	error = netmap_mem_rings_create(na);
237637e3a6d3SLuigi Rizzo 	if (error)
237737e3a6d3SLuigi Rizzo 		goto err_rel_excl;
2378847bf383SLuigi Rizzo 
2379847bf383SLuigi Rizzo 	/* in all cases, create a new netmap if */
2380c3e9b4dbSLuiz Otavio O Souza 	nifp = netmap_mem_if_new(na, priv);
2381847bf383SLuigi Rizzo 	if (nifp == NULL) {
2382f18be576SLuigi Rizzo 		error = ENOMEM;
2383cfa866f6SMatt Macy 		goto err_rel_excl;
2384ce3ee1e7SLuigi Rizzo 	}
2385847bf383SLuigi Rizzo 
238637e3a6d3SLuigi Rizzo 	if (nm_kring_pending(priv)) {
238737e3a6d3SLuigi Rizzo 		/* Some kring is switching mode, tell the adapter to
238837e3a6d3SLuigi Rizzo 		 * react on this. */
238937e3a6d3SLuigi Rizzo 		error = na->nm_register(na, 1);
239037e3a6d3SLuigi Rizzo 		if (error)
23912ff91c17SVincenzo Maffione 			goto err_del_if;
239237e3a6d3SLuigi Rizzo 	}
239337e3a6d3SLuigi Rizzo 
239437e3a6d3SLuigi Rizzo 	/* Commit the reference. */
239537e3a6d3SLuigi Rizzo 	na->active_fds++;
239637e3a6d3SLuigi Rizzo 
2397ce3ee1e7SLuigi Rizzo 	/*
2398847bf383SLuigi Rizzo 	 * advertise that the interface is ready by setting np_nifp.
2399847bf383SLuigi Rizzo 	 * The barrier is needed because readers (poll, *SYNC and mmap)
2400ce3ee1e7SLuigi Rizzo 	 * check for priv->np_nifp != NULL without locking
2401ce3ee1e7SLuigi Rizzo 	 */
2402847bf383SLuigi Rizzo 	mb(); /* make sure previous writes are visible to all CPUs */
2403ce3ee1e7SLuigi Rizzo 	priv->np_nifp = nifp;
2404847bf383SLuigi Rizzo 
2405847bf383SLuigi Rizzo 	return 0;
2406847bf383SLuigi Rizzo 
240737e3a6d3SLuigi Rizzo err_del_if:
2408847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
24094f80b14cSVincenzo Maffione err_rel_excl:
24104f80b14cSVincenzo Maffione 	netmap_krings_put(priv);
2411cfa866f6SMatt Macy 	netmap_mem_rings_delete(na);
2412847bf383SLuigi Rizzo err_del_krings:
2413847bf383SLuigi Rizzo 	if (na->active_fds == 0)
2414847bf383SLuigi Rizzo 		na->nm_krings_delete(na);
24152ff91c17SVincenzo Maffione err_put_lut:
24162ff91c17SVincenzo Maffione 	if (na->active_fds == 0)
24172ff91c17SVincenzo Maffione 		memset(&na->na_lut, 0, sizeof(na->na_lut));
2418847bf383SLuigi Rizzo err_drop_mem:
24194f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
2420847bf383SLuigi Rizzo err:
2421847bf383SLuigi Rizzo 	priv->np_na = NULL;
2422847bf383SLuigi Rizzo 	return error;
2423ce3ee1e7SLuigi Rizzo }
2424847bf383SLuigi Rizzo 
2425847bf383SLuigi Rizzo 
2426847bf383SLuigi Rizzo /*
242737e3a6d3SLuigi Rizzo  * update kring and ring at the end of rxsync/txsync.
2428847bf383SLuigi Rizzo  */
2429847bf383SLuigi Rizzo static inline void
243037e3a6d3SLuigi Rizzo nm_sync_finalize(struct netmap_kring *kring)
2431847bf383SLuigi Rizzo {
243237e3a6d3SLuigi Rizzo 	/*
243337e3a6d3SLuigi Rizzo 	 * Update ring tail to what the kernel knows
243437e3a6d3SLuigi Rizzo 	 * After txsync: head/rhead/hwcur might be behind cur/rcur
243537e3a6d3SLuigi Rizzo 	 * if no carrier.
243637e3a6d3SLuigi Rizzo 	 */
2437847bf383SLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
2438847bf383SLuigi Rizzo 
243975f4f3edSVincenzo Maffione 	nm_prdis(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
2440847bf383SLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
2441847bf383SLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
2442847bf383SLuigi Rizzo }
2443847bf383SLuigi Rizzo 
2444c3e9b4dbSLuiz Otavio O Souza /* set ring timestamp */
2445c3e9b4dbSLuiz Otavio O Souza static inline void
2446c3e9b4dbSLuiz Otavio O Souza ring_timestamp_set(struct netmap_ring *ring)
2447c3e9b4dbSLuiz Otavio O Souza {
2448c3e9b4dbSLuiz Otavio O Souza 	if (netmap_no_timestamp == 0 || ring->flags & NR_TIMESTAMP) {
2449c3e9b4dbSLuiz Otavio O Souza 		microtime(&ring->ts);
2450c3e9b4dbSLuiz Otavio O Souza 	}
2451c3e9b4dbSLuiz Otavio O Souza }
2452c3e9b4dbSLuiz Otavio O Souza 
24532ff91c17SVincenzo Maffione static int nmreq_copyin(struct nmreq_header *, int);
24542ff91c17SVincenzo Maffione static int nmreq_copyout(struct nmreq_header *, int);
24552ff91c17SVincenzo Maffione static int nmreq_checkoptions(struct nmreq_header *);
2456c3e9b4dbSLuiz Otavio O Souza 
245768b8534bSLuigi Rizzo /*
245868b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
245968b8534bSLuigi Rizzo  *
246068b8534bSLuigi Rizzo  * Following a list of accepted commands:
24612ff91c17SVincenzo Maffione  * - NIOCCTRL		device control API
24622ff91c17SVincenzo Maffione  * - NIOCTXSYNC		sync TX rings
24632ff91c17SVincenzo Maffione  * - NIOCRXSYNC		sync RX rings
246468b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
24652ff91c17SVincenzo Maffione  * - NIOCGINFO		deprecated (legacy API)
24662ff91c17SVincenzo Maffione  * - NIOCREGIF		deprecated (legacy API)
246768b8534bSLuigi Rizzo  *
246868b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
246968b8534bSLuigi Rizzo  */
2470f9790aebSLuigi Rizzo int
24712ff91c17SVincenzo Maffione netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
24722ff91c17SVincenzo Maffione 		struct thread *td, int nr_body_is_user)
247368b8534bSLuigi Rizzo {
2474c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
2475ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
2476c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd = NULL;
247737e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
247837e3a6d3SLuigi Rizzo 	int error = 0;
2479f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
24802ff91c17SVincenzo Maffione 	struct netmap_kring **krings;
2481c3e9b4dbSLuiz Otavio O Souza 	int sync_flags;
2482847bf383SLuigi Rizzo 	enum txrx t;
248368b8534bSLuigi Rizzo 
24842ff91c17SVincenzo Maffione 	switch (cmd) {
24852ff91c17SVincenzo Maffione 	case NIOCCTRL: {
24862ff91c17SVincenzo Maffione 		struct nmreq_header *hdr = (struct nmreq_header *)data;
24872ff91c17SVincenzo Maffione 
24882ff91c17SVincenzo Maffione 		if (hdr->nr_version < NETMAP_MIN_API ||
24892ff91c17SVincenzo Maffione 		    hdr->nr_version > NETMAP_MAX_API) {
2490b6e66be2SVincenzo Maffione 			nm_prerr("API mismatch: got %d need %d",
2491b6e66be2SVincenzo Maffione 				hdr->nr_version, NETMAP_API);
249217885a7bSLuigi Rizzo 			return EINVAL;
249317885a7bSLuigi Rizzo 		}
249468b8534bSLuigi Rizzo 
24952ff91c17SVincenzo Maffione 		/* Make a kernel-space copy of the user-space nr_body.
24962ff91c17SVincenzo Maffione 		 * For convenince, the nr_body pointer and the pointers
24972ff91c17SVincenzo Maffione 		 * in the options list will be replaced with their
24982ff91c17SVincenzo Maffione 		 * kernel-space counterparts. The original pointers are
24992ff91c17SVincenzo Maffione 		 * saved internally and later restored by nmreq_copyout
25002ff91c17SVincenzo Maffione 		 */
25012ff91c17SVincenzo Maffione 		error = nmreq_copyin(hdr, nr_body_is_user);
250237e3a6d3SLuigi Rizzo 		if (error) {
25032ff91c17SVincenzo Maffione 			return error;
2504ce3ee1e7SLuigi Rizzo 		}
2505ce3ee1e7SLuigi Rizzo 
25062ff91c17SVincenzo Maffione 		/* Sanitize hdr->nr_name. */
25072ff91c17SVincenzo Maffione 		hdr->nr_name[sizeof(hdr->nr_name) - 1] = '\0';
250868b8534bSLuigi Rizzo 
25092ff91c17SVincenzo Maffione 		switch (hdr->nr_reqtype) {
25102ff91c17SVincenzo Maffione 		case NETMAP_REQ_REGISTER: {
25112ff91c17SVincenzo Maffione 			struct nmreq_register *req =
2512cfa866f6SMatt Macy 				(struct nmreq_register *)(uintptr_t)hdr->nr_body;
2513b6e66be2SVincenzo Maffione 			struct netmap_if *nifp;
2514b6e66be2SVincenzo Maffione 
25152ff91c17SVincenzo Maffione 			/* Protect access to priv from concurrent requests. */
2516ce3ee1e7SLuigi Rizzo 			NMG_LOCK();
2517ce3ee1e7SLuigi Rizzo 			do {
25182ff91c17SVincenzo Maffione 				struct nmreq_option *opt;
2519b6e66be2SVincenzo Maffione 				u_int memflags;
2520ce3ee1e7SLuigi Rizzo 
2521847bf383SLuigi Rizzo 				if (priv->np_nifp != NULL) {	/* thread already registered */
2522f0ea3689SLuigi Rizzo 					error = EBUSY;
2523506cc70cSLuigi Rizzo 					break;
2524506cc70cSLuigi Rizzo 				}
2525c3e9b4dbSLuiz Otavio O Souza 
25262ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
2527253b2ec1SVincenzo Maffione 				opt = nmreq_getoption(hdr, NETMAP_REQ_OPT_EXTMEM);
25282ff91c17SVincenzo Maffione 				if (opt != NULL) {
25292ff91c17SVincenzo Maffione 					struct nmreq_opt_extmem *e =
25302ff91c17SVincenzo Maffione 						(struct nmreq_opt_extmem *)opt;
25312ff91c17SVincenzo Maffione 
25322ff91c17SVincenzo Maffione 					nmd = netmap_mem_ext_create(e->nro_usrptr,
25332ff91c17SVincenzo Maffione 							&e->nro_info, &error);
25342ff91c17SVincenzo Maffione 					opt->nro_status = error;
25352ff91c17SVincenzo Maffione 					if (nmd == NULL)
25362ff91c17SVincenzo Maffione 						break;
25372ff91c17SVincenzo Maffione 				}
25382ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
25392ff91c17SVincenzo Maffione 
25402ff91c17SVincenzo Maffione 				if (nmd == NULL && req->nr_mem_id) {
2541c3e9b4dbSLuiz Otavio O Souza 					/* find the allocator and get a reference */
25422ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id);
2543c3e9b4dbSLuiz Otavio O Souza 					if (nmd == NULL) {
2544b6e66be2SVincenzo Maffione 						if (netmap_verbose) {
2545b6e66be2SVincenzo Maffione 							nm_prerr("%s: failed to find mem_id %u",
2546b6e66be2SVincenzo Maffione 									hdr->nr_name, req->nr_mem_id);
2547b6e66be2SVincenzo Maffione 						}
2548c3e9b4dbSLuiz Otavio O Souza 						error = EINVAL;
2549c3e9b4dbSLuiz Otavio O Souza 						break;
2550c3e9b4dbSLuiz Otavio O Souza 					}
2551c3e9b4dbSLuiz Otavio O Souza 				}
255268b8534bSLuigi Rizzo 				/* find the interface and a reference */
25532ff91c17SVincenzo Maffione 				error = netmap_get_na(hdr, &na, &ifp, nmd,
255437e3a6d3SLuigi Rizzo 						      1 /* create */); /* keep reference */
255568b8534bSLuigi Rizzo 				if (error)
2556ce3ee1e7SLuigi Rizzo 					break;
2557f9790aebSLuigi Rizzo 				if (NETMAP_OWNED_BY_KERN(na)) {
2558ce3ee1e7SLuigi Rizzo 					error = EBUSY;
2559ce3ee1e7SLuigi Rizzo 					break;
2560f196ce38SLuigi Rizzo 				}
256137e3a6d3SLuigi Rizzo 
25622ff91c17SVincenzo Maffione 				if (na->virt_hdr_len && !(req->nr_flags & NR_ACCEPT_VNET_HDR)) {
2563b6e66be2SVincenzo Maffione 					nm_prerr("virt_hdr_len=%d, but application does "
2564b6e66be2SVincenzo Maffione 						"not accept it", na->virt_hdr_len);
256537e3a6d3SLuigi Rizzo 					error = EIO;
256637e3a6d3SLuigi Rizzo 					break;
256737e3a6d3SLuigi Rizzo 				}
256837e3a6d3SLuigi Rizzo 
25692ff91c17SVincenzo Maffione 				error = netmap_do_regif(priv, na, req->nr_mode,
25702ff91c17SVincenzo Maffione 							req->nr_ringid, req->nr_flags);
2571847bf383SLuigi Rizzo 				if (error) {    /* reg. failed, release priv and ref */
2572ce3ee1e7SLuigi Rizzo 					break;
257368b8534bSLuigi Rizzo 				}
2574b6e66be2SVincenzo Maffione 
2575253b2ec1SVincenzo Maffione 				opt = nmreq_getoption(hdr, NETMAP_REQ_OPT_CSB);
2576b6e66be2SVincenzo Maffione 				if (opt != NULL) {
2577b6e66be2SVincenzo Maffione 					struct nmreq_opt_csb *csbo =
2578b6e66be2SVincenzo Maffione 						(struct nmreq_opt_csb *)opt;
2579b6e66be2SVincenzo Maffione 					error = netmap_csb_validate(priv, csbo);
2580b6e66be2SVincenzo Maffione 					opt->nro_status = error;
2581b6e66be2SVincenzo Maffione 					if (error) {
2582b6e66be2SVincenzo Maffione 						netmap_do_unregif(priv);
2583b6e66be2SVincenzo Maffione 						break;
2584b6e66be2SVincenzo Maffione 					}
2585b6e66be2SVincenzo Maffione 				}
2586b6e66be2SVincenzo Maffione 
2587847bf383SLuigi Rizzo 				nifp = priv->np_nifp;
258868b8534bSLuigi Rizzo 
258968b8534bSLuigi Rizzo 				/* return the offset of the netmap_if object */
25902ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
25912ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
25922ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
25932ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
2594d12354a5SVincenzo Maffione 				req->nr_host_tx_rings = na->num_host_tx_rings;
2595d12354a5SVincenzo Maffione 				req->nr_host_rx_rings = na->num_host_rx_rings;
25962ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(na->nm_mem, &req->nr_memsize, &memflags,
25972ff91c17SVincenzo Maffione 					&req->nr_mem_id);
2598ce3ee1e7SLuigi Rizzo 				if (error) {
2599847bf383SLuigi Rizzo 					netmap_do_unregif(priv);
2600ce3ee1e7SLuigi Rizzo 					break;
2601ce3ee1e7SLuigi Rizzo 				}
2602ce3ee1e7SLuigi Rizzo 				if (memflags & NETMAP_MEM_PRIVATE) {
26033d819cb6SLuigi Rizzo 					*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2604ce3ee1e7SLuigi Rizzo 				}
2605847bf383SLuigi Rizzo 				for_rx_tx(t) {
2606847bf383SLuigi Rizzo 					priv->np_si[t] = nm_si_user(priv, t) ?
26072ff91c17SVincenzo Maffione 						&na->si[t] : &NMR(na, t)[priv->np_qfirst[t]]->si;
2608847bf383SLuigi Rizzo 				}
2609f0ea3689SLuigi Rizzo 
26102ff91c17SVincenzo Maffione 				if (req->nr_extra_bufs) {
261137e3a6d3SLuigi Rizzo 					if (netmap_verbose)
2612b6e66be2SVincenzo Maffione 						nm_prinf("requested %d extra buffers",
26132ff91c17SVincenzo Maffione 							req->nr_extra_bufs);
26142ff91c17SVincenzo Maffione 					req->nr_extra_bufs = netmap_extra_alloc(na,
26152ff91c17SVincenzo Maffione 						&nifp->ni_bufs_head, req->nr_extra_bufs);
261637e3a6d3SLuigi Rizzo 					if (netmap_verbose)
2617b6e66be2SVincenzo Maffione 						nm_prinf("got %d extra buffers", req->nr_extra_bufs);
2618f0ea3689SLuigi Rizzo 				}
26192ff91c17SVincenzo Maffione 				req->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
26202ff91c17SVincenzo Maffione 
26212ff91c17SVincenzo Maffione 				error = nmreq_checkoptions(hdr);
26222ff91c17SVincenzo Maffione 				if (error) {
26232ff91c17SVincenzo Maffione 					netmap_do_unregif(priv);
26242ff91c17SVincenzo Maffione 					break;
26252ff91c17SVincenzo Maffione 				}
262637e3a6d3SLuigi Rizzo 
262737e3a6d3SLuigi Rizzo 				/* store ifp reference so that priv destructor may release it */
262837e3a6d3SLuigi Rizzo 				priv->np_ifp = ifp;
2629ce3ee1e7SLuigi Rizzo 			} while (0);
2630c3e9b4dbSLuiz Otavio O Souza 			if (error) {
2631c3e9b4dbSLuiz Otavio O Souza 				netmap_unget_na(na, ifp);
2632c3e9b4dbSLuiz Otavio O Souza 			}
2633c3e9b4dbSLuiz Otavio O Souza 			/* release the reference from netmap_mem_find() or
2634c3e9b4dbSLuiz Otavio O Souza 			 * netmap_mem_ext_create()
2635c3e9b4dbSLuiz Otavio O Souza 			 */
2636c3e9b4dbSLuiz Otavio O Souza 			if (nmd)
2637c3e9b4dbSLuiz Otavio O Souza 				netmap_mem_put(nmd);
2638ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
263968b8534bSLuigi Rizzo 			break;
26402ff91c17SVincenzo Maffione 		}
26412ff91c17SVincenzo Maffione 
26422ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_INFO_GET: {
26432ff91c17SVincenzo Maffione 			struct nmreq_port_info_get *req =
2644cfa866f6SMatt Macy 				(struct nmreq_port_info_get *)(uintptr_t)hdr->nr_body;
26452ff91c17SVincenzo Maffione 
26462ff91c17SVincenzo Maffione 			NMG_LOCK();
26472ff91c17SVincenzo Maffione 			do {
26482ff91c17SVincenzo Maffione 				u_int memflags;
26492ff91c17SVincenzo Maffione 
26502ff91c17SVincenzo Maffione 				if (hdr->nr_name[0] != '\0') {
26512ff91c17SVincenzo Maffione 					/* Build a nmreq_register out of the nmreq_port_info_get,
26522ff91c17SVincenzo Maffione 					 * so that we can call netmap_get_na(). */
26532ff91c17SVincenzo Maffione 					struct nmreq_register regreq;
26542ff91c17SVincenzo Maffione 					bzero(&regreq, sizeof(regreq));
2655b6e66be2SVincenzo Maffione 					regreq.nr_mode = NR_REG_ALL_NIC;
26562ff91c17SVincenzo Maffione 					regreq.nr_tx_slots = req->nr_tx_slots;
26572ff91c17SVincenzo Maffione 					regreq.nr_rx_slots = req->nr_rx_slots;
26582ff91c17SVincenzo Maffione 					regreq.nr_tx_rings = req->nr_tx_rings;
26592ff91c17SVincenzo Maffione 					regreq.nr_rx_rings = req->nr_rx_rings;
2660d12354a5SVincenzo Maffione 					regreq.nr_host_tx_rings = req->nr_host_tx_rings;
2661d12354a5SVincenzo Maffione 					regreq.nr_host_rx_rings = req->nr_host_rx_rings;
26622ff91c17SVincenzo Maffione 					regreq.nr_mem_id = req->nr_mem_id;
26632ff91c17SVincenzo Maffione 
26642ff91c17SVincenzo Maffione 					/* get a refcount */
26652ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2666cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)&regreq;
26672ff91c17SVincenzo Maffione 					error = netmap_get_na(hdr, &na, &ifp, NULL, 1 /* create */);
26682ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_PORT_INFO_GET; /* reset type */
2669cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)req; /* reset nr_body */
26702ff91c17SVincenzo Maffione 					if (error) {
26712ff91c17SVincenzo Maffione 						na = NULL;
26722ff91c17SVincenzo Maffione 						ifp = NULL;
26732ff91c17SVincenzo Maffione 						break;
26742ff91c17SVincenzo Maffione 					}
26752ff91c17SVincenzo Maffione 					nmd = na->nm_mem; /* get memory allocator */
26762ff91c17SVincenzo Maffione 				} else {
26772ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id ? req->nr_mem_id : 1);
26782ff91c17SVincenzo Maffione 					if (nmd == NULL) {
2679b6e66be2SVincenzo Maffione 						if (netmap_verbose)
2680b6e66be2SVincenzo Maffione 							nm_prerr("%s: failed to find mem_id %u",
2681b6e66be2SVincenzo Maffione 									hdr->nr_name,
2682b6e66be2SVincenzo Maffione 									req->nr_mem_id ? req->nr_mem_id : 1);
26832ff91c17SVincenzo Maffione 						error = EINVAL;
26842ff91c17SVincenzo Maffione 						break;
26852ff91c17SVincenzo Maffione 					}
26862ff91c17SVincenzo Maffione 				}
26872ff91c17SVincenzo Maffione 
26882ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(nmd, &req->nr_memsize, &memflags,
26892ff91c17SVincenzo Maffione 					&req->nr_mem_id);
26902ff91c17SVincenzo Maffione 				if (error)
26912ff91c17SVincenzo Maffione 					break;
26922ff91c17SVincenzo Maffione 				if (na == NULL) /* only memory info */
26932ff91c17SVincenzo Maffione 					break;
26942ff91c17SVincenzo Maffione 				netmap_update_config(na);
26952ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
26962ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
26972ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
26982ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
2699d12354a5SVincenzo Maffione 				req->nr_host_tx_rings = na->num_host_tx_rings;
2700d12354a5SVincenzo Maffione 				req->nr_host_rx_rings = na->num_host_rx_rings;
27012ff91c17SVincenzo Maffione 			} while (0);
27022ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
27032ff91c17SVincenzo Maffione 			NMG_UNLOCK();
27042ff91c17SVincenzo Maffione 			break;
27052ff91c17SVincenzo Maffione 		}
27062ff91c17SVincenzo Maffione #ifdef WITH_VALE
27072ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_ATTACH: {
2708b6e66be2SVincenzo Maffione 			error = netmap_vale_attach(hdr, NULL /* userspace request */);
27092ff91c17SVincenzo Maffione 			break;
27102ff91c17SVincenzo Maffione 		}
27112ff91c17SVincenzo Maffione 
27122ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DETACH: {
2713b6e66be2SVincenzo Maffione 			error = netmap_vale_detach(hdr, NULL /* userspace request */);
27142ff91c17SVincenzo Maffione 			break;
27152ff91c17SVincenzo Maffione 		}
27162ff91c17SVincenzo Maffione 
27172ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_LIST: {
2718b6e66be2SVincenzo Maffione 			error = netmap_vale_list(hdr);
27192ff91c17SVincenzo Maffione 			break;
27202ff91c17SVincenzo Maffione 		}
27212ff91c17SVincenzo Maffione 
27222ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_SET: {
27232ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2724cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
27252ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
27262ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
27272ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
27282ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
2729b6e66be2SVincenzo Maffione 			regreq.nr_mode = NR_REG_ALL_NIC;
2730b6e66be2SVincenzo Maffione 
27312ff91c17SVincenzo Maffione 			/* For now we only support virtio-net headers, and only for
27322ff91c17SVincenzo Maffione 			 * VALE ports, but this may change in future. Valid lengths
27332ff91c17SVincenzo Maffione 			 * for the virtio-net header are 0 (no header), 10 and 12. */
27342ff91c17SVincenzo Maffione 			if (req->nr_hdr_len != 0 &&
27352ff91c17SVincenzo Maffione 				req->nr_hdr_len != sizeof(struct nm_vnet_hdr) &&
27362ff91c17SVincenzo Maffione 					req->nr_hdr_len != 12) {
2737b6e66be2SVincenzo Maffione 				if (netmap_verbose)
2738b6e66be2SVincenzo Maffione 					nm_prerr("invalid hdr_len %u", req->nr_hdr_len);
27392ff91c17SVincenzo Maffione 				error = EINVAL;
27402ff91c17SVincenzo Maffione 				break;
27412ff91c17SVincenzo Maffione 			}
27422ff91c17SVincenzo Maffione 			NMG_LOCK();
27432ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2744cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
27452a7db7a6SVincenzo Maffione 			error = netmap_get_vale_na(hdr, &na, NULL, 0);
27462ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_SET;
2747cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
27482ff91c17SVincenzo Maffione 			if (na && !error) {
27492ff91c17SVincenzo Maffione 				struct netmap_vp_adapter *vpna =
27502ff91c17SVincenzo Maffione 					(struct netmap_vp_adapter *)na;
27512ff91c17SVincenzo Maffione 				na->virt_hdr_len = req->nr_hdr_len;
27522ff91c17SVincenzo Maffione 				if (na->virt_hdr_len) {
27532ff91c17SVincenzo Maffione 					vpna->mfs = NETMAP_BUF_SIZE(na);
27542ff91c17SVincenzo Maffione 				}
2755b6e66be2SVincenzo Maffione 				if (netmap_verbose)
2756b6e66be2SVincenzo Maffione 					nm_prinf("Using vnet_hdr_len %d for %p", na->virt_hdr_len, na);
27572ff91c17SVincenzo Maffione 				netmap_adapter_put(na);
27582ff91c17SVincenzo Maffione 			} else if (!na) {
27592ff91c17SVincenzo Maffione 				error = ENXIO;
27602ff91c17SVincenzo Maffione 			}
27612ff91c17SVincenzo Maffione 			NMG_UNLOCK();
27622ff91c17SVincenzo Maffione 			break;
27632ff91c17SVincenzo Maffione 		}
27642ff91c17SVincenzo Maffione 
27652ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_GET: {
27662ff91c17SVincenzo Maffione 			/* Get vnet-header length for this netmap port */
27672ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2768cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
27692ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
27702ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
27712ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
27722ff91c17SVincenzo Maffione 			struct ifnet *ifp;
27732ff91c17SVincenzo Maffione 
27742ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
2775b6e66be2SVincenzo Maffione 			regreq.nr_mode = NR_REG_ALL_NIC;
27762ff91c17SVincenzo Maffione 			NMG_LOCK();
27772ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2778cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
27792ff91c17SVincenzo Maffione 			error = netmap_get_na(hdr, &na, &ifp, NULL, 0);
27802ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_GET;
2781cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
27822ff91c17SVincenzo Maffione 			if (na && !error) {
27832ff91c17SVincenzo Maffione 				req->nr_hdr_len = na->virt_hdr_len;
27842ff91c17SVincenzo Maffione 			}
27852ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
27862ff91c17SVincenzo Maffione 			NMG_UNLOCK();
27872ff91c17SVincenzo Maffione 			break;
27882ff91c17SVincenzo Maffione 		}
27892ff91c17SVincenzo Maffione 
27902ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_NEWIF: {
27912ff91c17SVincenzo Maffione 			error = nm_vi_create(hdr);
27922ff91c17SVincenzo Maffione 			break;
27932ff91c17SVincenzo Maffione 		}
27942ff91c17SVincenzo Maffione 
27952ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DELIF: {
27962ff91c17SVincenzo Maffione 			error = nm_vi_destroy(hdr->nr_name);
27972ff91c17SVincenzo Maffione 			break;
27982ff91c17SVincenzo Maffione 		}
27992ff91c17SVincenzo Maffione 
28002ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_ENABLE:
28012ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_DISABLE: {
28022ff91c17SVincenzo Maffione 			error = nm_bdg_polling(hdr);
28032ff91c17SVincenzo Maffione 			break;
28042ff91c17SVincenzo Maffione 		}
28052ff91c17SVincenzo Maffione #endif  /* WITH_VALE */
28062ff91c17SVincenzo Maffione 		case NETMAP_REQ_POOLS_INFO_GET: {
2807b6e66be2SVincenzo Maffione 			/* Get information from the memory allocator used for
2808b6e66be2SVincenzo Maffione 			 * hdr->nr_name. */
28092ff91c17SVincenzo Maffione 			struct nmreq_pools_info *req =
2810cfa866f6SMatt Macy 				(struct nmreq_pools_info *)(uintptr_t)hdr->nr_body;
28112ff91c17SVincenzo Maffione 			NMG_LOCK();
2812b6e66be2SVincenzo Maffione 			do {
2813b6e66be2SVincenzo Maffione 				/* Build a nmreq_register out of the nmreq_pools_info,
2814b6e66be2SVincenzo Maffione 				 * so that we can call netmap_get_na(). */
2815b6e66be2SVincenzo Maffione 				struct nmreq_register regreq;
2816b6e66be2SVincenzo Maffione 				bzero(&regreq, sizeof(regreq));
2817b6e66be2SVincenzo Maffione 				regreq.nr_mem_id = req->nr_mem_id;
2818b6e66be2SVincenzo Maffione 				regreq.nr_mode = NR_REG_ALL_NIC;
2819b6e66be2SVincenzo Maffione 
2820b6e66be2SVincenzo Maffione 				hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2821b6e66be2SVincenzo Maffione 				hdr->nr_body = (uintptr_t)&regreq;
2822b6e66be2SVincenzo Maffione 				error = netmap_get_na(hdr, &na, &ifp, NULL, 1 /* create */);
2823b6e66be2SVincenzo Maffione 				hdr->nr_reqtype = NETMAP_REQ_POOLS_INFO_GET; /* reset type */
2824b6e66be2SVincenzo Maffione 				hdr->nr_body = (uintptr_t)req; /* reset nr_body */
2825b6e66be2SVincenzo Maffione 				if (error) {
2826b6e66be2SVincenzo Maffione 					na = NULL;
2827b6e66be2SVincenzo Maffione 					ifp = NULL;
2828b6e66be2SVincenzo Maffione 					break;
28292ff91c17SVincenzo Maffione 				}
2830b6e66be2SVincenzo Maffione 				nmd = na->nm_mem; /* grab the memory allocator */
2831b6e66be2SVincenzo Maffione 				if (nmd == NULL) {
2832b6e66be2SVincenzo Maffione 					error = EINVAL;
2833b6e66be2SVincenzo Maffione 					break;
2834b6e66be2SVincenzo Maffione 				}
2835b6e66be2SVincenzo Maffione 
2836b6e66be2SVincenzo Maffione 				/* Finalize the memory allocator, get the pools
2837b6e66be2SVincenzo Maffione 				 * information and release the allocator. */
2838b6e66be2SVincenzo Maffione 				error = netmap_mem_finalize(nmd, na);
2839b6e66be2SVincenzo Maffione 				if (error) {
2840b6e66be2SVincenzo Maffione 					break;
2841b6e66be2SVincenzo Maffione 				}
2842b6e66be2SVincenzo Maffione 				error = netmap_mem_pools_info_get(req, nmd);
2843b6e66be2SVincenzo Maffione 				netmap_mem_drop(na);
2844b6e66be2SVincenzo Maffione 			} while (0);
2845b6e66be2SVincenzo Maffione 			netmap_unget_na(na, ifp);
28462ff91c17SVincenzo Maffione 			NMG_UNLOCK();
28472ff91c17SVincenzo Maffione 			break;
28482ff91c17SVincenzo Maffione 		}
28492ff91c17SVincenzo Maffione 
2850b6e66be2SVincenzo Maffione 		case NETMAP_REQ_CSB_ENABLE: {
2851b6e66be2SVincenzo Maffione 			struct nmreq_option *opt;
2852b6e66be2SVincenzo Maffione 
2853253b2ec1SVincenzo Maffione 			opt = nmreq_getoption(hdr, NETMAP_REQ_OPT_CSB);
2854b6e66be2SVincenzo Maffione 			if (opt == NULL) {
2855b6e66be2SVincenzo Maffione 				error = EINVAL;
2856b6e66be2SVincenzo Maffione 			} else {
2857b6e66be2SVincenzo Maffione 				struct nmreq_opt_csb *csbo =
2858b6e66be2SVincenzo Maffione 					(struct nmreq_opt_csb *)opt;
2859b6e66be2SVincenzo Maffione 				NMG_LOCK();
2860b6e66be2SVincenzo Maffione 				error = netmap_csb_validate(priv, csbo);
2861b6e66be2SVincenzo Maffione 				NMG_UNLOCK();
2862b6e66be2SVincenzo Maffione 				opt->nro_status = error;
2863b6e66be2SVincenzo Maffione 			}
2864b6e66be2SVincenzo Maffione 			break;
2865b6e66be2SVincenzo Maffione 		}
2866b6e66be2SVincenzo Maffione 
2867b6e66be2SVincenzo Maffione 		case NETMAP_REQ_SYNC_KLOOP_START: {
2868b6e66be2SVincenzo Maffione 			error = netmap_sync_kloop(priv, hdr);
2869b6e66be2SVincenzo Maffione 			break;
2870b6e66be2SVincenzo Maffione 		}
2871b6e66be2SVincenzo Maffione 
2872b6e66be2SVincenzo Maffione 		case NETMAP_REQ_SYNC_KLOOP_STOP: {
2873b6e66be2SVincenzo Maffione 			error = netmap_sync_kloop_stop(priv);
2874b6e66be2SVincenzo Maffione 			break;
2875b6e66be2SVincenzo Maffione 		}
2876b6e66be2SVincenzo Maffione 
28772ff91c17SVincenzo Maffione 		default: {
28782ff91c17SVincenzo Maffione 			error = EINVAL;
28792ff91c17SVincenzo Maffione 			break;
28802ff91c17SVincenzo Maffione 		}
28812ff91c17SVincenzo Maffione 		}
28822ff91c17SVincenzo Maffione 		/* Write back request body to userspace and reset the
28832ff91c17SVincenzo Maffione 		 * user-space pointer. */
28842ff91c17SVincenzo Maffione 		error = nmreq_copyout(hdr, error);
28852ff91c17SVincenzo Maffione 		break;
28862ff91c17SVincenzo Maffione 	}
288768b8534bSLuigi Rizzo 
288868b8534bSLuigi Rizzo 	case NIOCTXSYNC:
28892ff91c17SVincenzo Maffione 	case NIOCRXSYNC: {
2890b6e66be2SVincenzo Maffione 		if (unlikely(priv->np_nifp == NULL)) {
2891506cc70cSLuigi Rizzo 			error = ENXIO;
2892506cc70cSLuigi Rizzo 			break;
2893506cc70cSLuigi Rizzo 		}
28946641c68bSLuigi Rizzo 		mb(); /* make sure following reads are not from cache */
28958241616dSLuigi Rizzo 
2896b6e66be2SVincenzo Maffione 		if (unlikely(priv->np_csb_atok_base)) {
2897b6e66be2SVincenzo Maffione 			nm_prerr("Invalid sync in CSB mode");
2898b6e66be2SVincenzo Maffione 			error = EBUSY;
28998241616dSLuigi Rizzo 			break;
29008241616dSLuigi Rizzo 		}
29018241616dSLuigi Rizzo 
2902b6e66be2SVincenzo Maffione 		na = priv->np_na;      /* we have a reference */
2903b6e66be2SVincenzo Maffione 
2904c3e9b4dbSLuiz Otavio O Souza 		mbq_init(&q);
2905847bf383SLuigi Rizzo 		t = (cmd == NIOCTXSYNC ? NR_TX : NR_RX);
2906847bf383SLuigi Rizzo 		krings = NMR(na, t);
2907847bf383SLuigi Rizzo 		qfirst = priv->np_qfirst[t];
2908847bf383SLuigi Rizzo 		qlast = priv->np_qlast[t];
2909c3e9b4dbSLuiz Otavio O Souza 		sync_flags = priv->np_sync_flags;
291068b8534bSLuigi Rizzo 
2911f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
29122ff91c17SVincenzo Maffione 			struct netmap_kring *kring = krings[i];
291337e3a6d3SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
291437e3a6d3SLuigi Rizzo 
291537e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &error))) {
291637e3a6d3SLuigi Rizzo 				error = (error ? EIO : 0);
291737e3a6d3SLuigi Rizzo 				continue;
2918ce3ee1e7SLuigi Rizzo 			}
291937e3a6d3SLuigi Rizzo 
292068b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
2921b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_TXSYNC)
2922b6e66be2SVincenzo Maffione 					nm_prinf("pre txsync ring %d cur %d hwcur %d",
292337e3a6d3SLuigi Rizzo 					    i, ring->cur,
292468b8534bSLuigi Rizzo 					    kring->nr_hwcur);
292537e3a6d3SLuigi Rizzo 				if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
292617885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
2927c3e9b4dbSLuiz Otavio O Souza 				} else if (kring->nm_sync(kring, sync_flags | NAF_FORCE_RECLAIM) == 0) {
292837e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
292917885a7bSLuigi Rizzo 				}
2930b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_TXSYNC)
2931b6e66be2SVincenzo Maffione 					nm_prinf("post txsync ring %d cur %d hwcur %d",
293237e3a6d3SLuigi Rizzo 					    i, ring->cur,
293368b8534bSLuigi Rizzo 					    kring->nr_hwcur);
293468b8534bSLuigi Rizzo 			} else {
293537e3a6d3SLuigi Rizzo 				if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
2936847bf383SLuigi Rizzo 					netmap_ring_reinit(kring);
2937c3e9b4dbSLuiz Otavio O Souza 				}
2938c3e9b4dbSLuiz Otavio O Souza 				if (nm_may_forward_up(kring)) {
2939c3e9b4dbSLuiz Otavio O Souza 					/* transparent forwarding, see netmap_poll() */
2940c3e9b4dbSLuiz Otavio O Souza 					netmap_grab_packets(kring, &q, netmap_fwd);
2941c3e9b4dbSLuiz Otavio O Souza 				}
2942c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags | NAF_FORCE_READ) == 0) {
294337e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
2944847bf383SLuigi Rizzo 				}
2945c3e9b4dbSLuiz Otavio O Souza 				ring_timestamp_set(ring);
294668b8534bSLuigi Rizzo 			}
2947ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
294868b8534bSLuigi Rizzo 		}
294968b8534bSLuigi Rizzo 
2950c3e9b4dbSLuiz Otavio O Souza 		if (mbq_peek(&q)) {
2951c3e9b4dbSLuiz Otavio O Souza 			netmap_send_up(na->ifp, &q);
2952c3e9b4dbSLuiz Otavio O Souza 		}
2953c3e9b4dbSLuiz Otavio O Souza 
295468b8534bSLuigi Rizzo 		break;
295568b8534bSLuigi Rizzo 	}
2956f196ce38SLuigi Rizzo 
29572ff91c17SVincenzo Maffione 	default: {
29582ff91c17SVincenzo Maffione 		return netmap_ioctl_legacy(priv, cmd, data, td);
29592ff91c17SVincenzo Maffione 		break;
29602ff91c17SVincenzo Maffione 	}
296168b8534bSLuigi Rizzo 	}
296268b8534bSLuigi Rizzo 
296368b8534bSLuigi Rizzo 	return (error);
296468b8534bSLuigi Rizzo }
296568b8534bSLuigi Rizzo 
29662ff91c17SVincenzo Maffione size_t
29672ff91c17SVincenzo Maffione nmreq_size_by_type(uint16_t nr_reqtype)
29682ff91c17SVincenzo Maffione {
29692ff91c17SVincenzo Maffione 	switch (nr_reqtype) {
29702ff91c17SVincenzo Maffione 	case NETMAP_REQ_REGISTER:
29712ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_register);
29722ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_INFO_GET:
29732ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_info_get);
29742ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_ATTACH:
29752ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_attach);
29762ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DETACH:
29772ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_detach);
29782ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_LIST:
29792ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_list);
29802ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_SET:
29812ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_GET:
29822ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_hdr);
29832ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_NEWIF:
29842ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_newif);
29852ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DELIF:
2986b6e66be2SVincenzo Maffione 	case NETMAP_REQ_SYNC_KLOOP_STOP:
2987b6e66be2SVincenzo Maffione 	case NETMAP_REQ_CSB_ENABLE:
29882ff91c17SVincenzo Maffione 		return 0;
29892ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_ENABLE:
29902ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_DISABLE:
29912ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_polling);
29922ff91c17SVincenzo Maffione 	case NETMAP_REQ_POOLS_INFO_GET:
29932ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_pools_info);
2994b6e66be2SVincenzo Maffione 	case NETMAP_REQ_SYNC_KLOOP_START:
2995b6e66be2SVincenzo Maffione 		return sizeof(struct nmreq_sync_kloop_start);
29962ff91c17SVincenzo Maffione 	}
29972ff91c17SVincenzo Maffione 	return 0;
29982ff91c17SVincenzo Maffione }
29992ff91c17SVincenzo Maffione 
30002ff91c17SVincenzo Maffione static size_t
3001b6e66be2SVincenzo Maffione nmreq_opt_size_by_type(uint32_t nro_reqtype, uint64_t nro_size)
30022ff91c17SVincenzo Maffione {
30032ff91c17SVincenzo Maffione 	size_t rv = sizeof(struct nmreq_option);
30042ff91c17SVincenzo Maffione #ifdef NETMAP_REQ_OPT_DEBUG
30052ff91c17SVincenzo Maffione 	if (nro_reqtype & NETMAP_REQ_OPT_DEBUG)
30062ff91c17SVincenzo Maffione 		return (nro_reqtype & ~NETMAP_REQ_OPT_DEBUG);
30072ff91c17SVincenzo Maffione #endif /* NETMAP_REQ_OPT_DEBUG */
30082ff91c17SVincenzo Maffione 	switch (nro_reqtype) {
30092ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
30102ff91c17SVincenzo Maffione 	case NETMAP_REQ_OPT_EXTMEM:
30112ff91c17SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_extmem);
30122ff91c17SVincenzo Maffione 		break;
30132ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
3014b6e66be2SVincenzo Maffione 	case NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS:
3015b6e66be2SVincenzo Maffione 		if (nro_size >= rv)
3016b6e66be2SVincenzo Maffione 			rv = nro_size;
3017b6e66be2SVincenzo Maffione 		break;
3018b6e66be2SVincenzo Maffione 	case NETMAP_REQ_OPT_CSB:
3019b6e66be2SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_csb);
3020b6e66be2SVincenzo Maffione 		break;
30215faab778SVincenzo Maffione 	case NETMAP_REQ_OPT_SYNC_KLOOP_MODE:
30225faab778SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_sync_kloop_mode);
30235faab778SVincenzo Maffione 		break;
30242ff91c17SVincenzo Maffione 	}
30252ff91c17SVincenzo Maffione 	/* subtract the common header */
30262ff91c17SVincenzo Maffione 	return rv - sizeof(struct nmreq_option);
30272ff91c17SVincenzo Maffione }
30282ff91c17SVincenzo Maffione 
3029253b2ec1SVincenzo Maffione /*
3030253b2ec1SVincenzo Maffione  * nmreq_copyin: create an in-kernel version of the request.
3031253b2ec1SVincenzo Maffione  *
3032253b2ec1SVincenzo Maffione  * We build the following data structure:
3033253b2ec1SVincenzo Maffione  *
3034253b2ec1SVincenzo Maffione  * hdr -> +-------+                buf
3035253b2ec1SVincenzo Maffione  *        |       |          +---------------+
3036253b2ec1SVincenzo Maffione  *        +-------+          |usr body ptr   |
3037253b2ec1SVincenzo Maffione  *        |options|-.        +---------------+
3038253b2ec1SVincenzo Maffione  *        +-------+ |        |usr options ptr|
3039253b2ec1SVincenzo Maffione  *        |body   |--------->+---------------+
3040253b2ec1SVincenzo Maffione  *        +-------+ |        |               |
3041253b2ec1SVincenzo Maffione  *                  |        |  copy of body |
3042253b2ec1SVincenzo Maffione  *                  |        |               |
3043253b2ec1SVincenzo Maffione  *                  |        +---------------+
3044253b2ec1SVincenzo Maffione  *                  |        |    NULL       |
3045253b2ec1SVincenzo Maffione  *                  |        +---------------+
3046253b2ec1SVincenzo Maffione  *                  |    .---|               |\
3047253b2ec1SVincenzo Maffione  *                  |    |   +---------------+ |
3048253b2ec1SVincenzo Maffione  *                  | .------|               | |
3049253b2ec1SVincenzo Maffione  *                  | |  |   +---------------+  \ option table
3050253b2ec1SVincenzo Maffione  *                  | |  |   |      ...      |  / indexed by option
3051253b2ec1SVincenzo Maffione  *                  | |  |   +---------------+ |  type
3052253b2ec1SVincenzo Maffione  *                  | |  |   |               | |
3053253b2ec1SVincenzo Maffione  *                  | |  |   +---------------+/
3054253b2ec1SVincenzo Maffione  *                  | |  |   |usr next ptr 1 |
3055253b2ec1SVincenzo Maffione  *                  `-|----->+---------------+
3056253b2ec1SVincenzo Maffione  *                    |  |   | copy of opt 1 |
3057253b2ec1SVincenzo Maffione  *                    |  |   |               |
3058253b2ec1SVincenzo Maffione  *                    |  | .-| nro_next      |
3059253b2ec1SVincenzo Maffione  *                    |  | | +---------------+
3060253b2ec1SVincenzo Maffione  *                    |  | | |usr next ptr 2 |
3061253b2ec1SVincenzo Maffione  *                    |  `-`>+---------------+
3062253b2ec1SVincenzo Maffione  *                    |      | copy of opt 2 |
3063253b2ec1SVincenzo Maffione  *                    |      |               |
3064253b2ec1SVincenzo Maffione  *                    |    .-| nro_next      |
3065253b2ec1SVincenzo Maffione  *                    |    | +---------------+
3066253b2ec1SVincenzo Maffione  *                    |    | |               |
3067253b2ec1SVincenzo Maffione  *                    ~    ~ ~      ...      ~
3068253b2ec1SVincenzo Maffione  *                    |    .-|               |
3069253b2ec1SVincenzo Maffione  *                    `----->+---------------+
3070253b2ec1SVincenzo Maffione  *                         | |usr next ptr n |
3071253b2ec1SVincenzo Maffione  *                         `>+---------------+
3072253b2ec1SVincenzo Maffione  *                           | copy of opt n |
3073253b2ec1SVincenzo Maffione  *                           |               |
3074253b2ec1SVincenzo Maffione  *                           | nro_next(NULL)|
3075253b2ec1SVincenzo Maffione  *                           +---------------+
3076253b2ec1SVincenzo Maffione  *
3077253b2ec1SVincenzo Maffione  * The options and body fields of the hdr structure are overwritten
3078253b2ec1SVincenzo Maffione  * with in-kernel valid pointers inside the buf. The original user
3079253b2ec1SVincenzo Maffione  * pointers are saved in the buf and restored on copyout.
3080253b2ec1SVincenzo Maffione  * The list of options is copied and the pointers adjusted. The
3081253b2ec1SVincenzo Maffione  * original pointers are saved before the option they belonged.
3082253b2ec1SVincenzo Maffione  *
3083253b2ec1SVincenzo Maffione  * The option table has an entry for every availabe option.  Entries
3084253b2ec1SVincenzo Maffione  * for options that have not been passed contain NULL.
3085253b2ec1SVincenzo Maffione  *
3086253b2ec1SVincenzo Maffione  */
3087253b2ec1SVincenzo Maffione 
30882ff91c17SVincenzo Maffione int
30892ff91c17SVincenzo Maffione nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user)
30902ff91c17SVincenzo Maffione {
30912ff91c17SVincenzo Maffione 	size_t rqsz, optsz, bufsz;
3092253b2ec1SVincenzo Maffione 	int error = 0;
30932ff91c17SVincenzo Maffione 	char *ker = NULL, *p;
3094253b2ec1SVincenzo Maffione 	struct nmreq_option **next, *src, **opt_tab;
30952ff91c17SVincenzo Maffione 	struct nmreq_option buf;
30962ff91c17SVincenzo Maffione 	uint64_t *ptrs;
30972ff91c17SVincenzo Maffione 
3098b6e66be2SVincenzo Maffione 	if (hdr->nr_reserved) {
3099b6e66be2SVincenzo Maffione 		if (netmap_verbose)
3100b6e66be2SVincenzo Maffione 			nm_prerr("nr_reserved must be zero");
31012ff91c17SVincenzo Maffione 		return EINVAL;
3102b6e66be2SVincenzo Maffione 	}
31032ff91c17SVincenzo Maffione 
31042ff91c17SVincenzo Maffione 	if (!nr_body_is_user)
31052ff91c17SVincenzo Maffione 		return 0;
31062ff91c17SVincenzo Maffione 
31072ff91c17SVincenzo Maffione 	hdr->nr_reserved = nr_body_is_user;
31082ff91c17SVincenzo Maffione 
31092ff91c17SVincenzo Maffione 	/* compute the total size of the buffer */
31102ff91c17SVincenzo Maffione 	rqsz = nmreq_size_by_type(hdr->nr_reqtype);
31112ff91c17SVincenzo Maffione 	if (rqsz > NETMAP_REQ_MAXSIZE) {
31122ff91c17SVincenzo Maffione 		error = EMSGSIZE;
31132ff91c17SVincenzo Maffione 		goto out_err;
31142ff91c17SVincenzo Maffione 	}
3115cfa866f6SMatt Macy 	if ((rqsz && hdr->nr_body == (uintptr_t)NULL) ||
3116cfa866f6SMatt Macy 		(!rqsz && hdr->nr_body != (uintptr_t)NULL)) {
31172ff91c17SVincenzo Maffione 		/* Request body expected, but not found; or
31182ff91c17SVincenzo Maffione 		 * request body found but unexpected. */
3119b6e66be2SVincenzo Maffione 		if (netmap_verbose)
3120b6e66be2SVincenzo Maffione 			nm_prerr("nr_body expected but not found, or vice versa");
31212ff91c17SVincenzo Maffione 		error = EINVAL;
31222ff91c17SVincenzo Maffione 		goto out_err;
31232ff91c17SVincenzo Maffione 	}
31242ff91c17SVincenzo Maffione 
3125253b2ec1SVincenzo Maffione 	bufsz = 2 * sizeof(void *) + rqsz +
3126253b2ec1SVincenzo Maffione 		NETMAP_REQ_OPT_MAX * sizeof(opt_tab);
3127253b2ec1SVincenzo Maffione 	/* compute the size of the buf below the option table.
3128253b2ec1SVincenzo Maffione 	 * It must contain a copy of every received option structure.
3129253b2ec1SVincenzo Maffione 	 * For every option we also need to store a copy of the user
3130253b2ec1SVincenzo Maffione 	 * list pointer.
3131253b2ec1SVincenzo Maffione 	 */
31322ff91c17SVincenzo Maffione 	optsz = 0;
3133cfa866f6SMatt Macy 	for (src = (struct nmreq_option *)(uintptr_t)hdr->nr_options; src;
3134cfa866f6SMatt Macy 	     src = (struct nmreq_option *)(uintptr_t)buf.nro_next)
31352ff91c17SVincenzo Maffione 	{
31362ff91c17SVincenzo Maffione 		error = copyin(src, &buf, sizeof(*src));
31372ff91c17SVincenzo Maffione 		if (error)
31382ff91c17SVincenzo Maffione 			goto out_err;
31392ff91c17SVincenzo Maffione 		optsz += sizeof(*src);
3140b6e66be2SVincenzo Maffione 		optsz += nmreq_opt_size_by_type(buf.nro_reqtype, buf.nro_size);
31412ff91c17SVincenzo Maffione 		if (rqsz + optsz > NETMAP_REQ_MAXSIZE) {
31422ff91c17SVincenzo Maffione 			error = EMSGSIZE;
31432ff91c17SVincenzo Maffione 			goto out_err;
31442ff91c17SVincenzo Maffione 		}
3145253b2ec1SVincenzo Maffione 		bufsz += sizeof(void *);
31462ff91c17SVincenzo Maffione 	}
3147253b2ec1SVincenzo Maffione 	bufsz += optsz;
31482ff91c17SVincenzo Maffione 
31492ff91c17SVincenzo Maffione 	ker = nm_os_malloc(bufsz);
31502ff91c17SVincenzo Maffione 	if (ker == NULL) {
31512ff91c17SVincenzo Maffione 		error = ENOMEM;
31522ff91c17SVincenzo Maffione 		goto out_err;
31532ff91c17SVincenzo Maffione 	}
3154253b2ec1SVincenzo Maffione 	p = ker;	/* write pointer into the buffer */
31552ff91c17SVincenzo Maffione 
31562ff91c17SVincenzo Maffione 	/* make a copy of the user pointers */
31572ff91c17SVincenzo Maffione 	ptrs = (uint64_t*)p;
31582ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_body;
31592ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_options;
31602ff91c17SVincenzo Maffione 	p = (char *)ptrs;
31612ff91c17SVincenzo Maffione 
31622ff91c17SVincenzo Maffione 	/* copy the body */
3163cfa866f6SMatt Macy 	error = copyin((void *)(uintptr_t)hdr->nr_body, p, rqsz);
31642ff91c17SVincenzo Maffione 	if (error)
31652ff91c17SVincenzo Maffione 		goto out_restore;
31662ff91c17SVincenzo Maffione 	/* overwrite the user pointer with the in-kernel one */
3167cfa866f6SMatt Macy 	hdr->nr_body = (uintptr_t)p;
31682ff91c17SVincenzo Maffione 	p += rqsz;
3169253b2ec1SVincenzo Maffione 	/* start of the options table */
3170253b2ec1SVincenzo Maffione 	opt_tab = (struct nmreq_option **)p;
3171253b2ec1SVincenzo Maffione 	p += sizeof(opt_tab) * NETMAP_REQ_OPT_MAX;
31722ff91c17SVincenzo Maffione 
31732ff91c17SVincenzo Maffione 	/* copy the options */
31742ff91c17SVincenzo Maffione 	next = (struct nmreq_option **)&hdr->nr_options;
31752ff91c17SVincenzo Maffione 	src = *next;
31762ff91c17SVincenzo Maffione 	while (src) {
31772ff91c17SVincenzo Maffione 		struct nmreq_option *opt;
31782ff91c17SVincenzo Maffione 
31792ff91c17SVincenzo Maffione 		/* copy the option header */
31802ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)p;
31812ff91c17SVincenzo Maffione 		opt = (struct nmreq_option *)(ptrs + 1);
31822ff91c17SVincenzo Maffione 		error = copyin(src, opt, sizeof(*src));
31832ff91c17SVincenzo Maffione 		if (error)
31842ff91c17SVincenzo Maffione 			goto out_restore;
31852ff91c17SVincenzo Maffione 		/* make a copy of the user next pointer */
31862ff91c17SVincenzo Maffione 		*ptrs = opt->nro_next;
31872ff91c17SVincenzo Maffione 		/* overwrite the user pointer with the in-kernel one */
31882ff91c17SVincenzo Maffione 		*next = opt;
31892ff91c17SVincenzo Maffione 
31902ff91c17SVincenzo Maffione 		/* initialize the option as not supported.
31912ff91c17SVincenzo Maffione 		 * Recognized options will update this field.
31922ff91c17SVincenzo Maffione 		 */
31932ff91c17SVincenzo Maffione 		opt->nro_status = EOPNOTSUPP;
31942ff91c17SVincenzo Maffione 
3195253b2ec1SVincenzo Maffione 		/* check for invalid types */
3196253b2ec1SVincenzo Maffione 		if (opt->nro_reqtype < 1) {
3197253b2ec1SVincenzo Maffione 			if (netmap_verbose)
3198253b2ec1SVincenzo Maffione 				nm_prinf("invalid option type: %u", opt->nro_reqtype);
3199253b2ec1SVincenzo Maffione 			opt->nro_status = EINVAL;
3200253b2ec1SVincenzo Maffione 			error = EINVAL;
3201253b2ec1SVincenzo Maffione 			goto next;
3202253b2ec1SVincenzo Maffione 		}
3203253b2ec1SVincenzo Maffione 
3204253b2ec1SVincenzo Maffione 		if (opt->nro_reqtype >= NETMAP_REQ_OPT_MAX) {
3205253b2ec1SVincenzo Maffione 			/* opt->nro_status is already EOPNOTSUPP */
3206253b2ec1SVincenzo Maffione 			error = EOPNOTSUPP;
3207253b2ec1SVincenzo Maffione 			goto next;
3208253b2ec1SVincenzo Maffione 		}
3209253b2ec1SVincenzo Maffione 
3210253b2ec1SVincenzo Maffione 		/* if the type is valid, index the option in the table
3211253b2ec1SVincenzo Maffione 		 * unless it is a duplicate.
3212253b2ec1SVincenzo Maffione 		 */
3213253b2ec1SVincenzo Maffione 		if (opt_tab[opt->nro_reqtype] != NULL) {
3214253b2ec1SVincenzo Maffione 			if (netmap_verbose)
3215253b2ec1SVincenzo Maffione 				nm_prinf("duplicate option: %u", opt->nro_reqtype);
3216253b2ec1SVincenzo Maffione 			opt->nro_status = EINVAL;
3217253b2ec1SVincenzo Maffione 			opt_tab[opt->nro_reqtype]->nro_status = EINVAL;
3218253b2ec1SVincenzo Maffione 			error = EINVAL;
3219253b2ec1SVincenzo Maffione 			goto next;
3220253b2ec1SVincenzo Maffione 		}
3221253b2ec1SVincenzo Maffione 		opt_tab[opt->nro_reqtype] = opt;
3222253b2ec1SVincenzo Maffione 
32232ff91c17SVincenzo Maffione 		p = (char *)(opt + 1);
32242ff91c17SVincenzo Maffione 
32252ff91c17SVincenzo Maffione 		/* copy the option body */
3226b6e66be2SVincenzo Maffione 		optsz = nmreq_opt_size_by_type(opt->nro_reqtype,
3227b6e66be2SVincenzo Maffione 						opt->nro_size);
32282ff91c17SVincenzo Maffione 		if (optsz) {
32292ff91c17SVincenzo Maffione 			/* the option body follows the option header */
32302ff91c17SVincenzo Maffione 			error = copyin(src + 1, p, optsz);
32312ff91c17SVincenzo Maffione 			if (error)
32322ff91c17SVincenzo Maffione 				goto out_restore;
32332ff91c17SVincenzo Maffione 			p += optsz;
32342ff91c17SVincenzo Maffione 		}
32352ff91c17SVincenzo Maffione 
3236253b2ec1SVincenzo Maffione 	next:
32372ff91c17SVincenzo Maffione 		/* move to next option */
32382ff91c17SVincenzo Maffione 		next = (struct nmreq_option **)&opt->nro_next;
32392ff91c17SVincenzo Maffione 		src = *next;
32402ff91c17SVincenzo Maffione 	}
3241253b2ec1SVincenzo Maffione 	if (error)
3242253b2ec1SVincenzo Maffione 		nmreq_copyout(hdr, error);
3243253b2ec1SVincenzo Maffione 	return error;
32442ff91c17SVincenzo Maffione 
32452ff91c17SVincenzo Maffione out_restore:
32462ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker;
32472ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
32482ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs++;
32492ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
32502ff91c17SVincenzo Maffione 	nm_os_free(ker);
32512ff91c17SVincenzo Maffione out_err:
32522ff91c17SVincenzo Maffione 	return error;
32532ff91c17SVincenzo Maffione }
32542ff91c17SVincenzo Maffione 
32552ff91c17SVincenzo Maffione static int
32562ff91c17SVincenzo Maffione nmreq_copyout(struct nmreq_header *hdr, int rerror)
32572ff91c17SVincenzo Maffione {
32582ff91c17SVincenzo Maffione 	struct nmreq_option *src, *dst;
3259cfa866f6SMatt Macy 	void *ker = (void *)(uintptr_t)hdr->nr_body, *bufstart;
32602ff91c17SVincenzo Maffione 	uint64_t *ptrs;
32612ff91c17SVincenzo Maffione 	size_t bodysz;
32622ff91c17SVincenzo Maffione 	int error;
32632ff91c17SVincenzo Maffione 
32642ff91c17SVincenzo Maffione 	if (!hdr->nr_reserved)
32652ff91c17SVincenzo Maffione 		return rerror;
32662ff91c17SVincenzo Maffione 
32672ff91c17SVincenzo Maffione 	/* restore the user pointers in the header */
32682ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker - 2;
32692ff91c17SVincenzo Maffione 	bufstart = ptrs;
32702ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
3271cfa866f6SMatt Macy 	src = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
32722ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs;
32732ff91c17SVincenzo Maffione 
32742ff91c17SVincenzo Maffione 	if (!rerror) {
32752ff91c17SVincenzo Maffione 		/* copy the body */
32762ff91c17SVincenzo Maffione 		bodysz = nmreq_size_by_type(hdr->nr_reqtype);
3277cfa866f6SMatt Macy 		error = copyout(ker, (void *)(uintptr_t)hdr->nr_body, bodysz);
32782ff91c17SVincenzo Maffione 		if (error) {
32792ff91c17SVincenzo Maffione 			rerror = error;
32802ff91c17SVincenzo Maffione 			goto out;
32812ff91c17SVincenzo Maffione 		}
32822ff91c17SVincenzo Maffione 	}
32832ff91c17SVincenzo Maffione 
32842ff91c17SVincenzo Maffione 	/* copy the options */
3285cfa866f6SMatt Macy 	dst = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
32862ff91c17SVincenzo Maffione 	while (src) {
32872ff91c17SVincenzo Maffione 		size_t optsz;
32882ff91c17SVincenzo Maffione 		uint64_t next;
32892ff91c17SVincenzo Maffione 
32902ff91c17SVincenzo Maffione 		/* restore the user pointer */
32912ff91c17SVincenzo Maffione 		next = src->nro_next;
32922ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)src - 1;
32932ff91c17SVincenzo Maffione 		src->nro_next = *ptrs;
32942ff91c17SVincenzo Maffione 
32952ff91c17SVincenzo Maffione 		/* always copy the option header */
32962ff91c17SVincenzo Maffione 		error = copyout(src, dst, sizeof(*src));
32972ff91c17SVincenzo Maffione 		if (error) {
32982ff91c17SVincenzo Maffione 			rerror = error;
32992ff91c17SVincenzo Maffione 			goto out;
33002ff91c17SVincenzo Maffione 		}
33012ff91c17SVincenzo Maffione 
33022ff91c17SVincenzo Maffione 		/* copy the option body only if there was no error */
33032ff91c17SVincenzo Maffione 		if (!rerror && !src->nro_status) {
3304b6e66be2SVincenzo Maffione 			optsz = nmreq_opt_size_by_type(src->nro_reqtype,
3305b6e66be2SVincenzo Maffione 							src->nro_size);
33062ff91c17SVincenzo Maffione 			if (optsz) {
33072ff91c17SVincenzo Maffione 				error = copyout(src + 1, dst + 1, optsz);
33082ff91c17SVincenzo Maffione 				if (error) {
33092ff91c17SVincenzo Maffione 					rerror = error;
33102ff91c17SVincenzo Maffione 					goto out;
33112ff91c17SVincenzo Maffione 				}
33122ff91c17SVincenzo Maffione 			}
33132ff91c17SVincenzo Maffione 		}
3314cfa866f6SMatt Macy 		src = (struct nmreq_option *)(uintptr_t)next;
3315cfa866f6SMatt Macy 		dst = (struct nmreq_option *)(uintptr_t)*ptrs;
33162ff91c17SVincenzo Maffione 	}
33172ff91c17SVincenzo Maffione 
33182ff91c17SVincenzo Maffione 
33192ff91c17SVincenzo Maffione out:
33202ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
33212ff91c17SVincenzo Maffione 	nm_os_free(bufstart);
33222ff91c17SVincenzo Maffione 	return rerror;
33232ff91c17SVincenzo Maffione }
33242ff91c17SVincenzo Maffione 
33252ff91c17SVincenzo Maffione struct nmreq_option *
3326253b2ec1SVincenzo Maffione nmreq_getoption(struct nmreq_header *hdr, uint16_t reqtype)
33272ff91c17SVincenzo Maffione {
3328253b2ec1SVincenzo Maffione 	struct nmreq_option **opt_tab;
3329253b2ec1SVincenzo Maffione 
3330253b2ec1SVincenzo Maffione 	if (!hdr->nr_options)
33312ff91c17SVincenzo Maffione 		return NULL;
33322ff91c17SVincenzo Maffione 
3333760fa2abSVincenzo Maffione 	opt_tab = (struct nmreq_option **)((uintptr_t)hdr->nr_options) -
3334760fa2abSVincenzo Maffione 	    (NETMAP_REQ_OPT_MAX + 1);
3335253b2ec1SVincenzo Maffione 	return opt_tab[reqtype];
33362ff91c17SVincenzo Maffione }
33372ff91c17SVincenzo Maffione 
33382ff91c17SVincenzo Maffione static int
33392ff91c17SVincenzo Maffione nmreq_checkoptions(struct nmreq_header *hdr)
33402ff91c17SVincenzo Maffione {
33412ff91c17SVincenzo Maffione 	struct nmreq_option *opt;
33422ff91c17SVincenzo Maffione 	/* return error if there is still any option
33432ff91c17SVincenzo Maffione 	 * marked as not supported
33442ff91c17SVincenzo Maffione 	 */
33452ff91c17SVincenzo Maffione 
3346cfa866f6SMatt Macy 	for (opt = (struct nmreq_option *)(uintptr_t)hdr->nr_options; opt;
3347cfa866f6SMatt Macy 	     opt = (struct nmreq_option *)(uintptr_t)opt->nro_next)
33482ff91c17SVincenzo Maffione 		if (opt->nro_status == EOPNOTSUPP)
33492ff91c17SVincenzo Maffione 			return EOPNOTSUPP;
33502ff91c17SVincenzo Maffione 
33512ff91c17SVincenzo Maffione 	return 0;
33522ff91c17SVincenzo Maffione }
335368b8534bSLuigi Rizzo 
335468b8534bSLuigi Rizzo /*
335568b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
335668b8534bSLuigi Rizzo  *
335768b8534bSLuigi Rizzo  * Can be called for one or more queues.
335868b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
33598c9874f5SVincenzo Maffione  * If there are no ready events (and 'sr' is not NULL), do a
33608c9874f5SVincenzo Maffione  * selrecord on either individual selinfo or on the global one.
336168b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
336268b8534bSLuigi Rizzo  * are done through callbacks.
3363f196ce38SLuigi Rizzo  *
336401c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
336501c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
336601c7d25fSLuigi Rizzo  * hidden argument.
336768b8534bSLuigi Rizzo  */
3368f9790aebSLuigi Rizzo int
336937e3a6d3SLuigi Rizzo netmap_poll(struct netmap_priv_d *priv, int events, NM_SELRECORD_T *sr)
337068b8534bSLuigi Rizzo {
337168b8534bSLuigi Rizzo 	struct netmap_adapter *na;
337268b8534bSLuigi Rizzo 	struct netmap_kring *kring;
337337e3a6d3SLuigi Rizzo 	struct netmap_ring *ring;
3374b6e66be2SVincenzo Maffione 	u_int i, want[NR_TXRX], revents = 0;
3375b6e66be2SVincenzo Maffione 	NM_SELINFO_T *si[NR_TXRX];
3376847bf383SLuigi Rizzo #define want_tx want[NR_TX]
3377847bf383SLuigi Rizzo #define want_rx want[NR_RX]
3378c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
337901c7d25fSLuigi Rizzo 
3380f9790aebSLuigi Rizzo 	/*
3381f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
3382f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
3383f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
3384f9790aebSLuigi Rizzo 	 */
338517885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
3386ce3ee1e7SLuigi Rizzo 
3387c3e9b4dbSLuiz Otavio O Souza 	/* Transparent mode: send_down is 1 if we have found some
3388c3e9b4dbSLuiz Otavio O Souza 	 * packets to forward (host RX ring --> NIC) during the rx
3389c3e9b4dbSLuiz Otavio O Souza 	 * scan and we have not sent them down to the NIC yet.
3390c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode requires to bind all rings to a single
3391c3e9b4dbSLuiz Otavio O Souza 	 * file descriptor.
3392f0ea3689SLuigi Rizzo 	 */
339337e3a6d3SLuigi Rizzo 	int send_down = 0;
3394c3e9b4dbSLuiz Otavio O Souza 	int sync_flags = priv->np_sync_flags;
339537e3a6d3SLuigi Rizzo 
339637e3a6d3SLuigi Rizzo 	mbq_init(&q);
339768b8534bSLuigi Rizzo 
3398b6e66be2SVincenzo Maffione 	if (unlikely(priv->np_nifp == NULL)) {
33998241616dSLuigi Rizzo 		return POLLERR;
34008241616dSLuigi Rizzo 	}
3401847bf383SLuigi Rizzo 	mb(); /* make sure following reads are not from cache */
34028241616dSLuigi Rizzo 
3403f9790aebSLuigi Rizzo 	na = priv->np_na;
3404f9790aebSLuigi Rizzo 
3405b6e66be2SVincenzo Maffione 	if (unlikely(!nm_netmap_on(na)))
340668b8534bSLuigi Rizzo 		return POLLERR;
340768b8534bSLuigi Rizzo 
3408b6e66be2SVincenzo Maffione 	if (unlikely(priv->np_csb_atok_base)) {
3409b6e66be2SVincenzo Maffione 		nm_prerr("Invalid poll in CSB mode");
3410b6e66be2SVincenzo Maffione 		return POLLERR;
3411b6e66be2SVincenzo Maffione 	}
3412b6e66be2SVincenzo Maffione 
3413b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_ON)
3414b6e66be2SVincenzo Maffione 		nm_prinf("device %s events 0x%x", na->name, events);
341568b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
341668b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
341768b8534bSLuigi Rizzo 
341868b8534bSLuigi Rizzo 	/*
3419b6e66be2SVincenzo Maffione 	 * If the card has more than one queue AND the file descriptor is
3420b6e66be2SVincenzo Maffione 	 * bound to all of them, we sleep on the "global" selinfo, otherwise
3421b6e66be2SVincenzo Maffione 	 * we sleep on individual selinfo (FreeBSD only allows two selinfo's
3422b6e66be2SVincenzo Maffione 	 * per file descriptor).
3423ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
3424ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
342568b8534bSLuigi Rizzo 	 *
342668b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
342768b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
342868b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
342968b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
343068b8534bSLuigi Rizzo 	 */
34315faab778SVincenzo Maffione 	si[NR_RX] = priv->np_si[NR_RX];
34325faab778SVincenzo Maffione 	si[NR_TX] = priv->np_si[NR_TX];
343364ae02c3SLuigi Rizzo 
34344f80b14cSVincenzo Maffione #ifdef __FreeBSD__
343568b8534bSLuigi Rizzo 	/*
3436f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
3437f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
34384f80b14cSVincenzo Maffione 	 * routines. We can't do this on Linux, as the contract says
34394f80b14cSVincenzo Maffione 	 * that we must call nm_os_selrecord() unconditionally.
344068b8534bSLuigi Rizzo 	 */
344137e3a6d3SLuigi Rizzo 	if (want_tx) {
344258e18542SVincenzo Maffione 		const enum txrx t = NR_TX;
344358e18542SVincenzo Maffione 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
34442ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
344558e18542SVincenzo Maffione 			if (kring->ring->cur != kring->ring->tail) {
344658e18542SVincenzo Maffione 				/* Some unseen TX space is available, so what
344758e18542SVincenzo Maffione 				 * we don't need to run txsync. */
344837e3a6d3SLuigi Rizzo 				revents |= want[t];
344958e18542SVincenzo Maffione 				want[t] = 0;
345058e18542SVincenzo Maffione 				break;
345137e3a6d3SLuigi Rizzo 			}
345237e3a6d3SLuigi Rizzo 		}
345337e3a6d3SLuigi Rizzo 	}
345437e3a6d3SLuigi Rizzo 	if (want_rx) {
345558e18542SVincenzo Maffione 		const enum txrx t = NR_RX;
3456e1ed1fbdSVincenzo Maffione 		int rxsync_needed = 0;
3457e1ed1fbdSVincenzo Maffione 
345837e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
34592ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
346058e18542SVincenzo Maffione 			if (kring->ring->cur == kring->ring->tail
346158e18542SVincenzo Maffione 				|| kring->rhead != kring->ring->head) {
346258e18542SVincenzo Maffione 				/* There are no unseen packets on this ring,
346358e18542SVincenzo Maffione 				 * or there are some buffers to be returned
346458e18542SVincenzo Maffione 				 * to the netmap port. We therefore go ahead
346558e18542SVincenzo Maffione 				 * and run rxsync. */
3466e1ed1fbdSVincenzo Maffione 				rxsync_needed = 1;
3467e1ed1fbdSVincenzo Maffione 				break;
346837e3a6d3SLuigi Rizzo 			}
346937e3a6d3SLuigi Rizzo 		}
347058e18542SVincenzo Maffione 		if (!rxsync_needed) {
347158e18542SVincenzo Maffione 			revents |= want_rx;
347258e18542SVincenzo Maffione 			want_rx = 0;
347358e18542SVincenzo Maffione 		}
347437e3a6d3SLuigi Rizzo 	}
34754f80b14cSVincenzo Maffione #endif
34764f80b14cSVincenzo Maffione 
34774f80b14cSVincenzo Maffione #ifdef linux
34784f80b14cSVincenzo Maffione 	/* The selrecord must be unconditional on linux. */
3479b6e66be2SVincenzo Maffione 	nm_os_selrecord(sr, si[NR_RX]);
3480b6e66be2SVincenzo Maffione 	nm_os_selrecord(sr, si[NR_TX]);
34814f80b14cSVincenzo Maffione #endif /* linux */
348268b8534bSLuigi Rizzo 
348368b8534bSLuigi Rizzo 	/*
348417885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
348517885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
348617885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
3487f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
348868b8534bSLuigi Rizzo 	 */
348968b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
349017885a7bSLuigi Rizzo 		/*
349117885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
349217885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
349317885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
349417885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
3495ce3ee1e7SLuigi Rizzo 		 */
3496091fd0abSLuigi Rizzo flush_tx:
349737e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[NR_TX]; i < priv->np_qlast[NR_TX]; i++) {
349817885a7bSLuigi Rizzo 			int found = 0;
349917885a7bSLuigi Rizzo 
35002ff91c17SVincenzo Maffione 			kring = na->tx_rings[i];
350137e3a6d3SLuigi Rizzo 			ring = kring->ring;
350237e3a6d3SLuigi Rizzo 
35034f80b14cSVincenzo Maffione 			/*
35044f80b14cSVincenzo Maffione 			 * Don't try to txsync this TX ring if we already found some
35054f80b14cSVincenzo Maffione 			 * space in some of the TX rings (want_tx == 0) and there are no
35064f80b14cSVincenzo Maffione 			 * TX slots in this ring that need to be flushed to the NIC
35072ff91c17SVincenzo Maffione 			 * (head == hwcur).
35084f80b14cSVincenzo Maffione 			 */
35092ff91c17SVincenzo Maffione 			if (!send_down && !want_tx && ring->head == kring->nr_hwcur)
351068b8534bSLuigi Rizzo 				continue;
351137e3a6d3SLuigi Rizzo 
351237e3a6d3SLuigi Rizzo 			if (nm_kr_tryget(kring, 1, &revents))
351317885a7bSLuigi Rizzo 				continue;
351437e3a6d3SLuigi Rizzo 
351537e3a6d3SLuigi Rizzo 			if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
351617885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
351717885a7bSLuigi Rizzo 				revents |= POLLERR;
351817885a7bSLuigi Rizzo 			} else {
3519c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags))
352068b8534bSLuigi Rizzo 					revents |= POLLERR;
3521847bf383SLuigi Rizzo 				else
352237e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
352317885a7bSLuigi Rizzo 			}
352468b8534bSLuigi Rizzo 
352517885a7bSLuigi Rizzo 			/*
352617885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
352717885a7bSLuigi Rizzo 			 * listeners on the same ring.
352817885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
352917885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
3530f9790aebSLuigi Rizzo 			 */
353117885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
353217885a7bSLuigi Rizzo 			nm_kr_put(kring);
353317885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
353468b8534bSLuigi Rizzo 				revents |= want_tx;
353568b8534bSLuigi Rizzo 				want_tx = 0;
35364f80b14cSVincenzo Maffione #ifndef linux
3537847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
35384f80b14cSVincenzo Maffione #endif /* linux */
353968b8534bSLuigi Rizzo 			}
3540ce3ee1e7SLuigi Rizzo 		}
354137e3a6d3SLuigi Rizzo 		/* if there were any packet to forward we must have handled them by now */
354237e3a6d3SLuigi Rizzo 		send_down = 0;
354337e3a6d3SLuigi Rizzo 		if (want_tx && retry_tx && sr) {
35444f80b14cSVincenzo Maffione #ifndef linux
3545b6e66be2SVincenzo Maffione 			nm_os_selrecord(sr, si[NR_TX]);
35464f80b14cSVincenzo Maffione #endif /* !linux */
3547ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
3548ce3ee1e7SLuigi Rizzo 			goto flush_tx;
354968b8534bSLuigi Rizzo 		}
355068b8534bSLuigi Rizzo 	}
355168b8534bSLuigi Rizzo 
355268b8534bSLuigi Rizzo 	/*
355317885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
355468b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
355568b8534bSLuigi Rizzo 	 */
355668b8534bSLuigi Rizzo 	if (want_rx) {
355789cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
3558ce3ee1e7SLuigi Rizzo do_retry_rx:
3559847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[NR_RX]; i < priv->np_qlast[NR_RX]; i++) {
356017885a7bSLuigi Rizzo 			int found = 0;
356117885a7bSLuigi Rizzo 
35622ff91c17SVincenzo Maffione 			kring = na->rx_rings[i];
356337e3a6d3SLuigi Rizzo 			ring = kring->ring;
3564ce3ee1e7SLuigi Rizzo 
356537e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &revents)))
356617885a7bSLuigi Rizzo 				continue;
3567ce3ee1e7SLuigi Rizzo 
356837e3a6d3SLuigi Rizzo 			if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
3569847bf383SLuigi Rizzo 				netmap_ring_reinit(kring);
3570847bf383SLuigi Rizzo 				revents |= POLLERR;
3571847bf383SLuigi Rizzo 			}
3572847bf383SLuigi Rizzo 			/* now we can use kring->rcur, rtail */
3573847bf383SLuigi Rizzo 
357417885a7bSLuigi Rizzo 			/*
3575c3e9b4dbSLuiz Otavio O Souza 			 * transparent mode support: collect packets from
3576c3e9b4dbSLuiz Otavio O Souza 			 * hw rxring(s) that have been released by the user
3577ce3ee1e7SLuigi Rizzo 			 */
357837e3a6d3SLuigi Rizzo 			if (nm_may_forward_up(kring)) {
3579091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
3580091fd0abSLuigi Rizzo 			}
358168b8534bSLuigi Rizzo 
3582c3e9b4dbSLuiz Otavio O Souza 			/* Clear the NR_FORWARD flag anyway, it may be set by
3583c3e9b4dbSLuiz Otavio O Souza 			 * the nm_sync() below only on for the host RX ring (see
3584c3e9b4dbSLuiz Otavio O Souza 			 * netmap_rxsync_from_host()). */
358537e3a6d3SLuigi Rizzo 			kring->nr_kflags &= ~NR_FORWARD;
3586c3e9b4dbSLuiz Otavio O Souza 			if (kring->nm_sync(kring, sync_flags))
358768b8534bSLuigi Rizzo 				revents |= POLLERR;
3588847bf383SLuigi Rizzo 			else
358937e3a6d3SLuigi Rizzo 				nm_sync_finalize(kring);
3590c3e9b4dbSLuiz Otavio O Souza 			send_down |= (kring->nr_kflags & NR_FORWARD);
3591c3e9b4dbSLuiz Otavio O Souza 			ring_timestamp_set(ring);
359217885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
359317885a7bSLuigi Rizzo 			nm_kr_put(kring);
359417885a7bSLuigi Rizzo 			if (found) {
359568b8534bSLuigi Rizzo 				revents |= want_rx;
3596ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
35974f80b14cSVincenzo Maffione #ifndef linux
3598847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
35994f80b14cSVincenzo Maffione #endif /* linux */
360068b8534bSLuigi Rizzo 			}
360168b8534bSLuigi Rizzo 		}
360217885a7bSLuigi Rizzo 
36034f80b14cSVincenzo Maffione #ifndef linux
360437e3a6d3SLuigi Rizzo 		if (retry_rx && sr) {
3605b6e66be2SVincenzo Maffione 			nm_os_selrecord(sr, si[NR_RX]);
360637e3a6d3SLuigi Rizzo 		}
36074f80b14cSVincenzo Maffione #endif /* !linux */
3608c3e9b4dbSLuiz Otavio O Souza 		if (send_down || retry_rx) {
360917885a7bSLuigi Rizzo 			retry_rx = 0;
361017885a7bSLuigi Rizzo 			if (send_down)
361117885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
361217885a7bSLuigi Rizzo 			else
3613ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
3614ce3ee1e7SLuigi Rizzo 		}
361568b8534bSLuigi Rizzo 	}
3616091fd0abSLuigi Rizzo 
361717885a7bSLuigi Rizzo 	/*
3618c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode: released bufs (i.e. between kring->nr_hwcur and
3619c3e9b4dbSLuiz Otavio O Souza 	 * ring->head) marked with NS_FORWARD on hw rx rings are passed up
3620c3e9b4dbSLuiz Otavio O Souza 	 * to the host stack.
3621ce3ee1e7SLuigi Rizzo 	 */
3622091fd0abSLuigi Rizzo 
3623c3e9b4dbSLuiz Otavio O Souza 	if (mbq_peek(&q)) {
3624f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
362537e3a6d3SLuigi Rizzo 	}
362668b8534bSLuigi Rizzo 
362768b8534bSLuigi Rizzo 	return (revents);
3628847bf383SLuigi Rizzo #undef want_tx
3629847bf383SLuigi Rizzo #undef want_rx
363068b8534bSLuigi Rizzo }
363168b8534bSLuigi Rizzo 
36324f80b14cSVincenzo Maffione int
36334f80b14cSVincenzo Maffione nma_intr_enable(struct netmap_adapter *na, int onoff)
36344f80b14cSVincenzo Maffione {
36354f80b14cSVincenzo Maffione 	bool changed = false;
36364f80b14cSVincenzo Maffione 	enum txrx t;
36374f80b14cSVincenzo Maffione 	int i;
36384f80b14cSVincenzo Maffione 
36394f80b14cSVincenzo Maffione 	for_rx_tx(t) {
36404f80b14cSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t); i++) {
36412ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
36424f80b14cSVincenzo Maffione 			int on = !(kring->nr_kflags & NKR_NOINTR);
36434f80b14cSVincenzo Maffione 
36444f80b14cSVincenzo Maffione 			if (!!onoff != !!on) {
36454f80b14cSVincenzo Maffione 				changed = true;
36464f80b14cSVincenzo Maffione 			}
36474f80b14cSVincenzo Maffione 			if (onoff) {
36484f80b14cSVincenzo Maffione 				kring->nr_kflags &= ~NKR_NOINTR;
36494f80b14cSVincenzo Maffione 			} else {
36504f80b14cSVincenzo Maffione 				kring->nr_kflags |= NKR_NOINTR;
36514f80b14cSVincenzo Maffione 			}
36524f80b14cSVincenzo Maffione 		}
36534f80b14cSVincenzo Maffione 	}
36544f80b14cSVincenzo Maffione 
36554f80b14cSVincenzo Maffione 	if (!changed) {
36564f80b14cSVincenzo Maffione 		return 0; /* nothing to do */
36574f80b14cSVincenzo Maffione 	}
36584f80b14cSVincenzo Maffione 
36594f80b14cSVincenzo Maffione 	if (!na->nm_intr) {
3660b6e66be2SVincenzo Maffione 		nm_prerr("Cannot %s interrupts for %s", onoff ? "enable" : "disable",
36614f80b14cSVincenzo Maffione 		  na->name);
36624f80b14cSVincenzo Maffione 		return -1;
36634f80b14cSVincenzo Maffione 	}
36644f80b14cSVincenzo Maffione 
36654f80b14cSVincenzo Maffione 	na->nm_intr(na, onoff);
36664f80b14cSVincenzo Maffione 
36674f80b14cSVincenzo Maffione 	return 0;
36684f80b14cSVincenzo Maffione }
36694f80b14cSVincenzo Maffione 
367017885a7bSLuigi Rizzo 
367117885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
367268b8534bSLuigi Rizzo 
367389cc2556SLuigi Rizzo /* default notify callback */
3674f9790aebSLuigi Rizzo static int
3675847bf383SLuigi Rizzo netmap_notify(struct netmap_kring *kring, int flags)
3676f9790aebSLuigi Rizzo {
36772ff91c17SVincenzo Maffione 	struct netmap_adapter *na = kring->notify_na;
3678847bf383SLuigi Rizzo 	enum txrx t = kring->tx;
3679f9790aebSLuigi Rizzo 
368037e3a6d3SLuigi Rizzo 	nm_os_selwakeup(&kring->si);
368189cc2556SLuigi Rizzo 	/* optimization: avoid a wake up on the global
368289cc2556SLuigi Rizzo 	 * queue if nobody has registered for more
368389cc2556SLuigi Rizzo 	 * than one ring
368489cc2556SLuigi Rizzo 	 */
3685847bf383SLuigi Rizzo 	if (na->si_users[t] > 0)
368637e3a6d3SLuigi Rizzo 		nm_os_selwakeup(&na->si[t]);
3687847bf383SLuigi Rizzo 
368837e3a6d3SLuigi Rizzo 	return NM_IRQ_COMPLETED;
3689f9790aebSLuigi Rizzo }
3690f9790aebSLuigi Rizzo 
369189cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
369237e3a6d3SLuigi Rizzo  * provide some defaults and get a reference to the
369337e3a6d3SLuigi Rizzo  * memory allocator
369489cc2556SLuigi Rizzo  */
3695f9790aebSLuigi Rizzo int
3696f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
3697f9790aebSLuigi Rizzo {
36982ff91c17SVincenzo Maffione 	if (!na->rx_buf_maxsize) {
36992ff91c17SVincenzo Maffione 		/* Set a conservative default (larger is safer). */
37002ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = PAGE_SIZE;
37012ff91c17SVincenzo Maffione 	}
37022ff91c17SVincenzo Maffione 
370317885a7bSLuigi Rizzo #ifdef __FreeBSD__
370437e3a6d3SLuigi Rizzo 	if (na->na_flags & NAF_HOST_RINGS && na->ifp) {
370537e3a6d3SLuigi Rizzo 		na->if_input = na->ifp->if_input; /* for netmap_send_up */
37064bf50f18SLuigi Rizzo 	}
37074f80b14cSVincenzo Maffione 	na->pdev = na; /* make sure netmap_mem_map() is called */
370837e3a6d3SLuigi Rizzo #endif /* __FreeBSD__ */
37092a7db7a6SVincenzo Maffione 	if (na->na_flags & NAF_HOST_RINGS) {
37102a7db7a6SVincenzo Maffione 		if (na->num_host_rx_rings == 0)
37112a7db7a6SVincenzo Maffione 			na->num_host_rx_rings = 1;
37122a7db7a6SVincenzo Maffione 		if (na->num_host_tx_rings == 0)
37132a7db7a6SVincenzo Maffione 			na->num_host_tx_rings = 1;
37142a7db7a6SVincenzo Maffione 	}
3715f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
371689cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
371789cc2556SLuigi Rizzo 		 * since other port types all provide their own
371889cc2556SLuigi Rizzo 		 * nm_krings_create
371989cc2556SLuigi Rizzo 		 */
3720f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
372117885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
3722f9790aebSLuigi Rizzo 	}
3723f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
3724f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
3725f9790aebSLuigi Rizzo 	na->active_fds = 0;
3726f9790aebSLuigi Rizzo 
3727c3e9b4dbSLuiz Otavio O Souza 	if (na->nm_mem == NULL) {
37284bf50f18SLuigi Rizzo 		/* use the global allocator */
3729c3e9b4dbSLuiz Otavio O Souza 		na->nm_mem = netmap_mem_get(&nm_mem);
3730c3e9b4dbSLuiz Otavio O Souza 	}
3731847bf383SLuigi Rizzo #ifdef WITH_VALE
37324bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
37334bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
37344bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
37354bf50f18SLuigi Rizzo 		 */
37362a7db7a6SVincenzo Maffione 		na->nm_bdg_attach = netmap_default_bdg_attach;
3737847bf383SLuigi Rizzo #endif
373837e3a6d3SLuigi Rizzo 
3739f9790aebSLuigi Rizzo 	return 0;
3740f9790aebSLuigi Rizzo }
3741f9790aebSLuigi Rizzo 
374237e3a6d3SLuigi Rizzo /* Wrapper for the register callback provided netmap-enabled
374337e3a6d3SLuigi Rizzo  * hardware drivers.
374437e3a6d3SLuigi Rizzo  * nm_iszombie(na) means that the driver module has been
37454bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
374637e3a6d3SLuigi Rizzo  * nm_os_ifnet_lock() must guarantee mutual exclusion with
374737e3a6d3SLuigi Rizzo  * module unloading.
37484bf50f18SLuigi Rizzo  */
37494bf50f18SLuigi Rizzo static int
375037e3a6d3SLuigi Rizzo netmap_hw_reg(struct netmap_adapter *na, int onoff)
37514bf50f18SLuigi Rizzo {
37524bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
37534bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
375437e3a6d3SLuigi Rizzo 	int error = 0;
37554bf50f18SLuigi Rizzo 
375637e3a6d3SLuigi Rizzo 	nm_os_ifnet_lock();
37574bf50f18SLuigi Rizzo 
375837e3a6d3SLuigi Rizzo 	if (nm_iszombie(na)) {
375937e3a6d3SLuigi Rizzo 		if (onoff) {
376037e3a6d3SLuigi Rizzo 			error = ENXIO;
376137e3a6d3SLuigi Rizzo 		} else if (na != NULL) {
376237e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_NETMAP_ON;
376337e3a6d3SLuigi Rizzo 		}
376437e3a6d3SLuigi Rizzo 		goto out;
376537e3a6d3SLuigi Rizzo 	}
376637e3a6d3SLuigi Rizzo 
376737e3a6d3SLuigi Rizzo 	error = hwna->nm_hw_register(na, onoff);
376837e3a6d3SLuigi Rizzo 
376937e3a6d3SLuigi Rizzo out:
377037e3a6d3SLuigi Rizzo 	nm_os_ifnet_unlock();
377137e3a6d3SLuigi Rizzo 
377237e3a6d3SLuigi Rizzo 	return error;
377337e3a6d3SLuigi Rizzo }
377437e3a6d3SLuigi Rizzo 
377537e3a6d3SLuigi Rizzo static void
377637e3a6d3SLuigi Rizzo netmap_hw_dtor(struct netmap_adapter *na)
377737e3a6d3SLuigi Rizzo {
37782a7db7a6SVincenzo Maffione 	if (na->ifp == NULL)
377937e3a6d3SLuigi Rizzo 		return;
378037e3a6d3SLuigi Rizzo 
37812a7db7a6SVincenzo Maffione 	NM_DETACH_NA(na->ifp);
37824bf50f18SLuigi Rizzo }
37834bf50f18SLuigi Rizzo 
3784f18be576SLuigi Rizzo 
378568b8534bSLuigi Rizzo /*
3786c3e9b4dbSLuiz Otavio O Souza  * Allocate a netmap_adapter object, and initialize it from the
378737e3a6d3SLuigi Rizzo  * 'arg' passed by the driver on attach.
3788c3e9b4dbSLuiz Otavio O Souza  * We allocate a block of memory of 'size' bytes, which has room
3789c3e9b4dbSLuiz Otavio O Souza  * for struct netmap_adapter plus additional room private to
3790c3e9b4dbSLuiz Otavio O Souza  * the caller.
379168b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
379268b8534bSLuigi Rizzo  */
3793c3e9b4dbSLuiz Otavio O Souza int
37944f80b14cSVincenzo Maffione netmap_attach_ext(struct netmap_adapter *arg, size_t size, int override_reg)
379568b8534bSLuigi Rizzo {
3796f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
379737e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
379868b8534bSLuigi Rizzo 
3799c3e9b4dbSLuiz Otavio O Souza 	if (size < sizeof(struct netmap_hw_adapter)) {
3800b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
3801b6e66be2SVincenzo Maffione 			nm_prerr("Invalid netmap adapter size %d", (int)size);
3802c3e9b4dbSLuiz Otavio O Souza 		return EINVAL;
3803c3e9b4dbSLuiz Otavio O Souza 	}
3804c3e9b4dbSLuiz Otavio O Souza 
3805b6e66be2SVincenzo Maffione 	if (arg == NULL || arg->ifp == NULL) {
3806b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
3807b6e66be2SVincenzo Maffione 			nm_prerr("either arg or arg->ifp is NULL");
38082a7db7a6SVincenzo Maffione 		return EINVAL;
3809b6e66be2SVincenzo Maffione 	}
3810b6e66be2SVincenzo Maffione 
3811b6e66be2SVincenzo Maffione 	if (arg->num_tx_rings == 0 || arg->num_rx_rings == 0) {
3812b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
3813b6e66be2SVincenzo Maffione 			nm_prerr("%s: invalid rings tx %d rx %d",
3814b6e66be2SVincenzo Maffione 				arg->name, arg->num_tx_rings, arg->num_rx_rings);
3815b6e66be2SVincenzo Maffione 		return EINVAL;
3816b6e66be2SVincenzo Maffione 	}
38174f80b14cSVincenzo Maffione 
381837e3a6d3SLuigi Rizzo 	ifp = arg->ifp;
38192a7db7a6SVincenzo Maffione 	if (NM_NA_CLASH(ifp)) {
38204f80b14cSVincenzo Maffione 		/* If NA(ifp) is not null but there is no valid netmap
38214f80b14cSVincenzo Maffione 		 * adapter it means that someone else is using the same
38224f80b14cSVincenzo Maffione 		 * pointer (e.g. ax25_ptr on linux). This happens for
38234f80b14cSVincenzo Maffione 		 * instance when also PF_RING is in use. */
3824b6e66be2SVincenzo Maffione 		nm_prerr("Error: netmap adapter hook is busy");
38254f80b14cSVincenzo Maffione 		return EBUSY;
38264f80b14cSVincenzo Maffione 	}
38274f80b14cSVincenzo Maffione 
3828c3e9b4dbSLuiz Otavio O Souza 	hwna = nm_os_malloc(size);
3829f9790aebSLuigi Rizzo 	if (hwna == NULL)
3830ae10d1afSLuigi Rizzo 		goto fail;
3831f9790aebSLuigi Rizzo 	hwna->up = *arg;
3832847bf383SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS | NAF_NATIVE;
3833b6e66be2SVincenzo Maffione 	strlcpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
38344f80b14cSVincenzo Maffione 	if (override_reg) {
38354bf50f18SLuigi Rizzo 		hwna->nm_hw_register = hwna->up.nm_register;
383637e3a6d3SLuigi Rizzo 		hwna->up.nm_register = netmap_hw_reg;
38374f80b14cSVincenzo Maffione 	}
3838f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
3839c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(hwna);
3840f9790aebSLuigi Rizzo 		goto fail;
3841f9790aebSLuigi Rizzo 	}
3842f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
3843f9790aebSLuigi Rizzo 
384437e3a6d3SLuigi Rizzo 	NM_ATTACH_NA(ifp, &hwna->up);
384537e3a6d3SLuigi Rizzo 
38462a7db7a6SVincenzo Maffione 	nm_os_onattach(ifp);
38472a7db7a6SVincenzo Maffione 
384837e3a6d3SLuigi Rizzo 	if (arg->nm_dtor == NULL) {
384937e3a6d3SLuigi Rizzo 		hwna->up.nm_dtor = netmap_hw_dtor;
385037e3a6d3SLuigi Rizzo 	}
3851f9790aebSLuigi Rizzo 
38521ef2a881SVincenzo Maffione 	if_printf(ifp, "netmap queues/slots: TX %d/%d, RX %d/%d\n",
3853d82f9014SRui Paulo 	    hwna->up.num_tx_rings, hwna->up.num_tx_desc,
3854d82f9014SRui Paulo 	    hwna->up.num_rx_rings, hwna->up.num_rx_desc);
3855ae10d1afSLuigi Rizzo 	return 0;
385668b8534bSLuigi Rizzo 
3857ae10d1afSLuigi Rizzo fail:
3858b6e66be2SVincenzo Maffione 	nm_prerr("fail, arg %p ifp %p na %p", arg, ifp, hwna);
3859f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
386068b8534bSLuigi Rizzo }
386168b8534bSLuigi Rizzo 
386268b8534bSLuigi Rizzo 
386337e3a6d3SLuigi Rizzo int
386437e3a6d3SLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
386537e3a6d3SLuigi Rizzo {
38664f80b14cSVincenzo Maffione 	return netmap_attach_ext(arg, sizeof(struct netmap_hw_adapter),
38674f80b14cSVincenzo Maffione 			1 /* override nm_reg */);
386837e3a6d3SLuigi Rizzo }
386937e3a6d3SLuigi Rizzo 
387037e3a6d3SLuigi Rizzo 
3871f9790aebSLuigi Rizzo void
3872f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
3873f9790aebSLuigi Rizzo {
3874f9790aebSLuigi Rizzo 	if (!na) {
3875f9790aebSLuigi Rizzo 		return;
3876f9790aebSLuigi Rizzo 	}
3877f9790aebSLuigi Rizzo 
3878f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
3879f9790aebSLuigi Rizzo }
3880f9790aebSLuigi Rizzo 
3881f9790aebSLuigi Rizzo 
3882f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
3883f9790aebSLuigi Rizzo int
3884f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
3885f9790aebSLuigi Rizzo {
3886f9790aebSLuigi Rizzo 	if (!na)
3887f9790aebSLuigi Rizzo 		return 1;
3888f9790aebSLuigi Rizzo 
3889f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
3890f9790aebSLuigi Rizzo 		return 0;
3891f9790aebSLuigi Rizzo 
3892f9790aebSLuigi Rizzo 	if (na->nm_dtor)
3893f9790aebSLuigi Rizzo 		na->nm_dtor(na);
3894f9790aebSLuigi Rizzo 
38954f80b14cSVincenzo Maffione 	if (na->tx_rings) { /* XXX should not happen */
3896b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
3897b6e66be2SVincenzo Maffione 			nm_prerr("freeing leftover tx_rings");
38984f80b14cSVincenzo Maffione 		na->nm_krings_delete(na);
38994f80b14cSVincenzo Maffione 	}
39004f80b14cSVincenzo Maffione 	netmap_pipe_dealloc(na);
39014f80b14cSVincenzo Maffione 	if (na->nm_mem)
39024f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
39034f80b14cSVincenzo Maffione 	bzero(na, sizeof(*na));
39044f80b14cSVincenzo Maffione 	nm_os_free(na);
3905f9790aebSLuigi Rizzo 
3906f9790aebSLuigi Rizzo 	return 1;
3907f9790aebSLuigi Rizzo }
3908f9790aebSLuigi Rizzo 
390989cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
3910f9790aebSLuigi Rizzo int
3911f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
3912f9790aebSLuigi Rizzo {
3913f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
391417885a7bSLuigi Rizzo 	if (ret == 0) {
391517885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
39162a7db7a6SVincenzo Maffione 		u_int lim = netmap_real_rings(na, NR_RX), i;
39172a7db7a6SVincenzo Maffione 		for (i = na->num_rx_rings; i < lim; i++) {
39182a7db7a6SVincenzo Maffione 			mbq_safe_init(&NMR(na, NR_RX)[i]->rx_queue);
39192a7db7a6SVincenzo Maffione 		}
392075f4f3edSVincenzo Maffione 		nm_prdis("initialized sw rx queue %d", na->num_rx_rings);
392117885a7bSLuigi Rizzo 	}
392217885a7bSLuigi Rizzo 	return ret;
3923f9790aebSLuigi Rizzo }
3924f9790aebSLuigi Rizzo 
3925f9790aebSLuigi Rizzo 
3926f9790aebSLuigi Rizzo 
392768b8534bSLuigi Rizzo /*
392889cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
392968b8534bSLuigi Rizzo  */
393068b8534bSLuigi Rizzo void
393168b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
393268b8534bSLuigi Rizzo {
393368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
393468b8534bSLuigi Rizzo 
393568b8534bSLuigi Rizzo 	if (!na)
393668b8534bSLuigi Rizzo 		return;
393768b8534bSLuigi Rizzo 
3938f9790aebSLuigi Rizzo 	NMG_LOCK();
393937e3a6d3SLuigi Rizzo 	netmap_set_all_rings(na, NM_KR_LOCKED);
3940847bf383SLuigi Rizzo 	/*
3941847bf383SLuigi Rizzo 	 * if the netmap adapter is not native, somebody
3942847bf383SLuigi Rizzo 	 * changed it, so we can not release it here.
394337e3a6d3SLuigi Rizzo 	 * The NAF_ZOMBIE flag will notify the new owner that
3944847bf383SLuigi Rizzo 	 * the driver is gone.
3945847bf383SLuigi Rizzo 	 */
39464f80b14cSVincenzo Maffione 	if (!(na->na_flags & NAF_NATIVE) || !netmap_adapter_put(na)) {
39474f80b14cSVincenzo Maffione 		na->na_flags |= NAF_ZOMBIE;
3948847bf383SLuigi Rizzo 	}
394937e3a6d3SLuigi Rizzo 	/* give active users a chance to notice that NAF_ZOMBIE has been
395037e3a6d3SLuigi Rizzo 	 * turned on, so that they can stop and return an error to userspace.
395137e3a6d3SLuigi Rizzo 	 * Note that this becomes a NOP if there are no active users and,
395237e3a6d3SLuigi Rizzo 	 * therefore, the put() above has deleted the na, since now NA(ifp) is
395337e3a6d3SLuigi Rizzo 	 * NULL.
395437e3a6d3SLuigi Rizzo 	 */
3955f9790aebSLuigi Rizzo 	netmap_enable_all_rings(ifp);
3956f9790aebSLuigi Rizzo 	NMG_UNLOCK();
3957ae10d1afSLuigi Rizzo }
3958f18be576SLuigi Rizzo 
3959f18be576SLuigi Rizzo 
396068b8534bSLuigi Rizzo /*
396102ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
396202ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
396317885a7bSLuigi Rizzo  *
396417885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
396517885a7bSLuigi Rizzo  * in the relevant rxsync routine.
396617885a7bSLuigi Rizzo  *
3967ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
3968ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
3969ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
3970f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
397168b8534bSLuigi Rizzo  */
397268b8534bSLuigi Rizzo int
3973ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
397468b8534bSLuigi Rizzo {
397568b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
397637e3a6d3SLuigi Rizzo 	struct netmap_kring *kring, *tx_kring;
397717885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
397817885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
397937e3a6d3SLuigi Rizzo 	unsigned int txr;
398017885a7bSLuigi Rizzo 	struct mbq *q;
3981c3e9b4dbSLuiz Otavio O Souza 	int busy;
39822a7db7a6SVincenzo Maffione 	u_int i;
398368b8534bSLuigi Rizzo 
39842a7db7a6SVincenzo Maffione 	i = MBUF_TXQ(m);
39852a7db7a6SVincenzo Maffione 	if (i >= na->num_host_rx_rings) {
39862a7db7a6SVincenzo Maffione 		i = i % na->num_host_rx_rings;
39872a7db7a6SVincenzo Maffione 	}
39882a7db7a6SVincenzo Maffione 	kring = NMR(na, NR_RX)[nma_get_nrings(na, NR_RX) + i];
39892a7db7a6SVincenzo Maffione 
3990ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
3991ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
3992ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
399317885a7bSLuigi Rizzo 
39944bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
3995b6e66be2SVincenzo Maffione 		nm_prerr("%s not in netmap mode anymore", na->name);
3996ce3ee1e7SLuigi Rizzo 		error = ENXIO;
3997ce3ee1e7SLuigi Rizzo 		goto done;
3998ce3ee1e7SLuigi Rizzo 	}
3999ce3ee1e7SLuigi Rizzo 
400037e3a6d3SLuigi Rizzo 	txr = MBUF_TXQ(m);
400137e3a6d3SLuigi Rizzo 	if (txr >= na->num_tx_rings) {
400237e3a6d3SLuigi Rizzo 		txr %= na->num_tx_rings;
400337e3a6d3SLuigi Rizzo 	}
40042ff91c17SVincenzo Maffione 	tx_kring = NMR(na, NR_TX)[txr];
400537e3a6d3SLuigi Rizzo 
400637e3a6d3SLuigi Rizzo 	if (tx_kring->nr_mode == NKR_NETMAP_OFF) {
400737e3a6d3SLuigi Rizzo 		return MBUF_TRANSMIT(na, ifp, m);
400837e3a6d3SLuigi Rizzo 	}
400937e3a6d3SLuigi Rizzo 
401017885a7bSLuigi Rizzo 	q = &kring->rx_queue;
401117885a7bSLuigi Rizzo 
4012ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
40134bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
4014b6e66be2SVincenzo Maffione 		nm_prerr("%s from_host, drop packet size %d > %d", na->name,
40154bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
4016ce3ee1e7SLuigi Rizzo 		goto done;
4017849bec0eSLuigi Rizzo 	}
401817885a7bSLuigi Rizzo 
40192a7db7a6SVincenzo Maffione 	if (!netmap_generic_hwcsum) {
40202a7db7a6SVincenzo Maffione 		if (nm_os_mbuf_has_csum_offld(m)) {
402175f4f3edSVincenzo Maffione 			nm_prlim(1, "%s drop mbuf that needs checksum offload", na->name);
40222a7db7a6SVincenzo Maffione 			goto done;
40232a7db7a6SVincenzo Maffione 		}
40242a7db7a6SVincenzo Maffione 	}
40252a7db7a6SVincenzo Maffione 
40262a7db7a6SVincenzo Maffione 	if (nm_os_mbuf_has_seg_offld(m)) {
402775f4f3edSVincenzo Maffione 		nm_prlim(1, "%s drop mbuf that needs generic segmentation offload", na->name);
402837e3a6d3SLuigi Rizzo 		goto done;
402937e3a6d3SLuigi Rizzo 	}
403037e3a6d3SLuigi Rizzo 
403189a9a5b5SVincenzo Maffione #ifdef __FreeBSD__
403289a9a5b5SVincenzo Maffione 	ETHER_BPF_MTAP(ifp, m);
403389a9a5b5SVincenzo Maffione #endif /* __FreeBSD__ */
403489a9a5b5SVincenzo Maffione 
4035c3e9b4dbSLuiz Otavio O Souza 	/* protect against netmap_rxsync_from_host(), netmap_sw_to_nic()
403617885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
403717885a7bSLuigi Rizzo 	 * not possible on Linux).
4038c3e9b4dbSLuiz Otavio O Souza 	 * We enqueue the mbuf only if we are sure there is going to be
4039c3e9b4dbSLuiz Otavio O Souza 	 * enough room in the host RX ring, otherwise we drop it.
4040ce3ee1e7SLuigi Rizzo 	 */
4041997b054cSLuigi Rizzo 	mbq_lock(q);
404217885a7bSLuigi Rizzo 
4043c3e9b4dbSLuiz Otavio O Souza 	busy = kring->nr_hwtail - kring->nr_hwcur;
4044c3e9b4dbSLuiz Otavio O Souza 	if (busy < 0)
4045c3e9b4dbSLuiz Otavio O Souza 		busy += kring->nkr_num_slots;
4046c3e9b4dbSLuiz Otavio O Souza 	if (busy + mbq_len(q) >= kring->nkr_num_slots - 1) {
404775f4f3edSVincenzo Maffione 		nm_prlim(2, "%s full hwcur %d hwtail %d qlen %d", na->name,
4048c3e9b4dbSLuiz Otavio O Souza 			kring->nr_hwcur, kring->nr_hwtail, mbq_len(q));
4049ce3ee1e7SLuigi Rizzo 	} else {
405017885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
405175f4f3edSVincenzo Maffione 		nm_prdis(2, "%s %d bufs in queue", na->name, mbq_len(q));
405217885a7bSLuigi Rizzo 		/* notify outside the lock */
405317885a7bSLuigi Rizzo 		m = NULL;
405468b8534bSLuigi Rizzo 		error = 0;
4055ce3ee1e7SLuigi Rizzo 	}
4056997b054cSLuigi Rizzo 	mbq_unlock(q);
4057ce3ee1e7SLuigi Rizzo 
405868b8534bSLuigi Rizzo done:
405917885a7bSLuigi Rizzo 	if (m)
406068b8534bSLuigi Rizzo 		m_freem(m);
406117885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
4062847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
406389cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
406489cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
406589cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
406689cc2556SLuigi Rizzo 	 */
406768b8534bSLuigi Rizzo 
406868b8534bSLuigi Rizzo 	return (error);
406968b8534bSLuigi Rizzo }
407068b8534bSLuigi Rizzo 
407168b8534bSLuigi Rizzo 
407268b8534bSLuigi Rizzo /*
40737ba6ecf2SVincenzo Maffione  * Reset function to be called by the driver routines when reinitializing
40747ba6ecf2SVincenzo Maffione  * a hardware ring. The driver is in charge of locking to protect the kring
40757ba6ecf2SVincenzo Maffione  * while this operation is being performed.
40767ba6ecf2SVincenzo Maffione  * This is normally done by calling netmap_disable_all_rings() before
40777ba6ecf2SVincenzo Maffione  * triggering a reset.
40787ba6ecf2SVincenzo Maffione  * If the kring is not in netmap mode, return NULL to inform the caller
40797ba6ecf2SVincenzo Maffione  * that this is the case.
40807ba6ecf2SVincenzo Maffione  * If the kring is in netmap mode, reset the kring indices to 0.
40817ba6ecf2SVincenzo Maffione  * In any case, adjust kring->nr_mode.
408268b8534bSLuigi Rizzo  */
408368b8534bSLuigi Rizzo struct netmap_slot *
4084ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
408568b8534bSLuigi Rizzo 	u_int new_cur)
408668b8534bSLuigi Rizzo {
408768b8534bSLuigi Rizzo 	struct netmap_kring *kring;
408868b8534bSLuigi Rizzo 
40894bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
409075f4f3edSVincenzo Maffione 		nm_prdis("interface not in native netmap mode");
409168b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
4092ce3ee1e7SLuigi Rizzo 	}
409368b8534bSLuigi Rizzo 
409464ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
40958241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
40968241616dSLuigi Rizzo 			return NULL;
409737e3a6d3SLuigi Rizzo 
40982ff91c17SVincenzo Maffione 		kring = na->tx_rings[n];
409937e3a6d3SLuigi Rizzo 
410064ae02c3SLuigi Rizzo 	} else {
41018241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
41028241616dSLuigi Rizzo 			return NULL;
41032ff91c17SVincenzo Maffione 		kring = na->rx_rings[n];
41047ba6ecf2SVincenzo Maffione 	}
410537e3a6d3SLuigi Rizzo 	if (kring->nr_pending_mode == NKR_NETMAP_OFF) {
410637e3a6d3SLuigi Rizzo 		kring->nr_mode = NKR_NETMAP_OFF;
410737e3a6d3SLuigi Rizzo 		return NULL;
410837e3a6d3SLuigi Rizzo 	}
41097ba6ecf2SVincenzo Maffione 	if (netmap_verbose) {
41107ba6ecf2SVincenzo Maffione 	    nm_prinf("%s, was: hc %u h %u c %u ht %u", kring->name,
41117ba6ecf2SVincenzo Maffione 	        kring->nr_hwcur, kring->ring->head,
41127ba6ecf2SVincenzo Maffione 	        kring->ring->cur, kring->nr_hwtail);
411364ae02c3SLuigi Rizzo 	}
41147ba6ecf2SVincenzo Maffione 	/* For the moment being, nkr_hwofs is not used. */
41157ba6ecf2SVincenzo Maffione 	kring->rhead = kring->rcur = kring->nr_hwcur = kring->nkr_hwofs = 0;
41167ba6ecf2SVincenzo Maffione 	kring->nr_hwtail = (tx == NR_TX) ? (kring->nkr_num_slots - 1) : 0;
4117506cc70cSLuigi Rizzo 
411868b8534bSLuigi Rizzo 	/*
4119ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
4120506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
4121506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
412268b8534bSLuigi Rizzo 	 */
412337e3a6d3SLuigi Rizzo 	kring->nr_mode = NKR_NETMAP_ON;
4124847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
412568b8534bSLuigi Rizzo 	return kring->ring->slot;
412668b8534bSLuigi Rizzo }
412768b8534bSLuigi Rizzo 
412868b8534bSLuigi Rizzo 
4129ce3ee1e7SLuigi Rizzo /*
4130f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
4131ce3ee1e7SLuigi Rizzo  *
4132ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
4133ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
4134ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
4135849bec0eSLuigi Rizzo  *
4136f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
4137f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
4138f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
41394bf50f18SLuigi Rizzo  *   (see netmap_notify)
41404bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
41414bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
4142f9790aebSLuigi Rizzo  */
414337e3a6d3SLuigi Rizzo int
414437e3a6d3SLuigi Rizzo netmap_common_irq(struct netmap_adapter *na, u_int q, u_int *work_done)
4145f9790aebSLuigi Rizzo {
4146f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
4147847bf383SLuigi Rizzo 	enum txrx t = (work_done ? NR_RX : NR_TX);
4148f9790aebSLuigi Rizzo 
4149f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
4150f9790aebSLuigi Rizzo 
4151b6e66be2SVincenzo Maffione 	if (netmap_debug & (NM_DEBUG_RXINTR|NM_DEBUG_TXINTR)) {
4152b6e66be2SVincenzo Maffione 	        nm_prlim(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
4153f9790aebSLuigi Rizzo 	}
4154f9790aebSLuigi Rizzo 
4155847bf383SLuigi Rizzo 	if (q >= nma_get_nrings(na, t))
415637e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS; // not a physical queue
4157847bf383SLuigi Rizzo 
41582ff91c17SVincenzo Maffione 	kring = NMR(na, t)[q];
4159847bf383SLuigi Rizzo 
416037e3a6d3SLuigi Rizzo 	if (kring->nr_mode == NKR_NETMAP_OFF) {
416137e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
416237e3a6d3SLuigi Rizzo 	}
416337e3a6d3SLuigi Rizzo 
4164847bf383SLuigi Rizzo 	if (t == NR_RX) {
4165f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
4166f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
4167f9790aebSLuigi Rizzo 	}
416837e3a6d3SLuigi Rizzo 
416937e3a6d3SLuigi Rizzo 	return kring->nm_notify(kring, 0);
4170f9790aebSLuigi Rizzo }
4171f9790aebSLuigi Rizzo 
417217885a7bSLuigi Rizzo 
4173f9790aebSLuigi Rizzo /*
4174f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
4175f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
4176f9790aebSLuigi Rizzo  *
417737e3a6d3SLuigi Rizzo  * If the card is not in netmap mode, simply return NM_IRQ_PASS,
4178ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
417937e3a6d3SLuigi Rizzo  * Otherwise call netmap_common_irq().
4180ce3ee1e7SLuigi Rizzo  *
4181ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
4182ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
4183ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
418437e3a6d3SLuigi Rizzo  * and return NR_IRQ_COMPLETED.
4185ce3ee1e7SLuigi Rizzo  *
4186ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
418737e3a6d3SLuigi Rizzo  * calls the proper forwarding routine.
41881a26580eSLuigi Rizzo  */
4189babc7c12SLuigi Rizzo int
4190ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
41911a26580eSLuigi Rizzo {
41924bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
41934bf50f18SLuigi Rizzo 
41944bf50f18SLuigi Rizzo 	/*
41954bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
41964bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
41974bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
41984bf50f18SLuigi Rizzo 	 * nm_native_on() here.
41994bf50f18SLuigi Rizzo 	 */
42004bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
420137e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
4202849bec0eSLuigi Rizzo 
42034bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
420475f4f3edSVincenzo Maffione 		nm_prdis("use regular interrupt");
420537e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
42068241616dSLuigi Rizzo 	}
42078241616dSLuigi Rizzo 
420837e3a6d3SLuigi Rizzo 	return netmap_common_irq(na, q, work_done);
42091a26580eSLuigi Rizzo }
42101a26580eSLuigi Rizzo 
42112a7db7a6SVincenzo Maffione /* set/clear native flags and if_transmit/netdev_ops */
42122a7db7a6SVincenzo Maffione void
42132a7db7a6SVincenzo Maffione nm_set_native_flags(struct netmap_adapter *na)
42142a7db7a6SVincenzo Maffione {
42152a7db7a6SVincenzo Maffione 	struct ifnet *ifp = na->ifp;
42162a7db7a6SVincenzo Maffione 
42172a7db7a6SVincenzo Maffione 	/* We do the setup for intercepting packets only if we are the
42181d238b07SVincenzo Maffione 	 * first user of this adapter. */
42192a7db7a6SVincenzo Maffione 	if (na->active_fds > 0) {
42202a7db7a6SVincenzo Maffione 		return;
42212a7db7a6SVincenzo Maffione 	}
42222a7db7a6SVincenzo Maffione 
42232a7db7a6SVincenzo Maffione 	na->na_flags |= NAF_NETMAP_ON;
42242a7db7a6SVincenzo Maffione 	nm_os_onenter(ifp);
42252a7db7a6SVincenzo Maffione 	nm_update_hostrings_mode(na);
42262a7db7a6SVincenzo Maffione }
42272a7db7a6SVincenzo Maffione 
42282a7db7a6SVincenzo Maffione void
42292a7db7a6SVincenzo Maffione nm_clear_native_flags(struct netmap_adapter *na)
42302a7db7a6SVincenzo Maffione {
42312a7db7a6SVincenzo Maffione 	struct ifnet *ifp = na->ifp;
42322a7db7a6SVincenzo Maffione 
42332a7db7a6SVincenzo Maffione 	/* We undo the setup for intercepting packets only if we are the
4234b6e66be2SVincenzo Maffione 	 * last user of this adapter. */
42352a7db7a6SVincenzo Maffione 	if (na->active_fds > 0) {
42362a7db7a6SVincenzo Maffione 		return;
42372a7db7a6SVincenzo Maffione 	}
42382a7db7a6SVincenzo Maffione 
42392a7db7a6SVincenzo Maffione 	nm_update_hostrings_mode(na);
42402a7db7a6SVincenzo Maffione 	nm_os_onexit(ifp);
42412a7db7a6SVincenzo Maffione 
42422a7db7a6SVincenzo Maffione 	na->na_flags &= ~NAF_NETMAP_ON;
42432a7db7a6SVincenzo Maffione }
42442a7db7a6SVincenzo Maffione 
424575f4f3edSVincenzo Maffione void
424675f4f3edSVincenzo Maffione netmap_krings_mode_commit(struct netmap_adapter *na, int onoff)
424775f4f3edSVincenzo Maffione {
424875f4f3edSVincenzo Maffione 	enum txrx t;
424975f4f3edSVincenzo Maffione 
425075f4f3edSVincenzo Maffione 	for_rx_tx(t) {
425175f4f3edSVincenzo Maffione 		int i;
425275f4f3edSVincenzo Maffione 
425375f4f3edSVincenzo Maffione 		for (i = 0; i < netmap_real_rings(na, t); i++) {
425475f4f3edSVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
425575f4f3edSVincenzo Maffione 
425675f4f3edSVincenzo Maffione 			if (onoff && nm_kring_pending_on(kring))
425775f4f3edSVincenzo Maffione 				kring->nr_mode = NKR_NETMAP_ON;
425875f4f3edSVincenzo Maffione 			else if (!onoff && nm_kring_pending_off(kring))
425975f4f3edSVincenzo Maffione 				kring->nr_mode = NKR_NETMAP_OFF;
426075f4f3edSVincenzo Maffione 		}
426175f4f3edSVincenzo Maffione 	}
426275f4f3edSVincenzo Maffione }
426375f4f3edSVincenzo Maffione 
426401c7d25fSLuigi Rizzo /*
4265f9790aebSLuigi Rizzo  * Module loader and unloader
4266f196ce38SLuigi Rizzo  *
4267f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
4268f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
4269f9790aebSLuigi Rizzo  * (but there is no chance)
4270f9790aebSLuigi Rizzo  *
4271f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
4272f196ce38SLuigi Rizzo  */
4273babc7c12SLuigi Rizzo 
4274babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
4275f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
4276babc7c12SLuigi Rizzo 
427717885a7bSLuigi Rizzo 
4278f9790aebSLuigi Rizzo void
427968b8534bSLuigi Rizzo netmap_fini(void)
428068b8534bSLuigi Rizzo {
4281f9790aebSLuigi Rizzo 	if (netmap_dev)
428268b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
428337e3a6d3SLuigi Rizzo 	/* we assume that there are no longer netmap users */
428437e3a6d3SLuigi Rizzo 	nm_os_ifnet_fini();
428537e3a6d3SLuigi Rizzo 	netmap_uninit_bridges();
4286ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
4287ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
4288b6e66be2SVincenzo Maffione 	nm_prinf("netmap: unloaded module.");
428968b8534bSLuigi Rizzo }
429068b8534bSLuigi Rizzo 
429117885a7bSLuigi Rizzo 
4292f9790aebSLuigi Rizzo int
4293f9790aebSLuigi Rizzo netmap_init(void)
429468b8534bSLuigi Rizzo {
4295f9790aebSLuigi Rizzo 	int error;
429668b8534bSLuigi Rizzo 
4297f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
429868b8534bSLuigi Rizzo 
4299f9790aebSLuigi Rizzo 	error = netmap_mem_init();
4300f9790aebSLuigi Rizzo 	if (error != 0)
4301f9790aebSLuigi Rizzo 		goto fail;
4302c929ca72SLuigi Rizzo 	/*
4303c929ca72SLuigi Rizzo 	 * MAKEDEV_ETERNAL_KLD avoids an expensive check on syscalls
4304c929ca72SLuigi Rizzo 	 * when the module is compiled in.
4305c929ca72SLuigi Rizzo 	 * XXX could use make_dev_credv() to get error number
4306c929ca72SLuigi Rizzo 	 */
43070e73f29aSLuigi Rizzo 	netmap_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
430811c0b69cSAdrian Chadd 		&netmap_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0600,
43090e73f29aSLuigi Rizzo 			      "netmap");
4310f9790aebSLuigi Rizzo 	if (!netmap_dev)
4311f9790aebSLuigi Rizzo 		goto fail;
4312f9790aebSLuigi Rizzo 
4313847bf383SLuigi Rizzo 	error = netmap_init_bridges();
4314847bf383SLuigi Rizzo 	if (error)
4315847bf383SLuigi Rizzo 		goto fail;
4316847bf383SLuigi Rizzo 
43174bf50f18SLuigi Rizzo #ifdef __FreeBSD__
431837e3a6d3SLuigi Rizzo 	nm_os_vi_init_index();
43194bf50f18SLuigi Rizzo #endif
4320847bf383SLuigi Rizzo 
432137e3a6d3SLuigi Rizzo 	error = nm_os_ifnet_init();
432237e3a6d3SLuigi Rizzo 	if (error)
432337e3a6d3SLuigi Rizzo 		goto fail;
432437e3a6d3SLuigi Rizzo 
4325b6e66be2SVincenzo Maffione 	nm_prinf("netmap: loaded module");
4326f9790aebSLuigi Rizzo 	return (0);
4327f9790aebSLuigi Rizzo fail:
432868b8534bSLuigi Rizzo 	netmap_fini();
4329f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
433068b8534bSLuigi Rizzo }
4331