2023-07-30 13:18:32

by Leon Hwang

[permalink] [raw]
Subject: [PATCH bpf-next v4 0/2] bpf, xdp: Add tracepoint to xdp attaching failure

This series introduces a new tracepoint in bpf_xdp_link_attach(). By
this tracepoint, error message will be captured when error happens in
dev_xdp_attach(), e.g. invalid attaching flags.

v3 -> v4:
* Fix selftest-crashed issue.

Leon Hwang (2):
bpf, xdp: Add tracepoint to xdp attaching failure
selftests/bpf: Add testcase for xdp attaching failure tracepoint

include/trace/events/xdp.h | 17 +++++
net/core/dev.c | 5 +-
.../selftests/bpf/prog_tests/xdp_attach.c | 65 +++++++++++++++++++
.../bpf/progs/test_xdp_attach_fail.c | 54 +++++++++++++++
4 files changed, 140 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c


base-commit: a33d978500acd8fb67efac9773ba0a8502c1ff06
--
2.41.0



2023-07-30 13:32:34

by Leon Hwang

[permalink] [raw]
Subject: [PATCH bpf-next v4 2/2] selftests/bpf: Add testcase for xdp attaching failure tracepoint

Add a test case for the tracepoint of xdp attaching failure by bpf
tracepoint when attach XDP to a device with invalid flags option.

The bpf tracepoint retrieves error message from the tracepoint, and
then put the error message to a perf buffer. The testing code receives
error message from perf buffer, and then ASSERT "Invalid XDP flags for
BPF link attachment".

Signed-off-by: Leon Hwang <[email protected]>
---
.../selftests/bpf/prog_tests/xdp_attach.c | 65 +++++++++++++++++++
.../bpf/progs/test_xdp_attach_fail.c | 54 +++++++++++++++
2 files changed, 119 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_attach.c b/tools/testing/selftests/bpf/prog_tests/xdp_attach.c
index fa3cac5488f5d..8c1cde74e9cd6 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_attach.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_attach.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
+#include "test_xdp_attach_fail.skel.h"

#define IFINDEX_LO 1
#define XDP_FLAGS_REPLACE (1U << 4)
@@ -85,10 +86,74 @@ static void test_xdp_attach(const char *file)
bpf_object__close(obj1);
}

