-
Notifications
You must be signed in to change notification settings - Fork 1
/
libbpf.go
459 lines (375 loc) · 10.8 KB
/
libbpf.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
// Copyright 2021 Authors of kubearmor/libbpf
// SPDX-License-Identifier: Apache-2.0
package libbpf
import (
"unsafe"
"github.com/aquasecurity/libbpfgo"
unix "golang.org/x/sys/unix"
)
import "C"
// KABPFProgType type
type KABPFProgType uint32
// KABPFProgType constants
const (
KABPFProgTypeUnspec KABPFProgType = unix.BPF_PROG_TYPE_UNSPEC
KABPFProgTypeSocketFilter KABPFProgType = unix.BPF_PROG_TYPE_SOCKET_FILTER
KABPFProgTypeKprobe KABPFProgType = unix.BPF_PROG_TYPE_KPROBE
KABPFProgTypeSchedCls KABPFProgType = unix.BPF_PROG_TYPE_SCHED_CLS
KABPFProgTypeSchedAct KABPFProgType = unix.BPF_PROG_TYPE_SCHED_ACT
KABPFProgTypeTracepoint KABPFProgType = unix.BPF_PROG_TYPE_TRACEPOINT
KABPFProgTypeXDP KABPFProgType = unix.BPF_PROG_TYPE_XDP
KABPFProgTypePerfEvent KABPFProgType = unix.BPF_PROG_TYPE_PERF_EVENT
KABPFProgTypeCgroupSKB KABPFProgType = unix.BPF_PROG_TYPE_CGROUP_SKB
KABPFProgTypeCgroupSock KABPFProgType = unix.BPF_PROG_TYPE_CGROUP_SOCK
KABPFProgTypeLwtIn KABPFProgType = unix.BPF_PROG_TYPE_LWT_IN
KABPFProgTypeLwtOut KABPFProgType = unix.BPF_PROG_TYPE_LWT_OUT
KABPFProgTypeLwtXmit KABPFProgType = unix.BPF_PROG_TYPE_LWT_XMIT
KABPFProgTypeSockOps KABPFProgType = unix.BPF_PROG_TYPE_SOCK_OPS
KABPFProgTypeSkSKB KABPFProgType = unix.BPF_PROG_TYPE_SK_SKB
KABPFProgTypeCgroupDevice KABPFProgType = unix.BPF_PROG_TYPE_CGROUP_DEVICE
KABPFProgTypeSkMsg KABPFProgType = unix.BPF_PROG_TYPE_SK_MSG
KABPFProgTypeRawTracepoint KABPFProgType = unix.BPF_PROG_TYPE_RAW_TRACEPOINT
KABPFProgTypeCgroupSockAddr KABPFProgType = unix.BPF_PROG_TYPE_CGROUP_SOCK_ADDR
KABPFProgTypeLwtSeg6Local KABPFProgType = unix.BPF_PROG_TYPE_LWT_SEG6LOCAL
KABPFProgTypeLircMode2 KABPFProgType = unix.BPF_PROG_TYPE_LIRC_MODE2
KABPFProgTypeSkReuseport KABPFProgType = unix.BPF_PROG_TYPE_SK_REUSEPORT
KABPFProgTypeFlowDissector KABPFProgType = unix.BPF_PROG_TYPE_FLOW_DISSECTOR
KABPFProgTypeCgroupSysctl KABPFProgType = unix.BPF_PROG_TYPE_CGROUP_SYSCTL
KABPFProgTypeRawTracepointWritable KABPFProgType = unix.BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
KABPFProgTypeCgroupSockopt KABPFProgType = unix.BPF_PROG_TYPE_CGROUP_SOCKOPT
KABPFProgTypeTracing KABPFProgType = unix.BPF_PROG_TYPE_TRACING
KABPFProgTypeStructOps KABPFProgType = unix.BPF_PROG_TYPE_STRUCT_OPS
KABPFProgTypeExt KABPFProgType = unix.BPF_PROG_TYPE_EXT
KABPFProgTypeLSM KABPFProgType = unix.BPF_PROG_TYPE_LSM
KABPFProgTypeSkLookup KABPFProgType = unix.BPF_PROG_TYPE_SK_LOOKUP
)
// KABPFLinkType type
type KABPFLinkType uint32
// KABPFLinkType constants
const (
KABPFLinkTypeUnspec KABPFLinkType = iota
KABPFLinkTypeLSM
KABPFLinkTypeKprobe
KABPFLinkTypeKretprobe
KABPFLinkTypeRawTracepoint
KABPFLinkTypeTracepoint
)
// KubeArmor BPFObject wrapper structure
type KABPFObject struct {
bpfObj *libbpfgo.Module
}
// KubeArmor BPFMap wrapper structure
type KABPFMap struct {
bpfObj *KABPFObject
bpfMap *libbpfgo.BPFMap
}
// KubeArmor BPFMap Element interface
type KABPFMapElement interface {
KeyPointer() unsafe.Pointer
ValuePointer() unsafe.Pointer
MapName() string
SetFoundValue(value []byte)
}
// KubeArmor BPFProgram wrapper structure
type KABPFProgram struct {
bpfObj *KABPFObject
bpfProg *libbpfgo.BPFProg
}
// KubeArmor BPFLink wrapper structure
type KABPFLink struct {
bpfProg *KABPFProgram
eventName string
eventType KABPFLinkType
bpfLink *libbpfgo.BPFLink
}
// KubeArmor RingBuffer wrapper structure
type KABPFRingBuffer struct {
bpfMap *KABPFMap
bpfRingBuffer *libbpfgo.RingBuffer
}
// KubeArmor PerfBuffer wrapper structure
type KABPFPerfBuffer struct {
bpfMap *KABPFMap
bpfPerfBuffer *libbpfgo.PerfBuffer
}
// Open object file
func OpenObjectFromFile(bpfObjFile string) (*KABPFObject, error) {
mod, err := libbpfgo.NewModuleFromFile(bpfObjFile)
if err != nil {
return nil, err
}
return &KABPFObject{
bpfObj: mod,
}, nil
}
// Load object
func (o *KABPFObject) Load() error {
return o.bpfObj.BPFLoadObject()
}
// Close object
func (o *KABPFObject) Close() {
o.bpfObj.Close()
}
// Get map from object
func (o *KABPFObject) FindMapByName(mapName string) (*KABPFMap, error) {
m, err := o.bpfObj.GetMap(mapName)
if err != nil {
return nil, err
}
return &KABPFMap{
bpfObj: o,
bpfMap: m,
}, nil
}
// Get program from object
func (o *KABPFObject) FindProgramByName(progName string) (*KABPFProgram, error) {
p, err := o.bpfObj.GetProgram(progName)
if err != nil {
return nil, err
}
return &KABPFProgram{
bpfObj: o,
bpfProg: p,
}, nil
}
// Initialize ring buffer
func (o *KABPFObject) InitRingBuf(mapName string, eventsChan chan []byte) (*KABPFRingBuffer, error) {
var err error
var m *KABPFMap
m, err = o.FindMapByName(mapName)
if err != nil {
return nil, err
}
var rb *libbpfgo.RingBuffer
rb, err = o.bpfObj.InitRingBuf(m.Name(), eventsChan)
if err != nil {
return nil, err
}
return &KABPFRingBuffer{
bpfMap: m,
bpfRingBuffer: rb,
}, nil
}
// Initialize perf buffer
func (o *KABPFObject) InitPerfBuf(mapName string, eventsChan chan []byte, lostChan chan uint64, pageCnt int) (*KABPFPerfBuffer, error) {
var err error
var m *KABPFMap
m, err = o.FindMapByName(mapName)
if err != nil {
return nil, err
}
var pb *libbpfgo.PerfBuffer
pb, err = o.bpfObj.InitPerfBuf(m.Name(), eventsChan, lostChan, pageCnt)
if err != nil {
return nil, err
}
return &KABPFPerfBuffer{
bpfMap: m,
bpfPerfBuffer: pb,
}, nil
}
// Get map fd
func (m *KABPFMap) FD() int {
return m.bpfMap.GetFd()
}
// Get map name
func (m *KABPFMap) Name() string {
return m.bpfMap.GetName()
}
// Get map max entries
func (m *KABPFMap) MaxEntries() uint32 {
return m.bpfMap.GetMaxEntries()
}
// Set map max entries
func (m *KABPFMap) SetMaxEntries(maxEntries uint32) error {
return m.bpfMap.Resize(maxEntries)
}
// Check if map is pinned
func (m *KABPFMap) IsPinned() bool {
return m.bpfMap.IsPinned()
}
// Get map pin path
func (m *KABPFMap) PinPath() string {
return m.bpfMap.GetPinPath()
}
// Set map pin path
func (m *KABPFMap) SetPinPath(pinPath string) error {
return m.bpfMap.SetPinPath(pinPath)
}
// Pin map
func (m *KABPFMap) Pin(pinPath string) error {
return m.bpfMap.Pin(pinPath)
}
// Unpin map
func (m *KABPFMap) Unpin(pinPath string) error {
return m.bpfMap.Unpin(pinPath)
}
// Get map key size
func (m *KABPFMap) KeySize() int {
return m.bpfMap.KeySize()
}
// Get map value size
func (m *KABPFMap) ValueSize() int {
return m.bpfMap.ValueSize()
}
// Lookup map element
// The elem will have its value updated
func (m *KABPFMap) LookupElement(elem KABPFMapElement) ([]byte, error) {
val, err := m.bpfMap.GetValue(elem.KeyPointer())
if err != nil {
return nil, err
}
elem.SetFoundValue(val)
return val, err
}
// Update map element
func (m *KABPFMap) UpdateElement(elem KABPFMapElement) error {
return m.bpfMap.Update(elem.KeyPointer(), elem.ValuePointer())
}
// Delete map element
func (m *KABPFMap) DeleteElement(elem KABPFMapElement) error {
return m.bpfMap.DeleteKey(elem.KeyPointer())
}
// Get object pointer to which map belongs
func (m *KABPFMap) Object() *KABPFObject {
return m.bpfObj
}
// Initialize ring buffer
func (m *KABPFMap) InitRingBuf(eventsChan chan []byte) (*KABPFRingBuffer, error) {
return m.bpfObj.InitRingBuf(m.Name(), eventsChan)
}
// Initialize perf buffer
func (m *KABPFMap) InitPerfBuf(eventsChan chan []byte, lostChan chan uint64, pageCnt int) (*KABPFPerfBuffer, error) {
return m.bpfObj.InitPerfBuf(m.Name(), eventsChan, lostChan, pageCnt)
}
// Get program fd
func (p *KABPFProgram) FD() int {
return p.bpfProg.GetFd()
}
// Get program name
func (p *KABPFProgram) Name() string {
return p.bpfProg.GetName()
}
// Get program type
func (p *KABPFProgram) GetType() KABPFProgType {
return KABPFProgType(p.bpfProg.GetType())
}
// Attach LSM
func (p *KABPFProgram) AttachLSM() (*KABPFLink, error) {
l, err := p.bpfProg.AttachLSM()
if err != nil {
return nil, err
}
return &KABPFLink{
bpfProg: p,
eventName: "",
eventType: KABPFLinkTypeLSM,
bpfLink: l,
}, nil
}
// Attach Kprobe
// This should be used for kernels > 4.17
func (p *KABPFProgram) AttachKprobe(eventName string) (*KABPFLink, error) {
l, err := p.bpfProg.AttachKprobe(eventName)
if err != nil {
return nil, err
}
return &KABPFLink{
bpfProg: p,
eventName: eventName,
eventType: KABPFLinkTypeKprobe,
bpfLink: l,
}, nil
}
// Attach Kretprobe
// This should be used for kernels > 4.17
func (p *KABPFProgram) AttachKretprobe(eventName string) (*KABPFLink, error) {
l, err := p.bpfProg.AttachKretprobe(eventName)
if err != nil {
return nil, err
}
return &KABPFLink{
bpfProg: p,
eventName: eventName,
eventType: KABPFLinkTypeKretprobe,
bpfLink: l,
}, nil
}
// Attach Raw Tracepoint
func (p *KABPFProgram) AttachRawTracepoint(eventName string) (*KABPFLink, error) {
l, err := p.bpfProg.AttachRawTracepoint(eventName)
if err != nil {
return nil, err
}
return &KABPFLink{
bpfProg: p,
eventName: eventName,
eventType: KABPFLinkTypeRawTracepoint,
bpfLink: l,
}, nil
}
// Attach Tracepoint
func (p *KABPFProgram) AttachTracepoint(category, eventName string) (*KABPFLink, error) {
l, err := p.bpfProg.AttachTracepoint(category, eventName)
if err != nil {
return nil, err
}
return &KABPFLink{
bpfProg: p,
eventName: eventName,
eventType: KABPFLinkTypeTracepoint,
bpfLink: l,
}, nil
}
// Get object pointer to which program belongs
func (p *KABPFProgram) Object() *KABPFObject {
return p.bpfObj
}
// Get attached event name
func (l *KABPFLink) EventName() string {
return l.eventName
}
// Get attached event type
func (l *KABPFLink) EventType() KABPFLinkType {
return l.eventType
}
// Get program pointer to which link belongs
func (l *KABPFLink) Program() *KABPFProgram {
return l.bpfProg
}
// Destroy link
func (l *KABPFLink) Destroy() error {
return l.bpfLink.Destroy()
}
// Start to poll ring buffer
func (rb *KABPFRingBuffer) StartPoll() {
rb.bpfRingBuffer.Start()
}
// Stop to poll ring buffer
func (rb *KABPFRingBuffer) StopPoll() {
rb.bpfRingBuffer.Stop()
}
// Free ring buffer
func (rb *KABPFRingBuffer) Free() {
rb.bpfRingBuffer.Close()
}
// Get map pointer to which ring buffer relates
func (rb *KABPFRingBuffer) Map() *KABPFMap {
return rb.bpfMap
}
// Start to poll perf buffer
func (pb *KABPFPerfBuffer) StartPoll() {
pb.bpfPerfBuffer.Start()
}
// Stop to poll perf buffer
func (pb *KABPFPerfBuffer) StopPoll() {
pb.bpfPerfBuffer.Stop()
}
// Free perf buffer
func (pb *KABPFPerfBuffer) Free() {
pb.bpfPerfBuffer.Close()
}
// Get map pointer to which perf buffer relates
func (pb *KABPFPerfBuffer) Map() *KABPFMap {
return pb.bpfMap
}