diff --git a/pkg/managers/pluginmanager/pluginmanager.go b/pkg/managers/pluginmanager/pluginmanager.go index 15d133382f..8fc02b4c60 100644 --- a/pkg/managers/pluginmanager/pluginmanager.go +++ b/pkg/managers/pluginmanager/pluginmanager.go @@ -142,6 +142,7 @@ func (p *PluginManager) Start(ctx context.Context) error { if err != nil { return errors.Wrap(err, "failed to get conntrack instance") } + ct.SetConfig(p.cfg) g.Go(func() error { return errors.Wrapf(ct.Run(ctx), "failed to run conntrack GC") }) diff --git a/pkg/plugin/common/common_linux.go b/pkg/plugin/common/common_linux.go index 92371f9c91..2f43d6dbe0 100644 --- a/pkg/plugin/common/common_linux.go +++ b/pkg/plugin/common/common_linux.go @@ -89,3 +89,16 @@ func NewPerfReader(l *log.ZapLogger, m *ebpf.Map, max, min int) (*perf.Reader, e } return nil, errors.New("failed to create perf reader") } + +// IsPluginEnabled checks if a given plugin is enabled in the config +func IsPluginEnabled(enabledPlugins []string, pluginName string) bool { + if enabledPlugins == nil { + return false + } + for _, plugin := range enabledPlugins { + if plugin == pluginName { + return true + } + } + return false +} diff --git a/pkg/plugin/conntrack/conntrack_linux.go b/pkg/plugin/conntrack/conntrack_linux.go index dc2fb0c449..22c32bcaef 100644 --- a/pkg/plugin/conntrack/conntrack_linux.go +++ b/pkg/plugin/conntrack/conntrack_linux.go @@ -14,6 +14,7 @@ import ( "github.com/cilium/ebpf" "github.com/cilium/ebpf/rlimit" "github.com/microsoft/retina/internal/ktime" + "github.com/microsoft/retina/pkg/config" "github.com/microsoft/retina/pkg/loader" "github.com/microsoft/retina/pkg/log" plugincommon "github.com/microsoft/retina/pkg/plugin/common" @@ -46,13 +47,8 @@ func Init() error { return nil } -// New returns a new Conntrack instance. +// New creates a new Conntrack instance func New() (*Conntrack, error) { - ct := &Conntrack{ - l: log.Logger().Named("conntrack"), - gcFrequency: defaultGCFrequency, - } - objs := &conntrackObjects{} err := loadConntrackObjects(objs, &ebpf.CollectionOptions{ Maps: ebpf.MapOptions{ @@ -60,16 +56,23 @@ func New() (*Conntrack, error) { }, }) if err != nil { - ct.l.Error("loadConntrackObjects failed", zap.Error(err)) return nil, errors.Wrap(err, "failed to load conntrack objects") } - ct.objs = objs - // Get the conntrack map from the objects - ct.ctMap = objs.RetinaConntrack + ct := &Conntrack{ + l: log.Logger().Named("conntrack"), + gcFrequency: defaultGCFrequency, + objs: objs, + ctMap: objs.RetinaConntrack, + } return ct, nil } +// SetConfig sets the config after initialization +func (ct *Conntrack) SetConfig(cfg *config.Config) { + ct.cfg = cfg +} + // Build dynamic header path func BuildDynamicHeaderPath() string { // Get absolute path to this file during runtime. @@ -93,6 +96,12 @@ func GenerateDynamic(ctx context.Context, dynamicHeaderPath string, conntrackMet // Run starts the Conntrack garbage collection loop. func (ct *Conntrack) Run(ctx context.Context) error { + // Check if packetparser plugin is enabled + if !plugincommon.IsPluginEnabled(ct.cfg.EnabledPlugin, "packetparser") { + ct.l.Info("Skipping Conntrack GC loop as packetparser plugin is not enabled") + return nil + } + ticker := time.NewTicker(ct.gcFrequency) defer ticker.Stop() diff --git a/pkg/plugin/conntrack/conntrack_windows.go b/pkg/plugin/conntrack/conntrack_windows.go index df674e2fd5..ebf4afaea5 100644 --- a/pkg/plugin/conntrack/conntrack_windows.go +++ b/pkg/plugin/conntrack/conntrack_windows.go @@ -2,6 +2,8 @@ package conntrack import ( "context" + + "github.com/microsoft/retina/pkg/config" ) type Conntrack struct{} @@ -15,3 +17,8 @@ func New() (*Conntrack, error) { func (c *Conntrack) Run(_ context.Context) error { return nil } + +// SetConfig sets the config after initialization +func (c *Conntrack) SetConfig(_ *config.Config) { + // No-op for Windows +} diff --git a/pkg/plugin/conntrack/types_linux.go b/pkg/plugin/conntrack/types_linux.go index 63fa7e90a9..1d3fe8b7b6 100644 --- a/pkg/plugin/conntrack/types_linux.go +++ b/pkg/plugin/conntrack/types_linux.go @@ -5,6 +5,7 @@ import ( "time" "github.com/cilium/ebpf" + "github.com/microsoft/retina/pkg/config" "github.com/microsoft/retina/pkg/log" ) @@ -15,11 +16,13 @@ const ( dynamicHeaderFileName = "dynamic.h" ) +// Conntrack represents the conntrack plugin type Conntrack struct { l *log.ZapLogger objs *conntrackObjects ctMap *ebpf.Map gcFrequency time.Duration + cfg *config.Config } // Define TCP flag constants