Skip to content

Commit

Permalink
eventsmap: encapsulate in cell
Browse files Browse the repository at this point in the history
The cilium events map is used by the monitor.Agent. In preparation of
modularization of the agent, move the initialization of the eventsmap
into the hive.

Signed-off-by: David Bimmler <david.bimmler@isovalent.com>
  • Loading branch information
bimmlerd authored and julianwiedmann committed May 31, 2023
1 parent 1c0e769 commit 80d30af
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
8 changes: 0 additions & 8 deletions daemon/cmd/datapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/vishvananda/netlink"

"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/common"
"github.com/cilium/cilium/pkg/controller"
datapathIpcache "github.com/cilium/cilium/pkg/datapath/ipcache"
"github.com/cilium/cilium/pkg/datapath/linux/ipsec"
Expand All @@ -29,7 +28,6 @@ import (
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/ctmap"
"github.com/cilium/cilium/pkg/maps/eventsmap"
"github.com/cilium/cilium/pkg/maps/fragmap"
ipcachemap "github.com/cilium/cilium/pkg/maps/ipcache"
"github.com/cilium/cilium/pkg/maps/ipmasq"
Expand Down Expand Up @@ -362,12 +360,6 @@ func (d *Daemon) initMaps() error {
log.WithError(err).Fatal("Unable to initialize service maps")
}

possibleCPUs := common.GetNumPossibleCPUs(log)

if err := eventsmap.InitMap(possibleCPUs); err != nil {
return fmt.Errorf("initializing events map: %w", err)
}

if err := policymap.InitCallMaps(option.Config.EnableEnvoyConfig); err != nil {
return fmt.Errorf("initializing policy map: %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/datapath/cells.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cilium/cilium/pkg/hive/cell"
ipcache "github.com/cilium/cilium/pkg/ipcache/types"
"github.com/cilium/cilium/pkg/maps"
"github.com/cilium/cilium/pkg/maps/eventsmap"
"github.com/cilium/cilium/pkg/option"
wg "github.com/cilium/cilium/pkg/wireguard/agent"
wgTypes "github.com/cilium/cilium/pkg/wireguard/types"
Expand All @@ -35,6 +36,9 @@ var Cell = cell.Module(
// Utime synchronizes utime from userspace to datapath via configmap.Map.
utime.Cell,

// The cilium events map, used by the monitor agent.
eventsmap.Cell,

cell.Provide(
newWireguardAgent,
newDatapath,
Expand Down
51 changes: 51 additions & 0 deletions pkg/maps/eventsmap/cell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package eventsmap

import (
"fmt"

"github.com/sirupsen/logrus"

"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/common"
"github.com/cilium/cilium/pkg/hive"
"github.com/cilium/cilium/pkg/hive/cell"
)

// Cell provides eventsmap.Map, which is the hive representation of the cilium
// events perf event ring buffer.
var Cell = cell.Module(
"events-map",
"eBPF ring buffer of cilium events",

cell.Provide(newEventsMap),
)

var (
MaxEntries int
)

type Map interface{}

func newEventsMap(log logrus.FieldLogger, lifecycle hive.Lifecycle) bpf.MapOut[Map] {
eventsMap := &eventsMap{}

lifecycle.Append(hive.Hook{
OnStart: func(context hive.HookContext) error {
cpus := common.GetNumPossibleCPUs(log)
err := eventsMap.init(cpus)
if err != nil {
return fmt.Errorf("initializing events map: %w", err)
}
return nil
},
OnStop: func(context hive.HookContext) error {
// We don't currently care for cleaning up.
return nil
},
})

return bpf.NewMapOut(Map(eventsMap))
}
19 changes: 9 additions & 10 deletions pkg/maps/eventsmap/eventsmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/cilium/cilium/pkg/option"
)

var (
MaxEntries int
)

const (
// MapName is the BPF map name.
MapName = "cilium_events"
Expand Down Expand Up @@ -50,18 +46,21 @@ func (v *Value) String() string { return fmt.Sprintf("%d", v.progID) }
// map value.
func (k Key) NewValue() bpf.MapValue { return &Value{} }

// InitMap creates the events map in the kernel.
func InitMap(maxEntries int) error {
MaxEntries = maxEntries
eventsMap := bpf.NewMap(MapName,
type eventsMap struct {
m *bpf.Map
}

// init creates the events map in the kernel.
func (e *eventsMap) init(maxEntries int) error {
e.m = bpf.NewMap(MapName,
bpf.MapTypePerfEventArray,
&Key{},
int(unsafe.Sizeof(Key{})),
&Value{},
int(unsafe.Sizeof(Value{})),
MaxEntries,
maxEntries,
0,
bpf.ConvertKeyValue).
WithEvents(option.Config.GetEventBufferConfig(MapName))
return eventsMap.Create()
return e.m.Create()
}
5 changes: 2 additions & 3 deletions pkg/monitor/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import (
oldBPF "github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/eventsmap"
"github.com/cilium/cilium/pkg/monitor/agent/consumer"
"github.com/cilium/cilium/pkg/monitor/agent/listener"
"github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/monitor/payload"
)

const eventsMapName = "cilium_events"

// isCtxDone is a utility function that returns true when the context's Done()
// channel is closed. It is intended to simplify goroutines that need to check
// this multiple times in their loop.
Expand Down Expand Up @@ -99,7 +98,7 @@ func (a *Agent) AttachToEventsMap(nPages int) error {
}

// assert that we can actually connect the monitor
path := oldBPF.MapPath(eventsMapName)
path := oldBPF.MapPath(eventsmap.MapName)
eventsMap, err := ebpf.LoadPinnedMap(path, nil)
if err != nil {
return err
Expand Down

0 comments on commit 80d30af

Please sign in to comment.