xref: /freebsd/sys/dev/netmap/netmap.c (revision 2a7db7a63de8af153b9a626780dd15ca26ae3596)
1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi
537e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Luigi Rizzo
637e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Giuseppe Lettieri
737e3a6d3SLuigi Rizzo  * Copyright (C) 2011-2016 Vincenzo Maffione
837e3a6d3SLuigi Rizzo  * All rights reserved.
968b8534bSLuigi Rizzo  *
1068b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
1168b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
1268b8534bSLuigi Rizzo  * are met:
1368b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
1468b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1568b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1668b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1768b8534bSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1868b8534bSLuigi Rizzo  *
1968b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2068b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2168b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2268b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2368b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2468b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2568b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2668b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2768b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2868b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2968b8534bSLuigi Rizzo  * SUCH DAMAGE.
3068b8534bSLuigi Rizzo  */
3168b8534bSLuigi Rizzo 
32ce3ee1e7SLuigi Rizzo 
3368b8534bSLuigi Rizzo /*
34f9790aebSLuigi Rizzo  * $FreeBSD$
35f9790aebSLuigi Rizzo  *
3668b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3768b8534bSLuigi Rizzo  * see netmap(4).
3868b8534bSLuigi Rizzo  *
3968b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
4068b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
4168b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
4268b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
4368b8534bSLuigi Rizzo  *
4468b8534bSLuigi Rizzo  * Access to the network card works like this:
4568b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
4668b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4768b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4868b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4968b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
5068b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
5168b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
5268b8534bSLuigi Rizzo  *    the shared memory region.
5368b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
5468b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
5568b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
5668b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5768b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5868b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5968b8534bSLuigi Rizzo  *    packets on the output interface.
6068b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
6168b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
62ce3ee1e7SLuigi Rizzo  *
63ce3ee1e7SLuigi Rizzo 
64ce3ee1e7SLuigi Rizzo 		SYNCHRONIZATION (USER)
65ce3ee1e7SLuigi Rizzo 
66ce3ee1e7SLuigi Rizzo The netmap rings and data structures may be shared among multiple
67ce3ee1e7SLuigi Rizzo user threads or even independent processes.
68ce3ee1e7SLuigi Rizzo Any synchronization among those threads/processes is delegated
69ce3ee1e7SLuigi Rizzo to the threads themselves. Only one thread at a time can be in
70ce3ee1e7SLuigi Rizzo a system call on the same netmap ring. The OS does not enforce
71ce3ee1e7SLuigi Rizzo this and only guarantees against system crashes in case of
72ce3ee1e7SLuigi Rizzo invalid usage.
73ce3ee1e7SLuigi Rizzo 
74ce3ee1e7SLuigi Rizzo 		LOCKING (INTERNAL)
75ce3ee1e7SLuigi Rizzo 
76ce3ee1e7SLuigi Rizzo Within the kernel, access to the netmap rings is protected as follows:
77ce3ee1e7SLuigi Rizzo 
78ce3ee1e7SLuigi Rizzo - a spinlock on each ring, to handle producer/consumer races on
79ce3ee1e7SLuigi Rizzo   RX rings attached to the host stack (against multiple host
80ce3ee1e7SLuigi Rizzo   threads writing from the host stack to the same ring),
81ce3ee1e7SLuigi Rizzo   and on 'destination' rings attached to a VALE switch
82ce3ee1e7SLuigi Rizzo   (i.e. RX rings in VALE ports, and TX rings in NIC/host ports)
83ce3ee1e7SLuigi Rizzo   protecting multiple active senders for the same destination)
84ce3ee1e7SLuigi Rizzo 
85ce3ee1e7SLuigi Rizzo - an atomic variable to guarantee that there is at most one
86ce3ee1e7SLuigi Rizzo   instance of *_*xsync() on the ring at any time.
87ce3ee1e7SLuigi Rizzo   For rings connected to user file
88ce3ee1e7SLuigi Rizzo   descriptors, an atomic_test_and_set() protects this, and the
89ce3ee1e7SLuigi Rizzo   lock on the ring is not actually used.
90ce3ee1e7SLuigi Rizzo   For NIC RX rings connected to a VALE switch, an atomic_test_and_set()
91ce3ee1e7SLuigi Rizzo   is also used to prevent multiple executions (the driver might indeed
92ce3ee1e7SLuigi Rizzo   already guarantee this).
93ce3ee1e7SLuigi Rizzo   For NIC TX rings connected to a VALE switch, the lock arbitrates
94ce3ee1e7SLuigi Rizzo   access to the queue (both when allocating buffers and when pushing
95ce3ee1e7SLuigi Rizzo   them out).
96ce3ee1e7SLuigi Rizzo 
97ce3ee1e7SLuigi Rizzo - *xsync() should be protected against initializations of the card.
98ce3ee1e7SLuigi Rizzo   On FreeBSD most devices have the reset routine protected by
99ce3ee1e7SLuigi Rizzo   a RING lock (ixgbe, igb, em) or core lock (re). lem is missing
100ce3ee1e7SLuigi Rizzo   the RING protection on rx_reset(), this should be added.
101ce3ee1e7SLuigi Rizzo 
102ce3ee1e7SLuigi Rizzo   On linux there is an external lock on the tx path, which probably
103ce3ee1e7SLuigi Rizzo   also arbitrates access to the reset routine. XXX to be revised
104ce3ee1e7SLuigi Rizzo 
105ce3ee1e7SLuigi Rizzo - a per-interface core_lock protecting access from the host stack
106ce3ee1e7SLuigi Rizzo   while interfaces may be detached from netmap mode.
107ce3ee1e7SLuigi Rizzo   XXX there should be no need for this lock if we detach the interfaces
108ce3ee1e7SLuigi Rizzo   only while they are down.
109ce3ee1e7SLuigi Rizzo 
110ce3ee1e7SLuigi Rizzo 
111ce3ee1e7SLuigi Rizzo --- VALE SWITCH ---
112ce3ee1e7SLuigi Rizzo 
113ce3ee1e7SLuigi Rizzo NMG_LOCK() serializes all modifications to switches and ports.
114ce3ee1e7SLuigi Rizzo A switch cannot be deleted until all ports are gone.
115ce3ee1e7SLuigi Rizzo 
116ce3ee1e7SLuigi Rizzo For each switch, an SX lock (RWlock on linux) protects
117ce3ee1e7SLuigi Rizzo deletion of ports. When configuring or deleting a new port, the
118ce3ee1e7SLuigi Rizzo lock is acquired in exclusive mode (after holding NMG_LOCK).
119ce3ee1e7SLuigi Rizzo When forwarding, the lock is acquired in shared mode (without NMG_LOCK).
120ce3ee1e7SLuigi Rizzo The lock is held throughout the entire forwarding cycle,
121ce3ee1e7SLuigi Rizzo during which the thread may incur in a page fault.
122ce3ee1e7SLuigi Rizzo Hence it is important that sleepable shared locks are used.
123ce3ee1e7SLuigi Rizzo 
124ce3ee1e7SLuigi Rizzo On the rx ring, the per-port lock is grabbed initially to reserve
125ce3ee1e7SLuigi Rizzo a number of slot in the ring, then the lock is released,
126ce3ee1e7SLuigi Rizzo packets are copied from source to destination, and then
127ce3ee1e7SLuigi Rizzo the lock is acquired again and the receive ring is updated.
128ce3ee1e7SLuigi Rizzo (A similar thing is done on the tx ring for NIC and host stack
129ce3ee1e7SLuigi Rizzo ports attached to the switch)
130ce3ee1e7SLuigi Rizzo 
13168b8534bSLuigi Rizzo  */
13268b8534bSLuigi Rizzo 
1334bf50f18SLuigi Rizzo 
1344bf50f18SLuigi Rizzo /* --- internals ----
1354bf50f18SLuigi Rizzo  *
1364bf50f18SLuigi Rizzo  * Roadmap to the code that implements the above.
1374bf50f18SLuigi Rizzo  *
1384bf50f18SLuigi Rizzo  * > 1. a process/thread issues one or more open() on /dev/netmap, to create
1394bf50f18SLuigi Rizzo  * >    select()able file descriptor on which events are reported.
1404bf50f18SLuigi Rizzo  *
1414bf50f18SLuigi Rizzo  *  	Internally, we allocate a netmap_priv_d structure, that will be
14237e3a6d3SLuigi Rizzo  *  	initialized on ioctl(NIOCREGIF). There is one netmap_priv_d
14337e3a6d3SLuigi Rizzo  *  	structure for each open().
1444bf50f18SLuigi Rizzo  *
1454bf50f18SLuigi Rizzo  *      os-specific:
14637e3a6d3SLuigi Rizzo  *  	    FreeBSD: see netmap_open() (netmap_freebsd.c)
14737e3a6d3SLuigi Rizzo  *  	    linux:   see linux_netmap_open() (netmap_linux.c)
1484bf50f18SLuigi Rizzo  *
1494bf50f18SLuigi Rizzo  * > 2. on each descriptor, the process issues an ioctl() to identify
1504bf50f18SLuigi Rizzo  * >    the interface that should report events to the file descriptor.
1514bf50f18SLuigi Rizzo  *
1524bf50f18SLuigi Rizzo  * 	Implemented by netmap_ioctl(), NIOCREGIF case, with nmr->nr_cmd==0.
1534bf50f18SLuigi Rizzo  * 	Most important things happen in netmap_get_na() and
1544bf50f18SLuigi Rizzo  * 	netmap_do_regif(), called from there. Additional details can be
1554bf50f18SLuigi Rizzo  * 	found in the comments above those functions.
1564bf50f18SLuigi Rizzo  *
1574bf50f18SLuigi Rizzo  * 	In all cases, this action creates/takes-a-reference-to a
1584bf50f18SLuigi Rizzo  * 	netmap_*_adapter describing the port, and allocates a netmap_if
1594bf50f18SLuigi Rizzo  * 	and all necessary netmap rings, filling them with netmap buffers.
1604bf50f18SLuigi Rizzo  *
1614bf50f18SLuigi Rizzo  *      In this phase, the sync callbacks for each ring are set (these are used
1624bf50f18SLuigi Rizzo  *      in steps 5 and 6 below).  The callbacks depend on the type of adapter.
1634bf50f18SLuigi Rizzo  *      The adapter creation/initialization code puts them in the
1644bf50f18SLuigi Rizzo  * 	netmap_adapter (fields na->nm_txsync and na->nm_rxsync).  Then, they
1654bf50f18SLuigi Rizzo  * 	are copied from there to the netmap_kring's during netmap_do_regif(), by
1664bf50f18SLuigi Rizzo  * 	the nm_krings_create() callback.  All the nm_krings_create callbacks
1674bf50f18SLuigi Rizzo  * 	actually call netmap_krings_create() to perform this and the other
1684bf50f18SLuigi Rizzo  * 	common stuff. netmap_krings_create() also takes care of the host rings,
1694bf50f18SLuigi Rizzo  * 	if needed, by setting their sync callbacks appropriately.
1704bf50f18SLuigi Rizzo  *
1714bf50f18SLuigi Rizzo  * 	Additional actions depend on the kind of netmap_adapter that has been
1724bf50f18SLuigi Rizzo  * 	registered:
1734bf50f18SLuigi Rizzo  *
1744bf50f18SLuigi Rizzo  * 	- netmap_hw_adapter:  	     [netmap.c]
1754bf50f18SLuigi Rizzo  * 	     This is a system netdev/ifp with native netmap support.
1764bf50f18SLuigi Rizzo  * 	     The ifp is detached from the host stack by redirecting:
1774bf50f18SLuigi Rizzo  * 	       - transmissions (from the network stack) to netmap_transmit()
1784bf50f18SLuigi Rizzo  * 	       - receive notifications to the nm_notify() callback for
1794bf50f18SLuigi Rizzo  * 	         this adapter. The callback is normally netmap_notify(), unless
1804bf50f18SLuigi Rizzo  * 	         the ifp is attached to a bridge using bwrap, in which case it
1814bf50f18SLuigi Rizzo  * 	         is netmap_bwrap_intr_notify().
1824bf50f18SLuigi Rizzo  *
1834bf50f18SLuigi Rizzo  * 	- netmap_generic_adapter:      [netmap_generic.c]
1844bf50f18SLuigi Rizzo  * 	      A system netdev/ifp without native netmap support.
1854bf50f18SLuigi Rizzo  *
1864bf50f18SLuigi Rizzo  * 	(the decision about native/non native support is taken in
1874bf50f18SLuigi Rizzo  * 	 netmap_get_hw_na(), called by netmap_get_na())
1884bf50f18SLuigi Rizzo  *
1894bf50f18SLuigi Rizzo  * 	- netmap_vp_adapter 		[netmap_vale.c]
1904bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_bdg_na().
1914bf50f18SLuigi Rizzo  * 	      This is a persistent or ephemeral VALE port. Ephemeral ports
1924bf50f18SLuigi Rizzo  * 	      are created on the fly if they don't already exist, and are
1934bf50f18SLuigi Rizzo  * 	      always attached to a bridge.
194453130d9SPedro F. Giffuni  * 	      Persistent VALE ports must must be created separately, and i
1954bf50f18SLuigi Rizzo  * 	      then attached like normal NICs. The NIOCREGIF we are examining
1964bf50f18SLuigi Rizzo  * 	      will find them only if they had previosly been created and
1974bf50f18SLuigi Rizzo  * 	      attached (see VALE_CTL below).
1984bf50f18SLuigi Rizzo  *
1994bf50f18SLuigi Rizzo  * 	- netmap_pipe_adapter 	      [netmap_pipe.c]
2004bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_pipe_na().
2014bf50f18SLuigi Rizzo  * 	      Both pipe ends are created, if they didn't already exist.
2024bf50f18SLuigi Rizzo  *
2034bf50f18SLuigi Rizzo  * 	- netmap_monitor_adapter      [netmap_monitor.c]
2044bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_monitor_na().
2054bf50f18SLuigi Rizzo  * 	      If successful, the nm_sync callbacks of the monitored adapter
2064bf50f18SLuigi Rizzo  * 	      will be intercepted by the returned monitor.
2074bf50f18SLuigi Rizzo  *
2084bf50f18SLuigi Rizzo  * 	- netmap_bwrap_adapter	      [netmap_vale.c]
2094bf50f18SLuigi Rizzo  * 	      Cannot be obtained in this way, see VALE_CTL below
2104bf50f18SLuigi Rizzo  *
2114bf50f18SLuigi Rizzo  *
2124bf50f18SLuigi Rizzo  * 	os-specific:
2134bf50f18SLuigi Rizzo  * 	    linux: we first go through linux_netmap_ioctl() to
2144bf50f18SLuigi Rizzo  * 	           adapt the FreeBSD interface to the linux one.
2154bf50f18SLuigi Rizzo  *
2164bf50f18SLuigi Rizzo  *
2174bf50f18SLuigi Rizzo  * > 3. on each descriptor, the process issues an mmap() request to
2184bf50f18SLuigi Rizzo  * >    map the shared memory region within the process' address space.
2194bf50f18SLuigi Rizzo  * >    The list of interesting queues is indicated by a location in
2204bf50f18SLuigi Rizzo  * >    the shared memory region.
2214bf50f18SLuigi Rizzo  *
2224bf50f18SLuigi Rizzo  *      os-specific:
2234bf50f18SLuigi Rizzo  *  	    FreeBSD: netmap_mmap_single (netmap_freebsd.c).
2244bf50f18SLuigi Rizzo  *  	    linux:   linux_netmap_mmap (netmap_linux.c).
2254bf50f18SLuigi Rizzo  *
2264bf50f18SLuigi Rizzo  * > 4. using the functions in the netmap(4) userspace API, a process
2274bf50f18SLuigi Rizzo  * >    can look up the occupation state of a queue, access memory buffers,
2284bf50f18SLuigi Rizzo  * >    and retrieve received packets or enqueue packets to transmit.
2294bf50f18SLuigi Rizzo  *
2304bf50f18SLuigi Rizzo  * 	these actions do not involve the kernel.
2314bf50f18SLuigi Rizzo  *
2324bf50f18SLuigi Rizzo  * > 5. using some ioctl()s the process can synchronize the userspace view
2334bf50f18SLuigi Rizzo  * >    of the queue with the actual status in the kernel. This includes both
2344bf50f18SLuigi Rizzo  * >    receiving the notification of new packets, and transmitting new
2354bf50f18SLuigi Rizzo  * >    packets on the output interface.
2364bf50f18SLuigi Rizzo  *
2374bf50f18SLuigi Rizzo  * 	These are implemented in netmap_ioctl(), NIOCTXSYNC and NIOCRXSYNC
2384bf50f18SLuigi Rizzo  * 	cases. They invoke the nm_sync callbacks on the netmap_kring
2394bf50f18SLuigi Rizzo  * 	structures, as initialized in step 2 and maybe later modified
2404bf50f18SLuigi Rizzo  * 	by a monitor. Monitors, however, will always call the original
2414bf50f18SLuigi Rizzo  * 	callback before doing anything else.
2424bf50f18SLuigi Rizzo  *
2434bf50f18SLuigi Rizzo  *
2444bf50f18SLuigi Rizzo  * > 6. select() or poll() can be used to wait for events on individual
2454bf50f18SLuigi Rizzo  * >    transmit or receive queues (or all queues for a given interface).
2464bf50f18SLuigi Rizzo  *
2474bf50f18SLuigi Rizzo  * 	Implemented in netmap_poll(). This will call the same nm_sync()
2484bf50f18SLuigi Rizzo  * 	callbacks as in step 5 above.
2494bf50f18SLuigi Rizzo  *
2504bf50f18SLuigi Rizzo  * 	os-specific:
2514bf50f18SLuigi Rizzo  * 		linux: we first go through linux_netmap_poll() to adapt
2524bf50f18SLuigi Rizzo  * 		       the FreeBSD interface to the linux one.
2534bf50f18SLuigi Rizzo  *
2544bf50f18SLuigi Rizzo  *
2554bf50f18SLuigi Rizzo  *  ----  VALE_CTL -----
2564bf50f18SLuigi Rizzo  *
2574bf50f18SLuigi Rizzo  *  VALE switches are controlled by issuing a NIOCREGIF with a non-null
2584bf50f18SLuigi Rizzo  *  nr_cmd in the nmreq structure. These subcommands are handled by
2594bf50f18SLuigi Rizzo  *  netmap_bdg_ctl() in netmap_vale.c. Persistent VALE ports are created
2604bf50f18SLuigi Rizzo  *  and destroyed by issuing the NETMAP_BDG_NEWIF and NETMAP_BDG_DELIF
2614bf50f18SLuigi Rizzo  *  subcommands, respectively.
2624bf50f18SLuigi Rizzo  *
2634bf50f18SLuigi Rizzo  *  Any network interface known to the system (including a persistent VALE
2644bf50f18SLuigi Rizzo  *  port) can be attached to a VALE switch by issuing the
2652ff91c17SVincenzo Maffione  *  NETMAP_REQ_VALE_ATTACH command. After the attachment, persistent VALE ports
2664bf50f18SLuigi Rizzo  *  look exactly like ephemeral VALE ports (as created in step 2 above).  The
2674bf50f18SLuigi Rizzo  *  attachment of other interfaces, instead, requires the creation of a
2684bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  Moreover, the attached interface must be put in
2694bf50f18SLuigi Rizzo  *  netmap mode. This may require the creation of a netmap_generic_adapter if
2704bf50f18SLuigi Rizzo  *  we have no native support for the interface, or if generic adapters have
2714bf50f18SLuigi Rizzo  *  been forced by sysctl.
2724bf50f18SLuigi Rizzo  *
2734bf50f18SLuigi Rizzo  *  Both persistent VALE ports and bwraps are handled by netmap_get_bdg_na(),
2744bf50f18SLuigi Rizzo  *  called by nm_bdg_ctl_attach(), and discriminated by the nm_bdg_attach()
2754bf50f18SLuigi Rizzo  *  callback.  In the case of the bwrap, the callback creates the
2764bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  The initialization of the bwrap is then
2774bf50f18SLuigi Rizzo  *  completed by calling netmap_do_regif() on it, in the nm_bdg_ctl()
2784bf50f18SLuigi Rizzo  *  callback (netmap_bwrap_bdg_ctl in netmap_vale.c).
2794bf50f18SLuigi Rizzo  *  A generic adapter for the wrapped ifp will be created if needed, when
2804bf50f18SLuigi Rizzo  *  netmap_get_bdg_na() calls netmap_get_hw_na().
2814bf50f18SLuigi Rizzo  *
2824bf50f18SLuigi Rizzo  *
2834bf50f18SLuigi Rizzo  *  ---- DATAPATHS -----
2844bf50f18SLuigi Rizzo  *
2854bf50f18SLuigi Rizzo  *              -= SYSTEM DEVICE WITH NATIVE SUPPORT =-
2864bf50f18SLuigi Rizzo  *
2874bf50f18SLuigi Rizzo  *    na == NA(ifp) == netmap_hw_adapter created in DEVICE_netmap_attach()
2884bf50f18SLuigi Rizzo  *
2894bf50f18SLuigi Rizzo  *    - tx from netmap userspace:
2904bf50f18SLuigi Rizzo  *	 concurrently:
2914bf50f18SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
2924bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_txsync()
2934bf50f18SLuigi Rizzo  *           2) device interrupt handler
2944bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
2954bf50f18SLuigi Rizzo  *    - rx from netmap userspace:
2964bf50f18SLuigi Rizzo  *       concurrently:
2974bf50f18SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
2984bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_rxsync()
2994bf50f18SLuigi Rizzo  *           2) device interrupt handler
3004bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
301847bf383SLuigi Rizzo  *    - rx from host stack
3024bf50f18SLuigi Rizzo  *       concurrently:
3034bf50f18SLuigi Rizzo  *           1) host stack
3044bf50f18SLuigi Rizzo  *                netmap_transmit()
3054bf50f18SLuigi Rizzo  *                  na->nm_notify  == netmap_notify()
3064bf50f18SLuigi Rizzo  *           2) ioctl(NIOCRXSYNC)/netmap_poll() in process context
30737e3a6d3SLuigi Rizzo  *                kring->nm_sync() == netmap_rxsync_from_host
3084bf50f18SLuigi Rizzo  *                  netmap_rxsync_from_host(na, NULL, NULL)
3094bf50f18SLuigi Rizzo  *    - tx to host stack
3104bf50f18SLuigi Rizzo  *           ioctl(NIOCTXSYNC)/netmap_poll() in process context
31137e3a6d3SLuigi Rizzo  *             kring->nm_sync() == netmap_txsync_to_host
3124bf50f18SLuigi Rizzo  *               netmap_txsync_to_host(na)
31337e3a6d3SLuigi Rizzo  *                 nm_os_send_up()
31437e3a6d3SLuigi Rizzo  *                   FreeBSD: na->if_input() == ether_input()
3154bf50f18SLuigi Rizzo  *                   linux: netif_rx() with NM_MAGIC_PRIORITY_RX
3164bf50f18SLuigi Rizzo  *
3174bf50f18SLuigi Rizzo  *
3184bf50f18SLuigi Rizzo  *               -= SYSTEM DEVICE WITH GENERIC SUPPORT =-
3194bf50f18SLuigi Rizzo  *
320847bf383SLuigi Rizzo  *    na == NA(ifp) == generic_netmap_adapter created in generic_netmap_attach()
321847bf383SLuigi Rizzo  *
322847bf383SLuigi Rizzo  *    - tx from netmap userspace:
323847bf383SLuigi Rizzo  *       concurrently:
324847bf383SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
325847bf383SLuigi Rizzo  *               kring->nm_sync() == generic_netmap_txsync()
32637e3a6d3SLuigi Rizzo  *                   nm_os_generic_xmit_frame()
327847bf383SLuigi Rizzo  *                       linux:   dev_queue_xmit() with NM_MAGIC_PRIORITY_TX
32837e3a6d3SLuigi Rizzo  *                           ifp->ndo_start_xmit == generic_ndo_start_xmit()
32937e3a6d3SLuigi Rizzo  *                               gna->save_start_xmit == orig. dev. start_xmit
330847bf383SLuigi Rizzo  *                       FreeBSD: na->if_transmit() == orig. dev if_transmit
331847bf383SLuigi Rizzo  *           2) generic_mbuf_destructor()
332847bf383SLuigi Rizzo  *                   na->nm_notify() == netmap_notify()
333847bf383SLuigi Rizzo  *    - rx from netmap userspace:
334847bf383SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
335847bf383SLuigi Rizzo  *               kring->nm_sync() == generic_netmap_rxsync()
336847bf383SLuigi Rizzo  *                   mbq_safe_dequeue()
337847bf383SLuigi Rizzo  *           2) device driver
338847bf383SLuigi Rizzo  *               generic_rx_handler()
339847bf383SLuigi Rizzo  *                   mbq_safe_enqueue()
340847bf383SLuigi Rizzo  *                   na->nm_notify() == netmap_notify()
34137e3a6d3SLuigi Rizzo  *    - rx from host stack
34237e3a6d3SLuigi Rizzo  *        FreeBSD: same as native
34337e3a6d3SLuigi Rizzo  *        Linux: same as native except:
344847bf383SLuigi Rizzo  *           1) host stack
34537e3a6d3SLuigi Rizzo  *               dev_queue_xmit() without NM_MAGIC_PRIORITY_TX
34637e3a6d3SLuigi Rizzo  *                   ifp->ndo_start_xmit == generic_ndo_start_xmit()
347847bf383SLuigi Rizzo  *                       netmap_transmit()
348847bf383SLuigi Rizzo  *                           na->nm_notify() == netmap_notify()
34937e3a6d3SLuigi Rizzo  *    - tx to host stack (same as native):
3504bf50f18SLuigi Rizzo  *
3514bf50f18SLuigi Rizzo  *
352847bf383SLuigi Rizzo  *                           -= VALE =-
3534bf50f18SLuigi Rizzo  *
354847bf383SLuigi Rizzo  *   INCOMING:
3554bf50f18SLuigi Rizzo  *
356847bf383SLuigi Rizzo  *      - VALE ports:
357847bf383SLuigi Rizzo  *          ioctl(NIOCTXSYNC)/netmap_poll() in process context
358847bf383SLuigi Rizzo  *              kring->nm_sync() == netmap_vp_txsync()
3594bf50f18SLuigi Rizzo  *
360847bf383SLuigi Rizzo  *      - system device with native support:
361847bf383SLuigi Rizzo  *         from cable:
362847bf383SLuigi Rizzo  *             interrupt
363847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr != host ring)
364847bf383SLuigi Rizzo  *                     kring->nm_sync() == DEVICE_netmap_rxsync()
365847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
366847bf383SLuigi Rizzo  *                     kring->nm_sync() == DEVICE_netmap_rxsync()
367847bf383SLuigi Rizzo  *         from host stack:
368847bf383SLuigi Rizzo  *             netmap_transmit()
369847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr == host ring)
37037e3a6d3SLuigi Rizzo  *                     kring->nm_sync() == netmap_rxsync_from_host()
371847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
3724bf50f18SLuigi Rizzo  *
373847bf383SLuigi Rizzo  *      - system device with generic support:
374847bf383SLuigi Rizzo  *         from device driver:
375847bf383SLuigi Rizzo  *            generic_rx_handler()
376847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr != host ring)
377847bf383SLuigi Rizzo  *                     kring->nm_sync() == generic_netmap_rxsync()
378847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
379847bf383SLuigi Rizzo  *                     kring->nm_sync() == generic_netmap_rxsync()
380847bf383SLuigi Rizzo  *         from host stack:
381847bf383SLuigi Rizzo  *            netmap_transmit()
382847bf383SLuigi Rizzo  *                na->nm_notify() == netmap_bwrap_intr_notify(ring_nr == host ring)
38337e3a6d3SLuigi Rizzo  *                     kring->nm_sync() == netmap_rxsync_from_host()
384847bf383SLuigi Rizzo  *                     netmap_vp_txsync()
3854bf50f18SLuigi Rizzo  *
386847bf383SLuigi Rizzo  *   (all cases) --> nm_bdg_flush()
387847bf383SLuigi Rizzo  *                      dest_na->nm_notify() == (see below)
3884bf50f18SLuigi Rizzo  *
389847bf383SLuigi Rizzo  *   OUTGOING:
3904bf50f18SLuigi Rizzo  *
391847bf383SLuigi Rizzo  *      - VALE ports:
392847bf383SLuigi Rizzo  *         concurrently:
393c3e9b4dbSLuiz Otavio O Souza  *             1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
394847bf383SLuigi Rizzo  *                    kring->nm_sync() == netmap_vp_rxsync()
395847bf383SLuigi Rizzo  *             2) from nm_bdg_flush()
396847bf383SLuigi Rizzo  *                    na->nm_notify() == netmap_notify()
3974bf50f18SLuigi Rizzo  *
398847bf383SLuigi Rizzo  *      - system device with native support:
399847bf383SLuigi Rizzo  *          to cable:
400847bf383SLuigi Rizzo  *             na->nm_notify() == netmap_bwrap_notify()
401847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
402847bf383SLuigi Rizzo  *                 kring->nm_sync() == DEVICE_netmap_txsync()
403847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
404847bf383SLuigi Rizzo  *          to host stack:
405847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
40637e3a6d3SLuigi Rizzo  *                 kring->nm_sync() == netmap_txsync_to_host
407847bf383SLuigi Rizzo  *                 netmap_vp_rxsync_locked()
4084bf50f18SLuigi Rizzo  *
409847bf383SLuigi Rizzo  *      - system device with generic adapter:
410847bf383SLuigi Rizzo  *          to device driver:
411847bf383SLuigi Rizzo  *             na->nm_notify() == netmap_bwrap_notify()
412847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
413847bf383SLuigi Rizzo  *                 kring->nm_sync() == generic_netmap_txsync()
414847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
415847bf383SLuigi Rizzo  *          to host stack:
416847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
41737e3a6d3SLuigi Rizzo  *                 kring->nm_sync() == netmap_txsync_to_host
418847bf383SLuigi Rizzo  *                 netmap_vp_rxsync()
4194bf50f18SLuigi Rizzo  *
4204bf50f18SLuigi Rizzo  */
4214bf50f18SLuigi Rizzo 
422ce3ee1e7SLuigi Rizzo /*
423ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
424ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
425ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
426ce3ee1e7SLuigi Rizzo  */
42701c7d25fSLuigi Rizzo 
428ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
42968b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
43068b8534bSLuigi Rizzo #include <sys/types.h>
43168b8534bSLuigi Rizzo #include <sys/errno.h>
43268b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
43368b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
434f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
43589e3fd52SLuigi Rizzo #include <sys/filio.h>	/* FIONBIO */
43668b8534bSLuigi Rizzo #include <sys/sockio.h>
43768b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
43868b8534bSLuigi Rizzo #include <sys/malloc.h>
43968b8534bSLuigi Rizzo #include <sys/poll.h>
44089f6b863SAttilio Rao #include <sys/rwlock.h>
44168b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44268b8534bSLuigi Rizzo #include <sys/selinfo.h>
44368b8534bSLuigi Rizzo #include <sys/sysctl.h>
444339f59c0SGleb Smirnoff #include <sys/jail.h>
445339f59c0SGleb Smirnoff #include <net/vnet.h>
44668b8534bSLuigi Rizzo #include <net/if.h>
44776039bc8SGleb Smirnoff #include <net/if_var.h>
44868b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
44968b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
450ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
451ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
45268b8534bSLuigi Rizzo 
45368b8534bSLuigi Rizzo 
454ce3ee1e7SLuigi Rizzo #elif defined(linux)
455ce3ee1e7SLuigi Rizzo 
456ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
457ce3ee1e7SLuigi Rizzo 
458ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
459ce3ee1e7SLuigi Rizzo 
460ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
461ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
462ce3ee1e7SLuigi Rizzo 
46337e3a6d3SLuigi Rizzo #elif defined (_WIN32)
46437e3a6d3SLuigi Rizzo 
46537e3a6d3SLuigi Rizzo #include "win_glue.h"
46637e3a6d3SLuigi Rizzo 
467ce3ee1e7SLuigi Rizzo #else
468ce3ee1e7SLuigi Rizzo 
469ce3ee1e7SLuigi Rizzo #error	Unsupported platform
470ce3ee1e7SLuigi Rizzo 
471ce3ee1e7SLuigi Rizzo #endif /* unsupported */
472ce3ee1e7SLuigi Rizzo 
473ce3ee1e7SLuigi Rizzo /*
474ce3ee1e7SLuigi Rizzo  * common headers
475ce3ee1e7SLuigi Rizzo  */
4760b8ed8e0SLuigi Rizzo #include <net/netmap.h>
4770b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
478ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
4790b8ed8e0SLuigi Rizzo 
480ce3ee1e7SLuigi Rizzo 
4815819da83SLuigi Rizzo /* user-controlled variables */
4825819da83SLuigi Rizzo int netmap_verbose;
4835819da83SLuigi Rizzo 
4845819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
485c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
486f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
487c3e9b4dbSLuiz Otavio O Souza static int netmap_fwd = 0;	/* force transparent forwarding */
488f196ce38SLuigi Rizzo 
489f9790aebSLuigi Rizzo /*
490f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
491f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
492f9790aebSLuigi Rizzo  */
493f9790aebSLuigi Rizzo enum {	NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
494f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
495f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
496f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
497f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
498f9790aebSLuigi Rizzo 
49937e3a6d3SLuigi Rizzo /* netmap_generic_mit controls mitigation of RX notifications for
50037e3a6d3SLuigi Rizzo  * the generic netmap adapter. The value is a time interval in
50137e3a6d3SLuigi Rizzo  * nanoseconds. */
50237e3a6d3SLuigi Rizzo int netmap_generic_mit = 100*1000;
50337e3a6d3SLuigi Rizzo 
50437e3a6d3SLuigi Rizzo /* We use by default netmap-aware qdiscs with generic netmap adapters,
50537e3a6d3SLuigi Rizzo  * even if there can be a little performance hit with hardware NICs.
50637e3a6d3SLuigi Rizzo  * However, using the qdisc is the safer approach, for two reasons:
50737e3a6d3SLuigi Rizzo  * 1) it prevents non-fifo qdiscs to break the TX notification
50837e3a6d3SLuigi Rizzo  *    scheme, which is based on mbuf destructors when txqdisc is
50937e3a6d3SLuigi Rizzo  *    not used.
51037e3a6d3SLuigi Rizzo  * 2) it makes it possible to transmit over software devices that
51137e3a6d3SLuigi Rizzo  *    change skb->dev, like bridge, veth, ...
51237e3a6d3SLuigi Rizzo  *
51337e3a6d3SLuigi Rizzo  * Anyway users looking for the best performance should
51437e3a6d3SLuigi Rizzo  * use native adapters.
51537e3a6d3SLuigi Rizzo  */
5164f80b14cSVincenzo Maffione #ifdef linux
51737e3a6d3SLuigi Rizzo int netmap_generic_txqdisc = 1;
5184f80b14cSVincenzo Maffione #endif
51937e3a6d3SLuigi Rizzo 
52037e3a6d3SLuigi Rizzo /* Default number of slots and queues for generic adapters. */
52137e3a6d3SLuigi Rizzo int netmap_generic_ringsize = 1024;
52237e3a6d3SLuigi Rizzo int netmap_generic_rings = 1;
52337e3a6d3SLuigi Rizzo 
524*2a7db7a6SVincenzo Maffione /* Non-zero to enable checksum offloading in NIC drivers */
525*2a7db7a6SVincenzo Maffione int netmap_generic_hwcsum = 0;
526*2a7db7a6SVincenzo Maffione 
52737e3a6d3SLuigi Rizzo /* Non-zero if ptnet devices are allowed to use virtio-net headers. */
52837e3a6d3SLuigi Rizzo int ptnet_vnet_hdr = 1;
52937e3a6d3SLuigi Rizzo 
530c3e9b4dbSLuiz Otavio O Souza /* 0 if ptnetmap should not use worker threads for TX processing */
531c3e9b4dbSLuiz Otavio O Souza int ptnetmap_tx_workers = 1;
532c3e9b4dbSLuiz Otavio O Souza 
53337e3a6d3SLuigi Rizzo /*
53437e3a6d3SLuigi Rizzo  * SYSCTL calls are grouped between SYSBEGIN and SYSEND to be emulated
53537e3a6d3SLuigi Rizzo  * in some other operating systems
53637e3a6d3SLuigi Rizzo  */
53737e3a6d3SLuigi Rizzo SYSBEGIN(main_init);
53837e3a6d3SLuigi Rizzo 
53937e3a6d3SLuigi Rizzo SYSCTL_DECL(_dev_netmap);
54037e3a6d3SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
54137e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
54237e3a6d3SLuigi Rizzo 		CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
54337e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
54437e3a6d3SLuigi Rizzo 		CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
5454f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr, CTLFLAG_RW, &netmap_no_pendintr,
5464f80b14cSVincenzo Maffione 		0, "Always look for new received packets.");
54737e3a6d3SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
54837e3a6d3SLuigi Rizzo 		&netmap_txsync_retry, 0, "Number of txsync loops in bridge's flush.");
549f9790aebSLuigi Rizzo 
5504f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0,
5514f80b14cSVincenzo Maffione 		"Force NR_FORWARD mode");
5524f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0,
5534f80b14cSVincenzo Maffione 		"Adapter mode. 0 selects the best option available,"
5544f80b14cSVincenzo Maffione 		"1 forces native adapter, 2 forces emulated adapter");
555*2a7db7a6SVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_hwcsum, CTLFLAG_RW, &netmap_generic_hwcsum,
556*2a7db7a6SVincenzo Maffione 		0, "Hardware checksums. 0 to disable checksum generation by the NIC (default),"
557*2a7db7a6SVincenzo Maffione 		"1 to enable checksum generation by the NIC");
5584f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit,
5594f80b14cSVincenzo Maffione 		0, "RX notification interval in nanoseconds");
5604f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW,
5614f80b14cSVincenzo Maffione 		&netmap_generic_ringsize, 0,
5624f80b14cSVincenzo Maffione 		"Number of per-ring slots for emulated netmap mode");
5634f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW,
5644f80b14cSVincenzo Maffione 		&netmap_generic_rings, 0,
5654f80b14cSVincenzo Maffione 		"Number of TX/RX queues for emulated netmap adapters");
5664f80b14cSVincenzo Maffione #ifdef linux
5674f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, generic_txqdisc, CTLFLAG_RW,
5684f80b14cSVincenzo Maffione 		&netmap_generic_txqdisc, 0, "Use qdisc for generic adapters");
5694f80b14cSVincenzo Maffione #endif
5704f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, ptnet_vnet_hdr, CTLFLAG_RW, &ptnet_vnet_hdr,
5714f80b14cSVincenzo Maffione 		0, "Allow ptnet devices to use virtio-net headers");
5724f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, ptnetmap_tx_workers, CTLFLAG_RW,
5734f80b14cSVincenzo Maffione 		&ptnetmap_tx_workers, 0, "Use worker threads for pnetmap TX processing");
57437e3a6d3SLuigi Rizzo 
57537e3a6d3SLuigi Rizzo SYSEND;
576f196ce38SLuigi Rizzo 
577ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
578ce3ee1e7SLuigi Rizzo 
57917885a7bSLuigi Rizzo /*
58017885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
58117885a7bSLuigi Rizzo  * to make sure other users get to see it.
58237e3a6d3SLuigi Rizzo  * stopped must be either NR_KR_STOPPED (for unbounded stop)
58337e3a6d3SLuigi Rizzo  * of NR_KR_LOCKED (brief stop for mutual exclusion purposes)
58417885a7bSLuigi Rizzo  */
5854bf50f18SLuigi Rizzo static void
58637e3a6d3SLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr, int stopped)
587ce3ee1e7SLuigi Rizzo {
58837e3a6d3SLuigi Rizzo 	nm_kr_stop(kr, stopped);
58937e3a6d3SLuigi Rizzo 	// XXX check if nm_kr_stop is sufficient
590ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
591ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
592ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
593ce3ee1e7SLuigi Rizzo }
594ce3ee1e7SLuigi Rizzo 
595847bf383SLuigi Rizzo /* stop or enable a single ring */
5964bf50f18SLuigi Rizzo void
597847bf383SLuigi Rizzo netmap_set_ring(struct netmap_adapter *na, u_int ring_id, enum txrx t, int stopped)
5984bf50f18SLuigi Rizzo {
5994bf50f18SLuigi Rizzo 	if (stopped)
6002ff91c17SVincenzo Maffione 		netmap_disable_ring(NMR(na, t)[ring_id], stopped);
6014bf50f18SLuigi Rizzo 	else
6022ff91c17SVincenzo Maffione 		NMR(na, t)[ring_id]->nkr_stopped = 0;
6034bf50f18SLuigi Rizzo }
6044bf50f18SLuigi Rizzo 
605f9790aebSLuigi Rizzo 
60689cc2556SLuigi Rizzo /* stop or enable all the rings of na */
6074bf50f18SLuigi Rizzo void
6084bf50f18SLuigi Rizzo netmap_set_all_rings(struct netmap_adapter *na, int stopped)
609ce3ee1e7SLuigi Rizzo {
610ce3ee1e7SLuigi Rizzo 	int i;
611847bf383SLuigi Rizzo 	enum txrx t;
612ce3ee1e7SLuigi Rizzo 
6134bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
614ce3ee1e7SLuigi Rizzo 		return;
615ce3ee1e7SLuigi Rizzo 
616847bf383SLuigi Rizzo 	for_rx_tx(t) {
617847bf383SLuigi Rizzo 		for (i = 0; i < netmap_real_rings(na, t); i++) {
618847bf383SLuigi Rizzo 			netmap_set_ring(na, i, t, stopped);
619ce3ee1e7SLuigi Rizzo 		}
620ce3ee1e7SLuigi Rizzo 	}
621ce3ee1e7SLuigi Rizzo }
622ce3ee1e7SLuigi Rizzo 
62389cc2556SLuigi Rizzo /*
62489cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
62589cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
626ddb13598SKevin Lo  * netmap mode off, or before removing the hardware rings (e.g., on module
62737e3a6d3SLuigi Rizzo  * onload).
62889cc2556SLuigi Rizzo  */
629f9790aebSLuigi Rizzo void
630f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
631f9790aebSLuigi Rizzo {
63237e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
63337e3a6d3SLuigi Rizzo 		netmap_set_all_rings(NA(ifp), NM_KR_STOPPED);
63437e3a6d3SLuigi Rizzo 	}
635f9790aebSLuigi Rizzo }
636f9790aebSLuigi Rizzo 
63789cc2556SLuigi Rizzo /*
63889cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
63989cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
64089cc2556SLuigi Rizzo  * napi_enable().
64189cc2556SLuigi Rizzo  */
642f9790aebSLuigi Rizzo void
643f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
644f9790aebSLuigi Rizzo {
64537e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
6464bf50f18SLuigi Rizzo 		netmap_set_all_rings(NA(ifp), 0 /* enabled */);
647f9790aebSLuigi Rizzo 	}
64837e3a6d3SLuigi Rizzo }
649f9790aebSLuigi Rizzo 
65037e3a6d3SLuigi Rizzo void
65137e3a6d3SLuigi Rizzo netmap_make_zombie(struct ifnet *ifp)
65237e3a6d3SLuigi Rizzo {
65337e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
65437e3a6d3SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
65537e3a6d3SLuigi Rizzo 		netmap_set_all_rings(na, NM_KR_LOCKED);
65637e3a6d3SLuigi Rizzo 		na->na_flags |= NAF_ZOMBIE;
65737e3a6d3SLuigi Rizzo 		netmap_set_all_rings(na, 0);
65837e3a6d3SLuigi Rizzo 	}
65937e3a6d3SLuigi Rizzo }
66037e3a6d3SLuigi Rizzo 
66137e3a6d3SLuigi Rizzo void
66237e3a6d3SLuigi Rizzo netmap_undo_zombie(struct ifnet *ifp)
66337e3a6d3SLuigi Rizzo {
66437e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
66537e3a6d3SLuigi Rizzo 		struct netmap_adapter *na = NA(ifp);
66637e3a6d3SLuigi Rizzo 		if (na->na_flags & NAF_ZOMBIE) {
66737e3a6d3SLuigi Rizzo 			netmap_set_all_rings(na, NM_KR_LOCKED);
66837e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_ZOMBIE;
66937e3a6d3SLuigi Rizzo 			netmap_set_all_rings(na, 0);
67037e3a6d3SLuigi Rizzo 		}
67137e3a6d3SLuigi Rizzo 	}
67237e3a6d3SLuigi Rizzo }
673f9790aebSLuigi Rizzo 
674ce3ee1e7SLuigi Rizzo /*
675ce3ee1e7SLuigi Rizzo  * generic bound_checking function
676ce3ee1e7SLuigi Rizzo  */
677ce3ee1e7SLuigi Rizzo u_int
678ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
679ce3ee1e7SLuigi Rizzo {
680ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
681ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
682ce3ee1e7SLuigi Rizzo 
683ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
684ce3ee1e7SLuigi Rizzo 		dflt = lo;
685ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
686ce3ee1e7SLuigi Rizzo 		dflt = hi;
687ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
688ce3ee1e7SLuigi Rizzo 		*v = dflt;
689ce3ee1e7SLuigi Rizzo 		op = "Bump";
690ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
691ce3ee1e7SLuigi Rizzo 		*v = hi;
692ce3ee1e7SLuigi Rizzo 		op = "Clamp";
693ce3ee1e7SLuigi Rizzo 	}
694ce3ee1e7SLuigi Rizzo 	if (op && msg)
695c3e9b4dbSLuiz Otavio O Souza 		nm_prinf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
696ce3ee1e7SLuigi Rizzo 	return *v;
697ce3ee1e7SLuigi Rizzo }
698ce3ee1e7SLuigi Rizzo 
699f9790aebSLuigi Rizzo 
700ce3ee1e7SLuigi Rizzo /*
701ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
702ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
703ce3ee1e7SLuigi Rizzo  */
704ce3ee1e7SLuigi Rizzo const char *
705ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
706ce3ee1e7SLuigi Rizzo {
707ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
708ce3ee1e7SLuigi Rizzo 	int i, j, i0;
709ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
710ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
711ce3ee1e7SLuigi Rizzo 
712ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
713ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
714ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
715ce3ee1e7SLuigi Rizzo 	if (!dst)
716ce3ee1e7SLuigi Rizzo 		dst = _dst;
717ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
718ce3ee1e7SLuigi Rizzo 		lim = len;
719ce3ee1e7SLuigi Rizzo 	o = dst;
720ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
721ce3ee1e7SLuigi Rizzo 	o += strlen(o);
722ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
723ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
724ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
725ce3ee1e7SLuigi Rizzo 		o += strlen(o);
726ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
727ce3ee1e7SLuigi Rizzo 		i0 = i;
728ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
729ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
730ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
731ce3ee1e7SLuigi Rizzo 		}
732ce3ee1e7SLuigi Rizzo 		i = i0;
733ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
734ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
735ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
736ce3ee1e7SLuigi Rizzo 		o += j+49;
737ce3ee1e7SLuigi Rizzo 	}
738ce3ee1e7SLuigi Rizzo 	*o = '\0';
739ce3ee1e7SLuigi Rizzo #undef P_HI
740ce3ee1e7SLuigi Rizzo #undef P_LO
741ce3ee1e7SLuigi Rizzo #undef P_C
742ce3ee1e7SLuigi Rizzo 	return dst;
743ce3ee1e7SLuigi Rizzo }
744f196ce38SLuigi Rizzo 
745f18be576SLuigi Rizzo 
746ae10d1afSLuigi Rizzo /*
747ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
748ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
749ae10d1afSLuigi Rizzo  */
75089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
751f9790aebSLuigi Rizzo int
752ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
753ae10d1afSLuigi Rizzo {
7542ff91c17SVincenzo Maffione 	struct nm_config_info info;
755ae10d1afSLuigi Rizzo 
7562ff91c17SVincenzo Maffione 	bzero(&info, sizeof(info));
7576641c68bSLuigi Rizzo 	if (na->nm_config == NULL ||
7582ff91c17SVincenzo Maffione 	    na->nm_config(na, &info)) {
759ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
7602ff91c17SVincenzo Maffione 		info.num_tx_rings = na->num_tx_rings;
7612ff91c17SVincenzo Maffione 		info.num_tx_descs = na->num_tx_desc;
7622ff91c17SVincenzo Maffione 		info.num_rx_rings = na->num_rx_rings;
7632ff91c17SVincenzo Maffione 		info.num_rx_descs = na->num_rx_desc;
7642ff91c17SVincenzo Maffione 		info.rx_buf_maxsize = na->rx_buf_maxsize;
765ae10d1afSLuigi Rizzo 	}
766ae10d1afSLuigi Rizzo 
7672ff91c17SVincenzo Maffione 	if (na->num_tx_rings == info.num_tx_rings &&
7682ff91c17SVincenzo Maffione 	    na->num_tx_desc == info.num_tx_descs &&
7692ff91c17SVincenzo Maffione 	    na->num_rx_rings == info.num_rx_rings &&
7702ff91c17SVincenzo Maffione 	    na->num_rx_desc == info.num_rx_descs &&
7712ff91c17SVincenzo Maffione 	    na->rx_buf_maxsize == info.rx_buf_maxsize)
772ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
773f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
7742ff91c17SVincenzo Maffione 		na->num_tx_rings = info.num_tx_rings;
7752ff91c17SVincenzo Maffione 		na->num_tx_desc = info.num_tx_descs;
7762ff91c17SVincenzo Maffione 		na->num_rx_rings = info.num_rx_rings;
7772ff91c17SVincenzo Maffione 		na->num_rx_desc = info.num_rx_descs;
7782ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = info.rx_buf_maxsize;
779cfa866f6SMatt Macy 		D("configuration changed for %s: txring %d x %d, "
780cfa866f6SMatt Macy 			"rxring %d x %d, rxbufsz %d",
781cfa866f6SMatt Macy 			na->name, na->num_tx_rings, na->num_tx_desc,
782cfa866f6SMatt Macy 			na->num_rx_rings, na->num_rx_desc, na->rx_buf_maxsize);
783ae10d1afSLuigi Rizzo 		return 0;
784ae10d1afSLuigi Rizzo 	}
7852ff91c17SVincenzo Maffione 	D("WARNING: configuration changed for %s while active: "
7862ff91c17SVincenzo Maffione 		"txring %d x %d, rxring %d x %d, rxbufsz %d",
7872ff91c17SVincenzo Maffione 		na->name, info.num_tx_rings, info.num_tx_descs,
7882ff91c17SVincenzo Maffione 		info.num_rx_rings, info.num_rx_descs,
7892ff91c17SVincenzo Maffione 		info.rx_buf_maxsize);
790ae10d1afSLuigi Rizzo 	return 1;
791ae10d1afSLuigi Rizzo }
792ae10d1afSLuigi Rizzo 
79337e3a6d3SLuigi Rizzo /* nm_sync callbacks for the host rings */
79437e3a6d3SLuigi Rizzo static int netmap_txsync_to_host(struct netmap_kring *kring, int flags);
79537e3a6d3SLuigi Rizzo static int netmap_rxsync_from_host(struct netmap_kring *kring, int flags);
796f0ea3689SLuigi Rizzo 
797f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
798f0ea3689SLuigi Rizzo  * The array layout is this:
799f0ea3689SLuigi Rizzo  *
800f0ea3689SLuigi Rizzo  *                    +----------+
801f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
802f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
803f0ea3689SLuigi Rizzo  *                    |          | /
804f0ea3689SLuigi Rizzo  *                    +----------+
805f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
806f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
807f0ea3689SLuigi Rizzo  *                    |          | \
808f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
809f0ea3689SLuigi Rizzo  *                    |          | /
810f0ea3689SLuigi Rizzo  *                    +----------+
811f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
812f0ea3689SLuigi Rizzo  *                    +----------+
813f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
814f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
815f0ea3689SLuigi Rizzo  *                    |          | /
816f0ea3689SLuigi Rizzo  *                    +----------+
817f0ea3689SLuigi Rizzo  *
818f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
819f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
820f0ea3689SLuigi Rizzo  */
82189cc2556SLuigi Rizzo /* call with NMG_LOCK held */
822f9790aebSLuigi Rizzo int
823f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
824f9790aebSLuigi Rizzo {
825f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
826f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
827847bf383SLuigi Rizzo 	u_int n[NR_TXRX];
828847bf383SLuigi Rizzo 	enum txrx t;
829f9790aebSLuigi Rizzo 
830c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings != NULL) {
831c3e9b4dbSLuiz Otavio O Souza 		D("warning: krings were already created");
832c3e9b4dbSLuiz Otavio O Souza 		return 0;
833c3e9b4dbSLuiz Otavio O Souza 	}
834c3e9b4dbSLuiz Otavio O Souza 
835f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
836*2a7db7a6SVincenzo Maffione 	n[NR_TX] = netmap_all_rings(na, NR_TX);
837*2a7db7a6SVincenzo Maffione 	n[NR_RX] = netmap_all_rings(na, NR_RX);
838f0ea3689SLuigi Rizzo 
8392ff91c17SVincenzo Maffione 	len = (n[NR_TX] + n[NR_RX]) *
8402ff91c17SVincenzo Maffione 		(sizeof(struct netmap_kring) + sizeof(struct netmap_kring *))
8412ff91c17SVincenzo Maffione 		+ tailroom;
842f9790aebSLuigi Rizzo 
843c3e9b4dbSLuiz Otavio O Souza 	na->tx_rings = nm_os_malloc((size_t)len);
844f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
845f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
846f9790aebSLuigi Rizzo 		return ENOMEM;
847f9790aebSLuigi Rizzo 	}
848847bf383SLuigi Rizzo 	na->rx_rings = na->tx_rings + n[NR_TX];
8492ff91c17SVincenzo Maffione 	na->tailroom = na->rx_rings + n[NR_RX];
8502ff91c17SVincenzo Maffione 
8512ff91c17SVincenzo Maffione 	/* link the krings in the krings array */
8522ff91c17SVincenzo Maffione 	kring = (struct netmap_kring *)((char *)na->tailroom + tailroom);
8532ff91c17SVincenzo Maffione 	for (i = 0; i < n[NR_TX] + n[NR_RX]; i++) {
8542ff91c17SVincenzo Maffione 		na->tx_rings[i] = kring;
8552ff91c17SVincenzo Maffione 		kring++;
8562ff91c17SVincenzo Maffione 	}
857f9790aebSLuigi Rizzo 
85817885a7bSLuigi Rizzo 	/*
85917885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
86017885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
86117885a7bSLuigi Rizzo 	 */
862847bf383SLuigi Rizzo 	for_rx_tx(t) {
863847bf383SLuigi Rizzo 		ndesc = nma_get_ndesc(na, t);
864847bf383SLuigi Rizzo 		for (i = 0; i < n[t]; i++) {
8652ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
866f9790aebSLuigi Rizzo 			bzero(kring, sizeof(*kring));
867f9790aebSLuigi Rizzo 			kring->na = na;
8682ff91c17SVincenzo Maffione 			kring->notify_na = na;
86917885a7bSLuigi Rizzo 			kring->ring_id = i;
870847bf383SLuigi Rizzo 			kring->tx = t;
871f9790aebSLuigi Rizzo 			kring->nkr_num_slots = ndesc;
87237e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
87337e3a6d3SLuigi Rizzo 			kring->nr_pending_mode = NKR_NETMAP_OFF;
874847bf383SLuigi Rizzo 			if (i < nma_get_nrings(na, t)) {
875847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ? na->nm_txsync : na->nm_rxsync);
87637e3a6d3SLuigi Rizzo 			} else {
8772ff91c17SVincenzo Maffione 				if (!(na->na_flags & NAF_HOST_RINGS))
8782ff91c17SVincenzo Maffione 					kring->nr_kflags |= NKR_FAKERING;
879847bf383SLuigi Rizzo 				kring->nm_sync = (t == NR_TX ?
88037e3a6d3SLuigi Rizzo 						netmap_txsync_to_host:
88137e3a6d3SLuigi Rizzo 						netmap_rxsync_from_host);
882f0ea3689SLuigi Rizzo 			}
883847bf383SLuigi Rizzo 			kring->nm_notify = na->nm_notify;
884847bf383SLuigi Rizzo 			kring->rhead = kring->rcur = kring->nr_hwcur = 0;
885f9790aebSLuigi Rizzo 			/*
88617885a7bSLuigi Rizzo 			 * IMPORTANT: Always keep one slot empty.
887f9790aebSLuigi Rizzo 			 */
888847bf383SLuigi Rizzo 			kring->rtail = kring->nr_hwtail = (t == NR_TX ? ndesc - 1 : 0);
889847bf383SLuigi Rizzo 			snprintf(kring->name, sizeof(kring->name) - 1, "%s %s%d", na->name,
890847bf383SLuigi Rizzo 					nm_txrx2str(t), i);
891f0ea3689SLuigi Rizzo 			ND("ktx %s h %d c %d t %d",
892f0ea3689SLuigi Rizzo 				kring->name, kring->rhead, kring->rcur, kring->rtail);
893847bf383SLuigi Rizzo 			mtx_init(&kring->q_lock, (t == NR_TX ? "nm_txq_lock" : "nm_rxq_lock"), NULL, MTX_DEF);
89437e3a6d3SLuigi Rizzo 			nm_os_selinfo_init(&kring->si);
895f9790aebSLuigi Rizzo 		}
89637e3a6d3SLuigi Rizzo 		nm_os_selinfo_init(&na->si[t]);
897f0ea3689SLuigi Rizzo 	}
898f9790aebSLuigi Rizzo 
899f9790aebSLuigi Rizzo 
900f9790aebSLuigi Rizzo 	return 0;
901f9790aebSLuigi Rizzo }
902f9790aebSLuigi Rizzo 
903f9790aebSLuigi Rizzo 
904f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
90589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
906f9790aebSLuigi Rizzo void
907f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
908f9790aebSLuigi Rizzo {
9092ff91c17SVincenzo Maffione 	struct netmap_kring **kring = na->tx_rings;
910847bf383SLuigi Rizzo 	enum txrx t;
911847bf383SLuigi Rizzo 
912c3e9b4dbSLuiz Otavio O Souza 	if (na->tx_rings == NULL) {
913c3e9b4dbSLuiz Otavio O Souza 		D("warning: krings were already deleted");
914c3e9b4dbSLuiz Otavio O Souza 		return;
915c3e9b4dbSLuiz Otavio O Souza 	}
916c3e9b4dbSLuiz Otavio O Souza 
917847bf383SLuigi Rizzo 	for_rx_tx(t)
91837e3a6d3SLuigi Rizzo 		nm_os_selinfo_uninit(&na->si[t]);
919f9790aebSLuigi Rizzo 
920f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
921f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
9222ff91c17SVincenzo Maffione 		mtx_destroy(&(*kring)->q_lock);
9232ff91c17SVincenzo Maffione 		nm_os_selinfo_uninit(&(*kring)->si);
924f9790aebSLuigi Rizzo 	}
925c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(na->tx_rings);
926f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
927f9790aebSLuigi Rizzo }
928f9790aebSLuigi Rizzo 
929f9790aebSLuigi Rizzo 
93017885a7bSLuigi Rizzo /*
93117885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
93217885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
93317885a7bSLuigi Rizzo  * them first.
93417885a7bSLuigi Rizzo  */
93589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
93637e3a6d3SLuigi Rizzo void
93717885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
93817885a7bSLuigi Rizzo {
939*2a7db7a6SVincenzo Maffione 	u_int lim = netmap_real_rings(na, NR_RX), i;
94017885a7bSLuigi Rizzo 
941*2a7db7a6SVincenzo Maffione 	for (i = nma_get_nrings(na, NR_RX); i < lim; i++) {
942*2a7db7a6SVincenzo Maffione 		struct mbq *q = &NMR(na, NR_RX)[i]->rx_queue;
94317885a7bSLuigi Rizzo 		ND("destroy sw mbq with len %d", mbq_len(q));
94417885a7bSLuigi Rizzo 		mbq_purge(q);
94537e3a6d3SLuigi Rizzo 		mbq_safe_fini(q);
946*2a7db7a6SVincenzo Maffione 	}
94717885a7bSLuigi Rizzo 	netmap_krings_delete(na);
94817885a7bSLuigi Rizzo }
94917885a7bSLuigi Rizzo 
9504f80b14cSVincenzo Maffione static void
9514f80b14cSVincenzo Maffione netmap_mem_drop(struct netmap_adapter *na)
9524f80b14cSVincenzo Maffione {
9534f80b14cSVincenzo Maffione 	int last = netmap_mem_deref(na->nm_mem, na);
9544f80b14cSVincenzo Maffione 	/* if the native allocator had been overrided on regif,
9554f80b14cSVincenzo Maffione 	 * restore it now and drop the temporary one
9564f80b14cSVincenzo Maffione 	 */
9574f80b14cSVincenzo Maffione 	if (last && na->nm_mem_prev) {
9584f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
9594f80b14cSVincenzo Maffione 		na->nm_mem = na->nm_mem_prev;
9604f80b14cSVincenzo Maffione 		na->nm_mem_prev = NULL;
9614f80b14cSVincenzo Maffione 	}
9624f80b14cSVincenzo Maffione }
963f9790aebSLuigi Rizzo 
96468b8534bSLuigi Rizzo /*
965847bf383SLuigi Rizzo  * Undo everything that was done in netmap_do_regif(). In particular,
966847bf383SLuigi Rizzo  * call nm_register(ifp,0) to stop netmap mode on the interface and
9674bf50f18SLuigi Rizzo  * revert to normal operation.
96868b8534bSLuigi Rizzo  */
969ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
970847bf383SLuigi Rizzo static void netmap_unset_ringid(struct netmap_priv_d *);
97137e3a6d3SLuigi Rizzo static void netmap_krings_put(struct netmap_priv_d *);
97237e3a6d3SLuigi Rizzo void
973847bf383SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv)
97468b8534bSLuigi Rizzo {
975f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
97668b8534bSLuigi Rizzo 
977ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
978f9790aebSLuigi Rizzo 	na->active_fds--;
97937e3a6d3SLuigi Rizzo 	/* unset nr_pending_mode and possibly release exclusive mode */
98037e3a6d3SLuigi Rizzo 	netmap_krings_put(priv);
981847bf383SLuigi Rizzo 
982847bf383SLuigi Rizzo #ifdef	WITH_MONITOR
98337e3a6d3SLuigi Rizzo 	/* XXX check whether we have to do something with monitor
98437e3a6d3SLuigi Rizzo 	 * when rings change nr_mode. */
98537e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {
986847bf383SLuigi Rizzo 		/* walk through all the rings and tell any monitor
987847bf383SLuigi Rizzo 		 * that the port is going to exit netmap mode
988847bf383SLuigi Rizzo 		 */
989847bf383SLuigi Rizzo 		netmap_monitor_stop(na);
99037e3a6d3SLuigi Rizzo 	}
991847bf383SLuigi Rizzo #endif
99237e3a6d3SLuigi Rizzo 
99337e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0 || nm_kring_pending(priv)) {
99437e3a6d3SLuigi Rizzo 		na->nm_register(na, 0);
99537e3a6d3SLuigi Rizzo 	}
99637e3a6d3SLuigi Rizzo 
99737e3a6d3SLuigi Rizzo 	/* delete rings and buffers that are no longer needed */
99837e3a6d3SLuigi Rizzo 	netmap_mem_rings_delete(na);
99937e3a6d3SLuigi Rizzo 
100037e3a6d3SLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
100168b8534bSLuigi Rizzo 		/*
100237e3a6d3SLuigi Rizzo 		 * (TO CHECK) We enter here
1003f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
1004f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
1005f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
1006ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
1007ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
1008ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
1009ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
1010ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
1011ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
1012ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
1013ce3ee1e7SLuigi Rizzo 		 * syscall is running.
101468b8534bSLuigi Rizzo 		 */
101537e3a6d3SLuigi Rizzo 		if (netmap_verbose)
101637e3a6d3SLuigi Rizzo 			D("deleting last instance for %s", na->name);
101737e3a6d3SLuigi Rizzo 
101837e3a6d3SLuigi Rizzo 		if (nm_netmap_on(na)) {
101937e3a6d3SLuigi Rizzo 			D("BUG: netmap on while going to delete the krings");
102037e3a6d3SLuigi Rizzo 		}
102137e3a6d3SLuigi Rizzo 
1022f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
102368b8534bSLuigi Rizzo 	}
102437e3a6d3SLuigi Rizzo 
1025847bf383SLuigi Rizzo 	/* possibily decrement counter of tx_si/rx_si users */
1026847bf383SLuigi Rizzo 	netmap_unset_ringid(priv);
1027f9790aebSLuigi Rizzo 	/* delete the nifp */
1028847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, priv->np_nifp);
1029847bf383SLuigi Rizzo 	/* drop the allocator */
10304f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
1031847bf383SLuigi Rizzo 	/* mark the priv as unregistered */
1032847bf383SLuigi Rizzo 	priv->np_na = NULL;
1033847bf383SLuigi Rizzo 	priv->np_nifp = NULL;
10345819da83SLuigi Rizzo }
103568b8534bSLuigi Rizzo 
103689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1037f0ea3689SLuigi Rizzo static __inline int
1038847bf383SLuigi Rizzo nm_si_user(struct netmap_priv_d *priv, enum txrx t)
1039f0ea3689SLuigi Rizzo {
1040f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
1041847bf383SLuigi Rizzo 		(priv->np_qlast[t] - priv->np_qfirst[t] > 1));
1042f0ea3689SLuigi Rizzo }
1043f0ea3689SLuigi Rizzo 
104437e3a6d3SLuigi Rizzo struct netmap_priv_d*
104537e3a6d3SLuigi Rizzo netmap_priv_new(void)
104637e3a6d3SLuigi Rizzo {
104737e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
104837e3a6d3SLuigi Rizzo 
1049c3e9b4dbSLuiz Otavio O Souza 	priv = nm_os_malloc(sizeof(struct netmap_priv_d));
105037e3a6d3SLuigi Rizzo 	if (priv == NULL)
105137e3a6d3SLuigi Rizzo 		return NULL;
105237e3a6d3SLuigi Rizzo 	priv->np_refs = 1;
105337e3a6d3SLuigi Rizzo 	nm_os_get_module();
105437e3a6d3SLuigi Rizzo 	return priv;
105537e3a6d3SLuigi Rizzo }
105637e3a6d3SLuigi Rizzo 
1057ce3ee1e7SLuigi Rizzo /*
10588fd44c93SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd is closed
10598fd44c93SLuigi Rizzo  * Action: undo all the things done by NIOCREGIF,
10608fd44c93SLuigi Rizzo  * On FreeBSD we need to track whether there are active mmap()s,
10618fd44c93SLuigi Rizzo  * and we use np_active_mmaps for that. On linux, the field is always 0.
10628fd44c93SLuigi Rizzo  * Return: 1 if we can free priv, 0 otherwise.
106389cc2556SLuigi Rizzo  *
1064ce3ee1e7SLuigi Rizzo  */
106589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
106637e3a6d3SLuigi Rizzo void
106737e3a6d3SLuigi Rizzo netmap_priv_delete(struct netmap_priv_d *priv)
1068ce3ee1e7SLuigi Rizzo {
1069f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1070ce3ee1e7SLuigi Rizzo 
1071847adfb7SLuigi Rizzo 	/* number of active references to this fd */
10728fd44c93SLuigi Rizzo 	if (--priv->np_refs > 0) {
107337e3a6d3SLuigi Rizzo 		return;
1074ce3ee1e7SLuigi Rizzo 	}
107537e3a6d3SLuigi Rizzo 	nm_os_put_module();
107637e3a6d3SLuigi Rizzo 	if (na) {
1077847bf383SLuigi Rizzo 		netmap_do_unregif(priv);
107837e3a6d3SLuigi Rizzo 	}
107937e3a6d3SLuigi Rizzo 	netmap_unget_na(na, priv->np_ifp);
108037e3a6d3SLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* for safety */
1081c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(priv);
1082f196ce38SLuigi Rizzo }
10835819da83SLuigi Rizzo 
1084f9790aebSLuigi Rizzo 
108589cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
1086f9790aebSLuigi Rizzo void
10875819da83SLuigi Rizzo netmap_dtor(void *data)
10885819da83SLuigi Rizzo {
10895819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
10905819da83SLuigi Rizzo 
1091ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
109237e3a6d3SLuigi Rizzo 	netmap_priv_delete(priv);
1093ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1094ce3ee1e7SLuigi Rizzo }
109568b8534bSLuigi Rizzo 
1096f18be576SLuigi Rizzo 
109768b8534bSLuigi Rizzo /*
1098c3e9b4dbSLuiz Otavio O Souza  * Handlers for synchronization of the rings from/to the host stack.
1099c3e9b4dbSLuiz Otavio O Souza  * These are associated to a network interface and are just another
1100c3e9b4dbSLuiz Otavio O Souza  * ring pair managed by userspace.
1101c3e9b4dbSLuiz Otavio O Souza  *
1102c3e9b4dbSLuiz Otavio O Souza  * Netmap also supports transparent forwarding (NS_FORWARD and NR_FORWARD
1103c3e9b4dbSLuiz Otavio O Souza  * flags):
1104c3e9b4dbSLuiz Otavio O Souza  *
1105c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on hw RX rings, the application can mark
1106c3e9b4dbSLuiz Otavio O Souza  *   them with the NS_FORWARD flag. During the next RXSYNC or poll(), they
1107c3e9b4dbSLuiz Otavio O Souza  *   will be forwarded to the host stack, similarly to what happened if
1108c3e9b4dbSLuiz Otavio O Souza  *   the application moved them to the host TX ring.
1109c3e9b4dbSLuiz Otavio O Souza  *
1110c3e9b4dbSLuiz Otavio O Souza  * - Before releasing buffers on the host RX ring, the application can
1111c3e9b4dbSLuiz Otavio O Souza  *   mark them with the NS_FORWARD flag. During the next RXSYNC or poll(),
1112c3e9b4dbSLuiz Otavio O Souza  *   they will be forwarded to the hw TX rings, saving the application
1113c3e9b4dbSLuiz Otavio O Souza  *   from doing the same task in user-space.
1114c3e9b4dbSLuiz Otavio O Souza  *
1115c3e9b4dbSLuiz Otavio O Souza  * Transparent fowarding can be enabled per-ring, by setting the NR_FORWARD
1116c3e9b4dbSLuiz Otavio O Souza  * flag, or globally with the netmap_fwd sysctl.
1117c3e9b4dbSLuiz Otavio O Souza  *
1118091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1119091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1120091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1121091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1122091fd0abSLuigi Rizzo  */
1123091fd0abSLuigi Rizzo 
1124f18be576SLuigi Rizzo 
1125091fd0abSLuigi Rizzo /*
1126c3e9b4dbSLuiz Otavio O Souza  * Pass a whole queue of mbufs to the host stack as coming from 'dst'
112717885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1128c3e9b4dbSLuiz Otavio O Souza  * After this call the queue is empty.
1129091fd0abSLuigi Rizzo  */
1130091fd0abSLuigi Rizzo static void
1131f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1132091fd0abSLuigi Rizzo {
1133091fd0abSLuigi Rizzo 	struct mbuf *m;
113437e3a6d3SLuigi Rizzo 	struct mbuf *head = NULL, *prev = NULL;
1135091fd0abSLuigi Rizzo 
1136c3e9b4dbSLuiz Otavio O Souza 	/* Send packets up, outside the lock; head/prev machinery
1137c3e9b4dbSLuiz Otavio O Souza 	 * is only useful for Windows. */
1138f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1139091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
1140091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
114137e3a6d3SLuigi Rizzo 		prev = nm_os_send_up(dst, m, prev);
114237e3a6d3SLuigi Rizzo 		if (head == NULL)
114337e3a6d3SLuigi Rizzo 			head = prev;
1144091fd0abSLuigi Rizzo 	}
114537e3a6d3SLuigi Rizzo 	if (head)
114637e3a6d3SLuigi Rizzo 		nm_os_send_up(dst, NULL, head);
114737e3a6d3SLuigi Rizzo 	mbq_fini(q);
1148091fd0abSLuigi Rizzo }
1149091fd0abSLuigi Rizzo 
1150f18be576SLuigi Rizzo 
1151091fd0abSLuigi Rizzo /*
1152c3e9b4dbSLuiz Otavio O Souza  * Scan the buffers from hwcur to ring->head, and put a copy of those
1153c3e9b4dbSLuiz Otavio O Souza  * marked NS_FORWARD (or all of them if forced) into a queue of mbufs.
1154c3e9b4dbSLuiz Otavio O Souza  * Drop remaining packets in the unlikely event
115517885a7bSLuigi Rizzo  * of an mbuf shortage.
1156091fd0abSLuigi Rizzo  */
1157091fd0abSLuigi Rizzo static void
1158091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1159091fd0abSLuigi Rizzo {
116017885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1161847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
116217885a7bSLuigi Rizzo 	u_int n;
1163f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1164091fd0abSLuigi Rizzo 
116517885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
116617885a7bSLuigi Rizzo 		struct mbuf *m;
1167091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1168091fd0abSLuigi Rizzo 
1169091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1170091fd0abSLuigi Rizzo 			continue;
11714bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
117217885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
1173091fd0abSLuigi Rizzo 			continue;
1174091fd0abSLuigi Rizzo 		}
1175091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
117617885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
11774bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1178091fd0abSLuigi Rizzo 
1179091fd0abSLuigi Rizzo 		if (m == NULL)
1180091fd0abSLuigi Rizzo 			break;
1181f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1182091fd0abSLuigi Rizzo 	}
1183091fd0abSLuigi Rizzo }
1184091fd0abSLuigi Rizzo 
118537e3a6d3SLuigi Rizzo static inline int
118637e3a6d3SLuigi Rizzo _nm_may_forward(struct netmap_kring *kring)
118737e3a6d3SLuigi Rizzo {
118837e3a6d3SLuigi Rizzo 	return	((netmap_fwd || kring->ring->flags & NR_FORWARD) &&
118937e3a6d3SLuigi Rizzo 		 kring->na->na_flags & NAF_HOST_RINGS &&
119037e3a6d3SLuigi Rizzo 		 kring->tx == NR_RX);
119137e3a6d3SLuigi Rizzo }
119237e3a6d3SLuigi Rizzo 
119337e3a6d3SLuigi Rizzo static inline int
119437e3a6d3SLuigi Rizzo nm_may_forward_up(struct netmap_kring *kring)
119537e3a6d3SLuigi Rizzo {
119637e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
119737e3a6d3SLuigi Rizzo 		 kring->ring_id != kring->na->num_rx_rings;
119837e3a6d3SLuigi Rizzo }
119937e3a6d3SLuigi Rizzo 
120037e3a6d3SLuigi Rizzo static inline int
1201c3e9b4dbSLuiz Otavio O Souza nm_may_forward_down(struct netmap_kring *kring, int sync_flags)
120237e3a6d3SLuigi Rizzo {
120337e3a6d3SLuigi Rizzo 	return	_nm_may_forward(kring) &&
1204c3e9b4dbSLuiz Otavio O Souza 		 (sync_flags & NAF_CAN_FORWARD_DOWN) &&
120537e3a6d3SLuigi Rizzo 		 kring->ring_id == kring->na->num_rx_rings;
120637e3a6d3SLuigi Rizzo }
1207f18be576SLuigi Rizzo 
1208091fd0abSLuigi Rizzo /*
120917885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
1210c3e9b4dbSLuiz Otavio O Souza  * kring->nr_hwcur and kring->rhead.
1211c3e9b4dbSLuiz Otavio O Souza  * Called under kring->rx_queue.lock on the sw rx ring.
1212c3e9b4dbSLuiz Otavio O Souza  *
1213c3e9b4dbSLuiz Otavio O Souza  * It can only be called if the user opened all the TX hw rings,
1214c3e9b4dbSLuiz Otavio O Souza  * see NAF_CAN_FORWARD_DOWN flag.
1215c3e9b4dbSLuiz Otavio O Souza  * We can touch the TX netmap rings (slots, head and cur) since
1216c3e9b4dbSLuiz Otavio O Souza  * we are in poll/ioctl system call context, and the application
1217c3e9b4dbSLuiz Otavio O Souza  * is not supposed to touch the ring (using a different thread)
1218c3e9b4dbSLuiz Otavio O Souza  * during the execution of the system call.
1219091fd0abSLuigi Rizzo  */
122017885a7bSLuigi Rizzo static u_int
1221091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1222091fd0abSLuigi Rizzo {
12232ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[na->num_rx_rings];
122417885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
122517885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
122617885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
122717885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
122817885a7bSLuigi Rizzo 	u_int sent = 0;
1229ce3ee1e7SLuigi Rizzo 
123017885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
123117885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
12322ff91c17SVincenzo Maffione 		struct netmap_kring *kdst = na->tx_rings[i];
123317885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
123417885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1235ce3ee1e7SLuigi Rizzo 
123617885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
123717885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
123817885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1239091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
124037e3a6d3SLuigi Rizzo 			u_int dst_head = rdst->head;
124117885a7bSLuigi Rizzo 
124217885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
124317885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
124417885a7bSLuigi Rizzo 				continue;
124517885a7bSLuigi Rizzo 
124617885a7bSLuigi Rizzo 			sent++;
124717885a7bSLuigi Rizzo 
124837e3a6d3SLuigi Rizzo 			dst = &rdst->slot[dst_head];
124917885a7bSLuigi Rizzo 
1250091fd0abSLuigi Rizzo 			tmp = *src;
125117885a7bSLuigi Rizzo 
1252091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1253091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1254091fd0abSLuigi Rizzo 
1255091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1256091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1257091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1258091fd0abSLuigi Rizzo 
125937e3a6d3SLuigi Rizzo 			rdst->head = rdst->cur = nm_next(dst_head, dst_lim);
1260091fd0abSLuigi Rizzo 		}
1261c3e9b4dbSLuiz Otavio O Souza 		/* if (sent) XXX txsync ? it would be just an optimization */
1262091fd0abSLuigi Rizzo 	}
126317885a7bSLuigi Rizzo 	return sent;
1264091fd0abSLuigi Rizzo }
1265091fd0abSLuigi Rizzo 
1266f18be576SLuigi Rizzo 
1267091fd0abSLuigi Rizzo /*
1268ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
126902ad4083SLuigi Rizzo  * system call in user process context, and the only contention
127002ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1271091fd0abSLuigi Rizzo  * this routine concurrently.
127268b8534bSLuigi Rizzo  */
127337e3a6d3SLuigi Rizzo static int
127437e3a6d3SLuigi Rizzo netmap_txsync_to_host(struct netmap_kring *kring, int flags)
127568b8534bSLuigi Rizzo {
127637e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
127717885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1278f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1279f9790aebSLuigi Rizzo 	struct mbq q;
128068b8534bSLuigi Rizzo 
128117885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
1282c3e9b4dbSLuiz Otavio O Souza 	 * Force hwcur = head since netmap_grab_packets() stops at head
128368b8534bSLuigi Rizzo 	 */
1284f9790aebSLuigi Rizzo 	mbq_init(&q);
128517885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
128617885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
128717885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
128817885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
128917885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
129017885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
129168b8534bSLuigi Rizzo 
1292f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
129337e3a6d3SLuigi Rizzo 	return 0;
1294f18be576SLuigi Rizzo }
1295f18be576SLuigi Rizzo 
1296f18be576SLuigi Rizzo 
129768b8534bSLuigi Rizzo /*
129802ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
129917885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
130017885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
130102ad4083SLuigi Rizzo  *
1302c3e9b4dbSLuiz Otavio O Souza  * also moves to the nic hw rings any packet the user has marked
1303c3e9b4dbSLuiz Otavio O Souza  * for transparent-mode forwarding, then sets the NR_FORWARD
1304c3e9b4dbSLuiz Otavio O Souza  * flag in the kring to let the caller push them out
130568b8534bSLuigi Rizzo  */
13068fd44c93SLuigi Rizzo static int
130737e3a6d3SLuigi Rizzo netmap_rxsync_from_host(struct netmap_kring *kring, int flags)
130868b8534bSLuigi Rizzo {
130937e3a6d3SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
131068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
131117885a7bSLuigi Rizzo 	u_int nm_i, n;
131217885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1313f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
131417885a7bSLuigi Rizzo 	int ret = 0;
1315847bf383SLuigi Rizzo 	struct mbq *q = &kring->rx_queue, fq;
131668b8534bSLuigi Rizzo 
1317847bf383SLuigi Rizzo 	mbq_init(&fq); /* fq holds packets to be freed */
1318847bf383SLuigi Rizzo 
1319997b054cSLuigi Rizzo 	mbq_lock(q);
132017885a7bSLuigi Rizzo 
132117885a7bSLuigi Rizzo 	/* First part: import newly received packets */
132217885a7bSLuigi Rizzo 	n = mbq_len(q);
132317885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
132417885a7bSLuigi Rizzo 		struct mbuf *m;
132517885a7bSLuigi Rizzo 		uint32_t stop_i;
132617885a7bSLuigi Rizzo 
132717885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
1328c3e9b4dbSLuiz Otavio O Souza 		stop_i = nm_prev(kring->nr_hwcur, lim);
132917885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
133017885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
133117885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
133217885a7bSLuigi Rizzo 
13334bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
133417885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
133517885a7bSLuigi Rizzo 			if (netmap_verbose)
13364bf50f18SLuigi Rizzo 				D("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
133717885a7bSLuigi Rizzo 
133817885a7bSLuigi Rizzo 			slot->len = len;
13394f80b14cSVincenzo Maffione 			slot->flags = 0;
134017885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
1341847bf383SLuigi Rizzo 			mbq_enqueue(&fq, m);
134264ae02c3SLuigi Rizzo 		}
134317885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
134464ae02c3SLuigi Rizzo 	}
134517885a7bSLuigi Rizzo 
134617885a7bSLuigi Rizzo 	/*
134717885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
134817885a7bSLuigi Rizzo 	 */
134917885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
135017885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
1351c3e9b4dbSLuiz Otavio O Souza 		if (nm_may_forward_down(kring, flags)) {
135217885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
135337e3a6d3SLuigi Rizzo 			if (ret > 0) {
135437e3a6d3SLuigi Rizzo 				kring->nr_kflags |= NR_FORWARD;
135537e3a6d3SLuigi Rizzo 				ret = 0;
135637e3a6d3SLuigi Rizzo 			}
135737e3a6d3SLuigi Rizzo 		}
135817885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
135964ae02c3SLuigi Rizzo 	}
136017885a7bSLuigi Rizzo 
1361997b054cSLuigi Rizzo 	mbq_unlock(q);
1362847bf383SLuigi Rizzo 
1363847bf383SLuigi Rizzo 	mbq_purge(&fq);
136437e3a6d3SLuigi Rizzo 	mbq_fini(&fq);
1365847bf383SLuigi Rizzo 
136617885a7bSLuigi Rizzo 	return ret;
136768b8534bSLuigi Rizzo }
136868b8534bSLuigi Rizzo 
136968b8534bSLuigi Rizzo 
1370f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1371f9790aebSLuigi Rizzo  *
1372f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1373f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1374f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1375f9790aebSLuigi Rizzo  *
1376f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1377f9790aebSLuigi Rizzo  * EBUSY.
1378f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1379f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1380f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1381f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1382f9790aebSLuigi Rizzo  *
1383f9790aebSLuigi Rizzo  *					native_support
1384f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1385f9790aebSLuigi Rizzo  * -------------------------------------------------------
1386f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1387f9790aebSLuigi Rizzo  *
1388f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1389f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1390f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1391f9790aebSLuigi Rizzo  *
1392f9790aebSLuigi Rizzo  */
139337e3a6d3SLuigi Rizzo static void netmap_hw_dtor(struct netmap_adapter *); /* needed by NM_IS_NATIVE() */
1394f9790aebSLuigi Rizzo int
1395c3e9b4dbSLuiz Otavio O Souza netmap_get_hw_na(struct ifnet *ifp, struct netmap_mem_d *nmd, struct netmap_adapter **na)
1396f9790aebSLuigi Rizzo {
1397f9790aebSLuigi Rizzo 	/* generic support */
1398f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1399f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1400847bf383SLuigi Rizzo 	int error = 0;
1401f9790aebSLuigi Rizzo 
1402f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1403f9790aebSLuigi Rizzo 
1404f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1405f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1406f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1407f9790aebSLuigi Rizzo 
140837e3a6d3SLuigi Rizzo 	if (NM_NA_VALID(ifp)) {
14094bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1410f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1411f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1412f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1413f9790aebSLuigi Rizzo 		 * adapters.
1414f9790aebSLuigi Rizzo 		 */
14154bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
14164bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
14174bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
14184bf50f18SLuigi Rizzo #ifdef WITH_PIPES
14194bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
14204bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
14214bf50f18SLuigi Rizzo 			 */
14224bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
14234bf50f18SLuigi Rizzo #endif
14244bf50f18SLuigi Rizzo 		) {
14254bf50f18SLuigi Rizzo 			*na = prev_na;
1426c3e9b4dbSLuiz Otavio O Souza 			goto assign_mem;
1427f9790aebSLuigi Rizzo 		}
1428f9790aebSLuigi Rizzo 	}
1429f9790aebSLuigi Rizzo 
1430f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1431f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1432f9790aebSLuigi Rizzo 	 */
143337e3a6d3SLuigi Rizzo 	if (!NM_IS_NATIVE(ifp) && i == NETMAP_ADMODE_NATIVE)
1434f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1435f9790aebSLuigi Rizzo 
1436f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1437f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1438f9790aebSLuigi Rizzo 	 *
1439f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1440f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1441f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1442f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1443f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1444f9790aebSLuigi Rizzo 	 * must be that
1445f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1446f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1447f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1448f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1449f9790aebSLuigi Rizzo 	 */
1450f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1451f9790aebSLuigi Rizzo 	if (error)
1452f9790aebSLuigi Rizzo 		return error;
1453f9790aebSLuigi Rizzo 
1454f9790aebSLuigi Rizzo 	*na = NA(ifp);
1455c3e9b4dbSLuiz Otavio O Souza 
1456c3e9b4dbSLuiz Otavio O Souza assign_mem:
1457c3e9b4dbSLuiz Otavio O Souza 	if (nmd != NULL && !((*na)->na_flags & NAF_MEM_OWNER) &&
1458c3e9b4dbSLuiz Otavio O Souza 	    (*na)->active_fds == 0 && ((*na)->nm_mem != nmd)) {
14594f80b14cSVincenzo Maffione 		(*na)->nm_mem_prev = (*na)->nm_mem;
1460c3e9b4dbSLuiz Otavio O Souza 		(*na)->nm_mem = netmap_mem_get(nmd);
1461f9790aebSLuigi Rizzo 	}
1462f9790aebSLuigi Rizzo 
1463c3e9b4dbSLuiz Otavio O Souza 	return 0;
1464c3e9b4dbSLuiz Otavio O Souza }
1465f9790aebSLuigi Rizzo 
146668b8534bSLuigi Rizzo /*
1467ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1468ce3ee1e7SLuigi Rizzo  *
1469f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
14702ff91c17SVincenzo Maffione  * to the interface specified by req.
1471ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1472ce3ee1e7SLuigi Rizzo  *
1473f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1474f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1475f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1476f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1477f2637526SLuigi Rizzo  * could not be allocated.
1478f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1479f18be576SLuigi Rizzo  *
14802ff91c17SVincenzo Maffione  * If the interface specified by req is a system one, also keep
148137e3a6d3SLuigi Rizzo  * a reference to it and return a valid *ifp.
148268b8534bSLuigi Rizzo  */
1483f9790aebSLuigi Rizzo int
14842ff91c17SVincenzo Maffione netmap_get_na(struct nmreq_header *hdr,
14852ff91c17SVincenzo Maffione 	      struct netmap_adapter **na, struct ifnet **ifp,
14862ff91c17SVincenzo Maffione 	      struct netmap_mem_d *nmd, int create)
148768b8534bSLuigi Rizzo {
1488cfa866f6SMatt Macy 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
1489f9790aebSLuigi Rizzo 	int error = 0;
1490f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1491c3e9b4dbSLuiz Otavio O Souza 	int nmd_ref = 0;
1492f9790aebSLuigi Rizzo 
1493f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
149437e3a6d3SLuigi Rizzo 	*ifp = NULL;
1495f196ce38SLuigi Rizzo 
14962ff91c17SVincenzo Maffione 	if (hdr->nr_reqtype != NETMAP_REQ_REGISTER) {
14972ff91c17SVincenzo Maffione 		return EINVAL;
14982ff91c17SVincenzo Maffione 	}
14992ff91c17SVincenzo Maffione 
15002ff91c17SVincenzo Maffione 	if (req->nr_mode == NR_REG_PIPE_MASTER ||
15012ff91c17SVincenzo Maffione 			req->nr_mode == NR_REG_PIPE_SLAVE) {
15022ff91c17SVincenzo Maffione 		/* Do not accept deprecated pipe modes. */
15032ff91c17SVincenzo Maffione 		D("Deprecated pipe nr_mode, use xx{yy or xx}yy syntax");
15042ff91c17SVincenzo Maffione 		return EINVAL;
15052ff91c17SVincenzo Maffione 	}
15062ff91c17SVincenzo Maffione 
1507ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1508ce3ee1e7SLuigi Rizzo 
1509c3e9b4dbSLuiz Otavio O Souza 	/* if the request contain a memid, try to find the
1510c3e9b4dbSLuiz Otavio O Souza 	 * corresponding memory region
1511c3e9b4dbSLuiz Otavio O Souza 	 */
15122ff91c17SVincenzo Maffione 	if (nmd == NULL && req->nr_mem_id) {
15132ff91c17SVincenzo Maffione 		nmd = netmap_mem_find(req->nr_mem_id);
1514c3e9b4dbSLuiz Otavio O Souza 		if (nmd == NULL)
1515c3e9b4dbSLuiz Otavio O Souza 			return EINVAL;
1516c3e9b4dbSLuiz Otavio O Souza 		/* keep the rereference */
1517c3e9b4dbSLuiz Otavio O Souza 		nmd_ref = 1;
1518c3e9b4dbSLuiz Otavio O Souza 	}
1519c3e9b4dbSLuiz Otavio O Souza 
152037e3a6d3SLuigi Rizzo 	/* We cascade through all possible types of netmap adapter.
15214bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
15224bf50f18SLuigi Rizzo 	 * with the following combinations:
15234bf50f18SLuigi Rizzo 	 *
15244bf50f18SLuigi Rizzo 	 * error    na
15254bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
15264bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
15274bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
15284bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
15294bf50f18SLuigi Rizzo 	 */
15304bf50f18SLuigi Rizzo 
153137e3a6d3SLuigi Rizzo 	/* try to see if this is a ptnetmap port */
15322ff91c17SVincenzo Maffione 	error = netmap_get_pt_host_na(hdr, na, nmd, create);
153337e3a6d3SLuigi Rizzo 	if (error || *na != NULL)
1534c3e9b4dbSLuiz Otavio O Souza 		goto out;
153537e3a6d3SLuigi Rizzo 
15364bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
15372ff91c17SVincenzo Maffione 	error = netmap_get_monitor_na(hdr, na, nmd, create);
15384bf50f18SLuigi Rizzo 	if (error || *na != NULL)
1539c3e9b4dbSLuiz Otavio O Souza 		goto out;
15404bf50f18SLuigi Rizzo 
15414bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
15422ff91c17SVincenzo Maffione 	error = netmap_get_pipe_na(hdr, na, nmd, create);
1543f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1544c3e9b4dbSLuiz Otavio O Souza 		goto out;
1545ce3ee1e7SLuigi Rizzo 
15464bf50f18SLuigi Rizzo 	/* try to see if this is a bridge port */
1547*2a7db7a6SVincenzo Maffione 	error = netmap_get_vale_na(hdr, na, nmd, create);
1548f0ea3689SLuigi Rizzo 	if (error)
1549c3e9b4dbSLuiz Otavio O Souza 		goto out;
1550f0ea3689SLuigi Rizzo 
1551f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1552847bf383SLuigi Rizzo 		goto out;
1553f0ea3689SLuigi Rizzo 
155489cc2556SLuigi Rizzo 	/*
155589cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
155689cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
155789cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
155889cc2556SLuigi Rizzo 	 * persistent VALE port.
155989cc2556SLuigi Rizzo 	 */
15602ff91c17SVincenzo Maffione 	*ifp = ifunit_ref(hdr->nr_name);
156137e3a6d3SLuigi Rizzo 	if (*ifp == NULL) {
1562c3e9b4dbSLuiz Otavio O Souza 		error = ENXIO;
1563c3e9b4dbSLuiz Otavio O Souza 		goto out;
1564f196ce38SLuigi Rizzo 	}
1565ce3ee1e7SLuigi Rizzo 
1566c3e9b4dbSLuiz Otavio O Souza 	error = netmap_get_hw_na(*ifp, nmd, &ret);
1567f9790aebSLuigi Rizzo 	if (error)
1568f9790aebSLuigi Rizzo 		goto out;
1569f18be576SLuigi Rizzo 
1570f9790aebSLuigi Rizzo 	*na = ret;
1571f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1572f0ea3689SLuigi Rizzo 
1573f9790aebSLuigi Rizzo out:
157437e3a6d3SLuigi Rizzo 	if (error) {
157537e3a6d3SLuigi Rizzo 		if (ret)
1576f0ea3689SLuigi Rizzo 			netmap_adapter_put(ret);
157737e3a6d3SLuigi Rizzo 		if (*ifp) {
157837e3a6d3SLuigi Rizzo 			if_rele(*ifp);
157937e3a6d3SLuigi Rizzo 			*ifp = NULL;
158037e3a6d3SLuigi Rizzo 		}
158137e3a6d3SLuigi Rizzo 	}
1582c3e9b4dbSLuiz Otavio O Souza 	if (nmd_ref)
1583c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_put(nmd);
1584f18be576SLuigi Rizzo 
15855ab0d24dSLuigi Rizzo 	return error;
15865ab0d24dSLuigi Rizzo }
1587ce3ee1e7SLuigi Rizzo 
158837e3a6d3SLuigi Rizzo /* undo netmap_get_na() */
158937e3a6d3SLuigi Rizzo void
159037e3a6d3SLuigi Rizzo netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp)
159137e3a6d3SLuigi Rizzo {
159237e3a6d3SLuigi Rizzo 	if (ifp)
159337e3a6d3SLuigi Rizzo 		if_rele(ifp);
159437e3a6d3SLuigi Rizzo 	if (na)
159537e3a6d3SLuigi Rizzo 		netmap_adapter_put(na);
159637e3a6d3SLuigi Rizzo }
159737e3a6d3SLuigi Rizzo 
159837e3a6d3SLuigi Rizzo 
159937e3a6d3SLuigi Rizzo #define NM_FAIL_ON(t) do {						\
160037e3a6d3SLuigi Rizzo 	if (unlikely(t)) {						\
160137e3a6d3SLuigi Rizzo 		RD(5, "%s: fail '" #t "' "				\
160237e3a6d3SLuigi Rizzo 			"h %d c %d t %d "				\
160337e3a6d3SLuigi Rizzo 			"rh %d rc %d rt %d "				\
160437e3a6d3SLuigi Rizzo 			"hc %d ht %d",					\
160537e3a6d3SLuigi Rizzo 			kring->name,					\
160637e3a6d3SLuigi Rizzo 			head, cur, ring->tail,				\
160737e3a6d3SLuigi Rizzo 			kring->rhead, kring->rcur, kring->rtail,	\
160837e3a6d3SLuigi Rizzo 			kring->nr_hwcur, kring->nr_hwtail);		\
160937e3a6d3SLuigi Rizzo 		return kring->nkr_num_slots;				\
161037e3a6d3SLuigi Rizzo 	}								\
161137e3a6d3SLuigi Rizzo } while (0)
1612ce3ee1e7SLuigi Rizzo 
1613f9790aebSLuigi Rizzo /*
1614f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1615f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
161617885a7bSLuigi Rizzo  * in case of error.
1617f9790aebSLuigi Rizzo  *
161817885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
161917885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1620f9790aebSLuigi Rizzo  *
162117885a7bSLuigi Rizzo  * We want
162217885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1623f9790aebSLuigi Rizzo  *
162417885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1625f9790aebSLuigi Rizzo  */
162637e3a6d3SLuigi Rizzo u_int
162737e3a6d3SLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1628f9790aebSLuigi Rizzo {
162917885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1630f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1631f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1632ce3ee1e7SLuigi Rizzo 
163317885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
163417885a7bSLuigi Rizzo 		kring->name,
163517885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
163617885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
163717885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
163837e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->rhead >= n ||
163937e3a6d3SLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n);
1640f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
164117885a7bSLuigi Rizzo 	/*
164237e3a6d3SLuigi Rizzo 	 * user sanity checks. We only use head,
164337e3a6d3SLuigi Rizzo 	 * A, B, ... are possible positions for head:
164417885a7bSLuigi Rizzo 	 *
164537e3a6d3SLuigi Rizzo 	 *  0    A  rhead   B  rtail   C  n-1
164637e3a6d3SLuigi Rizzo 	 *  0    D  rtail   E  rhead   F  n-1
164717885a7bSLuigi Rizzo 	 *
164817885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
164917885a7bSLuigi Rizzo 	 */
165017885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
165117885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
165237e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->rhead || head > kring->rtail);
165317885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
165437e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->rtail);
165517885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
165617885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
165737e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head > kring->rtail && head < kring->rhead);
165817885a7bSLuigi Rizzo 
165917885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
166017885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
166117885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
166237e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->rtail);
166317885a7bSLuigi Rizzo 		} else { /* head >= rhead */
166417885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
166537e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur > kring->rtail && cur < head);
1666f18be576SLuigi Rizzo 		}
1667f9790aebSLuigi Rizzo 	}
166817885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
1669a2a74091SLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d", kring->name,
167017885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
167117885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
167217885a7bSLuigi Rizzo 	}
167317885a7bSLuigi Rizzo 	kring->rhead = head;
167417885a7bSLuigi Rizzo 	kring->rcur = cur;
167517885a7bSLuigi Rizzo 	return head;
167668b8534bSLuigi Rizzo }
167768b8534bSLuigi Rizzo 
167868b8534bSLuigi Rizzo 
167968b8534bSLuigi Rizzo /*
1680f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
168117885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1682f9790aebSLuigi Rizzo  *
168317885a7bSLuigi Rizzo  * For a valid configuration,
168417885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1685f9790aebSLuigi Rizzo  *
168617885a7bSLuigi Rizzo  * We only consider head and cur.
168717885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1688f9790aebSLuigi Rizzo  *
1689f9790aebSLuigi Rizzo  */
169037e3a6d3SLuigi Rizzo u_int
169137e3a6d3SLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
1692f9790aebSLuigi Rizzo {
169317885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
169417885a7bSLuigi Rizzo 	uint32_t head, cur;
1695f9790aebSLuigi Rizzo 
1696847bf383SLuigi Rizzo 	ND(5,"%s kc %d kt %d h %d c %d t %d",
169717885a7bSLuigi Rizzo 		kring->name,
169817885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
169917885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
170017885a7bSLuigi Rizzo 	/*
170117885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
170217885a7bSLuigi Rizzo 	 * move backwards. However:
170317885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
170417885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
170517885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
170617885a7bSLuigi Rizzo 	 */
170717885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
170817885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1709f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
171037e3a6d3SLuigi Rizzo 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->nr_hwtail >= n);
1711f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1712f9790aebSLuigi Rizzo 	/* user sanity checks */
171317885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
171417885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
171537e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur || head > kring->nr_hwtail);
171617885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
171737e3a6d3SLuigi Rizzo 		NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
1718f9790aebSLuigi Rizzo 	} else {
171917885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
172037e3a6d3SLuigi Rizzo 		NM_FAIL_ON(head < kring->nr_hwcur && head > kring->nr_hwtail);
172117885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
172217885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
172317885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
172437e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head || cur > kring->nr_hwtail);
172517885a7bSLuigi Rizzo 		} else {
172617885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
172737e3a6d3SLuigi Rizzo 			NM_FAIL_ON(cur < head && cur > kring->nr_hwtail);
1728f9790aebSLuigi Rizzo 		}
1729f9790aebSLuigi Rizzo 	}
173017885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
173117885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
173217885a7bSLuigi Rizzo 			kring->name,
173317885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
173417885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
173517885a7bSLuigi Rizzo 	}
173617885a7bSLuigi Rizzo 	return head;
1737f9790aebSLuigi Rizzo }
1738f9790aebSLuigi Rizzo 
173917885a7bSLuigi Rizzo 
1740f9790aebSLuigi Rizzo /*
174168b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
174217885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
174368b8534bSLuigi Rizzo  * Return 1 on reinit.
1744506cc70cSLuigi Rizzo  *
1745506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1746506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
174717885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1748506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1749506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1750506cc70cSLuigi Rizzo  * it under lock.
175168b8534bSLuigi Rizzo  */
175268b8534bSLuigi Rizzo int
175368b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
175468b8534bSLuigi Rizzo {
175568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
175668b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
175768b8534bSLuigi Rizzo 	int errors = 0;
175868b8534bSLuigi Rizzo 
1759ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
17604bf50f18SLuigi Rizzo 	RD(10, "called for %s", kring->name);
176117885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
176217885a7bSLuigi Rizzo 	kring->rhead = ring->head;
176317885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
176417885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
176517885a7bSLuigi Rizzo 
176668b8534bSLuigi Rizzo 	if (ring->cur > lim)
176768b8534bSLuigi Rizzo 		errors++;
176817885a7bSLuigi Rizzo 	if (ring->head > lim)
176917885a7bSLuigi Rizzo 		errors++;
177017885a7bSLuigi Rizzo 	if (ring->tail > lim)
177117885a7bSLuigi Rizzo 		errors++;
177268b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
177368b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
177468b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
1775847bf383SLuigi Rizzo 		if (idx < 2 || idx >= kring->na->na_lut.objtotal) {
177617885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
177768b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
177868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
17794bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
178068b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
178117885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
178268b8534bSLuigi Rizzo 		}
178368b8534bSLuigi Rizzo 	}
178468b8534bSLuigi Rizzo 	if (errors) {
17858241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
178617885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
178717885a7bSLuigi Rizzo 			kring->name,
178868b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
178917885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
179017885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
179117885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
179217885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
179368b8534bSLuigi Rizzo 	}
179468b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
179568b8534bSLuigi Rizzo }
179668b8534bSLuigi Rizzo 
17974bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
17984bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
17994bf50f18SLuigi Rizzo  *
18004bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
18014bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
18024bf50f18SLuigi Rizzo  *
180368b8534bSLuigi Rizzo  */
18044bf50f18SLuigi Rizzo int
18052ff91c17SVincenzo Maffione netmap_interp_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
18062ff91c17SVincenzo Maffione 			uint16_t nr_ringid, uint64_t nr_flags)
180768b8534bSLuigi Rizzo {
1808f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
180937e3a6d3SLuigi Rizzo 	int excluded_direction[] = { NR_TX_RINGS_ONLY, NR_RX_RINGS_ONLY };
1810847bf383SLuigi Rizzo 	enum txrx t;
18112ff91c17SVincenzo Maffione 	u_int j;
181268b8534bSLuigi Rizzo 
18132ff91c17SVincenzo Maffione 	if ((nr_flags & NR_PTNETMAP_HOST) && ((nr_mode != NR_REG_ALL_NIC) ||
18142ff91c17SVincenzo Maffione 			nr_flags & (NR_RX_RINGS_ONLY|NR_TX_RINGS_ONLY))) {
181537e3a6d3SLuigi Rizzo 		D("Error: only NR_REG_ALL_NIC supported with netmap passthrough");
181637e3a6d3SLuigi Rizzo 		return EINVAL;
181737e3a6d3SLuigi Rizzo 	}
181837e3a6d3SLuigi Rizzo 
181937e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
18202ff91c17SVincenzo Maffione 		if (nr_flags & excluded_direction[t]) {
182137e3a6d3SLuigi Rizzo 			priv->np_qfirst[t] = priv->np_qlast[t] = 0;
182237e3a6d3SLuigi Rizzo 			continue;
182337e3a6d3SLuigi Rizzo 		}
18242ff91c17SVincenzo Maffione 		switch (nr_mode) {
1825f0ea3689SLuigi Rizzo 		case NR_REG_ALL_NIC:
1826847bf383SLuigi Rizzo 			priv->np_qfirst[t] = 0;
1827847bf383SLuigi Rizzo 			priv->np_qlast[t] = nma_get_nrings(na, t);
182837e3a6d3SLuigi Rizzo 			ND("ALL/PIPE: %s %d %d", nm_txrx2str(t),
182937e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1830f0ea3689SLuigi Rizzo 			break;
1831f0ea3689SLuigi Rizzo 		case NR_REG_SW:
1832f0ea3689SLuigi Rizzo 		case NR_REG_NIC_SW:
1833f0ea3689SLuigi Rizzo 			if (!(na->na_flags & NAF_HOST_RINGS)) {
1834f0ea3689SLuigi Rizzo 				D("host rings not supported");
1835f0ea3689SLuigi Rizzo 				return EINVAL;
1836f0ea3689SLuigi Rizzo 			}
18372ff91c17SVincenzo Maffione 			priv->np_qfirst[t] = (nr_mode == NR_REG_SW ?
1838847bf383SLuigi Rizzo 				nma_get_nrings(na, t) : 0);
1839*2a7db7a6SVincenzo Maffione 			priv->np_qlast[t] = netmap_all_rings(na, t);
18402ff91c17SVincenzo Maffione 			ND("%s: %s %d %d", nr_mode == NR_REG_SW ? "SW" : "NIC+SW",
184137e3a6d3SLuigi Rizzo 				nm_txrx2str(t),
184237e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1843f0ea3689SLuigi Rizzo 			break;
1844f0ea3689SLuigi Rizzo 		case NR_REG_ONE_NIC:
18452ff91c17SVincenzo Maffione 			if (nr_ringid >= na->num_tx_rings &&
18462ff91c17SVincenzo Maffione 					nr_ringid >= na->num_rx_rings) {
18472ff91c17SVincenzo Maffione 				D("invalid ring id %d", nr_ringid);
1848f0ea3689SLuigi Rizzo 				return EINVAL;
1849f0ea3689SLuigi Rizzo 			}
1850f0ea3689SLuigi Rizzo 			/* if not enough rings, use the first one */
18512ff91c17SVincenzo Maffione 			j = nr_ringid;
1852847bf383SLuigi Rizzo 			if (j >= nma_get_nrings(na, t))
1853f0ea3689SLuigi Rizzo 				j = 0;
1854847bf383SLuigi Rizzo 			priv->np_qfirst[t] = j;
1855847bf383SLuigi Rizzo 			priv->np_qlast[t] = j + 1;
185637e3a6d3SLuigi Rizzo 			ND("ONE_NIC: %s %d %d", nm_txrx2str(t),
185737e3a6d3SLuigi Rizzo 				priv->np_qfirst[t], priv->np_qlast[t]);
1858f0ea3689SLuigi Rizzo 			break;
1859f0ea3689SLuigi Rizzo 		default:
18602ff91c17SVincenzo Maffione 			D("invalid regif type %d", nr_mode);
1861f0ea3689SLuigi Rizzo 			return EINVAL;
186268b8534bSLuigi Rizzo 		}
186337e3a6d3SLuigi Rizzo 	}
18642ff91c17SVincenzo Maffione 	priv->np_flags = nr_flags | nr_mode; // TODO
18654bf50f18SLuigi Rizzo 
1866c3e9b4dbSLuiz Otavio O Souza 	/* Allow transparent forwarding mode in the host --> nic
1867c3e9b4dbSLuiz Otavio O Souza 	 * direction only if all the TX hw rings have been opened. */
1868c3e9b4dbSLuiz Otavio O Souza 	if (priv->np_qfirst[NR_TX] == 0 &&
1869c3e9b4dbSLuiz Otavio O Souza 			priv->np_qlast[NR_TX] >= na->num_tx_rings) {
1870c3e9b4dbSLuiz Otavio O Souza 		priv->np_sync_flags |= NAF_CAN_FORWARD_DOWN;
1871c3e9b4dbSLuiz Otavio O Souza 	}
1872c3e9b4dbSLuiz Otavio O Souza 
1873ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1874f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
18754bf50f18SLuigi Rizzo 			na->name,
1876847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1877847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1878847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1879847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX],
18802ff91c17SVincenzo Maffione 			nr_ringid);
1881ae10d1afSLuigi Rizzo 	}
188268b8534bSLuigi Rizzo 	return 0;
188368b8534bSLuigi Rizzo }
188468b8534bSLuigi Rizzo 
18854bf50f18SLuigi Rizzo 
18864bf50f18SLuigi Rizzo /*
18874bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
18884bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
18894bf50f18SLuigi Rizzo  */
18904bf50f18SLuigi Rizzo static int
18912ff91c17SVincenzo Maffione netmap_set_ringid(struct netmap_priv_d *priv, uint32_t nr_mode,
18922ff91c17SVincenzo Maffione 		uint16_t nr_ringid, uint64_t nr_flags)
18934bf50f18SLuigi Rizzo {
18944bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
18954bf50f18SLuigi Rizzo 	int error;
1896847bf383SLuigi Rizzo 	enum txrx t;
18974bf50f18SLuigi Rizzo 
18982ff91c17SVincenzo Maffione 	error = netmap_interp_ringid(priv, nr_mode, nr_ringid, nr_flags);
18994bf50f18SLuigi Rizzo 	if (error) {
19004bf50f18SLuigi Rizzo 		return error;
19014bf50f18SLuigi Rizzo 	}
19024bf50f18SLuigi Rizzo 
19032ff91c17SVincenzo Maffione 	priv->np_txpoll = (nr_flags & NR_NO_TX_POLL) ? 0 : 1;
19044bf50f18SLuigi Rizzo 
19054bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
19064bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
19074bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
19084bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
19094bf50f18SLuigi Rizzo 	 */
1910847bf383SLuigi Rizzo 	for_rx_tx(t) {
1911847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1912847bf383SLuigi Rizzo 			na->si_users[t]++;
1913847bf383SLuigi Rizzo 	}
19144bf50f18SLuigi Rizzo 	return 0;
19154bf50f18SLuigi Rizzo }
19164bf50f18SLuigi Rizzo 
1917847bf383SLuigi Rizzo static void
1918847bf383SLuigi Rizzo netmap_unset_ringid(struct netmap_priv_d *priv)
1919847bf383SLuigi Rizzo {
1920847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1921847bf383SLuigi Rizzo 	enum txrx t;
1922847bf383SLuigi Rizzo 
1923847bf383SLuigi Rizzo 	for_rx_tx(t) {
1924847bf383SLuigi Rizzo 		if (nm_si_user(priv, t))
1925847bf383SLuigi Rizzo 			na->si_users[t]--;
1926847bf383SLuigi Rizzo 		priv->np_qfirst[t] = priv->np_qlast[t] = 0;
1927847bf383SLuigi Rizzo 	}
1928847bf383SLuigi Rizzo 	priv->np_flags = 0;
1929847bf383SLuigi Rizzo 	priv->np_txpoll = 0;
1930847bf383SLuigi Rizzo }
1931847bf383SLuigi Rizzo 
1932847bf383SLuigi Rizzo 
193337e3a6d3SLuigi Rizzo /* Set the nr_pending_mode for the requested rings.
193437e3a6d3SLuigi Rizzo  * If requested, also try to get exclusive access to the rings, provided
193537e3a6d3SLuigi Rizzo  * the rings we want to bind are not exclusively owned by a previous bind.
1936847bf383SLuigi Rizzo  */
1937847bf383SLuigi Rizzo static int
193837e3a6d3SLuigi Rizzo netmap_krings_get(struct netmap_priv_d *priv)
1939847bf383SLuigi Rizzo {
1940847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1941847bf383SLuigi Rizzo 	u_int i;
1942847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1943847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
1944847bf383SLuigi Rizzo 	enum txrx t;
1945847bf383SLuigi Rizzo 
19464f80b14cSVincenzo Maffione 	if (netmap_verbose)
19474f80b14cSVincenzo Maffione 		D("%s: grabbing tx [%d, %d) rx [%d, %d)",
1948847bf383SLuigi Rizzo 			na->name,
1949847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
1950847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
1951847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
1952847bf383SLuigi Rizzo 			priv->np_qlast[NR_RX]);
1953847bf383SLuigi Rizzo 
1954847bf383SLuigi Rizzo 	/* first round: check that all the requested rings
1955847bf383SLuigi Rizzo 	 * are neither alread exclusively owned, nor we
1956847bf383SLuigi Rizzo 	 * want exclusive ownership when they are already in use
1957847bf383SLuigi Rizzo 	 */
1958847bf383SLuigi Rizzo 	for_rx_tx(t) {
1959847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
19602ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
1961847bf383SLuigi Rizzo 			if ((kring->nr_kflags & NKR_EXCLUSIVE) ||
1962847bf383SLuigi Rizzo 			    (kring->users && excl))
1963847bf383SLuigi Rizzo 			{
1964847bf383SLuigi Rizzo 				ND("ring %s busy", kring->name);
1965847bf383SLuigi Rizzo 				return EBUSY;
1966847bf383SLuigi Rizzo 			}
1967847bf383SLuigi Rizzo 		}
1968847bf383SLuigi Rizzo 	}
1969847bf383SLuigi Rizzo 
197037e3a6d3SLuigi Rizzo 	/* second round: increment usage count (possibly marking them
197137e3a6d3SLuigi Rizzo 	 * as exclusive) and set the nr_pending_mode
1972847bf383SLuigi Rizzo 	 */
1973847bf383SLuigi Rizzo 	for_rx_tx(t) {
1974847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
19752ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
1976847bf383SLuigi Rizzo 			kring->users++;
1977847bf383SLuigi Rizzo 			if (excl)
1978847bf383SLuigi Rizzo 				kring->nr_kflags |= NKR_EXCLUSIVE;
197937e3a6d3SLuigi Rizzo 	                kring->nr_pending_mode = NKR_NETMAP_ON;
1980847bf383SLuigi Rizzo 		}
1981847bf383SLuigi Rizzo 	}
1982847bf383SLuigi Rizzo 
1983847bf383SLuigi Rizzo 	return 0;
1984847bf383SLuigi Rizzo 
1985847bf383SLuigi Rizzo }
1986847bf383SLuigi Rizzo 
198737e3a6d3SLuigi Rizzo /* Undo netmap_krings_get(). This is done by clearing the exclusive mode
198837e3a6d3SLuigi Rizzo  * if was asked on regif, and unset the nr_pending_mode if we are the
198937e3a6d3SLuigi Rizzo  * last users of the involved rings. */
1990847bf383SLuigi Rizzo static void
199137e3a6d3SLuigi Rizzo netmap_krings_put(struct netmap_priv_d *priv)
1992847bf383SLuigi Rizzo {
1993847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1994847bf383SLuigi Rizzo 	u_int i;
1995847bf383SLuigi Rizzo 	struct netmap_kring *kring;
1996847bf383SLuigi Rizzo 	int excl = (priv->np_flags & NR_EXCLUSIVE);
1997847bf383SLuigi Rizzo 	enum txrx t;
1998847bf383SLuigi Rizzo 
1999847bf383SLuigi Rizzo 	ND("%s: releasing tx [%d, %d) rx [%d, %d)",
2000847bf383SLuigi Rizzo 			na->name,
2001847bf383SLuigi Rizzo 			priv->np_qfirst[NR_TX],
2002847bf383SLuigi Rizzo 			priv->np_qlast[NR_TX],
2003847bf383SLuigi Rizzo 			priv->np_qfirst[NR_RX],
2004847bf383SLuigi Rizzo 			priv->np_qlast[MR_RX]);
2005847bf383SLuigi Rizzo 
2006847bf383SLuigi Rizzo 	for_rx_tx(t) {
2007847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
20082ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
2009847bf383SLuigi Rizzo 			if (excl)
2010847bf383SLuigi Rizzo 				kring->nr_kflags &= ~NKR_EXCLUSIVE;
2011847bf383SLuigi Rizzo 			kring->users--;
201237e3a6d3SLuigi Rizzo 			if (kring->users == 0)
201337e3a6d3SLuigi Rizzo 				kring->nr_pending_mode = NKR_NETMAP_OFF;
2014847bf383SLuigi Rizzo 		}
2015847bf383SLuigi Rizzo 	}
2016847bf383SLuigi Rizzo }
2017847bf383SLuigi Rizzo 
20182ff91c17SVincenzo Maffione static int
20192ff91c17SVincenzo Maffione nm_priv_rx_enabled(struct netmap_priv_d *priv)
20202ff91c17SVincenzo Maffione {
20212ff91c17SVincenzo Maffione 	return (priv->np_qfirst[NR_RX] != priv->np_qlast[NR_RX]);
20222ff91c17SVincenzo Maffione }
20232ff91c17SVincenzo Maffione 
2024f18be576SLuigi Rizzo /*
2025f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
2026f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
2027ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
20284bf50f18SLuigi Rizzo  *
20294bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
20304bf50f18SLuigi Rizzo  *
20314bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
20324bf50f18SLuigi Rizzo  * (get current number and size of rings)
20334bf50f18SLuigi Rizzo  *
20344bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
20354bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
20364bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
20374bf50f18SLuigi Rizzo  *
20384bf50f18SLuigi Rizzo  *
2039847bf383SLuigi Rizzo  * na->nm_krings_create()
20404bf50f18SLuigi Rizzo  * (create and init the krings array)
20414bf50f18SLuigi Rizzo  *
20424bf50f18SLuigi Rizzo  * 	One of the following:
20434bf50f18SLuigi Rizzo  *
20444bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
20454bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
20464bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
20474bf50f18SLuigi Rizzo  *
20484bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
20494bf50f18SLuigi Rizzo  * 		add leases and scratchpads
20504bf50f18SLuigi Rizzo  *
20514bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
20524bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
20534bf50f18SLuigi Rizzo  * 		cross-link them
20544bf50f18SLuigi Rizzo  *
20554bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
20564bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
20574bf50f18SLuigi Rizzo  *
20584bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
20594bf50f18SLuigi Rizzo  *      	create both the brap krings array,
20604bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
20614bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
20624bf50f18SLuigi Rizzo  *
20634bf50f18SLuigi Rizzo  * na->nm_register(, 1)
20644bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
20654bf50f18SLuigi Rizzo  *
20664bf50f18SLuigi Rizzo  * 	This may be one of the following:
20674bf50f18SLuigi Rizzo  *
206837e3a6d3SLuigi Rizzo  * 	* netmap_hw_reg				        (hw ports)
20694bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
20704bf50f18SLuigi Rizzo  * 		the hardware specific callback;
20714bf50f18SLuigi Rizzo  *
20724bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
20734bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
20744bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
20754bf50f18SLuigi Rizzo  *		bridge write lock.
20764bf50f18SLuigi Rizzo  *
20774bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
20784bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
2079453130d9SPedro F. Giffuni  *		longer responsible for the lifetime of this
20804bf50f18SLuigi Rizzo  *		pipe end
20814bf50f18SLuigi Rizzo  *
20824bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
20834bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
20844bf50f18SLuigi Rizzo  *		rings
20854bf50f18SLuigi Rizzo  *
208637e3a6d3SLuigi Rizzo  *	* netmap_bwrap_reg				(bwraps)
20874bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
20884bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
20894bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
20904bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
20914bf50f18SLuigi Rizzo  *
20924bf50f18SLuigi Rizzo  *
2093f18be576SLuigi Rizzo  */
2094847bf383SLuigi Rizzo int
2095f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
20962ff91c17SVincenzo Maffione 	uint32_t nr_mode, uint16_t nr_ringid, uint64_t nr_flags)
2097f18be576SLuigi Rizzo {
2098f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
2099847bf383SLuigi Rizzo 	int error;
2100f18be576SLuigi Rizzo 
2101ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
2102f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
2103847bf383SLuigi Rizzo 	error = netmap_mem_finalize(na->nm_mem, na);
2104ce3ee1e7SLuigi Rizzo 	if (error)
2105847bf383SLuigi Rizzo 		goto err;
2106847bf383SLuigi Rizzo 
2107847bf383SLuigi Rizzo 	if (na->active_fds == 0) {
21082ff91c17SVincenzo Maffione 
21092ff91c17SVincenzo Maffione 		/* cache the allocator info in the na */
21102ff91c17SVincenzo Maffione 		error = netmap_mem_get_lut(na->nm_mem, &na->na_lut);
21112ff91c17SVincenzo Maffione 		if (error)
21122ff91c17SVincenzo Maffione 			goto err_drop_mem;
21132ff91c17SVincenzo Maffione 		ND("lut %p bufs %u size %u", na->na_lut.lut, na->na_lut.objtotal,
21142ff91c17SVincenzo Maffione 					    na->na_lut.objsize);
21152ff91c17SVincenzo Maffione 
21162ff91c17SVincenzo Maffione 		/* ring configuration may have changed, fetch from the card */
21172ff91c17SVincenzo Maffione 		netmap_update_config(na);
2118cfa866f6SMatt Macy 	}
21192ff91c17SVincenzo Maffione 
2120cfa866f6SMatt Macy 	/* compute the range of tx and rx rings to monitor */
2121cfa866f6SMatt Macy 	error = netmap_set_ringid(priv, nr_mode, nr_ringid, nr_flags);
2122cfa866f6SMatt Macy 	if (error)
2123cfa866f6SMatt Macy 		goto err_put_lut;
2124cfa866f6SMatt Macy 
2125cfa866f6SMatt Macy 	if (na->active_fds == 0) {
2126847bf383SLuigi Rizzo 		/*
2127847bf383SLuigi Rizzo 		 * If this is the first registration of the adapter,
21284f80b14cSVincenzo Maffione 		 * perform sanity checks and create the in-kernel view
21294f80b14cSVincenzo Maffione 		 * of the netmap rings (the netmap krings).
2130847bf383SLuigi Rizzo 		 */
21312ff91c17SVincenzo Maffione 		if (na->ifp && nm_priv_rx_enabled(priv)) {
21324f80b14cSVincenzo Maffione 			/* This netmap adapter is attached to an ifnet. */
2133cfa866f6SMatt Macy 			unsigned nbs = NETMAP_BUF_SIZE(na);
21344f80b14cSVincenzo Maffione 			unsigned mtu = nm_os_ifnet_mtu(na->ifp);
21354f80b14cSVincenzo Maffione 
2136cfa866f6SMatt Macy 			ND("%s: mtu %d rx_buf_maxsize %d netmap_buf_size %d",
2137cfa866f6SMatt Macy 					na->name, mtu, na->rx_buf_maxsize, nbs);
2138cfa866f6SMatt Macy 
2139cfa866f6SMatt Macy 			if (na->rx_buf_maxsize == 0) {
2140cfa866f6SMatt Macy 				D("%s: error: rx_buf_maxsize == 0", na->name);
2141cfa866f6SMatt Macy 				error = EIO;
2142cfa866f6SMatt Macy 				goto err_drop_mem;
2143cfa866f6SMatt Macy 			}
21442ff91c17SVincenzo Maffione 
21452ff91c17SVincenzo Maffione 			if (mtu <= na->rx_buf_maxsize) {
21464f80b14cSVincenzo Maffione 				/* The MTU fits a single NIC slot. We only
21474f80b14cSVincenzo Maffione 				 * Need to check that netmap buffers are
21484f80b14cSVincenzo Maffione 				 * large enough to hold an MTU. NS_MOREFRAG
21494f80b14cSVincenzo Maffione 				 * cannot be used in this case. */
21504f80b14cSVincenzo Maffione 				if (nbs < mtu) {
21514f80b14cSVincenzo Maffione 					nm_prerr("error: netmap buf size (%u) "
21522ff91c17SVincenzo Maffione 						"< device MTU (%u)\n", nbs, mtu);
21534f80b14cSVincenzo Maffione 					error = EINVAL;
21544f80b14cSVincenzo Maffione 					goto err_drop_mem;
21554f80b14cSVincenzo Maffione 				}
21564f80b14cSVincenzo Maffione 			} else {
21574f80b14cSVincenzo Maffione 				/* More NIC slots may be needed to receive
21584f80b14cSVincenzo Maffione 				 * or transmit a single packet. Check that
21594f80b14cSVincenzo Maffione 				 * the adapter supports NS_MOREFRAG and that
21604f80b14cSVincenzo Maffione 				 * netmap buffers are large enough to hold
21614f80b14cSVincenzo Maffione 				 * the maximum per-slot size. */
21624f80b14cSVincenzo Maffione 				if (!(na->na_flags & NAF_MOREFRAG)) {
21634f80b14cSVincenzo Maffione 					nm_prerr("error: large MTU (%d) needed "
21644f80b14cSVincenzo Maffione 						"but %s does not support "
21652ff91c17SVincenzo Maffione 						"NS_MOREFRAG\n", mtu,
21664f80b14cSVincenzo Maffione 						na->ifp->if_xname);
21674f80b14cSVincenzo Maffione 					error = EINVAL;
21684f80b14cSVincenzo Maffione 					goto err_drop_mem;
21692ff91c17SVincenzo Maffione 				} else if (nbs < na->rx_buf_maxsize) {
21704f80b14cSVincenzo Maffione 					nm_prerr("error: using NS_MOREFRAG on "
21714f80b14cSVincenzo Maffione 						"%s requires netmap buf size "
21722ff91c17SVincenzo Maffione 						">= %u\n", na->ifp->if_xname,
21732ff91c17SVincenzo Maffione 						na->rx_buf_maxsize);
21744f80b14cSVincenzo Maffione 					error = EINVAL;
21754f80b14cSVincenzo Maffione 					goto err_drop_mem;
21764f80b14cSVincenzo Maffione 				} else {
21774f80b14cSVincenzo Maffione 					nm_prinf("info: netmap application on "
21784f80b14cSVincenzo Maffione 						"%s needs to support "
21794f80b14cSVincenzo Maffione 						"NS_MOREFRAG "
21802ff91c17SVincenzo Maffione 						"(MTU=%u,netmap_buf_size=%u)\n",
21814f80b14cSVincenzo Maffione 						na->ifp->if_xname, mtu, nbs);
21824f80b14cSVincenzo Maffione 				}
21834f80b14cSVincenzo Maffione 			}
21844f80b14cSVincenzo Maffione 		}
2185847bf383SLuigi Rizzo 
2186847bf383SLuigi Rizzo 		/*
2187847bf383SLuigi Rizzo 		 * Depending on the adapter, this may also create
2188847bf383SLuigi Rizzo 		 * the netmap rings themselves
2189847bf383SLuigi Rizzo 		 */
2190847bf383SLuigi Rizzo 		error = na->nm_krings_create(na);
2191847bf383SLuigi Rizzo 		if (error)
21922ff91c17SVincenzo Maffione 			goto err_put_lut;
2193847bf383SLuigi Rizzo 
2194ce3ee1e7SLuigi Rizzo 	}
2195847bf383SLuigi Rizzo 
219637e3a6d3SLuigi Rizzo 	/* now the krings must exist and we can check whether some
219737e3a6d3SLuigi Rizzo 	 * previous bind has exclusive ownership on them, and set
219837e3a6d3SLuigi Rizzo 	 * nr_pending_mode
2199847bf383SLuigi Rizzo 	 */
220037e3a6d3SLuigi Rizzo 	error = netmap_krings_get(priv);
2201847bf383SLuigi Rizzo 	if (error)
220237e3a6d3SLuigi Rizzo 		goto err_del_krings;
220337e3a6d3SLuigi Rizzo 
220437e3a6d3SLuigi Rizzo 	/* create all needed missing netmap rings */
220537e3a6d3SLuigi Rizzo 	error = netmap_mem_rings_create(na);
220637e3a6d3SLuigi Rizzo 	if (error)
220737e3a6d3SLuigi Rizzo 		goto err_rel_excl;
2208847bf383SLuigi Rizzo 
2209847bf383SLuigi Rizzo 	/* in all cases, create a new netmap if */
2210c3e9b4dbSLuiz Otavio O Souza 	nifp = netmap_mem_if_new(na, priv);
2211847bf383SLuigi Rizzo 	if (nifp == NULL) {
2212f18be576SLuigi Rizzo 		error = ENOMEM;
2213cfa866f6SMatt Macy 		goto err_rel_excl;
2214ce3ee1e7SLuigi Rizzo 	}
2215847bf383SLuigi Rizzo 
221637e3a6d3SLuigi Rizzo 	if (nm_kring_pending(priv)) {
221737e3a6d3SLuigi Rizzo 		/* Some kring is switching mode, tell the adapter to
221837e3a6d3SLuigi Rizzo 		 * react on this. */
221937e3a6d3SLuigi Rizzo 		error = na->nm_register(na, 1);
222037e3a6d3SLuigi Rizzo 		if (error)
22212ff91c17SVincenzo Maffione 			goto err_del_if;
222237e3a6d3SLuigi Rizzo 	}
222337e3a6d3SLuigi Rizzo 
222437e3a6d3SLuigi Rizzo 	/* Commit the reference. */
222537e3a6d3SLuigi Rizzo 	na->active_fds++;
222637e3a6d3SLuigi Rizzo 
2227ce3ee1e7SLuigi Rizzo 	/*
2228847bf383SLuigi Rizzo 	 * advertise that the interface is ready by setting np_nifp.
2229847bf383SLuigi Rizzo 	 * The barrier is needed because readers (poll, *SYNC and mmap)
2230ce3ee1e7SLuigi Rizzo 	 * check for priv->np_nifp != NULL without locking
2231ce3ee1e7SLuigi Rizzo 	 */
2232847bf383SLuigi Rizzo 	mb(); /* make sure previous writes are visible to all CPUs */
2233ce3ee1e7SLuigi Rizzo 	priv->np_nifp = nifp;
2234847bf383SLuigi Rizzo 
2235847bf383SLuigi Rizzo 	return 0;
2236847bf383SLuigi Rizzo 
223737e3a6d3SLuigi Rizzo err_del_if:
2238847bf383SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
22394f80b14cSVincenzo Maffione err_rel_excl:
22404f80b14cSVincenzo Maffione 	netmap_krings_put(priv);
2241cfa866f6SMatt Macy 	netmap_mem_rings_delete(na);
2242847bf383SLuigi Rizzo err_del_krings:
2243847bf383SLuigi Rizzo 	if (na->active_fds == 0)
2244847bf383SLuigi Rizzo 		na->nm_krings_delete(na);
22452ff91c17SVincenzo Maffione err_put_lut:
22462ff91c17SVincenzo Maffione 	if (na->active_fds == 0)
22472ff91c17SVincenzo Maffione 		memset(&na->na_lut, 0, sizeof(na->na_lut));
2248847bf383SLuigi Rizzo err_drop_mem:
22494f80b14cSVincenzo Maffione 	netmap_mem_drop(na);
2250847bf383SLuigi Rizzo err:
2251847bf383SLuigi Rizzo 	priv->np_na = NULL;
2252847bf383SLuigi Rizzo 	return error;
2253ce3ee1e7SLuigi Rizzo }
2254847bf383SLuigi Rizzo 
2255847bf383SLuigi Rizzo 
2256847bf383SLuigi Rizzo /*
225737e3a6d3SLuigi Rizzo  * update kring and ring at the end of rxsync/txsync.
2258847bf383SLuigi Rizzo  */
2259847bf383SLuigi Rizzo static inline void
226037e3a6d3SLuigi Rizzo nm_sync_finalize(struct netmap_kring *kring)
2261847bf383SLuigi Rizzo {
226237e3a6d3SLuigi Rizzo 	/*
226337e3a6d3SLuigi Rizzo 	 * Update ring tail to what the kernel knows
226437e3a6d3SLuigi Rizzo 	 * After txsync: head/rhead/hwcur might be behind cur/rcur
226537e3a6d3SLuigi Rizzo 	 * if no carrier.
226637e3a6d3SLuigi Rizzo 	 */
2267847bf383SLuigi Rizzo 	kring->ring->tail = kring->rtail = kring->nr_hwtail;
2268847bf383SLuigi Rizzo 
2269847bf383SLuigi Rizzo 	ND(5, "%s now hwcur %d hwtail %d head %d cur %d tail %d",
2270847bf383SLuigi Rizzo 		kring->name, kring->nr_hwcur, kring->nr_hwtail,
2271847bf383SLuigi Rizzo 		kring->rhead, kring->rcur, kring->rtail);
2272847bf383SLuigi Rizzo }
2273847bf383SLuigi Rizzo 
2274c3e9b4dbSLuiz Otavio O Souza /* set ring timestamp */
2275c3e9b4dbSLuiz Otavio O Souza static inline void
2276c3e9b4dbSLuiz Otavio O Souza ring_timestamp_set(struct netmap_ring *ring)
2277c3e9b4dbSLuiz Otavio O Souza {
2278c3e9b4dbSLuiz Otavio O Souza 	if (netmap_no_timestamp == 0 || ring->flags & NR_TIMESTAMP) {
2279c3e9b4dbSLuiz Otavio O Souza 		microtime(&ring->ts);
2280c3e9b4dbSLuiz Otavio O Souza 	}
2281c3e9b4dbSLuiz Otavio O Souza }
2282c3e9b4dbSLuiz Otavio O Souza 
22832ff91c17SVincenzo Maffione static int nmreq_copyin(struct nmreq_header *, int);
22842ff91c17SVincenzo Maffione static int nmreq_copyout(struct nmreq_header *, int);
22852ff91c17SVincenzo Maffione static int nmreq_checkoptions(struct nmreq_header *);
2286c3e9b4dbSLuiz Otavio O Souza 
228768b8534bSLuigi Rizzo /*
228868b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
228968b8534bSLuigi Rizzo  *
229068b8534bSLuigi Rizzo  * Following a list of accepted commands:
22912ff91c17SVincenzo Maffione  * - NIOCCTRL		device control API
22922ff91c17SVincenzo Maffione  * - NIOCTXSYNC		sync TX rings
22932ff91c17SVincenzo Maffione  * - NIOCRXSYNC		sync RX rings
229468b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
22952ff91c17SVincenzo Maffione  * - NIOCGINFO		deprecated (legacy API)
22962ff91c17SVincenzo Maffione  * - NIOCREGIF		deprecated (legacy API)
229768b8534bSLuigi Rizzo  *
229868b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
229968b8534bSLuigi Rizzo  */
2300f9790aebSLuigi Rizzo int
23012ff91c17SVincenzo Maffione netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
23022ff91c17SVincenzo Maffione 		struct thread *td, int nr_body_is_user)
230368b8534bSLuigi Rizzo {
2304c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
2305ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
2306c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd = NULL;
230737e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
230837e3a6d3SLuigi Rizzo 	int error = 0;
2309f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
231068b8534bSLuigi Rizzo 	struct netmap_if *nifp;
23112ff91c17SVincenzo Maffione 	struct netmap_kring **krings;
2312c3e9b4dbSLuiz Otavio O Souza 	int sync_flags;
2313847bf383SLuigi Rizzo 	enum txrx t;
231468b8534bSLuigi Rizzo 
23152ff91c17SVincenzo Maffione 	switch (cmd) {
23162ff91c17SVincenzo Maffione 	case NIOCCTRL: {
23172ff91c17SVincenzo Maffione 		struct nmreq_header *hdr = (struct nmreq_header *)data;
23182ff91c17SVincenzo Maffione 
23192ff91c17SVincenzo Maffione 		if (hdr->nr_version != NETMAP_API) {
23202ff91c17SVincenzo Maffione 			D("API mismatch for reqtype %d: got %d need %d",
23212ff91c17SVincenzo Maffione 				hdr->nr_version,
23222ff91c17SVincenzo Maffione 				hdr->nr_version, NETMAP_API);
23232ff91c17SVincenzo Maffione 			hdr->nr_version = NETMAP_API;
2324f0ea3689SLuigi Rizzo 		}
23252ff91c17SVincenzo Maffione 		if (hdr->nr_version < NETMAP_MIN_API ||
23262ff91c17SVincenzo Maffione 		    hdr->nr_version > NETMAP_MAX_API) {
232717885a7bSLuigi Rizzo 			return EINVAL;
232817885a7bSLuigi Rizzo 		}
232968b8534bSLuigi Rizzo 
23302ff91c17SVincenzo Maffione 		/* Make a kernel-space copy of the user-space nr_body.
23312ff91c17SVincenzo Maffione 		 * For convenince, the nr_body pointer and the pointers
23322ff91c17SVincenzo Maffione 		 * in the options list will be replaced with their
23332ff91c17SVincenzo Maffione 		 * kernel-space counterparts. The original pointers are
23342ff91c17SVincenzo Maffione 		 * saved internally and later restored by nmreq_copyout
23352ff91c17SVincenzo Maffione 		 */
23362ff91c17SVincenzo Maffione 		error = nmreq_copyin(hdr, nr_body_is_user);
233737e3a6d3SLuigi Rizzo 		if (error) {
23382ff91c17SVincenzo Maffione 			return error;
2339ce3ee1e7SLuigi Rizzo 		}
2340ce3ee1e7SLuigi Rizzo 
23412ff91c17SVincenzo Maffione 		/* Sanitize hdr->nr_name. */
23422ff91c17SVincenzo Maffione 		hdr->nr_name[sizeof(hdr->nr_name) - 1] = '\0';
234368b8534bSLuigi Rizzo 
23442ff91c17SVincenzo Maffione 		switch (hdr->nr_reqtype) {
23452ff91c17SVincenzo Maffione 		case NETMAP_REQ_REGISTER: {
23462ff91c17SVincenzo Maffione 			struct nmreq_register *req =
2347cfa866f6SMatt Macy 				(struct nmreq_register *)(uintptr_t)hdr->nr_body;
23482ff91c17SVincenzo Maffione 			/* Protect access to priv from concurrent requests. */
2349ce3ee1e7SLuigi Rizzo 			NMG_LOCK();
2350ce3ee1e7SLuigi Rizzo 			do {
2351ce3ee1e7SLuigi Rizzo 				u_int memflags;
23522ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
23532ff91c17SVincenzo Maffione 				struct nmreq_option *opt;
23542ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
2355ce3ee1e7SLuigi Rizzo 
2356847bf383SLuigi Rizzo 				if (priv->np_nifp != NULL) {	/* thread already registered */
2357f0ea3689SLuigi Rizzo 					error = EBUSY;
2358506cc70cSLuigi Rizzo 					break;
2359506cc70cSLuigi Rizzo 				}
2360c3e9b4dbSLuiz Otavio O Souza 
23612ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
2362cfa866f6SMatt Macy 				opt = nmreq_findoption((struct nmreq_option *)(uintptr_t)hdr->nr_options,
23632ff91c17SVincenzo Maffione 						NETMAP_REQ_OPT_EXTMEM);
23642ff91c17SVincenzo Maffione 				if (opt != NULL) {
23652ff91c17SVincenzo Maffione 					struct nmreq_opt_extmem *e =
23662ff91c17SVincenzo Maffione 						(struct nmreq_opt_extmem *)opt;
23672ff91c17SVincenzo Maffione 
23682ff91c17SVincenzo Maffione 					error = nmreq_checkduplicate(opt);
23692ff91c17SVincenzo Maffione 					if (error) {
23702ff91c17SVincenzo Maffione 						opt->nro_status = error;
23712ff91c17SVincenzo Maffione 						break;
23722ff91c17SVincenzo Maffione 					}
23732ff91c17SVincenzo Maffione 					nmd = netmap_mem_ext_create(e->nro_usrptr,
23742ff91c17SVincenzo Maffione 							&e->nro_info, &error);
23752ff91c17SVincenzo Maffione 					opt->nro_status = error;
23762ff91c17SVincenzo Maffione 					if (nmd == NULL)
23772ff91c17SVincenzo Maffione 						break;
23782ff91c17SVincenzo Maffione 				}
23792ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
23802ff91c17SVincenzo Maffione 
23812ff91c17SVincenzo Maffione 				if (nmd == NULL && req->nr_mem_id) {
2382c3e9b4dbSLuiz Otavio O Souza 					/* find the allocator and get a reference */
23832ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id);
2384c3e9b4dbSLuiz Otavio O Souza 					if (nmd == NULL) {
2385c3e9b4dbSLuiz Otavio O Souza 						error = EINVAL;
2386c3e9b4dbSLuiz Otavio O Souza 						break;
2387c3e9b4dbSLuiz Otavio O Souza 					}
2388c3e9b4dbSLuiz Otavio O Souza 				}
238968b8534bSLuigi Rizzo 				/* find the interface and a reference */
23902ff91c17SVincenzo Maffione 				error = netmap_get_na(hdr, &na, &ifp, nmd,
239137e3a6d3SLuigi Rizzo 						      1 /* create */); /* keep reference */
239268b8534bSLuigi Rizzo 				if (error)
2393ce3ee1e7SLuigi Rizzo 					break;
2394f9790aebSLuigi Rizzo 				if (NETMAP_OWNED_BY_KERN(na)) {
2395ce3ee1e7SLuigi Rizzo 					error = EBUSY;
2396ce3ee1e7SLuigi Rizzo 					break;
2397f196ce38SLuigi Rizzo 				}
239837e3a6d3SLuigi Rizzo 
23992ff91c17SVincenzo Maffione 				if (na->virt_hdr_len && !(req->nr_flags & NR_ACCEPT_VNET_HDR)) {
240037e3a6d3SLuigi Rizzo 					error = EIO;
240137e3a6d3SLuigi Rizzo 					break;
240237e3a6d3SLuigi Rizzo 				}
240337e3a6d3SLuigi Rizzo 
24042ff91c17SVincenzo Maffione 				error = netmap_do_regif(priv, na, req->nr_mode,
24052ff91c17SVincenzo Maffione 							req->nr_ringid, req->nr_flags);
2406847bf383SLuigi Rizzo 				if (error) {    /* reg. failed, release priv and ref */
2407ce3ee1e7SLuigi Rizzo 					break;
240868b8534bSLuigi Rizzo 				}
2409847bf383SLuigi Rizzo 				nifp = priv->np_nifp;
24102ff91c17SVincenzo Maffione 				priv->np_td = td; /* for debugging purposes */
241168b8534bSLuigi Rizzo 
241268b8534bSLuigi Rizzo 				/* return the offset of the netmap_if object */
24132ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
24142ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
24152ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
24162ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
24172ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(na->nm_mem, &req->nr_memsize, &memflags,
24182ff91c17SVincenzo Maffione 					&req->nr_mem_id);
2419ce3ee1e7SLuigi Rizzo 				if (error) {
2420847bf383SLuigi Rizzo 					netmap_do_unregif(priv);
2421ce3ee1e7SLuigi Rizzo 					break;
2422ce3ee1e7SLuigi Rizzo 				}
2423ce3ee1e7SLuigi Rizzo 				if (memflags & NETMAP_MEM_PRIVATE) {
24243d819cb6SLuigi Rizzo 					*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2425ce3ee1e7SLuigi Rizzo 				}
2426847bf383SLuigi Rizzo 				for_rx_tx(t) {
2427847bf383SLuigi Rizzo 					priv->np_si[t] = nm_si_user(priv, t) ?
24282ff91c17SVincenzo Maffione 						&na->si[t] : &NMR(na, t)[priv->np_qfirst[t]]->si;
2429847bf383SLuigi Rizzo 				}
2430f0ea3689SLuigi Rizzo 
24312ff91c17SVincenzo Maffione 				if (req->nr_extra_bufs) {
243237e3a6d3SLuigi Rizzo 					if (netmap_verbose)
24332ff91c17SVincenzo Maffione 						D("requested %d extra buffers",
24342ff91c17SVincenzo Maffione 							req->nr_extra_bufs);
24352ff91c17SVincenzo Maffione 					req->nr_extra_bufs = netmap_extra_alloc(na,
24362ff91c17SVincenzo Maffione 						&nifp->ni_bufs_head, req->nr_extra_bufs);
243737e3a6d3SLuigi Rizzo 					if (netmap_verbose)
24382ff91c17SVincenzo Maffione 						D("got %d extra buffers", req->nr_extra_bufs);
2439f0ea3689SLuigi Rizzo 				}
24402ff91c17SVincenzo Maffione 				req->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
24412ff91c17SVincenzo Maffione 
24422ff91c17SVincenzo Maffione 				error = nmreq_checkoptions(hdr);
24432ff91c17SVincenzo Maffione 				if (error) {
24442ff91c17SVincenzo Maffione 					netmap_do_unregif(priv);
24452ff91c17SVincenzo Maffione 					break;
24462ff91c17SVincenzo Maffione 				}
244737e3a6d3SLuigi Rizzo 
244837e3a6d3SLuigi Rizzo 				/* store ifp reference so that priv destructor may release it */
244937e3a6d3SLuigi Rizzo 				priv->np_ifp = ifp;
2450ce3ee1e7SLuigi Rizzo 			} while (0);
2451c3e9b4dbSLuiz Otavio O Souza 			if (error) {
2452c3e9b4dbSLuiz Otavio O Souza 				netmap_unget_na(na, ifp);
2453c3e9b4dbSLuiz Otavio O Souza 			}
2454c3e9b4dbSLuiz Otavio O Souza 			/* release the reference from netmap_mem_find() or
2455c3e9b4dbSLuiz Otavio O Souza 			 * netmap_mem_ext_create()
2456c3e9b4dbSLuiz Otavio O Souza 			 */
2457c3e9b4dbSLuiz Otavio O Souza 			if (nmd)
2458c3e9b4dbSLuiz Otavio O Souza 				netmap_mem_put(nmd);
2459ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
246068b8534bSLuigi Rizzo 			break;
24612ff91c17SVincenzo Maffione 		}
24622ff91c17SVincenzo Maffione 
24632ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_INFO_GET: {
24642ff91c17SVincenzo Maffione 			struct nmreq_port_info_get *req =
2465cfa866f6SMatt Macy 				(struct nmreq_port_info_get *)(uintptr_t)hdr->nr_body;
24662ff91c17SVincenzo Maffione 
24672ff91c17SVincenzo Maffione 			NMG_LOCK();
24682ff91c17SVincenzo Maffione 			do {
24692ff91c17SVincenzo Maffione 				u_int memflags;
24702ff91c17SVincenzo Maffione 
24712ff91c17SVincenzo Maffione 				if (hdr->nr_name[0] != '\0') {
24722ff91c17SVincenzo Maffione 					/* Build a nmreq_register out of the nmreq_port_info_get,
24732ff91c17SVincenzo Maffione 					 * so that we can call netmap_get_na(). */
24742ff91c17SVincenzo Maffione 					struct nmreq_register regreq;
24752ff91c17SVincenzo Maffione 					bzero(&regreq, sizeof(regreq));
24762ff91c17SVincenzo Maffione 					regreq.nr_tx_slots = req->nr_tx_slots;
24772ff91c17SVincenzo Maffione 					regreq.nr_rx_slots = req->nr_rx_slots;
24782ff91c17SVincenzo Maffione 					regreq.nr_tx_rings = req->nr_tx_rings;
24792ff91c17SVincenzo Maffione 					regreq.nr_rx_rings = req->nr_rx_rings;
24802ff91c17SVincenzo Maffione 					regreq.nr_mem_id = req->nr_mem_id;
24812ff91c17SVincenzo Maffione 
24822ff91c17SVincenzo Maffione 					/* get a refcount */
24832ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2484cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)&regreq;
24852ff91c17SVincenzo Maffione 					error = netmap_get_na(hdr, &na, &ifp, NULL, 1 /* create */);
24862ff91c17SVincenzo Maffione 					hdr->nr_reqtype = NETMAP_REQ_PORT_INFO_GET; /* reset type */
2487cfa866f6SMatt Macy 					hdr->nr_body = (uintptr_t)req; /* reset nr_body */
24882ff91c17SVincenzo Maffione 					if (error) {
24892ff91c17SVincenzo Maffione 						na = NULL;
24902ff91c17SVincenzo Maffione 						ifp = NULL;
24912ff91c17SVincenzo Maffione 						break;
24922ff91c17SVincenzo Maffione 					}
24932ff91c17SVincenzo Maffione 					nmd = na->nm_mem; /* get memory allocator */
24942ff91c17SVincenzo Maffione 				} else {
24952ff91c17SVincenzo Maffione 					nmd = netmap_mem_find(req->nr_mem_id ? req->nr_mem_id : 1);
24962ff91c17SVincenzo Maffione 					if (nmd == NULL) {
24972ff91c17SVincenzo Maffione 						error = EINVAL;
24982ff91c17SVincenzo Maffione 						break;
24992ff91c17SVincenzo Maffione 					}
25002ff91c17SVincenzo Maffione 				}
25012ff91c17SVincenzo Maffione 
25022ff91c17SVincenzo Maffione 				error = netmap_mem_get_info(nmd, &req->nr_memsize, &memflags,
25032ff91c17SVincenzo Maffione 					&req->nr_mem_id);
25042ff91c17SVincenzo Maffione 				if (error)
25052ff91c17SVincenzo Maffione 					break;
25062ff91c17SVincenzo Maffione 				if (na == NULL) /* only memory info */
25072ff91c17SVincenzo Maffione 					break;
25082ff91c17SVincenzo Maffione 				req->nr_offset = 0;
25092ff91c17SVincenzo Maffione 				req->nr_rx_slots = req->nr_tx_slots = 0;
25102ff91c17SVincenzo Maffione 				netmap_update_config(na);
25112ff91c17SVincenzo Maffione 				req->nr_rx_rings = na->num_rx_rings;
25122ff91c17SVincenzo Maffione 				req->nr_tx_rings = na->num_tx_rings;
25132ff91c17SVincenzo Maffione 				req->nr_rx_slots = na->num_rx_desc;
25142ff91c17SVincenzo Maffione 				req->nr_tx_slots = na->num_tx_desc;
25152ff91c17SVincenzo Maffione 			} while (0);
25162ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
25172ff91c17SVincenzo Maffione 			NMG_UNLOCK();
25182ff91c17SVincenzo Maffione 			break;
25192ff91c17SVincenzo Maffione 		}
25202ff91c17SVincenzo Maffione #ifdef WITH_VALE
25212ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_ATTACH: {
25222ff91c17SVincenzo Maffione 			error = nm_bdg_ctl_attach(hdr, NULL /* userspace request */);
25232ff91c17SVincenzo Maffione 			break;
25242ff91c17SVincenzo Maffione 		}
25252ff91c17SVincenzo Maffione 
25262ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DETACH: {
25272ff91c17SVincenzo Maffione 			error = nm_bdg_ctl_detach(hdr, NULL /* userspace request */);
25282ff91c17SVincenzo Maffione 			break;
25292ff91c17SVincenzo Maffione 		}
25302ff91c17SVincenzo Maffione 
25312ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_LIST: {
25322ff91c17SVincenzo Maffione 			error = netmap_bdg_list(hdr);
25332ff91c17SVincenzo Maffione 			break;
25342ff91c17SVincenzo Maffione 		}
25352ff91c17SVincenzo Maffione 
25362ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_SET: {
25372ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2538cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
25392ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
25402ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
25412ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
25422ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
25432ff91c17SVincenzo Maffione 			/* For now we only support virtio-net headers, and only for
25442ff91c17SVincenzo Maffione 			 * VALE ports, but this may change in future. Valid lengths
25452ff91c17SVincenzo Maffione 			 * for the virtio-net header are 0 (no header), 10 and 12. */
25462ff91c17SVincenzo Maffione 			if (req->nr_hdr_len != 0 &&
25472ff91c17SVincenzo Maffione 				req->nr_hdr_len != sizeof(struct nm_vnet_hdr) &&
25482ff91c17SVincenzo Maffione 					req->nr_hdr_len != 12) {
25492ff91c17SVincenzo Maffione 				error = EINVAL;
25502ff91c17SVincenzo Maffione 				break;
25512ff91c17SVincenzo Maffione 			}
25522ff91c17SVincenzo Maffione 			NMG_LOCK();
25532ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2554cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
2555*2a7db7a6SVincenzo Maffione 			error = netmap_get_vale_na(hdr, &na, NULL, 0);
25562ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_SET;
2557cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
25582ff91c17SVincenzo Maffione 			if (na && !error) {
25592ff91c17SVincenzo Maffione 				struct netmap_vp_adapter *vpna =
25602ff91c17SVincenzo Maffione 					(struct netmap_vp_adapter *)na;
25612ff91c17SVincenzo Maffione 				na->virt_hdr_len = req->nr_hdr_len;
25622ff91c17SVincenzo Maffione 				if (na->virt_hdr_len) {
25632ff91c17SVincenzo Maffione 					vpna->mfs = NETMAP_BUF_SIZE(na);
25642ff91c17SVincenzo Maffione 				}
25652ff91c17SVincenzo Maffione 				D("Using vnet_hdr_len %d for %p", na->virt_hdr_len, na);
25662ff91c17SVincenzo Maffione 				netmap_adapter_put(na);
25672ff91c17SVincenzo Maffione 			} else if (!na) {
25682ff91c17SVincenzo Maffione 				error = ENXIO;
25692ff91c17SVincenzo Maffione 			}
25702ff91c17SVincenzo Maffione 			NMG_UNLOCK();
25712ff91c17SVincenzo Maffione 			break;
25722ff91c17SVincenzo Maffione 		}
25732ff91c17SVincenzo Maffione 
25742ff91c17SVincenzo Maffione 		case NETMAP_REQ_PORT_HDR_GET: {
25752ff91c17SVincenzo Maffione 			/* Get vnet-header length for this netmap port */
25762ff91c17SVincenzo Maffione 			struct nmreq_port_hdr *req =
2577cfa866f6SMatt Macy 				(struct nmreq_port_hdr *)(uintptr_t)hdr->nr_body;
25782ff91c17SVincenzo Maffione 			/* Build a nmreq_register out of the nmreq_port_hdr,
25792ff91c17SVincenzo Maffione 			 * so that we can call netmap_get_bdg_na(). */
25802ff91c17SVincenzo Maffione 			struct nmreq_register regreq;
25812ff91c17SVincenzo Maffione 			struct ifnet *ifp;
25822ff91c17SVincenzo Maffione 
25832ff91c17SVincenzo Maffione 			bzero(&regreq, sizeof(regreq));
25842ff91c17SVincenzo Maffione 			NMG_LOCK();
25852ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_REGISTER;
2586cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)&regreq;
25872ff91c17SVincenzo Maffione 			error = netmap_get_na(hdr, &na, &ifp, NULL, 0);
25882ff91c17SVincenzo Maffione 			hdr->nr_reqtype = NETMAP_REQ_PORT_HDR_GET;
2589cfa866f6SMatt Macy 			hdr->nr_body = (uintptr_t)req;
25902ff91c17SVincenzo Maffione 			if (na && !error) {
25912ff91c17SVincenzo Maffione 				req->nr_hdr_len = na->virt_hdr_len;
25922ff91c17SVincenzo Maffione 			}
25932ff91c17SVincenzo Maffione 			netmap_unget_na(na, ifp);
25942ff91c17SVincenzo Maffione 			NMG_UNLOCK();
25952ff91c17SVincenzo Maffione 			break;
25962ff91c17SVincenzo Maffione 		}
25972ff91c17SVincenzo Maffione 
25982ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_NEWIF: {
25992ff91c17SVincenzo Maffione 			error = nm_vi_create(hdr);
26002ff91c17SVincenzo Maffione 			break;
26012ff91c17SVincenzo Maffione 		}
26022ff91c17SVincenzo Maffione 
26032ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_DELIF: {
26042ff91c17SVincenzo Maffione 			error = nm_vi_destroy(hdr->nr_name);
26052ff91c17SVincenzo Maffione 			break;
26062ff91c17SVincenzo Maffione 		}
26072ff91c17SVincenzo Maffione 
26082ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_ENABLE:
26092ff91c17SVincenzo Maffione 		case NETMAP_REQ_VALE_POLLING_DISABLE: {
26102ff91c17SVincenzo Maffione 			error = nm_bdg_polling(hdr);
26112ff91c17SVincenzo Maffione 			break;
26122ff91c17SVincenzo Maffione 		}
26132ff91c17SVincenzo Maffione #endif  /* WITH_VALE */
26142ff91c17SVincenzo Maffione 		case NETMAP_REQ_POOLS_INFO_GET: {
26152ff91c17SVincenzo Maffione 			struct nmreq_pools_info *req =
2616cfa866f6SMatt Macy 				(struct nmreq_pools_info *)(uintptr_t)hdr->nr_body;
26172ff91c17SVincenzo Maffione 			/* Get information from the memory allocator. This
26182ff91c17SVincenzo Maffione 			 * netmap device must already be bound to a port.
26192ff91c17SVincenzo Maffione 			 * Note that hdr->nr_name is ignored. */
26202ff91c17SVincenzo Maffione 			NMG_LOCK();
26212ff91c17SVincenzo Maffione 			if (priv->np_na && priv->np_na->nm_mem) {
26222ff91c17SVincenzo Maffione 				struct netmap_mem_d *nmd = priv->np_na->nm_mem;
26232ff91c17SVincenzo Maffione 				error = netmap_mem_pools_info_get(req, nmd);
26242ff91c17SVincenzo Maffione 			} else {
26252ff91c17SVincenzo Maffione 				error = EINVAL;
26262ff91c17SVincenzo Maffione 			}
26272ff91c17SVincenzo Maffione 			NMG_UNLOCK();
26282ff91c17SVincenzo Maffione 			break;
26292ff91c17SVincenzo Maffione 		}
26302ff91c17SVincenzo Maffione 
26312ff91c17SVincenzo Maffione 		default: {
26322ff91c17SVincenzo Maffione 			error = EINVAL;
26332ff91c17SVincenzo Maffione 			break;
26342ff91c17SVincenzo Maffione 		}
26352ff91c17SVincenzo Maffione 		}
26362ff91c17SVincenzo Maffione 		/* Write back request body to userspace and reset the
26372ff91c17SVincenzo Maffione 		 * user-space pointer. */
26382ff91c17SVincenzo Maffione 		error = nmreq_copyout(hdr, error);
26392ff91c17SVincenzo Maffione 		break;
26402ff91c17SVincenzo Maffione 	}
264168b8534bSLuigi Rizzo 
264268b8534bSLuigi Rizzo 	case NIOCTXSYNC:
26432ff91c17SVincenzo Maffione 	case NIOCRXSYNC: {
26448241616dSLuigi Rizzo 		nifp = priv->np_nifp;
26458241616dSLuigi Rizzo 
26468241616dSLuigi Rizzo 		if (nifp == NULL) {
2647506cc70cSLuigi Rizzo 			error = ENXIO;
2648506cc70cSLuigi Rizzo 			break;
2649506cc70cSLuigi Rizzo 		}
26506641c68bSLuigi Rizzo 		mb(); /* make sure following reads are not from cache */
26518241616dSLuigi Rizzo 
2652f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
26538241616dSLuigi Rizzo 
2654f9790aebSLuigi Rizzo 		if (na == NULL) {
2655f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
26568241616dSLuigi Rizzo 			error = ENXIO;
26578241616dSLuigi Rizzo 			break;
26588241616dSLuigi Rizzo 		}
26598241616dSLuigi Rizzo 
2660c3e9b4dbSLuiz Otavio O Souza 		mbq_init(&q);
2661847bf383SLuigi Rizzo 		t = (cmd == NIOCTXSYNC ? NR_TX : NR_RX);
2662847bf383SLuigi Rizzo 		krings = NMR(na, t);
2663847bf383SLuigi Rizzo 		qfirst = priv->np_qfirst[t];
2664847bf383SLuigi Rizzo 		qlast = priv->np_qlast[t];
2665c3e9b4dbSLuiz Otavio O Souza 		sync_flags = priv->np_sync_flags;
266668b8534bSLuigi Rizzo 
2667f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
26682ff91c17SVincenzo Maffione 			struct netmap_kring *kring = krings[i];
266937e3a6d3SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
267037e3a6d3SLuigi Rizzo 
267137e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &error))) {
267237e3a6d3SLuigi Rizzo 				error = (error ? EIO : 0);
267337e3a6d3SLuigi Rizzo 				continue;
2674ce3ee1e7SLuigi Rizzo 			}
267537e3a6d3SLuigi Rizzo 
267668b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
267768b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
26783c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
267937e3a6d3SLuigi Rizzo 					    i, ring->cur,
268068b8534bSLuigi Rizzo 					    kring->nr_hwcur);
268137e3a6d3SLuigi Rizzo 				if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
268217885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
2683c3e9b4dbSLuiz Otavio O Souza 				} else if (kring->nm_sync(kring, sync_flags | NAF_FORCE_RECLAIM) == 0) {
268437e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
268517885a7bSLuigi Rizzo 				}
268668b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
26873c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
268837e3a6d3SLuigi Rizzo 					    i, ring->cur,
268968b8534bSLuigi Rizzo 					    kring->nr_hwcur);
269068b8534bSLuigi Rizzo 			} else {
269137e3a6d3SLuigi Rizzo 				if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
2692847bf383SLuigi Rizzo 					netmap_ring_reinit(kring);
2693c3e9b4dbSLuiz Otavio O Souza 				}
2694c3e9b4dbSLuiz Otavio O Souza 				if (nm_may_forward_up(kring)) {
2695c3e9b4dbSLuiz Otavio O Souza 					/* transparent forwarding, see netmap_poll() */
2696c3e9b4dbSLuiz Otavio O Souza 					netmap_grab_packets(kring, &q, netmap_fwd);
2697c3e9b4dbSLuiz Otavio O Souza 				}
2698c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags | NAF_FORCE_READ) == 0) {
269937e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
2700847bf383SLuigi Rizzo 				}
2701c3e9b4dbSLuiz Otavio O Souza 				ring_timestamp_set(ring);
270268b8534bSLuigi Rizzo 			}
2703ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
270468b8534bSLuigi Rizzo 		}
270568b8534bSLuigi Rizzo 
2706c3e9b4dbSLuiz Otavio O Souza 		if (mbq_peek(&q)) {
2707c3e9b4dbSLuiz Otavio O Souza 			netmap_send_up(na->ifp, &q);
2708c3e9b4dbSLuiz Otavio O Souza 		}
2709c3e9b4dbSLuiz Otavio O Souza 
271068b8534bSLuigi Rizzo 		break;
271168b8534bSLuigi Rizzo 	}
2712f196ce38SLuigi Rizzo 
27132ff91c17SVincenzo Maffione 	default: {
27142ff91c17SVincenzo Maffione 		return netmap_ioctl_legacy(priv, cmd, data, td);
27152ff91c17SVincenzo Maffione 		break;
27162ff91c17SVincenzo Maffione 	}
271768b8534bSLuigi Rizzo 	}
271868b8534bSLuigi Rizzo 
271968b8534bSLuigi Rizzo 	return (error);
272068b8534bSLuigi Rizzo }
272168b8534bSLuigi Rizzo 
27222ff91c17SVincenzo Maffione size_t
27232ff91c17SVincenzo Maffione nmreq_size_by_type(uint16_t nr_reqtype)
27242ff91c17SVincenzo Maffione {
27252ff91c17SVincenzo Maffione 	switch (nr_reqtype) {
27262ff91c17SVincenzo Maffione 	case NETMAP_REQ_REGISTER:
27272ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_register);
27282ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_INFO_GET:
27292ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_info_get);
27302ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_ATTACH:
27312ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_attach);
27322ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DETACH:
27332ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_detach);
27342ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_LIST:
27352ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_list);
27362ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_SET:
27372ff91c17SVincenzo Maffione 	case NETMAP_REQ_PORT_HDR_GET:
27382ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_port_hdr);
27392ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_NEWIF:
27402ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_newif);
27412ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_DELIF:
27422ff91c17SVincenzo Maffione 		return 0;
27432ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_ENABLE:
27442ff91c17SVincenzo Maffione 	case NETMAP_REQ_VALE_POLLING_DISABLE:
27452ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_vale_polling);
27462ff91c17SVincenzo Maffione 	case NETMAP_REQ_POOLS_INFO_GET:
27472ff91c17SVincenzo Maffione 		return sizeof(struct nmreq_pools_info);
27482ff91c17SVincenzo Maffione 	}
27492ff91c17SVincenzo Maffione 	return 0;
27502ff91c17SVincenzo Maffione }
27512ff91c17SVincenzo Maffione 
27522ff91c17SVincenzo Maffione static size_t
27532ff91c17SVincenzo Maffione nmreq_opt_size_by_type(uint16_t nro_reqtype)
27542ff91c17SVincenzo Maffione {
27552ff91c17SVincenzo Maffione 	size_t rv = sizeof(struct nmreq_option);
27562ff91c17SVincenzo Maffione #ifdef NETMAP_REQ_OPT_DEBUG
27572ff91c17SVincenzo Maffione 	if (nro_reqtype & NETMAP_REQ_OPT_DEBUG)
27582ff91c17SVincenzo Maffione 		return (nro_reqtype & ~NETMAP_REQ_OPT_DEBUG);
27592ff91c17SVincenzo Maffione #endif /* NETMAP_REQ_OPT_DEBUG */
27602ff91c17SVincenzo Maffione 	switch (nro_reqtype) {
27612ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
27622ff91c17SVincenzo Maffione 	case NETMAP_REQ_OPT_EXTMEM:
27632ff91c17SVincenzo Maffione 		rv = sizeof(struct nmreq_opt_extmem);
27642ff91c17SVincenzo Maffione 		break;
27652ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
27662ff91c17SVincenzo Maffione 	}
27672ff91c17SVincenzo Maffione 	/* subtract the common header */
27682ff91c17SVincenzo Maffione 	return rv - sizeof(struct nmreq_option);
27692ff91c17SVincenzo Maffione }
27702ff91c17SVincenzo Maffione 
27712ff91c17SVincenzo Maffione int
27722ff91c17SVincenzo Maffione nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user)
27732ff91c17SVincenzo Maffione {
27742ff91c17SVincenzo Maffione 	size_t rqsz, optsz, bufsz;
27752ff91c17SVincenzo Maffione 	int error;
27762ff91c17SVincenzo Maffione 	char *ker = NULL, *p;
27772ff91c17SVincenzo Maffione 	struct nmreq_option **next, *src;
27782ff91c17SVincenzo Maffione 	struct nmreq_option buf;
27792ff91c17SVincenzo Maffione 	uint64_t *ptrs;
27802ff91c17SVincenzo Maffione 
27812ff91c17SVincenzo Maffione 	if (hdr->nr_reserved)
27822ff91c17SVincenzo Maffione 		return EINVAL;
27832ff91c17SVincenzo Maffione 
27842ff91c17SVincenzo Maffione 	if (!nr_body_is_user)
27852ff91c17SVincenzo Maffione 		return 0;
27862ff91c17SVincenzo Maffione 
27872ff91c17SVincenzo Maffione 	hdr->nr_reserved = nr_body_is_user;
27882ff91c17SVincenzo Maffione 
27892ff91c17SVincenzo Maffione 	/* compute the total size of the buffer */
27902ff91c17SVincenzo Maffione 	rqsz = nmreq_size_by_type(hdr->nr_reqtype);
27912ff91c17SVincenzo Maffione 	if (rqsz > NETMAP_REQ_MAXSIZE) {
27922ff91c17SVincenzo Maffione 		error = EMSGSIZE;
27932ff91c17SVincenzo Maffione 		goto out_err;
27942ff91c17SVincenzo Maffione 	}
2795cfa866f6SMatt Macy 	if ((rqsz && hdr->nr_body == (uintptr_t)NULL) ||
2796cfa866f6SMatt Macy 		(!rqsz && hdr->nr_body != (uintptr_t)NULL)) {
27972ff91c17SVincenzo Maffione 		/* Request body expected, but not found; or
27982ff91c17SVincenzo Maffione 		 * request body found but unexpected. */
27992ff91c17SVincenzo Maffione 		error = EINVAL;
28002ff91c17SVincenzo Maffione 		goto out_err;
28012ff91c17SVincenzo Maffione 	}
28022ff91c17SVincenzo Maffione 
28032ff91c17SVincenzo Maffione 	bufsz = 2 * sizeof(void *) + rqsz;
28042ff91c17SVincenzo Maffione 	optsz = 0;
2805cfa866f6SMatt Macy 	for (src = (struct nmreq_option *)(uintptr_t)hdr->nr_options; src;
2806cfa866f6SMatt Macy 	     src = (struct nmreq_option *)(uintptr_t)buf.nro_next)
28072ff91c17SVincenzo Maffione 	{
28082ff91c17SVincenzo Maffione 		error = copyin(src, &buf, sizeof(*src));
28092ff91c17SVincenzo Maffione 		if (error)
28102ff91c17SVincenzo Maffione 			goto out_err;
28112ff91c17SVincenzo Maffione 		optsz += sizeof(*src);
28122ff91c17SVincenzo Maffione 		optsz += nmreq_opt_size_by_type(buf.nro_reqtype);
28132ff91c17SVincenzo Maffione 		if (rqsz + optsz > NETMAP_REQ_MAXSIZE) {
28142ff91c17SVincenzo Maffione 			error = EMSGSIZE;
28152ff91c17SVincenzo Maffione 			goto out_err;
28162ff91c17SVincenzo Maffione 		}
28172ff91c17SVincenzo Maffione 		bufsz += optsz + sizeof(void *);
28182ff91c17SVincenzo Maffione 	}
28192ff91c17SVincenzo Maffione 
28202ff91c17SVincenzo Maffione 	ker = nm_os_malloc(bufsz);
28212ff91c17SVincenzo Maffione 	if (ker == NULL) {
28222ff91c17SVincenzo Maffione 		error = ENOMEM;
28232ff91c17SVincenzo Maffione 		goto out_err;
28242ff91c17SVincenzo Maffione 	}
28252ff91c17SVincenzo Maffione 	p = ker;
28262ff91c17SVincenzo Maffione 
28272ff91c17SVincenzo Maffione 	/* make a copy of the user pointers */
28282ff91c17SVincenzo Maffione 	ptrs = (uint64_t*)p;
28292ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_body;
28302ff91c17SVincenzo Maffione 	*ptrs++ = hdr->nr_options;
28312ff91c17SVincenzo Maffione 	p = (char *)ptrs;
28322ff91c17SVincenzo Maffione 
28332ff91c17SVincenzo Maffione 	/* copy the body */
2834cfa866f6SMatt Macy 	error = copyin((void *)(uintptr_t)hdr->nr_body, p, rqsz);
28352ff91c17SVincenzo Maffione 	if (error)
28362ff91c17SVincenzo Maffione 		goto out_restore;
28372ff91c17SVincenzo Maffione 	/* overwrite the user pointer with the in-kernel one */
2838cfa866f6SMatt Macy 	hdr->nr_body = (uintptr_t)p;
28392ff91c17SVincenzo Maffione 	p += rqsz;
28402ff91c17SVincenzo Maffione 
28412ff91c17SVincenzo Maffione 	/* copy the options */
28422ff91c17SVincenzo Maffione 	next = (struct nmreq_option **)&hdr->nr_options;
28432ff91c17SVincenzo Maffione 	src = *next;
28442ff91c17SVincenzo Maffione 	while (src) {
28452ff91c17SVincenzo Maffione 		struct nmreq_option *opt;
28462ff91c17SVincenzo Maffione 
28472ff91c17SVincenzo Maffione 		/* copy the option header */
28482ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)p;
28492ff91c17SVincenzo Maffione 		opt = (struct nmreq_option *)(ptrs + 1);
28502ff91c17SVincenzo Maffione 		error = copyin(src, opt, sizeof(*src));
28512ff91c17SVincenzo Maffione 		if (error)
28522ff91c17SVincenzo Maffione 			goto out_restore;
28532ff91c17SVincenzo Maffione 		/* make a copy of the user next pointer */
28542ff91c17SVincenzo Maffione 		*ptrs = opt->nro_next;
28552ff91c17SVincenzo Maffione 		/* overwrite the user pointer with the in-kernel one */
28562ff91c17SVincenzo Maffione 		*next = opt;
28572ff91c17SVincenzo Maffione 
28582ff91c17SVincenzo Maffione 		/* initialize the option as not supported.
28592ff91c17SVincenzo Maffione 		 * Recognized options will update this field.
28602ff91c17SVincenzo Maffione 		 */
28612ff91c17SVincenzo Maffione 		opt->nro_status = EOPNOTSUPP;
28622ff91c17SVincenzo Maffione 
28632ff91c17SVincenzo Maffione 		p = (char *)(opt + 1);
28642ff91c17SVincenzo Maffione 
28652ff91c17SVincenzo Maffione 		/* copy the option body */
28662ff91c17SVincenzo Maffione 		optsz = nmreq_opt_size_by_type(opt->nro_reqtype);
28672ff91c17SVincenzo Maffione 		if (optsz) {
28682ff91c17SVincenzo Maffione 			/* the option body follows the option header */
28692ff91c17SVincenzo Maffione 			error = copyin(src + 1, p, optsz);
28702ff91c17SVincenzo Maffione 			if (error)
28712ff91c17SVincenzo Maffione 				goto out_restore;
28722ff91c17SVincenzo Maffione 			p += optsz;
28732ff91c17SVincenzo Maffione 		}
28742ff91c17SVincenzo Maffione 
28752ff91c17SVincenzo Maffione 		/* move to next option */
28762ff91c17SVincenzo Maffione 		next = (struct nmreq_option **)&opt->nro_next;
28772ff91c17SVincenzo Maffione 		src = *next;
28782ff91c17SVincenzo Maffione 	}
28792ff91c17SVincenzo Maffione 	return 0;
28802ff91c17SVincenzo Maffione 
28812ff91c17SVincenzo Maffione out_restore:
28822ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker;
28832ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
28842ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs++;
28852ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
28862ff91c17SVincenzo Maffione 	nm_os_free(ker);
28872ff91c17SVincenzo Maffione out_err:
28882ff91c17SVincenzo Maffione 	return error;
28892ff91c17SVincenzo Maffione }
28902ff91c17SVincenzo Maffione 
28912ff91c17SVincenzo Maffione static int
28922ff91c17SVincenzo Maffione nmreq_copyout(struct nmreq_header *hdr, int rerror)
28932ff91c17SVincenzo Maffione {
28942ff91c17SVincenzo Maffione 	struct nmreq_option *src, *dst;
2895cfa866f6SMatt Macy 	void *ker = (void *)(uintptr_t)hdr->nr_body, *bufstart;
28962ff91c17SVincenzo Maffione 	uint64_t *ptrs;
28972ff91c17SVincenzo Maffione 	size_t bodysz;
28982ff91c17SVincenzo Maffione 	int error;
28992ff91c17SVincenzo Maffione 
29002ff91c17SVincenzo Maffione 	if (!hdr->nr_reserved)
29012ff91c17SVincenzo Maffione 		return rerror;
29022ff91c17SVincenzo Maffione 
29032ff91c17SVincenzo Maffione 	/* restore the user pointers in the header */
29042ff91c17SVincenzo Maffione 	ptrs = (uint64_t *)ker - 2;
29052ff91c17SVincenzo Maffione 	bufstart = ptrs;
29062ff91c17SVincenzo Maffione 	hdr->nr_body = *ptrs++;
2907cfa866f6SMatt Macy 	src = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
29082ff91c17SVincenzo Maffione 	hdr->nr_options = *ptrs;
29092ff91c17SVincenzo Maffione 
29102ff91c17SVincenzo Maffione 	if (!rerror) {
29112ff91c17SVincenzo Maffione 		/* copy the body */
29122ff91c17SVincenzo Maffione 		bodysz = nmreq_size_by_type(hdr->nr_reqtype);
2913cfa866f6SMatt Macy 		error = copyout(ker, (void *)(uintptr_t)hdr->nr_body, bodysz);
29142ff91c17SVincenzo Maffione 		if (error) {
29152ff91c17SVincenzo Maffione 			rerror = error;
29162ff91c17SVincenzo Maffione 			goto out;
29172ff91c17SVincenzo Maffione 		}
29182ff91c17SVincenzo Maffione 	}
29192ff91c17SVincenzo Maffione 
29202ff91c17SVincenzo Maffione 	/* copy the options */
2921cfa866f6SMatt Macy 	dst = (struct nmreq_option *)(uintptr_t)hdr->nr_options;
29222ff91c17SVincenzo Maffione 	while (src) {
29232ff91c17SVincenzo Maffione 		size_t optsz;
29242ff91c17SVincenzo Maffione 		uint64_t next;
29252ff91c17SVincenzo Maffione 
29262ff91c17SVincenzo Maffione 		/* restore the user pointer */
29272ff91c17SVincenzo Maffione 		next = src->nro_next;
29282ff91c17SVincenzo Maffione 		ptrs = (uint64_t *)src - 1;
29292ff91c17SVincenzo Maffione 		src->nro_next = *ptrs;
29302ff91c17SVincenzo Maffione 
29312ff91c17SVincenzo Maffione 		/* always copy the option header */
29322ff91c17SVincenzo Maffione 		error = copyout(src, dst, sizeof(*src));
29332ff91c17SVincenzo Maffione 		if (error) {
29342ff91c17SVincenzo Maffione 			rerror = error;
29352ff91c17SVincenzo Maffione 			goto out;
29362ff91c17SVincenzo Maffione 		}
29372ff91c17SVincenzo Maffione 
29382ff91c17SVincenzo Maffione 		/* copy the option body only if there was no error */
29392ff91c17SVincenzo Maffione 		if (!rerror && !src->nro_status) {
29402ff91c17SVincenzo Maffione 			optsz = nmreq_opt_size_by_type(src->nro_reqtype);
29412ff91c17SVincenzo Maffione 			if (optsz) {
29422ff91c17SVincenzo Maffione 				error = copyout(src + 1, dst + 1, optsz);
29432ff91c17SVincenzo Maffione 				if (error) {
29442ff91c17SVincenzo Maffione 					rerror = error;
29452ff91c17SVincenzo Maffione 					goto out;
29462ff91c17SVincenzo Maffione 				}
29472ff91c17SVincenzo Maffione 			}
29482ff91c17SVincenzo Maffione 		}
2949cfa866f6SMatt Macy 		src = (struct nmreq_option *)(uintptr_t)next;
2950cfa866f6SMatt Macy 		dst = (struct nmreq_option *)(uintptr_t)*ptrs;
29512ff91c17SVincenzo Maffione 	}
29522ff91c17SVincenzo Maffione 
29532ff91c17SVincenzo Maffione 
29542ff91c17SVincenzo Maffione out:
29552ff91c17SVincenzo Maffione 	hdr->nr_reserved = 0;
29562ff91c17SVincenzo Maffione 	nm_os_free(bufstart);
29572ff91c17SVincenzo Maffione 	return rerror;
29582ff91c17SVincenzo Maffione }
29592ff91c17SVincenzo Maffione 
29602ff91c17SVincenzo Maffione struct nmreq_option *
29612ff91c17SVincenzo Maffione nmreq_findoption(struct nmreq_option *opt, uint16_t reqtype)
29622ff91c17SVincenzo Maffione {
2963cfa866f6SMatt Macy 	for ( ; opt; opt = (struct nmreq_option *)(uintptr_t)opt->nro_next)
29642ff91c17SVincenzo Maffione 		if (opt->nro_reqtype == reqtype)
29652ff91c17SVincenzo Maffione 			return opt;
29662ff91c17SVincenzo Maffione 	return NULL;
29672ff91c17SVincenzo Maffione }
29682ff91c17SVincenzo Maffione 
29692ff91c17SVincenzo Maffione int
29702ff91c17SVincenzo Maffione nmreq_checkduplicate(struct nmreq_option *opt) {
29712ff91c17SVincenzo Maffione 	uint16_t type = opt->nro_reqtype;
29722ff91c17SVincenzo Maffione 	int dup = 0;
29732ff91c17SVincenzo Maffione 
2974cfa866f6SMatt Macy 	while ((opt = nmreq_findoption((struct nmreq_option *)(uintptr_t)opt->nro_next,
29752ff91c17SVincenzo Maffione 			type))) {
29762ff91c17SVincenzo Maffione 		dup++;
29772ff91c17SVincenzo Maffione 		opt->nro_status = EINVAL;
29782ff91c17SVincenzo Maffione 	}
29792ff91c17SVincenzo Maffione 	return (dup ? EINVAL : 0);
29802ff91c17SVincenzo Maffione }
29812ff91c17SVincenzo Maffione 
29822ff91c17SVincenzo Maffione static int
29832ff91c17SVincenzo Maffione nmreq_checkoptions(struct nmreq_header *hdr)
29842ff91c17SVincenzo Maffione {
29852ff91c17SVincenzo Maffione 	struct nmreq_option *opt;
29862ff91c17SVincenzo Maffione 	/* return error if there is still any option
29872ff91c17SVincenzo Maffione 	 * marked as not supported
29882ff91c17SVincenzo Maffione 	 */
29892ff91c17SVincenzo Maffione 
2990cfa866f6SMatt Macy 	for (opt = (struct nmreq_option *)(uintptr_t)hdr->nr_options; opt;
2991cfa866f6SMatt Macy 	     opt = (struct nmreq_option *)(uintptr_t)opt->nro_next)
29922ff91c17SVincenzo Maffione 		if (opt->nro_status == EOPNOTSUPP)
29932ff91c17SVincenzo Maffione 			return EOPNOTSUPP;
29942ff91c17SVincenzo Maffione 
29952ff91c17SVincenzo Maffione 	return 0;
29962ff91c17SVincenzo Maffione }
299768b8534bSLuigi Rizzo 
299868b8534bSLuigi Rizzo /*
299968b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
300068b8534bSLuigi Rizzo  *
300168b8534bSLuigi Rizzo  * Can be called for one or more queues.
300268b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
300368b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
3004ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
300568b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
300668b8534bSLuigi Rizzo  * are done through callbacks.
3007f196ce38SLuigi Rizzo  *
300801c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
300901c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
301001c7d25fSLuigi Rizzo  * hidden argument.
301168b8534bSLuigi Rizzo  */
3012f9790aebSLuigi Rizzo int
301337e3a6d3SLuigi Rizzo netmap_poll(struct netmap_priv_d *priv, int events, NM_SELRECORD_T *sr)
301468b8534bSLuigi Rizzo {
301568b8534bSLuigi Rizzo 	struct netmap_adapter *na;
301668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
301737e3a6d3SLuigi Rizzo 	struct netmap_ring *ring;
3018847bf383SLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want[NR_TXRX], revents = 0;
3019847bf383SLuigi Rizzo #define want_tx want[NR_TX]
3020847bf383SLuigi Rizzo #define want_rx want[NR_RX]
3021c3e9b4dbSLuiz Otavio O Souza 	struct mbq q;	/* packets from RX hw queues to host stack */
302201c7d25fSLuigi Rizzo 
3023f9790aebSLuigi Rizzo 	/*
3024f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
3025f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
3026f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
3027f9790aebSLuigi Rizzo 	 */
302817885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
3029ce3ee1e7SLuigi Rizzo 
3030c3e9b4dbSLuiz Otavio O Souza 	/* Transparent mode: send_down is 1 if we have found some
3031c3e9b4dbSLuiz Otavio O Souza 	 * packets to forward (host RX ring --> NIC) during the rx
3032c3e9b4dbSLuiz Otavio O Souza 	 * scan and we have not sent them down to the NIC yet.
3033c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode requires to bind all rings to a single
3034c3e9b4dbSLuiz Otavio O Souza 	 * file descriptor.
3035f0ea3689SLuigi Rizzo 	 */
303637e3a6d3SLuigi Rizzo 	int send_down = 0;
3037c3e9b4dbSLuiz Otavio O Souza 	int sync_flags = priv->np_sync_flags;
303837e3a6d3SLuigi Rizzo 
303937e3a6d3SLuigi Rizzo 	mbq_init(&q);
304068b8534bSLuigi Rizzo 
30418241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
30428241616dSLuigi Rizzo 		D("No if registered");
30438241616dSLuigi Rizzo 		return POLLERR;
30448241616dSLuigi Rizzo 	}
3045847bf383SLuigi Rizzo 	mb(); /* make sure following reads are not from cache */
30468241616dSLuigi Rizzo 
3047f9790aebSLuigi Rizzo 	na = priv->np_na;
3048f9790aebSLuigi Rizzo 
30494bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
305068b8534bSLuigi Rizzo 		return POLLERR;
305168b8534bSLuigi Rizzo 
305268b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
30534bf50f18SLuigi Rizzo 		D("device %s events 0x%x", na->name, events);
305468b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
305568b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
305668b8534bSLuigi Rizzo 
305768b8534bSLuigi Rizzo 	/*
3058f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
3059f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
3060ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
3061ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
3062ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
3063ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
306468b8534bSLuigi Rizzo 	 *
306568b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
306668b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
306768b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
306868b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
306968b8534bSLuigi Rizzo 	 */
3070847bf383SLuigi Rizzo 	check_all_tx = nm_si_user(priv, NR_TX);
3071847bf383SLuigi Rizzo 	check_all_rx = nm_si_user(priv, NR_RX);
307264ae02c3SLuigi Rizzo 
30734f80b14cSVincenzo Maffione #ifdef __FreeBSD__
307468b8534bSLuigi Rizzo 	/*
3075f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
3076f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
30774f80b14cSVincenzo Maffione 	 * routines. We can't do this on Linux, as the contract says
30784f80b14cSVincenzo Maffione 	 * that we must call nm_os_selrecord() unconditionally.
307968b8534bSLuigi Rizzo 	 */
308037e3a6d3SLuigi Rizzo 	if (want_tx) {
30814f80b14cSVincenzo Maffione 		enum txrx t = NR_TX;
308237e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[t]; want[t] && i < priv->np_qlast[t]; i++) {
30832ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
308437e3a6d3SLuigi Rizzo 			/* XXX compare ring->cur and kring->tail */
308537e3a6d3SLuigi Rizzo 			if (!nm_ring_empty(kring->ring)) {
308637e3a6d3SLuigi Rizzo 				revents |= want[t];
308737e3a6d3SLuigi Rizzo 				want[t] = 0;	/* also breaks the loop */
308837e3a6d3SLuigi Rizzo 			}
308937e3a6d3SLuigi Rizzo 		}
309037e3a6d3SLuigi Rizzo 	}
309137e3a6d3SLuigi Rizzo 	if (want_rx) {
30924f80b14cSVincenzo Maffione 		enum txrx t = NR_RX;
309337e3a6d3SLuigi Rizzo 		want_rx = 0; /* look for a reason to run the handlers */
309437e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[t]; i < priv->np_qlast[t]; i++) {
30952ff91c17SVincenzo Maffione 			kring = NMR(na, t)[i];
309637e3a6d3SLuigi Rizzo 			if (kring->ring->cur == kring->ring->tail /* try fetch new buffers */
309737e3a6d3SLuigi Rizzo 			    || kring->rhead != kring->ring->head /* release buffers */) {
309837e3a6d3SLuigi Rizzo 				want_rx = 1;
309937e3a6d3SLuigi Rizzo 			}
310037e3a6d3SLuigi Rizzo 		}
310137e3a6d3SLuigi Rizzo 		if (!want_rx)
310237e3a6d3SLuigi Rizzo 			revents |= events & (POLLIN | POLLRDNORM); /* we have data */
310337e3a6d3SLuigi Rizzo 	}
31044f80b14cSVincenzo Maffione #endif
31054f80b14cSVincenzo Maffione 
31064f80b14cSVincenzo Maffione #ifdef linux
31074f80b14cSVincenzo Maffione 	/* The selrecord must be unconditional on linux. */
31084f80b14cSVincenzo Maffione 	nm_os_selrecord(sr, check_all_tx ?
31092ff91c17SVincenzo Maffione 	    &na->si[NR_TX] : &na->tx_rings[priv->np_qfirst[NR_TX]]->si);
31104f80b14cSVincenzo Maffione 	nm_os_selrecord(sr, check_all_rx ?
31112ff91c17SVincenzo Maffione 		&na->si[NR_RX] : &na->rx_rings[priv->np_qfirst[NR_RX]]->si);
31124f80b14cSVincenzo Maffione #endif /* linux */
311368b8534bSLuigi Rizzo 
311468b8534bSLuigi Rizzo 	/*
311517885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
311617885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
311717885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
3118f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
311968b8534bSLuigi Rizzo 	 */
312068b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
312117885a7bSLuigi Rizzo 		/*
312217885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
312317885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
312417885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
312517885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
3126ce3ee1e7SLuigi Rizzo 		 */
3127091fd0abSLuigi Rizzo flush_tx:
312837e3a6d3SLuigi Rizzo 		for (i = priv->np_qfirst[NR_TX]; i < priv->np_qlast[NR_TX]; i++) {
312917885a7bSLuigi Rizzo 			int found = 0;
313017885a7bSLuigi Rizzo 
31312ff91c17SVincenzo Maffione 			kring = na->tx_rings[i];
313237e3a6d3SLuigi Rizzo 			ring = kring->ring;
313337e3a6d3SLuigi Rizzo 
31344f80b14cSVincenzo Maffione 			/*
31354f80b14cSVincenzo Maffione 			 * Don't try to txsync this TX ring if we already found some
31364f80b14cSVincenzo Maffione 			 * space in some of the TX rings (want_tx == 0) and there are no
31374f80b14cSVincenzo Maffione 			 * TX slots in this ring that need to be flushed to the NIC
31382ff91c17SVincenzo Maffione 			 * (head == hwcur).
31394f80b14cSVincenzo Maffione 			 */
31402ff91c17SVincenzo Maffione 			if (!send_down && !want_tx && ring->head == kring->nr_hwcur)
314168b8534bSLuigi Rizzo 				continue;
314237e3a6d3SLuigi Rizzo 
314337e3a6d3SLuigi Rizzo 			if (nm_kr_tryget(kring, 1, &revents))
314417885a7bSLuigi Rizzo 				continue;
314537e3a6d3SLuigi Rizzo 
314637e3a6d3SLuigi Rizzo 			if (nm_txsync_prologue(kring, ring) >= kring->nkr_num_slots) {
314717885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
314817885a7bSLuigi Rizzo 				revents |= POLLERR;
314917885a7bSLuigi Rizzo 			} else {
3150c3e9b4dbSLuiz Otavio O Souza 				if (kring->nm_sync(kring, sync_flags))
315168b8534bSLuigi Rizzo 					revents |= POLLERR;
3152847bf383SLuigi Rizzo 				else
315337e3a6d3SLuigi Rizzo 					nm_sync_finalize(kring);
315417885a7bSLuigi Rizzo 			}
315568b8534bSLuigi Rizzo 
315617885a7bSLuigi Rizzo 			/*
315717885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
315817885a7bSLuigi Rizzo 			 * listeners on the same ring.
315917885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
316017885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
3161f9790aebSLuigi Rizzo 			 */
316217885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
316317885a7bSLuigi Rizzo 			nm_kr_put(kring);
316417885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
316568b8534bSLuigi Rizzo 				revents |= want_tx;
316668b8534bSLuigi Rizzo 				want_tx = 0;
31674f80b14cSVincenzo Maffione #ifndef linux
3168847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
31694f80b14cSVincenzo Maffione #endif /* linux */
317068b8534bSLuigi Rizzo 			}
3171ce3ee1e7SLuigi Rizzo 		}
317237e3a6d3SLuigi Rizzo 		/* if there were any packet to forward we must have handled them by now */
317337e3a6d3SLuigi Rizzo 		send_down = 0;
317437e3a6d3SLuigi Rizzo 		if (want_tx && retry_tx && sr) {
31754f80b14cSVincenzo Maffione #ifndef linux
317637e3a6d3SLuigi Rizzo 			nm_os_selrecord(sr, check_all_tx ?
31772ff91c17SVincenzo Maffione 			    &na->si[NR_TX] : &na->tx_rings[priv->np_qfirst[NR_TX]]->si);
31784f80b14cSVincenzo Maffione #endif /* !linux */
3179ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
3180ce3ee1e7SLuigi Rizzo 			goto flush_tx;
318168b8534bSLuigi Rizzo 		}
318268b8534bSLuigi Rizzo 	}
318368b8534bSLuigi Rizzo 
318468b8534bSLuigi Rizzo 	/*
318517885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
318668b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
318768b8534bSLuigi Rizzo 	 */
318868b8534bSLuigi Rizzo 	if (want_rx) {
318989cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
3190ce3ee1e7SLuigi Rizzo do_retry_rx:
3191847bf383SLuigi Rizzo 		for (i = priv->np_qfirst[NR_RX]; i < priv->np_qlast[NR_RX]; i++) {
319217885a7bSLuigi Rizzo 			int found = 0;
319317885a7bSLuigi Rizzo 
31942ff91c17SVincenzo Maffione 			kring = na->rx_rings[i];
319537e3a6d3SLuigi Rizzo 			ring = kring->ring;
3196ce3ee1e7SLuigi Rizzo 
319737e3a6d3SLuigi Rizzo 			if (unlikely(nm_kr_tryget(kring, 1, &revents)))
319817885a7bSLuigi Rizzo 				continue;
3199ce3ee1e7SLuigi Rizzo 
320037e3a6d3SLuigi Rizzo 			if (nm_rxsync_prologue(kring, ring) >= kring->nkr_num_slots) {
3201847bf383SLuigi Rizzo 				netmap_ring_reinit(kring);
3202847bf383SLuigi Rizzo 				revents |= POLLERR;
3203847bf383SLuigi Rizzo 			}
3204847bf383SLuigi Rizzo 			/* now we can use kring->rcur, rtail */
3205847bf383SLuigi Rizzo 
320617885a7bSLuigi Rizzo 			/*
3207c3e9b4dbSLuiz Otavio O Souza 			 * transparent mode support: collect packets from
3208c3e9b4dbSLuiz Otavio O Souza 			 * hw rxring(s) that have been released by the user
3209ce3ee1e7SLuigi Rizzo 			 */
321037e3a6d3SLuigi Rizzo 			if (nm_may_forward_up(kring)) {
3211091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
3212091fd0abSLuigi Rizzo 			}
321368b8534bSLuigi Rizzo 
3214c3e9b4dbSLuiz Otavio O Souza 			/* Clear the NR_FORWARD flag anyway, it may be set by
3215c3e9b4dbSLuiz Otavio O Souza 			 * the nm_sync() below only on for the host RX ring (see
3216c3e9b4dbSLuiz Otavio O Souza 			 * netmap_rxsync_from_host()). */
321737e3a6d3SLuigi Rizzo 			kring->nr_kflags &= ~NR_FORWARD;
3218c3e9b4dbSLuiz Otavio O Souza 			if (kring->nm_sync(kring, sync_flags))
321968b8534bSLuigi Rizzo 				revents |= POLLERR;
3220847bf383SLuigi Rizzo 			else
322137e3a6d3SLuigi Rizzo 				nm_sync_finalize(kring);
3222c3e9b4dbSLuiz Otavio O Souza 			send_down |= (kring->nr_kflags & NR_FORWARD);
3223c3e9b4dbSLuiz Otavio O Souza 			ring_timestamp_set(ring);
322417885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
322517885a7bSLuigi Rizzo 			nm_kr_put(kring);
322617885a7bSLuigi Rizzo 			if (found) {
322768b8534bSLuigi Rizzo 				revents |= want_rx;
3228ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
32294f80b14cSVincenzo Maffione #ifndef linux
3230847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
32314f80b14cSVincenzo Maffione #endif /* linux */
323268b8534bSLuigi Rizzo 			}
323368b8534bSLuigi Rizzo 		}
323417885a7bSLuigi Rizzo 
32354f80b14cSVincenzo Maffione #ifndef linux
323637e3a6d3SLuigi Rizzo 		if (retry_rx && sr) {
323737e3a6d3SLuigi Rizzo 			nm_os_selrecord(sr, check_all_rx ?
32382ff91c17SVincenzo Maffione 			    &na->si[NR_RX] : &na->rx_rings[priv->np_qfirst[NR_RX]]->si);
323937e3a6d3SLuigi Rizzo 		}
32404f80b14cSVincenzo Maffione #endif /* !linux */
3241c3e9b4dbSLuiz Otavio O Souza 		if (send_down || retry_rx) {
324217885a7bSLuigi Rizzo 			retry_rx = 0;
324317885a7bSLuigi Rizzo 			if (send_down)
324417885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
324517885a7bSLuigi Rizzo 			else
3246ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
3247ce3ee1e7SLuigi Rizzo 		}
324868b8534bSLuigi Rizzo 	}
3249091fd0abSLuigi Rizzo 
325017885a7bSLuigi Rizzo 	/*
3251c3e9b4dbSLuiz Otavio O Souza 	 * Transparent mode: released bufs (i.e. between kring->nr_hwcur and
3252c3e9b4dbSLuiz Otavio O Souza 	 * ring->head) marked with NS_FORWARD on hw rx rings are passed up
3253c3e9b4dbSLuiz Otavio O Souza 	 * to the host stack.
3254ce3ee1e7SLuigi Rizzo 	 */
3255091fd0abSLuigi Rizzo 
3256c3e9b4dbSLuiz Otavio O Souza 	if (mbq_peek(&q)) {
3257f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
325837e3a6d3SLuigi Rizzo 	}
325968b8534bSLuigi Rizzo 
326068b8534bSLuigi Rizzo 	return (revents);
3261847bf383SLuigi Rizzo #undef want_tx
3262847bf383SLuigi Rizzo #undef want_rx
326368b8534bSLuigi Rizzo }
326468b8534bSLuigi Rizzo 
32654f80b14cSVincenzo Maffione int
32664f80b14cSVincenzo Maffione nma_intr_enable(struct netmap_adapter *na, int onoff)
32674f80b14cSVincenzo Maffione {
32684f80b14cSVincenzo Maffione 	bool changed = false;
32694f80b14cSVincenzo Maffione 	enum txrx t;
32704f80b14cSVincenzo Maffione 	int i;
32714f80b14cSVincenzo Maffione 
32724f80b14cSVincenzo Maffione 	for_rx_tx(t) {
32734f80b14cSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t); i++) {
32742ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
32754f80b14cSVincenzo Maffione 			int on = !(kring->nr_kflags & NKR_NOINTR);
32764f80b14cSVincenzo Maffione 
32774f80b14cSVincenzo Maffione 			if (!!onoff != !!on) {
32784f80b14cSVincenzo Maffione 				changed = true;
32794f80b14cSVincenzo Maffione 			}
32804f80b14cSVincenzo Maffione 			if (onoff) {
32814f80b14cSVincenzo Maffione 				kring->nr_kflags &= ~NKR_NOINTR;
32824f80b14cSVincenzo Maffione 			} else {
32834f80b14cSVincenzo Maffione 				kring->nr_kflags |= NKR_NOINTR;
32844f80b14cSVincenzo Maffione 			}
32854f80b14cSVincenzo Maffione 		}
32864f80b14cSVincenzo Maffione 	}
32874f80b14cSVincenzo Maffione 
32884f80b14cSVincenzo Maffione 	if (!changed) {
32894f80b14cSVincenzo Maffione 		return 0; /* nothing to do */
32904f80b14cSVincenzo Maffione 	}
32914f80b14cSVincenzo Maffione 
32924f80b14cSVincenzo Maffione 	if (!na->nm_intr) {
32934f80b14cSVincenzo Maffione 		D("Cannot %s interrupts for %s", onoff ? "enable" : "disable",
32944f80b14cSVincenzo Maffione 		  na->name);
32954f80b14cSVincenzo Maffione 		return -1;
32964f80b14cSVincenzo Maffione 	}
32974f80b14cSVincenzo Maffione 
32984f80b14cSVincenzo Maffione 	na->nm_intr(na, onoff);
32994f80b14cSVincenzo Maffione 
33004f80b14cSVincenzo Maffione 	return 0;
33014f80b14cSVincenzo Maffione }
33024f80b14cSVincenzo Maffione 
330317885a7bSLuigi Rizzo 
330417885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
330568b8534bSLuigi Rizzo 
330689cc2556SLuigi Rizzo /* default notify callback */
3307f9790aebSLuigi Rizzo static int
3308847bf383SLuigi Rizzo netmap_notify(struct netmap_kring *kring, int flags)
3309f9790aebSLuigi Rizzo {
33102ff91c17SVincenzo Maffione 	struct netmap_adapter *na = kring->notify_na;
3311847bf383SLuigi Rizzo 	enum txrx t = kring->tx;
3312f9790aebSLuigi Rizzo 
331337e3a6d3SLuigi Rizzo 	nm_os_selwakeup(&kring->si);
331489cc2556SLuigi Rizzo 	/* optimization: avoid a wake up on the global
331589cc2556SLuigi Rizzo 	 * queue if nobody has registered for more
331689cc2556SLuigi Rizzo 	 * than one ring
331789cc2556SLuigi Rizzo 	 */
3318847bf383SLuigi Rizzo 	if (na->si_users[t] > 0)
331937e3a6d3SLuigi Rizzo 		nm_os_selwakeup(&na->si[t]);
3320847bf383SLuigi Rizzo 
332137e3a6d3SLuigi Rizzo 	return NM_IRQ_COMPLETED;
3322f9790aebSLuigi Rizzo }
3323f9790aebSLuigi Rizzo 
332489cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
332537e3a6d3SLuigi Rizzo  * provide some defaults and get a reference to the
332637e3a6d3SLuigi Rizzo  * memory allocator
332789cc2556SLuigi Rizzo  */
3328f9790aebSLuigi Rizzo int
3329f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
3330f9790aebSLuigi Rizzo {
3331f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
3332f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
33334bf50f18SLuigi Rizzo 			na->name, na->num_tx_rings, na->num_rx_rings);
3334f9790aebSLuigi Rizzo 		return EINVAL;
3335f9790aebSLuigi Rizzo 	}
333617885a7bSLuigi Rizzo 
33372ff91c17SVincenzo Maffione 	if (!na->rx_buf_maxsize) {
33382ff91c17SVincenzo Maffione 		/* Set a conservative default (larger is safer). */
33392ff91c17SVincenzo Maffione 		na->rx_buf_maxsize = PAGE_SIZE;
33402ff91c17SVincenzo Maffione 	}
33412ff91c17SVincenzo Maffione 
334217885a7bSLuigi Rizzo #ifdef __FreeBSD__
334337e3a6d3SLuigi Rizzo 	if (na->na_flags & NAF_HOST_RINGS && na->ifp) {
334437e3a6d3SLuigi Rizzo 		na->if_input = na->ifp->if_input; /* for netmap_send_up */
33454bf50f18SLuigi Rizzo 	}
33464f80b14cSVincenzo Maffione 	na->pdev = na; /* make sure netmap_mem_map() is called */
334737e3a6d3SLuigi Rizzo #endif /* __FreeBSD__ */
3348*2a7db7a6SVincenzo Maffione 	if (na->na_flags & NAF_HOST_RINGS) {
3349*2a7db7a6SVincenzo Maffione 		if (na->num_host_rx_rings == 0)
3350*2a7db7a6SVincenzo Maffione 			na->num_host_rx_rings = 1;
3351*2a7db7a6SVincenzo Maffione 		if (na->num_host_tx_rings == 0)
3352*2a7db7a6SVincenzo Maffione 			na->num_host_tx_rings = 1;
3353*2a7db7a6SVincenzo Maffione 	}
3354f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
335589cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
335689cc2556SLuigi Rizzo 		 * since other port types all provide their own
335789cc2556SLuigi Rizzo 		 * nm_krings_create
335889cc2556SLuigi Rizzo 		 */
3359f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
336017885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
3361f9790aebSLuigi Rizzo 	}
3362f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
3363f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
3364f9790aebSLuigi Rizzo 	na->active_fds = 0;
3365f9790aebSLuigi Rizzo 
3366c3e9b4dbSLuiz Otavio O Souza 	if (na->nm_mem == NULL) {
33674bf50f18SLuigi Rizzo 		/* use the global allocator */
3368c3e9b4dbSLuiz Otavio O Souza 		na->nm_mem = netmap_mem_get(&nm_mem);
3369c3e9b4dbSLuiz Otavio O Souza 	}
3370847bf383SLuigi Rizzo #ifdef WITH_VALE
33714bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
33724bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
33734bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
33744bf50f18SLuigi Rizzo 		 */
3375*2a7db7a6SVincenzo Maffione 		na->nm_bdg_attach = netmap_default_bdg_attach;
3376847bf383SLuigi Rizzo #endif
337737e3a6d3SLuigi Rizzo 
3378f9790aebSLuigi Rizzo 	return 0;
3379f9790aebSLuigi Rizzo }
3380f9790aebSLuigi Rizzo 
338137e3a6d3SLuigi Rizzo /* Wrapper for the register callback provided netmap-enabled
338237e3a6d3SLuigi Rizzo  * hardware drivers.
338337e3a6d3SLuigi Rizzo  * nm_iszombie(na) means that the driver module has been
33844bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
338537e3a6d3SLuigi Rizzo  * nm_os_ifnet_lock() must guarantee mutual exclusion with
338637e3a6d3SLuigi Rizzo  * module unloading.
33874bf50f18SLuigi Rizzo  */
33884bf50f18SLuigi Rizzo static int
338937e3a6d3SLuigi Rizzo netmap_hw_reg(struct netmap_adapter *na, int onoff)
33904bf50f18SLuigi Rizzo {
33914bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
33924bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
339337e3a6d3SLuigi Rizzo 	int error = 0;
33944bf50f18SLuigi Rizzo 
339537e3a6d3SLuigi Rizzo 	nm_os_ifnet_lock();
33964bf50f18SLuigi Rizzo 
339737e3a6d3SLuigi Rizzo 	if (nm_iszombie(na)) {
339837e3a6d3SLuigi Rizzo 		if (onoff) {
339937e3a6d3SLuigi Rizzo 			error = ENXIO;
340037e3a6d3SLuigi Rizzo 		} else if (na != NULL) {
340137e3a6d3SLuigi Rizzo 			na->na_flags &= ~NAF_NETMAP_ON;
340237e3a6d3SLuigi Rizzo 		}
340337e3a6d3SLuigi Rizzo 		goto out;
340437e3a6d3SLuigi Rizzo 	}
340537e3a6d3SLuigi Rizzo 
340637e3a6d3SLuigi Rizzo 	error = hwna->nm_hw_register(na, onoff);
340737e3a6d3SLuigi Rizzo 
340837e3a6d3SLuigi Rizzo out:
340937e3a6d3SLuigi Rizzo 	nm_os_ifnet_unlock();
341037e3a6d3SLuigi Rizzo 
341137e3a6d3SLuigi Rizzo 	return error;
341237e3a6d3SLuigi Rizzo }
341337e3a6d3SLuigi Rizzo 
341437e3a6d3SLuigi Rizzo static void
341537e3a6d3SLuigi Rizzo netmap_hw_dtor(struct netmap_adapter *na)
341637e3a6d3SLuigi Rizzo {
3417*2a7db7a6SVincenzo Maffione 	if (na->ifp == NULL)
341837e3a6d3SLuigi Rizzo 		return;
341937e3a6d3SLuigi Rizzo 
3420*2a7db7a6SVincenzo Maffione 	NM_DETACH_NA(na->ifp);
34214bf50f18SLuigi Rizzo }
34224bf50f18SLuigi Rizzo 
3423f18be576SLuigi Rizzo 
342468b8534bSLuigi Rizzo /*
3425c3e9b4dbSLuiz Otavio O Souza  * Allocate a netmap_adapter object, and initialize it from the
342637e3a6d3SLuigi Rizzo  * 'arg' passed by the driver on attach.
3427c3e9b4dbSLuiz Otavio O Souza  * We allocate a block of memory of 'size' bytes, which has room
3428c3e9b4dbSLuiz Otavio O Souza  * for struct netmap_adapter plus additional room private to
3429c3e9b4dbSLuiz Otavio O Souza  * the caller.
343068b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
343168b8534bSLuigi Rizzo  */
3432c3e9b4dbSLuiz Otavio O Souza int
34334f80b14cSVincenzo Maffione netmap_attach_ext(struct netmap_adapter *arg, size_t size, int override_reg)
343468b8534bSLuigi Rizzo {
3435f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
343637e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
343768b8534bSLuigi Rizzo 
3438c3e9b4dbSLuiz Otavio O Souza 	if (size < sizeof(struct netmap_hw_adapter)) {
3439c3e9b4dbSLuiz Otavio O Souza 		D("Invalid netmap adapter size %d", (int)size);
3440c3e9b4dbSLuiz Otavio O Souza 		return EINVAL;
3441c3e9b4dbSLuiz Otavio O Souza 	}
3442c3e9b4dbSLuiz Otavio O Souza 
344337e3a6d3SLuigi Rizzo 	if (arg == NULL || arg->ifp == NULL)
3444*2a7db7a6SVincenzo Maffione 		return EINVAL;
34454f80b14cSVincenzo Maffione 
344637e3a6d3SLuigi Rizzo 	ifp = arg->ifp;
3447*2a7db7a6SVincenzo Maffione 	if (NM_NA_CLASH(ifp)) {
34484f80b14cSVincenzo Maffione 		/* If NA(ifp) is not null but there is no valid netmap
34494f80b14cSVincenzo Maffione 		 * adapter it means that someone else is using the same
34504f80b14cSVincenzo Maffione 		 * pointer (e.g. ax25_ptr on linux). This happens for
34514f80b14cSVincenzo Maffione 		 * instance when also PF_RING is in use. */
34524f80b14cSVincenzo Maffione 		D("Error: netmap adapter hook is busy");
34534f80b14cSVincenzo Maffione 		return EBUSY;
34544f80b14cSVincenzo Maffione 	}
34554f80b14cSVincenzo Maffione 
3456c3e9b4dbSLuiz Otavio O Souza 	hwna = nm_os_malloc(size);
3457f9790aebSLuigi Rizzo 	if (hwna == NULL)
3458ae10d1afSLuigi Rizzo 		goto fail;
3459f9790aebSLuigi Rizzo 	hwna->up = *arg;
3460847bf383SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS | NAF_NATIVE;
34614bf50f18SLuigi Rizzo 	strncpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
34624f80b14cSVincenzo Maffione 	if (override_reg) {
34634bf50f18SLuigi Rizzo 		hwna->nm_hw_register = hwna->up.nm_register;
346437e3a6d3SLuigi Rizzo 		hwna->up.nm_register = netmap_hw_reg;
34654f80b14cSVincenzo Maffione 	}
3466f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
3467c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(hwna);
3468f9790aebSLuigi Rizzo 		goto fail;
3469f9790aebSLuigi Rizzo 	}
3470f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
3471f9790aebSLuigi Rizzo 
347237e3a6d3SLuigi Rizzo 	NM_ATTACH_NA(ifp, &hwna->up);
347337e3a6d3SLuigi Rizzo 
3474*2a7db7a6SVincenzo Maffione 	nm_os_onattach(ifp);
3475*2a7db7a6SVincenzo Maffione 
347637e3a6d3SLuigi Rizzo 	if (arg->nm_dtor == NULL) {
347737e3a6d3SLuigi Rizzo 		hwna->up.nm_dtor = netmap_hw_dtor;
347837e3a6d3SLuigi Rizzo 	}
3479f9790aebSLuigi Rizzo 
3480d82f9014SRui Paulo 	if_printf(ifp, "netmap queues/slots: TX %d/%d, RX %d/%d\n",
3481d82f9014SRui Paulo 	    hwna->up.num_tx_rings, hwna->up.num_tx_desc,
3482d82f9014SRui Paulo 	    hwna->up.num_rx_rings, hwna->up.num_rx_desc);
3483ae10d1afSLuigi Rizzo 	return 0;
348468b8534bSLuigi Rizzo 
3485ae10d1afSLuigi Rizzo fail:
3486f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
3487f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
348868b8534bSLuigi Rizzo }
348968b8534bSLuigi Rizzo 
349068b8534bSLuigi Rizzo 
349137e3a6d3SLuigi Rizzo int
349237e3a6d3SLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
349337e3a6d3SLuigi Rizzo {
34944f80b14cSVincenzo Maffione 	return netmap_attach_ext(arg, sizeof(struct netmap_hw_adapter),
34954f80b14cSVincenzo Maffione 			1 /* override nm_reg */);
349637e3a6d3SLuigi Rizzo }
349737e3a6d3SLuigi Rizzo 
349837e3a6d3SLuigi Rizzo 
3499f9790aebSLuigi Rizzo void
3500f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
3501f9790aebSLuigi Rizzo {
3502f9790aebSLuigi Rizzo 	if (!na) {
3503f9790aebSLuigi Rizzo 		return;
3504f9790aebSLuigi Rizzo 	}
3505f9790aebSLuigi Rizzo 
3506f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
3507f9790aebSLuigi Rizzo }
3508f9790aebSLuigi Rizzo 
3509f9790aebSLuigi Rizzo 
3510f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
3511f9790aebSLuigi Rizzo int
3512f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
3513f9790aebSLuigi Rizzo {
3514f9790aebSLuigi Rizzo 	if (!na)
3515f9790aebSLuigi Rizzo 		return 1;
3516f9790aebSLuigi Rizzo 
3517f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
3518f9790aebSLuigi Rizzo 		return 0;
3519f9790aebSLuigi Rizzo 
3520f9790aebSLuigi Rizzo 	if (na->nm_dtor)
3521f9790aebSLuigi Rizzo 		na->nm_dtor(na);
3522f9790aebSLuigi Rizzo 
35234f80b14cSVincenzo Maffione 	if (na->tx_rings) { /* XXX should not happen */
35244f80b14cSVincenzo Maffione 		D("freeing leftover tx_rings");
35254f80b14cSVincenzo Maffione 		na->nm_krings_delete(na);
35264f80b14cSVincenzo Maffione 	}
35274f80b14cSVincenzo Maffione 	netmap_pipe_dealloc(na);
35284f80b14cSVincenzo Maffione 	if (na->nm_mem)
35294f80b14cSVincenzo Maffione 		netmap_mem_put(na->nm_mem);
35304f80b14cSVincenzo Maffione 	bzero(na, sizeof(*na));
35314f80b14cSVincenzo Maffione 	nm_os_free(na);
3532f9790aebSLuigi Rizzo 
3533f9790aebSLuigi Rizzo 	return 1;
3534f9790aebSLuigi Rizzo }
3535f9790aebSLuigi Rizzo 
353689cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
3537f9790aebSLuigi Rizzo int
3538f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
3539f9790aebSLuigi Rizzo {
3540f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
354117885a7bSLuigi Rizzo 	if (ret == 0) {
354217885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
3543*2a7db7a6SVincenzo Maffione 		u_int lim = netmap_real_rings(na, NR_RX), i;
3544*2a7db7a6SVincenzo Maffione 		for (i = na->num_rx_rings; i < lim; i++) {
3545*2a7db7a6SVincenzo Maffione 			mbq_safe_init(&NMR(na, NR_RX)[i]->rx_queue);
3546*2a7db7a6SVincenzo Maffione 		}
354717885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
354817885a7bSLuigi Rizzo 	}
354917885a7bSLuigi Rizzo 	return ret;
3550f9790aebSLuigi Rizzo }
3551f9790aebSLuigi Rizzo 
3552f9790aebSLuigi Rizzo 
3553f9790aebSLuigi Rizzo 
355468b8534bSLuigi Rizzo /*
355589cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
355668b8534bSLuigi Rizzo  */
355768b8534bSLuigi Rizzo void
355868b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
355968b8534bSLuigi Rizzo {
356068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
356168b8534bSLuigi Rizzo 
356268b8534bSLuigi Rizzo 	if (!na)
356368b8534bSLuigi Rizzo 		return;
356468b8534bSLuigi Rizzo 
3565f9790aebSLuigi Rizzo 	NMG_LOCK();
356637e3a6d3SLuigi Rizzo 	netmap_set_all_rings(na, NM_KR_LOCKED);
3567847bf383SLuigi Rizzo 	/*
3568847bf383SLuigi Rizzo 	 * if the netmap adapter is not native, somebody
3569847bf383SLuigi Rizzo 	 * changed it, so we can not release it here.
357037e3a6d3SLuigi Rizzo 	 * The NAF_ZOMBIE flag will notify the new owner that
3571847bf383SLuigi Rizzo 	 * the driver is gone.
3572847bf383SLuigi Rizzo 	 */
35734f80b14cSVincenzo Maffione 	if (!(na->na_flags & NAF_NATIVE) || !netmap_adapter_put(na)) {
35744f80b14cSVincenzo Maffione 		na->na_flags |= NAF_ZOMBIE;
3575847bf383SLuigi Rizzo 	}
357637e3a6d3SLuigi Rizzo 	/* give active users a chance to notice that NAF_ZOMBIE has been
357737e3a6d3SLuigi Rizzo 	 * turned on, so that they can stop and return an error to userspace.
357837e3a6d3SLuigi Rizzo 	 * Note that this becomes a NOP if there are no active users and,
357937e3a6d3SLuigi Rizzo 	 * therefore, the put() above has deleted the na, since now NA(ifp) is
358037e3a6d3SLuigi Rizzo 	 * NULL.
358137e3a6d3SLuigi Rizzo 	 */
3582f9790aebSLuigi Rizzo 	netmap_enable_all_rings(ifp);
3583f9790aebSLuigi Rizzo 	NMG_UNLOCK();
3584ae10d1afSLuigi Rizzo }
3585f18be576SLuigi Rizzo 
3586f18be576SLuigi Rizzo 
358768b8534bSLuigi Rizzo /*
358802ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
358902ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
359017885a7bSLuigi Rizzo  *
359117885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
359217885a7bSLuigi Rizzo  * in the relevant rxsync routine.
359317885a7bSLuigi Rizzo  *
3594ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
3595ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
3596ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
3597f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
359868b8534bSLuigi Rizzo  */
359968b8534bSLuigi Rizzo int
3600ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
360168b8534bSLuigi Rizzo {
360268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
360337e3a6d3SLuigi Rizzo 	struct netmap_kring *kring, *tx_kring;
360417885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
360517885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
360637e3a6d3SLuigi Rizzo 	unsigned int txr;
360717885a7bSLuigi Rizzo 	struct mbq *q;
3608c3e9b4dbSLuiz Otavio O Souza 	int busy;
3609*2a7db7a6SVincenzo Maffione 	u_int i;
361068b8534bSLuigi Rizzo 
3611*2a7db7a6SVincenzo Maffione 	i = MBUF_TXQ(m);
3612*2a7db7a6SVincenzo Maffione 	if (i >= na->num_host_rx_rings) {
3613*2a7db7a6SVincenzo Maffione 		i = i % na->num_host_rx_rings;
3614*2a7db7a6SVincenzo Maffione 	}
3615*2a7db7a6SVincenzo Maffione 	kring = NMR(na, NR_RX)[nma_get_nrings(na, NR_RX) + i];
3616*2a7db7a6SVincenzo Maffione 
3617ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
3618ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
3619ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
362017885a7bSLuigi Rizzo 
36214bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
36224bf50f18SLuigi Rizzo 		D("%s not in netmap mode anymore", na->name);
3623ce3ee1e7SLuigi Rizzo 		error = ENXIO;
3624ce3ee1e7SLuigi Rizzo 		goto done;
3625ce3ee1e7SLuigi Rizzo 	}
3626ce3ee1e7SLuigi Rizzo 
362737e3a6d3SLuigi Rizzo 	txr = MBUF_TXQ(m);
362837e3a6d3SLuigi Rizzo 	if (txr >= na->num_tx_rings) {
362937e3a6d3SLuigi Rizzo 		txr %= na->num_tx_rings;
363037e3a6d3SLuigi Rizzo 	}
36312ff91c17SVincenzo Maffione 	tx_kring = NMR(na, NR_TX)[txr];
363237e3a6d3SLuigi Rizzo 
363337e3a6d3SLuigi Rizzo 	if (tx_kring->nr_mode == NKR_NETMAP_OFF) {
363437e3a6d3SLuigi Rizzo 		return MBUF_TRANSMIT(na, ifp, m);
363537e3a6d3SLuigi Rizzo 	}
363637e3a6d3SLuigi Rizzo 
363717885a7bSLuigi Rizzo 	q = &kring->rx_queue;
363817885a7bSLuigi Rizzo 
3639ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
36404bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
36414bf50f18SLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", na->name,
36424bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
3643ce3ee1e7SLuigi Rizzo 		goto done;
3644849bec0eSLuigi Rizzo 	}
364517885a7bSLuigi Rizzo 
3646*2a7db7a6SVincenzo Maffione 	if (!netmap_generic_hwcsum) {
3647*2a7db7a6SVincenzo Maffione 		if (nm_os_mbuf_has_csum_offld(m)) {
3648*2a7db7a6SVincenzo Maffione 			RD(1, "%s drop mbuf that needs checksum offload", na->name);
3649*2a7db7a6SVincenzo Maffione 			goto done;
3650*2a7db7a6SVincenzo Maffione 		}
3651*2a7db7a6SVincenzo Maffione 	}
3652*2a7db7a6SVincenzo Maffione 
3653*2a7db7a6SVincenzo Maffione 	if (nm_os_mbuf_has_seg_offld(m)) {
3654*2a7db7a6SVincenzo Maffione 		RD(1, "%s drop mbuf that needs generic segmentation offload", na->name);
365537e3a6d3SLuigi Rizzo 		goto done;
365637e3a6d3SLuigi Rizzo 	}
365737e3a6d3SLuigi Rizzo 
3658c3e9b4dbSLuiz Otavio O Souza 	/* protect against netmap_rxsync_from_host(), netmap_sw_to_nic()
365917885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
366017885a7bSLuigi Rizzo 	 * not possible on Linux).
3661c3e9b4dbSLuiz Otavio O Souza 	 * We enqueue the mbuf only if we are sure there is going to be
3662c3e9b4dbSLuiz Otavio O Souza 	 * enough room in the host RX ring, otherwise we drop it.
3663ce3ee1e7SLuigi Rizzo 	 */
3664997b054cSLuigi Rizzo 	mbq_lock(q);
366517885a7bSLuigi Rizzo 
3666c3e9b4dbSLuiz Otavio O Souza 	busy = kring->nr_hwtail - kring->nr_hwcur;
3667c3e9b4dbSLuiz Otavio O Souza 	if (busy < 0)
3668c3e9b4dbSLuiz Otavio O Souza 		busy += kring->nkr_num_slots;
3669c3e9b4dbSLuiz Otavio O Souza 	if (busy + mbq_len(q) >= kring->nkr_num_slots - 1) {
3670c3e9b4dbSLuiz Otavio O Souza 		RD(2, "%s full hwcur %d hwtail %d qlen %d", na->name,
3671c3e9b4dbSLuiz Otavio O Souza 			kring->nr_hwcur, kring->nr_hwtail, mbq_len(q));
3672ce3ee1e7SLuigi Rizzo 	} else {
367317885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
3674c3e9b4dbSLuiz Otavio O Souza 		ND(2, "%s %d bufs in queue", na->name, mbq_len(q));
367517885a7bSLuigi Rizzo 		/* notify outside the lock */
367617885a7bSLuigi Rizzo 		m = NULL;
367768b8534bSLuigi Rizzo 		error = 0;
3678ce3ee1e7SLuigi Rizzo 	}
3679997b054cSLuigi Rizzo 	mbq_unlock(q);
3680ce3ee1e7SLuigi Rizzo 
368168b8534bSLuigi Rizzo done:
368217885a7bSLuigi Rizzo 	if (m)
368368b8534bSLuigi Rizzo 		m_freem(m);
368417885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
3685847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
368689cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
368789cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
368889cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
368989cc2556SLuigi Rizzo 	 */
369068b8534bSLuigi Rizzo 
369168b8534bSLuigi Rizzo 	return (error);
369268b8534bSLuigi Rizzo }
369368b8534bSLuigi Rizzo 
369468b8534bSLuigi Rizzo 
369568b8534bSLuigi Rizzo /*
369668b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
369768b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
3698f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
369937e3a6d3SLuigi Rizzo  * If native netmap mode is set, in particular, we have to set nr_mode to
370037e3a6d3SLuigi Rizzo  * NKR_NETMAP_ON.
370168b8534bSLuigi Rizzo  */
370268b8534bSLuigi Rizzo struct netmap_slot *
3703ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
370468b8534bSLuigi Rizzo 	u_int new_cur)
370568b8534bSLuigi Rizzo {
370668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
3707506cc70cSLuigi Rizzo 	int new_hwofs, lim;
370868b8534bSLuigi Rizzo 
37094bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
37104bf50f18SLuigi Rizzo 		ND("interface not in native netmap mode");
371168b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
3712ce3ee1e7SLuigi Rizzo 	}
371368b8534bSLuigi Rizzo 
3714ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
3715ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
3716ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
3717ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
3718ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
3719ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
3720ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
3721ce3ee1e7SLuigi Rizzo 	 */
372264ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
37238241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
37248241616dSLuigi Rizzo 			return NULL;
372537e3a6d3SLuigi Rizzo 
37262ff91c17SVincenzo Maffione 		kring = na->tx_rings[n];
372737e3a6d3SLuigi Rizzo 
372837e3a6d3SLuigi Rizzo 		if (kring->nr_pending_mode == NKR_NETMAP_OFF) {
372937e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
373037e3a6d3SLuigi Rizzo 			return NULL;
373137e3a6d3SLuigi Rizzo 		}
373237e3a6d3SLuigi Rizzo 
373317885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
3734506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
373564ae02c3SLuigi Rizzo 	} else {
37368241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
37378241616dSLuigi Rizzo 			return NULL;
37382ff91c17SVincenzo Maffione 		kring = na->rx_rings[n];
373937e3a6d3SLuigi Rizzo 
374037e3a6d3SLuigi Rizzo 		if (kring->nr_pending_mode == NKR_NETMAP_OFF) {
374137e3a6d3SLuigi Rizzo 			kring->nr_mode = NKR_NETMAP_OFF;
374237e3a6d3SLuigi Rizzo 			return NULL;
374337e3a6d3SLuigi Rizzo 		}
374437e3a6d3SLuigi Rizzo 
374517885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
374664ae02c3SLuigi Rizzo 	}
374764ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
3748506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
3749506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
3750506cc70cSLuigi Rizzo 
3751ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
375217885a7bSLuigi Rizzo 	if (netmap_verbose)
375317885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
37544bf50f18SLuigi Rizzo 		na->name,
375517885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
3756ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
375717885a7bSLuigi Rizzo 		kring->nr_hwtail,
375817885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
3759506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
376017885a7bSLuigi Rizzo 	if (tx == NR_TX) {
376117885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
376217885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
376317885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
376417885a7bSLuigi Rizzo 	}
3765506cc70cSLuigi Rizzo 
376668b8534bSLuigi Rizzo 	/*
3767ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
3768506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
3769506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
377068b8534bSLuigi Rizzo 	 */
377137e3a6d3SLuigi Rizzo 	kring->nr_mode = NKR_NETMAP_ON;
3772847bf383SLuigi Rizzo 	kring->nm_notify(kring, 0);
377368b8534bSLuigi Rizzo 	return kring->ring->slot;
377468b8534bSLuigi Rizzo }
377568b8534bSLuigi Rizzo 
377668b8534bSLuigi Rizzo 
3777ce3ee1e7SLuigi Rizzo /*
3778f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
3779ce3ee1e7SLuigi Rizzo  *
3780ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3781ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
3782ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
3783849bec0eSLuigi Rizzo  *
3784f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
3785f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
3786f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
37874bf50f18SLuigi Rizzo  *   (see netmap_notify)
37884bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
37894bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
3790f9790aebSLuigi Rizzo  */
379137e3a6d3SLuigi Rizzo int
379237e3a6d3SLuigi Rizzo netmap_common_irq(struct netmap_adapter *na, u_int q, u_int *work_done)
3793f9790aebSLuigi Rizzo {
3794f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
3795847bf383SLuigi Rizzo 	enum txrx t = (work_done ? NR_RX : NR_TX);
3796f9790aebSLuigi Rizzo 
3797f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
3798f9790aebSLuigi Rizzo 
3799f9790aebSLuigi Rizzo 	if (netmap_verbose) {
3800f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
3801f9790aebSLuigi Rizzo 	}
3802f9790aebSLuigi Rizzo 
3803847bf383SLuigi Rizzo 	if (q >= nma_get_nrings(na, t))
380437e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS; // not a physical queue
3805847bf383SLuigi Rizzo 
38062ff91c17SVincenzo Maffione 	kring = NMR(na, t)[q];
3807847bf383SLuigi Rizzo 
380837e3a6d3SLuigi Rizzo 	if (kring->nr_mode == NKR_NETMAP_OFF) {
380937e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
381037e3a6d3SLuigi Rizzo 	}
381137e3a6d3SLuigi Rizzo 
3812847bf383SLuigi Rizzo 	if (t == NR_RX) {
3813f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
3814f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
3815f9790aebSLuigi Rizzo 	}
381637e3a6d3SLuigi Rizzo 
381737e3a6d3SLuigi Rizzo 	return kring->nm_notify(kring, 0);
3818f9790aebSLuigi Rizzo }
3819f9790aebSLuigi Rizzo 
382017885a7bSLuigi Rizzo 
3821f9790aebSLuigi Rizzo /*
3822f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
3823f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
3824f9790aebSLuigi Rizzo  *
382537e3a6d3SLuigi Rizzo  * If the card is not in netmap mode, simply return NM_IRQ_PASS,
3826ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
382737e3a6d3SLuigi Rizzo  * Otherwise call netmap_common_irq().
3828ce3ee1e7SLuigi Rizzo  *
3829ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
3830ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
3831ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
383237e3a6d3SLuigi Rizzo  * and return NR_IRQ_COMPLETED.
3833ce3ee1e7SLuigi Rizzo  *
3834ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
383537e3a6d3SLuigi Rizzo  * calls the proper forwarding routine.
38361a26580eSLuigi Rizzo  */
3837babc7c12SLuigi Rizzo int
3838ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
38391a26580eSLuigi Rizzo {
38404bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
38414bf50f18SLuigi Rizzo 
38424bf50f18SLuigi Rizzo 	/*
38434bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
38444bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
38454bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
38464bf50f18SLuigi Rizzo 	 * nm_native_on() here.
38474bf50f18SLuigi Rizzo 	 */
38484bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
384937e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
3850849bec0eSLuigi Rizzo 
38514bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
38528241616dSLuigi Rizzo 		ND("use regular interrupt");
385337e3a6d3SLuigi Rizzo 		return NM_IRQ_PASS;
38548241616dSLuigi Rizzo 	}
38558241616dSLuigi Rizzo 
385637e3a6d3SLuigi Rizzo 	return netmap_common_irq(na, q, work_done);
38571a26580eSLuigi Rizzo }
38581a26580eSLuigi Rizzo 
3859*2a7db7a6SVincenzo Maffione /* set/clear native flags and if_transmit/netdev_ops */
3860*2a7db7a6SVincenzo Maffione void
3861*2a7db7a6SVincenzo Maffione nm_set_native_flags(struct netmap_adapter *na)
3862*2a7db7a6SVincenzo Maffione {
3863*2a7db7a6SVincenzo Maffione 	struct ifnet *ifp = na->ifp;
3864*2a7db7a6SVincenzo Maffione 
3865*2a7db7a6SVincenzo Maffione 	/* We do the setup for intercepting packets only if we are the
3866*2a7db7a6SVincenzo Maffione 	 * first user of this adapapter. */
3867*2a7db7a6SVincenzo Maffione 	if (na->active_fds > 0) {
3868*2a7db7a6SVincenzo Maffione 		return;
3869*2a7db7a6SVincenzo Maffione 	}
3870*2a7db7a6SVincenzo Maffione 
3871*2a7db7a6SVincenzo Maffione 	na->na_flags |= NAF_NETMAP_ON;
3872*2a7db7a6SVincenzo Maffione 	nm_os_onenter(ifp);
3873*2a7db7a6SVincenzo Maffione 	nm_update_hostrings_mode(na);
3874*2a7db7a6SVincenzo Maffione }
3875*2a7db7a6SVincenzo Maffione 
3876*2a7db7a6SVincenzo Maffione void
3877*2a7db7a6SVincenzo Maffione nm_clear_native_flags(struct netmap_adapter *na)
3878*2a7db7a6SVincenzo Maffione {
3879*2a7db7a6SVincenzo Maffione 	struct ifnet *ifp = na->ifp;
3880*2a7db7a6SVincenzo Maffione 
3881*2a7db7a6SVincenzo Maffione 	/* We undo the setup for intercepting packets only if we are the
3882*2a7db7a6SVincenzo Maffione 	 * last user of this adapapter. */
3883*2a7db7a6SVincenzo Maffione 	if (na->active_fds > 0) {
3884*2a7db7a6SVincenzo Maffione 		return;
3885*2a7db7a6SVincenzo Maffione 	}
3886*2a7db7a6SVincenzo Maffione 
3887*2a7db7a6SVincenzo Maffione 	nm_update_hostrings_mode(na);
3888*2a7db7a6SVincenzo Maffione 	nm_os_onexit(ifp);
3889*2a7db7a6SVincenzo Maffione 
3890*2a7db7a6SVincenzo Maffione 	na->na_flags &= ~NAF_NETMAP_ON;
3891*2a7db7a6SVincenzo Maffione }
3892*2a7db7a6SVincenzo Maffione 
389364ae02c3SLuigi Rizzo 
389401c7d25fSLuigi Rizzo /*
3895f9790aebSLuigi Rizzo  * Module loader and unloader
3896f196ce38SLuigi Rizzo  *
3897f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
3898f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
3899f9790aebSLuigi Rizzo  * (but there is no chance)
3900f9790aebSLuigi Rizzo  *
3901f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
3902f196ce38SLuigi Rizzo  */
3903babc7c12SLuigi Rizzo 
3904babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
3905f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
3906babc7c12SLuigi Rizzo 
390717885a7bSLuigi Rizzo 
3908f9790aebSLuigi Rizzo void
390968b8534bSLuigi Rizzo netmap_fini(void)
391068b8534bSLuigi Rizzo {
3911f9790aebSLuigi Rizzo 	if (netmap_dev)
391268b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
391337e3a6d3SLuigi Rizzo 	/* we assume that there are no longer netmap users */
391437e3a6d3SLuigi Rizzo 	nm_os_ifnet_fini();
391537e3a6d3SLuigi Rizzo 	netmap_uninit_bridges();
3916ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
3917ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
3918c3e9b4dbSLuiz Otavio O Souza 	nm_prinf("netmap: unloaded module.\n");
391968b8534bSLuigi Rizzo }
392068b8534bSLuigi Rizzo 
392117885a7bSLuigi Rizzo 
3922f9790aebSLuigi Rizzo int
3923f9790aebSLuigi Rizzo netmap_init(void)
392468b8534bSLuigi Rizzo {
3925f9790aebSLuigi Rizzo 	int error;
392668b8534bSLuigi Rizzo 
3927f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
392868b8534bSLuigi Rizzo 
3929f9790aebSLuigi Rizzo 	error = netmap_mem_init();
3930f9790aebSLuigi Rizzo 	if (error != 0)
3931f9790aebSLuigi Rizzo 		goto fail;
3932c929ca72SLuigi Rizzo 	/*
3933c929ca72SLuigi Rizzo 	 * MAKEDEV_ETERNAL_KLD avoids an expensive check on syscalls
3934c929ca72SLuigi Rizzo 	 * when the module is compiled in.
3935c929ca72SLuigi Rizzo 	 * XXX could use make_dev_credv() to get error number
3936c929ca72SLuigi Rizzo 	 */
39370e73f29aSLuigi Rizzo 	netmap_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD,
393811c0b69cSAdrian Chadd 		&netmap_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0600,
39390e73f29aSLuigi Rizzo 			      "netmap");
3940f9790aebSLuigi Rizzo 	if (!netmap_dev)
3941f9790aebSLuigi Rizzo 		goto fail;
3942f9790aebSLuigi Rizzo 
3943847bf383SLuigi Rizzo 	error = netmap_init_bridges();
3944847bf383SLuigi Rizzo 	if (error)
3945847bf383SLuigi Rizzo 		goto fail;
3946847bf383SLuigi Rizzo 
39474bf50f18SLuigi Rizzo #ifdef __FreeBSD__
394837e3a6d3SLuigi Rizzo 	nm_os_vi_init_index();
39494bf50f18SLuigi Rizzo #endif
3950847bf383SLuigi Rizzo 
395137e3a6d3SLuigi Rizzo 	error = nm_os_ifnet_init();
395237e3a6d3SLuigi Rizzo 	if (error)
395337e3a6d3SLuigi Rizzo 		goto fail;
395437e3a6d3SLuigi Rizzo 
3955c3e9b4dbSLuiz Otavio O Souza 	nm_prinf("netmap: loaded module\n");
3956f9790aebSLuigi Rizzo 	return (0);
3957f9790aebSLuigi Rizzo fail:
395868b8534bSLuigi Rizzo 	netmap_fini();
3959f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
396068b8534bSLuigi Rizzo }
3961