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,24 +1,22 @@
#include "bpf_map.h"
#include "bpf_object.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)
: parent_obj_(parent),
map_(raw_map),
map_fd_(-1),
map_name_(map_name),
key_size_(0),
value_size_(0) {
BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
const std::string &map_name)
: parent_obj_(parent), map_(raw_map), map_fd_(-1), map_name_(map_name),
key_size_(0), value_size_(0) {
if (!parent)
throw BpfException("Parent BpfObject is null");
if(!(parent->is_loaded()))
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_ + "'");
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_);
@ -37,20 +35,13 @@ py::object BpfMap::lookup(const py::object &key) const {
// 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);
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)
);
throw BpfException("Failed to lookup key in map '" + map_name_ +
"': " + std::strerror(-ret));
}
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(value, value_span);
const int ret = bpf_map__update_elem(
map_,
key_span.data(),
key_size_,
value_span.data(),
value_size_,
BPF_ANY);
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)
);
throw BpfException("Failed to update key in map '" + map_name_ +
"': " + std::strerror(-ret));
}
}
@ -92,15 +76,14 @@ void BpfMap::delete_elem(const py::object &key) const {
// 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);
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)
);
throw BpfException("Failed to delete key from map '" + map_name_ +
"': " + std::strerror(-ret));
}
}
@ -115,7 +98,8 @@ py::object BpfMap::get_next_key(const py::object &key) const {
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_);
ret = bpf_map__get_next_key(map_, key_bytes.data(), next_key.data(),
key_size_);
}
if (ret < 0) {
@ -123,10 +107,8 @@ py::object BpfMap::get_next_key(const py::object &key) const {
// No more keys
return py::none();
}
throw BpfException(
"Failed to get next key in map '" + map_name_ + "': " +
std::strerror(-ret)
);
throw BpfException("Failed to get next key in map '" + map_name_ +
"': " + std::strerror(-ret));
}
return bytes_to_python(next_key);
@ -145,7 +127,7 @@ py::dict BpfMap::items() const {
py::object value = lookup(current_key);
result[current_key] = value;
current_key = get_next_key(current_key);
} catch (const py::key_error&) {
} catch (const py::key_error &) {
break;
}
}
@ -182,7 +164,7 @@ py::list BpfMap::values() const {
py::object value = lookup(current_key);
result.append(value);
current_key = get_next_key(current_key);
} catch (const py::key_error&) {
} catch (const py::key_error &) {
break;
}
}
@ -190,17 +172,13 @@ py::list BpfMap::values() const {
return result;
}
int BpfMap::get_type() const {
return bpf_map__type(map_);
}
int BpfMap::get_max_entries() const {
return bpf_map__max_entries(map_);
}
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) {
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)) {
@ -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>();
if (bytes_str.size() > buffer.size()) {
throw BpfException(
"Bytes size " + std::to_string(bytes_str.size()) +
" exceeds expected size " + std::to_string(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());
@ -245,6 +222,6 @@ py::object BpfMap::bytes_to_python(std::span<const uint8_t> data) {
std::memcpy(&value, data.data(), 8);
return py::cast(value);
} else {
return py::bytes(reinterpret_cast<const char*>(data.data()), data.size());
return py::bytes(reinterpret_cast<const char *>(data.data()), data.size());
}
}

View File

@ -1,15 +1,15 @@
#ifndef PYLIBBPF_BPF_MAP_H
#define PYLIBBPF_BPF_MAP_H
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstring>
#include <libbpf.h>
#include <pybind11/pybind11.h>
#include <vector>
#include <string>
#include <span>
#include <array>
#include <algorithm>
#include <cstring>
#include <cerrno>
#include <string>
#include <vector>
class BpfObject;
@ -23,8 +23,7 @@ private:
std::string map_name_;
__u32 key_size_, value_size_;
template<size_t StackSize = 64>
struct BufferManager {
template <size_t StackSize = 64> struct BufferManager {
std::array<uint8_t, StackSize> stack_buf;
std::vector<uint8_t> heap_buf;
@ -39,14 +38,15 @@ private:
};
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(const BpfMap&) = delete;
BpfMap& operator=(const BpfMap&) = delete;
BpfMap(BpfMap&&) noexcept = default;
BpfMap& operator=(BpfMap&&) noexcept = 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;
void update(const py::object &key, const py::object &value) const;
@ -64,8 +64,9 @@ public:
[[nodiscard]] int get_max_entries() const;
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);
};
#endif //PYLIBBPF_MAPS_H
#endif // PYLIBBPF_MAPS_H

View File

@ -1,12 +1,11 @@
#include "bpf_object.h"
#include "bpf_program.h"
#include "bpf_map.h"
#include "bpf_exception.h"
#include "bpf_map.h"
#include "bpf_program.h"
#include <cerrno>
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() {
// Clear caches first (order matters!)
@ -20,18 +19,16 @@ BpfObject::~BpfObject() {
}
}
BpfObject::BpfObject(BpfObject&& other) noexcept
: obj_(other.obj_),
object_path_(std::move(other.object_path_)),
loaded_(other.loaded_),
prog_cache_(std::move(other.prog_cache_)),
BpfObject::BpfObject(BpfObject &&other) noexcept
: obj_(other.obj_), object_path_(std::move(other.object_path_)),
loaded_(other.loaded_), prog_cache_(std::move(other.prog_cache_)),
maps_cache_(std::move(other.maps_cache_)) {
other.obj_ = nullptr;
other.loaded_ = false;
}
BpfObject& BpfObject::operator=(BpfObject&& other) noexcept {
BpfObject &BpfObject::operator=(BpfObject &&other) noexcept {
if (this != &other) {
prog_cache_.clear();
maps_cache_.clear();
@ -65,7 +62,8 @@ void BpfObject::load() {
}
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_);
obj_ = nullptr;
throw BpfException(error_msg);
@ -92,7 +90,8 @@ py::list BpfObject::get_program_names() const {
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) {
throw BpfException("bpf_program pointer is null");
}
@ -113,7 +112,7 @@ std::shared_ptr<BpfProgram> BpfObject::_get_or_create_program(struct bpf_program
return bpf_prog;
}
std::shared_ptr<BpfProgram> BpfObject::get_program(const std::string& name) {
std::shared_ptr<BpfProgram> BpfObject::get_program(const std::string &name) {
if (!loaded_) {
throw BpfException("BPF object not loaded");
}
@ -132,12 +131,14 @@ std::shared_ptr<BpfProgram> BpfObject::get_program(const std::string& name) {
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_) {
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) {
throw BpfException("Program '" + name + "' not found");
}
@ -147,7 +148,7 @@ struct bpf_program* BpfObject::find_program_by_name(const std::string& name) con
py::dict BpfObject::get_cached_programs() const {
py::dict programs;
for (const auto& [name, prog] : prog_cache_) {
for (const auto &[name, prog] : prog_cache_) {
programs[name] = prog;
}
return programs;
@ -193,7 +194,7 @@ py::list BpfObject::get_map_names() const {
return names;
}
std::shared_ptr<BpfMap> BpfObject::get_map(const std::string& name) {
std::shared_ptr<BpfMap> BpfObject::get_map(const std::string &name) {
if (!loaded_) {
throw BpfException("BPF object not loaded");
}
@ -233,7 +234,7 @@ std::shared_ptr<BpfMap> BpfObject::_get_or_create_map(struct bpf_map *map) {
return bpf_map;
}
struct bpf_map* BpfObject::find_map_by_name(const std::string& name) const {
struct bpf_map *BpfObject::find_map_by_name(const std::string &name) const {
if (!loaded_) {
throw BpfException("BPF object not loaded");
}
@ -248,7 +249,7 @@ struct bpf_map* BpfObject::find_map_by_name(const std::string& name) const {
py::dict BpfObject::get_cached_maps() const {
py::dict maps;
for (const auto& [name, map] : maps_cache_) {
for (const auto &[name, map] : maps_cache_) {
maps[name] = map;
}
return maps;

View File

@ -2,8 +2,8 @@
#define PYLIBBPF_BPF_OBJECT_H
#include <libbpf.h>
#include <pybind11/pybind11.h>
#include <memory>
#include <pybind11/pybind11.h>
#include <string>
#include <unordered_map>
#include <vector>
@ -26,7 +26,8 @@ private:
bool loaded_;
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<BpfMap> _get_or_create_map(struct bpf_map *map);
@ -36,10 +37,10 @@ public:
~BpfObject();
// Disable copy, allow move
BpfObject(const BpfObject&) = delete;
BpfObject& operator=(const BpfObject&) = delete;
BpfObject(BpfObject&&) noexcept;
BpfObject& operator=(BpfObject&&) noexcept;
BpfObject(const BpfObject &) = delete;
BpfObject &operator=(const BpfObject &) = delete;
BpfObject(BpfObject &&) noexcept;
BpfObject &operator=(BpfObject &&) noexcept;
/**
* Load the BPF object into the kernel.
@ -56,7 +57,7 @@ public:
* Get the underlying bpf_object pointer.
* Only for internal use by BpfProgram and BpfMap.
*/
[[nodiscard]] struct bpf_object* get_obj() const { return obj_; }
[[nodiscard]] struct bpf_object *get_obj() const { return obj_; }
/**
* Attach all programs in the object.
@ -65,14 +66,16 @@ public:
// Program access
[[nodiscard]] py::list get_program_names() const;
[[nodiscard]] std::shared_ptr<BpfProgram> get_program(const std::string& name);
[[nodiscard]] struct bpf_program* find_program_by_name(const std::string& name) const;
[[nodiscard]] std::shared_ptr<BpfProgram>
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;
// Map access
[[nodiscard]] py::list get_map_names() const;
[[nodiscard]] std::shared_ptr<BpfMap> get_map(const std::string& name);
[[nodiscard]] struct bpf_map* find_map_by_name(const std::string& name) const;
[[nodiscard]] std::shared_ptr<BpfMap> get_map(const std::string &name);
[[nodiscard]] struct bpf_map *find_map_by_name(const std::string &name) const;
[[nodiscard]] py::dict get_cached_maps() const;
};

View File

@ -1,7 +1,8 @@
#include "bpf_perf_buffer.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);
// 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);
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)) {
if (!lost_callback.is_none()) {

View File

@ -2,8 +2,8 @@
#define PYLIBBPF_BPF_PERF_BUFFER_H
#include <libbpf.h>
#include <pybind11/pybind11.h>
#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;
@ -14,11 +14,13 @@ private:
py::function lost_callback_;
// 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);
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();
int poll(int timeout_ms);

View File

@ -1,36 +1,32 @@
#include "bpf_program.h"
#include "bpf_exception.h"
#include <utility>
#include <cerrno>
#include <utility>
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),
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(!(parent->is_loaded()))
if (!(parent->is_loaded()))
throw BpfException("Parent BpfObject is not loaded");
if (!raw_prog)
throw BpfException("bpf_program pointer is null");
}
BpfProgram::~BpfProgram() {
detach();
}
BpfProgram::~BpfProgram() { 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_)) {
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 {
BpfProgram &BpfProgram::operator=(BpfProgram &&other) noexcept {
if (this != &other) {
detach();
@ -62,7 +58,8 @@ void BpfProgram::attach() {
link_ = bpf_program__attach(prog_);
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);
}
}

View File

@ -15,14 +15,16 @@ private:
std::string program_name_;
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(const BpfProgram&) = delete;
BpfProgram& operator=(const BpfProgram&) = delete;
BpfProgram(BpfProgram&&) noexcept;
BpfProgram& operator=(BpfProgram&&) noexcept;
BpfProgram(const BpfProgram &) = delete;
BpfProgram &operator=(const BpfProgram &) = delete;
BpfProgram(BpfProgram &&) noexcept;
BpfProgram &operator=(BpfProgram &&) noexcept;
bool attach();
bool detach();
@ -31,4 +33,4 @@ public:
[[nodiscard]] std::string get_name() const { return program_name_; }
};
#endif //PYLIBBPF_BPF_PROGRAM_H
#endif // PYLIBBPF_BPF_PROGRAM_H