-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
map.go
197 lines (172 loc) · 4.91 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package bpf
import (
"regexp"
"time"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/datapath/linux/probes"
)
// MapType is an enumeration for valid BPF map types
type MapType int
// This enumeration must be in sync with enum bpf_map_type in <linux/bpf.h>
const (
MapTypeUnspec MapType = iota
MapTypeHash
MapTypeArray
MapTypeProgArray
MapTypePerfEventArray
MapTypePerCPUHash
MapTypePerCPUArray
MapTypeStackTrace
MapTypeCgroupArray
MapTypeLRUHash
MapTypeLRUPerCPUHash
MapTypeLPMTrie
MapTypeArrayOfMaps
MapTypeHashOfMaps
MapTypeDevMap
MapTypeSockMap
MapTypeCPUMap
MapTypeXSKMap
MapTypeSockHash
// MapTypeMaximum is the maximum supported known map type.
MapTypeMaximum
// maxSyncErrors is the maximum consecutive errors syncing before the
// controller bails out
maxSyncErrors = 512
// errorResolverSchedulerMinInterval is the minimum interval for the
// error resolver to be scheduled. This minimum interval ensures not to
// overschedule if a large number of updates fail in a row.
errorResolverSchedulerMinInterval = 5 * time.Second
// errorResolverSchedulerDelay is the delay to update the controller
// after determination that a run is needed. The delay allows to
// schedule the resolver after series of updates have failed.
errorResolverSchedulerDelay = 200 * time.Millisecond
)
var (
mapControllers = controller.NewManager()
supportedMapTypes *probes.MapTypes
)
func (t MapType) String() string {
switch t {
case MapTypeHash:
return "Hash"
case MapTypeArray:
return "Array"
case MapTypeProgArray:
return "Program array"
case MapTypePerfEventArray:
return "Event array"
case MapTypePerCPUHash:
return "Per-CPU hash"
case MapTypePerCPUArray:
return "Per-CPU array"
case MapTypeStackTrace:
return "Stack trace"
case MapTypeCgroupArray:
return "Cgroup array"
case MapTypeLRUHash:
return "LRU hash"
case MapTypeLRUPerCPUHash:
return "LRU per-CPU hash"
case MapTypeLPMTrie:
return "Longest prefix match trie"
case MapTypeArrayOfMaps:
return "Array of maps"
case MapTypeHashOfMaps:
return "Hash of maps"
case MapTypeDevMap:
return "Device Map"
case MapTypeSockMap:
return "Socket Map"
case MapTypeCPUMap:
return "CPU Redirect Map"
case MapTypeSockHash:
return "Socket Hash"
}
return "Unknown"
}
func (t MapType) allowsPreallocation() bool {
return t != MapTypeLPMTrie
}
func (t MapType) requiresPreallocation() bool {
switch t {
case MapTypeHash, MapTypePerCPUHash, MapTypeLPMTrie, MapTypeHashOfMaps:
return false
}
return true
}
// DesiredAction is the action to be performed on the BPF map
type DesiredAction int
const (
// OK indicates that to further action is required and the entry is in
// sync
OK DesiredAction = iota
// Insert indicates that the entry needs to be created or updated
Insert
// Delete indicates that the entry needs to be deleted
Delete
)
func (d DesiredAction) String() string {
switch d {
case OK:
return "sync"
case Insert:
return "to-be-inserted"
case Delete:
return "to-be-deleted"
default:
return "unknown"
}
}
// GetMapType determines whether the specified map type is supported by the
// kernel (as determined by bpftool feature checks), and if the map type is not
// supported, returns a more primitive map type that may be used to implement
// the map on older implementations. Otherwise, returns the specified map type.
func GetMapType(t MapType) MapType {
// If the supported map types have not been set, default to the system
// prober. This path enables unit tests to mock out the supported map
// types.
if supportedMapTypes == nil {
setMapTypesFromProber(probes.NewProbeManager())
}
switch t {
case MapTypeLPMTrie:
fallthrough
case MapTypeLRUHash:
if !supportedMapTypes.HaveLruHashMapType {
return MapTypeHash
}
}
return t
}
// setMapTypesFromProber initializes the supported map types from the given
// prober. This function is useful for testing purposes, as we require
// injecting our own mocked prober.
func setMapTypesFromProber(prober prober) {
features := prober.Probe()
supportedMapTypes = &features.MapTypes
}
// prober abstracts the notion of a kernel feature prober. This is useful for
// testing purposes as it allows us to mock out the kernel, enabling control
// control over what features are returned.
type prober interface {
// Probe returns the kernel feaures available on machine.
Probe() probes.Features
}
var commonNameRegexps = []*regexp.Regexp{
regexp.MustCompile(`^(cilium_)(.+)_reserved_[0-9]+$`),
regexp.MustCompile(`^(cilium_)(.+)_netdev_ns_[0-9]+$`),
regexp.MustCompile(`^(cilium_)(.+)_overlay_[0-9]+$`),
regexp.MustCompile(`^(cilium_)(.+)_[0-9]+$`),
regexp.MustCompile(`^(cilium_)(.+)+$`),
}
func extractCommonName(name string) string {
for _, r := range commonNameRegexps {
if replaced := r.ReplaceAllString(name, `$2`); replaced != name {
return replaced
}
}
return name
}