-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: implement and test custom testmod_seq iterator
Implement a trivial iterator returning same specified integer value N times as part of bpf_testmod kernel module. Add selftests to validate everything works end to end. We also reuse these tests as "verification-only" tests to validate that kernel prints the state of custom kernel module-defined iterator correctly: fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0) "testmod_seq" part is an iterator type, and is coming from module's BTF data dynamically at runtime. Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
- Loading branch information
Showing
5 changed files
with
169 additions
and
1 deletion.
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
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,79 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include "bpf_misc.h" | ||
|
||
struct bpf_iter_testmod_seq { | ||
u64 :64; | ||
u64 :64; | ||
}; | ||
|
||
extern int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt) __ksym; | ||
extern s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq *it) __ksym; | ||
extern void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it) __ksym; | ||
|
||
const volatile __s64 exp_empty = 0 + 1; | ||
__s64 res_empty; | ||
|
||
SEC("raw_tp/sys_enter") | ||
__success __log_level(2) | ||
__msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)") | ||
__msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)") | ||
__msg("call bpf_iter_testmod_seq_destroy") | ||
int testmod_seq_empty(const void *ctx) | ||
{ | ||
__s64 sum = 0, *i; | ||
|
||
bpf_for_each(testmod_seq, i, 1000, 0) sum += *i; | ||
res_empty = 1 + sum; | ||
|
||
return 0; | ||
} | ||
|
||
const volatile __s64 exp_full = 1000000; | ||
__s64 res_full; | ||
|
||
SEC("raw_tp/sys_enter") | ||
__success __log_level(2) | ||
__msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)") | ||
__msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)") | ||
__msg("call bpf_iter_testmod_seq_destroy") | ||
int testmod_seq_full(const void *ctx) | ||
{ | ||
__s64 sum = 0, *i; | ||
|
||
bpf_for_each(testmod_seq, i, 1000, 1000) sum += *i; | ||
res_full = sum; | ||
|
||
return 0; | ||
} | ||
|
||
const volatile __s64 exp_truncated = 10 * 1000000; | ||
__s64 res_truncated; | ||
|
||
static volatile int zero = 0; | ||
|
||
SEC("raw_tp/sys_enter") | ||
__success __log_level(2) | ||
__msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)") | ||
__msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)") | ||
__msg("call bpf_iter_testmod_seq_destroy") | ||
int testmod_seq_truncated(const void *ctx) | ||
{ | ||
__s64 sum = 0, *i; | ||
int cnt = zero; | ||
|
||
bpf_for_each(testmod_seq, i, 10, 2000000) { | ||
sum += *i; | ||
cnt++; | ||
if (cnt >= 1000000) | ||
break; | ||
} | ||
res_truncated = sum; | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |