-
Notifications
You must be signed in to change notification settings - Fork 95
/
module.go
492 lines (414 loc) · 11.5 KB
/
module.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package libbpfgo
/*
#cgo LDFLAGS: -lelf -lz
#include "libbpfgo.h"
*/
import "C"
import (
"bytes"
"debug/elf"
"encoding/binary"
"errors"
"fmt"
"syscall"
"unsafe"
)
//
// Module (BPF Object)
//
type Module struct {
obj *C.struct_bpf_object
links []*BPFLink
perfBufs []*PerfBuffer
ringBufs []*RingBuffer
elf *elf.File
loaded bool
}
//
// New Module Helpers
//
type NewModuleArgs struct {
KConfigFilePath string
BTFObjPath string
BPFObjName string
BPFObjPath string
BPFObjBuff []byte
SkipMemlockBump bool
KernelLogLevel uint32
}
func NewModuleFromFile(bpfObjPath string) (*Module, error) {
return NewModuleFromFileArgs(NewModuleArgs{
BPFObjPath: bpfObjPath,
})
}
func NewModuleFromFileArgs(args NewModuleArgs) (*Module, error) {
f, err := elf.Open(args.BPFObjPath)
if err != nil {
return nil, err
}
C.cgo_libbpf_set_print_fn()
// If skipped, we rely on libbpf to do the bumping if deemed necessary
if !args.SkipMemlockBump {
// TODO: remove this once libbpf memory limit bump issue is solved
if err := bumpMemlockRlimit(); err != nil {
return nil, err
}
}
var btfFilePathC *C.char
var kconfigPathC *C.char
// instruct libbpf to use user provided kernel BTF file
if args.BTFObjPath != "" {
btfFilePathC = C.CString(args.BTFObjPath)
defer C.free(unsafe.Pointer(btfFilePathC))
}
// instruct libbpf to use user provided KConfigFile
if args.KConfigFilePath != "" {
kconfigPathC = C.CString(args.KConfigFilePath)
defer C.free(unsafe.Pointer(kconfigPathC))
}
kernelLogLevelC := C.uint(args.KernelLogLevel)
optsC, errno := C.cgo_bpf_object_open_opts_new(btfFilePathC, kconfigPathC, nil, kernelLogLevelC)
if optsC == nil {
return nil, fmt.Errorf("failed to create bpf_object_open_opts: %w", errno)
}
defer C.cgo_bpf_object_open_opts_free(optsC)
bpfFileC := C.CString(args.BPFObjPath)
defer C.free(unsafe.Pointer(bpfFileC))
objC, errno := C.bpf_object__open_file(bpfFileC, optsC)
if objC == nil {
return nil, fmt.Errorf("failed to open BPF object at path %s: %w", args.BPFObjPath, errno)
}
return &Module{
obj: objC,
elf: f,
}, nil
}
func NewModuleFromBuffer(bpfObjBuff []byte, bpfObjName string) (*Module, error) {
return NewModuleFromBufferArgs(NewModuleArgs{
BPFObjBuff: bpfObjBuff,
BPFObjName: bpfObjName,
})
}
func NewModuleFromBufferArgs(args NewModuleArgs) (*Module, error) {
f, err := elf.NewFile(bytes.NewReader(args.BPFObjBuff))
if err != nil {
return nil, err
}
C.cgo_libbpf_set_print_fn()
// TODO: remove this once libbpf memory limit bump issue is solved
if err := bumpMemlockRlimit(); err != nil {
return nil, err
}
var btfFilePathC *C.char
var kConfigPathC *C.char
// instruct libbpf to use user provided kernel BTF file
if args.BTFObjPath != "" {
btfFilePathC = C.CString(args.BTFObjPath)
defer C.free(unsafe.Pointer(btfFilePathC))
}
// instruct libbpf to use user provided KConfigFile
if args.KConfigFilePath != "" {
kConfigPathC = C.CString(args.KConfigFilePath)
defer C.free(unsafe.Pointer(kConfigPathC))
}
bpfObjNameC := C.CString(args.BPFObjName)
defer C.free(unsafe.Pointer(bpfObjNameC))
bpfBuffC := unsafe.Pointer(C.CBytes(args.BPFObjBuff))
defer C.free(bpfBuffC)
bpfBuffSizeC := C.size_t(len(args.BPFObjBuff))
kernelLogLevelC := C.uint(args.KernelLogLevel)
if len(args.KConfigFilePath) <= 2 {
kConfigPathC = nil
}
optsC, errno := C.cgo_bpf_object_open_opts_new(btfFilePathC, kConfigPathC, bpfObjNameC, kernelLogLevelC)
if optsC == nil {
return nil, fmt.Errorf("failed to create bpf_object_open_opts: %w", errno)
}
defer C.cgo_bpf_object_open_opts_free(optsC)
objC, errno := C.bpf_object__open_mem(bpfBuffC, bpfBuffSizeC, optsC)
if objC == nil {
return nil, fmt.Errorf("failed to open BPF object %s: %w", args.BPFObjName, errno)
}
return &Module{
obj: objC,
elf: f,
}, nil
}
// NOTE: libbpf has started raising limits by default but, unfortunately, that
// seems to be failing in current libbpf version. The memory limit bump might be
// removed once this is sorted out.
func bumpMemlockRlimit() error {
var rLimit syscall.Rlimit
rLimit.Max = 512 << 20 /* 512 MBs */
rLimit.Cur = 512 << 20 /* 512 MBs */
err := syscall.Setrlimit(C.RLIMIT_MEMLOCK, &rLimit)
if err != nil {
return fmt.Errorf("error setting rlimit: %v", err)
}
return nil
}
//
// Module Methods
//
func (m *Module) Close() {
for _, pb := range m.perfBufs {
pb.Close()
}
for _, rb := range m.ringBufs {
rb.Close()
}
for _, link := range m.links {
if link.link != nil {
link.Destroy()
}
}
C.bpf_object__close(m.obj)
}
func (m *Module) BPFLoadObject() error {
retC := C.bpf_object__load(m.obj)
if retC < 0 {
return fmt.Errorf("failed to load BPF object: %w", syscall.Errno(-retC))
}
m.loaded = true
m.elf.Close()
m.elf = nil
return nil
}
// InitGlobalVariable sets global variables (defined in .data or .rodata)
// in bpf code. It must be called before the BPF object is loaded.
func (m *Module) InitGlobalVariable(name string, value interface{}) error {
if m.loaded {
return errors.New("must be called before the BPF object is loaded")
}
s, err := getGlobalVariableSymbol(m.elf, name)
if err != nil {
return err
}
bpfMap, err := m.GetMap(s.sectionName)
if err != nil {
return err
}
// get current value
currMapValue, err := bpfMap.InitialValue()
if err != nil {
return err
}
// generate new value
newMapValue := make([]byte, bpfMap.ValueSize())
copy(newMapValue, currMapValue)
data := bytes.NewBuffer(nil)
if err := binary.Write(data, s.byteOrder, value); err != nil {
return err
}
varValue := data.Bytes()
start := s.offset
end := s.offset + len(varValue)
if len(varValue) > s.size || end > bpfMap.ValueSize() {
return errors.New("invalid value")
}
copy(newMapValue[start:end], varValue)
// save new value
err = bpfMap.SetInitialValue(unsafe.Pointer(&newMapValue[0]))
return err
}
func (m *Module) GetMap(mapName string) (*BPFMap, error) {
mapNameC := C.CString(mapName)
defer C.free(unsafe.Pointer(mapNameC))
bpfMapC, errno := C.bpf_object__find_map_by_name(m.obj, mapNameC)
if bpfMapC == nil {
return nil, fmt.Errorf("failed to find BPF map %s: %w", mapName, errno)
}
bpfMap := &BPFMap{
bpfMap: bpfMapC,
module: m,
}
if !m.loaded {
bpfMap.bpfMapLow = &BPFMapLow{
fd: -1,
info: &BPFMapInfo{},
}
return bpfMap, nil
}
fd := bpfMap.FileDescriptor()
info, err := GetMapInfoByFD(fd)
if err != nil {
// Compatibility Note: Some older kernels lack BTF (BPF Type Format)
// support for specific BPF map types. In such scenarios, libbpf may
// fail (EPERM) when attempting to retrieve information for these maps.
// Reference: https://elixir.bootlin.com/linux/v5.15.75/source/tools/lib/bpf/gen_loader.c#L401
//
// However, we can still get some map info from the BPF map high level API.
bpfMap.bpfMapLow = &BPFMapLow{
fd: fd,
info: &BPFMapInfo{
Type: bpfMap.Type(),
ID: 0,
KeySize: uint32(bpfMap.KeySize()),
ValueSize: uint32(bpfMap.ValueSize()),
MaxEntries: bpfMap.MaxEntries(),
MapFlags: uint32(bpfMap.MapFlags()),
Name: bpfMap.Name(),
IfIndex: bpfMap.IfIndex(),
BTFVmlinuxValueTypeID: 0,
NetnsDev: 0,
NetnsIno: 0,
BTFID: 0,
BTFKeyTypeID: 0,
BTFValueTypeID: 0,
MapExtra: bpfMap.MapExtra(),
},
}
return bpfMap, nil
}
bpfMap.bpfMapLow = &BPFMapLow{
fd: fd,
info: info,
}
return bpfMap, nil
}
func (m *Module) GetProgram(progName string) (*BPFProg, error) {
progNameC := C.CString(progName)
defer C.free(unsafe.Pointer(progNameC))
progC, errno := C.bpf_object__find_program_by_name(m.obj, progNameC)
if progC == nil {
return nil, fmt.Errorf("failed to find BPF program %s: %w", progName, errno)
}
return &BPFProg{
prog: progC,
module: m,
}, nil
}
func (m *Module) InitRingBuf(mapName string, eventsChan chan []byte) (*RingBuffer, error) {
bpfMap, err := m.GetMap(mapName)
if err != nil {
return nil, err
}
if eventsChan == nil {
return nil, fmt.Errorf("events channel can not be nil")
}
slot := eventChannels.put(eventsChan)
if slot == -1 {
return nil, fmt.Errorf("max ring buffers reached")
}
rbC, errno := C.cgo_init_ring_buf(C.int(bpfMap.FileDescriptor()), C.uintptr_t(slot))
if rbC == nil {
eventChannels.remove(uint(slot))
return nil, fmt.Errorf("failed to initialize ring buffer: %w", errno)
}
ringBuf := &RingBuffer{
rb: rbC,
bpfMap: bpfMap,
slots: []uint{uint(slot)},
}
m.ringBufs = append(m.ringBufs, ringBuf)
return ringBuf, nil
}
func (m *Module) AddRingBuf(ringBuf *RingBuffer, mapName string, eventsChan chan []byte) (bool, error) {
bpfMap, err := m.GetMap(mapName)
if err != nil {
return false, err
}
if eventsChan == nil {
return false, fmt.Errorf("events channel can not be nil")
}
slot := eventChannels.put(eventsChan)
if slot == -1 {
return false, fmt.Errorf("max ring buffers reached")
}
ringBuf.slots = append(ringBuf.slots, uint(slot))
ret, errno := C.cgo_add_ring_buf(ringBuf.rb, C.int(bpfMap.FileDescriptor()), C.uintptr_t(slot))
if ret != 0 {
eventChannels.remove(uint(slot))
return false, fmt.Errorf("failed to add ring buffer: %w", errno)
}
return true, nil
}
func (m *Module) InitPerfBuf(mapName string, eventsChan chan []byte, lostChan chan uint64, pageCnt int) (*PerfBuffer, error) {
bpfMap, err := m.GetMap(mapName)
if err != nil {
return nil, fmt.Errorf("failed to init perf buffer: %v", err)
}
if eventsChan == nil {
return nil, fmt.Errorf("failed to init perf buffer: events channel can not be nil")
}
perfBuf := &PerfBuffer{
bpfMap: bpfMap,
eventsChan: eventsChan,
lostChan: lostChan,
}
slot := eventChannels.put(perfBuf)
if slot == -1 {
return nil, fmt.Errorf("max number of ring/perf buffers reached")
}
pbC, errno := C.cgo_init_perf_buf(C.int(bpfMap.FileDescriptor()), C.int(pageCnt), C.uintptr_t(slot))
if pbC == nil {
eventChannels.remove(uint(slot))
return nil, fmt.Errorf("failed to initialize perf buffer: %w", errno)
}
perfBuf.pb = pbC
perfBuf.slot = uint(slot)
m.perfBufs = append(m.perfBufs, perfBuf)
return perfBuf, nil
}
func (m *Module) TcHookInit() *TcHook {
return &TcHook{
hook: C.cgo_bpf_tc_hook_new(),
}
}
func (m *Module) Iterator() *BPFObjectIterator {
return &BPFObjectIterator{
m: m,
prevProg: nil,
prevMap: nil,
}
}
func (m *Module) linkExist(prog *BPFProg) bool {
for _, link := range m.links {
if link.prog.Name() == prog.Name() {
return true
}
}
return false
}
// AttachPrograms attach all loaded and no attached progs once like bpf_object__attach_skeleton
func (m *Module) AttachPrograms() error {
iters := m.Iterator()
for {
prog := iters.NextProgram()
if prog == nil {
break
}
if !prog.Autoload() || !prog.Autoattach() {
continue
}
// if link already exist (is attached), skip it
if m.linkExist(prog) {
continue
}
link, err := prog.AttachGeneric()
if err != nil {
return err
}
m.links = append(m.links, link)
}
return nil
}
// DetachPrograms detach all attached progs once like bpf_object__detach_skeleton
func (m *Module) DetachPrograms() error {
errInfo := make(map[string]error)
for _, link := range m.links {
err := link.Destroy()
if err != nil {
errInfo[link.prog.Name()] = err
}
}
m.links = nil
if len(errInfo) > 0 {
var str string
for name, err := range errInfo {
str += fmt.Sprintf(" [prog:%s,err:%s]", name, err)
}
return fmt.Errorf("link destroy failed:%s", str)
}
return nil
}