mirror of
https://github.com/varun-r-mallya/pylibbpf.git
synced 2026-02-12 16:11:00 +00:00
Compare commits
34 Commits
771d8fef0a
...
v0.0.6
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c1071fac0 | |||
| f99de9981c | |||
| fa5d181e1a | |||
| 867f142a7f | |||
| 8cc8f4267a | |||
| ff427c2e61 | |||
| fb82b609f9 | |||
| 88716ce19a | |||
| 003495e833 | |||
| eebfe61ccc | |||
| ec5377ba14 | |||
| a3c3dbe141 | |||
| 3085e8155d | |||
| dd552de02c | |||
| 638533c19a | |||
| c9a152adc3 | |||
| 92e92f134a | |||
| b7aa0807c5 | |||
| ddbbce400e | |||
| c580aab1c4 | |||
| 470afc5174 | |||
| 495318f622 | |||
| bbb39898ab | |||
| 23cafa4d7b | |||
| 30021e8520 | |||
| 0e454bd7d7 | |||
| eda08b05d7 | |||
| 1eb7ed460e | |||
| 8babf3087b | |||
| cbfe6ae95e | |||
| 05d5bba4f7 | |||
| f7874137ad | |||
| b4d0a49883 | |||
| 874d567825 |
4
.github/workflows/pip.yml
vendored
4
.github/workflows/pip.yml
vendored
@ -45,7 +45,7 @@ jobs:
|
||||
run: pip install --verbose .[test]
|
||||
|
||||
- name: Test import
|
||||
run: python -c "import pylibbpf; print('Import successful')"
|
||||
run: python -I -c "import pylibbpf; print('Import successful')"
|
||||
|
||||
- name: Test
|
||||
run: python -m pytest -v
|
||||
run: python -I -m pytest -v
|
||||
|
||||
@ -10,16 +10,22 @@ include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||
add_subdirectory(pybind11)
|
||||
pybind11_add_module(
|
||||
pylibbpf
|
||||
# Core
|
||||
src/core/bpf_program.h
|
||||
src/core/bpf_exception.h
|
||||
src/core/bpf_map.h
|
||||
src/core/bpf_object.h
|
||||
src/core/bpf_perf_buffer.h
|
||||
src/bindings/main.cpp
|
||||
src/core/bpf_program.cpp
|
||||
src/core/bpf_map.cpp
|
||||
src/core/bpf_object.cpp
|
||||
src/core/bpf_perf_buffer.cpp)
|
||||
# Maps
|
||||
src/maps/perf_event_array.h
|
||||
src/maps/perf_event_array.cpp
|
||||
# Utils
|
||||
src/utils/struct_parser.h
|
||||
src/utils/struct_parser.cpp
|
||||
# Bindings
|
||||
src/bindings/main.cpp)
|
||||
|
||||
# --- libbpf build rules ---
|
||||
set(LIBBPF_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libbpf/src)
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
import time
|
||||
from ctypes import c_int32, c_int64, c_uint64, c_void_p
|
||||
|
||||
from pylibbpf import BpfMap
|
||||
from pythonbpf import BPF, bpf, bpfglobal, map, section
|
||||
from pythonbpf.maps import HashMap
|
||||
|
||||
from pylibbpf import BpfMap
|
||||
|
||||
|
||||
@bpf
|
||||
@map
|
||||
|
||||
46
pylibbpf/__init__.py
Normal file
46
pylibbpf/__init__.py
Normal file
@ -0,0 +1,46 @@
|
||||
import logging
|
||||
|
||||
from .ir_to_ctypes import convert_structs_to_ctypes, is_pythonbpf_structs
|
||||
from .pylibbpf import (
|
||||
BpfException,
|
||||
BpfMap,
|
||||
BpfProgram,
|
||||
PerfEventArray,
|
||||
StructParser,
|
||||
)
|
||||
from .pylibbpf import (
|
||||
BpfObject as _BpfObject, # C++ object (internal)
|
||||
)
|
||||
from .wrappers import BpfObjectWrapper
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BpfObject(BpfObjectWrapper):
|
||||
"""BpfObject with automatic struct conversion"""
|
||||
|
||||
def __init__(self, object_path: str, structs=None):
|
||||
"""Create a BPF object"""
|
||||
if structs is None:
|
||||
structs = {}
|
||||
elif is_pythonbpf_structs(structs):
|
||||
logger.info(f"Auto-converting {len(structs)} PythonBPF structs to ctypes")
|
||||
structs = convert_structs_to_ctypes(structs)
|
||||
|
||||
# Create C++ BpfObject with converted structs
|
||||
cpp_obj = _BpfObject(object_path, structs)
|
||||
|
||||
# Initialize wrapper
|
||||
super().__init__(cpp_obj)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"BpfObject",
|
||||
"BpfProgram",
|
||||
"BpfMap",
|
||||
"PerfEventArray",
|
||||
"StructParser",
|
||||
"BpfException",
|
||||
]
|
||||
|
||||
__version__ = "0.0.6"
|
||||
105
pylibbpf/ir_to_ctypes.py
Normal file
105
pylibbpf/ir_to_ctypes.py
Normal file
@ -0,0 +1,105 @@
|
||||
import ctypes
|
||||
import logging
|
||||
from typing import Dict, Type
|
||||
|
||||
from llvmlite import ir
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def ir_type_to_ctypes(ir_type):
|
||||
"""Convert LLVM IR type to ctypes type."""
|
||||
if isinstance(ir_type, ir.IntType):
|
||||
width = ir_type.width
|
||||
type_map = {
|
||||
8: ctypes.c_uint8,
|
||||
16: ctypes.c_uint16,
|
||||
32: ctypes.c_uint32,
|
||||
64: ctypes.c_uint64,
|
||||
}
|
||||
if width not in type_map:
|
||||
raise ValueError(f"Unsupported integer width: {width}")
|
||||
return type_map[width]
|
||||
|
||||
elif isinstance(ir_type, ir.ArrayType):
|
||||
count = ir_type.count
|
||||
element_type_ir = ir_type.element
|
||||
|
||||
if isinstance(element_type_ir, ir.IntType) and element_type_ir.width == 8:
|
||||
# Use c_char for string fields (will have .decode())
|
||||
return ctypes.c_char * count
|
||||
else:
|
||||
element_type = ir_type_to_ctypes(element_type_ir)
|
||||
return element_type * count
|
||||
elif isinstance(ir_type, ir.PointerType):
|
||||
return ctypes.c_void_p
|
||||
|
||||
else:
|
||||
raise TypeError(f"Unsupported IR type: {ir_type}")
|
||||
|
||||
|
||||
def _make_repr(struct_name: str, fields: list):
|
||||
"""Create a __repr__ function for a struct"""
|
||||
|
||||
def __repr__(self):
|
||||
field_strs = []
|
||||
for field_name, _ in fields:
|
||||
value = getattr(self, field_name)
|
||||
field_strs.append(f"{field_name}={value}")
|
||||
return f"<{struct_name} {' '.join(field_strs)}>"
|
||||
|
||||
return __repr__
|
||||
|
||||
|
||||
def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structure]]:
|
||||
"""Convert PythonBPF's structs_sym_tab to ctypes.Structure classes."""
|
||||
if not structs_sym_tab:
|
||||
return {}
|
||||
|
||||
ctypes_structs = {}
|
||||
|
||||
for struct_name, struct_type_obj in structs_sym_tab.items():
|
||||
try:
|
||||
fields = []
|
||||
for field_name, field_ir_type in struct_type_obj.fields.items():
|
||||
field_ctypes = ir_type_to_ctypes(field_ir_type)
|
||||
fields.append((field_name, field_ctypes))
|
||||
|
||||
repr_func = _make_repr(struct_name, fields)
|
||||
|
||||
struct_class = type(
|
||||
struct_name,
|
||||
(ctypes.Structure,),
|
||||
{
|
||||
"_fields_": fields,
|
||||
"__module__": "pylibbpf.ir_to_ctypes",
|
||||
"__doc__": f"Auto-generated ctypes structure for {struct_name}",
|
||||
"__repr__": repr_func,
|
||||
},
|
||||
)
|
||||
|
||||
ctypes_structs[struct_name] = struct_class
|
||||
# Pretty print field info
|
||||
field_info = ", ".join(f"{name}: {typ.__name__}" for name, typ in fields)
|
||||
logger.debug(f" {struct_name}({field_info})")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to convert struct '{struct_name}': {e}")
|
||||
raise
|
||||
logger.info(f"Converted struct '{struct_name}' to ctypes")
|
||||
return ctypes_structs
|
||||
|
||||
|
||||
def is_pythonbpf_structs(structs) -> bool:
|
||||
"""Check if structs dict is from PythonBPF."""
|
||||
if not isinstance(structs, dict) or not structs:
|
||||
return False
|
||||
|
||||
first_value = next(iter(structs.values()))
|
||||
return (
|
||||
hasattr(first_value, "ir_type")
|
||||
and hasattr(first_value, "fields")
|
||||
and hasattr(first_value, "size")
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["convert_structs_to_ctypes", "is_pythonbpf_structs"]
|
||||
0
pylibbpf/py.typed
Normal file
0
pylibbpf/py.typed
Normal file
77
pylibbpf/wrappers.py
Normal file
77
pylibbpf/wrappers.py
Normal file
@ -0,0 +1,77 @@
|
||||
from typing import Callable, Optional
|
||||
|
||||
|
||||
class PerfEventArrayHelper:
|
||||
"""Fluent wrapper for PERF_EVENT_ARRAY maps."""
|
||||
|
||||
def __init__(self, bpf_map):
|
||||
self._map = bpf_map
|
||||
self._perf_buffer = None
|
||||
|
||||
def open_perf_buffer(
|
||||
self,
|
||||
callback: Callable,
|
||||
struct_name: str = "",
|
||||
page_cnt: int = 8,
|
||||
lost_callback: Optional[Callable] = None,
|
||||
):
|
||||
"""Open perf buffer with auto-deserialization."""
|
||||
from .pylibbpf import PerfEventArray
|
||||
|
||||
if struct_name:
|
||||
self._perf_buffer = PerfEventArray(
|
||||
self._map,
|
||||
page_cnt,
|
||||
callback,
|
||||
struct_name,
|
||||
lost_callback or (lambda cpu, cnt: None),
|
||||
)
|
||||
else:
|
||||
self._perf_buffer = PerfEventArray(
|
||||
self._map, page_cnt, callback, lost_callback or (lambda cpu, cnt: None)
|
||||
)
|
||||
|
||||
return self
|
||||
|
||||
def poll(self, timeout_ms: int = -1) -> int:
|
||||
if not self._perf_buffer:
|
||||
raise RuntimeError("Call open_perf_buffer() first")
|
||||
return self._perf_buffer.poll(timeout_ms)
|
||||
|
||||
def consume(self) -> int:
|
||||
if not self._perf_buffer:
|
||||
raise RuntimeError("Call open_perf_buffer() first")
|
||||
return self._perf_buffer.consume()
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._map, name)
|
||||
|
||||
|
||||
class BpfObjectWrapper:
|
||||
"""Smart wrapper that returns map-specific helpers."""
|
||||
|
||||
BPF_MAP_TYPE_PERF_EVENT_ARRAY = 4
|
||||
BPF_MAP_TYPE_RINGBUF = 27
|
||||
|
||||
def __init__(self, bpf_object):
|
||||
self._obj = bpf_object
|
||||
self._map_helpers = {}
|
||||
|
||||
def __getitem__(self, name: str):
|
||||
"""Return appropriate helper based on map type."""
|
||||
if name in self._map_helpers:
|
||||
return self._map_helpers[name]
|
||||
|
||||
map_obj = self._obj[name]
|
||||
map_type = map_obj.get_type()
|
||||
|
||||
if map_type == self.BPF_MAP_TYPE_PERF_EVENT_ARRAY:
|
||||
helper = PerfEventArrayHelper(map_obj)
|
||||
else:
|
||||
helper = map_obj
|
||||
|
||||
self._map_helpers[name] = helper
|
||||
return helper
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._obj, name)
|
||||
@ -4,12 +4,13 @@ requires = [
|
||||
"wheel",
|
||||
"ninja",
|
||||
"cmake>=4.0",
|
||||
"pybind11>=2.10",
|
||||
]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "pylibbpf"
|
||||
version = "0.0.5"
|
||||
version = "0.0.6"
|
||||
description = "Python Bindings for Libbpf"
|
||||
authors = [
|
||||
{ name = "r41k0u", email = "pragyanshchaturvedi18@gmail.com" },
|
||||
@ -32,14 +33,17 @@ classifiers = [
|
||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||
"Topic :: System :: Operating System Kernels :: Linux",
|
||||
]
|
||||
dependencies = [
|
||||
"llvmlite>=0.40.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
test = ["pytest>=6.0"]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://github.com/varun-r-mallya/pylibbpf"
|
||||
Repository = "https://github.com/varun-r-mallya/pylibbpf"
|
||||
Issues = "https://github.com/varun-r-mallya/pylibbpf/issues"
|
||||
Homepage = "https://github.com/pythonbpf/pylibbpf"
|
||||
Repository = "https://github.com/pythonbpf/pylibbpf"
|
||||
Issues = "https://github.com/pythonbpf/pylibbpf/issues"
|
||||
|
||||
[tool.mypy]
|
||||
files = "setup.py"
|
||||
|
||||
19
setup.py
19
setup.py
@ -3,7 +3,7 @@ import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from setuptools import Extension, setup
|
||||
from setuptools import Extension, find_packages, setup
|
||||
from setuptools.command.build_ext import build_ext
|
||||
|
||||
# Convert distutils Windows platform specifiers to CMake -A arguments
|
||||
@ -129,8 +129,11 @@ setup(
|
||||
description="Python Bindings for Libbpf",
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
url="https://github.com/varun-r-mallya/pylibbpf",
|
||||
ext_modules=[CMakeExtension("pylibbpf")],
|
||||
url="https://github.com/pythonbpf/pylibbpf",
|
||||
packages=find_packages(where="."),
|
||||
package_dir={"": "."},
|
||||
py_modules=[], # Empty since we use packages
|
||||
ext_modules=[CMakeExtension("pylibbpf.pylibbpf")],
|
||||
cmdclass={"build_ext": CMakeBuild},
|
||||
zip_safe=False,
|
||||
classifiers=[
|
||||
@ -147,6 +150,16 @@ setup(
|
||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||
"Topic :: System :: Operating System Kernels :: Linux",
|
||||
],
|
||||
install_requires=[
|
||||
"llvmlite>=0.40.0", # Required for struct conversion
|
||||
],
|
||||
extras_require={"test": ["pytest>=6.0"]},
|
||||
python_requires=">=3.8",
|
||||
package_data={
|
||||
"pylibbpf": [
|
||||
"*.py",
|
||||
"py.typed", # For type hints
|
||||
],
|
||||
},
|
||||
include_package_data=True,
|
||||
)
|
||||
|
||||
@ -2,20 +2,17 @@
|
||||
#define STRINGIFY(x) #x
|
||||
#define MACRO_STRINGIFY(x) STRINGIFY(x)
|
||||
|
||||
extern "C" {
|
||||
#include <libbpf.h>
|
||||
}
|
||||
|
||||
#include "core/bpf_object.h"
|
||||
#include "core/bpf_program.h"
|
||||
#include "core/bpf_exception.h"
|
||||
#include "core/bpf_map.h"
|
||||
#include "core/bpf_perf_buffer.h"
|
||||
#include "core/bpf_object.h"
|
||||
#include "core/bpf_program.h"
|
||||
#include "maps/perf_event_array.h"
|
||||
#include "utils/struct_parser.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
PYBIND11_MODULE(pylibbpf, m) {
|
||||
m.doc() = R"pbdoc(
|
||||
m.doc() = R"pbdoc(
|
||||
Pylibbpf - libbpf bindings for Python
|
||||
-----------------------
|
||||
|
||||
@ -29,56 +26,73 @@ PYBIND11_MODULE(pylibbpf, m) {
|
||||
BpfException
|
||||
)pbdoc";
|
||||
|
||||
// Register the custom exception
|
||||
py::register_exception<BpfException>(m, "BpfException");
|
||||
// Register the custom exception
|
||||
py::register_exception<BpfException>(m, "BpfException");
|
||||
|
||||
// BpfObject
|
||||
py::class_<BpfObject, std::shared_ptr<BpfObject>>(m, "BpfObject")
|
||||
.def(py::init<std::string>(), py::arg("object_path"))
|
||||
.def("load", &BpfObject::load)
|
||||
.def("is_loaded", &BpfObject::is_loaded)
|
||||
.def("get_program_names", &BpfObject::get_program_names)
|
||||
.def("get_program", &BpfObject::get_program, py::arg("name"))
|
||||
.def("attach_all", &BpfObject::attach_all)
|
||||
.def("get_map_names", &BpfObject::get_map_names)
|
||||
.def("get_map", &BpfObject::get_map, py::arg("name"));
|
||||
// BpfObject
|
||||
py::class_<BpfObject, std::shared_ptr<BpfObject>>(m, "BpfObject")
|
||||
.def(py::init<std::string, py::dict>(), py::arg("object_path"),
|
||||
py::arg("structs") = py::dict())
|
||||
.def("load", &BpfObject::load)
|
||||
.def("is_loaded", &BpfObject::is_loaded)
|
||||
.def("get_program_names", &BpfObject::get_program_names)
|
||||
.def("get_program", &BpfObject::get_program, py::arg("name"))
|
||||
.def("attach_all", &BpfObject::attach_all)
|
||||
.def("get_map_names", &BpfObject::get_map_names)
|
||||
.def("get_map", &BpfObject::get_map, py::arg("name"))
|
||||
.def("get_struct_defs", &BpfObject::get_struct_defs)
|
||||
.def("__getitem__", &BpfObject::get_map, py::arg("name"));
|
||||
|
||||
// BpfProgram
|
||||
py::class_<BpfProgram, std::shared_ptr<BpfProgram>>(m, "BpfProgram")
|
||||
.def("attach", &BpfProgram::attach)
|
||||
.def("detach", &BpfProgram::detach)
|
||||
.def("is_attached", &BpfProgram::is_attached)
|
||||
.def("get_name", &BpfProgram::get_name);
|
||||
// BpfProgram
|
||||
py::class_<BpfProgram, std::shared_ptr<BpfProgram>>(m, "BpfProgram")
|
||||
.def("attach", &BpfProgram::attach)
|
||||
.def("detach", &BpfProgram::detach)
|
||||
.def("is_attached", &BpfProgram::is_attached)
|
||||
.def("get_name", &BpfProgram::get_name);
|
||||
|
||||
// BpfMap
|
||||
py::class_<BpfMap, std::shared_ptr<BpfMap>>(m, "BpfMap")
|
||||
.def("lookup", &BpfMap::lookup, py::arg("key"))
|
||||
.def("update", &BpfMap::update, py::arg("key"), py::arg("value"))
|
||||
.def("delete_elem", &BpfMap::delete_elem, py::arg("key"))
|
||||
.def("get_next_key", &BpfMap::get_next_key, py::arg("key") = py::none())
|
||||
.def("items", &BpfMap::items)
|
||||
.def("keys", &BpfMap::keys)
|
||||
.def("values", &BpfMap::values)
|
||||
.def("get_name", &BpfMap::get_name)
|
||||
.def("get_fd", &BpfMap::get_fd)
|
||||
.def("get_type", &BpfMap::get_type)
|
||||
.def("get_key_size", &BpfMap::get_key_size)
|
||||
.def("get_value_size", &BpfMap::get_value_size)
|
||||
.def("get_max_entries", &BpfMap::get_max_entries);
|
||||
// BpfMap
|
||||
py::class_<BpfMap, std::shared_ptr<BpfMap>>(m, "BpfMap")
|
||||
.def("lookup", &BpfMap::lookup, py::arg("key"))
|
||||
.def("update", &BpfMap::update, py::arg("key"), py::arg("value"))
|
||||
.def("delete_elem", &BpfMap::delete_elem, py::arg("key"))
|
||||
.def("get_next_key", &BpfMap::get_next_key, py::arg("key") = py::none())
|
||||
.def("items", &BpfMap::items)
|
||||
.def("keys", &BpfMap::keys)
|
||||
.def("values", &BpfMap::values)
|
||||
.def("get_name", &BpfMap::get_name)
|
||||
.def("get_fd", &BpfMap::get_fd)
|
||||
.def("get_type", &BpfMap::get_type)
|
||||
.def("get_key_size", &BpfMap::get_key_size)
|
||||
.def("get_value_size", &BpfMap::get_value_size)
|
||||
.def("get_max_entries", &BpfMap::get_max_entries)
|
||||
.def("__getitem__", &BpfMap::lookup, py::arg("key"))
|
||||
.def("__setitem__", &BpfMap::update, py::arg("key"), py::arg("value"))
|
||||
.def("__delitem__", &BpfMap::delete_elem, py::arg("key"));
|
||||
|
||||
py::class_<BpfPerfBuffer>(m, "BpfPerfBuffer")
|
||||
.def(py::init<int, int, py::function, py::object>(),
|
||||
py::arg("map_fd"),
|
||||
py::arg("page_cnt") = 8,
|
||||
py::arg("callback"),
|
||||
py::arg("lost_callback") = py::none())
|
||||
.def("poll", &BpfPerfBuffer::poll, py::arg("timeout_ms") = -1)
|
||||
.def("consume", &BpfPerfBuffer::consume);
|
||||
// StructParser
|
||||
py::class_<StructParser>(m, "StructParser")
|
||||
.def(py::init<py::dict>(), py::arg("structs"))
|
||||
.def("parse", &StructParser::parse, py::arg("struct_name"),
|
||||
py::arg("data"))
|
||||
.def("has_struct", &StructParser::has_struct, py::arg("struct_name"));
|
||||
|
||||
// PerfEventArray
|
||||
py::class_<PerfEventArray, std::shared_ptr<PerfEventArray>>(m,
|
||||
"PerfEventArray")
|
||||
.def(py::init<std::shared_ptr<BpfMap>, int, py::function, py::object>(),
|
||||
py::arg("map"), py::arg("page_cnt"), py::arg("callback"),
|
||||
py::arg("lost_callback") = py::none())
|
||||
.def(py::init<std::shared_ptr<BpfMap>, int, py::function, std::string,
|
||||
py::object>(),
|
||||
py::arg("map"), py::arg("page_cnt"), py::arg("callback"),
|
||||
py::arg("struct_name"), py::arg("lost_callback") = py::none())
|
||||
.def("poll", &PerfEventArray::poll, py::arg("timeout_ms"))
|
||||
.def("consume", &PerfEventArray::consume)
|
||||
.def("get_map", &PerfEventArray::get_map);
|
||||
|
||||
#ifdef VERSION_INFO
|
||||
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
|
||||
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
|
||||
#else
|
||||
m.attr("__version__") = "dev";
|
||||
m.attr("__version__") = "dev";
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
#include "bpf_map.h"
|
||||
#include "bpf_exception.h"
|
||||
#include "bpf_object.h"
|
||||
#include "core/bpf_map.h"
|
||||
#include "core/bpf_exception.h"
|
||||
#include "core/bpf_object.h"
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
|
||||
BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
|
||||
const std::string &map_name)
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
#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 <span>
|
||||
@ -15,7 +12,7 @@ class BpfObject;
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
class BpfMap {
|
||||
class BpfMap : public std::enable_shared_from_this<BpfMap> {
|
||||
private:
|
||||
std::weak_ptr<BpfObject> parent_obj_;
|
||||
struct bpf_map *map_;
|
||||
@ -62,6 +59,9 @@ public:
|
||||
[[nodiscard]] int get_key_size() const { return key_size_; };
|
||||
[[nodiscard]] int get_value_size() const { return value_size_; };
|
||||
[[nodiscard]] int get_max_entries() const;
|
||||
[[nodiscard]] std::shared_ptr<BpfObject> get_parent() const {
|
||||
return parent_obj_.lock();
|
||||
}
|
||||
|
||||
private:
|
||||
static void python_to_bytes_inplace(const py::object &obj,
|
||||
@ -69,4 +69,4 @@ private:
|
||||
static py::object bytes_to_python(std::span<const uint8_t> data);
|
||||
};
|
||||
|
||||
#endif // PYLIBBPF_MAPS_H
|
||||
#endif // PYLIBBPF_BPF_MAP_H
|
||||
|
||||
@ -1,11 +1,15 @@
|
||||
#include "bpf_object.h"
|
||||
#include "bpf_exception.h"
|
||||
#include "bpf_map.h"
|
||||
#include "bpf_program.h"
|
||||
#include "core/bpf_object.h"
|
||||
#include "core/bpf_exception.h"
|
||||
#include "core/bpf_map.h"
|
||||
#include "core/bpf_program.h"
|
||||
#include "utils/struct_parser.h"
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
BpfObject::BpfObject(std::string object_path)
|
||||
: obj_(nullptr), object_path_(std::move(object_path)), loaded_(false) {}
|
||||
BpfObject::BpfObject(std::string object_path, py::dict structs)
|
||||
: obj_(nullptr), object_path_(std::move(object_path)), loaded_(false),
|
||||
struct_defs_(structs), struct_parser_(nullptr) {}
|
||||
|
||||
BpfObject::~BpfObject() {
|
||||
// Clear caches first (order matters!)
|
||||
@ -20,9 +24,13 @@ 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_)),
|
||||
maps_cache_(std::move(other.maps_cache_)) {
|
||||
: obj_(std::exchange(other.obj_, nullptr)),
|
||||
object_path_(std::move(other.object_path_)),
|
||||
loaded_(std::exchange(other.loaded_, false)),
|
||||
maps_cache_(std::move(other.maps_cache_)),
|
||||
prog_cache_(std::move(other.prog_cache_)),
|
||||
struct_defs_(std::move(other.struct_defs_)),
|
||||
struct_parser_(std::move(other.struct_parser_)) {
|
||||
|
||||
other.obj_ = nullptr;
|
||||
other.loaded_ = false;
|
||||
@ -36,14 +44,13 @@ BpfObject &BpfObject::operator=(BpfObject &&other) noexcept {
|
||||
bpf_object__close(obj_);
|
||||
}
|
||||
|
||||
obj_ = other.obj_;
|
||||
obj_ = std::exchange(other.obj_, nullptr);
|
||||
object_path_ = std::move(other.object_path_);
|
||||
loaded_ = other.loaded_;
|
||||
prog_cache_ = std::move(other.prog_cache_);
|
||||
loaded_ = std::exchange(other.loaded_, false);
|
||||
maps_cache_ = std::move(other.maps_cache_);
|
||||
|
||||
other.obj_ = nullptr;
|
||||
other.loaded_ = false;
|
||||
prog_cache_ = std::move(other.prog_cache_);
|
||||
struct_defs_ = std::move(other.struct_defs_);
|
||||
struct_parser_ = std::move(other.struct_parser_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -255,3 +262,11 @@ py::dict BpfObject::get_cached_maps() const {
|
||||
}
|
||||
return maps;
|
||||
}
|
||||
|
||||
std::shared_ptr<StructParser> BpfObject::get_struct_parser() const {
|
||||
if (!struct_parser_ && !struct_defs_.empty()) {
|
||||
// Create parser on first access
|
||||
struct_parser_ = std::make_shared<StructParser>(struct_defs_);
|
||||
}
|
||||
return struct_parser_;
|
||||
}
|
||||
|
||||
@ -6,12 +6,12 @@
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
class BpfProgram;
|
||||
class BpfMap;
|
||||
class StructParser;
|
||||
|
||||
/**
|
||||
* BpfObject - Represents a loaded BPF object file.
|
||||
@ -28,12 +28,14 @@ private:
|
||||
mutable std::unordered_map<std::string, std::shared_ptr<BpfMap>> maps_cache_;
|
||||
mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>>
|
||||
prog_cache_;
|
||||
py::dict struct_defs_;
|
||||
mutable std::shared_ptr<StructParser> struct_parser_;
|
||||
|
||||
std::shared_ptr<BpfProgram> _get_or_create_program(struct bpf_program *prog);
|
||||
std::shared_ptr<BpfMap> _get_or_create_map(struct bpf_map *map);
|
||||
|
||||
public:
|
||||
explicit BpfObject(std::string object_path);
|
||||
explicit BpfObject(std::string object_path, py::dict structs = py::dict());
|
||||
~BpfObject();
|
||||
|
||||
// Disable copy, allow move
|
||||
@ -77,6 +79,10 @@ public:
|
||||
[[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;
|
||||
|
||||
// Struct parsing
|
||||
[[nodiscard]] py::dict get_struct_defs() const { return struct_defs_; }
|
||||
[[nodiscard]] std::shared_ptr<StructParser> get_struct_parser() const;
|
||||
};
|
||||
|
||||
#endif // PYLIBBPF_BPF_OBJECT_H
|
||||
|
||||
@ -1,80 +0,0 @@
|
||||
#include "bpf_perf_buffer.h"
|
||||
#include "bpf_exception.h"
|
||||
|
||||
BpfPerfBuffer::BpfPerfBuffer(int map_fd, int page_cnt, py::function callback,
|
||||
py::object lost_callback)
|
||||
: pb_(nullptr), callback_(std::move(callback)),
|
||||
lost_callback_(lost_callback) {
|
||||
|
||||
if (page_cnt <= 0 || (page_cnt & (page_cnt - 1)) != 0) {
|
||||
throw BpfException("page_cnt must be a positive power of 2");
|
||||
}
|
||||
|
||||
struct perf_buffer_opts pb_opts = {};
|
||||
pb_opts.sz = sizeof(pb_opts); // Required for forward compatibility
|
||||
|
||||
pb_ = perf_buffer__new(
|
||||
map_fd, page_cnt,
|
||||
sample_callback_wrapper, // sample_cb
|
||||
lost_callback.is_none() ? nullptr : lost_callback_wrapper, // lost_cb
|
||||
this, // ctx
|
||||
&pb_opts // opts
|
||||
);
|
||||
|
||||
if (!pb_) {
|
||||
throw BpfException("Failed to create perf buffer: " +
|
||||
std::string(std::strerror(errno)));
|
||||
}
|
||||
}
|
||||
|
||||
BpfPerfBuffer::~BpfPerfBuffer() {
|
||||
if (pb_) {
|
||||
perf_buffer__free(pb_);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
py::gil_scoped_acquire acquire;
|
||||
|
||||
try {
|
||||
// Convert data to Python bytes
|
||||
py::bytes py_data(static_cast<const char *>(data), size);
|
||||
|
||||
// Call Python callback: callback(cpu, data, size)
|
||||
self->callback_(cpu, py_data, size);
|
||||
} catch (const py::error_already_set &e) {
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
return;
|
||||
}
|
||||
|
||||
py::gil_scoped_acquire acquire;
|
||||
|
||||
try {
|
||||
self->lost_callback_(cpu, cnt);
|
||||
} catch (const py::error_already_set &e) {
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
|
||||
int BpfPerfBuffer::poll(int timeout_ms) {
|
||||
// Release GIL during blocking poll
|
||||
py::gil_scoped_release release;
|
||||
return perf_buffer__poll(pb_, timeout_ms);
|
||||
}
|
||||
|
||||
int BpfPerfBuffer::consume() {
|
||||
py::gil_scoped_release release;
|
||||
return perf_buffer__consume(pb_);
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
#ifndef PYLIBBPF_BPF_PERF_BUFFER_H
|
||||
#define PYLIBBPF_BPF_PERF_BUFFER_H
|
||||
|
||||
#include <libbpf.h>
|
||||
#include <pybind11/functional.h>
|
||||
#include <pybind11/pybind11.h>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
class BpfPerfBuffer {
|
||||
private:
|
||||
struct perf_buffer *pb_;
|
||||
py::function callback_;
|
||||
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 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 = py::none());
|
||||
~BpfPerfBuffer();
|
||||
|
||||
int poll(int timeout_ms);
|
||||
int consume();
|
||||
[[nodiscard]] int fd() const;
|
||||
};
|
||||
|
||||
#endif // PYLIBBPF_BPF_PERF_BUFFER_H
|
||||
@ -1,7 +1,8 @@
|
||||
#include "bpf_program.h"
|
||||
#include "bpf_exception.h"
|
||||
#include "bpf_object.h"
|
||||
#include "core/bpf_program.h"
|
||||
#include "core/bpf_exception.h"
|
||||
#include "core/bpf_object.h"
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
|
||||
BpfProgram::BpfProgram(std::shared_ptr<BpfObject> parent,
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#ifndef PYLIBBPF_BPF_PROGRAM_H
|
||||
#define PYLIBBPF_BPF_PROGRAM_H
|
||||
|
||||
#include <cstring>
|
||||
#include <libbpf.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
117
src/maps/perf_event_array.cpp
Normal file
117
src/maps/perf_event_array.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
#include "maps/perf_event_array.h"
|
||||
#include "core/bpf_exception.h"
|
||||
#include "core/bpf_map.h"
|
||||
#include "core/bpf_object.h"
|
||||
#include "utils/struct_parser.h"
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
|
||||
PerfEventArray::PerfEventArray(std::shared_ptr<BpfMap> map, int page_cnt,
|
||||
py::function callback, py::object lost_callback)
|
||||
: map_(map), pb_(nullptr), callback_(std::move(callback)),
|
||||
lost_callback_(std::move(lost_callback)) {
|
||||
|
||||
if (map->get_type() != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
|
||||
throw BpfException("Map '" + map->get_name() +
|
||||
"' is not a PERF_EVENT_ARRAY");
|
||||
}
|
||||
|
||||
if (page_cnt <= 0 || (page_cnt & (page_cnt - 1)) != 0) {
|
||||
throw BpfException("page_cnt must be a positive power of 2");
|
||||
}
|
||||
|
||||
struct perf_buffer_opts pb_opts = {};
|
||||
pb_opts.sz = sizeof(pb_opts); // Required for forward compatibility
|
||||
|
||||
pb_ = perf_buffer__new(
|
||||
map->get_fd(), page_cnt,
|
||||
sample_callback_wrapper, // sample_cb
|
||||
lost_callback.is_none() ? nullptr : lost_callback_wrapper, // lost_cb
|
||||
this, // ctx
|
||||
&pb_opts // opts
|
||||
);
|
||||
|
||||
if (!pb_) {
|
||||
throw BpfException("Failed to create perf buffer: " +
|
||||
std::string(std::strerror(errno)));
|
||||
}
|
||||
}
|
||||
|
||||
PerfEventArray::PerfEventArray(std::shared_ptr<BpfMap> map, int page_cnt,
|
||||
py::function callback,
|
||||
const std::string &struct_name,
|
||||
py::object lost_callback)
|
||||
: PerfEventArray(map, page_cnt, callback, lost_callback) {
|
||||
|
||||
auto parent = map->get_parent();
|
||||
if (!parent) {
|
||||
throw BpfException("Parent BpfObject has been destroyed");
|
||||
}
|
||||
|
||||
parser_ = parent->get_struct_parser();
|
||||
struct_name_ = struct_name;
|
||||
|
||||
if (!parser_) {
|
||||
throw BpfException("No struct definitions available");
|
||||
}
|
||||
}
|
||||
|
||||
PerfEventArray::~PerfEventArray() {
|
||||
if (pb_) {
|
||||
perf_buffer__free(pb_);
|
||||
}
|
||||
}
|
||||
|
||||
void PerfEventArray::sample_callback_wrapper(void *ctx, int cpu, void *data,
|
||||
unsigned int size) {
|
||||
auto *self = static_cast<PerfEventArray *>(ctx);
|
||||
|
||||
// Acquire GIL for Python calls
|
||||
py::gil_scoped_acquire acquire;
|
||||
|
||||
try {
|
||||
// Convert data to Python bytes
|
||||
py::bytes py_data(static_cast<const char *>(data), size);
|
||||
|
||||
if (self->parser_ && !self->struct_name_.empty()) {
|
||||
py::object event = self->parser_->parse(self->struct_name_, py_data);
|
||||
self->callback_(cpu, event);
|
||||
} else {
|
||||
self->callback_(cpu, py_data);
|
||||
}
|
||||
|
||||
} catch (const py::error_already_set &e) {
|
||||
PyErr_Print();
|
||||
} catch (const std::exception &e) {
|
||||
py::print("C++ error in perf callback:", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void PerfEventArray::lost_callback_wrapper(void *ctx, int cpu,
|
||||
unsigned long long cnt) {
|
||||
auto *self = static_cast<PerfEventArray *>(ctx);
|
||||
|
||||
py::gil_scoped_acquire acquire;
|
||||
|
||||
try {
|
||||
if (!self->lost_callback_.is_none()) {
|
||||
py::function lost_fn = py::cast<py::function>(self->lost_callback_);
|
||||
lost_fn(cpu, cnt);
|
||||
} else {
|
||||
py::print("Lost", cnt, "events on CPU", cpu);
|
||||
}
|
||||
} catch (const py::error_already_set &e) {
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
|
||||
int PerfEventArray::poll(int timeout_ms) {
|
||||
// Release GIL during blocking poll
|
||||
py::gil_scoped_release release;
|
||||
return perf_buffer__poll(pb_, timeout_ms);
|
||||
}
|
||||
|
||||
int PerfEventArray::consume() {
|
||||
py::gil_scoped_release release;
|
||||
return perf_buffer__consume(pb_);
|
||||
}
|
||||
46
src/maps/perf_event_array.h
Normal file
46
src/maps/perf_event_array.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef PYLIBBPF_PERF_EVENT_ARRAY_H
|
||||
#define PYLIBBPF_PERF_EVENT_ARRAY_H
|
||||
|
||||
#include <libbpf.h>
|
||||
#include <memory>
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <string>
|
||||
|
||||
class StructParser;
|
||||
class BpfMap;
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
class PerfEventArray {
|
||||
private:
|
||||
std::shared_ptr<BpfMap> map_;
|
||||
struct perf_buffer *pb_;
|
||||
py::function callback_;
|
||||
py::object lost_callback_;
|
||||
|
||||
std::shared_ptr<StructParser> parser_;
|
||||
std::string struct_name_;
|
||||
|
||||
// Static callback wrappers for C API
|
||||
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:
|
||||
PerfEventArray(std::shared_ptr<BpfMap> map, int page_cnt,
|
||||
py::function callback, py::object lost_callback = py::none());
|
||||
PerfEventArray(std::shared_ptr<BpfMap> map, int page_cnt,
|
||||
py::function callback, const std::string &struct_name,
|
||||
py::object lost_callback = py::none());
|
||||
~PerfEventArray();
|
||||
|
||||
PerfEventArray(const PerfEventArray &) = delete;
|
||||
PerfEventArray &operator=(const PerfEventArray &) = delete;
|
||||
|
||||
int poll(int timeout_ms);
|
||||
int consume();
|
||||
|
||||
[[nodiscard]] std::shared_ptr<BpfMap> get_map() const { return map_; }
|
||||
};
|
||||
|
||||
#endif // PYLIBBPF_PERF_EVENT_ARRAY_H
|
||||
25
src/utils/struct_parser.cpp
Normal file
25
src/utils/struct_parser.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "struct_parser.h"
|
||||
#include "core/bpf_exception.h"
|
||||
|
||||
StructParser::StructParser(py::dict structs) {
|
||||
for (auto item : structs) {
|
||||
std::string name = py::str(item.first);
|
||||
struct_types_[name] = py::reinterpret_borrow<py::object>(item.second);
|
||||
}
|
||||
}
|
||||
|
||||
py::object StructParser::parse(const std::string &struct_name, py::bytes data) {
|
||||
auto it = struct_types_.find(struct_name);
|
||||
if (it == struct_types_.end()) {
|
||||
throw BpfException("Unknown struct: " + struct_name);
|
||||
}
|
||||
|
||||
py::object struct_type = it->second;
|
||||
|
||||
// Use ctypes.from_buffer_copy() to create struct from bytes
|
||||
return struct_type.attr("from_buffer_copy")(data);
|
||||
}
|
||||
|
||||
bool StructParser::has_struct(const std::string &struct_name) const {
|
||||
return struct_types_.find(struct_name) != struct_types_.end();
|
||||
}
|
||||
20
src/utils/struct_parser.h
Normal file
20
src/utils/struct_parser.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef PYLIBBPF_STRUCT_PARSER_H
|
||||
#define PYLIBBPF_STRUCT_PARSER_H
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
class StructParser {
|
||||
private:
|
||||
std::unordered_map<std::string, py::object> struct_types_;
|
||||
|
||||
public:
|
||||
explicit StructParser(py::dict structs);
|
||||
py::object parse(const std::string &struct_name, py::bytes data);
|
||||
bool has_struct(const std::string &struct_name) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -2,6 +2,6 @@ import pylibbpf as m
|
||||
|
||||
|
||||
def test_main():
|
||||
assert m.__version__ == "0.0.5"
|
||||
prog = m.BpfObject("tests/execve2.o")
|
||||
assert m.__version__ == "0.0.6"
|
||||
prog = m.BpfObject("tests/execve2.o", structs={})
|
||||
print(prog)
|
||||
|
||||
Reference in New Issue
Block a user