Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix Conntrack GC running in Retina default #1323

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/managers/pluginmanager/pluginmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
13 changes: 13 additions & 0 deletions pkg/plugin/common/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 19 additions & 10 deletions pkg/plugin/conntrack/conntrack_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -46,30 +47,32 @@ 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{
PinPath: plugincommon.MapPath,
},
})
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.
Expand All @@ -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()

Expand Down
7 changes: 7 additions & 0 deletions pkg/plugin/conntrack/conntrack_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package conntrack

import (
"context"

"github.com/microsoft/retina/pkg/config"
)

type Conntrack struct{}
Expand All @@ -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
}
3 changes: 3 additions & 0 deletions pkg/plugin/conntrack/types_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/cilium/ebpf"
"github.com/microsoft/retina/pkg/config"
"github.com/microsoft/retina/pkg/log"
)

Expand All @@ -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
Expand Down
Loading