From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ftboGsDP" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0A5CC9 for ; Tue, 5 Dec 2023 04:00:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701777635; 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=20GQcsknLlOrAs8uFrXzeujlWSijSdYal/a04hZduK0=; b=ftboGsDPriEI1WlInqR5jrZfbPBoGJhMSXPP/39zBCCxBSIw5cVFxYypv5aACEpi07pEQG 8S+A0scsd3DCcRQpJh2dTNx7aqy1geA3jy8EGKZPqAwp9Dl/23A8x422knqXFrbWHy/RN3 OlgESlEVu84bC2guaDJHPeGiRknQLTg= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-478-iMDMWNa3PiSUe61VpR9pTg-1; Tue, 05 Dec 2023 07:00:33 -0500 X-MC-Unique: iMDMWNa3PiSUe61VpR9pTg-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-54cf89b07b1so225485a12.1 for ; Tue, 05 Dec 2023 04:00:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701777632; x=1702382432; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=20GQcsknLlOrAs8uFrXzeujlWSijSdYal/a04hZduK0=; b=fvx0+Rwf8/1voMZm0ti/duV1JrwAo0lYmjLFONSo3fDEqqJh+3H7y2tnMmFzTsgEUi tpm+CMixHSLn3hnPKHSUaCcBuR2ObUyTyWiChnn+iQPJcrZ3tDiH/yJa+COHelXwpBAz LqiT9BCbRsGz8vhPQ0D/TvvNnF5EXlbtPdjvNk9k+i9fLfSmWFtsK57swUSHrZVC+XxQ e+gOgahsHIeXcdCN26SXihlJqbFaBI5Ckco45Kh8xLFaeX9je6ysIMNb4gylKsIZox/Y NUIK+jbPVrqvpPD8OTLHlVmZgeFFqK6VNEvR2HZIWUuxEvGafvgu/GUcEtTcbZKQGtZl lmrA== X-Gm-Message-State: AOJu0YxXtGNRgcPd611mZQlzwBQTIJGGwHnYjX3vULbft1QdZ6vZbgQn 9VPAgVle4kYyTMGTVtLQ66Kds2Nnx3DZbQKYeJiiB9TP/XnMXlPADFUrB7bgPhWRdDUzQDhVRrC T/+hq3x80sgbiI/dP X-Received: by 2002:a05:6402:430d:b0:54d:3838:490a with SMTP id m13-20020a056402430d00b0054d3838490amr816114edc.3.1701777632063; Tue, 05 Dec 2023 04:00:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IF9eONxd7d8LRO22GLsrViNctQNoTDf/CknACqwPnUGu/qVoAXLfeKj9F84EiNm67Wd5ElUDQ== X-Received: by 2002:a05:6402:430d:b0:54d:3838:490a with SMTP id m13-20020a056402430d00b0054d3838490amr816096edc.3.1701777631716; Tue, 05 Dec 2023 04:00:31 -0800 (PST) Received: from gerbillo.redhat.com (146-241-241-54.dyn.eolo.it. [146.241.241.54]) by smtp.gmail.com with ESMTPSA id n10-20020a056402060a00b0054c6a70a435sm986678edv.44.2023.12.05.04.00.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 04:00:31 -0800 (PST) Message-ID: <7e73dbfe6cad7d551516d02bb02881d885045498.camel@redhat.com> Subject: Re: [PATCHv3 net-next 01/14] selftests/net: add lib.sh From: Paolo Abeni To: Hangbin Liu , netdev@vger.kernel.org Cc: "David S. Miller" , Jakub Kicinski , Eric Dumazet , Shuah Khan , David Ahern , linux-kselftest@vger.kernel.org, Po-Hsu Lin , Guillaume Nault , Petr Machata , James Prestwood , Jaehee Park , Ido Schimmel , Justin Iurman , Xin Long , James Chapman Date: Tue, 05 Dec 2023 13:00:29 +0100 In-Reply-To: <20231202020110.362433-2-liuhangbin@gmail.com> References: <20231202020110.362433-1-liuhangbin@gmail.com> <20231202020110.362433-2-liuhangbin@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Sat, 2023-12-02 at 10:00 +0800, Hangbin Liu wrote: > Add a lib.sh for net selftests. This file can be used to define commonly > used variables and functions. Some commonly used functions can be moved > from forwarding/lib.sh to this lib file. e.g. busywait(). >=20 > Add function setup_ns() for user to create unique namespaces with given > prefix name. >=20 > Reviewed-by: Petr Machata > Signed-off-by: Hangbin Liu > --- > tools/testing/selftests/net/Makefile | 2 +- > tools/testing/selftests/net/forwarding/lib.sh | 27 +----- > tools/testing/selftests/net/lib.sh | 85 +++++++++++++++++++ > 3 files changed, 87 insertions(+), 27 deletions(-) > create mode 100644 tools/testing/selftests/net/lib.sh >=20 > diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftes= ts/net/Makefile > index 9274edfb76ff..14bd68da7466 100644 > --- a/tools/testing/selftests/net/Makefile > +++ b/tools/testing/selftests/net/Makefile > @@ -54,7 +54,7 @@ TEST_PROGS +=3D ip_local_port_range.sh > TEST_PROGS +=3D rps_default_mask.sh > TEST_PROGS +=3D big_tcp.sh > TEST_PROGS_EXTENDED :=3D in_netns.sh setup_loopback.sh setup_veth.sh > -TEST_PROGS_EXTENDED +=3D toeplitz_client.sh toeplitz.sh > +TEST_PROGS_EXTENDED +=3D toeplitz_client.sh toeplitz.sh lib.sh > TEST_GEN_FILES =3D socket nettest > TEST_GEN_FILES +=3D psock_fanout psock_tpacket msg_zerocopy reuseport_ad= dr_any > TEST_GEN_FILES +=3D tcp_mmap tcp_inq psock_snd txring_overwrite > diff --git a/tools/testing/selftests/net/forwarding/lib.sh b/tools/testin= g/selftests/net/forwarding/lib.sh > index e37a15eda6c2..8f6ca458af9a 100755 > --- a/tools/testing/selftests/net/forwarding/lib.sh > +++ b/tools/testing/selftests/net/forwarding/lib.sh > @@ -4,9 +4,6 @@ > ########################################################################= ###### > # Defines > =20 > -# Kselftest framework requirement - SKIP code is 4. > -ksft_skip=3D4 > - > # Can be overridden by the configuration file. > PING=3D${PING:=3Dping} > PING6=3D${PING6:=3Dping6} > @@ -41,6 +38,7 @@ if [[ -f $relative_path/forwarding.config ]]; then > source "$relative_path/forwarding.config" > fi > =20 > +source ../lib.sh > ########################################################################= ###### > # Sanity checks > =20 > @@ -395,29 +393,6 @@ log_info() > echo "INFO: $msg" > } > =20 > -busywait() > -{ > - local timeout=3D$1; shift > - > - local start_time=3D"$(date -u +%s%3N)" > - while true > - do > - local out > - out=3D$("$@") > - local ret=3D$? > - if ((!ret)); then > - echo -n "$out" > - return 0 > - fi > - > - local current_time=3D"$(date -u +%s%3N)" > - if ((current_time - start_time > timeout)); then > - echo -n "$out" > - return 1 > - fi > - done > -} > - > not() > { > "$@" > diff --git a/tools/testing/selftests/net/lib.sh b/tools/testing/selftests= /net/lib.sh > new file mode 100644 > index 000000000000..518eca57b815 > --- /dev/null > +++ b/tools/testing/selftests/net/lib.sh > @@ -0,0 +1,85 @@ > +#!/bin/bash > +# SPDX-License-Identifier: GPL-2.0 > + > +########################################################################= ###### > +# Defines > + > +# Kselftest framework requirement - SKIP code is 4. > +ksft_skip=3D4 > + > +########################################################################= ###### > +# Helpers > +busywait() > +{ > + local timeout=3D$1; shift > + > + local start_time=3D"$(date -u +%s%3N)" > + while true > + do > + local out > + out=3D$("$@") > + local ret=3D$? > + if ((!ret)); then > + echo -n "$out" > + return 0 > + fi > + > + local current_time=3D"$(date -u +%s%3N)" > + if ((current_time - start_time > timeout)); then > + echo -n "$out" > + return 1 > + fi > + done > +} > + > +cleanup_ns() > +{ > + local ns=3D"" > + local errexit=3D0 > + local ret=3D0 > + > + # disable errexit temporary > + if [[ $- =3D~ "e" ]]; then > + errexit=3D1 > + set +e > + fi > + > + for ns in "$@"; do > + ip netns delete "${ns}" &> /dev/null > + if ! busywait 2 ip netns list \| grep -vq "^$ns$" &> /dev/null; then > + echo "Warn: Failed to remove namespace $ns" > + ret=3D1 > + fi > + done > + > + [ $errexit -eq 1 ] && set -e > + return $ret > +} > + > +# setup netns with given names as prefix. e.g > +# setup_ns local remote > +setup_ns() > +{ > + local ns=3D"" > + local ns_name=3D"" > + local ns_list=3D"" > + for ns_name in "$@"; do > + # Some test may setup/remove same netns multi times > + if unset ${ns_name} 2> /dev/null; then > + ns=3D"${ns_name,,}-$(mktemp -u XXXXXX)" > + eval readonly ${ns_name}=3D"$ns" > + else > + eval ns=3D'$'${ns_name} > + cleanup_ns "$ns" > + > + fi > + > + if ! ip netns add "$ns"; then > + echo "Failed to create namespace $ns_name" > + cleanup_ns "$ns_list" > + return $ksft_skip > + fi > + ip -n "$ns" link set lo up > + ns_list=3D"$ns_list $ns" Side note for a possible follow-up: if you maintain $ns_list as global variable, and remove from such list the ns deleted by cleanup_ns, you could remove the cleanup trap from the individual test with something alike: final_cleanup_ns() { cleanup_ns $ns_list } trap final_cleanup_ns EXIT No respin needed for the above, could be a follow-up if agreed upon. Cheers, Paolo