mirror of
https://github.com/varun-r-mallya/pylibbpf.git
synced 2026-03-21 12:41:30 +00:00
Reimplement BpfProgram
This commit is contained in:
@ -65,7 +65,7 @@ void BpfObject::load() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bpf_object__load(obj_)) {
|
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_);
|
bpf_object__close(obj_);
|
||||||
obj_ = nullptr;
|
obj_ = nullptr;
|
||||||
throw BpfException(error_msg);
|
throw BpfException(error_msg);
|
||||||
|
|||||||
@ -1,84 +1,73 @@
|
|||||||
#include "bpf_program.h"
|
#include "bpf_program.h"
|
||||||
#include "bpf_exception.h"
|
#include "bpf_exception.h"
|
||||||
#include <filesystem>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
BpfProgram::BpfProgram(std::string object_path, std::string program_name)
|
BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name)
|
||||||
: obj_(nullptr), prog_(nullptr), link_(nullptr),
|
: parent_obj_(parent),
|
||||||
object_path_(std::move(object_path)), program_name_(std::move(program_name)) {
|
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() {
|
BpfProgram::~BpfProgram() {
|
||||||
destroy();
|
detach();
|
||||||
if (obj_) {
|
}
|
||||||
bpf_object__close(obj_);
|
|
||||||
|
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 {
|
void BpfProgram::detach() {
|
||||||
return obj_;
|
if (link_) {
|
||||||
}
|
bpf_link__destroy(link_);
|
||||||
|
link_ = nullptr;
|
||||||
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_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,7 @@ private:
|
|||||||
std::string program_name_;
|
std::string program_name_;
|
||||||
|
|
||||||
public:
|
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();
|
~BpfProgram();
|
||||||
|
|
||||||
@ -28,8 +28,6 @@ public:
|
|||||||
bool attach();
|
bool attach();
|
||||||
bool detach();
|
bool detach();
|
||||||
|
|
||||||
void load_and_attach();
|
|
||||||
|
|
||||||
[[nodiscard]] bool is_attached() const { return link_ != nullptr; }
|
[[nodiscard]] bool is_attached() const { return link_ != nullptr; }
|
||||||
[[nodiscard]] std::string get_name() const { return program_name_; }
|
[[nodiscard]] std::string get_name() const { return program_name_; }
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user