Skip to content

Commit 5c74be0

Browse files
committed
Reimplement BpfProgram
1 parent 2b99f01 commit 5c74be0

File tree

3 files changed

+55
-68
lines changed

3 files changed

+55
-68
lines changed

src/core/bpf_object.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void BpfObject::load() {
6565
}
6666

6767
if (bpf_object__load(obj_)) {
68-
std::string error_msg = " object from file '" + object_path_ + "': " + std::strerror(errno);
68+
error_msg += " object from file '" + object_path_ + "': " + std::strerror(errno);
6969
bpf_object__close(obj_);
7070
obj_ = nullptr;
7171
throw BpfException(error_msg);

src/core/bpf_program.cpp

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,73 @@
11
#include "bpf_program.h"
22
#include "bpf_exception.h"
3-
#include <filesystem>
43
#include <utility>
4+
#include <cerrno>
55

6-
BpfProgram::BpfProgram(std::string object_path, std::string program_name)
7-
: obj_(nullptr), prog_(nullptr), link_(nullptr),
8-
object_path_(std::move(object_path)), program_name_(std::move(program_name)) {
6+
BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name)
7+
: parent_obj_(parent),
8+
prog_(raw_prog),
9+
link_(nullptr),
10+
program_name_(program_name) {
11+
if (!parent)
12+
throw BpfException("Parent BpfObject is null");
13+
if (!raw_prog)
14+
throw BpfException("bpf_program pointer is null");
915
}
1016

1117
BpfProgram::~BpfProgram() {
12-
destroy();
13-
if (obj_) {
14-
bpf_object__close(obj_);
15-
}
18+
detach();
1619
}
1720

18-
struct bpf_object * BpfProgram::get_obj() const {
19-
return obj_;
21+
BpfProgram::BpfProgram(BpfProgram&& other) noexcept
22+
: parent_obj_(std::move(other.parent_obj_)),
23+
prog_(other.prog_),
24+
link_(other.link_),
25+
program_name_(std::move(other.program_name_)) {
26+
27+
other.prog_ = nullptr;
28+
other.link_ = nullptr;
2029
}
2130

22-
bool BpfProgram::load() {
23-
// Open the eBPF object file
24-
obj_ = bpf_object__open_file(object_path_.c_str(), nullptr);
25-
if (libbpf_get_error(obj_)) {
26-
throw BpfException("Failed to open BPF object file: " + object_path_);
31+
BpfProgram& BpfProgram::operator=(BpfProgram&& other) noexcept {
32+
if (this != &other) {
33+
detach();
34+
35+
parent_obj_ = std::move(other.parent_obj_);
36+
prog_ = other.prog_;
37+
link_ = other.link_;
38+
program_name_ = std::move(other.program_name_);
39+
40+
other.prog_ = nullptr;
41+
other.link_ = nullptr;
2742
}
43+
return *this;
44+
}
2845

29-
// Find the program by name (if specified)
30-
if (!program_name_.empty()) {
31-
prog_ = bpf_object__find_program_by_name(obj_, program_name_.c_str());
32-
if (!prog_) {
33-
throw BpfException("Program '" + program_name_ + "' not found in object");
34-
}
35-
} else {
36-
while ((prog_ = bpf_object__next_program(obj_, prog_)) != nullptr) {
37-
programs.emplace_back(prog_, nullptr);
38-
}
39-
40-
// throw if no programs found
41-
if (programs.empty()) {
42-
throw BpfException("No programs found in object file");
43-
}
46+
void BpfProgram::attach() {
47+
// Check if parent is still alive
48+
auto parent = parent_obj_.lock();
49+
if (!parent) {
50+
throw BpfException("Parent BpfObject has been destroyed");
4451
}
45-
46-
// Load the eBPF object into the kernel
47-
if (bpf_object__load(obj_)) {
48-
throw BpfException("Failed to load BPF object into kernel");
52+
53+
if (link_) {
54+
throw BpfException("Program '" + program_name_ + "' already attached");
4955
}
50-
51-
return true;
52-
}
53-
54-
bool BpfProgram::attach() {
55-
for (auto [prog, link]: programs) {
56-
if (!prog) {
57-
throw BpfException("Program not loaded");
58-
}
59-
60-
link = bpf_program__attach(prog);
61-
if (libbpf_get_error(link)) {
62-
link = nullptr;
63-
throw BpfException("Failed to attach BPF program");
64-
}
56+
57+
if (!prog_) {
58+
throw BpfException("Program '" + program_name_ + "' not initialized");
6559
}
66-
67-
return true;
68-
}
69-
70-
bool BpfProgram::destroy() {
71-
bool success = true;
72-
for (auto [prog, link]: programs) {
73-
if (!prog) {
74-
throw BpfException("Program not loaded");
75-
}
76-
success = success & bpf_link__destroy(link);
60+
61+
link_ = bpf_program__attach(prog_);
62+
if (!link_) {
63+
std::string err_msg = "bpf_program__attach failed for program '" + program_name_ + "': " + std::strerror(errno);
64+
throw BpfException(err_msg);
7765
}
78-
return success;
7966
}
8067

81-
void BpfProgram::load_and_attach() {
82-
load();
83-
attach();
68+
void BpfProgram::detach() {
69+
if (link_) {
70+
bpf_link__destroy(link_);
71+
link_ = nullptr;
72+
}
8473
}

src/core/bpf_program.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class BpfProgram {
1616
std::string program_name_;
1717

1818
public:
19-
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, std::string program_name = "");
19+
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name);
2020

2121
~BpfProgram();
2222

@@ -28,8 +28,6 @@ class BpfProgram {
2828
bool attach();
2929
bool detach();
3030

31-
void load_and_attach();
32-
3331
[[nodiscard]] bool is_attached() const { return link_ != nullptr; }
3432
[[nodiscard]] std::string get_name() const { return program_name_; }
3533
};

0 commit comments

Comments
 (0)