From patchwork Tue Apr 16 10:13:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631628 X-Patchwork-Delegate: bpf@iogearbox.net 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 797B386250; Tue, 16 Apr 2024 10:14: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=1713262461; cv=none; b=tBd6LQ9PRy7oeh6wvGh95mkYB00Jw5/2JNIIq6nxmDJosSnBvqeRG1NooOeUsmDl3lCepFJsLyLOY/hrxNhGTlpBTpfmTAwY9JmHI3diX/JMMEE2Rr8ISxoj8yIOYT1pe+J1eBx0YciqyRHDrkejv2l9Kq39fnaZ+AsR+LOJWtU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262461; c=relaxed/simple; bh=rr9iJteBMSLzZtXVxnrC2eTFd0xg6hkIfIm7606fjoU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n8bMV3G+vdDY9roBZe57MdTNWXCzvZSIN5vdyFrit31NoNFVObBaRC6rc3cQYP4lPUMHiUR3TRKCYexMtNMCYA8B8Qxv8f0sBBM9fI7cf7e0AWeFZukrPlcYCjj+mscQ7csob9b6xux1NFpiqFFdB2RVzJDBcdcHaBVdmGlsvxk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GNhIaJ0I; 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="GNhIaJ0I" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07948C2BD10; Tue, 16 Apr 2024 10:14:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262461; bh=rr9iJteBMSLzZtXVxnrC2eTFd0xg6hkIfIm7606fjoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GNhIaJ0IxfBmNOa2Rb4rvN3c4ffY7rX0+wREy7fsqbUPYhQ0MYQv1t4i8955tdKCG DxFCHwxQepQoMS6Eu7oVHeVt2Vi3ToY0JC9hRf3/eHiA2cVu54Yn0AF39BgeHdPTO2 zRpWAY8HGcYKo5adudCuOWbo336NejDbOqwsrhK9F3qPVHLKbQNs7SdH/whagltpWi kalPiZAzmOwEJybngg4C7vzjJSbvVstYVK26nP2O0G8KGKW6+ukrmSLlb8eQ7Mjeri GN/YmXltKmMiotCAUtwSDphlI5WWvIdlEL/eYEoEOaIVr3/eIZYuu2UIJ3eQl3vFQj FvTRyxD1hJ2RQ== 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, Geliang Tang Subject: [PATCH bpf-next v4 01/14] selftests/bpf: Update arguments of connect_to_addr Date: Tue, 16 Apr 2024 18:13:47 +0800 Message-Id: <3507e70e3e23cdccf6ca1905f9e066bcfd41337f.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 dc1fd7af9c7a..563dde8617dd 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 Tue Apr 16 10:13:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631629 X-Patchwork-Delegate: bpf@iogearbox.net 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 6B11686AFC; Tue, 16 Apr 2024 10:14: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=1713262467; cv=none; b=aHFtRuin/Brj8qaMAhEfX+NWxaIF3Cwo+N40iFakI/u/B5tZqX/+ekjhdsfHHKwmC0ahMWJsnHEuBmjin11LGhpisOYFcwDYPKDWTCyPiGOC1IiV1c4+Xt9yc/2agCMopC8JzPq6WLUyx1yxdJzW7BIHtuubDsc64klwkN1/DY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262467; c=relaxed/simple; bh=yMIeF1Ylf2uz1UFvysidSRBp1Extmmcm7xQCC+P842M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vDhD5fVdOM88VkKj7AA9qzUqi4pVZrM/oxNyVTTpcz6yfuWNHYfwYnCPxWGG+wm5i6xEP1+JBAt7ZcMk1bpYrPNz5DCVcwE//p+TNoaoelYvlPyxVAYlP6S3aZmm/v+orlaXOrohpZJQVP3hfb96PGe4Q64pUCdXh9/hyA0BCKo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=X7bu2ClQ; 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="X7bu2ClQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8205C32783; Tue, 16 Apr 2024 10:14:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262467; bh=yMIeF1Ylf2uz1UFvysidSRBp1Extmmcm7xQCC+P842M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X7bu2ClQFU3FCRFIOo/0Y5792/deB6uNS3laDmrabV9ICCfFZf1TGMip50O7eeQOc 0Y6aO1kiS/9nfIoHHspKDGM2TjyopxUpQcNBBbEmhSqh7FT2vKwnbOkFW/yEptx3FF zMWv6mN+27Tjtu2rh3JlA9fJSfrBxwIoAkCABniT0g0wOIBhDi5WAyXNDbahH3ba+y 3zgGh0k9AjK/83aNyMnVcVh7zaKcCg+ZOvSruntVfoopKtWTluKXsrplIiwPiOl2MK xlLYxLX6ayhi7H876BvQvmPitXBT/MvH6Ui9pTtHxy7MD7gcGF4e9GRrVGvwcsahtE 9p2vI14Gjf+wQ== 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, Geliang Tang Subject: [PATCH bpf-next v4 02/14] selftests/bpf: Add start_server_addr* helpers Date: Tue, 16 Apr 2024 18:13:48 +0800 Message-Id: <48186b788bc029cbd3a47007175c83357fa28668.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 563dde8617dd..836436688ca6 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 Tue Apr 16 10:13:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631630 X-Patchwork-Delegate: bpf@iogearbox.net 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 C55BF86AFC; Tue, 16 Apr 2024 10:14:33 +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=1713262473; cv=none; b=HE20I8lScHdRIoxXYULS48wTqEBrlQw9V3nr1X5fkPUFIPZhvjX7nHE0PlmPEkg02kOknDI38V2+iIDPuanKEcBwFQldsT0OgBNsxZiSR1QtPmltFSSDYW2cPHty8me7B5989KNCMXSYCPRx6UcSRX2G8tdT3f0D0zE2uOdABns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262473; c=relaxed/simple; bh=NHEB9TokBCk2Zcvs5aYjfmxZ3CANdUFDFp4WT/vneAU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IeJfZq5dkVfinBHZJU3Ott4uFTd0QM9KWFM2FypFbwGYwDB8/36+c02oYcGrIfUzr104hTo91nuYebQm2hGb5ZJjr7u8kA2nss8XDEIwj7oVU6EfpNwqRw68PvM3d0u3sFV87v0b20keR46YEMW09Yfn9rGVv45QpGgOonKI11k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MtiEvm15; 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="MtiEvm15" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B82EC2BD10; Tue, 16 Apr 2024 10:14:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262473; bh=NHEB9TokBCk2Zcvs5aYjfmxZ3CANdUFDFp4WT/vneAU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MtiEvm15RCq66te4B9nCMeCw9D5SoRnhlPq9irwKd2I1x2sGYhu7pXhd5EDWkn3GZ rwsPsLjNhwV0FXM6ibQ/RrLs79FQP7FR+FjmJGyXCphMUWdbai8sWsD7lUk0rUkGXA KOsUKqkM6o6eGCpMT067BuJoDWpIclZm8G01pUCTc1ohA3LkBwCGQbt2TXqj8fILCm qLteikQUweh7Weq5d960199tZuIUEK9eqNWg5xRfmlBXlG/ldVuzoVwSgSSFNm07Sh 9oUBRFXKKYq6Ah5A/x2VxPXHLTVMpHXao+WP8bpr+O2Wdlt7iAXNbazxRNpCYGT2vj 0XpwtsTBgpZcA== 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, Geliang Tang Subject: [PATCH bpf-next v4 03/14] selftests/bpf: Use start_server_addr in cls_redirect Date: Tue, 16 Apr 2024 18:13:49 +0800 Message-Id: <4358ac25a3924d2f66cd73efd034530443eee254.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 Tue Apr 16 10:13:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631631 X-Patchwork-Delegate: bpf@iogearbox.net 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 B201186250; Tue, 16 Apr 2024 10:14:39 +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=1713262479; cv=none; b=R+xBo1uJO43vttMCOPhw81jcXFSdB6tJd5eyPxuxISmT2k0SUUOkRFlRlF9yDAUStaOrc0XGJgcAcecvqLJizTsNTd8xDJIl2c4NBB/qYzzLLArjU1FaM1DwNdgXhpRmKy1WMmM2ZzV94Q2GeBRoIRaAiyjmrtRQrpXXKm7WiUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262479; c=relaxed/simple; bh=iu9Nt/eRnvrJD5oDNxZeMq9GCmOe6kMmAtqO73M1w5w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vA2Lm7tVyCf/xnsM+g6qUzWm7S8QDYqrKyhCX769X8jzqq2JmaRlddATdlgxgGYJTOW+uBeog7b1torU9e5lv3UCDug09kRgF+WwPSfTMA5nEHpNfjpcybcadBqp+Ke2+uYsydwoYTZvzjV/UEFSaFcka5ujfkBtHAQYdJCDrck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ngomNeS9; 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="ngomNeS9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E38CFC4AF07; Tue, 16 Apr 2024 10:14:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262479; bh=iu9Nt/eRnvrJD5oDNxZeMq9GCmOe6kMmAtqO73M1w5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ngomNeS9sFkSJiFY2SftQLNfyvWnn/yBGFzfn3ZwTfgosCadFL/f3QKx/hEz8BENJ CTHFU4uT3KzPaiNthxndndnpmR7dxxbFVIPKFog1Mi/JSla25d1eMBeFOMzJOL6c3r Ns3m5SAvcY05brDzBgcXO5L3dFH84vZ3MLrChXfgO/T+NLQe5HhJsSGrlmg/hf1/H1 jZiKlKw0bkdcFKlO3VbUzsMOQz+o63RKzSoeqDHeK+Wgx/n8nSK5jtqQxxQWc/1s/A QcnoTCiWMHvQ+0Ew9kTw284rx0wRbyNoicwOKLi9sC3dCByHUgwSCEgXvS/n5BArvR 24xTL4x+x22jw== 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, Geliang Tang Subject: [PATCH bpf-next v4 04/14] selftests/bpf: Use connect_to_addr in cls_redirect Date: Tue, 16 Apr 2024 18:13:50 +0800 Message-Id: <50f65e54ec143b2800fbe308204e10f78be98559.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 Tue Apr 16 10:13:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631632 X-Patchwork-Delegate: bpf@iogearbox.net 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 B7F1B1272BA; Tue, 16 Apr 2024 10:14:45 +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=1713262485; cv=none; b=fBmsNZ9h5e90hMQU2s6H8ZTEK7QdJ0lykZLFlWo5ab7d0Etukp3dUZBxsC1w2k1WnPW1fFPSyD49sFdzHdvE7D/54xobPnb7c1ci8udfmVOT/IfrL218tyB67MifzeYUbtVkE9kZZ7LNikv0CPUjXPU2yUqczdaFGiQ53us1qUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262485; c=relaxed/simple; bh=gPaD6ZJyxD4PgHJQ62WTuuIeGVpHrnbCX8NFKI8kpRY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Narf07AtXTwjfy+gua3FvSsqe1au8bedvvlWS71Kr1FF1qaAB5xwMo26Kr/NT1vS2Ia4AWrthel6vvuE+rFikld3qPeGluqop+8jqf/8cSZAsWvoZIJqi7++9HuVsfMzHIupEvTjfWRuPUDPJpREARS4m3AKTXfGpc744nDpRf0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p+t1n6it; 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="p+t1n6it" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0050C3277B; Tue, 16 Apr 2024 10:14:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262485; bh=gPaD6ZJyxD4PgHJQ62WTuuIeGVpHrnbCX8NFKI8kpRY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p+t1n6it++epyLjfopbsxQhdnhpX19IDElkBJxhK5EJGIUcmKlA2Fo/D6EP4uZyNq 3eovMO1vOkWJVqT6Ph1hytqkAsPxLQd3x9k68B78UwANABhyuEoiCWwQuIieDTqEtT djo2jiw5uXXS5JlyV+4fgkeEPcRJ3n01ZvBozKdUQA/Nko3KJwQC8cf6kb6R7rG2tz wxEjM9i8XNOPWIlNpH6mWgEP/NWWmxPcRyvSQi6MQno32AKYMyV1ZZ0dWwQ//jOoMx dEiBw4Gs8cBKrRpqmvKAhOsh6gGSVTsP/xUgw+lmXYTl91r4XJa/0cGENqcAJu7VkB UPFeFysdUYV8A== 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, Geliang Tang Subject: [PATCH bpf-next v4 05/14] selftests/bpf: Use start_server_addr in sk_assign Date: Tue, 16 Apr 2024 18:13:51 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 | 28 ++----------------- 1 file changed, 3 insertions(+), 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 1374b626a985..fa8f757c0edd 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) { @@ -310,7 +287,8 @@ void test_sk_assign(void) continue; prepare_addr(test->addr, test->family, BIND_PORT, false); addr = (const struct sockaddr *)test->addr; - server = start_server(addr, test->len, test->type); + server = start_server_addr(test->type, + (const struct sockaddr_storage *)addr, test->len); if (server == -1) goto close; From patchwork Tue Apr 16 10:13:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631633 X-Patchwork-Delegate: bpf@iogearbox.net 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 8BA28126F1A; Tue, 16 Apr 2024 10:14:51 +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=1713262491; cv=none; b=fzHuaDRlfY6h0B7U6YcZVqZstJxFs8Ygo1jFEpm/qhFSX2GEGhUtR8an2hguhZMORlSfikLFzf3IsFUnsKvOqo6rxAJKqCJfKGwR0pZzxEUA9ZVtozU6waaIR3SqBy+YVSQxApUaHeLgqtsZiqtSSpRG8g3Rayg63HS0CjhRRSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262491; c=relaxed/simple; bh=+UY5Yx87xkXUquSqExXlO++aZfWesHnQpbB/kBWWX7g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OoGg3eQ7N2oq0C41b22Vw/Z9hQzPtnkDjHFx49h7e2QQ4GR57WFZhl0ID7AnamfjTkMzs3pfFPdhNtmvNEPfAM34SK2XC1hzgVp5svLo475u65fXURTkFs6IavvJskXke+JLOOHbd1WUrHoMypT9ume8wfsXBTDYDxZqCpHPh34= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lDDkWE3+; 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="lDDkWE3+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DCF8EC3277B; Tue, 16 Apr 2024 10:14:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262491; bh=+UY5Yx87xkXUquSqExXlO++aZfWesHnQpbB/kBWWX7g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lDDkWE3+Y+fkkxOME0DTLSPg78HzyqKGnTNtSzRWz5hQgMBJxFxKnRo1/gLcsvmcS w9Ib+fs9WD6+CGlm7TTIy63nyfs2mAMIAN417uKY1NK6zXPc7l/X/NiqcW9zcn5KT0 yvri3fJOoH4/WW4Mg1ePWUqCSWNi1zjFvZfPSG+7dt2JplKy9b5vlHuBAwyaN9oHBb VBiAV6jYl96xHphgMjP95TXEPa19c09Lqz4GPL4wzAqUNzah1PGpETkAxZypHl7M3s z8U1GTCxMgEgQM1qje5Ac89w5O2wjn4dFVC0k08WXFxM6qu6Oy6wh80S9/w45T2JuI Rzkzd15tcKhSg== 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, Geliang Tang Subject: [PATCH bpf-next v4 06/14] selftests/bpf: Use connect_to_addr in sk_assign Date: Tue, 16 Apr 2024 18:13:52 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 fa8f757c0edd..766fc56f5fc7 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 Tue Apr 16 10:13:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631634 X-Patchwork-Delegate: bpf@iogearbox.net 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 6D92E86250; Tue, 16 Apr 2024 10:14:57 +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=1713262497; cv=none; b=F3cHBBGaPncNqgqkUZ9urCNkeZ2/XQSFRuRX2TyOlVUNZnwqQMQssbobbb9w5wu+oWNJ4BqV3c+1lSPnuqQP0z50SBD8c7aHzy7Z6wa2sRvC2ivqaaINFvar1P28E4j2OhgvIZuJpCGPSg9znYYfGUuFCRxJAVXQAFCCWIvLxo0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262497; c=relaxed/simple; bh=VaFHaAeKrb+cFvQ+qjKLi/JjL2jUmftRZ42QIrUtUbg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ao6gSqa01pyPN0FpIhf3G2bq9REfH2zMtCUSKLT3TlQ0EDuojpISKAc/GaEu8TFgfx/xQNmvOLfQ00Qs1u2U5zlargMlHkc+2DZAZG7q6t0zdZlmaewxMqUuEiq/l3/zBEstMPOOrMgbbrDbLOWkCIiu9KnzE4Pn3yD967CED4M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iQ1WKLFe; 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="iQ1WKLFe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9744C113CE; Tue, 16 Apr 2024 10:14:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262497; bh=VaFHaAeKrb+cFvQ+qjKLi/JjL2jUmftRZ42QIrUtUbg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iQ1WKLFeZcyipefdBQp4mAkTP8etW/o7xOST0eTyiQqWk9CV7LZ4JO0dFb18uA8WZ uz3+aON2nTdDSV5rr6GicbWtUa3/wx5QnGWIcK/bkiN/v2iI3KxJqZg7nPGnGB8tkJ LruGDZ5Cek8Z4XTDoiypJCU7ajF1YqxmtJJTvyMb8rfsdUp7pJ+yIgikze6nPEIqPc owQUBn2iU2DaGVQACM9pIOVrFUaabgkU2VSEg9VSTnhLbm9oKNF6+GGWDb/73jBByE m+rgfroKk4yxNRH6/0nCYzQr6hqOveh0xl9Z29s0SqnAaKHCezQay1tI5Rim1DnUGA mc9fjMO1bilXw== 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, Geliang Tang Subject: [PATCH bpf-next v4 07/14] selftests/bpf: Use get_socket_local_port in sk_assign Date: Tue, 16 Apr 2024 18:13:53 +0800 Message-Id: <68cea931420c8c26d34487979c245976531ac191.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper get_socket_local_port() exported in network_helpers.h instead of the local defined function get_port() in prog_tests/sk_assign.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_assign.c | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c index 766fc56f5fc7..c0ef210f7622 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c @@ -72,29 +72,6 @@ configure_stack(void) return true; } -static in_port_t -get_port(int fd) -{ - struct sockaddr_storage ss; - socklen_t slen = sizeof(ss); - in_port_t port = 0; - - if (CHECK_FAIL(getsockname(fd, (struct sockaddr *)&ss, &slen))) - return port; - - switch (ss.ss_family) { - case AF_INET: - port = ((struct sockaddr_in *)&ss)->sin_port; - break; - case AF_INET6: - port = ((struct sockaddr_in6 *)&ss)->sin6_port; - break; - default: - CHECK(1, "Invalid address family", "%d\n", ss.ss_family); - } - return port; -} - static ssize_t rcv_msg(int srv_client, int type) { @@ -138,7 +115,7 @@ run_test(int server_fd, const struct sockaddr *addr, socklen_t len, int type) goto out; } - port = get_port(srv_client); + port = get_socket_local_port(srv_client); if (CHECK_FAIL(!port)) goto out; /* SOCK_STREAM is connected via accept(), so the server's local address From patchwork Tue Apr 16 10:13:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631635 X-Patchwork-Delegate: bpf@iogearbox.net 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 553241272A0; Tue, 16 Apr 2024 10:15:04 +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=1713262504; cv=none; b=VnjwBq+E4h1aBkalDKZ+IS44lqKGKg21zzzWq41pUFzK8H+/2OQZuPNWQ/lo683tvphltJLDaStxB9xxOq344GFp4iI2qmuHR4dolJ4ezXqhp0ismEvkMIvzIAMOJicfgSJzypmc9BEHZnXs62tZ0PrI5enIaMnVIfmncg+qXW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262504; c=relaxed/simple; bh=qiNOeJlMK4Gap3egVspgufbRH3oE7PklngmbBiyzAxg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l0BcfgFzH+uYW7wEiGgeaLeuUXNu46qUmzKbC+jZQb1PColJJDY/Rlv1SDEvbJuYpbN3FFZTaqHik/lxRChpuqDj6l8UgCOYBNt5H/I4xPjqRu5xI9vxYcmCDGi5EkZv2ta5hffliRdXsJBhCHg8xCXpmZDJ/Ei32DAgs0fM9S0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FnTISD7n; 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="FnTISD7n" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93BCDC3277B; Tue, 16 Apr 2024 10:14:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262503; bh=qiNOeJlMK4Gap3egVspgufbRH3oE7PklngmbBiyzAxg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FnTISD7nSh7WsnjQ1YwBr2q68+HuBdn16DOwi9ME4sob4yA70dosIqVspBfXV3mGq hfKHKR1+cK1pH+NzkKGcQqRy6DS+/JdQcrJDnzI2+yqI4wnDXAFHwIpXX+BINMz6uL EUPysY0XoPeh9Lgjo4m55JNA0ocaLx7ve5aXk49yxTSWy141UIbiHy2yqINTnVoNzj 8A+xCIWJPc1qg6BlGmzE9RIyvdOSMTxiC7CLWURrQSUGVFZzRsuX2fp6wfqO3gQheM Z1/crCl4269AL0rcp0H0TVEnQAj561P2+oIY2msrwxu7noEs445CopxQlPGC7RJ/Ir c3N+qIRWTK2uQ== 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, Geliang Tang Subject: [PATCH bpf-next v4 08/14] selftests/bpf: Use make_sockaddr in sk_assign Date: Tue, 16 Apr 2024 18:13:54 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding it prepare_addr() in prog_tests/sk_assign.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/sk_assign.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c index c0ef210f7622..67daa2268cdc 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c @@ -148,24 +148,19 @@ prepare_addr(struct sockaddr *addr, int family, __u16 port, bool rewrite_addr) { struct sockaddr_in *addr4; struct sockaddr_in6 *addr6; + socklen_t len; + + make_sockaddr(family, family == AF_INET ? "127.0.0.1" : "::1", + port, (struct sockaddr_storage *)addr, &len); switch (family) { case AF_INET: addr4 = (struct sockaddr_in *)addr; - memset(addr4, 0, sizeof(*addr4)); - addr4->sin_family = family; - addr4->sin_port = htons(port); if (rewrite_addr) addr4->sin_addr.s_addr = htonl(TEST_DADDR); - else - addr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK); break; case AF_INET6: addr6 = (struct sockaddr_in6 *)addr; - memset(addr6, 0, sizeof(*addr6)); - addr6->sin6_family = family; - addr6->sin6_port = htons(port); - addr6->sin6_addr = in6addr_loopback; if (rewrite_addr) addr6->sin6_addr.s6_addr32[3] = htonl(TEST_DADDR); break; From patchwork Tue Apr 16 10:13:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631636 X-Patchwork-Delegate: bpf@iogearbox.net 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 B290B126F0D; Tue, 16 Apr 2024 10:15:10 +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=1713262510; cv=none; b=ae0Ma3ltV0cyX1h7W67J+g5MgWv0ipqq5CIp5HepV43BsTwfr53sw70B+Jt46VP3jY1RW+0bk+eLvZPvNwXMo3yBhL4tpMlPdL7Tb5tDVFPXYHt2g3QB5+ZdwW0M9WCU8+7sIUBtBw1IpkI6b8HNlE+IO0YeInAcdclb3RiL1qk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262510; c=relaxed/simple; bh=PBvfaey5R0D8GFPq99eSLMPwStpF2hvP7SLA9ylzT80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TrN7XfrxHgr8KnfbfEKImb8/X6fjB9qAoNoBrkJDg28+k8JM7IgFa25YxO3X/o2I7cfXKaWILlqLiT7LOsQcUz7pfCO2pKdZcUWfnghdGHKd9hvtLemVvPuzwhxsQUsymjwNzf0HqlOipYOBOIpW5T6BAuSfSlFHnRfiuznnEpY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oGhSwbiT; 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="oGhSwbiT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93208C3277B; Tue, 16 Apr 2024 10:15:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262510; bh=PBvfaey5R0D8GFPq99eSLMPwStpF2hvP7SLA9ylzT80=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oGhSwbiTW9vAfX/Ier6+5nr2pnjLC7mDpW9Ry47nmIhx4BFw6bKDaT6kwEKprHvdv GA0+/286WXTcMbEdXlYnfEPe0D3a5hn79MXL3ZkMV/ASKT5SaTLCTob0ikSgi16OBi lKEvNXIs0aEnp5VYsiRmpIJD1F4mvjwBL/FIV2v7GotBjsXtYHOKb21e8PpXBljRrk jEI2S/+ebCk5L3yp+QOHGHMIda/bWQmMxa8F5cY68sp+tHnO9qYcA3fD3jc1TBozmN 628ZT1xg0/pbUzkteLWMbttctbQCtrGlXtK2AAQqwGj8nsgrb5V+88NLk4P+rA0Nob xnhyCKgaX31zA== 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, Geliang Tang Subject: [PATCH bpf-next v4 09/14] selftests/bpf: Use log_err in network_helpers Date: Tue, 16 Apr 2024 18:13:55 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 836436688ca6..4fd3ab4fa72c 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 Tue Apr 16 10:13:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631637 X-Patchwork-Delegate: bpf@iogearbox.net 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 988CE86250; Tue, 16 Apr 2024 10:15:17 +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=1713262517; cv=none; b=b5Hxf91mJm7uh1JSqd0x8sktAJw0BLrQuiHI7acugm+N+xTbYX72wx3tKLhtE5GoGDr+Oqa5U+4beYs8NQL/UdbpBQCkFS6btMmZUsv37i68ZpSy/4ZNe5xU5ynA1XopvIVnIpRbzNpHQzXzmnZ/EWzj24obgNiohvGWUV8CXk0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262517; c=relaxed/simple; bh=W3URAAzTaKAuxanl+UQmGAIMDLYs5iSFe2bJxQA00Xo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XaFHpKIiw7gzvw2ONuBxsTIUkCc8JI37AG9PVHafAWiU2cshcY6L5vIzHSfhWdgox124hc7uVwfejrsjxDmq5fMt94QNyjLABtkODC+arU6mtj/1U8CCILq0sDgVTMQ/pW4dOfo3QSBdZG6kX23RXVx1TQHozu8Pp5u+L4ari1o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c1r1p4T1; 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="c1r1p4T1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2DCE6C4AF07; Tue, 16 Apr 2024 10:15:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262517; bh=W3URAAzTaKAuxanl+UQmGAIMDLYs5iSFe2bJxQA00Xo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c1r1p4T1luKbAL6tTNl4z0brf/gF+mQVC2jaCRGxuBXIOfwDbL2vMSX3J0+sIs3wV YNZhbGqgtCtVaXq2tKDrO03HcYVDjR/lfCSLRnrEZGPxcIaLzRvuOF/+5EoUFiU8Az BFchRMTPMywow7UZERQlLKUZmaegN+ehGg0VPSYN8kNjB85XAAgg4QDlFIQatRABdn 8nBM0iAo4zLLIPIEnaarAzKU4aecfG1vTKXOfaU5ZJJpe4PUnGikR31aAeUQrwGJnr fBVK1+aURdq9bxOlPQCeCKvCsFK7IUzlqZF0RkMBG6KYcV289FWQimSICWn3QcUWy8 miEho3V4OHVtg== 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, Geliang Tang Subject: [PATCH bpf-next v4 10/14] selftests/bpf: Use start_server_addr in test_sock_addr Date: Tue, 16 Apr 2024 18:13:56 +0800 Message-Id: <3692ba963ac309a855eeeb05c5b0cfa45dfeb7c8.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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 Tue Apr 16 10:13:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631638 X-Patchwork-Delegate: bpf@iogearbox.net 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 3ACC4126F3A; Tue, 16 Apr 2024 10:15:24 +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=1713262524; cv=none; b=UK+mOw2EHNGN3HCP98ezfWSI3rOlEAu0DehcM6AamqePNH6HR1xU21mOmY8rLfma56z7Av4S+rE6dnx1nROHEPFjo+my8uboApvwfP5R1lcFbRqZb7a3CklTAaU+5Yod/sbzdgHBBdymbnoAUvZpYgjrZcy51U2XSo2BcVhMr6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262524; c=relaxed/simple; bh=eEf0d+YYYSKJr8JQ0MHAhSmpH3ZHVzisAU/wx98NNtE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RxglRPJPl2ftkf6bm3esSE2aqwA0PcdnWvZdgLBmdgmp8v93og7M+YwFaEbix4sipo8NHpLpM/k+X0AkmXHJFktVQm/LHjMh5sFQxSyP+tHOzFvwXh9WTRSZJGOjOoGMZm2yQsAJ/WFE4za6n0yU+2DuqYbKxLjXl02xhcbT1OM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OtxLbKkB; 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="OtxLbKkB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C522BC113CE; Tue, 16 Apr 2024 10:15:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262524; bh=eEf0d+YYYSKJr8JQ0MHAhSmpH3ZHVzisAU/wx98NNtE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OtxLbKkBrV1BaWGGjjDZFYuSex/VF/hroDmChy7RT1vE9fs8l/Jlj34mMYfhweQ+7 +WgCSFcmNbDnmEjcqljfI9owu6hRexEznD2HjzNRU9QykPOVaAKTwYIsQHE9CiSE0+ 215dS07d+MASyf0D06uOBzeq2fnm5w6NPWTC8wY1niBe7B1TL/9kX0otLWNDDq7CBe ywQl5VwLNeVyDgfyfLPNhQaLjjEPUiN6/hUwfIX2LwCl4P9MgnqUmSfE3SgQp6S7RW Q+/fHrYzwGwIzoHBOmQLNnPyOJpH5/LicbNdu1Nl+/7NaFRHInv+lmsU7BZ8uxCPXD F7dTr5QMtdraA== 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, Geliang Tang Subject: [PATCH bpf-next v4 11/14] selftests/bpf: Use connect_to_addr in test_sock_addr Date: Tue, 16 Apr 2024 18:13:57 +0800 Message-Id: <3a28a36638e0095619921b7bb2c468f61647883f.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net 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; From patchwork Tue Apr 16 10:13:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631639 X-Patchwork-Delegate: bpf@iogearbox.net 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 18534126F37; Tue, 16 Apr 2024 10:15:30 +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=1713262531; cv=none; b=oO92DWlGNWbSkHVwwrZXhbQh7PuqyUB2wLEr0l3ra2MFfCLNKv4cfHzrq97m2SnITOPiIfC7ZP0JT1vmv4s0Jg0NyLgMoh9J4W26371HDKMETg1kjVKhhH7WM4IJEdmnXA5DvlBM4DRe+OVMpsupE0zQeWmsX7vYKPtR8rXhIsg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262531; c=relaxed/simple; bh=0SGNaJX4OtE+aw/WTKqwFvDrAJbNUVJ5YpY/yRg2hhw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l+gdSKJVR2cUjsYbIq/+TgudO0GVd2vWnkVUkj1s0CTn8OYSTqW/XfioCOvaEaufC7wlxnMlsW0MmvrHLepYk1iVPEXLE0U/MYoBLDZpQto0Htv01JDCDR27957aRJ794aZnzsiaDUQQ0sZApfr5A5akLUq/B8xjP8WwTUYtazE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MRI3vfEj; 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="MRI3vfEj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF89FC3277B; Tue, 16 Apr 2024 10:15:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262530; bh=0SGNaJX4OtE+aw/WTKqwFvDrAJbNUVJ5YpY/yRg2hhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MRI3vfEjxXyZY/UDF1HE39xYG+5JGcVQXz4sf+ndKy3oQNY2EK4BNQkAJmTY2c+qh geYRnOAn2pYlIwpbNSzNgFLwvuO2NM5hWzT0PUfe9r+uSnyVvw21Dd+cjhbTSa6xBv cgJmiVeHtezwOZlAv05JTbtQoOpFqFsVzct6TZAiyF4JNoXV16mN6rbi5TQC95hF47 tT0yKOQHtZthRbS/eYyFbeWn8eU/vadRvZyRhmFLZg0y6EKJNPXdoe13D4oA/Gc7WE vqqeAPZvIiqdlBGLseyfiOjmXlHYOHxVbDW/BLQW0df0gbO2vmI19dqKNjERGEtjHU j0xGgltnobwxw== 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, Geliang Tang Subject: [PATCH bpf-next v4 12/14] selftests/bpf: Use make_sockaddr in test_sock_addr Date: Tue, 16 Apr 2024 18:13:58 +0800 Message-Id: <3f19a4fdec7ba60ff306bbd88cc105db1079b2a5.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of the local defined function mk_sockaddr() in test_sock_addr.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/test_sock_addr.c | 62 ++++---------------- 1 file changed, 12 insertions(+), 50 deletions(-) diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c index 422255c8575e..09503a9b4bec 100644 --- a/tools/testing/selftests/bpf/test_sock_addr.c +++ b/tools/testing/selftests/bpf/test_sock_addr.c @@ -605,44 +605,6 @@ static struct sock_addr_test tests[] = { }, }; -static int mk_sockaddr(int domain, const char *ip, unsigned short port, - struct sockaddr *addr, socklen_t addr_len) -{ - struct sockaddr_in6 *addr6; - struct sockaddr_in *addr4; - - if (domain != AF_INET && domain != AF_INET6) { - log_err("Unsupported address family"); - return -1; - } - - memset(addr, 0, addr_len); - - if (domain == AF_INET) { - if (addr_len < sizeof(struct sockaddr_in)) - return -1; - addr4 = (struct sockaddr_in *)addr; - addr4->sin_family = domain; - addr4->sin_port = htons(port); - if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1) { - log_err("Invalid IPv4: %s", ip); - return -1; - } - } else if (domain == AF_INET6) { - if (addr_len < sizeof(struct sockaddr_in6)) - return -1; - addr6 = (struct sockaddr_in6 *)addr; - addr6->sin6_family = domain; - addr6->sin6_port = htons(port); - if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1) { - log_err("Invalid IPv6: %s", ip); - return -1; - } - } - - return 0; -} - static int load_insns(const struct sock_addr_test *test, const struct bpf_insn *insns, size_t insns_cnt) { @@ -749,6 +711,7 @@ static int recvmsg_deny_prog_load(const struct sock_addr_test *test) static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test) { + socklen_t addr_len = sizeof(struct sockaddr_storage); struct sockaddr_in dst4_rw_addr; struct in_addr src4_rw_ip; @@ -757,9 +720,8 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test) return -1; } - if (mk_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT, - (struct sockaddr *)&dst4_rw_addr, - sizeof(dst4_rw_addr)) == -1) + if (make_sockaddr(AF_INET, SERV4_REWRITE_IP, SERV4_REWRITE_PORT, + (struct sockaddr_storage *)&dst4_rw_addr, &addr_len) == -1) return -1; struct bpf_insn insns[] = { @@ -812,6 +774,7 @@ static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test) static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test, const char *rw_dst_ip) { + socklen_t addr_len = sizeof(struct sockaddr_storage); struct sockaddr_in6 dst6_rw_addr; struct in6_addr src6_rw_ip; @@ -820,9 +783,8 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test, return -1; } - if (mk_sockaddr(AF_INET6, rw_dst_ip, SERV6_REWRITE_PORT, - (struct sockaddr *)&dst6_rw_addr, - sizeof(dst6_rw_addr)) == -1) + if (make_sockaddr(AF_INET6, rw_dst_ip, SERV6_REWRITE_PORT, + (struct sockaddr_storage *)&dst6_rw_addr, &addr_len) == -1) return -1; struct bpf_insn insns[] = { @@ -1086,17 +1048,17 @@ static int init_addrs(const struct sock_addr_test *test, { socklen_t addr_len = sizeof(struct sockaddr_storage); - if (mk_sockaddr(test->domain, test->expected_ip, test->expected_port, - (struct sockaddr *)expected_addr, addr_len) == -1) + if (make_sockaddr(test->domain, test->expected_ip, test->expected_port, + expected_addr, &addr_len) == -1) goto err; - if (mk_sockaddr(test->domain, test->requested_ip, test->requested_port, - (struct sockaddr *)requested_addr, addr_len) == -1) + if (make_sockaddr(test->domain, test->requested_ip, test->requested_port, + requested_addr, &addr_len) == -1) goto err; if (test->expected_src_ip && - mk_sockaddr(test->domain, test->expected_src_ip, 0, - (struct sockaddr *)expected_src_addr, addr_len) == -1) + make_sockaddr(test->domain, test->expected_src_ip, 0, + expected_src_addr, &addr_len) == -1) goto err; return 0; From patchwork Tue Apr 16 10:13:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631640 X-Patchwork-Delegate: bpf@iogearbox.net 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 8949286244; Tue, 16 Apr 2024 10:15:37 +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=1713262537; cv=none; b=gtGwAyOt77o4GWWo0XbVE3u2rCUnnQJ7dX3Sc2u1SX/RFW95M1Pl85+Z+IAwv77dlPuVulhb5s2rGmxXZV0r0rzJcpwkK+FmZGdUOnlFE7Fdf4z9GckKhSZS/FrVG6v3T5Zzz8I2RJsyPrXP8dPlMEGHs97QsDesadz8LqtkKXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262537; c=relaxed/simple; bh=tyobW2Do6ObYQyVRaIWjj4xb8VK248vbw4vE6pBEZsY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l0NlH799WjkPswAR1kt3T/A6NKwM0lmF+8ZwVdTjkaaiYG4AD9s63o/Jmp1y3kX2gByvvva0s4QBI3usjPIMvlbrUiv++YEKGJWs8T5ihloglb3bMBxoGfknBGsIOsYwmPCRfyv2kUSagyEBayBK0TJpbUEZmCNCdkHiV3ax82Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BveOyk50; 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="BveOyk50" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50005C3277B; Tue, 16 Apr 2024 10:15:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262537; bh=tyobW2Do6ObYQyVRaIWjj4xb8VK248vbw4vE6pBEZsY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BveOyk509SYoVOi7MA1AV1Sv/Uurf+Me+EcRz3Mt23A7pYQGsMUAjyNReq1oSOM1b fC57XTGRwaQp1LsVxG0teq3AJlCGJxFyTz4P/TsRozwfuqf2fdF0mF+Ps8oQ9z5Yfx Er6I7XqVtwww7jnJEd5MGxoO3YS4lbt89q9jL1fQTdXFQCb4fuOPwnQeBpSQRs2mwt qcqlaXDzHtDuFMffCDpOAgV2aeBK5nagW7FITOmuc1gMndtqoyD70zEpRCpdjNqJKw 2KbyP5QadSDEpV9vQBfYwL8ngBkLb2dE4V7w495SkwiMpspUS9U7c12Da19zEKAyNQ RDOJzrum4QJeQ== 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, Geliang Tang Subject: [PATCH bpf-next v4 13/14] selftests/bpf: Use make_sockaddr in test_sock Date: Tue, 16 Apr 2024 18:13:59 +0800 Message-Id: <5fd898f4d55d407c2e0be2b7a3e60c93778135d2.1713262052.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in bind_sock() in test_sock.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/Makefile | 1 + tools/testing/selftests/bpf/test_sock.c | 28 ++++--------------------- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 53713f4495dd..017fe30f49a8 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -308,6 +308,7 @@ $(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS) $(OUTPUT)/test_maps: $(TESTING_HELPERS) $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS) $(OUTPUT)/xsk.o: $(BPFOBJ) +$(OUTPUT)/test_sock: $(NETWORK_HELPERS) BPFTOOL ?= $(DEFAULT_BPFTOOL) $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ diff --git a/tools/testing/selftests/bpf/test_sock.c b/tools/testing/selftests/bpf/test_sock.c index 810c3740b2cc..ac00f598ff8e 100644 --- a/tools/testing/selftests/bpf/test_sock.c +++ b/tools/testing/selftests/bpf/test_sock.c @@ -13,6 +13,7 @@ #include #include "cgroup_helpers.h" +#include "network_helpers.h" #include #include "bpf_util.h" @@ -414,8 +415,6 @@ static int bind_sock(int domain, int type, const char *ip, unsigned short port, unsigned short port_retry) { struct sockaddr_storage addr; - struct sockaddr_in6 *addr6; - struct sockaddr_in *addr4; int sockfd = -1; socklen_t len; int res = SUCCESS; @@ -424,25 +423,8 @@ static int bind_sock(int domain, int type, const char *ip, if (sockfd < 0) goto err; - memset(&addr, 0, sizeof(addr)); - - if (domain == AF_INET) { - len = sizeof(struct sockaddr_in); - addr4 = (struct sockaddr_in *)&addr; - addr4->sin_family = domain; - addr4->sin_port = htons(port); - if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1) - goto err; - } else if (domain == AF_INET6) { - len = sizeof(struct sockaddr_in6); - addr6 = (struct sockaddr_in6 *)&addr; - addr6->sin6_family = domain; - addr6->sin6_port = htons(port); - if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1) - goto err; - } else { + if (make_sockaddr(domain, ip, port, &addr, &len)) goto err; - } if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1) { /* sys_bind() may fail for different reasons, errno has to be @@ -458,10 +440,8 @@ static int bind_sock(int domain, int type, const char *ip, goto out; retry: - if (domain == AF_INET) - addr4->sin_port = htons(port_retry); - else - addr6->sin6_port = htons(port_retry); + if (make_sockaddr(domain, ip, port_retry, &addr, &len)) + goto err; if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1) { if (errno != EPERM) goto err; From patchwork Tue Apr 16 10:14:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13631641 X-Patchwork-Delegate: bpf@iogearbox.net 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 4C7EE1272AA; Tue, 16 Apr 2024 10:15:44 +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=1713262544; cv=none; b=ivfttcoV6D0VAOx4SPE+vHoV5yCMbnmphmZFOlDZtq16LRx9TZQvTb37nuuQNNUw6UOSJI40xzq2OcRsgpkCXyUpGwCPFs4JTW62eNwMuXoYWbiGRW4U+lfy8xibuXV9N4qp84rFIIEc9eulAyWRiIGNm0EBKPxUgqA9Xzdkq9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713262544; c=relaxed/simple; bh=aiwiWRr2Bq6SzRrCLCyHaPhZrNsvh6sFypke2ryaQjA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hv1Rs74V4td/s6zhphEdlFOvdmZtfWZjxwPhNVW6BAdWVISqw+uiOCRS6atkLtuk+mK6FWZBEH5LbKK4FgyMesuAM/WwyOcMU4NOOApEgbcSY6ybok9Cy5Y3uVAqiD9mG8mMw55aP/IBwU5l3kfhFDoowUc6Hh0aFwMYAQYSAJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RoPrHozl; 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="RoPrHozl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1457DC2BD10; Tue, 16 Apr 2024 10:15:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713262544; bh=aiwiWRr2Bq6SzRrCLCyHaPhZrNsvh6sFypke2ryaQjA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RoPrHozl0NRPz87IB1jlqgVCqEgQXomTZEUq5XGRSk7SBHLgZpTYmI01qej+lWIDN frVmUeOi57JT6jvdSEFotcBOLndBPqKFtxP+wQKCwl52EVNFvMsd6+Ncj8LOV+tlmC sTdQDaf4hYSFKIyz5/S6KiY4mW3lkBDXvYBgLYiU2987E6/WJoDk6jUWtTR2pg/UF5 2oCb+MJmszYnywdhpj5MbdPJ9lMwjM2imR3nm/WUvMkki9hKN5XIFfLw11lt7/Z60W bM+UfQMOplQW7ZHunk8BEhq4Ci1Z+FBTxwIQP9L5YQEkbG5wGHO/zqG5EvScZc+Qgs YpLoMcaalvrNg== 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, Geliang Tang Subject: [PATCH bpf-next v4 14/14] selftests/bpf: Use make_sockaddr in ip_check_defrag Date: Tue, 16 Apr 2024 18:14:00 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in test_bpf_ip_check_defrag_ok() in prog_tests/ip_check_defrag.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/ip_check_defrag.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) 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 284764e7179f..aafadb0ca666 100644 --- a/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +++ b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c @@ -220,21 +220,9 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken = open_netns(NS0); if (!ASSERT_OK_PTR(nstoken, "setns ns0")) goto out; - if (ipv6) { - struct sockaddr_in6 *c = (struct sockaddr_in6 *)&caddr; - - c->sin6_family = AF_INET6; - inet_pton(AF_INET6, VETH0_ADDR6, &c->sin6_addr); - c->sin6_port = htons(CLIENT_PORT); - err = bind(client_rx_fd, (struct sockaddr *)c, sizeof(*c)); - } else { - struct sockaddr_in *c = (struct sockaddr_in *)&caddr; - - c->sin_family = AF_INET; - inet_pton(AF_INET, VETH0_ADDR, &c->sin_addr); - c->sin_port = htons(CLIENT_PORT); - err = bind(client_rx_fd, (struct sockaddr *)c, sizeof(*c)); - } + make_sockaddr(ipv6 ? AF_INET6 : AF_INET, ipv6 ? VETH0_ADDR6 : VETH0_ADDR, + CLIENT_PORT, &caddr, &caddr_len); + err = bind(client_rx_fd, (struct sockaddr *)&caddr, sizeof(caddr)); close_netns(nstoken); if (!ASSERT_OK(err, "bind")) goto out;