Add basic class along with exception and attach

Signed-off-by: varun-r-mallya <varunrmallya@gmail.com>
This commit is contained in:
2025-09-21 14:27:07 +05:30
parent ecefff6b81
commit 3c8c6deb68
8 changed files with 156 additions and 53 deletions

View File

@ -1,9 +1,11 @@
cmake_minimum_required(VERSION 3.16) # 4.0 does not exist
cmake_minimum_required(VERSION 4.0)
project(pylibbpf)
# pybind11
include_directories(${CMAKE_SOURCE_DIR}/src)
add_subdirectory(pybind11)
pybind11_add_module(pylibbpf src/main.cpp)
pybind11_add_module(pylibbpf src/core/bpf_program.h src/core/bpf_exception.h
src/bindings/main.cpp src/core/bpf_program.cpp)
# --- libbpf build rules ---
set(LIBBPF_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libbpf/src)
@ -12,8 +14,8 @@ set(LIBBPF_A ${LIBBPF_BUILD_DIR}/libbpf.a)
add_custom_command(
OUTPUT ${LIBBPF_A}
COMMAND make BUILD_STATIC_ONLY=1 OBJDIR=${LIBBPF_BUILD_DIR} -C
${LIBBPF_SRC_DIR}
COMMAND make BUILD_STATIC_ONLY=1 OBJDIR=${LIBBPF_BUILD_DIR} CFLAGS="-fPIC"
LDFLAGS="-fPIC" -C ${LIBBPF_SRC_DIR}
WORKING_DIRECTORY ${LIBBPF_SRC_DIR}
COMMENT "Building libbpf.a"
VERBATIM)
@ -33,7 +35,7 @@ set_target_properties(
add_dependencies(libbpf_static libbpf_build)
# Link pybind11 module against libbpf
target_link_libraries(pylibbpf PRIVATE libbpf_static)
target_link_libraries(pylibbpf PRIVATE libbpf_static elf)
# Version info for Python extension
target_compile_definitions(pylibbpf

44
src/bindings/main.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <pybind11/pybind11.h>
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
extern "C" {
#include "libbpf.h"
}
#include "core/bpf_program.h"
#include "core/bpf_exception.h"
namespace py = pybind11;
PYBIND11_MODULE(pylibbpf, m) {
m.doc() = R"pbdoc(
Pylibbpf - libbpf bindings for Python
-----------------------
.. currentmodule:: pylibbpf
.. autosummary::
:toctree: _generate
BpfProgram
BpfException
)pbdoc";
// Register the custom exception
py::register_exception<BpfException>(m, "BpfException");
py::class_<BpfProgram>(m, "BpfProgram")
.def(py::init<const std::string&>())
.def(py::init<const std::string&, const std::string&>())
.def("load", &BpfProgram::load)
.def("attach", &BpfProgram::attach)
// .def("detach", &BpfProgram::detach)
.def("is_loaded", &BpfProgram::is_loaded)
.def("is_attached", &BpfProgram::is_attached);
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}

16
src/core/bpf_exception.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef PYLIBBPF_BPF_EXCEPTION_H
#define PYLIBBPF_BPF_EXCEPTION_H
#include <stdexcept>
#include <string>
class BpfException final : public std::runtime_error {
public:
explicit BpfException(const std::string& message)
: std::runtime_error(message) {}
explicit BpfException(const char* message)
: std::runtime_error(message) {}
};
#endif // PYLIBBPF_BPF_EXCEPTION_H

58
src/core/bpf_program.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "bpf_program.h"
#include "bpf_exception.h"
#include <filesystem>
BpfProgram::BpfProgram(const std::string& object_path, const std::string& program_name)
: obj_(nullptr), prog_(nullptr), link_(nullptr),
object_path_(object_path), program_name_(program_name) {
}
BpfProgram::~BpfProgram() {
//TODO: detach here as well
if (obj_) {
bpf_object__close(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_);
}
// 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 {
// Use the first program if no name specified
prog_ = bpf_object__next_program(obj_, nullptr);
if (!prog_) {
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() {
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;
}

29
src/core/bpf_program.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef PYLIBBPF_BPF_PROGRAM_H
#define PYLIBBPF_BPF_PROGRAM_H
#include "libbpf.h"
#include <pybind11/stl.h>
#include <string>
namespace py = pybind11;
class BpfProgram {
private:
struct bpf_object* obj_;
struct bpf_program* prog_;
struct bpf_link* link_;
std::string object_path_;
std::string program_name_;
public:
explicit BpfProgram(const std::string& object_path, const std::string& program_name = "");
~BpfProgram();
bool load();
bool attach();
bool is_loaded() const { return obj_ != nullptr; }
bool is_attached() const { return link_ != nullptr; }
};
#endif //PYLIBBPF_BPF_PROGRAM_H

View File

@ -1,46 +0,0 @@
#include <pybind11/pybind11.h>
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
extern "C" {
#include "libbpf.h"
}
int add(int i, int j) {
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(pylibbpf, m) {
m.doc() = R"pbdoc(
Pybind11 example plugin
-----------------------
.. currentmodule:: pylibbpf
.. autosummary::
:toctree: _generate
add
subtract
)pbdoc";
m.def("add", &add, R"pbdoc(
Add two numbers
Some other explanation about the add function.
)pbdoc");
m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
Subtract two numbers
Some other explanation about the subtract function.
)pbdoc");
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}

BIN
tests/execve2.o Normal file

Binary file not shown.

View File

@ -3,5 +3,5 @@ import pylibbpf as m
def test_main():
assert m.__version__ == "0.0.1"
assert m.add(1, 2) == 3
assert m.subtract(1, 2) == -1
prog = m.BpfProgram("tests/execve2.o")
print(prog)