From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Toke Høiland-Jørgensen" <toke@redhat.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <kafai@fb.com>,
"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
"John Fastabend" <john.fastabend@gmail.com>,
"KP Singh" <kpsingh@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
"Jakub Kicinski" <kuba@kernel.org>,
"Jesper Dangaard Brouer" <brouer@redhat.com>,
netdev@vger.kernel.org
Subject: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API
Date: Wed, 21 Apr 2021 01:07:40 +0530 [thread overview]
Message-ID: <20210420193740.124285-4-memxor@gmail.com> (raw)
In-Reply-To: <20210420193740.124285-1-memxor@gmail.com>
This adds some basic tests for the low level bpf_tc_* API.
Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
.../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++
.../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++
2 files changed, 181 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c
create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c
diff --git a/tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c b/tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c
new file mode 100644
index 000000000000..563a3944553c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c
@@ -0,0 +1,169 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <linux/err.h>
+#include <linux/limits.h>
+#include <bpf/libbpf.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <test_progs.h>
+#include <linux/if_ether.h>
+
+#define LO_IFINDEX 1
+
+static int test_tc_internal(int fd, __u32 parent_id)
+{
+ DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10,
+ .class_id = TC_H_MAKE(1UL << 16, 1));
+ struct bpf_tc_attach_id id = {};
+ struct bpf_tc_info info = {};
+ int ret;
+
+ ret = bpf_tc_attach(fd, LO_IFINDEX, parent_id, &opts, &id);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_attach"))
+ return ret;
+
+ ret = bpf_tc_get_info(LO_IFINDEX, parent_id, &id, &info);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info"))
+ goto end;
+
+ if (!ASSERT_EQ(info.id.handle, id.handle, "handle mismatch") ||
+ !ASSERT_EQ(info.id.priority, id.priority, "priority mismatch") ||
+ !ASSERT_EQ(info.id.handle, 1, "handle incorrect") ||
+ !ASSERT_EQ(info.chain_index, 0, "chain_index incorrect") ||
+ !ASSERT_EQ(info.id.priority, 10, "priority incorrect") ||
+ !ASSERT_EQ(info.class_id, TC_H_MAKE(1UL << 16, 1),
+ "class_id incorrect") ||
+ !ASSERT_EQ(info.protocol, ETH_P_ALL, "protocol incorrect"))
+ goto end;
+
+ opts.replace = true;
+ ret = bpf_tc_attach(fd, LO_IFINDEX, parent_id, &opts, &id);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_attach in replace mode"))
+ return ret;
+
+ /* Demonstrate changing attributes */
+ opts.class_id = TC_H_MAKE(1UL << 16, 2);
+
+ ret = bpf_tc_attach(fd, LO_IFINDEX, parent_id, &opts, &id);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc attach in replace mode"))
+ goto end;
+
+ ret = bpf_tc_get_info(LO_IFINDEX, parent_id, &id, &info);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info"))
+ goto end;
+
+ if (!ASSERT_EQ(info.class_id, TC_H_MAKE(1UL << 16, 2),
+ "class_id incorrect after replace"))
+ goto end;
+ if (!ASSERT_EQ(info.bpf_flags & TCA_BPF_FLAG_ACT_DIRECT, 1,
+ "direct action mode not set"))
+ goto end;
+
+end:
+ ret = bpf_tc_detach(LO_IFINDEX, parent_id, &id);
+ ASSERT_EQ(ret, 0, "detach failed");
+ return ret;
+}
+
+int test_tc_info(int fd)
+{
+ DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10,
+ .class_id = TC_H_MAKE(1UL << 16, 1));
+ struct bpf_tc_attach_id id = {}, old;
+ struct bpf_tc_info info = {};
+ int ret;
+
+ ret = bpf_tc_attach(fd, LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &opts, &id);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_attach"))
+ return ret;
+ old = id;
+
+ ret = bpf_tc_get_info(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &id, &info);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info"))
+ goto end_old;
+
+ if (!ASSERT_EQ(info.id.handle, id.handle, "handle mismatch") ||
+ !ASSERT_EQ(info.id.priority, id.priority, "priority mismatch") ||
+ !ASSERT_EQ(info.id.handle, 1, "handle incorrect") ||
+ !ASSERT_EQ(info.chain_index, 0, "chain_index incorrect") ||
+ !ASSERT_EQ(info.id.priority, 10, "priority incorrect") ||
+ !ASSERT_EQ(info.class_id, TC_H_MAKE(1UL << 16, 1),
+ "class_id incorrect") ||
+ !ASSERT_EQ(info.protocol, ETH_P_ALL, "protocol incorrect"))
+ goto end_old;
+
+ /* choose a priority */
+ opts.priority = 0;
+ ret = bpf_tc_attach(fd, LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &opts, &id);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_attach"))
+ goto end_old;
+
+ ret = bpf_tc_get_info(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &id, &info);
+ if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info"))
+ goto end;
+
+ if (!ASSERT_NEQ(id.priority, old.priority, "filter priority mismatch"))
+ goto end;
+ if (!ASSERT_EQ(info.id.priority, id.priority, "priority mismatch"))
+ goto end;
+
+end:
+ ret = bpf_tc_detach(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &id);
+ ASSERT_EQ(ret, 0, "detach failed");
+end_old:
+ ret = bpf_tc_detach(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &old);
+ ASSERT_EQ(ret, 0, "detach failed");
+ return ret;
+}
+
+void test_test_tc_bpf(void)
+{
+ const char *file = "./test_tc_bpf_kern.o";
+ struct bpf_program *clsp;
+ struct bpf_object *obj;
+ int cls_fd, ret;
+
+ obj = bpf_object__open(file);
+ if (!ASSERT_OK_PTR(obj, "bpf_object__open"))
+ return;
+
+ clsp = bpf_object__find_program_by_title(obj, "classifier");
+ if (!ASSERT_OK_PTR(clsp, "bpf_object__find_program_by_title"))
+ goto end;
+
+ ret = bpf_object__load(obj);
+ if (!ASSERT_EQ(ret, 0, "bpf_object__load"))
+ goto end;
+
+ cls_fd = bpf_program__fd(clsp);
+
+ system("tc qdisc del dev lo clsact");
+
+ ret = test_tc_internal(cls_fd, BPF_TC_CLSACT_INGRESS);
+ if (!ASSERT_EQ(ret, 0, "test_tc_internal INGRESS"))
+ goto end;
+
+ if (!ASSERT_EQ(system("tc qdisc del dev lo clsact"), 0,
+ "clsact qdisc delete failed"))
+ goto end;
+
+ ret = test_tc_info(cls_fd);
+ if (!ASSERT_EQ(ret, 0, "test_tc_info"))
+ goto end;
+
+ if (!ASSERT_EQ(system("tc qdisc del dev lo clsact"), 0,
+ "clsact qdisc delete failed"))
+ goto end;
+
+ ret = test_tc_internal(cls_fd, BPF_TC_CLSACT_EGRESS);
+ if (!ASSERT_EQ(ret, 0, "test_tc_internal EGRESS"))
+ goto end;
+
+ ASSERT_EQ(system("tc qdisc del dev lo clsact"), 0,
+ "clsact qdisc delete failed");
+
+end:
+ bpf_object__close(obj);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c b/tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c
new file mode 100644
index 000000000000..18a3a7ed924a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* Dummy prog to test TC-BPF API */
+
+SEC("classifier")
+int cls(struct __sk_buff *skb)
+{
+ return 0;
+}
--
2.30.2
next prev parent reply other threads:[~2021-04-20 19:37 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-20 19:37 [PATCH bpf-next v3 0/3] Add TC-BPF API Kumar Kartikeya Dwivedi
2021-04-20 19:37 ` [PATCH bpf-next v3 1/3] libbpf: add helpers for preparing netlink attributes Kumar Kartikeya Dwivedi
2021-04-21 6:37 ` Yonghong Song
2021-04-20 19:37 ` [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API Kumar Kartikeya Dwivedi
2021-04-21 6:58 ` Yonghong Song
2021-04-21 17:06 ` Kumar Kartikeya Dwivedi
2021-04-22 1:56 ` Yonghong Song
2021-04-21 18:19 ` Andrii Nakryiko
2021-04-21 19:48 ` Toke Høiland-Jørgensen
2021-04-21 23:14 ` Daniel Borkmann
2021-04-22 9:08 ` Toke Høiland-Jørgensen
2021-04-22 11:51 ` Daniel Borkmann
2021-04-22 12:46 ` Toke Høiland-Jørgensen
2021-04-21 22:59 ` Daniel Borkmann
2021-04-21 23:08 ` Kumar Kartikeya Dwivedi
2021-04-21 23:21 ` Daniel Borkmann
2021-04-21 23:30 ` Kumar Kartikeya Dwivedi
2021-04-21 23:41 ` Daniel Borkmann
2021-04-22 9:47 ` Shaun Crampton
2021-04-22 11:26 ` Daniel Borkmann
2021-04-22 3:43 ` Andrii Nakryiko
2021-04-22 15:35 ` Daniel Borkmann
2021-04-22 18:28 ` Andrii Nakryiko
2021-04-20 19:37 ` Kumar Kartikeya Dwivedi [this message]
2021-04-21 7:01 ` [PATCH bpf-next v3 3/3] libbpf: add selftests for " Yonghong Song
2021-04-21 18:24 ` Andrii Nakryiko
2021-04-21 19:56 ` Kumar Kartikeya Dwivedi
2021-04-21 20:38 ` Toke Høiland-Jørgensen
2021-04-21 22:41 ` Daniel Borkmann
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=20210420193740.124285-4-memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brouer@redhat.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=toke@redhat.com \
--cc=yhs@fb.com \
/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;
as well as URLs for NNTP newsgroup(s).