diff mbox series

[bpf-next,v2,2/2] bpf, selftests: Add test case for cgroup skb to get net_cls classid helpers

Message ID 20240918074516.5697-3-zhoufeng.zf@bytedance.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Cgroup skb add helper to get net_cls's classid | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 16 this patch: 16
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 18 of 18 maintainers
netdev/build_clang success Errors and warnings before: 17 this patch: 17
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 22 this patch: 22
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18

Commit Message

Feng Zhou Sept. 18, 2024, 7:45 a.m. UTC
From: Feng Zhou <zhoufeng.zf@bytedance.com>

This patch adds a test for cgroup skb to get classid.

Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
---
 .../bpf/prog_tests/cg_skb_get_classid.c       | 87 +++++++++++++++++++
 .../selftests/bpf/progs/cg_skb_get_classid.c  | 19 ++++
 2 files changed, 106 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
 create mode 100644 tools/testing/selftests/bpf/progs/cg_skb_get_classid.c

Comments

Martin KaFai Lau Oct. 1, 2024, 1:58 a.m. UTC | #1
On 9/18/24 12:45 AM, Feng zhou wrote:
> From: Feng Zhou <zhoufeng.zf@bytedance.com>
> 
> This patch adds a test for cgroup skb to get classid.
> 
> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
> ---
>   .../bpf/prog_tests/cg_skb_get_classid.c       | 87 +++++++++++++++++++
>   .../selftests/bpf/progs/cg_skb_get_classid.c  | 19 ++++
>   2 files changed, 106 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
>   create mode 100644 tools/testing/selftests/bpf/progs/cg_skb_get_classid.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
> new file mode 100644
> index 000000000000..13a5943c387d
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
> @@ -0,0 +1,87 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +/*
> + * Copyright 2024 Bytedance.
> + */
> +
> +#include <test_progs.h>
> +
> +#include "cg_skb_get_classid.skel.h"
> +
> +#include "cgroup_helpers.h"
> +#include "network_helpers.h"
> +
> +static int run_test(int cgroup_fd, int server_fd)
> +{
> +	struct cg_skb_get_classid *skel;
> +	int fd, err = 0;
> +
> +	skel = cg_skb_get_classid__open_and_load();
> +	if (!ASSERT_OK_PTR(skel, "skel_open"))
> +		return -1;
> +
> +	skel->links.cg_skb_classid =
> +		bpf_program__attach_cgroup(skel->progs.cg_skb_classid,
> +					   cgroup_fd);
> +	if (!ASSERT_OK_PTR(skel->links.cg_skb_classid, "prog_attach")) {
> +		err = -1;
> +		goto out;
> +	}
> +
> +	if (!ASSERT_OK(join_classid(), "join_classid")) {
> +		err = -1;
> +		goto out;
> +	}
> +
> +	errno = 0;
> +	fd = connect_to_fd_opts(server_fd, NULL);
> +	if (fd >= 0) {
> +		if (skel->bss->classid != getpid()) {
> +			log_err("Get unexpected classid");
> +			err = -1;
> +		}
> +
> +		close(fd);
> +	} else {
> +		log_err("Unexpected errno from connect to server");
> +		err = -1;
> +	}
> +out:
> +	cg_skb_get_classid__destroy(skel);
> +	return err;
> +}
> +
> +void test_cg_skb_get_classid(void)
> +{
> +	struct network_helper_opts opts = {};
> +	int server_fd, client_fd, cgroup_fd;
> +	static const int port = 60120;

Running a test with a specific port without netns could fail when test_progs is 
run in parallel (-j). e.g. cgroup_v1v2 is using the same port.

> +
> +	/* Step 1: Check base connectivity works without any BPF. */
> +	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
> +	if (!ASSERT_GE(server_fd, 0, "server_fd"))
> +		return;
> +	client_fd = connect_to_fd_opts(server_fd, &opts);
> +	if (!ASSERT_GE(client_fd, 0, "client_fd")) {
> +		close(server_fd);
> +		return;
> +	}
> +	close(client_fd);
> +	close(server_fd);

imo, this connection pre-test is unnecessary. I would remove it.

> +
> +	/* Step 2: Check BPF prog attached to cgroups. */
> +	cgroup_fd = test__join_cgroup("/cg_skb_get_classid");
> +	if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
> +		return;
> +	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
> +	if (!ASSERT_GE(server_fd, 0, "server_fd")) {
> +		close(cgroup_fd);
> +		return;
> +	}
> +	setup_classid_environment();
> +	set_classid();
> +	ASSERT_OK(run_test(cgroup_fd, server_fd), "cg_skb_get_classid");

Please run this test under a netns and without specifying a particular port. 
connect_to_fd_opts will figure out the port used in server_fd.

Patch 1 lgtm.

Please add a few words to the cover letter also.

pw-bot: cr
Feng Zhou Oct. 16, 2024, 8:28 a.m. UTC | #2
在 2024/10/1 09:58, Martin KaFai Lau 写道:
> On 9/18/24 12:45 AM, Feng zhou wrote:
>> From: Feng Zhou <zhoufeng.zf@bytedance.com>
>>
>> This patch adds a test for cgroup skb to get classid.
>>
>> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
>> ---
>>   .../bpf/prog_tests/cg_skb_get_classid.c       | 87 +++++++++++++++++++
>>   .../selftests/bpf/progs/cg_skb_get_classid.c  | 19 ++++
>>   2 files changed, 106 insertions(+)
>>   create mode 100644 
>> tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
>>   create mode 100644 
>> tools/testing/selftests/bpf/progs/cg_skb_get_classid.c
>>
>> diff --git 
>> a/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c 
>> b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
>> new file mode 100644
>> index 000000000000..13a5943c387d
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
>> @@ -0,0 +1,87 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +
>> +/*
>> + * Copyright 2024 Bytedance.
>> + */
>> +
>> +#include <test_progs.h>
>> +
>> +#include "cg_skb_get_classid.skel.h"
>> +
>> +#include "cgroup_helpers.h"
>> +#include "network_helpers.h"
>> +
>> +static int run_test(int cgroup_fd, int server_fd)
>> +{
>> +    struct cg_skb_get_classid *skel;
>> +    int fd, err = 0;
>> +
>> +    skel = cg_skb_get_classid__open_and_load();
>> +    if (!ASSERT_OK_PTR(skel, "skel_open"))
>> +        return -1;
>> +
>> +    skel->links.cg_skb_classid =
>> +        bpf_program__attach_cgroup(skel->progs.cg_skb_classid,
>> +                       cgroup_fd);
>> +    if (!ASSERT_OK_PTR(skel->links.cg_skb_classid, "prog_attach")) {
>> +        err = -1;
>> +        goto out;
>> +    }
>> +
>> +    if (!ASSERT_OK(join_classid(), "join_classid")) {
>> +        err = -1;
>> +        goto out;
>> +    }
>> +
>> +    errno = 0;
>> +    fd = connect_to_fd_opts(server_fd, NULL);
>> +    if (fd >= 0) {
>> +        if (skel->bss->classid != getpid()) {
>> +            log_err("Get unexpected classid");
>> +            err = -1;
>> +        }
>> +
>> +        close(fd);
>> +    } else {
>> +        log_err("Unexpected errno from connect to server");
>> +        err = -1;
>> +    }
>> +out:
>> +    cg_skb_get_classid__destroy(skel);
>> +    return err;
>> +}
>> +
>> +void test_cg_skb_get_classid(void)
>> +{
>> +    struct network_helper_opts opts = {};
>> +    int server_fd, client_fd, cgroup_fd;
>> +    static const int port = 60120;
> 
> Running a test with a specific port without netns could fail when 
> test_progs is run in parallel (-j). e.g. cgroup_v1v2 is using the same 
> port.
> 
>> +
>> +    /* Step 1: Check base connectivity works without any BPF. */
>> +    server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
>> +    if (!ASSERT_GE(server_fd, 0, "server_fd"))
>> +        return;
>> +    client_fd = connect_to_fd_opts(server_fd, &opts);
>> +    if (!ASSERT_GE(client_fd, 0, "client_fd")) {
>> +        close(server_fd);
>> +        return;
>> +    }
>> +    close(client_fd);
>> +    close(server_fd);
> 
> imo, this connection pre-test is unnecessary. I would remove it.
> 
>> +
>> +    /* Step 2: Check BPF prog attached to cgroups. */
>> +    cgroup_fd = test__join_cgroup("/cg_skb_get_classid");
>> +    if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
>> +        return;
>> +    server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
>> +    if (!ASSERT_GE(server_fd, 0, "server_fd")) {
>> +        close(cgroup_fd);
>> +        return;
>> +    }
>> +    setup_classid_environment();
>> +    set_classid();
>> +    ASSERT_OK(run_test(cgroup_fd, server_fd), "cg_skb_get_classid");
> 
> Please run this test under a netns and without specifying a particular 
> port. connect_to_fd_opts will figure out the port used in server_fd.
> 
> Patch 1 lgtm.
> 
> Please add a few words to the cover letter also.
> 
> pw-bot: cr

Sorry for taking so long to reply.

Will do, thanks.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
new file mode 100644
index 000000000000..13a5943c387d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c
@@ -0,0 +1,87 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright 2024 Bytedance.
+ */
+
+#include <test_progs.h>
+
+#include "cg_skb_get_classid.skel.h"
+
+#include "cgroup_helpers.h"
+#include "network_helpers.h"
+
+static int run_test(int cgroup_fd, int server_fd)
+{
+	struct cg_skb_get_classid *skel;
+	int fd, err = 0;
+
+	skel = cg_skb_get_classid__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return -1;
+
+	skel->links.cg_skb_classid =
+		bpf_program__attach_cgroup(skel->progs.cg_skb_classid,
+					   cgroup_fd);
+	if (!ASSERT_OK_PTR(skel->links.cg_skb_classid, "prog_attach")) {
+		err = -1;
+		goto out;
+	}
+
+	if (!ASSERT_OK(join_classid(), "join_classid")) {
+		err = -1;
+		goto out;
+	}
+
+	errno = 0;
+	fd = connect_to_fd_opts(server_fd, NULL);
+	if (fd >= 0) {
+		if (skel->bss->classid != getpid()) {
+			log_err("Get unexpected classid");
+			err = -1;
+		}
+
+		close(fd);
+	} else {
+		log_err("Unexpected errno from connect to server");
+		err = -1;
+	}
+out:
+	cg_skb_get_classid__destroy(skel);
+	return err;
+}
+
+void test_cg_skb_get_classid(void)
+{
+	struct network_helper_opts opts = {};
+	int server_fd, client_fd, cgroup_fd;
+	static const int port = 60120;
+
+	/* Step 1: Check base connectivity works without any BPF. */
+	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
+	if (!ASSERT_GE(server_fd, 0, "server_fd"))
+		return;
+	client_fd = connect_to_fd_opts(server_fd, &opts);
+	if (!ASSERT_GE(client_fd, 0, "client_fd")) {
+		close(server_fd);
+		return;
+	}
+	close(client_fd);
+	close(server_fd);
+
+	/* Step 2: Check BPF prog attached to cgroups. */
+	cgroup_fd = test__join_cgroup("/cg_skb_get_classid");
+	if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
+		return;
+	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0);
+	if (!ASSERT_GE(server_fd, 0, "server_fd")) {
+		close(cgroup_fd);
+		return;
+	}
+	setup_classid_environment();
+	set_classid();
+	ASSERT_OK(run_test(cgroup_fd, server_fd), "cg_skb_get_classid");
+	cleanup_classid_environment();
+	close(server_fd);
+	close(cgroup_fd);
+}
diff --git a/tools/testing/selftests/bpf/progs/cg_skb_get_classid.c b/tools/testing/selftests/bpf/progs/cg_skb_get_classid.c
new file mode 100644
index 000000000000..aef0265d24eb
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/cg_skb_get_classid.c
@@ -0,0 +1,19 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+
+/*
+ * Copyright 2024 Bytedance.
+ */
+
+#include <errno.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+__u32 classid = 0;
+
+SEC("cgroup_skb/egress")
+int cg_skb_classid(struct __sk_buff *ctx)
+{
+	classid = bpf_skb_cgroup_classid(ctx);
+
+	return 1;
+}