Skip to content

Commit f5281aa

Browse files
ThinkerYzu1Martin KaFai Lau
authored andcommitted
selftests/bpf: Add the traffic monitor option to test_progs.
Add option '-m' to test_progs to accept names and patterns of test cases. This option will be used later to enable traffic monitor that capture network packets generated by test cases. Acked-by: Stanislav Fomichev <[email protected]> Signed-off-by: Kui-Feng Lee <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent f52403b commit f5281aa

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ struct prog_test_def {
155155
void (*run_serial_test)(void);
156156
bool should_run;
157157
bool need_cgroup_cleanup;
158+
bool should_tmon;
158159
};
159160

160161
/* Override C runtime library's usleep() implementation to ensure nanosleep()
@@ -192,46 +193,59 @@ static bool should_run(struct test_selector *sel, int num, const char *name)
192193
return num < sel->num_set_len && sel->num_set[num];
193194
}
194195

195-
static bool should_run_subtest(struct test_selector *sel,
196-
struct test_selector *subtest_sel,
197-
int subtest_num,
198-
const char *test_name,
199-
const char *subtest_name)
196+
static bool match_subtest(struct test_filter_set *filter,
197+
const char *test_name,
198+
const char *subtest_name)
200199
{
201200
int i, j;
202201

203-
for (i = 0; i < sel->blacklist.cnt; i++) {
204-
if (glob_match(test_name, sel->blacklist.tests[i].name)) {
205-
if (!sel->blacklist.tests[i].subtest_cnt)
206-
return false;
207-
208-
for (j = 0; j < sel->blacklist.tests[i].subtest_cnt; j++) {
209-
if (glob_match(subtest_name,
210-
sel->blacklist.tests[i].subtests[j]))
211-
return false;
212-
}
213-
}
214-
}
215-
216-
for (i = 0; i < sel->whitelist.cnt; i++) {
217-
if (glob_match(test_name, sel->whitelist.tests[i].name)) {
218-
if (!sel->whitelist.tests[i].subtest_cnt)
202+
for (i = 0; i < filter->cnt; i++) {
203+
if (glob_match(test_name, filter->tests[i].name)) {
204+
if (!filter->tests[i].subtest_cnt)
219205
return true;
220206

221-
for (j = 0; j < sel->whitelist.tests[i].subtest_cnt; j++) {
207+
for (j = 0; j < filter->tests[i].subtest_cnt; j++) {
222208
if (glob_match(subtest_name,
223-
sel->whitelist.tests[i].subtests[j]))
209+
filter->tests[i].subtests[j]))
224210
return true;
225211
}
226212
}
227213
}
228214

215+
return false;
216+
}
217+
218+
static bool should_run_subtest(struct test_selector *sel,
219+
struct test_selector *subtest_sel,
220+
int subtest_num,
221+
const char *test_name,
222+
const char *subtest_name)
223+
{
224+
if (match_subtest(&sel->blacklist, test_name, subtest_name))
225+
return false;
226+
227+
if (match_subtest(&sel->whitelist, test_name, subtest_name))
228+
return true;
229+
229230
if (!sel->whitelist.cnt && !subtest_sel->num_set)
230231
return true;
231232

232233
return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num];
233234
}
234235

236+
static bool should_tmon(struct test_selector *sel, const char *name)
237+
{
238+
int i;
239+
240+
for (i = 0; i < sel->whitelist.cnt; i++) {
241+
if (glob_match(name, sel->whitelist.tests[i].name) &&
242+
!sel->whitelist.tests[i].subtest_cnt)
243+
return true;
244+
}
245+
246+
return false;
247+
}
248+
235249
static char *test_result(bool failed, bool skipped)
236250
{
237251
return failed ? "FAIL" : (skipped ? "SKIP" : "OK");
@@ -488,6 +502,10 @@ bool test__start_subtest(const char *subtest_name)
488502
return false;
489503
}
490504

505+
subtest_state->should_tmon = match_subtest(&env.tmon_selector.whitelist,
506+
test->test_name,
507+
subtest_name);
508+
491509
env.subtest_state = subtest_state;
492510
stdio_hijack_init(&subtest_state->log_buf, &subtest_state->log_cnt);
493511

@@ -667,7 +685,8 @@ enum ARG_KEYS {
667685
ARG_TEST_NAME_GLOB_DENYLIST = 'd',
668686
ARG_NUM_WORKERS = 'j',
669687
ARG_DEBUG = -1,
670-
ARG_JSON_SUMMARY = 'J'
688+
ARG_JSON_SUMMARY = 'J',
689+
ARG_TRAFFIC_MONITOR = 'm',
671690
};
672691

673692
static const struct argp_option opts[] = {
@@ -694,6 +713,10 @@ static const struct argp_option opts[] = {
694713
{ "debug", ARG_DEBUG, NULL, 0,
695714
"print extra debug information for test_progs." },
696715
{ "json-summary", ARG_JSON_SUMMARY, "FILE", 0, "Write report in json format to this file."},
716+
#ifdef TRAFFIC_MONITOR
717+
{ "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0,
718+
"Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." },
719+
#endif
697720
{},
698721
};
699722

@@ -905,6 +928,18 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
905928
break;
906929
case ARGP_KEY_END:
907930
break;
931+
#ifdef TRAFFIC_MONITOR
932+
case ARG_TRAFFIC_MONITOR:
933+
if (arg[0] == '@')
934+
err = parse_test_list_file(arg + 1,
935+
&env->tmon_selector.whitelist,
936+
true);
937+
else
938+
err = parse_test_list(arg,
939+
&env->tmon_selector.whitelist,
940+
true);
941+
break;
942+
#endif
908943
default:
909944
return ARGP_ERR_UNKNOWN;
910945
}
@@ -1736,6 +1771,8 @@ int main(int argc, char **argv)
17361771
test->test_num, test->test_name, test->test_name, test->test_name);
17371772
exit(EXIT_ERR_SETUP_INFRA);
17381773
}
1774+
if (test->should_run)
1775+
test->should_tmon = should_tmon(&env.tmon_selector, test->test_name);
17391776
}
17401777

17411778
/* ignore workers if we are just listing */
@@ -1820,6 +1857,7 @@ int main(int argc, char **argv)
18201857

18211858
free_test_selector(&env.test_selector);
18221859
free_test_selector(&env.subtest_selector);
1860+
free_test_selector(&env.tmon_selector);
18231861
free_test_states();
18241862

18251863
if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)

tools/testing/selftests/bpf/test_progs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct subtest_state {
7474
int error_cnt;
7575
bool skipped;
7676
bool filtered;
77+
bool should_tmon;
7778

7879
FILE *stdout_saved;
7980
};
@@ -98,6 +99,7 @@ struct test_state {
9899
struct test_env {
99100
struct test_selector test_selector;
100101
struct test_selector subtest_selector;
102+
struct test_selector tmon_selector;
101103
bool verifier_stats;
102104
bool debug;
103105
enum verbosity verbosity;

0 commit comments

Comments
 (0)