Skip to content

Commit 329e38f

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftest/bpf: centralize libbpf logging management for test_progs
Make test_progs test runner own libbpf logging. Also introduce two levels of verbosity: -v and -vv. First one will be used in subsequent patches to enable test log output always. Second one increases verbosity level of libbpf logging further to include debug output as well. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e87fd8b commit 329e38f

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ void test_bpf_verif_scale(void)
7070
const char *cg_sysctl[] = {
7171
"./test_sysctl_loop1.o", "./test_sysctl_loop2.o",
7272
};
73+
libbpf_print_fn_t old_print_fn = NULL;
7374
int err, i;
7475

7576
if (verifier_stats)
76-
libbpf_set_print(libbpf_debug_print);
77+
old_print_fn = libbpf_set_print(libbpf_debug_print);
7778

7879
err = check_load("./loop3.o", BPF_PROG_TYPE_RAW_TRACEPOINT);
7980
printf("test_scale:loop3:%s\n", err ? (error_cnt--, "OK") : "FAIL");
@@ -97,4 +98,7 @@ void test_bpf_verif_scale(void)
9798

9899
err = check_load("./test_seg6_loop.o", BPF_PROG_TYPE_LWT_SEG6LOCAL);
99100
printf("test_scale:test_seg6_loop:%s\n", err ? "FAIL" : "OK");
101+
102+
if (verifier_stats)
103+
libbpf_set_print(old_print_fn);
100104
}

tools/testing/selftests/bpf/prog_tests/reference_tracking.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <test_progs.h>
33

4-
static int libbpf_debug_print(enum libbpf_print_level level,
5-
const char *format, va_list args)
6-
{
7-
if (level == LIBBPF_DEBUG)
8-
return 0;
9-
10-
return vfprintf(stderr, format, args);
11-
}
12-
134
void test_reference_tracking(void)
145
{
156
const char *file = "./test_sk_lookup_kern.o";
@@ -36,9 +27,11 @@ void test_reference_tracking(void)
3627

3728
/* Expect verifier failure if test name has 'fail' */
3829
if (strstr(title, "fail") != NULL) {
39-
libbpf_set_print(NULL);
30+
libbpf_print_fn_t old_print_fn;
31+
32+
old_print_fn = libbpf_set_print(NULL);
4033
err = !bpf_program__load(prog, "GPL", 0);
41-
libbpf_set_print(libbpf_debug_print);
34+
libbpf_set_print(old_print_fn);
4235
} else {
4336
err = bpf_program__load(prog, "GPL", 0);
4437
}

tools/testing/selftests/bpf/test_progs.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ enum ARG_KEYS {
186186
ARG_TEST_NUM = 'n',
187187
ARG_TEST_NAME = 't',
188188
ARG_VERIFIER_STATS = 's',
189+
190+
ARG_VERBOSE = 'v',
189191
};
190192

191193
static const struct argp_option opts[] = {
@@ -195,19 +197,31 @@ static const struct argp_option opts[] = {
195197
"Run tests with names containing NAME" },
196198
{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
197199
"Output verifier statistics", },
200+
{ "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
201+
"Verbose output (use -vv for extra verbose output)" },
198202
{},
199203
};
200204

201205
struct test_env {
202206
int test_num_selector;
203207
const char *test_name_selector;
204208
bool verifier_stats;
209+
bool verbose;
210+
bool very_verbose;
205211
};
206212

207213
static struct test_env env = {
208214
.test_num_selector = -1,
209215
};
210216

217+
static int libbpf_print_fn(enum libbpf_print_level level,
218+
const char *format, va_list args)
219+
{
220+
if (!env.very_verbose && level == LIBBPF_DEBUG)
221+
return 0;
222+
return vfprintf(stderr, format, args);
223+
}
224+
211225
static error_t parse_arg(int key, char *arg, struct argp_state *state)
212226
{
213227
struct test_env *env = state->input;
@@ -229,6 +243,19 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
229243
case ARG_VERIFIER_STATS:
230244
env->verifier_stats = true;
231245
break;
246+
case ARG_VERBOSE:
247+
if (arg) {
248+
if (strcmp(arg, "v") == 0) {
249+
env->very_verbose = true;
250+
} else {
251+
fprintf(stderr,
252+
"Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
253+
arg);
254+
return -EINVAL;
255+
}
256+
}
257+
env->verbose = true;
258+
break;
232259
case ARGP_KEY_ARG:
233260
argp_usage(state);
234261
break;
@@ -255,6 +282,8 @@ int main(int argc, char **argv)
255282
if (err)
256283
return err;
257284

285+
libbpf_set_print(libbpf_print_fn);
286+
258287
srand(time(NULL));
259288

260289
jit_enabled = is_jit_enabled();

0 commit comments

Comments
 (0)