...
1 package features
2
3 import (
4 "fmt"
5 "math"
6 "os"
7 "testing"
8
9 "github.com/cilium/ebpf"
10 "github.com/cilium/ebpf/internal/testutils"
11 )
12
13 var mapTypeMinVersion = map[ebpf.MapType]string{
14 ebpf.Hash: "3.19",
15 ebpf.Array: "3.19",
16 ebpf.ProgramArray: "4.2",
17 ebpf.PerfEventArray: "4.3",
18 ebpf.PerCPUHash: "4.6",
19 ebpf.PerCPUArray: "4.6",
20 ebpf.StackTrace: "4.6",
21 ebpf.CGroupArray: "4.8",
22 ebpf.LRUHash: "4.10",
23 ebpf.LRUCPUHash: "4.10",
24 ebpf.LPMTrie: "4.11",
25 ebpf.ArrayOfMaps: "4.12",
26 ebpf.HashOfMaps: "4.12",
27 ebpf.DevMap: "4.14",
28 ebpf.SockMap: "4.14",
29 ebpf.CPUMap: "4.15",
30 ebpf.XSKMap: "4.18",
31 ebpf.SockHash: "4.18",
32 ebpf.CGroupStorage: "4.19",
33 ebpf.ReusePortSockArray: "4.19",
34 ebpf.PerCPUCGroupStorage: "4.20",
35 ebpf.Queue: "4.20",
36 ebpf.Stack: "4.20",
37 ebpf.SkStorage: "5.2",
38 ebpf.DevMapHash: "5.4",
39 ebpf.StructOpsMap: "5.6",
40 ebpf.RingBuf: "5.8",
41 ebpf.InodeStorage: "5.10",
42 ebpf.TaskStorage: "5.11",
43 }
44
45 func TestHaveMapType(t *testing.T) {
46 for mt := ebpf.UnspecifiedMap + 1; mt <= mt.Max(); mt++ {
47 minVersion, ok := mapTypeMinVersion[mt]
48 if !ok {
49
50
51 minVersion = "0.0"
52 }
53
54 feature := fmt.Sprintf("map type %s", mt.String())
55
56 t.Run(mt.String(), func(t *testing.T) {
57 if mt == ebpf.StructOpsMap {
58 t.Skip("Test for map type StructOpsMap requires working probe")
59 }
60
61 testutils.SkipOnOldKernel(t, minVersion, feature)
62
63 if err := HaveMapType(mt); err != nil {
64 t.Fatalf("Map type %s isn't supported even though kernel is at least %s: %v", mt.String(), minVersion, err)
65 }
66 })
67
68 }
69
70 }
71
72 func TestHaveMapTypeUnsupported(t *testing.T) {
73 if err := haveMapType(ebpf.MapType(math.MaxUint32)); err != ebpf.ErrNotSupported {
74 t.Fatalf("Expected ebpf.ErrNotSupported but was: %v", err)
75 }
76 }
77
78 func TestHaveMapTypeInvalid(t *testing.T) {
79 if err := HaveMapType(ebpf.MapType(math.MaxUint32)); err != os.ErrInvalid {
80 t.Fatalf("Expected os.ErrInvalid but was: %v", err)
81 }
82
83 if err := HaveMapType(ebpf.MapType(ebpf.StructOpsMap)); err == nil {
84 t.Fatal("Expected but was nil")
85 }
86 }
87
View as plain text