From: Feng zhou <zhoufeng.zf@bytedance.com>
To: martin.lau@linux.dev, daniel@iogearbox.net,
john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org,
eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev,
kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com,
jolsa@kernel.org, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com,
shuah@kernel.org, geliang@kernel.org, laoar.shao@gmail.com
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
yangzhenze@bytedance.com, wangdongdong.6@bytedance.com,
zhoufeng.zf@bytedance.com
Subject: [PATCH bpf-next v2 2/2] bpf, selftests: Add test case for cgroup skb to get net_cls classid helpers
Date: Wed, 18 Sep 2024 15:45:15 +0800 [thread overview]
Message-ID: <20240918074516.5697-3-zhoufeng.zf@bytedance.com> (raw)
In-Reply-To: <20240918074516.5697-1-zhoufeng.zf@bytedance.com>
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;
+
+ /* 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;
+}
--
2.30.2
next prev parent reply other threads:[~2024-09-18 7:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-18 7:45 [PATCH bpf-next v2 0/2] Cgroup skb add helper to get net_cls's classid Feng zhou
2024-09-18 7:45 ` [PATCH bpf-next v2 1/2] bpf: cg_skb add get classid helper Feng zhou
2024-09-18 7:45 ` Feng zhou [this message]
2024-10-01 1:58 ` [PATCH bpf-next v2 2/2] bpf, selftests: Add test case for cgroup skb to get net_cls classid helpers Martin KaFai Lau
2024-10-16 8:28 ` [External] " Feng Zhou
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240918074516.5697-3-zhoufeng.zf@bytedance.com \
--to=zhoufeng.zf@bytedance.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=geliang@kernel.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=laoar.shao@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=wangdongdong.6@bytedance.com \
--cc=yangzhenze@bytedance.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox