From nobody Mon Mar 06 19:03:38 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 4PVnzf3hX0z3wG6L; Mon, 6 Mar 2023 19:03:42 +0000 (UTC) (envelope-from rpokala@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (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 "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PVnzf3BKDz4K1t; Mon, 6 Mar 2023 19:03:42 +0000 (UTC) (envelope-from rpokala@freebsd.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678129422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7ZmAxv0btZdamQgVRGUb8jGox0pJrQm6wVTyMdROjoE=; b=AUCkN5k4khHoV9uEr+YXfyFZisDRoFpz724bvXUgJT2AvyjrvGUIIOoPGr9dlG4DurEeo4 rWmNfO4LCVE4gr2FIgYqXVkYDnOStMWapPfXcgzVMm9cnYkuBZq08Pofwp/FXDiQ1UQQzm R7WVOtCpmECCxKuQF2p+BuJzuz5UJHoD4SIR00Zzw1KKHbNeByBYwuyjXusjorQD/gbkW+ n4/9KrfgKZt6hTwyQjeHho+REXu3JRy7YMRELBh5EpdHmhByeLM3gupBmfPw2gX3Fttzpf H/HlYTNdkZ3l1X189YlE7DA6aWjs8HBQ3IA6pLTQhJP9RVwTyfXgnQU9wDo++A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678129422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7ZmAxv0btZdamQgVRGUb8jGox0pJrQm6wVTyMdROjoE=; b=WFBwh3x9fx6vQjSkxtxYJYSH2cSWuBQVy/J3B7kr522dgMARxXQzR9jZUHOohqgFRZSuyu pcMRO49R0royfd4uJlve97wuWpHe8lFRI8m/6FuSsFu2rrTgTb6WJTzEAEzP7v3XNsWSOF mQHPqU4B958P0UBaDquvMUEPjusPh/7LVUytFq4bHuTrRMdu5DNEpYOIPgFJp/7WhDFdFq EGSPEcmUV6RKTM8ndvPRHm0PJz/lIupMixK+PV/FvOeeIkesNvbZKvBHk1q1S5Uf9tNbpb /RR0wx37QGL+6+VJHUCWn8MS6yZQmuP2SvdZJg4jXpQGHPCJuTnpUEZ6S3K1QQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1678129422; a=rsa-sha256; cv=none; b=aLFHrHDM/uyGhWbBaSa/Bh8BqzcbLKv12AldS+LuR4tAXrVYsYkBKFhPpDbHXF/5evoIzj lndgAdhQzaFrQ1Gyw5wSSUJyGGQfXxF5wmoH+BJr553a1qWD6PeC3YXzl3DRKJwmSE4lRN brmFa41uPaJdmFaeEbd2KLxgt7T3z24hLvgPgXeateNeW1vN+HhUDE4udhG9HEASCaiuck 5jLAkLw9OxY97zc2BOeK3zAJuGiPNX408txBP2BgAj9nvXY36/9xBYrEdFiTe+AUYuFZia d+yRoUOJq9KdJjhVjaK5w8SeQqvEaHtfmhLA4qW3k33n2o6QyiFwog+4UNjLIw== Received: from [172.17.133.71] (unknown [207.141.91.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) (Authenticated sender: rpokala) by smtp.freebsd.org (Postfix) with ESMTPSA id 4PVnzd64yzz1CKN; Mon, 6 Mar 2023 19:03:41 +0000 (UTC) (envelope-from rpokala@freebsd.org) User-Agent: Microsoft-MacOutlook/16.70.23021201 Date: Mon, 06 Mar 2023 11:03:38 -0800 Subject: Re: df2b419a4105 - main - ifnet: add if_foreach_sleep() to allow ifnet iterations with sleep. From: Ravi Pokala To: Alexander Chernikov CC: "" , "" , "" Message-ID: Thread-Topic: df2b419a4105 - main - ifnet: add if_foreach_sleep() to allow ifnet iterations with sleep. References: <202303061508.326F8TiR076654@gitrepo.freebsd.org> <6CBFEAF8-1873-4DD3-B359-C7E972F6D7CD@FreeBSD.org> In-Reply-To: <6CBFEAF8-1873-4DD3-B359-C7E972F6D7CD@FreeBSD.org> 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: quoted-printable X-ThisMailContainsUnwantedMimeParts: N > Naming is hard :-) IMO if_foreach_sleepable marks the intent better . Min= d adding you suggestion to the original review? If there are no opposing con= cerns after a couple of days, I=E2=80=99ll rename it. Done. Thank you! :-) -Ravi (rpokala@) =EF=BB=BF-----Original Message----- From: Alexander Chernikov > Date: 2023-03-06, Monday at 09:06 To: Ravi Pokala > Cc: ">" >, ">" >, ">" > Subject: Re: df2b419a4105 - main - ifnet: add if_foreach_sleep() to allow i= fnet iterations with sleep. > On 6 Mar 2023, at 16:35, Ravi Pokala > wrote: >=20 > Hi Alexander, Hi Ravi, >=20 > Knowing ~nothing about ifnet, if I were to see 'if_foreach_sleep()', my f= irst thought would be that the loop itself was sleeping between each iterati= on, not that the callback was allowed to sleep. Perhaps this should be renam= ed to 'if_foreach_sleepable()=E2=80=99? Naming is hard :-) IMO if_foreach_sleepable marks the intent better . Mind = adding you suggestion to the original review? If there are no opposing conce= rns after a couple of days, I=E2=80=99ll rename it. >=20 > Just a thought. >=20 > Thanks, >=20 > Ravi (rpokala@) >=20 > =EF=BB=BF-----Original Message----- > From: >> on behalf of "Alexander V. Chernikov" >> > Date: 2023-03-06, Monday at 07:08 > To: >>, >>, <= dev-commits-src-main@FreeBSD.org <= mailto:dev-commits-src-main@FreeBSD.org >> > Subject: git: df2b419a4105 - main - ifnet: add if_foreach_sleep() to allo= w ifnet iterations with sleep. >=20 >=20 > The branch main has been updated by melifaro: >=20 >=20 > URL: https://cgit.FreeBSD.org/src/commit/?id=3Ddf2b419a4105588384a89a57442e= d6c6ca002455 >=20 >=20 > 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 >=20 >=20 > ifnet: add if_foreach_sleep() to allow ifnet iterations with sleep. >=20 >=20 > Subscribers: imp, ae, glebius >=20 >=20 > Differential Revision: https://reviews.freebsd.org/D38904 > --- > sys/net/if.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++= + > sys/net/if_var.h | 2 ++ > 2 files changed, 67 insertions(+) >=20 >=20 > 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); > } >=20 >=20 > +/* > + * Iterates over the list of interfaces, permitting callback function @c= b 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 mat= ched 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_foreac= h_cb_t cb, > + void *cb_arg) > +{ > + int match_count =3D 0, array_size =3D 16; /* 128 bytes for malloc */ > + struct ifnet **match_array =3D NULL; > + int error =3D 0; > + > + MPASS(cb); > + > + while (true) { > + struct ifnet **new_array; > + int new_size =3D array_size; > + struct epoch_tracker et; > + struct ifnet *ifp; > + > + while (new_size < match_count) > + new_size *=3D 2; > + new_array =3D malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK); > + if (match_array !=3D NULL) > + memcpy(new_array, match_array, array_size * sizeof(void *)); > + free(match_array, M_TEMP); > + match_array =3D new_array; > + array_size =3D new_size; > + > + match_count =3D 0; > + NET_EPOCH_ENTER(et); > + CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { > + if (match_cb !=3D NULL && !match_cb(ifp, match_arg)) > + continue; > + if (match_count < array_size) { > + if (if_try_ref(ifp)) > + match_array[match_count++] =3D ifp; > + } else > + match_count++; > + } > + NET_EPOCH_EXIT(et); > + > + if (match_count > array_size) { > + for (int i =3D 0; i < array_size; i++) > + if_rele(match_array[i]); > + continue; > + } else { > + for (int i =3D 0; i < match_count; i++) { > + if (error =3D=3D 0) > + error =3D 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); >=20 >=20 > 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 *= ); >=20 >=20 > /* Functions */ > void if_setinitfn(if_t ifp, if_init_fn_t); >=20 >=20 >=20 >=20