Reimplement BpfProgram

This commit is contained in:
Pragyansh Chaturvedi
2025-10-18 13:24:03 +05:30
parent 2b99f01b02
commit 5c74be041e
3 changed files with 63 additions and 76 deletions

View File

@ -65,7 +65,7 @@ void BpfObject::load() {
}
if (bpf_object__load(obj_)) {
std::string error_msg = " object from file '" + object_path_ + "': " + std::strerror(errno);
error_msg += " object from file '" + object_path_ + "': " + std::strerror(errno);
bpf_object__close(obj_);
obj_ = nullptr;
throw BpfException(error_msg);

View File

@ -1,84 +1,73 @@
#include "bpf_program.h"
#include "bpf_exception.h"
#include <filesystem>
#include <utility>
#include <cerrno>
BpfProgram::BpfProgram(std::string object_path, std::string program_name)
: obj_(nullptr), prog_(nullptr), link_(nullptr),
object_path_(std::move(object_path)), program_name_(std::move(program_name)) {
BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name)
: parent_obj_(parent),
prog_(raw_prog),
link_(nullptr),
program_name_(program_name) {
if (!parent)
throw BpfException("Parent BpfObject is null");
if (!raw_prog)
throw BpfException("bpf_program pointer is null");
}
BpfProgram::~BpfProgram() {
destroy();
if (obj_) {
bpf_object__close(obj_);
detach();
}
BpfProgram::BpfProgram(BpfProgram&& other) noexcept
: parent_obj_(std::move(other.parent_obj_)),
prog_(other.prog_),
link_(other.link_),
program_name_(std::move(other.program_name_)) {
other.prog_ = nullptr;
other.link_ = nullptr;
}
BpfProgram& BpfProgram::operator=(BpfProgram&& other) noexcept {
if (this != &other) {
detach();
parent_obj_ = std::move(other.parent_obj_);
prog_ = other.prog_;
link_ = other.link_;
program_name_ = std::move(other.program_name_);
other.prog_ = nullptr;
other.link_ = nullptr;
}
return *this;
}
void BpfProgram::attach() {
// Check if parent is still alive
auto parent = parent_obj_.lock();
if (!parent) {
throw BpfException("Parent BpfObject has been destroyed");
}
if (link_) {
throw BpfException("Program '" + program_name_ + "' already attached");
}
if (!prog_) {
throw BpfException("Program '" + program_name_ + "' not initialized");
}
link_ = bpf_program__attach(prog_);
if (!link_) {
std::string err_msg = "bpf_program__attach failed for program '" + program_name_ + "': " + std::strerror(errno);
throw BpfException(err_msg);
}
}
struct bpf_object * BpfProgram::get_obj() const {
return obj_;
}
bool BpfProgram::load() {
// Open the eBPF object file
obj_ = bpf_object__open_file(object_path_.c_str(), nullptr);
if (libbpf_get_error(obj_)) {
throw BpfException("Failed to open BPF object file: " + object_path_);
void BpfProgram::detach() {
if (link_) {
bpf_link__destroy(link_);
link_ = nullptr;
}
// Find the program by name (if specified)
if (!program_name_.empty()) {
prog_ = bpf_object__find_program_by_name(obj_, program_name_.c_str());
if (!prog_) {
throw BpfException("Program '" + program_name_ + "' not found in object");
}
} else {
while ((prog_ = bpf_object__next_program(obj_, prog_)) != nullptr) {
programs.emplace_back(prog_, nullptr);
}
// throw if no programs found
if (programs.empty()) {
throw BpfException("No programs found in object file");
}
}
// Load the eBPF object into the kernel
if (bpf_object__load(obj_)) {
throw BpfException("Failed to load BPF object into kernel");
}
return true;
}
bool BpfProgram::attach() {
for (auto [prog, link]: programs) {
if (!prog) {
throw BpfException("Program not loaded");
}
link = bpf_program__attach(prog);
if (libbpf_get_error(link)) {
link = nullptr;
throw BpfException("Failed to attach BPF program");
}
}
return true;
}
bool BpfProgram::destroy() {
bool success = true;
for (auto [prog, link]: programs) {
if (!prog) {
throw BpfException("Program not loaded");
}
success = success & bpf_link__destroy(link);
}
return success;
}
void BpfProgram::load_and_attach() {
load();
attach();
}

View File

@ -16,7 +16,7 @@ private:
std::string program_name_;
public:
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, std::string program_name = "");
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name);
~BpfProgram();
@ -28,8 +28,6 @@ public:
bool attach();
bool detach();
void load_and_attach();
[[nodiscard]] bool is_attached() const { return link_ != nullptr; }
[[nodiscard]] std::string get_name() const { return program_name_; }
};