From patchwork Tue Jul 23 22:22:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814067 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 C86391DDD6; Tue, 23 Jul 2024 22:22:41 +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=1721773361; cv=none; b=gyLufQm+HQ0IJzneuznKB2boX4Ahhntrvt8NTmq9Wthbzos+zu0lo6J6v+gXNAJF9HgExxJY2KfqnN8aOXBspiFE9zKctfbvQdEn25yHcEinGDzO7hcACXQGMK6ilVttUijcxFcsttFVW9wtE3/Nmc6bYwW4nCPH+2GmwlgIxt0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773361; c=relaxed/simple; bh=ElpE2SCASgLAzFZeIC5RV+POqvIJfTF2T+hNolb6Xgw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KkCT31E2R9RihEWYAyNsJooiWJp5Ttg4rAkfVpeokHQkFKyulQSDouY6egGvhgYm3oZKQHByBIgfRlyy6uafg35Ag+VPbOc6Hbk2HdwqfT0c0gLmeA0C7iDgH0YwSUPgRMT/zs+ad/NjMAViMqmwkANdqLDmfm1NYgkGr0PvXyM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d8w3Zo4i; 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="d8w3Zo4i" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBADFC4AF0F; Tue, 23 Jul 2024 22:22:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773361; bh=ElpE2SCASgLAzFZeIC5RV+POqvIJfTF2T+hNolb6Xgw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d8w3Zo4ix6WFStCKHCkvG3BDjigxNuemxP31xEgB+sI/2AxoqlME8Ayrij4YV2d3X nwfF4BhSfg+/+77MCdlxf5tZqBAaSS5JlmgFf9i7l6x71x0lwduRGrXz8HT9Tv5tF3 79HAj7ZTZcG1YVa5PoDD4JSnmNh26a3wuUp3s1v3F//hex1JnMIcZDUUBejF77OO/W rF+dHLQV4nP6cMrJFH9HvVEMKGZrR4+6Kq8nsQ/+9ziYjEGuYEVjB18jRr3qEKBt9m hXTnUn1HohDECViTDhFw5KS9kHhyqaL6BcGQW9AVR9GUV+ZHoXu4omMZ++ijEq58ac bXRAcHE/yFKBg== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 01/15] selftests/bpf: Use start_server_str in skc_to_unix_sock Date: Wed, 24 Jul 2024 06:22:07 +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 network helper start_server_str() in skc_to_unix_sock test in skc_to_unix_sock.c to simplify the code. This is the first time to use start_server_str() for a AF_UNIX socket. The first "@" of "sun_path" needs to be skipped by make_sockaddr(), so path string "sock_path + 1" ("skc_to_unix_sock") is passed to the helper, not "sock_path" ("@skc_to_unix_sock"). Signed-off-by: Geliang Tang --- .../bpf/prog_tests/skc_to_unix_sock.c | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c index 3eefdfed1db9..4c23409a3072 100644 --- a/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c +++ b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c @@ -4,13 +4,16 @@ #include #include #include "test_skc_to_unix_sock.skel.h" +#include "network_helpers.h" static const char *sock_path = "@skc_to_unix_sock"; void test_skc_to_unix_sock(void) { + struct network_helper_opts opts = { + .backlog = 1, + }; struct test_skc_to_unix_sock *skel; - struct sockaddr_un sockaddr; int err, sockfd = 0; skel = test_skc_to_unix_sock__open(); @@ -28,21 +31,8 @@ void test_skc_to_unix_sock(void) goto cleanup; /* trigger unix_listen */ - sockfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (!ASSERT_GT(sockfd, 0, "socket failed")) - goto cleanup; - - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sun_family = AF_UNIX; - strncpy(sockaddr.sun_path, sock_path, strlen(sock_path)); - sockaddr.sun_path[0] = '\0'; - - err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); - if (!ASSERT_OK(err, "bind failed")) - goto cleanup; - - err = listen(sockfd, 1); - if (!ASSERT_OK(err, "listen failed")) + sockfd = start_server_str(AF_UNIX, SOCK_STREAM, sock_path + 1, 0, &opts); + if (!ASSERT_OK_FD(sockfd, "start_server_str")) goto cleanup; ASSERT_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed"); From patchwork Tue Jul 23 22:22:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814066 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 131EF148827; Tue, 23 Jul 2024 22:22:50 +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=1721773370; cv=none; b=CKEqqIL2AsO7JTgiC4nGX6OIhnS5QtDAAZ3ROZ1nQyERHntk6jLKP4MGJbf9Q2d1CXdARmn9z/U344+Ry9c5beMFQkJ8oqcA6xlcSZkU81Y/1AUTvBPrP4jL4Vj25YFio0uXHcKcTe1lmC+bOclldR7D4xvCsjFSZC5pZV8EFf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773370; c=relaxed/simple; bh=KqMjTfdH3u15gtevijB3kg0N1GugqZKCG34zP8wwTAc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JDOGbUQlVwsUwYTkTYiCi+kjp8MVaAOnYCgr7iobfYZdV2RZzvd+mProkICoShHzjsX24HVdf7+kW85xaBqsO/wSrm9xOBdaxRI/rgKbD/tC8u8eCpFFHavP5dtYYQJ9eJARynHIX6BZW6mOq0hnC9kqX+xecVtLEgPIFUmuuHI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aA2k9J+R; 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="aA2k9J+R" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 424FFC4AF11; Tue, 23 Jul 2024 22:22:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773369; bh=KqMjTfdH3u15gtevijB3kg0N1GugqZKCG34zP8wwTAc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aA2k9J+RBpPquH6t/tiNY7iEvKjFI6LrEHxmUwtCF8WKySK0DzMhGyjnut9kJllWX buO5qJcPgG5DGs8ejkgNrrAfol47Z4oIqk1RRK5EBsjk0GQwFIp2IQnyP39EP8x+Fw peVHpfoCDikqoxQcs75jdPlhDXBVj18UHoH9BvxWdy55op/do6fOxwtT5pFE8sSPVP zzJZIAK+GfuXM9DLqjCM25BdUiQcfqHhjjF4QFFN6aDnIYp8x24ESmWrBkLHIi+NR1 2/rOyHesbqLuBkmjzcPi7Ns8NYEx95VvlbHUr6xzhz6Af2CUyer8mfWnfHatPY8Gu3 Q2324fnWJbKnw== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 03/15] selftests/bpf: Use start_server_str in lwt_redirect Date: Wed, 24 Jul 2024 06:22:09 +0800 Message-ID: <44231acb3ed09db3bca33a4b5f959f0ec476efe3.1721771340.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 AF_PACKET can be passed to start_server_str() now. Use it to simplify the code in lwt_redirect. In this test, use sprintf() to save the "ifindex" value into addr_str string, then pass it to the helper. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/lwt_redirect.c | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c index 835a1d756c16..f4c54bb7e416 100644 --- a/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/lwt_redirect.c @@ -94,30 +94,19 @@ static int new_packet_sock(const char *ifname) { int err = 0; int ignore_outgoing = 1; + char addr_str[32]; int ifindex = -1; int s = -1; - s = socket(AF_PACKET, SOCK_RAW, 0); - if (!ASSERT_GE(s, 0, "socket(AF_PACKET)")) - return -1; - ifindex = if_nametoindex(ifname); - if (!ASSERT_GE(ifindex, 0, "if_nametoindex")) { - close(s); + if (!ASSERT_GE(ifindex, 0, "if_nametoindex")) return -1; - } - struct sockaddr_ll addr = { - .sll_family = AF_PACKET, - .sll_protocol = htons(ETH_P_IP), - .sll_ifindex = ifindex, - }; + sprintf(addr_str, "%d", ifindex); - err = bind(s, (struct sockaddr *)&addr, sizeof(addr)); - if (!ASSERT_OK(err, "bind(AF_PACKET)")) { - close(s); + s = start_server_str(AF_PACKET, SOCK_RAW, addr_str, 0, NULL); + if (!ASSERT_OK_FD(s, "start_server_str")) return -1; - } /* Use packet socket to capture only the ingress, so we can distinguish * the case where a regression that actually redirects the packet to From patchwork Tue Jul 23 22:22:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814065 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 BF29F14884F; Tue, 23 Jul 2024 22:22:58 +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=1721773378; cv=none; b=QRzKjpOkydrNmO/JAGFVa2lOud+XzvG7Jp8viC1kHyWEN0mLd5AEZ3m55a3x/xkDgPGEe2yuLQl7jpAn8rzJdko0nraVb1MUGGN/0MaZB30QKzN+iaRdtw7JJ+7R8Ry8RajS+TmBESy4T+A9cvI7VWKzy2u13aS2DMcRLiRySc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773378; c=relaxed/simple; bh=vMawzUa6obfX+VBE9Ykam0WOLhjlYMSTbs5HX0CvXPs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y9+2HgOceyn59qX+8KF+oOJ7/uR2YsuzVY4nllrtxy4FJ0iBPiF4nWjFMRuPtbu9NqLQIGgmCjRC5t3dWKRCaIPVTTZUpK3nxTvjPz6bm8q8TwG/aFiGxMvl5grlX0cDw0PUcorxfTIHvsTOs0QPeEdDq2vklOkp9hjDZZdewOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WPSnLxjP; 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="WPSnLxjP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB26EC4AF09; Tue, 23 Jul 2024 22:22:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773378; bh=vMawzUa6obfX+VBE9Ykam0WOLhjlYMSTbs5HX0CvXPs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WPSnLxjPk5JyEmeMZwyF5/WZBbvqpLZmR6awUviXvVX02VnRG7QwIF2h0k1vV8tLW sRU4RPgLd4ik3vQ7YNruKqbmnvaN08C+a8JGe3QxLfNDW28fRiOdGMK4+Dn11PFlhd 1Gh5AeW+jCe6G2DVPvCBy+5+RIxz/y7uz5sGGU3bPTDb74gY34oRqSKYrPkyq0uYKd 1cTcsYPiyRX2KfQDrsXh9pIcWUD9pm4sJCgNqE5b53vDTC6SowYHenU+htjzrn0pBD BddBQVVaE25oqIGiF1p6XAQrGHxdJRIMGHQ3Le2T2811jtgd0iUlfRHj1tyEHMKz6Z Anda4sQ7ixQtA== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 05/15] selftests/bpf: Add send_to_addr_str helper Date: Wed, 24 Jul 2024 06:22:11 +0800 Message-ID: <7a909bf0164987f1b92f505a9dfb85509c57bf9d.1721771340.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 Similar to network helper connect_to_addr_str(), this patch adds a new helper send_to_addr_str(). It uses make_sockaddr() to make a sockaddr_storage type address from the "family", "addr_str" and "port" parameters and passes it to send_to_addr(). Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 16 ++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 311c3a559414..a6c158718267 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -229,6 +229,22 @@ static int send_to_addr(int type, const void *data, size_t datalen, int flags, return fd; } +int send_to_addr_str(int family, int type, const char *addr_str, __u16 port, + const void *data, size_t datalen, int flags, + const struct network_helper_opts *opts) +{ + struct sockaddr_storage addr; + socklen_t addrlen; + + if (!opts) + opts = &default_opts; + + if (make_sockaddr(family, addr_str, port, &addr, &addrlen)) + return -1; + + return send_to_addr(type, data, datalen, flags, &addr, addrlen, opts); +} + int send_to_fd_opts(int server_fd, const void *data, size_t datalen, int flags, const struct network_helper_opts *opts) { diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 19210a234bc1..ebed1fb1bd49 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -76,6 +76,9 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); int send_to_fd_opts(int server_fd, const void *data, size_t datalen, int flags, const struct network_helper_opts *opts); +int send_to_addr_str(int family, int type, const char *addr_str, __u16 port, + const void *data, size_t datalen, int flags, + const struct network_helper_opts *opts); int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int timeout_ms); int make_sockaddr(int family, const char *addr_str, __u16 port, From patchwork Tue Jul 23 22:22:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814064 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 0B9431482E2; Tue, 23 Jul 2024 22:23:07 +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=1721773387; cv=none; b=GRkzfcMVH6B23VaPtWumkgKkBR3qxB1MsUa0uvvFdzZgRVlFj+CD4jc10g6u7F626sPaEdQosCEliYJ9t+bIPVG6tQ/zdLUWgvSltd1JJHy4Pbyaedd1d1XPZeOoxNzxaN5YG70QOR2qpzNCvAOIn+ERfkB/lKHlYsOO+npiALU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773387; c=relaxed/simple; bh=YBqZd6bqszPv1L2BqgxtBBlG+WaL56Es26tyx6TtL3s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TgKtJ+1YQJzAuK5uE99DBW6KiFAEfP++juwMkRCdFR626yeQciwqDb0GQrMSXg94hU/Lf2sRU+gr1EfkFmecSnRr+X3PcqajwHnAaigXiyyn1P7dY34D/fpmcQuyrmWWtelvbIUUK1/494fQirbO+0J7G6z422W3KjoDrlwHJe4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eQk+CzTM; 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="eQk+CzTM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4113DC4AF15; Tue, 23 Jul 2024 22:23:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773386; bh=YBqZd6bqszPv1L2BqgxtBBlG+WaL56Es26tyx6TtL3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eQk+CzTM7Qr522xqMY9N7CAb4PyK2usQye6nLJMGuB4oq/koKJ4h9cQiirCeuw9O4 bYksXqn74TjF5oGYYYD92Ts8hKttp7/i87VP+/uOM4HDxq2RV380atq1xUmyGKHTNk tJZlPWngT97gYfOD7JaInXDB4dx5m7oQVRWqMF7xxUM8iNXD0JpVtZM++34Y77OpbS CrNKOcoLAOImAA423GvFi1X4pQ5DMu/VmoAD26ZZoTgDxBk3W+X71hqKF8oC7642yt txL0scyPEWcc5c88jU5qCe/m69WX49OfORb8m8GDMTT/y47vtebDZu2o4OQeOpzV+U CzPmAmgB019wg== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 07/15] selftests/bpf: AF_ALG support for make_sockaddr Date: Wed, 24 Jul 2024 06:22:13 +0800 Message-ID: <50e333e776a5b33393bdf51d24053bf091784402.1721771340.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 adds AF_ALG support for make_sockaddr(), uses sscanf() to parse the given addr_str string to "salg_type" and "salg_name" of struct sockaddr_alg. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index a6c158718267..8f39adbc2c16 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "bpf_util.h" #include "network_helpers.h" @@ -468,6 +469,20 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, if (len) *len = sizeof(*sll); return 0; + } else if (family == AF_ALG) { + struct sockaddr_alg *salg = (void *)addr; + + memset(addr, 0, sizeof(*salg)); + salg->salg_family = family; + if (addr_str && + sscanf(addr_str, "%s %s", + salg->salg_type, salg->salg_name) == -1) { + log_err("AF_ALG sscanf(%s)", addr_str); + return -1; + } + if (len) + *len = sizeof(*salg); + return 0; } return -1; } From patchwork Tue Jul 23 22:22:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814063 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 8322D1DDD6; Tue, 23 Jul 2024 22:23:15 +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=1721773395; cv=none; b=uDeiW6/75mlwShht3AqnVkzqkAeyyikHdiLT5SJ4UQVLYj+Za/4vOA7Xc66bP7xT/qiAu+NznyFFBcdP3Ook6g7h9U5gaerWFjgqvsfbOCw1fthn8y2ubh18TfHFkTT7iyFYAKpW+z6W/NUvjqpQRASC3EXpndi1X7MiqbKNgiw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773395; c=relaxed/simple; bh=xQr7aSrfooBfu5skylei+sIq3p9y31kRyAxMfGrXLWQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=goahHxboEDdwxfEBkj5bzKHjec2oAEZTr8GWsMy2evmrFSma8KtQjoQoQfa/i4rk4K0Y/8ywuTsdDQqOcuIAEkiDl6i5sAWy3JEhHG0sJbjx2+u7Hu7f74SYXAMdG/UrK6fqEe6qkBOhgiyFhtW9T5Pkci7qXuXZendoHMHlhK0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L6DAk+Yj; 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="L6DAk+Yj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BAFE3C4AF10; Tue, 23 Jul 2024 22:23:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773395; bh=xQr7aSrfooBfu5skylei+sIq3p9y31kRyAxMfGrXLWQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L6DAk+YjXuhKF2RBaZQVesMJSg2HZw5XYB9KnqU2DUZUqdjUW3U6G/40NYJ/St8Lv T7exNt7zD5fE7THzdu23M5tMgifA4vJmiwoybFgNeeinYRfp7Z6YSUyAIYCc8sfbZ9 u2qC2HR1pUnVpGi8Hsi8qjjjnPT8XD1u20tUHFx9UXKtR5gpZzyVjduSJmEqVYtxiJ RqUOCpIdDOdTDgkgEOZufOW01F/xrbzE6KAmhKKsAd4yaybmQI+1Bl06XF3/7H+n+b mgk/d9zUOmqogS2WUv9hibRtjL83rkb2KiJ1FZHN/N3aMU1rscipVbVsaghFT801ra o2IbNybxlW4ww== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 09/15] selftests/bpf: Use start_server_str in crypto_sanity Date: Wed, 24 Jul 2024 06:22:15 +0800 Message-ID: <11d7a17f7245e17d29c1001461af6260b62ea610.1721771340.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 start_server_str() to start a AF_ALG server in test crypto_sanity, since no listen is needed in this test, nolisten flag of network_helper_opts is set. Signed-off-by: Geliang Tang --- .../testing/selftests/bpf/prog_tests/crypto_sanity.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c b/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c index b1a3a49a822a..9b7a26c79123 100644 --- a/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c +++ b/tools/testing/selftests/bpf/prog_tests/crypto_sanity.c @@ -20,16 +20,12 @@ static int opfd = -1, tfmfd = -1; static const char algo[] = "ecb(aes)"; static int init_afalg(void) { - struct sockaddr_alg sa = { - .salg_family = AF_ALG, - .salg_type = "skcipher", - .salg_name = "ecb(aes)" + struct network_helper_opts opts = { + .nolisten = true, }; - tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0); - if (tfmfd == -1) - return errno; - if (bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) + tfmfd = start_server_str(AF_ALG, SOCK_SEQPACKET, "skcipher ecb(aes)", 0, &opts); + if (!ASSERT_OK_FD(tfmfd, "start_server_addr")) return errno; if (setsockopt(tfmfd, SOL_ALG, ALG_SET_KEY, crypto_key, 16) == -1) return errno; From patchwork Tue Jul 23 22:22:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814062 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 248A31487EA; Tue, 23 Jul 2024 22:23: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=1721773404; cv=none; b=QLVu6dMXXtGKrTmQL5kQwlcOAjkw4o8LnUxEzZq07loNmA6APCxrCHbid/mInZNvXz1g1ovwZjFbw1kc/myG2tj28Iufm5gghrppiHtqT9NAFvxIZQ4XWi/LyN9RKul0GgC2pvGbigO+biNmEICToqWKEQ4hgzhGvk95jfyxyoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773404; c=relaxed/simple; bh=ME+tSI8PBgw6F7c3OiWB1NRmntbKUJH1Ek+eeeScfqI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b8JFndErtPbUyWIuEVBDjIPGiBHaANKDTSh6okyCytihSv73SUd4COVPfxhfGWswxRW7m2mdkckYGnHGYUoTzExY5vKeSVp8ynhHfEXTbFVElQllDYMz8rKnMNwF1om2qymWWXDHOJ8mBmAXmamYHcY95Tz17TArKfwcZPc0oKQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WdPm2klD; 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="WdPm2klD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59A5FC4AF09; Tue, 23 Jul 2024 22:23:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773404; bh=ME+tSI8PBgw6F7c3OiWB1NRmntbKUJH1Ek+eeeScfqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WdPm2klDvcfsdO0G5UKGuhYAkHxrRaapwMfpAlhKxfl0Uy3Htgav78ANecohHQvDY oJdTNAIsY0tgbt3b2NxYEdLhyycCZ+rtXlFSXqbucZMJiQAZ8Nmlksa6S0cIuJjPZh fEqPNPlcJ4q5AjDVeEEEt3mzClRMLlxXksSn9VbNS5ltI9f+7H3yDcyfoYxuONR7C6 iINXm9lHwTRNBe4Nlsl5qM19KzhwkwYzE6TnqiRsFSeLFALSd9v/4RZy/PBQ5Nstva zvfK/PGqZe8pPAw5Yf01FAzUQvp4IoYDjdf1ernmv/xuGyQ8FmiNvpFrwPpLWGkClw M+kiEkEDecm5w== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 11/15] selftests/bpf: Add loopback_addr_str helper Date: Wed, 24 Jul 2024 06:22:17 +0800 Message-ID: <92d84e92e60304b26e7bae9dfc65bb3548d4f3a2.1721771340.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 adds a new helper loopback_addr_str() to get the loopback address string according to the given different protocol family. Also use this new helper in start_servers() in migrate_reuseport.c. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.h | 15 +++++++++++++++ .../selftests/bpf/prog_tests/migrate_reuseport.c | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 16f266c53b61..893f07c677da 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -55,6 +55,21 @@ struct ipv6_packet { } __packed; extern struct ipv6_packet pkt_v6; +static inline char *loopback_addr_str(int family) +{ + switch (family) { + case AF_INET: + return "127.0.0.1"; + case AF_INET6: + return "::1"; + case AF_VSOCK: + return "1"; /* VMADDR_CID_LOCAL */ + default: + fprintf(stderr, "unsupported address family %d", family); + return NULL; + } +} + int settimeo(int fd, int timeout_ms); int start_server_str(int family, int type, const char *addr_str, __u16 port, const struct network_helper_opts *opts); diff --git a/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c b/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c index 653b0a20fab9..7c31c7fdaecb 100644 --- a/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c +++ b/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c @@ -221,7 +221,7 @@ static int start_servers(struct migrate_reuseport_test_case *test_case, prog_fd = bpf_program__fd(skel->progs.migrate_reuseport); make_sockaddr(test_case->family, - test_case->family == AF_INET ? "127.0.0.1" : "::1", 0, + loopback_addr_str(test_case->family), 0, &test_case->addr, &test_case->addrlen); for (i = 0; i < NR_SERVERS; i++) { From patchwork Tue Jul 23 22:22:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814061 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 C515D1422AB; Tue, 23 Jul 2024 22:23:32 +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=1721773412; cv=none; b=lwYpaSC5Wu+WitBD4eDPhIUwMQ0SFJA95hQ/2yHhpNxoxLjom9pjuQ2cG+snHLvcOtxGQwIe3KHnn9iYTkT5ONWwjJVEV6P70HSxYaHzrHDjq6wH5WXvw2mDAFGljw//KOYJ3hZfaVNYftmuW1QajGltr5QyoMGHuxCHmVQWlgg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773412; c=relaxed/simple; bh=SDq3d/iEx9vI0ULJ3CqdeMcebgGgRTUsqukdR9Cpzt0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Gkvjj482ayvwGMJJat4P/8be87AM0JAyRSoliDppt6yL3VqxLSpynIKR1TE03kGH0uJQhl3zLwOX4F4kbO4V67B78APqg8C+UUg/C2HHTDdw8BhdJhzF0ZliMoeeeRBE4mA43w9VJ3yGasgkyl6+RXDOnVgZnjYLbyS/JLljWiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=beyXCFmY; 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="beyXCFmY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D36F3C4AF11; Tue, 23 Jul 2024 22:23:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773412; bh=SDq3d/iEx9vI0ULJ3CqdeMcebgGgRTUsqukdR9Cpzt0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=beyXCFmYzzEJqASQWEOUEMS7X2fzUJfTjHTs865sGKFa3TAegi8BdhGYhQfZkW2c1 10osi/NmWRPS0Q1V0nJvHyGax8TKsNMh8EkzOwg5uWxQMW6j3JBKFD7ProQF0b/lm/ btpiEX64j+PVa3FgxNYBXW2zp1F9/FzVSQw62DozXZuWO7rbFmeQXunhJUvrndslDL XL2ejo/uWhnnDz4qj+6cBs/lEPyC4jAFybR7Kl+XK7P5YI+RTtFMR4F/k5UPhLEQHU 6HMiRFpIbw2GlH+yh0Mi2qUUTyaBdGJQos8Y3WhMAk96Br2QH1F9tX1HWtyyr576ml WYJi4FUbXRY3g== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 13/15] selftests/bpf: Check listen support for start_server_addr Date: Wed, 24 Jul 2024 06:22:19 +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 No only SOCK_STREAM type supports listen for connections on a socket, SOCK_SEQPACKET used in sockmap_listen.c supports it too. This patch adds a new helper listen_support() to check whether a given "type" supports listen for connections on a socket or not. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 8b06b0bf66ae..6b6734b893e4 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -82,6 +82,17 @@ int settimeo(int fd, int timeout_ms) #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; }) +static bool listen_support(int type) +{ + switch (type) { + case SOCK_STREAM: + case SOCK_SEQPACKET: + return true; + default: + return false; + } +} + int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen, const struct network_helper_opts *opts) { @@ -110,7 +121,7 @@ int start_server_addr(int type, const struct sockaddr_storage *addr, socklen_t a goto error_close; } - if (!opts->nolisten && type == SOCK_STREAM) { + if (!opts->nolisten && listen_support(type)) { if (listen(fd, opts->backlog ? MAX(opts->backlog, 0) : 1) < 0) { log_err("Failed to listed on socket"); goto error_close; From patchwork Tue Jul 23 22:22:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 814060 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 73BF9143C6B; Tue, 23 Jul 2024 22:23:41 +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=1721773421; cv=none; b=ZEXTmchjE0yBoW8TDr9TheeDYjovurGKPKXf72E592IXrQGTIqQGgPRiZs3s426b26OqlVKOFZ7xZSWRvBUPGQdCPdQrN0jYtubVGPQaGgPjabTEuoPM+TmSg8AqnPY6nYTrLruOtOnjmIaJ1H17dXlVoLP1lwdhIBiff5QZGYI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721773421; c=relaxed/simple; bh=SHYjF4mo17ALM2nROj6zICCd1uAMwHnpyC0sTX26ffQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RPZiQl+d1n2uJ8v3ERsA4YPkM+XvxCV/yt66WC6ci0v7e260qFJwsCZ6nO0qJNt/OOZYlH3wwJLDxcABoQ/NwJui/ebV7g1upHUeZXzJNaUEkcapd9f/m+dcgCDYZFoZj03gVD1uXHbuAU/n7XKKhScRf/ne8LksMOdcvyIPbi4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c+/CRT+w; 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="c+/CRT+w" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5956BC4AF0A; Tue, 23 Jul 2024 22:23:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721773421; bh=SHYjF4mo17ALM2nROj6zICCd1uAMwHnpyC0sTX26ffQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c+/CRT+wOpVLTY3i81U+anoR/qpAysQBi9B6bWo+oaVKpRO6+ogl/+VPzLWsFTV3+ NlqcM7Jra4HTpWU+epoYQqD67LwvdDYAI4STtxisstomYnbRyBACa+0Qkju+4QGoA/ yGPmcIEOTe1ta7QffPVmfKZlb5NVQw+gm2WUa3pb3XuI/wgKnqPfMOEdvzPXDpwY0g Fbk2WzExYE0l5+960XzR9fmf4MiNW7ZPo3aoQdxbwHu8JwHweb807P6KOUfktMX4Rb rgHf3k/EKvxeYIU6+TMEPTty4nfJx7ys2k39gWyb/yDvOq98WkQx3uLGuQhAnKtT0Y fY6+ySy1Zrc2w== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 15/15] selftests/bpf: Use start_server_str in sockmap_helpers Date: Wed, 24 Jul 2024 06:22:21 +0800 Message-ID: <2de2cd7a23bcd02d4711ec66d3b3a77c5632d1e7.1721771340.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 Now both SOCK_SEQPACKET and SOCK_NONBLOCK can be passed as "type" parameter into start_server_str(), use this helper in socket_loopback_reuseport() in sockmap_helpers.h to simplify the code. Signed-off-by: Geliang Tang --- .../bpf/prog_tests/sockmap_helpers.h | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h index 0fe095497f34..1e681e71e9ad 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h @@ -310,8 +310,14 @@ static inline int create_socket_pairs(int s, int family, int sotype, return err; } -static inline int enable_reuseport(int s, int progfd) +struct cb_opts { + int progfd; +}; + +static int enable_reuseport(int s, void *opts) { + struct cb_opts *co = (struct cb_opts *)opts; + int progfd = co->progfd; int err, one = 1; err = xsetsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); @@ -327,34 +333,19 @@ static inline int enable_reuseport(int s, int progfd) static inline int socket_loopback_reuseport(int family, int sotype, int progfd) { - struct sockaddr_storage addr; - socklen_t len = 0; - int err, s; - - init_addr_loopback(family, &addr, &len); - - s = xsocket(family, sotype, 0); - if (s == -1) - return -1; - - if (progfd >= 0) - enable_reuseport(s, progfd); - - err = xbind(s, sockaddr(&addr), len); - if (err) - goto close; - - if (sotype & SOCK_DGRAM) - return s; - - err = xlisten(s, SOMAXCONN); - if (err) - goto close; + struct cb_opts cb_opts = { + .progfd = progfd, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + }; + + if (progfd >= 0) { + opts.post_socket_cb = enable_reuseport; + opts.cb_opts = &cb_opts; + } - return s; -close: - xclose(s); - return -1; + return start_server_str(family, sotype, loopback_addr_str(family), 0, &opts); } static inline int socket_loopback(int family, int sotype)