Skip to contentSkip to conversation

bpf, xdp: Add tracepoint to xdp attaching failure

PATCH RESEND Superseded Leon Hwang 2 participants 5 messages Jul 20, 2023 – Jul 27, 2023

Subsystems (7)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

v3 cover 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.

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     | 63 +++++++++++++++++++
 .../bpf/progs/test_xdp_attach_fail.c          | 51 +++++++++++++++
 4 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c

base-commit: 0858a95ec7491a7a5bfca4be9736dba4ee38c461

Signature · 1 line
2.41.0
author
#1 lore raw

PATCH RESEND v3 1/2 bpf, xdp: Add tracepoint to xdp attaching failure

When error happens in dev_xdp_attach(), it should have a way to tell users the error message like the netlink approach.

To avoid breaking uapi, adding a tracepoint in bpf_xdp_link_attach() is an appropriate way to notify users the error message.

Hence, bpf libraries are able to retrieve the error message by this tracepoint, and then report the error message to users.

---
 include/trace/events/xdp.h | 17 +++++++++++++++++
 net/core/dev.c             |  5 ++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
include/trace/events/xdp.h +17 -0
index c40fc97f94171..cd89f1d5ce7b8 100644
@@ -404,6 +404,23 @@ TRACE_EVENT(mem_return_failed,
 	)
 );
 
+TRACE_EVENT(bpf_xdp_link_attach_failed,
+
+	TP_PROTO(const char *msg),
+
+	TP_ARGS(msg),
+
+	TP_STRUCT__entry(
+		__string(msg, msg)
+	),
+
+	TP_fast_assign(
+		__assign_str(msg, msg);
+	),
+
+	TP_printk("errmsg=%s", __get_str(msg))
+);
+
 #endif /* _TRACE_XDP_H */
 
 #include <trace/define_trace.h>
net/core/dev.c +4 -1
index 8e7d0cb540cdb..49bed890f807e 100644
@@ -133,6 +133,7 @@
 #include <trace/events/net.h>
 #include <trace/events/skb.h>
 #include <trace/events/qdisc.h>
+#include <trace/events/xdp.h>
 #include <linux/inetdevice.h>
 #include <linux/cpu_rmap.h>
 #include <linux/static_key.h>
@@ -9472,6 +9473,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 	struct bpf_link_primer link_primer;
 	struct bpf_xdp_link *link;
 	struct net_device *dev;
+	struct netlink_ext_ack extack;
 	int err, fd;
 
 	rtnl_lock();
@@ -9497,12 +9499,13 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 		goto unlock;
 	}
 
-	err = dev_xdp_attach_link(dev, NULL, link);
+	err = dev_xdp_attach_link(dev, &extack, link);
 	rtnl_unlock();
 
 	if (err) {
 		link->dev = NULL;
 		bpf_link_cleanup(&link_primer);
+		trace_bpf_xdp_link_attach_failed(extack._msg);
 		goto out_put_dev;
 	}
 
Signature · 1 line
2.41.0
author
#2 lore raw

PATCH RESEND v3 2/2 selftests/bpf: Add testcase for xdp attaching failure tracepoint

In reply to Leon Hwang

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".

---
 .../selftests/bpf/prog_tests/xdp_attach.c     | 65 +++++++++++++++++++
 .../bpf/progs/test_xdp_attach_fail.c          | 52 +++++++++++++++
 2 files changed, 117 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +65 -0
index fa3cac5488f5d..99f8d03f3c8bd 100644
@@ -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);
 }
 
+struct xdp_errmsg {
+	char msg[64];
+};
+
+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, size);
+}
+
+static const char tgt_errmsg[] = "Invalid XDP flags for BPF link attachment";
+
+static void test_xdp_attach_fail(const char *file)
+{
+	__u32 duration = 0;
+	int err, fd_xdp, fd_link_xdp;
+	struct bpf_object *obj = NULL;
+	struct test_xdp_attach_fail *skel = NULL;
+	struct bpf_link *link = 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_skel"))
+		goto out_close;
+
+	link = bpf_program__attach_tracepoint(skel->progs.tp__xdp__bpf_xdp_link_attach_failed,
+					      "xdp", "bpf_xdp_link_attach_failed");
+	if (!ASSERT_OK_PTR(link, "attach_tp"))
+		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);
+
+	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &fd_xdp);
+	if (CHECK_FAIL(err))
+		goto out_close;
+
+	opts.flags = 0xFF; // invalid flags to fail to attach XDP prog
+	fd_link_xdp = bpf_link_create(fd_xdp, IFINDEX_LO, BPF_XDP, &opts);
+	if (CHECK(fd_link_xdp != -22, "bpf_link_create_failed",
+		  "created link fd: %d\n", fd_link_xdp))
+		goto out_close;
+
+	/* read perf buffer */
+	err = perf_buffer__poll(pb, 100);
+	if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
+		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");
 }
tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.41.0
#3 lore raw

Re: [RESEND PATCH bpf-next v3 2/2] selftests/bpf: Add testcase for xdp attaching failure tracepoint

