forked from DataDog/ebpf-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapspec_editor.go
74 lines (68 loc) · 2.23 KB
/
mapspec_editor.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
package manager
import (
"fmt"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
)
// MapSpecEditorFlag - Flag used to specify what a MapSpecEditor should edit.
type MapSpecEditorFlag uint
const (
EditType MapSpecEditorFlag = 1 << 1
EditMaxEntries MapSpecEditorFlag = 1 << 2
EditFlags MapSpecEditorFlag = 1 << 3
EditKeyValue MapSpecEditorFlag = 1 << 4
)
// MapSpecEditor - A MapSpec editor defines how specific parameters of specific maps should be updated at runtime
//
// For example, this can be used if you need to change the max_entries of a map before it is loaded in the kernel, but
// you don't know what this value should be initially.
type MapSpecEditor struct {
// Type - Type of the map.
Type ebpf.MapType
// MaxEntries - Max Entries of the map.
MaxEntries uint32
// Flags - Flags provided to the kernel during the loading process.
Flags uint32
// KeySize - Defines the key size of the map
KeySize uint32
// Key - Defines the BTF type of the keys of the map
Key btf.Type
// ValueSize - Defines the value size of the map
ValueSize uint32
// Value - Defines the BTF type of the values of the map
Value btf.Type
// EditorFlag - Use this flag to specify what fields should be updated. See MapSpecEditorFlag.
EditorFlag MapSpecEditorFlag
}
// editMapSpecs - Update the MapSpec with the provided MapSpec editors.
func (m *Manager) editMapSpecs() error {
for name, mapEditor := range m.options.MapSpecEditors {
// select the map spec
spec, exists, err := m.GetMapSpec(name)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("failed to edit maps/%s: couldn't find map: %w", name, ErrUnknownSection)
}
if mapEditor.EditorFlag == 0 {
return fmt.Errorf("failed to edit maps/%s: %w", name, ErrMissingEditorFlags)
}
if EditType&mapEditor.EditorFlag == EditType {
spec.Type = mapEditor.Type
}
if EditMaxEntries&mapEditor.EditorFlag == EditMaxEntries {
spec.MaxEntries = mapEditor.MaxEntries
}
if EditFlags&mapEditor.EditorFlag == EditFlags {
spec.Flags = mapEditor.Flags
}
if EditKeyValue&mapEditor.EditorFlag == EditKeyValue {
spec.Key = mapEditor.Key
spec.KeySize = mapEditor.KeySize
spec.Value = mapEditor.Value
spec.ValueSize = mapEditor.ValueSize
}
}
return nil
}