xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_test_kern4.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #define bpf_iter_meta bpf_iter_meta___not_used
4 #define bpf_iter__bpf_map bpf_iter__bpf_map___not_used
5 #include "vmlinux.h"
6 #undef bpf_iter_meta
7 #undef bpf_iter__bpf_map
8 #include <bpf/bpf_helpers.h>
9 
10 char _license[] SEC("license") = "GPL";
11 
12 struct bpf_iter_meta {
13 	struct seq_file *seq;
14 	__u64 session_id;
15 	__u64 seq_num;
16 } __attribute__((preserve_access_index));
17 
18 struct bpf_iter__bpf_map {
19 	struct bpf_iter_meta *meta;
20 	struct bpf_map *map;
21 } __attribute__((preserve_access_index));
22 
23 __u32 map1_id = 0, map2_id = 0;
24 __u32 map1_accessed = 0, map2_accessed = 0;
25 __u64 map1_seqnum = 0, map2_seqnum1 = 0, map2_seqnum2 = 0;
26 
27 static volatile const __u32 print_len;
28 static volatile const __u32 ret1;
29 
30 SEC("iter/bpf_map")
31 int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
32 {
33 	struct seq_file *seq = ctx->meta->seq;
34 	struct bpf_map *map = ctx->map;
35 	__u64 seq_num;
36 	int i, ret = 0;
37 
38 	if (map == (void *)0)
39 		return 0;
40 
41 	/* only dump map1_id and map2_id */
42 	if (map->id != map1_id && map->id != map2_id)
43 		return 0;
44 
45 	seq_num = ctx->meta->seq_num;
46 	if (map->id == map1_id) {
47 		map1_seqnum = seq_num;
48 		map1_accessed++;
49 	}
50 
51 	if (map->id == map2_id) {
52 		if (map2_accessed == 0) {
53 			map2_seqnum1 = seq_num;
54 			if (ret1)
55 				ret = 1;
56 		} else {
57 			map2_seqnum2 = seq_num;
58 		}
59 		map2_accessed++;
60 	}
61 
62 	/* fill seq_file buffer */
63 	for (i = 0; i < print_len; i++)
64 		bpf_seq_write(seq, &seq_num, sizeof(seq_num));
65 
66 	return ret;
67 }
68