Quoted 34 lines · Leon Hwang

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".

---
  .../selftests/bpf/prog_tests/xdp_attach.c     | 65 +++++++++++++++++++
  .../bpf/progs/test_xdp_attach_fail.c          | 52 +++++++++++++++
  2 files changed, 117 insertions(+)

create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c

tools/testing/selftests/bpf/prog_tests/xdp_attach.c +5 -0
index fa3cac5488f5d..99f8d03f3c8bd 100644
@@ -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);
  }
 
+struct xdp_errmsg {
+	char msg[64];
+};
+

tools/testing/selftests/bpf/prog_tests/xdp_attach.c static void test_xdp_attach(const char *file) · line 102

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +10 -0
+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, size);
+}
+
+static const char tgt_errmsg[] = "Invalid XDP flags for BPF link attachment";
+
+static void test_xdp_attach_fail(const char *file)

The test crashed: https://github.com/kernel-patches/bpf/actions/runs/5672753995/job/15373384795#step:6:8037

Please monitor the CI test result in the future.

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +21 -0
+{
+	__u32 duration = 0;
+	int err, fd_xdp, fd_link_xdp;
+	struct bpf_object *obj = NULL;
+	struct test_xdp_attach_fail *skel = NULL;
+	struct bpf_link *link = 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_skel"))
+		goto out_close;
+
+	link = bpf_program__attach_tracepoint(skel->progs.tp__xdp__bpf_xdp_link_attach_failed,
+					      "xdp", "bpf_xdp_link_attach_failed");
+	if (!ASSERT_OK_PTR(link, "attach_tp"))
+		goto out_close;
+
+	/* set up perf buffer */

tools/testing/selftests/bpf/prog_tests/xdp_attach.c static void test_xdp_attach(const char *file) · line 133

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +10 -0
+	pb = perf_buffer__new(bpf_map__fd(skel->maps.xdp_errmsg_pb), 1,
+			      on_xdp_errmsg, NULL, &errmsg, NULL);
+
+	err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &fd_xdp);
+	if (CHECK_FAIL(err))
+		goto out_close;
+
+	opts.flags = 0xFF; // invalid flags to fail to attach XDP prog
+	fd_link_xdp = bpf_link_create(fd_xdp, IFINDEX_LO, BPF_XDP, &opts);
+	if (CHECK(fd_link_xdp != -22, "bpf_link_create_failed",

Please stay with the ASSERT_* macro.

Signature · 1 line
pw-bot: cr
author
#4 lore raw

Re: [RESEND PATCH bpf-next v3 2/2] selftests/bpf: Add testcase for xdp attaching failure tracepoint

Quoted 46 lines · Martin KaFai Lau
Quoted 32 lines · Leon Hwang

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".

---
  .../selftests/bpf/prog_tests/xdp_attach.c     | 65 +++++++++++++++++++ .../bpf/progs/test_xdp_attach_fail.c          | 52 +++++++++++++++

  2 files changed, 117 insertions(+)

  create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c

tools/testing/selftests/bpf/prog_tests/xdp_attach.c +4 -0
index fa3cac5488f5d..99f8d03f3c8bd 100644
@@ -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);
   }
   +struct xdp_errmsg {
+    char msg[64];
+};
+

tools/testing/selftests/bpf/prog_tests/xdp_attach.c static void test_xdp_attach(const char *file) · line 101

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +10 -0
+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, size);
+}
+
+static const char tgt_errmsg[] = "Invalid XDP flags for BPF link attachment";
+
+static void test_xdp_attach_fail(const char *file)

The test crashed: https://github.com/kernel-patches/bpf/actions/runs/5672753995/job/15373384795#step:6:8037

Quoted 2 lines

Please monitor the CI test result in the future.

Get it. I'll fix it as soon as possible. And make sure all the CI tests are passed.

Quoted 25 lines
Quoted 25 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +25 -0
+{
+    __u32 duration = 0;
+    int err, fd_xdp, fd_link_xdp;
+    struct bpf_object *obj = NULL;
+    struct test_xdp_attach_fail *skel = NULL;
+    struct bpf_link *link = 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_skel"))
+        goto out_close;
+
+    link = bpf_program__attach_tracepoint(skel->progs.tp__xdp__bpf_xdp_link_attach_failed,
+                          "xdp", "bpf_xdp_link_attach_failed");
+    if (!ASSERT_OK_PTR(link, "attach_tp"))
+        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);
+
+    err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &fd_xdp);
Quoted 9 lines

tools/testing/selftests/bpf/prog_tests/xdp_attach.c static void test_xdp_attach(const char *file) · line 133

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/xdp_attach.c +6 -0
+    if (CHECK_FAIL(err))
+        goto out_close;
+
+    opts.flags = 0xFF; // invalid flags to fail to attach XDP prog
+    fd_link_xdp = bpf_link_create(fd_xdp, IFINDEX_LO, BPF_XDP, &opts);
+    if (CHECK(fd_link_xdp != -22, "bpf_link_create_failed",

Please stay with the ASSERT_* macro.

Get it.

Thanks,
Leon