Reduced Python frontend for eBPF
Project description
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.
Installation
- Have
clanginstalled. 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_type=c_uint64, value_type=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.pyto 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 installto install the required dependencies. - Run
maketo see the compilation output of the example. - Run
check.shto check if generated object file passes through the verifier inside the examples directory. - Run
makein theexamples/c-formdirectory 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 usingbpftool.
Authors
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pythonbpf-0.1.1.tar.gz.
File metadata
- Download URL: pythonbpf-0.1.1.tar.gz
- Upload date:
- Size: 24.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f88b2f386b3c83ba38b74c1dfb58ec2b2df1d2daf602485d4f69b96d559a9b06
|
|
| MD5 |
e8b7aae6052c3da9a96fa3de5044c56c
|
|
| BLAKE2b-256 |
6e370aac2c33b780f658fa1d395e154ec4a55abebda7076bf7c0d8f5f49b9ed1
|
Provenance
The following attestation bundles were made for pythonbpf-0.1.1.tar.gz:
Publisher:
python-publish.yml on varun-r-mallya/Python-BPF
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pythonbpf-0.1.1.tar.gz -
Subject digest:
f88b2f386b3c83ba38b74c1dfb58ec2b2df1d2daf602485d4f69b96d559a9b06 - Sigstore transparency entry: 497932111
- Sigstore integration time:
-
Permalink:
varun-r-mallya/Python-BPF@63375d1710acde80abe81ad5cef9fbd0240dc961 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/varun-r-mallya
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@63375d1710acde80abe81ad5cef9fbd0240dc961 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pythonbpf-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pythonbpf-0.1.1-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30796e7de1b42e7250915c862cd75ecfadab3a958aed774b99095840bbe46b77
|
|
| MD5 |
e9dd7a56202bd91dcaf14d5052db58c8
|
|
| BLAKE2b-256 |
9f4d543eba9f8ff3133d8c2f162981f5b863074949e8035cca0cc280c9ee68d7
|
Provenance
The following attestation bundles were made for pythonbpf-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on varun-r-mallya/Python-BPF
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pythonbpf-0.1.1-py3-none-any.whl -
Subject digest:
30796e7de1b42e7250915c862cd75ecfadab3a958aed774b99095840bbe46b77 - Sigstore transparency entry: 497932140
- Sigstore integration time:
-
Permalink:
varun-r-mallya/Python-BPF@63375d1710acde80abe81ad5cef9fbd0240dc961 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/varun-r-mallya
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@63375d1710acde80abe81ad5cef9fbd0240dc961 -
Trigger Event:
release
-
Statement type: