github.com/cilium/cilium@v1.16.2/pkg/maps/signalmap/signalmap.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package signalmap
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/cilium/ebpf"
    11  	"github.com/cilium/ebpf/perf"
    12  
    13  	"github.com/cilium/cilium/pkg/bpf"
    14  )
    15  
    16  const (
    17  	// MapName is the BPF map name.
    18  	MapName = "cilium_signals"
    19  )
    20  
    21  // Key is the index into the prog array map.
    22  type Key struct {
    23  	index uint32
    24  }
    25  
    26  // Value is the program ID in the prog array map.
    27  type Value struct {
    28  	progID uint32
    29  }
    30  
    31  // String converts the key into a human readable string format.
    32  func (k *Key) String() string  { return fmt.Sprintf("%d", k.index) }
    33  func (k *Key) New() bpf.MapKey { return &Key{} }
    34  
    35  // String converts the value into a human readable string format.
    36  func (v *Value) String() string    { return fmt.Sprintf("%d", v.progID) }
    37  func (v *Value) New() bpf.MapValue { return &Value{} }
    38  
    39  type signalMap struct {
    40  	oldBpfMap  *bpf.Map
    41  	ebpfMap    *ebpf.Map
    42  	maxEntries int
    43  }
    44  
    45  // initMap creates the signal map in the kernel.
    46  func initMap(maxEntries int) *signalMap {
    47  	return &signalMap{
    48  		maxEntries: maxEntries,
    49  		oldBpfMap: bpf.NewMap(MapName,
    50  			ebpf.PerfEventArray,
    51  			&Key{},
    52  			&Value{},
    53  			maxEntries,
    54  			0,
    55  		),
    56  	}
    57  }
    58  
    59  func (sm *signalMap) open() error {
    60  	if err := sm.oldBpfMap.Create(); err != nil {
    61  		return err
    62  	}
    63  	path := bpf.MapPath(MapName)
    64  
    65  	var err error
    66  	sm.ebpfMap, err = ebpf.LoadPinnedMap(path, nil)
    67  	return err
    68  }
    69  
    70  func (sm *signalMap) close() error {
    71  	if sm.ebpfMap != nil {
    72  		return sm.ebpfMap.Close()
    73  	}
    74  	return nil
    75  }
    76  
    77  func (sm *signalMap) NewReader() (PerfReader, error) {
    78  	return perf.NewReader(sm.ebpfMap, os.Getpagesize())
    79  }
    80  
    81  func (sm *signalMap) MapName() string {
    82  	return MapName
    83  }