Skip to content

Commit

Permalink
remove redudant management of the map
Browse files Browse the repository at this point in the history
  • Loading branch information
akarpz committed Oct 16, 2024
1 parent 37bcef7 commit c88a173
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 45 deletions.
6 changes: 1 addition & 5 deletions pkg/network/tracer/connection/ebpf_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,7 @@ func (t *ebpfTracer) Stop() {
}

func (t *ebpfTracer) GetMap(name string) *ebpf.Map {
m, _, err := t.m.GetMap(name)
if err != nil {
log.Warnf("error retrieving map %s: %s", name, err)
return nil
}
m, _, _ := t.m.GetMap(name)
return m
}

Expand Down
25 changes: 13 additions & 12 deletions pkg/network/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,18 @@ func newTracer(cfg *config.Config, telemetryComponent telemetryComponent.Compone
}

tr.reverseDNS = newReverseDNS(cfg, telemetryComponent)
tr.usmMonitor = newUSMMonitor(cfg, tr.ebpfTracer)
tr.usmMonitor = newUSMMonitor(cfg)

connectionProtocolMap := tr.ebpfTracer.GetMap(probes.ConnectionProtocolMap)

tr.connectionProtocolMapCleaner, err = usm.SetupConnectionProtocolMapCleaner(connectionProtocolMap)
if err != nil {
log.Warnf("could not setup connection protocol map cleaner: %s", err)
if cfg.ProtocolClassificationEnabled {
connectionProtocolMap := tr.ebpfTracer.GetMap(probes.ConnectionProtocolMap)
if connectionProtocolMap == nil {
log.Warn("could not get connection_protocol map, will not be able to expire connection_protocol data")
} else {
tr.connectionProtocolMapCleaner, err = usm.SetupConnectionProtocolMapCleaner(connectionProtocolMap)
if err != nil {
log.Warnf("could not setup connection protocol map cleaner: %s", err)
}
}
}

if cfg.EnableProcessEventMonitoring {
Expand Down Expand Up @@ -838,18 +843,14 @@ func (t *Tracer) DebugDumpProcessCache(_ context.Context) (interface{}, error) {
return nil, nil
}

func newUSMMonitor(c *config.Config, tracer connection.Tracer) *usm.Monitor {
// Shared map between NPM and USM
connectionProtocolMap := tracer.GetMap(probes.ConnectionProtocolMap)

func newUSMMonitor(c *config.Config) *usm.Monitor {
if !usmconfig.IsUSMSupportedAndEnabled(c) {
// If USM is not supported, or if USM is not enabled, we should not start the USM monitor.
// We should still clean the connection protocol map as it is used by NPM.
usm.SetupConnectionProtocolMapCleaner(connectionProtocolMap)
return nil
}

monitor, err := usm.NewMonitor(c, connectionProtocolMap)
monitor, err := usm.NewMonitor(c)
if err != nil {
log.Errorf("usm initialization failed: %s", err)
return nil
Expand Down
30 changes: 5 additions & 25 deletions pkg/network/usm/ebpf_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,16 @@ const (

type ebpfProgram struct {
*ddebpf.Manager
cfg *config.Config
tailCallRouter []manager.TailCallRoute
connectionProtocolMap *ebpf.Map
cfg *config.Config
tailCallRouter []manager.TailCallRoute

enabledProtocols []*protocols.ProtocolSpec
disabledProtocols []*protocols.ProtocolSpec

buildMode buildmode.Type
}

func newEBPFProgram(c *config.Config, connectionProtocolMap *ebpf.Map) (*ebpfProgram, error) {
func newEBPFProgram(c *config.Config) (*ebpfProgram, error) {
mgr := &manager.Manager{
Maps: []*manager.Map{
{Name: protocols.TLSDispatcherProgramsMap},
Expand Down Expand Up @@ -153,9 +152,8 @@ func newEBPFProgram(c *config.Config, connectionProtocolMap *ebpf.Map) (*ebpfPro
}

program := &ebpfProgram{
Manager: ddebpf.NewManager(mgr, &ebpftelemetry.ErrorsTelemetryModifier{}),
cfg: c,
connectionProtocolMap: connectionProtocolMap,
Manager: ddebpf.NewManager(mgr, &ebpftelemetry.ErrorsTelemetryModifier{}),
cfg: c,
}

opensslSpec.Factory = newSSLProgramProtocolFactory(mgr)
Expand Down Expand Up @@ -214,17 +212,6 @@ func (e *ebpfProgram) Init() error {
func (e *ebpfProgram) Start() error {
initializeTupleMaps(e.Manager)

// Mainly for tests, but possible for other cases as well, we might have a nil (not shared) connection protocol map
// between NPM and USM. In such a case we just create our own instance, but we don't modify the
// `e.connectionProtocolMap` field.
if e.connectionProtocolMap == nil {
m, _, err := e.GetMap(probes.ConnectionProtocolMap)
if err != nil {
return err
}
e.connectionProtocolMap = m
}

e.enabledProtocols = e.executePerProtocol(e.enabledProtocols, "pre-start",
func(protocol protocols.Protocol, m *manager.Manager) error { return protocol.PreStart(m) },
func(protocols.Protocol, *manager.Manager) {})
Expand Down Expand Up @@ -402,13 +389,6 @@ func (e *ebpfProgram) init(buf bytecode.AssetReader, options manager.Options) er
},
}

if e.connectionProtocolMap != nil {
if options.MapEditors == nil {
options.MapEditors = make(map[string]*ebpf.Map)
}
options.MapEditors[probes.ConnectionProtocolMap] = e.connectionProtocolMap
}

begin, end := network.EphemeralRange()
options.ConstantEditors = append(options.ConstantEditors,
manager.ConstantEditor{Name: "ephemeral_range_begin", Value: uint64(begin)},
Expand Down
5 changes: 2 additions & 3 deletions pkg/network/usm/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"syscall"
"time"

"github.com/cilium/ebpf"
"go.uber.org/atomic"

manager "github.com/DataDog/ebpf-manager"
Expand Down Expand Up @@ -53,7 +52,7 @@ type Monitor struct {
}

// NewMonitor returns a new Monitor instance
func NewMonitor(c *config.Config, connectionProtocolMap *ebpf.Map) (m *Monitor, err error) {
func NewMonitor(c *config.Config) (m *Monitor, err error) {
defer func() {
// capture error and wrap it
if err != nil {
Expand All @@ -63,7 +62,7 @@ func NewMonitor(c *config.Config, connectionProtocolMap *ebpf.Map) (m *Monitor,
}
}()

mgr, err := newEBPFProgram(c, connectionProtocolMap)
mgr, err := newEBPFProgram(c)
if err != nil {
return nil, fmt.Errorf("error setting up ebpf program: %w", err)
}
Expand Down

0 comments on commit c88a173

Please sign in to comment.