From 67e1a0818df94729a81e4c877d3794976a61ce30 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 13 May 2024 22:28:57 -0400 Subject: [PATCH 1/3] fix: load eBPF objects using function generated in wrapper files --- pkg/plugin/dropreason/dropreason_linux.go | 21 ++++-------------- .../packetforward/packetforward_linux.go | 21 +++++------------- pkg/plugin/packetparser/packetparser_linux.go | 22 +++++-------------- 3 files changed, 14 insertions(+), 50 deletions(-) diff --git a/pkg/plugin/dropreason/dropreason_linux.go b/pkg/plugin/dropreason/dropreason_linux.go index 8db207a383..0778252527 100644 --- a/pkg/plugin/dropreason/dropreason_linux.go +++ b/pkg/plugin/dropreason/dropreason_linux.go @@ -116,29 +116,16 @@ func (dr *dropReason) Compile(ctx context.Context) error { } func (dr *dropReason) Init() error { - var err error - if err = rlimit.RemoveMemlock(); err != nil { - dr.l.Error("RemoveMemLock failed:%w", zap.Error(err)) - return err - } - - // Get the absolute path to this file during runtime. - dir, err := absPath() - if err != nil { - return err - } - - bpfOutputFile := fmt.Sprintf("%s/%s", dir, bpfObjectFileName) - - objs := &kprobeObjects{} //nolint:typecheck - spec, err := ebpf.LoadCollectionSpec(bpfOutputFile) + err := rlimit.RemoveMemlock() if err != nil { + dr.l.Error("RemoveMemLock failed:%w", zap.Error(err)) return err } + objs := &kprobeObjects{} // TODO remove the opts - if err := spec.LoadAndAssign(objs, &ebpf.CollectionOptions{ + if err = loadKprobeObjects(objs, &ebpf.CollectionOptions{ Programs: ebpf.ProgramOptions{ LogLevel: 2, }, diff --git a/pkg/plugin/packetforward/packetforward_linux.go b/pkg/plugin/packetforward/packetforward_linux.go index 7ff9d58dfb..341544da29 100644 --- a/pkg/plugin/packetforward/packetforward_linux.go +++ b/pkg/plugin/packetforward/packetforward_linux.go @@ -17,7 +17,6 @@ import ( kcfg "github.com/microsoft/retina/pkg/config" hubblev1 "github.com/cilium/cilium/pkg/hubble/api/v1" - "github.com/cilium/ebpf" "github.com/cilium/ebpf/rlimit" "github.com/microsoft/retina/pkg/loader" "github.com/microsoft/retina/pkg/log" @@ -132,28 +131,18 @@ func (p *packetForward) Compile(ctx context.Context) error { } func (p *packetForward) Init() error { - if err := rlimit.RemoveMemlock(); err != nil { - p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) - return err - } - // Get the absolute path to this file during runtime. - dir, err := absPath() + err := rlimit.RemoveMemlock() if err != nil { + p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) return err } - bpfOutputFile := fmt.Sprintf("%s/%s", dir, bpfObjectFileName) - objs := &packetforwardObjects{} //nolint:typecheck - spec, err := ebpf.LoadCollectionSpec(bpfOutputFile) - if err != nil { - p.l.Error("Error loading collection specs: %w", zap.Error(err)) - return err - } - if err := spec.LoadAndAssign(objs, nil); err != nil { - p.l.Error("Error assigning specs: %w", zap.Error(err)) + err = loadPacketforwardObjects(objs, nil) + if err != nil { + p.l.Error("Error loading packetforward objects: %w", zap.Error(err)) return err } diff --git a/pkg/plugin/packetparser/packetparser_linux.go b/pkg/plugin/packetparser/packetparser_linux.go index 24bc5cf0b7..b5eecfd577 100644 --- a/pkg/plugin/packetparser/packetparser_linux.go +++ b/pkg/plugin/packetparser/packetparser_linux.go @@ -108,36 +108,24 @@ func (p *packetParser) Compile(ctx context.Context) error { } func (p *packetParser) Init() error { - var err error if !p.cfg.EnablePodLevel { p.l.Warn("packet parser and latency plugin will not init because pod level is disabled") return nil } - if err := rlimit.RemoveMemlock(); err != nil { - p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) - return err - } - - // Get the absolute path to this file during runtime. - dir, err := absPath() + err := rlimit.RemoveMemlock() if err != nil { + p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) return err } - bpfOutputFile := fmt.Sprintf("%s/%s", dir, bpfObjectFileName) - objs := &packetparserObjects{} - spec, err := ebpf.LoadCollectionSpec(bpfOutputFile) - if err != nil { - return err - } - //nolint:typecheck - if err := spec.LoadAndAssign(objs, &ebpf.CollectionOptions{ //nolint:typecheck + + if err = loadPacketparserObjects(objs, &ebpf.CollectionOptions{ Maps: ebpf.MapOptions{ PinPath: plugincommon.FilterMapPath, }, - }); err != nil { //nolint:typecheck + }); err != nil { p.l.Error("Error loading objects: %w", zap.Error(err)) return err } From 9f70228ff3344b6be47f913c6a33688a75535af9 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 13 May 2024 23:40:14 -0400 Subject: [PATCH 2/3] formatting --- pkg/plugin/dropreason/dropreason_linux.go | 1 - pkg/plugin/packetforward/packetforward_linux.go | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/plugin/dropreason/dropreason_linux.go b/pkg/plugin/dropreason/dropreason_linux.go index 0778252527..699ee8f8d5 100644 --- a/pkg/plugin/dropreason/dropreason_linux.go +++ b/pkg/plugin/dropreason/dropreason_linux.go @@ -116,7 +116,6 @@ func (dr *dropReason) Compile(ctx context.Context) error { } func (dr *dropReason) Init() error { - err := rlimit.RemoveMemlock() if err != nil { dr.l.Error("RemoveMemLock failed:%w", zap.Error(err)) diff --git a/pkg/plugin/packetforward/packetforward_linux.go b/pkg/plugin/packetforward/packetforward_linux.go index 341544da29..8ee958563c 100644 --- a/pkg/plugin/packetforward/packetforward_linux.go +++ b/pkg/plugin/packetforward/packetforward_linux.go @@ -131,14 +131,13 @@ func (p *packetForward) Compile(ctx context.Context) error { } func (p *packetForward) Init() error { - err := rlimit.RemoveMemlock() if err != nil { p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) return err } - objs := &packetforwardObjects{} //nolint:typecheck + objs := &packetforwardObjects{} err = loadPacketforwardObjects(objs, nil) if err != nil { From e1423cfc4157c3ff28fc614d1569e44b5642627f Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 13 May 2024 23:45:36 -0400 Subject: [PATCH 3/3] log fmt --- pkg/plugin/dropreason/dropreason_linux.go | 4 ++-- pkg/plugin/packetforward/packetforward_linux.go | 4 ++-- pkg/plugin/packetparser/packetparser_linux.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/plugin/dropreason/dropreason_linux.go b/pkg/plugin/dropreason/dropreason_linux.go index 699ee8f8d5..f5295208a8 100644 --- a/pkg/plugin/dropreason/dropreason_linux.go +++ b/pkg/plugin/dropreason/dropreason_linux.go @@ -118,7 +118,7 @@ func (dr *dropReason) Compile(ctx context.Context) error { func (dr *dropReason) Init() error { err := rlimit.RemoveMemlock() if err != nil { - dr.l.Error("RemoveMemLock failed:%w", zap.Error(err)) + dr.l.Error("remove memlock failed", zap.Error(err)) return err } @@ -132,7 +132,7 @@ func (dr *dropReason) Init() error { PinPath: plugincommon.FilterMapPath, }, }); err != nil { - dr.l.Error("Error loading objects: %w", zap.Error(err)) + dr.l.Error("failed to load kprobe objects", zap.Error(err)) return err } diff --git a/pkg/plugin/packetforward/packetforward_linux.go b/pkg/plugin/packetforward/packetforward_linux.go index 8ee958563c..537a75dc23 100644 --- a/pkg/plugin/packetforward/packetforward_linux.go +++ b/pkg/plugin/packetforward/packetforward_linux.go @@ -133,7 +133,7 @@ func (p *packetForward) Compile(ctx context.Context) error { func (p *packetForward) Init() error { err := rlimit.RemoveMemlock() if err != nil { - p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) + p.l.Error("remove memlock failed", zap.Error(err)) return err } @@ -141,7 +141,7 @@ func (p *packetForward) Init() error { err = loadPacketforwardObjects(objs, nil) if err != nil { - p.l.Error("Error loading packetforward objects: %w", zap.Error(err)) + p.l.Error("failed to load packetforward objects", zap.Error(err)) return err } diff --git a/pkg/plugin/packetparser/packetparser_linux.go b/pkg/plugin/packetparser/packetparser_linux.go index b5eecfd577..c39de3106d 100644 --- a/pkg/plugin/packetparser/packetparser_linux.go +++ b/pkg/plugin/packetparser/packetparser_linux.go @@ -115,7 +115,7 @@ func (p *packetParser) Init() error { err := rlimit.RemoveMemlock() if err != nil { - p.l.Error("RemoveMemLock failed:%w", zap.Error(err)) + p.l.Error("remove memlock failed", zap.Error(err)) return err } @@ -126,7 +126,7 @@ func (p *packetParser) Init() error { PinPath: plugincommon.FilterMapPath, }, }); err != nil { - p.l.Error("Error loading objects: %w", zap.Error(err)) + p.l.Error("failed to load packetparser objects", zap.Error(err)) return err } p.objs = objs