github.com/elfadel/cilium@v1.6.12/pkg/bpf/map_register_linux.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build linux
    16  
    17  package bpf
    18  
    19  import (
    20  	"path"
    21  
    22  	"github.com/cilium/cilium/api/v1/models"
    23  	"github.com/cilium/cilium/pkg/lock"
    24  )
    25  
    26  var (
    27  	mutex       lock.RWMutex
    28  	mapRegister = map[string]*Map{}
    29  )
    30  
    31  func registerMap(path string, m *Map) {
    32  	mutex.Lock()
    33  	mapRegister[path] = m
    34  	mutex.Unlock()
    35  
    36  	log.WithField("path", path).Debug("Registered BPF map")
    37  }
    38  
    39  func unregisterMap(path string, m *Map) {
    40  	mutex.Lock()
    41  	delete(mapRegister, path)
    42  	mutex.Unlock()
    43  
    44  	log.WithField("path", path).Debug("Unregistered BPF map")
    45  }
    46  
    47  // GetMap returns the registered map with the given name or absolute path
    48  func GetMap(name string) *Map {
    49  	mutex.RLock()
    50  	defer mutex.RUnlock()
    51  
    52  	if !path.IsAbs(name) {
    53  		name = MapPath(name)
    54  	}
    55  
    56  	return mapRegister[name]
    57  }
    58  
    59  // GetOpenMaps returns a slice of all open BPF maps. This is identical to
    60  // calling GetMap() on all open maps.
    61  func GetOpenMaps() []*models.BPFMap {
    62  	// create a copy of mapRegister so we can unlock the mutex again as
    63  	// locking Map.lock inside of the mutex is not permitted
    64  	mutex.RLock()
    65  	maps := []*Map{}
    66  	for _, m := range mapRegister {
    67  		maps = append(maps, m)
    68  	}
    69  	mutex.RUnlock()
    70  
    71  	mapList := make([]*models.BPFMap, len(maps))
    72  
    73  	i := 0
    74  	for _, m := range maps {
    75  		mapList[i] = m.GetModel()
    76  		i++
    77  	}
    78  
    79  	return mapList
    80  }