Skip to content

Commit 2c8f6c9

Browse files
pchaignoKernel Patches Daemon
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]>
1 parent 719d87f commit 2c8f6c9

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-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
@@ -119,6 +119,9 @@
119119
* Several __arch_* annotations could be specified at once.
120120
* When test case is not run on current arch it is marked as skipped.
121121
* __caps_unpriv Specify the capabilities that should be set when running the test.
122+
*
123+
* __linear_size Specify the size of the linear area of non-linear skbs, or
124+
* 0 for linear skbs.
122125
*/
123126
#define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg)))
124127
#define __xlated(msg) __attribute__((btf_decl_tag("comment:test_expect_xlated=" XSTR(__COUNTER__) "=" msg)))
@@ -150,6 +153,7 @@
150153
#define __stderr_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stderr_unpriv=" XSTR(__COUNTER__) "=" msg)))
151154
#define __stdout(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg)))
152155
#define __stdout_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg)))
156+
#define __linear_size(sz) __attribute__((btf_decl_tag("comment:test_linear_size="XSTR(sz))))
153157

154158
/* Define common capabilities tested using __caps_unpriv */
155159
#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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define TEST_TAG_EXPECT_STDERR_PFX_UNPRIV "comment:test_expect_stderr_unpriv="
4343
#define TEST_TAG_EXPECT_STDOUT_PFX "comment:test_expect_stdout="
4444
#define TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV "comment:test_expect_stdout_unpriv="
45+
#define TEST_TAG_LINEAR_SIZE "comment:test_linear_size="
4546

4647
/* Warning: duplicated in bpf_misc.h */
4748
#define POINTER_VALUE 0xbadcafe
@@ -100,6 +101,7 @@ struct test_spec {
100101
int mode_mask;
101102
int arch_mask;
102103
int load_mask;
104+
int linear_sz;
103105
bool auxiliary;
104106
bool valid;
105107
};
@@ -632,6 +634,11 @@ static int parse_test_spec(struct test_loader *tester,
632634
&spec->unpriv.stdout);
633635
if (err)
634636
goto cleanup;
637+
} else if (str_has_pfx(s, TEST_TAG_LINEAR_SIZE)) {
638+
val = s + sizeof(TEST_TAG_LINEAR_SIZE) - 1;
639+
err = parse_int(val, &spec->linear_sz, "test linear size");
640+
if (err)
641+
goto cleanup;
635642
}
636643
}
637644

@@ -906,10 +913,11 @@ static bool is_unpriv_capable_map(struct bpf_map *map)
906913
}
907914
}
908915

909-
static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts)
916+
static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts, int linear_sz)
910917
{
911918
__u8 tmp_out[TEST_DATA_LEN << 2] = {};
912919
__u8 tmp_in[TEST_DATA_LEN] = {};
920+
struct __sk_buff ctx = {};
913921
int err, saved_errno;
914922
LIBBPF_OPTS(bpf_test_run_opts, topts,
915923
.data_in = tmp_in,
@@ -919,6 +927,13 @@ static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts)
919927
.repeat = 1,
920928
);
921929

930+
if (linear_sz) {
931+
topts.flags = BPF_F_TEST_SKB_NON_LINEAR;
932+
ctx.data_end = linear_sz;
933+
topts.ctx_in = &ctx;
934+
topts.ctx_size_in = sizeof(ctx);
935+
}
936+
922937
if (empty_opts) {
923938
memset(&topts, 0, sizeof(struct bpf_test_run_opts));
924939
topts.sz = sizeof(struct bpf_test_run_opts);
@@ -1168,7 +1183,8 @@ void run_subtest(struct test_loader *tester,
11681183
}
11691184

11701185
err = do_prog_test_run(bpf_program__fd(tprog), &retval,
1171-
bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false);
1186+
bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false,
1187+
spec->linear_sz);
11721188
if (!err && retval != subspec->retval && subspec->retval != POINTER_VALUE) {
11731189
PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval);
11741190
goto tobj_cleanup;

0 commit comments

Comments
 (0)