forked from cilium/ebpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.go
228 lines (187 loc) · 6.09 KB
/
variable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package ebpf
import (
"fmt"
"io"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal/sysenc"
)
// VariableSpec is a convenience wrapper for modifying global variables of a
// CollectionSpec before loading it into the kernel.
//
// All operations on a VariableSpec's underlying MapSpec are performed in the
// host's native endianness.
type VariableSpec struct {
name string
offset uint64
size uint64
m *MapSpec
t *btf.Var
}
// Set sets the value of the VariableSpec to the provided input using the host's
// native endianness.
func (s *VariableSpec) Set(in any) error {
buf, err := sysenc.Marshal(in, int(s.size))
if err != nil {
return fmt.Errorf("marshaling value %s: %w", s.name, err)
}
b, _, err := s.m.dataSection()
if err != nil {
return fmt.Errorf("getting data section of map %s: %w", s.m.Name, err)
}
if int(s.offset+s.size) > len(b) {
return fmt.Errorf("offset %d(+%d) for variable %s is out of bounds", s.offset, s.size, s.name)
}
// MapSpec.Copy() performs a shallow copy. Fully copy the byte slice
// to avoid any changes affecting other copies of the MapSpec.
cpy := make([]byte, len(b))
copy(cpy, b)
buf.CopyTo(cpy[s.offset : s.offset+s.size])
s.m.Contents[0] = MapKV{Key: uint32(0), Value: cpy}
return nil
}
// Get writes the value of the VariableSpec to the provided output using the
// host's native endianness.
func (s *VariableSpec) Get(out any) error {
b, _, err := s.m.dataSection()
if err != nil {
return fmt.Errorf("getting data section of map %s: %w", s.m.Name, err)
}
if int(s.offset+s.size) > len(b) {
return fmt.Errorf("offset %d(+%d) for variable %s is out of bounds", s.offset, s.size, s.name)
}
if err := sysenc.Unmarshal(out, b[s.offset:s.offset+s.size]); err != nil {
return fmt.Errorf("unmarshaling value: %w", err)
}
return nil
}
// Size returns the size of the variable in bytes.
func (s *VariableSpec) Size() uint64 {
return s.size
}
// MapName returns the name of the underlying MapSpec.
func (s *VariableSpec) MapName() string {
return s.m.Name
}
// Offset returns the offset of the variable in the underlying MapSpec.
func (s *VariableSpec) Offset() uint64 {
return s.offset
}
// Constant returns true if the VariableSpec represents a variable that is
// read-only from the perspective of the BPF program.
func (s *VariableSpec) Constant() bool {
return s.m.readOnly()
}
// Type returns the [btf.Var] representing the variable in its data section.
// This is useful for inspecting the variable's decl tags and the type
// information of the inner type.
//
// Returns nil if the original ELF object did not contain BTF information.
func (s *VariableSpec) Type() *btf.Var {
return s.t
}
func (s *VariableSpec) String() string {
return fmt.Sprintf("%s (type=%v, map=%s, offset=%d, size=%d)", s.name, s.t, s.m.Name, s.offset, s.size)
}
// copy returns a new VariableSpec with the same values as the original,
// but with a different underlying MapSpec. This is useful when copying a
// CollectionSpec. Returns nil if a MapSpec with the same name is not found.
func (s *VariableSpec) copy(cpy *CollectionSpec) *VariableSpec {
out := &VariableSpec{
name: s.name,
offset: s.offset,
size: s.size,
t: s.t,
}
// Attempt to find a MapSpec with the same name in the copied CollectionSpec.
for _, m := range cpy.Maps {
if m.Name == s.m.Name {
out.m = m
return out
}
}
return nil
}
// Variable is a convenience wrapper for modifying global variables of a
// Collection after loading it into the kernel. Operations on a Variable are
// performed using direct memory access, bypassing the BPF map syscall API.
//
// On kernels older than 5.5, most interactions with Variable return
// [ErrNotSupported].
type Variable struct {
name string
offset uint64
size uint64
t *btf.Var
mm *Memory
}
func newVariable(name string, offset, size uint64, t *btf.Var, mm *Memory) (*Variable, error) {
if mm != nil {
if int(offset+size) > mm.Size() {
return nil, fmt.Errorf("offset %d(+%d) is out of bounds", offset, size)
}
}
return &Variable{
name: name,
offset: offset,
size: size,
t: t,
mm: mm,
}, nil
}
// Size returns the size of the variable.
func (v *Variable) Size() uint64 {
return v.size
}
// ReadOnly returns true if the Variable represents a variable that is read-only
// after loading the Collection into the kernel.
//
// On systems without BPF_F_MMAPABLE support, ReadOnly always returns true.
func (v *Variable) ReadOnly() bool {
if v.mm == nil {
return true
}
return v.mm.ReadOnly()
}
// Type returns the [btf.Var] representing the variable in its data section.
// This is useful for inspecting the variable's decl tags and the type
// information of the inner type.
//
// Returns nil if the original ELF object did not contain BTF information.
func (v *Variable) Type() *btf.Var {
return v.t
}
func (v *Variable) String() string {
return fmt.Sprintf("%s (type=%v)", v.name, v.t)
}
// Set the value of the Variable to the provided input. The input must marshal
// to the same length as the size of the Variable.
func (v *Variable) Set(in any) error {
if v.mm == nil {
return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
}
if v.ReadOnly() {
return fmt.Errorf("variable %s: %w", v.name, ErrReadOnly)
}
buf, err := sysenc.Marshal(in, int(v.size))
if err != nil {
return fmt.Errorf("marshaling value %s: %w", v.name, err)
}
if _, err := v.mm.WriteAt(buf.Bytes(), int64(v.offset)); err != nil {
return fmt.Errorf("writing value to %s: %w", v.name, err)
}
return nil
}
// Get writes the value of the Variable to the provided output. The output must
// be a pointer to a value whose size matches the Variable.
func (v *Variable) Get(out any) error {
if v.mm == nil {
return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
}
if !v.mm.bounds(v.offset, v.size) {
return fmt.Errorf("variable %s: access out of bounds: %w", v.name, io.EOF)
}
if err := sysenc.Unmarshal(out, v.mm.b[v.offset:v.offset+v.size]); err != nil {
return fmt.Errorf("unmarshaling value %s: %w", v.name, err)
}
return nil
}