Skip to content

Commit 8d45d03

Browse files
pchaignoMartin KaFai Lau
authored andcommitted
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 <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/7ad928ec7591daef4f1b84032aeb86c918b3e5a7.1760037899.git.paul.chaignon@gmail.com
1 parent 838baa3 commit 8d45d03

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

tools/testing/selftests/bpf/progs/bpf_misc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
* Several __arch_* annotations could be specified at once.
127127
* When test case is not run on current arch it is marked as skipped.
128128
* __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.
129132
*/
130133
#define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg)))
131134
#define __not_msg(msg) __attribute__((btf_decl_tag("comment:test_expect_not_msg=" XSTR(__COUNTER__) "=" msg)))
@@ -159,6 +162,7 @@
159162
#define __stderr_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stderr_unpriv=" XSTR(__COUNTER__) "=" msg)))
160163
#define __stdout(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg)))
161164
#define __stdout_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg)))
165+
#define __linear_size(sz) __attribute__((btf_decl_tag("comment:test_linear_size=" XSTR(sz))))
162166

163167
/* Define common capabilities tested using __caps_unpriv */
164168
#define CAP_NET_ADMIN 12

tools/testing/selftests/bpf/progs/verifier_direct_packet_access.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Converted from tools/testing/selftests/bpf/verifier/direct_packet_access.c */
33

4+
#include <linux/if_ether.h>
45
#include <linux/bpf.h>
56
#include <bpf/bpf_helpers.h>
67
#include "bpf_misc.h"

tools/testing/selftests/bpf/test_loader.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define TEST_TAG_EXPECT_STDERR_PFX_UNPRIV "comment:test_expect_stderr_unpriv="
4444
#define TEST_TAG_EXPECT_STDOUT_PFX "comment:test_expect_stdout="
4545
#define TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV "comment:test_expect_stdout_unpriv="
46+
#define TEST_TAG_LINEAR_SIZE "comment:test_linear_size="
4647

4748
/* Warning: duplicated in bpf_misc.h */
4849
#define POINTER_VALUE 0xbadcafe
@@ -89,6 +90,7 @@ struct test_spec {
8990
int mode_mask;
9091
int arch_mask;
9192
int load_mask;
93+
int linear_sz;
9294
bool auxiliary;
9395
bool valid;
9496
};
@@ -633,6 +635,21 @@ static int parse_test_spec(struct test_loader *tester,
633635
&spec->unpriv.stdout);
634636
if (err)
635637
goto cleanup;
638+
} else if (str_has_pfx(s, TEST_TAG_LINEAR_SIZE)) {
639+
switch (bpf_program__type(prog)) {
640+
case BPF_PROG_TYPE_SCHED_ACT:
641+
case BPF_PROG_TYPE_SCHED_CLS:
642+
case BPF_PROG_TYPE_CGROUP_SKB:
643+
val = s + sizeof(TEST_TAG_LINEAR_SIZE) - 1;
644+
err = parse_int(val, &spec->linear_sz, "test linear size");
645+
if (err)
646+
goto cleanup;
647+
break;
648+
default:
649+
PRINT_FAIL("__linear_size for unsupported program type");
650+
err = -EINVAL;
651+
goto cleanup;
652+
}
636653
}
637654
}
638655

@@ -1007,10 +1024,11 @@ static bool is_unpriv_capable_map(struct bpf_map *map)
10071024
}
10081025
}
10091026

1010-
static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts)
1027+
static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts, int linear_sz)
10111028
{
10121029
__u8 tmp_out[TEST_DATA_LEN << 2] = {};
10131030
__u8 tmp_in[TEST_DATA_LEN] = {};
1031+
struct __sk_buff ctx = {};
10141032
int err, saved_errno;
10151033
LIBBPF_OPTS(bpf_test_run_opts, topts,
10161034
.data_in = tmp_in,
@@ -1020,6 +1038,12 @@ static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts)
10201038
.repeat = 1,
10211039
);
10221040

1041+
if (linear_sz) {
1042+
ctx.data_end = linear_sz;
1043+
topts.ctx_in = &ctx;
1044+
topts.ctx_size_in = sizeof(ctx);
1045+
}
1046+
10231047
if (empty_opts) {
10241048
memset(&topts, 0, sizeof(struct bpf_test_run_opts));
10251049
topts.sz = sizeof(struct bpf_test_run_opts);
@@ -1269,7 +1293,8 @@ void run_subtest(struct test_loader *tester,
12691293
}
12701294

12711295
err = do_prog_test_run(bpf_program__fd(tprog), &retval,
1272-
bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false);
1296+
bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false,
1297+
spec->linear_sz);
12731298
if (!err && retval != subspec->retval && subspec->retval != POINTER_VALUE) {
12741299
PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval);
12751300
goto tobj_cleanup;

0 commit comments

Comments
 (0)