diff mbox series

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

Message ID 20230720155228.5708-3-hffilwlqm@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf, xdp: Add tracepoint to xdp attaching failure | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 3 maintainers not CCed: mykolal@fb.com shuah@kernel.org linux-kselftest@vger.kernel.org
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning CHECK: No space is necessary after a cast WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 83 exceeds 80 columns WARNING: line length of 94 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 fail Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 fail Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 fail Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 fail Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc

Commit Message

Leon Hwang July 20, 2023, 3:52 p.m. UTC
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>
---
 .../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

Comments

Martin KaFai Lau July 26, 2023, 9:52 p.m. UTC | #1
On 7/20/23 8:52 AM, Leon Hwang wrote:
> 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>
> ---
>   .../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
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_attach.c b/tools/testing/selftests/bpf/prog_tests/xdp_attach.c
> index fa3cac5488f5d..99f8d03f3c8bd 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);
>   }
>   
> +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)

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.

> +{
> +	__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",

Please stay with the ASSERT_* macro.
Leon Hwang July 27, 2023, 1:57 a.m. UTC | #2
On 27/7/23 05:52, Martin KaFai Lau wrote:
> On 7/20/23 8:52 AM, Leon Hwang wrote:
>> 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>
>> ---
>>   .../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
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_attach.c b/tools/testing/selftests/bpf/prog_tests/xdp_attach.c
>> index fa3cac5488f5d..99f8d03f3c8bd 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);
>>   }
>>   +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)
> 
> 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.
> 

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

>> +{
>> +    __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",
> 
> Please stay with the ASSERT_* macro.
> 

Get it.


Thanks,
Leon
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_attach.c b/tools/testing/selftests/bpf/prog_tests/xdp_attach.c
index fa3cac5488f5d..99f8d03f3c8bd 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);
 }
 
+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");
 }
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..64baf73910a0e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_xdp_attach_fail.c
@@ -0,0 +1,52 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright Leon Hwang */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+struct xdp_errmsg {
+	char msg[64];
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+	__uint(key_size, sizeof(__u32));
+	__uint(value_size, sizeof(__u32));
+} 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 up 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, sizeof(errmsg.msg), msg);
+	bpf_perf_event_output(ctx, &xdp_errmsg_pb, BPF_F_CURRENT_CPU, &errmsg,
+			      sizeof(errmsg));
+	return 0;
+}
+
+/*
+ * Reuse the XDP program in xdp_dummy.c.
+ */
+
+char LICENSE[] SEC("license") = "GPL";