From d80341d1588583e9362d1e8607d8ad14e1594809 Mon Sep 17 00:00:00 2001 From: Anastasios Papagiannis Date: Thu, 12 Oct 2023 05:51:17 +0000 Subject: [PATCH] Test Signed-off-by: Anastasios Papagiannis --- pkg/selectors/kernel_test.go | 2 +- pkg/selectors/selectors.go | 64 +++++++++++++++++------------------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/pkg/selectors/kernel_test.go b/pkg/selectors/kernel_test.go index 3b60f9998ca..461ced7bee4 100644 --- a/pkg/selectors/kernel_test.go +++ b/pkg/selectors/kernel_test.go @@ -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 diff --git a/pkg/selectors/selectors.go b/pkg/selectors/selectors.go index d9bee46b338..423549b73e4 100644 --- a/pkg/selectors/selectors.go +++ b/pkg/selectors/selectors.go @@ -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 @@ -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, } } @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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] }