Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <hffilwlqm@gmail.com>
Link: https://lore.kernel.org/r/20230801142621.7925-3-hffilwlqm@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Leon Hwang and committed by
Alexei Starovoitov
7fedbf32 bf4ea1d0

+119
+65
tools/testing/selftests/bpf/prog_tests/xdp_attach.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include <test_progs.h> 3 + #include "test_xdp_attach_fail.skel.h" 3 4 4 5 #define IFINDEX_LO 1 5 6 #define XDP_FLAGS_REPLACE (1U << 4) ··· 86 85 bpf_object__close(obj1); 87 86 } 88 87 88 + #define ERRMSG_LEN 64 89 + 90 + struct xdp_errmsg { 91 + char msg[ERRMSG_LEN]; 92 + }; 93 + 94 + static void on_xdp_errmsg(void *ctx, int cpu, void *data, __u32 size) 95 + { 96 + struct xdp_errmsg *ctx_errmg = ctx, *tp_errmsg = data; 97 + 98 + memcpy(&ctx_errmg->msg, &tp_errmsg->msg, ERRMSG_LEN); 99 + } 100 + 101 + static const char tgt_errmsg[] = "Invalid XDP flags for BPF link attachment"; 102 + 103 + static void test_xdp_attach_fail(const char *file) 104 + { 105 + struct test_xdp_attach_fail *skel = NULL; 106 + struct xdp_errmsg errmsg = {}; 107 + struct perf_buffer *pb = NULL; 108 + struct bpf_object *obj = NULL; 109 + int err, fd_xdp; 110 + 111 + LIBBPF_OPTS(bpf_link_create_opts, opts); 112 + 113 + skel = test_xdp_attach_fail__open_and_load(); 114 + if (!ASSERT_OK_PTR(skel, "test_xdp_attach_fail__open_and_load")) 115 + goto out_close; 116 + 117 + err = test_xdp_attach_fail__attach(skel); 118 + if (!ASSERT_EQ(err, 0, "test_xdp_attach_fail__attach")) 119 + goto out_close; 120 + 121 + /* set up perf buffer */ 122 + pb = perf_buffer__new(bpf_map__fd(skel->maps.xdp_errmsg_pb), 1, 123 + on_xdp_errmsg, NULL, &errmsg, NULL); 124 + if (!ASSERT_OK_PTR(pb, "perf_buffer__new")) 125 + goto out_close; 126 + 127 + err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &fd_xdp); 128 + if (!ASSERT_EQ(err, 0, "bpf_prog_test_load")) 129 + goto out_close; 130 + 131 + opts.flags = 0xFF; // invalid flags to fail to attach XDP prog 132 + err = bpf_link_create(fd_xdp, IFINDEX_LO, BPF_XDP, &opts); 133 + if (!ASSERT_EQ(err, -EINVAL, "bpf_link_create")) 134 + goto out_close; 135 + 136 + /* read perf buffer */ 137 + err = perf_buffer__poll(pb, 100); 138 + if (!ASSERT_GT(err, -1, "perf_buffer__poll")) 139 + goto out_close; 140 + 141 + ASSERT_STRNEQ((const char *) errmsg.msg, tgt_errmsg, 142 + 42 /* strlen(tgt_errmsg) */, "check error message"); 143 + 144 + out_close: 145 + perf_buffer__free(pb); 146 + bpf_object__close(obj); 147 + test_xdp_attach_fail__destroy(skel); 148 + } 149 + 89 150 void serial_test_xdp_attach(void) 90 151 { 91 152 if (test__start_subtest("xdp_attach")) 92 153 test_xdp_attach("./test_xdp.bpf.o"); 93 154 if (test__start_subtest("xdp_attach_dynptr")) 94 155 test_xdp_attach("./test_xdp_dynptr.bpf.o"); 156 + if (test__start_subtest("xdp_attach_failed")) 157 + test_xdp_attach_fail("./xdp_dummy.bpf.o"); 95 158 }
+54
tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright Leon Hwang */ 3 + 4 + #include <linux/bpf.h> 5 + #include <bpf/bpf_helpers.h> 6 + 7 + #define ERRMSG_LEN 64 8 + 9 + struct xdp_errmsg { 10 + char msg[ERRMSG_LEN]; 11 + }; 12 + 13 + struct { 14 + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); 15 + __type(key, int); 16 + __type(value, int); 17 + } xdp_errmsg_pb SEC(".maps"); 18 + 19 + struct xdp_attach_error_ctx { 20 + unsigned long unused; 21 + 22 + /* 23 + * bpf does not support tracepoint __data_loc directly. 24 + * 25 + * Actually, this field is a 32 bit integer whose value encodes 26 + * information on where to find the actual data. The first 2 bytes is 27 + * the size of the data. The last 2 bytes is the offset from the start 28 + * of the tracepoint struct where the data begins. 29 + * -- https://github.com/iovisor/bpftrace/pull/1542 30 + */ 31 + __u32 msg; // __data_loc char[] msg; 32 + }; 33 + 34 + /* 35 + * Catch the error message at the tracepoint. 36 + */ 37 + 38 + SEC("tp/xdp/bpf_xdp_link_attach_failed") 39 + int tp__xdp__bpf_xdp_link_attach_failed(struct xdp_attach_error_ctx *ctx) 40 + { 41 + char *msg = (void *)(__u64) ((void *) ctx + (__u16) ctx->msg); 42 + struct xdp_errmsg errmsg = {}; 43 + 44 + bpf_probe_read_kernel_str(&errmsg.msg, ERRMSG_LEN, msg); 45 + bpf_perf_event_output(ctx, &xdp_errmsg_pb, BPF_F_CURRENT_CPU, &errmsg, 46 + ERRMSG_LEN); 47 + return 0; 48 + } 49 + 50 + /* 51 + * Reuse the XDP program in xdp_dummy.c. 52 + */ 53 + 54 + char LICENSE[] SEC("license") = "GPL";