Skip to content

Commit

Permalink
tetragon: Add support to attach kprobe session program
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Dec 25, 2024
1 parent f9597aa commit c987472
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ func MultiKprobeAttach(load *Program, bpfDir string) AttachFunc {

opts := link.KprobeMultiOptions{
Symbols: data.Overrides,
Session: data.Session,
}

load.unloaderOverride, err = multiKprobeAttach(load, progOverride, progOverrideSpec, opts, bpfDir, "override")
Expand All @@ -520,6 +521,7 @@ func MultiKprobeAttach(load *Program, bpfDir string) AttachFunc {
opts := link.KprobeMultiOptions{
Symbols: data.Symbols,
Cookies: data.Cookies,
Session: data.Session,
}

return multiKprobeAttach(load, prog, spec, opts, bpfDir)
Expand Down
1 change: 1 addition & 0 deletions pkg/sensors/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type MultiKprobeAttachData struct {
Symbols []string
Cookies []uint64
Overrides []string
Session bool
}

type UprobeAttachData struct {
Expand Down
60 changes: 47 additions & 13 deletions pkg/sensors/tracing/generickprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ type pendingEventKey struct {
ktimeEnter uint64
}

type multiKprobeLoaderData struct {
ids []idtable.EntryID
session bool
}

type genericKprobeData struct {
// stackTraceMap reference is needed when retrieving stack traces from
// userspace when receiving events containing stacktrace IDs
Expand Down Expand Up @@ -262,6 +267,7 @@ func createMultiKprobeSensor(policyName string, multiIDs []idtable.EntryID, has
var multiRetIDs []idtable.EntryID
var progs []*program.Program
var maps []*program.Map
var load *program.Program

data := &genericKprobeData{}

Expand Down Expand Up @@ -290,14 +296,34 @@ func createMultiKprobeSensor(policyName string, multiIDs []idtable.EntryID, has
loadProgRetName = "bpf_multi_retkprobe_v511.o"
}

load := program.Builder(
path.Join(option.Config.HubbleLib, loadProgName),
fmt.Sprintf("kprobe_multi (%d functions)", len(multiIDs)),
"kprobe.multi/generic_kprobe",
"multi_kprobe",
"generic_kprobe").
SetLoaderData(multiIDs).
SetPolicy(policyName)
useSession := bpf.HasKprobeSession() && !has.override

if useSession {
load = program.Builder(
path.Join(option.Config.HubbleLib, "bpf_session_kprobe.o"),
fmt.Sprintf("kprobe_session (%d functions)", len(multiIDs)),
"kprobe.session/generic_kprobe",
"session_kprobe",
"generic_kprobe").
SetLoaderData(multiKprobeLoaderData{
ids: multiIDs,
session: useSession,
}).
SetPolicy(policyName)
} else {
load = program.Builder(
path.Join(option.Config.HubbleLib, loadProgName),
fmt.Sprintf("kprobe_multi (%d functions)", len(multiIDs)),
"kprobe.multi/generic_kprobe",
"multi_kprobe",
"generic_kprobe").
SetLoaderData(multiIDs).
SetLoaderData(multiKprobeLoaderData{
ids: multiIDs,
}).
SetPolicy(policyName)
}

progs = append(progs, load)

fdinstall := program.MapBuilderSensor("fdinstall_map", load)
Expand Down Expand Up @@ -367,6 +393,12 @@ func createMultiKprobeSensor(policyName string, multiIDs []idtable.EntryID, has
}
maps = append(maps, overrideTasksMap)

if useSession {
retTailCalls := program.MapBuilderProgram("retkprobe_calls", load)
maps = append(maps, retTailCalls)
return progs, maps, nil
}

if len(multiRetIDs) != 0 {
loadret := program.Builder(
path.Join(option.Config.HubbleLib, loadProgRetName),
Expand All @@ -375,7 +407,9 @@ func createMultiKprobeSensor(policyName string, multiIDs []idtable.EntryID, has
"multi_retkprobe",
"generic_kprobe").
SetRetProbe(true).
SetLoaderData(multiRetIDs).
SetLoaderData(multiKprobeLoaderData{
ids: multiRetIDs,
}).
SetPolicy(policyName)
progs = append(progs, loadret)

Expand Down Expand Up @@ -1096,10 +1130,10 @@ func loadSingleKprobeSensor(id idtable.EntryID, bpfDir string, load *program.Pro
return err
}

func loadMultiKprobeSensor(ids []idtable.EntryID, bpfDir string, load *program.Program, verbose int) error {
func loadMultiKprobeSensor(ids []idtable.EntryID, session bool, bpfDir string, load *program.Program, verbose int) error {
bin_buf := make([]bytes.Buffer, len(ids))

data := &program.MultiKprobeAttachData{}
data := &program.MultiKprobeAttachData{Session: session}

for index, id := range ids {
gk, err := genericKprobeTableGet(id)
Expand Down Expand Up @@ -1144,8 +1178,8 @@ func loadGenericKprobeSensor(bpfDir string, load *program.Program, verbose int)
if id, ok := load.LoaderData.(idtable.EntryID); ok {
return loadSingleKprobeSensor(id, bpfDir, load, verbose)
}
if ids, ok := load.LoaderData.([]idtable.EntryID); ok {
return loadMultiKprobeSensor(ids, bpfDir, load, verbose)
if data, ok := load.LoaderData.(multiKprobeLoaderData); ok {
return loadMultiKprobeSensor(data.ids, data.session, bpfDir, load, verbose)
}
return fmt.Errorf("invalid loadData type: expecting idtable.EntryID/[] and got: %T (%v)",
load.LoaderData, load.LoaderData)
Expand Down

0 comments on commit c987472

Please sign in to comment.