From patchwork Mon Apr 15 06:31:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629451 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33AC61CFA0; Mon, 15 Apr 2024 06:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162696; cv=none; b=JiBPZNgl4YsGQzGvY6S/jx3paeCdtch+/CokXOYs0jw8z0xVqdCnfk7D1h1A24G+p4SQwL8vEaaRQHP0WHFHpMR0xaiRpec3HDJtfziJiOyFqEc6ILB+XvGiW5WRki/fUnPPt3aLvTUEJ6MBlUl0LYGG0M05L46UgQEtC4anWA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162696; c=relaxed/simple; bh=lVQ27NIhN96wOrSBXkEwwp4P0gP9idgKCSLnupHHGBs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GmUCj932W+26FooYVcj4HcbOYZM9kTXS0wBf7Bw8QOC0UkCWvEd757S6Or02yJUdu/6qx3/m/2yZPUONQlzs5gUC/6II3RgByoIdaaaml7sUWUldgg6SMpcRAzncbG2Vs311qFlzS0M9D3TTOVIisHyeAs++PAdTfDo+XJ7GyMk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kFJbQVlv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kFJbQVlv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1E0B1C113CC; Mon, 15 Apr 2024 06:31:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162695; bh=lVQ27NIhN96wOrSBXkEwwp4P0gP9idgKCSLnupHHGBs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kFJbQVlvKjOsMb0PDoUm2OyO5A0Ik3BOcTfb4AMwB33fIXYbroJ4yhrYSjNCAAkNn GSXza7CABHSBvb6MmZ7IyfMtcCN5TSKUPIOcHxjQdqV0Fq7N0EsnBKpfoEaZwayPBz pUspRMs15ZDEC99/QqaSKBhHQP3KhKHP8pPz1APDNNpVedSDhET2Ot4umD10KhMzQ3 tFTrQT05H/spx26r8HNp/at4Xz2UX+tUv+st9YnresEsKJIZi0F9640AT0E2eJf6Ue CpFn+aQCXir9vzgIbRXK7WeZXDS2Ja4LgrWagiURtrZ3iSFD/IhYxrKa+JlCCRqD3s dQT7l7fQRZNZQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 1/9] selftests/bpf: Update arguments of connect_to_addr Date: Mon, 15 Apr 2024 14:31:10 +0800 Message-Id: <8f9c68db04baafd8945b8eb1454c842c4b9467d1.1713161974.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Move the third argument "int type" of connect_to_addr() to the first one which is closer to how the socket syscall is doing it. And change its usages in sock_addr.c too. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 2 +- tools/testing/selftests/bpf/prog_tests/sock_addr.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 2cbf07232f17..3759948439a7 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -258,7 +258,7 @@ static int connect_fd_to_addr(int fd, return 0; } -int connect_to_addr(const struct sockaddr_storage *addr, socklen_t addrlen, int type) +int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen) { int fd; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 70f4e4c92733..ac4da5fdcc95 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -54,7 +54,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, __u16 port, int timeout_ms, unsigned int nr_listens); void free_fds(int *fds, unsigned int nr_close_fds); -int connect_to_addr(const struct sockaddr_storage *addr, socklen_t len, int type); +int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len); int connect_to_fd(int server_fd, int timeout_ms); int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); diff --git a/tools/testing/selftests/bpf/prog_tests/sock_addr.c b/tools/testing/selftests/bpf/prog_tests/sock_addr.c index 5fd617718991..b182667e78c9 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_addr.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_addr.c @@ -328,7 +328,7 @@ static void test_bind(struct sock_addr_test *test) goto cleanup; /* Try to connect to server just in case */ - client = connect_to_addr(&expected_addr, expected_addr_len, test->socket_type); + client = connect_to_addr(test->socket_type, &expected_addr, expected_addr_len); if (!ASSERT_GE(client, 0, "connect_to_addr")) goto cleanup; @@ -357,7 +357,7 @@ static void test_connect(struct sock_addr_test *test) if (!ASSERT_EQ(err, 0, "make_sockaddr")) goto cleanup; - client = connect_to_addr(&addr, addr_len, test->socket_type); + client = connect_to_addr(test->socket_type, &addr, addr_len); if (!ASSERT_GE(client, 0, "connect_to_addr")) goto cleanup; @@ -538,7 +538,7 @@ static void test_getpeername(struct sock_addr_test *test) if (!ASSERT_EQ(err, 0, "make_sockaddr")) goto cleanup; - client = connect_to_addr(&addr, addr_len, test->socket_type); + client = connect_to_addr(test->socket_type, &addr, addr_len); if (!ASSERT_GE(client, 0, "connect_to_addr")) goto cleanup; From patchwork Mon Apr 15 06:31:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629452 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97AFA1CF8A; Mon, 15 Apr 2024 06:31:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162702; cv=none; b=oVbG8+F9H0bPoSpGZAILMAt1ct6J2S9WX3A3PADUiZV80rwAUS+q4gGSyf1g8768YiDjP+zP31XAEr6a7u8OddtAt6wA7hOUjQR/CEDhekH0PnFElBtFoSZJzziEqo9p0WT3he4VaYQknWPrhwVFWTbFBfsnMba6EPi2zo4PeLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162702; c=relaxed/simple; bh=fKbXIiKuYssY4VB9Gno3DXKEvI8risDYWZFnED80jcU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ryADD2ASKHlY08CcjF8kTs4+ivxQeq9idZZnk0SKWbrMUKxpkWi9JRriCcMWuLZhXN/Fd1LInJE/2wVYr1TKCggKebTKc8Z89TYR+ZiGYJh12lcCuDFkcD8k7KnwX26tORRadA8VItLPkFybWC2Nomr4D+cmnXggp935P8sX0i0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nhOUIsaW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nhOUIsaW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C992C113CC; Mon, 15 Apr 2024 06:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162702; bh=fKbXIiKuYssY4VB9Gno3DXKEvI8risDYWZFnED80jcU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nhOUIsaWZfsRh6b0uWNP+V/46kLR+ImeFMIcGXkWCLz7nKT9avbw9zC4gMYUgpKXv L09+gevU8DzXUklHalFBqgoRdhUuStesPiyoqDNSXjuDH72j8CWKBt4h/uyYFB9vS1 tgeg/bMbe7PKnO/T3EMoZy/NNzrjZ9IhJa9Gs2L6p/DF+DZ5SO2pTE2D4TIX391r+d +5B9dLjQIf4htR0kFLI548Ut1oCyORy6CAUzPnNcsMY4OtOM8xc4wUn8iOau3EYiq1 WWRyXwLuoXhpFHeJhaiquxJdRAK1h32g/YVP8N6bUZ6tgNmUDoMVLb+Zc0vYdI67GV KOWstHp3+8sNw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 2/9] selftests/bpf: Add start_server_addr* helpers Date: Mon, 15 Apr 2024 14:31:11 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang In order to pair up with connect_to_addr(), this patch adds a new helper start_server_addr(), and another one start_server_addr_opts(), which is a wrapper of __start_server(), only added a network_helper_opts arg at the end. They all accept an argument 'addr' of 'struct sockaddr_storage' type instead of a string type argument like start_server(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 16 ++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 3759948439a7..92c980b18afd 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -185,6 +185,22 @@ int *start_reuseport_server(int family, int type, const char *addr_str, return NULL; } +int start_server_addr_opts(int type, const struct sockaddr_storage *addr, socklen_t len, + const struct network_helper_opts *opts) +{ + return __start_server(type, 0, (struct sockaddr *)addr, len, + opts->timeout_ms, 0); +} + +int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len) +{ + struct network_helper_opts opts = { + .timeout_ms = 0, + }; + + return start_server_addr_opts(type, addr, len, &opts); +} + void free_fds(int *fds, unsigned int nr_close_fds) { if (fds) { diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index ac4da5fdcc95..9e6fcc89a8d0 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -53,6 +53,9 @@ int start_mptcp_server(int family, const char *addr, __u16 port, int *start_reuseport_server(int family, int type, const char *addr_str, __u16 port, int timeout_ms, unsigned int nr_listens); +int start_server_addr_opts(int type, const struct sockaddr_storage *addr, socklen_t len, + const struct network_helper_opts *opts); +int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t len); void free_fds(int *fds, unsigned int nr_close_fds); int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len); int connect_to_fd(int server_fd, int timeout_ms); From patchwork Mon Apr 15 06:31:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629453 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 064B31BF53; Mon, 15 Apr 2024 06:31:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162709; cv=none; b=kKS22033kpe3b4aVEUGajXJCovzipf4iFFxmqatNnmkqd8VWBsp/IpvA1HzXD7m1qdxmyHDuf7x3BUE/NZkKNDd3rE+/jENYWmKeNJEgWTleW3uc9e/LazZiSu1OBAdQ6QpSs9ZfgfyVDc7YIhlGTHmQ6R4mLri9477L4Eq/zXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162709; c=relaxed/simple; bh=NHEB9TokBCk2Zcvs5aYjfmxZ3CANdUFDFp4WT/vneAU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=smrVZCsvGFSYvWL8invLTfnvZyQt5V9tCX1msk0n01mPYM5gAI62UDIJT6awxaiCjTHbDhqvbu3iobMyPKVTcgiRvtercNY9X3Iv3Qje2AwyTmA8swjEYtMnjrakPx/I9OpeMR3KgWl4oyT7j9p9JecArMwJPfWV6SQD6hbbWQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ue7TP1Y0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ue7TP1Y0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8104C2BD11; Mon, 15 Apr 2024 06:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162708; bh=NHEB9TokBCk2Zcvs5aYjfmxZ3CANdUFDFp4WT/vneAU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ue7TP1Y09SRvxEyZiCBdv56CTK/mFPrQ14Ufv+fqzFHmmCWrXkip07+x7NE+d5gh0 vJER4KfS+hqyuQQv7aNvWpj78AWdCXccFhT4Jg9VCjPbxoka9dKqiQL7YV9Z8OOuJY 8UGavu2cG2qL6TnCe+Y5gIlOE/NLbQjSlQVBOFL1vv8OgqZ4a4ThKWB43/3pRhmtsk +kFZscxxV9IBLJSdqqqB6e43Wp06PMDng7WlNwgrsXSPzl9Nx998cwvwHf1wDWHvFJ LH2dj2/1JY5VNS9GDiL+s8/lFiEqHCxswRKo2Mp+IVLS1UF7n6vQ+M2D1CXKkIGsDB VzKZgVm19omsw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 3/9] selftests/bpf: Use start_server_addr in cls_redirect Date: Mon, 15 Apr 2024 14:31:12 +0800 Message-Id: <6b14b2c38ebfd1221555d3de3d413a0460282712.1713161975.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Include network_helpers.h in prog_tests/cls_redirect.c, use the newly added public helper start_server_addr() instead of the local defined function start_server(). This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/cls_redirect.c | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/cls_redirect.c b/tools/testing/selftests/bpf/prog_tests/cls_redirect.c index 2a55f717fc07..4050d470d2a2 100644 --- a/tools/testing/selftests/bpf/prog_tests/cls_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/cls_redirect.c @@ -10,6 +10,7 @@ #include #include +#include "network_helpers.h" #include "progs/test_cls_redirect.h" #include "test_cls_redirect.skel.h" @@ -35,23 +36,6 @@ struct tuple { struct addr_port dst; }; -static int start_server(const struct sockaddr *addr, socklen_t len, int type) -{ - int fd = socket(addr->sa_family, type, 0); - if (CHECK_FAIL(fd == -1)) - return -1; - if (CHECK_FAIL(bind(fd, addr, len) == -1)) - goto err; - if (type == SOCK_STREAM && CHECK_FAIL(listen(fd, 128) == -1)) - goto err; - - return fd; - -err: - close(fd); - return -1; -} - static int connect_to_server(const struct sockaddr *addr, socklen_t len, int type) { @@ -98,7 +82,7 @@ static bool set_up_conn(const struct sockaddr *addr, socklen_t len, int type, socklen_t slen = sizeof(ss); struct sockaddr *sa = (struct sockaddr *)&ss; - *server = start_server(addr, len, type); + *server = start_server_addr(type, (struct sockaddr_storage *)addr, len); if (*server < 0) return false; From patchwork Mon Apr 15 06:31:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629454 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0996914F62; Mon, 15 Apr 2024 06:31:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162716; cv=none; b=qHhDnCHuz+xS/EIJ1sMFs1gQiipQbh5/YpZaPfPcv/8D4VVDg3iymi9IxNU+Jhc5xRXL3MlNp8DSpAa65+qkJ9d6KBoWLj2eUnH4/VyxqJVXMne5Tzq3PmmiozI+bvG7zXSFh2/8Xaj/hqguTad1XnecOdmFf1Cv9q6pdIcQgJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162716; c=relaxed/simple; bh=iu9Nt/eRnvrJD5oDNxZeMq9GCmOe6kMmAtqO73M1w5w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dR4N+JO3jVg5pseNDtFb2WWyoiRhgKWz4KcD+xC1ZJvd2oda0rLE45naYa04vs6zYDXu65nyPz5xmQXsVEGwPuebyoimF7UPm3mtccpvE6u/aBpO0rcDTl9C7OrNa/IrXSkPld/+Go3a4nOKqkYTDtystLQfT9YH7Wef5adA8sM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L7CLJp/K; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="L7CLJp/K" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29D88C113CC; Mon, 15 Apr 2024 06:31:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162715; bh=iu9Nt/eRnvrJD5oDNxZeMq9GCmOe6kMmAtqO73M1w5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L7CLJp/KAmPqKnc/bv22fqFzIaQyZ6lJe/HeWuRGHHvZxjzLW6JuQWqH16cy1VamM wrkX82X7chPiMr3ww6xIKH0jME5RkWuQFsPAfBK/xbsE0tDcH00e+U2I9gqvB7Rhnv zIr9rlH1YoQCCxVw+ymplSLHgbR9aUgpOCpbL8vGmcYprj/xKuDIM8lj06BsVVNBKe PJl6CIRJiij61A1IS+DXNa/3WGu3rYHM9PBCFNVlG+jo34MEee8+31TyBxSKvPkiMY ENiXGIHCsT4a3AO26ZzW8wnbDGGFoUyChet430WHArxtqj9Eu0CvTcDtcoI+XSGoEr NHPF3i9vOYvhw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 4/9] selftests/bpf: Use connect_to_addr in cls_redirect Date: Mon, 15 Apr 2024 14:31:13 +0800 Message-Id: <7fbfb8bcdeb1f0bec699f351a715b869857591c3.1713161975.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch uses public helper connect_to_addr() exported in network_helpers.h instead of the local defined function connect_to_server() in prog_tests/cls_redirect.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/cls_redirect.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/cls_redirect.c b/tools/testing/selftests/bpf/prog_tests/cls_redirect.c index 4050d470d2a2..9aa2f3b12884 100644 --- a/tools/testing/selftests/bpf/prog_tests/cls_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/cls_redirect.c @@ -36,22 +36,6 @@ struct tuple { struct addr_port dst; }; -static int connect_to_server(const struct sockaddr *addr, socklen_t len, - int type) -{ - int fd = socket(addr->sa_family, type, 0); - if (CHECK_FAIL(fd == -1)) - return -1; - if (CHECK_FAIL(connect(fd, addr, len))) - goto err; - - return fd; - -err: - close(fd); - return -1; -} - static bool fill_addr_port(const struct sockaddr *sa, struct addr_port *ap) { const struct sockaddr_in6 *in6; @@ -89,7 +73,7 @@ static bool set_up_conn(const struct sockaddr *addr, socklen_t len, int type, if (CHECK_FAIL(getsockname(*server, sa, &slen))) goto close_server; - *conn = connect_to_server(sa, slen, type); + *conn = connect_to_addr(type, (struct sockaddr_storage *)sa, slen); if (*conn < 0) goto close_server; From patchwork Mon Apr 15 06:31:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629455 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1004314A84; Mon, 15 Apr 2024 06:32:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162722; cv=none; b=cxMduys9z1rZKLeR0RqRT25NOMPy4j89Q4SE7N2BkGYtbMCYsYOJM44xiUEvFsolL2F5TlwvYR+UMgRnV7PO5alBYgJsAAY8eeKs0/fiHfMct1S4NHS1UAyaWz27mFJ4V3x51zS2XXEfxV6YvWOatjU0CJAnrO73THPMoiNT21E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162722; c=relaxed/simple; bh=54jFizVFFf8NBRR2CCv6ylZ9Ap71aVlDuz+dvVztuuQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=THyup5VElaZkPhcN6TZ3Xqbfhc2NbR3rwMHYtqL/qcRW28F9m62tBUzAP2o6xSlEj3mLK3EXPW2ni//kNlZFa7i5CE9GozSdgkBN16Jj3E6iZHt04EcJk98xm6p6Xf1mydJs2HFU9SsrSzNPYR0zybafFJdZ/nEnQTpZSH7lNlA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GGwxwatN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GGwxwatN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F7A4C2BD11; Mon, 15 Apr 2024 06:31:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162721; bh=54jFizVFFf8NBRR2CCv6ylZ9Ap71aVlDuz+dvVztuuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GGwxwatNr0mDNPv963n/RBppSt2KmWo+qgwv3bON1HqiZvx+cPmukaAU8dEh1jmaf zIFWg0tSJtrazm39KzfJ3A0KidIlfqTpKerCm8Rjj+QHvCp0go/GcoJfv5Pr1sREUo D13lUJTahVXCtIgC/Uim2OFq9QEFo+tZA16TVYONuvG0flD1QbmjkLfjaAMx0bc/8I gt9y8T4F7xHtVb0UW6P8lB3mA1hj6aYlSooYxKeQTiH+kyABEE+3GA+4vVyfTu8rX1 CzhCJFWg0R8Fu6duzxuL4PB/TROvudZ2vzWNTAe6FC03BX27bYisrZ3PWmXjRmpS9F RPvBiHnD4e2hQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 5/9] selftests/bpf: Use start_server_addr in sk_assign Date: Mon, 15 Apr 2024 14:31:14 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Include network_helpers.h in prog_tests/sk_assign.c, use the newly added public helper start_server_addr() instead of the local defined function start_server(). This can avoid duplicate code. The code that sets SO_RCVTIMEO timeout as timeo_sec (3s) can be dropped, since start_server_addr() sets default timeout as 3s. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_assign.c | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c index 1374b626a985..70c1a5893830 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c @@ -15,6 +15,7 @@ #include #include "test_progs.h" +#include "network_helpers.h" #define BIND_PORT 1234 #define CONNECT_PORT 4321 @@ -73,30 +74,6 @@ configure_stack(void) return true; } -static int -start_server(const struct sockaddr *addr, socklen_t len, int type) -{ - int fd; - - fd = socket(addr->sa_family, type, 0); - if (CHECK_FAIL(fd == -1)) - goto out; - if (CHECK_FAIL(setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, - timeo_optlen))) - goto close_out; - if (CHECK_FAIL(bind(fd, addr, len) == -1)) - goto close_out; - if (type == SOCK_STREAM && CHECK_FAIL(listen(fd, 128) == -1)) - goto close_out; - - goto out; -close_out: - close(fd); - fd = -1; -out: - return fd; -} - static int connect_to_server(const struct sockaddr *addr, socklen_t len, int type) { @@ -302,15 +279,15 @@ void test_sk_assign(void) for (i = 0; i < ARRAY_SIZE(tests) && !READ_ONCE(stop); i++) { struct test_sk_cfg *test = &tests[i]; - const struct sockaddr *addr; + const struct sockaddr_storage *addr; const int zero = 0; int err; if (!test__start_subtest(test->name)) continue; prepare_addr(test->addr, test->family, BIND_PORT, false); - addr = (const struct sockaddr *)test->addr; - server = start_server(addr, test->len, test->type); + addr = (const struct sockaddr_storage *)test->addr; + server = start_server_addr(test->type, addr, test->len); if (server == -1) goto close; @@ -323,7 +300,7 @@ void test_sk_assign(void) /* connect to unbound ports */ prepare_addr(test->addr, test->family, CONNECT_PORT, test->rewrite_addr); - if (run_test(server, addr, test->len, test->type)) + if (run_test(server, test->addr, test->len, test->type)) goto close; close(server); From patchwork Mon Apr 15 06:31:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629456 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C5C74689; Mon, 15 Apr 2024 06:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162728; cv=none; b=JHt0I3KbArHv2ShU83WJPETRERN9JmMIARXV5f7nru6HXydZcl9BnmR5Sd68nDa50lnJ9yUPWpn3Pe30+jJQROb6nb+2Oqn8XLf9H+XKbh7D4FNy/2VKLjnDqfe7TybaYYMSBYBDMHTn3MZ2Oc1Z23eOj+jTd+0u62i90GcjeXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162728; c=relaxed/simple; bh=rHc0ZaD+/SOM6afPT4tmEZZK6h9YUARUauoAo1057pA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iyC2oBySUKthuHtQopaziZ+1RyqtHfHp5bjZzfgArPLPQ90KrmtUdHUVMzpMi87aamlhVkNF+mYDgcaYixJajp71mEOS7hQj2VU7J805xAxHJ3JEWctlwUGpC0543KRU76G8gmY9gfyYCSj3pk8B9opE5swywSTjH+KZXS/d67U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pMAEkcLp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pMAEkcLp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8DCFCC113CC; Mon, 15 Apr 2024 06:32:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162728; bh=rHc0ZaD+/SOM6afPT4tmEZZK6h9YUARUauoAo1057pA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pMAEkcLpDdmIK21t7ivGQqwmRWjbWyOqeYfI6hbV8wf/P/mLvsvYtU+yrVeTKlOPQ dbSubInKCZlcS0aex+mNypOmExc3GMtj6AKx8y5eGl/lOd6uhI7JF4YWCYDizem8Y6 4T+oRV/GtQ4oqeeHW87aUoi4bF24/ZbEc3+vWEHCN6uyz2ORJvDCE+/8lMX2/h3XXN rtQqZjJgR2+7hD0v4mceKWPkGZu5RBNRACsXnc3cwIIpEG+FL6mSO7zGwvSZTn/3yl kf8GVzFjCIAQ0qxdfD9cdRrQEoItfI0Y2Dr+RMp3cMmpgvFYRNRi0xJCaPLU7LQa57 IwEBTn5m063zQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 6/9] selftests/bpf: Use connect_to_addr in sk_assign Date: Mon, 15 Apr 2024 14:31:15 +0800 Message-Id: <7a2b32cb2cb222d2e226df91bb0c336c409f5e18.1713161975.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch uses public helper connect_to_addr() exported in network_helpers.h instead of the local defined function connect_to_server() in prog_tests/sk_assign.c. This can avoid duplicate code. The code that sets SO_SNDTIMEO timeout as timeo_sec (3s) can be dropped, since connect_to_addr() sets default timeout as 3s. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_assign.c | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c index 70c1a5893830..b2d2b689b0cb 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c @@ -23,8 +23,6 @@ #define NS_SELF "/proc/self/ns/net" #define SERVER_MAP_PATH "/sys/fs/bpf/tc/globals/server_map" -static const struct timeval timeo_sec = { .tv_sec = 3 }; -static const size_t timeo_optlen = sizeof(timeo_sec); static int stop, duration; static bool @@ -74,28 +72,6 @@ configure_stack(void) return true; } -static int -connect_to_server(const struct sockaddr *addr, socklen_t len, int type) -{ - int fd = -1; - - fd = socket(addr->sa_family, type, 0); - if (CHECK_FAIL(fd == -1)) - goto out; - if (CHECK_FAIL(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo_sec, - timeo_optlen))) - goto close_out; - if (CHECK_FAIL(connect(fd, addr, len))) - goto close_out; - - goto out; -close_out: - close(fd); - fd = -1; -out: - return fd; -} - static in_port_t get_port(int fd) { @@ -138,7 +114,7 @@ run_test(int server_fd, const struct sockaddr *addr, socklen_t len, int type) in_port_t port; int ret = 1; - client = connect_to_server(addr, len, type); + client = connect_to_addr(type, (struct sockaddr_storage *)addr, len); if (client == -1) { perror("Cannot connect to server"); goto out; From patchwork Mon Apr 15 06:31:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629457 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C08F1EB3F; Mon, 15 Apr 2024 06:32:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162735; cv=none; b=Cr+m9r9e44SSZyz88O1PwYg4yK4TvKQSddSnd/h5RpW8XS8/4SiofwoPDG43iJbsQ3jFzeyVuCEhaf+sPB24gpShncerfCM4HcNSZ81XS8JLefVTYVqvV3o8de7ZtfFDdm+CSXD7ajmsrz/0n1xXqFdSjYJH+OLKH7kozlGjxok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162735; c=relaxed/simple; bh=NYUCRwJCj293M6MCbmKpGp72XZsjJqmAQNAxggd5AGU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BBn1D4O+FWrVUFULEUzuDQtz2122wGCTDrX9QGvBs6Sd8issNNzqntE5DNS67Bz7ryP9ht7Arr1i0ctVOnrgRjxdGBWHHIbMPrwCoNgoDTngB2KZfoyzgyD6mDhVx7Q1FJeLEtWKXaiZsHgVa9tZ1IkNwBuU376BpkH85tRhlJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UNYMnLaq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UNYMnLaq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3754C4AF0A; Mon, 15 Apr 2024 06:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162734; bh=NYUCRwJCj293M6MCbmKpGp72XZsjJqmAQNAxggd5AGU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UNYMnLaq1lZR+HYYt2E0L95HmabD8PTX/H/5iGqvjTC0mSo68gzIxmZuolQ5Uhm3g vPFdp9JV+y9nBP8Tjx5DSZgRtvR8TZ1kzKpxabfo6pcK7ENOBMQDjq4xPlG0Mj/enM df6cUof3IovaJivhxv18JSGMMseupzDV3uJmXP+WEi54OXy+xbnguA6sEDH5TFnrRQ 7VXU9uB8bMF/yZFsEd8BBK6j4OtVTSVBjFCu8Eq+CM1FOeg6KX6iArZMstwm6dgDJP 1JeIFv+4mYzNBrWgg2PyCe7JnU7djMkPuvUdBKgrmvyNTGEG/kFhmKqfDqTiR6rSYF 4OMMjs27suYzw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 7/9] selftests/bpf: Use log_err in network_helpers Date: Mon, 15 Apr 2024 14:31:16 +0800 Message-Id: <3063b283a7336931ae56a88fc669b65ccbac1075.1713161975.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang The helpers ASSERT_OK/GE/OK_PTR should avoid using in public functions. This patch uses log_err() to replace them in network_helpers.c, then uses ASSERT_OK_PTR() to check the return values of all open_netns(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 19 ++++++++++++++----- .../selftests/bpf/prog_tests/empty_skb.c | 2 ++ .../bpf/prog_tests/ip_check_defrag.c | 2 ++ .../selftests/bpf/prog_tests/tc_redirect.c | 2 +- .../selftests/bpf/prog_tests/test_tunnel.c | 4 ++++ .../selftests/bpf/prog_tests/xdp_metadata.c | 16 ++++++++++++++++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 92c980b18afd..9404db032a5c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -458,22 +458,30 @@ struct nstoken *open_netns(const char *name) struct nstoken *token; token = calloc(1, sizeof(struct nstoken)); - if (!ASSERT_OK_PTR(token, "malloc token")) + if (!token) { + log_err("Failed to malloc token"); return NULL; + } token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY); - if (!ASSERT_GE(token->orig_netns_fd, 0, "open /proc/self/ns/net")) + if (token->orig_netns_fd <= 0) { + log_err("Failed to open /proc/self/ns/net"); goto fail; + } snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name); nsfd = open(nspath, O_RDONLY | O_CLOEXEC); - if (!ASSERT_GE(nsfd, 0, "open netns fd")) + if (nsfd <= 0) { + log_err("Failed to open netns fd"); goto fail; + } err = setns(nsfd, CLONE_NEWNET); close(nsfd); - if (!ASSERT_OK(err, "setns")) + if (err) { + log_err("Failed to setns"); goto fail; + } return token; fail: @@ -486,7 +494,8 @@ void close_netns(struct nstoken *token) if (!token) return; - ASSERT_OK(setns(token->orig_netns_fd, CLONE_NEWNET), "setns"); + if (setns(token->orig_netns_fd, CLONE_NEWNET)) + log_err("Failed to setns"); close(token->orig_netns_fd); free(token); } diff --git a/tools/testing/selftests/bpf/prog_tests/empty_skb.c b/tools/testing/selftests/bpf/prog_tests/empty_skb.c index 261228eb68e8..438583e1f2d1 100644 --- a/tools/testing/selftests/bpf/prog_tests/empty_skb.c +++ b/tools/testing/selftests/bpf/prog_tests/empty_skb.c @@ -94,6 +94,8 @@ void test_empty_skb(void) SYS(out, "ip netns add empty_skb"); tok = open_netns("empty_skb"); + if (!ASSERT_OK_PTR(tok, "setns")) + goto out; SYS(out, "ip link add veth0 type veth peer veth1"); SYS(out, "ip link set dev veth0 up"); SYS(out, "ip link set dev veth1 up"); diff --git a/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c index 8dd2af9081f4..284764e7179f 100644 --- a/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +++ b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c @@ -88,6 +88,8 @@ static int attach(struct ip_check_defrag *skel, bool ipv6) int err = -1; nstoken = open_netns(NS1); + if (!ASSERT_OK_PTR(nstoken, "setns")) + goto out; skel->links.defrag = bpf_program__attach_netfilter(skel->progs.defrag, &opts); if (!ASSERT_OK_PTR(skel->links.defrag, "program attach")) diff --git a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c b/tools/testing/selftests/bpf/prog_tests/tc_redirect.c index dbe06aeaa2b2..b1073d36d77a 100644 --- a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/tc_redirect.c @@ -530,7 +530,7 @@ static int wait_netstamp_needed_key(void) __u64 tstamp = 0; nstoken = open_netns(NS_DST); - if (!nstoken) + if (!ASSERT_OK_PTR(nstoken, "setns dst")) return -1; srv_fd = start_server(AF_INET6, SOCK_DGRAM, "::1", 0, 0); diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index 5f1fb0a2ea56..cec746e77cd3 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -612,6 +612,8 @@ static void test_ipip_tunnel(enum ipip_encap encap) /* ping from at_ns0 namespace test */ nstoken = open_netns("at_ns0"); + if (!ASSERT_OK_PTR(nstoken, "setns")) + goto done; err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); if (!ASSERT_OK(err, "test_ping")) goto done; @@ -666,6 +668,8 @@ static void test_xfrm_tunnel(void) /* ping from at_ns0 namespace test */ nstoken = open_netns("at_ns0"); + if (!ASSERT_OK_PTR(nstoken, "setns")) + goto done; err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); close_netns(nstoken); if (!ASSERT_OK(err, "test_ping")) diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c b/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c index 05edcf32f528..f76b5d67a3ee 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c @@ -384,6 +384,8 @@ void test_xdp_metadata(void) SYS(out, "ip netns add " RX_NETNS_NAME); tok = open_netns(TX_NETNS_NAME); + if (!ASSERT_OK_PTR(tok, "setns")) + goto out; SYS(out, "ip link add numtxqueues 1 numrxqueues 1 " TX_NAME " type veth peer " RX_NAME " numtxqueues 1 numrxqueues 1"); SYS(out, "ip link set " RX_NAME " netns " RX_NETNS_NAME); @@ -400,6 +402,8 @@ void test_xdp_metadata(void) SYS(out, "ip -4 neigh add " RX_ADDR " lladdr " RX_MAC " dev " TX_NAME_VLAN); switch_ns_to_rx(&tok); + if (!ASSERT_OK_PTR(tok, "setns rx")) + goto out; SYS(out, "ip link set dev " RX_NAME " address " RX_MAC); SYS(out, "ip link set dev " RX_NAME " up"); @@ -449,6 +453,8 @@ void test_xdp_metadata(void) goto out; switch_ns_to_tx(&tok); + if (!ASSERT_OK_PTR(tok, "setns tx")) + goto out; /* Setup separate AF_XDP for TX interface nad send packet to the RX socket. */ tx_ifindex = if_nametoindex(TX_NAME); @@ -461,6 +467,8 @@ void test_xdp_metadata(void) goto out; switch_ns_to_rx(&tok); + if (!ASSERT_OK_PTR(tok, "setns rx")) + goto out; /* Verify packet sent from AF_XDP has proper metadata. */ if (!ASSERT_GE(verify_xsk_metadata(&rx_xsk, true), 0, @@ -468,6 +476,8 @@ void test_xdp_metadata(void) goto out; switch_ns_to_tx(&tok); + if (!ASSERT_OK_PTR(tok, "setns tx")) + goto out; complete_tx(&tx_xsk); /* Now check metadata of packet, generated with network stack */ @@ -475,6 +485,8 @@ void test_xdp_metadata(void) goto out; switch_ns_to_rx(&tok); + if (!ASSERT_OK_PTR(tok, "setns rx")) + goto out; if (!ASSERT_GE(verify_xsk_metadata(&rx_xsk, false), 0, "verify_xsk_metadata")) @@ -498,6 +510,8 @@ void test_xdp_metadata(void) goto out; switch_ns_to_tx(&tok); + if (!ASSERT_OK_PTR(tok, "setns tx")) + goto out; /* Send packet to trigger . */ if (!ASSERT_GE(generate_packet(&tx_xsk, AF_XDP_CONSUMER_PORT), 0, @@ -505,6 +519,8 @@ void test_xdp_metadata(void) goto out; switch_ns_to_rx(&tok); + if (!ASSERT_OK_PTR(tok, "setns rx")) + goto out; while (!retries--) { if (bpf_obj2->bss->called) From patchwork Mon Apr 15 06:31:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629458 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 873ED1CAAF; Mon, 15 Apr 2024 06:32:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162741; cv=none; b=erdpJs/crskTqY1lBWG0v/DJhKOM73jBRMYvUqBto+MS0cvoCBzk+W7/Ozm0jljcfYI2mw5cY5w4dXdhsKZy1aDyVFgxXn8+HJRBA0aT+ufhDJavOm/qBfmjsBllqz16svD1OkpVaXR5IbIWB2rcFfi2721uRqdKNnxRAormzo0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162741; c=relaxed/simple; bh=W3URAAzTaKAuxanl+UQmGAIMDLYs5iSFe2bJxQA00Xo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R7lipW/481ErrdNaooMQj7wcLZT+THN+UyCPQ5dvOAVjgvPAfCqn3g4hf/blbYzmOG7J+W+TpQ9vzU87jM+EJbJaRBtbSBhxKH/g18kc28WqV9yO0RkJvd+bKVJCvtyqyhh5uSFwO1jK1PJuJSWTDES1G3iFhkJq5qP2R/Z5imc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BWjD2I1i; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BWjD2I1i" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5ECCEC113CC; Mon, 15 Apr 2024 06:32:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162741; bh=W3URAAzTaKAuxanl+UQmGAIMDLYs5iSFe2bJxQA00Xo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BWjD2I1i8F3r+h4Wy1sIVd/07PrScqwW/WUM0+sPHrZCMoj0LW+Re+HX9wJYi5+Yl IN/DTwVGUvT1YVT2Ww5VJT6BfGYS4ohQ6CtyMmsJGRNrxdG7jP4VTZDp6X6dJnSeAW 9aKC5AzVZgYmn8LqWi14aQpvGi/rBXPEma6AnKhmEZTxyJ9p8dp70ZAv/LXka/62ZV 7fmvCPyg91Sbobcet7BNoefIHtoSeIFdxXwPMY7qeTgcKSwimOvEBwlsww+RH6wJw8 6mg2P0+itna/+XsRxxvx+qEopC2dfju52fFxpCAhErv4mWZ+sjTMOl1yJI6al+Q8az t7khOlvrHNb1w== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 8/9] selftests/bpf: Use start_server_addr in test_sock_addr Date: Mon, 15 Apr 2024 14:31:17 +0800 Message-Id: <7fe8f7d0716ffaa12531d97a4f1ad1c2b1cd75ad.1713161975.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Include network_helpers.h in test_sock_addr.c, use the newly added public helper start_server_addr() instead of the local defined function start_server(). This can avoid duplicate code. In order to use functions defined in network_helpers.c, Makefile needs to be updated and needs to be included in network_helpers.h to avoid compilation errors. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/network_helpers.h | 1 + tools/testing/selftests/bpf/test_sock_addr.c | 38 ++----------------- 3 files changed, 7 insertions(+), 35 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index b0be07f29dde..53713f4495dd 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -289,11 +289,12 @@ UNPRIV_HELPERS := $(OUTPUT)/unpriv_helpers.o TRACE_HELPERS := $(OUTPUT)/trace_helpers.o JSON_WRITER := $(OUTPUT)/json_writer.o CAP_HELPERS := $(OUTPUT)/cap_helpers.o +NETWORK_HELPERS := $(OUTPUT)/network_helpers.o $(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) -$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) +$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(NETWORK_HELPERS) $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) $(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 9e6fcc89a8d0..5e0806a3d745 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -11,6 +11,7 @@ typedef __u16 __sum16; #include #include #include +#include #include #include #include diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c index 80c42583f597..9ce5dad7468b 100644 --- a/tools/testing/selftests/bpf/test_sock_addr.c +++ b/tools/testing/selftests/bpf/test_sock_addr.c @@ -19,6 +19,7 @@ #include #include "cgroup_helpers.h" +#include "network_helpers.h" #include "testing_helpers.h" #include "bpf_util.h" @@ -939,37 +940,6 @@ static int cmp_peer_addr(int sock1, const struct sockaddr_storage *addr2) return cmp_sock_addr(getpeername, sock1, addr2, /*cmp_port*/ 1); } -static int start_server(int type, const struct sockaddr_storage *addr, - socklen_t addr_len) -{ - int fd; - - fd = socket(addr->ss_family, type, 0); - if (fd == -1) { - log_err("Failed to create server socket"); - goto out; - } - - if (bind(fd, (const struct sockaddr *)addr, addr_len) == -1) { - log_err("Failed to bind server socket"); - goto close_out; - } - - if (type == SOCK_STREAM) { - if (listen(fd, 128) == -1) { - log_err("Failed to listen on server socket"); - goto close_out; - } - } - - goto out; -close_out: - close(fd); - fd = -1; -out: - return fd; -} - static int connect_to_server(int type, const struct sockaddr_storage *addr, socklen_t addr_len) { @@ -1178,7 +1148,7 @@ static int run_bind_test_case(const struct sock_addr_test *test) if (init_addrs(test, &requested_addr, &expected_addr, NULL)) goto err; - servfd = start_server(test->type, &requested_addr, addr_len); + servfd = start_server_addr(test->type, &requested_addr, addr_len); if (servfd == -1) goto err; @@ -1214,7 +1184,7 @@ static int run_connect_test_case(const struct sock_addr_test *test) goto err; /* Prepare server to connect to */ - servfd = start_server(test->type, &expected_addr, addr_len); + servfd = start_server_addr(test->type, &expected_addr, addr_len); if (servfd == -1) goto err; @@ -1271,7 +1241,7 @@ static int run_xmsg_test_case(const struct sock_addr_test *test, int max_cmsg) goto err; /* Prepare server to sendmsg to */ - servfd = start_server(test->type, &server_addr, addr_len); + servfd = start_server_addr(test->type, &server_addr, addr_len); if (servfd == -1) goto err; From patchwork Mon Apr 15 06:31:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13629459 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F35581BF53; Mon, 15 Apr 2024 06:32:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162748; cv=none; b=h83IwBlRNCVQ3MvlxjHtexxXZ/QcYPJJtzV/rxJBbIryGVgV/zExPTyOc2IHcm2t/fn305X+RzcHRXwvNNacKd/WolwG0ZC/pPH/meI4KBAXWdXBau1c5ORdOwsmwKllUYHuuORcDVcTddLN9N+WEYM0d6Wbczlhsf8KH7YCCEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713162748; c=relaxed/simple; bh=eEf0d+YYYSKJr8JQ0MHAhSmpH3ZHVzisAU/wx98NNtE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CR2iBmE8ETbzZ9hG2HWUZs+punEk0Eyvxu8+9mx35ebzC1oGYsELyt4A38ZFrWPyi+rS5QGtnF8V8m04CQQTA5Y9xTwHNxkC4J6G9+Ld0kT07gSRWtBCWR5zrfDIKfgz42MSXh4oI9HY3RbXhEDjEmtjdWfiwJgcPIoGflsPHIM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jdfg+wD4; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jdfg+wD4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3485C3277B; Mon, 15 Apr 2024 06:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713162747; bh=eEf0d+YYYSKJr8JQ0MHAhSmpH3ZHVzisAU/wx98NNtE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jdfg+wD48J7YavJupue+qQOrKAQrYYrHy/70nDD2Zdwf0hY0MQ3BLUgvGAqA+MmGg lLzxA34lBirrQC704IZHHXKj8xls5ZXvBwSEn8UFfQcflRQaHETHAvXhnjSR0sSmKX Jc/W/cyYaGGGXB46nqEZz7TrQ6EF+jQz8YfRR4hXHtflzNa88Fp8btuFB0yjz0vE2U sW9NZXpGrJjiTwQLiOXwGBH0wnoxx0zgg3ctA4WFMP4qyGwyunxdJGxn1SHCviK9Ei jd8lvUiDAQUUMVd/ppa4bnqVEUC9JAydbZpfI7oJU8FcoBAdlqaFHyyhZ/QLHg3NdP uf2m2m0ViEMWA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 9/9] selftests/bpf: Use connect_to_addr in test_sock_addr Date: Mon, 15 Apr 2024 14:31:18 +0800 Message-Id: <80583a00f977ab90b6a0753a4ca8a0eb946babc1.1713161975.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch uses public helper connect_to_addr() exported in network_helpers.h instead of the local defined function connect_to_server() in test_sock_addr.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/test_sock_addr.c | 36 ++------------------ 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c index 9ce5dad7468b..422255c8575e 100644 --- a/tools/testing/selftests/bpf/test_sock_addr.c +++ b/tools/testing/selftests/bpf/test_sock_addr.c @@ -940,38 +940,6 @@ static int cmp_peer_addr(int sock1, const struct sockaddr_storage *addr2) return cmp_sock_addr(getpeername, sock1, addr2, /*cmp_port*/ 1); } -static int connect_to_server(int type, const struct sockaddr_storage *addr, - socklen_t addr_len) -{ - int domain; - int fd = -1; - - domain = addr->ss_family; - - if (domain != AF_INET && domain != AF_INET6) { - log_err("Unsupported address family"); - goto err; - } - - fd = socket(domain, type, 0); - if (fd == -1) { - log_err("Failed to create client socket"); - goto err; - } - - if (connect(fd, (const struct sockaddr *)addr, addr_len) == -1) { - log_err("Fail to connect to server"); - goto err; - } - - goto out; -err: - close(fd); - fd = -1; -out: - return fd; -} - int init_pktinfo(int domain, struct cmsghdr *cmsg) { struct in6_pktinfo *pktinfo6; @@ -1156,7 +1124,7 @@ static int run_bind_test_case(const struct sock_addr_test *test) goto err; /* Try to connect to server just in case */ - clientfd = connect_to_server(test->type, &expected_addr, addr_len); + clientfd = connect_to_addr(test->type, &expected_addr, addr_len); if (clientfd == -1) goto err; @@ -1188,7 +1156,7 @@ static int run_connect_test_case(const struct sock_addr_test *test) if (servfd == -1) goto err; - clientfd = connect_to_server(test->type, &requested_addr, addr_len); + clientfd = connect_to_addr(test->type, &requested_addr, addr_len); if (clientfd == -1) goto err;