Skip to content

Commit

Permalink
tetragon: Factor uprobe sensor setup
Browse files Browse the repository at this point in the history
Separating uprobe sensor setup into addUprobe function, that creates
and sets up uprobeEntry object and createSingleUprobeSensor that
creates the actual sensor progs/maps arrays.

This way we can easily add multi uprobe sensor create function in
following changes.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed Feb 11, 2024
1 parent 4a27e0a commit 3df2d56
Showing 1 changed file with 150 additions and 103 deletions.
253 changes: 150 additions & 103 deletions pkg/sensors/tracing/genericuprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,136 +170,183 @@ func isValidUprobeSelectors(selectors []v1alpha1.KProbeSelector) error {
return nil
}

type addUprobeIn struct {
sensorPath string
policyName string
}

func createGenericUprobeSensor(
name string,
uprobes []v1alpha1.UProbeSpec,
policyName string,
) (*sensors.Sensor, error) {
var progs []*program.Program
var maps []*program.Map
var ids []idtable.EntryID
var err error

sensorPath := name

loadProgName := "bpf_generic_uprobe.o"
if kernels.EnableV61Progs() {
loadProgName = "bpf_generic_uprobe_v61.o"
} else if kernels.EnableLargeProgs() {
loadProgName = "bpf_generic_uprobe_v53.o"
in := addUprobeIn{
sensorPath: name,
policyName: policyName,
}

for _, spec := range uprobes {
var args []v1alpha1.KProbeArg

if err := isValidUprobeSelectors(spec.Selectors); err != nil {
ids, err = addUprobe(&spec, ids, &in)
if err != nil {
return nil, err
}
}

progs, maps, err = createSingleUprobeSensor(name, ids)
if err != nil {
return nil, err
}

// Parse Filters into kernel filter logic
uprobeSelectorState, err := selectors.InitKernelSelectorState(spec.Selectors, args, nil, nil, nil)
return &sensors.Sensor{
Name: name,
Progs: progs,
Maps: maps,
}, nil
}

func addUprobe(spec *v1alpha1.UProbeSpec, ids []idtable.EntryID, in *addUprobeIn) ([]idtable.EntryID, error) {
var args []v1alpha1.KProbeArg

if err := isValidUprobeSelectors(spec.Selectors); err != nil {
return nil, err
}

// Parse Filters into kernel filter logic
uprobeSelectorState, err := selectors.InitKernelSelectorState(spec.Selectors, args, nil, nil, nil)
if err != nil {
return nil, err
}

msgField, err := getPolicyMessage(spec.Message)
if errors.Is(err, ErrMsgSyntaxShort) || errors.Is(err, ErrMsgSyntaxEscape) {
return nil, err
} else if errors.Is(err, ErrMsgSyntaxLong) {
logger.GetLogger().WithField("policy-name", in.policyName).
Warnf("TracingPolicy 'message' field too long, truncated to %d characters", TpMaxMessageLen)
}

var (
argTypes [api.EventConfigMaxArgs]int32
argMeta [api.EventConfigMaxArgs]uint32
argSet [api.EventConfigMaxArgs]bool

argPrinters []argPrinter
)

// Parse Arguments
for i, a := range spec.Args {
argType := gt.GenericTypeFromString(a.Type)
if argType == gt.GenericInvalidType {
return nil, fmt.Errorf("Arg(%d) type '%s' unsupported", i, a.Type)
}
argMValue, err := getMetaValue(&a)
if err != nil {
return nil, err
}
if a.Index > 4 {
return nil, fmt.Errorf("Error add arg: ArgType %s Index %d out of bounds",
a.Type, int(a.Index))
}
argTypes[a.Index] = int32(argType)
argMeta[a.Index] = uint32(argMValue)
argSet[a.Index] = true

msgField, err := getPolicyMessage(spec.Message)
if errors.Is(err, ErrMsgSyntaxShort) || errors.Is(err, ErrMsgSyntaxEscape) {
return nil, err
} else if errors.Is(err, ErrMsgSyntaxLong) {
logger.GetLogger().WithField("policy-name", policyName).Warnf("TracingPolicy 'message' field too long, truncated to %d characters", TpMaxMessageLen)
argPrinters = append(argPrinters, argPrinter{index: i, ty: argType})
}

// Mark remaining arguments as 'nops' the kernel side will skip
// copying 'nop' args.
for i, a := range argSet {
if !a {
argTypes[i] = gt.GenericNopType
argMeta[i] = 0
}
}

var (
argTypes [api.EventConfigMaxArgs]int32
argMeta [api.EventConfigMaxArgs]uint32
argSet [api.EventConfigMaxArgs]bool

argPrinters []argPrinter
)

// Parse Arguments
for i, a := range spec.Args {
argType := gt.GenericTypeFromString(a.Type)
if argType == gt.GenericInvalidType {
return nil, fmt.Errorf("Arg(%d) type '%s' unsupported", i, a.Type)
}
argMValue, err := getMetaValue(&a)
if err != nil {
return nil, err
}
if a.Index > 4 {
return nil, fmt.Errorf("Error add arg: ArgType %s Index %d out of bounds",
a.Type, int(a.Index))
}
argTypes[a.Index] = int32(argType)
argMeta[a.Index] = uint32(argMValue)
argSet[a.Index] = true

argPrinters = append(argPrinters, argPrinter{index: i, ty: argType})
for _, sym := range spec.Symbols {
config := &api.EventConfig{
Arg: argTypes,
ArgM: argMeta,
}

// Mark remaining arguments as 'nops' the kernel side will skip
// copying 'nop' args.
for i, a := range argSet {
if !a {
argTypes[i] = gt.GenericNopType
argMeta[i] = 0
}
uprobeEntry := &genericUprobe{
tableId: idtable.UninitializedEntryID,
config: config,
path: spec.Path,
symbol: sym,
selectors: uprobeSelectorState,
policyName: in.policyName,
message: msgField,
argPrinters: argPrinters,
}

for _, sym := range spec.Symbols {
config := &api.EventConfig{
Arg: argTypes,
ArgM: argMeta,
}

uprobeEntry := &genericUprobe{
tableId: idtable.UninitializedEntryID,
config: config,
path: spec.Path,
symbol: sym,
selectors: uprobeSelectorState,
policyName: policyName,
message: msgField,
argPrinters: argPrinters,
}

uprobeTable.AddEntry(uprobeEntry)
id := uprobeEntry.tableId.ID

uprobeEntry.pinPathPrefix = sensors.PathJoin(sensorPath, fmt.Sprintf("%d", id))
config.FuncId = uint32(id)

pinPath := uprobeEntry.pinPathPrefix
pinProg := sensors.PathJoin(pinPath, "prog")

attachData := &program.UprobeAttachData{
Path: spec.Path,
Symbol: sym,
}

load := program.Builder(
path.Join(option.Config.HubbleLib, loadProgName),
"",
"uprobe/generic_uprobe",
pinProg,
"generic_uprobe").
SetAttachData(attachData).
SetLoaderData(uprobeEntry)

progs = append(progs, load)

configMap := program.MapBuilderPin("config_map", sensors.PathJoin(pinPath, "config_map"), load)
tailCalls := program.MapBuilderPin("uprobe_calls", sensors.PathJoin(pinPath, "up_calls"), load)
filterMap := program.MapBuilderPin("filter_map", sensors.PathJoin(pinPath, "filter_map"), load)
selMatchBinariesMap := program.MapBuilderPin("tg_mb_sel_opts", sensors.PathJoin(pinPath, "tg_mb_sel_opts"), load)
maps = append(maps, configMap, tailCalls, filterMap, selMatchBinariesMap)
uprobeTable.AddEntry(uprobeEntry)
id := uprobeEntry.tableId

uprobeEntry.pinPathPrefix = sensors.PathJoin(in.sensorPath, fmt.Sprintf("%d", id.ID))
config.FuncId = uint32(id.ID)

ids = append(ids, id)
}

return ids, nil
}

func createSingleUprobeSensor(sensorPath string, ids []idtable.EntryID) ([]*program.Program, []*program.Map, error) {
var progs []*program.Program
var maps []*program.Map

for _, id := range ids {
uprobeEntry, err := genericUprobeTableGet(id)
if err != nil {
return nil, nil, err
}
progs, maps = createUprobeSensorFromEntry(uprobeEntry, sensorPath, progs, maps)
}

return &sensors.Sensor{
Name: name,
Progs: progs,
Maps: maps,
}, nil
return progs, maps, nil
}

func createUprobeSensorFromEntry(uprobeEntry *genericUprobe, sensorPath string,

Check warning on line 315 in pkg/sensors/tracing/genericuprobe.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unused-parameter: parameter 'sensorPath' seems to be unused, consider removing or renaming it as _ (revive)
progs []*program.Program, maps []*program.Map) ([]*program.Program, []*program.Map) {

loadProgName := "bpf_generic_uprobe.o"
if kernels.EnableV61Progs() {
loadProgName = "bpf_generic_uprobe_v61.o"
} else if kernels.EnableLargeProgs() {
loadProgName = "bpf_generic_uprobe_v53.o"
}

pinPath := uprobeEntry.pinPathPrefix
pinProg := sensors.PathJoin(pinPath, "prog")

attachData := &program.UprobeAttachData{
Path: uprobeEntry.path,
Symbol: uprobeEntry.symbol,
}

load := program.Builder(
path.Join(option.Config.HubbleLib, loadProgName),
"",
"uprobe/generic_uprobe",
pinProg,
"generic_uprobe").
SetAttachData(attachData).
SetLoaderData(uprobeEntry)

progs = append(progs, load)

configMap := program.MapBuilderPin("config_map", sensors.PathJoin(pinPath, "config_map"), load)
tailCalls := program.MapBuilderPin("uprobe_calls", sensors.PathJoin(pinPath, "up_calls"), load)
filterMap := program.MapBuilderPin("filter_map", sensors.PathJoin(pinPath, "filter_map"), load)
selMatchBinariesMap := program.MapBuilderPin("tg_mb_sel_opts", sensors.PathJoin(pinPath, "tg_mb_sel_opts"), load)
maps = append(maps, configMap, tailCalls, filterMap, selMatchBinariesMap)
return progs, maps
}

func (k *observerUprobeSensor) PolicyHandler(
Expand Down

0 comments on commit 3df2d56

Please sign in to comment.