Skip to content

Commit b0cd726

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: test passing iterator to a kfunc
Define BPF iterator "getter" kfunc, which accepts iterator pointer as one of the arguments. Make sure that argument passed doesn't have to be the very first argument (unlike new-next-destroy combo). Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent baebe9a commit b0cd726

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ bpf_testmod_test_mod_kfunc(int i)
141141

142142
__bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
143143
{
144-
if (cnt < 0) {
145-
it->cnt = 0;
144+
it->cnt = cnt;
145+
146+
if (cnt < 0)
146147
return -EINVAL;
147-
}
148148

149149
it->value = value;
150-
it->cnt = cnt;
151150

152151
return 0;
153152
}
@@ -162,6 +161,14 @@ __bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
162161
return &it->value;
163162
}
164163

164+
__bpf_kfunc s64 bpf_iter_testmod_seq_value(int val, struct bpf_iter_testmod_seq* it__iter)
165+
{
166+
if (it__iter->cnt < 0)
167+
return 0;
168+
169+
return val + it__iter->value;
170+
}
171+
165172
__bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
166173
{
167174
it->cnt = 0;
@@ -531,6 +538,7 @@ BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
531538
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
532539
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
533540
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
541+
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value)
534542
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
535543
BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
536544
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)

tools/testing/selftests/bpf/progs/iters_testmod_seq.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct bpf_iter_testmod_seq {
1212

1313
extern int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt) __ksym;
1414
extern s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq *it) __ksym;
15+
extern s64 bpf_iter_testmod_seq_value(int blah, struct bpf_iter_testmod_seq *it) __ksym;
1516
extern void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it) __ksym;
1617

1718
const volatile __s64 exp_empty = 0 + 1;
@@ -76,4 +77,53 @@ int testmod_seq_truncated(const void *ctx)
7677
return 0;
7778
}
7879

80+
SEC("?raw_tp")
81+
__failure
82+
__msg("expected an initialized iter_testmod_seq as arg #2")
83+
int testmod_seq_getter_before_bad(const void *ctx)
84+
{
85+
struct bpf_iter_testmod_seq it;
86+
87+
return bpf_iter_testmod_seq_value(0, &it);
88+
}
89+
90+
SEC("?raw_tp")
91+
__failure
92+
__msg("expected an initialized iter_testmod_seq as arg #2")
93+
int testmod_seq_getter_after_bad(const void *ctx)
94+
{
95+
struct bpf_iter_testmod_seq it;
96+
s64 sum = 0, *v;
97+
98+
bpf_iter_testmod_seq_new(&it, 100, 100);
99+
100+
while ((v = bpf_iter_testmod_seq_next(&it))) {
101+
sum += *v;
102+
}
103+
104+
bpf_iter_testmod_seq_destroy(&it);
105+
106+
return sum + bpf_iter_testmod_seq_value(0, &it);
107+
}
108+
109+
SEC("?socket")
110+
__success __retval(1000000)
111+
int testmod_seq_getter_good(const void *ctx)
112+
{
113+
struct bpf_iter_testmod_seq it;
114+
s64 sum = 0, *v;
115+
116+
bpf_iter_testmod_seq_new(&it, 100, 100);
117+
118+
while ((v = bpf_iter_testmod_seq_next(&it))) {
119+
sum += *v;
120+
}
121+
122+
sum *= bpf_iter_testmod_seq_value(0, &it);
123+
124+
bpf_iter_testmod_seq_destroy(&it);
125+
126+
return sum;
127+
}
128+
79129
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)