github.com/cilium/cilium@v1.16.2/pkg/bpf/map_register_linux.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 //go:build linux 5 6 package bpf 7 8 import ( 9 "path" 10 11 "github.com/cilium/cilium/api/v1/models" 12 "github.com/cilium/cilium/pkg/lock" 13 ) 14 15 var ( 16 mutex lock.RWMutex 17 mapRegister = map[string]*Map{} 18 ) 19 20 func registerMap(path string, m *Map) { 21 mutex.Lock() 22 mapRegister[path] = m 23 mutex.Unlock() 24 25 log.WithField("path", path).Debug("Registered BPF map") 26 } 27 28 func unregisterMap(path string, m *Map) { 29 mutex.Lock() 30 delete(mapRegister, path) 31 mutex.Unlock() 32 33 log.WithField("path", path).Debug("Unregistered BPF map") 34 } 35 36 // GetMap returns the registered map with the given name or absolute path 37 func GetMap(name string) *Map { 38 mutex.RLock() 39 defer mutex.RUnlock() 40 41 if !path.IsAbs(name) { 42 name = MapPath(name) 43 } 44 45 return mapRegister[name] 46 } 47 48 // GetOpenMaps returns a slice of all open BPF maps. This is identical to 49 // calling GetMap() on all open maps. 50 func GetOpenMaps() []*models.BPFMap { 51 // create a copy of mapRegister so we can unlock the mutex again as 52 // locking Map.lock inside of the mutex is not permitted 53 mutex.RLock() 54 maps := make([]*Map, 0, len(mapRegister)) 55 for _, m := range mapRegister { 56 maps = append(maps, m) 57 } 58 mutex.RUnlock() 59 60 mapList := make([]*models.BPFMap, len(maps)) 61 62 i := 0 63 for _, m := range maps { 64 mapList[i] = m.GetModel() 65 i++ 66 } 67 68 return mapList 69 }