forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add tests for bpf_for_each
In this patch - 1) Add a new prog "for_each_helper" which tests the basic functionality of the bpf_for_each helper. 2) Add pyperf600_foreach and strobemeta_foreach to test the performance of using bpf_for_each instead of a for loop The results of pyperf600 and strobemeta are as follows: ~strobemeta~ Baseline verification time 6808200 usec stack depth 496 processed 592132 insns (limit 1000000) max_states_per_insn 14 total_states 16018 peak_states 13684 mark_read 3132 torvalds#188 verif_scale_strobemeta:OK (unrolled loop) Using bpf_for_each verification time 31589 usec stack depth 96+408 processed 1630 insns (limit 1000000) max_states_per_insn 4 total_states 107 peak_states 107 mark_read 60 torvalds#189 verif_scale_strobemeta_foreach:OK ~pyperf600~ Baseline verification time 29702486 usec stack depth 368 processed 626838 insns (limit 1000000) max_states_per_insn 7 total_states 30368 peak_states 30279 mark_read 748 torvalds#182 verif_scale_pyperf600:OK (unrolled loop) Using bpf_for_each verification time 148488 usec stack depth 320+40 processed 10518 insns (limit 1000000) max_states_per_insn 10 total_states 705 peak_states 517 mark_read 38 torvalds#183 verif_scale_pyperf600_foreach:OK Using the bpf_for_each helper led to approximately a 100% decrease in the verification time and in the number of instructions. Signed-off-by: Joanne Koong <[email protected]>
- Loading branch information
1 parent
39b67c7
commit 1b5f8eb
Showing
7 changed files
with
295 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Facebook */ | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct callback_ctx { | ||
int output; | ||
}; | ||
|
||
/* This should be set by the user program */ | ||
u32 nr_iterations; | ||
u32 stop_index = -1; | ||
|
||
/* Making these global variables so that the userspace program | ||
* can verify the output through the skeleton | ||
*/ | ||
int nr_iterations_completed; | ||
int g_output; | ||
int err; | ||
|
||
static int callback_fn(__u32 index, void *data) | ||
{ | ||
struct callback_ctx *ctx = data; | ||
|
||
if (index >= stop_index) | ||
return 1; | ||
|
||
ctx->output += index; | ||
|
||
return 0; | ||
} | ||
|
||
static int empty_callback_fn(__u32 index, void *data) | ||
{ | ||
return 0; | ||
} | ||
|
||
SEC("tc") | ||
int test_prog(struct __sk_buff *skb) | ||
{ | ||
struct callback_ctx data = {}; | ||
|
||
nr_iterations_completed = bpf_for_each(nr_iterations, callback_fn, &data, 0); | ||
|
||
g_output = data.output; | ||
|
||
return 0; | ||
} | ||
|
||
SEC("tc") | ||
int prog_null_ctx(struct __sk_buff *skb) | ||
{ | ||
nr_iterations_completed = bpf_for_each(nr_iterations, empty_callback_fn, NULL, 0); | ||
|
||
return 0; | ||
} | ||
|
||
SEC("tc") | ||
int prog_invalid_flags(struct __sk_buff *skb) | ||
{ | ||
struct callback_ctx data = {}; | ||
|
||
err = bpf_for_each(nr_iterations, callback_fn, &data, 1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2021 Facebook | ||
#define STACK_MAX_LEN 600 | ||
#define USE_FOREACH | ||
#include "pyperf.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
// Copyright (c) 2021 Facebook | ||
|
||
#define STROBE_MAX_INTS 2 | ||
#define STROBE_MAX_STRS 25 | ||
#define STROBE_MAX_MAPS 100 | ||
#define STROBE_MAX_MAP_ENTRIES 20 | ||
#define USE_FOREACH | ||
#include "strobemeta.h" |