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

tetragon: Add LoadProgramOpts function #2489

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Changes from 1 commit
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
65 changes: 36 additions & 29 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ type AttachFunc func(*ebpf.Collection, *ebpf.CollectionSpec, *ebpf.Program, *ebp

type OpenFunc func(*ebpf.CollectionSpec) error

type customInstall struct {
mapName string
secPrefix string
type tailCall struct {
name string
prefix string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

}

type loadOpts struct {
attach AttachFunc
open OpenFunc
ci *customInstall

tcMap string
tcPrefix string
}

func RawAttach(targetFD int) AttachFunc {
Expand Down Expand Up @@ -476,16 +478,17 @@ func MultiKprobeAttach(load *Program, bpfDir string) AttachFunc {
}

func LoadTracepointProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
var tc tailCall
for mName, mPath := range load.PinMap {
if mName == "tp_calls" || mName == "execve_calls" {
ci = &customInstall{mPath, "tracepoint"}
tc = tailCall{mPath, "tracepoint"}
break
}
}
opts := &loadOpts{
attach: TracepointAttach(load),
ci: ci,
attach: TracepointAttach(load),
tcMap: tc.name,
tcPrefix: tc.prefix,
}
return loadProgram(bpfDir, load, opts, verbose)
}
Expand All @@ -498,17 +501,18 @@ func LoadRawTracepointProgram(bpfDir string, load *Program, verbose int) error {
}

func LoadKprobeProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
var tc tailCall
for mName, mPath := range load.PinMap {
if mName == "kprobe_calls" || mName == "retkprobe_calls" {
ci = &customInstall{mPath, "kprobe"}
tc = tailCall{mPath, "kprobe"}
break
}
}
opts := &loadOpts{
attach: KprobeAttach(load, bpfDir),
open: KprobeOpen(load),
ci: ci,
attach: KprobeAttach(load, bpfDir),
open: KprobeOpen(load),
tcMap: tc.name,
tcPrefix: tc.prefix,
}
return loadProgram(bpfDir, load, opts, verbose)
}
Expand Down Expand Up @@ -543,32 +547,34 @@ func LoadKprobeProgramAttachMany(bpfDir string, load *Program, syms []string, ve
}

func LoadUprobeProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
var tc tailCall
for mName, mPath := range load.PinMap {
if mName == "uprobe_calls" {
ci = &customInstall{mPath, "uprobe"}
tc = tailCall{mPath, "uprobe"}
break
}
}
opts := &loadOpts{
attach: UprobeAttach(load),
ci: ci,
attach: UprobeAttach(load),
tcMap: tc.name,
tcPrefix: tc.prefix,
}
return loadProgram(bpfDir, load, opts, verbose)
}

func LoadMultiKprobeProgram(bpfDir string, load *Program, verbose int) error {
var ci *customInstall
var tc tailCall
for mName, mPath := range load.PinMap {
if mName == "kprobe_calls" || mName == "retkprobe_calls" {
ci = &customInstall{mPath, "kprobe"}
tc = tailCall{mPath, "kprobe"}
break
}
}
opts := &loadOpts{
attach: MultiKprobeAttach(load, bpfDir),
open: KprobeOpen(load),
ci: ci,
attach: MultiKprobeAttach(load, bpfDir),
open: KprobeOpen(load),
tcMap: tc.name,
tcPrefix: tc.prefix,
}
return loadProgram(bpfDir, load, opts, verbose)
}
Expand Down Expand Up @@ -624,10 +630,11 @@ func LoadLSMProgram(bpfDir string, load *Program, verbose int) error {
}

func LoadMultiUprobeProgram(bpfDir string, load *Program, verbose int) error {
ci := &customInstall{fmt.Sprintf("%s-up_calls", load.PinPath), "uprobe"}
tc := tailCall{fmt.Sprintf("%s-up_calls", load.PinPath), "uprobe"}
opts := &loadOpts{
attach: MultiUprobeAttach(load),
ci: ci,
attach: MultiUprobeAttach(load),
tcMap: tc.name,
tcPrefix: tc.prefix,
}
return loadProgram(bpfDir, load, opts, verbose)
}
Expand Down Expand Up @@ -667,7 +674,7 @@ func slimVerifierError(errStr string) string {
return errStr[:headEnd] + "\n...\n" + errStr[tailStart:]
}

func installTailCalls(bpfDir string, spec *ebpf.CollectionSpec, coll *ebpf.Collection, ci *customInstall) error {
func installTailCalls(bpfDir string, spec *ebpf.CollectionSpec, coll *ebpf.Collection, loadOpts *loadOpts) error {
// FIXME(JM): This should be replaced by using the cilium/ebpf prog array initialization.

secToProgName := make(map[string]string)
Expand Down Expand Up @@ -705,8 +712,8 @@ func installTailCalls(bpfDir string, spec *ebpf.CollectionSpec, coll *ebpf.Colle
if err := install("tls_calls", "classifier"); err != nil {
return err
}
if ci != nil {
if err := install(ci.mapName, ci.secPrefix); err != nil {
if len(loadOpts.tcMap) != 0 {
if err := install(loadOpts.tcMap, loadOpts.tcPrefix); err != nil {
return err
}
}
Expand Down Expand Up @@ -845,7 +852,7 @@ func doLoadProgram(
}
defer coll.Close()

err = installTailCalls(bpfDir, spec, coll, loadOpts.ci)
err = installTailCalls(bpfDir, spec, coll, loadOpts)
if err != nil {
return nil, fmt.Errorf("installing tail calls failed: %s", err)
}
Expand Down