summaryrefslogtreecommitdiffstats
path: root/examples/bpf/legacy/bpf_map_in_map.c
blob: 575f88128fd187e245adc9302b1c954042061a84 (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
#include "../../../include/bpf_api.h"

#define MAP_INNER_ID	42

struct bpf_elf_map __section_maps map_inner = {
	.type		= BPF_MAP_TYPE_ARRAY,
	.size_key	= sizeof(uint32_t),
	.size_value	= sizeof(uint32_t),
	.id		= MAP_INNER_ID,
	.inner_idx	= 0,
	.pinning	= PIN_GLOBAL_NS,
	.max_elem	= 1,
};

struct bpf_elf_map __section_maps map_outer = {
	.type		= BPF_MAP_TYPE_ARRAY_OF_MAPS,
	.size_key	= sizeof(uint32_t),
	.size_value	= sizeof(uint32_t),
	.inner_id	= MAP_INNER_ID,
	.pinning	= PIN_GLOBAL_NS,
	.max_elem	= 1,
};

__section("egress")
int emain(struct __sk_buff *skb)
{
	struct bpf_elf_map *map_inner;
	int key = 0, *val;

	map_inner = map_lookup_elem(&map_outer, &key);
	if (map_inner) {
		val = map_lookup_elem(map_inner, &key);
		if (val)
			lock_xadd(val, 1);
	}

	return BPF_H_DEFAULT;
}

__section("ingress")
int imain(struct __sk_buff *skb)
{
	struct bpf_elf_map *map_inner;
	int key = 0, *val;

	map_inner = map_lookup_elem(&map_outer, &key);
	if (map_inner) {
		val = map_lookup_elem(map_inner, &key);
		if (val)
			printt("map val: %d\n", *val);
	}

	return BPF_H_DEFAULT;
}

BPF_LICENSE("GPL");