mirror of
https://github.com/varun-r-mallya/pylibbpf.git
synced 2026-02-12 16:11:00 +00:00
Compare commits
27 Commits
v0.0.6
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
| 194f336964 | |||
| ecd94057b7 | |||
| 68f2bc4b9b | |||
| ccce772b51 | |||
| 8ac2d67a76 | |||
| d4202cdc88 | |||
| 3335e0471b | |||
| 13554e9da9 | |||
| 9a54aedb37 | |||
| 270f4337d3 | |||
| ffc5a9f15f | |||
| 503e6da987 | |||
| 8585f170cb | |||
| 0780615fc5 | |||
| c26617d64f | |||
| 303fe4e6c8 | |||
| 9c18f5967d | |||
| 434f145f9b | |||
| b3e9410e0d | |||
| fd739e87c1 | |||
| 87c698e940 | |||
| 298df7ede6 | |||
| b48f6a8a97 | |||
| 6e4dc0f5aa | |||
| a51bed14ca | |||
| 3a85a6446e | |||
| 6bc378defd |
2
.github/workflows/format.yml
vendored
2
.github/workflows/format.yml
vendored
@ -15,7 +15,7 @@ jobs:
|
|||||||
name: Format
|
name: Format
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
- uses: actions/setup-python@v6
|
- uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: "3.x"
|
python-version: "3.x"
|
||||||
|
|||||||
108
.github/workflows/pip.yml
vendored
108
.github/workflows/pip.yml
vendored
@ -1,29 +1,38 @@
|
|||||||
name: Pip
|
name: Pip
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
|
||||||
pull_request:
|
|
||||||
push:
|
push:
|
||||||
branches:
|
branches: [main, master]
|
||||||
- master
|
pull_request:
|
||||||
- main
|
branches: [main, master]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
test:
|
||||||
|
name: Test on Python ${{ matrix.python-version }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
platform: [ubuntu-latest]
|
python-version: ['3.12', '3.13']
|
||||||
python-version: ["3.8", "3.12"]
|
|
||||||
|
|
||||||
runs-on: ${{ matrix.platform }}
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: recursive
|
||||||
|
|
||||||
- uses: actions/setup-python@v6
|
- name: Verify submodules
|
||||||
|
run: |
|
||||||
|
echo "Checking submodule status:"
|
||||||
|
git submodule status
|
||||||
|
echo "Checking libbpf directory:"
|
||||||
|
ls -la libbpf/
|
||||||
|
echo "Checking libbpf/src:"
|
||||||
|
ls -la libbpf/src/ || echo "libbpf/src not found!"
|
||||||
|
|
||||||
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
@ -33,19 +42,72 @@ jobs:
|
|||||||
sudo apt-get install -y \
|
sudo apt-get install -y \
|
||||||
libbpf-dev \
|
libbpf-dev \
|
||||||
libelf-dev \
|
libelf-dev \
|
||||||
linux-headers-generic \
|
zlib1g-dev \
|
||||||
build-essential \
|
build-essential \
|
||||||
|
clang \
|
||||||
cmake \
|
cmake \
|
||||||
ninja-build
|
ninja-build \
|
||||||
|
pkg-config \
|
||||||
|
git \
|
||||||
|
make
|
||||||
|
|
||||||
- name: Add requirements
|
- name: Install Python build dependencies
|
||||||
run: python -m pip install --upgrade pip wheel setuptools
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install --upgrade "setuptools>=77.0.0" wheel
|
||||||
|
pip install cmake ninja pybind11
|
||||||
|
|
||||||
- name: Build and install
|
- name: Check build requirements
|
||||||
run: pip install --verbose .[test]
|
run: |
|
||||||
|
echo "Python version:"
|
||||||
|
python --version
|
||||||
|
echo "CMake version:"
|
||||||
|
cmake --version
|
||||||
|
echo "Ninja version:"
|
||||||
|
ninja --version
|
||||||
|
echo "Setuptools version:"
|
||||||
|
python -c "import setuptools; print(setuptools.__version__)"
|
||||||
|
|
||||||
- name: Test import
|
- name: Build and install pylibbpf (verbose)
|
||||||
run: python -I -c "import pylibbpf; print('Import successful')"
|
run: |
|
||||||
|
pip install -v -e . 2>&1 | tee build.log
|
||||||
|
continue-on-error: false
|
||||||
|
|
||||||
- name: Test
|
- name: Check build output
|
||||||
run: python -I -m pytest -v
|
run: |
|
||||||
|
echo "Build directory contents:"
|
||||||
|
find build -type f -name "*.so" 2>/dev/null || echo "No .so files found in build/"
|
||||||
|
echo ""
|
||||||
|
echo "Looking for pylibbpf extension:"
|
||||||
|
find . -name "pylibbpf*.so" -o -name "pylibbpf*.pyd"
|
||||||
|
echo ""
|
||||||
|
echo "Site-packages contents:"
|
||||||
|
python -c "import site; print(site.getsitepackages())"
|
||||||
|
ls -la $(python -c "import site; print(site.getsitepackages()[0])")/pylibbpf/ || echo "pylibbpf not in site-packages"
|
||||||
|
|
||||||
|
- name: Try importing extension directly
|
||||||
|
run: |
|
||||||
|
python -c "
|
||||||
|
import sys
|
||||||
|
print('Python path:', sys.path)
|
||||||
|
try:
|
||||||
|
from pylibbpf import pylibbpf
|
||||||
|
print('Successfully imported pylibbpf.pylibbpf')
|
||||||
|
print('pylibbpf.pylibbpf members:', dir(pylibbpf))
|
||||||
|
except ImportError as e:
|
||||||
|
print(f'Failed to import pylibbpf.pylibbpf: {e}')
|
||||||
|
"
|
||||||
|
|
||||||
|
- name: Verify extension loaded
|
||||||
|
run: |
|
||||||
|
python -c "import pylibbpf; print('Members:', dir(pylibbpf)); assert hasattr(pylibbpf, 'BpfObject'), 'BpfObject not found!'; print('✓ OK')"
|
||||||
|
|
||||||
|
- name: Install test dependencies
|
||||||
|
if: success()
|
||||||
|
run: |
|
||||||
|
pip install pytest pytest-cov
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
if: success()
|
||||||
|
run: |
|
||||||
|
python -I -m pytest -v -s --cov=pylibbpf --cov-report=term-missing
|
||||||
|
|||||||
24
.github/workflows/wheels.yml
vendored
24
.github/workflows/wheels.yml
vendored
@ -15,9 +15,9 @@ jobs:
|
|||||||
name: Build SDist
|
name: Build SDist
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: recursive
|
||||||
|
|
||||||
- name: Build SDist
|
- name: Build SDist
|
||||||
run: pipx run build --sdist
|
run: pipx run build --sdist
|
||||||
@ -25,7 +25,7 @@ jobs:
|
|||||||
- name: Check metadata
|
- name: Check metadata
|
||||||
run: pipx run twine check dist/*
|
run: pipx run twine check dist/*
|
||||||
|
|
||||||
- uses: actions/upload-artifact@v4
|
- uses: actions/upload-artifact@v6
|
||||||
with:
|
with:
|
||||||
name: cibw-sdist
|
name: cibw-sdist
|
||||||
path: dist/*.tar.gz
|
path: dist/*.tar.gz
|
||||||
@ -39,31 +39,33 @@ jobs:
|
|||||||
arch: [x86_64]
|
arch: [x86_64]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: recursive
|
||||||
|
|
||||||
- name: Build wheels
|
- name: Build wheels
|
||||||
uses: pypa/cibuildwheel@v3.2
|
uses: pypa/cibuildwheel@v3.3
|
||||||
env:
|
env:
|
||||||
CIBW_PLATFORM: linux
|
CIBW_PLATFORM: linux
|
||||||
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
|
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
|
||||||
CIBW_BUILD: "cp38-* cp311-*"
|
CIBW_BUILD: "cp312-* cp313-*"
|
||||||
CIBW_SKIP: "*-musllinux*"
|
CIBW_SKIP: "*-musllinux*"
|
||||||
CIBW_TEST_COMMAND: "python -c 'import pylibbpf; print(f\"pylibbpf {pylibbpf.__version__} imported successfully\")'"
|
CIBW_TEST_COMMAND: "python -c 'import pylibbpf; assert hasattr(pylibbpf, \"BpfObject\"), \"BpfObject not found\"; print(f\"pylibbpf {pylibbpf.__version__} OK\")'"
|
||||||
CIBW_TEST_SKIP: "*-linux_aarch64"
|
CIBW_TEST_SKIP: "*-linux_aarch64"
|
||||||
|
|
||||||
CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28
|
CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28
|
||||||
|
|
||||||
CIBW_BEFORE_ALL_LINUX: |
|
CIBW_BEFORE_ALL_LINUX: |
|
||||||
dnf install -y elfutils-libelf-devel zlib-devel
|
dnf install -y elfutils-libelf-devel zlib-devel make gcc gcc-c++ git
|
||||||
|
CIBW_BEFORE_BUILD: >
|
||||||
|
pip install --upgrade "setuptools>=77.0.0" wheel cmake ninja pybind11
|
||||||
|
|
||||||
- name: Verify clean directory
|
- name: Verify clean directory
|
||||||
run: git diff --exit-code
|
run: git diff --exit-code
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Upload wheels
|
- name: Upload wheels
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v6
|
||||||
with:
|
with:
|
||||||
name: cibw-wheels-linux-${{ matrix.arch }}
|
name: cibw-wheels-linux-${{ matrix.arch }}
|
||||||
path: wheelhouse/*.whl
|
path: wheelhouse/*.whl
|
||||||
@ -80,7 +82,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Download all artifacts
|
- name: Download all artifacts
|
||||||
uses: actions/download-artifact@v5
|
uses: actions/download-artifact@v7
|
||||||
with:
|
with:
|
||||||
pattern: cibw-*
|
pattern: cibw-*
|
||||||
path: dist
|
path: dist
|
||||||
|
|||||||
@ -5,6 +5,11 @@ set(CMAKE_CXX_STANDARD 20)
|
|||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
find_package(
|
||||||
|
Python
|
||||||
|
COMPONENTS Interpreter Development.Module
|
||||||
|
REQUIRED)
|
||||||
|
|
||||||
# pybind11
|
# pybind11
|
||||||
include_directories(${CMAKE_SOURCE_DIR}/src)
|
include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||||
add_subdirectory(pybind11)
|
add_subdirectory(pybind11)
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
</picture>
|
</picture>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<!-- PyPI -->
|
<!-- PyPI -->
|
||||||
<a href="https://www.python.org/downloads/release/python-3080/"><img src="https://img.shields.io/badge/python-3.8-blue.svg"></a>
|
<a href="https://www.python.org/downloads/release/python-3120/"><img src="https://img.shields.io/badge/python-3.12-blue.svg"></a>
|
||||||
<a href="https://pypi.org/project/pylibbpf"><img src="https://badge.fury.io/py/pylibbpf.svg"></a>
|
<a href="https://pypi.org/project/pylibbpf"><img src="https://badge.fury.io/py/pylibbpf.svg"></a>
|
||||||
<!-- <a href="https://pypi.org/project/pythonbpf/"><img src="https://img.shields.io/pypi/status/pythonbpf" alt="PyPI Status"></a> -->
|
<!-- <a href="https://pypi.org/project/pythonbpf/"><img src="https://img.shields.io/pypi/status/pythonbpf" alt="PyPI Status"></a> -->
|
||||||
<a href="https://pepy.tech/project/pylibbpf"><img src="https://pepy.tech/badge/pylibbpf" alt="Downloads"></a>
|
<a href="https://pepy.tech/project/pylibbpf"><img src="https://pepy.tech/badge/pylibbpf" alt="Downloads"></a>
|
||||||
|
|||||||
@ -43,4 +43,4 @@ __all__ = [
|
|||||||
"BpfException",
|
"BpfException",
|
||||||
]
|
]
|
||||||
|
|
||||||
__version__ = "0.0.6"
|
__version__ = "0.0.7"
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import ctypes
|
import ctypes
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict, Type
|
|
||||||
|
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
|
|
||||||
@ -51,7 +50,7 @@ def _make_repr(struct_name: str, fields: list):
|
|||||||
return __repr__
|
return __repr__
|
||||||
|
|
||||||
|
|
||||||
def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structure]]:
|
def convert_structs_to_ctypes(structs_sym_tab) -> dict[str, type[ctypes.Structure]]:
|
||||||
"""Convert PythonBPF's structs_sym_tab to ctypes.Structure classes."""
|
"""Convert PythonBPF's structs_sym_tab to ctypes.Structure classes."""
|
||||||
if not structs_sym_tab:
|
if not structs_sym_tab:
|
||||||
return {}
|
return {}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from typing import Callable, Optional
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
|
||||||
class PerfEventArrayHelper:
|
class PerfEventArrayHelper:
|
||||||
@ -13,7 +13,7 @@ class PerfEventArrayHelper:
|
|||||||
callback: Callable,
|
callback: Callable,
|
||||||
struct_name: str = "",
|
struct_name: str = "",
|
||||||
page_cnt: int = 8,
|
page_cnt: int = 8,
|
||||||
lost_callback: Optional[Callable] = None,
|
lost_callback: Callable | None = None,
|
||||||
):
|
):
|
||||||
"""Open perf buffer with auto-deserialization."""
|
"""Open perf buffer with auto-deserialization."""
|
||||||
from .pylibbpf import PerfEventArray
|
from .pylibbpf import PerfEventArray
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[build-system]
|
[build-system]
|
||||||
requires = [
|
requires = [
|
||||||
"setuptools>=42",
|
"setuptools>=77",
|
||||||
"wheel",
|
"wheel",
|
||||||
"ninja",
|
"ninja",
|
||||||
"cmake>=4.0",
|
"cmake>=4.0",
|
||||||
@ -10,15 +10,15 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "pylibbpf"
|
name = "pylibbpf"
|
||||||
version = "0.0.6"
|
version = "0.0.7"
|
||||||
description = "Python Bindings for Libbpf"
|
description = "Python Bindings for Libbpf"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "r41k0u", email = "pragyanshchaturvedi18@gmail.com" },
|
{ name = "r41k0u", email = "pragyanshchaturvedi18@gmail.com" },
|
||||||
{ name = "varun-r-mallya", email = "varunrmallya@gmail.com" }
|
{ name = "varun-r-mallya", email = "varunrmallya@gmail.com" }
|
||||||
]
|
]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = { file = "LICENSE" }
|
license = "Apache-2.0"
|
||||||
requires-python = ">=3.8"
|
requires-python = ">=3.12"
|
||||||
classifiers = [
|
classifiers = [
|
||||||
"Development Status :: 3 - Alpha",
|
"Development Status :: 3 - Alpha",
|
||||||
"Intended Audience :: Developers",
|
"Intended Audience :: Developers",
|
||||||
@ -29,6 +29,7 @@ classifiers = [
|
|||||||
"Programming Language :: Python :: 3.10",
|
"Programming Language :: Python :: 3.10",
|
||||||
"Programming Language :: Python :: 3.11",
|
"Programming Language :: Python :: 3.11",
|
||||||
"Programming Language :: Python :: 3.12",
|
"Programming Language :: Python :: 3.12",
|
||||||
|
"Programming Language :: Python :: 3.13",
|
||||||
"Programming Language :: C++",
|
"Programming Language :: C++",
|
||||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||||
"Topic :: System :: Operating System Kernels :: Linux",
|
"Topic :: System :: Operating System Kernels :: Linux",
|
||||||
@ -47,7 +48,7 @@ Issues = "https://github.com/pythonbpf/pylibbpf/issues"
|
|||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
files = "setup.py"
|
files = "setup.py"
|
||||||
python_version = "3.8"
|
python_version = "3.12"
|
||||||
strict = true
|
strict = true
|
||||||
show_error_codes = true
|
show_error_codes = true
|
||||||
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
|
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
|
||||||
@ -68,7 +69,7 @@ filterwarnings = [
|
|||||||
testpaths = ["tests"]
|
testpaths = ["tests"]
|
||||||
|
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
target-version = "py38"
|
target-version = "py312"
|
||||||
line-length = 88
|
line-length = 88
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
@ -79,3 +80,7 @@ extend-select = [
|
|||||||
"RUF", # Ruff-specific
|
"RUF", # Ruff-specific
|
||||||
"UP", # pyupgrade
|
"UP", # pyupgrade
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[tool.setuptools]
|
||||||
|
packages = ["pylibbpf"]
|
||||||
|
package-data = {"pylibbpf" = ["*.py", "*.so", "*.pyd", "py.typed"]}
|
||||||
|
|||||||
50
setup.py
50
setup.py
@ -3,17 +3,9 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from setuptools import Extension, find_packages, setup
|
from setuptools import Extension, setup
|
||||||
from setuptools.command.build_ext import build_ext
|
from setuptools.command.build_ext import build_ext
|
||||||
|
|
||||||
# Convert distutils Windows platform specifiers to CMake -A arguments
|
|
||||||
PLAT_TO_CMAKE = {
|
|
||||||
"win32": "Win32",
|
|
||||||
"win-amd64": "x64",
|
|
||||||
"win-arm32": "ARM",
|
|
||||||
"win-arm64": "ARM64",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# A CMakeExtension needs a sourcedir instead of a file list.
|
# A CMakeExtension needs a sourcedir instead of a file list.
|
||||||
# The name must be the _single_ output extension from the CMake build.
|
# The name must be the _single_ output extension from the CMake build.
|
||||||
@ -45,8 +37,8 @@ class CMakeBuild(build_ext):
|
|||||||
# from Python.
|
# from Python.
|
||||||
cmake_args = [
|
cmake_args = [
|
||||||
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
|
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
|
||||||
f"-DPYTHON_EXECUTABLE={sys.executable}",
|
|
||||||
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
|
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
|
||||||
|
f"-DPython_EXECUTABLE={sys.executable}",
|
||||||
]
|
]
|
||||||
build_args = []
|
build_args = []
|
||||||
|
|
||||||
@ -122,44 +114,6 @@ if readme_path.exists():
|
|||||||
long_description = readme_path.read_text(encoding="utf-8")
|
long_description = readme_path.read_text(encoding="utf-8")
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="pylibbpf",
|
|
||||||
version="0.0.1",
|
|
||||||
author="varun-r-mallya, r41k0u",
|
|
||||||
author_email="varunrmallyagmail.com",
|
|
||||||
description="Python Bindings for Libbpf",
|
|
||||||
long_description=long_description,
|
|
||||||
long_description_content_type="text/markdown",
|
|
||||||
url="https://github.com/pythonbpf/pylibbpf",
|
|
||||||
packages=find_packages(where="."),
|
|
||||||
package_dir={"": "."},
|
|
||||||
py_modules=[], # Empty since we use packages
|
|
||||||
ext_modules=[CMakeExtension("pylibbpf.pylibbpf")],
|
ext_modules=[CMakeExtension("pylibbpf.pylibbpf")],
|
||||||
cmdclass={"build_ext": CMakeBuild},
|
cmdclass={"build_ext": CMakeBuild},
|
||||||
zip_safe=False,
|
|
||||||
classifiers=[
|
|
||||||
"Development Status :: 3 - Alpha",
|
|
||||||
"Intended Audience :: Developers",
|
|
||||||
"Operating System :: POSIX :: Linux",
|
|
||||||
"Programming Language :: Python :: 3",
|
|
||||||
"Programming Language :: Python :: 3.8",
|
|
||||||
"Programming Language :: Python :: 3.9",
|
|
||||||
"Programming Language :: Python :: 3.10",
|
|
||||||
"Programming Language :: Python :: 3.11",
|
|
||||||
"Programming Language :: Python :: 3.12",
|
|
||||||
"Programming Language :: C++",
|
|
||||||
"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,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@ -65,6 +65,10 @@ PYBIND11_MODULE(pylibbpf, m) {
|
|||||||
.def("get_key_size", &BpfMap::get_key_size)
|
.def("get_key_size", &BpfMap::get_key_size)
|
||||||
.def("get_value_size", &BpfMap::get_value_size)
|
.def("get_value_size", &BpfMap::get_value_size)
|
||||||
.def("get_max_entries", &BpfMap::get_max_entries)
|
.def("get_max_entries", &BpfMap::get_max_entries)
|
||||||
|
.def("set_value_struct", &BpfMap::set_value_struct,
|
||||||
|
py::arg("struct_name"))
|
||||||
|
.def("get_value_struct_name", &BpfMap::get_value_struct_name)
|
||||||
|
.def("has_struct_value", &BpfMap::has_struct_value)
|
||||||
.def("__getitem__", &BpfMap::lookup, py::arg("key"))
|
.def("__getitem__", &BpfMap::lookup, py::arg("key"))
|
||||||
.def("__setitem__", &BpfMap::update, py::arg("key"), py::arg("value"))
|
.def("__setitem__", &BpfMap::update, py::arg("key"), py::arg("value"))
|
||||||
.def("__delitem__", &BpfMap::delete_elem, py::arg("key"));
|
.def("__delitem__", &BpfMap::delete_elem, py::arg("key"));
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include "core/bpf_map.h"
|
#include "core/bpf_map.h"
|
||||||
#include "core/bpf_exception.h"
|
#include "core/bpf_exception.h"
|
||||||
#include "core/bpf_object.h"
|
#include "core/bpf_object.h"
|
||||||
|
#include "utils/struct_parser.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -25,6 +26,24 @@ BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
|
|||||||
value_size_ = bpf_map__value_size(map_);
|
value_size_ = bpf_map__value_size(map_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BpfMap::set_value_struct(const std::string &struct_name) {
|
||||||
|
auto parent = parent_obj_.lock();
|
||||||
|
if (!parent) {
|
||||||
|
throw BpfException("Parent BpfObject no longer exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct_parser_ = parent->get_struct_parser();
|
||||||
|
if (!struct_parser_) {
|
||||||
|
throw BpfException("No struct definitions available in BpfObject");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!struct_parser_->has_struct(struct_name)) {
|
||||||
|
throw BpfException("Unknown struct: " + struct_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
value_struct_name_ = struct_name;
|
||||||
|
}
|
||||||
|
|
||||||
py::object BpfMap::lookup(const py::object &key) const {
|
py::object BpfMap::lookup(const py::object &key) const {
|
||||||
if (map_fd_ < 0)
|
if (map_fd_ < 0)
|
||||||
throw BpfException("Map '" + map_name_ + "' is not initialized properly");
|
throw BpfException("Map '" + map_name_ + "' is not initialized properly");
|
||||||
@ -42,7 +61,7 @@ py::object BpfMap::lookup(const py::object &key) const {
|
|||||||
value_span.data(), value_size_, BPF_ANY);
|
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_ + "'");
|
return py::none();
|
||||||
throw BpfException("Failed to lookup key in map '" + map_name_ +
|
throw BpfException("Failed to lookup key in map '" + map_name_ +
|
||||||
"': " + std::strerror(-ret));
|
"': " + std::strerror(-ret));
|
||||||
}
|
}
|
||||||
@ -215,7 +234,13 @@ void BpfMap::python_to_bytes_inplace(const py::object &obj,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
py::object BpfMap::bytes_to_python(std::span<const uint8_t> data) {
|
py::object BpfMap::bytes_to_python(std::span<const uint8_t> data) const {
|
||||||
|
// NOTE: Struct parsing for value type
|
||||||
|
if (struct_parser_ && !value_struct_name_.empty()) {
|
||||||
|
py::bytes py_data(reinterpret_cast<const char *>(data.data()), data.size());
|
||||||
|
return struct_parser_->parse(value_struct_name_, py_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);
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class BpfObject;
|
class BpfObject;
|
||||||
|
class StructParser;
|
||||||
|
|
||||||
namespace py = pybind11;
|
namespace py = pybind11;
|
||||||
|
|
||||||
@ -20,6 +21,11 @@ private:
|
|||||||
std::string map_name_;
|
std::string map_name_;
|
||||||
__u32 key_size_, value_size_;
|
__u32 key_size_, value_size_;
|
||||||
|
|
||||||
|
// TODO: For now, we'll only support struct parsing for value types
|
||||||
|
// later we can extend this to keys
|
||||||
|
std::shared_ptr<StructParser> struct_parser_;
|
||||||
|
std::string value_struct_name_;
|
||||||
|
|
||||||
template <size_t StackSize = 64> struct BufferManager {
|
template <size_t StackSize = 64> 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;
|
||||||
@ -52,6 +58,7 @@ public:
|
|||||||
py::dict items() const;
|
py::dict items() const;
|
||||||
py::list keys() const;
|
py::list keys() const;
|
||||||
py::list values() const;
|
py::list values() const;
|
||||||
|
void set_value_struct(const std::string &struct_name);
|
||||||
|
|
||||||
[[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_; }
|
||||||
@ -62,11 +69,17 @@ public:
|
|||||||
[[nodiscard]] std::shared_ptr<BpfObject> get_parent() const {
|
[[nodiscard]] std::shared_ptr<BpfObject> get_parent() const {
|
||||||
return parent_obj_.lock();
|
return parent_obj_.lock();
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] std::string get_value_struct_name() const {
|
||||||
|
return value_struct_name_;
|
||||||
|
}
|
||||||
|
[[nodiscard]] bool has_struct_value() const {
|
||||||
|
return !value_struct_name_.empty();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void python_to_bytes_inplace(const py::object &obj,
|
static void python_to_bytes_inplace(const py::object &obj,
|
||||||
std::span<uint8_t> buffer);
|
std::span<uint8_t> buffer);
|
||||||
static py::object bytes_to_python(std::span<const uint8_t> data);
|
py::object bytes_to_python(std::span<const uint8_t> data) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PYLIBBPF_BPF_MAP_H
|
#endif // PYLIBBPF_BPF_MAP_H
|
||||||
|
|||||||
@ -2,6 +2,6 @@ import pylibbpf as m
|
|||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
assert m.__version__ == "0.0.6"
|
assert m.__version__ == "0.0.7"
|
||||||
prog = m.BpfObject("tests/execve2.o", structs={})
|
prog = m.BpfObject("tests/execve2.o", structs={})
|
||||||
print(prog)
|
print(prog)
|
||||||
|
|||||||
Reference in New Issue
Block a user