1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7
8 struct bpf_iter_testmod_seq {
9 u64 :64;
10 u64 :64;
11 };
12
13 extern int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt) __ksym;
14 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;
16 extern void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it) __ksym;
17
18 const volatile __s64 exp_empty = 0 + 1;
19 __s64 res_empty;
20
21 SEC("raw_tp/sys_enter")
22 __success __log_level(2)
23 __msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)")
24 __msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)")
25 __msg("call bpf_iter_testmod_seq_destroy")
testmod_seq_empty(const void * ctx)26 int testmod_seq_empty(const void *ctx)
27 {
28 __s64 sum = 0, *i;
29
30 bpf_for_each(testmod_seq, i, 1000, 0) sum += *i;
31 res_empty = 1 + sum;
32
33 return 0;
34 }
35
36 const volatile __s64 exp_full = 1000000;
37 __s64 res_full;
38
39 SEC("raw_tp/sys_enter")
40 __success __log_level(2)
41 __msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)")
42 __msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)")
43 __msg("call bpf_iter_testmod_seq_destroy")
testmod_seq_full(const void * ctx)44 int testmod_seq_full(const void *ctx)
45 {
46 __s64 sum = 0, *i;
47
48 bpf_for_each(testmod_seq, i, 1000, 1000) sum += *i;
49 res_full = sum;
50
51 return 0;
52 }
53
54 const volatile __s64 exp_truncated = 10 * 1000000;
55 __s64 res_truncated;
56
57 static volatile int zero = 0;
58
59 SEC("raw_tp/sys_enter")
60 __success __log_level(2)
61 __msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)")
62 __msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)")
63 __msg("call bpf_iter_testmod_seq_destroy")
testmod_seq_truncated(const void * ctx)64 int testmod_seq_truncated(const void *ctx)
65 {
66 __s64 sum = 0, *i;
67 int cnt = zero;
68
69 bpf_for_each(testmod_seq, i, 10, 2000000) {
70 sum += *i;
71 cnt++;
72 if (cnt >= 1000000)
73 break;
74 }
75 res_truncated = sum;
76
77 return 0;
78 }
79
80 SEC("?raw_tp")
81 __failure
82 __msg("expected an initialized iter_testmod_seq as arg #2")
testmod_seq_getter_before_bad(const void * ctx)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")
testmod_seq_getter_after_bad(const void * ctx)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)
testmod_seq_getter_good(const void * ctx)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
129 char _license[] SEC("license") = "GPL";
130