mirror of
https://github.com/Fishwaldo/Star64_linux.git
synced 2025-03-19 05:34:08 +00:00
Merge branch 'bpf-xdp-add-tracepoint-to-xdp-attaching-failure'
Leon Hwang says: ==================== 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. v4 -> v5: * Initialise the extack variable. * Fix code style issue of variable declaration lines. v3 -> v4: * Fix selftest-crashed issue. ==================== Link: https://lore.kernel.org/r/20230801142621.7925-1-hffilwlqm@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
commit
87dc2bb329
4 changed files with 140 additions and 1 deletions
|
@ -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 */
|
#endif /* _TRACE_XDP_H */
|
||||||
|
|
||||||
#include <trace/define_trace.h>
|
#include <trace/define_trace.h>
|
||||||
|
|
|
@ -133,6 +133,7 @@
|
||||||
#include <trace/events/net.h>
|
#include <trace/events/net.h>
|
||||||
#include <trace/events/skb.h>
|
#include <trace/events/skb.h>
|
||||||
#include <trace/events/qdisc.h>
|
#include <trace/events/qdisc.h>
|
||||||
|
#include <trace/events/xdp.h>
|
||||||
#include <linux/inetdevice.h>
|
#include <linux/inetdevice.h>
|
||||||
#include <linux/cpu_rmap.h>
|
#include <linux/cpu_rmap.h>
|
||||||
#include <linux/static_key.h>
|
#include <linux/static_key.h>
|
||||||
|
@ -9470,6 +9471,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
|
||||||
{
|
{
|
||||||
struct net *net = current->nsproxy->net_ns;
|
struct net *net = current->nsproxy->net_ns;
|
||||||
struct bpf_link_primer link_primer;
|
struct bpf_link_primer link_primer;
|
||||||
|
struct netlink_ext_ack extack = {};
|
||||||
struct bpf_xdp_link *link;
|
struct bpf_xdp_link *link;
|
||||||
struct net_device *dev;
|
struct net_device *dev;
|
||||||
int err, fd;
|
int err, fd;
|
||||||
|
@ -9497,12 +9499,13 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
|
||||||
goto unlock;
|
goto unlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dev_xdp_attach_link(dev, NULL, link);
|
err = dev_xdp_attach_link(dev, &extack, link);
|
||||||
rtnl_unlock();
|
rtnl_unlock();
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
link->dev = NULL;
|
link->dev = NULL;
|
||||||
bpf_link_cleanup(&link_primer);
|
bpf_link_cleanup(&link_primer);
|
||||||
|
trace_bpf_xdp_link_attach_failed(extack._msg);
|
||||||
goto out_put_dev;
|
goto out_put_dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
#include <test_progs.h>
|
#include <test_progs.h>
|
||||||
|
#include "test_xdp_attach_fail.skel.h"
|
||||||
|
|
||||||
#define IFINDEX_LO 1
|
#define IFINDEX_LO 1
|
||||||
#define XDP_FLAGS_REPLACE (1U << 4)
|
#define XDP_FLAGS_REPLACE (1U << 4)
|
||||||
|
@ -85,10 +86,74 @@ out_1:
|
||||||
bpf_object__close(obj1);
|
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)
|
||||||
|
{
|
||||||
|
struct test_xdp_attach_fail *skel = NULL;
|
||||||
|
struct xdp_errmsg errmsg = {};
|
||||||
|
struct perf_buffer *pb = NULL;
|
||||||
|
struct bpf_object *obj = NULL;
|
||||||
|
int err, fd_xdp;
|
||||||
|
|
||||||
|
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)
|
void serial_test_xdp_attach(void)
|
||||||
{
|
{
|
||||||
if (test__start_subtest("xdp_attach"))
|
if (test__start_subtest("xdp_attach"))
|
||||||
test_xdp_attach("./test_xdp.bpf.o");
|
test_xdp_attach("./test_xdp.bpf.o");
|
||||||
if (test__start_subtest("xdp_attach_dynptr"))
|
if (test__start_subtest("xdp_attach_dynptr"))
|
||||||
test_xdp_attach("./test_xdp_dynptr.bpf.o");
|
test_xdp_attach("./test_xdp_dynptr.bpf.o");
|
||||||
|
if (test__start_subtest("xdp_attach_failed"))
|
||||||
|
test_xdp_attach_fail("./xdp_dummy.bpf.o");
|
||||||
}
|
}
|
||||||
|
|
54
tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
Normal file
54
tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
Normal file
|
@ -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)
|
||||||
|
{
|
||||||
|
char *msg = (void *)(__u64) ((void *) ctx + (__u16) ctx->msg);
|
||||||
|
struct xdp_errmsg errmsg = {};
|
||||||
|
|
||||||
|
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";
|
Loading…
Add table
Reference in a new issue