From nobody Mon Mar 06 15:08:29 2023 X-Original-To: dev-commits-src-all@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 4PVhmG0Z3bz3x0QQ; Mon, 6 Mar 2023 15:08:30 +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 4PVhmF6qC3z3mvJ; Mon, 6 Mar 2023 15:08:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678115310; 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=0NwwhU8ZjXD9ATwXNIVS25tJ5igBbzIkK7+PGoSRZ/8=; b=xaQ7dmuqjbUPSXpe5uzFw6O4aRtoyvw349K+63ZbofQsnmQoltth/USKPzWyBhBBjx0UNO ALM242HDLFScleeKhJAJnQo58x6KgBOkBz5YbYWibvchpvNifVnwt0mnne9DB/oJlFCiEj Z7dpFbXa6ZI85ezHDrWT/gb1+s89WtoJJzL1ocDkVa5qvaAitx+VyjL+VEBRqcasgaPDSM BvqU5B5cRBAUyjfXqgnWlXJwcPWCVI+tQMdfNUAfT+kqes0+eyTj8RyvZM/5dMm6gFMc/B Wfsy66vgRzKYI91qv6mq5ctY/plyoFmcy0lYRnyhX9mMR3c0gjCRPPp9L+QisA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678115310; 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=0NwwhU8ZjXD9ATwXNIVS25tJ5igBbzIkK7+PGoSRZ/8=; b=mkkXHDgJgomIbZhqHTSX/AnsmKI032+eItQeEXDAXPZsm7OrnSeFfEb8XX0awPcQiXoqeC WoEl86bWkWn4PCjsPhJoZQJjJKs5yaplvRgDzKPXpg1+QAdIwH5A/bVm0xf6ajzTEQ85n4 Dk0YI4PEAu5zF4oOwWP1sKmWfazhTvtVqMAjjvvRkOnTdQkQdFRbnGf2XzCSYEn+U8pWx5 I468w9GI+y2+spmKKMbRUk9xo+lhK5A9/Bc7t6TT6FLMQFHwu9fK4hIsRvO1eZgMENX6n2 HyxIGGD6doLbFROkG2FQKiF5f39bX44XGlu9VaLyU/i1rkC+59YD/9IbCkWJUQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1678115310; a=rsa-sha256; cv=none; b=mlPG5pj/8CdaiceMrqQQJ4tqY5Jq/T9BxtmFqQO5lTBjFEh6B9CvdXfM3fMVAvtHdTx+YA 9U+Mvop56vXlkW0v+ypopUr4Mtz0dt1Dq2WNKQgYL0EULMt5ho/Ap97A/tV2YKLkn3xWZb p3MaPqbPz/YHFUoRq4MdqC3tFMzOy6FcaQzeGahmHONzTVrap4DLQhxJq++TKYbZlsDWH+ tNJlEsZfqIZdyanjT1QCM88LPTUjT9W8TKdQySNNckO+AFVb0xWZqTGr4S852Gji2jATsM Z0wLdmmM3zm3g9QEuCXvYGIhwg6O9wuBv3e4F9nH4pXPyeBrcP48Iva/kfHWPw== 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 4PVhmF5rJGz16Cn; Mon, 6 Mar 2023 15:08: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 326F8TTM076655; Mon, 6 Mar 2023 15:08:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 326F8TiR076654; Mon, 6 Mar 2023 15:08:29 GMT (envelope-from git) Date: Mon, 6 Mar 2023 15:08:29 GMT Message-Id: <202303061508.326F8TiR076654@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: df2b419a4105 - main - ifnet: add if_foreach_sleep() to allow ifnet iterations with sleep. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: df2b419a4105588384a89a57442ed6c6ca002455 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=df2b419a4105588384a89a57442ed6c6ca002455 commit df2b419a4105588384a89a57442ed6c6ca002455 Author: Alexander V. Chernikov AuthorDate: 2023-03-04 10:09:09 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-03-06 15:08:08 +0000 ifnet: add if_foreach_sleep() to allow ifnet iterations with sleep. Subscribers: imp, ae, glebius Differential Revision: https://reviews.freebsd.org/D38904 --- sys/net/if.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sys/net/if_var.h | 2 ++ 2 files changed, 67 insertions(+) diff --git a/sys/net/if.c b/sys/net/if.c index 58711061eb5e..f3ef822178ff 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -4535,6 +4535,71 @@ if_foreach(if_foreach_cb_t cb, void *cb_arg) return (error); } +/* + * Iterates over the list of interfaces, permitting callback function @cb to sleep. + * Stops iteration if @cb returns non-zero error code. + * Returns the last error code from @cb. + * @match_cb: optional match callback limiting the iteration to only matched interfaces + * @match_arg: argument to pass to @match_cb + * @cb: iteration callback + * @cb_arg: argument to pass to @cb + */ +int +if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb, + void *cb_arg) +{ + int match_count = 0, array_size = 16; /* 128 bytes for malloc */ + struct ifnet **match_array = NULL; + int error = 0; + + MPASS(cb); + + while (true) { + struct ifnet **new_array; + int new_size = array_size; + struct epoch_tracker et; + struct ifnet *ifp; + + while (new_size < match_count) + new_size *= 2; + new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK); + if (match_array != NULL) + memcpy(new_array, match_array, array_size * sizeof(void *)); + free(match_array, M_TEMP); + match_array = new_array; + array_size = new_size; + + match_count = 0; + NET_EPOCH_ENTER(et); + CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { + if (match_cb != NULL && !match_cb(ifp, match_arg)) + continue; + if (match_count < array_size) { + if (if_try_ref(ifp)) + match_array[match_count++] = ifp; + } else + match_count++; + } + NET_EPOCH_EXIT(et); + + if (match_count > array_size) { + for (int i = 0; i < array_size; i++) + if_rele(match_array[i]); + continue; + } else { + for (int i = 0; i < match_count; i++) { + if (error == 0) + error = cb(match_array[i], cb_arg); + if_rele(match_array[i]); + } + free(match_array, M_TEMP); + break; + } + } + + return (error); +} + 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 c9b2de736d10..3e4d6c883c13 100644 --- a/sys/net/if_var.h +++ b/sys/net/if_var.h @@ -680,7 +680,9 @@ typedef u_int if_addr_cb_t(void *, struct ifaddr *, u_int); u_int if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg); typedef int (*if_foreach_cb_t)(if_t, void *); +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 *); /* Functions */ void if_setinitfn(if_t ifp, if_init_fn_t);