Skip to content

Commit c38ecf6

Browse files
committed
Merge branch 'master' into helper-refactor
2 parents 81807ac + 2f02f94 commit c38ecf6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+83428
-82655
lines changed

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
updates:
3+
# Maintain dependencies for GitHub Actions
4+
- package-ecosystem: "github-actions"
5+
directory: "/"
6+
schedule:
7+
interval: "weekly"
8+
groups:
9+
actions:
10+
patterns:
11+
- "*"

.github/workflows/format.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This is a format job. Pre-commit has a first-party GitHub action, so we use
2+
# that: https://github.com/pre-commit/action
3+
4+
name: Format
5+
6+
on:
7+
workflow_dispatch:
8+
pull_request:
9+
push:
10+
branches:
11+
- master
12+
13+
jobs:
14+
pre-commit:
15+
name: Format
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
- uses: actions/setup-python@v5
20+
with:
21+
python-version: "3.x"
22+
- uses: pre-commit/action@v3.0.1

.github/workflows/python-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ jobs:
2020
runs-on: ubuntu-latest
2121

2222
steps:
23-
- uses: actions/checkout@v4
23+
- uses: actions/checkout@v5
2424

25-
- uses: actions/setup-python@v5
25+
- uses: actions/setup-python@v6
2626
with:
2727
python-version: "3.x"
2828

@@ -59,7 +59,7 @@ jobs:
5959

6060
steps:
6161
- name: Retrieve release distributions
62-
uses: actions/download-artifact@v4
62+
uses: actions/download-artifact@v5
6363
with:
6464
name: release-dists
6565
path: dist/

.pre-commit-config.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# To use:
2+
#
3+
# pre-commit run -a
4+
#
5+
# Or:
6+
#
7+
# pre-commit install # (runs every time you commit in git)
8+
#
9+
# To update this file:
10+
#
11+
# pre-commit autoupdate
12+
#
13+
# See https://github.com/pre-commit/pre-commit
14+
15+
exclude: 'vmlinux.*\.py$'
16+
17+
ci:
18+
autoupdate_commit_msg: "chore: update pre-commit hooks"
19+
autofix_commit_msg: "style: pre-commit fixes"
20+
21+
repos:
22+
# Standard hooks
23+
- repo: https://github.com/pre-commit/pre-commit-hooks
24+
rev: v4.6.0
25+
hooks:
26+
- id: check-added-large-files
27+
- id: check-case-conflict
28+
- id: check-merge-conflict
29+
- id: check-symlinks
30+
- id: check-yaml
31+
exclude: ^conda\.recipe/meta\.yaml$
32+
- id: debug-statements
33+
- id: end-of-file-fixer
34+
- id: mixed-line-ending
35+
- id: requirements-txt-fixer
36+
- id: trailing-whitespace
37+
38+
- repo: https://github.com/astral-sh/ruff-pre-commit
39+
rev: "v0.4.2"
40+
hooks:
41+
- id: ruff
42+
args: ["--fix", "--show-fixes"]
43+
- id: ruff-format
44+
exclude: ^(docs)
45+
46+
## Checking static types
47+
#- repo: https://github.com/pre-commit/mirrors-mypy
48+
# rev: "v1.10.0"
49+
# hooks:
50+
# - id: mypy
51+
# files: "setup.py"
52+
# args: []
53+
# additional_dependencies: [types-setuptools]
54+
55+
# Changes tabs to spaces
56+
- repo: https://github.com/Lucas-C/pre-commit-hooks
57+
rev: v1.5.5
58+
hooks:
59+
- id: remove-tabs
60+
exclude: '^(docs)|.*/Makefile$|Makefile$'

LICENSE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,3 @@
200200
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201201
See the License for the specific language governing permissions and
202202
limitations under the License.
203-

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
compile:
2-
chmod +x ./tools/compile.py
3-
./tools/compile.py ./examples/execve3.py
4-
5-
install:
1+
install:
62
pip install -e .
73

