mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Update imports to use direct namespace imports
WARNING: OLD STYLE IMPORTS DO NOT WORK The old style of importing the full module and using pb.* prefixes has been replaced with direct imports of the needed names. This makes the code more explicit about what is being used and removes the unnecessary pb prefix.
This commit is contained in:
20
README.md
20
README.md
@ -13,25 +13,25 @@ This is an LLVM IR generator for eBPF programs in Python. We use llvmlite to gen
|
||||
## Usage
|
||||
```python
|
||||
# pythonbpf_example.py
|
||||
import pythonbpf as pb
|
||||
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
|
||||
|
||||
@pb.bpf
|
||||
@pb.map
|
||||
@bpf
|
||||
@map
|
||||
def last() -> HashMap:
|
||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
||||
|
||||
@pb.bpf
|
||||
@pb.section("tracepoint/syscalls/sys_enter_execve")
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_execve")
|
||||
def hello(ctx: c_void_p) -> c_int32:
|
||||
print("entered")
|
||||
return c_int32(0)
|
||||
|
||||
@pb.bpf
|
||||
@pb.section("tracepoint/syscalls/sys_exit_execve")
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_exit_execve")
|
||||
def hello_again(ctx: c_void_p) -> c_int64:
|
||||
print("exited")
|
||||
key = 0
|
||||
@ -40,8 +40,8 @@ def hello_again(ctx: c_void_p) -> c_int64:
|
||||
ts = bpf_ktime_get_ns()
|
||||
return c_int64(0)
|
||||
|
||||
@pb.bpf
|
||||
@pb.bpfglobal
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
@ -49,7 +49,7 @@ def some_normal_function():
|
||||
print("normal function")
|
||||
|
||||
# compiles and dumps object file in the same directory
|
||||
pb.compile()
|
||||
compile()
|
||||
```
|
||||
- Run `python pythonbpf_example.py` to get the compiled object file that can be then loaded into the kernel.
|
||||
|
||||
|
||||
@ -1,25 +1,25 @@
|
||||
import pythonbpf as pb
|
||||
from pythonbpf import bpf, map, section, bpfglobal, 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
|
||||
|
||||
@pb.bpf
|
||||
@pb.map
|
||||
@bpf
|
||||
@map
|
||||
def last() -> HashMap:
|
||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
||||
|
||||
|
||||
@pb.bpf
|
||||
@pb.section("tracepoint/syscalls/sys_enter_execve")
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_execve")
|
||||
def hello(ctx: c_void_p) -> c_int32:
|
||||
print("entered")
|
||||
print("multi constant support")
|
||||
return c_int32(0)
|
||||
|
||||
|
||||
@pb.bpf
|
||||
@pb.section("tracepoint/syscalls/sys_exit_execve")
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_exit_execve")
|
||||
def hello_again(ctx: c_void_p) -> c_int64:
|
||||
print("exited")
|
||||
key = 0
|
||||
@ -34,9 +34,9 @@ def hello_again(ctx: c_void_p) -> c_int64:
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@pb.bpf
|
||||
@pb.bpfglobal
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
pb.compile()
|
||||
compile()
|
||||
|
||||
@ -6,8 +6,8 @@ from .maps_pass import maps_proc
|
||||
from .globals_pass import globals_processing
|
||||
import os
|
||||
import subprocess
|
||||
import pathlib
|
||||
import __main__
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
|
||||
def find_bpf_chunks(tree):
|
||||
"""Find all functions decorated with @bpf in the AST."""
|
||||
@ -96,12 +96,14 @@ def compile_to_ir(filename: str, output: str):
|
||||
return output
|
||||
|
||||
def compile():
|
||||
main_file = pathlib.Path(__main__.__file__).resolve()
|
||||
# Look one level up the stack to the caller of this function
|
||||
caller_frame = inspect.stack()[1]
|
||||
caller_file = Path(caller_frame.filename).resolve()
|
||||
|
||||
ll_file = pathlib.Path("/tmp") / main_file.with_suffix(".ll").name
|
||||
o_file = main_file.with_suffix(".o")
|
||||
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name
|
||||
o_file = caller_file.with_suffix(".o")
|
||||
|
||||
compile_to_ir(str(main_file), str(ll_file))
|
||||
compile_to_ir(str(caller_file), str(ll_file))
|
||||
|
||||
subprocess.run([
|
||||
"llc", "-march=bpf", "-filetype=obj", "-O2",
|
||||
|
||||
Reference in New Issue
Block a user