xref: /freebsd/sys/dev/netmap/netmap.c (revision 4bf50f18eb272037afaa212738f3d82d4098e782)
168b8534bSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
26ce3ee1e7SLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
28f9790aebSLuigi Rizzo  * $FreeBSD$
29f9790aebSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3168b8534bSLuigi Rizzo  * see netmap(4).
3268b8534bSLuigi Rizzo  *
3368b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3468b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3568b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3668b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3768b8534bSLuigi Rizzo  *
3868b8534bSLuigi Rizzo  * Access to the network card works like this:
3968b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
4068b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4168b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4268b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4368b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4468b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4568b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4668b8534bSLuigi Rizzo  *    the shared memory region.
4768b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4868b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4968b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
5068b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5168b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5268b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5368b8534bSLuigi Rizzo  *    packets on the output interface.
5468b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5568b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
56ce3ee1e7SLuigi Rizzo  *
57ce3ee1e7SLuigi Rizzo 
58ce3ee1e7SLuigi Rizzo 		SYNCHRONIZATION (USER)
59ce3ee1e7SLuigi Rizzo 
60ce3ee1e7SLuigi Rizzo The netmap rings and data structures may be shared among multiple
61ce3ee1e7SLuigi Rizzo user threads or even independent processes.
62ce3ee1e7SLuigi Rizzo Any synchronization among those threads/processes is delegated
63ce3ee1e7SLuigi Rizzo to the threads themselves. Only one thread at a time can be in
64ce3ee1e7SLuigi Rizzo a system call on the same netmap ring. The OS does not enforce
65ce3ee1e7SLuigi Rizzo this and only guarantees against system crashes in case of
66ce3ee1e7SLuigi Rizzo invalid usage.
67ce3ee1e7SLuigi Rizzo 
68ce3ee1e7SLuigi Rizzo 		LOCKING (INTERNAL)
69ce3ee1e7SLuigi Rizzo 
70ce3ee1e7SLuigi Rizzo Within the kernel, access to the netmap rings is protected as follows:
71ce3ee1e7SLuigi Rizzo 
72ce3ee1e7SLuigi Rizzo - a spinlock on each ring, to handle producer/consumer races on
73ce3ee1e7SLuigi Rizzo   RX rings attached to the host stack (against multiple host
74ce3ee1e7SLuigi Rizzo   threads writing from the host stack to the same ring),
75ce3ee1e7SLuigi Rizzo   and on 'destination' rings attached to a VALE switch
76ce3ee1e7SLuigi Rizzo   (i.e. RX rings in VALE ports, and TX rings in NIC/host ports)
77ce3ee1e7SLuigi Rizzo   protecting multiple active senders for the same destination)
78ce3ee1e7SLuigi Rizzo 
79ce3ee1e7SLuigi Rizzo - an atomic variable to guarantee that there is at most one
80ce3ee1e7SLuigi Rizzo   instance of *_*xsync() on the ring at any time.
81ce3ee1e7SLuigi Rizzo   For rings connected to user file
82ce3ee1e7SLuigi Rizzo   descriptors, an atomic_test_and_set() protects this, and the
83ce3ee1e7SLuigi Rizzo   lock on the ring is not actually used.
84ce3ee1e7SLuigi Rizzo   For NIC RX rings connected to a VALE switch, an atomic_test_and_set()
85ce3ee1e7SLuigi Rizzo   is also used to prevent multiple executions (the driver might indeed
86ce3ee1e7SLuigi Rizzo   already guarantee this).
87ce3ee1e7SLuigi Rizzo   For NIC TX rings connected to a VALE switch, the lock arbitrates
88ce3ee1e7SLuigi Rizzo   access to the queue (both when allocating buffers and when pushing
89ce3ee1e7SLuigi Rizzo   them out).
90ce3ee1e7SLuigi Rizzo 
91ce3ee1e7SLuigi Rizzo - *xsync() should be protected against initializations of the card.
92ce3ee1e7SLuigi Rizzo   On FreeBSD most devices have the reset routine protected by
93ce3ee1e7SLuigi Rizzo   a RING lock (ixgbe, igb, em) or core lock (re). lem is missing
94ce3ee1e7SLuigi Rizzo   the RING protection on rx_reset(), this should be added.
95ce3ee1e7SLuigi Rizzo 
96ce3ee1e7SLuigi Rizzo   On linux there is an external lock on the tx path, which probably
97ce3ee1e7SLuigi Rizzo   also arbitrates access to the reset routine. XXX to be revised
98ce3ee1e7SLuigi Rizzo 
99ce3ee1e7SLuigi Rizzo - a per-interface core_lock protecting access from the host stack
100ce3ee1e7SLuigi Rizzo   while interfaces may be detached from netmap mode.
101ce3ee1e7SLuigi Rizzo   XXX there should be no need for this lock if we detach the interfaces
102ce3ee1e7SLuigi Rizzo   only while they are down.
103ce3ee1e7SLuigi Rizzo 
104ce3ee1e7SLuigi Rizzo 
105ce3ee1e7SLuigi Rizzo --- VALE SWITCH ---
106ce3ee1e7SLuigi Rizzo 
107ce3ee1e7SLuigi Rizzo NMG_LOCK() serializes all modifications to switches and ports.
108ce3ee1e7SLuigi Rizzo A switch cannot be deleted until all ports are gone.
109ce3ee1e7SLuigi Rizzo 
110ce3ee1e7SLuigi Rizzo For each switch, an SX lock (RWlock on linux) protects
111ce3ee1e7SLuigi Rizzo deletion of ports. When configuring or deleting a new port, the
112ce3ee1e7SLuigi Rizzo lock is acquired in exclusive mode (after holding NMG_LOCK).
113ce3ee1e7SLuigi Rizzo When forwarding, the lock is acquired in shared mode (without NMG_LOCK).
114ce3ee1e7SLuigi Rizzo The lock is held throughout the entire forwarding cycle,
115ce3ee1e7SLuigi Rizzo during which the thread may incur in a page fault.
116ce3ee1e7SLuigi Rizzo Hence it is important that sleepable shared locks are used.
117ce3ee1e7SLuigi Rizzo 
118ce3ee1e7SLuigi Rizzo On the rx ring, the per-port lock is grabbed initially to reserve
119ce3ee1e7SLuigi Rizzo a number of slot in the ring, then the lock is released,
120ce3ee1e7SLuigi Rizzo packets are copied from source to destination, and then
121ce3ee1e7SLuigi Rizzo the lock is acquired again and the receive ring is updated.
122ce3ee1e7SLuigi Rizzo (A similar thing is done on the tx ring for NIC and host stack
123ce3ee1e7SLuigi Rizzo ports attached to the switch)
124ce3ee1e7SLuigi Rizzo 
12568b8534bSLuigi Rizzo  */
12668b8534bSLuigi Rizzo 
127*4bf50f18SLuigi Rizzo 
128*4bf50f18SLuigi Rizzo /* --- internals ----
129*4bf50f18SLuigi Rizzo  *
130*4bf50f18SLuigi Rizzo  * Roadmap to the code that implements the above.
131*4bf50f18SLuigi Rizzo  *
132*4bf50f18SLuigi Rizzo  * > 1. a process/thread issues one or more open() on /dev/netmap, to create
133*4bf50f18SLuigi Rizzo  * >    select()able file descriptor on which events are reported.
134*4bf50f18SLuigi Rizzo  *
135*4bf50f18SLuigi Rizzo  *  	Internally, we allocate a netmap_priv_d structure, that will be
136*4bf50f18SLuigi Rizzo  *  	initialized on ioctl(NIOCREGIF).
137*4bf50f18SLuigi Rizzo  *
138*4bf50f18SLuigi Rizzo  *      os-specific:
139*4bf50f18SLuigi Rizzo  *  	    FreeBSD: netmap_open (netmap_freebsd.c). The priv is
140*4bf50f18SLuigi Rizzo  *  		     per-thread.
141*4bf50f18SLuigi Rizzo  *  	    linux:   linux_netmap_open (netmap_linux.c). The priv is
142*4bf50f18SLuigi Rizzo  *  		     per-open.
143*4bf50f18SLuigi Rizzo  *
144*4bf50f18SLuigi Rizzo  * > 2. on each descriptor, the process issues an ioctl() to identify
145*4bf50f18SLuigi Rizzo  * >    the interface that should report events to the file descriptor.
146*4bf50f18SLuigi Rizzo  *
147*4bf50f18SLuigi Rizzo  * 	Implemented by netmap_ioctl(), NIOCREGIF case, with nmr->nr_cmd==0.
148*4bf50f18SLuigi Rizzo  * 	Most important things happen in netmap_get_na() and
149*4bf50f18SLuigi Rizzo  * 	netmap_do_regif(), called from there. Additional details can be
150*4bf50f18SLuigi Rizzo  * 	found in the comments above those functions.
151*4bf50f18SLuigi Rizzo  *
152*4bf50f18SLuigi Rizzo  * 	In all cases, this action creates/takes-a-reference-to a
153*4bf50f18SLuigi Rizzo  * 	netmap_*_adapter describing the port, and allocates a netmap_if
154*4bf50f18SLuigi Rizzo  * 	and all necessary netmap rings, filling them with netmap buffers.
155*4bf50f18SLuigi Rizzo  *
156*4bf50f18SLuigi Rizzo  *      In this phase, the sync callbacks for each ring are set (these are used
157*4bf50f18SLuigi Rizzo  *      in steps 5 and 6 below).  The callbacks depend on the type of adapter.
158*4bf50f18SLuigi Rizzo  *      The adapter creation/initialization code puts them in the
159*4bf50f18SLuigi Rizzo  * 	netmap_adapter (fields na->nm_txsync and na->nm_rxsync).  Then, they
160*4bf50f18SLuigi Rizzo  * 	are copied from there to the netmap_kring's during netmap_do_regif(), by
161*4bf50f18SLuigi Rizzo  * 	the nm_krings_create() callback.  All the nm_krings_create callbacks
162*4bf50f18SLuigi Rizzo  * 	actually call netmap_krings_create() to perform this and the other
163*4bf50f18SLuigi Rizzo  * 	common stuff. netmap_krings_create() also takes care of the host rings,
164*4bf50f18SLuigi Rizzo  * 	if needed, by setting their sync callbacks appropriately.
165*4bf50f18SLuigi Rizzo  *
166*4bf50f18SLuigi Rizzo  * 	Additional actions depend on the kind of netmap_adapter that has been
167*4bf50f18SLuigi Rizzo  * 	registered:
168*4bf50f18SLuigi Rizzo  *
169*4bf50f18SLuigi Rizzo  * 	- netmap_hw_adapter:  	     [netmap.c]
170*4bf50f18SLuigi Rizzo  * 	     This is a system netdev/ifp with native netmap support.
171*4bf50f18SLuigi Rizzo  * 	     The ifp is detached from the host stack by redirecting:
172*4bf50f18SLuigi Rizzo  * 	       - transmissions (from the network stack) to netmap_transmit()
173*4bf50f18SLuigi Rizzo  * 	       - receive notifications to the nm_notify() callback for
174*4bf50f18SLuigi Rizzo  * 	         this adapter. The callback is normally netmap_notify(), unless
175*4bf50f18SLuigi Rizzo  * 	         the ifp is attached to a bridge using bwrap, in which case it
176*4bf50f18SLuigi Rizzo  * 	         is netmap_bwrap_intr_notify().
177*4bf50f18SLuigi Rizzo  *
178*4bf50f18SLuigi Rizzo  * 	- netmap_generic_adapter:      [netmap_generic.c]
179*4bf50f18SLuigi Rizzo  * 	      A system netdev/ifp without native netmap support.
180*4bf50f18SLuigi Rizzo  *
181*4bf50f18SLuigi Rizzo  * 	(the decision about native/non native support is taken in
182*4bf50f18SLuigi Rizzo  * 	 netmap_get_hw_na(), called by netmap_get_na())
183*4bf50f18SLuigi Rizzo  *
184*4bf50f18SLuigi Rizzo  * 	- netmap_vp_adapter 		[netmap_vale.c]
185*4bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_bdg_na().
186*4bf50f18SLuigi Rizzo  * 	      This is a persistent or ephemeral VALE port. Ephemeral ports
187*4bf50f18SLuigi Rizzo  * 	      are created on the fly if they don't already exist, and are
188*4bf50f18SLuigi Rizzo  * 	      always attached to a bridge.
189*4bf50f18SLuigi Rizzo  * 	      Persistent VALE ports must must be created seperately, and i
190*4bf50f18SLuigi Rizzo  * 	      then attached like normal NICs. The NIOCREGIF we are examining
191*4bf50f18SLuigi Rizzo  * 	      will find them only if they had previosly been created and
192*4bf50f18SLuigi Rizzo  * 	      attached (see VALE_CTL below).
193*4bf50f18SLuigi Rizzo  *
194*4bf50f18SLuigi Rizzo  * 	- netmap_pipe_adapter 	      [netmap_pipe.c]
195*4bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_pipe_na().
196*4bf50f18SLuigi Rizzo  * 	      Both pipe ends are created, if they didn't already exist.
197*4bf50f18SLuigi Rizzo  *
198*4bf50f18SLuigi Rizzo  * 	- netmap_monitor_adapter      [netmap_monitor.c]
199*4bf50f18SLuigi Rizzo  * 	      Returned by netmap_get_monitor_na().
200*4bf50f18SLuigi Rizzo  * 	      If successful, the nm_sync callbacks of the monitored adapter
201*4bf50f18SLuigi Rizzo  * 	      will be intercepted by the returned monitor.
202*4bf50f18SLuigi Rizzo  *
203*4bf50f18SLuigi Rizzo  * 	- netmap_bwrap_adapter	      [netmap_vale.c]
204*4bf50f18SLuigi Rizzo  * 	      Cannot be obtained in this way, see VALE_CTL below
205*4bf50f18SLuigi Rizzo  *
206*4bf50f18SLuigi Rizzo  *
207*4bf50f18SLuigi Rizzo  * 	os-specific:
208*4bf50f18SLuigi Rizzo  * 	    linux: we first go through linux_netmap_ioctl() to
209*4bf50f18SLuigi Rizzo  * 	           adapt the FreeBSD interface to the linux one.
210*4bf50f18SLuigi Rizzo  *
211*4bf50f18SLuigi Rizzo  *
212*4bf50f18SLuigi Rizzo  * > 3. on each descriptor, the process issues an mmap() request to
213*4bf50f18SLuigi Rizzo  * >    map the shared memory region within the process' address space.
214*4bf50f18SLuigi Rizzo  * >    The list of interesting queues is indicated by a location in
215*4bf50f18SLuigi Rizzo  * >    the shared memory region.
216*4bf50f18SLuigi Rizzo  *
217*4bf50f18SLuigi Rizzo  *      os-specific:
218*4bf50f18SLuigi Rizzo  *  	    FreeBSD: netmap_mmap_single (netmap_freebsd.c).
219*4bf50f18SLuigi Rizzo  *  	    linux:   linux_netmap_mmap (netmap_linux.c).
220*4bf50f18SLuigi Rizzo  *
221*4bf50f18SLuigi Rizzo  * > 4. using the functions in the netmap(4) userspace API, a process
222*4bf50f18SLuigi Rizzo  * >    can look up the occupation state of a queue, access memory buffers,
223*4bf50f18SLuigi Rizzo  * >    and retrieve received packets or enqueue packets to transmit.
224*4bf50f18SLuigi Rizzo  *
225*4bf50f18SLuigi Rizzo  * 	these actions do not involve the kernel.
226*4bf50f18SLuigi Rizzo  *
227*4bf50f18SLuigi Rizzo  * > 5. using some ioctl()s the process can synchronize the userspace view
228*4bf50f18SLuigi Rizzo  * >    of the queue with the actual status in the kernel. This includes both
229*4bf50f18SLuigi Rizzo  * >    receiving the notification of new packets, and transmitting new
230*4bf50f18SLuigi Rizzo  * >    packets on the output interface.
231*4bf50f18SLuigi Rizzo  *
232*4bf50f18SLuigi Rizzo  * 	These are implemented in netmap_ioctl(), NIOCTXSYNC and NIOCRXSYNC
233*4bf50f18SLuigi Rizzo  * 	cases. They invoke the nm_sync callbacks on the netmap_kring
234*4bf50f18SLuigi Rizzo  * 	structures, as initialized in step 2 and maybe later modified
235*4bf50f18SLuigi Rizzo  * 	by a monitor. Monitors, however, will always call the original
236*4bf50f18SLuigi Rizzo  * 	callback before doing anything else.
237*4bf50f18SLuigi Rizzo  *
238*4bf50f18SLuigi Rizzo  *
239*4bf50f18SLuigi Rizzo  * > 6. select() or poll() can be used to wait for events on individual
240*4bf50f18SLuigi Rizzo  * >    transmit or receive queues (or all queues for a given interface).
241*4bf50f18SLuigi Rizzo  *
242*4bf50f18SLuigi Rizzo  * 	Implemented in netmap_poll(). This will call the same nm_sync()
243*4bf50f18SLuigi Rizzo  * 	callbacks as in step 5 above.
244*4bf50f18SLuigi Rizzo  *
245*4bf50f18SLuigi Rizzo  * 	os-specific:
246*4bf50f18SLuigi Rizzo  * 		linux: we first go through linux_netmap_poll() to adapt
247*4bf50f18SLuigi Rizzo  * 		       the FreeBSD interface to the linux one.
248*4bf50f18SLuigi Rizzo  *
249*4bf50f18SLuigi Rizzo  *
250*4bf50f18SLuigi Rizzo  *  ----  VALE_CTL -----
251*4bf50f18SLuigi Rizzo  *
252*4bf50f18SLuigi Rizzo  *  VALE switches are controlled by issuing a NIOCREGIF with a non-null
253*4bf50f18SLuigi Rizzo  *  nr_cmd in the nmreq structure. These subcommands are handled by
254*4bf50f18SLuigi Rizzo  *  netmap_bdg_ctl() in netmap_vale.c. Persistent VALE ports are created
255*4bf50f18SLuigi Rizzo  *  and destroyed by issuing the NETMAP_BDG_NEWIF and NETMAP_BDG_DELIF
256*4bf50f18SLuigi Rizzo  *  subcommands, respectively.
257*4bf50f18SLuigi Rizzo  *
258*4bf50f18SLuigi Rizzo  *  Any network interface known to the system (including a persistent VALE
259*4bf50f18SLuigi Rizzo  *  port) can be attached to a VALE switch by issuing the
260*4bf50f18SLuigi Rizzo  *  NETMAP_BDG_ATTACH subcommand. After the attachment, persistent VALE ports
261*4bf50f18SLuigi Rizzo  *  look exactly like ephemeral VALE ports (as created in step 2 above).  The
262*4bf50f18SLuigi Rizzo  *  attachment of other interfaces, instead, requires the creation of a
263*4bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  Moreover, the attached interface must be put in
264*4bf50f18SLuigi Rizzo  *  netmap mode. This may require the creation of a netmap_generic_adapter if
265*4bf50f18SLuigi Rizzo  *  we have no native support for the interface, or if generic adapters have
266*4bf50f18SLuigi Rizzo  *  been forced by sysctl.
267*4bf50f18SLuigi Rizzo  *
268*4bf50f18SLuigi Rizzo  *  Both persistent VALE ports and bwraps are handled by netmap_get_bdg_na(),
269*4bf50f18SLuigi Rizzo  *  called by nm_bdg_ctl_attach(), and discriminated by the nm_bdg_attach()
270*4bf50f18SLuigi Rizzo  *  callback.  In the case of the bwrap, the callback creates the
271*4bf50f18SLuigi Rizzo  *  netmap_bwrap_adapter.  The initialization of the bwrap is then
272*4bf50f18SLuigi Rizzo  *  completed by calling netmap_do_regif() on it, in the nm_bdg_ctl()
273*4bf50f18SLuigi Rizzo  *  callback (netmap_bwrap_bdg_ctl in netmap_vale.c).
274*4bf50f18SLuigi Rizzo  *  A generic adapter for the wrapped ifp will be created if needed, when
275*4bf50f18SLuigi Rizzo  *  netmap_get_bdg_na() calls netmap_get_hw_na().
276*4bf50f18SLuigi Rizzo  *
277*4bf50f18SLuigi Rizzo  *
278*4bf50f18SLuigi Rizzo  *  ---- DATAPATHS -----
279*4bf50f18SLuigi Rizzo  *
280*4bf50f18SLuigi Rizzo  *              -= SYSTEM DEVICE WITH NATIVE SUPPORT =-
281*4bf50f18SLuigi Rizzo  *
282*4bf50f18SLuigi Rizzo  *    na == NA(ifp) == netmap_hw_adapter created in DEVICE_netmap_attach()
283*4bf50f18SLuigi Rizzo  *
284*4bf50f18SLuigi Rizzo  *    - tx from netmap userspace:
285*4bf50f18SLuigi Rizzo  *	 concurrently:
286*4bf50f18SLuigi Rizzo  *           1) ioctl(NIOCTXSYNC)/netmap_poll() in process context
287*4bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_txsync()
288*4bf50f18SLuigi Rizzo  *           2) device interrupt handler
289*4bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
290*4bf50f18SLuigi Rizzo  *    - rx from netmap userspace:
291*4bf50f18SLuigi Rizzo  *       concurrently:
292*4bf50f18SLuigi Rizzo  *           1) ioctl(NIOCRXSYNC)/netmap_poll() in process context
293*4bf50f18SLuigi Rizzo  *                kring->nm_sync() == DEVICE_netmap_rxsync()
294*4bf50f18SLuigi Rizzo  *           2) device interrupt handler
295*4bf50f18SLuigi Rizzo  *                na->nm_notify()  == netmap_notify()
296*4bf50f18SLuigi Rizzo  *    - tx from host stack
297*4bf50f18SLuigi Rizzo  *       concurrently:
298*4bf50f18SLuigi Rizzo  *           1) host stack
299*4bf50f18SLuigi Rizzo  *                netmap_transmit()
300*4bf50f18SLuigi Rizzo  *                  na->nm_notify  == netmap_notify()
301*4bf50f18SLuigi Rizzo  *           2) ioctl(NIOCRXSYNC)/netmap_poll() in process context
302*4bf50f18SLuigi Rizzo  *                kring->nm_sync() == netmap_rxsync_from_host_compat
303*4bf50f18SLuigi Rizzo  *                  netmap_rxsync_from_host(na, NULL, NULL)
304*4bf50f18SLuigi Rizzo  *    - tx to host stack
305*4bf50f18SLuigi Rizzo  *           ioctl(NIOCTXSYNC)/netmap_poll() in process context
306*4bf50f18SLuigi Rizzo  *             kring->nm_sync() == netmap_txsync_to_host_compat
307*4bf50f18SLuigi Rizzo  *               netmap_txsync_to_host(na)
308*4bf50f18SLuigi Rizzo  *                 NM_SEND_UP()
309*4bf50f18SLuigi Rizzo  *                   FreeBSD: na->if_input() == ?? XXX
310*4bf50f18SLuigi Rizzo  *                   linux: netif_rx() with NM_MAGIC_PRIORITY_RX
311*4bf50f18SLuigi Rizzo  *
312*4bf50f18SLuigi Rizzo  *
313*4bf50f18SLuigi Rizzo  *
314*4bf50f18SLuigi Rizzo  *               -= SYSTEM DEVICE WITH GENERIC SUPPORT =-
315*4bf50f18SLuigi Rizzo  *
316*4bf50f18SLuigi Rizzo  *
317*4bf50f18SLuigi Rizzo  *
318*4bf50f18SLuigi Rizzo  *                           -= VALE PORT =-
319*4bf50f18SLuigi Rizzo  *
320*4bf50f18SLuigi Rizzo  *
321*4bf50f18SLuigi Rizzo  *
322*4bf50f18SLuigi Rizzo  *                           -= NETMAP PIPE =-
323*4bf50f18SLuigi Rizzo  *
324*4bf50f18SLuigi Rizzo  *
325*4bf50f18SLuigi Rizzo  *
326*4bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH NATIVE SUPPORT, CONNECTED TO VALE, NO HOST RINGS =-
327*4bf50f18SLuigi Rizzo  *
328*4bf50f18SLuigi Rizzo  *
329*4bf50f18SLuigi Rizzo  *
330*4bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH NATIVE SUPPORT, CONNECTED TO VALE, WITH HOST RINGS =-
331*4bf50f18SLuigi Rizzo  *
332*4bf50f18SLuigi Rizzo  *
333*4bf50f18SLuigi Rizzo  *
334*4bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH GENERIC SUPPORT, CONNECTED TO VALE, NO HOST RINGS =-
335*4bf50f18SLuigi Rizzo  *
336*4bf50f18SLuigi Rizzo  *
337*4bf50f18SLuigi Rizzo  *
338*4bf50f18SLuigi Rizzo  *  -= SYSTEM DEVICE WITH GENERIC SUPPORT, CONNECTED TO VALE, WITH HOST RINGS =-
339*4bf50f18SLuigi Rizzo  *
340*4bf50f18SLuigi Rizzo  *
341*4bf50f18SLuigi Rizzo  *
342*4bf50f18SLuigi Rizzo  */
343*4bf50f18SLuigi Rizzo 
344ce3ee1e7SLuigi Rizzo /*
345ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
346ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
347ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
348ce3ee1e7SLuigi Rizzo  */
34901c7d25fSLuigi Rizzo 
350ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
35168b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
35268b8534bSLuigi Rizzo #include <sys/types.h>
35368b8534bSLuigi Rizzo #include <sys/errno.h>
35468b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
35568b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
356f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
35789e3fd52SLuigi Rizzo #include <sys/filio.h>	/* FIONBIO */
35868b8534bSLuigi Rizzo #include <sys/sockio.h>
35968b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
36068b8534bSLuigi Rizzo #include <sys/malloc.h>
36168b8534bSLuigi Rizzo #include <sys/poll.h>
36289f6b863SAttilio Rao #include <sys/rwlock.h>
36368b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
36468b8534bSLuigi Rizzo #include <sys/selinfo.h>
36568b8534bSLuigi Rizzo #include <sys/sysctl.h>
366339f59c0SGleb Smirnoff #include <sys/jail.h>
367339f59c0SGleb Smirnoff #include <net/vnet.h>
36868b8534bSLuigi Rizzo #include <net/if.h>
36976039bc8SGleb Smirnoff #include <net/if_var.h>
37068b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
37168b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
372ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
373ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
37468b8534bSLuigi Rizzo 
37568b8534bSLuigi Rizzo 
376f9790aebSLuigi Rizzo /* reduce conditional code */
377f0ea3689SLuigi Rizzo // linux API, use for the knlist in FreeBSD
378f0ea3689SLuigi Rizzo #define init_waitqueue_head(x)	knlist_init_mtx(&(x)->si_note, NULL)
379ce3ee1e7SLuigi Rizzo 
380f0ea3689SLuigi Rizzo void freebsd_selwakeup(struct selinfo *si, int pri);
381f0ea3689SLuigi Rizzo #define OS_selwakeup(a, b)	freebsd_selwakeup(a, b)
382ce3ee1e7SLuigi Rizzo 
383ce3ee1e7SLuigi Rizzo #elif defined(linux)
384ce3ee1e7SLuigi Rizzo 
385ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
386ce3ee1e7SLuigi Rizzo 
387ce3ee1e7SLuigi Rizzo 
388ce3ee1e7SLuigi Rizzo 
389ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
390ce3ee1e7SLuigi Rizzo 
391ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
392ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
393ce3ee1e7SLuigi Rizzo 
394ce3ee1e7SLuigi Rizzo #else
395ce3ee1e7SLuigi Rizzo 
396ce3ee1e7SLuigi Rizzo #error	Unsupported platform
397ce3ee1e7SLuigi Rizzo 
398ce3ee1e7SLuigi Rizzo #endif /* unsupported */
399ce3ee1e7SLuigi Rizzo 
400ce3ee1e7SLuigi Rizzo /*
401ce3ee1e7SLuigi Rizzo  * common headers
402ce3ee1e7SLuigi Rizzo  */
4030b8ed8e0SLuigi Rizzo #include <net/netmap.h>
4040b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
405ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
4060b8ed8e0SLuigi Rizzo 
407ce3ee1e7SLuigi Rizzo 
408ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
409ce3ee1e7SLuigi Rizzo 
410ce3ee1e7SLuigi Rizzo /*
411ce3ee1e7SLuigi Rizzo  * The following variables are used by the drivers and replicate
412ce3ee1e7SLuigi Rizzo  * fields in the global memory pool. They only refer to buffers
413ce3ee1e7SLuigi Rizzo  * used by physical interfaces.
414ce3ee1e7SLuigi Rizzo  */
4155819da83SLuigi Rizzo u_int netmap_total_buffers;
4168241616dSLuigi Rizzo u_int netmap_buf_size;
417ce3ee1e7SLuigi Rizzo char *netmap_buffer_base;	/* also address of an invalid buffer */
4185819da83SLuigi Rizzo 
4195819da83SLuigi Rizzo /* user-controlled variables */
4205819da83SLuigi Rizzo int netmap_verbose;
4215819da83SLuigi Rizzo 
4225819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
4235819da83SLuigi Rizzo 
4245819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
4255819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
4265819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
4275819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
4285819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
4295819da83SLuigi Rizzo int netmap_mitigate = 1;
4305819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
431c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
4325819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
4335819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
434f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
435f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
436f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
4375819da83SLuigi Rizzo 
438*4bf50f18SLuigi Rizzo int netmap_adaptive_io = 0;
439*4bf50f18SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, adaptive_io, CTLFLAG_RW,
440*4bf50f18SLuigi Rizzo     &netmap_adaptive_io, 0 , "Adaptive I/O on paravirt");
441*4bf50f18SLuigi Rizzo 
442f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
443091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
444ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */
445f196ce38SLuigi Rizzo 
446f9790aebSLuigi Rizzo /*
447f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
448f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
449f9790aebSLuigi Rizzo  */
450f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
451f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
452f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
453f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
454f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
455f9790aebSLuigi Rizzo 
456f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
457f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
458f0ea3689SLuigi Rizzo int netmap_generic_rings = 1;   /* number of queues in generic. */
459f9790aebSLuigi Rizzo 
460f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
461091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
462ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, "");
463f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
464f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
465f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
466f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW, &netmap_generic_rings, 0 , "");
467f196ce38SLuigi Rizzo 
468ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
469ce3ee1e7SLuigi Rizzo 
470ce3ee1e7SLuigi Rizzo 
471f9790aebSLuigi Rizzo static void
472f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr)
473ce3ee1e7SLuigi Rizzo {
474ce3ee1e7SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
475ce3ee1e7SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
476ce3ee1e7SLuigi Rizzo }
477ce3ee1e7SLuigi Rizzo 
478f9790aebSLuigi Rizzo 
47917885a7bSLuigi Rizzo /*
48017885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
48117885a7bSLuigi Rizzo  * to make sure other users get to see it.
48217885a7bSLuigi Rizzo  */
483*4bf50f18SLuigi Rizzo static void
484f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
485ce3ee1e7SLuigi Rizzo {
486ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
487ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
488ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
489ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
490ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
491ce3ee1e7SLuigi Rizzo }
492ce3ee1e7SLuigi Rizzo 
493*4bf50f18SLuigi Rizzo /* stop or enable a single tx ring */
494*4bf50f18SLuigi Rizzo void
495*4bf50f18SLuigi Rizzo netmap_set_txring(struct netmap_adapter *na, u_int ring_id, int stopped)
496*4bf50f18SLuigi Rizzo {
497*4bf50f18SLuigi Rizzo 	if (stopped)
498*4bf50f18SLuigi Rizzo 		netmap_disable_ring(na->tx_rings + ring_id);
499*4bf50f18SLuigi Rizzo 	else
500*4bf50f18SLuigi Rizzo 		na->tx_rings[ring_id].nkr_stopped = 0;
501*4bf50f18SLuigi Rizzo 	/* nofify that the stopped state has changed. This is currently
502*4bf50f18SLuigi Rizzo 	 *only used by bwrap to propagate the state to its own krings.
503*4bf50f18SLuigi Rizzo 	 * (see netmap_bwrap_intr_notify).
504*4bf50f18SLuigi Rizzo 	 */
505*4bf50f18SLuigi Rizzo 	na->nm_notify(na, ring_id, NR_TX, NAF_DISABLE_NOTIFY);
506*4bf50f18SLuigi Rizzo }
507*4bf50f18SLuigi Rizzo 
508*4bf50f18SLuigi Rizzo /* stop or enable a single rx ring */
509*4bf50f18SLuigi Rizzo void
510*4bf50f18SLuigi Rizzo netmap_set_rxring(struct netmap_adapter *na, u_int ring_id, int stopped)
511*4bf50f18SLuigi Rizzo {
512*4bf50f18SLuigi Rizzo 	if (stopped)
513*4bf50f18SLuigi Rizzo 		netmap_disable_ring(na->rx_rings + ring_id);
514*4bf50f18SLuigi Rizzo 	else
515*4bf50f18SLuigi Rizzo 		na->rx_rings[ring_id].nkr_stopped = 0;
516*4bf50f18SLuigi Rizzo 	/* nofify that the stopped state has changed. This is currently
517*4bf50f18SLuigi Rizzo 	 *only used by bwrap to propagate the state to its own krings.
518*4bf50f18SLuigi Rizzo 	 * (see netmap_bwrap_intr_notify).
519*4bf50f18SLuigi Rizzo 	 */
520*4bf50f18SLuigi Rizzo 	na->nm_notify(na, ring_id, NR_RX, NAF_DISABLE_NOTIFY);
521*4bf50f18SLuigi Rizzo }
522*4bf50f18SLuigi Rizzo 
523f9790aebSLuigi Rizzo 
52489cc2556SLuigi Rizzo /* stop or enable all the rings of na */
525*4bf50f18SLuigi Rizzo void
526*4bf50f18SLuigi Rizzo netmap_set_all_rings(struct netmap_adapter *na, int stopped)
527ce3ee1e7SLuigi Rizzo {
528ce3ee1e7SLuigi Rizzo 	int i;
529f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
530ce3ee1e7SLuigi Rizzo 
531*4bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
532ce3ee1e7SLuigi Rizzo 		return;
533ce3ee1e7SLuigi Rizzo 
534f0ea3689SLuigi Rizzo 	ntx = netmap_real_tx_rings(na);
535f0ea3689SLuigi Rizzo 	nrx = netmap_real_rx_rings(na);
536f0ea3689SLuigi Rizzo 
537f0ea3689SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
538*4bf50f18SLuigi Rizzo 		netmap_set_txring(na, i, stopped);
539ce3ee1e7SLuigi Rizzo 	}
540f9790aebSLuigi Rizzo 
541f0ea3689SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
542*4bf50f18SLuigi Rizzo 		netmap_set_rxring(na, i, stopped);
543ce3ee1e7SLuigi Rizzo 	}
544ce3ee1e7SLuigi Rizzo }
545ce3ee1e7SLuigi Rizzo 
54689cc2556SLuigi Rizzo /*
54789cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
54889cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
54989cc2556SLuigi Rizzo  * netmap mode off, or before removing the harware rings (e.g., on module
55089cc2556SLuigi Rizzo  * onload).  As a rule of thumb for linux drivers, this should be placed near
55189cc2556SLuigi Rizzo  * each napi_disable().
55289cc2556SLuigi Rizzo  */
553f9790aebSLuigi Rizzo void
554f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
555f9790aebSLuigi Rizzo {
556*4bf50f18SLuigi Rizzo 	netmap_set_all_rings(NA(ifp), 1 /* stopped */);
557f9790aebSLuigi Rizzo }
558f9790aebSLuigi Rizzo 
55989cc2556SLuigi Rizzo /*
56089cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
56189cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
56289cc2556SLuigi Rizzo  * napi_enable().
56389cc2556SLuigi Rizzo  */
564f9790aebSLuigi Rizzo void
565f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
566f9790aebSLuigi Rizzo {
567*4bf50f18SLuigi Rizzo 	netmap_set_all_rings(NA(ifp), 0 /* enabled */);
568f9790aebSLuigi Rizzo }
569f9790aebSLuigi Rizzo 
570f9790aebSLuigi Rizzo 
571ce3ee1e7SLuigi Rizzo /*
572ce3ee1e7SLuigi Rizzo  * generic bound_checking function
573ce3ee1e7SLuigi Rizzo  */
574ce3ee1e7SLuigi Rizzo u_int
575ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
576ce3ee1e7SLuigi Rizzo {
577ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
578ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
579ce3ee1e7SLuigi Rizzo 
580ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
581ce3ee1e7SLuigi Rizzo 		dflt = lo;
582ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
583ce3ee1e7SLuigi Rizzo 		dflt = hi;
584ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
585ce3ee1e7SLuigi Rizzo 		*v = dflt;
586ce3ee1e7SLuigi Rizzo 		op = "Bump";
587ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
588ce3ee1e7SLuigi Rizzo 		*v = hi;
589ce3ee1e7SLuigi Rizzo 		op = "Clamp";
590ce3ee1e7SLuigi Rizzo 	}
591ce3ee1e7SLuigi Rizzo 	if (op && msg)
592ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
593ce3ee1e7SLuigi Rizzo 	return *v;
594ce3ee1e7SLuigi Rizzo }
595ce3ee1e7SLuigi Rizzo 
596f9790aebSLuigi Rizzo 
597ce3ee1e7SLuigi Rizzo /*
598ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
599ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
600ce3ee1e7SLuigi Rizzo  */
601ce3ee1e7SLuigi Rizzo const char *
602ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
603ce3ee1e7SLuigi Rizzo {
604ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
605ce3ee1e7SLuigi Rizzo 	int i, j, i0;
606ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
607ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
608ce3ee1e7SLuigi Rizzo 
609ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
610ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
611ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
612ce3ee1e7SLuigi Rizzo 	if (!dst)
613ce3ee1e7SLuigi Rizzo 		dst = _dst;
614ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
615ce3ee1e7SLuigi Rizzo 		lim = len;
616ce3ee1e7SLuigi Rizzo 	o = dst;
617ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
618ce3ee1e7SLuigi Rizzo 	o += strlen(o);
619ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
620ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
621ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
622ce3ee1e7SLuigi Rizzo 		o += strlen(o);
623ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
624ce3ee1e7SLuigi Rizzo 		i0 = i;
625ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
626ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
627ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
628ce3ee1e7SLuigi Rizzo 		}
629ce3ee1e7SLuigi Rizzo 		i = i0;
630ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
631ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
632ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
633ce3ee1e7SLuigi Rizzo 		o += j+49;
634ce3ee1e7SLuigi Rizzo 	}
635ce3ee1e7SLuigi Rizzo 	*o = '\0';
636ce3ee1e7SLuigi Rizzo #undef P_HI
637ce3ee1e7SLuigi Rizzo #undef P_LO
638ce3ee1e7SLuigi Rizzo #undef P_C
639ce3ee1e7SLuigi Rizzo 	return dst;
640ce3ee1e7SLuigi Rizzo }
641f196ce38SLuigi Rizzo 
642f18be576SLuigi Rizzo 
643ae10d1afSLuigi Rizzo /*
644ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
645ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
646ae10d1afSLuigi Rizzo  */
64789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
648f9790aebSLuigi Rizzo int
649ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
650ae10d1afSLuigi Rizzo {
651ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
652ae10d1afSLuigi Rizzo 
653ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
654ae10d1afSLuigi Rizzo 	if (na->nm_config) {
655f9790aebSLuigi Rizzo 		na->nm_config(na, &txr, &txd, &rxr, &rxd);
656ae10d1afSLuigi Rizzo 	} else {
657ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
658ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
659ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
660ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
661ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
662ae10d1afSLuigi Rizzo 	}
663ae10d1afSLuigi Rizzo 
664ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
665ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
666ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
667f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
668ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
669*4bf50f18SLuigi Rizzo 			na->name,
670ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
671ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
672ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
673*4bf50f18SLuigi Rizzo 			na->name, txr, txd, rxr, rxd);
674ae10d1afSLuigi Rizzo 	}
675f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
676ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
677ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
678ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
679ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
680ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
681ae10d1afSLuigi Rizzo 		return 0;
682ae10d1afSLuigi Rizzo 	}
683ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
684ae10d1afSLuigi Rizzo 	return 1;
685ae10d1afSLuigi Rizzo }
686ae10d1afSLuigi Rizzo 
68789cc2556SLuigi Rizzo /* kring->nm_sync callback for the host tx ring */
688f0ea3689SLuigi Rizzo static int
689f0ea3689SLuigi Rizzo netmap_txsync_to_host_compat(struct netmap_kring *kring, int flags)
690f0ea3689SLuigi Rizzo {
69189cc2556SLuigi Rizzo 	(void)flags; /* unused */
692f0ea3689SLuigi Rizzo 	netmap_txsync_to_host(kring->na);
693f0ea3689SLuigi Rizzo 	return 0;
694f0ea3689SLuigi Rizzo }
695f0ea3689SLuigi Rizzo 
69689cc2556SLuigi Rizzo /* kring->nm_sync callback for the host rx ring */
697f0ea3689SLuigi Rizzo static int
698f0ea3689SLuigi Rizzo netmap_rxsync_from_host_compat(struct netmap_kring *kring, int flags)
699f0ea3689SLuigi Rizzo {
70089cc2556SLuigi Rizzo 	(void)flags; /* unused */
701f0ea3689SLuigi Rizzo 	netmap_rxsync_from_host(kring->na, NULL, NULL);
702f0ea3689SLuigi Rizzo 	return 0;
703f0ea3689SLuigi Rizzo }
704f0ea3689SLuigi Rizzo 
705f0ea3689SLuigi Rizzo 
706f0ea3689SLuigi Rizzo 
707f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
708f0ea3689SLuigi Rizzo  * The array layout is this:
709f0ea3689SLuigi Rizzo  *
710f0ea3689SLuigi Rizzo  *                    +----------+
711f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
712f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
713f0ea3689SLuigi Rizzo  *                    |          | /
714f0ea3689SLuigi Rizzo  *                    +----------+
715f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
716f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
717f0ea3689SLuigi Rizzo  *                    |          | \
718f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
719f0ea3689SLuigi Rizzo  *                    |          | /
720f0ea3689SLuigi Rizzo  *                    +----------+
721f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
722f0ea3689SLuigi Rizzo  *                    +----------+
723f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
724f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
725f0ea3689SLuigi Rizzo  *                    |          | /
726f0ea3689SLuigi Rizzo  *                    +----------+
727f0ea3689SLuigi Rizzo  *
728f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
729f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
730f0ea3689SLuigi Rizzo  */
73189cc2556SLuigi Rizzo /* call with NMG_LOCK held */
732f9790aebSLuigi Rizzo int
733f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
734f9790aebSLuigi Rizzo {
735f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
736f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
737f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
738f9790aebSLuigi Rizzo 
739f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
740f0ea3689SLuigi Rizzo 	ntx = na->num_tx_rings + 1;
741f0ea3689SLuigi Rizzo 	nrx = na->num_rx_rings + 1;
742f0ea3689SLuigi Rizzo 
743f9790aebSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom;
744f9790aebSLuigi Rizzo 
745f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
746f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
747f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
748f9790aebSLuigi Rizzo 		return ENOMEM;
749f9790aebSLuigi Rizzo 	}
750f9790aebSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
751f9790aebSLuigi Rizzo 
75217885a7bSLuigi Rizzo 	/*
75317885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
75417885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
75517885a7bSLuigi Rizzo 	 */
756f9790aebSLuigi Rizzo 	ndesc = na->num_tx_desc;
757f9790aebSLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
758f9790aebSLuigi Rizzo 		kring = &na->tx_rings[i];
759f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
760f9790aebSLuigi Rizzo 		kring->na = na;
76117885a7bSLuigi Rizzo 		kring->ring_id = i;
762f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
763f0ea3689SLuigi Rizzo 		if (i < na->num_tx_rings) {
764*4bf50f18SLuigi Rizzo 			kring->nm_sync = na->nm_txsync;
765f0ea3689SLuigi Rizzo 		} else if (i == na->num_tx_rings) {
766f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_txsync_to_host_compat;
767f0ea3689SLuigi Rizzo 		}
768f9790aebSLuigi Rizzo 		/*
76917885a7bSLuigi Rizzo 		 * IMPORTANT: Always keep one slot empty.
770f9790aebSLuigi Rizzo 		 */
77117885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
77217885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = ndesc - 1;
773*4bf50f18SLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", na->name, i);
774f0ea3689SLuigi Rizzo 		ND("ktx %s h %d c %d t %d",
775f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
776f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF);
777f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
778f9790aebSLuigi Rizzo 	}
779f9790aebSLuigi Rizzo 
780f9790aebSLuigi Rizzo 	ndesc = na->num_rx_desc;
781f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
782f9790aebSLuigi Rizzo 		kring = &na->rx_rings[i];
783f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
784f9790aebSLuigi Rizzo 		kring->na = na;
78517885a7bSLuigi Rizzo 		kring->ring_id = i;
786f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
787f0ea3689SLuigi Rizzo 		if (i < na->num_rx_rings) {
788*4bf50f18SLuigi Rizzo 			kring->nm_sync = na->nm_rxsync;
789f0ea3689SLuigi Rizzo 		} else if (i == na->num_rx_rings) {
790f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_rxsync_from_host_compat;
791f0ea3689SLuigi Rizzo 		}
79217885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
79317885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = 0;
794*4bf50f18SLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", na->name, i);
795f0ea3689SLuigi Rizzo 		ND("krx %s h %d c %d t %d",
796f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
797f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF);
798f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
799f9790aebSLuigi Rizzo 	}
800f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
801f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
802f9790aebSLuigi Rizzo 
803f9790aebSLuigi Rizzo 	na->tailroom = na->rx_rings + nrx;
804f9790aebSLuigi Rizzo 
805f9790aebSLuigi Rizzo 	return 0;
806f9790aebSLuigi Rizzo }
807f9790aebSLuigi Rizzo 
808f9790aebSLuigi Rizzo 
809f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
81089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
811f9790aebSLuigi Rizzo void
812f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
813f9790aebSLuigi Rizzo {
814f0ea3689SLuigi Rizzo 	struct netmap_kring *kring = na->tx_rings;
815f9790aebSLuigi Rizzo 
816f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
817f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
818f0ea3689SLuigi Rizzo 		mtx_destroy(&kring->q_lock);
819f9790aebSLuigi Rizzo 	}
820f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
821f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
822f9790aebSLuigi Rizzo }
823f9790aebSLuigi Rizzo 
824f9790aebSLuigi Rizzo 
82517885a7bSLuigi Rizzo /*
82617885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
82717885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
82817885a7bSLuigi Rizzo  * them first.
82917885a7bSLuigi Rizzo  */
83089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
83117885a7bSLuigi Rizzo static void
83217885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
83317885a7bSLuigi Rizzo {
83417885a7bSLuigi Rizzo 	struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue;
83517885a7bSLuigi Rizzo 
83617885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
83717885a7bSLuigi Rizzo 	mbq_purge(q);
83817885a7bSLuigi Rizzo 	mbq_safe_destroy(q);
83917885a7bSLuigi Rizzo 	netmap_krings_delete(na);
84017885a7bSLuigi Rizzo }
84117885a7bSLuigi Rizzo 
84217885a7bSLuigi Rizzo 
84389cc2556SLuigi Rizzo /* create a new netmap_if for a newly registered fd.
84489cc2556SLuigi Rizzo  * If this is the first registration of the adapter,
84589cc2556SLuigi Rizzo  * also create the netmap rings and their in-kernel view,
84689cc2556SLuigi Rizzo  * the netmap krings.
84789cc2556SLuigi Rizzo  */
84889cc2556SLuigi Rizzo /* call with NMG_LOCK held */
849ce3ee1e7SLuigi Rizzo static struct netmap_if*
850*4bf50f18SLuigi Rizzo netmap_if_new(struct netmap_adapter *na)
851ce3ee1e7SLuigi Rizzo {
852f9790aebSLuigi Rizzo 	struct netmap_if *nifp;
853f9790aebSLuigi Rizzo 
854ce3ee1e7SLuigi Rizzo 	if (netmap_update_config(na)) {
855ce3ee1e7SLuigi Rizzo 		/* configuration mismatch, report and fail */
856ce3ee1e7SLuigi Rizzo 		return NULL;
857ce3ee1e7SLuigi Rizzo 	}
858f9790aebSLuigi Rizzo 
85989cc2556SLuigi Rizzo 	if (na->active_fds)	/* already registered */
860f9790aebSLuigi Rizzo 		goto final;
861f9790aebSLuigi Rizzo 
86289cc2556SLuigi Rizzo 	/* create and init the krings arrays.
86389cc2556SLuigi Rizzo 	 * Depending on the adapter, this may also create
86489cc2556SLuigi Rizzo 	 * the netmap rings themselves
86589cc2556SLuigi Rizzo 	 */
866f9790aebSLuigi Rizzo 	if (na->nm_krings_create(na))
867*4bf50f18SLuigi Rizzo 		return NULL;
868f9790aebSLuigi Rizzo 
86989cc2556SLuigi Rizzo 	/* create all missing netmap rings */
870f9790aebSLuigi Rizzo 	if (netmap_mem_rings_create(na))
871f9790aebSLuigi Rizzo 		goto cleanup;
872f9790aebSLuigi Rizzo 
873f9790aebSLuigi Rizzo final:
874f9790aebSLuigi Rizzo 
87589cc2556SLuigi Rizzo 	/* in all cases, create a new netmap if */
876*4bf50f18SLuigi Rizzo 	nifp = netmap_mem_if_new(na);
877f9790aebSLuigi Rizzo 	if (nifp == NULL)
878f9790aebSLuigi Rizzo 		goto cleanup;
879f9790aebSLuigi Rizzo 
880f9790aebSLuigi Rizzo 	return (nifp);
881f9790aebSLuigi Rizzo 
882f9790aebSLuigi Rizzo cleanup:
883f9790aebSLuigi Rizzo 
884f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
885f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
886f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
887ce3ee1e7SLuigi Rizzo 	}
88868b8534bSLuigi Rizzo 
889f9790aebSLuigi Rizzo 	return NULL;
890f9790aebSLuigi Rizzo }
8918241616dSLuigi Rizzo 
89268b8534bSLuigi Rizzo 
893ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already.  The
894ce3ee1e7SLuigi Rizzo  * reference is taken from the netmap_adapter registered with the priv.
895ce3ee1e7SLuigi Rizzo  */
89689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
897ce3ee1e7SLuigi Rizzo static int
898ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p)
899ce3ee1e7SLuigi Rizzo {
900ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *nmd;
901ce3ee1e7SLuigi Rizzo 	int error = 0;
902ce3ee1e7SLuigi Rizzo 
903f9790aebSLuigi Rizzo 	if (p->np_na == NULL) {
904ce3ee1e7SLuigi Rizzo 		if (!netmap_mmap_unreg)
905ce3ee1e7SLuigi Rizzo 			return ENODEV;
906ce3ee1e7SLuigi Rizzo 		/* for compatibility with older versions of the API
907ce3ee1e7SLuigi Rizzo  		 * we use the global allocator when no interface has been
908ce3ee1e7SLuigi Rizzo  		 * registered
909ce3ee1e7SLuigi Rizzo  		 */
910ce3ee1e7SLuigi Rizzo 		nmd = &nm_mem;
911ce3ee1e7SLuigi Rizzo 	} else {
912f9790aebSLuigi Rizzo 		nmd = p->np_na->nm_mem;
913ce3ee1e7SLuigi Rizzo 	}
914ce3ee1e7SLuigi Rizzo 	if (p->np_mref == NULL) {
915*4bf50f18SLuigi Rizzo 		error = netmap_mem_finalize(nmd, p->np_na);
916ce3ee1e7SLuigi Rizzo 		if (!error)
917ce3ee1e7SLuigi Rizzo 			p->np_mref = nmd;
918ce3ee1e7SLuigi Rizzo 	} else if (p->np_mref != nmd) {
919ce3ee1e7SLuigi Rizzo 		/* a virtual port has been registered, but previous
920ce3ee1e7SLuigi Rizzo  		 * syscalls already used the global allocator.
921ce3ee1e7SLuigi Rizzo  		 * We cannot continue
922ce3ee1e7SLuigi Rizzo  		 */
923ce3ee1e7SLuigi Rizzo 		error = ENODEV;
924ce3ee1e7SLuigi Rizzo 	}
925ce3ee1e7SLuigi Rizzo 	return error;
926ce3ee1e7SLuigi Rizzo }
92768b8534bSLuigi Rizzo 
928f9790aebSLuigi Rizzo 
92989cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
930f9790aebSLuigi Rizzo int
9318241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
9328241616dSLuigi Rizzo {
933ce3ee1e7SLuigi Rizzo 	int error;
934ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
935ce3ee1e7SLuigi Rizzo 	error = netmap_get_memory_locked(p);
936ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
9378241616dSLuigi Rizzo 	return error;
9388241616dSLuigi Rizzo }
9398241616dSLuigi Rizzo 
940f9790aebSLuigi Rizzo 
94189cc2556SLuigi Rizzo /* call with NMG_LOCK held */
942ce3ee1e7SLuigi Rizzo static int
943ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p)
944ce3ee1e7SLuigi Rizzo {
945ce3ee1e7SLuigi Rizzo 	return p->np_mref != NULL;
946ce3ee1e7SLuigi Rizzo }
947ce3ee1e7SLuigi Rizzo 
948f9790aebSLuigi Rizzo 
94989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
950ce3ee1e7SLuigi Rizzo static void
951ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p)
952ce3ee1e7SLuigi Rizzo {
953ce3ee1e7SLuigi Rizzo 	if (p->np_mref) {
954*4bf50f18SLuigi Rizzo 		netmap_mem_deref(p->np_mref, p->np_na);
955ce3ee1e7SLuigi Rizzo 		p->np_mref = NULL;
956ce3ee1e7SLuigi Rizzo 	}
957ce3ee1e7SLuigi Rizzo }
958ce3ee1e7SLuigi Rizzo 
959f9790aebSLuigi Rizzo 
96068b8534bSLuigi Rizzo /*
96168b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
962*4bf50f18SLuigi Rizzo  * revert to normal operation.
963ce3ee1e7SLuigi Rizzo  * The second argument is the nifp to work on. In some cases it is
964ce3ee1e7SLuigi Rizzo  * not attached yet to the netmap_priv_d so we need to pass it as
965ce3ee1e7SLuigi Rizzo  * a separate argument.
96668b8534bSLuigi Rizzo  */
967ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
96868b8534bSLuigi Rizzo static void
969ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp)
97068b8534bSLuigi Rizzo {
971f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
97268b8534bSLuigi Rizzo 
973ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
974f9790aebSLuigi Rizzo 	na->active_fds--;
975f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
97668b8534bSLuigi Rizzo 
977ae10d1afSLuigi Rizzo 		if (netmap_verbose)
978*4bf50f18SLuigi Rizzo 			D("deleting last instance for %s", na->name);
97968b8534bSLuigi Rizzo 		/*
980f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
981f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
982f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
983f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
984ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
985ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
986ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
987ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
988ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
989ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
990ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
991ce3ee1e7SLuigi Rizzo 		 * syscall is running.
99268b8534bSLuigi Rizzo 		 */
993f9790aebSLuigi Rizzo 		na->nm_register(na, 0); /* off, clear flags */
99468b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
99568b8534bSLuigi Rizzo 		 * then return POLLERR
996ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
997ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
99868b8534bSLuigi Rizzo 		 */
9992f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
10002f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
10012f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
1002f9790aebSLuigi Rizzo 
1003f9790aebSLuigi Rizzo 		/* delete rings and buffers */
1004f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
1005f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
100668b8534bSLuigi Rizzo 	}
1007f9790aebSLuigi Rizzo 	/* delete the nifp */
1008ce3ee1e7SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
10095819da83SLuigi Rizzo }
101068b8534bSLuigi Rizzo 
101189cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1012f0ea3689SLuigi Rizzo static __inline int
1013f0ea3689SLuigi Rizzo nm_tx_si_user(struct netmap_priv_d *priv)
1014f0ea3689SLuigi Rizzo {
1015f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
1016f0ea3689SLuigi Rizzo 		(priv->np_txqlast - priv->np_txqfirst > 1));
1017f0ea3689SLuigi Rizzo }
1018f0ea3689SLuigi Rizzo 
101989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1020f0ea3689SLuigi Rizzo static __inline int
1021f0ea3689SLuigi Rizzo nm_rx_si_user(struct netmap_priv_d *priv)
1022f0ea3689SLuigi Rizzo {
1023f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
1024f0ea3689SLuigi Rizzo 		(priv->np_rxqlast - priv->np_rxqfirst > 1));
1025f0ea3689SLuigi Rizzo }
1026f0ea3689SLuigi Rizzo 
1027f18be576SLuigi Rizzo 
1028ce3ee1e7SLuigi Rizzo /*
102989cc2556SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd has
103089cc2556SLuigi Rizzo  * no active open() and mmap(). Also called in error paths.
103189cc2556SLuigi Rizzo  *
1032ce3ee1e7SLuigi Rizzo  * returns 1 if this is the last instance and we can free priv
1033ce3ee1e7SLuigi Rizzo  */
103489cc2556SLuigi Rizzo /* call with NMG_LOCK held */
1035f9790aebSLuigi Rizzo int
1036ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
1037ce3ee1e7SLuigi Rizzo {
1038f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1039ce3ee1e7SLuigi Rizzo 
1040ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
1041ce3ee1e7SLuigi Rizzo 	/*
1042ce3ee1e7SLuigi Rizzo 	 * np_refcount is the number of active mmaps on
1043ce3ee1e7SLuigi Rizzo 	 * this file descriptor
1044ce3ee1e7SLuigi Rizzo 	 */
1045ce3ee1e7SLuigi Rizzo 	if (--priv->np_refcount > 0) {
1046ce3ee1e7SLuigi Rizzo 		return 0;
1047ce3ee1e7SLuigi Rizzo 	}
1048ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
1049f9790aebSLuigi Rizzo 	if (!na) {
1050f9790aebSLuigi Rizzo 	    return 1; //XXX is it correct?
1051ce3ee1e7SLuigi Rizzo 	}
1052f9790aebSLuigi Rizzo 	netmap_do_unregif(priv, priv->np_nifp);
1053f9790aebSLuigi Rizzo 	priv->np_nifp = NULL;
1054ce3ee1e7SLuigi Rizzo 	netmap_drop_memory_locked(priv);
1055f9790aebSLuigi Rizzo 	if (priv->np_na) {
1056f0ea3689SLuigi Rizzo 		if (nm_tx_si_user(priv))
1057f0ea3689SLuigi Rizzo 			na->tx_si_users--;
1058f0ea3689SLuigi Rizzo 		if (nm_rx_si_user(priv))
1059f0ea3689SLuigi Rizzo 			na->rx_si_users--;
1060f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
1061f9790aebSLuigi Rizzo 		priv->np_na = NULL;
1062ce3ee1e7SLuigi Rizzo 	}
1063ce3ee1e7SLuigi Rizzo 	return 1;
1064f196ce38SLuigi Rizzo }
10655819da83SLuigi Rizzo 
1066f9790aebSLuigi Rizzo 
106789cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
1068f9790aebSLuigi Rizzo void
10695819da83SLuigi Rizzo netmap_dtor(void *data)
10705819da83SLuigi Rizzo {
10715819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
1072ce3ee1e7SLuigi Rizzo 	int last_instance;
10735819da83SLuigi Rizzo 
1074ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
1075ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
1076ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
1077ce3ee1e7SLuigi Rizzo 	if (last_instance) {
1078ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
107968b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
108068b8534bSLuigi Rizzo 	}
1081ce3ee1e7SLuigi Rizzo }
108268b8534bSLuigi Rizzo 
1083f18be576SLuigi Rizzo 
108468b8534bSLuigi Rizzo 
108568b8534bSLuigi Rizzo 
108668b8534bSLuigi Rizzo /*
108702ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
1088091fd0abSLuigi Rizzo  * Netmap has two operating modes:
1089091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
1090091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
1091091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
1092091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
1093091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
1094091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
1095091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
1096091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
1097091fd0abSLuigi Rizzo  *   to the other side.
1098091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
1099091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
1100091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
1101091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
1102091fd0abSLuigi Rizzo  */
1103091fd0abSLuigi Rizzo 
1104f18be576SLuigi Rizzo 
1105091fd0abSLuigi Rizzo /*
1106091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
110717885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
1108091fd0abSLuigi Rizzo  */
1109091fd0abSLuigi Rizzo static void
1110f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
1111091fd0abSLuigi Rizzo {
1112091fd0abSLuigi Rizzo 	struct mbuf *m;
1113091fd0abSLuigi Rizzo 
1114091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
1115f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
1116091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
1117091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
1118091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
1119091fd0abSLuigi Rizzo 	}
1120f9790aebSLuigi Rizzo 	mbq_destroy(q);
1121091fd0abSLuigi Rizzo }
1122091fd0abSLuigi Rizzo 
1123f18be576SLuigi Rizzo 
1124091fd0abSLuigi Rizzo /*
1125091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
112617885a7bSLuigi Rizzo  * Take packets from hwcur to ring->head marked NS_FORWARD (or forced)
112717885a7bSLuigi Rizzo  * and pass them up. Drop remaining packets in the unlikely event
112817885a7bSLuigi Rizzo  * of an mbuf shortage.
1129091fd0abSLuigi Rizzo  */
1130091fd0abSLuigi Rizzo static void
1131091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
1132091fd0abSLuigi Rizzo {
113317885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
113417885a7bSLuigi Rizzo 	u_int const head = kring->ring->head;
113517885a7bSLuigi Rizzo 	u_int n;
1136f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
1137091fd0abSLuigi Rizzo 
113817885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
113917885a7bSLuigi Rizzo 		struct mbuf *m;
1140091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
1141091fd0abSLuigi Rizzo 
1142091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
1143091fd0abSLuigi Rizzo 			continue;
1144*4bf50f18SLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE(na)) {
114517885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
1146091fd0abSLuigi Rizzo 			continue;
1147091fd0abSLuigi Rizzo 		}
1148091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
114917885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
1150*4bf50f18SLuigi Rizzo 		m = m_devget(NMB(na, slot), slot->len, 0, na->ifp, NULL);
1151091fd0abSLuigi Rizzo 
1152091fd0abSLuigi Rizzo 		if (m == NULL)
1153091fd0abSLuigi Rizzo 			break;
1154f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
1155091fd0abSLuigi Rizzo 	}
1156091fd0abSLuigi Rizzo }
1157091fd0abSLuigi Rizzo 
1158f18be576SLuigi Rizzo 
1159091fd0abSLuigi Rizzo /*
116017885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
116117885a7bSLuigi Rizzo  * kring->nr_hwcur and kring->rhead
116217885a7bSLuigi Rizzo  * Called under kring->rx_queue.lock on the sw rx ring,
1163091fd0abSLuigi Rizzo  */
116417885a7bSLuigi Rizzo static u_int
1165091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
1166091fd0abSLuigi Rizzo {
1167091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
116817885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
116917885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
117017885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
117117885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
117217885a7bSLuigi Rizzo 	u_int sent = 0;
1173ce3ee1e7SLuigi Rizzo 
117417885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
117517885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
117617885a7bSLuigi Rizzo 		struct netmap_kring *kdst = &na->tx_rings[i];
117717885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
117817885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
1179ce3ee1e7SLuigi Rizzo 
118017885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
118117885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
118217885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
1183091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
118417885a7bSLuigi Rizzo 			u_int dst_cur = rdst->cur;
118517885a7bSLuigi Rizzo 
118617885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
118717885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
118817885a7bSLuigi Rizzo 				continue;
118917885a7bSLuigi Rizzo 
119017885a7bSLuigi Rizzo 			sent++;
119117885a7bSLuigi Rizzo 
119217885a7bSLuigi Rizzo 			dst = &rdst->slot[dst_cur];
119317885a7bSLuigi Rizzo 
1194091fd0abSLuigi Rizzo 			tmp = *src;
119517885a7bSLuigi Rizzo 
1196091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
1197091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
1198091fd0abSLuigi Rizzo 
1199091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
1200091fd0abSLuigi Rizzo 			dst->len = tmp.len;
1201091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
1202091fd0abSLuigi Rizzo 
1203*4bf50f18SLuigi Rizzo 			rdst->cur = nm_next(dst_cur, dst_lim);
1204091fd0abSLuigi Rizzo 		}
120517885a7bSLuigi Rizzo 		/* if (sent) XXX txsync ? */
1206091fd0abSLuigi Rizzo 	}
120717885a7bSLuigi Rizzo 	return sent;
1208091fd0abSLuigi Rizzo }
1209091fd0abSLuigi Rizzo 
1210f18be576SLuigi Rizzo 
1211091fd0abSLuigi Rizzo /*
1212ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
121302ad4083SLuigi Rizzo  * system call in user process context, and the only contention
121402ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
1215091fd0abSLuigi Rizzo  * this routine concurrently.
121668b8534bSLuigi Rizzo  */
1217f9790aebSLuigi Rizzo void
1218ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
121968b8534bSLuigi Rizzo {
1220d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
122168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
122217885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1223f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1224f9790aebSLuigi Rizzo 	struct mbq q;
122568b8534bSLuigi Rizzo 
122617885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
122717885a7bSLuigi Rizzo 	 * force head = cur since netmap_grab_packets() stops at head
122868b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
122968b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
123068b8534bSLuigi Rizzo 	 */
1231f9790aebSLuigi Rizzo 	mbq_init(&q);
123217885a7bSLuigi Rizzo 	ring->cur = head;
123317885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
123417885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
123517885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
123617885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
123717885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
123817885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
123917885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
124068b8534bSLuigi Rizzo 
1241f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
1242f18be576SLuigi Rizzo }
1243f18be576SLuigi Rizzo 
1244f18be576SLuigi Rizzo 
124568b8534bSLuigi Rizzo /*
124602ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
124717885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
124817885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
124902ad4083SLuigi Rizzo  *
1250*4bf50f18SLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
1251*4bf50f18SLuigi Rizzo  * (we know because td != NULL).
1252*4bf50f18SLuigi Rizzo  *
1253*4bf50f18SLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
1254*4bf50f18SLuigi Rizzo  *     as an additional hidden argument.
125517885a7bSLuigi Rizzo  * returns the number of packets delivered to tx queues in
125617885a7bSLuigi Rizzo  * transparent mode, or a negative value if error
125768b8534bSLuigi Rizzo  */
125817885a7bSLuigi Rizzo int
1259ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
126068b8534bSLuigi Rizzo {
1261d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
126268b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
126317885a7bSLuigi Rizzo 	u_int nm_i, n;
126417885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1265f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
126617885a7bSLuigi Rizzo 	int ret = 0;
126717885a7bSLuigi Rizzo 	struct mbq *q = &kring->rx_queue;
126868b8534bSLuigi Rizzo 
126901c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
1270f0ea3689SLuigi Rizzo 	(void)td;
127117885a7bSLuigi Rizzo 
1272997b054cSLuigi Rizzo 	mbq_lock(q);
127317885a7bSLuigi Rizzo 
127417885a7bSLuigi Rizzo 	/* First part: import newly received packets */
127517885a7bSLuigi Rizzo 	n = mbq_len(q);
127617885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
127717885a7bSLuigi Rizzo 		struct mbuf *m;
127817885a7bSLuigi Rizzo 		uint32_t stop_i;
127917885a7bSLuigi Rizzo 
128017885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
128117885a7bSLuigi Rizzo 		stop_i = nm_prev(nm_i, lim);
128217885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
128317885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
128417885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
128517885a7bSLuigi Rizzo 
1286*4bf50f18SLuigi Rizzo 			m_copydata(m, 0, len, NMB(na, slot));
128717885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
128817885a7bSLuigi Rizzo 			if (netmap_verbose)
1289*4bf50f18SLuigi Rizzo                                 D("%s", nm_dump_buf(NMB(na, slot),len, 128, NULL));
129017885a7bSLuigi Rizzo 
129117885a7bSLuigi Rizzo 			slot->len = len;
129217885a7bSLuigi Rizzo 			slot->flags = kring->nkr_slot_flags;
129317885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
1294*4bf50f18SLuigi Rizzo 			m_freem(m);
129564ae02c3SLuigi Rizzo 		}
129617885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
129764ae02c3SLuigi Rizzo 	}
129817885a7bSLuigi Rizzo 
129917885a7bSLuigi Rizzo 	/*
130017885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
130117885a7bSLuigi Rizzo 	 */
130217885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
130317885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
130417885a7bSLuigi Rizzo 		if (netmap_fwd || kring->ring->flags & NR_FORWARD)
130517885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
130617885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
130764ae02c3SLuigi Rizzo 	}
130817885a7bSLuigi Rizzo 
130917885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
131017885a7bSLuigi Rizzo 
1311*4bf50f18SLuigi Rizzo 	/* access copies of cur,tail in the kring */
1312*4bf50f18SLuigi Rizzo 	if (kring->rcur == kring->rtail && td) /* no bufs available */
1313*4bf50f18SLuigi Rizzo 		selrecord(td, &kring->si);
1314*4bf50f18SLuigi Rizzo 
1315997b054cSLuigi Rizzo 	mbq_unlock(q);
131617885a7bSLuigi Rizzo 	return ret;
131768b8534bSLuigi Rizzo }
131868b8534bSLuigi Rizzo 
131968b8534bSLuigi Rizzo 
1320f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1321f9790aebSLuigi Rizzo  *
1322f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1323f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1324f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1325f9790aebSLuigi Rizzo  *
1326f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1327f9790aebSLuigi Rizzo  * EBUSY.
1328f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1329f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1330f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1331f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1332f9790aebSLuigi Rizzo  *
1333f9790aebSLuigi Rizzo  *					native_support
1334f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1335f9790aebSLuigi Rizzo  * -------------------------------------------------------
1336f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1337f9790aebSLuigi Rizzo  *
1338f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1339f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1340f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1341f9790aebSLuigi Rizzo  *
1342f9790aebSLuigi Rizzo  */
1343f9790aebSLuigi Rizzo 
1344f9790aebSLuigi Rizzo int
1345f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
1346f9790aebSLuigi Rizzo {
1347f9790aebSLuigi Rizzo 	/* generic support */
1348f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1349f9790aebSLuigi Rizzo 	int error = 0;
1350f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1351f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1352f9790aebSLuigi Rizzo 
1353f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1354f9790aebSLuigi Rizzo 
1355f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1356f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1357f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1358f9790aebSLuigi Rizzo 
1359f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
1360*4bf50f18SLuigi Rizzo 		prev_na = NA(ifp);
1361f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1362f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1363f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1364f9790aebSLuigi Rizzo 		 * adapters.
1365f9790aebSLuigi Rizzo 		 */
1366*4bf50f18SLuigi Rizzo 		if (NETMAP_OWNED_BY_ANY(prev_na)
1367*4bf50f18SLuigi Rizzo 			|| i != NETMAP_ADMODE_GENERIC
1368*4bf50f18SLuigi Rizzo 			|| prev_na->na_flags & NAF_FORCE_NATIVE
1369*4bf50f18SLuigi Rizzo #ifdef WITH_PIPES
1370*4bf50f18SLuigi Rizzo 			/* ugly, but we cannot allow an adapter switch
1371*4bf50f18SLuigi Rizzo 			 * if some pipe is referring to this one
1372*4bf50f18SLuigi Rizzo 			 */
1373*4bf50f18SLuigi Rizzo 			|| prev_na->na_next_pipe > 0
1374*4bf50f18SLuigi Rizzo #endif
1375*4bf50f18SLuigi Rizzo 		) {
1376*4bf50f18SLuigi Rizzo 			*na = prev_na;
1377f9790aebSLuigi Rizzo 			return 0;
1378f9790aebSLuigi Rizzo 		}
1379f9790aebSLuigi Rizzo 	}
1380f9790aebSLuigi Rizzo 
1381f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1382f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1383f9790aebSLuigi Rizzo 	 */
1384f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1385f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1386f9790aebSLuigi Rizzo 
1387f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1388f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1389f9790aebSLuigi Rizzo 	 *
1390f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1391f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1392f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1393f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1394f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1395f9790aebSLuigi Rizzo 	 * must be that
1396f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1397f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1398f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1399f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1400f9790aebSLuigi Rizzo 	 */
1401f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1402f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1403f9790aebSLuigi Rizzo 	if (error)
1404f9790aebSLuigi Rizzo 		return error;
1405f9790aebSLuigi Rizzo 
1406f9790aebSLuigi Rizzo 	*na = NA(ifp);
1407f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1408f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1409f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1410f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1411f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1412f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1413f9790aebSLuigi Rizzo 	}
141417885a7bSLuigi Rizzo 	ND("Created generic NA %p (prev %p)", gna, gna->prev);
1415f9790aebSLuigi Rizzo 
1416f9790aebSLuigi Rizzo 	return 0;
1417f9790aebSLuigi Rizzo }
1418f9790aebSLuigi Rizzo 
1419f9790aebSLuigi Rizzo 
142068b8534bSLuigi Rizzo /*
1421ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1422ce3ee1e7SLuigi Rizzo  *
1423f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
1424f2637526SLuigi Rizzo  * to the interface specified by nmr.
1425ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1426ce3ee1e7SLuigi Rizzo  *
1427f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1428f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1429f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1430f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1431f2637526SLuigi Rizzo  * could not be allocated.
1432f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1433f18be576SLuigi Rizzo  *
1434f2637526SLuigi Rizzo  * No reference is kept on the real interface, which may then
1435f2637526SLuigi Rizzo  * disappear at any time.
143668b8534bSLuigi Rizzo  */
1437f9790aebSLuigi Rizzo int
1438f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
143968b8534bSLuigi Rizzo {
1440f0ea3689SLuigi Rizzo 	struct ifnet *ifp = NULL;
1441f9790aebSLuigi Rizzo 	int error = 0;
1442f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1443f9790aebSLuigi Rizzo 
1444f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1445f196ce38SLuigi Rizzo 
1446ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1447ce3ee1e7SLuigi Rizzo 
1448*4bf50f18SLuigi Rizzo 	/* we cascade through all possibile types of netmap adapter.
1449*4bf50f18SLuigi Rizzo 	 * All netmap_get_*_na() functions return an error and an na,
1450*4bf50f18SLuigi Rizzo 	 * with the following combinations:
1451*4bf50f18SLuigi Rizzo 	 *
1452*4bf50f18SLuigi Rizzo 	 * error    na
1453*4bf50f18SLuigi Rizzo 	 *   0	   NULL		type doesn't match
1454*4bf50f18SLuigi Rizzo 	 *  !0	   NULL		type matches, but na creation/lookup failed
1455*4bf50f18SLuigi Rizzo 	 *   0	  !NULL		type matches and na created/found
1456*4bf50f18SLuigi Rizzo 	 *  !0    !NULL		impossible
1457*4bf50f18SLuigi Rizzo 	 */
1458*4bf50f18SLuigi Rizzo 
1459*4bf50f18SLuigi Rizzo 	/* try to see if this is a monitor port */
1460*4bf50f18SLuigi Rizzo 	error = netmap_get_monitor_na(nmr, na, create);
1461*4bf50f18SLuigi Rizzo 	if (error || *na != NULL)
1462*4bf50f18SLuigi Rizzo 		return error;
1463*4bf50f18SLuigi Rizzo 
1464*4bf50f18SLuigi Rizzo 	/* try to see if this is a pipe port */
1465f0ea3689SLuigi Rizzo 	error = netmap_get_pipe_na(nmr, na, create);
1466f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1467f9790aebSLuigi Rizzo 		return error;
1468ce3ee1e7SLuigi Rizzo 
1469*4bf50f18SLuigi Rizzo 	/* try to see if this is a bridge port */
1470f0ea3689SLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1471f0ea3689SLuigi Rizzo 	if (error)
1472f0ea3689SLuigi Rizzo 		return error;
1473f0ea3689SLuigi Rizzo 
1474f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1475f0ea3689SLuigi Rizzo 		goto pipes;
1476f0ea3689SLuigi Rizzo 
147789cc2556SLuigi Rizzo 	/*
147889cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
147989cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
148089cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
148189cc2556SLuigi Rizzo 	 * persistent VALE port.
148289cc2556SLuigi Rizzo 	 */
1483f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1484f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1485ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1486f196ce38SLuigi Rizzo 	}
1487ce3ee1e7SLuigi Rizzo 
1488f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1489f9790aebSLuigi Rizzo 	if (error)
1490f9790aebSLuigi Rizzo 		goto out;
1491f18be576SLuigi Rizzo 
1492f9790aebSLuigi Rizzo 	*na = ret;
1493f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1494f0ea3689SLuigi Rizzo 
1495f0ea3689SLuigi Rizzo pipes:
149689cc2556SLuigi Rizzo 	/*
149789cc2556SLuigi Rizzo 	 * If we are opening a pipe whose parent was not in netmap mode,
149889cc2556SLuigi Rizzo 	 * we have to allocate the pipe array now.
149989cc2556SLuigi Rizzo 	 * XXX get rid of this clumsiness (2014-03-15)
150089cc2556SLuigi Rizzo 	 */
1501f0ea3689SLuigi Rizzo 	error = netmap_pipe_alloc(*na, nmr);
1502f0ea3689SLuigi Rizzo 
1503f9790aebSLuigi Rizzo out:
1504f0ea3689SLuigi Rizzo 	if (error && ret != NULL)
1505f0ea3689SLuigi Rizzo 		netmap_adapter_put(ret);
1506f0ea3689SLuigi Rizzo 
1507f0ea3689SLuigi Rizzo 	if (ifp)
150889cc2556SLuigi Rizzo 		if_rele(ifp); /* allow live unloading of drivers modules */
1509f18be576SLuigi Rizzo 
15105ab0d24dSLuigi Rizzo 	return error;
15115ab0d24dSLuigi Rizzo }
1512ce3ee1e7SLuigi Rizzo 
1513ce3ee1e7SLuigi Rizzo 
1514f9790aebSLuigi Rizzo /*
1515f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1516f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
151717885a7bSLuigi Rizzo  * in case of error.
1518f9790aebSLuigi Rizzo  *
151917885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
152017885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1521f9790aebSLuigi Rizzo  *
152217885a7bSLuigi Rizzo  * We want
152317885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1524f9790aebSLuigi Rizzo  *
152517885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1526f9790aebSLuigi Rizzo  */
1527f9790aebSLuigi Rizzo u_int
152817885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring)
1529f9790aebSLuigi Rizzo {
1530f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
153117885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1532f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1533f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1534ce3ee1e7SLuigi Rizzo 
153517885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
153617885a7bSLuigi Rizzo 		kring->name,
153717885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
153817885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
153917885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
154017885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->rhead >= n ||
154117885a7bSLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n)
1542f9790aebSLuigi Rizzo 		goto error;
1543f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
154417885a7bSLuigi Rizzo 	/*
154517885a7bSLuigi Rizzo 	 * user sanity checks. We only use 'cur',
154617885a7bSLuigi Rizzo 	 * A, B, ... are possible positions for cur:
154717885a7bSLuigi Rizzo 	 *
154817885a7bSLuigi Rizzo 	 *  0    A  cur   B  tail  C  n-1
154917885a7bSLuigi Rizzo 	 *  0    D  tail  E  cur   F  n-1
155017885a7bSLuigi Rizzo 	 *
155117885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
155217885a7bSLuigi Rizzo 	 */
155317885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
155417885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
155517885a7bSLuigi Rizzo 		if (head < kring->rhead || head > kring->rtail)
1556f9790aebSLuigi Rizzo 			goto error;
155717885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
155817885a7bSLuigi Rizzo 		if (cur < head || cur > kring->rtail)
1559f9790aebSLuigi Rizzo 			goto error;
156017885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
156117885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
156217885a7bSLuigi Rizzo 		if (head > kring->rtail && head < kring->rhead)
156317885a7bSLuigi Rizzo 			goto error;
156417885a7bSLuigi Rizzo 
156517885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
156617885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
156717885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
156817885a7bSLuigi Rizzo 			if (cur < head || cur > kring->rtail)
156917885a7bSLuigi Rizzo 				goto error;
157017885a7bSLuigi Rizzo 		} else { /* head >= rhead */
157117885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
157217885a7bSLuigi Rizzo 			if (cur > kring->rtail && cur < head)
157317885a7bSLuigi Rizzo 				goto error;
1574f18be576SLuigi Rizzo 		}
1575f9790aebSLuigi Rizzo 	}
157617885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
157717885a7bSLuigi Rizzo 		RD(5, "tail overwritten was %d need %d",
157817885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
157917885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
158017885a7bSLuigi Rizzo 	}
158117885a7bSLuigi Rizzo 	kring->rhead = head;
158217885a7bSLuigi Rizzo 	kring->rcur = cur;
158317885a7bSLuigi Rizzo 	return head;
1584f9790aebSLuigi Rizzo 
1585f9790aebSLuigi Rizzo error:
158617885a7bSLuigi Rizzo 	RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d",
158717885a7bSLuigi Rizzo 		kring->name,
1588f9790aebSLuigi Rizzo 		kring->nr_hwcur,
158917885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
159017885a7bSLuigi Rizzo 		cur, ring->tail);
1591f9790aebSLuigi Rizzo 	return n;
159268b8534bSLuigi Rizzo }
159368b8534bSLuigi Rizzo 
159468b8534bSLuigi Rizzo 
159568b8534bSLuigi Rizzo /*
1596f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
159717885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1598f9790aebSLuigi Rizzo  *
159917885a7bSLuigi Rizzo  * For a valid configuration,
160017885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1601f9790aebSLuigi Rizzo  *
160217885a7bSLuigi Rizzo  * We only consider head and cur.
160317885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1604f9790aebSLuigi Rizzo  *
1605f9790aebSLuigi Rizzo  */
1606f9790aebSLuigi Rizzo u_int
160717885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring)
1608f9790aebSLuigi Rizzo {
1609f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
161017885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
161117885a7bSLuigi Rizzo 	uint32_t head, cur;
1612f9790aebSLuigi Rizzo 
161317885a7bSLuigi Rizzo 	ND("%s kc %d kt %d h %d c %d t %d",
161417885a7bSLuigi Rizzo 		kring->name,
161517885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
161617885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
161717885a7bSLuigi Rizzo 	/*
161817885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
161917885a7bSLuigi Rizzo 	 * move backwards. However:
162017885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
162117885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
162217885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
162317885a7bSLuigi Rizzo 	 */
162417885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
162517885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1626f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
162717885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwtail >= n)
1628f9790aebSLuigi Rizzo 		goto error;
1629f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1630f9790aebSLuigi Rizzo 	/* user sanity checks */
163117885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
163217885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
163317885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur || head > kring->nr_hwtail)
1634f9790aebSLuigi Rizzo 			goto error;
163517885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
163617885a7bSLuigi Rizzo 		if (cur < head || cur > kring->nr_hwtail)
1637f9790aebSLuigi Rizzo 			goto error;
1638f9790aebSLuigi Rizzo 	} else {
163917885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
164017885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur && head > kring->nr_hwtail)
1641f9790aebSLuigi Rizzo 			goto error;
164217885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
164317885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
164417885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
164517885a7bSLuigi Rizzo 			if (cur < head || cur > kring->nr_hwtail)
1646f9790aebSLuigi Rizzo 				goto error;
164717885a7bSLuigi Rizzo 		} else {
164817885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
164917885a7bSLuigi Rizzo 			if (cur < head && cur > kring->nr_hwtail)
1650f9790aebSLuigi Rizzo 				goto error;
1651f9790aebSLuigi Rizzo 		}
1652f9790aebSLuigi Rizzo 	}
165317885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
165417885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
165517885a7bSLuigi Rizzo 			kring->name,
165617885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
165717885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
165817885a7bSLuigi Rizzo 	}
165917885a7bSLuigi Rizzo 	return head;
1660f9790aebSLuigi Rizzo 
1661f9790aebSLuigi Rizzo error:
166217885a7bSLuigi Rizzo 	RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d",
1663f9790aebSLuigi Rizzo 		kring->nr_hwcur,
166417885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
166517885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, ring->tail);
1666f9790aebSLuigi Rizzo 	return n;
1667f9790aebSLuigi Rizzo }
1668f9790aebSLuigi Rizzo 
166917885a7bSLuigi Rizzo 
1670f9790aebSLuigi Rizzo /*
167168b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
167217885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
167368b8534bSLuigi Rizzo  * Return 1 on reinit.
1674506cc70cSLuigi Rizzo  *
1675506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1676506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
167717885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1678506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1679506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1680506cc70cSLuigi Rizzo  * it under lock.
168168b8534bSLuigi Rizzo  */
168268b8534bSLuigi Rizzo int
168368b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
168468b8534bSLuigi Rizzo {
168568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
168668b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
168768b8534bSLuigi Rizzo 	int errors = 0;
168868b8534bSLuigi Rizzo 
1689ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
1690*4bf50f18SLuigi Rizzo 	RD(10, "called for %s", kring->name);
169117885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
169217885a7bSLuigi Rizzo 	kring->rhead = ring->head;
169317885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
169417885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
169517885a7bSLuigi Rizzo 
169668b8534bSLuigi Rizzo 	if (ring->cur > lim)
169768b8534bSLuigi Rizzo 		errors++;
169817885a7bSLuigi Rizzo 	if (ring->head > lim)
169917885a7bSLuigi Rizzo 		errors++;
170017885a7bSLuigi Rizzo 	if (ring->tail > lim)
170117885a7bSLuigi Rizzo 		errors++;
170268b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
170368b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
170468b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
170568b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
170617885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
170768b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
170868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
1709*4bf50f18SLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE(kring->na)) {
171068b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
171117885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
171268b8534bSLuigi Rizzo 		}
171368b8534bSLuigi Rizzo 	}
171468b8534bSLuigi Rizzo 	if (errors) {
17158241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
171617885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
171717885a7bSLuigi Rizzo 			kring->name,
171868b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
171917885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
172017885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
172117885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
172217885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
172368b8534bSLuigi Rizzo 	}
172468b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
172568b8534bSLuigi Rizzo }
172668b8534bSLuigi Rizzo 
1727*4bf50f18SLuigi Rizzo /* interpret the ringid and flags fields of an nmreq, by translating them
1728*4bf50f18SLuigi Rizzo  * into a pair of intervals of ring indices:
1729*4bf50f18SLuigi Rizzo  *
1730*4bf50f18SLuigi Rizzo  * [priv->np_txqfirst, priv->np_txqlast) and
1731*4bf50f18SLuigi Rizzo  * [priv->np_rxqfirst, priv->np_rxqlast)
1732*4bf50f18SLuigi Rizzo  *
173368b8534bSLuigi Rizzo  */
1734*4bf50f18SLuigi Rizzo int
1735*4bf50f18SLuigi Rizzo netmap_interp_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
173668b8534bSLuigi Rizzo {
1737f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1738f0ea3689SLuigi Rizzo 	u_int j, i = ringid & NETMAP_RING_MASK;
1739f0ea3689SLuigi Rizzo 	u_int reg = flags & NR_REG_MASK;
174068b8534bSLuigi Rizzo 
1741f0ea3689SLuigi Rizzo 	if (reg == NR_REG_DEFAULT) {
1742f0ea3689SLuigi Rizzo 		/* convert from old ringid to flags */
174368b8534bSLuigi Rizzo 		if (ringid & NETMAP_SW_RING) {
1744f0ea3689SLuigi Rizzo 			reg = NR_REG_SW;
174568b8534bSLuigi Rizzo 		} else if (ringid & NETMAP_HW_RING) {
1746f0ea3689SLuigi Rizzo 			reg = NR_REG_ONE_NIC;
174768b8534bSLuigi Rizzo 		} else {
1748f0ea3689SLuigi Rizzo 			reg = NR_REG_ALL_NIC;
1749f0ea3689SLuigi Rizzo 		}
1750f0ea3689SLuigi Rizzo 		D("deprecated API, old ringid 0x%x -> ringid %x reg %d", ringid, i, reg);
1751f0ea3689SLuigi Rizzo 	}
1752f0ea3689SLuigi Rizzo 	switch (reg) {
1753f0ea3689SLuigi Rizzo 	case NR_REG_ALL_NIC:
1754f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_MASTER:
1755f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_SLAVE:
1756f0ea3689SLuigi Rizzo 		priv->np_txqfirst = 0;
1757f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings;
1758f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = 0;
1759f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings;
1760f0ea3689SLuigi Rizzo 		ND("%s %d %d", "ALL/PIPE",
1761f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1762f0ea3689SLuigi Rizzo 		break;
1763f0ea3689SLuigi Rizzo 	case NR_REG_SW:
1764f0ea3689SLuigi Rizzo 	case NR_REG_NIC_SW:
1765f0ea3689SLuigi Rizzo 		if (!(na->na_flags & NAF_HOST_RINGS)) {
1766f0ea3689SLuigi Rizzo 			D("host rings not supported");
1767f0ea3689SLuigi Rizzo 			return EINVAL;
1768f0ea3689SLuigi Rizzo 		}
1769f0ea3689SLuigi Rizzo 		priv->np_txqfirst = (reg == NR_REG_SW ?
1770f0ea3689SLuigi Rizzo 			na->num_tx_rings : 0);
1771f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings + 1;
1772f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = (reg == NR_REG_SW ?
1773f0ea3689SLuigi Rizzo 			na->num_rx_rings : 0);
1774f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings + 1;
1775f0ea3689SLuigi Rizzo 		ND("%s %d %d", reg == NR_REG_SW ? "SW" : "NIC+SW",
1776f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1777f0ea3689SLuigi Rizzo 		break;
1778f0ea3689SLuigi Rizzo 	case NR_REG_ONE_NIC:
1779f0ea3689SLuigi Rizzo 		if (i >= na->num_tx_rings && i >= na->num_rx_rings) {
1780f0ea3689SLuigi Rizzo 			D("invalid ring id %d", i);
1781f0ea3689SLuigi Rizzo 			return EINVAL;
1782f0ea3689SLuigi Rizzo 		}
1783f0ea3689SLuigi Rizzo 		/* if not enough rings, use the first one */
1784f0ea3689SLuigi Rizzo 		j = i;
1785f0ea3689SLuigi Rizzo 		if (j >= na->num_tx_rings)
1786f0ea3689SLuigi Rizzo 			j = 0;
1787f0ea3689SLuigi Rizzo 		priv->np_txqfirst = j;
1788f0ea3689SLuigi Rizzo 		priv->np_txqlast = j + 1;
1789f0ea3689SLuigi Rizzo 		j = i;
1790f0ea3689SLuigi Rizzo 		if (j >= na->num_rx_rings)
1791f0ea3689SLuigi Rizzo 			j = 0;
1792f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = j;
1793f0ea3689SLuigi Rizzo 		priv->np_rxqlast = j + 1;
1794f0ea3689SLuigi Rizzo 		break;
1795f0ea3689SLuigi Rizzo 	default:
1796f0ea3689SLuigi Rizzo 		D("invalid regif type %d", reg);
1797f0ea3689SLuigi Rizzo 		return EINVAL;
179868b8534bSLuigi Rizzo 	}
1799f0ea3689SLuigi Rizzo 	priv->np_flags = (flags & ~NR_REG_MASK) | reg;
1800*4bf50f18SLuigi Rizzo 
1801ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1802f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
1803*4bf50f18SLuigi Rizzo 			na->name,
1804f0ea3689SLuigi Rizzo 			priv->np_txqfirst,
1805f0ea3689SLuigi Rizzo 			priv->np_txqlast,
1806f0ea3689SLuigi Rizzo 			priv->np_rxqfirst,
1807f0ea3689SLuigi Rizzo 			priv->np_rxqlast,
1808f0ea3689SLuigi Rizzo 			i);
1809ae10d1afSLuigi Rizzo 	}
181068b8534bSLuigi Rizzo 	return 0;
181168b8534bSLuigi Rizzo }
181268b8534bSLuigi Rizzo 
1813*4bf50f18SLuigi Rizzo 
1814*4bf50f18SLuigi Rizzo /*
1815*4bf50f18SLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
1816*4bf50f18SLuigi Rizzo  * for all rings is the same as a single ring.
1817*4bf50f18SLuigi Rizzo  */
1818*4bf50f18SLuigi Rizzo static int
1819*4bf50f18SLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
1820*4bf50f18SLuigi Rizzo {
1821*4bf50f18SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1822*4bf50f18SLuigi Rizzo 	int error;
1823*4bf50f18SLuigi Rizzo 
1824*4bf50f18SLuigi Rizzo 	error = netmap_interp_ringid(priv, ringid, flags);
1825*4bf50f18SLuigi Rizzo 	if (error) {
1826*4bf50f18SLuigi Rizzo 		return error;
1827*4bf50f18SLuigi Rizzo 	}
1828*4bf50f18SLuigi Rizzo 
1829*4bf50f18SLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
1830*4bf50f18SLuigi Rizzo 
1831*4bf50f18SLuigi Rizzo 	/* optimization: count the users registered for more than
1832*4bf50f18SLuigi Rizzo 	 * one ring, which are the ones sleeping on the global queue.
1833*4bf50f18SLuigi Rizzo 	 * The default netmap_notify() callback will then
1834*4bf50f18SLuigi Rizzo 	 * avoid signaling the global queue if nobody is using it
1835*4bf50f18SLuigi Rizzo 	 */
1836*4bf50f18SLuigi Rizzo 	if (nm_tx_si_user(priv))
1837*4bf50f18SLuigi Rizzo 		na->tx_si_users++;
1838*4bf50f18SLuigi Rizzo 	if (nm_rx_si_user(priv))
1839*4bf50f18SLuigi Rizzo 		na->rx_si_users++;
1840*4bf50f18SLuigi Rizzo 	return 0;
1841*4bf50f18SLuigi Rizzo }
1842*4bf50f18SLuigi Rizzo 
1843f18be576SLuigi Rizzo /*
1844f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1845f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1846ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
1847*4bf50f18SLuigi Rizzo  *
1848*4bf50f18SLuigi Rizzo  * The following na callbacks are called in the process:
1849*4bf50f18SLuigi Rizzo  *
1850*4bf50f18SLuigi Rizzo  * na->nm_config()			[by netmap_update_config]
1851*4bf50f18SLuigi Rizzo  * (get current number and size of rings)
1852*4bf50f18SLuigi Rizzo  *
1853*4bf50f18SLuigi Rizzo  *  	We have a generic one for linux (netmap_linux_config).
1854*4bf50f18SLuigi Rizzo  *  	The bwrap has to override this, since it has to forward
1855*4bf50f18SLuigi Rizzo  *  	the request to the wrapped adapter (netmap_bwrap_config).
1856*4bf50f18SLuigi Rizzo  *
1857*4bf50f18SLuigi Rizzo  *    	XXX netmap_if_new calls this again (2014-03-15)
1858*4bf50f18SLuigi Rizzo  *
1859*4bf50f18SLuigi Rizzo  * na->nm_krings_create()		[by netmap_if_new]
1860*4bf50f18SLuigi Rizzo  * (create and init the krings array)
1861*4bf50f18SLuigi Rizzo  *
1862*4bf50f18SLuigi Rizzo  * 	One of the following:
1863*4bf50f18SLuigi Rizzo  *
1864*4bf50f18SLuigi Rizzo  *	* netmap_hw_krings_create, 			(hw ports)
1865*4bf50f18SLuigi Rizzo  *		creates the standard layout for the krings
1866*4bf50f18SLuigi Rizzo  * 		and adds the mbq (used for the host rings).
1867*4bf50f18SLuigi Rizzo  *
1868*4bf50f18SLuigi Rizzo  * 	* netmap_vp_krings_create			(VALE ports)
1869*4bf50f18SLuigi Rizzo  * 		add leases and scratchpads
1870*4bf50f18SLuigi Rizzo  *
1871*4bf50f18SLuigi Rizzo  * 	* netmap_pipe_krings_create			(pipes)
1872*4bf50f18SLuigi Rizzo  * 		create the krings and rings of both ends and
1873*4bf50f18SLuigi Rizzo  * 		cross-link them
1874*4bf50f18SLuigi Rizzo  *
1875*4bf50f18SLuigi Rizzo  *      * netmap_monitor_krings_create 			(monitors)
1876*4bf50f18SLuigi Rizzo  *      	avoid allocating the mbq
1877*4bf50f18SLuigi Rizzo  *
1878*4bf50f18SLuigi Rizzo  *      * netmap_bwrap_krings_create			(bwraps)
1879*4bf50f18SLuigi Rizzo  *      	create both the brap krings array,
1880*4bf50f18SLuigi Rizzo  *      	the krings array of the wrapped adapter, and
1881*4bf50f18SLuigi Rizzo  *      	(if needed) the fake array for the host adapter
1882*4bf50f18SLuigi Rizzo  *
1883*4bf50f18SLuigi Rizzo  * na->nm_register(, 1)
1884*4bf50f18SLuigi Rizzo  * (put the adapter in netmap mode)
1885*4bf50f18SLuigi Rizzo  *
1886*4bf50f18SLuigi Rizzo  * 	This may be one of the following:
1887*4bf50f18SLuigi Rizzo  * 	(XXX these should be either all *_register or all *_reg 2014-03-15)
1888*4bf50f18SLuigi Rizzo  *
1889*4bf50f18SLuigi Rizzo  * 	* netmap_hw_register				(hw ports)
1890*4bf50f18SLuigi Rizzo  * 		checks that the ifp is still there, then calls
1891*4bf50f18SLuigi Rizzo  * 		the hardware specific callback;
1892*4bf50f18SLuigi Rizzo  *
1893*4bf50f18SLuigi Rizzo  * 	* netmap_vp_reg					(VALE ports)
1894*4bf50f18SLuigi Rizzo  *		If the port is connected to a bridge,
1895*4bf50f18SLuigi Rizzo  *		set the NAF_NETMAP_ON flag under the
1896*4bf50f18SLuigi Rizzo  *		bridge write lock.
1897*4bf50f18SLuigi Rizzo  *
1898*4bf50f18SLuigi Rizzo  *	* netmap_pipe_reg				(pipes)
1899*4bf50f18SLuigi Rizzo  *		inform the other pipe end that it is no
1900*4bf50f18SLuigi Rizzo  *		longer responsibile for the lifetime of this
1901*4bf50f18SLuigi Rizzo  *		pipe end
1902*4bf50f18SLuigi Rizzo  *
1903*4bf50f18SLuigi Rizzo  *	* netmap_monitor_reg				(monitors)
1904*4bf50f18SLuigi Rizzo  *		intercept the sync callbacks of the monitored
1905*4bf50f18SLuigi Rizzo  *		rings
1906*4bf50f18SLuigi Rizzo  *
1907*4bf50f18SLuigi Rizzo  *	* netmap_bwrap_register				(bwraps)
1908*4bf50f18SLuigi Rizzo  *		cross-link the bwrap and hwna rings,
1909*4bf50f18SLuigi Rizzo  *		forward the request to the hwna, override
1910*4bf50f18SLuigi Rizzo  *		the hwna notify callback (to get the frames
1911*4bf50f18SLuigi Rizzo  *		coming from outside go through the bridge).
1912*4bf50f18SLuigi Rizzo  *
1913*4bf50f18SLuigi Rizzo  * XXX maybe netmap_if_new() should be merged with this (2014-03-15).
1914*4bf50f18SLuigi Rizzo  *
1915f18be576SLuigi Rizzo  */
1916f9790aebSLuigi Rizzo struct netmap_if *
1917f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1918f0ea3689SLuigi Rizzo 	uint16_t ringid, uint32_t flags, int *err)
1919f18be576SLuigi Rizzo {
1920f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1921f9790aebSLuigi Rizzo 	int error, need_mem = 0;
1922f18be576SLuigi Rizzo 
1923ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1924f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1925f18be576SLuigi Rizzo 	netmap_update_config(na);
1926f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1927f0ea3689SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid, flags);
1928f18be576SLuigi Rizzo 	if (error)
1929f18be576SLuigi Rizzo 		goto out;
1930ce3ee1e7SLuigi Rizzo 	/* ensure allocators are ready */
1931ce3ee1e7SLuigi Rizzo 	need_mem = !netmap_have_memory_locked(priv);
1932ce3ee1e7SLuigi Rizzo 	if (need_mem) {
1933ce3ee1e7SLuigi Rizzo 		error = netmap_get_memory_locked(priv);
1934ce3ee1e7SLuigi Rizzo 		ND("get_memory returned %d", error);
1935ce3ee1e7SLuigi Rizzo 		if (error)
1936ce3ee1e7SLuigi Rizzo 			goto out;
1937ce3ee1e7SLuigi Rizzo 	}
193889cc2556SLuigi Rizzo 	/* Allocate a netmap_if and, if necessary, all the netmap_ring's */
1939*4bf50f18SLuigi Rizzo 	nifp = netmap_if_new(na);
1940f18be576SLuigi Rizzo 	if (nifp == NULL) { /* allocation failed */
1941f18be576SLuigi Rizzo 		error = ENOMEM;
1942ce3ee1e7SLuigi Rizzo 		goto out;
1943ce3ee1e7SLuigi Rizzo 	}
1944f9790aebSLuigi Rizzo 	na->active_fds++;
1945*4bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
1946*4bf50f18SLuigi Rizzo 		/* Netmap not active, set the card in netmap mode
1947f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1948ce3ee1e7SLuigi Rizzo 		 */
194989cc2556SLuigi Rizzo 		/* cache the allocator info in the na */
1950*4bf50f18SLuigi Rizzo 		na->na_lut = netmap_mem_get_lut(na->nm_mem);
1951f9790aebSLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut);
1952*4bf50f18SLuigi Rizzo 		na->na_lut_objtotal = netmap_mem_get_buftotal(na->nm_mem);
1953*4bf50f18SLuigi Rizzo 		na->na_lut_objsize = netmap_mem_get_bufsize(na->nm_mem);
1954f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1955f18be576SLuigi Rizzo 		if (error) {
1956ce3ee1e7SLuigi Rizzo 			netmap_do_unregif(priv, nifp);
1957f18be576SLuigi Rizzo 			nifp = NULL;
1958f18be576SLuigi Rizzo 		}
1959f18be576SLuigi Rizzo 	}
1960f18be576SLuigi Rizzo out:
1961f18be576SLuigi Rizzo 	*err = error;
1962f9790aebSLuigi Rizzo 	if (error) {
196389cc2556SLuigi Rizzo 		/* we should drop the allocator, but only
196489cc2556SLuigi Rizzo 		 * if we were the ones who grabbed it
196589cc2556SLuigi Rizzo 		 */
1966f9790aebSLuigi Rizzo 		if (need_mem)
1967f9790aebSLuigi Rizzo 			netmap_drop_memory_locked(priv);
1968*4bf50f18SLuigi Rizzo 		priv->np_na = NULL;
1969f9790aebSLuigi Rizzo 	}
1970ce3ee1e7SLuigi Rizzo 	if (nifp != NULL) {
1971ce3ee1e7SLuigi Rizzo 		/*
1972ce3ee1e7SLuigi Rizzo 		 * advertise that the interface is ready bt setting ni_nifp.
1973ce3ee1e7SLuigi Rizzo 		 * The barrier is needed because readers (poll and *SYNC)
1974ce3ee1e7SLuigi Rizzo 		 * check for priv->np_nifp != NULL without locking
1975ce3ee1e7SLuigi Rizzo 		 */
1976ce3ee1e7SLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1977ce3ee1e7SLuigi Rizzo 		priv->np_nifp = nifp;
1978ce3ee1e7SLuigi Rizzo 	}
1979f18be576SLuigi Rizzo 	return nifp;
1980f18be576SLuigi Rizzo }
1981f18be576SLuigi Rizzo 
1982f18be576SLuigi Rizzo 
1983f18be576SLuigi Rizzo 
198468b8534bSLuigi Rizzo /*
198568b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
198668b8534bSLuigi Rizzo  *
198768b8534bSLuigi Rizzo  * Following a list of accepted commands:
198868b8534bSLuigi Rizzo  * - NIOCGINFO
198968b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
199068b8534bSLuigi Rizzo  * - NIOCREGIF
199168b8534bSLuigi Rizzo  * - NIOCTXSYNC
199268b8534bSLuigi Rizzo  * - NIOCRXSYNC
199368b8534bSLuigi Rizzo  *
199468b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
199568b8534bSLuigi Rizzo  */
1996f9790aebSLuigi Rizzo int
19970b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
19980b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
199968b8534bSLuigi Rizzo {
200068b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
200168b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
2002ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
200368b8534bSLuigi Rizzo 	int error;
2004f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
200568b8534bSLuigi Rizzo 	struct netmap_if *nifp;
2006ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
200768b8534bSLuigi Rizzo 
20080b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
20090b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
2010f196ce38SLuigi Rizzo 
201117885a7bSLuigi Rizzo 	if (cmd == NIOCGINFO || cmd == NIOCREGIF) {
201217885a7bSLuigi Rizzo 		/* truncate name */
201317885a7bSLuigi Rizzo 		nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';
201417885a7bSLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
201517885a7bSLuigi Rizzo 			D("API mismatch for %s got %d need %d",
201617885a7bSLuigi Rizzo 				nmr->nr_name,
201717885a7bSLuigi Rizzo 				nmr->nr_version, NETMAP_API);
201817885a7bSLuigi Rizzo 			nmr->nr_version = NETMAP_API;
2019f0ea3689SLuigi Rizzo 		}
2020f0ea3689SLuigi Rizzo 		if (nmr->nr_version < NETMAP_MIN_API ||
2021f0ea3689SLuigi Rizzo 		    nmr->nr_version > NETMAP_MAX_API) {
202217885a7bSLuigi Rizzo 			return EINVAL;
202317885a7bSLuigi Rizzo 		}
202417885a7bSLuigi Rizzo 	}
2025506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
2026506cc70cSLuigi Rizzo 
202768b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
20288241616dSLuigi Rizzo 	if (error) {
2029506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
20308241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
20318241616dSLuigi Rizzo 		 * is now created in the open */
20328241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
2033506cc70cSLuigi Rizzo 	}
203468b8534bSLuigi Rizzo 
203568b8534bSLuigi Rizzo 	switch (cmd) {
203668b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
2037f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
2038f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
2039f18be576SLuigi Rizzo 			break;
2040f18be576SLuigi Rizzo 		}
2041ce3ee1e7SLuigi Rizzo 
2042ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2043ce3ee1e7SLuigi Rizzo 		do {
2044ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
2045ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
2046ce3ee1e7SLuigi Rizzo 			u_int memflags;
2047ce3ee1e7SLuigi Rizzo 
2048ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
2049ce3ee1e7SLuigi Rizzo 				/* get a refcount */
2050f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
20518241616dSLuigi Rizzo 				if (error)
20528241616dSLuigi Rizzo 					break;
2053f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
2054ce3ee1e7SLuigi Rizzo 			}
2055ce3ee1e7SLuigi Rizzo 
2056f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags,
2057f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
2058ce3ee1e7SLuigi Rizzo 			if (error)
2059ce3ee1e7SLuigi Rizzo 				break;
2060ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
2061ce3ee1e7SLuigi Rizzo 				break;
20628241616dSLuigi Rizzo 			nmr->nr_offset = 0;
20638241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
2064ae10d1afSLuigi Rizzo 			netmap_update_config(na);
2065d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
2066d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
206764ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
206864ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
2069f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
2070ce3ee1e7SLuigi Rizzo 		} while (0);
2071ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
207268b8534bSLuigi Rizzo 		break;
207368b8534bSLuigi Rizzo 
207468b8534bSLuigi Rizzo 	case NIOCREGIF:
2075f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
2076f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
2077f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
2078*4bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_VNET_HDR
2079*4bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_NEWIF
2080*4bf50f18SLuigi Rizzo 				|| i == NETMAP_BDG_DELIF) {
2081f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
2082f18be576SLuigi Rizzo 			break;
2083f18be576SLuigi Rizzo 		} else if (i != 0) {
2084f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
2085f18be576SLuigi Rizzo 			error = EINVAL;
2086f18be576SLuigi Rizzo 			break;
2087f18be576SLuigi Rizzo 		}
2088f18be576SLuigi Rizzo 
20898241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
2090ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2091ce3ee1e7SLuigi Rizzo 		do {
2092ce3ee1e7SLuigi Rizzo 			u_int memflags;
2093ce3ee1e7SLuigi Rizzo 
2094f9790aebSLuigi Rizzo 			if (priv->np_na != NULL) {	/* thread already registered */
2095f0ea3689SLuigi Rizzo 				error = EBUSY;
2096506cc70cSLuigi Rizzo 				break;
2097506cc70cSLuigi Rizzo 			}
209868b8534bSLuigi Rizzo 			/* find the interface and a reference */
2099f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
210068b8534bSLuigi Rizzo 			if (error)
2101ce3ee1e7SLuigi Rizzo 				break;
2102f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
2103f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2104ce3ee1e7SLuigi Rizzo 				error = EBUSY;
2105ce3ee1e7SLuigi Rizzo 				break;
2106f196ce38SLuigi Rizzo 			}
2107f0ea3689SLuigi Rizzo 			nifp = netmap_do_regif(priv, na, nmr->nr_ringid, nmr->nr_flags, &error);
2108f18be576SLuigi Rizzo 			if (!nifp) {    /* reg. failed, release priv and ref */
2109f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
21108241616dSLuigi Rizzo 				priv->np_nifp = NULL;
2111ce3ee1e7SLuigi Rizzo 				break;
211268b8534bSLuigi Rizzo 			}
2113f0ea3689SLuigi Rizzo 			priv->np_td = td; // XXX kqueue, debugging only
211468b8534bSLuigi Rizzo 
211568b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
2116d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
2117d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
211864ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
211964ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
2120f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags,
2121f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
2122ce3ee1e7SLuigi Rizzo 			if (error) {
2123f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
2124ce3ee1e7SLuigi Rizzo 				break;
2125ce3ee1e7SLuigi Rizzo 			}
2126ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
21273d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
2128ce3ee1e7SLuigi Rizzo 			}
2129f0ea3689SLuigi Rizzo 			priv->np_txsi = (priv->np_txqlast - priv->np_txqfirst > 1) ?
2130f0ea3689SLuigi Rizzo 				&na->tx_si : &na->tx_rings[priv->np_txqfirst].si;
2131f0ea3689SLuigi Rizzo 			priv->np_rxsi = (priv->np_rxqlast - priv->np_rxqfirst > 1) ?
2132f0ea3689SLuigi Rizzo 				&na->rx_si : &na->rx_rings[priv->np_rxqfirst].si;
2133f0ea3689SLuigi Rizzo 
2134f0ea3689SLuigi Rizzo 			if (nmr->nr_arg3) {
2135f0ea3689SLuigi Rizzo 				D("requested %d extra buffers", nmr->nr_arg3);
2136f0ea3689SLuigi Rizzo 				nmr->nr_arg3 = netmap_extra_alloc(na,
2137f0ea3689SLuigi Rizzo 					&nifp->ni_bufs_head, nmr->nr_arg3);
2138f0ea3689SLuigi Rizzo 				D("got %d extra buffers", nmr->nr_arg3);
2139f0ea3689SLuigi Rizzo 			}
2140ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
2141ce3ee1e7SLuigi Rizzo 		} while (0);
2142ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
214368b8534bSLuigi Rizzo 		break;
214468b8534bSLuigi Rizzo 
214568b8534bSLuigi Rizzo 	case NIOCTXSYNC:
214668b8534bSLuigi Rizzo 	case NIOCRXSYNC:
21478241616dSLuigi Rizzo 		nifp = priv->np_nifp;
21488241616dSLuigi Rizzo 
21498241616dSLuigi Rizzo 		if (nifp == NULL) {
2150506cc70cSLuigi Rizzo 			error = ENXIO;
2151506cc70cSLuigi Rizzo 			break;
2152506cc70cSLuigi Rizzo 		}
21538241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
21548241616dSLuigi Rizzo 
2155f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
21568241616dSLuigi Rizzo 
2157f9790aebSLuigi Rizzo 		if (na == NULL) {
2158f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
21598241616dSLuigi Rizzo 			error = ENXIO;
21608241616dSLuigi Rizzo 			break;
21618241616dSLuigi Rizzo 		}
21628241616dSLuigi Rizzo 
2163*4bf50f18SLuigi Rizzo 		if (!nm_netmap_on(na)) {
2164f9790aebSLuigi Rizzo 			error = ENXIO;
2165f9790aebSLuigi Rizzo 			break;
2166f9790aebSLuigi Rizzo 		}
2167f9790aebSLuigi Rizzo 
2168f0ea3689SLuigi Rizzo 		if (cmd == NIOCTXSYNC) {
2169f0ea3689SLuigi Rizzo 			krings = na->tx_rings;
2170f0ea3689SLuigi Rizzo 			qfirst = priv->np_txqfirst;
2171f0ea3689SLuigi Rizzo 			qlast = priv->np_txqlast;
2172f0ea3689SLuigi Rizzo 		} else {
2173f0ea3689SLuigi Rizzo 			krings = na->rx_rings;
2174f0ea3689SLuigi Rizzo 			qfirst = priv->np_rxqfirst;
2175f0ea3689SLuigi Rizzo 			qlast = priv->np_rxqlast;
217668b8534bSLuigi Rizzo 		}
217768b8534bSLuigi Rizzo 
2178f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
2179ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
2180ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
2181ce3ee1e7SLuigi Rizzo 				error = EBUSY;
2182ce3ee1e7SLuigi Rizzo 				goto out;
2183ce3ee1e7SLuigi Rizzo 			}
218468b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
218568b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
21863c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
218768b8534bSLuigi Rizzo 					    i, kring->ring->cur,
218868b8534bSLuigi Rizzo 					    kring->nr_hwcur);
218917885a7bSLuigi Rizzo 				if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
219017885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
219117885a7bSLuigi Rizzo 				} else {
2192f0ea3689SLuigi Rizzo 					kring->nm_sync(kring, NAF_FORCE_RECLAIM);
219317885a7bSLuigi Rizzo 				}
219468b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
21953c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
219668b8534bSLuigi Rizzo 					    i, kring->ring->cur,
219768b8534bSLuigi Rizzo 					    kring->nr_hwcur);
219868b8534bSLuigi Rizzo 			} else {
2199f0ea3689SLuigi Rizzo 				kring->nm_sync(kring, NAF_FORCE_READ);
220068b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
220168b8534bSLuigi Rizzo 			}
2202ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
220368b8534bSLuigi Rizzo 		}
220468b8534bSLuigi Rizzo 
220568b8534bSLuigi Rizzo 		break;
220668b8534bSLuigi Rizzo 
2207*4bf50f18SLuigi Rizzo 	case NIOCCONFIG:
2208*4bf50f18SLuigi Rizzo 		error = netmap_bdg_config(nmr);
2209*4bf50f18SLuigi Rizzo 		break;
2210f196ce38SLuigi Rizzo #ifdef __FreeBSD__
221189e3fd52SLuigi Rizzo 	case FIONBIO:
221289e3fd52SLuigi Rizzo 	case FIOASYNC:
221389e3fd52SLuigi Rizzo 		ND("FIONBIO/FIOASYNC are no-ops");
221489e3fd52SLuigi Rizzo 		break;
221589e3fd52SLuigi Rizzo 
221668b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
221768b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
221868b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
221968b8534bSLuigi Rizzo 	case BIOCSSEESENT:
222068b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
222168b8534bSLuigi Rizzo 		break;
222268b8534bSLuigi Rizzo 
2223babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
222468b8534bSLuigi Rizzo 	    {
222568b8534bSLuigi Rizzo 		struct socket so;
2226*4bf50f18SLuigi Rizzo 		struct ifnet *ifp;
2227ce3ee1e7SLuigi Rizzo 
222868b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
2229ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
2230f9790aebSLuigi Rizzo 		error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */
2231ce3ee1e7SLuigi Rizzo 		if (error) {
2232f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
2233ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
223468b8534bSLuigi Rizzo 			break;
2235ce3ee1e7SLuigi Rizzo 		}
2236f9790aebSLuigi Rizzo 		ifp = na->ifp;
223768b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
223868b8534bSLuigi Rizzo 		// so->so_proto not null.
223968b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
2240f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
2241ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
2242babc7c12SLuigi Rizzo 		break;
224368b8534bSLuigi Rizzo 	    }
2244f196ce38SLuigi Rizzo 
2245f196ce38SLuigi Rizzo #else /* linux */
2246f196ce38SLuigi Rizzo 	default:
2247f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
2248f196ce38SLuigi Rizzo #endif /* linux */
224968b8534bSLuigi Rizzo 	}
2250ce3ee1e7SLuigi Rizzo out:
225168b8534bSLuigi Rizzo 
2252506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
225368b8534bSLuigi Rizzo 	return (error);
225468b8534bSLuigi Rizzo }
225568b8534bSLuigi Rizzo 
225668b8534bSLuigi Rizzo 
225768b8534bSLuigi Rizzo /*
225868b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
225968b8534bSLuigi Rizzo  *
226068b8534bSLuigi Rizzo  * Can be called for one or more queues.
226168b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
226268b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
2263ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
226468b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
226568b8534bSLuigi Rizzo  * are done through callbacks.
2266f196ce38SLuigi Rizzo  *
226701c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
226801c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
226901c7d25fSLuigi Rizzo  * hidden argument.
227068b8534bSLuigi Rizzo  */
2271f9790aebSLuigi Rizzo int
227201c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
227368b8534bSLuigi Rizzo {
227468b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
227568b8534bSLuigi Rizzo 	struct netmap_adapter *na;
227668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2277954dca4cSLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0;
227817885a7bSLuigi Rizzo 	struct mbq q;		/* packets from hw queues to host stack */
227901c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
2280f0ea3689SLuigi Rizzo 	int is_kevent = 0;
228101c7d25fSLuigi Rizzo 
2282f9790aebSLuigi Rizzo 	/*
2283f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
2284f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
2285f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
2286f9790aebSLuigi Rizzo 	 */
228717885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
2288ce3ee1e7SLuigi Rizzo 
228901c7d25fSLuigi Rizzo 	(void)pwait;
2290f9790aebSLuigi Rizzo 	mbq_init(&q);
229168b8534bSLuigi Rizzo 
2292f0ea3689SLuigi Rizzo 	/*
2293f0ea3689SLuigi Rizzo 	 * XXX kevent has curthread->tp_fop == NULL,
2294f0ea3689SLuigi Rizzo 	 * so devfs_get_cdevpriv() fails. We circumvent this by passing
2295f0ea3689SLuigi Rizzo 	 * priv as the first argument, which is also useful to avoid
2296f0ea3689SLuigi Rizzo 	 * the selrecord() which are not necessary in that case.
2297f0ea3689SLuigi Rizzo 	 */
2298f0ea3689SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0) {
2299f0ea3689SLuigi Rizzo 		is_kevent = 1;
2300f0ea3689SLuigi Rizzo 		if (netmap_verbose)
2301f0ea3689SLuigi Rizzo 			D("called from kevent");
2302f0ea3689SLuigi Rizzo 		priv = (struct netmap_priv_d *)dev;
2303f0ea3689SLuigi Rizzo 	}
2304f0ea3689SLuigi Rizzo 	if (priv == NULL)
230568b8534bSLuigi Rizzo 		return POLLERR;
230668b8534bSLuigi Rizzo 
23078241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
23088241616dSLuigi Rizzo 		D("No if registered");
23098241616dSLuigi Rizzo 		return POLLERR;
23108241616dSLuigi Rizzo 	}
23118241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
23128241616dSLuigi Rizzo 
2313f9790aebSLuigi Rizzo 	na = priv->np_na;
2314f9790aebSLuigi Rizzo 
2315*4bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
231668b8534bSLuigi Rizzo 		return POLLERR;
231768b8534bSLuigi Rizzo 
231868b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
2319*4bf50f18SLuigi Rizzo 		D("device %s events 0x%x", na->name, events);
232068b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
232168b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
232268b8534bSLuigi Rizzo 
2323091fd0abSLuigi Rizzo 
232468b8534bSLuigi Rizzo 	/*
2325f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
2326f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
2327ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
2328ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
2329ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
2330ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
233168b8534bSLuigi Rizzo 	 *
233268b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
233368b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
233468b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
233568b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
233668b8534bSLuigi Rizzo 	 */
2337f0ea3689SLuigi Rizzo 	check_all_tx = nm_tx_si_user(priv);
2338f0ea3689SLuigi Rizzo 	check_all_rx = nm_rx_si_user(priv);
233964ae02c3SLuigi Rizzo 
234068b8534bSLuigi Rizzo 	/*
2341f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
2342f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
234368b8534bSLuigi Rizzo 	 * routines.
234468b8534bSLuigi Rizzo 	 */
2345f0ea3689SLuigi Rizzo 	for (i = priv->np_rxqfirst; want_rx && i < priv->np_rxqlast; i++) {
234668b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
234717885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
234817885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
234968b8534bSLuigi Rizzo 			revents |= want_rx;
235068b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
235168b8534bSLuigi Rizzo 		}
235268b8534bSLuigi Rizzo 	}
2353f0ea3689SLuigi Rizzo 	for (i = priv->np_txqfirst; want_tx && i < priv->np_txqlast; i++) {
235468b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
235517885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
235617885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
235768b8534bSLuigi Rizzo 			revents |= want_tx;
235868b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
235968b8534bSLuigi Rizzo 		}
236068b8534bSLuigi Rizzo 	}
236168b8534bSLuigi Rizzo 
236268b8534bSLuigi Rizzo 	/*
236317885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
236417885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
236517885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
2366f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
2367f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
236868b8534bSLuigi Rizzo 	 */
236968b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
237017885a7bSLuigi Rizzo 		/*
237117885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
237217885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
237317885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
237417885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
2375ce3ee1e7SLuigi Rizzo 		 */
2376091fd0abSLuigi Rizzo flush_tx:
2377f0ea3689SLuigi Rizzo 		for (i = priv->np_txqfirst; i < priv->np_txqlast; i++) {
237817885a7bSLuigi Rizzo 			int found = 0;
237917885a7bSLuigi Rizzo 
238068b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
238168b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
238268b8534bSLuigi Rizzo 				continue;
238317885a7bSLuigi Rizzo 			/* only one thread does txsync */
2384ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
238589cc2556SLuigi Rizzo 				/* either busy or stopped
238689cc2556SLuigi Rizzo 				 * XXX if the ring is stopped, sleeping would
238789cc2556SLuigi Rizzo 				 * be better. In current code, however, we only
238889cc2556SLuigi Rizzo 				 * stop the rings for brief intervals (2014-03-14)
238989cc2556SLuigi Rizzo 				 */
239089e3fd52SLuigi Rizzo 				if (netmap_verbose)
239189e3fd52SLuigi Rizzo 					RD(2, "%p lost race on txring %d, ok",
239289e3fd52SLuigi Rizzo 					    priv, i);
239317885a7bSLuigi Rizzo 				continue;
239468b8534bSLuigi Rizzo 			}
239517885a7bSLuigi Rizzo 			if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
239617885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
239717885a7bSLuigi Rizzo 				revents |= POLLERR;
239817885a7bSLuigi Rizzo 			} else {
2399f0ea3689SLuigi Rizzo 				if (kring->nm_sync(kring, 0))
240068b8534bSLuigi Rizzo 					revents |= POLLERR;
240117885a7bSLuigi Rizzo 			}
240268b8534bSLuigi Rizzo 
240317885a7bSLuigi Rizzo 			/*
240417885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
240517885a7bSLuigi Rizzo 			 * listeners on the same ring.
240617885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
240717885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
2408f9790aebSLuigi Rizzo 			 */
240917885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
241017885a7bSLuigi Rizzo 			nm_kr_put(kring);
241117885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
241268b8534bSLuigi Rizzo 				revents |= want_tx;
241368b8534bSLuigi Rizzo 				want_tx = 0;
2414f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_TX, 0);
241568b8534bSLuigi Rizzo 			}
2416ce3ee1e7SLuigi Rizzo 		}
2417f0ea3689SLuigi Rizzo 		if (want_tx && retry_tx && !is_kevent) {
2418954dca4cSLuigi Rizzo 			selrecord(td, check_all_tx ?
2419f0ea3689SLuigi Rizzo 			    &na->tx_si : &na->tx_rings[priv->np_txqfirst].si);
2420ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
2421ce3ee1e7SLuigi Rizzo 			goto flush_tx;
242268b8534bSLuigi Rizzo 		}
242368b8534bSLuigi Rizzo 	}
242468b8534bSLuigi Rizzo 
242568b8534bSLuigi Rizzo 	/*
242617885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
242768b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
242868b8534bSLuigi Rizzo 	 */
242968b8534bSLuigi Rizzo 	if (want_rx) {
243017885a7bSLuigi Rizzo 		int send_down = 0; /* transparent mode */
243189cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
2432ce3ee1e7SLuigi Rizzo do_retry_rx:
2433f0ea3689SLuigi Rizzo 		for (i = priv->np_rxqfirst; i < priv->np_rxqlast; i++) {
243417885a7bSLuigi Rizzo 			int found = 0;
243517885a7bSLuigi Rizzo 
243668b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
2437ce3ee1e7SLuigi Rizzo 
2438ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
243989e3fd52SLuigi Rizzo 				if (netmap_verbose)
244089e3fd52SLuigi Rizzo 					RD(2, "%p lost race on rxring %d, ok",
244189e3fd52SLuigi Rizzo 					    priv, i);
244217885a7bSLuigi Rizzo 				continue;
244368b8534bSLuigi Rizzo 			}
2444ce3ee1e7SLuigi Rizzo 
244517885a7bSLuigi Rizzo 			/*
244617885a7bSLuigi Rizzo 			 * transparent mode support: collect packets
244717885a7bSLuigi Rizzo 			 * from the rxring(s).
2448*4bf50f18SLuigi Rizzo 			 * XXX NR_FORWARD should only be read on
2449*4bf50f18SLuigi Rizzo 			 * physical or NIC ports
2450ce3ee1e7SLuigi Rizzo 			 */
2451091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
2452091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
2453091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
2454091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
2455091fd0abSLuigi Rizzo 			}
245668b8534bSLuigi Rizzo 
2457f0ea3689SLuigi Rizzo 			if (kring->nm_sync(kring, 0))
245868b8534bSLuigi Rizzo 				revents |= POLLERR;
24595819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
24605819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
246168b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
24625819da83SLuigi Rizzo 			}
246317885a7bSLuigi Rizzo 			/* after an rxsync we can use kring->rcur, rtail */
246417885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
246517885a7bSLuigi Rizzo 			nm_kr_put(kring);
246617885a7bSLuigi Rizzo 			if (found) {
246768b8534bSLuigi Rizzo 				revents |= want_rx;
2468ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
2469f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_RX, 0);
247068b8534bSLuigi Rizzo 			}
247168b8534bSLuigi Rizzo 		}
247217885a7bSLuigi Rizzo 
247317885a7bSLuigi Rizzo 		/* transparent mode XXX only during first pass ? */
2474f0ea3689SLuigi Rizzo 		if (na->na_flags & NAF_HOST_RINGS) {
2475f0ea3689SLuigi Rizzo 			kring = &na->rx_rings[na->num_rx_rings];
2476*4bf50f18SLuigi Rizzo 			if (check_all_rx
2477*4bf50f18SLuigi Rizzo 			    && (netmap_fwd || kring->ring->flags & NR_FORWARD)) {
2478*4bf50f18SLuigi Rizzo 				/* XXX fix to use kring fields */
2479*4bf50f18SLuigi Rizzo 				if (nm_ring_empty(kring->ring))
248017885a7bSLuigi Rizzo 					send_down = netmap_rxsync_from_host(na, td, dev);
2481*4bf50f18SLuigi Rizzo 				if (!nm_ring_empty(kring->ring))
2482*4bf50f18SLuigi Rizzo 					revents |= want_rx;
248317885a7bSLuigi Rizzo 			}
2484f0ea3689SLuigi Rizzo 		}
248517885a7bSLuigi Rizzo 
2486f0ea3689SLuigi Rizzo 		if (retry_rx && !is_kevent)
2487954dca4cSLuigi Rizzo 			selrecord(td, check_all_rx ?
2488f0ea3689SLuigi Rizzo 			    &na->rx_si : &na->rx_rings[priv->np_rxqfirst].si);
248917885a7bSLuigi Rizzo 		if (send_down > 0 || retry_rx) {
249017885a7bSLuigi Rizzo 			retry_rx = 0;
249117885a7bSLuigi Rizzo 			if (send_down)
249217885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
249317885a7bSLuigi Rizzo 			else
2494ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
2495ce3ee1e7SLuigi Rizzo 		}
249668b8534bSLuigi Rizzo 	}
2497091fd0abSLuigi Rizzo 
249817885a7bSLuigi Rizzo 	/*
249917885a7bSLuigi Rizzo 	 * Transparent mode: marked bufs on rx rings between
250017885a7bSLuigi Rizzo 	 * kring->nr_hwcur and ring->head
250117885a7bSLuigi Rizzo 	 * are passed to the other endpoint.
250217885a7bSLuigi Rizzo 	 *
250317885a7bSLuigi Rizzo 	 * In this mode we also scan the sw rxring, which in
250417885a7bSLuigi Rizzo 	 * turn passes packets up.
250517885a7bSLuigi Rizzo 	 *
250617885a7bSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
250717885a7bSLuigi Rizzo  	 * rings to a single file descriptor.
2508ce3ee1e7SLuigi Rizzo 	 */
2509091fd0abSLuigi Rizzo 
2510*4bf50f18SLuigi Rizzo 	if (q.head && na->ifp != NULL)
2511f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
251268b8534bSLuigi Rizzo 
251368b8534bSLuigi Rizzo 	return (revents);
251468b8534bSLuigi Rizzo }
251568b8534bSLuigi Rizzo 
251617885a7bSLuigi Rizzo 
251717885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
251868b8534bSLuigi Rizzo 
2519f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
2520f9790aebSLuigi Rizzo 
252189cc2556SLuigi Rizzo /* default notify callback */
2522f9790aebSLuigi Rizzo static int
252317885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring,
252417885a7bSLuigi Rizzo 	enum txrx tx, int flags)
2525f9790aebSLuigi Rizzo {
2526f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2527f9790aebSLuigi Rizzo 
2528f9790aebSLuigi Rizzo 	if (tx == NR_TX) {
2529f9790aebSLuigi Rizzo 		kring = na->tx_rings + n_ring;
2530f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
253189cc2556SLuigi Rizzo 		/* optimization: avoid a wake up on the global
253289cc2556SLuigi Rizzo 		 * queue if nobody has registered for more
253389cc2556SLuigi Rizzo 		 * than one ring
253489cc2556SLuigi Rizzo 		 */
2535f0ea3689SLuigi Rizzo 		if (na->tx_si_users > 0)
2536f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->tx_si, PI_NET);
2537f9790aebSLuigi Rizzo 	} else {
2538f9790aebSLuigi Rizzo 		kring = na->rx_rings + n_ring;
2539f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
254089cc2556SLuigi Rizzo 		/* optimization: same as above */
2541f0ea3689SLuigi Rizzo 		if (na->rx_si_users > 0)
2542f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->rx_si, PI_NET);
2543f9790aebSLuigi Rizzo 	}
2544f9790aebSLuigi Rizzo 	return 0;
2545f9790aebSLuigi Rizzo }
2546f9790aebSLuigi Rizzo 
2547f9790aebSLuigi Rizzo 
254889cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
254989cc2556SLuigi Rizzo  * Attach na to the ifp (if any) and provide defaults
255089cc2556SLuigi Rizzo  * for optional callbacks. Defaults assume that we
255189cc2556SLuigi Rizzo  * are creating an hardware netmap_adapter.
255289cc2556SLuigi Rizzo  */
2553f9790aebSLuigi Rizzo int
2554f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2555f9790aebSLuigi Rizzo {
2556f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2557f9790aebSLuigi Rizzo 
2558f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2559f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
2560*4bf50f18SLuigi Rizzo 			na->name, na->num_tx_rings, na->num_rx_rings);
2561f9790aebSLuigi Rizzo 		return EINVAL;
2562f9790aebSLuigi Rizzo 	}
2563*4bf50f18SLuigi Rizzo 	/* ifp is NULL for virtual adapters (bwrap, non-persistent VALE ports,
2564*4bf50f18SLuigi Rizzo 	 * pipes, monitors). For bwrap we actually have a non-null ifp for
2565*4bf50f18SLuigi Rizzo 	 * use by the external modules, but that is set after this
2566*4bf50f18SLuigi Rizzo 	 * function has been called.
2567*4bf50f18SLuigi Rizzo 	 * XXX this is ugly, maybe split this function in two (2014-03-14)
2568*4bf50f18SLuigi Rizzo 	 */
2569*4bf50f18SLuigi Rizzo 	if (ifp != NULL) {
2570f9790aebSLuigi Rizzo 		WNA(ifp) = na;
257117885a7bSLuigi Rizzo 
257217885a7bSLuigi Rizzo 	/* the following is only needed for na that use the host port.
257317885a7bSLuigi Rizzo 	 * XXX do we have something similar for linux ?
257417885a7bSLuigi Rizzo 	 */
257517885a7bSLuigi Rizzo #ifdef __FreeBSD__
257617885a7bSLuigi Rizzo 		na->if_input = ifp->if_input; /* for netmap_send_up */
257717885a7bSLuigi Rizzo #endif /* __FreeBSD__ */
257817885a7bSLuigi Rizzo 
2579f9790aebSLuigi Rizzo 		NETMAP_SET_CAPABLE(ifp);
2580*4bf50f18SLuigi Rizzo 	}
2581f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
258289cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
258389cc2556SLuigi Rizzo 		 * since other port types all provide their own
258489cc2556SLuigi Rizzo 		 * nm_krings_create
258589cc2556SLuigi Rizzo 		 */
2586f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
258717885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
2588f9790aebSLuigi Rizzo 	}
2589f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2590f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2591f9790aebSLuigi Rizzo 	na->active_fds = 0;
2592f9790aebSLuigi Rizzo 
2593f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
2594*4bf50f18SLuigi Rizzo 		/* use the global allocator */
2595f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
2596*4bf50f18SLuigi Rizzo 	if (na->nm_bdg_attach == NULL)
2597*4bf50f18SLuigi Rizzo 		/* no special nm_bdg_attach callback. On VALE
2598*4bf50f18SLuigi Rizzo 		 * attach, we need to interpose a bwrap
2599*4bf50f18SLuigi Rizzo 		 */
2600*4bf50f18SLuigi Rizzo 		na->nm_bdg_attach = netmap_bwrap_attach;
2601f9790aebSLuigi Rizzo 	return 0;
2602f9790aebSLuigi Rizzo }
2603f9790aebSLuigi Rizzo 
2604f9790aebSLuigi Rizzo 
260589cc2556SLuigi Rizzo /* standard cleanup, called by all destructors */
2606f9790aebSLuigi Rizzo void
2607f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2608f9790aebSLuigi Rizzo {
260989cc2556SLuigi Rizzo 	if (na->ifp != NULL)
2610f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2611f9790aebSLuigi Rizzo 
2612f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2613f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2614f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2615f9790aebSLuigi Rizzo 	}
2616f0ea3689SLuigi Rizzo 	netmap_pipe_dealloc(na);
2617f9790aebSLuigi Rizzo 	if (na->na_flags & NAF_MEM_OWNER)
2618f9790aebSLuigi Rizzo 		netmap_mem_private_delete(na->nm_mem);
2619f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2620f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2621f9790aebSLuigi Rizzo }
2622f9790aebSLuigi Rizzo 
2623*4bf50f18SLuigi Rizzo /* Wrapper for the register callback provided hardware drivers.
2624*4bf50f18SLuigi Rizzo  * na->ifp == NULL means the the driver module has been
2625*4bf50f18SLuigi Rizzo  * unloaded, so we cannot call into it.
2626*4bf50f18SLuigi Rizzo  * Note that module unloading, in our patched linux drivers,
2627*4bf50f18SLuigi Rizzo  * happens under NMG_LOCK and after having stopped all the
2628*4bf50f18SLuigi Rizzo  * nic rings (see netmap_detach). This provides sufficient
2629*4bf50f18SLuigi Rizzo  * protection for the other driver-provied callbacks
2630*4bf50f18SLuigi Rizzo  * (i.e., nm_config and nm_*xsync), that therefore don't need
2631*4bf50f18SLuigi Rizzo  * to wrapped.
2632*4bf50f18SLuigi Rizzo  */
2633*4bf50f18SLuigi Rizzo static int
2634*4bf50f18SLuigi Rizzo netmap_hw_register(struct netmap_adapter *na, int onoff)
2635*4bf50f18SLuigi Rizzo {
2636*4bf50f18SLuigi Rizzo 	struct netmap_hw_adapter *hwna =
2637*4bf50f18SLuigi Rizzo 		(struct netmap_hw_adapter*)na;
2638*4bf50f18SLuigi Rizzo 
2639*4bf50f18SLuigi Rizzo 	if (na->ifp == NULL)
2640*4bf50f18SLuigi Rizzo 		return onoff ? ENXIO : 0;
2641*4bf50f18SLuigi Rizzo 
2642*4bf50f18SLuigi Rizzo 	return hwna->nm_hw_register(na, onoff);
2643*4bf50f18SLuigi Rizzo }
2644*4bf50f18SLuigi Rizzo 
2645f18be576SLuigi Rizzo 
264668b8534bSLuigi Rizzo /*
264768b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
264868b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
264968b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
265068b8534bSLuigi Rizzo  * of hardware rings):
265168b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
265268b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
265317885a7bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues. // XXX still true ?
265468b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
265568b8534bSLuigi Rizzo  */
265668b8534bSLuigi Rizzo int
2657f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
265868b8534bSLuigi Rizzo {
2659f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2660f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2661ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
266268b8534bSLuigi Rizzo 
2663ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2664ae10d1afSLuigi Rizzo 		goto fail;
2665f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2666f9790aebSLuigi Rizzo 	if (hwna == NULL)
2667ae10d1afSLuigi Rizzo 		goto fail;
2668f9790aebSLuigi Rizzo 	hwna->up = *arg;
2669f0ea3689SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS;
2670*4bf50f18SLuigi Rizzo 	strncpy(hwna->up.name, ifp->if_xname, sizeof(hwna->up.name));
2671*4bf50f18SLuigi Rizzo 	hwna->nm_hw_register = hwna->up.nm_register;
2672*4bf50f18SLuigi Rizzo 	hwna->up.nm_register = netmap_hw_register;
2673f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2674f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2675f9790aebSLuigi Rizzo 		goto fail;
2676f9790aebSLuigi Rizzo 	}
2677f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2678f9790aebSLuigi Rizzo 
267964ae02c3SLuigi Rizzo #ifdef linux
2680f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2681f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2682f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2683f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2684f18be576SLuigi Rizzo #else
2685f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2686f18be576SLuigi Rizzo #endif
2687f18be576SLuigi Rizzo 	}
2688f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
2689*4bf50f18SLuigi Rizzo 	if (ifp->ethtool_ops) {
2690*4bf50f18SLuigi Rizzo 		hwna->nm_eto = *ifp->ethtool_ops;
2691*4bf50f18SLuigi Rizzo 	}
2692*4bf50f18SLuigi Rizzo 	hwna->nm_eto.set_ringparam = linux_netmap_set_ringparam;
2693*4bf50f18SLuigi Rizzo #ifdef ETHTOOL_SCHANNELS
2694*4bf50f18SLuigi Rizzo 	hwna->nm_eto.set_channels = linux_netmap_set_channels;
2695*4bf50f18SLuigi Rizzo #endif
2696*4bf50f18SLuigi Rizzo 	if (arg->nm_config == NULL) {
2697*4bf50f18SLuigi Rizzo 		hwna->up.nm_config = netmap_linux_config;
2698*4bf50f18SLuigi Rizzo 	}
2699ce3ee1e7SLuigi Rizzo #endif /* linux */
2700f9790aebSLuigi Rizzo 
270189cc2556SLuigi Rizzo 	D("success for %s tx %d/%d rx %d/%d queues/slots",
2702*4bf50f18SLuigi Rizzo 		hwna->up.name,
270389cc2556SLuigi Rizzo 		hwna->up.num_tx_rings, hwna->up.num_tx_desc,
270489cc2556SLuigi Rizzo 		hwna->up.num_rx_rings, hwna->up.num_rx_desc
270589cc2556SLuigi Rizzo 		);
2706ae10d1afSLuigi Rizzo 	return 0;
270768b8534bSLuigi Rizzo 
2708ae10d1afSLuigi Rizzo fail:
2709f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2710441ab64fSLuigi Rizzo 	if (ifp)
2711849bec0eSLuigi Rizzo 		netmap_detach(ifp);
2712f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
271368b8534bSLuigi Rizzo }
271468b8534bSLuigi Rizzo 
271568b8534bSLuigi Rizzo 
2716f9790aebSLuigi Rizzo void
2717f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2718f9790aebSLuigi Rizzo {
2719f9790aebSLuigi Rizzo 	if (!na) {
2720f9790aebSLuigi Rizzo 		return;
2721f9790aebSLuigi Rizzo 	}
2722f9790aebSLuigi Rizzo 
2723f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2724f9790aebSLuigi Rizzo }
2725f9790aebSLuigi Rizzo 
2726f9790aebSLuigi Rizzo 
2727f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2728f9790aebSLuigi Rizzo int
2729f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2730f9790aebSLuigi Rizzo {
2731f9790aebSLuigi Rizzo 	if (!na)
2732f9790aebSLuigi Rizzo 		return 1;
2733f9790aebSLuigi Rizzo 
2734f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2735f9790aebSLuigi Rizzo 		return 0;
2736f9790aebSLuigi Rizzo 
2737f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2738f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2739f9790aebSLuigi Rizzo 
2740f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2741f9790aebSLuigi Rizzo 
2742f9790aebSLuigi Rizzo 	return 1;
2743f9790aebSLuigi Rizzo }
2744f9790aebSLuigi Rizzo 
274589cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
2746f9790aebSLuigi Rizzo int
2747f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2748f9790aebSLuigi Rizzo {
2749f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
275017885a7bSLuigi Rizzo 	if (ret == 0) {
275117885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
275217885a7bSLuigi Rizzo 		mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue);
275317885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
275417885a7bSLuigi Rizzo 	}
275517885a7bSLuigi Rizzo 	return ret;
2756f9790aebSLuigi Rizzo }
2757f9790aebSLuigi Rizzo 
2758f9790aebSLuigi Rizzo 
2759f9790aebSLuigi Rizzo 
276068b8534bSLuigi Rizzo /*
276189cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
276268b8534bSLuigi Rizzo  */
276368b8534bSLuigi Rizzo void
276468b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
276568b8534bSLuigi Rizzo {
276668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
276768b8534bSLuigi Rizzo 
276868b8534bSLuigi Rizzo 	if (!na)
276968b8534bSLuigi Rizzo 		return;
277068b8534bSLuigi Rizzo 
2771f9790aebSLuigi Rizzo 	NMG_LOCK();
2772f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2773fb25194fSLuigi Rizzo 	if (!netmap_adapter_put(na)) {
2774fb25194fSLuigi Rizzo 		/* someone is still using the adapter,
2775fb25194fSLuigi Rizzo 		 * tell them that the interface is gone
2776fb25194fSLuigi Rizzo 		 */
2777f9790aebSLuigi Rizzo 		na->ifp = NULL;
2778*4bf50f18SLuigi Rizzo 		// XXX also clear NAF_NATIVE_ON ?
2779*4bf50f18SLuigi Rizzo 		na->na_flags &= ~NAF_NETMAP_ON;
2780fb25194fSLuigi Rizzo 		/* give them a chance to notice */
2781f9790aebSLuigi Rizzo 		netmap_enable_all_rings(ifp);
2782fb25194fSLuigi Rizzo 	}
2783f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2784ae10d1afSLuigi Rizzo }
2785f18be576SLuigi Rizzo 
2786f18be576SLuigi Rizzo 
278768b8534bSLuigi Rizzo /*
278802ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
278902ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
279017885a7bSLuigi Rizzo  *
279117885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
279217885a7bSLuigi Rizzo  * in the relevant rxsync routine.
279317885a7bSLuigi Rizzo  *
2794ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2795ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2796ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2797f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
279868b8534bSLuigi Rizzo  */
279968b8534bSLuigi Rizzo int
2800ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
280168b8534bSLuigi Rizzo {
280268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2803ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
280417885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
280517885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
280617885a7bSLuigi Rizzo 	struct mbq *q;
280717885a7bSLuigi Rizzo 	int space;
280868b8534bSLuigi Rizzo 
2809ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2810ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2811ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
281217885a7bSLuigi Rizzo 
2813*4bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na)) {
2814*4bf50f18SLuigi Rizzo 		D("%s not in netmap mode anymore", na->name);
2815ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2816ce3ee1e7SLuigi Rizzo 		goto done;
2817ce3ee1e7SLuigi Rizzo 	}
2818ce3ee1e7SLuigi Rizzo 
2819ce3ee1e7SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
282017885a7bSLuigi Rizzo 	q = &kring->rx_queue;
282117885a7bSLuigi Rizzo 
2822ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
2823*4bf50f18SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE(na)) { /* too long for us */
2824*4bf50f18SLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", na->name,
2825*4bf50f18SLuigi Rizzo 			len, NETMAP_BUF_SIZE(na));
2826ce3ee1e7SLuigi Rizzo 		goto done;
2827849bec0eSLuigi Rizzo 	}
282817885a7bSLuigi Rizzo 
282917885a7bSLuigi Rizzo 	/* protect against rxsync_from_host(), netmap_sw_to_nic()
283017885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
283117885a7bSLuigi Rizzo 	 * not possible on Linux).
283217885a7bSLuigi Rizzo 	 * Also avoid overflowing the queue.
2833ce3ee1e7SLuigi Rizzo 	 */
2834997b054cSLuigi Rizzo 	mbq_lock(q);
283517885a7bSLuigi Rizzo 
283617885a7bSLuigi Rizzo         space = kring->nr_hwtail - kring->nr_hwcur;
283717885a7bSLuigi Rizzo         if (space < 0)
283817885a7bSLuigi Rizzo                 space += kring->nkr_num_slots;
283917885a7bSLuigi Rizzo 	if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX
284017885a7bSLuigi Rizzo 		RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p",
2841*4bf50f18SLuigi Rizzo 			na->name, kring->nr_hwcur, kring->nr_hwtail, mbq_len(q),
284217885a7bSLuigi Rizzo 			len, m);
2843ce3ee1e7SLuigi Rizzo 	} else {
284417885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
284517885a7bSLuigi Rizzo 		ND(10, "%s %d bufs in queue len %d m %p",
2846*4bf50f18SLuigi Rizzo 			na->name, mbq_len(q), len, m);
284717885a7bSLuigi Rizzo 		/* notify outside the lock */
284817885a7bSLuigi Rizzo 		m = NULL;
284968b8534bSLuigi Rizzo 		error = 0;
2850ce3ee1e7SLuigi Rizzo 	}
2851997b054cSLuigi Rizzo 	mbq_unlock(q);
2852ce3ee1e7SLuigi Rizzo 
285368b8534bSLuigi Rizzo done:
285417885a7bSLuigi Rizzo 	if (m)
285568b8534bSLuigi Rizzo 		m_freem(m);
285617885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
285717885a7bSLuigi Rizzo 	na->nm_notify(na, na->num_rx_rings, NR_RX, 0);
285889cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
285989cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
286089cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
286189cc2556SLuigi Rizzo 	 */
286268b8534bSLuigi Rizzo 
286368b8534bSLuigi Rizzo 	return (error);
286468b8534bSLuigi Rizzo }
286568b8534bSLuigi Rizzo 
286668b8534bSLuigi Rizzo 
286768b8534bSLuigi Rizzo /*
286868b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
286968b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2870f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
287168b8534bSLuigi Rizzo  */
287268b8534bSLuigi Rizzo struct netmap_slot *
2873ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
287468b8534bSLuigi Rizzo 	u_int new_cur)
287568b8534bSLuigi Rizzo {
287668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2877506cc70cSLuigi Rizzo 	int new_hwofs, lim;
287868b8534bSLuigi Rizzo 
2879*4bf50f18SLuigi Rizzo 	if (!nm_native_on(na)) {
2880*4bf50f18SLuigi Rizzo 		ND("interface not in native netmap mode");
288168b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2882ce3ee1e7SLuigi Rizzo 	}
288368b8534bSLuigi Rizzo 
2884ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2885ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2886ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2887ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2888ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2889ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2890ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2891ce3ee1e7SLuigi Rizzo 	 */
289264ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
28938241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
28948241616dSLuigi Rizzo 			return NULL;
289564ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
289617885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
2897506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
289864ae02c3SLuigi Rizzo 	} else {
28998241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
29008241616dSLuigi Rizzo 			return NULL;
290164ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
290217885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
290364ae02c3SLuigi Rizzo 	}
290464ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2905506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2906506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2907506cc70cSLuigi Rizzo 
2908ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
290917885a7bSLuigi Rizzo 	if (netmap_verbose)
291017885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
2911*4bf50f18SLuigi Rizzo 		na->name,
291217885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
2913ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
291417885a7bSLuigi Rizzo 		kring->nr_hwtail,
291517885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
2916506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
291717885a7bSLuigi Rizzo 	if (tx == NR_TX) {
291817885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
291917885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
292017885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
292117885a7bSLuigi Rizzo 	}
2922506cc70cSLuigi Rizzo 
2923f196ce38SLuigi Rizzo #if 0 // def linux
2924f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
2925f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
2926f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2927f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2928f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
2929f196ce38SLuigi Rizzo 		// XXX fix error handling
2930f196ce38SLuigi Rizzo 	}
2931f196ce38SLuigi Rizzo 
2932f196ce38SLuigi Rizzo #endif /* linux */
293368b8534bSLuigi Rizzo 	/*
2934ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
2935506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
2936506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
293768b8534bSLuigi Rizzo 	 */
2938f0ea3689SLuigi Rizzo 	na->nm_notify(na, n, tx, 0);
293968b8534bSLuigi Rizzo 	return kring->ring->slot;
294068b8534bSLuigi Rizzo }
294168b8534bSLuigi Rizzo 
294268b8534bSLuigi Rizzo 
2943ce3ee1e7SLuigi Rizzo /*
2944f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
2945ce3ee1e7SLuigi Rizzo  *
2946ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2947ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
2948ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
2949849bec0eSLuigi Rizzo  *
2950f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
2951f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
2952f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
2953*4bf50f18SLuigi Rizzo  *   (see netmap_notify)
2954*4bf50f18SLuigi Rizzo  * - for a nic connected to a switch, call the proper forwarding routine
2955*4bf50f18SLuigi Rizzo  *   (see netmap_bwrap_intr_notify)
2956f9790aebSLuigi Rizzo  */
2957f9790aebSLuigi Rizzo void
2958f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
2959f9790aebSLuigi Rizzo {
2960f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2961f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2962f9790aebSLuigi Rizzo 
2963f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
2964f9790aebSLuigi Rizzo 
2965f9790aebSLuigi Rizzo 	if (netmap_verbose) {
2966f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2967f9790aebSLuigi Rizzo 	}
2968f9790aebSLuigi Rizzo 
2969f9790aebSLuigi Rizzo 	if (work_done) { /* RX path */
2970f9790aebSLuigi Rizzo 		if (q >= na->num_rx_rings)
2971f9790aebSLuigi Rizzo 			return;	// not a physical queue
2972f9790aebSLuigi Rizzo 		kring = na->rx_rings + q;
2973f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
2974f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_RX, 0);
2975f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
2976f9790aebSLuigi Rizzo 	} else { /* TX path */
2977f9790aebSLuigi Rizzo 		if (q >= na->num_tx_rings)
2978f9790aebSLuigi Rizzo 			return;	// not a physical queue
2979f9790aebSLuigi Rizzo 		kring = na->tx_rings + q;
2980f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_TX, 0);
2981f9790aebSLuigi Rizzo 	}
2982f9790aebSLuigi Rizzo }
2983f9790aebSLuigi Rizzo 
298417885a7bSLuigi Rizzo 
2985f9790aebSLuigi Rizzo /*
2986f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
2987f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2988f9790aebSLuigi Rizzo  *
2989ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
2990ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
2991f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
2992ce3ee1e7SLuigi Rizzo  *
2993ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
2994ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
2995ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
2996ce3ee1e7SLuigi Rizzo  * and return 1.
2997ce3ee1e7SLuigi Rizzo  *
2998ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
2999ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
30001a26580eSLuigi Rizzo  */
3001babc7c12SLuigi Rizzo int
3002ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
30031a26580eSLuigi Rizzo {
3004*4bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
3005*4bf50f18SLuigi Rizzo 
3006*4bf50f18SLuigi Rizzo 	/*
3007*4bf50f18SLuigi Rizzo 	 * XXX emulated netmap mode sets NAF_SKIP_INTR so
3008*4bf50f18SLuigi Rizzo 	 * we still use the regular driver even though the previous
3009*4bf50f18SLuigi Rizzo 	 * check fails. It is unclear whether we should use
3010*4bf50f18SLuigi Rizzo 	 * nm_native_on() here.
3011*4bf50f18SLuigi Rizzo 	 */
3012*4bf50f18SLuigi Rizzo 	if (!nm_netmap_on(na))
30131a26580eSLuigi Rizzo 		return 0;
3014849bec0eSLuigi Rizzo 
3015*4bf50f18SLuigi Rizzo 	if (na->na_flags & NAF_SKIP_INTR) {
30168241616dSLuigi Rizzo 		ND("use regular interrupt");
30178241616dSLuigi Rizzo 		return 0;
30188241616dSLuigi Rizzo 	}
30198241616dSLuigi Rizzo 
3020f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
30211a26580eSLuigi Rizzo 	return 1;
30221a26580eSLuigi Rizzo }
30231a26580eSLuigi Rizzo 
302464ae02c3SLuigi Rizzo 
302501c7d25fSLuigi Rizzo /*
3026f9790aebSLuigi Rizzo  * Module loader and unloader
3027f196ce38SLuigi Rizzo  *
3028f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
3029f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
3030f9790aebSLuigi Rizzo  * (but there is no chance)
3031f9790aebSLuigi Rizzo  *
3032f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
3033f196ce38SLuigi Rizzo  */
3034babc7c12SLuigi Rizzo 
3035babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
3036f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
3037babc7c12SLuigi Rizzo 
303817885a7bSLuigi Rizzo 
3039f9790aebSLuigi Rizzo void
304068b8534bSLuigi Rizzo netmap_fini(void)
304168b8534bSLuigi Rizzo {
3042f9790aebSLuigi Rizzo 	// XXX destroy_bridges() ?
3043f9790aebSLuigi Rizzo 	if (netmap_dev)
304468b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
3045ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
3046ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
304768b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
304868b8534bSLuigi Rizzo }
304968b8534bSLuigi Rizzo 
305017885a7bSLuigi Rizzo 
3051f9790aebSLuigi Rizzo int
3052f9790aebSLuigi Rizzo netmap_init(void)
305368b8534bSLuigi Rizzo {
3054f9790aebSLuigi Rizzo 	int error;
305568b8534bSLuigi Rizzo 
3056f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
305768b8534bSLuigi Rizzo 
3058f9790aebSLuigi Rizzo 	error = netmap_mem_init();
3059f9790aebSLuigi Rizzo 	if (error != 0)
3060f9790aebSLuigi Rizzo 		goto fail;
3061f9790aebSLuigi Rizzo 	/* XXX could use make_dev_credv() to get error number */
3062f9790aebSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
3063f9790aebSLuigi Rizzo 			      "netmap");
3064f9790aebSLuigi Rizzo 	if (!netmap_dev)
3065f9790aebSLuigi Rizzo 		goto fail;
3066f9790aebSLuigi Rizzo 
3067f9790aebSLuigi Rizzo 	netmap_init_bridges();
3068*4bf50f18SLuigi Rizzo #ifdef __FreeBSD__
3069*4bf50f18SLuigi Rizzo 	nm_vi_init_index();
3070*4bf50f18SLuigi Rizzo #endif
3071f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
3072f9790aebSLuigi Rizzo 	return (0);
3073f9790aebSLuigi Rizzo fail:
307468b8534bSLuigi Rizzo 	netmap_fini();
3075f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
307668b8534bSLuigi Rizzo }
3077