84
clean:
95
rm -rf build dist *.egg-info
106
rm -rf examples/*.ll examples/*.o
117

12-
all: install compile
8+
all: clean install
139

1410
.PHONY: all clean

README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1-
# Python-BPF
2-
3-
Python-BPF is an LLVM IR generator for eBPF programs written in Python. It uses [llvmlite](https://github.com/numba/llvmlite) to generate LLVM IR and then compiles to LLVM object files. These object files can be loaded into the kernel for execution. Unlike BCC, Python-BPF performs compilation without relying on its infrastructure.
1+
<picture>
2+
<source
3+
media="(prefers-color-scheme: light)"
4+
srcset="https://github.com/user-attachments/assets/f3738131-d7cb-4b5c-8699-c7010295a159"
5+
width="450"
6+
alt="Light‐mode image">
7+
<img
8+
src="https://github.com/user-attachments/assets/b175bf39-23cb-475d-a6e1-7b5c99a1ed72"
9+
width="450"
10+
alt="Dark‐mode image">
11+
</picture>
12+
<!-- Badges -->
13+
<p align="center">
14+
<!-- PyPI -->
15+
<a href="https://pypi.org/project/pythonbpf/"><img src="https://img.shields.io/pypi/v/pythonbpf?color=blue" alt="PyPI version"></a>
16+
<!-- <a href="https://pypi.org/project/pythonbpf/"><img src="https://img.shields.io/pypi/pyversions/pythonbpf" alt="Python versions"></a> -->
17+
<!-- <a href="https://pypi.org/project/pythonbpf/"><img src="https://img.shields.io/pypi/dm/pythonbpf" alt="PyPI downloads"></a> -->
18+
<!-- <a href="https://pypi.org/project/pythonbpf/"><img src="https://img.shields.io/pypi/status/pythonbpf" alt="PyPI Status"></a> -->
19+
<a href="https://pepy.tech/project/pythonbpf"><img src="https://pepy.tech/badge/pythonbpf" alt="Downloads"></a>
20+
<!-- Build & CI -->
21+
<a href="https://github.com/pythonbpf/python-bpf/actions"><img src="https://github.com/pythonbpf/python-bpf/actions/workflows/python-publish.yml/badge.svg" alt="Build Status"></a>
22+
<!-- Meta -->
23+
<a href="https://github.com/pythonbpf/python-bpf/blob/main/LICENSE"><img src="https://img.shields.io/github/license/pythonbpf/python-bpf" alt="License"></a>
24+
</p>
25+
26+
27+
Python-BPF is an LLVM IR generator for eBPF programs written in Python. It uses [llvmlite](https://github.com/numba/llvmlite) to generate LLVM IR and then compiles to LLVM object files. These object files can be loaded into the kernel for execution. Python-BPF performs compilation without relying on BCC.
428

529
> **Note**: This project is under active development and not ready for production use.
630
@@ -145,23 +169,17 @@ This architecture eliminates the need for embedding C code in Python, allowing f
145169
```bash
146170
make install
147171
```
148-
149-
3. Build and test examples:
150-
151-
```bash
152-
make
153-
```
154-
155-
4. Verify an object file with the kernel verifier:
172+
Then, run any example in `examples`
173+
3. Verify an object file with the kernel verifier:
156174

157175
```bash
158-
./check.sh check execve2.o
176+
./tools/check.sh check execve2.o
159177
```
160178

161179
5. Run an object file using `bpftool`:
162180

163181
```bash
164-
./check.sh run execve2.o
182+
./tools/check.sh run execve2.o
165183
```
166184

167185
6. Explore LLVM IR output from clang in `examples/c-form` by running `make`.

TODO.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
## Short term
22

33
- Implement enough functionality to port the BCC tutorial examples in PythonBPF
4-
4+
- Static Typing
5+
- Add all maps
6+
- XDP support in pylibbpf
7+
- ringbuf support
8+
- recursive expression resolution
59

610
## Long term
711

examples/binops_demo.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# 3. Run the program with sudo: sudo tools/check.sh run examples/binops_demo.py
1111
# 4. Start up any program and watch the output
1212

13+
1314
@bpf
1415
@map
1516
def last() -> HashMap:
@@ -23,26 +24,28 @@ def do_trace(ctx: c_void_p) -> c_int64:
2324
tsp = last().lookup(key)
2425
if tsp:
2526
kt = ktime()
26-
delta = (kt - tsp)
27+
delta = kt - tsp
2728
if delta < 1000000000:
28-
time_ms = (delta // 1000000)
29+
time_ms = delta // 1000000
2930
print(f"Execve syscall entered within last second, last {time_ms} ms ago")
3031
last().delete(key)
3132
else:
3233
kt = ktime()
3334
last().update(key, kt)
3435
return c_int64(0)
3536

37+
3638
@bpf
3739
@section("tracepoint/syscalls/sys_exit_execve")
3840
def do_exit(ctx: c_void_p) -> c_int64:
3941
va = 8
4042
nm = 5 ^ va
4143
al = 6 & 3
42-
ru = (nm + al)
44+
ru = nm + al
4345
print(f"this is a variable {ru}")
4446
return c_int64(0)
4547

48+
4649
@bpf
4750
@bpfglobal
4851
def LICENSE() -> str:

examples/blk_request.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
from pythonbpf import bpf, map, section, bpfglobal, compile
2-
from pythonbpf.helpers import ktime, deref
2+
from pythonbpf.helpers import ktime
33
from pythonbpf.maps import HashMap
44

5-
from ctypes import c_void_p, c_int64, c_int32, c_uint64
5+
from ctypes import c_void_p, c_int32, c_uint64
66

77

88
@bpf
99
@map
1010
def last() -> HashMap:
1111
return HashMap(key=c_uint64, value=c_uint64, max_entries=3)
1212

13+
1314
@bpf
1415
@section("blk_start_request")
1516
def trace_start(ctx: c_void_p) -> c_int32:
1617
ts = ktime()
17-
print("req started")
18+
print(f"req started {ts}")
1819
return c_int32(0)
1920

2021

0 commit comments

Comments
 (0)