From patchwork Wed Apr 10 07:29:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623688 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 C78B126AC7; Wed, 10 Apr 2024 07:30:05 +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=1712734205; cv=none; b=gV3NuRWB+eZjBl6vuSugbWM5Cr5GpR+8XKam/nstBR4QGN5unf1L0BTBxpzm47WAelVbYgEMi4P5gj4QUAlPCc8IraNI+TSFSo1I+bX9iYWzQxNYmpAUtL4fSRKnfRkyJOTkqkfCJIlIxI87cw2cQoVGw+umwd5i5na/duOxuzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734205; c=relaxed/simple; bh=XKWe2SVlzlSokHo1aMPqJ8xIFb2FMSLePvMYzMc62os=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jtlGKtPi5HkG+yWg2+SZ91Of6Xn6bTVVVFEiQIkKHl0MV9OsejpYFiyNS3bi8YJjFa7Ya3pB9RGdB3PXckz3B/nyw3uzqFOHCcqrNsu0Oj5NCpI8sfWEcdtRZq0zCEdfhuq8gS1XBmwArp+729IAuu+ergn/PhWTdd6N+730DHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XMcMOyrs; 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="XMcMOyrs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61DD0C433F1; Wed, 10 Apr 2024 07:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734205; bh=XKWe2SVlzlSokHo1aMPqJ8xIFb2FMSLePvMYzMc62os=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XMcMOyrsPGfx5p4x8ns4s8lwwYxOnkRDVlR++0u9qjhGxsNtdRwHZpZqLlltIybo1 vfWtw+OtRFXBvfLizdnTUQnnIquNiUrYE6ICokv5Y0UY5/8jYIe2FNfdCGm3pSDISi ctwsSm6cvNMXNu3o4gZfk4FtM+O975jiMFZfvMrvf+/2oJzxs9zuO3CGH4E5gtyLgn xfIyJmyv443sQ4PlTVPU4r/9z534BOv1xhuQsiG73y1Og92KCOpeig6ERL9qH5dhdP Ss5d5j51As8XwNJK+NOSy5DFaiNaHcWGE3ZN1hky4RU37iTWIpK4cYcI7Js+dfFVRX 7JE2eti4MgRoA== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 01/14] selftests/bpf: Add start_server_addr helper Date: Wed, 10 Apr 2024 15:29:34 +0800 Message-Id: <6f45dd890878de4eeb4d6b3a657f51e4eb60aff2.1712733999.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 In order to pair up with connect_to addr(), this patch adds a new helper start_server_addr(), which is a wrapper of __start_server(), and accepts an argument 'addr' of 'struct sockaddr' type instead of a string type argument like start_server(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 5 +++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 6 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index ca16ef2b648e..7ddeb6698ec7 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -185,6 +185,11 @@ int *start_reuseport_server(int family, int type, const char *addr_str, return NULL; } +int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type) +{ + return __start_server(type, 0, addr, addrlen, 0, 0); +} + 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 70f4e4c92733..89f59b65ce76 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -53,6 +53,7 @@ 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(const struct sockaddr *addr, socklen_t addrlen, int type); 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_fd(int server_fd, int timeout_ms); From patchwork Wed Apr 10 07:29:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623689 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 027AB26AC7; Wed, 10 Apr 2024 07:30: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=1712734211; cv=none; b=Q8GqwzAcommxgmmvJ6QZB7EiUtAEXCLUq2WUPcVcPF/T6z+bvyCHH4Yv13m8uit6riJrhjgT5qptCRRvPSeB5+MxJmsGnJ9rFUrUenFBp96T9oIgezQub5PbcPqkzkYCmzExU8UDHGEpe1cyaPH/fReQkWE5ok21mJvjDhHoNVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734211; c=relaxed/simple; bh=XWeh00IOXYJlftJnTBXDAKK+X/bhZEtWsLxktauMllw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ljg+qF2/lM/36tHF5702vooSuoIuV4PAXfthINykpzhxPEr5+lG9AQdmUMPV+jOjSTBuBMm1d2pVagnvyVl3Zi/HKkoInrg4OiqNvMdrToMASkDfuSCw412u/c/LxBGs0gf+e1ZakzS5CHpDmzUwathRd37LjdZlsThZQW36Yc4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EgUYQH0S; 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="EgUYQH0S" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDBF1C43394; Wed, 10 Apr 2024 07:30:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734210; bh=XWeh00IOXYJlftJnTBXDAKK+X/bhZEtWsLxktauMllw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EgUYQH0SROeLQFgZo1MdFpPvG9ZBWGj/UvDXDMl5hUvuVVl5NRPUAPmjDhj4ACQyc BeZhhZdCqNMhgnZYKKAltYaHIGXaOsc1grE5wPzrAy6/Dvt2UutcJ8BPT8khMhMDoe 20gaOVU8Ji2OS3D+UJpHPigr4ipgJldO6RR70cf7ZSTntUeiszxMkDK7cxlNdVN0bD /ANLUKd/MGfW9lnRZAeAI2fwL/ZqT2jiPGooNzGdvnoJ8/wJStJURglBXOf2myxlJH bHgW9wlUGJ6xSypYiYDC+2H7b/kC1rUaCGRBwaz9Ksxhp3BCKCVh/AZNQlW05d11bk 4xEJcKCEXtSYA== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 02/14] selftests/bpf: Use start_server_addr in cls_redirect Date: Wed, 10 Apr 2024 15:29:35 +0800 Message-Id: <741f199a0bd26b9caed6a8b13c48ab98b3c28b42.1712733999.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..013051555ce6 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(addr, len, type); if (*server < 0) return false; From patchwork Wed Apr 10 07:29:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623690 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 D4B1213C9D7; Wed, 10 Apr 2024 07:30: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=1712734217; cv=none; b=rCbREDOHhGweoGM0eJwWCrItydIvCrlcEnBW4RoQ+RIn6S58YaY6Dr0TmzN2n1uqToY+cwJgj3g0oQ0zbjZbkBVIhJkz4S6f3GjCwi0IZpXCuFgTqjCK1dAI9W96OmXdBbhB04AztnE1xHyBJrRTHCsmlh04NZeom1AcHIfk3i4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734217; c=relaxed/simple; bh=UoddJk6fHo9GNGgLLytEAkLLAyBEiqpzG4m+11ozUEI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V7hwpPLBq9v/Rw4bNx9W5ejZR6IpUH4hgpmb+wmUB7KfNZAhiJQi5X5wbCvkTc3zxbI+jhlBSnr9wUtooy9F1emUfPDk6xjhih254G0vS9nl8Zdls8LR8gUuDWuhYHd5dppDIHWqDM/eAldhis1qa2Qz8L7v41xcOs/L4m4Z5d4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oacjKIGo; 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="oacjKIGo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77351C43142; Wed, 10 Apr 2024 07:30:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734217; bh=UoddJk6fHo9GNGgLLytEAkLLAyBEiqpzG4m+11ozUEI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oacjKIGo50myhLg/QpZQg5KR/GCgSte4+gWBSswH/sFzYS5n7OCYZdvQh/lphZXE3 KYtQCzZfUM5ydf15OIlONASEJ7PULWcW/oXv4lKeFrg9u2KzRGJN361Ex077XYclC4 Y0OarUMJyY72JzSMDkvbtVFQVKvpt74uTkPqXiWhxySHa3wH/UP92pwutzub0CYctt vzjDLzwXKGGFX6aI0JBO5ZHa/VOnqCHjt7VdpbZ88dzdtZXPOA07VjLutt7KWvt6ab Rf72BePk5aD3ehqVzPQ53jlzmKBV5e9F5KG5fdno+FCOFimF5pfZ7K2l4bnon8M5PE pNsLIxsiqIOzQ== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 03/14] selftests/bpf: Use connect_to_addr in cls_redirect Date: Wed, 10 Apr 2024 15:29:36 +0800 Message-Id: <0f97d46b5495544b0f9f9059378fddd42ad827f6.1712733999.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 013051555ce6..4b185b608fae 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((struct sockaddr_storage *)sa, slen, type); if (*conn < 0) goto close_server; From patchwork Wed Apr 10 07:29:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623691 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 661B013D27C; Wed, 10 Apr 2024 07:30: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=1712734224; cv=none; b=ETgfNi12QFp5ZMVvgL6Ih3QrAVefgSibWt8l5SEMhRa0Dcj32qpRHTA++ZcyQmgwFIurAn7d8DDvjDhUY4h2M674mOATrCVOIa887flx25U/QKgYkM98UyHtqNjP9tGqpLcFBWRnp/Cm85NTOu/igfAH35MDhGk99nd4Re5fHOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734224; c=relaxed/simple; bh=vLrjMmKetyO8Sy9BYX3xzhrWl5qjTGc6cDgENe6iOaY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IPQYJTGpK9h5Mmkgghq5IZnNhnqIiX8BjpxC/jL0X8i7jmU8FB1r8fJ9sVamSs/XDIaAaSqGThsPk48rw4ltF9T34AgQRzDnZ32Vdaw0GqUEuezIv5Ei8IZ5sfgxL3i4f3494Q4Y1svNsHztLj8Feo7u4nbgJirG+4onoeUBgsU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=g7D2vscr; 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="g7D2vscr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E637FC433F1; Wed, 10 Apr 2024 07:30:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734224; bh=vLrjMmKetyO8Sy9BYX3xzhrWl5qjTGc6cDgENe6iOaY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g7D2vscr04nAqGNFsCz5OSHMjlH148YNIzPrwBXQB3zsGbPy8ayCzm+ZGXaP5QK9P O+pYCxXnqTFipNUEPq7JxBeR3M0hXRajsMtk79R6RN42FKvhy3bxAzaDHxtvdw0Iah 9JMQWLmJZfPMX0W9vbe1df/9+Z9n6Hr3mJKO2MqfEBX2zZ/MGlKuVoAyWqVkZbL+SU q0oQutgs01gZPiVh8jC9ubn0aDlfBow92rdl5fnpa1K+3yxSNcS0ctd2btyANnbXuU XIzNXaCnIt4vFBmAMItmFou5sgqbeXNanjp9T9H63ntjqXCsdtj5RxVQQOral7bFNK RQGDtawXLadLw== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 04/14] selftests/bpf: Use start_server_addr in sk_assign Date: Wed, 10 Apr 2024 15:29:37 +0800 Message-Id: <4a7eeff9a79df18d4723ec4477f7ee5fc2409e58.1712733999.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/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 set 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 | 27 ++----------------- 1 file changed, 2 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..130aafe8cff6 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,7 @@ 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(addr, test->len, test->type); if (server == -1) goto close; From patchwork Wed Apr 10 07:29:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623692 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 B6FA113C9D7; Wed, 10 Apr 2024 07:30: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=1712734230; cv=none; b=X052qkvaFqkDwNRxMuoBt7+ooC0FbxDh5NunblNV29llFVvgvX2aYx9h6MLvGyp/5drfyYEI6eSg/2JQ+/pQxaVI+X87xTqnpAtIedxaKzfdfaTUI0QQWEet0zgik15933zKWMUs/JnDBaLjAr+kwZnsxOScHQs67MbH+wLEOkI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734230; c=relaxed/simple; bh=U2m3gTB3QVoYDws8kPOeNlwkY75f28fDIoqvSNHpaKU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kx4mEOgynvFIBGn9IL94ByXhQh3o0tWTJgL8isrebbFbJuv03ASjQO9tlQq+hXa5Q9gz2Oq7BdbkTAJCt74RdyTD/xrqQ2ov1qCexNYXMmpNgaXVGMDZKUWwXstJsij74CSXPRmQudUFKNQxNDRZFamzhhrc5ksHyaCz0egQp5c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hduXVhUK; 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="hduXVhUK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4374C433C7; Wed, 10 Apr 2024 07:30:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734230; bh=U2m3gTB3QVoYDws8kPOeNlwkY75f28fDIoqvSNHpaKU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hduXVhUKjiMxbiCQpUBluQ1wlXhLKyLa+ltRcjH+mSZ7dkZWcqXoTLsJtq1L6G6ah WCHrCs5UTMFvuMaAZScYFawXTYO/iSiD2LetjiGbVrWc37V8M7Bt2UB7SU5aaDIuu+ WImykDglxLEoV1eG+WQgUQDHGzzrkZ09uQOnaPm7+sXdBmgYFkA5ScAmWAeWipxwB1 phL0XfeDpJRDAEUOT8YU9yD6w34EjB6qpee78J6s0lU9HeLBNTFqwA/Bf4sfmde18J zz8c/rTBrRC6ZyeVE9gBco0mxdPF2Z16Utbc5ST58Jbcx8O/zFIBkkRii6zdyz7qVe 4yobAF/eIl+0Q== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 05/14] selftests/bpf: Use connect_to_addr in sk_assign Date: Wed, 10 Apr 2024 15:29:38 +0800 Message-Id: <42e78119c9ee2d487170d5a2b7915a5c8837ae82.1712733999.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 set 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 130aafe8cff6..b574e162ce6e 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((struct sockaddr_storage *)addr, len, type); if (client == -1) { perror("Cannot connect to server"); goto out; From patchwork Wed Apr 10 07:29:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623693 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 F1AB313D250; Wed, 10 Apr 2024 07:30:36 +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=1712734237; cv=none; b=B9ngtIkMaXgdNPOFzboojNw1SJiaNqJKM0ouQrUkxMNDOW40Jts8WWfl9t24ruke+jZD1QclUQ7+sCy8SioM/hykzx8dg5eGfuEM2XhEdMWiUYGHQ8GaVwlsE/IZWtG0344G0VYtItPbBTCEolRePpNLnCpOEDtp1eupuBJUHDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734237; c=relaxed/simple; bh=YgGRSRvqNQn9bdPBBGWomNv06kRDRT22onFlm2KCtuQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AVLXgq06YAy7G+uIUnbR6TkNZX0LwOigvoyN8ERSDYDZ4amgt+E2gC3fNLXcwmdM83Z1fp0F1xpdpQsUpCCZWiAO4jz7pWe5lS5rsNgOuVhy0vMtMWiMlQcCvV7Fl2mpy6dsUkwFKrUn8WpG2Svi30JyWsDv440x2Q4L69zlMY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U2Q5WbWZ; 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="U2Q5WbWZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E690BC43390; Wed, 10 Apr 2024 07:30:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734236; bh=YgGRSRvqNQn9bdPBBGWomNv06kRDRT22onFlm2KCtuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U2Q5WbWZ/0q2Y0ZwpuTY/u+GQOYywkRx3SZJsJSff8TfayM4TqHXHLZ1dklXQVUVd ke0C6IRIrgffUmbTk+vh+XfPxIMSa/q+VvJZddRL8Mmqh+CFDiY1dk7G0DPSIONw8J 2FLnoyiww/vRVUANUIgsD/+aUsaLAwUyfLly7Uj+jpiQWFimUYXaADb7qH63xUYFiT M8SMhQ5IiRRLX0ia858Zt4IGEC4nlxHvXuS9erQ1qX664kkPuv8lCaZoZ2U9FuZNRh e2Y9RsQUUxOUGA/STdo23MgzRm4Pm+Pt20IGVtDW6TAhteEQnAj4KiW5kqFMpApg6Z YaY/BUPAzaGBA== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 06/14] selftests/bpf: Use log_err in network_helpers Date: Wed, 10 Apr 2024 15:29:39 +0800 Message-Id: <77e0288d4757b2fcc2c491a2ed535ef223268071.1712733999.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. And drop '#include "test_progs.h"' in it, include and instead. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 7ddeb6698ec7..9fd271d5d571 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -7,11 +7,13 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -20,7 +22,6 @@ #include "bpf_util.h" #include "network_helpers.h" -#include "test_progs.h" #ifndef IPPROTO_MPTCP #define IPPROTO_MPTCP 262 @@ -447,22 +448,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("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("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("open netns fd"); goto fail; + } err = setns(nsfd, CLONE_NEWNET); close(nsfd); - if (!ASSERT_OK(err, "setns")) + if (err) { + log_err("setns"); goto fail; + } return token; fail: @@ -475,7 +484,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("setns"); close(token->orig_netns_fd); free(token); } From patchwork Wed Apr 10 07:29:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623694 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 9301113CFB3; Wed, 10 Apr 2024 07:30: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=1712734242; cv=none; b=MqHZycsQ4jESSroTea9ZH8kCfHuCxSqcCjnOJJ4hPcZuU9CwGPr5+dpgZKXSZurBHQxsJ2cEaKie9c0TSZqMek+qmlMsIhJYFKrrH03ornBa+CMzj/T2BVxTqO7PxPNJ5+g841JL5pnwsls9SlNkhQLf35hB3jU4bx8bsYBFHjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734242; c=relaxed/simple; bh=mgqYB1+Oi/BXz0bANvnkyaMwR8Tdhukzp+HALE0rTuo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KUgPGJXeC9eibGrjg89CiNdDl9w2W7SsCxpK/4W7yULzVIKKOlFmz9pXpPhh5bqpX0V7hpXxHALdREpZcAD2RCcflCN3kbf/Qpl6anowYha3wGNLH+dqlu48ixdRYKYskfSt+WgPlFip7IJ+HQ/shaUaAfR4qujobYBgdQG+New= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cKcdXw2Z; 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="cKcdXw2Z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F110AC43394; Wed, 10 Apr 2024 07:30:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734242; bh=mgqYB1+Oi/BXz0bANvnkyaMwR8Tdhukzp+HALE0rTuo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cKcdXw2ZyfCwdNwosugMQdZI4RNNsdjX+/s+b7K7Fbcv4VZZ5StVcsLvuK6if5E5b 8SEEfuyIZrnQ0OYS8imne7oW3ggO5Lyt9HbZsIj/TNmUV8jHd7oebcehK4ID24iUTw E36nALGN8bnHj3Ry2cYmxllhWaZP6KkJwab1Ldyp2dR3ChzKEF2b5yEyOYVyfzW1Gd RO31e7lp6u7rP7PxZrfOGHsTpQyePFXlcwkvFD06n3V0Iy3VAFXS0Hc6wRLuxpQyy3 mK7TQ5zwLROvHEQiQ8CUYmo0ABphPO46Pk0ao7FfrfOAn85tvKwfCPc6QtFh6d464V qN0hWnXixieDw== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 07/14] selftests/bpf: Use start_server_addr in test_sock_addr Date: Wed, 10 Apr 2024 15:29:40 +0800 Message-Id: <2b5dc831a2978669e74415e34d9cf1fb06e31c1c.1712733999.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 eea5b8deaaf0..60c9edc04f73 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -290,11 +290,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 89f59b65ce76..738b1764f562 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..8fa6bec8f7a5 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((struct sockaddr *)&requested_addr, addr_len, test->type); 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((struct sockaddr *)&expected_addr, addr_len, test->type); 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((struct sockaddr *)&server_addr, addr_len, test->type); if (servfd == -1) goto err; From patchwork Wed Apr 10 07:29:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623695 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 4F3C113C9D7; Wed, 10 Apr 2024 07:30: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=1712734248; cv=none; b=rbBGUQjAV/PyOWcb8LlkPljeRrGByhwLJm0w3NBU++sa6PIb5AZDoGNm9wq8KfsEa5z5q1NOX38dh+RPLPPJP7eeMjszwN1hvc/B9JTJIHFGUBpUWDAEAhmavNuFVPXWgW2jjUudHiAK6Qp2yCXiOgYOP8Ik8OUKTvSnj0+/b3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734248; c=relaxed/simple; bh=zgJeV6DRmGVjHX4hBjMfwVzhEFdDeZ/nyzCdaQvCHS4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bEWapS9/nptKxQUUT44QOE1zBieZgJVQ94yOLNhNX3BVHNQtgyU+V3l4Ip0yozASnTA1ELgSfHpLKW0nlGf/5bFmfqJ58tC8NUnXnZjo/KJrz9dXXomZzV5UR2D0EHi9O28YpPqiZI+CXSsL0M85vgl/xHaLP7eYMXXpmhQwoLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uRAOD7m9; 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="uRAOD7m9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4B9AC433C7; Wed, 10 Apr 2024 07:30:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734247; bh=zgJeV6DRmGVjHX4hBjMfwVzhEFdDeZ/nyzCdaQvCHS4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uRAOD7m9ARxurceey5Wd8IB0sRj8aEEEqo51FnvqDEM9SCDIRbJKUd33D77AfnWZm PkoBkOOIMst8kRfggAWfdaaJTB3cIrI4hEKTRTKF3Z06zcmw+4saAlYtqhf50N/IRx geCsvFFINiCQS/pKalI48Eb2uICOnbMqn96Mt3/4SD8jYz6YoURR79lpjUvnD3tdqj sTnPrxXEgqTp2uZqnGCQ7qrjrJQsBmFle21i2eycZbGwQml5lxXWwgVoquyq6IecZk +ImfOXeeGxjvytR6KH6Isi1LWxxR2JZbPTD4X5aCZhgBQyt8jrux1ZKGuy6Qq0W0aY OGkinPwwwFCUA== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 08/14] selftests/bpf: Use connect_to_addr in test_sock_addr Date: Wed, 10 Apr 2024 15:29:41 +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 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 8fa6bec8f7a5..743ff05e7755 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(&expected_addr, addr_len, test->type); 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(&requested_addr, addr_len, test->type); if (clientfd == -1) goto err; From patchwork Wed Apr 10 07:29:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623696 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 DAEDF13C9D7; Wed, 10 Apr 2024 07:30:53 +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=1712734254; cv=none; b=ftpYa4F1sgK98TDViRQkm9WWaq+v5EMewnAqdIhSN4OOzpj9uWpUXBzKKC9glF9z0HU2b4imndLWEfaDHHnfEYvRE6g3Rl/oRdAwTywstlNic4nvHBUauSbYl7uyMFosOwt8Hesh1f2DW3rcB3TUMw1DMampoIjey5G66e1k0r4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734254; c=relaxed/simple; bh=FHmeh3Gxq3axYQxHKov1FbRWZQgWqcM/yndGkDGtHC4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z6Yn89ulay42hxLaCCqU2bPs6XKcGta/EqOCqQIYjuP76tDfc4zu07VjPK2EbS7sNp036EjJUMdXIwBRz+nTn1gecSDiArlxo8LmZIGTZ7yC2Wu/serQdq/mp8iyz4Bh9tIWPZOjpDbdfjmp1wtWy7461KoukkFDraLHqIZwwGw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EfEkA+aN; 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="EfEkA+aN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72918C43399; Wed, 10 Apr 2024 07:30:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734253; bh=FHmeh3Gxq3axYQxHKov1FbRWZQgWqcM/yndGkDGtHC4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EfEkA+aN1ATrwyEiHaCJCU5Tvj9QxGjxvAWiQjRDpathzE+urBOW6vLUoEZPgVr60 j2eIjbTqkBqEFI/KH2D1GLz7FP5OJwcNLzw6dCXXjGFqzHqUhw5g5l2rSQJkFX77t8 xcMUgZkwkd4g2BMXqtHOg2t7GxT8zQmzcQt8hk6hJ8D2UjltjIbTU9t/QYbIf78Y6K YQrX0FaBmd7wOSWzxbokvuBeQLMuHxUuF/wwDGnzGgcJYlhGw78mm9cwJQKFmCDn9f FBLTQ9fCRRcZQaMEu6goN8Z7GlWoIa5ASyrOlkbuMPwyrE1/lkf5x5EOY0OinfyUqu jCn/R/EI1q+FA== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 09/14] selftests/bpf: Add function pointer for __start_server Date: Wed, 10 Apr 2024 15:29:42 +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 __start_server() sets SO_REUSPORT through setsockopt() when the parameter 'reuseport' is set. This patch makes it more flexible by accepting a function pointer named setsockopt. Then the original start_reuseport_server() can be implemented by passing in a newly defined setsockopt_reuse() function pointer to __start_server(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 9fd271d5d571..ff210f0ad3ce 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -80,9 +80,9 @@ int settimeo(int fd, int timeout_ms) #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; }) static int __start_server(int type, int protocol, const struct sockaddr *addr, - socklen_t addrlen, int timeout_ms, bool reuseport) + socklen_t addrlen, int timeout_ms, + int *(*setsockopt)(int fd, int val), int val) { - int on = 1; int fd; fd = socket(addr->sa_family, type, protocol); @@ -94,9 +94,8 @@ static int __start_server(int type, int protocol, const struct sockaddr *addr, if (settimeo(fd, timeout_ms)) goto error_close; - if (reuseport && - setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on))) { - log_err("Failed to set SO_REUSEPORT"); + if (setsockopt && setsockopt(fd, val)) { + log_err("Failed to set sockopt"); goto error_close; } @@ -129,7 +128,7 @@ static int start_server_proto(int family, int type, int protocol, return -1; return __start_server(type, protocol, (struct sockaddr *)&addr, - addrlen, timeout_ms, false); + addrlen, timeout_ms, NULL, 0); } int start_server(int family, int type, const char *addr_str, __u16 port, @@ -145,6 +144,11 @@ int start_mptcp_server(int family, const char *addr_str, __u16 port, port, timeout_ms); } +static int setsockopt_reuse(int fd, int on) +{ + return setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)); +} + int *start_reuseport_server(int family, int type, const char *addr_str, __u16 port, int timeout_ms, unsigned int nr_listens) { @@ -164,7 +168,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, return NULL; fds[0] = __start_server(type, 0, (struct sockaddr *)&addr, addrlen, - timeout_ms, true); + timeout_ms, (void *)setsockopt_reuse, 1); if (fds[0] == -1) goto close_fds; nr_fds = 1; @@ -174,7 +178,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, for (; nr_fds < nr_listens; nr_fds++) { fds[nr_fds] = __start_server(type, 0, (struct sockaddr *)&addr, - addrlen, timeout_ms, true); + addrlen, timeout_ms, (void *)setsockopt_reuse, 1); if (fds[nr_fds] == -1) goto close_fds; } @@ -188,7 +192,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type) { - return __start_server(type, 0, addr, addrlen, 0, 0); + return __start_server(type, 0, addr, addrlen, 0, NULL, 0); } void free_fds(int *fds, unsigned int nr_close_fds) From patchwork Wed Apr 10 07:29:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623697 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 5260513CF98; Wed, 10 Apr 2024 07:30:59 +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=1712734259; cv=none; b=Fm0RwpkjzGNtoxVGEhRMUiOsFP8V6wVOijbBvt4zXjBAMic43Xznxicq6bRCNuaHsOmvxEy8NGxOA/pCOOEW4vtAwtnnv+IJyDai08N0u1HxNDupUtppgLjrcX3wmr+wx5aznovSO3gfluLyK3hquhUATpiqmyM3yXod9GHyfnE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734259; c=relaxed/simple; bh=+SDCASeBlQbdY90sTxT88yCEbksIr0IlK67a/PVmHMg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=st79bQ6r8oX467HFJBBRxj894pai1+p5e95Ny1SXHjmCfJWVWMxPQSbUbWuC5UwgHnVLz8SxE/rm6ARjEhXbL13t/336n7CdX4FhoEMFLh6OsvvSRN7gTyP7gDn8UK7cZmUjKHAlSeOx+TI/friOJybThxpG1TWU+0s+myCR6ng= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L/7wixBX; 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="L/7wixBX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC377C433F1; Wed, 10 Apr 2024 07:30:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734258; bh=+SDCASeBlQbdY90sTxT88yCEbksIr0IlK67a/PVmHMg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L/7wixBXsfPDs4fs/uEc91FNz2P5hJI4AMxP1wgSpdEUoNuUveHLAv4OPPFK38TqU hkXSBMfhhR5wQNfOAhu5bG/PQOTIMufCitdCUHV081XfJ83mGYT3kjiEm4PWGSmXzM FXQ/gA+Q1gGmUbtgKVUfsyOQJHt3hVx8OG1LCQwspxdM+xQpWfsYhexzw8uED8tUVi X9O7FrGwEI7t4KBSLPLYb0ozvP5TdCvecxxUwPtsEaSqet2Z8SgehWItjGl0IRdz0U 34LF+BmX/+9wZODx1Ra+0+YYh2GyRcIsRQpQcuPWggF8QrHHl8orvBMKPf40IIDG9b 45gRpxMFdW4rQ== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 10/14] selftests/bpf: Add start_server_setsockopt helper Date: Wed, 10 Apr 2024 15:29:43 +0800 Message-Id: <89960c05cde52f06be2cbfb18b500b40aed5c105.1712733999.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 adds a new helper start_server_setsockopt(), which is a wrapper of __start_server(), and accepts a function pointer setsockopt as a parameter. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 6 ++++++ tools/testing/selftests/bpf/network_helpers.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index ff210f0ad3ce..42e19268cd24 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -195,6 +195,12 @@ int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type) return __start_server(type, 0, addr, addrlen, 0, NULL, 0); } +int start_server_setsockopt(const struct sockaddr *addr, socklen_t addrlen, int type, + int *(*setsockopt)(int fd, int val), int val) +{ + return __start_server(type, 0, addr, addrlen, 0, setsockopt, val); +} + 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 738b1764f562..543295230062 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -55,6 +55,8 @@ 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(const struct sockaddr *addr, socklen_t addrlen, int type); +int start_server_setsockopt(const struct sockaddr *addr, socklen_t addrlen, int type, + int *(*setsockopt)(int fd, int val), int val); 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_fd(int server_fd, int timeout_ms); From patchwork Wed Apr 10 07:29:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623698 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 CB2DE13CFA1; Wed, 10 Apr 2024 07:31: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=1712734264; cv=none; b=lNlBDTGWHxjq3RaxiStzISdwe4MnVwsgB/8ldo0QcYDFcUXkCTPzat4sxXMnFknPAWVu+U8b7rXrK9jpr68L6mAntl+JeL+iRm9j0l9Odxm/s64sxX2n7wkHvS62GY98u3lUKsSdWGMgdho9Hy+a8p76h/KtY+vuvmY/HuwS3i4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734264; c=relaxed/simple; bh=ddcUc2GvC6cttiBg+2jPY0ijTi1aBmIgeRcsxp0Owrw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iefPgFyy9p+gc7vh2nk4XkaalS+XlcS7RXvQyNphw7DUxlp2za9CRD/tKYSdwdbEdKMFkHIh00MEBNuBw3zyUu1EosIQH6vac9fT3a1C4eX1lsEvSFyNlrLb95vQhBSEFEu3Eo/xaMZLM9YAn32mCa6NE9/Fkiu6nHev8A1ajPo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jMFOkm9e; 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="jMFOkm9e" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C9B4C433C7; Wed, 10 Apr 2024 07:30:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734264; bh=ddcUc2GvC6cttiBg+2jPY0ijTi1aBmIgeRcsxp0Owrw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jMFOkm9eZUD9ez0myPzvjIRjUFqeYHXjavOvFX2g3LsW87qqsN3PSuT+1BCWeVNYa rs3GSOAHAcKi5iHcHfOInuWjKr2qHT3pMIrGiPYZL/6YDPQdYDVdtmhVwjFMQla3E3 8jGIZhC9rAFxHwnhNyxphhNxzqDosx7Nl8us7TWYn151wIwtZoTSzIkxSLM/A3e2RU unp+EcHYCSrsDzdFwcBNEBOqDcDFFYKPM/QKs9ooJU1IcEm8clVxpKu12wN1Uq7Gsy D2Ra+IeCSJ0oG+Xn3YYPslkAm7UnZmBYgji+ADFxDlCs2ow26ywrFSyuY3zGTg7Xr7 OMi7yhFM8Ty+Q== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 11/14] selftests/bpf: Use start_server_setsockopt in sockopt_inherit Date: Wed, 10 Apr 2024 15:29:44 +0800 Message-Id: <4452c137ced92a1b513acf35ef2546db9298b644.1712733999.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/sockopt_inherit.c, use public helpers make_sockaddr() and start_server_setsockopt() instead of the local defined function start_server(). This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../bpf/prog_tests/sockopt_inherit.c | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c b/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c index 917f486db826..f000d9229765 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c +++ b/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include "cgroup_helpers.h" +#include "network_helpers.h" #include "sockopt_inherit.skel.h" @@ -98,23 +99,12 @@ static void *server_thread(void *arg) return (void *)(long)err; } -static int start_server(void) +static int setsockopt_loop(int fd, int val) { - struct sockaddr_in addr = { - .sin_family = AF_INET, - .sin_addr.s_addr = htonl(INADDR_LOOPBACK), - }; char buf; int err; - int fd; int i; - fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) { - log_err("Failed to create server socket"); - return -1; - } - for (i = CUSTOM_INHERIT1; i <= CUSTOM_LISTENER; i++) { buf = 0x01; err = setsockopt(fd, SOL_CUSTOM, i, &buf, 1); @@ -125,13 +115,18 @@ static int start_server(void) } } - if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) { - log_err("Failed to bind socket"); - close(fd); - return -1; - } + return 0; +} - return fd; +static int start_server_lo(void) +{ + struct sockaddr_storage addr; + socklen_t addrlen; + + if (make_sockaddr(AF_INET, "127.0.0.1", 0, &addr, &addrlen)) + return -1; + return start_server_setsockopt((struct sockaddr *)&addr, addrlen, + SOCK_STREAM, (void *)setsockopt_loop, 0); } static void run_test(int cgroup_fd) @@ -160,7 +155,7 @@ static void run_test(int cgroup_fd) if (!ASSERT_OK_PTR(link_setsockopt, "cg-attach-setsockopt")) goto close_bpf_object; - server_fd = start_server(); + server_fd = start_server_lo(); if (!ASSERT_GE(server_fd, 0, "start_server")) goto close_bpf_object; From patchwork Wed Apr 10 07:29:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623699 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 5DACB13CFA1; Wed, 10 Apr 2024 07:31:11 +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=1712734271; cv=none; b=tJfz+RqZEf+kEaav5I/Axj0z8V2olzB+K9SL3xhoI+sLjMveNS5oFT6bYXAdf6y86cVQ15RLuJRd0mU84RqVOEClorh6ruXhbkAc+Uzos7vwd1XyAh/JOOyYm+Zp0lSJJz5uXbVko4v/4ZGqqLrhDGjPHUZgl031tU6Zn/OvIHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734271; c=relaxed/simple; bh=yZjR/xeezxILen9lY9+6wkHKExavQ/iubE/fh5VTeiw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tOQpvYKOIQsbzjBKe3J3emMF4WUCUWVyQqEo5PD9bi5Ja4c3mjuE75+w3SrBHSCJwL8vpzdrjRDkhLrVrUWXN0L+o123bGaSYfASYMvYuV6wl91OAhF/PXh6UhoN9rroVAK/L1BE1RxGkbvtX1H3amfu11wwS5z/l0Im1ESmd4I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NwfIvGjH; 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="NwfIvGjH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56189C43390; Wed, 10 Apr 2024 07:31:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734270; bh=yZjR/xeezxILen9lY9+6wkHKExavQ/iubE/fh5VTeiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NwfIvGjHz6lk/GwyeSH/PJHH9+X+oCSOGy0SnBwdLfxZ948IHhXVwpnirwrR/JBJK ksa1Rl9+MF0LJGIigHe42jd7dUg7Ny7DMajrcb+D3P1ym2hKTlO97fCOdZLzkrwjtz r0cgsNn3HEYlpeMnaHeJQNGmAU3JIz0CBMsG+Wf74eYa2IkGl4aNWLMYAFR3uRW8o3 tSZjcqvflVEXYqlydZVj7fD62X0o0yxxu+194K+TNj/xmMq3Lp5xNUE8SptN0IvP4v gznXsM5e35kiFmpHwYPpW/ijKksaOArepxGZO617EJ9MpSlJPwVRS3lWyF47KEmArc muG+Guew+lDqQ== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 12/14] selftests/bpf: Use connect_to_fd in sockopt_inherit Date: Wed, 10 Apr 2024 15:29:45 +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 This patch uses public helper connect_to_fd() exported in network_helpers.h instead of the local defined function connect_to_server() in prog_tests/sockopt_inherit.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../bpf/prog_tests/sockopt_inherit.c | 31 +------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c b/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c index f000d9229765..4fee4b343255 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c +++ b/tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c @@ -10,35 +10,6 @@ #define CUSTOM_INHERIT2 1 #define CUSTOM_LISTENER 2 -static int connect_to_server(int server_fd) -{ - struct sockaddr_storage addr; - socklen_t len = sizeof(addr); - int fd; - - fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) { - log_err("Failed to create client socket"); - return -1; - } - - if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) { - log_err("Failed to get server addr"); - goto out; - } - - if (connect(fd, (const struct sockaddr *)&addr, len) < 0) { - log_err("Fail to connect to server"); - goto out; - } - - return fd; - -out: - close(fd); - return -1; -} - static int verify_sockopt(int fd, int optname, const char *msg, char expected) { socklen_t optlen = 1; @@ -168,7 +139,7 @@ static void run_test(int cgroup_fd) pthread_cond_wait(&server_started, &server_started_mtx); pthread_mutex_unlock(&server_started_mtx); - client_fd = connect_to_server(server_fd); + client_fd = connect_to_fd(server_fd, 0); if (!ASSERT_GE(client_fd, 0, "connect_to_server")) goto close_server_fd; From patchwork Wed Apr 10 07:29:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13623700 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 B1A4F13C9D7; Wed, 10 Apr 2024 07:31: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=1712734277; cv=none; b=pVhBp+2Mhf5+LckXi/iHf9NdcGLXmri3+Rr5VCCXvolweMxZECax8ZZcGhPRg0C5wlgu08OGKjPLWiq78ZODq2FBS4Fkx4M/jK3trX9pylDUxKLm0W8Yb51TMQK6XUaAbHwAbeBq2RAgeZ7xNlvHjIyi72QL8xbOdYvHcCJNGKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734277; c=relaxed/simple; bh=8vFSssmI4/kc94zG0VyHk4HODVY34CyEtQSmqYfN5/Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AZhpsJFWWHDmj2XasEpTz11LhvX0mNRVqMcGfNkNlhhnn7CBr7OkCdtOCYm5fLeOpEnVc3w19sHOhSmr2q8QAIfZVgDcybeZb6k1KijGNaJZdVcRw2wgKyjqs3Upquk3XgmYpOsxX9VlcGsvCRRxWGeEo+fsf88dG/A/QUNstoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=X3Q6KbJs; 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="X3Q6KbJs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED9E8C433A6; Wed, 10 Apr 2024 07:31:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734277; bh=8vFSssmI4/kc94zG0VyHk4HODVY34CyEtQSmqYfN5/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X3Q6KbJsaDHa1PdiBamw3IOKQDhuX6vsrnGJcK3Qbgb+EAereZGi0jRmQTir12b/F DG47wm1jiJUCNQ/d9/uRwOlVWIT8J/xNsWQx/iu0yPUa60kTH8fOGJmqfkH4yJB8xW kXgT8lfN0sAXW0g2XMAO5Fi1v+JOQnh83sau/FfyojANuJFTNu6ZMQ3YjESo9qZJB+ B0n0KMDKYW+ZoHrudxcxR7x5hRw9kGS+fgoJmM47YyWRCa6rU630l30IKxOqi2mmJ7 Grh2TgvVikBmIBLLmgqMopCWTObwfDq29pLWOoHi+6b3DiNmCVXfHHMDBGgSbMM3a0 2i8Lrt8NTtrqw== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 13/14] selftests/bpf: Use start_server_* in test_tcp_check_syncookie Date: Wed, 10 Apr 2024 15:29:46 +0800 Message-Id: <7ace7f90ad09f4136a5c590c79216922dd8167db.1712733999.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_tcp_check_syncookie_user.c, use public helpers start_server_addr() and start_server_setsockopt() in it 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 to avoid compilation errors. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/Makefile | 1 + .../bpf/test_tcp_check_syncookie_user.c | 50 ++++--------------- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 60c9edc04f73..4f42dd241c38 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -309,6 +309,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_tcp_check_syncookie_user: $(NETWORK_HELPERS) BPFTOOL ?= $(DEFAULT_BPFTOOL) $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ diff --git a/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c b/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c index 32df93747095..e6ad4895d2b3 100644 --- a/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c +++ b/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c @@ -16,43 +16,13 @@ #include #include "cgroup_helpers.h" +#include "network_helpers.h" -static int start_server(const struct sockaddr *addr, socklen_t len, bool dual) +static int setsockopt_mode(int fd, int dual) { int mode = !dual; - int fd; - fd = socket(addr->sa_family, SOCK_STREAM, 0); - if (fd == -1) { - log_err("Failed to create server socket"); - goto out; - } - - if (addr->sa_family == AF_INET6) { - if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, - sizeof(mode)) == -1) { - log_err("Failed to set the dual-stack mode"); - goto close_out; - } - } - - if (bind(fd, addr, len) == -1) { - log_err("Failed to bind server socket"); - goto close_out; - } - - 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; + return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, sizeof(mode)); } static int connect_to_server(const struct sockaddr *addr, socklen_t len) @@ -259,18 +229,20 @@ int main(int argc, char **argv) addr6dual.sin6_addr = in6addr_any; addr6dual.sin6_port = 0; - server = start_server((const struct sockaddr *)&addr4, sizeof(addr4), - false); + server = start_server_addr((const struct sockaddr *)&addr4, + sizeof(addr4), SOCK_STREAM); if (server == -1 || !get_port(server, &addr4.sin_port)) goto err; - server_v6 = start_server((const struct sockaddr *)&addr6, - sizeof(addr6), false); + server_v6 = start_server_setsockopt((const struct sockaddr *)&addr6, + sizeof(addr6), SOCK_STREAM, + (void *)setsockopt_mode, false); if (server_v6 == -1 || !get_port(server_v6, &addr6.sin6_port)) goto err; - server_dual = start_server((const struct sockaddr *)&addr6dual, - sizeof(addr6dual), true); + server_dual = start_server_setsockopt((const struct sockaddr *)&addr6dual, + sizeof(addr6dual), SOCK_STREAM, + (void *)setsockopt_mode, true); if (server_dual == -1 || !get_port(server_dual, &addr4dual.sin_port)) goto err; From patchwork Wed Apr 10 07:29: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: 13623701 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 519E413C9D7; Wed, 10 Apr 2024 07:31:23 +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=1712734283; cv=none; b=Rj+zCnKJJ6RNTB3h0q/Rrq6OhjR3//XYV3pTpFe5pgUFd7BQpUmcoCpH28Jlo1TIiEWtq/oA11GjKivdph7l8fKFZ8KF5Y3sdMfGTTpsuLMD/VBwVRAY14MAAyM0bfrM2fwb3fhjLD1Yq86VtiXo5o6LCx7IDwU0HRc8FluOQnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712734283; c=relaxed/simple; bh=UMsJz02F0Aq81sLV0Yh1Banv8hIJKOdtNjKb5msmB68=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nvGPAyfbE7PnKZX3T9lM8SK4N54jdrDVj6HyeLOeCMc9En1WWC/fqX2hvmnIHlqB8+fSeki38zms6if/lRdw2rGhE+63ztVQlcluqUGIFs0KARBri8p8ZfAQoCM2BCEkxJN7xm6OHrpoBlI0wLKPZ8OI6tGxLbxILvpU23UleQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oBrbwd5o; 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="oBrbwd5o" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7CE1C433F1; Wed, 10 Apr 2024 07:31:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712734282; bh=UMsJz02F0Aq81sLV0Yh1Banv8hIJKOdtNjKb5msmB68=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oBrbwd5o8HWzeckdlCK1pQlFqykBqYI1by6d5/CcBAE0VPKkC6f5jUYeSNqOIlp1r 7Ju2XzQdMtsB2IrQgAV4flkMrsgw9C/lepxIACtI4k6nY8gZteHVS0QkhvvSpaSzl2 tAUo828ch0rk/gmikVY+ajs1bjPaa8dkwB5KTz9T3Ui3IcCmv9Gb3mniLfYX4JL6WA /UI+AVTk1y3WNAnRSvx57Cnvc3FwEAR0oUdMiXtCKtQKoCAfpqnMFl1ylyRWqLxqic 7CLo+PNHK+07WIl3n94241pmcwQ3vPzK/7B0lbASidLyUQ5kz/M9w0YSH8JNY0QJ8a ObTAHGCnredzQ== 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, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 14/14] selftests/bpf: Use connect_to_addr in test_tcp_check_syncookie Date: Wed, 10 Apr 2024 15:29:47 +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 This patch uses public helper connect_to_addr() exported in network_helpers.h instead of the local defined function connect_to_server() in test_tcp_check_syncookie_user.c. This can avoid duplicate code. Signed-off-by: Geliang Tang --- .../bpf/test_tcp_check_syncookie_user.c | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c b/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c index e6ad4895d2b3..e678228b915f 100644 --- a/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c +++ b/tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c @@ -25,30 +25,6 @@ static int setsockopt_mode(int fd, int dual) return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, sizeof(mode)); } -static int connect_to_server(const struct sockaddr *addr, socklen_t len) -{ - int fd = -1; - - fd = socket(addr->sa_family, SOCK_STREAM, 0); - if (fd == -1) { - log_err("Failed to create client socket"); - goto out; - } - - if (connect(fd, (const struct sockaddr *)addr, len) == -1) { - log_err("Fail to connect to server"); - goto close_out; - } - - goto out; - -close_out: - close(fd); - fd = -1; -out: - return fd; -} - static int get_map_fd_by_prog_id(int prog_id, bool *xdp) { struct bpf_prog_info info = {}; @@ -114,7 +90,7 @@ static int run_test(int server_fd, int results_fd, bool xdp, goto err; } - client = connect_to_server(addr, len); + client = connect_to_addr((struct sockaddr_storage *)addr, len, SOCK_STREAM); if (client == -1) goto err;