2 Commits

Author SHA1 Message Date
4a5ff0c1c2 Janitorial: clang-format 2025-10-18 21:00:16 +05:30
c5a485b526 Reimplement BpfMap 2025-10-18 20:59:31 +05:30
8 changed files with 664 additions and 611 deletions

View File

@ -1,43 +1,218 @@
#include "bpf_map.h" #include "bpf_map.h"
#include "bpf_exception.h" #include "bpf_exception.h"
#include "bpf_object.h"
BpfMap::BpfMap(BpfProgram *program_, const py::object &map_from_python) { BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
if (py::isinstance<py::function>(map_from_python)) { const std::string &map_name)
const auto name = map_from_python.attr("__name__").cast<std::string>(); : parent_obj_(parent), map_(raw_map), map_fd_(-1), map_name_(map_name),
bpf_program = program_; key_size_(0), value_size_(0) {
map_ = bpf_object__find_map_by_name(bpf_program->get_obj(), name.c_str()); if (!parent)
if (!map_) { throw BpfException("Parent BpfObject is null");
throw BpfException("Failed to find map by name"); if (!(parent->is_loaded()))
throw BpfException("Parent BpfObject is not loaded");
if (!raw_map)
throw BpfException("bpf_map pointer is null");
map_fd_ = bpf_map__fd(map_);
if (map_fd_ < 0)
throw BpfException("Failed to get file descriptor for map '" + map_name_ +
"'");
key_size_ = bpf_map__key_size(map_);
value_size_ = bpf_map__value_size(map_);
} }
map_fd = bpf_map__fd(map_);
if (map_fd == -1) { py::object BpfMap::lookup(const py::object &key) const {
throw BpfException("Failed to open map File Descriptor"); if (map_fd_ < 0)
throw BpfException("Map '" + map_name_ + "' is not initialized properly");
BufferManager<> key_buf, value_buf;
auto key_span = key_buf.get_span(key_size_);
auto value_span = value_buf.get_span(value_size_);
// Convert Python → bytes
python_to_bytes_inplace(key, key_span);
// The flags field here matters only when spin locks are used.
// Skipping it for now.
const int ret = bpf_map__lookup_elem(map_, key_span.data(), key_size_,
value_span.data(), value_size_, BPF_ANY);
if (ret < 0) {
if (ret == -ENOENT)
throw py::key_error("Key not found in map '" + map_name_ + "'");
throw BpfException("Failed to lookup key in map '" + map_name_ +
"': " + std::strerror(-ret));
} }
return bytes_to_python(value_span);
}
void BpfMap::update(const py::object &key, const py::object &value) const {
if (map_fd_ < 0)
throw BpfException("Map '" + map_name_ + "' is not initialized properly");
BufferManager<> key_buf, value_buf;
auto key_span = key_buf.get_span(key_size_);
auto value_span = value_buf.get_span(value_size_);
python_to_bytes_inplace(key, key_span);
python_to_bytes_inplace(value, value_span);
const int ret = bpf_map__update_elem(map_, key_span.data(), key_size_,
value_span.data(), value_size_, BPF_ANY);
if (ret < 0) {
throw BpfException("Failed to update key in map '" + map_name_ +
"': " + std::strerror(-ret));
}
}
void BpfMap::delete_elem(const py::object &key) const {
if (map_fd_ < 0)
throw BpfException("Map '" + map_name_ + "' is not initialized properly");
BufferManager<> key_buf;
auto key_span = key_buf.get_span(key_size_);
// Convert Python → bytes
python_to_bytes_inplace(key, key_span);
const int ret =
bpf_map__delete_elem(map_, key_span.data(), key_size_, BPF_ANY);
if (ret != 0) {
if (ret == -ENOENT)
throw py::key_error("Key not found in map '" + map_name_ + "'");
throw BpfException("Failed to delete key from map '" + map_name_ +
"': " + std::strerror(-ret));
}
}
py::object BpfMap::get_next_key(const py::object &key) const {
BufferManager<> next_key_buf;
auto next_key = next_key_buf.get_span(key_size_);
int ret;
if (key.is_none()) {
ret = bpf_map__get_next_key(map_, nullptr, next_key.data(), key_size_);
} else { } else {
throw BpfException("Invalid map object passed to function."); BufferManager<> key_buf;
} auto key_bytes = key_buf.get_span(key_size_);
python_to_bytes_inplace(key, key_bytes);
ret = bpf_map__get_next_key(map_, key_bytes.data(), next_key.data(),
key_size_);
} }
std::vector<uint8_t> BpfMap::python_to_bytes(const py::object &obj, size_t size) { if (ret < 0) {
std::vector<uint8_t> result(size, 0); if (ret == -ENOENT) {
// No more keys
return py::none();
}
throw BpfException("Failed to get next key in map '" + map_name_ +
"': " + std::strerror(-ret));
}
if (py::isinstance<py::int_>(obj)) { return bytes_to_python(next_key);
const auto value = obj.cast<uint64_t>(); }
std::memcpy(result.data(), &value, std::min(size, sizeof(uint64_t)));
} else if (py::isinstance<py::bytes>(obj)) { py::dict BpfMap::items() const {
const auto bytes_str = obj.cast<std::string>(); py::dict result;
std::memcpy(result.data(), bytes_str.data(), std::min(size, bytes_str.size()));
} else if (py::isinstance<py::str>(obj)) { py::object current_key = get_next_key(py::none());
const auto str_val = obj.cast<std::string>(); if (current_key.is_none()) {
std::memcpy(result.data(), str_val.data(), std::min(size, str_val.size())); return result;
}
while (!current_key.is_none()) {
try {
py::object value = lookup(current_key);
result[current_key] = value;
current_key = get_next_key(current_key);
} catch (const py::key_error &) {
break;
}
} }
return result; return result;
} }
py::object BpfMap::bytes_to_python(const std::vector<uint8_t> &data) { py::list BpfMap::keys() const {
// Try to interpret as integer if it's a common integer size py::list result;
py::object current_key = get_next_key(py::none());
if (current_key.is_none()) {
return result;
}
while (!current_key.is_none()) {
result.append(current_key);
current_key = get_next_key(current_key);
}
return result;
}
py::list BpfMap::values() const {
py::list result;
py::object current_key = get_next_key(py::none());
if (current_key.is_none()) {
return result;
}
while (!current_key.is_none()) {
try {
py::object value = lookup(current_key);
result.append(value);
current_key = get_next_key(current_key);
} catch (const py::key_error &) {
break;
}
}
return result;
}
int BpfMap::get_type() const { return bpf_map__type(map_); }
int BpfMap::get_max_entries() const { return bpf_map__max_entries(map_); }
// Helper functions
void BpfMap::python_to_bytes_inplace(const py::object &obj,
std::span<uint8_t> buffer) {
std::fill(buffer.begin(), buffer.end(), 0);
if (py::isinstance<py::int_>(obj)) {
if (buffer.size() <= sizeof(uint64_t)) {
uint64_t value = obj.cast<uint64_t>();
std::memcpy(buffer.data(), &value, buffer.size());
} else {
throw BpfException("Integer key/value size exceeds maximum (8 bytes)");
}
} else if (py::isinstance<py::bytes>(obj)) {
std::string bytes_str = obj.cast<std::string>();
if (bytes_str.size() > buffer.size()) {
throw BpfException("Bytes size " + std::to_string(bytes_str.size()) +
" exceeds expected size " +
std::to_string(buffer.size()));
}
std::memcpy(buffer.data(), bytes_str.data(), bytes_str.size());
} else if (py::isinstance<py::str>(obj)) {
std::string str_val = obj.cast<std::string>();
if (str_val.size() >= buffer.size()) {
throw BpfException("String size exceeds expected size");
}
std::memcpy(buffer.data(), str_val.data(), str_val.size());
buffer[str_val.size()] = '\0';
} else {
throw BpfException("Unsupported type for BPF map key/value");
}
}
py::object BpfMap::bytes_to_python(std::span<const uint8_t> data) {
if (data.size() == 4) { if (data.size() == 4) {
uint32_t value; uint32_t value;
std::memcpy(&value, data.data(), 4); std::memcpy(&value, data.data(), 4);
@ -47,165 +222,6 @@ py::object BpfMap::bytes_to_python(const std::vector<uint8_t> &data) {
std::memcpy(&value, data.data(), 8); std::memcpy(&value, data.data(), 8);
return py::cast(value); return py::cast(value);
} else { } else {
// Return as bytes
return py::bytes(reinterpret_cast<const char *>(data.data()), data.size()); return py::bytes(reinterpret_cast<const char *>(data.data()), data.size());
} }
} }
void BpfMap::update(const py::object &key, const py::object &value) const {
const size_t key_size = bpf_map__key_size(map_);
const size_t value_size = bpf_map__value_size(map_);
const auto key_bytes = python_to_bytes(key, key_size);
const auto value_bytes = python_to_bytes(value, value_size);
const int ret = bpf_map__update_elem(
map_,
key_bytes.data(),
key_size,
value_bytes.data(),
value_size,
BPF_ANY);
if (ret != 0) {
throw BpfException("Failed to update map element");
}
}
void BpfMap::delete_elem(const py::object &key) const {
const size_t key_size = bpf_map__key_size(map_);
std::vector<uint8_t> key_bytes;
key_bytes = python_to_bytes(key, key_size);
if (const int ret = bpf_map__delete_elem(map_, key_bytes.data(), key_size, BPF_ANY); ret != 0) {
throw BpfException("Failed to delete map element");
}
}
py::list BpfMap::get_next_key(const py::object &key) const {
const size_t key_size = bpf_map__key_size(map_);
std::vector<uint8_t> next_key(key_size);
int ret;
if (key.is_none()) {
ret = bpf_map__get_next_key(map_, nullptr, next_key.data(), key_size);
} else {
const auto key_bytes = python_to_bytes(key, key_size);
ret = bpf_map__get_next_key(map_, key_bytes.data(), next_key.data(), key_size);
}
py::list result;
if (ret == 0) {
result.append(bytes_to_python(next_key));
}
return result;
}
py::list BpfMap::keys() const {
py::list result;
const size_t key_size = bpf_map__key_size(map_);
std::vector<uint8_t> key(key_size);
std::vector<uint8_t> next_key(key_size);
int ret = bpf_map__get_next_key(map_, nullptr, key.data(), key_size);
while (ret == 0) {
result.append(bytes_to_python(key));
ret = bpf_map__get_next_key(map_, key.data(), next_key.data(), key_size);
key = next_key;
}
return result;
}
py::list BpfMap::values() const {
py::list result;
const size_t key_size = bpf_map__key_size(map_);
const size_t value_size = bpf_map__value_size(map_);
std::vector<uint8_t> key(key_size);
std::vector<uint8_t> next_key(key_size);
std::vector<uint8_t> value(value_size);
int ret = bpf_map__get_next_key(map_, nullptr, key.data(), key_size);
while (ret == 0) {
if (bpf_map__lookup_elem(map_, key.data(), key_size, value.data(), value_size, BPF_ANY) == 0) {
result.append(bytes_to_python(value));
}
ret = bpf_map__get_next_key(map_, key.data(), next_key.data(), key_size);
key = next_key;
}
return result;
}
std::string BpfMap::get_name() const {
const char *name = bpf_map__name(map_);
return name ? std::string(name) : "";
}
int BpfMap::get_type() const {
return bpf_map__type(map_);
}
int BpfMap::get_key_size() const {
return bpf_map__key_size(map_);
}
int BpfMap::get_value_size() const {
return bpf_map__value_size(map_);
}
int BpfMap::get_max_entries() const {
return bpf_map__max_entries(map_);
}
py::dict BpfMap::items() const {
py::dict result;
const size_t key_size = bpf_map__key_size(map_);
const size_t value_size = bpf_map__value_size(map_);
std::vector<uint8_t> key(key_size);
std::vector<uint8_t> next_key(key_size);
std::vector<uint8_t> value(value_size);
// Get first key
int ret = bpf_map__get_next_key(map_, nullptr, key.data(), key_size);
while (ret == 0) {
// Lookup value for current key
if (bpf_map__lookup_elem(map_, key.data(), key_size, value.data(), value_size, BPF_ANY) == 0) {
result[bytes_to_python(key)] = bytes_to_python(value);
}
// Get next key
ret = bpf_map__get_next_key(map_, key.data(), next_key.data(), key_size);
key = next_key;
}
return result;
}
py::object BpfMap::lookup(const py::object &key) const {
const __u32 key_size = bpf_map__key_size(map_);
const __u32 value_size = bpf_map__value_size(map_);
const auto key_bytes = python_to_bytes(key, key_size);
std::vector<uint8_t> value_bytes(value_size);
// The flags field here matters only when spin locks are used which is close to fucking never, so fuck no,
// im not adding it
const int ret = bpf_map__lookup_elem(
map_,
key_bytes.data(),
key_size,
value_bytes.data(),
value_size,
BPF_ANY);
if (ret != 0) {
return py::none();
}
return bytes_to_python(value_bytes);
}

View File

@ -1,10 +1,15 @@
#ifndef PYLIBBPF_BPF_MAP_H #ifndef PYLIBBPF_BPF_MAP_H
#define PYLIBBPF_BPF_MAP_H #define PYLIBBPF_BPF_MAP_H
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstring>
#include <libbpf.h> #include <libbpf.h>
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <vector> #include <span>
#include <string> #include <string>
#include <vector>
class BpfObject; class BpfObject;
@ -16,16 +21,37 @@ private:
struct bpf_map *map_; struct bpf_map *map_;
int map_fd_; int map_fd_;
std::string map_name_; std::string map_name_;
__u32 key_size_, value_size_;
template <size_t StackSize = 64> struct BufferManager {
std::array<uint8_t, StackSize> stack_buf;
std::vector<uint8_t> heap_buf;
std::span<uint8_t> get_span(size_t size) {
if (size <= StackSize) {
return std::span<uint8_t>(stack_buf.data(), size);
} else {
heap_buf.resize(size);
return std::span<uint8_t>(heap_buf);
}
}
};
public: public:
BpfMap(std::shared_ptr<BpfObject>, struct bpf_map *raw_map, const std::string &map_name); BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
const std::string &map_name);
~BpfMap() = default; ~BpfMap() = default;
BpfMap(const BpfMap &) = delete;
BpfMap &operator=(const BpfMap &) = delete;
BpfMap(BpfMap &&) noexcept = default;
BpfMap &operator=(BpfMap &&) noexcept = default;
[[nodiscard]] py::object lookup(const py::object &key) const; [[nodiscard]] py::object lookup(const py::object &key) const;
void update(const py::object &key, const py::object &value) const; void update(const py::object &key, const py::object &value) const;
void delete_elem(const py::object &key) const; void delete_elem(const py::object &key) const;
py::list get_next_key(const py::object &key = py::none()) const; py::object get_next_key(const py::object &key = py::none()) const;
py::dict items() const; py::dict items() const;
py::list keys() const; py::list keys() const;
py::list values() const; py::list values() const;
@ -33,13 +59,14 @@ public:
[[nodiscard]] std::string get_name() const { return map_name_; } [[nodiscard]] std::string get_name() const { return map_name_; }
[[nodiscard]] int get_fd() const { return map_fd_; } [[nodiscard]] int get_fd() const { return map_fd_; }
[[nodiscard]] int get_type() const; [[nodiscard]] int get_type() const;
[[nodiscard]] int get_key_size() const; [[nodiscard]] int get_key_size() const { return key_size_; };
[[nodiscard]] int get_value_size() const; [[nodiscard]] int get_value_size() const { return value_size_; };
[[nodiscard]] int get_max_entries() const; [[nodiscard]] int get_max_entries() const;
private: private:
static std::vector<uint8_t> python_to_bytes(const py::object &obj, size_t size); static void python_to_bytes_inplace(const py::object &obj,
static py::object bytes_to_python(const std::vector<uint8_t> &data); std::span<uint8_t> buffer);
static py::object bytes_to_python(std::span<const uint8_t> data);
}; };
#endif // PYLIBBPF_MAPS_H #endif // PYLIBBPF_MAPS_H

