Debian Codegraph @

examples/map_in_map/main.go

raw symbols SCIP
1 //go:build linux
2
3 // An example of using maps within maps. This example demonstrates a few
4 // features. Firstly, creating eBPF map specifications in pure Go
5 // (typically you'd see them being generated from a loaded ELF).
6 // Additionally, creating maps and placing them in other maps (with
7 // dynamically sized inner maps).
8 package main
9
10 import (
11 "log"
12 "math/rand"
13 "time"
14
15 "github.com/cilium/ebpf"
16 "github.com/cilium/ebpf/rlimit"
17 )
18
19 const BPF_F_INNER_MAP = 0x1000
20
21 func main() {
22 // Allow the current process to lock memory for eBPF resources.
23 if err := rlimit.RemoveMemlock(); err != nil {
24 log.Fatal(err)
25 }
26
27 // We're creating a map spec in pure Go here, but a map spec like
28 // this can be loaded from an ELF too.
29 outerMapSpec := ebpf.MapSpec{
30 Name: "outer_map",
31 Type: ebpf.ArrayOfMaps,
32 KeySize: 4, // 4 bytes for u32
33 ValueSize: 4,
34 MaxEntries: 5, // We'll have 5 maps inside this map
35 Contents: make([]ebpf.MapKV, 5),
36 InnerMap: &ebpf.MapSpec{
37 Name: "inner_map",
38 Type: ebpf.Array,
39 KeySize: 4, // 4 bytes for u32
40 ValueSize: 4, // 4 bytes for u32
41
42 // This flag is required for dynamically sized inner maps.
43 // Added in linux 5.10.
44 Flags: BPF_F_INNER_MAP,
45
46 // We set this to 1 now, but this inner map spec gets copied
47 // and altered later.
48 MaxEntries: 1,
49 },
50 }
51
52 r := rand.New(rand.NewSource(time.Now().UnixNano()))
53
54 // For each entry we want to create in the outer map...
55 for i := uint32(0); i < outerMapSpec.MaxEntries; i++ {
56 // Copy the inner map spec
57 innerMapSpec := outerMapSpec.InnerMap.Copy()
58
59 // Randomly generate inner map length
60 innerMapSpec.MaxEntries = uint32(r.Intn(50) + 1) // Can't be zero.
61
62 // populate the inner map contents
63 innerMapSpec.Contents = make([]ebpf.MapKV, innerMapSpec.MaxEntries)
64
65 for j := range innerMapSpec.Contents {
66 innerMapSpec.Contents[uint32(j)] = ebpf.MapKV{Key: uint32(j), Value: uint32(0xCAFE)}
67 }
68
69 // Create the inner map
70 innerMap, err := ebpf.NewMap(innerMapSpec)
71 if err != nil {
72 log.Fatalf("inner_map: %v", err)
73 }
74 // In this example we close all references to maps before exit.
75 // But typically you may actually want to hold on to the map
76 // reference so that you control the lifecycle of the map. For
77 // the inner (nested) map though, it's safe to close the file
78 // descriptor in userspace once the outer map holds a reference
79 // in the kernel.
80 defer innerMap.Close()
81
82 // Inner map is created successfully and lives in the kernel,
83 // let's add it to the contents of the outer map spec.
84 outerMapSpec.Contents[i] = ebpf.MapKV{Key: i, Value: innerMap}
85 }
86
87 // All inner maps are created and inserted into the outer map spec,
88 // time to create the outer map.
89 outerMap, err := ebpf.NewMap(&outerMapSpec)
90 if err != nil {
91 log.Fatalf("outer_map: %v", err)
92 }
93 defer outerMap.Close()
94
95 // The outer map is created successfully and lives happily in the
96 // kernel. Let's iterate over the map in the kernel to see what's
97 // been made.
98 mapIter := outerMap.Iterate()
99 var outerMapKey uint32
100 var innerMapID ebpf.MapID
101 for mapIter.Next(&outerMapKey, &innerMapID) {
102 // With maps that contain maps, performing a lookup doesn't give
103 // you the map directly, instead it gives you an ID, which you
104 // can then use to get a full map pointer.
105 innerMap, err := ebpf.NewMapFromID(innerMapID)
106 if err != nil {
107 log.Fatal(err)
108 }
109
110 innerMapInfo, err := innerMap.Info()
111 if err != nil {
112 log.Fatal(err)
113 }
114
115 log.Printf("outerMapKey %d, innerMap.Info: %+v", outerMapKey, innerMapInfo)
116 }
117 }