-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
map.go
204 lines (160 loc) · 4.32 KB
/
map.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ebpf
import (
"errors"
"fmt"
"os"
"path/filepath"
ciliumebpf "github.com/cilium/ebpf"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/lock"
)
type MapSpec = ciliumebpf.MapSpec
const (
Hash = ciliumebpf.Hash
PerCPUHash = ciliumebpf.PerCPUHash
Array = ciliumebpf.Array
HashOfMaps = ciliumebpf.HashOfMaps
LPMTrie = ciliumebpf.LPMTrie
PinByName = ciliumebpf.PinByName
)
var (
ErrKeyNotExist = ciliumebpf.ErrKeyNotExist
)
// IterateCallback represents the signature of the callback function expected by
// the IterateWithCallback method, which in turn is used to iterate all the
// keys/values of a map.
type IterateCallback func(key, value interface{})
// Map represents an eBPF map.
type Map struct {
lock lock.RWMutex
*ciliumebpf.Map
spec *MapSpec
path string
}
// NewMap creates a new Map object.
func NewMap(spec *MapSpec) *Map {
return &Map{
spec: spec,
}
}
// LoadRegisterMap loads the specified map from a bpffs pin path and registers
// its handle in the package-global map register.
func LoadRegisterMap(mapName string) (*Map, error) {
path := bpf.MapPath(mapName)
m, err := LoadPinnedMap(path)
if err != nil {
return nil, err
}
registerMap(m)
return m, nil
}
// LoadPinnedMap wraps cilium/ebpf's LoadPinnedMap.
func LoadPinnedMap(fileName string) (*Map, error) {
m, err := ciliumebpf.LoadPinnedMap(fileName, nil)
if err != nil {
return nil, err
}
return &Map{
Map: m,
path: fileName,
}, nil
}
func MapFromID(id int) (*Map, error) {
newMap, err := ciliumebpf.NewMapFromID(ciliumebpf.MapID(id))
if err != nil {
return nil, err
}
return &Map{
Map: newMap,
}, nil
}
// OpenOrCreate tries to open or create the eBPF map identified by the spec in
// the Map object.
func (m *Map) OpenOrCreate() error {
m.lock.Lock()
defer m.lock.Unlock()
if m.Map != nil {
return nil
}
if m.spec == nil {
return fmt.Errorf("cannot create map: nil map spec")
}
opts := ciliumebpf.MapOptions{
PinPath: bpf.MapPrefixPath(),
}
mapType := bpf.GetMapType(bpf.MapType(m.spec.Type))
m.spec.Flags = m.spec.Flags | bpf.GetPreAllocateMapFlags(mapType)
path := bpf.MapPath(m.spec.Name)
if m.spec.Pinning == ciliumebpf.PinByName {
mapDir := filepath.Dir(path)
if _, err := os.Stat(mapDir); os.IsNotExist(err) {
if err = os.MkdirAll(mapDir, 0755); err != nil {
return &os.PathError{
Op: "Unable create map base directory",
Path: path,
Err: err,
}
}
}
}
newMap, err := ciliumebpf.NewMapWithOptions(m.spec, opts)
if err != nil {
if !errors.Is(err, ciliumebpf.ErrMapIncompatible) {
return fmt.Errorf("unable to create map: %w", err)
}
// There already exists a pinned map but it has a different
// configuration (e.g different type, k/v size or flags).
// Try to delete and recreate it.
log.WithField("map", m.spec.Name).
WithError(err).Warn("Removing map to allow for property upgrade (expect map data loss)")
oldMap, err := ciliumebpf.LoadPinnedMap(path, &opts.LoadPinOptions)
if err != nil {
return fmt.Errorf("cannot load pinned map %s: %w", m.spec.Name, err)
}
defer func() {
if err := oldMap.Close(); err != nil {
log.WithField("map", m.spec.Name).Warnf("Cannot close map: %v", err)
}
}()
if err = oldMap.Unpin(); err != nil {
return fmt.Errorf("cannot unpin map %s: %w", m.spec.Name, err)
}
newMap, err = ciliumebpf.NewMapWithOptions(m.spec, opts)
if err != nil {
return fmt.Errorf("unable to create map: %w", err)
}
}
m.Map = newMap
m.path = path
registerMap(m)
return nil
}
// IterateWithCallback iterates through all the keys/values of a map, passing
// each key/value pair to the cb callback.
func (m *Map) IterateWithCallback(key, value interface{}, cb IterateCallback) error {
if m.Map == nil {
if err := m.OpenOrCreate(); err != nil {
return err
}
}
m.lock.RLock()
defer m.lock.RUnlock()
entries := m.Iterate()
for entries.Next(key, value) {
cb(key, value)
}
return nil
}
// GetModel returns a BPF map in the representation served via the API.
func (m *Map) GetModel() *models.BPFMap {
m.lock.RLock()
defer m.lock.RUnlock()
mapModel := &models.BPFMap{
Path: m.path,
}
// TODO: handle map cache. See pkg/bpf/map_linux.go:GetModel()
return mapModel
}