1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2020 Facebook */
4 #include <bpf/bpf_helpers.h>
7 __uint(type, BPF_MAP_TYPE_ARRAY);
8 __uint(max_entries, 1);
11 } inner_map1 SEC(".maps"),
12 inner_map2 SEC(".maps");
14 struct inner_map_sz2 {
15 __uint(type, BPF_MAP_TYPE_ARRAY);
16 __uint(max_entries, 2);
19 } inner_map_sz2 SEC(".maps");
22 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
23 __uint(max_entries, 3);
26 /* it's possible to use anonymous struct as inner map definition here */
27 __array(values, struct {
28 __uint(type, BPF_MAP_TYPE_ARRAY);
29 /* changing max_entries to 2 will fail during load
30 * due to incompatibility with inner_map definition */
31 __uint(max_entries, 1);
35 } outer_arr SEC(".maps") = {
36 /* (void *) cast is necessary because we didn't use `struct inner_map`
37 * in __inner(values, ...)
38 * Actually, a conscious effort is required to screw up initialization
39 * of inner map slots, which is a great thing!
41 .values = { (void *)&inner_map1, 0, (void *)&inner_map2 },
44 struct inner_map_sz3 {
45 __uint(type, BPF_MAP_TYPE_ARRAY);
46 __uint(map_flags, BPF_F_INNER_MAP);
47 __uint(max_entries, 3);
50 } inner_map3 SEC(".maps"),
51 inner_map4 SEC(".maps");
53 struct inner_map_sz4 {
54 __uint(type, BPF_MAP_TYPE_ARRAY);
55 __uint(map_flags, BPF_F_INNER_MAP);
56 __uint(max_entries, 5);
59 } inner_map5 SEC(".maps");
61 struct outer_arr_dyn {
62 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
63 __uint(max_entries, 3);
66 __array(values, struct {
67 __uint(type, BPF_MAP_TYPE_ARRAY);
68 __uint(map_flags, BPF_F_INNER_MAP);
69 __uint(max_entries, 1);
73 } outer_arr_dyn SEC(".maps") = {
75 [0] = (void *)&inner_map3,
76 [1] = (void *)&inner_map4,
77 [2] = (void *)&inner_map5,
82 __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
83 __uint(max_entries, 5);
85 /* Here everything works flawlessly due to reuse of struct inner_map
86 * and compiler will complain at the attempt to use non-inner_map
87 * references below. This is great experience.
89 __array(values, struct inner_map);
90 } outer_hash SEC(".maps") = {
98 __uint(type, BPF_MAP_TYPE_REUSEPORT_SOCKARRAY);
99 __uint(max_entries, 1);
102 } sockarr_sz1 SEC(".maps");
105 __uint(type, BPF_MAP_TYPE_REUSEPORT_SOCKARRAY);
106 __uint(max_entries, 2);
109 } sockarr_sz2 SEC(".maps");
111 struct outer_sockarr_sz1 {
112 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
113 __uint(max_entries, 1);
116 __array(values, struct sockarr_sz1);
117 } outer_sockarr SEC(".maps") = {
118 .values = { (void *)&sockarr_sz1 },
123 SEC("raw_tp/sys_enter")
124 int handle__sys_enter(void *ctx)
126 struct inner_map *inner_map;
129 inner_map = bpf_map_lookup_elem(&outer_arr, &key);
133 bpf_map_update_elem(inner_map, &key, &val, 0);
135 inner_map = bpf_map_lookup_elem(&outer_hash, &key);
139 bpf_map_update_elem(inner_map, &key, &val, 0);
141 inner_map = bpf_map_lookup_elem(&outer_arr_dyn, &key);
145 bpf_map_update_elem(inner_map, &key, &val, 0);
150 char _license[] SEC("license") = "GPL";