mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
update helpers and change examples.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ __pycache__/
|
|||||||
*.ll
|
*.ll
|
||||||
*.o
|
*.o
|
||||||
.ipynb_checkpoints/
|
.ipynb_checkpoints/
|
||||||
|
vmlinux.py
|
||||||
|
|||||||
248446
examples/vmlinux.py
248446
examples/vmlinux.py
File diff suppressed because it is too large
Load Diff
@ -15,5 +15,8 @@ def deref(ptr):
|
|||||||
return result if result is not None else 0
|
return result if result is not None else 0
|
||||||
|
|
||||||
|
|
||||||
|
XDP_ABORTED = ctypes.c_int64(0)
|
||||||
XDP_DROP = ctypes.c_int64(1)
|
XDP_DROP = ctypes.c_int64(1)
|
||||||
XDP_PASS = ctypes.c_int64(2)
|
XDP_PASS = ctypes.c_int64(2)
|
||||||
|
XDP_TX = ctypes.c_int64(3)
|
||||||
|
XDP_REDIRECT = ctypes.c_int64(4)
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
SEC("xdp")
|
SEC("xdp")
|
||||||
int hello(struct xdp_md *ctx) {
|
int hello(struct xdp_md *ctx) {
|
||||||
// ctx.
|
|
||||||
bpf_printk("Hello, World! %ud \n", ctx->data);
|
bpf_printk("Hello, World! %ud \n", ctx->data);
|
||||||
return XDP_PASS;
|
return XDP_PASS;
|
||||||
}
|
}
|
||||||
|
|||||||
43
tests/failing_tests/xdp_pass.py
Normal file
43
tests/failing_tests/xdp_pass.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir
|
||||||
|
from pythonbpf.maps import HashMap
|
||||||
|
from vmlinux import struct_xdp_md, XDP_PASS
|
||||||
|
from ctypes import c_void_p, c_int64
|
||||||
|
|
||||||
|
# Instructions to how to run this program
|
||||||
|
# 1. Install PythonBPF: pip install pythonbpf
|
||||||
|
# 2. Run the program: python examples/xdp_pass.py
|
||||||
|
# 3. Run the program with sudo: sudo tools/check.sh run examples/xdp_pass.o
|
||||||
|
# 4. Attach object file to any network device with something like ./check.sh xdp examples/xdp_pass.o tailscale0
|
||||||
|
# 5. send traffic through the device and observe effects
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@map
|
||||||
|
def count() -> HashMap:
|
||||||
|
return HashMap(key=c_int64, value=c_int64, max_entries=1)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("xdp")
|
||||||
|
def hello_world(ctx: struct_xdp_md) -> c_int64:
|
||||||
|
key = 0
|
||||||
|
one = 1
|
||||||
|
prev = count().lookup(key)
|
||||||
|
if prev:
|
||||||
|
prevval = prev + 1
|
||||||
|
print(f"count: {prevval}")
|
||||||
|
count().update(key, prevval)
|
||||||
|
return XDP_PASS
|
||||||
|
else:
|
||||||
|
count().update(key, one)
|
||||||
|
|
||||||
|
return XDP_PASS
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
compile_to_ir("xdp_pass.py", "xdp_pass.ll")
|
||||||
|
compile()
|
||||||
Reference in New Issue
Block a user