git: e2427c6917dd - main - IfAPI: Add iterator to complement if_foreach()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 23 Mar 2023 13:38:28 UTC
The branch main has been updated by jhibbits:
URL: https://cgit.FreeBSD.org/src/commit/?id=e2427c6917dd66265f1e02aef536f668df19a814
commit e2427c6917dd66265f1e02aef536f668df19a814
Author: Justin Hibbits <jhibbits@FreeBSD.org>
AuthorDate: 2023-03-16 20:24:56 +0000
Commit: Justin Hibbits <jhibbits@FreeBSD.org>
CommitDate: 2023-03-23 13:39:26 +0000
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
---
sys/net/if.c | 36 ++++++++++++++++++++++++++++++++++++
sys/net/if_var.h | 9 +++++++++
2 files changed, 45 insertions(+)
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)
{
diff --git a/sys/net/if_var.h b/sys/net/if_var.h
index 3e4d6c883c13..5f11998ecab8 100644
--- a/sys/net/if_var.h
+++ b/sys/net/if_var.h
@@ -684,6 +684,15 @@ typedef bool (*if_foreach_match_t)(if_t, void *);
int if_foreach(if_foreach_cb_t, void *);
int if_foreach_sleep(if_foreach_match_t, void *, if_foreach_cb_t, void *);
+/* Opaque iterator structure for iterating over interfaces. */
+struct if_iter {
+ void *context[4];
+};
+
+if_t if_iter_start(struct if_iter *);
+if_t if_iter_next(struct if_iter *);
+void if_iter_finish(struct if_iter *);
+
/* Functions */
void if_setinitfn(if_t ifp, if_init_fn_t);
void if_setinputfn(if_t ifp, if_input_fn_t);