github.com/cilium/cilium@v1.16.2/pkg/ebpf/map_register.go (about)

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