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 RD(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 ND("%s %x", kring->name, flags);
156 kring->nr_hwcur = kring->rhead;
161 /* nm_krings_create callbacks for monitors.
164 netmap_monitor_krings_create(struct netmap_adapter *na)
166 int error = netmap_krings_create(na, 0);
169 /* override the host rings callbacks */
170 na->tx_rings[na->num_tx_rings]->nm_sync = netmap_monitor_txsync;
171 na->rx_rings[na->num_rx_rings]->nm_sync = netmap_monitor_rxsync;
175 /* nm_krings_delete callback for monitors */
177 netmap_monitor_krings_delete(struct netmap_adapter *na)
179 netmap_krings_delete(na);
184 nm_txrx2flag(enum txrx t)
186 return (t == NR_RX ? NR_MONITOR_RX : NR_MONITOR_TX);
189 /* allocate the monitors array in the monitored kring */
191 nm_monitor_alloc(struct netmap_kring *kring, u_int n)
194 struct netmap_kring **nm;
196 if (n <= kring->max_monitors)
197 /* we already have more entries that requested */
200 old_len = sizeof(struct netmap_kring *)*kring->max_monitors;
201 len = sizeof(struct netmap_kring *) * n;
202 nm = nm_os_realloc(kring->monitors, len, old_len);
206 kring->monitors = nm;
207 kring->max_monitors = n;
212 /* deallocate the parent array in the parent adapter */
214 nm_monitor_dealloc(struct netmap_kring *kring)
216 if (kring->monitors) {
217 if (kring->n_monitors > 0) {
218 D("freeing not empty monitor array for %s (%d dangling monitors)!", kring->name,
221 nm_os_free(kring->monitors);
222 kring->monitors = NULL;
223 kring->max_monitors = 0;
224 kring->n_monitors = 0;
228 /* returns 1 iff kring has no monitors */
230 nm_monitor_none(struct netmap_kring *kring)
232 return kring->n_monitors == 0 &&
233 kring->zmon_list[NR_TX].next == NULL &&
234 kring->zmon_list[NR_RX].next == NULL;
238 * monitors work by replacing the nm_sync() and possibly the
239 * nm_notify() callbacks in the monitored rings.
241 static int netmap_zmon_parent_txsync(struct netmap_kring *, int);
242 static int netmap_zmon_parent_rxsync(struct netmap_kring *, int);
243 static int netmap_monitor_parent_txsync(struct netmap_kring *, int);
244 static int netmap_monitor_parent_rxsync(struct netmap_kring *, int);
245 static int netmap_monitor_parent_notify(struct netmap_kring *, int);
247 /* add the monitor mkring to the list of monitors of kring.
248 * If this is the first monitor, intercept the callbacks
251 netmap_monitor_add(struct netmap_kring *mkring, struct netmap_kring *kring, int zmon)
253 int error = NM_IRQ_COMPLETED;
254 enum txrx t = kring->tx;
255 struct netmap_zmon_list *z = &kring->zmon_list[t];
256 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
258 /* a zero-copy monitor which is not the first in the list
259 * must monitor the previous monitor
261 if (zmon && z->prev != NULL)
264 /* synchronize with concurrently running nm_sync()s */
265 nm_kr_stop(kring, NM_KR_LOCKED);
267 if (nm_monitor_none(kring)) {
268 /* this is the first monitor, intercept callbacks */
269 ND("intercept callbacks on %s", kring->name);
270 kring->mon_sync = kring->nm_sync;
271 kring->mon_notify = kring->nm_notify;
272 if (kring->tx == NR_TX) {
273 kring->nm_sync = netmap_monitor_parent_txsync;
275 kring->nm_sync = netmap_monitor_parent_rxsync;
276 kring->nm_notify = netmap_monitor_parent_notify;
277 kring->mon_tail = kring->nr_hwtail;
282 /* append the zmon to the list */
283 struct netmap_monitor_adapter *mna =
284 (struct netmap_monitor_adapter *)mkring->na;
285 struct netmap_adapter *pna;
288 z->prev->zmon_list[t].next = mkring;
294 /* grap a reference to the previous netmap adapter
295 * in the chain (this may be the monitored port
296 * or another zero-copy monitor)
299 netmap_adapter_get(pna);
300 netmap_adapter_put(mna->priv.np_na);
301 mna->priv.np_na = pna;
303 /* make sure the monitor array exists and is big enough */
304 error = nm_monitor_alloc(kring, kring->n_monitors + 1);
307 kring->monitors[kring->n_monitors] = mkring;
308 mkring->mon_pos[kring->tx] = kring->n_monitors;
317 /* remove the monitor mkring from the list of monitors of kring.
318 * If this is the last monitor, restore the original callbacks
321 netmap_monitor_del(struct netmap_kring *mkring, struct netmap_kring *kring)
323 struct netmap_zmon_list *mz = &mkring->zmon_list[kring->tx];
324 int zmon = nm_is_zmon(mkring->na);
327 if (zmon && mz->prev != NULL)
330 /* synchronize with concurrently running nm_sync()s */
331 nm_kr_stop(kring, NM_KR_LOCKED);
334 /* remove the monitor from the list */
335 if (mz->prev != NULL)
336 mz->prev->zmon_list[kring->tx].next = mz->next;
338 kring->zmon_list[kring->tx].next = mz->next;
339 if (mz->next != NULL) {
340 mz->next->zmon_list[kring->tx].prev = mz->prev;
342 kring->zmon_list[kring->tx].prev = mz->prev;
345 /* this is a copy monitor */
346 uint32_t mon_pos = mkring->mon_pos[kring->tx];
348 if (mon_pos != kring->n_monitors) {
349 kring->monitors[mon_pos] =
350 kring->monitors[kring->n_monitors];
351 kring->monitors[mon_pos]->mon_pos[kring->tx] = mon_pos;
353 kring->monitors[kring->n_monitors] = NULL;
354 if (kring->n_monitors == 0) {
355 nm_monitor_dealloc(kring);
359 if (nm_monitor_none(kring)) {
360 /* this was the last monitor, restore the callbacks */
361 ND("%s: restoring sync on %s: %p", mkring->name, kring->name,
363 kring->nm_sync = kring->mon_sync;
364 kring->mon_sync = NULL;
365 if (kring->tx == NR_RX) {
366 ND("%s: restoring notify on %s: %p",
367 mkring->name, kring->name, kring->mon_notify);
368 kring->nm_notify = kring->mon_notify;
369 kring->mon_notify = NULL;
377 /* This is called when the monitored adapter leaves netmap mode
378 * (see netmap_do_unregif).
379 * We need to notify the monitors that the monitored rings are gone.
380 * We do this by setting their mna->priv.np_na to NULL.
381 * Note that the rings are already stopped when this happens, so
382 * no monitor ring callback can be active.
385 netmap_monitor_stop(struct netmap_adapter *na)
392 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
393 struct netmap_kring *kring = NMR(na, t)[i];
394 struct netmap_kring *zkring;
397 for (j = 0; j < kring->n_monitors; j++) {
398 struct netmap_kring *mkring =
400 struct netmap_monitor_adapter *mna =
401 (struct netmap_monitor_adapter *)mkring->na;
402 /* forget about this adapter */
403 if (mna->priv.np_na != NULL) {
404 netmap_adapter_put(mna->priv.np_na);
405 mna->priv.np_na = NULL;
409 zkring = kring->zmon_list[kring->tx].next;
410 if (zkring != NULL) {
411 struct netmap_monitor_adapter *next =
412 (struct netmap_monitor_adapter *)zkring->na;
413 struct netmap_monitor_adapter *this =
414 (struct netmap_monitor_adapter *)na;
415 struct netmap_adapter *pna = this->priv.np_na;
416 /* let the next monitor forget about us */
417 if (next->priv.np_na != NULL) {
418 netmap_adapter_put(next->priv.np_na);
420 if (pna != NULL && nm_is_zmon(na)) {
421 /* we are a monitor ourselves and we may
422 * need to pass down the reference to
423 * the previous adapter in the chain
425 netmap_adapter_get(pna);
426 next->priv.np_na = pna;
429 next->priv.np_na = NULL;
436 /* common functions for the nm_register() callbacks of both kind of
440 netmap_monitor_reg_common(struct netmap_adapter *na, int onoff, int zmon)
442 struct netmap_monitor_adapter *mna =
443 (struct netmap_monitor_adapter *)na;
444 struct netmap_priv_d *priv = &mna->priv;
445 struct netmap_adapter *pna = priv->np_na;
446 struct netmap_kring *kring, *mkring;
450 ND("%p: onoff %d", na, onoff);
453 /* parent left netmap mode, fatal */
454 D("%s: internal error", na->name);
458 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
459 mkring = NMR(na, t)[i];
460 if (!nm_kring_pending_on(mkring))
462 mkring->nr_mode = NKR_NETMAP_ON;
466 if (i > nma_get_nrings(pna, s))
468 if (mna->flags & nm_txrx2flag(s)) {
469 kring = NMR(pna, s)[i];
470 netmap_monitor_add(mkring, kring, zmon);
475 na->na_flags |= NAF_NETMAP_ON;
477 if (na->active_fds == 0)
478 na->na_flags &= ~NAF_NETMAP_ON;
480 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
481 mkring = NMR(na, t)[i];
482 if (!nm_kring_pending_off(mkring))
484 mkring->nr_mode = NKR_NETMAP_OFF;
487 /* we cannot access the parent krings if the parent
488 * has left netmap mode. This is signaled by a NULL
494 if (i > nma_get_nrings(pna, s))
496 if (mna->flags & nm_txrx2flag(s)) {
497 kring = NMR(pna, s)[i];
498 netmap_monitor_del(mkring, kring);
508 ****************************************************************
509 * functions specific for zero-copy monitors
510 ****************************************************************
514 * Common function for both zero-copy tx and rx nm_sync()
518 netmap_zmon_parent_sync(struct netmap_kring *kring, int flags, enum txrx tx)
520 struct netmap_kring *mkring = kring->zmon_list[tx].next;
521 struct netmap_ring *ring = kring->ring, *mring;
523 int rel_slots, free_slots, busy, sent = 0;
525 u_int lim = kring->nkr_num_slots - 1,
526 mlim; // = mkring->nkr_num_slots - 1;
528 if (mkring == NULL) {
529 RD(5, "NULL monitor on %s", kring->name);
532 mring = mkring->ring;
533 mlim = mkring->nkr_num_slots - 1;
535 /* get the relased slots (rel_slots) */
537 beg = kring->nr_hwtail + 1;
538 error = kring->mon_sync(kring, flags);
541 end = kring->nr_hwtail + 1;
543 beg = kring->nr_hwcur;
547 rel_slots = end - beg;
549 rel_slots += kring->nkr_num_slots;
552 /* no released slots, but we still need
553 * to call rxsync if this is a rx ring
558 /* we need to lock the monitor receive ring, since it
559 * is the target of bot tx and rx traffic from the monitored
562 mtx_lock(&mkring->q_lock);
563 /* get the free slots available on the monitor ring */
564 i = mkring->nr_hwtail;
565 busy = i - mkring->nr_hwcur;
567 busy += mkring->nkr_num_slots;
568 free_slots = mlim - busy;
573 /* swap min(free_slots, rel_slots) slots */
574 if (free_slots < rel_slots) {
575 beg += (rel_slots - free_slots);
576 rel_slots = free_slots;
578 if (unlikely(beg >= kring->nkr_num_slots))
579 beg -= kring->nkr_num_slots;
582 for ( ; rel_slots; rel_slots--) {
583 struct netmap_slot *s = &ring->slot[beg];
584 struct netmap_slot *ms = &mring->slot[i];
588 ms->buf_idx = s->buf_idx;
590 ND(5, "beg %d buf_idx %d", beg, tmp);
596 s->flags |= NS_BUF_CHANGED;
598 beg = nm_next(beg, lim);
599 i = nm_next(i, mlim);
603 mkring->nr_hwtail = i;
606 mtx_unlock(&mkring->q_lock);
609 /* notify the new frames to the monitor */
610 mkring->nm_notify(mkring, 0);
615 error = kring->mon_sync(kring, flags);
620 /* callback used to replace the nm_sync callback in the monitored tx rings */
622 netmap_zmon_parent_txsync(struct netmap_kring *kring, int flags)
624 return netmap_zmon_parent_sync(kring, flags, NR_TX);
627 /* callback used to replace the nm_sync callback in the monitored rx rings */
629 netmap_zmon_parent_rxsync(struct netmap_kring *kring, int flags)
631 return netmap_zmon_parent_sync(kring, flags, NR_RX);
635 netmap_zmon_reg(struct netmap_adapter *na, int onoff)
637 return netmap_monitor_reg_common(na, onoff, 1 /* zcopy */);
640 /* nm_dtor callback for monitors */
642 netmap_zmon_dtor(struct netmap_adapter *na)
644 struct netmap_monitor_adapter *mna =
645 (struct netmap_monitor_adapter *)na;
646 struct netmap_priv_d *priv = &mna->priv;
647 struct netmap_adapter *pna = priv->np_na;
649 netmap_adapter_put(pna);
653 ****************************************************************
654 * functions specific for copy monitors
655 ****************************************************************
659 netmap_monitor_parent_sync(struct netmap_kring *kring, u_int first_new, int new_slots)
663 for (j = 0; j < kring->n_monitors; j++) {
664 struct netmap_kring *mkring = kring->monitors[j];
666 int free_slots, busy, sent = 0, m;
667 u_int lim = kring->nkr_num_slots - 1;
668 struct netmap_ring *ring = kring->ring, *mring = mkring->ring;
669 u_int max_len = NETMAP_BUF_SIZE(mkring->na);
671 mlim = mkring->nkr_num_slots - 1;
673 /* we need to lock the monitor receive ring, since it
674 * is the target of bot tx and rx traffic from the monitored
677 mtx_lock(&mkring->q_lock);
678 /* get the free slots available on the monitor ring */
679 i = mkring->nr_hwtail;
680 busy = i - mkring->nr_hwcur;
682 busy += mkring->nkr_num_slots;
683 free_slots = mlim - busy;
688 /* copy min(free_slots, new_slots) slots */
691 if (free_slots < m) {
692 beg += (m - free_slots);
693 if (beg >= kring->nkr_num_slots)
694 beg -= kring->nkr_num_slots;
699 struct netmap_slot *s = &ring->slot[beg];
700 struct netmap_slot *ms = &mring->slot[i];
701 u_int copy_len = s->len;
702 char *src = NMB(kring->na, s),
703 *dst = NMB(mkring->na, ms);
705 if (unlikely(copy_len > max_len)) {
706 RD(5, "%s->%s: truncating %d to %d", kring->name,
707 mkring->name, copy_len, max_len);
711 memcpy(dst, src, copy_len);
715 beg = nm_next(beg, lim);
716 i = nm_next(i, mlim);
719 mkring->nr_hwtail = i;
721 mtx_unlock(&mkring->q_lock);
724 /* notify the new frames to the monitor */
725 mkring->nm_notify(mkring, 0);
730 /* callback used to replace the nm_sync callback in the monitored tx rings */
732 netmap_monitor_parent_txsync(struct netmap_kring *kring, int flags)
737 /* get the new slots */
738 if (kring->n_monitors > 0) {
739 first_new = kring->nr_hwcur;
740 new_slots = kring->rhead - first_new;
742 new_slots += kring->nkr_num_slots;
744 netmap_monitor_parent_sync(kring, first_new, new_slots);
746 if (kring->zmon_list[NR_TX].next != NULL) {
747 return netmap_zmon_parent_txsync(kring, flags);
749 return kring->mon_sync(kring, flags);
752 /* callback used to replace the nm_sync callback in the monitored rx rings */
754 netmap_monitor_parent_rxsync(struct netmap_kring *kring, int flags)
757 int new_slots, error;
759 /* get the new slots */
760 if (kring->zmon_list[NR_RX].next != NULL) {
761 error = netmap_zmon_parent_rxsync(kring, flags);
763 error = kring->mon_sync(kring, flags);
767 if (kring->n_monitors > 0) {
768 first_new = kring->mon_tail;
769 new_slots = kring->nr_hwtail - first_new;
771 new_slots += kring->nkr_num_slots;
773 netmap_monitor_parent_sync(kring, first_new, new_slots);
774 kring->mon_tail = kring->nr_hwtail;
779 /* callback used to replace the nm_notify() callback in the monitored rx rings */
781 netmap_monitor_parent_notify(struct netmap_kring *kring, int flags)
783 int (*notify)(struct netmap_kring*, int);
784 ND(5, "%s %x", kring->name, flags);
785 /* ?xsync callbacks have tryget called by their callers
786 * (NIOCREGIF and poll()), but here we have to call it
789 if (nm_kr_tryget(kring, 0, NULL)) {
790 /* in all cases, just skip the sync */
791 return NM_IRQ_COMPLETED;
793 if (kring->n_monitors > 0) {
794 netmap_monitor_parent_rxsync(kring, NAF_FORCE_READ);
796 if (nm_monitor_none(kring)) {
797 /* we are no longer monitoring this ring, so both
798 * mon_sync and mon_notify are NULL
800 notify = kring->nm_notify;
802 notify = kring->mon_notify;
805 return notify(kring, flags);
810 netmap_monitor_reg(struct netmap_adapter *na, int onoff)
812 return netmap_monitor_reg_common(na, onoff, 0 /* no zcopy */);
816 netmap_monitor_dtor(struct netmap_adapter *na)
818 struct netmap_monitor_adapter *mna =
819 (struct netmap_monitor_adapter *)na;
820 struct netmap_priv_d *priv = &mna->priv;
821 struct netmap_adapter *pna = priv->np_na;
823 netmap_adapter_put(pna);
827 /* check if req is a request for a monitor adapter that we can satisfy */
829 netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
830 struct netmap_mem_d *nmd, int create)
832 struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
833 struct nmreq_register preq;
834 struct netmap_adapter *pna; /* parent adapter */
835 struct netmap_monitor_adapter *mna;
836 struct ifnet *ifp = NULL;
838 int zcopy = (req->nr_flags & NR_ZCOPY_MON);
839 char monsuff[10] = "";
842 req->nr_flags |= (NR_MONITOR_TX | NR_MONITOR_RX);
844 if ((req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX)) == 0) {
848 /* this is a request for a monitor adapter */
850 ND("flags %lx", req->nr_flags);
852 /* First, try to find the adapter that we want to monitor.
853 * We use the same req, after we have turned off the monitor flags.
854 * In this way we can potentially monitor everything netmap understands,
855 * except other monitors.
857 memcpy(&preq, req, sizeof(preq));
858 preq.nr_flags &= ~(NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON);
859 hdr->nr_body = (uintptr_t)&preq;
860 error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
861 hdr->nr_body = (uintptr_t)req;
863 D("parent lookup failed: %d", error);
866 ND("found parent: %s", pna->name);
868 if (!nm_netmap_on(pna)) {
869 /* parent not in netmap mode */
870 /* XXX we can wait for the parent to enter netmap mode,
871 * by intercepting its nm_register callback (2014-03-16)
873 D("%s not in netmap mode", pna->name);
878 mna = nm_os_malloc(sizeof(*mna));
884 mna->priv.np_na = pna;
886 /* grab all the rings we need in the parent */
887 error = netmap_interp_ringid(&mna->priv, req->nr_mode, req->nr_ringid,
893 if (mna->priv.np_qlast[NR_TX] - mna->priv.np_qfirst[NR_TX] == 1) {
894 snprintf(monsuff, 10, "-%d", mna->priv.np_qfirst[NR_TX]);
896 snprintf(mna->up.name, sizeof(mna->up.name), "%s%s/%s%s%s", pna->name,
899 (req->nr_flags & NR_MONITOR_RX) ? "r" : "",
900 (req->nr_flags & NR_MONITOR_TX) ? "t" : "");
902 /* the monitor supports the host rings iff the parent does */
903 mna->up.na_flags |= (pna->na_flags & NAF_HOST_RINGS);
904 /* a do-nothing txsync: monitors cannot be used to inject packets */
905 mna->up.nm_txsync = netmap_monitor_txsync;
906 mna->up.nm_rxsync = netmap_monitor_rxsync;
907 mna->up.nm_krings_create = netmap_monitor_krings_create;
908 mna->up.nm_krings_delete = netmap_monitor_krings_delete;
909 mna->up.num_tx_rings = 1; // XXX what should we do here with chained zmons?
910 /* we set the number of our rx_rings to be max(num_rx_rings, num_rx_rings)
913 mna->up.num_rx_rings = pna->num_rx_rings;
914 if (pna->num_tx_rings > pna->num_rx_rings)
915 mna->up.num_rx_rings = pna->num_tx_rings;
916 /* by default, the number of slots is the same as in
917 * the parent rings, but the user may ask for a different
920 mna->up.num_tx_desc = req->nr_tx_slots;
921 nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
922 1, NM_MONITOR_MAXSLOTS, NULL);
923 mna->up.num_rx_desc = req->nr_rx_slots;
924 nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
925 1, NM_MONITOR_MAXSLOTS, NULL);
927 mna->up.nm_register = netmap_zmon_reg;
928 mna->up.nm_dtor = netmap_zmon_dtor;
929 /* to have zero copy, we need to use the same memory allocator
930 * as the monitored port
932 mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
933 /* and the allocator cannot be changed */
934 mna->up.na_flags |= NAF_MEM_OWNER;
936 mna->up.nm_register = netmap_monitor_reg;
937 mna->up.nm_dtor = netmap_monitor_dtor;
938 mna->up.nm_mem = netmap_mem_private_new(
939 mna->up.num_tx_rings,
941 mna->up.num_rx_rings,
946 if (mna->up.nm_mem == NULL)
950 error = netmap_attach_common(&mna->up);
952 D("attach_common error");
956 /* remember the traffic directions we have to monitor */
957 mna->flags = (req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON));
960 netmap_adapter_get(*na);
962 /* keep the reference to the parent */
965 /* drop the reference to the ifp, if any */
972 netmap_mem_put(mna->up.nm_mem);
976 netmap_unget_na(pna, ifp);
981 #endif /* WITH_MONITOR */