Debian Codegraph @

examples/bpf/bpf_map_in_map.c

raw
1 #include "../../include/bpf_api.h"
2
3 struct inner_map {
4 __uint(type, BPF_MAP_TYPE_ARRAY);
5 __uint(key_size, sizeof(uint32_t));
6 __uint(value_size, sizeof(uint32_t));
7 __uint(max_entries, 1);
8 } map_inner __section(".maps");
9
10 struct {
11 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
12 __uint(key_size, sizeof(uint32_t));
13 __uint(value_size, sizeof(uint32_t));
14 __uint(max_entries, 1);
15 __uint(pinning, LIBBPF_PIN_BY_NAME);
16 __array(values, struct inner_map);
17 } map_outer __section(".maps") = {
18 .values = {
19 [0] = &map_inner,
20 },
21 };
22
23 __section("egress")
24 int emain(struct __sk_buff *skb)
25 {
26 struct bpf_elf_map *map_inner;
27 int key = 0, *val;
28
29 map_inner = map_lookup_elem(&map_outer, &key);
30 if (map_inner) {
31 val = map_lookup_elem(map_inner, &key);
32 if (val)
33 lock_xadd(val, 1);
34 }
35
36 return BPF_H_DEFAULT;
37 }
38
39 __section("ingress")
40 int imain(struct __sk_buff *skb)
41 {
42 struct bpf_elf_map *map_inner;
43 int key = 0, *val;
44
45 map_inner = map_lookup_elem(&map_outer, &key);
46 if (map_inner) {
47 val = map_lookup_elem(map_inner, &key);
48 if (val)
49 printt("map val: %d\n", *val);
50 }
51
52 return BPF_H_DEFAULT;
53 }
54
55 BPF_LICENSE("GPL");