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.
42 * If the monitor is not able to cope with the stream of frames, excess traffic
45 * If the monitored adapter leaves netmap mode, the monitor has to be restarted.
47 * Monitors can be either zero-copy or copy-based.
49 * Copy monitors see the frames before they are consumed:
51 * - For tx traffic, this is when the application sends them, before they are
52 * passed down to the adapter.
54 * - For rx traffic, this is when they are received by the adapter, before
55 * they are sent up to the application, if any (note that, if no
56 * application is reading from a monitored ring, the ring will eventually
57 * fill up and traffic will stop).
59 * Zero-copy monitors only see the frames after they have been consumed:
61 * - For tx traffic, this is after the slots containing the frames have been
62 * marked as free. Note that this may happen at a considerably delay after
63 * frame transmission, since freeing of slots is often done lazily.
65 * - For rx traffic, this is after the consumer on the monitored adapter
66 * has released them. In most cases, the consumer is a userspace
67 * application which may have modified the frame contents.
69 * Several copy or zero-copy monitors may be active on any ring.
74 #if defined(__FreeBSD__)
75 #include <sys/cdefs.h> /* prerequisite */
77 #include <sys/types.h>
78 #include <sys/errno.h>
79 #include <sys/param.h> /* defines used in kernel.h */
80 #include <sys/kernel.h> /* types used in module initialization */
81 #include <sys/malloc.h>
84 #include <sys/rwlock.h>
85 #include <sys/selinfo.h>
86 #include <sys/sysctl.h>
87 #include <sys/socket.h> /* sockaddrs */
89 #include <net/if_var.h>
90 #include <machine/bus.h> /* bus_dmamap_* */
91 #include <sys/refcount.h>
98 #elif defined(__APPLE__)
100 #warning OSX support is only partial
101 #include "osx_glue.h"
103 #elif defined(_WIN32)
104 #include "win_glue.h"
107 #error Unsupported platform
109 #endif /* unsupported */
115 #include <net/netmap.h>
116 #include <dev/netmap/netmap_kern.h>
117 #include <dev/netmap/netmap_mem2.h>
121 #define NM_MONITOR_MAXSLOTS 4096
124 ********************************************************************
125 * functions common to both kind of monitors
126 ********************************************************************
129 static int netmap_zmon_reg(struct netmap_adapter *, int);
131 nm_is_zmon(struct netmap_adapter *na)
133 return na->nm_register == netmap_zmon_reg;
136 /* nm_sync callback for the monitor's own tx rings.
137 * This makes no sense and always returns error
140 netmap_monitor_txsync(struct netmap_kring *kring, int flags)
142 nm_prlim(1, "%s %x", kring->name, flags);
146 /* nm_sync callback for the monitor's own rx rings.
147 * Note that the lock in netmap_zmon_parent_sync only protects
148 * writers among themselves. Synchronization between writers
149 * (i.e., netmap_zmon_parent_txsync and netmap_zmon_parent_rxsync)
150 * and readers (i.e., netmap_zmon_rxsync) relies on memory barriers.
153 netmap_monitor_rxsync(struct netmap_kring *kring, int flags)
155 struct netmap_monitor_adapter *mna =
156 (struct netmap_monitor_adapter *)kring->na;
157 if (unlikely(mna->priv.np_na == NULL)) {
158 /* parent left netmap mode */
161 nm_prdis("%s %x", kring->name, flags);
162 kring->nr_hwcur = kring->rhead;
167 /* nm_krings_create callbacks for monitors.
170 netmap_monitor_krings_create(struct netmap_adapter *na)
172 int error = netmap_krings_create(na, 0);
177 /* override the host rings callbacks */
180 u_int first = nma_get_nrings(na, t);
181 for (i = 0; i < nma_get_host_nrings(na, t); i++) {
182 struct netmap_kring *kring = NMR(na, t)[first + i];
183 kring->nm_sync = t == NR_TX ? netmap_monitor_txsync :
184 netmap_monitor_rxsync;
190 /* nm_krings_delete callback for monitors */
192 netmap_monitor_krings_delete(struct netmap_adapter *na)
194 netmap_krings_delete(na);
199 nm_txrx2flag(enum txrx t)
201 return (t == NR_RX ? NR_MONITOR_RX : NR_MONITOR_TX);
204 /* allocate the monitors array in the monitored kring */
206 nm_monitor_alloc(struct netmap_kring *kring, u_int n)
209 struct netmap_kring **nm;
211 if (n <= kring->max_monitors)
212 /* we already have more entries that requested */
215 old_len = sizeof(struct netmap_kring *)*kring->max_monitors;
216 len = sizeof(struct netmap_kring *) * n;
217 nm = nm_os_realloc(kring->monitors, len, old_len);
221 kring->monitors = nm;
222 kring->max_monitors = n;
227 /* deallocate the parent array in the parent adapter */
229 nm_monitor_dealloc(struct netmap_kring *kring)
231 if (kring->monitors) {
232 if (kring->n_monitors > 0) {
233 nm_prerr("freeing not empty monitor array for %s (%d dangling monitors)!",
234 kring->name, kring->n_monitors);
236 nm_os_free(kring->monitors);
237 kring->monitors = NULL;
238 kring->max_monitors = 0;
239 kring->n_monitors = 0;
243 /* returns 1 iff kring has no monitors */
245 nm_monitor_none(struct netmap_kring *kring)
247 return kring->n_monitors == 0 &&
248 kring->zmon_list[NR_TX].next == NULL &&
249 kring->zmon_list[NR_RX].next == NULL;
253 * monitors work by replacing the nm_sync() and possibly the
254 * nm_notify() callbacks in the monitored rings.
256 static int netmap_zmon_parent_txsync(struct netmap_kring *, int);
257 static int netmap_zmon_parent_rxsync(struct netmap_kring *, int);
258 static int netmap_monitor_parent_txsync(struct netmap_kring *, int);
259 static int netmap_monitor_parent_rxsync(struct netmap_kring *, int);
260 static int netmap_monitor_parent_notify(struct netmap_kring *, int);
263 nm_monitor_dummycb(struct netmap_kring *kring, int flags)
271 nm_monitor_intercept_callbacks(struct netmap_kring *kring)
273 nm_prdis("intercept callbacks on %s", kring->name);
274 kring->mon_sync = kring->nm_sync != NULL ?
275 kring->nm_sync : nm_monitor_dummycb;
276 kring->mon_notify = kring->nm_notify;
277 if (kring->tx == NR_TX) {
278 kring->nm_sync = netmap_monitor_parent_txsync;
280 kring->nm_sync = netmap_monitor_parent_rxsync;
281 kring->nm_notify = netmap_monitor_parent_notify;
282 kring->mon_tail = kring->nr_hwtail;
287 nm_monitor_restore_callbacks(struct netmap_kring *kring)
289 nm_prdis("restoring callbacks on %s", kring->name);
290 kring->nm_sync = kring->mon_sync;
291 kring->mon_sync = NULL;
292 if (kring->tx == NR_RX) {
293 kring->nm_notify = kring->mon_notify;
295 kring->mon_notify = NULL;
298 static struct netmap_kring *
299 nm_zmon_list_head(struct netmap_kring *mkring, enum txrx t)
301 struct netmap_adapter *na = mkring->na;
302 struct netmap_kring *kring = mkring;
303 struct netmap_zmon_list *z = &kring->zmon_list[t];
304 /* reach the head of the list */
305 while (nm_is_zmon(na) && z->prev != NULL) {
308 z = &kring->zmon_list[t];
310 return nm_is_zmon(na) ? NULL : kring;
313 /* add the monitor mkring to the list of monitors of kring.
314 * If this is the first monitor, intercept the callbacks
317 netmap_monitor_add(struct netmap_kring *mkring, struct netmap_kring *kring, int zmon)
319 int error = NM_IRQ_COMPLETED;
320 enum txrx t = kring->tx;
321 struct netmap_zmon_list *z = &kring->zmon_list[t];
322 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
323 struct netmap_kring *ikring = kring;
325 /* a zero-copy monitor which is not the first in the list
326 * must monitor the previous monitor
328 if (zmon && z->prev != NULL)
329 ikring = z->prev; /* tail of the list */
331 /* synchronize with concurrently running nm_sync()s */
332 nm_kr_stop(kring, NM_KR_LOCKED);
334 if (nm_monitor_none(ikring)) {
335 /* this is the first monitor, intercept the callbacks */
336 nm_prdis("%s: intercept callbacks on %s", mkring->name, ikring->name);
337 nm_monitor_intercept_callbacks(ikring);
341 /* append the zmon to the list */
342 ikring->zmon_list[t].next = mkring;
343 z->prev = mkring; /* new tail */
346 /* grab a reference to the previous netmap adapter
347 * in the chain (this may be the monitored port
348 * or another zero-copy monitor)
350 netmap_adapter_get(ikring->na);
352 /* make sure the monitor array exists and is big enough */
353 error = nm_monitor_alloc(kring, kring->n_monitors + 1);
356 kring->monitors[kring->n_monitors] = mkring;
357 mkring->mon_pos[kring->tx] = kring->n_monitors;
366 /* remove the monitor mkring from the list of monitors of kring.
367 * If this is the last monitor, restore the original callbacks
370 netmap_monitor_del(struct netmap_kring *mkring, struct netmap_kring *kring, enum txrx t)
372 int zmon = nm_is_zmon(mkring->na);
373 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
374 struct netmap_kring *ikring = kring;
378 /* get to the head of the list */
379 kring = nm_zmon_list_head(mkring, t);
383 /* synchronize with concurrently running nm_sync()s
384 * if kring is NULL (orphaned list) the monitored port
385 * has exited netmap mode, so there is nothing to stop
388 nm_kr_stop(kring, NM_KR_LOCKED);
391 /* remove the monitor from the list */
392 if (mz->next != NULL) {
393 mz->next->zmon_list[t].prev = mz->prev;
394 /* we also need to let the next monitor drop the
395 * reference to us and grab the reference to the
396 * previous ring owner, instead
398 if (mz->prev != NULL)
399 netmap_adapter_get(mz->prev->na);
400 netmap_adapter_put(mkring->na);
401 } else if (kring != NULL) {
402 /* in the monitored kring, prev is actually the
403 * pointer to the tail of the list
405 kring->zmon_list[t].prev =
406 (mz->prev != kring ? mz->prev : NULL);
408 if (mz->prev != NULL) {
409 netmap_adapter_put(mz->prev->na);
410 mz->prev->zmon_list[t].next = mz->next;
415 /* this is a copy monitor */
416 uint32_t mon_pos = mkring->mon_pos[kring->tx];
418 if (mon_pos != kring->n_monitors) {
419 kring->monitors[mon_pos] =
420 kring->monitors[kring->n_monitors];
421 kring->monitors[mon_pos]->mon_pos[kring->tx] = mon_pos;
423 kring->monitors[kring->n_monitors] = NULL;
424 if (kring->n_monitors == 0) {
425 nm_monitor_dealloc(kring);
429 if (ikring != NULL && nm_monitor_none(ikring)) {
430 /* this was the last monitor, restore the callbacks */
431 nm_monitor_restore_callbacks(ikring);
439 /* This is called when the monitored adapter leaves netmap mode
440 * (see netmap_do_unregif).
441 * We need to notify the monitors that the monitored rings are gone.
442 * We do this by setting their mna->priv.np_na to NULL.
443 * Note that the rings are already stopped when this happens, so
444 * no monitor ring callback can be active.
447 netmap_monitor_stop(struct netmap_adapter *na)
454 for (i = 0; i < netmap_all_rings(na, t); i++) {
455 struct netmap_kring *kring = NMR(na, t)[i];
456 struct netmap_zmon_list *z = &kring->zmon_list[t];
459 for (j = 0; j < kring->n_monitors; j++) {
460 struct netmap_kring *mkring =
462 struct netmap_monitor_adapter *mna =
463 (struct netmap_monitor_adapter *)mkring->na;
464 /* forget about this adapter */
465 if (mna->priv.np_na != NULL) {
466 netmap_adapter_put(mna->priv.np_na);
467 mna->priv.np_na = NULL;
469 kring->monitors[j] = NULL;
472 if (!nm_is_zmon(na)) {
473 /* we are the head of at most one list */
474 struct netmap_kring *zkring;
475 for (zkring = z->next; zkring != NULL;
476 zkring = zkring->zmon_list[t].next)
478 struct netmap_monitor_adapter *next =
479 (struct netmap_monitor_adapter *)zkring->na;
480 /* let the monitor forget about us */
481 netmap_adapter_put(next->priv.np_na); /* nop if null */
482 next->priv.np_na = NULL;
484 /* orhpan the zmon list */
486 z->next->zmon_list[t].prev = NULL;
491 if (!nm_monitor_none(kring)) {
493 kring->n_monitors = 0;
494 nm_monitor_dealloc(kring);
495 nm_monitor_restore_callbacks(kring);
502 /* common functions for the nm_register() callbacks of both kind of
506 netmap_monitor_reg_common(struct netmap_adapter *na, int onoff, int zmon)
508 struct netmap_monitor_adapter *mna =
509 (struct netmap_monitor_adapter *)na;
510 struct netmap_priv_d *priv = &mna->priv;
511 struct netmap_adapter *pna = priv->np_na;
512 struct netmap_kring *kring, *mkring;
516 nm_prdis("%p: onoff %d", na, onoff);
519 /* parent left netmap mode, fatal */
520 nm_prerr("%s: parent left netmap mode", na->name);
524 for (i = 0; i < netmap_all_rings(na, t); i++) {
525 mkring = NMR(na, t)[i];
526 if (!nm_kring_pending_on(mkring))
528 mkring->nr_mode = NKR_NETMAP_ON;
532 if (i > nma_get_nrings(pna, s))
534 if (mna->flags & nm_txrx2flag(s)) {
535 kring = NMR(pna, s)[i];
536 netmap_monitor_add(mkring, kring, zmon);
541 na->na_flags |= NAF_NETMAP_ON;
543 if (na->active_fds == 0)
544 na->na_flags &= ~NAF_NETMAP_ON;
546 for (i = 0; i < netmap_all_rings(na, t); i++) {
547 mkring = NMR(na, t)[i];
548 if (!nm_kring_pending_off(mkring))
550 mkring->nr_mode = NKR_NETMAP_OFF;
553 /* we cannot access the parent krings if the parent
554 * has left netmap mode. This is signaled by a NULL
560 if (i > nma_get_nrings(pna, s))
562 if (mna->flags & nm_txrx2flag(s)) {
563 kring = NMR(pna, s)[i];
564 netmap_monitor_del(mkring, kring, s);
574 ****************************************************************
575 * functions specific for zero-copy monitors
576 ****************************************************************
580 * Common function for both zero-copy tx and rx nm_sync()
584 netmap_zmon_parent_sync(struct netmap_kring *kring, int flags, enum txrx tx)
586 struct netmap_kring *mkring = kring->zmon_list[tx].next;
587 struct netmap_ring *ring = kring->ring, *mring;
589 int rel_slots, free_slots, busy, sent = 0;
591 u_int lim = kring->nkr_num_slots - 1,
592 mlim; // = mkring->nkr_num_slots - 1;
594 if (mkring == NULL) {
595 nm_prlim(5, "NULL monitor on %s", kring->name);
598 mring = mkring->ring;
599 mlim = mkring->nkr_num_slots - 1;
601 /* get the relased slots (rel_slots) */
603 beg = kring->nr_hwtail + 1;
604 error = kring->mon_sync(kring, flags);
607 end = kring->nr_hwtail + 1;
609 beg = kring->nr_hwcur;
613 rel_slots = end - beg;
615 rel_slots += kring->nkr_num_slots;
618 /* no released slots, but we still need
619 * to call rxsync if this is a rx ring
624 /* we need to lock the monitor receive ring, since it
625 * is the target of bot tx and rx traffic from the monitored
628 mtx_lock(&mkring->q_lock);
629 /* get the free slots available on the monitor ring */
630 i = mkring->nr_hwtail;
631 busy = i - mkring->nr_hwcur;
633 busy += mkring->nkr_num_slots;
634 free_slots = mlim - busy;
639 /* swap min(free_slots, rel_slots) slots */
640 if (free_slots < rel_slots) {
641 beg += (rel_slots - free_slots);
642 rel_slots = free_slots;
644 if (unlikely(beg >= kring->nkr_num_slots))
645 beg -= kring->nkr_num_slots;
648 for ( ; rel_slots; rel_slots--) {
649 struct netmap_slot *s = &ring->slot[beg];
650 struct netmap_slot *ms = &mring->slot[i];
654 ms->buf_idx = s->buf_idx;
656 nm_prdis(5, "beg %d buf_idx %d", beg, tmp);
662 ms->flags = s->flags;
663 s->flags |= NS_BUF_CHANGED;
665 beg = nm_next(beg, lim);
666 i = nm_next(i, mlim);
670 mkring->nr_hwtail = i;
673 mtx_unlock(&mkring->q_lock);
676 /* notify the new frames to the monitor */
677 mkring->nm_notify(mkring, 0);
682 error = kring->mon_sync(kring, flags);
687 /* callback used to replace the nm_sync callback in the monitored tx rings */
689 netmap_zmon_parent_txsync(struct netmap_kring *kring, int flags)
691 return netmap_zmon_parent_sync(kring, flags, NR_TX);
694 /* callback used to replace the nm_sync callback in the monitored rx rings */
696 netmap_zmon_parent_rxsync(struct netmap_kring *kring, int flags)
698 return netmap_zmon_parent_sync(kring, flags, NR_RX);
702 netmap_zmon_reg(struct netmap_adapter *na, int onoff)
704 return netmap_monitor_reg_common(na, onoff, 1 /* zcopy */);
707 /* nm_dtor callback for monitors */
709 netmap_zmon_dtor(struct netmap_adapter *na)
711 struct netmap_monitor_adapter *mna =
712 (struct netmap_monitor_adapter *)na;
713 struct netmap_priv_d *priv = &mna->priv;
714 struct netmap_adapter *pna = priv->np_na;
716 netmap_adapter_put(pna);
720 ****************************************************************
721 * functions specific for copy monitors
722 ****************************************************************
726 netmap_monitor_parent_sync(struct netmap_kring *kring, u_int first_new, int new_slots)
730 for (j = 0; j < kring->n_monitors; j++) {
731 struct netmap_kring *mkring = kring->monitors[j];
733 int free_slots, busy, sent = 0, m;
734 u_int lim = kring->nkr_num_slots - 1;
735 struct netmap_ring *ring = kring->ring, *mring = mkring->ring;
736 u_int max_len = NETMAP_BUF_SIZE(mkring->na);
738 mlim = mkring->nkr_num_slots - 1;
740 /* we need to lock the monitor receive ring, since it
741 * is the target of bot tx and rx traffic from the monitored
744 mtx_lock(&mkring->q_lock);
745 /* get the free slots available on the monitor ring */
746 i = mkring->nr_hwtail;
747 busy = i - mkring->nr_hwcur;
749 busy += mkring->nkr_num_slots;
750 free_slots = mlim - busy;
755 /* copy min(free_slots, new_slots) slots */
758 if (free_slots < m) {
759 beg += (m - free_slots);
760 if (beg >= kring->nkr_num_slots)
761 beg -= kring->nkr_num_slots;
766 struct netmap_slot *s = &ring->slot[beg];
767 struct netmap_slot *ms = &mring->slot[i];
768 u_int copy_len = s->len;
769 char *src = NMB(kring->na, s),
770 *dst = NMB(mkring->na, ms);
772 if (unlikely(copy_len > max_len)) {
773 nm_prlim(5, "%s->%s: truncating %d to %d", kring->name,
774 mkring->name, copy_len, max_len);
778 memcpy(dst, src, copy_len);
780 ms->flags = s->flags;
783 beg = nm_next(beg, lim);
784 i = nm_next(i, mlim);
787 mkring->nr_hwtail = i;
789 mtx_unlock(&mkring->q_lock);
792 /* notify the new frames to the monitor */
793 mkring->nm_notify(mkring, 0);
798 /* callback used to replace the nm_sync callback in the monitored tx rings */
800 netmap_monitor_parent_txsync(struct netmap_kring *kring, int flags)
805 /* get the new slots */
806 if (kring->n_monitors > 0) {
807 first_new = kring->nr_hwcur;
808 new_slots = kring->rhead - first_new;
810 new_slots += kring->nkr_num_slots;
812 netmap_monitor_parent_sync(kring, first_new, new_slots);
814 if (kring->zmon_list[NR_TX].next != NULL) {
815 return netmap_zmon_parent_txsync(kring, flags);
817 return kring->mon_sync(kring, flags);
820 /* callback used to replace the nm_sync callback in the monitored rx rings */
822 netmap_monitor_parent_rxsync(struct netmap_kring *kring, int flags)
825 int new_slots, error;
827 /* get the new slots */
828 if (kring->zmon_list[NR_RX].next != NULL) {
829 error = netmap_zmon_parent_rxsync(kring, flags);
831 error = kring->mon_sync(kring, flags);
835 if (kring->n_monitors > 0) {
836 first_new = kring->mon_tail;
837 new_slots = kring->nr_hwtail - first_new;
839 new_slots += kring->nkr_num_slots;
841 netmap_monitor_parent_sync(kring, first_new, new_slots);
842 kring->mon_tail = kring->nr_hwtail;
847 /* callback used to replace the nm_notify() callback in the monitored rx rings */
849 netmap_monitor_parent_notify(struct netmap_kring *kring, int flags)
851 int (*notify)(struct netmap_kring*, int);
852 nm_prdis(5, "%s %x", kring->name, flags);
853 /* ?xsync callbacks have tryget called by their callers
854 * (NIOCREGIF and poll()), but here we have to call it
857 if (nm_kr_tryget(kring, 0, NULL)) {
858 /* in all cases, just skip the sync */
859 return NM_IRQ_COMPLETED;
861 if (kring->n_monitors > 0) {
862 netmap_monitor_parent_rxsync(kring, NAF_FORCE_READ);
864 if (nm_monitor_none(kring)) {
865 /* we are no longer monitoring this ring, so both
866 * mon_sync and mon_notify are NULL
868 notify = kring->nm_notify;
870 notify = kring->mon_notify;
873 return notify(kring, flags);
878 netmap_monitor_reg(struct netmap_adapter *na, int onoff)
880 return netmap_monitor_reg_common(na, onoff, 0 /* no zcopy */);
884 netmap_monitor_dtor(struct netmap_adapter *na)
886 struct netmap_monitor_adapter *mna =
887 (struct netmap_monitor_adapter *)na;
888 struct netmap_priv_d *priv = &mna->priv;
889 struct netmap_adapter *pna = priv->np_na;
891 netmap_adapter_put(pna);
895 /* check if req is a request for a monitor adapter that we can satisfy */
897 netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
898 struct netmap_mem_d *nmd, int create)
900 struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
901 struct nmreq_register preq;
902 struct netmap_adapter *pna; /* parent adapter */
903 struct netmap_monitor_adapter *mna;
904 struct ifnet *ifp = NULL;
906 int zcopy = (req->nr_flags & NR_ZCOPY_MON);
909 req->nr_flags |= (NR_MONITOR_TX | NR_MONITOR_RX);
911 if ((req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX)) == 0) {
912 nm_prdis("not a monitor");
915 /* this is a request for a monitor adapter */
917 nm_prdis("flags %lx", req->nr_flags);
919 /* First, try to find the adapter that we want to monitor.
920 * We use the same req, after we have turned off the monitor flags.
921 * In this way we can potentially monitor everything netmap understands,
922 * except other monitors.
924 memcpy(&preq, req, sizeof(preq));
925 preq.nr_flags &= ~(NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON);
926 hdr->nr_body = (uintptr_t)&preq;
927 error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
928 hdr->nr_body = (uintptr_t)req;
930 nm_prerr("parent lookup failed: %d", error);
933 nm_prdis("found parent: %s", pna->name);
935 if (!nm_netmap_on(pna)) {
936 /* parent not in netmap mode */
937 /* XXX we can wait for the parent to enter netmap mode,
938 * by intercepting its nm_register callback (2014-03-16)
940 nm_prerr("%s not in netmap mode", pna->name);
945 mna = nm_os_malloc(sizeof(*mna));
950 mna->priv.np_na = pna;
952 /* grab all the rings we need in the parent */
953 error = netmap_interp_ringid(&mna->priv, req->nr_mode, req->nr_ringid,
956 nm_prerr("ringid error");
959 snprintf(mna->up.name, sizeof(mna->up.name), "%s/%s%s%s#%lu", pna->name,
961 (req->nr_flags & NR_MONITOR_RX) ? "r" : "",
962 (req->nr_flags & NR_MONITOR_TX) ? "t" : "",
965 /* the monitor supports the host rings iff the parent does */
966 mna->up.na_flags |= (pna->na_flags & NAF_HOST_RINGS);
967 /* a do-nothing txsync: monitors cannot be used to inject packets */
968 mna->up.nm_txsync = netmap_monitor_txsync;
969 mna->up.nm_rxsync = netmap_monitor_rxsync;
970 mna->up.nm_krings_create = netmap_monitor_krings_create;
971 mna->up.nm_krings_delete = netmap_monitor_krings_delete;
972 mna->up.num_tx_rings = 1; // XXX what should we do here with chained zmons?
973 /* we set the number of our rx_rings to be max(num_rx_rings, num_rx_rings)
976 mna->up.num_rx_rings = pna->num_rx_rings;
977 if (pna->num_tx_rings > pna->num_rx_rings)
978 mna->up.num_rx_rings = pna->num_tx_rings;
979 /* by default, the number of slots is the same as in
980 * the parent rings, but the user may ask for a different
983 mna->up.num_tx_desc = req->nr_tx_slots;
984 nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
985 1, NM_MONITOR_MAXSLOTS, NULL);
986 mna->up.num_rx_desc = req->nr_rx_slots;
987 nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
988 1, NM_MONITOR_MAXSLOTS, NULL);
990 mna->up.nm_register = netmap_zmon_reg;
991 mna->up.nm_dtor = netmap_zmon_dtor;
992 /* to have zero copy, we need to use the same memory allocator
993 * as the monitored port
995 mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
996 /* and the allocator cannot be changed */
997 mna->up.na_flags |= NAF_MEM_OWNER;
999 mna->up.nm_register = netmap_monitor_reg;
1000 mna->up.nm_dtor = netmap_monitor_dtor;
1001 mna->up.nm_mem = netmap_mem_private_new(
1002 mna->up.num_tx_rings,
1003 mna->up.num_tx_desc,
1004 mna->up.num_rx_rings,
1005 mna->up.num_rx_desc,
1009 if (mna->up.nm_mem == NULL)
1013 error = netmap_attach_common(&mna->up);
1015 nm_prerr("netmap_attach_common failed");
1019 /* remember the traffic directions we have to monitor */
1020 mna->flags = (req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON));
1023 netmap_adapter_get(*na);
1025 /* keep the reference to the parent */
1026 nm_prdis("monitor ok");
1028 /* drop the reference to the ifp, if any */
1035 netmap_mem_put(mna->up.nm_mem);
1039 netmap_unget_na(pna, ifp);
1044 #endif /* WITH_MONITOR */