summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/iters_num.c
blob: 7a77a8daee0dcf1ba0e5b707d2ffb22673e9d349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#include <limits.h>
#include <linux/errno.h>
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"

const volatile __s64 exp_empty_zero = 0 + 1;
__s64 res_empty_zero;

SEC("raw_tp/sys_enter")
int num_empty_zero(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, 0, 0) sum += i;
	res_empty_zero = 1 + sum;

	return 0;
}

const volatile __s64 exp_empty_int_min = 0 + 2;
__s64 res_empty_int_min;

SEC("raw_tp/sys_enter")
int num_empty_int_min(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, INT_MIN, INT_MIN) sum += i;
	res_empty_int_min = 2 + sum;

	return 0;
}

const volatile __s64 exp_empty_int_max = 0 + 3;
__s64 res_empty_int_max;

SEC("raw_tp/sys_enter")
int num_empty_int_max(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, INT_MAX, INT_MAX) sum += i;
	res_empty_int_max = 3 + sum;

	return 0;
}

const volatile __s64 exp_empty_minus_one = 0 + 4;
__s64 res_empty_minus_one;

SEC("raw_tp/sys_enter")
int num_empty_minus_one(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, -1, -1) sum += i;
	res_empty_minus_one = 4 + sum;

	return 0;
}

const volatile __s64 exp_simple_sum = 9 * 10 / 2;
__s64 res_simple_sum;

SEC("raw_tp/sys_enter")
int num_simple_sum(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, 0, 10) sum += i;
	res_simple_sum = sum;

	return 0;
}

const volatile __s64 exp_neg_sum = -11 * 10 / 2;
__s64 res_neg_sum;

SEC("raw_tp/sys_enter")
int num_neg_sum(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, -10, 0) sum += i;
	res_neg_sum = sum;

	return 0;
}

const volatile __s64 exp_very_neg_sum = INT_MIN + (__s64)(INT_MIN + 1);
__s64 res_very_neg_sum;

SEC("raw_tp/sys_enter")
int num_very_neg_sum(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, INT_MIN, INT_MIN + 2) sum += i;
	res_very_neg_sum = sum;

	return 0;
}

const volatile __s64 exp_very_big_sum = (__s64)(INT_MAX - 1) + (__s64)(INT_MAX - 2);
__s64 res_very_big_sum;

SEC("raw_tp/sys_enter")
int num_very_big_sum(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, INT_MAX - 2, INT_MAX) sum += i;
	res_very_big_sum = sum;

	return 0;
}

const volatile __s64 exp_neg_pos_sum = -3;
__s64 res_neg_pos_sum;

SEC("raw_tp/sys_enter")
int num_neg_pos_sum(const void *ctx)
{
	__s64 sum = 0, i;

	bpf_for(i, -3, 3) sum += i;
	res_neg_pos_sum = sum;

	return 0;
}

const volatile __s64 exp_invalid_range = -EINVAL;
__s64 res_invalid_range;

SEC("raw_tp/sys_enter")
int num_invalid_range(const void *ctx)
{
	struct bpf_iter_num it;

	res_invalid_range = bpf_iter_num_new(&it, 1, 0);
	bpf_iter_num_destroy(&it);

	return 0;
}

const volatile __s64 exp_max_range = 0 + 10;
__s64 res_max_range;

SEC("raw_tp/sys_enter")
int num_max_range(const void *ctx)
{
	struct bpf_iter_num it;

	res_max_range = 10 + bpf_iter_num_new(&it, 0, BPF_MAX_LOOPS);
	bpf_iter_num_destroy(&it);

	return 0;
}

const volatile __s64 exp_e2big_range = -E2BIG;
__s64 res_e2big_range;

SEC("raw_tp/sys_enter")
int num_e2big_range(const void *ctx)
{
	struct bpf_iter_num it;

	res_e2big_range = bpf_iter_num_new(&it, -1, BPF_MAX_LOOPS);
	bpf_iter_num_destroy(&it);

	return 0;
}

const volatile __s64 exp_succ_elem_cnt = 10;
__s64 res_succ_elem_cnt;

SEC("raw_tp/sys_enter")
int num_succ_elem_cnt(const void *ctx)
{
	struct bpf_iter_num it;
	int cnt = 0, *v;

	bpf_iter_num_new(&it, 0, 10);
	while ((v = bpf_iter_num_next(&it))) {
		cnt++;
	}
	bpf_iter_num_destroy(&it);

	res_succ_elem_cnt = cnt;

	return 0;
}

const volatile __s64 exp_overfetched_elem_cnt = 5;
__s64 res_overfetched_elem_cnt;

SEC("raw_tp/sys_enter")
int num_overfetched_elem_cnt(const void *ctx)
{
	struct bpf_iter_num it;
	int cnt = 0, *v, i;

	bpf_iter_num_new(&it, 0, 5);
	for (i = 0; i < 10; i++) {
		v = bpf_iter_num_next(&it);
		if (v)
			cnt++;
	}
	bpf_iter_num_destroy(&it);

	res_overfetched_elem_cnt = cnt;

	return 0;
}

const volatile __s64 exp_fail_elem_cnt = 20 + 0;
__s64 res_fail_elem_cnt;

SEC("raw_tp/sys_enter")
int num_fail_elem_cnt(const void *ctx)
{
	struct bpf_iter_num it;
	int cnt = 0, *v, i;

	bpf_iter_num_new(&it, 100, 10);
	for (i = 0; i < 10; i++) {
		v = bpf_iter_num_next(&it);
		if (v)
			cnt++;
	}
	bpf_iter_num_destroy(&it);

	res_fail_elem_cnt = 20 + cnt;

	return 0;
}

char _license[] SEC("license") = "GPL";