14bf50f18SLuigi Rizzo /*
237e3a6d3SLuigi Rizzo  * Copyright (C) 2014-2016 Giuseppe Lettieri
337e3a6d3SLuigi Rizzo  * All rights reserved.
44bf50f18SLuigi Rizzo  *
54bf50f18SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
64bf50f18SLuigi Rizzo  * modification, are permitted provided that the following conditions
74bf50f18SLuigi Rizzo  * are met:
84bf50f18SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
94bf50f18SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
104bf50f18SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
114bf50f18SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
124bf50f18SLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
134bf50f18SLuigi Rizzo  *
144bf50f18SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154bf50f18SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164bf50f18SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174bf50f18SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184bf50f18SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194bf50f18SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204bf50f18SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214bf50f18SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224bf50f18SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234bf50f18SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244bf50f18SLuigi Rizzo  * SUCH DAMAGE.
254bf50f18SLuigi Rizzo  */
264bf50f18SLuigi Rizzo 
274bf50f18SLuigi Rizzo /*
284bf50f18SLuigi Rizzo  * $FreeBSD$
294bf50f18SLuigi Rizzo  *
304bf50f18SLuigi Rizzo  * Monitors
314bf50f18SLuigi Rizzo  *
32847bf383SLuigi Rizzo  * netmap monitors can be used to do monitoring of network traffic
334bf50f18SLuigi Rizzo  * on another adapter, when the latter adapter is working in netmap mode.
344bf50f18SLuigi Rizzo  *
354bf50f18SLuigi Rizzo  * Monitors offer to userspace the same interface as any other netmap port,
364bf50f18SLuigi Rizzo  * with as many pairs of netmap rings as the monitored adapter.
374bf50f18SLuigi Rizzo  * However, only the rx rings are actually used. Each monitor rx ring receives
384bf50f18SLuigi Rizzo  * the traffic transiting on both the tx and rx corresponding rings in the
394bf50f18SLuigi Rizzo  * monitored adapter. During registration, the user can choose if she wants
404bf50f18SLuigi Rizzo  * to intercept tx only, rx only, or both tx and rx traffic.
414bf50f18SLuigi Rizzo  *
42847bf383SLuigi Rizzo  * If the monitor is not able to cope with the stream of frames, excess traffic
43847bf383SLuigi Rizzo  * will be dropped.
44847bf383SLuigi Rizzo  *
45847bf383SLuigi Rizzo  * If the monitored adapter leaves netmap mode, the monitor has to be restarted.
46847bf383SLuigi Rizzo  *
47847bf383SLuigi Rizzo  * Monitors can be either zero-copy or copy-based.
48847bf383SLuigi Rizzo  *
49847bf383SLuigi Rizzo  * Copy monitors see the frames before they are consumed:
50847bf383SLuigi Rizzo  *
51847bf383SLuigi Rizzo  *  - For tx traffic, this is when the application sends them, before they are
52847bf383SLuigi Rizzo  *    passed down to the adapter.
53847bf383SLuigi Rizzo  *
54847bf383SLuigi Rizzo  *  - For rx traffic, this is when they are received by the adapter, before
55847bf383SLuigi Rizzo  *    they are sent up to the application, if any (note that, if no
56847bf383SLuigi Rizzo  *    application is reading from a monitored ring, the ring will eventually
57847bf383SLuigi Rizzo  *    fill up and traffic will stop).
58847bf383SLuigi Rizzo  *
59847bf383SLuigi Rizzo  * Zero-copy monitors only see the frames after they have been consumed:
604bf50f18SLuigi Rizzo  *
614bf50f18SLuigi Rizzo  *  - For tx traffic, this is after the slots containing the frames have been
624bf50f18SLuigi Rizzo  *    marked as free. Note that this may happen at a considerably delay after
634bf50f18SLuigi Rizzo  *    frame transmission, since freeing of slots is often done lazily.
644bf50f18SLuigi Rizzo  *
654bf50f18SLuigi Rizzo  *  - For rx traffic, this is after the consumer on the monitored adapter
664bf50f18SLuigi Rizzo  *    has released them. In most cases, the consumer is a userspace
674bf50f18SLuigi Rizzo  *    application which may have modified the frame contents.
684bf50f18SLuigi Rizzo  *
694f80b14cSVincenzo Maffione  * Several copy or zero-copy monitors may be active on any ring.
704bf50f18SLuigi Rizzo  *
714bf50f18SLuigi Rizzo  */
724bf50f18SLuigi Rizzo 
734bf50f18SLuigi Rizzo 
744bf50f18SLuigi Rizzo #if defined(__FreeBSD__)
754bf50f18SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
764bf50f18SLuigi Rizzo 
774bf50f18SLuigi Rizzo #include <sys/types.h>
784bf50f18SLuigi Rizzo #include <sys/errno.h>
794bf50f18SLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
804bf50f18SLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
814bf50f18SLuigi Rizzo #include <sys/malloc.h>
824bf50f18SLuigi Rizzo #include <sys/poll.h>
834bf50f18SLuigi Rizzo #include <sys/lock.h>
844bf50f18SLuigi Rizzo #include <sys/rwlock.h>
854bf50f18SLuigi Rizzo #include <sys/selinfo.h>
864bf50f18SLuigi Rizzo #include <sys/sysctl.h>
874bf50f18SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
884bf50f18SLuigi Rizzo #include <net/if.h>
894bf50f18SLuigi Rizzo #include <net/if_var.h>
904bf50f18SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
914bf50f18SLuigi Rizzo #include <sys/refcount.h>
924bf50f18SLuigi Rizzo 
934bf50f18SLuigi Rizzo 
944bf50f18SLuigi Rizzo #elif defined(linux)
954bf50f18SLuigi Rizzo 
964bf50f18SLuigi Rizzo #include "bsd_glue.h"
974bf50f18SLuigi Rizzo 
984bf50f18SLuigi Rizzo #elif defined(__APPLE__)
994bf50f18SLuigi Rizzo 
1004bf50f18SLuigi Rizzo #warning OSX support is only partial
1014bf50f18SLuigi Rizzo #include "osx_glue.h"
1024bf50f18SLuigi Rizzo 
10337e3a6d3SLuigi Rizzo #elif defined(_WIN32)
10437e3a6d3SLuigi Rizzo #include "win_glue.h"
1054bf50f18SLuigi Rizzo #else
1064bf50f18SLuigi Rizzo 
1074bf50f18SLuigi Rizzo #error	Unsupported platform
1084bf50f18SLuigi Rizzo 
1094bf50f18SLuigi Rizzo #endif /* unsupported */
1104bf50f18SLuigi Rizzo 
1114bf50f18SLuigi Rizzo /*
1124bf50f18SLuigi Rizzo  * common headers
1134bf50f18SLuigi Rizzo  */
1144bf50f18SLuigi Rizzo 
1154bf50f18SLuigi Rizzo #include <net/netmap.h>
1164bf50f18SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
1174bf50f18SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
1184bf50f18SLuigi Rizzo 
1194bf50f18SLuigi Rizzo #ifdef WITH_MONITOR
1204bf50f18SLuigi Rizzo 
1214bf50f18SLuigi Rizzo #define NM_MONITOR_MAXSLOTS 4096
1224bf50f18SLuigi Rizzo 
123847bf383SLuigi Rizzo /*
124847bf383SLuigi Rizzo  ********************************************************************
125847bf383SLuigi Rizzo  * functions common to both kind of monitors
126847bf383SLuigi Rizzo  ********************************************************************
127847bf383SLuigi Rizzo  */
128847bf383SLuigi Rizzo 
129c3e9b4dbSLuiz Otavio O Souza static int netmap_zmon_reg(struct netmap_adapter *, int);
130c3e9b4dbSLuiz Otavio O Souza static int
131c3e9b4dbSLuiz Otavio O Souza nm_is_zmon(struct netmap_adapter *na)
132c3e9b4dbSLuiz Otavio O Souza {
133c3e9b4dbSLuiz Otavio O Souza 	return na->nm_register == netmap_zmon_reg;
134c3e9b4dbSLuiz Otavio O Souza }
135c3e9b4dbSLuiz Otavio O Souza 
136847bf383SLuigi Rizzo /* nm_sync callback for the monitor's own tx rings.
137847bf383SLuigi Rizzo  * This makes no sense and always returns error
1384bf50f18SLuigi Rizzo  */
1394bf50f18SLuigi Rizzo static int
140847bf383SLuigi Rizzo netmap_monitor_txsync(struct netmap_kring *kring, int flags)
1414bf50f18SLuigi Rizzo {
142847bf383SLuigi Rizzo         RD(1, "%s %x", kring->name, flags);
143847bf383SLuigi Rizzo 	return EIO;
144847bf383SLuigi Rizzo }
145847bf383SLuigi Rizzo 
146847bf383SLuigi Rizzo /* nm_sync callback for the monitor's own rx rings.
147847bf383SLuigi Rizzo  * Note that the lock in netmap_zmon_parent_sync only protects
148847bf383SLuigi Rizzo  * writers among themselves. Synchronization between writers
149847bf383SLuigi Rizzo  * (i.e., netmap_zmon_parent_txsync and netmap_zmon_parent_rxsync)
150847bf383SLuigi Rizzo  * and readers (i.e., netmap_zmon_rxsync) relies on memory barriers.
151847bf383SLuigi Rizzo  */
152847bf383SLuigi Rizzo static int
153847bf383SLuigi Rizzo netmap_monitor_rxsync(struct netmap_kring *kring, int flags)
154847bf383SLuigi Rizzo {
155847bf383SLuigi Rizzo         ND("%s %x", kring->name, flags);
156c3e9b4dbSLuiz Otavio O Souza 	kring->nr_hwcur = kring->rhead;
157847bf383SLuigi Rizzo 	mb();
158847bf383SLuigi Rizzo         return 0;
159847bf383SLuigi Rizzo }
160847bf383SLuigi Rizzo 
161847bf383SLuigi Rizzo /* nm_krings_create callbacks for monitors.
162847bf383SLuigi Rizzo  */
163847bf383SLuigi Rizzo static int
164847bf383SLuigi Rizzo netmap_monitor_krings_create(struct netmap_adapter *na)
165847bf383SLuigi Rizzo {
16637e3a6d3SLuigi Rizzo 	int error = netmap_krings_create(na, 0);
16737e3a6d3SLuigi Rizzo 	if (error)
16837e3a6d3SLuigi Rizzo 		return error;
16937e3a6d3SLuigi Rizzo 	/* override the host rings callbacks */
170*2ff91c17SVincenzo Maffione 	na->tx_rings[na->num_tx_rings]->nm_sync = netmap_monitor_txsync;
171*2ff91c17SVincenzo Maffione 	na->rx_rings[na->num_rx_rings]->nm_sync = netmap_monitor_rxsync;
17237e3a6d3SLuigi Rizzo 	return 0;
173847bf383SLuigi Rizzo }
174847bf383SLuigi Rizzo 
175847bf383SLuigi Rizzo /* nm_krings_delete callback for monitors */
176847bf383SLuigi Rizzo static void
177847bf383SLuigi Rizzo netmap_monitor_krings_delete(struct netmap_adapter *na)
178847bf383SLuigi Rizzo {
179847bf383SLuigi Rizzo 	netmap_krings_delete(na);
180847bf383SLuigi Rizzo }
181847bf383SLuigi Rizzo 
182847bf383SLuigi Rizzo 
183847bf383SLuigi Rizzo static u_int
184847bf383SLuigi Rizzo nm_txrx2flag(enum txrx t)
185847bf383SLuigi Rizzo {
186847bf383SLuigi Rizzo 	return (t == NR_RX ? NR_MONITOR_RX : NR_MONITOR_TX);
187847bf383SLuigi Rizzo }
188847bf383SLuigi Rizzo 
189847bf383SLuigi Rizzo /* allocate the monitors array in the monitored kring */
190847bf383SLuigi Rizzo static int
191847bf383SLuigi Rizzo nm_monitor_alloc(struct netmap_kring *kring, u_int n)
192847bf383SLuigi Rizzo {
193c3e9b4dbSLuiz Otavio O Souza 	size_t old_len, len;
194847bf383SLuigi Rizzo 	struct netmap_kring **nm;
195847bf383SLuigi Rizzo 
196847bf383SLuigi Rizzo 	if (n <= kring->max_monitors)
197847bf383SLuigi Rizzo 		/* we already have more entries that requested */
198847bf383SLuigi Rizzo 		return 0;
199847bf383SLuigi Rizzo 
200c3e9b4dbSLuiz Otavio O Souza 	old_len = sizeof(struct netmap_kring *)*kring->max_monitors;
201847bf383SLuigi Rizzo         len = sizeof(struct netmap_kring *) * n;
202c3e9b4dbSLuiz Otavio O Souza 	nm = nm_os_realloc(kring->monitors, len, old_len);
203847bf383SLuigi Rizzo 	if (nm == NULL)
204847bf383SLuigi Rizzo 		return ENOMEM;
205847bf383SLuigi Rizzo 
206847bf383SLuigi Rizzo 	kring->monitors = nm;
207847bf383SLuigi Rizzo 	kring->max_monitors = n;
208847bf383SLuigi Rizzo 
209847bf383SLuigi Rizzo 	return 0;
210847bf383SLuigi Rizzo }
211847bf383SLuigi Rizzo 
212847bf383SLuigi Rizzo /* deallocate the parent array in the parent adapter */
213847bf383SLuigi Rizzo static void
214847bf383SLuigi Rizzo nm_monitor_dealloc(struct netmap_kring *kring)
215847bf383SLuigi Rizzo {
216847bf383SLuigi Rizzo 	if (kring->monitors) {
217847bf383SLuigi Rizzo 		if (kring->n_monitors > 0) {
218847bf383SLuigi Rizzo 			D("freeing not empty monitor array for %s (%d dangling monitors)!", kring->name,
219847bf383SLuigi Rizzo 					kring->n_monitors);
220847bf383SLuigi Rizzo 		}
221c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(kring->monitors);
222847bf383SLuigi Rizzo 		kring->monitors = NULL;
223847bf383SLuigi Rizzo 		kring->max_monitors = 0;
224847bf383SLuigi Rizzo 		kring->n_monitors = 0;
225847bf383SLuigi Rizzo 	}
226847bf383SLuigi Rizzo }
227847bf383SLuigi Rizzo 
228c3e9b4dbSLuiz Otavio O Souza /* returns 1 iff kring has no monitors */
229c3e9b4dbSLuiz Otavio O Souza static inline int
230c3e9b4dbSLuiz Otavio O Souza nm_monitor_none(struct netmap_kring *kring)
231c3e9b4dbSLuiz Otavio O Souza {
232c3e9b4dbSLuiz Otavio O Souza 	return kring->n_monitors == 0 &&
233c3e9b4dbSLuiz Otavio O Souza 		kring->zmon_list[NR_TX].next == NULL &&
234c3e9b4dbSLuiz Otavio O Souza 		kring->zmon_list[NR_RX].next == NULL;
235c3e9b4dbSLuiz Otavio O Souza }
236c3e9b4dbSLuiz Otavio O Souza 
237847bf383SLuigi Rizzo /*
238847bf383SLuigi Rizzo  * monitors work by replacing the nm_sync() and possibly the
239847bf383SLuigi Rizzo  * nm_notify() callbacks in the monitored rings.
240847bf383SLuigi Rizzo  */
241847bf383SLuigi Rizzo static int netmap_zmon_parent_txsync(struct netmap_kring *, int);
242847bf383SLuigi Rizzo static int netmap_zmon_parent_rxsync(struct netmap_kring *, int);
243847bf383SLuigi Rizzo static int netmap_monitor_parent_txsync(struct netmap_kring *, int);
244847bf383SLuigi Rizzo static int netmap_monitor_parent_rxsync(struct netmap_kring *, int);
245847bf383SLuigi Rizzo static int netmap_monitor_parent_notify(struct netmap_kring *, int);
246847bf383SLuigi Rizzo 
247847bf383SLuigi Rizzo /* add the monitor mkring to the list of monitors of kring.
248847bf383SLuigi Rizzo  * If this is the first monitor, intercept the callbacks
249847bf383SLuigi Rizzo  */
250847bf383SLuigi Rizzo static int
251c3e9b4dbSLuiz Otavio O Souza netmap_monitor_add(struct netmap_kring *mkring, struct netmap_kring *kring, int zmon)
252847bf383SLuigi Rizzo {
25337e3a6d3SLuigi Rizzo 	int error = NM_IRQ_COMPLETED;
254c3e9b4dbSLuiz Otavio O Souza 	enum txrx t = kring->tx;
255c3e9b4dbSLuiz Otavio O Souza 	struct netmap_zmon_list *z = &kring->zmon_list[t];
256c3e9b4dbSLuiz Otavio O Souza 	struct netmap_zmon_list *mz = &mkring->zmon_list[t];
257c3e9b4dbSLuiz Otavio O Souza 
258c3e9b4dbSLuiz Otavio O Souza 	/* a zero-copy monitor which is not the first in the list
259c3e9b4dbSLuiz Otavio O Souza 	 * must monitor the previous monitor
260c3e9b4dbSLuiz Otavio O Souza 	 */
261c3e9b4dbSLuiz Otavio O Souza 	if (zmon && z->prev != NULL)
262c3e9b4dbSLuiz Otavio O Souza 		kring = z->prev;
263847bf383SLuigi Rizzo 
2644f80b14cSVincenzo Maffione 	/* synchronize with concurrently running nm_sync()s */
26537e3a6d3SLuigi Rizzo 	nm_kr_stop(kring, NM_KR_LOCKED);
266c3e9b4dbSLuiz Otavio O Souza 
267c3e9b4dbSLuiz Otavio O Souza 	if (nm_monitor_none(kring)) {
268c3e9b4dbSLuiz Otavio O Souza 		/* this is the first monitor, intercept callbacks */
269c3e9b4dbSLuiz Otavio O Souza 		ND("intercept callbacks on %s", kring->name);
270c3e9b4dbSLuiz Otavio O Souza 		kring->mon_sync = kring->nm_sync;
271c3e9b4dbSLuiz Otavio O Souza 		kring->mon_notify = kring->nm_notify;
272c3e9b4dbSLuiz Otavio O Souza 		if (kring->tx == NR_TX) {
273c3e9b4dbSLuiz Otavio O Souza 			kring->nm_sync = netmap_monitor_parent_txsync;
274c3e9b4dbSLuiz Otavio O Souza 		} else {
275c3e9b4dbSLuiz Otavio O Souza 			kring->nm_sync = netmap_monitor_parent_rxsync;
276c3e9b4dbSLuiz Otavio O Souza 			kring->nm_notify = netmap_monitor_parent_notify;
277c3e9b4dbSLuiz Otavio O Souza 			kring->mon_tail = kring->nr_hwtail;
278c3e9b4dbSLuiz Otavio O Souza 		}
279c3e9b4dbSLuiz Otavio O Souza 	}
280c3e9b4dbSLuiz Otavio O Souza 
281c3e9b4dbSLuiz Otavio O Souza 	if (zmon) {
282c3e9b4dbSLuiz Otavio O Souza 		/* append the zmon to the list */
283c3e9b4dbSLuiz Otavio O Souza 		struct netmap_monitor_adapter *mna =
284c3e9b4dbSLuiz Otavio O Souza 			(struct netmap_monitor_adapter *)mkring->na;
285c3e9b4dbSLuiz Otavio O Souza 		struct netmap_adapter *pna;
286c3e9b4dbSLuiz Otavio O Souza 
287c3e9b4dbSLuiz Otavio O Souza 		if (z->prev != NULL)
288c3e9b4dbSLuiz Otavio O Souza 			z->prev->zmon_list[t].next = mkring;
289c3e9b4dbSLuiz Otavio O Souza 		mz->prev = z->prev;
290c3e9b4dbSLuiz Otavio O Souza 		z->prev = mkring;
291c3e9b4dbSLuiz Otavio O Souza 		if (z->next == NULL)
292c3e9b4dbSLuiz Otavio O Souza 			z->next = mkring;
293c3e9b4dbSLuiz Otavio O Souza 
294c3e9b4dbSLuiz Otavio O Souza 		/* grap a reference to the previous netmap adapter
295c3e9b4dbSLuiz Otavio O Souza 		 * in the chain (this may be the monitored port
296c3e9b4dbSLuiz Otavio O Souza 		 * or another zero-copy monitor)
297c3e9b4dbSLuiz Otavio O Souza 		 */
298c3e9b4dbSLuiz Otavio O Souza 		pna = kring->na;
299c3e9b4dbSLuiz Otavio O Souza 		netmap_adapter_get(pna);
300c3e9b4dbSLuiz Otavio O Souza 		netmap_adapter_put(mna->priv.np_na);
301c3e9b4dbSLuiz Otavio O Souza 		mna->priv.np_na = pna;
302c3e9b4dbSLuiz Otavio O Souza 	} else {
303847bf383SLuigi Rizzo 		/* make sure the monitor array exists and is big enough */
304847bf383SLuigi Rizzo 		error = nm_monitor_alloc(kring, kring->n_monitors + 1);
305847bf383SLuigi Rizzo 		if (error)
306847bf383SLuigi Rizzo 			goto out;
307847bf383SLuigi Rizzo 		kring->monitors[kring->n_monitors] = mkring;
308c3e9b4dbSLuiz Otavio O Souza 		mkring->mon_pos[kring->tx] = kring->n_monitors;
309847bf383SLuigi Rizzo 		kring->n_monitors++;
310847bf383SLuigi Rizzo 	}
311847bf383SLuigi Rizzo 
312847bf383SLuigi Rizzo out:
31337e3a6d3SLuigi Rizzo 	nm_kr_start(kring);
314847bf383SLuigi Rizzo 	return error;
315847bf383SLuigi Rizzo }
316847bf383SLuigi Rizzo 
317847bf383SLuigi Rizzo /* remove the monitor mkring from the list of monitors of kring.
318847bf383SLuigi Rizzo  * If this is the last monitor, restore the original callbacks
319847bf383SLuigi Rizzo  */
320847bf383SLuigi Rizzo static void
321847bf383SLuigi Rizzo netmap_monitor_del(struct netmap_kring *mkring, struct netmap_kring *kring)
322847bf383SLuigi Rizzo {
323c3e9b4dbSLuiz Otavio O Souza 	struct netmap_zmon_list *mz = &mkring->zmon_list[kring->tx];
324c3e9b4dbSLuiz Otavio O Souza 	int zmon = nm_is_zmon(mkring->na);
325c3e9b4dbSLuiz Otavio O Souza 
326c3e9b4dbSLuiz Otavio O Souza 
327c3e9b4dbSLuiz Otavio O Souza 	if (zmon && mz->prev != NULL)
328c3e9b4dbSLuiz Otavio O Souza 		kring = mz->prev;
329c3e9b4dbSLuiz Otavio O Souza 
3304f80b14cSVincenzo Maffione 	/* synchronize with concurrently running nm_sync()s */
33137e3a6d3SLuigi Rizzo 	nm_kr_stop(kring, NM_KR_LOCKED);
332c3e9b4dbSLuiz Otavio O Souza 
333c3e9b4dbSLuiz Otavio O Souza 	if (zmon) {
334c3e9b4dbSLuiz Otavio O Souza 		/* remove the monitor from the list */
335c3e9b4dbSLuiz Otavio O Souza 		if (mz->prev != NULL)
336c3e9b4dbSLuiz Otavio O Souza 			mz->prev->zmon_list[kring->tx].next = mz->next;
337c3e9b4dbSLuiz Otavio O Souza 		else
338c3e9b4dbSLuiz Otavio O Souza 			kring->zmon_list[kring->tx].next = mz->next;
339c3e9b4dbSLuiz Otavio O Souza 		if (mz->next != NULL) {
340c3e9b4dbSLuiz Otavio O Souza 			mz->next->zmon_list[kring->tx].prev = mz->prev;
341c3e9b4dbSLuiz Otavio O Souza 		} else {
342c3e9b4dbSLuiz Otavio O Souza 			kring->zmon_list[kring->tx].prev = mz->prev;
343c3e9b4dbSLuiz Otavio O Souza 		}
344c3e9b4dbSLuiz Otavio O Souza 	} else {
345c3e9b4dbSLuiz Otavio O Souza 		/* this is a copy monitor */
346c3e9b4dbSLuiz Otavio O Souza 		uint32_t mon_pos = mkring->mon_pos[kring->tx];
347847bf383SLuigi Rizzo 		kring->n_monitors--;
348c3e9b4dbSLuiz Otavio O Souza 		if (mon_pos != kring->n_monitors) {
349c3e9b4dbSLuiz Otavio O Souza 			kring->monitors[mon_pos] =
350c3e9b4dbSLuiz Otavio O Souza 				kring->monitors[kring->n_monitors];
351c3e9b4dbSLuiz Otavio O Souza 			kring->monitors[mon_pos]->mon_pos[kring->tx] = mon_pos;
352847bf383SLuigi Rizzo 		}
353847bf383SLuigi Rizzo 		kring->monitors[kring->n_monitors] = NULL;
354847bf383SLuigi Rizzo 		if (kring->n_monitors == 0) {
355c3e9b4dbSLuiz Otavio O Souza 			nm_monitor_dealloc(kring);
356c3e9b4dbSLuiz Otavio O Souza 		}
357c3e9b4dbSLuiz Otavio O Souza 	}
358c3e9b4dbSLuiz Otavio O Souza 
359c3e9b4dbSLuiz Otavio O Souza 	if (nm_monitor_none(kring)) {
360c3e9b4dbSLuiz Otavio O Souza 		/* this was the last monitor, restore the callbacks */
361c3e9b4dbSLuiz Otavio O Souza 		ND("%s: restoring sync on %s: %p", mkring->name, kring->name,
362c3e9b4dbSLuiz Otavio O Souza 				kring->mon_sync);
363847bf383SLuigi Rizzo 		kring->nm_sync = kring->mon_sync;
364847bf383SLuigi Rizzo 		kring->mon_sync = NULL;
365847bf383SLuigi Rizzo 		if (kring->tx == NR_RX) {
36637e3a6d3SLuigi Rizzo 			ND("%s: restoring notify on %s: %p",
367847bf383SLuigi Rizzo 					mkring->name, kring->name, kring->mon_notify);
368847bf383SLuigi Rizzo 			kring->nm_notify = kring->mon_notify;
369847bf383SLuigi Rizzo 			kring->mon_notify = NULL;
370847bf383SLuigi Rizzo 		}
371847bf383SLuigi Rizzo 	}
372c3e9b4dbSLuiz Otavio O Souza 
37337e3a6d3SLuigi Rizzo 	nm_kr_start(kring);
374847bf383SLuigi Rizzo }
375847bf383SLuigi Rizzo 
376847bf383SLuigi Rizzo 
377847bf383SLuigi Rizzo /* This is called when the monitored adapter leaves netmap mode
378847bf383SLuigi Rizzo  * (see netmap_do_unregif).
379847bf383SLuigi Rizzo  * We need to notify the monitors that the monitored rings are gone.
380847bf383SLuigi Rizzo  * We do this by setting their mna->priv.np_na to NULL.
381847bf383SLuigi Rizzo  * Note that the rings are already stopped when this happens, so
382847bf383SLuigi Rizzo  * no monitor ring callback can be active.
383847bf383SLuigi Rizzo  */
384847bf383SLuigi Rizzo void
385847bf383SLuigi Rizzo netmap_monitor_stop(struct netmap_adapter *na)
386847bf383SLuigi Rizzo {
387847bf383SLuigi Rizzo 	enum txrx t;
388847bf383SLuigi Rizzo 
389847bf383SLuigi Rizzo 	for_rx_tx(t) {
390847bf383SLuigi Rizzo 		u_int i;
391847bf383SLuigi Rizzo 
39237e3a6d3SLuigi Rizzo 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
393*2ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
394c3e9b4dbSLuiz Otavio O Souza 			struct netmap_kring *zkring;
395847bf383SLuigi Rizzo 			u_int j;
396847bf383SLuigi Rizzo 
397847bf383SLuigi Rizzo 			for (j = 0; j < kring->n_monitors; j++) {
398847bf383SLuigi Rizzo 				struct netmap_kring *mkring =
399847bf383SLuigi Rizzo 					kring->monitors[j];
400847bf383SLuigi Rizzo 				struct netmap_monitor_adapter *mna =
401847bf383SLuigi Rizzo 					(struct netmap_monitor_adapter *)mkring->na;
402847bf383SLuigi Rizzo 				/* forget about this adapter */
403c3e9b4dbSLuiz Otavio O Souza 				if (mna->priv.np_na != NULL) {
40405f76057SLuigi Rizzo 					netmap_adapter_put(mna->priv.np_na);
405847bf383SLuigi Rizzo 					mna->priv.np_na = NULL;
406847bf383SLuigi Rizzo 				}
407847bf383SLuigi Rizzo 			}
408c3e9b4dbSLuiz Otavio O Souza 
409c3e9b4dbSLuiz Otavio O Souza 			zkring = kring->zmon_list[kring->tx].next;
410c3e9b4dbSLuiz Otavio O Souza 			if (zkring != NULL) {
411c3e9b4dbSLuiz Otavio O Souza 				struct netmap_monitor_adapter *next =
412c3e9b4dbSLuiz Otavio O Souza 					(struct netmap_monitor_adapter *)zkring->na;
413c3e9b4dbSLuiz Otavio O Souza 				struct netmap_monitor_adapter *this =
414c3e9b4dbSLuiz Otavio O Souza 						(struct netmap_monitor_adapter *)na;
415c3e9b4dbSLuiz Otavio O Souza 				struct netmap_adapter *pna = this->priv.np_na;
416c3e9b4dbSLuiz Otavio O Souza 				/* let the next monitor forget about us */
417c3e9b4dbSLuiz Otavio O Souza 				if (next->priv.np_na != NULL) {
418c3e9b4dbSLuiz Otavio O Souza 					netmap_adapter_put(next->priv.np_na);
419c3e9b4dbSLuiz Otavio O Souza 				}
420c3e9b4dbSLuiz Otavio O Souza 				if (pna != NULL && nm_is_zmon(na)) {
421c3e9b4dbSLuiz Otavio O Souza 					/* we are a monitor ourselves and we may
422c3e9b4dbSLuiz Otavio O Souza 					 * need to pass down the reference to
423c3e9b4dbSLuiz Otavio O Souza 					 * the previous adapter in the chain
424c3e9b4dbSLuiz Otavio O Souza 					 */
425c3e9b4dbSLuiz Otavio O Souza 					netmap_adapter_get(pna);
426c3e9b4dbSLuiz Otavio O Souza 					next->priv.np_na = pna;
427c3e9b4dbSLuiz Otavio O Souza 					continue;
428c3e9b4dbSLuiz Otavio O Souza 				}
429c3e9b4dbSLuiz Otavio O Souza 				next->priv.np_na = NULL;
430c3e9b4dbSLuiz Otavio O Souza 			}
431c3e9b4dbSLuiz Otavio O Souza 		}
432847bf383SLuigi Rizzo 	}
433847bf383SLuigi Rizzo }
434847bf383SLuigi Rizzo 
435847bf383SLuigi Rizzo 
436847bf383SLuigi Rizzo /* common functions for the nm_register() callbacks of both kind of
437847bf383SLuigi Rizzo  * monitors.
438847bf383SLuigi Rizzo  */
439847bf383SLuigi Rizzo static int
440847bf383SLuigi Rizzo netmap_monitor_reg_common(struct netmap_adapter *na, int onoff, int zmon)
441847bf383SLuigi Rizzo {
442847bf383SLuigi Rizzo 	struct netmap_monitor_adapter *mna =
443847bf383SLuigi Rizzo 		(struct netmap_monitor_adapter *)na;
444847bf383SLuigi Rizzo 	struct netmap_priv_d *priv = &mna->priv;
445847bf383SLuigi Rizzo 	struct netmap_adapter *pna = priv->np_na;
446847bf383SLuigi Rizzo 	struct netmap_kring *kring, *mkring;
447847bf383SLuigi Rizzo 	int i;
448c3e9b4dbSLuiz Otavio O Souza 	enum txrx t, s;
449847bf383SLuigi Rizzo 
450847bf383SLuigi Rizzo 	ND("%p: onoff %d", na, onoff);
451847bf383SLuigi Rizzo 	if (onoff) {
452847bf383SLuigi Rizzo 		if (pna == NULL) {
453847bf383SLuigi Rizzo 			/* parent left netmap mode, fatal */
454847bf383SLuigi Rizzo 			D("%s: internal error", na->name);
455847bf383SLuigi Rizzo 			return ENXIO;
456847bf383SLuigi Rizzo 		}
457847bf383SLuigi Rizzo 		for_rx_tx(t) {
458c3e9b4dbSLuiz Otavio O Souza 			for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
459*2ff91c17SVincenzo Maffione 				mkring = NMR(na, t)[i];
460c3e9b4dbSLuiz Otavio O Souza 				if (!nm_kring_pending_on(mkring))
461c3e9b4dbSLuiz Otavio O Souza 					continue;
46237e3a6d3SLuigi Rizzo 				mkring->nr_mode = NKR_NETMAP_ON;
463c3e9b4dbSLuiz Otavio O Souza 				if (t == NR_TX)
464c3e9b4dbSLuiz Otavio O Souza 					continue;
465c3e9b4dbSLuiz Otavio O Souza 				for_rx_tx(s) {
466c3e9b4dbSLuiz Otavio O Souza 					if (i > nma_get_nrings(pna, s))
467c3e9b4dbSLuiz Otavio O Souza 						continue;
468c3e9b4dbSLuiz Otavio O Souza 					if (mna->flags & nm_txrx2flag(s)) {
469*2ff91c17SVincenzo Maffione 						kring = NMR(pna, s)[i];
470c3e9b4dbSLuiz Otavio O Souza 						netmap_monitor_add(mkring, kring, zmon);
47137e3a6d3SLuigi Rizzo 					}
472847bf383SLuigi Rizzo 				}
473847bf383SLuigi Rizzo 			}
474847bf383SLuigi Rizzo 		}
475847bf383SLuigi Rizzo 		na->na_flags |= NAF_NETMAP_ON;
476847bf383SLuigi Rizzo 	} else {
47737e3a6d3SLuigi Rizzo 		if (na->active_fds == 0)
478847bf383SLuigi Rizzo 			na->na_flags &= ~NAF_NETMAP_ON;
479847bf383SLuigi Rizzo 		for_rx_tx(t) {
480c3e9b4dbSLuiz Otavio O Souza 			for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
481*2ff91c17SVincenzo Maffione 				mkring = NMR(na, t)[i];
482c3e9b4dbSLuiz Otavio O Souza 				if (!nm_kring_pending_off(mkring))
483c3e9b4dbSLuiz Otavio O Souza 					continue;
48437e3a6d3SLuigi Rizzo 				mkring->nr_mode = NKR_NETMAP_OFF;
485c3e9b4dbSLuiz Otavio O Souza 				if (t == NR_TX)
486c3e9b4dbSLuiz Otavio O Souza 					continue;
48737e3a6d3SLuigi Rizzo 				/* we cannot access the parent krings if the parent
48837e3a6d3SLuigi Rizzo 				 * has left netmap mode. This is signaled by a NULL
48937e3a6d3SLuigi Rizzo 				 * pna pointer
49037e3a6d3SLuigi Rizzo 				 */
491c3e9b4dbSLuiz Otavio O Souza 				if (pna == NULL)
492c3e9b4dbSLuiz Otavio O Souza 					continue;
493c3e9b4dbSLuiz Otavio O Souza 				for_rx_tx(s) {
494c3e9b4dbSLuiz Otavio O Souza 					if (i > nma_get_nrings(pna, s))
495c3e9b4dbSLuiz Otavio O Souza 						continue;
496c3e9b4dbSLuiz Otavio O Souza 					if (mna->flags & nm_txrx2flag(s)) {
497*2ff91c17SVincenzo Maffione 						kring = NMR(pna, s)[i];
498847bf383SLuigi Rizzo 						netmap_monitor_del(mkring, kring);
499847bf383SLuigi Rizzo 					}
500847bf383SLuigi Rizzo 				}
501847bf383SLuigi Rizzo 			}
502847bf383SLuigi Rizzo 		}
50337e3a6d3SLuigi Rizzo 	}
504847bf383SLuigi Rizzo 	return 0;
505847bf383SLuigi Rizzo }
506847bf383SLuigi Rizzo 
507847bf383SLuigi Rizzo /*
508847bf383SLuigi Rizzo  ****************************************************************
509847bf383SLuigi Rizzo  * functions specific for zero-copy monitors
510847bf383SLuigi Rizzo  ****************************************************************
511847bf383SLuigi Rizzo  */
512847bf383SLuigi Rizzo 
513847bf383SLuigi Rizzo /*
514847bf383SLuigi Rizzo  * Common function for both zero-copy tx and rx nm_sync()
515847bf383SLuigi Rizzo  * callbacks
516847bf383SLuigi Rizzo  */
517847bf383SLuigi Rizzo static int
518847bf383SLuigi Rizzo netmap_zmon_parent_sync(struct netmap_kring *kring, int flags, enum txrx tx)
519847bf383SLuigi Rizzo {
520c3e9b4dbSLuiz Otavio O Souza 	struct netmap_kring *mkring = kring->zmon_list[tx].next;
521847bf383SLuigi Rizzo 	struct netmap_ring *ring = kring->ring, *mring;
522847bf383SLuigi Rizzo 	int error = 0;
523847bf383SLuigi Rizzo 	int rel_slots, free_slots, busy, sent = 0;
5244bf50f18SLuigi Rizzo 	u_int beg, end, i;
5254bf50f18SLuigi Rizzo 	u_int lim = kring->nkr_num_slots - 1,
526847bf383SLuigi Rizzo 	      mlim; // = mkring->nkr_num_slots - 1;
527847bf383SLuigi Rizzo 
528847bf383SLuigi Rizzo 	if (mkring == NULL) {
529847bf383SLuigi Rizzo 		RD(5, "NULL monitor on %s", kring->name);
530847bf383SLuigi Rizzo 		return 0;
531847bf383SLuigi Rizzo 	}
532847bf383SLuigi Rizzo 	mring = mkring->ring;
5334bf50f18SLuigi Rizzo 	mlim = mkring->nkr_num_slots - 1;
5344bf50f18SLuigi Rizzo 
5354bf50f18SLuigi Rizzo 	/* get the relased slots (rel_slots) */
536847bf383SLuigi Rizzo 	if (tx == NR_TX) {
537c3e9b4dbSLuiz Otavio O Souza 		beg = kring->nr_hwtail + 1;
538847bf383SLuigi Rizzo 		error = kring->mon_sync(kring, flags);
5394bf50f18SLuigi Rizzo 		if (error)
5404bf50f18SLuigi Rizzo 			return error;
541c3e9b4dbSLuiz Otavio O Souza 		end = kring->nr_hwtail + 1;
542847bf383SLuigi Rizzo 	} else { /* NR_RX */
543847bf383SLuigi Rizzo 		beg = kring->nr_hwcur;
544847bf383SLuigi Rizzo 		end = kring->rhead;
545847bf383SLuigi Rizzo 	}
546847bf383SLuigi Rizzo 
5474bf50f18SLuigi Rizzo 	rel_slots = end - beg;
5484bf50f18SLuigi Rizzo 	if (rel_slots < 0)
5494bf50f18SLuigi Rizzo 		rel_slots += kring->nkr_num_slots;
5504bf50f18SLuigi Rizzo 
5514bf50f18SLuigi Rizzo 	if (!rel_slots) {
552847bf383SLuigi Rizzo 		/* no released slots, but we still need
553847bf383SLuigi Rizzo 		 * to call rxsync if this is a rx ring
554847bf383SLuigi Rizzo 		 */
555847bf383SLuigi Rizzo 		goto out_rxsync;
5564bf50f18SLuigi Rizzo 	}
5574bf50f18SLuigi Rizzo 
5584bf50f18SLuigi Rizzo 	/* we need to lock the monitor receive ring, since it
5594bf50f18SLuigi Rizzo 	 * is the target of bot tx and rx traffic from the monitored
5604bf50f18SLuigi Rizzo 	 * adapter
5614bf50f18SLuigi Rizzo 	 */
5624bf50f18SLuigi Rizzo 	mtx_lock(&mkring->q_lock);
5634bf50f18SLuigi Rizzo 	/* get the free slots available on the monitor ring */
5644bf50f18SLuigi Rizzo 	i = mkring->nr_hwtail;
5654bf50f18SLuigi Rizzo 	busy = i - mkring->nr_hwcur;
5664bf50f18SLuigi Rizzo 	if (busy < 0)
5674bf50f18SLuigi Rizzo 		busy += mkring->nkr_num_slots;
5684bf50f18SLuigi Rizzo 	free_slots = mlim - busy;
5694bf50f18SLuigi Rizzo 
570847bf383SLuigi Rizzo 	if (!free_slots)
571847bf383SLuigi Rizzo 		goto out;
5724bf50f18SLuigi Rizzo 
5734bf50f18SLuigi Rizzo 	/* swap min(free_slots, rel_slots) slots */
5744bf50f18SLuigi Rizzo 	if (free_slots < rel_slots) {
5754bf50f18SLuigi Rizzo 		beg += (rel_slots - free_slots);
5764bf50f18SLuigi Rizzo 		rel_slots = free_slots;
5774bf50f18SLuigi Rizzo 	}
578c3e9b4dbSLuiz Otavio O Souza 	if (unlikely(beg >= kring->nkr_num_slots))
579c3e9b4dbSLuiz Otavio O Souza 		beg -= kring->nkr_num_slots;
5804bf50f18SLuigi Rizzo 
581847bf383SLuigi Rizzo 	sent = rel_slots;
5824bf50f18SLuigi Rizzo 	for ( ; rel_slots; rel_slots--) {
5834bf50f18SLuigi Rizzo 		struct netmap_slot *s = &ring->slot[beg];
5844bf50f18SLuigi Rizzo 		struct netmap_slot *ms = &mring->slot[i];
5854bf50f18SLuigi Rizzo 		uint32_t tmp;
5864bf50f18SLuigi Rizzo 
5874bf50f18SLuigi Rizzo 		tmp = ms->buf_idx;
5884bf50f18SLuigi Rizzo 		ms->buf_idx = s->buf_idx;
5894bf50f18SLuigi Rizzo 		s->buf_idx = tmp;
590847bf383SLuigi Rizzo 		ND(5, "beg %d buf_idx %d", beg, tmp);
5914bf50f18SLuigi Rizzo 
5924bf50f18SLuigi Rizzo 		tmp = ms->len;
5934bf50f18SLuigi Rizzo 		ms->len = s->len;
5944bf50f18SLuigi Rizzo 		s->len = tmp;
5954bf50f18SLuigi Rizzo 
5964bf50f18SLuigi Rizzo 		s->flags |= NS_BUF_CHANGED;
5974bf50f18SLuigi Rizzo 
5984bf50f18SLuigi Rizzo 		beg = nm_next(beg, lim);
5994bf50f18SLuigi Rizzo 		i = nm_next(i, mlim);
6004bf50f18SLuigi Rizzo 
6014bf50f18SLuigi Rizzo 	}
602ad15cc59SLuigi Rizzo 	mb();
6034bf50f18SLuigi Rizzo 	mkring->nr_hwtail = i;
6044bf50f18SLuigi Rizzo 
605847bf383SLuigi Rizzo out:
6064bf50f18SLuigi Rizzo 	mtx_unlock(&mkring->q_lock);
607847bf383SLuigi Rizzo 
608847bf383SLuigi Rizzo 	if (sent) {
6094bf50f18SLuigi Rizzo 		/* notify the new frames to the monitor */
610847bf383SLuigi Rizzo 		mkring->nm_notify(mkring, 0);
611847bf383SLuigi Rizzo 	}
612847bf383SLuigi Rizzo 
613847bf383SLuigi Rizzo out_rxsync:
614847bf383SLuigi Rizzo 	if (tx == NR_RX)
615847bf383SLuigi Rizzo 		error = kring->mon_sync(kring, flags);
616847bf383SLuigi Rizzo 
617847bf383SLuigi Rizzo 	return error;
618847bf383SLuigi Rizzo }
619847bf383SLuigi Rizzo 
620847bf383SLuigi Rizzo /* callback used to replace the nm_sync callback in the monitored tx rings */
621847bf383SLuigi Rizzo static int
622847bf383SLuigi Rizzo netmap_zmon_parent_txsync(struct netmap_kring *kring, int flags)
623847bf383SLuigi Rizzo {
624847bf383SLuigi Rizzo         return netmap_zmon_parent_sync(kring, flags, NR_TX);
625847bf383SLuigi Rizzo }
626847bf383SLuigi Rizzo 
627847bf383SLuigi Rizzo /* callback used to replace the nm_sync callback in the monitored rx rings */
628847bf383SLuigi Rizzo static int
629847bf383SLuigi Rizzo netmap_zmon_parent_rxsync(struct netmap_kring *kring, int flags)
630847bf383SLuigi Rizzo {
631847bf383SLuigi Rizzo         return netmap_zmon_parent_sync(kring, flags, NR_RX);
632847bf383SLuigi Rizzo }
633847bf383SLuigi Rizzo 
634847bf383SLuigi Rizzo static int
635847bf383SLuigi Rizzo netmap_zmon_reg(struct netmap_adapter *na, int onoff)
636847bf383SLuigi Rizzo {
637847bf383SLuigi Rizzo 	return netmap_monitor_reg_common(na, onoff, 1 /* zcopy */);
638847bf383SLuigi Rizzo }
639847bf383SLuigi Rizzo 
640847bf383SLuigi Rizzo /* nm_dtor callback for monitors */
641847bf383SLuigi Rizzo static void
642847bf383SLuigi Rizzo netmap_zmon_dtor(struct netmap_adapter *na)
643847bf383SLuigi Rizzo {
644847bf383SLuigi Rizzo 	struct netmap_monitor_adapter *mna =
645847bf383SLuigi Rizzo 		(struct netmap_monitor_adapter *)na;
646847bf383SLuigi Rizzo 	struct netmap_priv_d *priv = &mna->priv;
647847bf383SLuigi Rizzo 	struct netmap_adapter *pna = priv->np_na;
648847bf383SLuigi Rizzo 
649847bf383SLuigi Rizzo 	netmap_adapter_put(pna);
650847bf383SLuigi Rizzo }
651847bf383SLuigi Rizzo 
652847bf383SLuigi Rizzo /*
653847bf383SLuigi Rizzo  ****************************************************************
654847bf383SLuigi Rizzo  * functions specific for copy monitors
655847bf383SLuigi Rizzo  ****************************************************************
656847bf383SLuigi Rizzo  */
657847bf383SLuigi Rizzo 
658847bf383SLuigi Rizzo static void
659847bf383SLuigi Rizzo netmap_monitor_parent_sync(struct netmap_kring *kring, u_int first_new, int new_slots)
660847bf383SLuigi Rizzo {
661847bf383SLuigi Rizzo 	u_int j;
662847bf383SLuigi Rizzo 
663847bf383SLuigi Rizzo 	for (j = 0; j < kring->n_monitors; j++) {
664847bf383SLuigi Rizzo 		struct netmap_kring *mkring = kring->monitors[j];
665847bf383SLuigi Rizzo 		u_int i, mlim, beg;
666847bf383SLuigi Rizzo 		int free_slots, busy, sent = 0, m;
667847bf383SLuigi Rizzo 		u_int lim = kring->nkr_num_slots - 1;
668847bf383SLuigi Rizzo 		struct netmap_ring *ring = kring->ring, *mring = mkring->ring;
669847bf383SLuigi Rizzo 		u_int max_len = NETMAP_BUF_SIZE(mkring->na);
670847bf383SLuigi Rizzo 
671847bf383SLuigi Rizzo 		mlim = mkring->nkr_num_slots - 1;
672847bf383SLuigi Rizzo 
673847bf383SLuigi Rizzo 		/* we need to lock the monitor receive ring, since it
674847bf383SLuigi Rizzo 		 * is the target of bot tx and rx traffic from the monitored
675847bf383SLuigi Rizzo 		 * adapter
676847bf383SLuigi Rizzo 		 */
677847bf383SLuigi Rizzo 		mtx_lock(&mkring->q_lock);
678847bf383SLuigi Rizzo 		/* get the free slots available on the monitor ring */
679847bf383SLuigi Rizzo 		i = mkring->nr_hwtail;
680847bf383SLuigi Rizzo 		busy = i - mkring->nr_hwcur;
681847bf383SLuigi Rizzo 		if (busy < 0)
682847bf383SLuigi Rizzo 			busy += mkring->nkr_num_slots;
683847bf383SLuigi Rizzo 		free_slots = mlim - busy;
684847bf383SLuigi Rizzo 
685847bf383SLuigi Rizzo 		if (!free_slots)
686847bf383SLuigi Rizzo 			goto out;
687847bf383SLuigi Rizzo 
688847bf383SLuigi Rizzo 		/* copy min(free_slots, new_slots) slots */
689847bf383SLuigi Rizzo 		m = new_slots;
690847bf383SLuigi Rizzo 		beg = first_new;
691847bf383SLuigi Rizzo 		if (free_slots < m) {
692847bf383SLuigi Rizzo 			beg += (m - free_slots);
693847bf383SLuigi Rizzo 			if (beg >= kring->nkr_num_slots)
694847bf383SLuigi Rizzo 				beg -= kring->nkr_num_slots;
695847bf383SLuigi Rizzo 			m = free_slots;
696847bf383SLuigi Rizzo 		}
697847bf383SLuigi Rizzo 
698847bf383SLuigi Rizzo 		for ( ; m; m--) {
699847bf383SLuigi Rizzo 			struct netmap_slot *s = &ring->slot[beg];
700847bf383SLuigi Rizzo 			struct netmap_slot *ms = &mring->slot[i];
701847bf383SLuigi Rizzo 			u_int copy_len = s->len;
702847bf383SLuigi Rizzo 			char *src = NMB(kring->na, s),
703847bf383SLuigi Rizzo 			     *dst = NMB(mkring->na, ms);
704847bf383SLuigi Rizzo 
705847bf383SLuigi Rizzo 			if (unlikely(copy_len > max_len)) {
706847bf383SLuigi Rizzo 				RD(5, "%s->%s: truncating %d to %d", kring->name,
707847bf383SLuigi Rizzo 						mkring->name, copy_len, max_len);
708847bf383SLuigi Rizzo 				copy_len = max_len;
709847bf383SLuigi Rizzo 			}
710847bf383SLuigi Rizzo 
711847bf383SLuigi Rizzo 			memcpy(dst, src, copy_len);
712847bf383SLuigi Rizzo 			ms->len = copy_len;
713847bf383SLuigi Rizzo 			sent++;
714847bf383SLuigi Rizzo 
715847bf383SLuigi Rizzo 			beg = nm_next(beg, lim);
716847bf383SLuigi Rizzo 			i = nm_next(i, mlim);
717847bf383SLuigi Rizzo 		}
718847bf383SLuigi Rizzo 		mb();
719847bf383SLuigi Rizzo 		mkring->nr_hwtail = i;
720847bf383SLuigi Rizzo 	out:
721847bf383SLuigi Rizzo 		mtx_unlock(&mkring->q_lock);
722847bf383SLuigi Rizzo 
723847bf383SLuigi Rizzo 		if (sent) {
724847bf383SLuigi Rizzo 			/* notify the new frames to the monitor */
725847bf383SLuigi Rizzo 			mkring->nm_notify(mkring, 0);
726847bf383SLuigi Rizzo 		}
727847bf383SLuigi Rizzo 	}
7284bf50f18SLuigi Rizzo }
7294bf50f18SLuigi Rizzo 
7304bf50f18SLuigi Rizzo /* callback used to replace the nm_sync callback in the monitored tx rings */
7314bf50f18SLuigi Rizzo static int
7324bf50f18SLuigi Rizzo netmap_monitor_parent_txsync(struct netmap_kring *kring, int flags)
7334bf50f18SLuigi Rizzo {
734847bf383SLuigi Rizzo 	u_int first_new;
735847bf383SLuigi Rizzo 	int new_slots;
736847bf383SLuigi Rizzo 
737847bf383SLuigi Rizzo 	/* get the new slots */
738c3e9b4dbSLuiz Otavio O Souza 	if (kring->n_monitors > 0) {
739847bf383SLuigi Rizzo 		first_new = kring->nr_hwcur;
740847bf383SLuigi Rizzo 		new_slots = kring->rhead - first_new;
741847bf383SLuigi Rizzo 		if (new_slots < 0)
742847bf383SLuigi Rizzo 			new_slots += kring->nkr_num_slots;
743847bf383SLuigi Rizzo 		if (new_slots)
744847bf383SLuigi Rizzo 			netmap_monitor_parent_sync(kring, first_new, new_slots);
745c3e9b4dbSLuiz Otavio O Souza 	}
746c3e9b4dbSLuiz Otavio O Souza 	if (kring->zmon_list[NR_TX].next != NULL) {
747c3e9b4dbSLuiz Otavio O Souza 		return netmap_zmon_parent_txsync(kring, flags);
748c3e9b4dbSLuiz Otavio O Souza 	}
749847bf383SLuigi Rizzo 	return kring->mon_sync(kring, flags);
7504bf50f18SLuigi Rizzo }
7514bf50f18SLuigi Rizzo 
7524bf50f18SLuigi Rizzo /* callback used to replace the nm_sync callback in the monitored rx rings */
7534bf50f18SLuigi Rizzo static int
7544bf50f18SLuigi Rizzo netmap_monitor_parent_rxsync(struct netmap_kring *kring, int flags)
7554bf50f18SLuigi Rizzo {
756847bf383SLuigi Rizzo 	u_int first_new;
757847bf383SLuigi Rizzo 	int new_slots, error;
7584bf50f18SLuigi Rizzo 
759847bf383SLuigi Rizzo 	/* get the new slots */
760c3e9b4dbSLuiz Otavio O Souza 	if (kring->zmon_list[NR_RX].next != NULL) {
761c3e9b4dbSLuiz Otavio O Souza 		error = netmap_zmon_parent_rxsync(kring, flags);
762c3e9b4dbSLuiz Otavio O Souza 	} else {
763847bf383SLuigi Rizzo 		error =  kring->mon_sync(kring, flags);
764c3e9b4dbSLuiz Otavio O Souza 	}
765847bf383SLuigi Rizzo 	if (error)
766847bf383SLuigi Rizzo 		return error;
767c3e9b4dbSLuiz Otavio O Souza 	if (kring->n_monitors > 0) {
768847bf383SLuigi Rizzo 		first_new = kring->mon_tail;
769847bf383SLuigi Rizzo 		new_slots = kring->nr_hwtail - first_new;
770847bf383SLuigi Rizzo 		if (new_slots < 0)
771847bf383SLuigi Rizzo 			new_slots += kring->nkr_num_slots;
772847bf383SLuigi Rizzo 		if (new_slots)
773847bf383SLuigi Rizzo 			netmap_monitor_parent_sync(kring, first_new, new_slots);
774847bf383SLuigi Rizzo 		kring->mon_tail = kring->nr_hwtail;
775c3e9b4dbSLuiz Otavio O Souza 	}
7764bf50f18SLuigi Rizzo 	return 0;
7774bf50f18SLuigi Rizzo }
7784bf50f18SLuigi Rizzo 
779847bf383SLuigi Rizzo /* callback used to replace the nm_notify() callback in the monitored rx rings */
7804bf50f18SLuigi Rizzo static int
781847bf383SLuigi Rizzo netmap_monitor_parent_notify(struct netmap_kring *kring, int flags)
7824bf50f18SLuigi Rizzo {
78337e3a6d3SLuigi Rizzo 	int (*notify)(struct netmap_kring*, int);
784847bf383SLuigi Rizzo 	ND(5, "%s %x", kring->name, flags);
785847bf383SLuigi Rizzo 	/* ?xsync callbacks have tryget called by their callers
786847bf383SLuigi Rizzo 	 * (NIOCREGIF and poll()), but here we have to call it
787847bf383SLuigi Rizzo 	 * by ourself
788847bf383SLuigi Rizzo 	 */
78937e3a6d3SLuigi Rizzo 	if (nm_kr_tryget(kring, 0, NULL)) {
79037e3a6d3SLuigi Rizzo 		/* in all cases, just skip the sync */
79137e3a6d3SLuigi Rizzo 		return NM_IRQ_COMPLETED;
79237e3a6d3SLuigi Rizzo 	}
79337e3a6d3SLuigi Rizzo 	if (kring->n_monitors > 0) {
794847bf383SLuigi Rizzo 		netmap_monitor_parent_rxsync(kring, NAF_FORCE_READ);
795c3e9b4dbSLuiz Otavio O Souza 	}
796c3e9b4dbSLuiz Otavio O Souza 	if (nm_monitor_none(kring)) {
79737e3a6d3SLuigi Rizzo 		/* we are no longer monitoring this ring, so both
79837e3a6d3SLuigi Rizzo 		 * mon_sync and mon_notify are NULL
79937e3a6d3SLuigi Rizzo 		 */
80037e3a6d3SLuigi Rizzo 		notify = kring->nm_notify;
801c3e9b4dbSLuiz Otavio O Souza 	} else {
802c3e9b4dbSLuiz Otavio O Souza 		notify = kring->mon_notify;
80337e3a6d3SLuigi Rizzo 	}
804847bf383SLuigi Rizzo 	nm_kr_put(kring);
80537e3a6d3SLuigi Rizzo         return notify(kring, flags);
8064bf50f18SLuigi Rizzo }
8074bf50f18SLuigi Rizzo 
8084bf50f18SLuigi Rizzo 
8094bf50f18SLuigi Rizzo static int
8104bf50f18SLuigi Rizzo netmap_monitor_reg(struct netmap_adapter *na, int onoff)
8114bf50f18SLuigi Rizzo {
812847bf383SLuigi Rizzo 	return netmap_monitor_reg_common(na, onoff, 0 /* no zcopy */);
8134bf50f18SLuigi Rizzo }
8144bf50f18SLuigi Rizzo 
8154bf50f18SLuigi Rizzo static void
8164bf50f18SLuigi Rizzo netmap_monitor_dtor(struct netmap_adapter *na)
8174bf50f18SLuigi Rizzo {
8184bf50f18SLuigi Rizzo 	struct netmap_monitor_adapter *mna =
8194bf50f18SLuigi Rizzo 		(struct netmap_monitor_adapter *)na;
8204bf50f18SLuigi Rizzo 	struct netmap_priv_d *priv = &mna->priv;
8214bf50f18SLuigi Rizzo 	struct netmap_adapter *pna = priv->np_na;
8224bf50f18SLuigi Rizzo 
8234bf50f18SLuigi Rizzo 	netmap_adapter_put(pna);
8244bf50f18SLuigi Rizzo }
8254bf50f18SLuigi Rizzo 
8264bf50f18SLuigi Rizzo 
827*2ff91c17SVincenzo Maffione /* check if req is a request for a monitor adapter that we can satisfy */
8284bf50f18SLuigi Rizzo int
829*2ff91c17SVincenzo Maffione netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
830c3e9b4dbSLuiz Otavio O Souza 			struct netmap_mem_d *nmd, int create)
8314bf50f18SLuigi Rizzo {
832*2ff91c17SVincenzo Maffione 	struct nmreq_register *req = (struct nmreq_register *)hdr->nr_body;
833*2ff91c17SVincenzo Maffione 	struct nmreq_register preq;
8344bf50f18SLuigi Rizzo 	struct netmap_adapter *pna; /* parent adapter */
8354bf50f18SLuigi Rizzo 	struct netmap_monitor_adapter *mna;
83637e3a6d3SLuigi Rizzo 	struct ifnet *ifp = NULL;
837c3e9b4dbSLuiz Otavio O Souza 	int  error;
838*2ff91c17SVincenzo Maffione 	int zcopy = (req->nr_flags & NR_ZCOPY_MON);
839847bf383SLuigi Rizzo 	char monsuff[10] = "";
8404bf50f18SLuigi Rizzo 
841c3e9b4dbSLuiz Otavio O Souza 	if (zcopy) {
842*2ff91c17SVincenzo Maffione 		req->nr_flags |= (NR_MONITOR_TX | NR_MONITOR_RX);
84337e3a6d3SLuigi Rizzo 	}
844*2ff91c17SVincenzo Maffione 	if ((req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX)) == 0) {
8454bf50f18SLuigi Rizzo 		ND("not a monitor");
8464bf50f18SLuigi Rizzo 		return 0;
8474bf50f18SLuigi Rizzo 	}
8484bf50f18SLuigi Rizzo 	/* this is a request for a monitor adapter */
8494bf50f18SLuigi Rizzo 
850*2ff91c17SVincenzo Maffione 	ND("flags %lx", req->nr_flags);
8514bf50f18SLuigi Rizzo 
852*2ff91c17SVincenzo Maffione 	/* First, try to find the adapter that we want to monitor.
853*2ff91c17SVincenzo Maffione 	 * We use the same req, after we have turned off the monitor flags.
8544bf50f18SLuigi Rizzo 	 * In this way we can potentially monitor everything netmap understands,
8554bf50f18SLuigi Rizzo 	 * except other monitors.
8564bf50f18SLuigi Rizzo 	 */
857*2ff91c17SVincenzo Maffione 	memcpy(&preq, req, sizeof(preq));
858*2ff91c17SVincenzo Maffione 	preq.nr_flags &= ~(NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON);
859*2ff91c17SVincenzo Maffione 	hdr->nr_body = (uint64_t)&preq;
860*2ff91c17SVincenzo Maffione 	error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
861*2ff91c17SVincenzo Maffione 	hdr->nr_body = (uint64_t)req;
8624bf50f18SLuigi Rizzo 	if (error) {
8634bf50f18SLuigi Rizzo 		D("parent lookup failed: %d", error);
8644bf50f18SLuigi Rizzo 		return error;
8654bf50f18SLuigi Rizzo 	}
86637e3a6d3SLuigi Rizzo 	ND("found parent: %s", pna->name);
8674bf50f18SLuigi Rizzo 
8684bf50f18SLuigi Rizzo 	if (!nm_netmap_on(pna)) {
8694bf50f18SLuigi Rizzo 		/* parent not in netmap mode */
8704bf50f18SLuigi Rizzo 		/* XXX we can wait for the parent to enter netmap mode,
8714bf50f18SLuigi Rizzo 		 * by intercepting its nm_register callback (2014-03-16)
8724bf50f18SLuigi Rizzo 		 */
8734bf50f18SLuigi Rizzo 		D("%s not in netmap mode", pna->name);
8744bf50f18SLuigi Rizzo 		error = EINVAL;
8754bf50f18SLuigi Rizzo 		goto put_out;
8764bf50f18SLuigi Rizzo 	}
8774bf50f18SLuigi Rizzo 
878c3e9b4dbSLuiz Otavio O Souza 	mna = nm_os_malloc(sizeof(*mna));
879c3e9b4dbSLuiz Otavio O Souza 	if (mna == NULL) {
880c3e9b4dbSLuiz Otavio O Souza 		D("memory error");
881c3e9b4dbSLuiz Otavio O Souza 		error = ENOMEM;
882c3e9b4dbSLuiz Otavio O Souza 		goto put_out;
883c3e9b4dbSLuiz Otavio O Souza 	}
8844bf50f18SLuigi Rizzo 	mna->priv.np_na = pna;
885c3e9b4dbSLuiz Otavio O Souza 
886c3e9b4dbSLuiz Otavio O Souza 	/* grab all the rings we need in the parent */
887*2ff91c17SVincenzo Maffione 	error = netmap_interp_ringid(&mna->priv, req->nr_mode, req->nr_ringid,
888*2ff91c17SVincenzo Maffione 					req->nr_flags);
8894bf50f18SLuigi Rizzo 	if (error) {
8904bf50f18SLuigi Rizzo 		D("ringid error");
891c3e9b4dbSLuiz Otavio O Souza 		goto free_out;
8924bf50f18SLuigi Rizzo 	}
893847bf383SLuigi Rizzo 	if (mna->priv.np_qlast[NR_TX] - mna->priv.np_qfirst[NR_TX] == 1) {
894847bf383SLuigi Rizzo 		snprintf(monsuff, 10, "-%d", mna->priv.np_qfirst[NR_TX]);
895847bf383SLuigi Rizzo 	}
896847bf383SLuigi Rizzo 	snprintf(mna->up.name, sizeof(mna->up.name), "%s%s/%s%s%s", pna->name,
897847bf383SLuigi Rizzo 			monsuff,
898847bf383SLuigi Rizzo 			zcopy ? "z" : "",
899*2ff91c17SVincenzo Maffione 			(req->nr_flags & NR_MONITOR_RX) ? "r" : "",
900*2ff91c17SVincenzo Maffione 			(req->nr_flags & NR_MONITOR_TX) ? "t" : "");
901847bf383SLuigi Rizzo 
902847bf383SLuigi Rizzo 	/* the monitor supports the host rings iff the parent does */
903c3e9b4dbSLuiz Otavio O Souza 	mna->up.na_flags |= (pna->na_flags & NAF_HOST_RINGS);
904847bf383SLuigi Rizzo 	/* a do-nothing txsync: monitors cannot be used to inject packets */
905847bf383SLuigi Rizzo 	mna->up.nm_txsync = netmap_monitor_txsync;
906847bf383SLuigi Rizzo 	mna->up.nm_rxsync = netmap_monitor_rxsync;
9074bf50f18SLuigi Rizzo 	mna->up.nm_krings_create = netmap_monitor_krings_create;
9084bf50f18SLuigi Rizzo 	mna->up.nm_krings_delete = netmap_monitor_krings_delete;
909c3e9b4dbSLuiz Otavio O Souza 	mna->up.num_tx_rings = 1; // XXX what should we do here with chained zmons?
9104bf50f18SLuigi Rizzo 	/* we set the number of our rx_rings to be max(num_rx_rings, num_rx_rings)
9114bf50f18SLuigi Rizzo 	 * in the parent
9124bf50f18SLuigi Rizzo 	 */
9134bf50f18SLuigi Rizzo 	mna->up.num_rx_rings = pna->num_rx_rings;
9144bf50f18SLuigi Rizzo 	if (pna->num_tx_rings > pna->num_rx_rings)
9154bf50f18SLuigi Rizzo 		mna->up.num_rx_rings = pna->num_tx_rings;
9164bf50f18SLuigi Rizzo 	/* by default, the number of slots is the same as in
9174bf50f18SLuigi Rizzo 	 * the parent rings, but the user may ask for a different
9184bf50f18SLuigi Rizzo 	 * number
9194bf50f18SLuigi Rizzo 	 */
920*2ff91c17SVincenzo Maffione 	mna->up.num_tx_desc = req->nr_tx_slots;
9214bf50f18SLuigi Rizzo 	nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
9224bf50f18SLuigi Rizzo 			1, NM_MONITOR_MAXSLOTS, NULL);
923*2ff91c17SVincenzo Maffione 	mna->up.num_rx_desc = req->nr_rx_slots;
9244bf50f18SLuigi Rizzo 	nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
9254bf50f18SLuigi Rizzo 			1, NM_MONITOR_MAXSLOTS, NULL);
926c3e9b4dbSLuiz Otavio O Souza 	if (zcopy) {
927c3e9b4dbSLuiz Otavio O Souza 		mna->up.nm_register = netmap_zmon_reg;
928c3e9b4dbSLuiz Otavio O Souza 		mna->up.nm_dtor = netmap_zmon_dtor;
929c3e9b4dbSLuiz Otavio O Souza 		/* to have zero copy, we need to use the same memory allocator
930c3e9b4dbSLuiz Otavio O Souza 		 * as the monitored port
931c3e9b4dbSLuiz Otavio O Souza 		 */
932c3e9b4dbSLuiz Otavio O Souza 		mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
933c3e9b4dbSLuiz Otavio O Souza 		/* and the allocator cannot be changed */
934c3e9b4dbSLuiz Otavio O Souza 		mna->up.na_flags |= NAF_MEM_OWNER;
935c3e9b4dbSLuiz Otavio O Souza 	} else {
936c3e9b4dbSLuiz Otavio O Souza 		mna->up.nm_register = netmap_monitor_reg;
937c3e9b4dbSLuiz Otavio O Souza 		mna->up.nm_dtor = netmap_monitor_dtor;
938c3e9b4dbSLuiz Otavio O Souza 		mna->up.nm_mem = netmap_mem_private_new(
939c3e9b4dbSLuiz Otavio O Souza 				mna->up.num_tx_rings,
940c3e9b4dbSLuiz Otavio O Souza 				mna->up.num_tx_desc,
941c3e9b4dbSLuiz Otavio O Souza 				mna->up.num_rx_rings,
942c3e9b4dbSLuiz Otavio O Souza 				mna->up.num_rx_desc,
943c3e9b4dbSLuiz Otavio O Souza 				0, /* extra bufs */
944c3e9b4dbSLuiz Otavio O Souza 				0, /* pipes */
945c3e9b4dbSLuiz Otavio O Souza 				&error);
946c3e9b4dbSLuiz Otavio O Souza 		if (mna->up.nm_mem == NULL)
947847bf383SLuigi Rizzo 			goto put_out;
9484bf50f18SLuigi Rizzo 	}
9494bf50f18SLuigi Rizzo 
950c3e9b4dbSLuiz Otavio O Souza 	error = netmap_attach_common(&mna->up);
951c3e9b4dbSLuiz Otavio O Souza 	if (error) {
952c3e9b4dbSLuiz Otavio O Souza 		D("attach_common error");
953c3e9b4dbSLuiz Otavio O Souza 		goto mem_put_out;
954c3e9b4dbSLuiz Otavio O Souza 	}
955c3e9b4dbSLuiz Otavio O Souza 
9564bf50f18SLuigi Rizzo 	/* remember the traffic directions we have to monitor */
957*2ff91c17SVincenzo Maffione 	mna->flags = (req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON));
9584bf50f18SLuigi Rizzo 
9594bf50f18SLuigi Rizzo 	*na = &mna->up;
9604bf50f18SLuigi Rizzo 	netmap_adapter_get(*na);
9614bf50f18SLuigi Rizzo 
9624bf50f18SLuigi Rizzo 	/* keep the reference to the parent */
96337e3a6d3SLuigi Rizzo 	ND("monitor ok");
96437e3a6d3SLuigi Rizzo 
96537e3a6d3SLuigi Rizzo 	/* drop the reference to the ifp, if any */
96637e3a6d3SLuigi Rizzo 	if (ifp)
96737e3a6d3SLuigi Rizzo 		if_rele(ifp);
9684bf50f18SLuigi Rizzo 
9694bf50f18SLuigi Rizzo 	return 0;
9704bf50f18SLuigi Rizzo 
971c3e9b4dbSLuiz Otavio O Souza mem_put_out:
972c3e9b4dbSLuiz Otavio O Souza 	netmap_mem_put(mna->up.nm_mem);
973c3e9b4dbSLuiz Otavio O Souza free_out:
974c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(mna);
9754bf50f18SLuigi Rizzo put_out:
97637e3a6d3SLuigi Rizzo 	netmap_unget_na(pna, ifp);
9774bf50f18SLuigi Rizzo 	return error;
9784bf50f18SLuigi Rizzo }
9794bf50f18SLuigi Rizzo 
9804bf50f18SLuigi Rizzo 
9814bf50f18SLuigi Rizzo #endif /* WITH_MONITOR */
982