|
1 | 1 | #include "bpf_program.h" |
2 | 2 | #include "bpf_exception.h" |
3 | | -#include <filesystem> |
4 | 3 | #include <utility> |
| 4 | +#include <cerrno> |
5 | 5 |
|
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"); |
9 | 15 | } |
10 | 16 |
|
11 | 17 | BpfProgram::~BpfProgram() { |
12 | | - destroy(); |
13 | | - if (obj_) { |
14 | | - bpf_object__close(obj_); |
15 | | - } |
| 18 | + detach(); |
16 | 19 | } |
17 | 20 |
|
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; |
20 | 29 | } |
21 | 30 |
|
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; |
27 | 42 | } |
| 43 | + return *this; |
| 44 | +} |
28 | 45 |
|
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"); |
44 | 51 | } |
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"); |
49 | 55 | } |
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"); |
65 | 59 | } |
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); |
77 | 65 | } |
78 | | - return success; |
79 | 66 | } |
80 | 67 |
|
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 | + } |
84 | 73 | } |
0 commit comments