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

selftests/bpf: Support non-linear flag in test loader

This patch adds support for a new tag __linear_size in the test loader,
to specify the size of the linear area in case of non-linear skbs. If
the tag is absent or null, a linear skb is crafted.

Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/7ad928ec7591daef4f1b84032aeb86c918b3e5a7.1760037899.git.paul.chaignon@gmail.com

authored by

Paul Chaignon and committed by
Martin KaFai Lau
8d45d039 838baa35

+32 -2
+4
tools/testing/selftests/bpf/progs/bpf_misc.h
··· 126 126 * Several __arch_* annotations could be specified at once. 127 127 * When test case is not run on current arch it is marked as skipped. 128 128 * __caps_unpriv Specify the capabilities that should be set when running the test. 129 + * 130 + * __linear_size Specify the size of the linear area of non-linear skbs, or 131 + * 0 for linear skbs. 129 132 */ 130 133 #define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg))) 131 134 #define __not_msg(msg) __attribute__((btf_decl_tag("comment:test_expect_not_msg=" XSTR(__COUNTER__) "=" msg))) ··· 162 159 #define __stderr_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stderr_unpriv=" XSTR(__COUNTER__) "=" msg))) 163 160 #define __stdout(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg))) 164 161 #define __stdout_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg))) 162 + #define __linear_size(sz) __attribute__((btf_decl_tag("comment:test_linear_size=" XSTR(sz)))) 165 163 166 164 /* Define common capabilities tested using __caps_unpriv */ 167 165 #define CAP_NET_ADMIN 12
+1
tools/testing/selftests/bpf/progs/verifier_direct_packet_access.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Converted from tools/testing/selftests/bpf/verifier/direct_packet_access.c */ 3 3 4 + #include <linux/if_ether.h> 4 5 #include <linux/bpf.h> 5 6 #include <bpf/bpf_helpers.h> 6 7 #include "bpf_misc.h"
+27 -2
tools/testing/selftests/bpf/test_loader.c
··· 43 43 #define TEST_TAG_EXPECT_STDERR_PFX_UNPRIV "comment:test_expect_stderr_unpriv=" 44 44 #define TEST_TAG_EXPECT_STDOUT_PFX "comment:test_expect_stdout=" 45 45 #define TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV "comment:test_expect_stdout_unpriv=" 46 + #define TEST_TAG_LINEAR_SIZE "comment:test_linear_size=" 46 47 47 48 /* Warning: duplicated in bpf_misc.h */ 48 49 #define POINTER_VALUE 0xbadcafe ··· 90 89 int mode_mask; 91 90 int arch_mask; 92 91 int load_mask; 92 + int linear_sz; 93 93 bool auxiliary; 94 94 bool valid; 95 95 }; ··· 635 633 &spec->unpriv.stdout); 636 634 if (err) 637 635 goto cleanup; 636 + } else if (str_has_pfx(s, TEST_TAG_LINEAR_SIZE)) { 637 + switch (bpf_program__type(prog)) { 638 + case BPF_PROG_TYPE_SCHED_ACT: 639 + case BPF_PROG_TYPE_SCHED_CLS: 640 + case BPF_PROG_TYPE_CGROUP_SKB: 641 + val = s + sizeof(TEST_TAG_LINEAR_SIZE) - 1; 642 + err = parse_int(val, &spec->linear_sz, "test linear size"); 643 + if (err) 644 + goto cleanup; 645 + break; 646 + default: 647 + PRINT_FAIL("__linear_size for unsupported program type"); 648 + err = -EINVAL; 649 + goto cleanup; 650 + } 638 651 } 639 652 } 640 653 ··· 1024 1007 } 1025 1008 } 1026 1009 1027 - static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts) 1010 + static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts, int linear_sz) 1028 1011 { 1029 1012 __u8 tmp_out[TEST_DATA_LEN << 2] = {}; 1030 1013 __u8 tmp_in[TEST_DATA_LEN] = {}; 1014 + struct __sk_buff ctx = {}; 1031 1015 int err, saved_errno; 1032 1016 LIBBPF_OPTS(bpf_test_run_opts, topts, 1033 1017 .data_in = tmp_in, ··· 1037 1019 .data_size_out = sizeof(tmp_out), 1038 1020 .repeat = 1, 1039 1021 ); 1022 + 1023 + if (linear_sz) { 1024 + ctx.data_end = linear_sz; 1025 + topts.ctx_in = &ctx; 1026 + topts.ctx_size_in = sizeof(ctx); 1027 + } 1040 1028 1041 1029 if (empty_opts) { 1042 1030 memset(&topts, 0, sizeof(struct bpf_test_run_opts)); ··· 1293 1269 } 1294 1270 1295 1271 err = do_prog_test_run(bpf_program__fd(tprog), &retval, 1296 - bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false); 1272 + bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false, 1273 + spec->linear_sz); 1297 1274 if (!err && retval != subspec->retval && subspec->retval != POINTER_VALUE) { 1298 1275 PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval); 1299 1276 goto tobj_cleanup;