xref: /freebsd/sys/dev/netmap/netmap.c (revision 09a1893398fdcd1d000dae1cb3fd5239c0bfb360)
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
19645c67e8fSVincenzo Maffione  * 	      will find them only if they had previously 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 
622bb714db6SVincenzo Maffione 	if (netmap_verbose) {
623bb714db6SVincenzo Maffione 		nm_prinf("%s: %sable all rings", na->name,
624bb714db6SVincenzo Maffione 		    (stopped ? "dis" : "en"));
625bb714db6SVincenzo 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)) {
64355f0ad5fSVincenzo Maffione 		netmap_set_all_rings(NA(ifp), NM_KR_LOCKED);
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 
808a6d768d8SVincenzo Maffione static int
809a6d768d8SVincenzo Maffione netmap_default_bufcfg(struct netmap_kring *kring, uint64_t target)
810a6d768d8SVincenzo Maffione {
811a6d768d8SVincenzo Maffione 	kring->hwbuf_len = target;
812a6d768d8SVincenzo Maffione 	kring->buf_align = 0; /* no alignment */
813a6d768d8SVincenzo Maffione 	return 0;
814a6d768d8SVincenzo Maffione }
815a6d768d8SVincenzo Maffione 
816f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
817f0ea3689SLuigi Rizzo  * The array layout is this:
818f0ea3689SLuigi Rizzo  *
819f0ea3689SLuigi Rizzo  *                    +----------+
820f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
821f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
822f0ea3689SLuigi Rizzo  *                    |          | /
823f0ea3689SLuigi Rizzo  *                    +----------+
824f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
825f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
826f0ea3689SLuigi Rizzo  *                    |          | \
827f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
828f0ea3689SLuigi Rizzo  *                    |          | /
829f0ea3689SLuigi Rizzo  *                    +----------+
830f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
831f0ea3689SLuigi Rizzo  *                    +----------+
832f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
833f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
834f0ea3689SLuigi Rizzo  *                    |          | /
835f0ea3689SLuigi Rizzo  *                    +----------+
836f0ea3689SLuigi Rizzo  *
837f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
838f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
839f0ea3689SLuigi Rizzo  */
84089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
841f9790aebSLuigi Rizzo int
842f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
843f9790aebSLuigi Rizzo {
844f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
845f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
846847bf383SLuigi Rizzo 	u_int n[NR_TXRX];
847847bf383SLuigi Rizzo 	enum txrx t;
84819c4ec08SVincenzo Maffione 	int err = 0;
849f9790aebSLuigi Rizzo 
850c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings != NULL) {
851b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
852b6e66be2SVincenzo Maffione 			nm_prerr("warning: krings were already created");
853c3e9b4dbSLuiz Otavio O Souza 		return 0;
854c3e9b4dbSLuiz Otavio O Souza 	}
855c3e9b4dbSLuiz Otavio O Souza 
856f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
8572a7db7a6SVincenzo Maffione 	n[NR_TX] = netmap_all_rings(na, NR_TX);
8582a7db7a6SVincenzo Maffione 	n[NR_RX] = netmap_all_rings(na, NR_RX);
859f0ea3689SLuigi Rizzo 
8602ff91c17SVincenzo Maffione 	len = (n[NR_TX] + n[NR_RX]) *
8612ff91c17SVincenzo Maffione 		(sizeof(struct netmap_kring) + sizeof(struct netmap_kring *))
8622ff91c17SVincenzo Maffione 		+ tailroom;
863f9790aebSLuigi Rizzo 
864c3e9b4dbSLuiz Otavio O Souza 	na->tx_rings = nm_os_malloc((size_t)len);
865f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
866b6e66be2SVincenzo Maffione 		nm_prerr("Cannot allocate krings");
867f9790aebSLuigi Rizzo 		return ENOMEM;
868f9790aebSLuigi Rizzo 	}
869847bf383SLuigi Rizzo 	na->rx_rings = na->tx_rings + n[NR_TX];
8702ff91c17SVincenzo Maffione 	na->tailroom = na->rx_rings + n[NR_RX];
8712ff91c17SVincenzo Maffione 
8722ff91c17SVincenzo Maffione 	/* link the krings in the krings array */
8732ff91c17SVincenzo Maffione 	kring = (struct netmap_kring *)((char *)na->tailroom + tailroom);
8742ff91c17SVincenzo Maffione 	for (i = 0; i < n[NR_TX] + n[NR_RX]; i++) {
8752ff91c17SVincenzo Maffione 		na->tx_rings[i] = kring;
8762ff91c17SVincenzo Maffione 		kring++;
8772ff91c17SVincenzo Maffione 	}
878f9790aebSLuigi Rizzo 
87917885a7bSLuigi Rizzo 	/*
88017885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
88117885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
88217885a7bSLuigi Rizzo 	 */
883847bf383SLuigi Rizzo 	for_rx_tx(t) {
884847bf383SLuigi Rizzo 		ndesc = nma_get_ndesc(na, t);
885847bf383SLuigi Rizzo 		for (i = 0; i < n[t]; i++) {
8862ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
887f9790aebSLuigi Rizzo 			bzero(kring, sizeof(*kring));
8882ff91c17SVincenzo Maffione 			kring->notify_na = na;
88917885a7bSLuigi Rizzo 			kring->ring_id = i;
890847bf383SLuigi Rizzo 			kring->tx = t;
891f9790aebSLuigi Rizzo 			kring->nkr_num_slots = ndesc;
89237e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
89337e3a6d3SLuigi Rizzo 			kring->nr_pending_mode = NKR_NETMAP_OFF;
894847bf383SLuigi Rizzo 			if (i < nma_get_nrings(na, t)) {
895847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ? na->nm_txsync : na->nm_rxsync);
896a6d768d8SVincenzo Maffione 				kring->nm_bufcfg = na->nm_bufcfg;
897a6d768d8SVincenzo Maffione 				if (kring->nm_bufcfg == NULL)
898a6d768d8SVincenzo Maffione 					kring->nm_bufcfg = netmap_default_bufcfg;
89937e3a6d3SLuigi Rizzo 			} else {
9002ff91c17SVincenzo Maffione 				if (!(na->na_flags & NAF_HOST_RINGS))
9012ff91c17SVincenzo Maffione 					kring->nr_kflags |= NKR_FAKERING;
902847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ?
90337e3a6d3SLuigi Rizzo 						netmap_txsync_to_host:
90437e3a6d3SLuigi Rizzo 						netmap_rxsync_from_host);
905a6d768d8SVincenzo Maffione 				kring->nm_bufcfg = netmap_default_bufcfg;
906f0ea3689SLuigi Rizzo 			}
907847bf383SLuigi Rizzo 			kring->nm_notify = na->nm_notify;
908847bf383SLuigi Rizzo 			kring->rhead = kring->rcur = kring->nr_hwcur = 0;
909f9790aebSLuigi Rizzo 			/*
91017885a7bSLuigi Rizzo 			 * IMPORTANT: Always keep one slot empty.
911f9790aebSLuigi Rizzo 			 */
912847bf383SLuigi Rizzo 			kring->rtail = kring->nr_hwtail = (t == NR_TX ? ndesc - 1 : 0);
913847bf383SLuigi Rizzo 			snprintf(kring->name, sizeof(kring->name) - 1, "%s %s%d", na->name,
914847bf383SLuigi Rizzo 					nm_txrx2str(t), i);
91575f4f3edSVincenzo Maffione 			nm_prdis("ktx %s h %d c %d t %d",
916f0ea3689SLuigi Rizzo 				kring->name, kring->rhead, kring->rcur, kring->rtail);
91719c4ec08SVincenzo Maffione 			err = nm_os_selinfo_init(&kring->si, kring->name);
91819c4ec08SVincenzo Maffione 			if (err) {
91919c4ec08SVincenzo Maffione 				netmap_krings_delete(na);
92019c4ec08SVincenzo Maffione 				return err;
92119c4ec08SVincenzo Maffione 			}
922847bf383SLuigi Rizzo 			mtx_init(&kring->q_lock, (t == NR_TX ? "nm_txq_lock" : "nm_rxq_lock"), NULL, MTX_DEF);
92319c4ec08SVincenzo Maffione 			kring->na = na;	/* setting this field marks the mutex as initialized */
924f9790aebSLuigi Rizzo 		}
92519c4ec08SVincenzo Maffione 		err = nm_os_selinfo_init(&na->si[t], na->name);
92619c4ec08SVincenzo Maffione 		if (err) {
92719c4ec08SVincenzo Maffione 			netmap_krings_delete(na);
92819c4ec08SVincenzo Maffione 			return err;
929f0ea3689SLuigi Rizzo 		}
93019c4ec08SVincenzo Maffione 	}
931f9790aebSLuigi Rizzo 
932f9790aebSLuigi Rizzo 	return 0;
933f9790aebSLuigi Rizzo }
934f9790aebSLuigi Rizzo 
935f9790aebSLuigi Rizzo 
936f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
93789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
938f9790aebSLuigi Rizzo void
939f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
940f9790aebSLuigi Rizzo {
9412ff91c17SVincenzo Maffione 	struct netmap_kring **kring = na->tx_rings;
942847bf383SLuigi Rizzo 	enum txrx t;
943847bf383SLuigi Rizzo 
944c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings == NULL) {
945b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
946b6e66be2SVincenzo Maffione 			nm_prerr("warning: krings were already deleted");
947c3e9b4dbSLuiz Otavio O Souza 		return;
948c3e9b4dbSLuiz Otavio O Souza 	}
949c3e9b4dbSLuiz Otavio O Souza 
950847bf383SLuigi Rizzo 	for_rx_tx(t)
95137e3a6d3SLuigi Rizzo 		nm_os_selinfo_uninit(&na->si[t]);
952f9790aebSLuigi Rizzo 
953f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
954f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
95519c4ec08SVincenzo Maffione 		if ((*kring)->na != NULL)
9562ff91c17SVincenzo Maffione 			mtx_destroy(&(*kring)->q_lock);
9572ff91c17SVincenzo Maffione 		nm_os_selinfo_uninit(&(*kring)->si);
958f9790aebSLuigi Rizzo 	}
959c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(na->tx_rings);
960f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
961f9790aebSLuigi Rizzo }
962f9790aebSLuigi Rizzo 
963f9790aebSLuigi Rizzo 
96417885a7bSLuigi Rizzo /*
96517885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
96617885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
96717885a7bSLuigi Rizzo  * them first.
96817885a7bSLuigi Rizzo  */
96989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
97037e3a6d3SLuigi Rizzo void
97117885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
97217885a7bSLuigi Rizzo {
9732a7db7a6SVincenzo Maffione 	u_int lim = netmap_real_rings(na, NR_RX), i;
97417885a7bSLuigi Rizzo 
9752a7db7a6SVincenzo Maffione 	for (i = nma_get_nrings(na, NR_RX); i < lim; i++) {
9762a7db7a6SVincenzo Maffione 		struct mbq *q = &NMR(na, NR_RX)[i]->rx_queue;
97775f4f3edSVincenzo Maffione 		nm_prdis("destroy sw mbq with len %d", mbq_len(q));
97817885a7bSLuigi Rizzo 		mbq_purge(q);
97937e3a6d3SLuigi Rizzo 		mbq_safe_fini(q);
9802a7db7a6SVincenzo Maffione 	}
98117885a7bSLuigi Rizzo 	netmap_krings_delete(na);
98217885a7bSLuigi Rizzo }
98317885a7bSLuigi Rizzo 
984a6d768d8SVincenzo Maffione void
985a6d768d8SVincenzo Maffione netmap_mem_restore(struct netmap_adapter *na)
9864f80b14cSVincenzo Maffione {
987a6d768d8SVincenzo Maffione 	if (na->nm_mem_prev) {
9884f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
9894f80b14cSVincenzo Maffione 		na->nm_mem = na->nm_mem_prev;
9904f80b14cSVincenzo Maffione 		na->nm_mem_prev = NULL;
9914f80b14cSVincenzo Maffione 	}
9924f80b14cSVincenzo Maffione }
993f9790aebSLuigi Rizzo 
994a6d768d8SVincenzo Maffione static void
995a6d768d8SVincenzo Maffione netmap_mem_drop(struct netmap_adapter *na)
996a6d768d8SVincenzo Maffione {
997*09a18933SVincenzo Maffione 	netmap_mem_deref(na->nm_mem, na);
998*09a18933SVincenzo Maffione 
999*09a18933SVincenzo Maffione 	if (na->active_fds <= 0) {
100045c67e8fSVincenzo Maffione 		/* if the native allocator had been overridden on regif,
1001a6d768d8SVincenzo Maffione 		 * restore it now and drop the temporary one
1002a6d768d8SVincenzo Maffione 		 */
1003a6d768d8SVincenzo Maffione 		netmap_mem_restore(na);
1004a6d768d8SVincenzo Maffione 	}
1005a6d768d8SVincenzo Maffione }
1006a6d768d8SVincenzo Maffione 
100798399ab0SVincenzo Maffione static void
100898399ab0SVincenzo Maffione netmap_update_hostrings_mode(struct netmap_adapter *na)
100998399ab0SVincenzo Maffione {
101098399ab0SVincenzo Maffione 	enum txrx t;
101198399ab0SVincenzo Maffione 	struct netmap_kring *kring;
101298399ab0SVincenzo Maffione 	int i;
101398399ab0SVincenzo Maffione 
101498399ab0SVincenzo Maffione 	for_rx_tx(t) {
101598399ab0SVincenzo Maffione 		for (i = nma_get_nrings(na, t);
101698399ab0SVincenzo Maffione 		     i < netmap_real_rings(na, t); i++) {
101798399ab0SVincenzo Maffione 			kring = NMR(na, t)[i];
101898399ab0SVincenzo Maffione 			kring->nr_mode = kring->nr_pending_mode;
101998399ab0SVincenzo Maffione 		}
102098399ab0SVincenzo Maffione 	}
102198399ab0SVincenzo Maffione }
102298399ab0SVincenzo Maffione 
102368b8534bSLuigi Rizzo /*
1024847bf383SLuigi Rizzo  * Undo everything that was done in netmap_do_regif(). In particular,
1025847bf383SLuigi Rizzo  * call nm_register(ifp,0) to stop netmap mode on the interface and
10264bf50f18SLuigi Rizzo  * revert to normal operation.
102768b8534bSLuigi Rizzo  */
1028ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
1029847bf383SLuigi Rizzo static void netmap_unset_ringid(struct netmap_priv_d *);
103037e3a6d3SLuigi Rizzo static void netmap_krings_put(struct netmap_priv_d *);
103137e3a6d3SLuigi Rizzo void
1032847bf383SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv)
103368b8534bSLuigi Rizzo {
1034f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
103568b8534bSLuigi Rizzo 
1036ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1037f9790aebSLuigi Rizzo 	na->active_fds--;
103837e3a6d3SLuigi Rizzo 	/* unset nr_pending_mode and possibly release exclusive mode */
103937e3a6d3SLuigi Rizzo 	netmap_krings_put(priv);
1040847bf383SLuigi Rizzo 
1041847bf383SLuigi Rizzo #ifdef	WITH_MONITOR
104237e3a6d3SLuigi Rizzo 	/* XXX check whether we have to do something with monitor
104337e3a6d3SLuigi Rizzo 	 * when rings change nr_mode. */
104437e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {
1045847bf383SLuigi Rizzo 		/* walk through all the rings and tell any monitor
1046847bf383SLuigi Rizzo 		 * that the port is going to exit netmap mode
1047847bf383SLuigi Rizzo 		 */
1048847bf383SLuigi Rizzo 		netmap_monitor_stop(na);
104937e3a6d3SLuigi Rizzo 	}
1050847bf383SLuigi Rizzo #endif
105137e3a6d3SLuigi Rizzo 
105237e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0 || nm_kring_pending(priv)) {
105398399ab0SVincenzo Maffione 		netmap_set_all_rings(na, NM_KR_LOCKED);
105437e3a6d3SLuigi Rizzo 		na->nm_register(na, 0);
105598399ab0SVincenzo Maffione 		netmap_set_all_rings(na, 0);
105637e3a6d3SLuigi Rizzo 	}
105737e3a6d3SLuigi Rizzo 
105837e3a6d3SLuigi Rizzo 	/* delete rings and buffers that are no longer needed */
105937e3a6d3SLuigi Rizzo 	netmap_mem_rings_delete(na);
106037e3a6d3SLuigi Rizzo 
106137e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
106268b8534bSLuigi Rizzo 		/*
106337e3a6d3SLuigi Rizzo 		 * (TO CHECK) We enter here
1064f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
1065f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
1066f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
1067ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
1068ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
1069ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
1070ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
1071ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
1072ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
1073ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
1074ce3ee1e7SLuigi Rizzo 		 * syscall is running.
107568b8534bSLuigi Rizzo 		 */
1076b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
1077b6e66be2SVincenzo Maffione 			nm_prinf("deleting last instance for %s", na->name);
107837e3a6d3SLuigi Rizzo 
107937e3a6d3SLuigi Rizzo 		if (nm_netmap_on(na)) {
1080b6e66be2SVincenzo Maffione 			nm_prerr("BUG: netmap on while going to delete the krings");
108137e3a6d3SLuigi Rizzo 		}
108237e3a6d3SLuigi Rizzo 
1083f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
1084d12354a5SVincenzo Maffione 
1085d12354a5SVincenzo Maffione 		/* restore the default number of host tx and rx rings */
1086253b2ec1SVincenzo Maffione 		if (na->na_flags & NAF_HOST_RINGS) {
1087d12354a5SVincenzo Maffione 			na->num_host_tx_rings = 1;
1088d12354a5SVincenzo Maffione 			na->num_host_rx_rings = 1;
1089253b2ec1SVincenzo Maffione 		} else {
1090253b2ec1SVincenzo Maffione 			na->num_host_tx_rings = 0;
1091253b2ec1SVincenzo Maffione 			na->num_host_rx_rings = 0;
1092253b2ec1SVincenzo Maffione 		}
109368b8534bSLuigi Rizzo 	}
109437e3a6d3SLuigi Rizzo 
109545c67e8fSVincenzo Maffione 	/* possibly decrement counter of tx_si/rx_si users */
1096847bf383SLuigi Rizzo 	netmap_unset_ringid(priv);
1097f9790aebSLuigi Rizzo 	/* delete the nifp */
1098847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, priv->np_nifp);
1099847bf383SLuigi Rizzo 	/* drop the allocator */
11004f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
1101847bf383SLuigi Rizzo 	/* mark the priv as unregistered */
1102847bf383SLuigi Rizzo 	priv->np_na = NULL;
1103847bf383SLuigi Rizzo 	priv->np_nifp = NULL;
11045819da83SLuigi Rizzo }
110568b8534bSLuigi Rizzo 
110637e3a6d3SLuigi Rizzo struct netmap_priv_d*
110737e3a6d3SLuigi Rizzo netmap_priv_new(void)
110837e3a6d3SLuigi Rizzo {
110937e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
111037e3a6d3SLuigi Rizzo 
1111c3e9b4dbSLuiz Otavio O Souza 	priv = nm_os_malloc(sizeof(struct netmap_priv_d));
111237e3a6d3SLuigi Rizzo 	if (priv == NULL)
111337e3a6d3SLuigi Rizzo 		return NULL;
111437e3a6d3SLuigi Rizzo 	priv->np_refs = 1;
111537e3a6d3SLuigi Rizzo 	nm_os_get_module();
111637e3a6d3SLuigi Rizzo 	return priv;
111737e3a6d3SLuigi Rizzo }
111837e3a6d3SLuigi Rizzo 
1119ce3ee1e7SLuigi Rizzo /*
11208fd44c93SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd is closed
11218fd44c93SLuigi Rizzo  * Action: undo all the things done by NIOCREGIF,
11228fd44c93SLuigi Rizzo  * On FreeBSD we need to track whether there are active mmap()s,
11238fd44c93SLuigi Rizzo  * and we use np_active_mmaps for that. On linux, the field is always 0.
11248fd44c93SLuigi Rizzo  * Return: 1 if we can free priv, 0 otherwise.
112589cc2556SLuigi Rizzo  *
1126ce3ee1e7SLuigi Rizzo  */
112789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
112837e3a6d3SLuigi Rizzo void
112937e3a6d3SLuigi Rizzo netmap_priv_delete(struct netmap_priv_d *priv)
1130ce3ee1e7SLuigi Rizzo {
1131f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1132ce3ee1e7SLuigi Rizzo 
1133847adfb7SLuigi Rizzo 	/* number of active references to this fd */
11348fd44c93SLuigi Rizzo 	if (--priv->np_refs > 0) {
113537e3a6d3SLuigi Rizzo 		return;
1136ce3ee1e7SLuigi Rizzo 	}
113737e3a6d3SLuigi Rizzo 	nm_os_put_module();
113837e3a6d3SLuigi Rizzo 	if (na) {
1139847bf383SLuigi Rizzo 		netmap_do_unregif(priv);
114037e3a6d3SLuigi Rizzo 	}
114137e3a6d3SLuigi Rizzo 	netmap_unget_na(na, priv->np_ifp);
114237e3a6d3SLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* for safety */
1143c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(priv);
1144f196ce38SLuigi Rizzo }
11455819da83SLuigi Rizzo 
1146f9790aebSLuigi Rizzo 
114789cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
1148f9790aebSLuigi Rizzo void
11495819da83SLuigi Rizzo netmap_dtor(void *data)
11505819da83SLuigi Rizzo {
11515819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
11525819da83SLuigi Rizzo 
1153ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
115437e3a6d3SLuigi Rizzo 	netmap_priv_delete(priv);
1155ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1156ce3ee1e7SLuigi Rizzo }
115768b8534bSLuigi Rizzo 
1158f18be576SLuigi Rizzo 
115968b8534bSLuigi Rizzo /*
1160c3e9b4dbSLuiz Otavio O Souza  * Handlers for synchronization of the rings from/to the host stack.
1161c3e9b4dbSLuiz Otavio O Souza  * These are associated to a network interface and are just another
1162c3e9b4dbSLuiz Otavio O Souza  * ring pair managed by userspace.
1163c3e9b4dbSLuiz Otavio O Souza  *
1164c3e9b4dbSLuiz Otavio O Souza  * Netmap also supports transparent forwarding (NS_FORWARD and NR_FORWARD
1165c3e9b4dbSLuiz Otavio O Souza  * flags):
1166c3e9b4dbSLuiz Otavio O Souza  *
1167c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on hw RX rings, the application can mark
1168c3e9b4dbSLuiz Otavio O Souza  *   them with the NS_FORWARD flag. During the next RXSYNC or poll(), they
1169c3e9b4dbSLuiz Otavio O Souza  *   will be forwarded to the host stack, similarly to what happened if
1170c3e9b4dbSLuiz Otavio O Souza  *   the application moved them to the host TX ring.
1171c3e9b4dbSLuiz Otavio O Souza  *
1172c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on the host RX ring, the application can
1173c3e9b4dbSLuiz Otavio O Souza  *   mark them with the NS_FORWARD flag. During the next RXSYNC or poll(),
1174c3e9b4dbSLuiz Otavio O Souza  *   they will be forwarded to the hw TX rings, saving the application
1175c3e9b4dbSLuiz Otavio O Souza  *   from doing the same task in user-space.
1176c3e9b4dbSLuiz Otavio O Souza  *
117745c67e8fSVincenzo Maffione  * Transparent forwarding can be enabled per-ring, by setting the NR_FORWARD
1178c3e9b4dbSLuiz Otavio O Souza  * flag, or globally with the netmap_fwd sysctl.
1179c3e9b4dbSLuiz Otavio O Souza  *
1180091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1181091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1182091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1183091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1184091fd0abSLuigi Rizzo  */
1185091fd0abSLuigi Rizzo 
1186f18be576SLuigi Rizzo 
1187091fd0abSLuigi Rizzo /*
1188c3e9b4dbSLuiz Otavio O Souza  * Pass a whole queue of mbufs to the host stack as coming from 'dst'
118917885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1190c3e9b4dbSLuiz Otavio O Souza  * After this call the queue is empty.
1191091fd0abSLuigi Rizzo  */
1192091fd0abSLuigi Rizzo static void
1193f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1194091fd0abSLuigi Rizzo {
1195091fd0abSLuigi Rizzo 	struct mbuf *m;
119637e3a6d3SLuigi Rizzo 	struct mbuf *head = NULL, *prev = NULL;
1197b7d69138SVincenzo Maffione #ifdef __FreeBSD__
1198b7d69138SVincenzo Maffione 	struct epoch_tracker et;
1199091fd0abSLuigi Rizzo 
1200a4470078SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1201b7d69138SVincenzo Maffione #endif /* __FreeBSD__ */
1202c3e9b4dbSLuiz Otavio O Souza 	/* Send packets up, outside the lock; head/prev machinery
1203c3e9b4dbSLuiz Otavio O Souza 	 * is only useful for Windows. */
1204f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1205b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_HOST)
1206b6e66be2SVincenzo Maffione 			nm_prinf("sending up pkt %p size %d", m, MBUF_LEN(m));
120737e3a6d3SLuigi Rizzo 		prev = nm_os_send_up(dst, m, prev);
120837e3a6d3SLuigi Rizzo 		if (head == NULL)
120937e3a6d3SLuigi Rizzo 			head = prev;
1210091fd0abSLuigi Rizzo 	}
121137e3a6d3SLuigi Rizzo 	if (head)
121237e3a6d3SLuigi Rizzo 		nm_os_send_up(dst, NULL, head);
1213b7d69138SVincenzo Maffione #ifdef __FreeBSD__
1214a4470078SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1215b7d69138SVincenzo Maffione #endif /* __FreeBSD__ */
121637e3a6d3SLuigi Rizzo 	mbq_fini(q);
1217091fd0abSLuigi Rizzo }
1218091fd0abSLuigi Rizzo 
1219f18be576SLuigi Rizzo 
1220091fd0abSLuigi Rizzo /*
1221c3e9b4dbSLuiz Otavio O Souza  * Scan the buffers from hwcur to ring->head, and put a copy of those
1222c3e9b4dbSLuiz Otavio O Souza  * marked NS_FORWARD (or all of them if forced) into a queue of mbufs.
1223c3e9b4dbSLuiz Otavio O Souza  * Drop remaining packets in the unlikely event
122417885a7bSLuigi Rizzo  * of an mbuf shortage.
1225091fd0abSLuigi Rizzo  */
1226091fd0abSLuigi Rizzo static void
1227091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1228091fd0abSLuigi Rizzo {
122917885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1230847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
123117885a7bSLuigi Rizzo 	u_int n;
1232f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1233091fd0abSLuigi Rizzo 
123417885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
123517885a7bSLuigi Rizzo 		struct mbuf *m;
1236091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1237091fd0abSLuigi Rizzo 
1238091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1239091fd0abSLuigi Rizzo 			continue;
12404bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
124175f4f3edSVincenzo Maffione 			nm_prlim(5, "bad pkt at %d len %d", n, slot->len);
1242091fd0abSLuigi Rizzo 			continue;
1243091fd0abSLuigi Rizzo 		}
1244091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
124517885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
12464bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1247091fd0abSLuigi Rizzo 
1248091fd0abSLuigi Rizzo 		if (m == NULL)
1249091fd0abSLuigi Rizzo 			break;
1250f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1251091fd0abSLuigi Rizzo 	}
1252091fd0abSLuigi Rizzo }
1253091fd0abSLuigi Rizzo 
125437e3a6d3SLuigi Rizzo static inline int
125537e3a6d3SLuigi Rizzo _nm_may_forward(struct netmap_kring *kring)
125637e3a6d3SLuigi Rizzo {
125737e3a6d3SLuigi Rizzo 	return	((netmap_fwd || kring->ring->flags & NR_FORWARD) &&
125837e3a6d3SLuigi Rizzo 		 kring->na->na_flags & NAF_HOST_RINGS &&
125937e3a6d3SLuigi Rizzo 		 kring->tx == NR_RX);
126037e3a6d3SLuigi Rizzo }
126137e3a6d3SLuigi Rizzo 
126237e3a6d3SLuigi Rizzo static inline int
126337e3a6d3SLuigi Rizzo nm_may_forward_up(struct netmap_kring *kring)
126437e3a6d3SLuigi Rizzo {
126537e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
126637e3a6d3SLuigi Rizzo 		 kring->ring_id != kring->na->num_rx_rings;
126737e3a6d3SLuigi Rizzo }
126837e3a6d3SLuigi Rizzo 
126937e3a6d3SLuigi Rizzo static inline int
1270c3e9b4dbSLuiz Otavio O Souza nm_may_forward_down(struct netmap_kring *kring, int sync_flags)
127137e3a6d3SLuigi Rizzo {
127237e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
1273c3e9b4dbSLuiz Otavio O Souza 		 (sync_flags & NAF_CAN_FORWARD_DOWN) &&
127437e3a6d3SLuigi Rizzo 		 kring->ring_id == kring->na->num_rx_rings;
127537e3a6d3SLuigi Rizzo }
1276f18be576SLuigi Rizzo 
1277091fd0abSLuigi Rizzo /*
127817885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
1279c3e9b4dbSLuiz Otavio O Souza  * kring->nr_hwcur and kring->rhead.
1280c3e9b4dbSLuiz Otavio O Souza  * Called under kring->rx_queue.lock on the sw rx ring.
1281c3e9b4dbSLuiz Otavio O Souza  *
1282c3e9b4dbSLuiz Otavio O Souza  * It can only be called if the user opened all the TX hw rings,
1283c3e9b4dbSLuiz Otavio O Souza  * see NAF_CAN_FORWARD_DOWN flag.
1284c3e9b4dbSLuiz Otavio O Souza  * We can touch the TX netmap rings (slots, head and cur) since
1285c3e9b4dbSLuiz Otavio O Souza  * we are in poll/ioctl system call context, and the application
1286c3e9b4dbSLuiz Otavio O Souza  * is not supposed to touch the ring (using a different thread)
1287c3e9b4dbSLuiz Otavio O Souza  * during the execution of the system call.
1288091fd0abSLuigi Rizzo  */
128917885a7bSLuigi Rizzo static u_int
1290091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1291091fd0abSLuigi Rizzo {
12922ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[na->num_rx_rings];
129317885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
129417885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
129517885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
129617885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
129717885a7bSLuigi Rizzo 	u_int sent = 0;
1298ce3ee1e7SLuigi Rizzo 
129917885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
130017885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
13012ff91c17SVincenzo Maffione 		struct netmap_kring *kdst = na->tx_rings[i];
130217885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
130317885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1304ce3ee1e7SLuigi Rizzo 
130517885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
130617885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
130717885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1308091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
130937e3a6d3SLuigi Rizzo 			u_int dst_head = rdst->head;
131017885a7bSLuigi Rizzo 
131117885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
131217885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
131317885a7bSLuigi Rizzo 				continue;
131417885a7bSLuigi Rizzo 
131517885a7bSLuigi Rizzo 			sent++;
131617885a7bSLuigi Rizzo 
131737e3a6d3SLuigi Rizzo 			dst = &rdst->slot[dst_head];
131817885a7bSLuigi Rizzo 
1319091fd0abSLuigi Rizzo 			tmp = *src;
132017885a7bSLuigi Rizzo 
1321091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1322091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1323091fd0abSLuigi Rizzo 
1324091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1325091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1326091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1327091fd0abSLuigi Rizzo 
132837e3a6d3SLuigi Rizzo 			rdst->head = rdst->cur = nm_next(dst_head, dst_lim);
1329091fd0abSLuigi Rizzo 		}
1330c3e9b4dbSLuiz Otavio O Souza 		/* if (sent) XXX txsync ? it would be just an optimization */
1331091fd0abSLuigi Rizzo 	}
133217885a7bSLuigi Rizzo 	return sent;
1333091fd0abSLuigi Rizzo }
1334091fd0abSLuigi Rizzo 
1335f18be576SLuigi Rizzo 
1336091fd0abSLuigi Rizzo /*
1337ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
133802ad4083SLuigi Rizzo  * system call in user process context, and the only contention
133902ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1340091fd0abSLuigi Rizzo  * this routine concurrently.
134168b8534bSLuigi Rizzo  */
134237e3a6d3SLuigi Rizzo static int
134337e3a6d3SLuigi Rizzo netmap_txsync_to_host(struct netmap_kring *kring, int flags)
134468b8534bSLuigi Rizzo {
134537e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
134617885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1347f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1348f9790aebSLuigi Rizzo 	struct mbq q;
134968b8534bSLuigi Rizzo 
135017885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
1351c3e9b4dbSLuiz Otavio O Souza 	 * Force hwcur = head since netmap_grab_packets() stops at head
135268b8534bSLuigi Rizzo 	 */
1353f9790aebSLuigi Rizzo 	mbq_init(&q);
135417885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
135575f4f3edSVincenzo Maffione 	nm_prdis("have %d pkts in queue", mbq_len(&q));
135617885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
135717885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
135817885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
135917885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
136068b8534bSLuigi Rizzo 
1361f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
136237e3a6d3SLuigi Rizzo 	return 0;
1363f18be576SLuigi Rizzo }
1364f18be576SLuigi Rizzo 
1365f18be576SLuigi Rizzo 
136668b8534bSLuigi Rizzo /*
136702ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
136817885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
136917885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
137002ad4083SLuigi Rizzo  *
1371c3e9b4dbSLuiz Otavio O Souza  * also moves to the nic hw rings any packet the user has marked
1372c3e9b4dbSLuiz Otavio O Souza  * for transparent-mode forwarding, then sets the NR_FORWARD
1373c3e9b4dbSLuiz Otavio O Souza  * flag in the kring to let the caller push them out
137468b8534bSLuigi Rizzo  */
13758fd44c93SLuigi Rizzo static int
137637e3a6d3SLuigi Rizzo netmap_rxsync_from_host(struct netmap_kring *kring, int flags)
137768b8534bSLuigi Rizzo {
137837e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
137968b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
138017885a7bSLuigi Rizzo 	u_int nm_i, n;
138117885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1382f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
138317885a7bSLuigi Rizzo 	int ret = 0;
1384847bf383SLuigi Rizzo 	struct mbq *q = &kring->rx_queue, fq;
138568b8534bSLuigi Rizzo 
1386847bf383SLuigi Rizzo 	mbq_init(&fq); /* fq holds packets to be freed */
1387847bf383SLuigi Rizzo 
1388997b054cSLuigi Rizzo 	mbq_lock(q);
138917885a7bSLuigi Rizzo 
139017885a7bSLuigi Rizzo 	/* First part: import newly received packets */
139117885a7bSLuigi Rizzo 	n = mbq_len(q);
139217885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
139317885a7bSLuigi Rizzo 		struct mbuf *m;
139417885a7bSLuigi Rizzo 		uint32_t stop_i;
139517885a7bSLuigi Rizzo 
139617885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
1397c3e9b4dbSLuiz Otavio O Souza 		stop_i = nm_prev(kring->nr_hwcur, lim);
139817885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
139917885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
140017885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
140117885a7bSLuigi Rizzo 
14024bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
140375f4f3edSVincenzo Maffione 			nm_prdis("nm %d len %d", nm_i, len);
1404b6e66be2SVincenzo Maffione 			if (netmap_debug & NM_DEBUG_HOST)
1405b6e66be2SVincenzo Maffione 				nm_prinf("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
140617885a7bSLuigi Rizzo 
140717885a7bSLuigi Rizzo 			slot->len = len;
14084f80b14cSVincenzo Maffione 			slot->flags = 0;
140917885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
1410847bf383SLuigi Rizzo 			mbq_enqueue(&fq, m);
141164ae02c3SLuigi Rizzo 		}
141217885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
141364ae02c3SLuigi Rizzo 	}
141417885a7bSLuigi Rizzo 
141517885a7bSLuigi Rizzo 	/*
141617885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
141717885a7bSLuigi Rizzo 	 */
141817885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
141917885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
1420c3e9b4dbSLuiz Otavio O Souza 		if (nm_may_forward_down(kring, flags)) {
142117885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
142237e3a6d3SLuigi Rizzo 			if (ret > 0) {
142337e3a6d3SLuigi Rizzo 				kring->nr_kflags |= NR_FORWARD;
142437e3a6d3SLuigi Rizzo 				ret = 0;
142537e3a6d3SLuigi Rizzo 			}
142637e3a6d3SLuigi Rizzo 		}
142717885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
142864ae02c3SLuigi Rizzo 	}
142917885a7bSLuigi Rizzo 
1430997b054cSLuigi Rizzo 	mbq_unlock(q);
1431847bf383SLuigi Rizzo 
1432847bf383SLuigi Rizzo 	mbq_purge(&fq);
143337e3a6d3SLuigi Rizzo 	mbq_fini(&fq);
1434847bf383SLuigi Rizzo 
143517885a7bSLuigi Rizzo 	return ret;
143668b8534bSLuigi Rizzo }
143768b8534bSLuigi Rizzo 
143868b8534bSLuigi Rizzo 
1439f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1440f9790aebSLuigi Rizzo  *
1441f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1442f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1443f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1444f9790aebSLuigi Rizzo  *
1445f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1446f9790aebSLuigi Rizzo  * EBUSY.
1447f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1448f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1449f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1450f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1451f9790aebSLuigi Rizzo  *
1452f9790aebSLuigi Rizzo  *					native_support
1453f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1454f9790aebSLuigi Rizzo  * -------------------------------------------------------
1455f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1456f9790aebSLuigi Rizzo  *
1457f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1458f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1459f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1460f9790aebSLuigi Rizzo  *
1461f9790aebSLuigi Rizzo  */
146237e3a6d3SLuigi Rizzo static void netmap_hw_dtor(struct netmap_adapter *); /* needed by NM_IS_NATIVE() */
1463f9790aebSLuigi Rizzo int
1464c3e9b4dbSLuiz Otavio O Souza netmap_get_hw_na(struct ifnet *ifp, struct netmap_mem_d *nmd, struct netmap_adapter **na)
1465f9790aebSLuigi Rizzo {
1466f9790aebSLuigi Rizzo 	/* generic support */
1467f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1468f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1469847bf383SLuigi Rizzo 	int error = 0;
1470f9790aebSLuigi Rizzo 
1471f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1472f9790aebSLuigi Rizzo 
1473f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1474f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1475f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1476f9790aebSLuigi Rizzo 
147737e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
14784bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1479f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1480f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1481f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1482f9790aebSLuigi Rizzo 		 * adapters.
1483f9790aebSLuigi Rizzo 		 */
14844bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
14854bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
14864bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
14874bf50f18SLuigi Rizzo #ifdef WITH_PIPES
14884bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
14894bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
14904bf50f18SLuigi Rizzo 			 */
14914bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
14924bf50f18SLuigi Rizzo #endif
14934bf50f18SLuigi Rizzo 		) {
14944bf50f18SLuigi Rizzo 			*na = prev_na;
1495c3e9b4dbSLuiz Otavio O Souza 			goto assign_mem;
1496f9790aebSLuigi Rizzo 		}
1497f9790aebSLuigi Rizzo 	}
1498f9790aebSLuigi Rizzo 
1499f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1500f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1501f9790aebSLuigi Rizzo 	 */
150237e3a6d3SLuigi Rizzo 	if (!NM_IS_NATIVE(ifp) && i == NETMAP_ADMODE_NATIVE)
1503f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1504f9790aebSLuigi Rizzo 
1505f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1506f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1507f9790aebSLuigi Rizzo 	 *
1508f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1509f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1510f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1511f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1512f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1513f9790aebSLuigi Rizzo 	 * must be that
1514f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1515f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1516f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1517f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1518f9790aebSLuigi Rizzo 	 */
1519f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1520f9790aebSLuigi Rizzo 	if (error)
1521f9790aebSLuigi Rizzo 		return error;
1522f9790aebSLuigi Rizzo 
1523f9790aebSLuigi Rizzo 	*na = NA(ifp);
1524c3e9b4dbSLuiz Otavio O Souza 
1525c3e9b4dbSLuiz Otavio O Souza assign_mem:
1526c3e9b4dbSLuiz Otavio O Souza 	if (nmd != NULL && !((*na)->na_flags & NAF_MEM_OWNER) &&
1527c3e9b4dbSLuiz Otavio O Souza 	    (*na)->active_fds == 0 && ((*na)->nm_mem != nmd)) {
15284f80b14cSVincenzo Maffione 		(*na)->nm_mem_prev = (*na)->nm_mem;
1529c3e9b4dbSLuiz Otavio O Souza 		(*na)->nm_mem = netmap_mem_get(nmd);
1530f9790aebSLuigi Rizzo 	}
1531f9790aebSLuigi Rizzo 
1532c3e9b4dbSLuiz Otavio O Souza 	return 0;
1533c3e9b4dbSLuiz Otavio O Souza }
1534f9790aebSLuigi Rizzo 
153568b8534bSLuigi Rizzo /*
1536ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1537ce3ee1e7SLuigi Rizzo  *
1538f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
15392ff91c17SVincenzo Maffione  * to the interface specified by req.
1540ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1541ce3ee1e7SLuigi Rizzo  *
1542f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1543f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1544f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1545f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1546f2637526SLuigi Rizzo  * could not be allocated.
1547f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1548f18be576SLuigi Rizzo  *
15492ff91c17SVincenzo Maffione  * If the interface specified by req is a system one, also keep
155037e3a6d3SLuigi Rizzo  * a reference to it and return a valid *ifp.
155168b8534bSLuigi Rizzo  */
1552f9790aebSLuigi Rizzo int
15532ff91c17SVincenzo Maffione netmap_get_na(struct nmreq_header *hdr,
15542ff91c17SVincenzo Maffione 	      struct netmap_adapter **na, struct ifnet **ifp,
15552ff91c17SVincenzo Maffione 	      struct netmap_mem_d *nmd, int create)
155668b8534bSLuigi Rizzo {
1557cfa866f6SMatt Macy 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
1558f9790aebSLuigi Rizzo 	int error = 0;
1559f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1560c3e9b4dbSLuiz Otavio O Souza 	int nmd_ref = 0;
1561f9790aebSLuigi Rizzo 
1562f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
156337e3a6d3SLuigi Rizzo 	*ifp = NULL;
1564f196ce38SLuigi Rizzo 
15652ff91c17SVincenzo Maffione 	if (hdr->nr_reqtype != NETMAP_REQ_REGISTER) {
15662ff91c17SVincenzo Maffione 		return EINVAL;
15672ff91c17SVincenzo Maffione 	}
15682ff91c17SVincenzo Maffione 
15692ff91c17SVincenzo Maffione 	if (req->nr_mode == NR_REG_PIPE_MASTER ||
15702ff91c17SVincenzo Maffione 			req->nr_mode == NR_REG_PIPE_SLAVE) {
15712ff91c17SVincenzo Maffione 		/* Do not accept deprecated pipe modes. */
1572b6e66be2SVincenzo Maffione 		nm_prerr("Deprecated pipe nr_mode, use xx{yy or xx}yy syntax");
15732ff91c17SVincenzo Maffione 		return EINVAL;
15742ff91c17SVincenzo Maffione 	}
15752ff91c17SVincenzo Maffione 
1576ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1577ce3ee1e7SLuigi Rizzo 
1578c3e9b4dbSLuiz Otavio O Souza 	/* if the request contain a memid, try to find the
1579c3e9b4dbSLuiz Otavio O Souza 	 * corresponding memory region
1580c3e9b4dbSLuiz Otavio O Souza 	 */
15812ff91c17SVincenzo Maffione 	if (nmd == NULL && req->nr_mem_id) {
15822ff91c17SVincenzo Maffione 		nmd = netmap_mem_find(req->nr_mem_id);
1583c3e9b4dbSLuiz Otavio O Souza 		if (nmd == NULL)
1584c3e9b4dbSLuiz Otavio O Souza 			return EINVAL;
1585c3e9b4dbSLuiz Otavio O Souza 		/* keep the rereference */
1586c3e9b4dbSLuiz Otavio O Souza 		nmd_ref = 1;
1587c3e9b4dbSLuiz Otavio O Souza 	}
1588c3e9b4dbSLuiz Otavio O Souza 
158937e3a6d3SLuigi Rizzo 	/* We cascade through all possible types of netmap adapter.
15904bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
15914bf50f18SLuigi Rizzo 	 * with the following combinations:
15924bf50f18SLuigi Rizzo 	 *
15934bf50f18SLuigi Rizzo 	 * error    na
15944bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
15954bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
15964bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
15974bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
15984bf50f18SLuigi Rizzo 	 */
1599b6e66be2SVincenzo Maffione 	error = netmap_get_null_na(hdr, na, nmd, create);
160037e3a6d3SLuigi Rizzo 	if (error || *na != NULL)
1601c3e9b4dbSLuiz Otavio O Souza 		goto out;
160237e3a6d3SLuigi Rizzo 
16034bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
16042ff91c17SVincenzo Maffione 	error = netmap_get_monitor_na(hdr, na, nmd, create);
16054bf50f18SLuigi Rizzo 	if (error || *na != NULL)
1606c3e9b4dbSLuiz Otavio O Souza 		goto out;
16074bf50f18SLuigi Rizzo 
16084bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
16092ff91c17SVincenzo Maffione 	error = netmap_get_pipe_na(hdr, na, nmd, create);
1610f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1611c3e9b4dbSLuiz Otavio O Souza 		goto out;
1612ce3ee1e7SLuigi Rizzo 
1613a6d768d8SVincenzo Maffione 	/* try to see if this is a vale port */
16142a7db7a6SVincenzo Maffione 	error = netmap_get_vale_na(hdr, na, nmd, create);
1615f0ea3689SLuigi Rizzo 	if (error)
1616c3e9b4dbSLuiz Otavio O Souza 		goto out;
1617f0ea3689SLuigi Rizzo 
1618f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1619847bf383SLuigi Rizzo 		goto out;
1620f0ea3689SLuigi Rizzo 
162189cc2556SLuigi Rizzo 	/*
162289cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
162389cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
162489cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
162589cc2556SLuigi Rizzo 	 * persistent VALE port.
162689cc2556SLuigi Rizzo 	 */
16272ff91c17SVincenzo Maffione 	*ifp = ifunit_ref(hdr->nr_name);
162837e3a6d3SLuigi Rizzo 	if (*ifp == NULL) {
1629c3e9b4dbSLuiz Otavio O Souza 		error = ENXIO;
1630c3e9b4dbSLuiz Otavio O Souza 		goto out;
1631f196ce38SLuigi Rizzo 	}
1632ce3ee1e7SLuigi Rizzo 
1633c3e9b4dbSLuiz Otavio O Souza 	error = netmap_get_hw_na(*ifp, nmd, &ret);
1634f9790aebSLuigi Rizzo 	if (error)
1635f9790aebSLuigi Rizzo 		goto out;
1636f18be576SLuigi Rizzo 
1637f9790aebSLuigi Rizzo 	*na = ret;
1638f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1639f0ea3689SLuigi Rizzo 
1640d12354a5SVincenzo Maffione 	/*
164145c67e8fSVincenzo Maffione 	 * if the adapter supports the host rings and it is not already open,
1642d12354a5SVincenzo Maffione 	 * try to set the number of host rings as requested by the user
1643d12354a5SVincenzo Maffione 	 */
1644d12354a5SVincenzo Maffione 	if (((*na)->na_flags & NAF_HOST_RINGS) && (*na)->active_fds == 0) {
1645d12354a5SVincenzo Maffione 		if (req->nr_host_tx_rings)
1646d12354a5SVincenzo Maffione 			(*na)->num_host_tx_rings = req->nr_host_tx_rings;
1647d12354a5SVincenzo Maffione 		if (req->nr_host_rx_rings)
1648d12354a5SVincenzo Maffione 			(*na)->num_host_rx_rings = req->nr_host_rx_rings;
1649d12354a5SVincenzo Maffione 	}
1650d12354a5SVincenzo Maffione 	nm_prdis("%s: host tx %d rx %u", (*na)->name, (*na)->num_host_tx_rings,
1651d12354a5SVincenzo Maffione 			(*na)->num_host_rx_rings);
1652d12354a5SVincenzo Maffione 
1653f9790aebSLuigi Rizzo out:
165437e3a6d3SLuigi Rizzo 	if (error) {
165537e3a6d3SLuigi Rizzo 		if (ret)
1656f0ea3689SLuigi Rizzo 			netmap_adapter_put(ret);
165737e3a6d3SLuigi Rizzo 		if (*ifp) {
165837e3a6d3SLuigi Rizzo 			if_rele(*ifp);
165937e3a6d3SLuigi Rizzo 			*ifp = NULL;
166037e3a6d3SLuigi Rizzo 		}
166137e3a6d3SLuigi Rizzo 	}
1662c3e9b4dbSLuiz Otavio O Souza 	if (nmd_ref)
1663c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_put(nmd);
1664f18be576SLuigi Rizzo 
16655ab0d24dSLuigi Rizzo 	return error;
16665ab0d24dSLuigi Rizzo }
1667ce3ee1e7SLuigi Rizzo 
166837e3a6d3SLuigi Rizzo /* undo netmap_get_na() */
166937e3a6d3SLuigi Rizzo void
167037e3a6d3SLuigi Rizzo netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp)
167137e3a6d3SLuigi Rizzo {
167237e3a6d3SLuigi Rizzo 	if (ifp)
167337e3a6d3SLuigi Rizzo 		if_rele(ifp);
167437e3a6d3SLuigi Rizzo 	if (na)
167537e3a6d3SLuigi Rizzo 		netmap_adapter_put(na);
167637e3a6d3SLuigi Rizzo }
167737e3a6d3SLuigi Rizzo 
167837e3a6d3SLuigi Rizzo 
167937e3a6d3SLuigi Rizzo #define NM_FAIL_ON(t) do {						\
168037e3a6d3SLuigi Rizzo 	if (unlikely(t)) {						\
168175f4f3edSVincenzo Maffione 		nm_prlim(5, "%s: fail '" #t "' "				\
168237e3a6d3SLuigi Rizzo 			"h %d c %d t %d "				\
168337e3a6d3SLuigi Rizzo 			"rh %d rc %d rt %d "				\
168437e3a6d3SLuigi Rizzo 			"hc %d ht %d",					\
168537e3a6d3SLuigi Rizzo 			kring->name,					\
168637e3a6d3SLuigi Rizzo 			head, cur, ring->tail,				\
168737e3a6d3SLuigi Rizzo 			kring->rhead, kring->rcur, kring->rtail,	\
168837e3a6d3SLuigi Rizzo 			kring->nr_hwcur, kring->nr_hwtail);		\
168937e3a6d3SLuigi Rizzo 		return kring->nkr_num_slots;				\
169037e3a6d3SLuigi Rizzo 	}								\
169137e3a6d3SLuigi Rizzo } while (0)
1692ce3ee1e7SLuigi Rizzo 
1693f9790aebSLuigi Rizzo /*
1694f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1695f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
169617885a7bSLuigi Rizzo  * in case of error.
1697f9790aebSLuigi Rizzo  *
169817885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
169917885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1700f9790aebSLuigi Rizzo  *
170117885a7bSLuigi Rizzo  * We want
170217885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1703f9790aebSLuigi Rizzo  *
170417885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1705f9790aebSLuigi Rizzo  */
170637e3a6d3SLuigi Rizzo u_int
170737e3a6d3SLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1708f9790aebSLuigi Rizzo {
170917885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1710f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1711f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1712ce3ee1e7SLuigi Rizzo 
171375f4f3edSVincenzo Maffione 	nm_prdis(5, "%s kcur %d ktail %d head %d cur %d tail %d",
171417885a7bSLuigi Rizzo 		kring->name,
171517885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
171617885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
171717885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
171837e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->rhead >= n ||
171937e3a6d3SLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n);
1720f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
172117885a7bSLuigi Rizzo 	/*
172237e3a6d3SLuigi Rizzo 	 * user sanity checks. We only use head,
172337e3a6d3SLuigi Rizzo 	 * A, B, ... are possible positions for head:
172417885a7bSLuigi Rizzo 	 *
172537e3a6d3SLuigi Rizzo 	 *  0    A  rhead   B  rtail   C  n-1
172637e3a6d3SLuigi Rizzo 	 *  0    D  rtail   E  rhead   F  n-1
172717885a7bSLuigi Rizzo 	 *
172817885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
172917885a7bSLuigi Rizzo 	 */
173017885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
173117885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
173237e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->rhead || head > kring->rtail);
173317885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
173437e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->rtail);
173517885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
173617885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
173737e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head > kring->rtail && head < kring->rhead);
173817885a7bSLuigi Rizzo 
173917885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
174017885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
174117885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
174237e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->rtail);
174317885a7bSLuigi Rizzo 		} else { /* head >= rhead */
174417885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
174537e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur > kring->rtail && cur < head);
1746f18be576SLuigi Rizzo 		}
1747f9790aebSLuigi Rizzo 	}
174817885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
174975f4f3edSVincenzo Maffione 		nm_prlim(5, "%s tail overwritten was %d need %d", kring->name,
175017885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
175117885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
175217885a7bSLuigi Rizzo 	}
175317885a7bSLuigi Rizzo 	kring->rhead = head;
175417885a7bSLuigi Rizzo 	kring->rcur = cur;
175517885a7bSLuigi Rizzo 	return head;
175668b8534bSLuigi Rizzo }
175768b8534bSLuigi Rizzo 
175868b8534bSLuigi Rizzo 
175968b8534bSLuigi Rizzo /*
1760f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
176117885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1762f9790aebSLuigi Rizzo  *
176317885a7bSLuigi Rizzo  * For a valid configuration,
176417885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1765f9790aebSLuigi Rizzo  *
176617885a7bSLuigi Rizzo  * We only consider head and cur.
176717885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1768f9790aebSLuigi Rizzo  *
1769f9790aebSLuigi Rizzo  */
177037e3a6d3SLuigi Rizzo u_int
177137e3a6d3SLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1772f9790aebSLuigi Rizzo {
177317885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
177417885a7bSLuigi Rizzo 	uint32_t head, cur;
1775f9790aebSLuigi Rizzo 
177675f4f3edSVincenzo Maffione 	nm_prdis(5,"%s kc %d kt %d h %d c %d t %d",
177717885a7bSLuigi Rizzo 		kring->name,
177817885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
177917885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
178017885a7bSLuigi Rizzo 	/*
178117885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
178217885a7bSLuigi Rizzo 	 * move backwards. However:
178317885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
178417885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
178517885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
178617885a7bSLuigi Rizzo 	 */
178717885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
178817885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1789f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
179037e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->nr_hwtail >= n);
1791f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1792f9790aebSLuigi Rizzo 	/* user sanity checks */
179317885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
179417885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
179537e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur || head > kring->nr_hwtail);
179617885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
179737e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
1798f9790aebSLuigi Rizzo 	} else {
179917885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
180037e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur && head > kring->nr_hwtail);
180117885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
180217885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
180317885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
180437e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
180517885a7bSLuigi Rizzo 		} else {
180617885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
180737e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head && cur > kring->nr_hwtail);
1808f9790aebSLuigi Rizzo 		}
1809f9790aebSLuigi Rizzo 	}
181017885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
181175f4f3edSVincenzo Maffione 		nm_prlim(5, "%s tail overwritten was %d need %d",
181217885a7bSLuigi Rizzo 			kring->name,
181317885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
181417885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
181517885a7bSLuigi Rizzo 	}
181617885a7bSLuigi Rizzo 	return head;
1817f9790aebSLuigi Rizzo }
1818f9790aebSLuigi Rizzo 
181917885a7bSLuigi Rizzo 
1820f9790aebSLuigi Rizzo /*
182168b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
182217885a7bSLuigi Rizzo  * Can't do much more than resetting head = cur = hwcur, tail = hwtail
182368b8534bSLuigi Rizzo  * Return 1 on reinit.
1824506cc70cSLuigi Rizzo  *
1825506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1826506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
182717885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1828506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1829506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1830506cc70cSLuigi Rizzo  * it under lock.
183168b8534bSLuigi Rizzo  */
183268b8534bSLuigi Rizzo int
183368b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
183468b8534bSLuigi Rizzo {
183568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
183668b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
183768b8534bSLuigi Rizzo 	int errors = 0;
183868b8534bSLuigi Rizzo 
1839ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
184075f4f3edSVincenzo Maffione 	nm_prlim(10, "called for %s", kring->name);
184117885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
184217885a7bSLuigi Rizzo 	kring->rhead = ring->head;
184317885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
184417885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
184517885a7bSLuigi Rizzo 
184668b8534bSLuigi Rizzo 	if (ring->cur > lim)
184768b8534bSLuigi Rizzo 		errors++;
184817885a7bSLuigi Rizzo 	if (ring->head > lim)
184917885a7bSLuigi Rizzo 		errors++;
185017885a7bSLuigi Rizzo 	if (ring->tail > lim)
185117885a7bSLuigi Rizzo 		errors++;
185268b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
185368b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
185468b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
1855847bf383SLuigi Rizzo 		if (idx < 2 || idx >= kring->na->na_lut.objtotal) {
185675f4f3edSVincenzo Maffione 			nm_prlim(5, "bad index at slot %d idx %d len %d ", i, idx, len);
185768b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
185868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
18594bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
186068b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
186175f4f3edSVincenzo Maffione 			nm_prlim(5, "bad len at slot %d idx %d len %d", i, idx, len);
186268b8534bSLuigi Rizzo 		}
186368b8534bSLuigi Rizzo 	}
186468b8534bSLuigi Rizzo 	if (errors) {
186575f4f3edSVincenzo Maffione 		nm_prlim(10, "total %d errors", errors);
186675f4f3edSVincenzo Maffione 		nm_prlim(10, "%s reinit, cur %d -> %d tail %d -> %d",
186717885a7bSLuigi Rizzo 			kring->name,
186868b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
186917885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
187017885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
187117885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
187217885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
187368b8534bSLuigi Rizzo 	}
187468b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
187568b8534bSLuigi Rizzo }
187668b8534bSLuigi Rizzo 
18774bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
18784bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
18794bf50f18SLuigi Rizzo  *
18804bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
18814bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
18824bf50f18SLuigi Rizzo  *
188368b8534bSLuigi Rizzo  */
18844bf50f18SLuigi Rizzo int
1885ee0005f1SVincenzo Maffione netmap_interp_ringid(struct netmap_priv_d *priv, struct nmreq_header *hdr)
188668b8534bSLuigi Rizzo {
1887f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1888ee0005f1SVincenzo Maffione 	struct nmreq_register *reg = (struct nmreq_register *)hdr->nr_body;
188937e3a6d3SLuigi Rizzo 	int excluded_direction[] = { NR_TX_RINGS_ONLY, NR_RX_RINGS_ONLY };
1890847bf383SLuigi Rizzo 	enum txrx t;
18912ff91c17SVincenzo Maffione 	u_int j;
1892ee0005f1SVincenzo Maffione 	u_int nr_flags = reg->nr_flags, nr_mode = reg->nr_mode,
1893ee0005f1SVincenzo Maffione 	      nr_ringid = reg->nr_ringid;
189468b8534bSLuigi Rizzo 
189537e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
18962ff91c17SVincenzo Maffione 		if (nr_flags & excluded_direction[t]) {
189737e3a6d3SLuigi Rizzo 			priv->np_qfirst[t] = priv->np_qlast[t] = 0;
189837e3a6d3SLuigi Rizzo 			continue;
189937e3a6d3SLuigi Rizzo 		}
19002ff91c17SVincenzo Maffione 		switch (nr_mode) {
1901f0ea3689SLuigi Rizzo 		case NR_REG_ALL_NIC:
1902b6e66be2SVincenzo Maffione 		case NR_REG_NULL:
1903847bf383SLuigi Rizzo 			priv->np_qfirst[t] = 0;
1904847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t);
190575f4f3edSVincenzo Maffione 			nm_prdis("ALL/PIPE: %s %d %d", nm_txrx2str(t),
190637e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1907f0ea3689SLuigi Rizzo 			break;
1908f0ea3689SLuigi Rizzo 		case NR_REG_SW:
1909f0ea3689SLuigi Rizzo 		case NR_REG_NIC_SW:
1910f0ea3689SLuigi Rizzo 			if (!(na->na_flags & NAF_HOST_RINGS)) {
1911b6e66be2SVincenzo Maffione 				nm_prerr("host rings not supported");
1912f0ea3689SLuigi Rizzo 				return EINVAL;
1913f0ea3689SLuigi Rizzo 			}
19142ff91c17SVincenzo Maffione 			priv->np_qfirst[t] = (nr_mode == NR_REG_SW ?
1915847bf383SLuigi Rizzo 				nma_get_nrings(na, t) : 0);
19162a7db7a6SVincenzo Maffione 			priv->np_qlast[t] = netmap_all_rings(na, t);
191775f4f3edSVincenzo Maffione 			nm_prdis("%s: %s %d %d", nr_mode == NR_REG_SW ? "SW" : "NIC+SW",
191837e3a6d3SLuigi Rizzo 				nm_txrx2str(t),
191937e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1920f0ea3689SLuigi Rizzo 			break;
1921f0ea3689SLuigi Rizzo 		case NR_REG_ONE_NIC:
19222ff91c17SVincenzo Maffione 			if (nr_ringid >= na->num_tx_rings &&
19232ff91c17SVincenzo Maffione 					nr_ringid >= na->num_rx_rings) {
1924b6e66be2SVincenzo Maffione 				nm_prerr("invalid ring id %d", nr_ringid);
1925f0ea3689SLuigi Rizzo 				return EINVAL;
1926f0ea3689SLuigi Rizzo 			}
1927f0ea3689SLuigi Rizzo 			/* if not enough rings, use the first one */
19282ff91c17SVincenzo Maffione 			j = nr_ringid;
1929847bf383SLuigi Rizzo 			if (j >= nma_get_nrings(na, t))
1930f0ea3689SLuigi Rizzo 				j = 0;
1931847bf383SLuigi Rizzo 			priv->np_qfirst[t] = j;
1932847bf383SLuigi Rizzo 			priv->np_qlast[t] = j + 1;
193375f4f3edSVincenzo Maffione 			nm_prdis("ONE_NIC: %s %d %d", nm_txrx2str(t),
193437e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1935f0ea3689SLuigi Rizzo 			break;
1936d12354a5SVincenzo Maffione 		case NR_REG_ONE_SW:
1937d12354a5SVincenzo Maffione 			if (!(na->na_flags & NAF_HOST_RINGS)) {
1938d12354a5SVincenzo Maffione 				nm_prerr("host rings not supported");
1939d12354a5SVincenzo Maffione 				return EINVAL;
1940d12354a5SVincenzo Maffione 			}
1941d12354a5SVincenzo Maffione 			if (nr_ringid >= na->num_host_tx_rings &&
1942d12354a5SVincenzo Maffione 					nr_ringid >= na->num_host_rx_rings) {
1943d12354a5SVincenzo Maffione 				nm_prerr("invalid ring id %d", nr_ringid);
1944d12354a5SVincenzo Maffione 				return EINVAL;
1945d12354a5SVincenzo Maffione 			}
1946d12354a5SVincenzo Maffione 			/* if not enough rings, use the first one */
1947d12354a5SVincenzo Maffione 			j = nr_ringid;
1948d12354a5SVincenzo Maffione 			if (j >= nma_get_host_nrings(na, t))
1949d12354a5SVincenzo Maffione 				j = 0;
1950d12354a5SVincenzo Maffione 			priv->np_qfirst[t] = nma_get_nrings(na, t) + j;
1951d12354a5SVincenzo Maffione 			priv->np_qlast[t] = nma_get_nrings(na, t) + j + 1;
1952d12354a5SVincenzo Maffione 			nm_prdis("ONE_SW: %s %d %d", nm_txrx2str(t),
1953d12354a5SVincenzo Maffione 				priv->np_qfirst[t], priv->np_qlast[t]);
1954d12354a5SVincenzo Maffione 			break;
1955f0ea3689SLuigi Rizzo 		default:
1956b6e66be2SVincenzo Maffione 			nm_prerr("invalid regif type %d", nr_mode);
1957f0ea3689SLuigi Rizzo 			return EINVAL;
195868b8534bSLuigi Rizzo 		}
195937e3a6d3SLuigi Rizzo 	}
1960b6e66be2SVincenzo Maffione 	priv->np_flags = nr_flags;
19614bf50f18SLuigi Rizzo 
1962c3e9b4dbSLuiz Otavio O Souza 	/* Allow transparent forwarding mode in the host --> nic
1963c3e9b4dbSLuiz Otavio O Souza 	 * direction only if all the TX hw rings have been opened. */
1964c3e9b4dbSLuiz Otavio O Souza 	if (priv->np_qfirst[NR_TX] == 0 &&
1965c3e9b4dbSLuiz Otavio O Souza 			priv->np_qlast[NR_TX] >= na->num_tx_rings) {
1966c3e9b4dbSLuiz Otavio O Souza 		priv->np_sync_flags |= NAF_CAN_FORWARD_DOWN;
1967c3e9b4dbSLuiz Otavio O Souza 	}
1968c3e9b4dbSLuiz Otavio O Souza 
1969ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1970b6e66be2SVincenzo Maffione 		nm_prinf("%s: tx [%d,%d) rx [%d,%d) id %d",
19714bf50f18SLuigi Rizzo 			na->name,
1972847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1973847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1974847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1975847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX],
19762ff91c17SVincenzo Maffione 			nr_ringid);
1977ae10d1afSLuigi Rizzo 	}
197868b8534bSLuigi Rizzo 	return 0;
197968b8534bSLuigi Rizzo }
198068b8534bSLuigi Rizzo 
19814bf50f18SLuigi Rizzo 
19824bf50f18SLuigi Rizzo /*
19834bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
19844bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
19854bf50f18SLuigi Rizzo  */
19864bf50f18SLuigi Rizzo static int
1987ee0005f1SVincenzo Maffione netmap_set_ringid(struct netmap_priv_d *priv, struct nmreq_header *hdr)
19884bf50f18SLuigi Rizzo {
19894bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1990ee0005f1SVincenzo Maffione 	struct nmreq_register *reg = (struct nmreq_register *)hdr->nr_body;
19914bf50f18SLuigi Rizzo 	int error;
1992847bf383SLuigi Rizzo 	enum txrx t;
19934bf50f18SLuigi Rizzo 
1994ee0005f1SVincenzo Maffione 	error = netmap_interp_ringid(priv, hdr);
19954bf50f18SLuigi Rizzo 	if (error) {
19964bf50f18SLuigi Rizzo 		return error;
19974bf50f18SLuigi Rizzo 	}
19984bf50f18SLuigi Rizzo 
1999ee0005f1SVincenzo Maffione 	priv->np_txpoll = (reg->nr_flags & NR_NO_TX_POLL) ? 0 : 1;
20004bf50f18SLuigi Rizzo 
20014bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
20024bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
20034bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
20044bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
20054bf50f18SLuigi Rizzo 	 */
2006847bf383SLuigi Rizzo 	for_rx_tx(t) {
2007847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
2008847bf383SLuigi Rizzo 			na->si_users[t]++;
2009847bf383SLuigi Rizzo 	}
20104bf50f18SLuigi Rizzo 	return 0;
20114bf50f18SLuigi Rizzo }
20124bf50f18SLuigi Rizzo 
2013847bf383SLuigi Rizzo static void
2014847bf383SLuigi Rizzo netmap_unset_ringid(struct netmap_priv_d *priv)
2015847bf383SLuigi Rizzo {
2016847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
2017847bf383SLuigi Rizzo 	enum txrx t;
2018847bf383SLuigi Rizzo 
2019847bf383SLuigi Rizzo 	for_rx_tx(t) {
2020847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
2021847bf383SLuigi Rizzo 			na->si_users[t]--;
2022847bf383SLuigi Rizzo 		priv->np_qfirst[t] = priv->np_qlast[t] = 0;
2023847bf383SLuigi Rizzo 	}
2024847bf383SLuigi Rizzo 	priv->np_flags = 0;
2025847bf383SLuigi Rizzo 	priv->np_txpoll = 0;
2026b6e66be2SVincenzo Maffione 	priv->np_kloop_state = 0;
2027847bf383SLuigi Rizzo }
2028847bf383SLuigi Rizzo 
2029ee0005f1SVincenzo Maffione #define within_sel(p_, t_, i_)					  	  \
2030ee0005f1SVincenzo Maffione 	((i_) < (p_)->np_qlast[(t_)])
2031ee0005f1SVincenzo Maffione #define nonempty_sel(p_, t_)						  \
2032ee0005f1SVincenzo Maffione 	(within_sel((p_), (t_), (p_)->np_qfirst[(t_)]))
2033ee0005f1SVincenzo Maffione #define foreach_selected_ring(p_, t_, i_, kring_)			  \
2034ee0005f1SVincenzo Maffione 	for ((t_) = nonempty_sel((p_), NR_RX) ? NR_RX : NR_TX,		  \
2035ee0005f1SVincenzo Maffione 	     (i_) = (p_)->np_qfirst[(t_)];				  \
2036ee0005f1SVincenzo Maffione 	     (t_ == NR_RX ||						  \
2037ee0005f1SVincenzo Maffione 	      (t == NR_TX && within_sel((p_), (t_), (i_)))) &&     	  \
2038ee0005f1SVincenzo Maffione 	      ((kring_) = NMR((p_)->np_na, (t_))[(i_)]); 		  \
2039ee0005f1SVincenzo Maffione 	     (i_) = within_sel((p_), (t_), (i_) + 1) ? (i_) + 1 :         \
2040ee0005f1SVincenzo Maffione 		(++(t_) < NR_TXRX ? (p_)->np_qfirst[(t_)] : (i_)))
2041ee0005f1SVincenzo Maffione 
2042847bf383SLuigi Rizzo 
204337e3a6d3SLuigi Rizzo /* Set the nr_pending_mode for the requested rings.
204437e3a6d3SLuigi Rizzo  * If requested, also try to get exclusive access to the rings, provided
204537e3a6d3SLuigi Rizzo  * the rings we want to bind are not exclusively owned by a previous bind.
2046847bf383SLuigi Rizzo  */
2047847bf383SLuigi Rizzo static int
204837e3a6d3SLuigi Rizzo netmap_krings_get(struct netmap_priv_d *priv)
2049847bf383SLuigi Rizzo {
2050847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
2051847bf383SLuigi Rizzo 	u_int i;
2052847bf383SLuigi Rizzo 	struct netmap_kring *kring;
2053847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
2054847bf383SLuigi Rizzo 	enum txrx t;
2055847bf383SLuigi Rizzo 
2056b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_ON)
2057b6e66be2SVincenzo Maffione 		nm_prinf("%s: grabbing tx [%d, %d) rx [%d, %d)",
2058847bf383SLuigi Rizzo 			na->name,
2059847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
2060847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
2061847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
2062847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX]);
2063847bf383SLuigi Rizzo 
2064847bf383SLuigi Rizzo 	/* first round: check that all the requested rings
206545c67e8fSVincenzo Maffione 	 * are neither already exclusively owned, nor we
2066847bf383SLuigi Rizzo 	 * want exclusive ownership when they are already in use
2067847bf383SLuigi Rizzo 	 */
2068ee0005f1SVincenzo Maffione 	foreach_selected_ring(priv, t, i, kring) {
2069847bf383SLuigi Rizzo 		if ((kring->nr_kflags & NKR_EXCLUSIVE) ||
2070847bf383SLuigi Rizzo 		    (kring->users && excl))
2071847bf383SLuigi Rizzo 		{
207275f4f3edSVincenzo Maffione 			nm_prdis("ring %s busy", kring->name);
2073847bf383SLuigi Rizzo 			return EBUSY;
2074847bf383SLuigi Rizzo 		}
2075847bf383SLuigi Rizzo 	}
2076847bf383SLuigi Rizzo 
207737e3a6d3SLuigi Rizzo 	/* second round: increment usage count (possibly marking them
207837e3a6d3SLuigi Rizzo 	 * as exclusive) and set the nr_pending_mode
2079847bf383SLuigi Rizzo 	 */
2080ee0005f1SVincenzo Maffione 	foreach_selected_ring(priv, t, i, kring) {
2081847bf383SLuigi Rizzo 		kring->users++;
2082847bf383SLuigi Rizzo 		if (excl)
2083847bf383SLuigi Rizzo 			kring->nr_kflags |= NKR_EXCLUSIVE;
208437e3a6d3SLuigi Rizzo 		kring->nr_pending_mode = NKR_NETMAP_ON;
2085847bf383SLuigi Rizzo 	}
2086847bf383SLuigi Rizzo 
2087847bf383SLuigi Rizzo 	return 0;
2088847bf383SLuigi Rizzo 
2089847bf383SLuigi Rizzo }
2090847bf383SLuigi Rizzo 
209137e3a6d3SLuigi Rizzo /* Undo netmap_krings_get(). This is done by clearing the exclusive mode
209237e3a6d3SLuigi Rizzo  * if was asked on regif, and unset the nr_pending_mode if we are the
209337e3a6d3SLuigi Rizzo  * last users of the involved rings. */
2094847bf383SLuigi Rizzo static void
209537e3a6d3SLuigi Rizzo netmap_krings_put(struct netmap_priv_d *priv)
2096847bf383SLuigi Rizzo {
2097847bf383SLuigi Rizzo 	u_int i;
2098847bf383SLuigi Rizzo 	struct netmap_kring *kring;
2099847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
2100847bf383SLuigi Rizzo 	enum txrx t;
2101847bf383SLuigi Rizzo 
210275f4f3edSVincenzo Maffione 	nm_prdis("%s: releasing tx [%d, %d) rx [%d, %d)",
2103847bf383SLuigi Rizzo 			na->name,
2104847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
2105847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
2106847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
2107847bf383SLuigi Rizzo 			priv->np_qlast[MR_RX]);
2108847bf383SLuigi Rizzo 
2109ee0005f1SVincenzo Maffione 	foreach_selected_ring(priv, t, i, kring) {
2110847bf383SLuigi Rizzo 		if (excl)
2111847bf383SLuigi Rizzo 			kring->nr_kflags &= ~NKR_EXCLUSIVE;
2112847bf383SLuigi Rizzo 		kring->users--;
211337e3a6d3SLuigi Rizzo 		if (kring->users == 0)
211437e3a6d3SLuigi Rizzo 			kring->nr_pending_mode = NKR_NETMAP_OFF;
2115847bf383SLuigi Rizzo 	}
2116847bf383SLuigi Rizzo }
2117847bf383SLuigi Rizzo 
21182ff91c17SVincenzo Maffione static int
21192ff91c17SVincenzo Maffione nm_priv_rx_enabled(struct netmap_priv_d *priv)
21202ff91c17SVincenzo Maffione {
21212ff91c17SVincenzo Maffione 	return (priv->np_qfirst[NR_RX] != priv->np_qlast[NR_RX]);
21222ff91c17SVincenzo Maffione }
21232ff91c17SVincenzo Maffione 
2124b6e66be2SVincenzo Maffione /* Validate the CSB entries for both directions (atok and ktoa).
2125b6e66be2SVincenzo Maffione  * To be called under NMG_LOCK(). */
2126b6e66be2SVincenzo Maffione static int
2127b6e66be2SVincenzo Maffione netmap_csb_validate(struct netmap_priv_d *priv, struct nmreq_opt_csb *csbo)
2128b6e66be2SVincenzo Maffione {
2129b6e66be2SVincenzo Maffione 	struct nm_csb_atok *csb_atok_base =
2130b6e66be2SVincenzo Maffione 		(struct nm_csb_atok *)(uintptr_t)csbo->csb_atok;
2131b6e66be2SVincenzo Maffione 	struct nm_csb_ktoa *csb_ktoa_base =
2132b6e66be2SVincenzo Maffione 		(struct nm_csb_ktoa *)(uintptr_t)csbo->csb_ktoa;
2133b6e66be2SVincenzo Maffione 	enum txrx t;
2134b6e66be2SVincenzo Maffione 	int num_rings[NR_TXRX], tot_rings;
2135b6e66be2SVincenzo Maffione 	size_t entry_size[2];
2136b6e66be2SVincenzo Maffione 	void *csb_start[2];
2137b6e66be2SVincenzo Maffione 	int i;
2138b6e66be2SVincenzo Maffione 
2139b6e66be2SVincenzo Maffione 	if (priv->np_kloop_state & NM_SYNC_KLOOP_RUNNING) {
2140b6e66be2SVincenzo Maffione 		nm_prerr("Cannot update CSB while kloop is running");
2141b6e66be2SVincenzo Maffione 		return EBUSY;
2142b6e66be2SVincenzo Maffione 	}
2143b6e66be2SVincenzo Maffione 
2144b6e66be2SVincenzo Maffione 	tot_rings = 0;
2145b6e66be2SVincenzo Maffione 	for_rx_tx(t) {
2146b6e66be2SVincenzo Maffione 		num_rings[t] = priv->np_qlast[t] - priv->np_qfirst[t];
2147b6e66be2SVincenzo Maffione 		tot_rings += num_rings[t];
2148b6e66be2SVincenzo Maffione 	}
2149b6e66be2SVincenzo Maffione 	if (tot_rings <= 0)
2150b6e66be2SVincenzo Maffione 		return 0;
2151b6e66be2SVincenzo Maffione 
2152b6e66be2SVincenzo Maffione 	if (!(priv->np_flags & NR_EXCLUSIVE)) {
2153b6e66be2SVincenzo Maffione 		nm_prerr("CSB mode requires NR_EXCLUSIVE");
2154b6e66be2SVincenzo Maffione 		return EINVAL;
2155b6e66be2SVincenzo Maffione 	}
2156b6e66be2SVincenzo Maffione 
2157b6e66be2SVincenzo Maffione 	entry_size[0] = sizeof(*csb_atok_base);
2158b6e66be2SVincenzo Maffione 	entry_size[1] = sizeof(*csb_ktoa_base);
2159b6e66be2SVincenzo Maffione 	csb_start[0] = (void *)csb_atok_base;
2160b6e66be2SVincenzo Maffione 	csb_start[1] = (void *)csb_ktoa_base;
2161b6e66be2SVincenzo Maffione 
2162b6e66be2SVincenzo Maffione 	for (i = 0; i < 2; i++) {
2163b6e66be2SVincenzo Maffione 		/* On Linux we could use access_ok() to simplify
2164b6e66be2SVincenzo Maffione 		 * the validation. However, the advantage of
2165b6e66be2SVincenzo Maffione 		 * this approach is that it works also on
2166b6e66be2SVincenzo Maffione 		 * FreeBSD. */
2167b6e66be2SVincenzo Maffione 		size_t csb_size = tot_rings * entry_size[i];
2168b6e66be2SVincenzo Maffione 		void *tmp;
2169b6e66be2SVincenzo Maffione 		int err;
2170b6e66be2SVincenzo Maffione 
2171b6e66be2SVincenzo Maffione 		if ((uintptr_t)csb_start[i] & (entry_size[i]-1)) {
2172b6e66be2SVincenzo Maffione 			nm_prerr("Unaligned CSB address");
2173b6e66be2SVincenzo Maffione 			return EINVAL;
2174b6e66be2SVincenzo Maffione 		}
2175b6e66be2SVincenzo Maffione 
2176b6e66be2SVincenzo Maffione 		tmp = nm_os_malloc(csb_size);
2177b6e66be2SVincenzo Maffione 		if (!tmp)
2178b6e66be2SVincenzo Maffione 			return ENOMEM;
2179b6e66be2SVincenzo Maffione 		if (i == 0) {
2180b6e66be2SVincenzo Maffione 			/* Application --> kernel direction. */
2181b6e66be2SVincenzo Maffione 			err = copyin(csb_start[i], tmp, csb_size);
2182b6e66be2SVincenzo Maffione 		} else {
2183b6e66be2SVincenzo Maffione 			/* Kernel --> application direction. */
2184b6e66be2SVincenzo Maffione 			memset(tmp, 0, csb_size);
2185b6e66be2SVincenzo Maffione 			err = copyout(tmp, csb_start[i], csb_size);
2186b6e66be2SVincenzo Maffione 		}
2187b6e66be2SVincenzo Maffione 		nm_os_free(tmp);
2188b6e66be2SVincenzo Maffione 		if (err) {
2189b6e66be2SVincenzo Maffione 			nm_prerr("Invalid CSB address");
2190b6e66be2SVincenzo Maffione 			return err;
2191b6e66be2SVincenzo Maffione 		}
2192b6e66be2SVincenzo Maffione 	}
2193b6e66be2SVincenzo Maffione 
2194b6e66be2SVincenzo Maffione 	priv->np_csb_atok_base = csb_atok_base;
2195b6e66be2SVincenzo Maffione 	priv->np_csb_ktoa_base = csb_ktoa_base;
2196b6e66be2SVincenzo Maffione 
2197b6e66be2SVincenzo Maffione 	/* Initialize the CSB. */
2198b6e66be2SVincenzo Maffione 	for_rx_tx(t) {
2199b6e66be2SVincenzo Maffione 		for (i = 0; i < num_rings[t]; i++) {
2200b6e66be2SVincenzo Maffione 			struct netmap_kring *kring =
2201b6e66be2SVincenzo Maffione 				NMR(priv->np_na, t)[i + priv->np_qfirst[t]];
2202b6e66be2SVincenzo Maffione 			struct nm_csb_atok *csb_atok = csb_atok_base + i;
2203b6e66be2SVincenzo Maffione 			struct nm_csb_ktoa *csb_ktoa = csb_ktoa_base + i;
2204b6e66be2SVincenzo Maffione 
2205b6e66be2SVincenzo Maffione 			if (t == NR_RX) {
2206b6e66be2SVincenzo Maffione 				csb_atok += num_rings[NR_TX];
2207b6e66be2SVincenzo Maffione 				csb_ktoa += num_rings[NR_TX];
2208b6e66be2SVincenzo Maffione 			}
2209b6e66be2SVincenzo Maffione 
2210b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, head, kring->rhead);
2211b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, cur, kring->rcur);
2212b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, appl_need_kick, 1);
2213b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_atok, sync_flags, 1);
2214b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_ktoa, hwcur, kring->nr_hwcur);
2215b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_ktoa, hwtail, kring->nr_hwtail);
2216b6e66be2SVincenzo Maffione 			CSB_WRITE(csb_ktoa, kern_need_kick, 1);
2217b6e66be2SVincenzo Maffione 
2218b6e66be2SVincenzo Maffione 			nm_prinf("csb_init for kring %s: head %u, cur %u, "
2219b6e66be2SVincenzo Maffione 				"hwcur %u, hwtail %u", kring->name,
2220b6e66be2SVincenzo Maffione 				kring->rhead, kring->rcur, kring->nr_hwcur,
2221b6e66be2SVincenzo Maffione 				kring->nr_hwtail);
2222b6e66be2SVincenzo Maffione 		}
2223b6e66be2SVincenzo Maffione 	}
2224b6e66be2SVincenzo Maffione 
2225b6e66be2SVincenzo Maffione 	return 0;
2226b6e66be2SVincenzo Maffione }
2227b6e66be2SVincenzo Maffione 
222877a2baf5SVincenzo Maffione /* Ensure that the netmap adapter can support the given MTU.
222977a2baf5SVincenzo Maffione  * @return EINVAL if the na cannot be set to mtu, 0 otherwise.
223077a2baf5SVincenzo Maffione  */
223177a2baf5SVincenzo Maffione int
223277a2baf5SVincenzo Maffione netmap_buf_size_validate(const struct netmap_adapter *na, unsigned mtu) {
223377a2baf5SVincenzo Maffione 	unsigned nbs = NETMAP_BUF_SIZE(na);
223477a2baf5SVincenzo Maffione 
223577a2baf5SVincenzo Maffione 	if (mtu <= na->rx_buf_maxsize) {
223677a2baf5SVincenzo Maffione 		/* The MTU fits a single NIC slot. We only
223777a2baf5SVincenzo Maffione 		 * Need to check that netmap buffers are
223877a2baf5SVincenzo Maffione 		 * large enough to hold an MTU. NS_MOREFRAG
223977a2baf5SVincenzo Maffione 		 * cannot be used in this case. */
224077a2baf5SVincenzo Maffione 		if (nbs < mtu) {
224177a2baf5SVincenzo Maffione 			nm_prerr("error: netmap buf size (%u) "
224277a2baf5SVincenzo Maffione 				 "< device MTU (%u)", nbs, mtu);
224377a2baf5SVincenzo Maffione 			return EINVAL;
224477a2baf5SVincenzo Maffione 		}
224577a2baf5SVincenzo Maffione 	} else {
224677a2baf5SVincenzo Maffione 		/* More NIC slots may be needed to receive
224777a2baf5SVincenzo Maffione 		 * or transmit a single packet. Check that
224877a2baf5SVincenzo Maffione 		 * the adapter supports NS_MOREFRAG and that
224977a2baf5SVincenzo Maffione 		 * netmap buffers are large enough to hold
225077a2baf5SVincenzo Maffione 		 * the maximum per-slot size. */
225177a2baf5SVincenzo Maffione 		if (!(na->na_flags & NAF_MOREFRAG)) {
225277a2baf5SVincenzo Maffione 			nm_prerr("error: large MTU (%d) needed "
225377a2baf5SVincenzo Maffione 				 "but %s does not support "
225477a2baf5SVincenzo Maffione 				 "NS_MOREFRAG", mtu,
225577a2baf5SVincenzo Maffione 				 na->ifp->if_xname);
225677a2baf5SVincenzo Maffione 			return EINVAL;
225777a2baf5SVincenzo Maffione 		} else if (nbs < na->rx_buf_maxsize) {
225877a2baf5SVincenzo Maffione 			nm_prerr("error: using NS_MOREFRAG on "
225977a2baf5SVincenzo Maffione 				 "%s requires netmap buf size "
226077a2baf5SVincenzo Maffione 				 ">= %u", na->ifp->if_xname,
226177a2baf5SVincenzo Maffione 				 na->rx_buf_maxsize);
226277a2baf5SVincenzo Maffione 			return EINVAL;
226377a2baf5SVincenzo Maffione 		} else {
226477a2baf5SVincenzo Maffione 			nm_prinf("info: netmap application on "
226577a2baf5SVincenzo Maffione 				 "%s needs to support "
226677a2baf5SVincenzo Maffione 				 "NS_MOREFRAG "
226777a2baf5SVincenzo Maffione 				 "(MTU=%u,netmap_buf_size=%u)",
226877a2baf5SVincenzo Maffione 				 na->ifp->if_xname, mtu, nbs);
226977a2baf5SVincenzo Maffione 		}
227077a2baf5SVincenzo Maffione 	}
227177a2baf5SVincenzo Maffione 	return 0;
227277a2baf5SVincenzo Maffione }
227377a2baf5SVincenzo Maffione 
2274a6d768d8SVincenzo Maffione /* Handle the offset option, if present in the hdr.
2275a6d768d8SVincenzo Maffione  * Returns 0 on success, or an error.
2276a6d768d8SVincenzo Maffione  */
2277a6d768d8SVincenzo Maffione static int
2278a6d768d8SVincenzo Maffione netmap_offsets_init(struct netmap_priv_d *priv, struct nmreq_header *hdr)
2279a6d768d8SVincenzo Maffione {
2280a6d768d8SVincenzo Maffione 	struct nmreq_opt_offsets *opt;
2281a6d768d8SVincenzo Maffione 	struct netmap_adapter *na = priv->np_na;
2282a6d768d8SVincenzo Maffione 	struct netmap_kring *kring;
2283a6d768d8SVincenzo Maffione 	uint64_t mask = 0, bits = 0, maxbits = sizeof(uint64_t) * 8,
2284a6d768d8SVincenzo Maffione 		 max_offset = 0, initial_offset = 0, min_gap = 0;
2285a6d768d8SVincenzo Maffione 	u_int i;
2286a6d768d8SVincenzo Maffione 	enum txrx t;
2287a6d768d8SVincenzo Maffione 	int error = 0;
2288a6d768d8SVincenzo Maffione 
2289a6d768d8SVincenzo Maffione 	opt = (struct nmreq_opt_offsets *)
2290a6d768d8SVincenzo Maffione 		nmreq_getoption(hdr, NETMAP_REQ_OPT_OFFSETS);
2291a6d768d8SVincenzo Maffione 	if (opt == NULL)
2292a6d768d8SVincenzo Maffione 		return 0;
2293a6d768d8SVincenzo Maffione 
2294a6d768d8SVincenzo Maffione 	if (!(na->na_flags & NAF_OFFSETS)) {
2295a6d768d8SVincenzo Maffione 		if (netmap_verbose)
2296a6d768d8SVincenzo Maffione 			nm_prerr("%s does not support offsets",
2297a6d768d8SVincenzo Maffione 				na->name);
2298a6d768d8SVincenzo Maffione 		error = EOPNOTSUPP;
2299a6d768d8SVincenzo Maffione 		goto out;
2300a6d768d8SVincenzo Maffione 	}
2301a6d768d8SVincenzo Maffione 
2302a6d768d8SVincenzo Maffione 	/* check sanity of the opt values */
2303a6d768d8SVincenzo Maffione 	max_offset = opt->nro_max_offset;
2304a6d768d8SVincenzo Maffione 	min_gap = opt->nro_min_gap;
2305a6d768d8SVincenzo Maffione 	initial_offset = opt->nro_initial_offset;
2306a6d768d8SVincenzo Maffione 	bits = opt->nro_offset_bits;
2307a6d768d8SVincenzo Maffione 
2308a6d768d8SVincenzo Maffione 	if (bits > maxbits) {
2309a6d768d8SVincenzo Maffione 		if (netmap_verbose)
2310a6d768d8SVincenzo Maffione 			nm_prerr("bits: %llu too large (max %llu)",
2311a6d768d8SVincenzo Maffione 				(unsigned long long)bits,
2312a6d768d8SVincenzo Maffione 				(unsigned long long)maxbits);
2313a6d768d8SVincenzo Maffione 		error = EINVAL;
2314a6d768d8SVincenzo Maffione 		goto out;
2315a6d768d8SVincenzo Maffione 	}
2316a6d768d8SVincenzo Maffione 	/* we take bits == 0 as a request to use the entire field */
2317a6d768d8SVincenzo Maffione 	if (bits == 0 || bits == maxbits) {
2318a6d768d8SVincenzo Maffione 		/* shifting a type by sizeof(type) is undefined */
2319a6d768d8SVincenzo Maffione 		bits = maxbits;
2320a6d768d8SVincenzo Maffione 		mask = 0xffffffffffffffff;
2321a6d768d8SVincenzo Maffione 	} else {
2322a6d768d8SVincenzo Maffione 		mask = (1ULL << bits) - 1;
2323a6d768d8SVincenzo Maffione 	}
2324a6d768d8SVincenzo Maffione 	if (max_offset > NETMAP_BUF_SIZE(na)) {
2325a6d768d8SVincenzo Maffione 		if (netmap_verbose)
2326a6d768d8SVincenzo Maffione 			nm_prerr("max offset %llu > buf size %u",
2327a6d768d8SVincenzo Maffione 				(unsigned long long)max_offset, NETMAP_BUF_SIZE(na));
2328a6d768d8SVincenzo Maffione 		error = EINVAL;
2329a6d768d8SVincenzo Maffione 		goto out;
2330a6d768d8SVincenzo Maffione 	}
2331a6d768d8SVincenzo Maffione 	if ((max_offset & mask) != max_offset) {
2332a6d768d8SVincenzo Maffione 		if (netmap_verbose)
2333a6d768d8SVincenzo Maffione 			nm_prerr("max offset %llu to large for %llu bits",
2334a6d768d8SVincenzo Maffione 				(unsigned long long)max_offset,
2335a6d768d8SVincenzo Maffione 				(unsigned long long)bits);
2336a6d768d8SVincenzo Maffione 		error = EINVAL;
2337a6d768d8SVincenzo Maffione 		goto out;
2338a6d768d8SVincenzo Maffione 	}
2339a6d768d8SVincenzo Maffione 	if (initial_offset > max_offset) {
2340a6d768d8SVincenzo Maffione 		if (netmap_verbose)
2341a6d768d8SVincenzo Maffione 			nm_prerr("initial offset %llu > max offset %llu",
2342a6d768d8SVincenzo Maffione 				(unsigned long long)initial_offset,
2343a6d768d8SVincenzo Maffione 				(unsigned long long)max_offset);
2344a6d768d8SVincenzo Maffione 		error = EINVAL;
2345a6d768d8SVincenzo Maffione 		goto out;
2346a6d768d8SVincenzo Maffione 	}
2347a6d768d8SVincenzo Maffione 
2348a6d768d8SVincenzo Maffione 	/* initialize the kring and ring fields. */
2349a6d768d8SVincenzo Maffione 	foreach_selected_ring(priv, t, i, kring) {
2350a6d768d8SVincenzo Maffione 		struct netmap_kring *kring = NMR(na, t)[i];
2351a6d768d8SVincenzo Maffione 		struct netmap_ring *ring = kring->ring;
2352a6d768d8SVincenzo Maffione 		u_int j;
2353a6d768d8SVincenzo Maffione 
2354a6d768d8SVincenzo Maffione 		/* it the ring is already in use we check that the
2355a6d768d8SVincenzo Maffione 		 * new request is compatible with the existing one
2356a6d768d8SVincenzo Maffione 		 */
2357a6d768d8SVincenzo Maffione 		if (kring->offset_mask) {
2358a6d768d8SVincenzo Maffione 			if ((kring->offset_mask & mask) != mask ||
2359a6d768d8SVincenzo Maffione 			     kring->offset_max < max_offset) {
2360a6d768d8SVincenzo Maffione 				if (netmap_verbose)
2361a6d768d8SVincenzo Maffione 					nm_prinf("%s: cannot increase"
2362a6d768d8SVincenzo Maffione 						 "offset mask and/or max"
2363a6d768d8SVincenzo Maffione 						 "(current: mask=%llx,max=%llu",
2364a6d768d8SVincenzo Maffione 							kring->name,
2365a6d768d8SVincenzo Maffione 							(unsigned long long)kring->offset_mask,
2366a6d768d8SVincenzo Maffione 							(unsigned long long)kring->offset_max);
2367a6d768d8SVincenzo Maffione 				error = EBUSY;
2368a6d768d8SVincenzo Maffione 				goto out;
2369a6d768d8SVincenzo Maffione 			}
2370a6d768d8SVincenzo Maffione 			mask = kring->offset_mask;
2371a6d768d8SVincenzo Maffione 			max_offset = kring->offset_max;
2372a6d768d8SVincenzo Maffione 		} else {
2373a6d768d8SVincenzo Maffione 			kring->offset_mask = mask;
2374a6d768d8SVincenzo Maffione 			*(uint64_t *)(uintptr_t)&ring->offset_mask = mask;
2375a6d768d8SVincenzo Maffione 			kring->offset_max = max_offset;
2376a6d768d8SVincenzo Maffione 			kring->offset_gap = min_gap;
2377a6d768d8SVincenzo Maffione 		}
2378a6d768d8SVincenzo Maffione 
2379a6d768d8SVincenzo Maffione 		/* if there is an initial offset, put it into
2380a6d768d8SVincenzo Maffione 		 * all the slots
2381a6d768d8SVincenzo Maffione 		 *
2382a6d768d8SVincenzo Maffione 		 * Note: we cannot change the offsets if the
2383a6d768d8SVincenzo Maffione 		 * ring is already in use.
2384a6d768d8SVincenzo Maffione 		 */
2385a6d768d8SVincenzo Maffione 		if (!initial_offset || kring->users > 1)
2386a6d768d8SVincenzo Maffione 			continue;
2387a6d768d8SVincenzo Maffione 
2388a6d768d8SVincenzo Maffione 		for (j = 0; j < kring->nkr_num_slots; j++) {
2389a6d768d8SVincenzo Maffione 			struct netmap_slot *slot = ring->slot + j;
2390a6d768d8SVincenzo Maffione 
2391a6d768d8SVincenzo Maffione 			nm_write_offset(kring, slot, initial_offset);
2392a6d768d8SVincenzo Maffione 		}
2393a6d768d8SVincenzo Maffione 	}
2394a6d768d8SVincenzo Maffione 
2395a6d768d8SVincenzo Maffione out:
2396a6d768d8SVincenzo Maffione 	opt->nro_opt.nro_status = error;
2397a6d768d8SVincenzo Maffione 	if (!error) {
2398a6d768d8SVincenzo Maffione 		opt->nro_max_offset = max_offset;
2399a6d768d8SVincenzo Maffione 	}
2400a6d768d8SVincenzo Maffione 	return error;
2401a6d768d8SVincenzo Maffione 
2402a6d768d8SVincenzo Maffione }
2403a6d768d8SVincenzo Maffione 
2404f4a54f43SVincenzo Maffione 
2405f4a54f43SVincenzo Maffione /* set the hardware buffer length in each one of the newly opened rings
2406f4a54f43SVincenzo Maffione  * (hwbuf_len field in the kring struct). The purpose it to select
2407f4a54f43SVincenzo Maffione  * the maximum supported input buffer lenght that will not cause writes
2408f4a54f43SVincenzo Maffione  * outside of the available space, even when offsets are in use.
2409f4a54f43SVincenzo Maffione  */
2410a6d768d8SVincenzo Maffione static int
2411a6d768d8SVincenzo Maffione netmap_compute_buf_len(struct netmap_priv_d *priv)
2412a6d768d8SVincenzo Maffione {
2413a6d768d8SVincenzo Maffione 	enum txrx t;
2414a6d768d8SVincenzo Maffione 	u_int i;
2415a6d768d8SVincenzo Maffione 	struct netmap_kring *kring;
2416a6d768d8SVincenzo Maffione 	int error = 0;
2417a6d768d8SVincenzo Maffione 	unsigned mtu = 0;
2418a6d768d8SVincenzo Maffione 	struct netmap_adapter *na = priv->np_na;
2419f4a54f43SVincenzo Maffione 	uint64_t target;
2420a6d768d8SVincenzo Maffione 
2421a6d768d8SVincenzo Maffione 	foreach_selected_ring(priv, t, i, kring) {
2422f4a54f43SVincenzo Maffione 		/* rings that are already active have their hwbuf_len
2423f4a54f43SVincenzo Maffione 		 * already set and we cannot change it.
2424f4a54f43SVincenzo Maffione 		 */
2425a6d768d8SVincenzo Maffione 		if (kring->users > 1)
2426a6d768d8SVincenzo Maffione 			continue;
2427a6d768d8SVincenzo Maffione 
2428f4a54f43SVincenzo Maffione 		/* For netmap buffers which are not shared among several ring
2429f4a54f43SVincenzo Maffione 		 * slots (the normal case), the available space is the buf size
2430f4a54f43SVincenzo Maffione 		 * minus the max offset declared by the user at open time.  If
2431f4a54f43SVincenzo Maffione 		 * the user plans to have several slots pointing to different
2432f4a54f43SVincenzo Maffione 		 * offsets into the same large buffer, she must also declare a
2433f4a54f43SVincenzo Maffione 		 * "minimum gap" between two such consecutive offsets. In this
2434f4a54f43SVincenzo Maffione 		 * case the user-declared 'offset_gap' is taken as the
2435f4a54f43SVincenzo Maffione 		 * available space and offset_max is ignored.
2436f4a54f43SVincenzo Maffione 		 */
2437f4a54f43SVincenzo Maffione 
2438f4a54f43SVincenzo Maffione 		/* start with the normal case (unshared buffers) */
2439a6d768d8SVincenzo Maffione 		target = NETMAP_BUF_SIZE(kring->na) -
2440a6d768d8SVincenzo Maffione 			kring->offset_max;
2441f4a54f43SVincenzo Maffione 		/* if offset_gap is zero, the user does not intend to use
2442f4a54f43SVincenzo Maffione 		 * shared buffers. In this case the minimum gap between
2443f4a54f43SVincenzo Maffione 		 * two consective offsets into the same buffer can be
2444f4a54f43SVincenzo Maffione 		 * assumed to be equal to the buffer size. In this way
2445f4a54f43SVincenzo Maffione 		 * offset_gap always contains the available space ignoring
2446f4a54f43SVincenzo Maffione 		 * offset_max. This may be used by drivers of NICs that
2447f4a54f43SVincenzo Maffione 		 * are guaranteed to never write more than MTU bytes, even
2448f4a54f43SVincenzo Maffione 		 * if the input buffer is larger: if the MTU is less
2449f4a54f43SVincenzo Maffione 		 * than the target they can set hwbuf_len to offset_gap.
2450f4a54f43SVincenzo Maffione 		 */
2451a6d768d8SVincenzo Maffione 		if (!kring->offset_gap)
2452a6d768d8SVincenzo Maffione 			kring->offset_gap =
2453a6d768d8SVincenzo Maffione 				NETMAP_BUF_SIZE(kring->na);
2454f4a54f43SVincenzo Maffione 
2455a6d768d8SVincenzo Maffione 		if (kring->offset_gap < target)
2456a6d768d8SVincenzo Maffione 			target = kring->offset_gap;
2457a6d768d8SVincenzo Maffione 		error = kring->nm_bufcfg(kring, target);
2458a6d768d8SVincenzo Maffione 		if (error)
2459a6d768d8SVincenzo Maffione 			goto out;
2460a6d768d8SVincenzo Maffione 
2461a6d768d8SVincenzo Maffione 		*(uint64_t *)(uintptr_t)&kring->ring->buf_align = kring->buf_align;
2462a6d768d8SVincenzo Maffione 
2463a6d768d8SVincenzo Maffione 		if (mtu && t == NR_RX && kring->hwbuf_len < mtu) {
2464a6d768d8SVincenzo Maffione 			if (!(na->na_flags & NAF_MOREFRAG)) {
2465a6d768d8SVincenzo Maffione 				nm_prerr("error: large MTU (%d) needed "
2466a6d768d8SVincenzo Maffione 					 "but %s does not support "
2467a6d768d8SVincenzo Maffione 					 "NS_MOREFRAG", mtu,
2468a6d768d8SVincenzo Maffione 					 na->name);
2469a6d768d8SVincenzo Maffione 				error = EINVAL;
2470a6d768d8SVincenzo Maffione 				goto out;
2471a6d768d8SVincenzo Maffione 			} else {
2472a6d768d8SVincenzo Maffione 				nm_prinf("info: netmap application on "
2473a6d768d8SVincenzo Maffione 					 "%s needs to support "
2474a6d768d8SVincenzo Maffione 					 "NS_MOREFRAG "
2475a6d768d8SVincenzo Maffione 					 "(MTU=%u,buf_size=%llu)",
2476a6d768d8SVincenzo Maffione 					 kring->name, mtu,
2477a6d768d8SVincenzo Maffione 					 (unsigned long long)kring->hwbuf_len);
2478a6d768d8SVincenzo Maffione 			}
2479a6d768d8SVincenzo Maffione 		}
2480a6d768d8SVincenzo Maffione 	}
2481a6d768d8SVincenzo Maffione out:
2482a6d768d8SVincenzo Maffione 	return error;
2483a6d768d8SVincenzo Maffione }
248477a2baf5SVincenzo Maffione 
2485f18be576SLuigi Rizzo /*
2486f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
2487f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
2488ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
24894bf50f18SLuigi Rizzo  *
24904bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
24914bf50f18SLuigi Rizzo  *
24924bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
24934bf50f18SLuigi Rizzo  * (get current number and size of rings)
24944bf50f18SLuigi Rizzo  *
24954bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
24964bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
24974bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
24984bf50f18SLuigi Rizzo  *
24994bf50f18SLuigi Rizzo  *
2500847bf383SLuigi Rizzo  * na->nm_krings_create()
25014bf50f18SLuigi Rizzo  * (create and init the krings array)
25024bf50f18SLuigi Rizzo  *
25034bf50f18SLuigi Rizzo  * 	One of the following:
25044bf50f18SLuigi Rizzo  *
25054bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
25064bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
25074bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
25084bf50f18SLuigi Rizzo  *
25094bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
25104bf50f18SLuigi Rizzo  * 		add leases and scratchpads
25114bf50f18SLuigi Rizzo  *
25124bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
25134bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
25144bf50f18SLuigi Rizzo  * 		cross-link them
25154bf50f18SLuigi Rizzo  *
25164bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
25174bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
25184bf50f18SLuigi Rizzo  *
25194bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
25204bf50f18SLuigi Rizzo  *      	create both the brap krings array,
25214bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
25224bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
25234bf50f18SLuigi Rizzo  *
25244bf50f18SLuigi Rizzo  * na->nm_register(, 1)
25254bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
25264bf50f18SLuigi Rizzo  *
25274bf50f18SLuigi Rizzo  * 	This may be one of the following:
25284bf50f18SLuigi Rizzo  *
252937e3a6d3SLuigi Rizzo  * 	* netmap_hw_reg				        (hw ports)
25304bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
25314bf50f18SLuigi Rizzo  * 		the hardware specific callback;
25324bf50f18SLuigi Rizzo  *
25334bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
25344bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
25354bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
25364bf50f18SLuigi Rizzo  *		bridge write lock.
25374bf50f18SLuigi Rizzo  *
25384bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
25394bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
2540453130d9SPedro F. Giffuni  *		longer responsible for the lifetime of this
25414bf50f18SLuigi Rizzo  *		pipe end
25424bf50f18SLuigi Rizzo  *
25434bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
25444bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
25454bf50f18SLuigi Rizzo  *		rings
25464bf50f18SLuigi Rizzo  *
254737e3a6d3SLuigi Rizzo  *	* netmap_bwrap_reg				(bwraps)
25484bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
25494bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
25504bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
25514bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
25524bf50f18SLuigi Rizzo  *
25534bf50f18SLuigi Rizzo  *
2554f18be576SLuigi Rizzo  */
2555847bf383SLuigi Rizzo int
2556f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
2557ee0005f1SVincenzo Maffione 	struct nmreq_header *hdr)
2558f18be576SLuigi Rizzo {
2559f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
2560847bf383SLuigi Rizzo 	int error;
2561f18be576SLuigi Rizzo 
2562ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
2563f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
2564847bf383SLuigi Rizzo 	error = netmap_mem_finalize(na->nm_mem, na);
2565ce3ee1e7SLuigi Rizzo 	if (error)
2566847bf383SLuigi Rizzo 		goto err;
2567847bf383SLuigi Rizzo 
2568847bf383SLuigi Rizzo 	if (na->active_fds == 0) {
25692ff91c17SVincenzo Maffione 
25702ff91c17SVincenzo Maffione 		/* cache the allocator info in the na */
25712ff91c17SVincenzo Maffione 		error = netmap_mem_get_lut(na->nm_mem, &na->na_lut);
25722ff91c17SVincenzo Maffione 		if (error)
25732ff91c17SVincenzo Maffione 			goto err_drop_mem;
257475f4f3edSVincenzo Maffione 		nm_prdis("lut %p bufs %u size %u", na->na_lut.lut, na->na_lut.objtotal,
25752ff91c17SVincenzo Maffione 					    na->na_lut.objsize);
25762ff91c17SVincenzo Maffione 
25772ff91c17SVincenzo Maffione 		/* ring configuration may have changed, fetch from the card */
25782ff91c17SVincenzo Maffione 		netmap_update_config(na);
2579cfa866f6SMatt Macy 	}
25802ff91c17SVincenzo Maffione 
2581cfa866f6SMatt Macy 	/* compute the range of tx and rx rings to monitor */
2582ee0005f1SVincenzo Maffione 	error = netmap_set_ringid(priv, hdr);
2583cfa866f6SMatt Macy 	if (error)
2584cfa866f6SMatt Macy 		goto err_put_lut;
2585cfa866f6SMatt Macy 
2586cfa866f6SMatt Macy 	if (na->active_fds == 0) {
2587847bf383SLuigi Rizzo 		/*
2588847bf383SLuigi Rizzo 		 * If this is the first registration of the adapter,
25894f80b14cSVincenzo Maffione 		 * perform sanity checks and create the in-kernel view
25904f80b14cSVincenzo Maffione 		 * of the netmap rings (the netmap krings).
2591847bf383SLuigi Rizzo 		 */
25922ff91c17SVincenzo Maffione 		if (na->ifp && nm_priv_rx_enabled(priv)) {
25934f80b14cSVincenzo Maffione 			/* This netmap adapter is attached to an ifnet. */
25944f80b14cSVincenzo Maffione 			unsigned mtu = nm_os_ifnet_mtu(na->ifp);
25954f80b14cSVincenzo Maffione 
259675f4f3edSVincenzo Maffione 			nm_prdis("%s: mtu %d rx_buf_maxsize %d netmap_buf_size %d",
259777a2baf5SVincenzo Maffione 				na->name, mtu, na->rx_buf_maxsize, NETMAP_BUF_SIZE(na));
2598cfa866f6SMatt Macy 
2599cfa866f6SMatt Macy 			if (na->rx_buf_maxsize == 0) {
2600b6e66be2SVincenzo Maffione 				nm_prerr("%s: error: rx_buf_maxsize == 0", na->name);
2601cfa866f6SMatt Macy 				error = EIO;
2602cfa866f6SMatt Macy 				goto err_drop_mem;
2603cfa866f6SMatt Macy 			}
26042ff91c17SVincenzo Maffione 
260577a2baf5SVincenzo Maffione 			error = netmap_buf_size_validate(na, mtu);
260677a2baf5SVincenzo Maffione 			if (error)
26074f80b14cSVincenzo Maffione 				goto err_drop_mem;
26084f80b14cSVincenzo Maffione 		}
2609847bf383SLuigi Rizzo 
2610847bf383SLuigi Rizzo 		/*
2611847bf383SLuigi Rizzo 		 * Depending on the adapter, this may also create
2612847bf383SLuigi Rizzo 		 * the netmap rings themselves
2613847bf383SLuigi Rizzo 		 */
2614847bf383SLuigi Rizzo 		error = na->nm_krings_create(na);
2615847bf383SLuigi Rizzo 		if (error)
26162ff91c17SVincenzo Maffione 			goto err_put_lut;
2617847bf383SLuigi Rizzo 
2618ce3ee1e7SLuigi Rizzo 	}
2619847bf383SLuigi Rizzo 
262037e3a6d3SLuigi Rizzo 	/* now the krings must exist and we can check whether some
262137e3a6d3SLuigi Rizzo 	 * previous bind has exclusive ownership on them, and set
262237e3a6d3SLuigi Rizzo 	 * nr_pending_mode
2623847bf383SLuigi Rizzo 	 */
262437e3a6d3SLuigi Rizzo 	error = netmap_krings_get(priv);
2625847bf383SLuigi Rizzo 	if (error)
262637e3a6d3SLuigi Rizzo 		goto err_del_krings;
262737e3a6d3SLuigi Rizzo 
262837e3a6d3SLuigi Rizzo 	/* create all needed missing netmap rings */
262937e3a6d3SLuigi Rizzo 	error = netmap_mem_rings_create(na);
263037e3a6d3SLuigi Rizzo 	if (error)
263137e3a6d3SLuigi Rizzo 		goto err_rel_excl;
2632847bf383SLuigi Rizzo 
2633a6d768d8SVincenzo Maffione 	/* initialize offsets if requested */
2634a6d768d8SVincenzo Maffione 	error = netmap_offsets_init(priv, hdr);
2635a6d768d8SVincenzo Maffione 	if (error)
2636a6d768d8SVincenzo Maffione 		goto err_rel_excl;
2637a6d768d8SVincenzo Maffione 
263845c67e8fSVincenzo Maffione 	/* compute and validate the buf lengths */
2639a6d768d8SVincenzo Maffione 	error = netmap_compute_buf_len(priv);
2640a6d768d8SVincenzo Maffione 	if (error)
2641a6d768d8SVincenzo Maffione 		goto err_rel_excl;
2642a6d768d8SVincenzo Maffione 
2643847bf383SLuigi Rizzo 	/* in all cases, create a new netmap if */
2644c3e9b4dbSLuiz Otavio O Souza 	nifp = netmap_mem_if_new(na, priv);
2645847bf383SLuigi Rizzo 	if (nifp == NULL) {
2646f18be576SLuigi Rizzo 		error = ENOMEM;
2647cfa866f6SMatt Macy 		goto err_rel_excl;
2648ce3ee1e7SLuigi Rizzo 	}
2649847bf383SLuigi Rizzo 
265037e3a6d3SLuigi Rizzo 	if (nm_kring_pending(priv)) {
265137e3a6d3SLuigi Rizzo 		/* Some kring is switching mode, tell the adapter to
265237e3a6d3SLuigi Rizzo 		 * react on this. */
265398399ab0SVincenzo Maffione 		netmap_set_all_rings(na, NM_KR_LOCKED);
265437e3a6d3SLuigi Rizzo 		error = na->nm_register(na, 1);
265598399ab0SVincenzo Maffione 		netmap_set_all_rings(na, 0);
265637e3a6d3SLuigi Rizzo 		if (error)
26572ff91c17SVincenzo Maffione 			goto err_del_if;
265837e3a6d3SLuigi Rizzo 	}
265937e3a6d3SLuigi Rizzo 
266037e3a6d3SLuigi Rizzo 	/* Commit the reference. */
266137e3a6d3SLuigi Rizzo 	na->active_fds++;
266237e3a6d3SLuigi Rizzo 
2663ce3ee1e7SLuigi Rizzo 	/*
2664847bf383SLuigi Rizzo 	 * advertise that the interface is ready by setting np_nifp.
2665847bf383SLuigi Rizzo 	 * The barrier is needed because readers (poll, *SYNC and mmap)
2666ce3ee1e7SLuigi Rizzo 	 * check for priv->np_nifp != NULL without locking
2667ce3ee1e7SLuigi Rizzo 	 */
2668847bf383SLuigi Rizzo 	mb(); /* make sure previous writes are visible to all CPUs */
2669ce3ee1e7SLuigi Rizzo 	priv->np_nifp = nifp;
2670847bf383SLuigi Rizzo 
2671847bf383SLuigi Rizzo 	return 0;
2672847bf383SLuigi Rizzo 
267337e3a6d3SLuigi Rizzo err_del_if:
2674847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
26754f80b14cSVincenzo Maffione err_rel_excl:
26764f80b14cSVincenzo Maffione 	netmap_krings_put(priv);
2677cfa866f6SMatt Macy 	netmap_mem_rings_delete(na);
2678847bf383SLuigi Rizzo err_del_krings:
2679847bf383SLuigi Rizzo 	if (na->active_fds == 0)
2680847bf383SLuigi Rizzo 		na->nm_krings_delete(na);
26812ff91c17SVincenzo Maffione err_put_lut:
26822ff91c17SVincenzo Maffione 	if (na->active_fds == 0)
26832ff91c17SVincenzo Maffione 		memset(&na->na_lut, 0, sizeof(na->na_lut));
2684847bf383SLuigi Rizzo err_drop_mem:
26854f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
2686847bf383SLuigi Rizzo err:
2687847bf383SLuigi Rizzo 	priv->np_na = NULL;
2688847bf383SLuigi Rizzo 	return error;
2689ce3ee1e7SLuigi Rizzo }
2690847bf383SLuigi Rizzo 
2691847bf383SLuigi Rizzo 
2692847bf383SLuigi Rizzo /*
269337e3a6d3SLuigi Rizzo  * update kring and ring at the end of rxsync/txsync.
2694847bf383SLuigi Rizzo  */
2695847bf383SLuigi Rizzo static inline void
269637e3a6d3SLuigi Rizzo nm_sync_finalize(struct netmap_kring *kring)
2697847bf383SLuigi Rizzo {
269837e3a6d3SLuigi Rizzo 	/*
269937e3a6d3SLuigi Rizzo 	 * Update ring tail to what the kernel knows
270037e3a6d3SLuigi Rizzo 	 * After txsync: head/rhead/hwcur might be behind cur/rcur
270137e3a6d3SLuigi Rizzo 	 * if no carrier.
270237e3a6d3SLuigi Rizzo 	 */
2703847bf383SLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
2704847bf383SLuigi Rizzo 
270575f4f3edSVincenzo Maffione 	nm_prdis(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
2706847bf383SLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
2707847bf383SLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
2708847bf383SLuigi Rizzo }
2709847bf383SLuigi Rizzo 
2710c3e9b4dbSLuiz Otavio O Souza /* set ring timestamp */
2711c3e9b4dbSLuiz Otavio O Souza static inline void
2712c3e9b4dbSLuiz Otavio O Souza ring_timestamp_set(struct netmap_ring *ring)
2713c3e9b4dbSLuiz Otavio O Souza {
2714c3e9b4dbSLuiz Otavio O Souza 	if (netmap_no_timestamp == 0 || ring->flags & NR_TIMESTAMP) {
2715c3e9b4dbSLuiz Otavio O Souza 		microtime(&ring->ts);
2716c3e9b4dbSLuiz Otavio O Souza 	}
2717c3e9b4dbSLuiz Otavio O Souza }
2718c3e9b4dbSLuiz Otavio O Souza 
27192ff91c17SVincenzo Maffione static int nmreq_copyin(struct nmreq_header *, int);
27202ff91c17SVincenzo Maffione static int nmreq_copyout(struct nmreq_header *, int);
27212ff91c17SVincenzo Maffione static int nmreq_checkoptions(struct nmreq_header *);
2722c3e9b4dbSLuiz Otavio O Souza 
272368b8534bSLuigi Rizzo /*
272468b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
272568b8534bSLuigi Rizzo  *
272668b8534bSLuigi Rizzo  * Following a list of accepted commands:
27272ff91c17SVincenzo Maffione  * - NIOCCTRL		device control API
27282ff91c17SVincenzo Maffione  * - NIOCTXSYNC		sync TX rings
27292ff91c17SVincenzo Maffione  * - NIOCRXSYNC		sync RX rings
273068b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
27312ff91c17SVincenzo Maffione  * - NIOCGINFO		deprecated (legacy API)
27322ff91c17SVincenzo Maffione  * - NIOCREGIF		deprecated (legacy API)
273368b8534bSLuigi Rizzo  *
273468b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
273568b8534bSLuigi Rizzo  */
2736f9790aebSLuigi Rizzo int
27372ff91c17SVincenzo Maffione netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
27382ff91c17SVincenzo Maffione 		struct thread *td, int nr_body_is_user)
273968b8534bSLuigi Rizzo {
2740c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
2741ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
2742c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd = NULL;
274337e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
274437e3a6d3SLuigi Rizzo 	int error = 0;
2745f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
27462ff91c17SVincenzo Maffione 	struct netmap_kring **krings;
2747c3e9b4dbSLuiz Otavio O Souza 	int sync_flags;
2748847bf383SLuigi Rizzo 	enum txrx t;
274968b8534bSLuigi Rizzo 
27502ff91c17SVincenzo Maffione 	switch (cmd) {
27512ff91c17SVincenzo Maffione 	case NIOCCTRL: {
27522ff91c17SVincenzo Maffione 		struct nmreq_header *hdr = (struct nmreq_header *)data;
27532ff91c17SVincenzo Maffione 
27542ff91c17SVincenzo Maffione 		if (hdr->nr_version < NETMAP_MIN_API ||
27552ff91c17SVincenzo Maffione 		    hdr->nr_version > NETMAP_MAX_API) {
2756b6e66be2SVincenzo Maffione 			nm_prerr("API mismatch: got %d need %d",
2757b6e66be2SVincenzo Maffione 				hdr->nr_version, NETMAP_API);
275817885a7bSLuigi Rizzo 			return EINVAL;
275917885a7bSLuigi Rizzo 		}
276068b8534bSLuigi Rizzo 
27612ff91c17SVincenzo Maffione 		/* Make a kernel-space copy of the user-space nr_body.
276245c67e8fSVincenzo Maffione 		 * For convenience, the nr_body pointer and the pointers
27632ff91c17SVincenzo Maffione 		 * in the options list will be replaced with their
27642ff91c17SVincenzo Maffione 		 * kernel-space counterparts. The original pointers are
27652ff91c17SVincenzo Maffione 		 * saved internally and later restored by nmreq_copyout
27662ff91c17SVincenzo Maffione 		 */
27672ff91c17SVincenzo Maffione 		error = nmreq_copyin(hdr, nr_body_is_user);
276837e3a6d3SLuigi Rizzo 		if (error) {
27692ff91c17SVincenzo Maffione 			return error;
2770ce3ee1e7SLuigi Rizzo 		}
2771ce3ee1e7SLuigi Rizzo 
27722ff91c17SVincenzo Maffione 		/* Sanitize hdr->nr_name. */
27732ff91c17SVincenzo Maffione 		hdr->nr_name[sizeof(hdr->nr_name) - 1] = '\0';
277468b8534bSLuigi Rizzo 
27752ff91c17SVincenzo Maffione 		switch (hdr->nr_reqtype) {
27762ff91c17SVincenzo Maffione 		case NETMAP_REQ_REGISTER: {
27772ff91c17SVincenzo Maffione 			struct nmreq_register *req =
2778cfa866f6SMatt Macy 				(struct nmreq_register *)(uintptr_t)hdr->nr_body;
2779b6e66be2SVincenzo Maffione 			struct netmap_if *nifp;
2780b6e66be2SVincenzo Maffione 
27812ff91c17SVincenzo Maffione 			/* Protect access to priv from concurrent requests. */
2782ce3ee1e7SLuigi Rizzo 			NMG_LOCK();
2783ce3ee1e7SLuigi Rizzo 			do {
27842ff91c17SVincenzo Maffione 				struct nmreq_option *opt;
2785b6e66be2SVincenzo Maffione 				u_int memflags;
2786ce3ee1e7SLuigi Rizzo 
2787847bf383SLuigi Rizzo 				if (priv->np_nifp != NULL) {	/* thread already registered */
2788f0ea3689SLuigi Rizzo 					error = EBUSY;
2789506cc70cSLuigi Rizzo 					break;
2790506cc70cSLuigi Rizzo 				}
2791c3e9b4dbSLuiz Otavio O Souza 
27922ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
2793253b2ec1SVincenzo Maffione 				opt = nmreq_getoption(hdr, NETMAP_REQ_OPT_EXTMEM);
27942ff91c17SVincenzo Maffione 				if (opt != NULL) {
27952ff91c17SVincenzo Maffione 					struct nmreq_opt_extmem *e =
27962ff91c17SVincenzo Maffione 						(struct nmreq_opt_extmem *)opt;
27972ff91c17SVincenzo Maffione 
27982ff91c17SVincenzo Maffione 					nmd = netmap_mem_ext_create(e->nro_usrptr,
27992ff91c17SVincenzo Maffione 							&e->nro_info, &error);
28002ff91c17SVincenzo Maffione 					opt->nro_status = error;
28012ff91c17SVincenzo Maffione 					if (nmd == NULL)
28022ff91c17SVincenzo Maffione 						break;
28032ff91c17SVincenzo Maffione 				}
28042ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
28052ff91c17SVincenzo Maffione 
28062ff91c17SVincenzo Maffione 				if (nmd == NULL && req->nr_mem_id) {
2807c3e9b4dbSLuiz Otavio O Souza 					/* find the allocator and get a reference */
28082ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id);
2809c3e9b4dbSLuiz Otavio O Souza 					if (nmd == NULL) {
2810b6e66be2SVincenzo Maffione 						if (netmap_verbose) {
2811b6e66be2SVincenzo Maffione 							nm_prerr("%s: failed to find mem_id %u",
2812b6e66be2SVincenzo Maffione 									hdr->nr_name, req->nr_mem_id);
2813b6e66be2SVincenzo Maffione 						}
2814c3e9b4dbSLuiz Otavio O Souza 						error = EINVAL;
2815c3e9b4dbSLuiz Otavio O Souza 						break;
2816c3e9b4dbSLuiz Otavio O Souza 					}
2817c3e9b4dbSLuiz Otavio O Souza 				}
281868b8534bSLuigi Rizzo 				/* find the interface and a reference */
28192ff91c17SVincenzo Maffione 				error = netmap_get_na(hdr, &na, &ifp, nmd,
282037e3a6d3SLuigi Rizzo 						      1 /* create */); /* keep reference */
282168b8534bSLuigi Rizzo 				if (error)
2822ce3ee1e7SLuigi Rizzo 					break;
2823f9790aebSLuigi Rizzo 				if (NETMAP_OWNED_BY_KERN(na)) {
2824ce3ee1e7SLuigi Rizzo 					error = EBUSY;
2825ce3ee1e7SLuigi Rizzo 					break;
2826f196ce38SLuigi Rizzo 				}
282737e3a6d3SLuigi Rizzo 
28282ff91c17SVincenzo Maffione 				if (na->virt_hdr_len && !(req->nr_flags & NR_ACCEPT_VNET_HDR)) {
2829b6e66be2SVincenzo Maffione 					nm_prerr("virt_hdr_len=%d, but application does "
2830b6e66be2SVincenzo Maffione 						"not accept it", na->virt_hdr_len);
283137e3a6d3SLuigi Rizzo 					error = EIO;
283237e3a6d3SLuigi Rizzo 					break;
283337e3a6d3SLuigi Rizzo 				}
283437e3a6d3SLuigi Rizzo 
2835ee0005f1SVincenzo Maffione 				error = netmap_do_regif(priv, na, hdr);
2836847bf383SLuigi Rizzo 				if (error) {    /* reg. failed, release priv and ref */
2837ce3ee1e7SLuigi Rizzo 					break;
283868b8534bSLuigi Rizzo 				}
2839b6e66be2SVincenzo Maffione 
2840253b2ec1SVincenzo Maffione 				opt = nmreq_getoption(hdr, NETMAP_REQ_OPT_CSB);
2841b6e66be2SVincenzo Maffione 				if (opt != NULL) {
2842b6e66be2SVincenzo Maffione 					struct nmreq_opt_csb *csbo =
2843b6e66be2SVincenzo Maffione 						(struct nmreq_opt_csb *)opt;
2844b6e66be2SVincenzo Maffione 					error = netmap_csb_validate(priv, csbo);
2845b6e66be2SVincenzo Maffione 					opt->nro_status = error;
2846b6e66be2SVincenzo Maffione 					if (error) {
2847b6e66be2SVincenzo Maffione 						netmap_do_unregif(priv);
2848b6e66be2SVincenzo Maffione 						break;
2849b6e66be2SVincenzo Maffione 					}
2850b6e66be2SVincenzo Maffione 				}
2851b6e66be2SVincenzo Maffione 
2852847bf383SLuigi Rizzo 				nifp = priv->np_nifp;
285368b8534bSLuigi Rizzo 
285468b8534bSLuigi Rizzo 				/* return the offset of the netmap_if object */
28552ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
28562ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
28572ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
28582ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
2859d12354a5SVincenzo Maffione 				req->nr_host_tx_rings = na->num_host_tx_rings;
2860d12354a5SVincenzo Maffione 				req->nr_host_rx_rings = na->num_host_rx_rings;
28612ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(na->nm_mem, &req->nr_memsize, &memflags,
28622ff91c17SVincenzo Maffione 					&req->nr_mem_id);
2863ce3ee1e7SLuigi Rizzo 				if (error) {
2864847bf383SLuigi Rizzo 					netmap_do_unregif(priv);
2865ce3ee1e7SLuigi Rizzo 					break;
2866ce3ee1e7SLuigi Rizzo 				}
2867ce3ee1e7SLuigi Rizzo 				if (memflags & NETMAP_MEM_PRIVATE) {
28683d819cb6SLuigi Rizzo 					*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2869ce3ee1e7SLuigi Rizzo 				}
2870847bf383SLuigi Rizzo 				for_rx_tx(t) {
2871847bf383SLuigi Rizzo 					priv->np_si[t] = nm_si_user(priv, t) ?
28722ff91c17SVincenzo Maffione 						&na->si[t] : &NMR(na, t)[priv->np_qfirst[t]]->si;
2873847bf383SLuigi Rizzo 				}
2874f0ea3689SLuigi Rizzo 
28752ff91c17SVincenzo Maffione 				if (req->nr_extra_bufs) {
287637e3a6d3SLuigi Rizzo 					if (netmap_verbose)
2877b6e66be2SVincenzo Maffione 						nm_prinf("requested %d extra buffers",
28782ff91c17SVincenzo Maffione 							req->nr_extra_bufs);
28792ff91c17SVincenzo Maffione 					req->nr_extra_bufs = netmap_extra_alloc(na,
28802ff91c17SVincenzo Maffione 						&nifp->ni_bufs_head, req->nr_extra_bufs);
288137e3a6d3SLuigi Rizzo 					if (netmap_verbose)
2882b6e66be2SVincenzo Maffione 						nm_prinf("got %d extra buffers", req->nr_extra_bufs);
288398399ab0SVincenzo Maffione 				} else {
288498399ab0SVincenzo Maffione 					nifp->ni_bufs_head = 0;
2885f0ea3689SLuigi Rizzo 				}
28862ff91c17SVincenzo Maffione 				req->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
28872ff91c17SVincenzo Maffione 
28882ff91c17SVincenzo Maffione 				error = nmreq_checkoptions(hdr);
28892ff91c17SVincenzo Maffione 				if (error) {
28902ff91c17SVincenzo Maffione 					netmap_do_unregif(priv);
28912ff91c17SVincenzo Maffione 					break;
28922ff91c17SVincenzo Maffione 				}
289337e3a6d3SLuigi Rizzo 
289437e3a6d3SLuigi Rizzo 				/* store ifp reference so that priv destructor may release it */
289537e3a6d3SLuigi Rizzo 				priv->np_ifp = ifp;
2896ce3ee1e7SLuigi Rizzo 			} while (0);
2897c3e9b4dbSLuiz Otavio O Souza 			if (error) {
2898c3e9b4dbSLuiz Otavio O Souza 				netmap_unget_na(na, ifp);
2899c3e9b4dbSLuiz Otavio O Souza 			}
2900c3e9b4dbSLuiz Otavio O Souza 			/* release the reference from netmap_mem_find() or
2901c3e9b4dbSLuiz Otavio O Souza 			 * netmap_mem_ext_create()
2902c3e9b4dbSLuiz Otavio O Souza 			 */
2903c3e9b4dbSLuiz Otavio O Souza 			if (nmd)
2904c3e9b4dbSLuiz Otavio O Souza 				netmap_mem_put(nmd);
2905ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
290668b8534bSLuigi Rizzo 			break;
29072ff91c17SVincenzo Maffione 		}
29082ff91c17SVincenzo Maffione 
29092ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_INFO_GET: {
29102ff91c17SVincenzo Maffione 			struct nmreq_port_info_get *req =
2911cfa866f6SMatt Macy 				(struct nmreq_port_info_get *)(uintptr_t)hdr->nr_body;
29120ab5902eSVincenzo Maffione 			int nmd_ref = 0;
29132ff91c17SVincenzo Maffione 
29142ff91c17SVincenzo Maffione 			NMG_LOCK();
29152ff91c17SVincenzo Maffione 			do {
29162ff91c17SVincenzo Maffione 				u_int memflags;
29172ff91c17SVincenzo Maffione 
29182ff91c17SVincenzo Maffione 				if (hdr->nr_name[0] != '\0') {
29192ff91c17SVincenzo Maffione 					/* Build a nmreq_register out of the nmreq_port_info_get,
29202ff91c17SVincenzo Maffione 					 * so that we can call netmap_get_na(). */
29212ff91c17SVincenzo Maffione 					struct nmreq_register regreq;
29222ff91c17SVincenzo Maffione 					bzero(&regreq, sizeof(regreq));
2923b6e66be2SVincenzo Maffione 					regreq.nr_mode = NR_REG_ALL_NIC;
29242ff91c17SVincenzo Maffione 					regreq.nr_tx_slots = req->nr_tx_slots;
29252ff91c17SVincenzo Maffione 					regreq.nr_rx_slots = req->nr_rx_slots;
29262ff91c17SVincenzo Maffione 					regreq.nr_tx_rings = req->nr_tx_rings;
29272ff91c17SVincenzo Maffione 					regreq.nr_rx_rings = req->nr_rx_rings;
2928d12354a5SVincenzo Maffione 					regreq.nr_host_tx_rings = req->nr_host_tx_rings;
2929d12354a5SVincenzo Maffione 					regreq.nr_host_rx_rings = req->nr_host_rx_rings;
29302ff91c17SVincenzo Maffione 					regreq.nr_mem_id = req->nr_mem_id;
29312ff91c17SVincenzo Maffione 
29322ff91c17SVincenzo Maffione 					/* get a refcount */
29332ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2934cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)&regreq;
29352ff91c17SVincenzo Maffione 					error = netmap_get_na(hdr, &na, &ifp, NULL, 1 /* create */);
29362ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_PORT_INFO_GET; /* reset type */
2937cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)req; /* reset nr_body */
29382ff91c17SVincenzo Maffione 					if (error) {
29392ff91c17SVincenzo Maffione 						na = NULL;
29402ff91c17SVincenzo Maffione 						ifp = NULL;
29412ff91c17SVincenzo Maffione 						break;
29422ff91c17SVincenzo Maffione 					}
29432ff91c17SVincenzo Maffione 					nmd = na->nm_mem; /* get memory allocator */
29442ff91c17SVincenzo Maffione 				} else {
29452ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id ? req->nr_mem_id : 1);
29462ff91c17SVincenzo Maffione 					if (nmd == NULL) {
2947b6e66be2SVincenzo Maffione 						if (netmap_verbose)
2948b6e66be2SVincenzo Maffione 							nm_prerr("%s: failed to find mem_id %u",
2949b6e66be2SVincenzo Maffione 									hdr->nr_name,
2950b6e66be2SVincenzo Maffione 									req->nr_mem_id ? req->nr_mem_id : 1);
29512ff91c17SVincenzo Maffione 						error = EINVAL;
29522ff91c17SVincenzo Maffione 						break;
29532ff91c17SVincenzo Maffione 					}
29540ab5902eSVincenzo Maffione 					nmd_ref = 1;
29552ff91c17SVincenzo Maffione 				}
29562ff91c17SVincenzo Maffione 
29572ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(nmd, &req->nr_memsize, &memflags,
29582ff91c17SVincenzo Maffione 					&req->nr_mem_id);
29592ff91c17SVincenzo Maffione 				if (error)
29602ff91c17SVincenzo Maffione 					break;
29612ff91c17SVincenzo Maffione 				if (na == NULL) /* only memory info */
29622ff91c17SVincenzo Maffione 					break;
29632ff91c17SVincenzo Maffione 				netmap_update_config(na);
29642ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
29652ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
29662ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
29672ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
2968d12354a5SVincenzo Maffione 				req->nr_host_tx_rings = na->num_host_tx_rings;
2969d12354a5SVincenzo Maffione 				req->nr_host_rx_rings = na->num_host_rx_rings;
29702ff91c17SVincenzo Maffione 			} while (0);
29712ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
29720ab5902eSVincenzo Maffione 			if (nmd_ref)
29730ab5902eSVincenzo Maffione 				netmap_mem_put(nmd);
29742ff91c17SVincenzo Maffione 			NMG_UNLOCK();
29752ff91c17SVincenzo Maffione 			break;
29762ff91c17SVincenzo Maffione 		}
29772ff91c17SVincenzo Maffione #ifdef WITH_VALE
29782ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_ATTACH: {
2979a6d768d8SVincenzo Maffione 			error = netmap_bdg_attach(hdr, NULL /* userspace request */);
29802ff91c17SVincenzo Maffione 			break;
29812ff91c17SVincenzo Maffione 		}
29822ff91c17SVincenzo Maffione 
29832ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DETACH: {
2984a6d768d8SVincenzo Maffione 			error = netmap_bdg_detach(hdr, NULL /* userspace request */);
29852ff91c17SVincenzo Maffione 			break;
29862ff91c17SVincenzo Maffione 		}
29872ff91c17SVincenzo Maffione 
29882ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_SET: {
29892ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2990cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
29912ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
29922ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
29932ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
29942ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
2995b6e66be2SVincenzo Maffione 			regreq.nr_mode = NR_REG_ALL_NIC;
2996b6e66be2SVincenzo Maffione 
29972ff91c17SVincenzo Maffione 			/* For now we only support virtio-net headers, and only for
29982ff91c17SVincenzo Maffione 			 * VALE ports, but this may change in future. Valid lengths
29992ff91c17SVincenzo Maffione 			 * for the virtio-net header are 0 (no header), 10 and 12. */
30002ff91c17SVincenzo Maffione 			if (req->nr_hdr_len != 0 &&
30012ff91c17SVincenzo Maffione 				req->nr_hdr_len != sizeof(struct nm_vnet_hdr) &&
30022ff91c17SVincenzo Maffione 					req->nr_hdr_len != 12) {
3003b6e66be2SVincenzo Maffione 				if (netmap_verbose)
3004b6e66be2SVincenzo Maffione 					nm_prerr("invalid hdr_len %u", req->nr_hdr_len);
30052ff91c17SVincenzo Maffione 				error = EINVAL;
30062ff91c17SVincenzo Maffione 				break;
30072ff91c17SVincenzo Maffione 			}
30082ff91c17SVincenzo Maffione 			NMG_LOCK();
30092ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
3010cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
30112a7db7a6SVincenzo Maffione 			error = netmap_get_vale_na(hdr, &na, NULL, 0);
30122ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_SET;
3013cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
30142ff91c17SVincenzo Maffione 			if (na && !error) {
30152ff91c17SVincenzo Maffione 				struct netmap_vp_adapter *vpna =
30162ff91c17SVincenzo Maffione 					(struct netmap_vp_adapter *)na;
30172ff91c17SVincenzo Maffione 				na->virt_hdr_len = req->nr_hdr_len;
30182ff91c17SVincenzo Maffione 				if (na->virt_hdr_len) {
30192ff91c17SVincenzo Maffione 					vpna->mfs = NETMAP_BUF_SIZE(na);
30202ff91c17SVincenzo Maffione 				}
3021b6e66be2SVincenzo Maffione 				if (netmap_verbose)
3022b6e66be2SVincenzo Maffione 					nm_prinf("Using vnet_hdr_len %d for %p", na->virt_hdr_len, na);
30232ff91c17SVincenzo Maffione 				netmap_adapter_put(na);
30242ff91c17SVincenzo Maffione 			} else if (!na) {
30252ff91c17SVincenzo Maffione 				error = ENXIO;
30262ff91c17SVincenzo Maffione 			}
30272ff91c17SVincenzo Maffione 			NMG_UNLOCK();
30282ff91c17SVincenzo Maffione 			break;
30292ff91c17SVincenzo Maffione 		}
30302ff91c17SVincenzo Maffione 
30312ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_GET: {
30322ff91c17SVincenzo Maffione 			/* Get vnet-header length for this netmap port */
30332ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
3034cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
30352ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
30362ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
30372ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
30382ff91c17SVincenzo Maffione 			struct ifnet *ifp;
30392ff91c17SVincenzo Maffione 
30402ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
3041b6e66be2SVincenzo Maffione 			regreq.nr_mode = NR_REG_ALL_NIC;
30422ff91c17SVincenzo Maffione 			NMG_LOCK();
30432ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
3044cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
30452ff91c17SVincenzo Maffione 			error = netmap_get_na(hdr, &na, &ifp, NULL, 0);
30462ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_GET;
3047cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
30482ff91c17SVincenzo Maffione 			if (na && !error) {
30492ff91c17SVincenzo Maffione 				req->nr_hdr_len = na->virt_hdr_len;
30502ff91c17SVincenzo Maffione 			}
30512ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
30522ff91c17SVincenzo Maffione 			NMG_UNLOCK();
30532ff91c17SVincenzo Maffione 			break;
30542ff91c17SVincenzo Maffione 		}
30552ff91c17SVincenzo Maffione 
3056a6d768d8SVincenzo Maffione 		case NETMAP_REQ_VALE_LIST: {
3057a6d768d8SVincenzo Maffione 			error = netmap_vale_list(hdr);
3058a6d768d8SVincenzo Maffione 			break;
3059a6d768d8SVincenzo Maffione 		}
3060a6d768d8SVincenzo Maffione 
30612ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_NEWIF: {
30622ff91c17SVincenzo Maffione 			error = nm_vi_create(hdr);
30632ff91c17SVincenzo Maffione 			break;
30642ff91c17SVincenzo Maffione 		}
30652ff91c17SVincenzo Maffione 
30662ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DELIF: {
30672ff91c17SVincenzo Maffione 			error = nm_vi_destroy(hdr->nr_name);
30682ff91c17SVincenzo Maffione 			break;
30692ff91c17SVincenzo Maffione 		}
3070a6d768d8SVincenzo Maffione #endif  /* WITH_VALE */
30712ff91c17SVincenzo Maffione 
30722ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_ENABLE:
30732ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_DISABLE: {
30742ff91c17SVincenzo Maffione 			error = nm_bdg_polling(hdr);
30752ff91c17SVincenzo Maffione 			break;
30762ff91c17SVincenzo Maffione 		}
30772ff91c17SVincenzo Maffione 		case NETMAP_REQ_POOLS_INFO_GET: {
3078b6e66be2SVincenzo Maffione 			/* Get information from the memory allocator used for
3079b6e66be2SVincenzo Maffione 			 * hdr->nr_name. */
30802ff91c17SVincenzo Maffione 			struct nmreq_pools_info *req =
3081cfa866f6SMatt Macy 				(struct nmreq_pools_info *)(uintptr_t)hdr->nr_body;
30822ff91c17SVincenzo Maffione 			NMG_LOCK();
3083b6e66be2SVincenzo Maffione 			do {
3084b6e66be2SVincenzo Maffione 				/* Build a nmreq_register out of the nmreq_pools_info,
3085b6e66be2SVincenzo Maffione 				 * so that we can call netmap_get_na(). */
3086b6e66be2SVincenzo Maffione 				struct nmreq_register regreq;
3087b6e66be2SVincenzo Maffione 				bzero(&regreq, sizeof(regreq));
3088b6e66be2SVincenzo Maffione 				regreq.nr_mem_id = req->nr_mem_id;
3089b6e66be2SVincenzo Maffione 				regreq.nr_mode = NR_REG_ALL_NIC;
3090b6e66be2SVincenzo Maffione 
3091b6e66be2SVincenzo Maffione 				hdr->nr_reqtype = NETMAP_REQ_REGISTER;
3092b6e66be2SVincenzo Maffione 				hdr->nr_body = (uintptr_t)&regreq;
3093b6e66be2SVincenzo Maffione 				error = netmap_get_na(hdr, &na, &ifp, NULL, 1 /* create */);
3094b6e66be2SVincenzo Maffione 				hdr->nr_reqtype = NETMAP_REQ_POOLS_INFO_GET; /* reset type */
3095b6e66be2SVincenzo Maffione 				hdr->nr_body = (uintptr_t)req; /* reset nr_body */
3096b6e66be2SVincenzo Maffione 				if (error) {
3097b6e66be2SVincenzo Maffione 					na = NULL;
3098b6e66be2SVincenzo Maffione 					ifp = NULL;
3099b6e66be2SVincenzo Maffione 					break;
31002ff91c17SVincenzo Maffione 				}
3101b6e66be2SVincenzo Maffione 				nmd = na->nm_mem; /* grab the memory allocator */
3102b6e66be2SVincenzo Maffione 				if (nmd == NULL) {
3103b6e66be2SVincenzo Maffione 					error = EINVAL;
3104b6e66be2SVincenzo Maffione 					break;
3105b6e66be2SVincenzo Maffione 				}
3106b6e66be2SVincenzo Maffione 
3107b6e66be2SVincenzo Maffione 				/* Finalize the memory allocator, get the pools
3108b6e66be2SVincenzo Maffione 				 * information and release the allocator. */
3109b6e66be2SVincenzo Maffione 				error = netmap_mem_finalize(nmd, na);
3110b6e66be2SVincenzo Maffione 				if (error) {
3111b6e66be2SVincenzo Maffione 					break;
3112b6e66be2SVincenzo Maffione 				}
3113b6e66be2SVincenzo Maffione 				error = netmap_mem_pools_info_get(req, nmd);
3114b6e66be2SVincenzo Maffione 				netmap_mem_drop(na);
3115b6e66be2SVincenzo Maffione 			} while (0);
3116b6e66be2SVincenzo Maffione 			netmap_unget_na(na, ifp);
31172ff91c17SVincenzo Maffione 			NMG_UNLOCK();
31182ff91c17SVincenzo Maffione 			break;
31192ff91c17SVincenzo Maffione 		}
31202ff91c17SVincenzo Maffione 
3121b6e66be2SVincenzo Maffione 		case NETMAP_REQ_CSB_ENABLE: {
3122b6e66be2SVincenzo Maffione 			struct nmreq_option *opt;
3123b6e66be2SVincenzo Maffione 
3124253b2ec1SVincenzo Maffione 			opt = nmreq_getoption(hdr, NETMAP_REQ_OPT_CSB);
3125b6e66be2SVincenzo Maffione 			if (opt == NULL) {
3126b6e66be2SVincenzo Maffione 				error = EINVAL;
3127b6e66be2SVincenzo Maffione 			} else {
3128b6e66be2SVincenzo Maffione 				struct nmreq_opt_csb *csbo =
3129b6e66be2SVincenzo Maffione 					(struct nmreq_opt_csb *)opt;
3130b6e66be2SVincenzo Maffione 				NMG_LOCK();
3131b6e66be2SVincenzo Maffione 				error = netmap_csb_validate(priv, csbo);
3132b6e66be2SVincenzo Maffione 				NMG_UNLOCK();
3133b6e66be2SVincenzo Maffione 				opt->nro_status = error;
3134b6e66be2SVincenzo Maffione 			}
3135b6e66be2SVincenzo Maffione 			break;
3136b6e66be2SVincenzo Maffione 		}
3137b6e66be2SVincenzo Maffione 
3138b6e66be2SVincenzo Maffione 		case NETMAP_REQ_SYNC_KLOOP_START: {
3139b6e66be2SVincenzo Maffione 			error = netmap_sync_kloop(priv, hdr);
3140b6e66be2SVincenzo Maffione 			break;
3141b6e66be2SVincenzo Maffione 		}
3142b6e66be2SVincenzo Maffione 
3143b6e66be2SVincenzo Maffione 		case NETMAP_REQ_SYNC_KLOOP_STOP: {
3144b6e66be2SVincenzo Maffione 			error = netmap_sync_kloop_stop(priv);
3145b6e66be2SVincenzo Maffione 			break;
3146b6e66be2SVincenzo Maffione 		}
3147b6e66be2SVincenzo Maffione 
31482ff91c17SVincenzo Maffione 		default: {
31492ff91c17SVincenzo Maffione 			error = EINVAL;
31502ff91c17SVincenzo Maffione 			break;
31512ff91c17SVincenzo Maffione 		}
31522ff91c17SVincenzo Maffione 		}
31532ff91c17SVincenzo Maffione 		/* Write back request body to userspace and reset the
31542ff91c17SVincenzo Maffione 		 * user-space pointer. */
31552ff91c17SVincenzo Maffione 		error = nmreq_copyout(hdr, error);
31562ff91c17SVincenzo Maffione 		break;
31572ff91c17SVincenzo Maffione 	}
315868b8534bSLuigi Rizzo 
315968b8534bSLuigi Rizzo 	case NIOCTXSYNC:
31602ff91c17SVincenzo Maffione 	case NIOCRXSYNC: {
3161b6e66be2SVincenzo Maffione 		if (unlikely(priv->np_nifp == NULL)) {
3162506cc70cSLuigi Rizzo 			error = ENXIO;
3163506cc70cSLuigi Rizzo 			break;
3164506cc70cSLuigi Rizzo 		}
31656641c68bSLuigi Rizzo 		mb(); /* make sure following reads are not from cache */
31668241616dSLuigi Rizzo 
3167b6e66be2SVincenzo Maffione 		if (unlikely(priv->np_csb_atok_base)) {
3168b6e66be2SVincenzo Maffione 			nm_prerr("Invalid sync in CSB mode");
3169b6e66be2SVincenzo Maffione 			error = EBUSY;
31708241616dSLuigi Rizzo 			break;
31718241616dSLuigi Rizzo 		}
31728241616dSLuigi Rizzo 
3173b6e66be2SVincenzo Maffione 		na = priv->np_na;      /* we have a reference */
3174b6e66be2SVincenzo Maffione 
3175c3e9b4dbSLuiz Otavio O Souza 		mbq_init(&q);
3176847bf383SLuigi Rizzo 		t = (cmd == NIOCTXSYNC ? NR_TX : NR_RX);
3177847bf383SLuigi Rizzo 		krings = NMR(na, t);
3178847bf383SLuigi Rizzo 		qfirst = priv->np_qfirst[t];
3179847bf383SLuigi Rizzo 		qlast = priv->np_qlast[t];
3180c3e9b4dbSLuiz Otavio O Souza 		sync_flags = priv->np_sync_flags;
318168b8534bSLuigi Rizzo 
3182f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
31832ff91c17SVincenzo Maffione 			struct netmap_kring *kring = krings[i];
318437e3a6d3SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
318537e3a6d3SLuigi Rizzo 
318637e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &error))) {
318737e3a6d3SLuigi Rizzo 				error = (error ? EIO : 0);
318837e3a6d3SLuigi Rizzo 				continue;
3189ce3ee1e7SLuigi Rizzo 			}
319037e3a6d3SLuigi Rizzo 
319168b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
3192b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_TXSYNC)
3193b6e66be2SVincenzo Maffione 					nm_prinf("pre txsync ring %d cur %d hwcur %d",
319437e3a6d3SLuigi Rizzo 					    i, ring->cur,
319568b8534bSLuigi Rizzo 					    kring->nr_hwcur);
319637e3a6d3SLuigi Rizzo 				if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
319717885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
3198c3e9b4dbSLuiz Otavio O Souza 				} else if (kring->nm_sync(kring, sync_flags | NAF_FORCE_RECLAIM) == 0) {
319937e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
320017885a7bSLuigi Rizzo 				}
3201b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_TXSYNC)
3202b6e66be2SVincenzo Maffione 					nm_prinf("post txsync ring %d cur %d hwcur %d",
320337e3a6d3SLuigi Rizzo 					    i, ring->cur,
320468b8534bSLuigi Rizzo 					    kring->nr_hwcur);
320568b8534bSLuigi Rizzo 			} else {
320637e3a6d3SLuigi Rizzo 				if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
3207847bf383SLuigi Rizzo 					netmap_ring_reinit(kring);
3208c3e9b4dbSLuiz Otavio O Souza 				}
3209c3e9b4dbSLuiz Otavio O Souza 				if (nm_may_forward_up(kring)) {
3210c3e9b4dbSLuiz Otavio O Souza 					/* transparent forwarding, see netmap_poll() */
3211c3e9b4dbSLuiz Otavio O Souza 					netmap_grab_packets(kring, &q, netmap_fwd);
3212c3e9b4dbSLuiz Otavio O Souza 				}
3213c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags | NAF_FORCE_READ) == 0) {
321437e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
3215847bf383SLuigi Rizzo 				}
3216c3e9b4dbSLuiz Otavio O Souza 				ring_timestamp_set(ring);
321768b8534bSLuigi Rizzo 			}
3218ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
321968b8534bSLuigi Rizzo 		}
322068b8534bSLuigi Rizzo 
3221c3e9b4dbSLuiz Otavio O Souza 		if (mbq_peek(&q)) {
3222c3e9b4dbSLuiz Otavio O Souza 			netmap_send_up(na->ifp, &q);
3223c3e9b4dbSLuiz Otavio O Souza 		}
3224c3e9b4dbSLuiz Otavio O Souza 
322568b8534bSLuigi Rizzo 		break;
322668b8534bSLuigi Rizzo 	}
3227f196ce38SLuigi Rizzo 
32282ff91c17SVincenzo Maffione 	default: {
32292ff91c17SVincenzo Maffione 		return netmap_ioctl_legacy(priv, cmd, data, td);
32302ff91c17SVincenzo Maffione 		break;
32312ff91c17SVincenzo Maffione 	}
323268b8534bSLuigi Rizzo 	}
323368b8534bSLuigi Rizzo 
323468b8534bSLuigi Rizzo 	return (error);
323568b8534bSLuigi Rizzo }
323668b8534bSLuigi Rizzo 
32372ff91c17SVincenzo Maffione size_t
32382ff91c17SVincenzo Maffione nmreq_size_by_type(uint16_t nr_reqtype)
32392ff91c17SVincenzo Maffione {
32402ff91c17SVincenzo Maffione 	switch (nr_reqtype) {
32412ff91c17SVincenzo Maffione 	case NETMAP_REQ_REGISTER:
32422ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_register);
32432ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_INFO_GET:
32442ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_info_get);
32452ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_ATTACH:
32462ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_attach);
32472ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DETACH:
32482ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_detach);
32492ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_LIST:
32502ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_list);
32512ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_SET:
32522ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_GET:
32532ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_hdr);
32542ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_NEWIF:
32552ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_newif);
32562ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DELIF:
3257b6e66be2SVincenzo Maffione 	case NETMAP_REQ_SYNC_KLOOP_STOP:
3258b6e66be2SVincenzo Maffione 	case NETMAP_REQ_CSB_ENABLE:
32592ff91c17SVincenzo Maffione 		return 0;
32602ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_ENABLE:
32612ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_DISABLE:
32622ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_polling);
32632ff91c17SVincenzo Maffione 	case NETMAP_REQ_POOLS_INFO_GET:
32642ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_pools_info);
3265b6e66be2SVincenzo Maffione 	case NETMAP_REQ_SYNC_KLOOP_START:
3266b6e66be2SVincenzo Maffione 		return sizeof(struct nmreq_sync_kloop_start);
32672ff91c17SVincenzo Maffione 	}
32682ff91c17SVincenzo Maffione 	return 0;
32692ff91c17SVincenzo Maffione }
32702ff91c17SVincenzo Maffione 
32712ff91c17SVincenzo Maffione static size_t
3272b6e66be2SVincenzo Maffione nmreq_opt_size_by_type(uint32_t nro_reqtype, uint64_t nro_size)
32732ff91c17SVincenzo Maffione {
32742ff91c17SVincenzo Maffione 	size_t rv = sizeof(struct nmreq_option);
32752ff91c17SVincenzo Maffione #ifdef NETMAP_REQ_OPT_DEBUG
32762ff91c17SVincenzo Maffione 	if (nro_reqtype & NETMAP_REQ_OPT_DEBUG)
32772ff91c17SVincenzo Maffione 		return (nro_reqtype & ~NETMAP_REQ_OPT_DEBUG);
32782ff91c17SVincenzo Maffione #endif /* NETMAP_REQ_OPT_DEBUG */
32792ff91c17SVincenzo Maffione 	switch (nro_reqtype) {
32802ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
32812ff91c17SVincenzo Maffione 	case NETMAP_REQ_OPT_EXTMEM:
32822ff91c17SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_extmem);
32832ff91c17SVincenzo Maffione 		break;
32842ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
3285b6e66be2SVincenzo Maffione 	case NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS:
3286b6e66be2SVincenzo Maffione 		if (nro_size >= rv)
3287b6e66be2SVincenzo Maffione 			rv = nro_size;
3288b6e66be2SVincenzo Maffione 		break;
3289b6e66be2SVincenzo Maffione 	case NETMAP_REQ_OPT_CSB:
3290b6e66be2SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_csb);
3291b6e66be2SVincenzo Maffione 		break;
32925faab778SVincenzo Maffione 	case NETMAP_REQ_OPT_SYNC_KLOOP_MODE:
32935faab778SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_sync_kloop_mode);
32945faab778SVincenzo Maffione 		break;
3295a6d768d8SVincenzo Maffione 	case NETMAP_REQ_OPT_OFFSETS:
3296a6d768d8SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_offsets);
3297a6d768d8SVincenzo Maffione 		break;
32982ff91c17SVincenzo Maffione 	}
32992ff91c17SVincenzo Maffione 	/* subtract the common header */
33002ff91c17SVincenzo Maffione 	return rv - sizeof(struct nmreq_option);
33012ff91c17SVincenzo Maffione }
33022ff91c17SVincenzo Maffione 
3303253b2ec1SVincenzo Maffione /*
3304253b2ec1SVincenzo Maffione  * nmreq_copyin: create an in-kernel version of the request.
3305253b2ec1SVincenzo Maffione  *
3306253b2ec1SVincenzo Maffione  * We build the following data structure:
3307253b2ec1SVincenzo Maffione  *
3308253b2ec1SVincenzo Maffione  * hdr -> +-------+                buf
3309253b2ec1SVincenzo Maffione  *        |       |          +---------------+
3310253b2ec1SVincenzo Maffione  *        +-------+          |usr body ptr   |
3311253b2ec1SVincenzo Maffione  *        |options|-.        +---------------+
3312253b2ec1SVincenzo Maffione  *        +-------+ |        |usr options ptr|
3313253b2ec1SVincenzo Maffione  *        |body   |--------->+---------------+
3314253b2ec1SVincenzo Maffione  *        +-------+ |        |               |
3315253b2ec1SVincenzo Maffione  *                  |        |  copy of body |
3316253b2ec1SVincenzo Maffione  *                  |        |               |
3317253b2ec1SVincenzo Maffione  *                  |        +---------------+
3318253b2ec1SVincenzo Maffione  *                  |        |    NULL       |
3319253b2ec1SVincenzo Maffione  *                  |        +---------------+
3320253b2ec1SVincenzo Maffione  *                  |    .---|               |\
3321253b2ec1SVincenzo Maffione  *                  |    |   +---------------+ |
3322253b2ec1SVincenzo Maffione  *                  | .------|               | |
3323253b2ec1SVincenzo Maffione  *                  | |  |   +---------------+  \ option table
3324253b2ec1SVincenzo Maffione  *                  | |  |   |      ...      |  / indexed by option
3325253b2ec1SVincenzo Maffione  *                  | |  |   +---------------+ |  type
3326253b2ec1SVincenzo Maffione  *                  | |  |   |               | |
3327253b2ec1SVincenzo Maffione  *                  | |  |   +---------------+/
3328253b2ec1SVincenzo Maffione  *                  | |  |   |usr next ptr 1 |
3329253b2ec1SVincenzo Maffione  *                  `-|----->+---------------+
3330253b2ec1SVincenzo Maffione  *                    |  |   | copy of opt 1 |
3331253b2ec1SVincenzo Maffione  *                    |  |   |               |
3332253b2ec1SVincenzo Maffione  *                    |  | .-| nro_next      |
3333253b2ec1SVincenzo Maffione  *                    |  | | +---------------+
3334253b2ec1SVincenzo Maffione  *                    |  | | |usr next ptr 2 |
3335253b2ec1SVincenzo Maffione  *                    |  `-`>+---------------+
3336253b2ec1SVincenzo Maffione  *                    |      | copy of opt 2 |
3337253b2ec1SVincenzo Maffione  *                    |      |               |
3338253b2ec1SVincenzo Maffione  *                    |    .-| nro_next      |
3339253b2ec1SVincenzo Maffione  *                    |    | +---------------+
3340253b2ec1SVincenzo Maffione  *                    |    | |               |
3341253b2ec1SVincenzo Maffione  *                    ~    ~ ~      ...      ~
3342253b2ec1SVincenzo Maffione  *                    |    .-|               |
3343253b2ec1SVincenzo Maffione  *                    `----->+---------------+
3344253b2ec1SVincenzo Maffione  *                         | |usr next ptr n |
3345253b2ec1SVincenzo Maffione  *                         `>+---------------+
3346253b2ec1SVincenzo Maffione  *                           | copy of opt n |
3347253b2ec1SVincenzo Maffione  *                           |               |
3348253b2ec1SVincenzo Maffione  *                           | nro_next(NULL)|
3349253b2ec1SVincenzo Maffione  *                           +---------------+
3350253b2ec1SVincenzo Maffione  *
3351253b2ec1SVincenzo Maffione  * The options and body fields of the hdr structure are overwritten
3352253b2ec1SVincenzo Maffione  * with in-kernel valid pointers inside the buf. The original user
3353253b2ec1SVincenzo Maffione  * pointers are saved in the buf and restored on copyout.
3354253b2ec1SVincenzo Maffione  * The list of options is copied and the pointers adjusted. The
3355253b2ec1SVincenzo Maffione  * original pointers are saved before the option they belonged.
3356253b2ec1SVincenzo Maffione  *
335745c67e8fSVincenzo Maffione  * The option table has an entry for every available option.  Entries
3358253b2ec1SVincenzo Maffione  * for options that have not been passed contain NULL.
3359253b2ec1SVincenzo Maffione  *
3360253b2ec1SVincenzo Maffione  */
3361253b2ec1SVincenzo Maffione 
33622ff91c17SVincenzo Maffione int
33632ff91c17SVincenzo Maffione nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user)
33642ff91c17SVincenzo Maffione {
33652ff91c17SVincenzo Maffione 	size_t rqsz, optsz, bufsz;
3366253b2ec1SVincenzo Maffione 	int error = 0;
33672ff91c17SVincenzo Maffione 	char *ker = NULL, *p;
3368253b2ec1SVincenzo Maffione 	struct nmreq_option **next, *src, **opt_tab;
33692ff91c17SVincenzo Maffione 	struct nmreq_option buf;
33702ff91c17SVincenzo Maffione 	uint64_t *ptrs;
33712ff91c17SVincenzo Maffione 
3372b6e66be2SVincenzo Maffione 	if (hdr->nr_reserved) {
3373b6e66be2SVincenzo Maffione 		if (netmap_verbose)
3374b6e66be2SVincenzo Maffione 			nm_prerr("nr_reserved must be zero");
33752ff91c17SVincenzo Maffione 		return EINVAL;
3376b6e66be2SVincenzo Maffione 	}
33772ff91c17SVincenzo Maffione 
33782ff91c17SVincenzo Maffione 	if (!nr_body_is_user)
33792ff91c17SVincenzo Maffione 		return 0;
33802ff91c17SVincenzo Maffione 
33812ff91c17SVincenzo Maffione 	hdr->nr_reserved = nr_body_is_user;
33822ff91c17SVincenzo Maffione 
33832ff91c17SVincenzo Maffione 	/* compute the total size of the buffer */
33842ff91c17SVincenzo Maffione 	rqsz = nmreq_size_by_type(hdr->nr_reqtype);
33852ff91c17SVincenzo Maffione 	if (rqsz > NETMAP_REQ_MAXSIZE) {
33862ff91c17SVincenzo Maffione 		error = EMSGSIZE;
33872ff91c17SVincenzo Maffione 		goto out_err;
33882ff91c17SVincenzo Maffione 	}
3389cfa866f6SMatt Macy 	if ((rqsz && hdr->nr_body == (uintptr_t)NULL) ||
3390cfa866f6SMatt Macy 		(!rqsz && hdr->nr_body != (uintptr_t)NULL)) {
33912ff91c17SVincenzo Maffione 		/* Request body expected, but not found; or
33922ff91c17SVincenzo Maffione 		 * request body found but unexpected. */
3393b6e66be2SVincenzo Maffione 		if (netmap_verbose)
3394b6e66be2SVincenzo Maffione 			nm_prerr("nr_body expected but not found, or vice versa");
33952ff91c17SVincenzo Maffione 		error = EINVAL;
33962ff91c17SVincenzo Maffione 		goto out_err;
33972ff91c17SVincenzo Maffione 	}
33982ff91c17SVincenzo Maffione 
3399253b2ec1SVincenzo Maffione 	bufsz = 2 * sizeof(void *) + rqsz +
3400253b2ec1SVincenzo Maffione 		NETMAP_REQ_OPT_MAX * sizeof(opt_tab);
3401253b2ec1SVincenzo Maffione 	/* compute the size of the buf below the option table.
3402253b2ec1SVincenzo Maffione 	 * It must contain a copy of every received option structure.
3403253b2ec1SVincenzo Maffione 	 * For every option we also need to store a copy of the user
3404253b2ec1SVincenzo Maffione 	 * list pointer.
3405253b2ec1SVincenzo Maffione 	 */
34062ff91c17SVincenzo Maffione 	optsz = 0;
3407cfa866f6SMatt Macy 	for (src = (struct nmreq_option *)(uintptr_t)hdr->nr_options; src;
3408cfa866f6SMatt Macy 	     src = (struct nmreq_option *)(uintptr_t)buf.nro_next)
34092ff91c17SVincenzo Maffione 	{
34102ff91c17SVincenzo Maffione 		error = copyin(src, &buf, sizeof(*src));
34112ff91c17SVincenzo Maffione 		if (error)
34122ff91c17SVincenzo Maffione 			goto out_err;
34132ff91c17SVincenzo Maffione 		optsz += sizeof(*src);
3414b6e66be2SVincenzo Maffione 		optsz += nmreq_opt_size_by_type(buf.nro_reqtype, buf.nro_size);
34152ff91c17SVincenzo Maffione 		if (rqsz + optsz > NETMAP_REQ_MAXSIZE) {
34162ff91c17SVincenzo Maffione 			error = EMSGSIZE;
34172ff91c17SVincenzo Maffione 			goto out_err;
34182ff91c17SVincenzo Maffione 		}
3419253b2ec1SVincenzo Maffione 		bufsz += sizeof(void *);
34202ff91c17SVincenzo Maffione 	}
3421253b2ec1SVincenzo Maffione 	bufsz += optsz;
34222ff91c17SVincenzo Maffione 
34232ff91c17SVincenzo Maffione 	ker = nm_os_malloc(bufsz);
34242ff91c17SVincenzo Maffione 	if (ker == NULL) {
34252ff91c17SVincenzo Maffione 		error = ENOMEM;
34262ff91c17SVincenzo Maffione 		goto out_err;
34272ff91c17SVincenzo Maffione 	}
3428253b2ec1SVincenzo Maffione 	p = ker;	/* write pointer into the buffer */
34292ff91c17SVincenzo Maffione 
34302ff91c17SVincenzo Maffione 	/* make a copy of the user pointers */
34312ff91c17SVincenzo Maffione 	ptrs = (uint64_t*)p;
34322ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_body;
34332ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_options;
34342ff91c17SVincenzo Maffione 	p = (char *)ptrs;
34352ff91c17SVincenzo Maffione 
34362ff91c17SVincenzo Maffione 	/* copy the body */
3437cfa866f6SMatt Macy 	error = copyin((void *)(uintptr_t)hdr->nr_body, p, rqsz);
34382ff91c17SVincenzo Maffione 	if (error)
34392ff91c17SVincenzo Maffione 		goto out_restore;
34402ff91c17SVincenzo Maffione 	/* overwrite the user pointer with the in-kernel one */
3441cfa866f6SMatt Macy 	hdr->nr_body = (uintptr_t)p;
34422ff91c17SVincenzo Maffione 	p += rqsz;
3443253b2ec1SVincenzo Maffione 	/* start of the options table */
3444253b2ec1SVincenzo Maffione 	opt_tab = (struct nmreq_option **)p;
3445253b2ec1SVincenzo Maffione 	p += sizeof(opt_tab) * NETMAP_REQ_OPT_MAX;
34462ff91c17SVincenzo Maffione 
34472ff91c17SVincenzo Maffione 	/* copy the options */
34482ff91c17SVincenzo Maffione 	next = (struct nmreq_option **)&hdr->nr_options;
34492ff91c17SVincenzo Maffione 	src = *next;
34502ff91c17SVincenzo Maffione 	while (src) {
34512ff91c17SVincenzo Maffione 		struct nmreq_option *opt;
34522ff91c17SVincenzo Maffione 
34532ff91c17SVincenzo Maffione 		/* copy the option header */
34542ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)p;
34552ff91c17SVincenzo Maffione 		opt = (struct nmreq_option *)(ptrs + 1);
34562ff91c17SVincenzo Maffione 		error = copyin(src, opt, sizeof(*src));
34572ff91c17SVincenzo Maffione 		if (error)
34582ff91c17SVincenzo Maffione 			goto out_restore;
34592ff91c17SVincenzo Maffione 		/* make a copy of the user next pointer */
34602ff91c17SVincenzo Maffione 		*ptrs = opt->nro_next;
34612ff91c17SVincenzo Maffione 		/* overwrite the user pointer with the in-kernel one */
34622ff91c17SVincenzo Maffione 		*next = opt;
34632ff91c17SVincenzo Maffione 
34642ff91c17SVincenzo Maffione 		/* initialize the option as not supported.
34652ff91c17SVincenzo Maffione 		 * Recognized options will update this field.
34662ff91c17SVincenzo Maffione 		 */
34672ff91c17SVincenzo Maffione 		opt->nro_status = EOPNOTSUPP;
34682ff91c17SVincenzo Maffione 
3469253b2ec1SVincenzo Maffione 		/* check for invalid types */
3470253b2ec1SVincenzo Maffione 		if (opt->nro_reqtype < 1) {
3471253b2ec1SVincenzo Maffione 			if (netmap_verbose)
3472253b2ec1SVincenzo Maffione 				nm_prinf("invalid option type: %u", opt->nro_reqtype);
3473253b2ec1SVincenzo Maffione 			opt->nro_status = EINVAL;
3474253b2ec1SVincenzo Maffione 			error = EINVAL;
3475253b2ec1SVincenzo Maffione 			goto next;
3476253b2ec1SVincenzo Maffione 		}
3477253b2ec1SVincenzo Maffione 
3478253b2ec1SVincenzo Maffione 		if (opt->nro_reqtype >= NETMAP_REQ_OPT_MAX) {
3479253b2ec1SVincenzo Maffione 			/* opt->nro_status is already EOPNOTSUPP */
3480253b2ec1SVincenzo Maffione 			error = EOPNOTSUPP;
3481253b2ec1SVincenzo Maffione 			goto next;
3482253b2ec1SVincenzo Maffione 		}
3483253b2ec1SVincenzo Maffione 
3484253b2ec1SVincenzo Maffione 		/* if the type is valid, index the option in the table
3485253b2ec1SVincenzo Maffione 		 * unless it is a duplicate.
3486253b2ec1SVincenzo Maffione 		 */
3487253b2ec1SVincenzo Maffione 		if (opt_tab[opt->nro_reqtype] != NULL) {
3488253b2ec1SVincenzo Maffione 			if (netmap_verbose)
3489253b2ec1SVincenzo Maffione 				nm_prinf("duplicate option: %u", opt->nro_reqtype);
3490253b2ec1SVincenzo Maffione 			opt->nro_status = EINVAL;
3491253b2ec1SVincenzo Maffione 			opt_tab[opt->nro_reqtype]->nro_status = EINVAL;
3492253b2ec1SVincenzo Maffione 			error = EINVAL;
3493253b2ec1SVincenzo Maffione 			goto next;
3494253b2ec1SVincenzo Maffione 		}
3495253b2ec1SVincenzo Maffione 		opt_tab[opt->nro_reqtype] = opt;
3496253b2ec1SVincenzo Maffione 
34972ff91c17SVincenzo Maffione 		p = (char *)(opt + 1);
34982ff91c17SVincenzo Maffione 
34992ff91c17SVincenzo Maffione 		/* copy the option body */
3500b6e66be2SVincenzo Maffione 		optsz = nmreq_opt_size_by_type(opt->nro_reqtype,
3501b6e66be2SVincenzo Maffione 						opt->nro_size);
35022ff91c17SVincenzo Maffione 		if (optsz) {
35032ff91c17SVincenzo Maffione 			/* the option body follows the option header */
35042ff91c17SVincenzo Maffione 			error = copyin(src + 1, p, optsz);
35052ff91c17SVincenzo Maffione 			if (error)
35062ff91c17SVincenzo Maffione 				goto out_restore;
35072ff91c17SVincenzo Maffione 			p += optsz;
35082ff91c17SVincenzo Maffione 		}
35092ff91c17SVincenzo Maffione 
3510253b2ec1SVincenzo Maffione 	next:
35112ff91c17SVincenzo Maffione 		/* move to next option */
35122ff91c17SVincenzo Maffione 		next = (struct nmreq_option **)&opt->nro_next;
35132ff91c17SVincenzo Maffione 		src = *next;
35142ff91c17SVincenzo Maffione 	}
3515253b2ec1SVincenzo Maffione 	if (error)
3516253b2ec1SVincenzo Maffione 		nmreq_copyout(hdr, error);
3517253b2ec1SVincenzo Maffione 	return error;
35182ff91c17SVincenzo Maffione 
35192ff91c17SVincenzo Maffione out_restore:
35202ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker;
35212ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
35222ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs++;
35232ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
35242ff91c17SVincenzo Maffione 	nm_os_free(ker);
35252ff91c17SVincenzo Maffione out_err:
35262ff91c17SVincenzo Maffione 	return error;
35272ff91c17SVincenzo Maffione }
35282ff91c17SVincenzo Maffione 
35292ff91c17SVincenzo Maffione static int
35302ff91c17SVincenzo Maffione nmreq_copyout(struct nmreq_header *hdr, int rerror)
35312ff91c17SVincenzo Maffione {
35322ff91c17SVincenzo Maffione 	struct nmreq_option *src, *dst;
3533cfa866f6SMatt Macy 	void *ker = (void *)(uintptr_t)hdr->nr_body, *bufstart;
35342ff91c17SVincenzo Maffione 	uint64_t *ptrs;
35352ff91c17SVincenzo Maffione 	size_t bodysz;
35362ff91c17SVincenzo Maffione 	int error;
35372ff91c17SVincenzo Maffione 
35382ff91c17SVincenzo Maffione 	if (!hdr->nr_reserved)
35392ff91c17SVincenzo Maffione 		return rerror;
35402ff91c17SVincenzo Maffione 
35412ff91c17SVincenzo Maffione 	/* restore the user pointers in the header */
35422ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker - 2;
35432ff91c17SVincenzo Maffione 	bufstart = ptrs;
35442ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
3545cfa866f6SMatt Macy 	src = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
35462ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs;
35472ff91c17SVincenzo Maffione 
35482ff91c17SVincenzo Maffione 	if (!rerror) {
35492ff91c17SVincenzo Maffione 		/* copy the body */
35502ff91c17SVincenzo Maffione 		bodysz = nmreq_size_by_type(hdr->nr_reqtype);
3551cfa866f6SMatt Macy 		error = copyout(ker, (void *)(uintptr_t)hdr->nr_body, bodysz);
35522ff91c17SVincenzo Maffione 		if (error) {
35532ff91c17SVincenzo Maffione 			rerror = error;
35542ff91c17SVincenzo Maffione 			goto out;
35552ff91c17SVincenzo Maffione 		}
35562ff91c17SVincenzo Maffione 	}
35572ff91c17SVincenzo Maffione 
35582ff91c17SVincenzo Maffione 	/* copy the options */
3559cfa866f6SMatt Macy 	dst = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
35602ff91c17SVincenzo Maffione 	while (src) {
35612ff91c17SVincenzo Maffione 		size_t optsz;
35622ff91c17SVincenzo Maffione 		uint64_t next;
35632ff91c17SVincenzo Maffione 
35642ff91c17SVincenzo Maffione 		/* restore the user pointer */
35652ff91c17SVincenzo Maffione 		next = src->nro_next;
35662ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)src - 1;
35672ff91c17SVincenzo Maffione 		src->nro_next = *ptrs;
35682ff91c17SVincenzo Maffione 
35692ff91c17SVincenzo Maffione 		/* always copy the option header */
35702ff91c17SVincenzo Maffione 		error = copyout(src, dst, sizeof(*src));
35712ff91c17SVincenzo Maffione 		if (error) {
35722ff91c17SVincenzo Maffione 			rerror = error;
35732ff91c17SVincenzo Maffione 			goto out;
35742ff91c17SVincenzo Maffione 		}
35752ff91c17SVincenzo Maffione 
35762ff91c17SVincenzo Maffione 		/* copy the option body only if there was no error */
35772ff91c17SVincenzo Maffione 		if (!rerror && !src->nro_status) {
3578b6e66be2SVincenzo Maffione 			optsz = nmreq_opt_size_by_type(src->nro_reqtype,
3579b6e66be2SVincenzo Maffione 							src->nro_size);
35802ff91c17SVincenzo Maffione 			if (optsz) {
35812ff91c17SVincenzo Maffione 				error = copyout(src + 1, dst + 1, optsz);
35822ff91c17SVincenzo Maffione 				if (error) {
35832ff91c17SVincenzo Maffione 					rerror = error;
35842ff91c17SVincenzo Maffione 					goto out;
35852ff91c17SVincenzo Maffione 				}
35862ff91c17SVincenzo Maffione 			}
35872ff91c17SVincenzo Maffione 		}
3588cfa866f6SMatt Macy 		src = (struct nmreq_option *)(uintptr_t)next;
3589cfa866f6SMatt Macy 		dst = (struct nmreq_option *)(uintptr_t)*ptrs;
35902ff91c17SVincenzo Maffione 	}
35912ff91c17SVincenzo Maffione 
35922ff91c17SVincenzo Maffione 
35932ff91c17SVincenzo Maffione out:
35942ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
35952ff91c17SVincenzo Maffione 	nm_os_free(bufstart);
35962ff91c17SVincenzo Maffione 	return rerror;
35972ff91c17SVincenzo Maffione }
35982ff91c17SVincenzo Maffione 
35992ff91c17SVincenzo Maffione struct nmreq_option *
3600253b2ec1SVincenzo Maffione nmreq_getoption(struct nmreq_header *hdr, uint16_t reqtype)
36012ff91c17SVincenzo Maffione {
3602253b2ec1SVincenzo Maffione 	struct nmreq_option **opt_tab;
3603253b2ec1SVincenzo Maffione 
3604253b2ec1SVincenzo Maffione 	if (!hdr->nr_options)
36052ff91c17SVincenzo Maffione 		return NULL;
36062ff91c17SVincenzo Maffione 
3607760fa2abSVincenzo Maffione 	opt_tab = (struct nmreq_option **)((uintptr_t)hdr->nr_options) -
3608760fa2abSVincenzo Maffione 	    (NETMAP_REQ_OPT_MAX + 1);
3609253b2ec1SVincenzo Maffione 	return opt_tab[reqtype];
36102ff91c17SVincenzo Maffione }
36112ff91c17SVincenzo Maffione 
36122ff91c17SVincenzo Maffione static int
36132ff91c17SVincenzo Maffione nmreq_checkoptions(struct nmreq_header *hdr)
36142ff91c17SVincenzo Maffione {
36152ff91c17SVincenzo Maffione 	struct nmreq_option *opt;
36162ff91c17SVincenzo Maffione 	/* return error if there is still any option
36172ff91c17SVincenzo Maffione 	 * marked as not supported
36182ff91c17SVincenzo Maffione 	 */
36192ff91c17SVincenzo Maffione 
3620cfa866f6SMatt Macy 	for (opt = (struct nmreq_option *)(uintptr_t)hdr->nr_options; opt;
3621cfa866f6SMatt Macy 	     opt = (struct nmreq_option *)(uintptr_t)opt->nro_next)
36222ff91c17SVincenzo Maffione 		if (opt->nro_status == EOPNOTSUPP)
36232ff91c17SVincenzo Maffione 			return EOPNOTSUPP;
36242ff91c17SVincenzo Maffione 
36252ff91c17SVincenzo Maffione 	return 0;
36262ff91c17SVincenzo Maffione }
362768b8534bSLuigi Rizzo 
362868b8534bSLuigi Rizzo /*
362968b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
363068b8534bSLuigi Rizzo  *
363168b8534bSLuigi Rizzo  * Can be called for one or more queues.
363268b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
36338c9874f5SVincenzo Maffione  * If there are no ready events (and 'sr' is not NULL), do a
36348c9874f5SVincenzo Maffione  * selrecord on either individual selinfo or on the global one.
363568b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
363668b8534bSLuigi Rizzo  * are done through callbacks.
3637f196ce38SLuigi Rizzo  *
363801c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
363901c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
364001c7d25fSLuigi Rizzo  * hidden argument.
364168b8534bSLuigi Rizzo  */
3642f9790aebSLuigi Rizzo int
364337e3a6d3SLuigi Rizzo netmap_poll(struct netmap_priv_d *priv, int events, NM_SELRECORD_T *sr)
364468b8534bSLuigi Rizzo {
364568b8534bSLuigi Rizzo 	struct netmap_adapter *na;
364668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
364737e3a6d3SLuigi Rizzo 	struct netmap_ring *ring;
3648b6e66be2SVincenzo Maffione 	u_int i, want[NR_TXRX], revents = 0;
3649b6e66be2SVincenzo Maffione 	NM_SELINFO_T *si[NR_TXRX];
3650847bf383SLuigi Rizzo #define want_tx want[NR_TX]
3651847bf383SLuigi Rizzo #define want_rx want[NR_RX]
3652c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
365301c7d25fSLuigi Rizzo 
3654f9790aebSLuigi Rizzo 	/*
3655f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
3656f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
3657f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
3658f9790aebSLuigi Rizzo 	 */
365917885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
3660ce3ee1e7SLuigi Rizzo 
3661c3e9b4dbSLuiz Otavio O Souza 	/* Transparent mode: send_down is 1 if we have found some
3662c3e9b4dbSLuiz Otavio O Souza 	 * packets to forward (host RX ring --> NIC) during the rx
3663c3e9b4dbSLuiz Otavio O Souza 	 * scan and we have not sent them down to the NIC yet.
3664c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode requires to bind all rings to a single
3665c3e9b4dbSLuiz Otavio O Souza 	 * file descriptor.
3666f0ea3689SLuigi Rizzo 	 */
366737e3a6d3SLuigi Rizzo 	int send_down = 0;
3668c3e9b4dbSLuiz Otavio O Souza 	int sync_flags = priv->np_sync_flags;
366937e3a6d3SLuigi Rizzo 
367037e3a6d3SLuigi Rizzo 	mbq_init(&q);
367168b8534bSLuigi Rizzo 
3672b6e66be2SVincenzo Maffione 	if (unlikely(priv->np_nifp == NULL)) {
36738241616dSLuigi Rizzo 		return POLLERR;
36748241616dSLuigi Rizzo 	}
3675847bf383SLuigi Rizzo 	mb(); /* make sure following reads are not from cache */
36768241616dSLuigi Rizzo 
3677f9790aebSLuigi Rizzo 	na = priv->np_na;
3678f9790aebSLuigi Rizzo 
3679b6e66be2SVincenzo Maffione 	if (unlikely(!nm_netmap_on(na)))
368068b8534bSLuigi Rizzo 		return POLLERR;
368168b8534bSLuigi Rizzo 
3682b6e66be2SVincenzo Maffione 	if (unlikely(priv->np_csb_atok_base)) {
3683b6e66be2SVincenzo Maffione 		nm_prerr("Invalid poll in CSB mode");
3684b6e66be2SVincenzo Maffione 		return POLLERR;
3685b6e66be2SVincenzo Maffione 	}
3686b6e66be2SVincenzo Maffione 
3687b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_ON)
3688b6e66be2SVincenzo Maffione 		nm_prinf("device %s events 0x%x", na->name, events);
368968b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
369068b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
369168b8534bSLuigi Rizzo 
369268b8534bSLuigi Rizzo 	/*
3693b6e66be2SVincenzo Maffione 	 * If the card has more than one queue AND the file descriptor is
3694b6e66be2SVincenzo Maffione 	 * bound to all of them, we sleep on the "global" selinfo, otherwise
3695b6e66be2SVincenzo Maffione 	 * we sleep on individual selinfo (FreeBSD only allows two selinfo's
3696b6e66be2SVincenzo Maffione 	 * per file descriptor).
3697ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
3698ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
369968b8534bSLuigi Rizzo 	 *
370068b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
370168b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
370268b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
370368b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
370468b8534bSLuigi Rizzo 	 */
37055faab778SVincenzo Maffione 	si[NR_RX] = priv->np_si[NR_RX];
37065faab778SVincenzo Maffione 	si[NR_TX] = priv->np_si[NR_TX];
370764ae02c3SLuigi Rizzo 
37084f80b14cSVincenzo Maffione #ifdef __FreeBSD__
370968b8534bSLuigi Rizzo 	/*
3710f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
3711f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
37124f80b14cSVincenzo Maffione 	 * routines. We can't do this on Linux, as the contract says
37134f80b14cSVincenzo Maffione 	 * that we must call nm_os_selrecord() unconditionally.
371468b8534bSLuigi Rizzo 	 */
371537e3a6d3SLuigi Rizzo 	if (want_tx) {
371658e18542SVincenzo Maffione 		const enum txrx t = NR_TX;
371758e18542SVincenzo Maffione 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
37182ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
371958e18542SVincenzo Maffione 			if (kring->ring->cur != kring->ring->tail) {
372058e18542SVincenzo Maffione 				/* Some unseen TX space is available, so what
372158e18542SVincenzo Maffione 				 * we don't need to run txsync. */
372237e3a6d3SLuigi Rizzo 				revents |= want[t];
372358e18542SVincenzo Maffione 				want[t] = 0;
372458e18542SVincenzo Maffione 				break;
372537e3a6d3SLuigi Rizzo 			}
372637e3a6d3SLuigi Rizzo 		}
372737e3a6d3SLuigi Rizzo 	}
372837e3a6d3SLuigi Rizzo 	if (want_rx) {
372958e18542SVincenzo Maffione 		const enum txrx t = NR_RX;
3730e1ed1fbdSVincenzo Maffione 		int rxsync_needed = 0;
3731e1ed1fbdSVincenzo Maffione 
373237e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
37332ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
373458e18542SVincenzo Maffione 			if (kring->ring->cur == kring->ring->tail
373558e18542SVincenzo Maffione 				|| kring->rhead != kring->ring->head) {
373658e18542SVincenzo Maffione 				/* There are no unseen packets on this ring,
373758e18542SVincenzo Maffione 				 * or there are some buffers to be returned
373858e18542SVincenzo Maffione 				 * to the netmap port. We therefore go ahead
373958e18542SVincenzo Maffione 				 * and run rxsync. */
3740e1ed1fbdSVincenzo Maffione 				rxsync_needed = 1;
3741e1ed1fbdSVincenzo Maffione 				break;
374237e3a6d3SLuigi Rizzo 			}
374337e3a6d3SLuigi Rizzo 		}
374458e18542SVincenzo Maffione 		if (!rxsync_needed) {
374558e18542SVincenzo Maffione 			revents |= want_rx;
374658e18542SVincenzo Maffione 			want_rx = 0;
374758e18542SVincenzo Maffione 		}
374837e3a6d3SLuigi Rizzo 	}
37494f80b14cSVincenzo Maffione #endif
37504f80b14cSVincenzo Maffione 
37514f80b14cSVincenzo Maffione #ifdef linux
37524f80b14cSVincenzo Maffione 	/* The selrecord must be unconditional on linux. */
3753b6e66be2SVincenzo Maffione 	nm_os_selrecord(sr, si[NR_RX]);
3754b6e66be2SVincenzo Maffione 	nm_os_selrecord(sr, si[NR_TX]);
37554f80b14cSVincenzo Maffione #endif /* linux */
375668b8534bSLuigi Rizzo 
375768b8534bSLuigi Rizzo 	/*
375817885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
375917885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
376017885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
3761f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
376268b8534bSLuigi Rizzo 	 */
376368b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
376417885a7bSLuigi Rizzo 		/*
376517885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
376617885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
376717885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
376817885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
3769ce3ee1e7SLuigi Rizzo 		 */
3770091fd0abSLuigi Rizzo flush_tx:
377137e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[NR_TX]; i < priv->np_qlast[NR_TX]; i++) {
377217885a7bSLuigi Rizzo 			int found = 0;
377317885a7bSLuigi Rizzo 
37742ff91c17SVincenzo Maffione 			kring = na->tx_rings[i];
377537e3a6d3SLuigi Rizzo 			ring = kring->ring;
377637e3a6d3SLuigi Rizzo 
37774f80b14cSVincenzo Maffione 			/*
37784f80b14cSVincenzo Maffione 			 * Don't try to txsync this TX ring if we already found some
37794f80b14cSVincenzo Maffione 			 * space in some of the TX rings (want_tx == 0) and there are no
37804f80b14cSVincenzo Maffione 			 * TX slots in this ring that need to be flushed to the NIC
37812ff91c17SVincenzo Maffione 			 * (head == hwcur).
37824f80b14cSVincenzo Maffione 			 */
37832ff91c17SVincenzo Maffione 			if (!send_down && !want_tx && ring->head == kring->nr_hwcur)
378468b8534bSLuigi Rizzo 				continue;
378537e3a6d3SLuigi Rizzo 
378637e3a6d3SLuigi Rizzo 			if (nm_kr_tryget(kring, 1, &revents))
378717885a7bSLuigi Rizzo 				continue;
378837e3a6d3SLuigi Rizzo 
378937e3a6d3SLuigi Rizzo 			if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
379017885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
379117885a7bSLuigi Rizzo 				revents |= POLLERR;
379217885a7bSLuigi Rizzo 			} else {
3793c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags))
379468b8534bSLuigi Rizzo 					revents |= POLLERR;
3795847bf383SLuigi Rizzo 				else
379637e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
379717885a7bSLuigi Rizzo 			}
379868b8534bSLuigi Rizzo 
379917885a7bSLuigi Rizzo 			/*
380017885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
380117885a7bSLuigi Rizzo 			 * listeners on the same ring.
380217885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
380317885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
3804f9790aebSLuigi Rizzo 			 */
380517885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
380617885a7bSLuigi Rizzo 			nm_kr_put(kring);
380717885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
380868b8534bSLuigi Rizzo 				revents |= want_tx;
380968b8534bSLuigi Rizzo 				want_tx = 0;
38104f80b14cSVincenzo Maffione #ifndef linux
3811847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
38124f80b14cSVincenzo Maffione #endif /* linux */
381368b8534bSLuigi Rizzo 			}
3814ce3ee1e7SLuigi Rizzo 		}
381537e3a6d3SLuigi Rizzo 		/* if there were any packet to forward we must have handled them by now */
381637e3a6d3SLuigi Rizzo 		send_down = 0;
381737e3a6d3SLuigi Rizzo 		if (want_tx && retry_tx && sr) {
38184f80b14cSVincenzo Maffione #ifndef linux
3819b6e66be2SVincenzo Maffione 			nm_os_selrecord(sr, si[NR_TX]);
38204f80b14cSVincenzo Maffione #endif /* !linux */
3821ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
3822ce3ee1e7SLuigi Rizzo 			goto flush_tx;
382368b8534bSLuigi Rizzo 		}
382468b8534bSLuigi Rizzo 	}
382568b8534bSLuigi Rizzo 
382668b8534bSLuigi Rizzo 	/*
382717885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
382868b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
382968b8534bSLuigi Rizzo 	 */
383068b8534bSLuigi Rizzo 	if (want_rx) {
383189cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
3832ce3ee1e7SLuigi Rizzo do_retry_rx:
3833847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[NR_RX]; i < priv->np_qlast[NR_RX]; i++) {
383417885a7bSLuigi Rizzo 			int found = 0;
383517885a7bSLuigi Rizzo 
38362ff91c17SVincenzo Maffione 			kring = na->rx_rings[i];
383737e3a6d3SLuigi Rizzo 			ring = kring->ring;
3838ce3ee1e7SLuigi Rizzo 
383937e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &revents)))
384017885a7bSLuigi Rizzo 				continue;
3841ce3ee1e7SLuigi Rizzo 
384237e3a6d3SLuigi Rizzo 			if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
3843847bf383SLuigi Rizzo 				netmap_ring_reinit(kring);
3844847bf383SLuigi Rizzo 				revents |= POLLERR;
3845847bf383SLuigi Rizzo 			}
3846847bf383SLuigi Rizzo 			/* now we can use kring->rcur, rtail */
3847847bf383SLuigi Rizzo 
384817885a7bSLuigi Rizzo 			/*
3849c3e9b4dbSLuiz Otavio O Souza 			 * transparent mode support: collect packets from
3850c3e9b4dbSLuiz Otavio O Souza 			 * hw rxring(s) that have been released by the user
3851ce3ee1e7SLuigi Rizzo 			 */
385237e3a6d3SLuigi Rizzo 			if (nm_may_forward_up(kring)) {
3853091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
3854091fd0abSLuigi Rizzo 			}
385568b8534bSLuigi Rizzo 
3856c3e9b4dbSLuiz Otavio O Souza 			/* Clear the NR_FORWARD flag anyway, it may be set by
3857c3e9b4dbSLuiz Otavio O Souza 			 * the nm_sync() below only on for the host RX ring (see
3858c3e9b4dbSLuiz Otavio O Souza 			 * netmap_rxsync_from_host()). */
385937e3a6d3SLuigi Rizzo 			kring->nr_kflags &= ~NR_FORWARD;
3860c3e9b4dbSLuiz Otavio O Souza 			if (kring->nm_sync(kring, sync_flags))
386168b8534bSLuigi Rizzo 				revents |= POLLERR;
3862847bf383SLuigi Rizzo 			else
386337e3a6d3SLuigi Rizzo 				nm_sync_finalize(kring);
3864c3e9b4dbSLuiz Otavio O Souza 			send_down |= (kring->nr_kflags & NR_FORWARD);
3865c3e9b4dbSLuiz Otavio O Souza 			ring_timestamp_set(ring);
386617885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
386717885a7bSLuigi Rizzo 			nm_kr_put(kring);
386817885a7bSLuigi Rizzo 			if (found) {
386968b8534bSLuigi Rizzo 				revents |= want_rx;
3870ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
38714f80b14cSVincenzo Maffione #ifndef linux
3872847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
38734f80b14cSVincenzo Maffione #endif /* linux */
387468b8534bSLuigi Rizzo 			}
387568b8534bSLuigi Rizzo 		}
387617885a7bSLuigi Rizzo 
38774f80b14cSVincenzo Maffione #ifndef linux
387837e3a6d3SLuigi Rizzo 		if (retry_rx && sr) {
3879b6e66be2SVincenzo Maffione 			nm_os_selrecord(sr, si[NR_RX]);
388037e3a6d3SLuigi Rizzo 		}
38814f80b14cSVincenzo Maffione #endif /* !linux */
3882c3e9b4dbSLuiz Otavio O Souza 		if (send_down || retry_rx) {
388317885a7bSLuigi Rizzo 			retry_rx = 0;
388417885a7bSLuigi Rizzo 			if (send_down)
388517885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
388617885a7bSLuigi Rizzo 			else
3887ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
3888ce3ee1e7SLuigi Rizzo 		}
388968b8534bSLuigi Rizzo 	}
3890091fd0abSLuigi Rizzo 
389117885a7bSLuigi Rizzo 	/*
3892c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode: released bufs (i.e. between kring->nr_hwcur and
3893c3e9b4dbSLuiz Otavio O Souza 	 * ring->head) marked with NS_FORWARD on hw rx rings are passed up
3894c3e9b4dbSLuiz Otavio O Souza 	 * to the host stack.
3895ce3ee1e7SLuigi Rizzo 	 */
3896091fd0abSLuigi Rizzo 
3897c3e9b4dbSLuiz Otavio O Souza 	if (mbq_peek(&q)) {
3898f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
389937e3a6d3SLuigi Rizzo 	}
390068b8534bSLuigi Rizzo 
390168b8534bSLuigi Rizzo 	return (revents);
3902847bf383SLuigi Rizzo #undef want_tx
3903847bf383SLuigi Rizzo #undef want_rx
390468b8534bSLuigi Rizzo }
390568b8534bSLuigi Rizzo 
39064f80b14cSVincenzo Maffione int
39074f80b14cSVincenzo Maffione nma_intr_enable(struct netmap_adapter *na, int onoff)
39084f80b14cSVincenzo Maffione {
39094f80b14cSVincenzo Maffione 	bool changed = false;
39104f80b14cSVincenzo Maffione 	enum txrx t;
39114f80b14cSVincenzo Maffione 	int i;
39124f80b14cSVincenzo Maffione 
39134f80b14cSVincenzo Maffione 	for_rx_tx(t) {
39144f80b14cSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t); i++) {
39152ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
39164f80b14cSVincenzo Maffione 			int on = !(kring->nr_kflags & NKR_NOINTR);
39174f80b14cSVincenzo Maffione 
39184f80b14cSVincenzo Maffione 			if (!!onoff != !!on) {
39194f80b14cSVincenzo Maffione 				changed = true;
39204f80b14cSVincenzo Maffione 			}
39214f80b14cSVincenzo Maffione 			if (onoff) {
39224f80b14cSVincenzo Maffione 				kring->nr_kflags &= ~NKR_NOINTR;
39234f80b14cSVincenzo Maffione 			} else {
39244f80b14cSVincenzo Maffione 				kring->nr_kflags |= NKR_NOINTR;
39254f80b14cSVincenzo Maffione 			}
39264f80b14cSVincenzo Maffione 		}
39274f80b14cSVincenzo Maffione 	}
39284f80b14cSVincenzo Maffione 
39294f80b14cSVincenzo Maffione 	if (!changed) {
39304f80b14cSVincenzo Maffione 		return 0; /* nothing to do */
39314f80b14cSVincenzo Maffione 	}
39324f80b14cSVincenzo Maffione 
39334f80b14cSVincenzo Maffione 	if (!na->nm_intr) {
3934b6e66be2SVincenzo Maffione 		nm_prerr("Cannot %s interrupts for %s", onoff ? "enable" : "disable",
39354f80b14cSVincenzo Maffione 		  na->name);
39364f80b14cSVincenzo Maffione 		return -1;
39374f80b14cSVincenzo Maffione 	}
39384f80b14cSVincenzo Maffione 
39394f80b14cSVincenzo Maffione 	na->nm_intr(na, onoff);
39404f80b14cSVincenzo Maffione 
39414f80b14cSVincenzo Maffione 	return 0;
39424f80b14cSVincenzo Maffione }
39434f80b14cSVincenzo Maffione 
394417885a7bSLuigi Rizzo 
394517885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
394668b8534bSLuigi Rizzo 
394789cc2556SLuigi Rizzo /* default notify callback */
3948f9790aebSLuigi Rizzo static int
3949847bf383SLuigi Rizzo netmap_notify(struct netmap_kring *kring, int flags)
3950f9790aebSLuigi Rizzo {
39512ff91c17SVincenzo Maffione 	struct netmap_adapter *na = kring->notify_na;
3952847bf383SLuigi Rizzo 	enum txrx t = kring->tx;
3953f9790aebSLuigi Rizzo 
395437e3a6d3SLuigi Rizzo 	nm_os_selwakeup(&kring->si);
395589cc2556SLuigi Rizzo 	/* optimization: avoid a wake up on the global
395689cc2556SLuigi Rizzo 	 * queue if nobody has registered for more
395789cc2556SLuigi Rizzo 	 * than one ring
395889cc2556SLuigi Rizzo 	 */
3959847bf383SLuigi Rizzo 	if (na->si_users[t] > 0)
396037e3a6d3SLuigi Rizzo 		nm_os_selwakeup(&na->si[t]);
3961847bf383SLuigi Rizzo 
396237e3a6d3SLuigi Rizzo 	return NM_IRQ_COMPLETED;
3963f9790aebSLuigi Rizzo }
3964f9790aebSLuigi Rizzo 
396589cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
396637e3a6d3SLuigi Rizzo  * provide some defaults and get a reference to the
396737e3a6d3SLuigi Rizzo  * memory allocator
396889cc2556SLuigi Rizzo  */
3969f9790aebSLuigi Rizzo int
3970f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
3971f9790aebSLuigi Rizzo {
39722ff91c17SVincenzo Maffione 	if (!na->rx_buf_maxsize) {
39732ff91c17SVincenzo Maffione 		/* Set a conservative default (larger is safer). */
39742ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = PAGE_SIZE;
39752ff91c17SVincenzo Maffione 	}
39762ff91c17SVincenzo Maffione 
397717885a7bSLuigi Rizzo #ifdef __FreeBSD__
397837e3a6d3SLuigi Rizzo 	if (na->na_flags & NAF_HOST_RINGS && na->ifp) {
397937e3a6d3SLuigi Rizzo 		na->if_input = na->ifp->if_input; /* for netmap_send_up */
39804bf50f18SLuigi Rizzo 	}
39814f80b14cSVincenzo Maffione 	na->pdev = na; /* make sure netmap_mem_map() is called */
398237e3a6d3SLuigi Rizzo #endif /* __FreeBSD__ */
39832a7db7a6SVincenzo Maffione 	if (na->na_flags & NAF_HOST_RINGS) {
39842a7db7a6SVincenzo Maffione 		if (na->num_host_rx_rings == 0)
39852a7db7a6SVincenzo Maffione 			na->num_host_rx_rings = 1;
39862a7db7a6SVincenzo Maffione 		if (na->num_host_tx_rings == 0)
39872a7db7a6SVincenzo Maffione 			na->num_host_tx_rings = 1;
39882a7db7a6SVincenzo Maffione 	}
3989f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
399089cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
399189cc2556SLuigi Rizzo 		 * since other port types all provide their own
399289cc2556SLuigi Rizzo 		 * nm_krings_create
399389cc2556SLuigi Rizzo 		 */
3994f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
399517885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
3996f9790aebSLuigi Rizzo 	}
3997f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
3998f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
3999f9790aebSLuigi Rizzo 	na->active_fds = 0;
4000f9790aebSLuigi Rizzo 
4001c3e9b4dbSLuiz Otavio O Souza 	if (na->nm_mem == NULL) {
4002a6d768d8SVincenzo Maffione 		/* use iommu or global allocator */
4003a6d768d8SVincenzo Maffione 		na->nm_mem = netmap_mem_get_iommu(na);
4004c3e9b4dbSLuiz Otavio O Souza 	}
40054bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
40064bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
40074bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
40084bf50f18SLuigi Rizzo 		 */
40092a7db7a6SVincenzo Maffione 		na->nm_bdg_attach = netmap_default_bdg_attach;
401037e3a6d3SLuigi Rizzo 
4011f9790aebSLuigi Rizzo 	return 0;
4012f9790aebSLuigi Rizzo }
4013f9790aebSLuigi Rizzo 
401437e3a6d3SLuigi Rizzo /* Wrapper for the register callback provided netmap-enabled
401537e3a6d3SLuigi Rizzo  * hardware drivers.
401637e3a6d3SLuigi Rizzo  * nm_iszombie(na) means that the driver module has been
40174bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
401837e3a6d3SLuigi Rizzo  * nm_os_ifnet_lock() must guarantee mutual exclusion with
401937e3a6d3SLuigi Rizzo  * module unloading.
40204bf50f18SLuigi Rizzo  */
40214bf50f18SLuigi Rizzo static int
402237e3a6d3SLuigi Rizzo netmap_hw_reg(struct netmap_adapter *na, int onoff)
40234bf50f18SLuigi Rizzo {
40244bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
40254bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
402637e3a6d3SLuigi Rizzo 	int error = 0;
40274bf50f18SLuigi Rizzo 
402837e3a6d3SLuigi Rizzo 	nm_os_ifnet_lock();
40294bf50f18SLuigi Rizzo 
403037e3a6d3SLuigi Rizzo 	if (nm_iszombie(na)) {
403137e3a6d3SLuigi Rizzo 		if (onoff) {
403237e3a6d3SLuigi Rizzo 			error = ENXIO;
403337e3a6d3SLuigi Rizzo 		} else if (na != NULL) {
403437e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_NETMAP_ON;
403537e3a6d3SLuigi Rizzo 		}
403637e3a6d3SLuigi Rizzo 		goto out;
403737e3a6d3SLuigi Rizzo 	}
403837e3a6d3SLuigi Rizzo 
403937e3a6d3SLuigi Rizzo 	error = hwna->nm_hw_register(na, onoff);
404037e3a6d3SLuigi Rizzo 
404137e3a6d3SLuigi Rizzo out:
404237e3a6d3SLuigi Rizzo 	nm_os_ifnet_unlock();
404337e3a6d3SLuigi Rizzo 
404437e3a6d3SLuigi Rizzo 	return error;
404537e3a6d3SLuigi Rizzo }
404637e3a6d3SLuigi Rizzo 
404737e3a6d3SLuigi Rizzo static void
404837e3a6d3SLuigi Rizzo netmap_hw_dtor(struct netmap_adapter *na)
404937e3a6d3SLuigi Rizzo {
40502a7db7a6SVincenzo Maffione 	if (na->ifp == NULL)
405137e3a6d3SLuigi Rizzo 		return;
405237e3a6d3SLuigi Rizzo 
40532a7db7a6SVincenzo Maffione 	NM_DETACH_NA(na->ifp);
40544bf50f18SLuigi Rizzo }
40554bf50f18SLuigi Rizzo 
4056f18be576SLuigi Rizzo 
405768b8534bSLuigi Rizzo /*
4058c3e9b4dbSLuiz Otavio O Souza  * Allocate a netmap_adapter object, and initialize it from the
405937e3a6d3SLuigi Rizzo  * 'arg' passed by the driver on attach.
4060c3e9b4dbSLuiz Otavio O Souza  * We allocate a block of memory of 'size' bytes, which has room
4061c3e9b4dbSLuiz Otavio O Souza  * for struct netmap_adapter plus additional room private to
4062c3e9b4dbSLuiz Otavio O Souza  * the caller.
406368b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
406468b8534bSLuigi Rizzo  */
4065c3e9b4dbSLuiz Otavio O Souza int
40664f80b14cSVincenzo Maffione netmap_attach_ext(struct netmap_adapter *arg, size_t size, int override_reg)
406768b8534bSLuigi Rizzo {
4068f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
406937e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
407068b8534bSLuigi Rizzo 
4071c3e9b4dbSLuiz Otavio O Souza 	if (size < sizeof(struct netmap_hw_adapter)) {
4072b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
4073b6e66be2SVincenzo Maffione 			nm_prerr("Invalid netmap adapter size %d", (int)size);
4074c3e9b4dbSLuiz Otavio O Souza 		return EINVAL;
4075c3e9b4dbSLuiz Otavio O Souza 	}
4076c3e9b4dbSLuiz Otavio O Souza 
4077b6e66be2SVincenzo Maffione 	if (arg == NULL || arg->ifp == NULL) {
4078b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
4079b6e66be2SVincenzo Maffione 			nm_prerr("either arg or arg->ifp is NULL");
40802a7db7a6SVincenzo Maffione 		return EINVAL;
4081b6e66be2SVincenzo Maffione 	}
4082b6e66be2SVincenzo Maffione 
4083b6e66be2SVincenzo Maffione 	if (arg->num_tx_rings == 0 || arg->num_rx_rings == 0) {
4084b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
4085b6e66be2SVincenzo Maffione 			nm_prerr("%s: invalid rings tx %d rx %d",
4086b6e66be2SVincenzo Maffione 				arg->name, arg->num_tx_rings, arg->num_rx_rings);
4087b6e66be2SVincenzo Maffione 		return EINVAL;
4088b6e66be2SVincenzo Maffione 	}
40894f80b14cSVincenzo Maffione 
409037e3a6d3SLuigi Rizzo 	ifp = arg->ifp;
40912a7db7a6SVincenzo Maffione 	if (NM_NA_CLASH(ifp)) {
40924f80b14cSVincenzo Maffione 		/* If NA(ifp) is not null but there is no valid netmap
40934f80b14cSVincenzo Maffione 		 * adapter it means that someone else is using the same
40944f80b14cSVincenzo Maffione 		 * pointer (e.g. ax25_ptr on linux). This happens for
40954f80b14cSVincenzo Maffione 		 * instance when also PF_RING is in use. */
4096b6e66be2SVincenzo Maffione 		nm_prerr("Error: netmap adapter hook is busy");
40974f80b14cSVincenzo Maffione 		return EBUSY;
40984f80b14cSVincenzo Maffione 	}
40994f80b14cSVincenzo Maffione 
4100c3e9b4dbSLuiz Otavio O Souza 	hwna = nm_os_malloc(size);
4101f9790aebSLuigi Rizzo 	if (hwna == NULL)
4102ae10d1afSLuigi Rizzo 		goto fail;
4103f9790aebSLuigi Rizzo 	hwna->up = *arg;
4104847bf383SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS | NAF_NATIVE;
4105b6e66be2SVincenzo Maffione 	strlcpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
41064f80b14cSVincenzo Maffione 	if (override_reg) {
41074bf50f18SLuigi Rizzo 		hwna->nm_hw_register = hwna->up.nm_register;
410837e3a6d3SLuigi Rizzo 		hwna->up.nm_register = netmap_hw_reg;
41094f80b14cSVincenzo Maffione 	}
4110f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
4111c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(hwna);
4112f9790aebSLuigi Rizzo 		goto fail;
4113f9790aebSLuigi Rizzo 	}
4114f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
4115f9790aebSLuigi Rizzo 
411637e3a6d3SLuigi Rizzo 	NM_ATTACH_NA(ifp, &hwna->up);
411737e3a6d3SLuigi Rizzo 
41182a7db7a6SVincenzo Maffione 	nm_os_onattach(ifp);
41192a7db7a6SVincenzo Maffione 
412037e3a6d3SLuigi Rizzo 	if (arg->nm_dtor == NULL) {
412137e3a6d3SLuigi Rizzo 		hwna->up.nm_dtor = netmap_hw_dtor;
412237e3a6d3SLuigi Rizzo 	}
4123f9790aebSLuigi Rizzo 
41241ef2a881SVincenzo Maffione 	if_printf(ifp, "netmap queues/slots: TX %d/%d, RX %d/%d\n",
4125d82f9014SRui Paulo 	    hwna->up.num_tx_rings, hwna->up.num_tx_desc,
4126d82f9014SRui Paulo 	    hwna->up.num_rx_rings, hwna->up.num_rx_desc);
4127ae10d1afSLuigi Rizzo 	return 0;
412868b8534bSLuigi Rizzo 
4129ae10d1afSLuigi Rizzo fail:
4130b6e66be2SVincenzo Maffione 	nm_prerr("fail, arg %p ifp %p na %p", arg, ifp, hwna);
4131f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
413268b8534bSLuigi Rizzo }
413368b8534bSLuigi Rizzo 
413468b8534bSLuigi Rizzo 
413537e3a6d3SLuigi Rizzo int
413637e3a6d3SLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
413737e3a6d3SLuigi Rizzo {
41384f80b14cSVincenzo Maffione 	return netmap_attach_ext(arg, sizeof(struct netmap_hw_adapter),
41394f80b14cSVincenzo Maffione 			1 /* override nm_reg */);
414037e3a6d3SLuigi Rizzo }
414137e3a6d3SLuigi Rizzo 
414237e3a6d3SLuigi Rizzo 
4143f9790aebSLuigi Rizzo void
4144f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
4145f9790aebSLuigi Rizzo {
4146f9790aebSLuigi Rizzo 	if (!na) {
4147f9790aebSLuigi Rizzo 		return;
4148f9790aebSLuigi Rizzo 	}
4149f9790aebSLuigi Rizzo 
4150f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
4151f9790aebSLuigi Rizzo }
4152f9790aebSLuigi Rizzo 
4153f9790aebSLuigi Rizzo 
4154f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
4155f9790aebSLuigi Rizzo int
4156f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
4157f9790aebSLuigi Rizzo {
4158f9790aebSLuigi Rizzo 	if (!na)
4159f9790aebSLuigi Rizzo 		return 1;
4160f9790aebSLuigi Rizzo 
4161f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
4162f9790aebSLuigi Rizzo 		return 0;
4163f9790aebSLuigi Rizzo 
4164f9790aebSLuigi Rizzo 	if (na->nm_dtor)
4165f9790aebSLuigi Rizzo 		na->nm_dtor(na);
4166f9790aebSLuigi Rizzo 
41674f80b14cSVincenzo Maffione 	if (na->tx_rings) { /* XXX should not happen */
4168b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_ON)
4169b6e66be2SVincenzo Maffione 			nm_prerr("freeing leftover tx_rings");
41704f80b14cSVincenzo Maffione 		na->nm_krings_delete(na);
41714f80b14cSVincenzo Maffione 	}
41724f80b14cSVincenzo Maffione 	netmap_pipe_dealloc(na);
41734f80b14cSVincenzo Maffione 	if (na->nm_mem)
41744f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
41754f80b14cSVincenzo Maffione 	bzero(na, sizeof(*na));
41764f80b14cSVincenzo Maffione 	nm_os_free(na);
4177f9790aebSLuigi Rizzo 
4178f9790aebSLuigi Rizzo 	return 1;
4179f9790aebSLuigi Rizzo }
4180f9790aebSLuigi Rizzo 
418189cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
4182f9790aebSLuigi Rizzo int
4183f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
4184f9790aebSLuigi Rizzo {
4185f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
418617885a7bSLuigi Rizzo 	if (ret == 0) {
418717885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
41882a7db7a6SVincenzo Maffione 		u_int lim = netmap_real_rings(na, NR_RX), i;
41892a7db7a6SVincenzo Maffione 		for (i = na->num_rx_rings; i < lim; i++) {
41902a7db7a6SVincenzo Maffione 			mbq_safe_init(&NMR(na, NR_RX)[i]->rx_queue);
41912a7db7a6SVincenzo Maffione 		}
419275f4f3edSVincenzo Maffione 		nm_prdis("initialized sw rx queue %d", na->num_rx_rings);
419317885a7bSLuigi Rizzo 	}
419417885a7bSLuigi Rizzo 	return ret;
4195f9790aebSLuigi Rizzo }
4196f9790aebSLuigi Rizzo 
4197f9790aebSLuigi Rizzo 
4198f9790aebSLuigi Rizzo 
419968b8534bSLuigi Rizzo /*
420089cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
420168b8534bSLuigi Rizzo  */
420268b8534bSLuigi Rizzo void
420368b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
420468b8534bSLuigi Rizzo {
420598399ab0SVincenzo Maffione 	struct netmap_adapter *na;
420668b8534bSLuigi Rizzo 
4207f9790aebSLuigi Rizzo 	NMG_LOCK();
420898399ab0SVincenzo Maffione 
420998399ab0SVincenzo Maffione 	if (!NM_NA_VALID(ifp)) {
421098399ab0SVincenzo Maffione 		NMG_UNLOCK();
421198399ab0SVincenzo Maffione 		return;
421298399ab0SVincenzo Maffione 	}
421398399ab0SVincenzo Maffione 
421498399ab0SVincenzo Maffione 	na = NA(ifp);
421537e3a6d3SLuigi Rizzo 	netmap_set_all_rings(na, NM_KR_LOCKED);
4216847bf383SLuigi Rizzo 	/*
4217847bf383SLuigi Rizzo 	 * if the netmap adapter is not native, somebody
4218847bf383SLuigi Rizzo 	 * changed it, so we can not release it here.
421937e3a6d3SLuigi Rizzo 	 * The NAF_ZOMBIE flag will notify the new owner that
4220847bf383SLuigi Rizzo 	 * the driver is gone.
4221847bf383SLuigi Rizzo 	 */
42224f80b14cSVincenzo Maffione 	if (!(na->na_flags & NAF_NATIVE) || !netmap_adapter_put(na)) {
42234f80b14cSVincenzo Maffione 		na->na_flags |= NAF_ZOMBIE;
4224847bf383SLuigi Rizzo 	}
422537e3a6d3SLuigi Rizzo 	/* give active users a chance to notice that NAF_ZOMBIE has been
422637e3a6d3SLuigi Rizzo 	 * turned on, so that they can stop and return an error to userspace.
422737e3a6d3SLuigi Rizzo 	 * Note that this becomes a NOP if there are no active users and,
422837e3a6d3SLuigi Rizzo 	 * therefore, the put() above has deleted the na, since now NA(ifp) is
422937e3a6d3SLuigi Rizzo 	 * NULL.
423037e3a6d3SLuigi Rizzo 	 */
4231f9790aebSLuigi Rizzo 	netmap_enable_all_rings(ifp);
4232f9790aebSLuigi Rizzo 	NMG_UNLOCK();
4233ae10d1afSLuigi Rizzo }
4234f18be576SLuigi Rizzo 
4235f18be576SLuigi Rizzo 
423668b8534bSLuigi Rizzo /*
423702ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
423802ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
423917885a7bSLuigi Rizzo  *
424017885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
424117885a7bSLuigi Rizzo  * in the relevant rxsync routine.
424217885a7bSLuigi Rizzo  *
4243ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
4244ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
4245ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
4246f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
424768b8534bSLuigi Rizzo  */
424868b8534bSLuigi Rizzo int
4249ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
425068b8534bSLuigi Rizzo {
425168b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
425237e3a6d3SLuigi Rizzo 	struct netmap_kring *kring, *tx_kring;
425317885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
425417885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
425537e3a6d3SLuigi Rizzo 	unsigned int txr;
425617885a7bSLuigi Rizzo 	struct mbq *q;
4257c3e9b4dbSLuiz Otavio O Souza 	int busy;
42582a7db7a6SVincenzo Maffione 	u_int i;
425968b8534bSLuigi Rizzo 
42602a7db7a6SVincenzo Maffione 	i = MBUF_TXQ(m);
42612a7db7a6SVincenzo Maffione 	if (i >= na->num_host_rx_rings) {
42622a7db7a6SVincenzo Maffione 		i = i % na->num_host_rx_rings;
42632a7db7a6SVincenzo Maffione 	}
42642a7db7a6SVincenzo Maffione 	kring = NMR(na, NR_RX)[nma_get_nrings(na, NR_RX) + i];
42652a7db7a6SVincenzo Maffione 
4266ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
4267ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
4268ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
426917885a7bSLuigi Rizzo 
42704bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
4271b6e66be2SVincenzo Maffione 		nm_prerr("%s not in netmap mode anymore", na->name);
4272ce3ee1e7SLuigi Rizzo 		error = ENXIO;
4273ce3ee1e7SLuigi Rizzo 		goto done;
4274ce3ee1e7SLuigi Rizzo 	}
4275ce3ee1e7SLuigi Rizzo 
427637e3a6d3SLuigi Rizzo 	txr = MBUF_TXQ(m);
427737e3a6d3SLuigi Rizzo 	if (txr >= na->num_tx_rings) {
427837e3a6d3SLuigi Rizzo 		txr %= na->num_tx_rings;
427937e3a6d3SLuigi Rizzo 	}
42802ff91c17SVincenzo Maffione 	tx_kring = NMR(na, NR_TX)[txr];
428137e3a6d3SLuigi Rizzo 
428237e3a6d3SLuigi Rizzo 	if (tx_kring->nr_mode == NKR_NETMAP_OFF) {
428337e3a6d3SLuigi Rizzo 		return MBUF_TRANSMIT(na, ifp, m);
428437e3a6d3SLuigi Rizzo 	}
428537e3a6d3SLuigi Rizzo 
428617885a7bSLuigi Rizzo 	q = &kring->rx_queue;
428717885a7bSLuigi Rizzo 
4288ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
42894bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
4290b6e66be2SVincenzo Maffione 		nm_prerr("%s from_host, drop packet size %d > %d", na->name,
42914bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
4292ce3ee1e7SLuigi Rizzo 		goto done;
4293849bec0eSLuigi Rizzo 	}
429417885a7bSLuigi Rizzo 
42952a7db7a6SVincenzo Maffione 	if (!netmap_generic_hwcsum) {
42962a7db7a6SVincenzo Maffione 		if (nm_os_mbuf_has_csum_offld(m)) {
429775f4f3edSVincenzo Maffione 			nm_prlim(1, "%s drop mbuf that needs checksum offload", na->name);
42982a7db7a6SVincenzo Maffione 			goto done;
42992a7db7a6SVincenzo Maffione 		}
43002a7db7a6SVincenzo Maffione 	}
43012a7db7a6SVincenzo Maffione 
43022a7db7a6SVincenzo Maffione 	if (nm_os_mbuf_has_seg_offld(m)) {
430375f4f3edSVincenzo Maffione 		nm_prlim(1, "%s drop mbuf that needs generic segmentation offload", na->name);
430437e3a6d3SLuigi Rizzo 		goto done;
430537e3a6d3SLuigi Rizzo 	}
430637e3a6d3SLuigi Rizzo 
430789a9a5b5SVincenzo Maffione #ifdef __FreeBSD__
430889a9a5b5SVincenzo Maffione 	ETHER_BPF_MTAP(ifp, m);
430989a9a5b5SVincenzo Maffione #endif /* __FreeBSD__ */
431089a9a5b5SVincenzo Maffione 
4311c3e9b4dbSLuiz Otavio O Souza 	/* protect against netmap_rxsync_from_host(), netmap_sw_to_nic()
431217885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
431317885a7bSLuigi Rizzo 	 * not possible on Linux).
4314c3e9b4dbSLuiz Otavio O Souza 	 * We enqueue the mbuf only if we are sure there is going to be
4315c3e9b4dbSLuiz Otavio O Souza 	 * enough room in the host RX ring, otherwise we drop it.
4316ce3ee1e7SLuigi Rizzo 	 */
4317997b054cSLuigi Rizzo 	mbq_lock(q);
431817885a7bSLuigi Rizzo 
4319c3e9b4dbSLuiz Otavio O Souza 	busy = kring->nr_hwtail - kring->nr_hwcur;
4320c3e9b4dbSLuiz Otavio O Souza 	if (busy < 0)
4321c3e9b4dbSLuiz Otavio O Souza 		busy += kring->nkr_num_slots;
4322c3e9b4dbSLuiz Otavio O Souza 	if (busy + mbq_len(q) >= kring->nkr_num_slots - 1) {
432375f4f3edSVincenzo Maffione 		nm_prlim(2, "%s full hwcur %d hwtail %d qlen %d", na->name,
4324c3e9b4dbSLuiz Otavio O Souza 			kring->nr_hwcur, kring->nr_hwtail, mbq_len(q));
4325ce3ee1e7SLuigi Rizzo 	} else {
432617885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
432775f4f3edSVincenzo Maffione 		nm_prdis(2, "%s %d bufs in queue", na->name, mbq_len(q));
432817885a7bSLuigi Rizzo 		/* notify outside the lock */
432917885a7bSLuigi Rizzo 		m = NULL;
433068b8534bSLuigi Rizzo 		error = 0;
4331ce3ee1e7SLuigi Rizzo 	}
4332997b054cSLuigi Rizzo 	mbq_unlock(q);
4333ce3ee1e7SLuigi Rizzo 
433468b8534bSLuigi Rizzo done:
433517885a7bSLuigi Rizzo 	if (m)
433668b8534bSLuigi Rizzo 		m_freem(m);
433717885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
4338847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
433989cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
434089cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
434189cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
434289cc2556SLuigi Rizzo 	 */
434368b8534bSLuigi Rizzo 
434468b8534bSLuigi Rizzo 	return (error);
434568b8534bSLuigi Rizzo }
434668b8534bSLuigi Rizzo 
434768b8534bSLuigi Rizzo 
434868b8534bSLuigi Rizzo /*
43497ba6ecf2SVincenzo Maffione  * Reset function to be called by the driver routines when reinitializing
43507ba6ecf2SVincenzo Maffione  * a hardware ring. The driver is in charge of locking to protect the kring
435155f0ad5fSVincenzo Maffione  * while this operation is being performed. This is normally achieved by
435255f0ad5fSVincenzo Maffione  * calling netmap_disable_all_rings() before triggering a reset.
43537ba6ecf2SVincenzo Maffione  * If the kring is not in netmap mode, return NULL to inform the caller
43547ba6ecf2SVincenzo Maffione  * that this is the case.
435555f0ad5fSVincenzo Maffione  * If the kring is in netmap mode, set hwofs so that the netmap indices
435655f0ad5fSVincenzo Maffione  * seen by userspace (head/cut/tail) do not change, although the internal
435755f0ad5fSVincenzo Maffione  * NIC indices have been reset to 0.
43587ba6ecf2SVincenzo Maffione  * In any case, adjust kring->nr_mode.
435968b8534bSLuigi Rizzo  */
436068b8534bSLuigi Rizzo struct netmap_slot *
4361ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
436268b8534bSLuigi Rizzo 	u_int new_cur)
436368b8534bSLuigi Rizzo {
436468b8534bSLuigi Rizzo 	struct netmap_kring *kring;
436555f0ad5fSVincenzo Maffione 	u_int new_hwtail, new_hwofs;
436668b8534bSLuigi Rizzo 
43674bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
436875f4f3edSVincenzo Maffione 		nm_prdis("interface not in native netmap mode");
436968b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
4370ce3ee1e7SLuigi Rizzo 	}
437168b8534bSLuigi Rizzo 
437264ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
43738241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
43748241616dSLuigi Rizzo 			return NULL;
43752ff91c17SVincenzo Maffione 		kring = na->tx_rings[n];
437655f0ad5fSVincenzo Maffione 		/*
437755f0ad5fSVincenzo Maffione 		 * Set hwofs to rhead, so that slots[rhead] is mapped to
437855f0ad5fSVincenzo Maffione 		 * the NIC internal slot 0, and thus the netmap buffer
437955f0ad5fSVincenzo Maffione 		 * at rhead is the next to be transmitted. Transmissions
438055f0ad5fSVincenzo Maffione 		 * that were pending before the reset are considered as
438155f0ad5fSVincenzo Maffione 		 * sent, so that we can have hwcur = rhead. All the slots
438255f0ad5fSVincenzo Maffione 		 * are now owned by the user, so we can also reinit hwtail.
438355f0ad5fSVincenzo Maffione 		 */
438455f0ad5fSVincenzo Maffione 		new_hwofs = kring->rhead;
438555f0ad5fSVincenzo Maffione 		new_hwtail = nm_prev(kring->rhead, kring->nkr_num_slots - 1);
438664ae02c3SLuigi Rizzo 	} else {
43878241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
43888241616dSLuigi Rizzo 			return NULL;
43892ff91c17SVincenzo Maffione 		kring = na->rx_rings[n];
439055f0ad5fSVincenzo Maffione 		/*
439155f0ad5fSVincenzo Maffione 		 * Set hwofs to hwtail, so that slots[hwtail] is mapped to
439255f0ad5fSVincenzo Maffione 		 * the NIC internal slot 0, and thus the netmap buffer
439355f0ad5fSVincenzo Maffione 		 * at hwtail is the next to be given to the NIC.
439455f0ad5fSVincenzo Maffione 		 * Unread slots (the ones in [rhead,hwtail[) are owned by
439555f0ad5fSVincenzo Maffione 		 * the user, and thus the caller cannot give them
439655f0ad5fSVincenzo Maffione 		 * to the NIC right now.
439755f0ad5fSVincenzo Maffione 		 */
439855f0ad5fSVincenzo Maffione 		new_hwofs = kring->nr_hwtail;
439955f0ad5fSVincenzo Maffione 		new_hwtail = kring->nr_hwtail;
44007ba6ecf2SVincenzo Maffione 	}
440137e3a6d3SLuigi Rizzo 	if (kring->nr_pending_mode == NKR_NETMAP_OFF) {
440237e3a6d3SLuigi Rizzo 		kring->nr_mode = NKR_NETMAP_OFF;
440337e3a6d3SLuigi Rizzo 		return NULL;
440437e3a6d3SLuigi Rizzo 	}
44057ba6ecf2SVincenzo Maffione 	if (netmap_verbose) {
440655f0ad5fSVincenzo Maffione 	    nm_prinf("%s, hc %u->%u, ht %u->%u, ho %u->%u", kring->name,
440755f0ad5fSVincenzo Maffione 	        kring->nr_hwcur, kring->rhead,
440855f0ad5fSVincenzo Maffione 	        kring->nr_hwtail, new_hwtail,
440955f0ad5fSVincenzo Maffione 		kring->nkr_hwofs, new_hwofs);
441064ae02c3SLuigi Rizzo 	}
441155f0ad5fSVincenzo Maffione 	kring->nr_hwcur = kring->rhead;
441255f0ad5fSVincenzo Maffione 	kring->nr_hwtail = new_hwtail;
441355f0ad5fSVincenzo Maffione 	kring->nkr_hwofs = new_hwofs;
4414506cc70cSLuigi Rizzo 
441568b8534bSLuigi Rizzo 	/*
4416ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
4417506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
4418506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
441968b8534bSLuigi Rizzo 	 */
442037e3a6d3SLuigi Rizzo 	kring->nr_mode = NKR_NETMAP_ON;
4421847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
442268b8534bSLuigi Rizzo 	return kring->ring->slot;
442368b8534bSLuigi Rizzo }
442468b8534bSLuigi Rizzo 
442568b8534bSLuigi Rizzo 
4426ce3ee1e7SLuigi Rizzo /*
4427f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
4428ce3ee1e7SLuigi Rizzo  *
4429ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
4430ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
4431ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
4432849bec0eSLuigi Rizzo  *
4433f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
4434f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
4435f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
44364bf50f18SLuigi Rizzo  *   (see netmap_notify)
44374bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
44384bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
4439f9790aebSLuigi Rizzo  */
444037e3a6d3SLuigi Rizzo int
444137e3a6d3SLuigi Rizzo netmap_common_irq(struct netmap_adapter *na, u_int q, u_int *work_done)
4442f9790aebSLuigi Rizzo {
4443f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
4444847bf383SLuigi Rizzo 	enum txrx t = (work_done ? NR_RX : NR_TX);
4445f9790aebSLuigi Rizzo 
4446f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
4447f9790aebSLuigi Rizzo 
4448b6e66be2SVincenzo Maffione 	if (netmap_debug & (NM_DEBUG_RXINTR|NM_DEBUG_TXINTR)) {
4449b6e66be2SVincenzo Maffione 	        nm_prlim(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
4450f9790aebSLuigi Rizzo 	}
4451f9790aebSLuigi Rizzo 
4452847bf383SLuigi Rizzo 	if (q >= nma_get_nrings(na, t))
445337e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS; // not a physical queue
4454847bf383SLuigi Rizzo 
44552ff91c17SVincenzo Maffione 	kring = NMR(na, t)[q];
4456847bf383SLuigi Rizzo 
445737e3a6d3SLuigi Rizzo 	if (kring->nr_mode == NKR_NETMAP_OFF) {
445837e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
445937e3a6d3SLuigi Rizzo 	}
446037e3a6d3SLuigi Rizzo 
4461847bf383SLuigi Rizzo 	if (t == NR_RX) {
4462f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
4463f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
4464f9790aebSLuigi Rizzo 	}
446537e3a6d3SLuigi Rizzo 
446637e3a6d3SLuigi Rizzo 	return kring->nm_notify(kring, 0);
4467f9790aebSLuigi Rizzo }
4468f9790aebSLuigi Rizzo 
446917885a7bSLuigi Rizzo 
4470f9790aebSLuigi Rizzo /*
4471f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
4472f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
4473f9790aebSLuigi Rizzo  *
447437e3a6d3SLuigi Rizzo  * If the card is not in netmap mode, simply return NM_IRQ_PASS,
4475ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
447637e3a6d3SLuigi Rizzo  * Otherwise call netmap_common_irq().
4477ce3ee1e7SLuigi Rizzo  *
4478ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
4479ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
4480ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
448137e3a6d3SLuigi Rizzo  * and return NR_IRQ_COMPLETED.
4482ce3ee1e7SLuigi Rizzo  *
4483ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
448437e3a6d3SLuigi Rizzo  * calls the proper forwarding routine.
44851a26580eSLuigi Rizzo  */
4486babc7c12SLuigi Rizzo int
4487ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
44881a26580eSLuigi Rizzo {
44894bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
44904bf50f18SLuigi Rizzo 
44914bf50f18SLuigi Rizzo 	/*
44924bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
44934bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
44944bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
44954bf50f18SLuigi Rizzo 	 * nm_native_on() here.
44964bf50f18SLuigi Rizzo 	 */
44974bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
449837e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
4499849bec0eSLuigi Rizzo 
45004bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
450175f4f3edSVincenzo Maffione 		nm_prdis("use regular interrupt");
450237e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
45038241616dSLuigi Rizzo 	}
45048241616dSLuigi Rizzo 
450537e3a6d3SLuigi Rizzo 	return netmap_common_irq(na, q, work_done);
45061a26580eSLuigi Rizzo }
45071a26580eSLuigi Rizzo 
45082a7db7a6SVincenzo Maffione /* set/clear native flags and if_transmit/netdev_ops */
45092a7db7a6SVincenzo Maffione void
45102a7db7a6SVincenzo Maffione nm_set_native_flags(struct netmap_adapter *na)
45112a7db7a6SVincenzo Maffione {
45122a7db7a6SVincenzo Maffione 	struct ifnet *ifp = na->ifp;
45132a7db7a6SVincenzo Maffione 
45142a7db7a6SVincenzo Maffione 	/* We do the setup for intercepting packets only if we are the
45151d238b07SVincenzo Maffione 	 * first user of this adapter. */
45162a7db7a6SVincenzo Maffione 	if (na->active_fds > 0) {
45172a7db7a6SVincenzo Maffione 		return;
45182a7db7a6SVincenzo Maffione 	}
45192a7db7a6SVincenzo Maffione 
45202a7db7a6SVincenzo Maffione 	na->na_flags |= NAF_NETMAP_ON;
45212a7db7a6SVincenzo Maffione 	nm_os_onenter(ifp);
452298399ab0SVincenzo Maffione 	netmap_update_hostrings_mode(na);
45232a7db7a6SVincenzo Maffione }
45242a7db7a6SVincenzo Maffione 
45252a7db7a6SVincenzo Maffione void
45262a7db7a6SVincenzo Maffione nm_clear_native_flags(struct netmap_adapter *na)
45272a7db7a6SVincenzo Maffione {
45282a7db7a6SVincenzo Maffione 	struct ifnet *ifp = na->ifp;
45292a7db7a6SVincenzo Maffione 
45302a7db7a6SVincenzo Maffione 	/* We undo the setup for intercepting packets only if we are the
4531b6e66be2SVincenzo Maffione 	 * last user of this adapter. */
45322a7db7a6SVincenzo Maffione 	if (na->active_fds > 0) {
45332a7db7a6SVincenzo Maffione 		return;
45342a7db7a6SVincenzo Maffione 	}
45352a7db7a6SVincenzo Maffione 
453698399ab0SVincenzo Maffione 	netmap_update_hostrings_mode(na);
45372a7db7a6SVincenzo Maffione 	nm_os_onexit(ifp);
45382a7db7a6SVincenzo Maffione 
45392a7db7a6SVincenzo Maffione 	na->na_flags &= ~NAF_NETMAP_ON;
45402a7db7a6SVincenzo Maffione }
45412a7db7a6SVincenzo Maffione 
454275f4f3edSVincenzo Maffione void
454375f4f3edSVincenzo Maffione netmap_krings_mode_commit(struct netmap_adapter *na, int onoff)
454475f4f3edSVincenzo Maffione {
454575f4f3edSVincenzo Maffione 	enum txrx t;
454675f4f3edSVincenzo Maffione 
454775f4f3edSVincenzo Maffione 	for_rx_tx(t) {
454875f4f3edSVincenzo Maffione 		int i;
454975f4f3edSVincenzo Maffione 
455075f4f3edSVincenzo Maffione 		for (i = 0; i < netmap_real_rings(na, t); i++) {
455175f4f3edSVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
455275f4f3edSVincenzo Maffione 
455375f4f3edSVincenzo Maffione 			if (onoff && nm_kring_pending_on(kring))
455475f4f3edSVincenzo Maffione 				kring->nr_mode = NKR_NETMAP_ON;
455575f4f3edSVincenzo Maffione 			else if (!onoff && nm_kring_pending_off(kring))
455675f4f3edSVincenzo Maffione 				kring->nr_mode = NKR_NETMAP_OFF;
455775f4f3edSVincenzo Maffione 		}
455875f4f3edSVincenzo Maffione 	}
455975f4f3edSVincenzo Maffione }
456075f4f3edSVincenzo Maffione 
456101c7d25fSLuigi Rizzo /*
4562f9790aebSLuigi Rizzo  * Module loader and unloader
4563f196ce38SLuigi Rizzo  *
4564f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
4565f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
4566f9790aebSLuigi Rizzo  * (but there is no chance)
4567f9790aebSLuigi Rizzo  *
4568f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
4569f196ce38SLuigi Rizzo  */
4570babc7c12SLuigi Rizzo 
4571babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
4572f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
4573babc7c12SLuigi Rizzo 
457417885a7bSLuigi Rizzo 
4575f9790aebSLuigi Rizzo void
457668b8534bSLuigi Rizzo netmap_fini(void)
457768b8534bSLuigi Rizzo {
4578f9790aebSLuigi Rizzo 	if (netmap_dev)
457968b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
458037e3a6d3SLuigi Rizzo 	/* we assume that there are no longer netmap users */
458137e3a6d3SLuigi Rizzo 	nm_os_ifnet_fini();
458237e3a6d3SLuigi Rizzo 	netmap_uninit_bridges();
4583ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
4584ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
4585b6e66be2SVincenzo Maffione 	nm_prinf("netmap: unloaded module.");
458668b8534bSLuigi Rizzo }
458768b8534bSLuigi Rizzo 
458817885a7bSLuigi Rizzo 
4589f9790aebSLuigi Rizzo int
4590f9790aebSLuigi Rizzo netmap_init(void)
459168b8534bSLuigi Rizzo {
4592f9790aebSLuigi Rizzo 	int error;
459368b8534bSLuigi Rizzo 
4594f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
459568b8534bSLuigi Rizzo 
4596f9790aebSLuigi Rizzo 	error = netmap_mem_init();
4597f9790aebSLuigi Rizzo 	if (error != 0)
4598f9790aebSLuigi Rizzo 		goto fail;
4599c929ca72SLuigi Rizzo 	/*
4600c929ca72SLuigi Rizzo 	 * MAKEDEV_ETERNAL_KLD avoids an expensive check on syscalls
4601c929ca72SLuigi Rizzo 	 * when the module is compiled in.
4602c929ca72SLuigi Rizzo 	 * XXX could use make_dev_credv() to get error number
4603c929ca72SLuigi Rizzo 	 */
46040e73f29aSLuigi Rizzo 	netmap_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
460511c0b69cSAdrian Chadd 		&netmap_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0600,
46060e73f29aSLuigi Rizzo 			      "netmap");
4607f9790aebSLuigi Rizzo 	if (!netmap_dev)
4608f9790aebSLuigi Rizzo 		goto fail;
4609f9790aebSLuigi Rizzo 
4610847bf383SLuigi Rizzo 	error = netmap_init_bridges();
4611847bf383SLuigi Rizzo 	if (error)
4612847bf383SLuigi Rizzo 		goto fail;
4613847bf383SLuigi Rizzo 
46144bf50f18SLuigi Rizzo #ifdef __FreeBSD__
461537e3a6d3SLuigi Rizzo 	nm_os_vi_init_index();
46164bf50f18SLuigi Rizzo #endif
4617847bf383SLuigi Rizzo 
461837e3a6d3SLuigi Rizzo 	error = nm_os_ifnet_init();
461937e3a6d3SLuigi Rizzo 	if (error)
462037e3a6d3SLuigi Rizzo 		goto fail;
462137e3a6d3SLuigi Rizzo 
4622fef84509SMark Johnston #if !defined(__FreeBSD__) || defined(KLD_MODULE)
4623b6e66be2SVincenzo Maffione 	nm_prinf("netmap: loaded module");
4624fef84509SMark Johnston #endif
4625f9790aebSLuigi Rizzo 	return (0);
4626f9790aebSLuigi Rizzo fail:
462768b8534bSLuigi Rizzo 	netmap_fini();
4628f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
462968b8534bSLuigi Rizzo }
4630