From nobody Thu Mar 23 13:38:28 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Pj5yY27JGz41MJ0; Thu, 23 Mar 2023 13:38:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Pj5yY1bcxz4Sph; Thu, 23 Mar 2023 13:38:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679578709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NINlpA9a7obZHPZIirccWXWkmflr7USUZ5dlXbV6f4s=; b=Z0CZMm5Z0TR/J+pB/RdpbBE9FCHXL8vV675pErNp4Xt0nNPdl75E41za9AZxdgjNvwcGyV Beb2dzjdahLFeUlZRUWbX67jCxlzY7vE4+7D84v9nnnUwT22dL4oitWiHVwob55mGH0jMc t35eIFB+SS75QKL0c4lQCVFdMjM7c3omxbFF/OkFUG4G5nXx4yLwE4qQgWHFE1ydaMn5K5 WjKD1Ywp8DCmaoIHSISbJCXmFpEnz2U5P22U2yZW8iBXRKfYd5bfKnqLiG3F9ZfLDQvQpI p++ZkglOWqqWX2slwQmBbTGYv53okgGze7ly0D0r9TB0A9yhUje4HWZljG0Oxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679578709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NINlpA9a7obZHPZIirccWXWkmflr7USUZ5dlXbV6f4s=; b=StUGXsgQfmdMj1EtGE39hl/ZyHY7ze0ZqQstuKanL3H+jIBo+RNvLiezlltGdJhLM876dM V2cEDzY6tfHGvqjksH5OFonhBgr6BxGTVDzzvpg/dxhWqysJD/Hu3umjsqVH0tNMMdXWFS K8Z8XybswdGRRHEDTdevSBZDKaBpZc0vFNMGbe0jQVz5lK3Ug/FQkyq70T2M/ez05U3/E+ U8wrhwxZyqcgCHSyTtKFKLSwl+BZazcA7oEIUG79Bc+o/vKMiqI2YwTlzIEyoCm1xcvrLi JoSlFzDCtRE4ViBgMruH/2r5gjr5pTMr2pIRcEyxJb634UD5dK1duHSS0V7jOA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679578709; a=rsa-sha256; cv=none; b=LGM4Ik7/r9gidpkAyOER64rwxQijr/m5wKtDYxHBhV8OAMARc1+Z/3uJwCvArKsXl2TM9L U9qDnGn2fcmW4PD5th5e6tcfC4pGLP7LyWNqw2P/ytW7O1hkAtHbhxN6kPthk8A4R2sN1R 6itk3jCqt1dqlsgxEpWwX3p/DyaOIqkAGuEfJcSMb1oZOCzdiz7SGY9i2XapR05uQNQitp k1SyCL5ddRsTUN9UFh8wLMc0Wg+b4yxFo7lxOdzwsrty1MlU3oC70D8rjTzu/oEGRi1DtF nrZiZljqedNYpmQq1X9QfhOcMuNOU9Rk7ua2v4+FrG0gJtKyM3ouUqBw6UpTJg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Pj5yY0L9KzdSq; Thu, 23 Mar 2023 13:38:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 32NDcSpM096726; Thu, 23 Mar 2023 13:38:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32NDcSeb096725; Thu, 23 Mar 2023 13:38:28 GMT (envelope-from git) Date: Thu, 23 Mar 2023 13:38:28 GMT Message-Id: <202303231338.32NDcSeb096725@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Justin Hibbits Subject: git: e2427c6917dd - main - IfAPI: Add iterator to complement if_foreach() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhibbits X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e2427c6917dd66265f1e02aef536f668df19a814 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhibbits: URL: https://cgit.FreeBSD.org/src/commit/?id=e2427c6917dd66265f1e02aef536f668df19a814 commit e2427c6917dd66265f1e02aef536f668df19a814 Author: Justin Hibbits AuthorDate: 2023-03-16 20:24:56 +0000 Commit: Justin Hibbits 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);