+#define ERRMSG_LEN 64
+
+struct xdp_errmsg {
+ char msg[ERRMSG_LEN];
+};
+
+static void on_xdp_errmsg(void *ctx, int cpu, void *data, __u32 size)
+{
+ struct xdp_errmsg *ctx_errmg = ctx, *tp_errmsg = data;
+
+ memcpy(&ctx_errmg->msg, &tp_errmsg->msg, ERRMSG_LEN);
+}
+
+static const char tgt_errmsg[] = "Invalid XDP flags for BPF link attachment";
+
+static void test_xdp_attach_fail(const char *file)
+{
+ int err, fd_xdp;
+ struct bpf_object *obj = NULL;
+ struct test_xdp_attach_fail *skel = NULL;
+ struct perf_buffer *pb = NULL;
+ struct xdp_errmsg errmsg = {};
+
+ LIBBPF_OPTS(bpf_link_create_opts, opts);
+
+ skel = test_xdp_attach_fail__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_xdp_attach_fail__open_and_load"))
+ goto out_close;
+
+ err = test_xdp_attach_fail__attach(skel);
+ if (!ASSERT_EQ(err, 0, "test_xdp_attach_fail__attach"))
+ goto out_close;
+
+ /* set up perf buffer */
+ pb = perf_buffer__new(bpf_map__fd(skel->maps.xdp_errmsg_pb), 1,
+ on_xdp_errmsg, NULL, &errmsg, NULL);
+ if (!ASSERT_OK_PTR(pb, "perf_buffer__new"))
+ goto out_close;
+
+ err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &fd_xdp);
+ if (!ASSERT_EQ(err, 0, "bpf_prog_test_load"))
+ goto out_close;
+
+ opts.flags = 0xFF; // invalid flags to fail to attach XDP prog
+ err = bpf_link_create(fd_xdp, IFINDEX_LO, BPF_XDP, &opts);
+ if (!ASSERT_EQ(err, -EINVAL, "bpf_link_create"))
+ goto out_close;
+
+ /* read perf buffer */
+ err = perf_buffer__poll(pb, 100);
+ if (!ASSERT_GT(err, -1, "perf_buffer__poll"))
+ goto out_close;
+
+ ASSERT_STRNEQ((const char *) errmsg.msg, tgt_errmsg,
+ 42 /* strlen(tgt_errmsg) */, "check error message");
+
+out_close:
+ perf_buffer__free(pb);
+ bpf_object__close(obj);
+ test_xdp_attach_fail__destroy(skel);
+}
+
void serial_test_xdp_attach(void)
{
if (test__start_subtest("xdp_attach"))
test_xdp_attach("./test_xdp.bpf.o");
if (test__start_subtest("xdp_attach_dynptr"))
test_xdp_attach("./test_xdp_dynptr.bpf.o");
+ if (test__start_subtest("xdp_attach_failed"))
+ test_xdp_attach_fail("./xdp_dummy.bpf.o");
}
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c b/tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
new file mode 100644
index 0000000000000..d7149bbd95f75
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright Leon Hwang */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+#define ERRMSG_LEN 64
+
+struct xdp_errmsg {
+ char msg[ERRMSG_LEN];
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+ __type(key, int);
+ __type(value, int);
+} xdp_errmsg_pb SEC(".maps");
+
+struct xdp_attach_error_ctx {
+ unsigned long unused;
+
+ /*
+ * bpf does not support tracepoint __data_loc directly.
+ *
+ * Actually, this field is a 32 bit integer whose value encodes
+ * information on where to find the actual data. The first 2 bytes is
+ * the size of the data. The last 2 bytes is the offset from the start
+ * of the tracepoint struct where the data begins.
+ * -- https://github.com/iovisor/bpftrace/pull/1542
+ */
+ __u32 msg; // __data_loc char[] msg;
+};
+
+/*
+ * Catch the error message at the tracepoint.
+ */
+
+SEC("tp/xdp/bpf_xdp_link_attach_failed")
+int tp__xdp__bpf_xdp_link_attach_failed(struct xdp_attach_error_ctx *ctx)
+{
+ struct xdp_errmsg errmsg;
+ char *msg = (void *)(__u64) ((void *) ctx + (__u16) ctx->msg);
+
+ bpf_probe_read_kernel_str(&errmsg.msg, ERRMSG_LEN, msg);
+ bpf_perf_event_output(ctx, &xdp_errmsg_pb, BPF_F_CURRENT_CPU, &errmsg,
+ ERRMSG_LEN);
+ return 0;
+}
+
+/*
+ * Reuse the XDP program in xdp_dummy.c.
+ */
+
+char LICENSE[] SEC("license") = "GPL";
--
2.41.0


2023-07-30 15:38:53

by Manjusaka

[permalink] [raw]
Subject: Re: [PATCH bpf-next v4 0/2] bpf, xdp: Add tracepoint to xdp attaching failure

This patch is very important to help us to debug the xdp program. At
the same time, we can make some monitoring tools to observe the kernel
status by using this trace event

李者璈 & Zheaoli

Email: [email protected]
Github: https://github.com/Zheaoli


Leon Hwang <[email protected]> 于2023年7月30日周日 19:50写道:
>
> This series introduces a new tracepoint in bpf_xdp_link_attach(). By
> this tracepoint, error message will be captured when error happens in
> dev_xdp_attach(), e.g. invalid attaching flags.
>
> v3 -> v4:
> * Fix selftest-crashed issue.
>
> Leon Hwang (2):
> bpf, xdp: Add tracepoint to xdp attaching failure
> selftests/bpf: Add testcase for xdp attaching failure tracepoint
>
> include/trace/events/xdp.h | 17 +++++
> net/core/dev.c | 5 +-
> .../selftests/bpf/prog_tests/xdp_attach.c | 65 +++++++++++++++++++
> .../bpf/progs/test_xdp_attach_fail.c | 54 +++++++++++++++
> 4 files changed, 140 insertions(+), 1 deletion(-)
> create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
>
>
> base-commit: a33d978500acd8fb67efac9773ba0a8502c1ff06
> --
> 2.41.0
>
>

2023-07-31 13:51:18

by Leon Hwang

[permalink] [raw]
Subject: Re: [PATCH bpf-next v4 0/2] bpf, xdp: Add tracepoint to xdp attaching failure



On 2023/7/30 21:49, Manjusaka wrote:
> This patch is very important to help us to debug the xdp program. At
> the same time, we can make some monitoring tools to observe the kernel
> status by using this trace event
>
> 李者璈 & Zheaoli
>
> Email: [email protected]
> Github: https://github.com/Zheaoli
>

Thank you for your feedback.

I'm glad that it's helpful for you.

Thanks,
Leon