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 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 ND("%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 D("freeing not empty monitor array for %s (%d dangling monitors)!", kring->name,
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_intercept_callbacks(struct netmap_kring *kring)
265 ND("intercept callbacks on %s", kring->name);
266 kring->mon_sync = kring->nm_sync;
267 kring->mon_notify = kring->nm_notify;
268 if (kring->tx == NR_TX) {
269 kring->nm_sync = netmap_monitor_parent_txsync;
271 kring->nm_sync = netmap_monitor_parent_rxsync;
272 kring->nm_notify = netmap_monitor_parent_notify;
273 kring->mon_tail = kring->nr_hwtail;
278 nm_monitor_restore_callbacks(struct netmap_kring *kring)
280 ND("restoring callbacks on %s", kring->name);
281 kring->nm_sync = kring->mon_sync;
282 kring->mon_sync = NULL;
283 if (kring->tx == NR_RX) {
284 kring->nm_notify = kring->mon_notify;
286 kring->mon_notify = NULL;
289 static struct netmap_kring *
290 nm_zmon_list_head(struct netmap_kring *mkring, enum txrx t)
292 struct netmap_adapter *na = mkring->na;
293 struct netmap_kring *kring = mkring;
294 struct netmap_zmon_list *z = &kring->zmon_list[t];
295 /* reach the head of the list */
296 while (nm_is_zmon(na) && z->prev != NULL) {
299 z = &kring->zmon_list[t];
301 return nm_is_zmon(na) ? NULL : kring;
304 /* add the monitor mkring to the list of monitors of kring.
305 * If this is the first monitor, intercept the callbacks
308 netmap_monitor_add(struct netmap_kring *mkring, struct netmap_kring *kring, int zmon)
310 int error = NM_IRQ_COMPLETED;
311 enum txrx t = kring->tx;
312 struct netmap_zmon_list *z = &kring->zmon_list[t];
313 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
314 struct netmap_kring *ikring = kring;
316 /* a zero-copy monitor which is not the first in the list
317 * must monitor the previous monitor
319 if (zmon && z->prev != NULL)
320 ikring = z->prev; /* tail of the list */
322 /* synchronize with concurrently running nm_sync()s */
323 nm_kr_stop(kring, NM_KR_LOCKED);
325 if (nm_monitor_none(ikring)) {
326 /* this is the first monitor, intercept the callbacks */
327 ND("%s: intercept callbacks on %s", mkring->name, ikring->name);
328 nm_monitor_intercept_callbacks(ikring);
332 /* append the zmon to the list */
333 ikring->zmon_list[t].next = mkring;
334 z->prev = mkring; /* new tail */
337 /* grab a reference to the previous netmap adapter
338 * in the chain (this may be the monitored port
339 * or another zero-copy monitor)
341 netmap_adapter_get(ikring->na);
343 /* make sure the monitor array exists and is big enough */
344 error = nm_monitor_alloc(kring, kring->n_monitors + 1);
347 kring->monitors[kring->n_monitors] = mkring;
348 mkring->mon_pos[kring->tx] = kring->n_monitors;
357 /* remove the monitor mkring from the list of monitors of kring.
358 * If this is the last monitor, restore the original callbacks
361 netmap_monitor_del(struct netmap_kring *mkring, struct netmap_kring *kring, enum txrx t)
363 int zmon = nm_is_zmon(mkring->na);
364 struct netmap_zmon_list *mz = &mkring->zmon_list[t];
365 struct netmap_kring *ikring = kring;
369 /* get to the head of the list */
370 kring = nm_zmon_list_head(mkring, t);
374 /* synchronize with concurrently running nm_sync()s
375 * if kring is NULL (orphaned list) the monitored port
376 * has exited netmap mode, so there is nothing to stop
379 nm_kr_stop(kring, NM_KR_LOCKED);
382 /* remove the monitor from the list */
383 if (mz->next != NULL) {
384 mz->next->zmon_list[t].prev = mz->prev;
385 /* we also need to let the next monitor drop the
386 * reference to us and grab the reference to the
387 * previous ring owner, instead
389 if (mz->prev != NULL)
390 netmap_adapter_get(mz->prev->na);
391 netmap_adapter_put(mkring->na);
392 } else if (kring != NULL) {
393 /* in the monitored kring, prev is actually the
394 * pointer to the tail of the list
396 kring->zmon_list[t].prev =
397 (mz->prev != kring ? mz->prev : NULL);
399 if (mz->prev != NULL) {
400 netmap_adapter_put(mz->prev->na);
401 mz->prev->zmon_list[t].next = mz->next;
406 /* this is a copy monitor */
407 uint32_t mon_pos = mkring->mon_pos[kring->tx];
409 if (mon_pos != kring->n_monitors) {
410 kring->monitors[mon_pos] =
411 kring->monitors[kring->n_monitors];
412 kring->monitors[mon_pos]->mon_pos[kring->tx] = mon_pos;
414 kring->monitors[kring->n_monitors] = NULL;
415 if (kring->n_monitors == 0) {
416 nm_monitor_dealloc(kring);
420 if (ikring != NULL && nm_monitor_none(ikring)) {
421 /* this was the last monitor, restore the callbacks */
422 nm_monitor_restore_callbacks(ikring);
430 /* This is called when the monitored adapter leaves netmap mode
431 * (see netmap_do_unregif).
432 * We need to notify the monitors that the monitored rings are gone.
433 * We do this by setting their mna->priv.np_na to NULL.
434 * Note that the rings are already stopped when this happens, so
435 * no monitor ring callback can be active.
438 netmap_monitor_stop(struct netmap_adapter *na)
445 for (i = 0; i < netmap_all_rings(na, t); i++) {
446 struct netmap_kring *kring = NMR(na, t)[i];
447 struct netmap_zmon_list *z = &kring->zmon_list[t];
450 for (j = 0; j < kring->n_monitors; j++) {
451 struct netmap_kring *mkring =
453 struct netmap_monitor_adapter *mna =
454 (struct netmap_monitor_adapter *)mkring->na;
455 /* forget about this adapter */
456 if (mna->priv.np_na != NULL) {
457 netmap_adapter_put(mna->priv.np_na);
458 mna->priv.np_na = NULL;
460 kring->monitors[j] = NULL;
463 if (!nm_is_zmon(na)) {
464 /* we are the head of at most one list */
465 struct netmap_kring *zkring;
466 for (zkring = z->next; zkring != NULL;
467 zkring = zkring->zmon_list[t].next)
469 struct netmap_monitor_adapter *next =
470 (struct netmap_monitor_adapter *)zkring->na;
471 /* let the monitor forget about us */
472 netmap_adapter_put(next->priv.np_na); /* nop if null */
473 next->priv.np_na = NULL;
475 /* orhpan the zmon list */
477 z->next->zmon_list[t].prev = NULL;
482 if (!nm_monitor_none(kring)) {
484 kring->n_monitors = 0;
485 nm_monitor_dealloc(kring);
486 nm_monitor_restore_callbacks(kring);
493 /* common functions for the nm_register() callbacks of both kind of
497 netmap_monitor_reg_common(struct netmap_adapter *na, int onoff, int zmon)
499 struct netmap_monitor_adapter *mna =
500 (struct netmap_monitor_adapter *)na;
501 struct netmap_priv_d *priv = &mna->priv;
502 struct netmap_adapter *pna = priv->np_na;
503 struct netmap_kring *kring, *mkring;
507 ND("%p: onoff %d", na, onoff);
510 /* parent left netmap mode, fatal */
511 D("%s: internal error", na->name);
515 for (i = 0; i < netmap_all_rings(na, t); i++) {
516 mkring = NMR(na, t)[i];
517 if (!nm_kring_pending_on(mkring))
519 mkring->nr_mode = NKR_NETMAP_ON;
523 if (i > nma_get_nrings(pna, s))
525 if (mna->flags & nm_txrx2flag(s)) {
526 kring = NMR(pna, s)[i];
527 netmap_monitor_add(mkring, kring, zmon);
532 na->na_flags |= NAF_NETMAP_ON;
534 if (na->active_fds == 0)
535 na->na_flags &= ~NAF_NETMAP_ON;
537 for (i = 0; i < netmap_all_rings(na, t); i++) {
538 mkring = NMR(na, t)[i];
539 if (!nm_kring_pending_off(mkring))
541 mkring->nr_mode = NKR_NETMAP_OFF;
544 /* we cannot access the parent krings if the parent
545 * has left netmap mode. This is signaled by a NULL
551 if (i > nma_get_nrings(pna, s))
553 if (mna->flags & nm_txrx2flag(s)) {
554 kring = NMR(pna, s)[i];
555 netmap_monitor_del(mkring, kring, s);
565 ****************************************************************
566 * functions specific for zero-copy monitors
567 ****************************************************************
571 * Common function for both zero-copy tx and rx nm_sync()
575 netmap_zmon_parent_sync(struct netmap_kring *kring, int flags, enum txrx tx)
577 struct netmap_kring *mkring = kring->zmon_list[tx].next;
578 struct netmap_ring *ring = kring->ring, *mring;
580 int rel_slots, free_slots, busy, sent = 0;
582 u_int lim = kring->nkr_num_slots - 1,
583 mlim; // = mkring->nkr_num_slots - 1;
585 if (mkring == NULL) {
586 RD(5, "NULL monitor on %s", kring->name);
589 mring = mkring->ring;
590 mlim = mkring->nkr_num_slots - 1;
592 /* get the relased slots (rel_slots) */
594 beg = kring->nr_hwtail + 1;
595 error = kring->mon_sync(kring, flags);
598 end = kring->nr_hwtail + 1;
600 beg = kring->nr_hwcur;
604 rel_slots = end - beg;
606 rel_slots += kring->nkr_num_slots;
609 /* no released slots, but we still need
610 * to call rxsync if this is a rx ring
615 /* we need to lock the monitor receive ring, since it
616 * is the target of bot tx and rx traffic from the monitored
619 mtx_lock(&mkring->q_lock);
620 /* get the free slots available on the monitor ring */
621 i = mkring->nr_hwtail;
622 busy = i - mkring->nr_hwcur;
624 busy += mkring->nkr_num_slots;
625 free_slots = mlim - busy;
630 /* swap min(free_slots, rel_slots) slots */
631 if (free_slots < rel_slots) {
632 beg += (rel_slots - free_slots);
633 rel_slots = free_slots;
635 if (unlikely(beg >= kring->nkr_num_slots))
636 beg -= kring->nkr_num_slots;
639 for ( ; rel_slots; rel_slots--) {
640 struct netmap_slot *s = &ring->slot[beg];
641 struct netmap_slot *ms = &mring->slot[i];
645 ms->buf_idx = s->buf_idx;
647 ND(5, "beg %d buf_idx %d", beg, tmp);
653 ms->flags = s->flags;
654 s->flags |= NS_BUF_CHANGED;
656 beg = nm_next(beg, lim);
657 i = nm_next(i, mlim);
661 mkring->nr_hwtail = i;
664 mtx_unlock(&mkring->q_lock);
667 /* notify the new frames to the monitor */
668 mkring->nm_notify(mkring, 0);
673 error = kring->mon_sync(kring, flags);
678 /* callback used to replace the nm_sync callback in the monitored tx rings */
680 netmap_zmon_parent_txsync(struct netmap_kring *kring, int flags)
682 return netmap_zmon_parent_sync(kring, flags, NR_TX);
685 /* callback used to replace the nm_sync callback in the monitored rx rings */
687 netmap_zmon_parent_rxsync(struct netmap_kring *kring, int flags)
689 return netmap_zmon_parent_sync(kring, flags, NR_RX);
693 netmap_zmon_reg(struct netmap_adapter *na, int onoff)
695 return netmap_monitor_reg_common(na, onoff, 1 /* zcopy */);
698 /* nm_dtor callback for monitors */
700 netmap_zmon_dtor(struct netmap_adapter *na)
702 struct netmap_monitor_adapter *mna =
703 (struct netmap_monitor_adapter *)na;
704 struct netmap_priv_d *priv = &mna->priv;
705 struct netmap_adapter *pna = priv->np_na;
707 netmap_adapter_put(pna);
711 ****************************************************************
712 * functions specific for copy monitors
713 ****************************************************************
717 netmap_monitor_parent_sync(struct netmap_kring *kring, u_int first_new, int new_slots)
721 for (j = 0; j < kring->n_monitors; j++) {
722 struct netmap_kring *mkring = kring->monitors[j];
724 int free_slots, busy, sent = 0, m;
725 u_int lim = kring->nkr_num_slots - 1;
726 struct netmap_ring *ring = kring->ring, *mring = mkring->ring;
727 u_int max_len = NETMAP_BUF_SIZE(mkring->na);
729 mlim = mkring->nkr_num_slots - 1;
731 /* we need to lock the monitor receive ring, since it
732 * is the target of bot tx and rx traffic from the monitored
735 mtx_lock(&mkring->q_lock);
736 /* get the free slots available on the monitor ring */
737 i = mkring->nr_hwtail;
738 busy = i - mkring->nr_hwcur;
740 busy += mkring->nkr_num_slots;
741 free_slots = mlim - busy;
746 /* copy min(free_slots, new_slots) slots */
749 if (free_slots < m) {
750 beg += (m - free_slots);
751 if (beg >= kring->nkr_num_slots)
752 beg -= kring->nkr_num_slots;
757 struct netmap_slot *s = &ring->slot[beg];
758 struct netmap_slot *ms = &mring->slot[i];
759 u_int copy_len = s->len;
760 char *src = NMB(kring->na, s),
761 *dst = NMB(mkring->na, ms);
763 if (unlikely(copy_len > max_len)) {
764 RD(5, "%s->%s: truncating %d to %d", kring->name,
765 mkring->name, copy_len, max_len);
769 memcpy(dst, src, copy_len);
771 ms->flags = s->flags;
774 beg = nm_next(beg, lim);
775 i = nm_next(i, mlim);
778 mkring->nr_hwtail = i;
780 mtx_unlock(&mkring->q_lock);
783 /* notify the new frames to the monitor */
784 mkring->nm_notify(mkring, 0);
789 /* callback used to replace the nm_sync callback in the monitored tx rings */
791 netmap_monitor_parent_txsync(struct netmap_kring *kring, int flags)
796 /* get the new slots */
797 if (kring->n_monitors > 0) {
798 first_new = kring->nr_hwcur;
799 new_slots = kring->rhead - first_new;
801 new_slots += kring->nkr_num_slots;
803 netmap_monitor_parent_sync(kring, first_new, new_slots);
805 if (kring->zmon_list[NR_TX].next != NULL) {
806 return netmap_zmon_parent_txsync(kring, flags);
808 return kring->mon_sync(kring, flags);
811 /* callback used to replace the nm_sync callback in the monitored rx rings */
813 netmap_monitor_parent_rxsync(struct netmap_kring *kring, int flags)
816 int new_slots, error;
818 /* get the new slots */
819 if (kring->zmon_list[NR_RX].next != NULL) {
820 error = netmap_zmon_parent_rxsync(kring, flags);
822 error = kring->mon_sync(kring, flags);
826 if (kring->n_monitors > 0) {
827 first_new = kring->mon_tail;
828 new_slots = kring->nr_hwtail - first_new;
830 new_slots += kring->nkr_num_slots;
832 netmap_monitor_parent_sync(kring, first_new, new_slots);
833 kring->mon_tail = kring->nr_hwtail;
838 /* callback used to replace the nm_notify() callback in the monitored rx rings */
840 netmap_monitor_parent_notify(struct netmap_kring *kring, int flags)
842 int (*notify)(struct netmap_kring*, int);
843 ND(5, "%s %x", kring->name, flags);
844 /* ?xsync callbacks have tryget called by their callers
845 * (NIOCREGIF and poll()), but here we have to call it
848 if (nm_kr_tryget(kring, 0, NULL)) {
849 /* in all cases, just skip the sync */
850 return NM_IRQ_COMPLETED;
852 if (kring->n_monitors > 0) {
853 netmap_monitor_parent_rxsync(kring, NAF_FORCE_READ);
855 if (nm_monitor_none(kring)) {
856 /* we are no longer monitoring this ring, so both
857 * mon_sync and mon_notify are NULL
859 notify = kring->nm_notify;
861 notify = kring->mon_notify;
864 return notify(kring, flags);
869 netmap_monitor_reg(struct netmap_adapter *na, int onoff)
871 return netmap_monitor_reg_common(na, onoff, 0 /* no zcopy */);
875 netmap_monitor_dtor(struct netmap_adapter *na)
877 struct netmap_monitor_adapter *mna =
878 (struct netmap_monitor_adapter *)na;
879 struct netmap_priv_d *priv = &mna->priv;
880 struct netmap_adapter *pna = priv->np_na;
882 netmap_adapter_put(pna);
886 /* check if req is a request for a monitor adapter that we can satisfy */
888 netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
889 struct netmap_mem_d *nmd, int create)
891 struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
892 struct nmreq_register preq;
893 struct netmap_adapter *pna; /* parent adapter */
894 struct netmap_monitor_adapter *mna;
895 struct ifnet *ifp = NULL;
897 int zcopy = (req->nr_flags & NR_ZCOPY_MON);
900 req->nr_flags |= (NR_MONITOR_TX | NR_MONITOR_RX);
902 if ((req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX)) == 0) {
906 /* this is a request for a monitor adapter */
908 ND("flags %lx", req->nr_flags);
910 /* First, try to find the adapter that we want to monitor.
911 * We use the same req, after we have turned off the monitor flags.
912 * In this way we can potentially monitor everything netmap understands,
913 * except other monitors.
915 memcpy(&preq, req, sizeof(preq));
916 preq.nr_flags &= ~(NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON);
917 hdr->nr_body = (uintptr_t)&preq;
918 error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
919 hdr->nr_body = (uintptr_t)req;
921 D("parent lookup failed: %d", error);
924 ND("found parent: %s", pna->name);
926 if (!nm_netmap_on(pna)) {
927 /* parent not in netmap mode */
928 /* XXX we can wait for the parent to enter netmap mode,
929 * by intercepting its nm_register callback (2014-03-16)
931 D("%s not in netmap mode", pna->name);
936 mna = nm_os_malloc(sizeof(*mna));
942 mna->priv.np_na = pna;
944 /* grab all the rings we need in the parent */
945 error = netmap_interp_ringid(&mna->priv, req->nr_mode, req->nr_ringid,
951 snprintf(mna->up.name, sizeof(mna->up.name), "%s/%s%s%s#%lu", pna->name,
953 (req->nr_flags & NR_MONITOR_RX) ? "r" : "",
954 (req->nr_flags & NR_MONITOR_TX) ? "t" : "",
957 /* the monitor supports the host rings iff the parent does */
958 mna->up.na_flags |= (pna->na_flags & NAF_HOST_RINGS);
959 /* a do-nothing txsync: monitors cannot be used to inject packets */
960 mna->up.nm_txsync = netmap_monitor_txsync;
961 mna->up.nm_rxsync = netmap_monitor_rxsync;
962 mna->up.nm_krings_create = netmap_monitor_krings_create;
963 mna->up.nm_krings_delete = netmap_monitor_krings_delete;
964 mna->up.num_tx_rings = 1; // XXX what should we do here with chained zmons?
965 /* we set the number of our rx_rings to be max(num_rx_rings, num_rx_rings)
968 mna->up.num_rx_rings = pna->num_rx_rings;
969 if (pna->num_tx_rings > pna->num_rx_rings)
970 mna->up.num_rx_rings = pna->num_tx_rings;
971 /* by default, the number of slots is the same as in
972 * the parent rings, but the user may ask for a different
975 mna->up.num_tx_desc = req->nr_tx_slots;
976 nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
977 1, NM_MONITOR_MAXSLOTS, NULL);
978 mna->up.num_rx_desc = req->nr_rx_slots;
979 nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
980 1, NM_MONITOR_MAXSLOTS, NULL);
982 mna->up.nm_register = netmap_zmon_reg;
983 mna->up.nm_dtor = netmap_zmon_dtor;
984 /* to have zero copy, we need to use the same memory allocator
985 * as the monitored port
987 mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
988 /* and the allocator cannot be changed */
989 mna->up.na_flags |= NAF_MEM_OWNER;
991 mna->up.nm_register = netmap_monitor_reg;
992 mna->up.nm_dtor = netmap_monitor_dtor;
993 mna->up.nm_mem = netmap_mem_private_new(
994 mna->up.num_tx_rings,
996 mna->up.num_rx_rings,
1001 if (mna->up.nm_mem == NULL)
1005 error = netmap_attach_common(&mna->up);
1007 D("attach_common error");
1011 /* remember the traffic directions we have to monitor */
1012 mna->flags = (req->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX | NR_ZCOPY_MON));
1015 netmap_adapter_get(*na);
1017 /* keep the reference to the parent */
1020 /* drop the reference to the ifp, if any */
1027 netmap_mem_put(mna->up.nm_mem);
1031 netmap_unget_na(pna, ifp);
1036 #endif /* WITH_MONITOR */