Janitorial: clang-format

This commit is contained in:
Pragyansh Chaturvedi
2025-10-18 21:00:16 +05:30
parent c5a485b526
commit 4a5ff0c1c2
8 changed files with 623 additions and 637 deletions

View File

@ -1,14 +1,11 @@
#include "bpf_map.h" #include "bpf_map.h"
#include "bpf_object.h"
#include "bpf_exception.h" #include "bpf_exception.h"
#include "bpf_object.h"
BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map, const std::string &map_name) BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
: parent_obj_(parent), const std::string &map_name)
map_(raw_map), : parent_obj_(parent), map_(raw_map), map_fd_(-1), map_name_(map_name),
map_fd_(-1), key_size_(0), value_size_(0) {
map_name_(map_name),
key_size_(0),
value_size_(0) {
if (!parent) if (!parent)
throw BpfException("Parent BpfObject is null"); throw BpfException("Parent BpfObject is null");
if (!(parent->is_loaded())) if (!(parent->is_loaded()))
@ -18,7 +15,8 @@ BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map, const
map_fd_ = bpf_map__fd(map_); map_fd_ = bpf_map__fd(map_);
if (map_fd_ < 0) if (map_fd_ < 0)
throw BpfException("Failed to get file descriptor for map '" + map_name_ + "'"); throw BpfException("Failed to get file descriptor for map '" + map_name_ +
"'");
key_size_ = bpf_map__key_size(map_); key_size_ = bpf_map__key_size(map_);
value_size_ = bpf_map__value_size(map_); value_size_ = bpf_map__value_size(map_);
@ -37,20 +35,13 @@ py::object BpfMap::lookup(const py::object &key) const {
// The flags field here matters only when spin locks are used. // The flags field here matters only when spin locks are used.
// Skipping it for now. // Skipping it for now.
const int ret = bpf_map__lookup_elem( const int ret = bpf_map__lookup_elem(map_, key_span.data(), key_size_,
map_, value_span.data(), value_size_, BPF_ANY);
key_span.data(),
key_size_,
value_span.data(),
value_size_,
BPF_ANY);
if (ret < 0) { if (ret < 0) {
if (ret == -ENOENT) if (ret == -ENOENT)
throw py::key_error("Key not found in map '" + map_name_ + "'"); throw py::key_error("Key not found in map '" + map_name_ + "'");
throw BpfException( throw BpfException("Failed to lookup key in map '" + map_name_ +
"Failed to lookup key in map '" + map_name_ + "': " + "': " + std::strerror(-ret));
std::strerror(-ret)
);
} }
return bytes_to_python(value_span); return bytes_to_python(value_span);
@ -67,18 +58,11 @@ void BpfMap::update(const py::object &key, const py::object &value) const {
python_to_bytes_inplace(key, key_span); python_to_bytes_inplace(key, key_span);
python_to_bytes_inplace(value, value_span); python_to_bytes_inplace(value, value_span);
const int ret = bpf_map__update_elem( const int ret = bpf_map__update_elem(map_, key_span.data(), key_size_,
map_, value_span.data(), value_size_, BPF_ANY);
key_span.data(),
key_size_,
value_span.data(),
value_size_,
BPF_ANY);
if (ret < 0) { if (ret < 0) {
throw BpfException( throw BpfException("Failed to update key in map '" + map_name_ +
"Failed to update key in map '" + map_name_ + "': " + "': " + std::strerror(-ret));
std::strerror(-ret)
);
} }
} }
@ -92,15 +76,14 @@ void BpfMap::delete_elem(const py::object &key) const {
// Convert Python → bytes // Convert Python → bytes
python_to_bytes_inplace(key, key_span); python_to_bytes_inplace(key, key_span);
const int ret = bpf_map__delete_elem(map_, key_span.data(), key_size_, BPF_ANY); const int ret =
bpf_map__delete_elem(map_, key_span.data(), key_size_, BPF_ANY);
if (ret != 0) { if (ret != 0) {
if (ret == -ENOENT) if (ret == -ENOENT)
throw py::key_error("Key not found in map '" + map_name_ + "'"); throw py::key_error("Key not found in map '" + map_name_ + "'");
throw BpfException( throw BpfException("Failed to delete key from map '" + map_name_ +
"Failed to delete key from map '" + map_name_ + "': " + "': " + std::strerror(-ret));
std::strerror(-ret)
);
} }
} }
@ -115,7 +98,8 @@ py::object BpfMap::get_next_key(const py::object &key) const {
BufferManager<> key_buf; BufferManager<> key_buf;
auto key_bytes = key_buf.get_span(key_size_); auto key_bytes = key_buf.get_span(key_size_);
python_to_bytes_inplace(key, key_bytes); python_to_bytes_inplace(key, key_bytes);
ret = bpf_map__get_next_key(map_, key_bytes.data(), next_key.data(), key_size_); ret = bpf_map__get_next_key(map_, key_bytes.data(), next_key.data(),
key_size_);
} }
if (ret < 0) { if (ret < 0) {
@ -123,10 +107,8 @@ py::object BpfMap::get_next_key(const py::object &key) const {
// No more keys // No more keys
return py::none(); return py::none();
} }
throw BpfException( throw BpfException("Failed to get next key in map '" + map_name_ +
"Failed to get next key in map '" + map_name_ + "': " + "': " + std::strerror(-ret));
std::strerror(-ret)
);
} }
return bytes_to_python(next_key); return bytes_to_python(next_key);
@ -190,17 +172,13 @@ py::list BpfMap::values() const {
return result; return result;
} }
int BpfMap::get_type() const { int BpfMap::get_type() const { return bpf_map__type(map_); }
return bpf_map__type(map_);
}
int BpfMap::get_max_entries() const {
return bpf_map__max_entries(map_);
}
int BpfMap::get_max_entries() const { return bpf_map__max_entries(map_); }
// Helper functions // Helper functions
void BpfMap::python_to_bytes_inplace(const py::object &obj, std::span<uint8_t> buffer) { void BpfMap::python_to_bytes_inplace(const py::object &obj,
std::span<uint8_t> buffer) {
std::fill(buffer.begin(), buffer.end(), 0); std::fill(buffer.begin(), buffer.end(), 0);
if (py::isinstance<py::int_>(obj)) { if (py::isinstance<py::int_>(obj)) {
@ -214,10 +192,9 @@ void BpfMap::python_to_bytes_inplace(const py::object &obj, std::span<uint8_t> b
std::string bytes_str = obj.cast<std::string>(); std::string bytes_str = obj.cast<std::string>();
if (bytes_str.size() > buffer.size()) { if (bytes_str.size() > buffer.size()) {
throw BpfException( throw BpfException("Bytes size " + std::to_string(bytes_str.size()) +
"Bytes size " + std::to_string(bytes_str.size()) + " exceeds expected size " +
" exceeds expected size " + std::to_string(buffer.size()) std::to_string(buffer.size()));
);
} }
std::memcpy(buffer.data(), bytes_str.data(), bytes_str.size()); std::memcpy(buffer.data(), bytes_str.data(), bytes_str.size());

View File

@ -1,15 +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 <string>
#include <span> #include <span>
#include <array> #include <string>
#include <algorithm> #include <vector>
#include <cstring>
#include <cerrno>
class BpfObject; class BpfObject;
@ -23,8 +23,7 @@ private:
std::string map_name_; std::string map_name_;
__u32 key_size_, value_size_; __u32 key_size_, value_size_;
template<size_t StackSize = 64> template <size_t StackSize = 64> struct BufferManager {
struct BufferManager {
std::array<uint8_t, StackSize> stack_buf; std::array<uint8_t, StackSize> stack_buf;
std::vector<uint8_t> heap_buf; std::vector<uint8_t> heap_buf;
@ -39,7 +38,8 @@ private:
}; };
public: public:
BpfMap(std::shared_ptr<BpfObject> parent, 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;
@ -64,7 +64,8 @@ public:
[[nodiscard]] int get_max_entries() const; [[nodiscard]] int get_max_entries() const;
private: private:
static void python_to_bytes_inplace(const py::object &obj, std::span<uint8_t> buffer); static void python_to_bytes_inplace(const py::object &obj,
std::span<uint8_t> buffer);
static py::object bytes_to_python(std::span<const uint8_t> data); static py::object bytes_to_python(std::span<const uint8_t> data);
}; };

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,12 +1,12 @@
#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");
@ -16,15 +16,11 @@ BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *ra
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;
@ -62,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();