forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
libbpfgo.c
570 lines (453 loc) · 13.2 KB
/
libbpfgo.c
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "libbpfgo.h"
extern void loggerCallback(enum libbpf_print_level level, char *output);
extern void perfCallback(void *ctx, int cpu, void *data, __u32 size);
extern void perfLostCallback(void *ctx, int cpu, __u64 cnt);
extern int ringbufferCallback(void *ctx, void *data, size_t size);
int libbpf_print_fn(enum libbpf_print_level level, // libbpf print level
const char *format, // format used for the msg
va_list args) // args used by format
{
int ret;
size_t len;
char *out;
va_list check;
va_copy(check, args);
ret = vsnprintf(NULL, 0, format, check); // get output length
va_end(check);
if (ret < 0)
return ret;
len = ret + 1; // add 1 for NUL
out = malloc(len);
if (!out)
return -ENOMEM;
va_copy(check, args);
ret = vsnprintf(out, len, format, check);
va_end(check);
if (ret > 0)
loggerCallback(level, out);
free(out);
return ret;
}
void cgo_libbpf_set_print_fn()
{
libbpf_set_print(libbpf_print_fn);
}
struct ring_buffer *cgo_init_ring_buf(int map_fd, uintptr_t ctx)
{
struct ring_buffer *rb = NULL;
rb = ring_buffer__new(map_fd, ringbufferCallback, (void *) ctx, NULL);
if (!rb) {
int saved_errno = errno;
fprintf(stderr, "Failed to initialize ring buffer: %s\n", strerror(errno));
errno = saved_errno;
return NULL;
}
return rb;
}
int cgo_add_ring_buf(struct ring_buffer *rb, int map_fd, uintptr_t ctx)
{
int ret = ring_buffer__add(rb, map_fd, ringbufferCallback, (void *) ctx);
if (ret != 0) {
int saved_errno = errno;
fprintf(stderr, "Failed to add ring buffer: %s\n", strerror(errno));
errno = saved_errno;
return ret;
}
return ret;
}
struct perf_buffer *cgo_init_perf_buf(int map_fd, int page_cnt, uintptr_t ctx)
{
struct perf_buffer_opts pb_opts = {};
struct perf_buffer *pb = NULL;
pb_opts.sz = sizeof(struct perf_buffer_opts);
pb = perf_buffer__new(map_fd, page_cnt, perfCallback, perfLostCallback, (void *) ctx, &pb_opts);
if (!pb) {
int saved_errno = errno;
fprintf(stderr, "Failed to initialize perf buffer: %s\n", strerror(errno));
errno = saved_errno;
return NULL;
}
return pb;
}
void cgo_bpf_map__initial_value(struct bpf_map *map, void *value)
{
size_t psize;
const void *data;
data = bpf_map__initial_value(map, &psize);
if (!data)
return;
memcpy(value, data, psize);
}
int cgo_bpf_prog_attach_cgroup_legacy(int prog_fd, // eBPF program file descriptor
int target_fd, // cgroup directory file descriptor
int type) // BPF_CGROUP_INET_{INGRESS,EGRESS}, ...
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.target_fd = target_fd;
attr.attach_bpf_fd = prog_fd;
attr.attach_type = type;
attr.attach_flags = BPF_F_ALLOW_MULTI; // or BPF_F_ALLOW_OVERRIDE
return syscall(__NR_bpf, BPF_PROG_ATTACH, &attr, sizeof(attr));
}
int cgo_bpf_prog_detach_cgroup_legacy(int prog_fd, // eBPF program file descriptor
int target_fd, // cgroup directory file descriptor
int type) // BPF_CGROUP_INET_{INGRESS,EGRESS}, ...
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.target_fd = target_fd;
attr.attach_bpf_fd = prog_fd;
attr.attach_type = type;
return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
}
//
// struct handlers
//
struct bpf_iter_attach_opts *cgo_bpf_iter_attach_opts_new(__u32 map_fd,
enum bpf_cgroup_iter_order order,
__u32 cgroup_fd,
__u64 cgroup_id,
__u32 tid,
__u32 pid,
__u32 pid_fd)
{
union bpf_iter_link_info *linfo;
linfo = calloc(1, sizeof(*linfo));
if (!linfo)
return NULL;
linfo->map.map_fd = map_fd;
linfo->cgroup.order = order;
linfo->cgroup.cgroup_fd = cgroup_fd;
linfo->cgroup.cgroup_id = cgroup_id;
linfo->task.tid = tid;
linfo->task.pid = pid;
linfo->task.pid_fd = pid_fd;
struct bpf_iter_attach_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts) {
free(linfo);
return NULL;
}
opts->sz = sizeof(*opts);
opts->link_info_len = sizeof(*linfo);
opts->link_info = linfo;
return opts;
}
void cgo_bpf_iter_attach_opts_free(struct bpf_iter_attach_opts *opts)
{
if (!opts)
return;
free(opts->link_info);
free(opts);
}
struct bpf_test_run_opts *cgo_bpf_test_run_opts_new(const void *data_in,
void *data_out,
__u32 data_size_in,
__u32 data_size_out,
const void *ctx_in,
void *ctx_out,
__u32 ctx_size_in,
__u32 ctx_size_out,
int repeat,
__u32 flags,
__u32 cpu,
__u32 batch_size)
{
struct bpf_test_run_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->data_in = data_in;
opts->data_out = data_out;
opts->data_size_in = data_size_in;
opts->data_size_out = data_size_out;
opts->ctx_in = ctx_in;
opts->ctx_out = ctx_out;
opts->ctx_size_in = ctx_size_in;
opts->ctx_size_out = ctx_size_out;
opts->repeat = repeat;
opts->flags = flags;
opts->cpu = cpu;
opts->batch_size = batch_size;
return opts;
}
void cgo_bpf_test_run_opts_free(struct bpf_test_run_opts *opts)
{
if (!opts)
return;
free(opts);
}
struct bpf_object_open_opts *cgo_bpf_object_open_opts_new(const char *btf_file_path,
const char *kconfig_path,
const char *bpf_obj_name,
__u32 kernel_log_level)
{
struct bpf_object_open_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->btf_custom_path = btf_file_path;
opts->kconfig = kconfig_path;
opts->object_name = bpf_obj_name;
opts->kernel_log_level = kernel_log_level;
return opts;
}
void cgo_bpf_object_open_opts_free(struct bpf_object_open_opts *opts)
{
free(opts);
}
struct bpf_map_create_opts *cgo_bpf_map_create_opts_new(__u32 btf_fd,
__u32 btf_key_type_id,
__u32 btf_value_type_id,
__u32 btf_vmlinux_value_type_id,
__u32 inner_map_fd,
__u32 map_flags,
__u64 map_extra,
__u32 numa_node,
__u32 map_ifindex)
{
struct bpf_map_create_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->btf_fd = btf_fd;
opts->btf_key_type_id = btf_key_type_id;
opts->btf_value_type_id = btf_value_type_id;
opts->btf_vmlinux_value_type_id = btf_vmlinux_value_type_id;
opts->inner_map_fd = inner_map_fd;
opts->map_flags = map_flags;
opts->map_extra = map_extra;
opts->numa_node = numa_node;
opts->map_ifindex = map_ifindex;
return opts;
}
void cgo_bpf_map_create_opts_free(struct bpf_map_create_opts *opts)
{
free(opts);
}
struct bpf_map_batch_opts *cgo_bpf_map_batch_opts_new(__u64 elem_flags, __u64 flags)
{
struct bpf_map_batch_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->elem_flags = elem_flags;
opts->flags = flags;
return opts;
}
void cgo_bpf_map_batch_opts_free(struct bpf_map_batch_opts *opts)
{
free(opts);
}
struct bpf_map_info *cgo_bpf_map_info_new()
{
struct bpf_map_info *info;
info = calloc(1, sizeof(*info));
if (!info)
return NULL;
return info;
}
__u32 cgo_bpf_map_info_size()
{
return sizeof(struct bpf_map_info);
}
void cgo_bpf_map_info_free(struct bpf_map_info *info)
{
free(info);
}
struct bpf_tc_opts *cgo_bpf_tc_opts_new(
int prog_fd, __u32 flags, __u32 prog_id, __u32 handle, __u32 priority)
{
struct bpf_tc_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->prog_fd = prog_fd;
opts->flags = flags;
opts->prog_id = prog_id;
opts->handle = handle;
opts->priority = priority;
return opts;
}
void cgo_bpf_tc_opts_free(struct bpf_tc_opts *opts)
{
free(opts);
}
struct bpf_tc_hook *cgo_bpf_tc_hook_new()
{
struct bpf_tc_hook *hook;
hook = calloc(1, sizeof(*hook));
if (!hook)
return NULL;
hook->sz = sizeof(*hook);
return hook;
}
void cgo_bpf_tc_hook_free(struct bpf_tc_hook *hook)
{
free(hook);
}
struct bpf_kprobe_opts *cgo_bpf_kprobe_opts_new(__u64 bpf_cookie,
size_t offset,
bool retprobe,
int attach_mode)
{
struct bpf_kprobe_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->bpf_cookie = bpf_cookie;
opts->offset = offset;
opts->retprobe = retprobe;
opts->attach_mode = attach_mode;
return opts;
}
void cgo_bpf_kprobe_opts_free(struct bpf_kprobe_opts *opts)
{
free(opts);
}
//
// struct getters
//
// bpf_map_info
__u32 cgo_bpf_map_info_type(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->type;
}
__u32 cgo_bpf_map_info_id(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->id;
}
__u32 cgo_bpf_map_info_key_size(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->key_size;
}
__u32 cgo_bpf_map_info_value_size(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->value_size;
}
__u32 cgo_bpf_map_info_max_entries(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->max_entries;
}
__u32 cgo_bpf_map_info_map_flags(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->map_flags;
}
char *cgo_bpf_map_info_name(struct bpf_map_info *info)
{
if (!info)
return NULL;
return info->name;
}
__u32 cgo_bpf_map_info_ifindex(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->ifindex;
}
__u32 cgo_bpf_map_info_btf_vmlinux_value_type_id(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->btf_vmlinux_value_type_id;
}
__u64 cgo_bpf_map_info_netns_dev(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->netns_dev;
}
__u64 cgo_bpf_map_info_netns_ino(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->netns_ino;
}
__u32 cgo_bpf_map_info_btf_id(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->btf_id;
}
__u32 cgo_bpf_map_info_btf_key_type_id(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->btf_key_type_id;
}
__u32 cgo_bpf_map_info_btf_value_type_id(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->btf_value_type_id;
}
__u64 cgo_bpf_map_info_map_extra(struct bpf_map_info *info)
{
if (!info)
return 0;
return info->map_extra;
}
// bpf_tc_opts
int cgo_bpf_tc_opts_prog_fd(struct bpf_tc_opts *opts)
{
if (!opts)
return 0;
return opts->prog_fd;
}
__u32 cgo_bpf_tc_opts_flags(struct bpf_tc_opts *opts)
{
if (!opts)
return 0;
return opts->flags;
}
__u32 cgo_bpf_tc_opts_prog_id(struct bpf_tc_opts *opts)
{
if (!opts)
return 0;
return opts->prog_id;
}
__u32 cgo_bpf_tc_opts_handle(struct bpf_tc_opts *opts)
{
if (!opts)
return 0;
return opts->handle;
}
__u32 cgo_bpf_tc_opts_priority(struct bpf_tc_opts *opts)
{
if (!opts)
return 0;
return opts->priority;
}
struct bpf_xdp_attach_opts *cgo_bpf_xdp_attach_opts_new(__u32 fd)
{
struct bpf_xdp_attach_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->old_prog_fd = fd;
return opts;
}
void cgo_bpf_xdp_attach_opts_free(struct bpf_xdp_attach_opts *opts)
{
free(opts);
}