-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathBPFProgramIterator.cpp
65 lines (48 loc) · 1.21 KB
/
BPFProgramIterator.cpp
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
#include "BPFProgramIterator.h"
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "bpf/common.h"
#include "program_iter.skel.h"
namespace collector::sources {
static struct program_iter_bpf* skeleton_;
bool BPFProgramIterator::Load() {
skeleton_ = program_iter_bpf__open_and_load();
if (!skeleton_) {
return false;
}
if (program_iter_bpf__attach(skeleton_) != 0) {
return false;
}
return true;
}
void BPFProgramIterator::Unload() {
program_iter_bpf__destroy(skeleton_);
}
std::vector<sensor::SignalStreamMessage*> BPFProgramIterator::LoadedPrograms() {
int iter_fd = bpf_iter_create(bpf_link__fd(skeleton_->links.dump_bpf_prog));
if (iter_fd < 0) {
return {};
}
std::vector<sensor::SignalStreamMessage*> messages;
struct bpf_prog_result result;
while (true) {
int ret = read(iter_fd, &result, sizeof(struct bpf_prog_result));
if (ret < 0) {
if (errno == EAGAIN) {
continue;
}
break;
}
if (ret == 0) {
break;
}
auto message = formatter_.ToProtoMessage(&result);
if (message == nullptr) {
continue;
}
messages.push_back(message);
}
return messages;
}
} // namespace collector::sources