2 * Copyright (C) 2014-2016 Giuseppe Lettieri
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * netmap monitors can be used to do monitoring of network traffic
33 * on another adapter, when the latter adapter is working in netmap mode.
35 * Monitors offer to userspace the same interface as any other netmap port,
36 * with as many pairs of netmap rings as the monitored adapter.
37 * However, only the rx rings are actually used. Each monitor rx ring receives
38 * the traffic transiting on both the tx and rx corresponding rings in the
39 * monitored adapter. During registration, the user can choose if she wants
40 * to intercept tx only, rx only, or both tx and rx traffic.
41 * The slots containing traffic intercepted in the tx direction will have
42 * the NS_TXMON flag set.
44 * If the monitor is not able to cope with the stream of frames, excess traffic
47 * If the monitored adapter leaves netmap mode, the monitor has to be restarted.
49 * Monitors can be either zero-copy or copy-based.
51 * Copy monitors see the frames before they are consumed:
53 * - For tx traffic, this is when the application sends them, before they are
54 * passed down to the adapter.
56 * - For rx traffic, this is when they are received by the adapter, before
57 * they are sent up to the application, if any (note that, if no
58 * application is reading from a monitored ring, the ring will eventually
59 * fill up and traffic will stop).
61 * Zero-copy monitors only see the frames after they have been consumed:
63 * - For tx traffic, this is after the slots containing the frames have been
64 * marked as free. Note that this may happen at a considerably delay after
65 * frame transmission, since freeing of slots is often done lazily.
67 * - For rx traffic, this is after the consumer on the monitored adapter
68 * has released them. In most cases, the consumer is a userspace
69 * application which may have modified the frame contents.
71 * Several copy or zero-copy monitors may be active on any ring.
76 #if defined(__FreeBSD__)
77 #include <sys/cdefs.h> /* prerequisite */
79 #include <sys/types.h>
80 #include <sys/errno.h>
81 #include <sys/param.h> /* defines used in kernel.h */
82 #include <sys/kernel.h> /* types used in module initialization */
83 #include <sys/malloc.h>
86 #include <sys/rwlock.h>
87 #include <sys/selinfo.h>
88 #include <sys/sysctl.h>
89 #include <sys/socket.h> /* sockaddrs */
91 #include <net/if_var.h>
92 #include <machine/bus.h> /* bus_dmamap_* */
93 #include <sys/refcount.h>
100 #elif defined(__APPLE__)
102 #warning OSX support is only partial
103 #include "osx_glue.h"
105 #elif defined(_WIN32)
106 #include "win_glue.h"
109 #error Unsupported platform
111 #endif /* unsupported */
117 #include <net/netmap.h>
118 #include <dev/netmap/netmap_kern.h>
119 #include <dev/netmap/netmap_mem2.h>
123 #define NM_MONITOR_MAXSLOTS 4096
126 ********************************************************************
127 * functions common to both kind of monitors
128 ********************************************************************
131 static int netmap_zmon_reg(struct netmap_adapter *, int);
133 nm_is_zmon(struct netmap_adapter *na)
135 return na->nm_register == netmap_zmon_reg;
138 /* nm_sync callback for the monitor's own tx rings.
139 * This makes no sense and always returns error
142 netmap_monitor_txsync(struct netmap_kring *kring, int flags)
144 nm_prlim(1, "%s %x", kring->name, flags);
148 /* nm_sync callback for the monitor's own rx rings.
149 * Note that the lock in netmap_zmon_parent_sync only protects
150 * writers among themselves. Synchronization between writers
151 * (i.e., netmap_zmon_parent_txsync and netmap_zmon_parent_rxsync)
152 * and readers (i.e., netmap_zmon_rxsync) relies on memory barriers.
155 netmap_monitor_rxsync(struct netmap_kring *kring, int flags)
157 struct netmap_monitor_adapter *mna =
158 (struct netmap_monitor_adapter *)kring->na;
159 if (unlikely(mna->priv.np_na == NULL)) {
160 /* parent left netmap mode */
163 nm_prdis("%s %x", kring->name, flags);
164 kring->nr_hwcur = kring->rhead;
169 /* nm_krings_create callbacks for monitors.
172 netmap_monitor_krings_create(struct netmap_adapter *na)
174 int error = netmap_krings_create(na, 0);
179 /* override the host rings callbacks */
182 u_int first = nma_get_nrings(na, t);
183 for (i = 0; i < nma_get_host_nrings(na, t); i++) {
184 struct netmap_kring *kring = NMR(na, t)[first + i];
185 kring->nm_sync = t == NR_TX ? netmap_monitor_txsync :
186 netmap_monitor_rxsync;
192 /* nm_krings_delete callback for monitors */
194 netmap_monitor_krings_delete(struct netmap_adapter *na)
196 netmap_krings_delete(na);
201 nm_txrx2flag(enum txrx t)
203 return (t == NR_RX ? NR_MONITOR_RX : NR_MONITOR_TX);
206 /* allocate the monitors array in the monitored kring */
208 nm_monitor_alloc(struct netmap_kring *kring, u_int n)
211 struct netmap_kring **nm;
213 if (n <= kring->max_monitors)
214 /* we already have more entries that requested */
217 old_len = sizeof(struct netmap_kring *)*kring->max_monitors;
218 len = sizeof(struct netmap_kring *) * n;
219 nm = nm_os_realloc(kring->monitors, len, old_len);
223 kring->monitors = nm;
224 kring->max_monitors = n;
229 /* deallocate the parent array in the parent adapter */
231 nm_monitor_dealloc(struct netmap_kring *kring)
233 if (kring->monitors) {
234 if (kring->n_monitors > 0) {
235 nm_prerr("freeing not empty monitor array for %s (%d dangling monitors)!",
236 kring->name, kring->n_monitors);
238 nm_os_free(kring->monitors);
239 kring->monitors = NULL;
240 kring->max_monitors = 0;
241 kring->n_monitors = 0;
245 /* returns 1 iff kring has no monitors */
247 nm_monitor_none(struct netmap_kring *kring)
249 return kring->n_monitors == 0 &&
250 kring->zmon_list[NR_TX].next == NULL &&
251 kring->zmon_list[NR_RX].next == NULL;
255 * monitors work by replacing the nm_sync() and possibly the
256 * nm_notify() callbacks in the monitored rings.
258 static int netmap_zmon_parent_txsync(struct netmap_kring *, int);
259 static int netmap_zmon_parent_rxsync(struct netmap_kring *, int);
260 static int netmap_monitor_parent_txsync(struct netmap_kring *, int);
261 static int netmap_monitor_parent_rxsync(struct netmap_kring *, int);
262 static int netmap_monitor_parent_notify(struct netmap_kring *, int);
265 nm_monitor_dummycb(struct netmap_kring *kring, int flags)
273 nm_monitor_intercept_callbacks(struct netmap_kring *kring)
275 nm_prdis("intercept callbacks on %s", kring->name);
276 kring->mon_sync = kring->nm_sync != NULL ?
277 kring->nm_sync : nm_monitor_dummycb;
278 kring->mon_notify = kring->nm_notify;
279 if (kring->tx == NR_TX) {
280 kring->nm_sync = netmap_monitor_parent_txsync;
282 kring->nm_sync = netmap_monitor_parent_rxsync;
283 kring->nm_notify = netmap_monitor_parent_notify;
284 kring->mon_tail = kring->nr_hwtail;
289 nm_monitor_restore_callbacks(struct netmap_kring *kring)
291 nm_prdis("restoring callbacks on %s", kring->name);
292 kring->nm_sync = kring->mon_sync;
293 kring->mon_sync = NULL;
294 if (kring->tx == NR_RX) {
295 kring->nm_notify = kring->mon_notify;
297 kring->mon_notify = NULL;
300 static struct netmap_kring *
301 nm_zmon_list_head(struct netmap_kring *mkring, enum txrx t)
303 struct netmap_adapter *na = mkring->na;
304 struct netmap_kring *kring = mkring;
305 struct netmap_zmon_list *z = &kring->zmon_list[t];
306 /* reach the head of the list */
307 while (nm_is_zmon(na) && z->prev != NULL) {
310 z = &kring->zmon_list[t];
312 return nm_is_zmon(na) ? NULL : kring;
315 /* add the monitor mkring to the list of monitors of kring.
316 * If this is the first monitor, intercept the callbacks
319 netmap_monitor_add(struct netmap_kring *mkring, struct netmap_kring *kring, int zmon)
321 int error = NM_IRQ_COMPLETED;
322 enum txrx t = kring->tx;
323 struct netmap_zmon_list *z = &kring->zmon_list[t];
324 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
325 struct netmap_kring *ikring = kring;
327 /* a zero-copy monitor which is not the first in the list
328 * must monitor the previous monitor
330 if (zmon && z->prev != NULL)
331 ikring = z->prev; /* tail of the list */
333 /* synchronize with concurrently running nm_sync()s */
334 nm_kr_stop(kring, NM_KR_LOCKED);
336 if (nm_monitor_none(ikring)) {
337 /* this is the first monitor, intercept the callbacks */
338 nm_prdis("%s: intercept callbacks on %s", mkring->name, ikring->name);
339 nm_monitor_intercept_callbacks(ikring);
343 /* append the zmon to the list */
344 ikring->zmon_list[t].next = mkring;
345 z->prev = mkring; /* new tail */
348 /* grab a reference to the previous netmap adapter
349 * in the chain (this may be the monitored port
350 * or another zero-copy monitor)
352 netmap_adapter_get(ikring->na);
354 /* make sure the monitor array exists and is big enough */
355 error = nm_monitor_alloc(kring, kring->n_monitors + 1);
358 kring->monitors[kring->n_monitors] = mkring;
359 mkring->mon_pos[kring->tx] = kring->n_monitors;
368 /* remove the monitor mkring from the list of monitors of kring.
369 * If this is the last monitor, restore the original callbacks
372 netmap_monitor_del(struct netmap_kring *mkring, struct netmap_kring *kring, enum txrx t)
374 int zmon = nm_is_zmon(mkring->na);
375 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
376 struct netmap_kring *ikring = kring;
380 /* get to the head of the list */
381 kring = nm_zmon_list_head(mkring, t);
385 /* synchronize with concurrently running nm_sync()s
386 * if kring is NULL (orphaned list) the monitored port
387 * has exited netmap mode, so there is nothing to stop
390 nm_kr_stop(kring, NM_KR_LOCKED);
393 /* remove the monitor from the list */
394 if (mz->next != NULL) {
395 mz->next->zmon_list[t].prev = mz->prev;
396 /* we also need to let the next monitor drop the
397 * reference to us and grab the reference to the
398 * previous ring owner, instead
400 if (mz->prev != NULL)
401 netmap_adapter_get(mz->prev->na);
402 netmap_adapter_put(mkring->na);
403 } else if (kring != NULL) {
404 /* in the monitored kring, prev is actually the
405 * pointer to the tail of the list
407 kring->zmon_list[t].prev =
408 (mz->prev != kring ? mz->prev : NULL);
410 if (mz->prev != NULL) {
411 netmap_adapter_put(mz->prev->na);
412 mz->prev->zmon_list[t].next = mz->next;
417 /* this is a copy monitor */
418 uint32_t mon_pos = mkring->mon_pos[kring->tx];
420 if (mon_pos != kring->n_monitors) {
421 kring->monitors[mon_pos] =
422 kring->monitors[kring->n_monitors];
423 kring->monitors[mon_pos]->mon_pos[kring->tx] = mon_pos;
425 kring->monitors[kring->n_monitors] = NULL;
426 if (kring->n_monitors == 0) {
427 nm_monitor_dealloc(kring);
431 if (ikring != NULL && nm_monitor_none(ikring)) {
432 /* this was the last monitor, restore the callbacks */
433 nm_monitor_restore_callbacks(ikring);
441 /* This is called when the monitored adapter leaves netmap mode
442 * (see netmap_do_unregif).
443 * We need to notify the monitors that the monitored rings are gone.
444 * We do this by setting their mna->priv.np_na to NULL.
445 * Note that the rings are already stopped when this happens, so
446 * no monitor ring callback can be active.
449 netmap_monitor_stop(struct netmap_adapter *na)
456 for (i = 0; i < netmap_all_rings(na, t); i++) {
457 struct netmap_kring *kring = NMR(na, t)[i];
458 struct netmap_zmon_list *z = &kring->zmon_list[t];
461 for (j = 0; j < kring->n_monitors; j++) {
462 struct netmap_kring *mkring =
464 struct netmap_monitor_adapter *mna =
465 (struct netmap_monitor_adapter *)mkring->na;
466 /* forget about this adapter */
467 if (mna->priv.np_na != NULL) {
468 netmap_adapter_put(mna->priv.np_na);
469 mna->priv.np_na = NULL;
471 kring->monitors[j] = NULL;
474 if (!nm_is_zmon(na)) {
475 /* we are the head of at most one list */
476 struct netmap_kring *zkring;
477 for (zkring = z->next; zkring != NULL;
478 zkring = zkring->zmon_list[t].next)
480 struct netmap_monitor_adapter *next =
481 (struct netmap_monitor_adapter *)zkring->na;
482 /* let the monitor forget about us */
483 netmap_adapter_put(next->priv.np_na); /* nop if null */
484 next->priv.np_na = NULL;
486 /* orphan the zmon list */
488 z->next->zmon_list[t].prev = NULL;
493 if (!nm_monitor_none(kring)) {
495 kring->n_monitors = 0;
496 nm_monitor_dealloc(kring);
497 nm_monitor_restore_callbacks(kring);
504 /* common functions for the nm_register() callbacks of both kind of
508 netmap_monitor_reg_common(struct netmap_adapter *na, int onoff, int zmon)
510 struct netmap_monitor_adapter *mna =
511 (struct netmap_monitor_adapter *)na;
512 struct netmap_priv_d *priv = &mna->priv;
513 struct netmap_adapter *pna = priv->np_na;
514 struct netmap_kring *kring, *mkring;
518 nm_prdis("%p: onoff %d", na, onoff);
521 /* parent left netmap mode, fatal */
522 nm_prerr("%s: parent left netmap mode", na->name);
526 for (i = 0; i < netmap_all_rings(na, t); i++) {
527 mkring = NMR(na, t)[i];
528 if (!nm_kring_pending_on(mkring))
530 mkring->nr_mode = NKR_NETMAP_ON;
534 if (i > nma_get_nrings(pna, s))
536 if (mna->flags & nm_txrx2flag(s)) {
537 kring = NMR(pna, s)[i];
538 netmap_monitor_add(mkring, kring, zmon);
543 na->na_flags |= NAF_NETMAP_ON;
545 if (na->active_fds == 0)
546 na->na_flags &= ~NAF_NETMAP_ON;
548 for (i = 0; i < netmap_all_rings(na, t); i++) {
549 mkring = NMR(na, t)[i];
550 if (!nm_kring_pending_off(mkring))
552 mkring->nr_mode = NKR_NETMAP_OFF;
555 /* we cannot access the parent krings if the parent
556 * has left netmap mode. This is signaled by a NULL
562 if (i > nma_get_nrings(pna, s))
564 if (mna->flags & nm_txrx2flag(s)) {
565 kring = NMR(pna, s)[i];
566 netmap_monitor_del(mkring, kring, s);
576 ****************************************************************
577 * functions specific for zero-copy monitors
578 ****************************************************************
582 * Common function for both zero-copy tx and rx nm_sync()
586 netmap_zmon_parent_sync(struct netmap_kring *kring, int flags, enum txrx tx)
588 struct netmap_kring *mkring = kring->zmon_list[tx].next;
589 struct netmap_ring *ring = kring->ring, *mring;
591 int rel_slots, free_slots, busy, sent = 0;
593 u_int lim = kring->nkr_num_slots - 1,
594 mlim; // = mkring->nkr_num_slots - 1;
595 uint16_t txmon = kring->tx == NR_TX ? NS_TXMON : 0;
597 if (mkring == NULL) {
598 nm_prlim(5, "NULL monitor on %s", kring->name);
601 mring = mkring->ring;
602 mlim = mkring->nkr_num_slots - 1;
604 /* get the released slots (rel_slots) */
606 beg = kring->nr_hwtail + 1;
607 error = kring->mon_sync(kring, flags);
610 end = kring->nr_hwtail + 1;
612 beg = kring->nr_hwcur;
616 rel_slots = end - beg;
618 rel_slots += kring->nkr_num_slots;
621 /* no released slots, but we still need
622 * to call rxsync if this is a rx ring
627 /* we need to lock the monitor receive ring, since it
628 * is the target of bot tx and rx traffic from the monitored
631 mtx_lock(&mkring->q_lock);
632 /* get the free slots available on the monitor ring */
633 i = mkring->nr_hwtail;
634 busy = i - mkring->nr_hwcur;
636 busy += mkring->nkr_num_slots;
637 free_slots = mlim - busy;
642 /* swap min(free_slots, rel_slots) slots */
643 if (free_slots < rel_slots) {
644 beg += (rel_slots - free_slots);
645 rel_slots = free_slots;
647 if (unlikely(beg >= kring->nkr_num_slots))
648 beg -= kring->nkr_num_slots;
651 for ( ; rel_slots; rel_slots--) {
652 struct netmap_slot *s = &ring->slot[beg];
653 struct netmap_slot *ms = &mring->slot[i];
657 ms->buf_idx = s->buf_idx;
659 nm_prdis(5, "beg %d buf_idx %d", beg, tmp);
665 ms->flags = (s->flags & ~NS_TXMON) | txmon;
666 s->flags |= NS_BUF_CHANGED;
668 beg = nm_next(beg, lim);
669 i = nm_next(i, mlim);
673 mkring->nr_hwtail = i;
676 mtx_unlock(&mkring->q_lock);
679 /* notify the new frames to the monitor */
680 mkring->nm_notify(mkring, 0);
685 error = kring->mon_sync(kring, flags);
690 /* callback used to replace the nm_sync callback in the monitored tx rings */
692 netmap_zmon_parent_txsync(struct netmap_kring *kring, int flags)
694 return netmap_zmon_parent_sync(kring, flags, NR_TX);
697 /* callback used to replace the nm_sync callback in the monitored rx rings */
699 netmap_zmon_parent_rxsync(struct netmap_kring *kring, int flags)
701 return netmap_zmon_parent_sync(kring, flags, NR_RX);
705 netmap_zmon_reg(struct netmap_adapter *na, int onoff)
707 return netmap_monitor_reg_common(na, onoff, 1 /* zcopy */);
710 /* nm_dtor callback for monitors */
712 netmap_zmon_dtor(struct netmap_adapter *na)
714 struct netmap_monitor_adapter *mna =
715 (struct netmap_monitor_adapter *)na;
716 struct netmap_priv_d *priv = &mna->priv;
717 struct netmap_adapter *pna = priv->np_na;
719 netmap_adapter_put(pna);
723 ****************************************************************
724 * functions specific for copy monitors
725 ****************************************************************
729 netmap_monitor_parent_sync(struct netmap_kring *kring, u_int first_new, int new_slots)
732 uint16_t txmon = kring->tx == NR_TX ? NS_TXMON : 0;
734 for (j = 0; j < kring->n_monitors; j++) {
735 struct netmap_kring *mkring = kring->monitors[j];
737 int free_slots, busy, sent = 0, m;
738 u_int lim = kring->nkr_num_slots - 1;
739 struct netmap_ring *ring = kring->ring, *mring = mkring->ring;
740 u_int max_len = NETMAP_BUF_SIZE(mkring->na);
742 mlim = mkring->nkr_num_slots - 1;
744 /* we need to lock the monitor receive ring, since it
745 * is the target of bot tx and rx traffic from the monitored
748 mtx_lock(&mkring->q_lock);
749 /* get the free slots available on the monitor ring */
750 i = mkring->nr_hwtail;
751 busy = i - mkring->nr_hwcur;
753 busy += mkring->nkr_num_slots;
754 free_slots = mlim - busy;
759 /* copy min(free_slots, new_slots) slots */
762 if (free_slots < m) {
763 beg += (m - free_slots);
764 if (beg >= kring->nkr_num_slots)
765 beg -= kring->nkr_num_slots;
770 struct netmap_slot *s = &ring->slot[beg];
771 struct netmap_slot *ms = &mring->slot[i];
772 u_int copy_len = s->len;
773 char *src = NMB(kring->na, s),
774 *dst = NMB(mkring->na, ms);
776 if (unlikely(copy_len > max_len)) {
777 nm_prlim(5, "%s->%s: truncating %d to %d", kring->name,
778 mkring->name, copy_len, max_len);
782 memcpy(dst, src, copy_len);
784 ms->flags = (s->flags & ~NS_TXMON) | txmon;
787 beg = nm_next(beg, lim);
788 i = nm_next(i, mlim);
791 mkring->nr_hwtail = i;
793 mtx_unlock(&mkring->q_lock);
796 /* notify the new frames to the monitor */
797 mkring->nm_notify(mkring, 0);
802 /* callback used to replace the nm_sync callback in the monitored tx rings */
804 netmap_monitor_parent_txsync(struct netmap_kring *kring, int flags)
809 /* get the new slots */
810 if (kring->n_monitors > 0) {
811 first_new = kring->nr_hwcur;
812 new_slots = kring->rhead - first_new;
814 new_slots += kring->nkr_num_slots;
816 netmap_monitor_parent_sync(kring, first_new, new_slots);
818 if (kring->zmon_list[NR_TX].next != NULL) {
819 return netmap_zmon_parent_txsync(kring, flags);
821 return kring->mon_sync(kring, flags);
824 /* callback used to replace the nm_sync callback in the monitored rx rings */
826 netmap_monitor_parent_rxsync(struct netmap_kring *kring, int flags)
829 int new_slots, error;
831 /* get the new slots */
832 if (kring->zmon_list[NR_RX].next != NULL) {
833 error = netmap_zmon_parent_rxsync(kring, flags);
835 error = kring->mon_sync(kring, flags);
839 if (kring->n_monitors > 0) {
840 first_new = kring->mon_tail;
841 new_slots = kring->nr_hwtail - first_new;
843 new_slots += kring->nkr_num_slots;
845 netmap_monitor_parent_sync(kring, first_new, new_slots);
846 kring->mon_tail = kring->nr_hwtail;
851 /* callback used to replace the nm_notify() callback in the monitored rx rings */
853 netmap_monitor_parent_notify(struct netmap_kring *kring, int flags)
855 int (*notify)(struct netmap_kring*, int);
856 nm_prdis(5, "%s %x", kring->name, flags);
857 /* ?xsync callbacks have tryget called by their callers
858 * (NIOCREGIF and poll()), but here we have to call it
861 if (nm_kr_tryget(kring, 0, NULL)) {
862 /* in all cases, just skip the sync */
863 return NM_IRQ_COMPLETED;
865 if (kring->n_monitors > 0) {
866 netmap_monitor_parent_rxsync(kring, NAF_FORCE_READ);
868 if (nm_monitor_none(kring)) {
869 /* we are no longer monitoring this ring, so both
870 * mon_sync and mon_notify are NULL
872 notify = kring->nm_notify;
874 notify = kring->mon_notify;
877 return notify(kring, flags);
882 netmap_monitor_reg(struct netmap_adapter *na, int onoff)
884 return netmap_monitor_reg_common(na, onoff, 0 /* no zcopy */);
888 netmap_monitor_dtor(struct netmap_adapter *na)
890 struct netmap_monitor_adapter *mna =
891 (struct netmap_monitor_adapter *)na;
892 struct netmap_priv_d *priv = &mna->priv;
893 struct netmap_adapter *pna = priv->np_na;
895 netmap_adapter_put(pna);
899 /* check if req is a request for a monitor adapter that we can satisfy */
901 netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
902 struct netmap_mem_d *nmd, int create)
904 struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
905 struct nmreq_register preq;
906 struct netmap_adapter *pna; /* parent adapter */
907 struct netmap_monitor_adapter *mna;
908 struct ifnet *ifp = NULL;
910 int zcopy = (req->nr_flags & NR_ZCOPY_MON);
913 req->nr_flags |= (NR_MONITOR_TX | NR_MONITOR_RX);
915 if ((req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX)) == 0) {
916 nm_prdis("not a monitor");
919 /* this is a request for a monitor adapter */
921 nm_prdis("flags %lx", req->nr_flags);
923 /* First, try to find the adapter that we want to monitor.
924 * We use the same req, after we have turned off the monitor flags.
925 * In this way we can potentially monitor everything netmap understands,
926 * except other monitors.
928 memcpy(&preq, req, sizeof(preq));
929 preq.nr_flags &= ~(NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON);
930 hdr->nr_body = (uintptr_t)&preq;
931 error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
932 hdr->nr_body = (uintptr_t)req;
934 nm_prerr("parent lookup failed: %d", error);
937 nm_prdis("found parent: %s", pna->name);
939 if (!nm_netmap_on(pna)) {
940 /* parent not in netmap mode */
941 /* XXX we can wait for the parent to enter netmap mode,
942 * by intercepting its nm_register callback (2014-03-16)
944 nm_prerr("%s not in netmap mode", pna->name);
949 mna = nm_os_malloc(sizeof(*mna));
954 mna->priv.np_na = pna;
956 /* grab all the rings we need in the parent */
957 error = netmap_interp_ringid(&mna->priv, hdr);
959 nm_prerr("ringid error");
962 snprintf(mna->up.name, sizeof(mna->up.name), "%s/%s%s%s#%lu", pna->name,
964 (req->nr_flags & NR_MONITOR_RX) ? "r" : "",
965 (req->nr_flags & NR_MONITOR_TX) ? "t" : "",
968 /* the monitor supports the host rings iff the parent does */
969 mna->up.na_flags |= (pna->na_flags & NAF_HOST_RINGS);
970 /* a do-nothing txsync: monitors cannot be used to inject packets */
971 mna->up.nm_txsync = netmap_monitor_txsync;
972 mna->up.nm_rxsync = netmap_monitor_rxsync;
973 mna->up.nm_krings_create = netmap_monitor_krings_create;
974 mna->up.nm_krings_delete = netmap_monitor_krings_delete;
975 mna->up.num_tx_rings = 1; // XXX what should we do here with chained zmons?
976 /* we set the number of our rx_rings to be max(num_rx_rings, num_rx_rings)
979 mna->up.num_rx_rings = pna->num_rx_rings;
980 if (pna->num_tx_rings > pna->num_rx_rings)
981 mna->up.num_rx_rings = pna->num_tx_rings;
982 /* by default, the number of slots is the same as in
983 * the parent rings, but the user may ask for a different
986 mna->up.num_tx_desc = req->nr_tx_slots;
987 nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
988 1, NM_MONITOR_MAXSLOTS, NULL);
989 mna->up.num_rx_desc = req->nr_rx_slots;
990 nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
991 1, NM_MONITOR_MAXSLOTS, NULL);
993 mna->up.nm_register = netmap_zmon_reg;
994 mna->up.nm_dtor = netmap_zmon_dtor;
995 /* to have zero copy, we need to use the same memory allocator
996 * as the monitored port
998 mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
999 /* and the allocator cannot be changed */
1000 mna->up.na_flags |= NAF_MEM_OWNER;
1002 mna->up.nm_register = netmap_monitor_reg;
1003 mna->up.nm_dtor = netmap_monitor_dtor;
1004 mna->up.nm_mem = netmap_mem_private_new(
1005 mna->up.num_tx_rings,
1006 mna->up.num_tx_desc,
1007 mna->up.num_rx_rings,
1008 mna->up.num_rx_desc,
1012 if (mna->up.nm_mem == NULL)
1016 error = netmap_attach_common(&mna->up);
1018 nm_prerr("netmap_attach_common failed");
1022 /* remember the traffic directions we have to monitor */
1023 mna->flags = (req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON));
1026 netmap_adapter_get(*na);
1028 /* keep the reference to the parent */
1029 nm_prdis("monitor ok");
1031 /* drop the reference to the ifp, if any */
1038 netmap_mem_put(mna->up.nm_mem);
1042 netmap_unget_na(pna, ifp);
1047 #endif /* WITH_MONITOR */