Debian Codegraph @

btf/feature.go

raw symbols SCIP
1 package btf
2
3 import (
4 "errors"
5 "math"
6
7 "github.com/cilium/ebpf/internal"
8 "github.com/cilium/ebpf/internal/sys"
9 "github.com/cilium/ebpf/internal/unix"
10 )
11
12 // haveBTF attempts to load a BTF blob containing an Int. It should pass on any
13 // kernel that supports BPF_BTF_LOAD.
14 var haveBTF = internal.NewFeatureTest("BTF", func() error {
15 // 0-length anonymous integer
16 err := probeBTF(&Int{})
17 if errors.Is(err, unix.EINVAL) || errors.Is(err, unix.EPERM) {
18 return internal.ErrNotSupported
19 }
20 return err
21 }, "4.18")
22
23 // haveMapBTF attempts to load a minimal BTF blob containing a Var. It is
24 // used as a proxy for .bss, .data and .rodata map support, which generally
25 // come with a Var and Datasec. These were introduced in Linux 5.2.
26 var haveMapBTF = internal.NewFeatureTest("Map BTF (Var/Datasec)", func() error {
27 if err := haveBTF(); err != nil {
28 return err
29 }
30
31 v := &Var{
32 Name: "a",
33 Type: &Pointer{(*Void)(nil)},
34 }
35
36 err := probeBTF(v)
37 if errors.Is(err, unix.EINVAL) || errors.Is(err, unix.EPERM) {
38 // Treat both EINVAL and EPERM as not supported: creating the map may still
39 // succeed without Btf* attrs.
40 return internal.ErrNotSupported
41 }
42 return err
43 }, "5.2")
44
45 // haveProgBTF attempts to load a BTF blob containing a Func and FuncProto. It
46 // is used as a proxy for ext_info (func_info) support, which depends on
47 // Func(Proto) by definition.
48 var haveProgBTF = internal.NewFeatureTest("Program BTF (func/line_info)", func() error {
49 if err := haveBTF(); err != nil {
50 return err
51 }
52
53 fn := &Func{
54 Name: "a",
55 Type: &FuncProto{Return: (*Void)(nil)},
56 }
57
58 err := probeBTF(fn)
59 if errors.Is(err, unix.EINVAL) || errors.Is(err, unix.EPERM) {
60 return internal.ErrNotSupported
61 }
62 return err
63 }, "5.0")
64
65 var haveFuncLinkage = internal.NewFeatureTest("BTF func linkage", func() error {
66 if err := haveProgBTF(); err != nil {
67 return err
68 }
69
70 fn := &Func{
71 Name: "a",
72 Type: &FuncProto{Return: (*Void)(nil)},
73 Linkage: GlobalFunc,
74 }
75
76 err := probeBTF(fn)
77 if errors.Is(err, unix.EINVAL) {
78 return internal.ErrNotSupported
79 }
80 return err
81 }, "5.6")
82
83 var haveDeclTags = internal.NewFeatureTest("BTF decl tags", func() error {
84 if err := haveBTF(); err != nil {
85 return err
86 }
87
88 t := &Typedef{
89 Name: "a",
90 Type: &Int{},
91 Tags: []string{"a"},
92 }
93
94 err := probeBTF(t)
95 if errors.Is(err, unix.EINVAL) {
96 return internal.ErrNotSupported
97 }
98 return err
99 }, "5.16")
100
101 var haveTypeTags = internal.NewFeatureTest("BTF type tags", func() error {
102 if err := haveBTF(); err != nil {
103 return err
104 }
105
106 t := &TypeTag{
107 Type: &Int{},
108 Value: "a",
109 }
110
111 err := probeBTF(t)
112 if errors.Is(err, unix.EINVAL) {
113 return internal.ErrNotSupported
114 }
115 return err
116 }, "5.17")
117
118 var haveEnum64 = internal.NewFeatureTest("ENUM64", func() error {
119 if err := haveBTF(); err != nil {
120 return err
121 }
122
123 enum := &Enum{
124 Size: 8,
125 Values: []EnumValue{
126 {"TEST", math.MaxUint32 + 1},
127 },
128 }
129
130 err := probeBTF(enum)
131 if errors.Is(err, unix.EINVAL) {
132 return internal.ErrNotSupported
133 }
134 return err
135 }, "6.0")
136
137 func probeBTF(typ Type) error {
138 b, err := NewBuilder([]Type{typ}, nil)
139 if err != nil {
140 return err
141 }
142
143 buf, err := b.Marshal(nil, nil)
144 if err != nil {
145 return err
146 }
147
148 fd, err := sys.BtfLoad(&sys.BtfLoadAttr{
149 Btf: sys.SlicePointer(buf),
150 BtfSize: uint32(len(buf)),
151 })
152
153 if err == nil {
154 fd.Close()
155 }
156
157 return err
158 }