Skip to content

Commit

Permalink
tetragon: Add LoadProgramOpts function
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed May 30, 2024
1 parent 2210ffc commit d21f442
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type customInstall struct {
secPrefix string
}

type loadOpts struct {
type LoadOpts struct {
attach AttachFunc
open OpenFunc
ci *customInstall
Expand Down Expand Up @@ -483,15 +483,15 @@ func LoadTracepointProgram(bpfDir string, load *Program, verbose int) error {
break
}
}
opts := &loadOpts{
opts := &LoadOpts{
attach: TracepointAttach(load),
ci: ci,
}
return loadProgram(bpfDir, load, opts, verbose)
}

func LoadRawTracepointProgram(bpfDir string, load *Program, verbose int) error {
opts := &loadOpts{
opts := &LoadOpts{
attach: RawTracepointAttach(load),
}
return loadProgram(bpfDir, load, opts, verbose)
Expand All @@ -505,7 +505,7 @@ func LoadKprobeProgram(bpfDir string, load *Program, verbose int) error {
break
}
}
opts := &loadOpts{
opts := &LoadOpts{
attach: KprobeAttach(load, bpfDir),
open: KprobeOpen(load),
ci: ci,
Expand Down Expand Up @@ -536,7 +536,7 @@ func KprobeAttachMany(load *Program, syms []string) AttachFunc {
}

func LoadKprobeProgramAttachMany(bpfDir string, load *Program, syms []string, verbose int) error {
opts := &loadOpts{
opts := &LoadOpts{
attach: KprobeAttachMany(load, syms),
}
return loadProgram(bpfDir, load, opts, verbose)
Expand All @@ -550,7 +550,7 @@ func LoadUprobeProgram(bpfDir string, load *Program, verbose int) error {
break
}
}
opts := &loadOpts{
opts := &LoadOpts{
attach: UprobeAttach(load),
ci: ci,
}
Expand All @@ -565,7 +565,7 @@ func LoadMultiKprobeProgram(bpfDir string, load *Program, verbose int) error {
break
}
}
opts := &loadOpts{
opts := &LoadOpts{
attach: MultiKprobeAttach(load, bpfDir),
open: KprobeOpen(load),
ci: ci,
Expand All @@ -574,7 +574,7 @@ func LoadMultiKprobeProgram(bpfDir string, load *Program, verbose int) error {
}

func LoadFmodRetProgram(bpfDir string, load *Program, progName string, verbose int) error {
opts := &loadOpts{
opts := &LoadOpts{
attach: func(
_ *ebpf.Collection,
_ *ebpf.CollectionSpec,
Expand Down Expand Up @@ -610,14 +610,14 @@ func LoadFmodRetProgram(bpfDir string, load *Program, progName string, verbose i
}

func LoadTracingProgram(bpfDir string, load *Program, verbose int) error {
opts := &loadOpts{
opts := &LoadOpts{
attach: TracingAttach(),
}
return loadProgram(bpfDir, load, opts, verbose)
}

func LoadLSMProgram(bpfDir string, load *Program, verbose int) error {
opts := &loadOpts{
opts := &LoadOpts{
attach: LSMAttach(),
}
return loadProgram(bpfDir, load, opts, verbose)
Expand All @@ -632,7 +632,7 @@ func LoadMultiUprobeProgram(bpfDir string, load *Program, verbose int) error {
break
}
}
opts := &loadOpts{
opts := &LoadOpts{
attach: MultiUprobeAttach(load),
ci: ci,
}
Expand Down Expand Up @@ -724,7 +724,7 @@ func installTailCalls(bpfDir string, spec *ebpf.CollectionSpec, coll *ebpf.Colle
func doLoadProgram(
bpfDir string,
load *Program,
loadOpts *loadOpts,
LoadOpts *LoadOpts,
verbose int,
) (*LoadedCollection, error) {
var btfSpec *btf.Spec
Expand All @@ -748,8 +748,8 @@ func doLoadProgram(
return nil, fmt.Errorf("loading collection spec failed: %w", err)
}

if loadOpts.open != nil {
if err := loadOpts.open(spec); err != nil {
if LoadOpts.open != nil {
if err := LoadOpts.open(spec); err != nil {
return nil, fmt.Errorf("open spec function failed: %w", err)
}
}
Expand Down Expand Up @@ -853,7 +853,7 @@ func doLoadProgram(
}
defer coll.Close()

err = installTailCalls(bpfDir, spec, coll, loadOpts.ci)
err = installTailCalls(bpfDir, spec, coll, LoadOpts.ci)
if err != nil {
return nil, fmt.Errorf("installing tail calls failed: %s", err)
}
Expand Down Expand Up @@ -897,7 +897,7 @@ func doLoadProgram(
return nil, fmt.Errorf("pinning '%s' to '%s' failed: %w", load.Label, pinPath, err)
}

load.unloader, err = loadOpts.attach(coll, spec, prog, progSpec)
load.unloader, err = LoadOpts.attach(coll, spec, prog, progSpec)
if err != nil {
if err := prog.Unpin(); err != nil {
logger.GetLogger().Warnf("Unpinning '%s' failed: %w", pinPath, err)
Expand All @@ -913,32 +913,32 @@ func doLoadProgram(
}

// The loadProgram loads and attach bpf object @load. It is expected that user
// provides @loadOpts with mandatory attach function and optional open function.
// provides @LoadOpts with mandatory attach function and optional open function.
//
// The load process is roughly as follows:
//
// - load object | ebpf.LoadCollectionSpec
// - open callback | loadOpts.open(spec)
// - open callback | LoadOpts.open(spec)
// - open refferenced maps |
// - creates collection | ebpf.NewCollectionWithOptions(spec, opts)
// - install tail calls | loadOpts.ci
// - install tail calls | LoadOpts.ci
// - load maps with values |
// - pin main program |
// - attach callback | loadOpts.attach(coll, spec, prog, progSpec)
// - attach callback | LoadOpts.attach(coll, spec, prog, progSpec)
// - print loaded progs/maps | if KeepCollection == true
//
// The @loadOpts.open callback can be used to customize ebpf.CollectionSpec
// The @LoadOpts.open callback can be used to customize ebpf.CollectionSpec
// before it's loaded into kernel (like disable/enable programs).
//
// The @loadOpts.attach callback is used to actually attach main object program
// The @LoadOpts.attach callback is used to actually attach main object program
// to desired function/symbol/whatever..
//
// The @loadOpts.ci defines specific installation of tailcalls in object.
// The @LoadOpts.ci defines specific installation of tailcalls in object.

func loadProgram(
bpfDir string,
load *Program,
opts *loadOpts,
opts *LoadOpts,
verbose int,
) error {

Expand All @@ -964,6 +964,15 @@ func LoadProgram(
attach AttachFunc,
verbose int,
) error {
opts := &loadOpts{attach: attach}
opts := &LoadOpts{attach: attach}
return loadProgram(bpfDir, load, opts, verbose)
}

func LoadProgramOpts(
bpfDir string,
load *Program,
opts *LoadOpts,
verbose int,
) error {
return loadProgram(bpfDir, load, opts, verbose)
}

0 comments on commit d21f442

Please sign in to comment.