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 12, 2023
1 parent b9ca5d7 commit d80341d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pkg/selectors/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestParseMatchArg(t *testing.T) {
}

arg1 := &v1alpha1.ArgSelector{Index: 1, Operator: "Equal", Values: []string{"foobar"}}
k := &KernelSelectorState{off: 0}
k := NewKernelSelectorState(nil, true)
expected1 := []byte{
0x01, 0x00, 0x00, 0x00, // Index == 1
0x03, 0x00, 0x00, 0x00, // operator == equal
Expand Down
64 changes: 30 additions & 34 deletions pkg/selectors/selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ type KernelLPMTrieStringPostfix struct {
data [StringPostfixMaxLength]byte
}

var gStringMaps StringMapLists
var gStringPrefixMaps []map[KernelLPMTrieStringPrefix]struct{}
var gStringPostfixMaps []map[KernelLPMTrieStringPostfix]struct{}
type KernelSelectorMaps struct {
// 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{}
// stringPostfixMaps are used to populate string and char buf postfix matches
stringPostfixMaps []map[KernelLPMTrieStringPostfix]struct{}
}

var gMaps *KernelSelectorMaps

type KernelSelectorState struct {
off uint32 // offset into encoding
Expand All @@ -96,30 +103,19 @@ 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

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

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

Expand Down Expand Up @@ -179,15 +175,15 @@ func (k *KernelSelectorState) Addr6Maps() []map[KernelLPMTrie6]struct{} {
}

func (k *KernelSelectorState) StringMaps(subMap int) []map[[MaxStringMapsSize]byte]struct{} {
return k.stringMaps[subMap]
return k.maps.stringMaps[subMap]
}

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

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

// ValueMapsMaxEntries returns the maximum entries over all maps
Expand Down Expand Up @@ -226,7 +222,7 @@ func (k *KernelSelectorState) Addr6MapsMaxEntries() int {
// StringMapsMaxEntries returns the maximum entries over all maps inside a particular map of map
func (k *KernelSelectorState) StringMapsMaxEntries(subMap int) int {
maxEntries := 1
for _, vm := range k.stringMaps[subMap] {
for _, vm := range k.maps.stringMaps[subMap] {
if l := len(vm); l > maxEntries {
maxEntries = l
}
Expand All @@ -237,7 +233,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.maps.stringPrefixMaps {
if l := len(vm); l > maxEntries {
maxEntries = l
}
Expand All @@ -248,7 +244,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.maps.stringPostfixMaps {
if l := len(vm); l > maxEntries {
maxEntries = l
}
Expand Down Expand Up @@ -416,8 +412,8 @@ func (k *KernelSelectorState) insertStringMaps(stringMaps SelectorStringMaps) [S

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

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

func (k *KernelSelectorState) newStringPostfixMap() (uint32, map[KernelLPMTrieStringPostfix]struct{}) {
mapid := len(gStringPostfixMaps)
gStringPostfixMaps = append(gStringPostfixMaps, map[KernelLPMTrieStringPostfix]struct{}{})
return uint32(mapid), gStringPostfixMaps[mapid]
mapid := len(k.maps.stringPostfixMaps)
k.maps.stringPostfixMaps = append(k.maps.stringPostfixMaps, map[KernelLPMTrieStringPostfix]struct{}{})
return uint32(mapid), k.maps.stringPostfixMaps[mapid]
}

0 comments on commit d80341d

Please sign in to comment.