aboutsummaryrefslogtreecommitdiff
path: root/sys/net/if.c
diff options
context:
space:
mode:
authorJustin Hibbits <jhibbits@FreeBSD.org>2023-03-16 20:24:56 +0000
committerJustin Hibbits <jhibbits@FreeBSD.org>2023-03-23 13:39:26 +0000
commite2427c6917dd66265f1e02aef536f668df19a814 (patch)
treea79133505a2fcab4f46baa75ff64830bae865974 /sys/net/if.c
parent2af158ae8313b250077f5ba60dc41fbed4e711af (diff)
downloadsrc-e2427c6917dd66265f1e02aef536f668df19a814.tar.gz
src-e2427c6917dd66265f1e02aef536f668df19a814.zip
IfAPI: Add iterator to complement if_foreach()
Summary: Sometimes an if_foreach() callback can be trivial, or need a lot of outer context. In this case a regular `for` loop makes more sense. To keep things hidden in the new API, use an opaque `if_iter` structure that can still be instantiated on the stack. The current implementation uses just a single pointer out of the 4 alotted to the opaque context, and the cleanup does nothing, but may be used in the future. Reviewed by: melifaro Sponsored by: Juniper Networks, Inc. Differential Revision: https://reviews.freebsd.org/D39138
Diffstat (limited to 'sys/net/if.c')
-rw-r--r--sys/net/if.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/sys/net/if.c b/sys/net/if.c
index f3ef822178ff..ff942bceb090 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -4600,6 +4600,42 @@ if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t c
return (error);
}
+
+/*
+ * Uses just 1 pointer of the 4 available in the public struct.
+ */
+if_t
+if_iter_start(struct if_iter *iter)
+{
+ if_t ifp;
+
+ NET_EPOCH_ASSERT();
+
+ bzero(iter, sizeof(*iter));
+ ifp = CK_STAILQ_FIRST(&V_ifnet);
+ if (ifp != NULL)
+ iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
+ else
+ iter->context[0] = NULL;
+ return (ifp);
+}
+
+if_t
+if_iter_next(struct if_iter *iter)
+{
+ if_t cur_ifp = iter->context[0];
+
+ if (cur_ifp != NULL)
+ iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
+ return (cur_ifp);
+}
+
+void
+if_iter_finish(struct if_iter *iter)
+{
+ /* Nothing to do here for now. */
+}
+
u_int
if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
{