Thread (18 messages) 18 messages, 4 authors, 2021-04-27

Re: [PATCH bpf-next v4 3/3] libbpf: add selftests for TC-BPF API

From: Andrii Nakryiko <hidden>
Date: 2021-04-27 21:50:23
Also in: bpf

On Fri, Apr 23, 2021 at 8:06 AM Kumar Kartikeya Dwivedi
[off-list ref] wrote:
This adds some basic tests for the low level bpf_tc_* API.

Reviewed-by: Toke Høiland-Jørgensen <redacted>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../testing/selftests/bpf/prog_tests/tc_bpf.c | 204 ++++++++++++++++++
 .../testing/selftests/bpf/progs/test_tc_bpf.c |  12 ++
 2 files changed, 216 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf.c
[...]
+
+void test_tc_bpf(void)
+{
+       struct bpf_tc_ctx *ctx_ing = NULL, *ctx_eg = NULL;
+       struct test_tc_bpf *skel = NULL;
+       int cls_fd, ret;
+
+       skel = test_tc_bpf__open_and_load();
+       if (!ASSERT_NEQ(skel, NULL, "test_tc_bpf skeleton"))
+               goto end;
+
+       cls_fd = bpf_program__fd(skel->progs.cls);
+
+       ctx_ing = bpf_tc_ctx_init(LO_IFINDEX, BPF_TC_INGRESS, NULL);
+       if (!ASSERT_NEQ(ctx_ing, NULL, "bpf_tc_ctx_init(BPF_TC_INGRESS)"))
please use ASSERT_OK_PTR() and ASSERT_ERR_PTR() for pointer checks.
They handle both NULL/non-NULL cases and ERR_PTR() errors.
quoted hunk ↗ jump to hunk
+               goto end;
+
+       ctx_eg = bpf_tc_ctx_init(LO_IFINDEX, BPF_TC_EGRESS, NULL);
+       if (!ASSERT_NEQ(ctx_eg, NULL, "bpf_tc_ctx_init(BPF_TC_EGRESS)"))
+               goto end;
+
+       ret = test_tc_internal(ctx_ing, cls_fd, BPF_TC_INGRESS);
+       if (!ASSERT_EQ(ret, 0, "test_tc_internal ingress"))
+               goto end;
+
+       ret = test_tc_internal(ctx_eg, cls_fd, BPF_TC_EGRESS);
+       if (!ASSERT_EQ(ret, 0, "test_tc_internal egress"))
+               goto end;
+
+       ret = test_tc_invalid(ctx_ing, cls_fd);
+       if (!ASSERT_EQ(ret, 0, "test_tc_invalid"))
+               goto end;
+
+end:
+       bpf_tc_ctx_destroy(ctx_eg);
+       bpf_tc_ctx_destroy(ctx_ing);
+       test_tc_bpf__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_tc_bpf.c b/tools/testing/selftests/bpf/progs/test_tc_bpf.c
new file mode 100644
index 000000000000..18a3a7ed924a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_tc_bpf.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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help