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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package signalmap
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/cilium/ebpf"
    10  	"github.com/cilium/ebpf/perf"
    11  	"github.com/cilium/hive/cell"
    12  
    13  	"github.com/cilium/cilium/pkg/bpf"
    14  )
    15  
    16  // Cell initializes and manages the config map.
    17  var Cell = cell.Module(
    18  	"signal-map",
    19  	"eBPF map signal passes wakeup events from Cilium datapath",
    20  
    21  	cell.Provide(newMap),
    22  )
    23  
    24  // PerfReader is an interface for reading from perf records. Implementations need to be safe to call
    25  // from multiple goroutines.
    26  type PerfReader interface {
    27  	Read() (perf.Record, error)
    28  	Pause() error
    29  	Resume() error
    30  	Close() error
    31  }
    32  
    33  type Map interface {
    34  	NewReader() (PerfReader, error)
    35  	MapName() string
    36  }
    37  
    38  func newMap(lifecycle cell.Lifecycle) (bpf.MapOut[Map], error) {
    39  	possibleCPUs, err := ebpf.PossibleCPU()
    40  	if err != nil {
    41  		return bpf.MapOut[Map]{}, fmt.Errorf("failed to get number of possible CPUs: %w", err)
    42  	}
    43  	signalmap := initMap(possibleCPUs)
    44  
    45  	lifecycle.Append(cell.Hook{
    46  		OnStart: func(startCtx cell.HookContext) error {
    47  			return signalmap.open()
    48  		},
    49  		OnStop: func(stopCtx cell.HookContext) error {
    50  			return signalmap.close()
    51  		},
    52  	})
    53  
    54  	return bpf.NewMapOut(Map(signalmap)), nil
    55  }