Debian Codegraph @

variable.go

raw symbols SCIP
1 package ebpf
2
3 import (
4 "encoding/binary"
5 "fmt"
6 "io"
7 "reflect"
8 "slices"
9
10 "github.com/cilium/ebpf/btf"
11 "github.com/cilium/ebpf/internal/sysenc"
12 )
13
14 // VariableSpec is a convenience wrapper for modifying global variables of a
15 // CollectionSpec before loading it into the kernel.
16 //
17 // All operations on a VariableSpec's underlying MapSpec are performed in the
18 // host's native endianness.
19 type VariableSpec struct {
20 Name string
21 // Name of the section this variable was allocated in.
22 SectionName string
23 // Offset of the variable within the datasec.
24 Offset uint32
25 // Byte representation of the variable's value.
26 Value []byte
27 // Type information of the variable. Optional.
28 Type *btf.Var
29 }
30
31 // Set sets the value of the VariableSpec to the provided input using the host's
32 // native endianness.
33 func (s *VariableSpec) Set(in any) error {
34 size := int(s.Size())
35 if size == 0 {
36 bs := binary.Size(in)
37 if bs < 0 {
38 return fmt.Errorf("cannot determine binary size of value %v", in)
39 }
40 size = bs
41 }
42
43 if s.Value == nil {
44 s.Value = make([]byte, size)
45 }
46
47 buf, err := sysenc.Marshal(in, size)
48 if err != nil {
49 return fmt.Errorf("marshaling value %s: %w", s.Name, err)
50 }
51
52 buf.CopyTo(s.Value)
53 return nil
54 }
55
56 // Get writes the value of the VariableSpec to the provided output using the
57 // host's native endianness.
58 //
59 // Returns an error if the variable is not initialized or if the unmarshaling fails.
60 func (s *VariableSpec) Get(out any) error {
61 if s.Value == nil {
62 return fmt.Errorf("variable is not initialized")
63 }
64
65 if err := sysenc.Unmarshal(out, s.Value); err != nil {
66 return fmt.Errorf("unmarshaling value: %w", err)
67 }
68
69 return nil
70 }
71
72 // Size returns the size of the variable in bytes.
73 func (s *VariableSpec) Size() uint32 {
74 if s.Value != nil {
75 return uint32(len(s.Value))
76 }
77
78 if s.Type != nil {
79 size, err := btf.Sizeof(s.Type.Type)
80 if err != nil {
81 return 0
82 }
83 return uint32(size)
84 }
85
86 return 0
87 }
88
89 // Constant returns true if the variable is located in a data section intended
90 // for constant values.
91 func (s *VariableSpec) Constant() bool {
92 return isConstantDataSection(s.SectionName)
93 }
94
95 func (s *VariableSpec) String() string {
96 return fmt.Sprintf("%s (type=%v, section=%s, offset=%d, size=%d)", s.Name, s.Type, s.SectionName, s.Offset, s.Size())
97 }
98
99 // Copy the VariableSpec.
100 func (s *VariableSpec) Copy() *VariableSpec {
101 cpy := *s
102 cpy.Value = slices.Clone(s.Value)
103
104 if s.Type != nil {
105 cpy.Type = btf.Copy(s.Type).(*btf.Var)
106 }
107
108 return &cpy
109 }
110
111 // Variable is a convenience wrapper for modifying global variables of a
112 // Collection after loading it into the kernel. Operations on a Variable are
113 // performed using direct memory access, bypassing the BPF map syscall API.
114 //
115 // On kernels older than 5.5, most interactions with Variable return
116 // [ErrNotSupported].
117 type Variable struct {
118 name string
119 offset uint32
120 size uint32
121 t *btf.Var
122
123 mm *Memory
124 }
125
126 func newVariable(name string, offset, size uint32, t *btf.Var, mm *Memory) (*Variable, error) {
127 if mm != nil {
128 if !mm.bounds(offset, size) {
129 return nil, fmt.Errorf("offset %d(+%d) is out of bounds", offset, size)
130 }
131 }
132
133 return &Variable{
134 name: name,
135 offset: offset,
136 size: size,
137 t: t,
138 mm: mm,
139 }, nil
140 }
141
142 // Size returns the size of the variable.
143 func (v *Variable) Size() uint32 {
144 return v.size
145 }
146
147 // ReadOnly returns true if the Variable represents a variable that is read-only
148 // after loading the Collection into the kernel.
149 //
150 // On systems without BPF_F_MMAPABLE support, ReadOnly always returns true.
151 func (v *Variable) ReadOnly() bool {
152 if v.mm == nil {
153 return true
154 }
155 return v.mm.ReadOnly()
156 }
157
158 // Type returns the [btf.Var] representing the variable in its data section.
159 // This is useful for inspecting the variable's decl tags and the type
160 // information of the inner type.
161 //
162 // Returns nil if the original ELF object did not contain BTF information.
163 func (v *Variable) Type() *btf.Var {
164 return v.t
165 }
166
167 func (v *Variable) String() string {
168 return fmt.Sprintf("%s (type=%v)", v.name, v.t)
169 }
170
171 // Set the value of the Variable to the provided input. The input must marshal
172 // to the same length as the size of the Variable.
173 func (v *Variable) Set(in any) error {
174 if v.mm == nil {
175 return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
176 }
177
178 if v.ReadOnly() {
179 return fmt.Errorf("variable %s: %w", v.name, ErrReadOnly)
180 }
181
182 if !v.mm.bounds(v.offset, v.size) {
183 return fmt.Errorf("variable %s: access out of bounds: %w", v.name, io.EOF)
184 }
185
186 buf, err := sysenc.Marshal(in, int(v.size))
187 if err != nil {
188 return fmt.Errorf("marshaling value %s: %w", v.name, err)
189 }
190
191 if _, err := v.mm.WriteAt(buf.Bytes(), int64(v.offset)); err != nil {
192 return fmt.Errorf("writing value to %s: %w", v.name, err)
193 }
194
195 return nil
196 }
197
198 // Get writes the value of the Variable to the provided output. The output must
199 // be a pointer to a value whose size matches the Variable.
200 func (v *Variable) Get(out any) error {
201 if v.mm == nil {
202 return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
203 }
204
205 if !v.mm.bounds(v.offset, v.size) {
206 return fmt.Errorf("variable %s: access out of bounds: %w", v.name, io.EOF)
207 }
208
209 if err := sysenc.Unmarshal(out, v.mm.b[v.offset:v.offset+v.size]); err != nil {
210 return fmt.Errorf("unmarshaling value %s: %w", v.name, err)
211 }
212
213 return nil
214 }
215
216 func checkVariable[T any](v *Variable) error {
217 if v.ReadOnly() {
218 return ErrReadOnly
219 }
220
221 t := reflect.TypeFor[T]()
222 if t.Kind() == reflect.Uintptr && v.size == 8 {
223 // uintptr is 8 bytes on 64-bit and 4 on 32-bit. In BPF/BTF, pointers are
224 // always 8 bytes. For the sake of portability, allow accessing 8-byte BPF
225 // variables as uintptr on 32-bit systems, since the upper 32 bits of the
226 // pointer should be zero anyway.
227 return nil
228 }
229 if uintptr(v.size) != t.Size() {
230 return fmt.Errorf("can't create %d-byte accessor to %d-byte variable: %w", t.Size(), v.size, ErrInvalidType)
231 }
232
233 return nil
234 }
235
236 // VariablePointer returns a pointer to a variable of type T backed by memory
237 // shared with the BPF program. Requires building the Go application with -tags
238 // ebpf_unsafe_memory_experiment.
239 //
240 // T must contain only fixed-size, non-Go-pointer types: bools, floats,
241 // (u)int[8-64], arrays, and structs containing them. Structs must embed
242 // [structs.HostLayout]. [ErrInvalidType] is returned if T is not a valid type.
243 func VariablePointer[T comparable](v *Variable) (*T, error) {
244 if err := checkVariable[T](v); err != nil {
245 return nil, fmt.Errorf("variable pointer %s: %w", v.name, err)
246 }
247 return memoryPointer[T](v.mm, v.offset)
248 }