View File

@ -1,12 +1,11 @@
#include "bpf_object.h" #include "bpf_object.h"
#include "bpf_program.h"
#include "bpf_map.h"
#include "bpf_exception.h" #include "bpf_exception.h"
#include "bpf_map.h"
#include "bpf_program.h"
#include <cerrno> #include <cerrno>
BpfObject::BpfObject(std::string object_path) BpfObject::BpfObject(std::string object_path)
: obj_(nullptr), object_path_(std::move(object_path)), loaded_(false) { : obj_(nullptr), object_path_(std::move(object_path)), loaded_(false) {}
}
BpfObject::~BpfObject() { BpfObject::~BpfObject() {
// Clear caches first (order matters!) // Clear caches first (order matters!)
@ -21,10 +20,8 @@ BpfObject::~BpfObject() {
} }
BpfObject::BpfObject(BpfObject &&other) noexcept BpfObject::BpfObject(BpfObject &&other) noexcept
: obj_(other.obj_), : obj_(other.obj_), object_path_(std::move(other.object_path_)),
object_path_(std::move(other.object_path_)), loaded_(other.loaded_), prog_cache_(std::move(other.prog_cache_)),
loaded_(other.loaded_),
prog_cache_(std::move(other.prog_cache_)),
maps_cache_(std::move(other.maps_cache_)) { maps_cache_(std::move(other.maps_cache_)) {
other.obj_ = nullptr; other.obj_ = nullptr;
@ -65,7 +62,8 @@ void BpfObject::load() {
} }
if (bpf_object__load(obj_)) { if (bpf_object__load(obj_)) {
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);
@ -92,7 +90,8 @@ py::list BpfObject::get_program_names() const {
return names; return names;
} }
std::shared_ptr<BpfProgram> BpfObject::_get_or_create_program(struct bpf_program *prog) { std::shared_ptr<BpfProgram>
BpfObject::_get_or_create_program(struct bpf_program *prog) {
if (!prog) { if (!prog) {
throw BpfException("bpf_program pointer is null"); throw BpfException("bpf_program pointer is null");
} }
@ -132,12 +131,14 @@ std::shared_ptr<BpfProgram> BpfObject::get_program(const std::string& name) {
return prog; return prog;
} }
struct bpf_program* BpfObject::find_program_by_name(const std::string& name) const { struct bpf_program *
BpfObject::find_program_by_name(const std::string &name) const {
if (!loaded_) { if (!loaded_) {
throw BpfException("BPF object not loaded"); throw BpfException("BPF object not loaded");
} }
struct bpf_program *prog = bpf_object__find_program_by_name(obj_, name.c_str()); struct bpf_program *prog =
bpf_object__find_program_by_name(obj_, name.c_str());
if (!prog) { if (!prog) {
throw BpfException("Program '" + name + "' not found"); throw BpfException("Program '" + name + "' not found");
} }

View File

@ -2,8 +2,8 @@
#define PYLIBBPF_BPF_OBJECT_H #define PYLIBBPF_BPF_OBJECT_H
#include <libbpf.h> #include <libbpf.h>
#include <pybind11/pybind11.h>
#include <memory> #include <memory>
#include <pybind11/pybind11.h>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -26,7 +26,8 @@ private:
bool loaded_; bool loaded_;
mutable std::unordered_map<std::string, std::shared_ptr<BpfMap>> maps_cache_; mutable std::unordered_map<std::string, std::shared_ptr<BpfMap>> maps_cache_;
mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>> prog_cache_; mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>>
prog_cache_;
std::shared_ptr<BpfProgram> _get_or_create_program(struct bpf_program *prog); std::shared_ptr<BpfProgram> _get_or_create_program(struct bpf_program *prog);
std::shared_ptr<BpfMap> _get_or_create_map(struct bpf_map *map); std::shared_ptr<BpfMap> _get_or_create_map(struct bpf_map *map);
@ -65,8 +66,10 @@ public:
// Program access // Program access
[[nodiscard]] py::list get_program_names() const; [[nodiscard]] py::list get_program_names() const;
[[nodiscard]] std::shared_ptr<BpfProgram> get_program(const std::string& name); [[nodiscard]] std::shared_ptr<BpfProgram>
[[nodiscard]] struct bpf_program* find_program_by_name(const std::string& name) const; get_program(const std::string &name);
[[nodiscard]] struct bpf_program *
find_program_by_name(const std::string &name) const;
[[nodiscard]] py::dict get_cached_programs() const; [[nodiscard]] py::dict get_cached_programs() const;
// Map access // Map access

View File

@ -1,7 +1,8 @@
#include "bpf_perf_buffer.h" #include "bpf_perf_buffer.h"
#include "bpf_exception.h" #include "bpf_exception.h"
void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data, unsigned int size) { void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data,
unsigned int size) {
auto *self = static_cast<BpfPerfBuffer *>(ctx); auto *self = static_cast<BpfPerfBuffer *>(ctx);
// Acquire GIL for Python calls // Acquire GIL for Python calls
@ -18,7 +19,8 @@ void BpfPerfBuffer::sample_callback_wrapper(void *ctx, int cpu, void *data, unsi
} }
} }
void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt) { void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu,
unsigned long long cnt) {
auto *self = static_cast<BpfPerfBuffer *>(ctx); auto *self = static_cast<BpfPerfBuffer *>(ctx);
if (self->lost_callback_.is_none()) { if (self->lost_callback_.is_none()) {
@ -34,7 +36,8 @@ void BpfPerfBuffer::lost_callback_wrapper(void *ctx, int cpu, unsigned long long
} }
} }
BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback, py::object lost_callback) BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
py::object lost_callback)
: pb_(nullptr), callback_(std::move(callback)) { : pb_(nullptr), callback_(std::move(callback)) {
if (!lost_callback.is_none()) { if (!lost_callback.is_none()) {

View File

@ -2,8 +2,8 @@
#define PYLIBBPF_BPF_PERF_BUFFER_H #define PYLIBBPF_BPF_PERF_BUFFER_H
#include <libbpf.h> #include <libbpf.h>
#include <pybind11/pybind11.h>
#include <pybind11/functional.h> #include <pybind11/functional.h>
#include <pybind11/pybind11.h>
namespace py = pybind11; namespace py = pybind11;
@ -14,11 +14,13 @@ private:
py::function lost_callback_; py::function lost_callback_;
// Static callback wrappers for C API // Static callback wrappers for C API
static void sample_callback_wrapper(void *ctx, int cpu, void *data, unsigned int size); static void sample_callback_wrapper(void *ctx, int cpu, void *data,
unsigned int size);
static void lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt); static void lost_callback_wrapper(void *ctx, int cpu, unsigned long long cnt);
public: public:
BpfPerfBuffer(int map_fd, int page_cnt, py::function callback, py::object lost_callback); BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
py::object lost_callback);
~BpfPerfBuffer(); ~BpfPerfBuffer();
int poll(int timeout_ms); int poll(int timeout_ms);

View File

@ -1,28 +1,26 @@
#include "bpf_program.h" #include "bpf_program.h"
#include "bpf_exception.h" #include "bpf_exception.h"
#include <utility>
#include <cerrno> #include <cerrno>
#include <utility>
BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name) BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent,
: parent_obj_(parent), struct bpf_program *raw_prog,
prog_(raw_prog), const std::string &program_name)
link_(nullptr), : parent_obj_(parent), prog_(raw_prog), link_(nullptr),
program_name_(program_name) { program_name_(program_name) {
if (!parent) if (!parent)
throw BpfException("Parent BpfObject is null"); throw BpfException("Parent BpfObject is null");
if (!(parent->is_loaded()))
throw BpfException("Parent BpfObject is not loaded");
if (!raw_prog) if (!raw_prog)
throw BpfException("bpf_program pointer is null"); throw BpfException("bpf_program pointer is null");
} }
BpfProgram::~BpfProgram() { BpfProgram::~BpfProgram() { detach(); }
detach();
}
BpfProgram::BpfProgram(BpfProgram &&other) noexcept BpfProgram::BpfProgram(BpfProgram &&other) noexcept
: parent_obj_(std::move(other.parent_obj_)), : parent_obj_(std::move(other.parent_obj_)), prog_(other.prog_),
prog_(other.prog_), link_(other.link_), program_name_(std::move(other.program_name_)) {
link_(other.link_),
program_name_(std::move(other.program_name_)) {
other.prog_ = nullptr; other.prog_ = nullptr;
other.link_ = nullptr; other.link_ = nullptr;
@ -60,7 +58,8 @@ void BpfProgram::attach() {
link_ = bpf_program__attach(prog_); link_ = bpf_program__attach(prog_);
if (!link_) { if (!link_) {
std::string err_msg = "bpf_program__attach failed for program '" + program_name_ + "': " + std::strerror(errno); std::string err_msg = "bpf_program__attach failed for program '" +
program_name_ + "': " + std::strerror(errno);
throw BpfException(err_msg); throw BpfException(err_msg);
} }
} }

View File

@ -15,7 +15,9 @@ private:
std::string program_name_; std::string program_name_;
public: public:
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, const std::string& program_name); explicit BpfProgram(std::shared_ptr<BpfObject> parent,
struct bpf_program *raw_prog,
const std::string &program_name);
~BpfProgram(); ~BpfProgram();