mirror of
https://github.com/torvalds/linux.git
synced 2026-05-27 00:22:00 +02:00
selftests: bpf: introduce __stderr and __stdout
Add __stderr and __stdout to validate the output of BPF streams for bpf selftests. Similar to __xlated, __jited, etc., __stderr/out can be used in the BPF progs to compare a string (regex supported) to the output in the bpf streams. Signed-off-by: Puranjay Mohan <puranjay@kernel.org> Acked-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20250911145808.58042-5-puranjay@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
5c5240d020
commit
744eeb2b27
|
|
@ -35,6 +35,12 @@
|
|||
* inside the brackets.
|
||||
* __msg_unpriv Same as __msg but for unprivileged mode.
|
||||
*
|
||||
* __stderr Message expected to be found in bpf stderr stream. The
|
||||
* same regex rules apply like __msg.
|
||||
* __stderr_unpriv Same as __stderr but for unpriveleged mode.
|
||||
* __stdout Same as __stderr but for stdout stream.
|
||||
* __stdout_unpriv Same as __stdout but for unpriveleged mode.
|
||||
*
|
||||
* __xlated Expect a line in a disassembly log after verifier applies rewrites.
|
||||
* Multiple __xlated attributes could be specified.
|
||||
* Regular expressions could be specified same way as in __msg.
|
||||
|
|
@ -140,6 +146,10 @@
|
|||
#define __caps_unpriv(caps) __attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
|
||||
#define __load_if_JITed() __attribute__((btf_decl_tag("comment:load_mode=jited")))
|
||||
#define __load_if_no_JITed() __attribute__((btf_decl_tag("comment:load_mode=no_jited")))
|
||||
#define __stderr(msg) __attribute__((btf_decl_tag("comment:test_expect_stderr=" XSTR(__COUNTER__) "=" msg)))
|
||||
#define __stderr_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stderr_unpriv=" XSTR(__COUNTER__) "=" msg)))
|
||||
#define __stdout(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg)))
|
||||
#define __stdout_unpriv(msg) __attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg)))
|
||||
|
||||
/* Define common capabilities tested using __caps_unpriv */
|
||||
#define CAP_NET_ADMIN 12
|
||||
|
|
|
|||
|
|
@ -38,6 +38,10 @@
|
|||
#define TEST_TAG_JITED_PFX_UNPRIV "comment:test_jited_unpriv="
|
||||
#define TEST_TAG_CAPS_UNPRIV "comment:test_caps_unpriv="
|
||||
#define TEST_TAG_LOAD_MODE_PFX "comment:load_mode="
|
||||
#define TEST_TAG_EXPECT_STDERR_PFX "comment:test_expect_stderr="
|
||||
#define TEST_TAG_EXPECT_STDERR_PFX_UNPRIV "comment:test_expect_stderr_unpriv="
|
||||
#define TEST_TAG_EXPECT_STDOUT_PFX "comment:test_expect_stdout="
|
||||
#define TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV "comment:test_expect_stdout_unpriv="
|
||||
|
||||
/* Warning: duplicated in bpf_misc.h */
|
||||
#define POINTER_VALUE 0xbadcafe
|
||||
|
|
@ -79,6 +83,8 @@ struct test_subspec {
|
|||
struct expected_msgs expect_msgs;
|
||||
struct expected_msgs expect_xlated;
|
||||
struct expected_msgs jited;
|
||||
struct expected_msgs stderr;
|
||||
struct expected_msgs stdout;
|
||||
int retval;
|
||||
bool execute;
|
||||
__u64 caps;
|
||||
|
|
@ -139,6 +145,10 @@ static void free_test_spec(struct test_spec *spec)
|
|||
free_msgs(&spec->unpriv.expect_xlated);
|
||||
free_msgs(&spec->priv.jited);
|
||||
free_msgs(&spec->unpriv.jited);
|
||||
free_msgs(&spec->unpriv.stderr);
|
||||
free_msgs(&spec->priv.stderr);
|
||||
free_msgs(&spec->unpriv.stdout);
|
||||
free_msgs(&spec->priv.stdout);
|
||||
|
||||
free(spec->priv.name);
|
||||
free(spec->unpriv.name);
|
||||
|
|
@ -407,6 +417,10 @@ static int parse_test_spec(struct test_loader *tester,
|
|||
bool xlated_on_next_line = true;
|
||||
bool unpriv_jit_on_next_line;
|
||||
bool jit_on_next_line;
|
||||
bool stderr_on_next_line = true;
|
||||
bool unpriv_stderr_on_next_line = true;
|
||||
bool stdout_on_next_line = true;
|
||||
bool unpriv_stdout_on_next_line = true;
|
||||
bool collect_jit = false;
|
||||
int func_id, i, err = 0;
|
||||
u32 arch_mask = 0;
|
||||
|
|
@ -598,6 +612,26 @@ static int parse_test_spec(struct test_loader *tester,
|
|||
err = -EINVAL;
|
||||
goto cleanup;
|
||||
}
|
||||
} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDERR_PFX))) {
|
||||
err = push_disasm_msg(msg, &stderr_on_next_line,
|
||||
&spec->priv.stderr);
|
||||
if (err)
|
||||
goto cleanup;
|
||||
} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDERR_PFX_UNPRIV))) {
|
||||
err = push_disasm_msg(msg, &unpriv_stderr_on_next_line,
|
||||
&spec->unpriv.stderr);
|
||||
if (err)
|
||||
goto cleanup;
|
||||
} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDOUT_PFX))) {
|
||||
err = push_disasm_msg(msg, &stdout_on_next_line,
|
||||
&spec->priv.stdout);
|
||||
if (err)
|
||||
goto cleanup;
|
||||
} else if ((msg = skip_dynamic_pfx(s, TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV))) {
|
||||
err = push_disasm_msg(msg, &unpriv_stdout_on_next_line,
|
||||
&spec->unpriv.stdout);
|
||||
if (err)
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -651,6 +685,10 @@ static int parse_test_spec(struct test_loader *tester,
|
|||
clone_msgs(&spec->priv.expect_xlated, &spec->unpriv.expect_xlated);
|
||||
if (spec->unpriv.jited.cnt == 0)
|
||||
clone_msgs(&spec->priv.jited, &spec->unpriv.jited);
|
||||
if (spec->unpriv.stderr.cnt == 0)
|
||||
clone_msgs(&spec->priv.stderr, &spec->unpriv.stderr);
|
||||
if (spec->unpriv.stdout.cnt == 0)
|
||||
clone_msgs(&spec->priv.stdout, &spec->unpriv.stdout);
|
||||
}
|
||||
|
||||
spec->valid = true;
|
||||
|
|
@ -712,6 +750,20 @@ static void emit_jited(const char *jited, bool force)
|
|||
fprintf(stdout, "JITED:\n=============\n%s=============\n", jited);
|
||||
}
|
||||
|
||||
static void emit_stderr(const char *stderr, bool force)
|
||||
{
|
||||
if (!force && env.verbosity == VERBOSE_NONE)
|
||||
return;
|
||||
fprintf(stdout, "STDERR:\n=============\n%s=============\n", stderr);
|
||||
}
|
||||
|
||||
static void emit_stdout(const char *bpf_stdout, bool force)
|
||||
{
|
||||
if (!force && env.verbosity == VERBOSE_NONE)
|
||||
return;
|
||||
fprintf(stdout, "STDOUT:\n=============\n%s=============\n", bpf_stdout);
|
||||
}
|
||||
|
||||
static void validate_msgs(char *log_buf, struct expected_msgs *msgs,
|
||||
void (*emit_fn)(const char *buf, bool force))
|
||||
{
|
||||
|
|
@ -934,6 +986,19 @@ static int get_xlated_program_text(int prog_fd, char *text, size_t text_sz)
|
|||
return err;
|
||||
}
|
||||
|
||||
/* Read the bpf stream corresponding to the stream_id */
|
||||
static int get_stream(int stream_id, int prog_fd, char *text, size_t text_sz)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts);
|
||||
int ret;
|
||||
|
||||
ret = bpf_prog_stream_read(prog_fd, stream_id, text, text_sz, &ropts);
|
||||
ASSERT_GT(ret, 0, "stream read");
|
||||
text[ret] = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* this function is forced noinline and has short generic name to look better
|
||||
* in test_progs output (in case of a failure)
|
||||
*/
|
||||
|
|
@ -1108,6 +1173,31 @@ void run_subtest(struct test_loader *tester,
|
|||
PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval);
|
||||
goto tobj_cleanup;
|
||||
}
|
||||
|
||||
if (subspec->stderr.cnt) {
|
||||
err = get_stream(2, bpf_program__fd(tprog),
|
||||
tester->log_buf, tester->log_buf_sz);
|
||||
if (err <= 0) {
|
||||
PRINT_FAIL("Unexpected retval from get_stream(): %d, errno = %d\n",
|
||||
err, errno);
|
||||
goto tobj_cleanup;
|
||||
}
|
||||
emit_stderr(tester->log_buf, false /*force*/);
|
||||
validate_msgs(tester->log_buf, &subspec->stderr, emit_stderr);
|
||||
}
|
||||
|
||||
if (subspec->stdout.cnt) {
|
||||
err = get_stream(1, bpf_program__fd(tprog),
|
||||
tester->log_buf, tester->log_buf_sz);
|
||||
if (err <= 0) {
|
||||
PRINT_FAIL("Unexpected retval from get_stream(): %d, errno = %d\n",
|
||||
err, errno);
|
||||
goto tobj_cleanup;
|
||||
}
|
||||
emit_stdout(tester->log_buf, false /*force*/);
|
||||
validate_msgs(tester->log_buf, &subspec->stdout, emit_stdout);
|
||||
}
|
||||
|
||||
/* redo bpf_map__attach_struct_ops for each test */
|
||||
while (links_cnt > 0)
|
||||
bpf_link__destroy(links[--links_cnt]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user