github.com/cilium/cilium@v1.16.2/pkg/maps/configmap/config_map.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package configmap 5 6 import ( 7 "fmt" 8 9 "github.com/cilium/cilium/pkg/bpf" 10 "github.com/cilium/cilium/pkg/ebpf" 11 ) 12 13 const ( 14 // MapName name of map used to pin map for datapath 15 MapName = "cilium_runtime_config" 16 17 // MaxEntries represents the maximum number of config entries. 18 // Initially defined as 256, so that downgrade from a future version having more than one 19 // entry works without necessarily resizing the map. Entries not known by the datapath 20 // version are simply ignored. 21 MaxEntries = 256 22 ) 23 24 // Index is the index to the runtime config array. 25 type Index uint32 26 27 // All supported indices in one place. 28 // Must be in sync with RUNTIME_CONFIG_ enum in bpf/lib/common.h 29 const ( 30 UTimeOffset Index = iota 31 AgentLiveness 32 ) 33 34 // String pretty print the Index 35 func (r Index) String() string { 36 switch r { 37 case UTimeOffset: 38 return "UTimeOffset" 39 case AgentLiveness: 40 return "AgentLiveness" 41 default: 42 return "Unknown" 43 } 44 } 45 46 // Value is the generic datapath runtime config value. 47 type Value uint64 48 49 func (k *Index) New() bpf.MapKey { return new(Index) } 50 51 // String pretty print the config Value. 52 func (v *Value) String() string { 53 return fmt.Sprintf("%d", uint64(*v)) 54 } 55 56 func (v *Value) New() bpf.MapValue { return new(Value) } 57 58 // Map provides access to the eBPF map configmap. 59 type Map interface { 60 // Update writes the given uint64 value to the bpf map at the given index. 61 Update(index Index, val uint64) error 62 63 Get(index Index) (uint64, error) 64 } 65 66 type configMap struct { 67 bpfMap *bpf.Map 68 } 69 70 func newConfigMap() *configMap { 71 var index Index 72 var value Value 73 74 return &configMap{ 75 bpfMap: bpf.NewMap(MapName, 76 ebpf.Array, 77 &index, 78 &value, 79 MaxEntries, 80 0, 81 ), 82 } 83 } 84 85 // LoadMap loads the pre-initialized config map for access. 86 // This should only be used from components which aren't capable of using hive - mainly the Cilium CLI. 87 // It needs to initialized beforehand via the Cilium Agent. 88 func LoadMap() (Map, error) { 89 var index Index 90 var value Value 91 92 m, err := bpf.OpenMap(bpf.MapPath(MapName), &index, &value) 93 if err != nil { 94 return nil, fmt.Errorf("failed to load bpf map: %w", err) 95 } 96 97 return &configMap{bpfMap: m}, nil 98 } 99 100 func (m *configMap) init() error { 101 if err := m.bpfMap.OpenOrCreate(); err != nil { 102 return fmt.Errorf("failed to init bpf map: %w", err) 103 } 104 105 return nil 106 } 107 108 func (m *configMap) close() error { 109 if err := m.bpfMap.Close(); err != nil { 110 return fmt.Errorf("failed to close bpf map: %w", err) 111 } 112 113 return nil 114 } 115 116 func (m *configMap) Get(index Index) (uint64, error) { 117 v, err := m.bpfMap.Lookup(&index) 118 if err != nil { 119 return 0, fmt.Errorf("failed to lookup entry: %w", err) 120 } 121 122 mapValue, ok := v.(*Value) 123 if !ok { 124 return 0, fmt.Errorf("wrong config map value: %w", err) 125 } 126 127 return uint64(*mapValue), nil 128 } 129 130 func (m *configMap) Update(index Index, val uint64) error { 131 value := Value(val) 132 return m.bpfMap.Update(&index, &value) 133 }