summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/iters_testmod_seq.c
blob: 6543d5b6e0a976b7af2d723177698f15517643d7 (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
// 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 s64 bpf_iter_testmod_seq_value(int blah, 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;
}

SEC("?raw_tp")
__failure
__msg("expected an initialized iter_testmod_seq as arg #1")
int testmod_seq_getter_before_bad(const void *ctx)
{
	struct bpf_iter_testmod_seq it;

	return bpf_iter_testmod_seq_value(0, &it);
}

SEC("?raw_tp")
__failure
__msg("expected an initialized iter_testmod_seq as arg #1")
int testmod_seq_getter_after_bad(const void *ctx)
{
	struct bpf_iter_testmod_seq it;
	s64 sum = 0, *v;

	bpf_iter_testmod_seq_new(&it, 100, 100);

	while ((v = bpf_iter_testmod_seq_next(&it))) {
		sum += *v;
	}

	bpf_iter_testmod_seq_destroy(&it);

	return sum + bpf_iter_testmod_seq_value(0, &it);
}

SEC("?socket")
__success __retval(1000000)
int testmod_seq_getter_good(const void *ctx)
{
	struct bpf_iter_testmod_seq it;
	s64 sum = 0, *v;

	bpf_iter_testmod_seq_new(&it, 100, 100);

	while ((v = bpf_iter_testmod_seq_next(&it))) {
		sum += *v;
	}

	sum *= bpf_iter_testmod_seq_value(0, &it);

	bpf_iter_testmod_seq_destroy(&it);

	return sum;
}

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