aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/bpf/progs/bpf_iter_test_kern4.c
blob: 0aa71b333cf364edcfe6d6e5165d6d9fb0773a5d (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#define bpf_iter_meta bpf_iter_meta___not_used
#define bpf_iter__bpf_map bpf_iter__bpf_map___not_used
#include "vmlinux.h"
#undef bpf_iter_meta
#undef bpf_iter__bpf_map
#include <bpf/bpf_helpers.h>

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

struct bpf_iter_meta {
	struct seq_file *seq;
	__u64 session_id;
	__u64 seq_num;
} __attribute__((preserve_access_index));

struct bpf_iter__bpf_map {
	struct bpf_iter_meta *meta;
	struct bpf_map *map;
} __attribute__((preserve_access_index));

__u32 map1_id = 0, map2_id = 0;
__u32 map1_accessed = 0, map2_accessed = 0;
__u64 map1_seqnum = 0, map2_seqnum1 = 0, map2_seqnum2 = 0;

static volatile const __u32 print_len;
static volatile const __u32 ret1;

SEC("iter/bpf_map")
int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
{
	struct seq_file *seq = ctx->meta->seq;
	struct bpf_map *map = ctx->map;
	__u64 seq_num;
	int i, ret = 0;

	if (map == (void *)0)
		return 0;

	/* only dump map1_id and map2_id */
	if (map->id != map1_id && map->id != map2_id)
		return 0;

	seq_num = ctx->meta->seq_num;
	if (map->id == map1_id) {
		map1_seqnum = seq_num;
		map1_accessed++;
	}

	if (map->id == map2_id) {
		if (map2_accessed == 0) {
			map2_seqnum1 = seq_num;
			if (ret1)
				ret = 1;
		} else {
			map2_seqnum2 = seq_num;
		}
		map2_accessed++;
	}

	/* fill seq_file buffer */
	for (i = 0; i < print_len; i++)
		bpf_seq_write(seq, &seq_num, sizeof(seq_num));

	return ret;
}