3 Commits

5 changed files with 88 additions and 113 deletions

View File

@ -1,54 +1,44 @@
#ifndef PYLIBBPF_MAPS_H
#define PYLIBBPF_MAPS_H
#ifndef PYLIBBPF_BPF_MAP_H
#define PYLIBBPF_BPF_MAP_H
#include <libbpf.h>
#include <pybind11/pybind11.h>
#include <vector>
#include <string>
#include "bpf_program.h"
class BpfObject;
namespace py = pybind11;
class BpfMap {
private:
std::weak_ptr<BpfObject> parent_obj_;
struct bpf_map *map_;
int map_fd = -1;
//TODO: turn below into a shared pointer and ref count it so that there is no resource leakage
BpfProgram *bpf_program;
int map_fd_;
std::string map_name_;
public:
BpfMap(BpfProgram *program_, const py::object &map_from_python);
BpfMap(std::shared_ptr<BpfObject>, struct bpf_map *raw_map, const std::string &map_name);
~BpfMap() = default;
[[nodiscard]] py::object lookup(const py::object &key) const;
void update(const py::object &key, const py::object &value) const;
void delete_elem(const py::object &key) const;
py::list get_next_key(const py::object &key = py::none()) const;
py::dict items() const;
py::list keys() const;
py::list values() const;
[[nodiscard]] std::string get_name() const;
int get_type() const;
int get_key_size() const;
int get_value_size() const;
int get_max_entries() const;
[[nodiscard]] std::string get_name() const { return map_name_; }
[[nodiscard]] int get_fd() const { return map_fd_; }
[[nodiscard]] int get_type() const;
[[nodiscard]] int get_key_size() const;
[[nodiscard]] int get_value_size() const;
[[nodiscard]] int get_max_entries() const;
private:
static std::vector<uint8_t> python_to_bytes(const py::object &obj, size_t size);
static py::object bytes_to_python(const std::vector<uint8_t> &data);
};

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);
@ -206,7 +206,7 @@ std::shared_ptr<BpfMap> BpfObject::get_map(const std::string& name) {
// Create and cache
struct bpf_map *raw_map = find_map_by_name(name);
auto map = std::make_shared<BpfMap>(this, raw_map, name);
auto map = std::make_shared<BpfMap>(shared_from_this(), raw_map, name);
maps_cache_[name] = map;
return map;
@ -227,7 +227,7 @@ std::shared_ptr<BpfMap> BpfObject::_get_or_create_map(struct bpf_map *map) {
}
// Create and cache
auto bpf_map = std::make_shared<BpfMap>(this, map, map_name);
auto bpf_map = std::make_shared<BpfMap>(shared_from_this(), map, map_name);
maps_cache_[map_name] = bpf_map;
return bpf_map;

View File

@ -19,7 +19,7 @@ class BpfMap;
* This is the main entry point for loading BPF programs.
* Owns the bpf_object* and manages all programs and maps within it.
*/
class BpfObject {
class BpfObject : public std::enable_shared_from_this<BpfObject> {
private:
struct bpf_object *obj_;
std::string object_path_;

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

@ -2,37 +2,33 @@
#define PYLIBBPF_BPF_PROGRAM_H
#include <libbpf.h>
#include <pybind11/stl.h>
#include <memory>
#include <string>
namespace py = pybind11;
class BpfObject;
class BpfProgram {
private:
struct bpf_object *obj_;
std::weak_ptr<BpfObject> parent_obj_;
struct bpf_program *prog_;
struct bpf_link *link_;
std::string object_path_;
std::string program_name_;
std::vector<std::pair<bpf_program *, bpf_link *> > programs;
public:
explicit BpfProgram(std::string object_path, std::string program_name = "");
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name);
~BpfProgram();
struct bpf_object *get_obj() const;
bool load();
BpfProgram(const BpfProgram&) = delete;
BpfProgram& operator=(const BpfProgram&) = delete;
BpfProgram(BpfProgram&&) noexcept;
BpfProgram& operator=(BpfProgram&&) noexcept;
bool attach();
bool detach();
bool destroy();
void load_and_attach();
[[nodiscard]] bool is_loaded() const { return obj_ != nullptr; }
[[nodiscard]] bool is_attached() const { return link_ != nullptr; }
[[nodiscard]] std::string get_name() const { return program_name_; }
};
#endif //PYLIBBPF_BPF_PROGRAM_H