Skip to content

Commit e9ebab7

Browse files
authored
NETOBSERV-2364: Typo in packet record map name used with bpfman (#811)
* Create maps package * Fix map name typo * Integrate map name consistency check * remove verify-maps make target * fix formatting
1 parent 0000a2a commit e9ebab7

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

.mk/bc.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define MAPS
3232
"direct_flows":"ringbuf",
3333
"aggregated_flows":"hash",
3434
"additional_flow_metrics":"per_cpu_hash",
35-
"packets_record":"ringbuf",
35+
"packet_record":"ringbuf",
3636
"dns_flows":"hash",
3737
"global_counters":"per_cpu_array",
3838
"filter_map":"lpm_trie",

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ test: ## Test code using go test
164164
@echo "### Testing code"
165165
GOOS=$(GOOS) go test -mod vendor ./pkg/... ./cmd/... -coverpkg=./... -coverprofile cover.all.out
166166

167+
.PHONY: verify-maps
168+
verify-maps: ## Verify map names consistency across all sources
169+
@echo "### Verifying map names consistency"
170+
go test -v ./pkg/maps
171+
167172
.PHONY: test-race
168173
test-race: ## Test code using go test -race
169174
@echo "### Testing code for race conditions"

pkg/maps/maps.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package maps
2+
3+
// Map names
4+
var Maps = []string{
5+
"direct_flows",
6+
"aggregated_flows",
7+
"additional_flow_metrics",
8+
"packet_record",
9+
"dns_flows",
10+
"global_counters",
11+
"filter_map",
12+
"peer_filter_map",
13+
"ipsec_ingress_map",
14+
"ipsec_egress_map",
15+
}

pkg/maps/maps_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package maps
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"reflect"
9+
"regexp"
10+
"sort"
11+
"strings"
12+
"testing"
13+
14+
"github.com/netobserv/netobserv-ebpf-agent/pkg/ebpf"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestMapNamesMatchEBPFDefinitions(t *testing.T) {
20+
// Extract map names from netobserv-ebpf-agent/pkg/ebpf
21+
ebpfMaps := extractEBPFMapNames()
22+
23+
// Get map names from our package
24+
packageMaps := Maps
25+
26+
// Sort both slices for comparison
27+
sort.Strings(ebpfMaps)
28+
sort.Strings(packageMaps)
29+
fmt.Printf("eBPF maps: %+v\n", ebpfMaps)
30+
fmt.Printf("Package maps: %+v\n", packageMaps)
31+
32+
// Verify they match
33+
assert.Equal(t, ebpfMaps, packageMaps,
34+
"Map names in pkg/maps/maps.go do not match eBPF definitions in bpf/maps_definition.h")
35+
}
36+
37+
func TestMapNamesMatchBcMk(t *testing.T) {
38+
// Extract map names from .mk/bc.mk
39+
bcMaps, err := extractBcMkMapNames("../../.mk/bc.mk")
40+
require.NoError(t, err, "Failed to extract map names from .mk/bc.mk")
41+
42+
// Get map names from our package
43+
packageMaps := Maps
44+
45+
// Sort both slices for comparison
46+
sort.Strings(bcMaps)
47+
sort.Strings(packageMaps)
48+
fmt.Printf("bc.mk maps: %+v\n", bcMaps)
49+
fmt.Printf("Package maps: %+v\n", packageMaps)
50+
51+
// Verify they match
52+
assert.Equal(t, bcMaps, packageMaps,
53+
"Map names in pkg/maps/maps.go do not match definitions in .mk/bc.mk.\n"+
54+
"If you've added/removed maps, please update both files.")
55+
}
56+
57+
// extractBcMkMapNames parses the .mk/bc.mk file and extracts map names from the MAPS JSON
58+
func extractBcMkMapNames(filePath string) ([]string, error) {
59+
content, err := os.ReadFile(filePath)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
// Find the MAPS definition block
65+
mapsRegex := regexp.MustCompile(`define MAPS\s*\n((.|\s)*?)\nendef`)
66+
matches := mapsRegex.FindStringSubmatch(string(content))
67+
68+
if len(matches) < 2 {
69+
return nil, errors.New("Could not find MAPS definition in .mk/bc.mk")
70+
}
71+
72+
// Extract the JSON content
73+
jsonContent := strings.TrimSpace(matches[1])
74+
75+
// Parse JSON to extract map names
76+
var mapsData map[string]string
77+
if err := json.Unmarshal([]byte(jsonContent), &mapsData); err != nil {
78+
return nil, err
79+
}
80+
81+
// Extract just the map names (keys)
82+
var mapNames []string
83+
for mapName := range mapsData {
84+
mapNames = append(mapNames, mapName)
85+
}
86+
87+
return mapNames, nil
88+
}
89+
90+
// extractEBPFMapNames extracts map names from the compiled eBPF agent
91+
func extractEBPFMapNames() []string {
92+
var maps []string
93+
mapType := reflect.ValueOf(ebpf.BpfMapSpecs{})
94+
for i := 0; i < mapType.NumField(); i++ {
95+
val := reflect.Indirect(mapType)
96+
mapName := val.Type().Field(i).Tag.Get("ebpf")
97+
98+
if mapName != "" {
99+
maps = append(maps, mapName)
100+
}
101+
}
102+
103+
return maps
104+
}

0 commit comments

Comments
 (0)