Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
  • Loading branch information
tpapagian committed Oct 11, 2023
1 parent 08b79a9 commit f60cccc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
7 changes: 3 additions & 4 deletions pkg/selectors/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,17 +1258,16 @@ func parseSelector(
//
// For some examples, see kernel_test.go
func InitKernelSelectors(selectors []v1alpha1.KProbeSelector, args []v1alpha1.KProbeArg, actionArgTable *idtable.Table) ([4096]byte, error) {
kernelSelectors, err := InitKernelSelectorState(selectors, args, actionArgTable, nil)
kernelSelectors, err := InitKernelSelectorState(selectors, args, actionArgTable, nil, true)
if err != nil {
return [4096]byte{}, err
}
return kernelSelectors.e, nil
}

func InitKernelSelectorState(selectors []v1alpha1.KProbeSelector, args []v1alpha1.KProbeArg,
actionArgTable *idtable.Table, listReader ValueReader) (*KernelSelectorState, error) {
kernelSelectors := NewKernelSelectorState(listReader)

actionArgTable *idtable.Table, listReader ValueReader, newMaps bool) (*KernelSelectorState, error) {
kernelSelectors := NewKernelSelectorState(listReader, newMaps)
WriteSelectorUint32(kernelSelectors, uint32(len(selectors)))
soff := make([]uint32, len(selectors))
for i := range selectors {
Expand Down
54 changes: 34 additions & 20 deletions pkg/selectors/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type KernelLPMTrieStringPostfix struct {
data [StringPostfixMaxLength]byte
}

var gStringMaps StringMapLists
var gStringPrefixMaps []map[KernelLPMTrieStringPrefix]struct{}
var gStringPostfixMaps []map[KernelLPMTrieStringPostfix]struct{}

type KernelSelectorState struct {
off uint32 // offset into encoding
e [4096]byte // kernel encoding of selectors
Expand All @@ -92,20 +96,30 @@ type KernelSelectorState struct {
newBinVals map[uint32]string // these should be added in the names_map

listReader ValueReader
// stringMaps are used to populate string and char buf matches
stringMaps StringMapLists

// stringMaps are used to populate string and char buf matches
stringMaps *StringMapLists
// stringPrefixMaps are used to populate string and char buf prefix matches
stringPrefixMaps []map[KernelLPMTrieStringPrefix]struct{}
stringPrefixMaps *[]map[KernelLPMTrieStringPrefix]struct{}
// stringPostfixMaps are used to populate string and char buf postfix matches
stringPostfixMaps []map[KernelLPMTrieStringPostfix]struct{}
stringPostfixMaps *[]map[KernelLPMTrieStringPostfix]struct{}
}

func NewKernelSelectorState(listReader ValueReader) *KernelSelectorState {
func NewKernelSelectorState(listReader ValueReader, newMaps bool) *KernelSelectorState {
if newMaps {
for i := 0; i < StringMapsNumSubMaps; i++ {
gStringMaps[i] = nil
}
gStringPrefixMaps = nil
gStringPostfixMaps = nil
}
return &KernelSelectorState{
matchBinaries: make(map[int]*MatchBinariesMappings),
newBinVals: make(map[uint32]string),
listReader: listReader,
matchBinaries: make(map[int]*MatchBinariesMappings),
newBinVals: make(map[uint32]string),
listReader: listReader,
stringMaps: &gStringMaps,
stringPrefixMaps: &gStringPrefixMaps,
stringPostfixMaps: &gStringPostfixMaps,
}
}

Expand Down Expand Up @@ -169,11 +183,11 @@ func (k *KernelSelectorState) StringMaps(subMap int) []map[[MaxStringMapsSize]by
}

func (k *KernelSelectorState) StringPrefixMaps() []map[KernelLPMTrieStringPrefix]struct{} {
return k.stringPrefixMaps
return *k.stringPrefixMaps
}

func (k *KernelSelectorState) StringPostfixMaps() []map[KernelLPMTrieStringPostfix]struct{} {
return k.stringPostfixMaps
return *k.stringPostfixMaps
}

// ValueMapsMaxEntries returns the maximum entries over all maps
Expand Down Expand Up @@ -223,7 +237,7 @@ func (k *KernelSelectorState) StringMapsMaxEntries(subMap int) int {
// StringPrefixMapsMaxEntries returns the maximum entries over all maps
func (k *KernelSelectorState) StringPrefixMapsMaxEntries() int {
maxEntries := 1
for _, vm := range k.stringPrefixMaps {
for _, vm := range *k.stringPrefixMaps {
if l := len(vm); l > maxEntries {
maxEntries = l
}
Expand All @@ -234,7 +248,7 @@ func (k *KernelSelectorState) StringPrefixMapsMaxEntries() int {
// StringPostfixMapsMaxEntries returns the maximum entries over all maps
func (k *KernelSelectorState) StringPostfixMapsMaxEntries() int {
maxEntries := 1
for _, vm := range k.stringPostfixMaps {
for _, vm := range *k.stringPostfixMaps {
if l := len(vm); l > maxEntries {
maxEntries = l
}
Expand Down Expand Up @@ -402,8 +416,8 @@ func (k *KernelSelectorState) insertStringMaps(stringMaps SelectorStringMaps) [S

for subMap := 0; subMap < StringMapsNumSubMaps; subMap++ {
if len(stringMaps[subMap]) > 0 {
mapid = uint32(len(k.stringMaps[subMap]))
k.stringMaps[subMap] = append(k.stringMaps[subMap], stringMaps[subMap])
mapid = uint32(len(gStringMaps[subMap]))
gStringMaps[subMap] = append(gStringMaps[subMap], stringMaps[subMap])
} else {
mapid = 0xffffffff
}
Expand All @@ -414,13 +428,13 @@ func (k *KernelSelectorState) insertStringMaps(stringMaps SelectorStringMaps) [S
}

func (k *KernelSelectorState) newStringPrefixMap() (uint32, map[KernelLPMTrieStringPrefix]struct{}) {
mapid := len(k.stringPrefixMaps)
k.stringPrefixMaps = append(k.stringPrefixMaps, map[KernelLPMTrieStringPrefix]struct{}{})
return uint32(mapid), k.stringPrefixMaps[mapid]
mapid := len(gStringPrefixMaps)
gStringPrefixMaps = append(gStringPrefixMaps, map[KernelLPMTrieStringPrefix]struct{}{})
return uint32(mapid), (gStringPrefixMaps)[mapid]
}

func (k *KernelSelectorState) newStringPostfixMap() (uint32, map[KernelLPMTrieStringPostfix]struct{}) {
mapid := len(k.stringPostfixMaps)
k.stringPostfixMaps = append(k.stringPostfixMaps, map[KernelLPMTrieStringPostfix]struct{}{})
return uint32(mapid), k.stringPostfixMaps[mapid]
mapid := len(gStringPostfixMaps)
gStringPostfixMaps = append(gStringPostfixMaps, map[KernelLPMTrieStringPostfix]struct{}{})
return uint32(mapid), (gStringPostfixMaps)[mapid]
}
11 changes: 6 additions & 5 deletions pkg/sensors/tracing/generickprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,11 @@ func createGenericKprobeSensor(
var progs []*program.Program
var maps []*program.Map
var multiIDs, multiRetIDs []idtable.EntryID
var useMulti bool

// use multi kprobe only if:
// - it's not disabled by user
// - there's support detected
useMulti = !option.Config.DisableKprobeMulti &&
useMulti := !option.Config.DisableKprobeMulti &&
bpf.HasKprobeMulti()

in := addKprobeIn{
Expand All @@ -528,7 +527,7 @@ func createGenericKprobeSensor(
kprobes[i].Syscall = syscall

for idx := range syms {
out, err := addKprobe(syms[idx], &kprobes[i], &in)
out, err := addKprobe(i, syms[idx], &kprobes[i], &in)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -581,7 +580,7 @@ func createGenericKprobeSensor(
// addKprobe will, amongst other things, create a generic kprobe entry and add
// it to the genericKprobeTable. The caller should make sure that this entry is
// properly removed on kprobe unload.
func addKprobe(funcName string, f *v1alpha1.KProbeSpec, in *addKprobeIn) (out *addKprobeOut, err error) {
func addKprobe(index int, funcName string, f *v1alpha1.KProbeSpec, in *addKprobeIn) (out *addKprobeOut, err error) {
var argSigPrinters []argPrinters
var argReturnPrinters []argPrinters
var setRetprobe bool
Expand Down Expand Up @@ -746,8 +745,10 @@ func addKprobe(funcName string, f *v1alpha1.KProbeSpec, in *addKprobeIn) (out *a
hasOverride: selectors.HasOverride(f),
}

fmt.Println("index ==== ", index)

// Parse Filters into kernel filter logic
kprobeEntry.loadArgs.selectors, err = selectors.InitKernelSelectorState(f.Selectors, f.Args, &kprobeEntry.actionArgs, nil)
kprobeEntry.loadArgs.selectors, err = selectors.InitKernelSelectorState(f.Selectors, f.Args, &kprobeEntry.actionArgs, nil, !in.useMulti || (in.useMulti && index == 0))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/tracing/generictracepoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func (tp *genericTracepoint) InitKernelSelectors(lists []v1alpha1.ListSpec) erro
}
}

selectors, err := selectors.InitKernelSelectorState(selSelectors, selArgs, &tp.actionArgs, &listReader{lists})
selectors, err := selectors.InitKernelSelectorState(selSelectors, selArgs, &tp.actionArgs, &listReader{lists}, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sensors/tracing/genericuprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func createGenericUprobeSensor(
}

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

0 comments on commit f60cccc

Please sign in to comment.