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
8struct bpf_iter_testmod_seq {
9	u64 :64;
10	u64 :64;
11};
12
13extern int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt) __ksym;
14extern s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq *it) __ksym;
15extern void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it) __ksym;
16
17const volatile __s64 exp_empty = 0 + 1;
18__s64 res_empty;
19
20SEC("raw_tp/sys_enter")
21__success __log_level(2)
22__msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)")
23__msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)")
24__msg("call bpf_iter_testmod_seq_destroy")
25int testmod_seq_empty(const void *ctx)
26{
27	__s64 sum = 0, *i;
28
29	bpf_for_each(testmod_seq, i, 1000, 0) sum += *i;
30	res_empty = 1 + sum;
31
32	return 0;
33}
34
35const volatile __s64 exp_full = 1000000;
36__s64 res_full;
37
38SEC("raw_tp/sys_enter")
39__success __log_level(2)
40__msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)")
41__msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)")
42__msg("call bpf_iter_testmod_seq_destroy")
43int testmod_seq_full(const void *ctx)
44{
45	__s64 sum = 0, *i;
46
47	bpf_for_each(testmod_seq, i, 1000, 1000) sum += *i;
48	res_full = sum;
49
50	return 0;
51}
52
53const volatile __s64 exp_truncated = 10 * 1000000;
54__s64 res_truncated;
55
56static volatile int zero = 0;
57
58SEC("raw_tp/sys_enter")
59__success __log_level(2)
60__msg("fp-16_w=iter_testmod_seq(ref_id=1,state=active,depth=0)")
61__msg("fp-16=iter_testmod_seq(ref_id=1,state=drained,depth=0)")
62__msg("call bpf_iter_testmod_seq_destroy")
63int testmod_seq_truncated(const void *ctx)
64{
65	__s64 sum = 0, *i;
66	int cnt = zero;
67
68	bpf_for_each(testmod_seq, i, 10, 2000000) {
69		sum += *i;
70		cnt++;
71		if (cnt >= 1000000)
72			break;
73	}
74	res_truncated = sum;
75
76	return 0;
77}
78
79char _license[] SEC("license") = "GPL";
80