Skip to main content

Python-BPF

This is an LLVM IR generator for eBPF programs in Python. We use llvmlite to generate LLVM IR from pure Python. This is then compiled to LLVM object files, which can be loaded into the kernel for execution. We do not rely on BCC to do our compilation.

DO NOT USE IN PRODUCTION. IN DEVELOPMENT.

Video Demo

Video demo for code under demo/

Slide Deck

Slide deck explaining the project

Installation

  • Have clang installed.
  • pip install pythonbpf

Usage

# pythonbpf_example.py
from pythonbpf import bpf, map, bpfglobal, section, compile
from pythonbpf.helpers import bpf_ktime_get_ns
from pythonbpf.maps import HashMap

from ctypes import c_void_p, c_int64, c_int32, c_uint64

@bpf
@map
def last() -> HashMap:
    return HashMap(key=c_uint64, value=c_uint64, max_entries=1)

@bpf
@section("tracepoint/syscalls/sys_enter_execve")
def hello(ctx: c_void_p) -> c_int32:
    print("entered")
    return c_int32(0)

@bpf
@section("tracepoint/syscalls/sys_exit_execve")
def hello_again(ctx: c_void_p) -> c_int64:
    print("exited")
    key = 0
    tsp = last().lookup(key)
    print(tsp)
    ts = bpf_ktime_get_ns()
    return c_int64(0)

@bpf
@bpfglobal
def LICENSE() -> str:
    return "GPL"

def some_normal_function():
    print("normal function")

# compiles and dumps object file in the same directory
compile()
  • Run python pythonbpf_example.py to get the compiled object file that can be then loaded into the kernel.

Development

  • Make a virtual environment and activate it using python3 -m venv .venv && source .venv/bin/activate.
  • Run make install to install the required dependencies.
  • Run make to see the compilation output of the example.
  • Run check.sh to check if generated object file passes through the verifier inside the examples directory.
  • Run make in the examples/c-form directory to modify the example C BPF program to check the actual LLVM IR generated by clang.

Development Notes

  • Run ./check.sh check execve2.o; in examples folder to check if the object code passes the verifier.
  • Run ./check.sh run execve2.o; in examples folder to run the object code using bpftool.

Authors

Metadata

Release files for pythonbpf 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pythonbpf 0.1.3
File Size Uploaded Provenance Actions
pythonbpf-0.1.3.tar.gz 28.6 kB With provenance Details for pythonbpf-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pythonbpf 0.1.3
File Interpreter ABI Platform Actions
pythonbpf-0.1.3-py3-none-any.whl Python 3 none any Details for pythonbpf-0.1.3-py3-none-any.whl

Total release size: 60.6 kB

Release files / pythonbpf-0.1.3.tar.gz

Download URL pythonbpf-0.1.3.tar.gz
Size 28.6 kB
Tags Source
SHA-256 checksum
How to use checksums
89ef2291ee808ea594e3fa8d302854e4b1710fcea4540353d80dab129c9434d5
BLAKE2b-256 checksum
How to use checksums
1aca5342263ca61bbbd87aee08c4e2ac8e5ea2636e6e383f28636aa715eefa64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2025.

Transparency log

Release files / pythonbpf-0.1.3-py3-none-any.whl

Download URL pythonbpf-0.1.3-py3-none-any.whl
Size 31.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63917df8f5364ccc76ad4c993f1ef6e0968f314f19470f417813683da28fffc0
BLAKE2b-256 checksum
How to use checksums
48dca6307d55a389d96d4628db3ffa59bae389f30a113e0e4389388f2d8d89d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2025.

Transparency log

Security for pythonbpf 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pythonbpf-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page