From patchwork Mon Jun 24 04:33: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: 13708887 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 A9ADBD502; Mon, 24 Jun 2024 04:34: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=1719203677; cv=none; b=f2BhLo0JjNFqVTkWC/9xi56AEnf3sU9SLhDuqoLLOKUJjzAu/P2+SZVkjljmLy5sTy7BbMM9Hu2VBtE67UUJcfVsWCA45768huiJ2D1WqPD8isCAuFhGPMbcvvJ3U24o2HtHnvUML9DgY97zcSBVjpM0w2qG3rOg7NXQe8UW48c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203677; c=relaxed/simple; bh=6J3QFqYCAkOLhnKytwHZMLodd5u01jabVIARSdmS/Qk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rhx2YXTkBqD6AY8/It+npEIL9Ecrst9bhKQU58bJwQuG3rLJC0E3q3fkXKin1AMNEMTiUG9LwXj1nxu5MSjscAmJnExXIPxtgNoV6OPGipbUl14Mm0UR3mzjf94VlZma7KlVmQHUrWVPKs008nM3rWwKtbxlh6d7fdNM6qdwY/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RPxPRtwP; 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="RPxPRtwP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AB8CC32786; Mon, 24 Jun 2024 04:34:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203677; bh=6J3QFqYCAkOLhnKytwHZMLodd5u01jabVIARSdmS/Qk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RPxPRtwPz7Hn9A1r3DvBUwhImgwPNt5IYCcGHGhv1yXGNtP1qIP9grzdYUNDwjN4I niIbdrsboxNivMhSXFikd+785hi/MD+mqqUVRGV//MjA1RSf8G+R2bPkXu0XeXR+bf 5pDZEKq4kO83OKkOadXDHdNGzWVW0pn0cJNMHRs+3EI9frTLW620OT/p6GFambMX5f 6ZERD2jm4ECJ5DETGrtsZP1e6ehiS4LbyW8S/rFvaOv72C4Pt60ZZGhBee/0RXTsB2 deqYt/2/TDW3uxi2tXuHhXIo9FpPeawlAfOMp6y48OjCHlHRsFf3Rh7Ot2DUK1FYGb H5udKrbibaqdw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 1/9] selftests/bpf: Add backlog for network_helper_opts Date: Mon, 24 Jun 2024 12:33:48 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 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 Some callers expect __start_server() helper to pass their own "backlog" value to listen() instead of the default of 1. So this patch adds struct member "backlog" for network_helper_opts to allow callers to set "backlog" value via start_server_str() helper. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 44c2c8fa542a..16cbb3fdcabf 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -106,7 +106,7 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl } if (type == SOCK_STREAM) { - if (listen(fd, 1) < 0) { + if (listen(fd, opts->backlog ? : 1) < 0) { log_err("Failed to listed on socket"); goto error_close; } diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 9ea36524b9db..8339c4e4b075 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -25,6 +25,7 @@ struct network_helper_opts { int timeout_ms; bool must_fail; int proto; + int backlog; int (*post_socket_cb)(int fd, void *opts); void *cb_opts; }; From patchwork Mon Jun 24 04:33: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: 13708888 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 802E379CF; Mon, 24 Jun 2024 04:34: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=1719203682; cv=none; b=Nyg1+9EVJtQweGphiqfJC6ZBILjLqZiXpYUrBDVfbsxst8RjtaPtzz/zOmiHtY6ytmFSTvZ8DqdMrkeDbL8hv3fuZ79U2x6xdMG2UJluhONUuoW1fPDFfo396QbJc3BojdduVzLpESEZQGVjGbjSnkKb914A6KqAyQesTd4z45U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203682; c=relaxed/simple; bh=RPUpwnHqWv9OF1LgymXpVyycZoyJsq5oTKQuRymN69I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JTkURTX/JKfiMXvKSNGU9dh/9RAXAsSB3NGGBNTfdjjM8j7qBXGL7xLniFYtil+3NSdDdVTdWWlkaE1IddYhv8tiRieDc1RCMiiEDU6+Ftx49uqECrCX+cSnhALgj3lEmB7L7EyHKTUJsUmYBuLdizzxQ4jggohrnTub9lm9vaE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=s3mcTMQs; 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="s3mcTMQs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C726DC2BBFC; Mon, 24 Jun 2024 04:34:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203682; bh=RPUpwnHqWv9OF1LgymXpVyycZoyJsq5oTKQuRymN69I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s3mcTMQsr/zVMS1qZBmwIQ5bygydOv0OeVP2JdvwnSm/xKJV6tVZwZFE5SoCvtsTy g/vzGk5cmcYuIOmole3BWKiHV6RCTtO9awnaXGD+TRUybzC4S6581RmLuxCrDcuvhJ FplZzCsMeI9w8HIAUwYo7XGYMJd/e6Q9rh5MqS3alICnPkHyt1WCenSNgN/xWZWoQo v8gNCZfs+AW+fI7LJznyk2kNFLYcrtn2Im6HAMp0N0NlHkNd8ZaO6NoyX3PNpafu8X Q3yn9tMxl3eNYBjKZH/k4Q9P9dtDqpu3HKk/Y3UpPPvuHXKWvi8qxS0ZfyX/4J4/x2 1sEnEydIoCHCw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 2/9] selftests/bpf: Use start_server_str in sockmap_ktls Date: Mon, 24 Jun 2024 12:33:49 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 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/sockmap_ktls.c, use public network helper start_server_str() instead of local defined function tcp_server(). This can avoid duplicate code. Technically, this is not a one-for-one replacement, as start_server_str() also does bind(). But the difference does not seem to matter. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 2d0796314862..4dc7933bb556 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -6,25 +6,11 @@ #include #include "test_progs.h" +#include "network_helpers.h" #define MAX_TEST_NAME 80 #define TCP_ULP 31 -static int tcp_server(int family) -{ - int err, s; - - s = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(s, 0, "socket")) - return -1; - - err = listen(s, SOMAXCONN); - if (!ASSERT_OK(err, "listen")) - return -1; - - return s; -} - static int disconnect(int fd) { struct sockaddr unspec = { AF_UNSPEC }; @@ -35,11 +21,14 @@ static int disconnect(int fd) /* Disconnect (unhash) a kTLS socket after removing it from sockmap. */ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) { + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + }; struct sockaddr_storage addr = {0}; socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; - srv = tcp_server(family); + srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); if (srv == -1) return; From patchwork Mon Jun 24 04:33: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: 13708889 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 41B9ED529; Mon, 24 Jun 2024 04:34:46 +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=1719203687; cv=none; b=bGSpCpPp3N67wxO5qJWOmxKcUoby0vIDL2kGE6DxIFTzOo7OQLZX3LPflqgesgFhWm4gfvCqQdaNovFFzuxMCor4Zp/ml2SBIcukUtd86JB/KrQuwtnckuudyriUwAoR2jWEc/8FXHUXBMED7a5xf0r7vvZGoGoXEaPS/uL5VpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203687; c=relaxed/simple; bh=SX7vg9jAuZBrRh97twO0P4gGLK++5xW/iSIpKE0CJtE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VeIwpRISWYQ+WpUyafCp/n5dvg98XaTMXAYtQpHg/924b+aqoYJ3scElrBjYFkULNGYRggskSsRrns+wA2kG3UMpNH8e0ojRC3xEAbvsPQeC1jjtcuye8Gquu578QWx6D17atWi4fvmTPzRqO7T4TWwtQ1R+Pzg0BLmNXW89Urw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SD7XCC/0; 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="SD7XCC/0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91381C32786; Mon, 24 Jun 2024 04:34:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203686; bh=SX7vg9jAuZBrRh97twO0P4gGLK++5xW/iSIpKE0CJtE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SD7XCC/0nMvB0rnpCYWVU/KjSjMuL4fiKEWNfZCWFB35gXO4kn8H7yBKUX31lakNV Wp6vgHIJ/IKre+4Y+2AnIGN/jpezSmC6GBFABJvsXCWHLo1BcqMouKEm/IjGrNV4PU Ok8NWhSzw3lggZkcQlSCoocqRHY5lIonbTcVZpjW5TzLymhApTMytTLxn63PGafhqS 2L4r71lJZ9Sh1zP5pmJfqpHxHE2wV8xaKEo/FXz5KwJRBEpdxqWEpn9UJkF7j1R/9o E9zChzxLgosE7NhOOxr/HlMj0EjsNoYS/nFe7yXo2kWUdRq2yxf1jJHw3P3zzDLzN0 JS+1ou6BXoLdQ== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 3/9] selftests/bpf: Use connect_to_fd in sockmap_ktls Date: Mon, 24 Jun 2024 12:33:50 +0800 Message-ID: <8e07fd6509d105c4529d3fb57dc030337d57e47a.1719203293.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 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 Use public network helper connect_to_fd() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 4dc7933bb556..a6b0ed633505 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -24,26 +24,16 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) struct network_helper_opts opts = { .backlog = SOMAXCONN, }; - struct sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); if (srv == -1) return; - err = getsockname(srv, (struct sockaddr *)&addr, &len); - if (!ASSERT_OK(err, "getsockopt")) - goto close_srv; - - cli = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(cli, 0, "socket")) + cli = connect_to_fd(srv, 0); + if (!ASSERT_GE(cli, 0, "connect_to_fd")) goto close_srv; - err = connect(cli, (struct sockaddr *)&addr, len); - if (!ASSERT_OK(err, "connect")) - goto close_cli; - err = bpf_map_update_elem(map, &zero, &cli, 0); if (!ASSERT_OK(err, "bpf_map_update_elem")) goto close_cli; From patchwork Mon Jun 24 04:33: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: 13708890 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 1622FC8FF; Mon, 24 Jun 2024 04:34: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=1719203692; cv=none; b=HOpyvFTfb+QU7Sf4KmZXuNHcefUSWlJwtbZWEkM9U9r2jo0EzTZX+zuhWB2WB4E0qJ0oj67aCqb8O58yPL0AQifkxniD7M9gXGl8slCMapui9jQbU1dkeKDSyoSpCkUXnZrmhl/OThruO5zIOR9h2AIiUdStaMrqdu3QVMZcsD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203692; c=relaxed/simple; bh=QDEh2CYMbBuWgLm2dE1VLkBeQTd6vXFkfnNF78tpSvA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CKo6jUYXHvNUSzcl+k4hpKmetod3m+d11Z/CoNAAr7AEQxkP3jvpJjrMPYWaGAN8F0pPS6qqZ97fcYMnKVHorl6ScfqG21t3lHUbckwQ3+B2mU3qHRr1+RtDgJgEzh60jBK5bIe84ZXIsVtCbSDAh8ybcN5Lcu6xTRONrntA4kk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MPBmMieM; 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="MPBmMieM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BEA1C2BBFC; Mon, 24 Jun 2024 04:34:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203691; bh=QDEh2CYMbBuWgLm2dE1VLkBeQTd6vXFkfnNF78tpSvA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MPBmMieMC1Zbf+SSuCKJ9pX3OGnZAONyMBbq9rv16ocf9ln/TdZw1DfWwj1m4OiJw 6XhMW44XtZ6NQ5DpufQYtXCG0lOvpVLB35NEHoEV6h5T7LEwupJTWt/9KRt/ewOiBw 0OEK66g5khKIp85i4LyJTPVrHkUyt6OGF8faqa7VToLKXpnJsVrmWunBX3RLOim/kn 94ePu6UwGyB5OEBPojfaHnEToN+ufWXlXOuWIwj+E/OpOxGOJLbvlSpeq6Oatk38vc MJWybva7aaS+UMzmFXILgv6vgjLlezSqHEvithmPAbnx3fTCckfpHmKqDqAgW8wlLW Knpi11q8xb6sA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 4/9] selftests/bpf: Use make_sockaddr in sockmap_ktls Date: Mon, 24 Jun 2024 12:33:51 +0800 Message-ID: <7f6985c0ad1c031662ff677b6b4be5f51f920c4a.1719203293.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 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 make_sockaddr() exported in network_helpers.h instead of open-coding in sockmap_ktls.c. This can avoid duplicate code. Signed-off-by: Geliang Tang Acked-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index a6b0ed633505..34fdb1cf10f1 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -59,23 +59,11 @@ static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map { struct sockaddr_storage addr = {}; socklen_t len = sizeof(addr); - struct sockaddr_in6 *v6; - struct sockaddr_in *v4; int err, s, zero = 0; - switch (family) { - case AF_INET: - v4 = (struct sockaddr_in *)&addr; - v4->sin_family = AF_INET; - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *)&addr; - v6->sin6_family = AF_INET6; - break; - default: - PRINT_FAIL("unsupported socket family %d", family); + err = make_sockaddr(family, NULL, 0, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return; - } s = socket(family, SOCK_STREAM, 0); if (!ASSERT_GE(s, 0, "socket")) From patchwork Mon Jun 24 04:33: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: 13708891 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 69EFCC8FF; Mon, 24 Jun 2024 04:34: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=1719203697; cv=none; b=gubX+OxQfYzdXhJm/2TBehHpTg7W3ePL/+2TQMZ1CD94n/xsxPMtJ3rVmudT+h0khzXZ8cL86lwDiTLgqxSrv7ByWCMM+ciqF2ARleDK4FKwLjD4VrOtXDk+ydc1gpjxlm+cMfoGRuIllyF8pJsVc3XmNRYt0CRI+BwMfrvh8Lw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203697; c=relaxed/simple; bh=77ptUYc9mTzaKoAWPDm8Ub6GybZslnYg2gGWI2kKJhA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ARaqXdTlg8jOjCVJlbbRYDmh7z7lO6rp2mZnuzmve+pewH/YQ73GuehuA59FAx+LsWbaoZQWiAE82eB9CxVcYhwinHY4OglL0wxKSS5YugsQ+HClFcw6JFbQh+pVvG1UC59mN96SL2elI9sYPBeC0gAkyedMAiMkG8aJUvNKDZs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i52dzCmC; 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="i52dzCmC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25F7FC32786; Mon, 24 Jun 2024 04:34:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203696; bh=77ptUYc9mTzaKoAWPDm8Ub6GybZslnYg2gGWI2kKJhA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i52dzCmCLBOPDBxDPD5M+wzykLF0Ajq4LDMaZeUP/rZuXgUcOBnVjzg982lmZEwu7 hwT19bAwOfnkr3vX+GCEHoV+cTGPMraybEbYdqstlyr3yTUlvQevKqv7/H51D4tYP/ EDlaT2NOKXWKUWQ7HQEMlMGmUr1E77HlTybFgswUhcbCgRzAWyoKJ5nm4878tLAExx sw0g5Y7Y0PuOp/qlzUcDG/07TgJv5v5ojS6ZilRNFSevRMdqROWT2a+SIGHSTzgaSd MPnvggzGoEmCCSMQox3tSLsTZJejSbJHnUz1/IYqLNmpFJ/cYA7zHRTh5OWjypFG5I YDZwe6+29Cjdg== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 5/9] selftests/bpf: Close fd in error path in drop_on_reuseport Date: Mon, 24 Jun 2024 12:33:52 +0800 Message-ID: <956fc734ac44a0b458b8819960cce2ee5a03dea1.1719203293.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 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 Server 1 fd should be closed in the error path when update_lookup_map() fails. This patch fixes it by goto "close_srv1" instead of "detach" lable in that case. Signed-off-by: Geliang Tang Acked-by: Eduard Zingerman --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 597d0467a926..de2466547efe 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -994,7 +994,7 @@ static void drop_on_reuseport(const struct test *t) err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) - goto detach; + goto close_srv1; /* second server on destination address we should never reach */ server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port, From patchwork Mon Jun 24 04:33: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: 13708892 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 CBE24DDC1; Mon, 24 Jun 2024 04:35:01 +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=1719203701; cv=none; b=MoY+srqrRmPA82ED0XxnpN374h90coi8Dag2vGJq4HEFhAiN+8lZsjG2ijhNXtBEyJmqjjPxttLybWdHTe8BedCPqBA/mcFGd3NkDAHYW0Lz4K2FCCbGQRPpMN1VL0D4+ew8BCaxglXYy4s1xe7owAjCEZHpvscyZk3GkxPDYp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203701; c=relaxed/simple; bh=24De1xKL58mJSoi6TfZj/WEwDXisabbp5ItsRB2yUd4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HKsjAKaNI/m15TilDiXSlITtAf2/uZghQvRqBdanW2TRREk+TEwygCEkLEyZRb7MclEBn+ZvCkoNt8XpR/dpYxekFq8la1JkxcZG8Qc53QsypMvHBCEFi1bUAQXnxEP30CRMaFasM5Vot/kSYZ+RYebxCy7xFURnKXzB4t//30Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tVQQaRIy; 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="tVQQaRIy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7042EC2BBFC; Mon, 24 Jun 2024 04:34:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203701; bh=24De1xKL58mJSoi6TfZj/WEwDXisabbp5ItsRB2yUd4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tVQQaRIy62DSBHswtGtzyqHAIcoo5W0qH4b0ldMBh5K2zrq5d2zbFHkbU4G3t3jSA SfA3s4h4RNqDQKRgPIlNSWTaHUpZrJRSq2RTNoaQ0R8eX4xGabqCHvU5Jmc2jbwe/G mj6+OaisSvfOxqQUjRaUqjch72W2SPZ8oX2aR/xaI/vTTrCYIXA5DAx1XPeqOTNAQQ 2g/w2FGj4Bf/WcBEX3FIJ5gmlpdoailDVdZHJSkxJLMO6+Zb1CIK+0zFxVdbsy8IXG qoID6uMITqWGHlSQkFI5+8HDg4uB2QjawqtsmajSqZP2uV32I2gszCpil5v2cs/KsA P69AZs9reHgdg== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 6/9] selftests/bpf: Invoke attach_reuseport out of make_server Date: Mon, 24 Jun 2024 12:33:53 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 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 facilitate subsequent commits to drop make_server(), this patch invokes attach_reuseport() out of make_server(), right after invoking make_server() if the passed "reuseport_prog" argument is not NULL. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index de2466547efe..d87dfcf5db07 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -204,15 +204,6 @@ static int make_server(int sotype, const char *ip, int port, } } - /* Late attach reuseport prog so we can have one init path */ - if (reuseport_prog) { - err = attach_reuseport(fd, reuseport_prog); - if (CHECK(err, "attach_reuseport", "failed\n")) { - log_err("failed to attach reuseport prog"); - goto fail; - } - } - return fd; fail: close(fd); @@ -610,7 +601,8 @@ static void run_lookup_prog(const struct test *t) server_fds[i] = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, t->reuseport_prog); - if (server_fds[i] < 0) + if (server_fds[i] < 0 || + attach_reuseport(server_fds[i], t->reuseport_prog)) goto close; err = update_lookup_map(t->sock_map, i, server_fds[i]); @@ -636,7 +628,8 @@ static void run_lookup_prog(const struct test *t) reuse_conn_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, t->reuseport_prog); - if (reuse_conn_fd < 0) + if (reuse_conn_fd < 0 || + attach_reuseport(reuse_conn_fd, t->reuseport_prog)) goto close; /* Connect the extra socket to itself */ @@ -878,6 +871,9 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach; + if (attach_reuseport(server_fd, t->reuseport_prog)) + goto close_srv; + client_fd = make_socket(t->sotype, t->connect_to.ip, t->connect_to.port, &dst); if (client_fd < 0) @@ -992,6 +988,9 @@ static void drop_on_reuseport(const struct test *t) if (server1 < 0) goto detach; + if (attach_reuseport(server1, t->reuseport_prog)) + goto close_srv1; + err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) goto close_srv1; From patchwork Mon Jun 24 04:33: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: 13708893 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 F1605D272; Mon, 24 Jun 2024 04:35:06 +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=1719203707; cv=none; b=ReiZPC7vt8cjPWkQBh0ZEUhGrCfhaycnMxfzvMp8gJo56Ttx1dgMEqTlq6FxGOAtzwWaewGknIIqLq7Lc1P+snEPQq7iKRokT71SKtunrtC8l6MSZbPySuTZKwHdLnrPLvaBkT1VtDgxwemjnRCE2h3D3B2XbMJ0vZGYWz4oPs4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203707; c=relaxed/simple; bh=uJQBUbS6RPM4Xxdn++RhzaL/J0PbD/chkKcS3w7EuFk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JRgiq2bS3l15U+3R4Op5CuZhj4Pt3Tqh8tUIh2cAMBRWHCocB27uZaxZKsUmVIG6bRQ2Q+qzbEpeVm0jyRbINy/MGS6Ac9slck74ZYZCSs7upZzxoRieEeuNB7+37IZzFIkfr7K8tM/oBTkNaHhZvkNdNZDP43CiKjhyZAYwTGo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BByVBVf7; 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="BByVBVf7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40D8FC4AF0B; Mon, 24 Jun 2024 04:35:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203706; bh=uJQBUbS6RPM4Xxdn++RhzaL/J0PbD/chkKcS3w7EuFk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BByVBVf7StKiXAfVQWodAfEUlV0e94DKmbSPm7ehQawzBErY5E77iemRE2IFSQ1d+ FJitXoA3YBoC08ZF/G1hER5e+MmSZ9zJ/9BJC2k2AUY2k4Qz2LqSgBI8qCfJPV4TdB 5bGAHRfmE7K3kRXhQ0Fk1rHSCWLDSR5b7JmeHwnB12teHYQeAXdJ+khaPcM5fDLVJn 0H/u3qTkCwV4mU6GVs8JMnlhIxNBPV/UAxU2PH8F2i2eXildaqPM3ze0gCdxHAH11n 6hKEx2mZUS3uEpIkdbK/TntR1NDi0zQ1+E+nVUNIUGeSo64FGFeI0FylY7NK1m3wrE eowJEvngnd9sA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 7/9] selftests/bpf: Use start_server_str in sk_lookup Date: Mon, 24 Jun 2024 12:33:54 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 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 start_server_addr() instead of local defined function make_server() in prog_tests/sk_lookup.c to avoid duplicate code. Add a helper setsockopts() to set SOL_CUSTOM sockopt looply, set it to setsockopt pointer of struct network_helper_opts, and pass it to start_server_addr(). Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 141 ++++++++++++------ 1 file changed, 96 insertions(+), 45 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index d87dfcf5db07..634c2ac0595e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -77,6 +77,12 @@ struct test { bool reuseport_has_conns; /* Add a connected socket to reuseport group */ }; +struct cb_opts { + int family; + int sotype; + bool reuseport; +}; + static __u32 duration; /* for CHECK macro */ static bool is_ipv6(const char *ip) @@ -142,19 +148,14 @@ static int make_socket(int sotype, const char *ip, int port, return fd; } -static int make_server(int sotype, const char *ip, int port, - struct bpf_program *reuseport_prog) +static int setsockopts(int fd, void *opts) { - struct sockaddr_storage addr = {0}; + struct cb_opts *co = (struct cb_opts *)opts; const int one = 1; - int err, fd = -1; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; + int err = 0; /* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */ - if (sotype == SOCK_DGRAM) { + if (co->sotype == SOCK_DGRAM) { err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) { @@ -163,7 +164,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) { + if (co->sotype == SOCK_DGRAM && co->family == AF_INET6) { err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) { @@ -172,7 +173,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_STREAM) { + if (co->sotype == SOCK_STREAM) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) { @@ -181,7 +182,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (reuseport_prog) { + if (co->reuseport) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) { @@ -190,24 +191,8 @@ static int make_server(int sotype, const char *ip, int port, } } - err = bind(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "bind", "failed\n")) { - log_err("failed to bind listen socket"); - goto fail; - } - - if (sotype == SOCK_STREAM) { - err = listen(fd, SOMAXCONN); - if (CHECK(err, "make_server", "listen")) { - log_err("failed to listen on port %d", port); - goto fail; - } - } - - return fd; fail: - close(fd); - return -1; + return err; } static int make_client(int sotype, const char *ip, int port) @@ -588,6 +573,17 @@ static void query_lookup_prog(struct test_sk_lookup *skel) static void run_lookup_prog(const struct test *t) { + int family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET; + struct cb_opts cb_opts = { + .family = family, + .sotype = t->sotype, + .reuseport = t->reuseport_prog, + }; + struct network_helper_opts srv_opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 }; int client_fd, reuse_conn_fd = -1; struct bpf_link *lookup_link; @@ -598,9 +594,8 @@ static void run_lookup_prog(const struct test *t) return; for (i = 0; i < ARRAY_SIZE(server_fds); i++) { - server_fds[i] = make_server(t->sotype, t->listen_at.ip, - t->listen_at.port, - t->reuseport_prog); + server_fds[i] = start_server_str(family, t->sotype, t->listen_at.ip, + t->listen_at.port, &srv_opts); if (server_fds[i] < 0 || attach_reuseport(server_fds[i], t->reuseport_prog)) goto close; @@ -625,9 +620,8 @@ static void run_lookup_prog(const struct test *t) socklen_t len = sizeof(addr); /* Add an extra socket to reuseport group */ - reuse_conn_fd = make_server(t->sotype, t->listen_at.ip, - t->listen_at.port, - t->reuseport_prog); + reuse_conn_fd = start_server_str(family, t->sotype, t->listen_at.ip, + t->listen_at.port, &srv_opts); if (reuse_conn_fd < 0 || attach_reuseport(reuse_conn_fd, t->reuseport_prog)) goto close; @@ -857,6 +851,16 @@ static void test_redirect_lookup(struct test_sk_lookup *skel) static void drop_on_lookup(const struct test *t) { + struct cb_opts cb_opts = { + .family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET, + .sotype = t->sotype, + .reuseport = t->reuseport_prog, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; @@ -866,8 +870,8 @@ static void drop_on_lookup(const struct test *t) if (!lookup_link) return; - server_fd = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, - t->reuseport_prog); + server_fd = start_server_str(cb_opts.family, t->sotype, t->listen_at.ip, + t->listen_at.port, &opts); if (server_fd < 0) goto detach; @@ -974,6 +978,25 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel) static void drop_on_reuseport(const struct test *t) { + struct cb_opts cb_opts1 = { + .family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET, + .sotype = t->sotype, + .reuseport = t->reuseport_prog, + }; + struct network_helper_opts opts1 = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts1, + }; + struct cb_opts cb_opts2 = { + .family = is_ipv6(t->connect_to.ip) ? AF_INET6 : AF_INET, + .sotype = t->sotype, + }; + struct network_helper_opts opts2 = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts2, + }; struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; @@ -983,8 +1006,8 @@ static void drop_on_reuseport(const struct test *t) if (!lookup_link) return; - server1 = make_server(t->sotype, t->listen_at.ip, t->listen_at.port, - t->reuseport_prog); + server1 = start_server_str(cb_opts1.family, t->sotype, t->listen_at.ip, + t->listen_at.port, &opts1); if (server1 < 0) goto detach; @@ -996,8 +1019,8 @@ static void drop_on_reuseport(const struct test *t) goto close_srv1; /* second server on destination address we should never reach */ - server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port, - NULL /* reuseport prog */); + server2 = start_server_str(cb_opts2.family, t->sotype, t->connect_to.ip, + t->connect_to.port, &opts2); if (server2 < 0) goto close_srv1; @@ -1082,6 +1105,15 @@ static void run_sk_assign(struct test_sk_lookup *skel, struct bpf_program *lookup_prog, const char *remote_ip, const char *local_ip) { + struct cb_opts cb_opts = { + .family = is_ipv6(local_ip) ? AF_INET6 : AF_INET, + .sotype = SOCK_STREAM, + }; + struct network_helper_opts srv_opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 }; struct bpf_sk_lookup ctx; __u64 server_cookie; @@ -1100,7 +1132,8 @@ static void run_sk_assign(struct test_sk_lookup *skel, ctx.protocol = IPPROTO_TCP; for (i = 0; i < ARRAY_SIZE(server_fds); i++) { - server_fds[i] = make_server(SOCK_STREAM, local_ip, 0, NULL); + server_fds[i] = start_server_str(cb_opts.family, SOCK_STREAM, + local_ip, 0, &srv_opts); if (server_fds[i] < 0) goto close_servers; @@ -1146,10 +1179,19 @@ static void run_sk_assign_v6(struct test_sk_lookup *skel, static void run_sk_assign_connected(struct test_sk_lookup *skel, int sotype) { + struct cb_opts cb_opts = { + .family = AF_INET, + .sotype = sotype, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; int err, client_fd, connected_fd, server_fd; struct bpf_link *lookup_link; - server_fd = make_server(sotype, EXT_IP4, EXT_PORT, NULL); + server_fd = start_server_str(AF_INET, sotype, EXT_IP4, EXT_PORT, &opts); if (server_fd < 0) return; @@ -1216,6 +1258,15 @@ struct test_multi_prog { static void run_multi_prog_lookup(const struct test_multi_prog *t) { + struct cb_opts cb_opts = { + .family = is_ipv6(t->listen_at.ip) ? AF_INET6 : AF_INET, + .sotype = SOCK_STREAM, + }; + struct network_helper_opts srv_opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; @@ -1240,8 +1291,8 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (!link2) goto out_unlink1; - server_fd = make_server(SOCK_STREAM, t->listen_at.ip, - t->listen_at.port, NULL); + server_fd = start_server_str(cb_opts.family, SOCK_STREAM, t->listen_at.ip, + t->listen_at.port, &srv_opts); if (server_fd < 0) goto out_unlink2; From patchwork Mon Jun 24 04:33: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: 13708894 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 AC8C7DDDC; Mon, 24 Jun 2024 04:35: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=1719203711; cv=none; b=Dlu6mOpwoIDqgmSQYum4SNyG7sSkMdE8AhhwDpP3wrGaocEJfEkn/OHHsjadqrFncMsKXXN2ozcCUTV8f6coMwZXnb1c3tfWLapVMyZqA56jXsjZoG+tUqGNMEBncHUfffmxLXxZimUrnEQDxMTitT49CNeuKfPjkRm5wYfyu7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203711; c=relaxed/simple; bh=uJEQg3aL1HWizXL1rTp0C2Wp9owEq4OCbwJu5gsFv8o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LRyUu9h65+8Kpbv5jNSbwSsvA6l9Vj/o6U7dhHWD1ij6DHWtO+L8MM/46RbIFH6dOvHlVBcQZuiFBOqhxQbaFQqrt6HAhpnrGOk6XBFV6IL+u+H+j0ROUH1Zbnz7RZf226oqHajFLebCtI5EO74ExqLGSz+LjJYjvlklIPVfSxc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=df+70FAz; 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="df+70FAz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 068F3C2BBFC; Mon, 24 Jun 2024 04:35:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203711; bh=uJEQg3aL1HWizXL1rTp0C2Wp9owEq4OCbwJu5gsFv8o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=df+70FAzRr7eVjxDw+85/iMtUbzf/fFHRpz0QSY+8Yp1AaKzxr4Q3G8Fcn54QcfLD ZE3K9/Te8HNtvo57bD8egT+J4FZPwAMkRs+GI43RmLM0C/8Bom4smHOf8laQJOf3S8 Y1jp3zomymLC+IWGKQvTMaRNih5iHH0NaNOrpXtdNDzRZjMHuh7E3EicQX7z6vaTtz WF4I0nUwQUSJilevUY4oSXHUmoI3A0cJwjYgZzRH4bAwxLLENssh8mr4gGWbsFRQTI 13Ko8v/+91Ens1yVXRNS/gnn+deXQCuBczoGbbl8Nz4pFZaf6NyVuCkrrTj9lXTZys s9+HBynDCFU/w== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 8/9] selftests/bpf: Use connect_to_fd in sk_lookup Date: Mon, 24 Jun 2024 12:33:55 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 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 using make_socket() and connect() in prog_tests/sk_lookup.c. This can simplify the code. Signed-off-by: Geliang Tang --- .../testing/selftests/bpf/prog_tests/sk_lookup.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 634c2ac0595e..e1c5b7d1fb3a 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -861,7 +861,6 @@ static void drop_on_lookup(const struct test *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts, }; - struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; ssize_t n; @@ -878,12 +877,11 @@ static void drop_on_lookup(const struct test *t) if (attach_reuseport(server_fd, t->reuseport_prog)) goto close_srv; - client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client_fd = connect_to_fd(server_fd, IO_TIMEOUT_SEC); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -997,7 +995,6 @@ static void drop_on_reuseport(const struct test *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts2, }; - struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; ssize_t n; @@ -1024,12 +1021,11 @@ static void drop_on_reuseport(const struct test *t) if (server2 < 0) goto close_srv1; - client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client = connect_to_fd(server2, IO_TIMEOUT_SEC); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1195,7 +1191,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, if (server_fd < 0) return; - connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = connect_to_fd(server_fd, IO_TIMEOUT_SEC); if (connected_fd < 0) goto out_close_server; @@ -1209,7 +1205,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, goto out_close_connected; /* Try to redirect TCP SYN / UDP packet to a connected socket */ - client_fd = make_client(sotype, EXT_IP4, EXT_PORT); + client_fd = connect_to_fd(server_fd, IO_TIMEOUT_SEC); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { From patchwork Mon Jun 24 04:33: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: 13708895 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 879AFD272; Mon, 24 Jun 2024 04:35:16 +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=1719203716; cv=none; b=bmLhNaFnm84yJT7YVHzCSn1bLWilG9mGXjhgQ6Oe1iNXmrf+0oPbWMuNwLr05Vzgfvmf1nj/WF6tWBJFXa++BeKvtdDRbKQIyU+cRx82+tDfcY2CSbmMYlIqRcm3HQ9A9SAazcQtzoPuE6L7aCrgKoTzVYtpbhvaCvH0EwSSXaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719203716; c=relaxed/simple; bh=NUcvhzvIWuSrzj8jF97gJxnz+3HAHz0QSs+9uEknrlQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kbFIqqcns0huv79BIIcFT/xlBffZ5y76YKZ/qv+8QrjzfJ408YG0odFGoV+qUFyHUXxGe5blt1sANoFcHjaWPjPGfe0YUM/ztCGihWrGyfzkyXZfZoByIqs8z3yNILanjhd7UwAnEqI1J/3Y7KejIkKmc7WTNHvef40OvjehN5c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MjW6cImh; 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="MjW6cImh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C50FAC32789; Mon, 24 Jun 2024 04:35:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719203716; bh=NUcvhzvIWuSrzj8jF97gJxnz+3HAHz0QSs+9uEknrlQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MjW6cImhrOikhmDQNuEWN5epW0uK8J7QiQL5O4sb9lkm4gGt6P97LONCOF0tpoJiO 3WgxmlUfotpKDVBdr9thKceve1Ov8VjGQAqdIYhqhKNaXAwzc9w8/iqgHS7t70srBj QUrv8mot4n0P3aYIbsoZCuQ20JqTxJ8n81ZHRo4ZUBAntFOXvZu+zW7RAtV1Dmv8rK 8n9JOaIS3ArMCTInBrvOp8LtFjr8ZkDpNHZgpwsd637t5Q1i4Zord/ePTl5j0NkvIw m4JPZtdLFMshjJyF51W6eTap+3lrHZE6PMhGnnHgxfLduYTxXoNPo0zdLccZHmj47z x8yva6NPuqQAg== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v4 9/9] selftests/bpf: Use connect_to_addr in sk_lookup Date: Mon, 24 Jun 2024 12:33:56 +0800 Message-ID: <26ad08128e97ad9a2647edc4714e182460a56394.1719203293.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 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 Use public network helpers make_sockaddr() and connect_to_addr() instead of using make_socket() + connect() or make_client(). Now local defined functions inetaddr_len(), make_socket() and make_client() all can be dropped. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 83 ++++--------------- 1 file changed, 16 insertions(+), 67 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index e1c5b7d1fb3a..5556796068f0 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,46 +108,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; } -static socklen_t inetaddr_len(const struct sockaddr_storage *addr) -{ - return (addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) : - addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0); -} - -static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr) -{ - struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; - int err, family, fd; - - family = is_ipv6(ip) ? AF_INET6 : AF_INET; - err = make_sockaddr(family, ip, port, addr, NULL); - if (CHECK(err, "make_address", "failed\n")) - return -1; - - fd = socket(addr->ss_family, sotype, 0); - if (CHECK(fd < 0, "socket", "failed\n")) { - log_err("failed to make socket"); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) { - log_err("failed to set SNDTIMEO"); - close(fd); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) { - log_err("failed to set RCVTIMEO"); - close(fd); - return -1; - } - - return fd; -} - static int setsockopts(int fd, void *opts) { struct cb_opts *co = (struct cb_opts *)opts; @@ -195,27 +155,6 @@ static int setsockopts(int fd, void *opts) return err; } -static int make_client(int sotype, const char *ip, int port) -{ - struct sockaddr_storage addr = {0}; - int err, fd; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; - - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { - log_err("failed to connect client socket"); - goto fail; - } - - return fd; -fail: - close(fd); - return -1; -} - static __u64 socket_cookie(int fd) { __u64 cookie; @@ -584,8 +523,13 @@ static void run_lookup_prog(const struct test *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts, }; + struct network_helper_opts cli_opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; int server_fds[] = { [0 ... MAX_SERVERS - 1] = -1 }; int client_fd, reuse_conn_fd = -1; + struct sockaddr_storage addr = {}; + socklen_t len = sizeof(addr); struct bpf_link *lookup_link; int i, err; @@ -616,9 +560,6 @@ static void run_lookup_prog(const struct test *t) * BPF socket lookup. */ if (t->reuseport_has_conns) { - struct sockaddr_storage addr = {}; - socklen_t len = sizeof(addr); - /* Add an extra socket to reuseport group */ reuse_conn_fd = start_server_str(family, t->sotype, t->listen_at.ip, t->listen_at.port, &srv_opts); @@ -635,7 +576,9 @@ static void run_lookup_prog(const struct test *t) goto close; } - client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port); + if (make_sockaddr(family, t->connect_to.ip, t->connect_to.port, &addr, &len)) + goto close; + client_fd = connect_to_addr(t->sotype, &addr, len, &cli_opts); if (client_fd < 0) goto close; @@ -1263,10 +1206,14 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) .post_socket_cb = setsockopts, .cb_opts = &cb_opts, }; + struct network_helper_opts cli_opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; + socklen_t len; map_fd = bpf_map__fd(t->run_map); @@ -1296,11 +1243,13 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server; - client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst); + if (make_sockaddr(AF_INET, EXT_IP4, EXT_PORT, &dst, &len)) + goto out_close_server; + client_fd = connect_to_addr(SOCK_STREAM, &dst, len, &cli_opts); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;