mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
18 Commits
5a8b64f1d9
...
symex
| Author | SHA1 | Date | |
|---|---|---|---|
| da45daa972 | |||
| 39a0746db4 | |||
| e9bb90cb70 | |||
| 9d76502d5a | |||
| a10da4a277 | |||
| 29e90601b7 | |||
| 56df05a93c | |||
| a55efc6469 | |||
| 64cd2d2fc2 | |||
| cbddc0aa96 | |||
| 209df33c8f | |||
| 7a56e5d0cd | |||
| 1d7a436c9f | |||
| 5eaeb3e921 | |||
| cd52d0d91b | |||
| df981be095 | |||
| 316c21c428 | |||
| c883d95655 |
@ -68,8 +68,6 @@ def callback(cpu, event):
|
|||||||
|
|
||||||
perf = b["events"].open_perf_buffer(callback, struct_name="data_t")
|
perf = b["events"].open_perf_buffer(callback, struct_name="data_t")
|
||||||
print("Starting to poll... (Ctrl+C to stop)")
|
print("Starting to poll... (Ctrl+C to stop)")
|
||||||
print("Try running: fork() or clone() system calls to trigger events")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
b["events"].poll(1000)
|
b["events"].poll(1000)
|
||||||
|
|||||||
@ -26,7 +26,7 @@ classifiers = [
|
|||||||
]
|
]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = {text = "Apache-2.0"}
|
license = {text = "Apache-2.0"}
|
||||||
requires-python = ">=3.8"
|
requires-python = ">=3.10"
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"llvmlite",
|
"llvmlite",
|
||||||
|
|||||||
@ -86,7 +86,7 @@ def processor(source_code, filename, module):
|
|||||||
license_processing(tree, module)
|
license_processing(tree, module)
|
||||||
globals_processing(tree, module)
|
globals_processing(tree, module)
|
||||||
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
|
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
|
||||||
map_sym_tab = maps_proc(tree, module, bpf_chunks)
|
map_sym_tab = maps_proc(tree, module, bpf_chunks, structs_sym_tab)
|
||||||
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
|
func_proc(tree, module, bpf_chunks, map_sym_tab, structs_sym_tab)
|
||||||
|
|
||||||
globals_list_creation(tree, module)
|
globals_list_creation(tree, module)
|
||||||
@ -218,13 +218,11 @@ def compile(loglevel=logging.WARNING) -> bool:
|
|||||||
def BPF(loglevel=logging.WARNING) -> BpfObject:
|
def BPF(loglevel=logging.WARNING) -> BpfObject:
|
||||||
caller_frame = inspect.stack()[1]
|
caller_frame = inspect.stack()[1]
|
||||||
src = inspect.getsource(caller_frame.frame)
|
src = inspect.getsource(caller_frame.frame)
|
||||||
with tempfile.NamedTemporaryFile(
|
with (
|
||||||
mode="w+", delete=True, suffix=".py"
|
tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".py") as f,
|
||||||
) as f, tempfile.NamedTemporaryFile(
|
tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter,
|
||||||
mode="w+", delete=True, suffix=".ll"
|
tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file,
|
||||||
) as inter, tempfile.NamedTemporaryFile(
|
):
|
||||||
mode="w+", delete=False, suffix=".o"
|
|
||||||
) as obj_file:
|
|
||||||
f.write(src)
|
f.write(src)
|
||||||
f.flush()
|
f.flush()
|
||||||
source = f.name
|
source = f.name
|
||||||
|
|||||||
@ -12,6 +12,7 @@ from .helpers import (
|
|||||||
smp_processor_id,
|
smp_processor_id,
|
||||||
uid,
|
uid,
|
||||||
skb_store_bytes,
|
skb_store_bytes,
|
||||||
|
get_stack,
|
||||||
XDP_DROP,
|
XDP_DROP,
|
||||||
XDP_PASS,
|
XDP_PASS,
|
||||||
)
|
)
|
||||||
@ -83,6 +84,7 @@ __all__ = [
|
|||||||
"smp_processor_id",
|
"smp_processor_id",
|
||||||
"uid",
|
"uid",
|
||||||
"skb_store_bytes",
|
"skb_store_bytes",
|
||||||
|
"get_stack",
|
||||||
"XDP_DROP",
|
"XDP_DROP",
|
||||||
"XDP_PASS",
|
"XDP_PASS",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -12,11 +12,10 @@ from .helper_utils import (
|
|||||||
get_int_value_from_arg,
|
get_int_value_from_arg,
|
||||||
)
|
)
|
||||||
from .printk_formatter import simple_string_print, handle_fstring_print
|
from .printk_formatter import simple_string_print, handle_fstring_print
|
||||||
|
from pythonbpf.maps import BPFMapType
|
||||||
from logging import Logger
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class BPFHelperID(Enum):
|
class BPFHelperID(Enum):
|
||||||
@ -33,7 +32,12 @@ class BPFHelperID(Enum):
|
|||||||
BPF_GET_CURRENT_UID_GID = 15
|
BPF_GET_CURRENT_UID_GID = 15
|
||||||
BPF_GET_CURRENT_COMM = 16
|
BPF_GET_CURRENT_COMM = 16
|
||||||
BPF_PERF_EVENT_OUTPUT = 25
|
BPF_PERF_EVENT_OUTPUT = 25
|
||||||
|
BPF_GET_STACK = 67
|
||||||
BPF_PROBE_READ_KERNEL_STR = 115
|
BPF_PROBE_READ_KERNEL_STR = 115
|
||||||
|
BPF_RINGBUF_OUTPUT = 130
|
||||||
|
BPF_RINGBUF_RESERVE = 131
|
||||||
|
BPF_RINGBUF_SUBMIT = 132
|
||||||
|
BPF_RINGBUF_DISCARD = 133
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register(
|
@HelperHandlerRegistry.register(
|
||||||
@ -358,11 +362,6 @@ def bpf_get_current_pid_tgid_emitter(
|
|||||||
return pid, ir.IntType(64)
|
return pid, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
@HelperHandlerRegistry.register(
|
|
||||||
"output",
|
|
||||||
param_types=[ir.PointerType(ir.IntType(8))],
|
|
||||||
return_type=ir.IntType(64),
|
|
||||||
)
|
|
||||||
def bpf_perf_event_output_handler(
|
def bpf_perf_event_output_handler(
|
||||||
call,
|
call,
|
||||||
map_ptr,
|
map_ptr,
|
||||||
@ -373,6 +372,10 @@ def bpf_perf_event_output_handler(
|
|||||||
struct_sym_tab=None,
|
struct_sym_tab=None,
|
||||||
map_sym_tab=None,
|
map_sym_tab=None,
|
||||||
):
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_perf_event_output helper function call.
|
||||||
|
"""
|
||||||
|
|
||||||
if len(call.args) != 1:
|
if len(call.args) != 1:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Perf event output expects exactly one argument, got {len(call.args)}"
|
f"Perf event output expects exactly one argument, got {len(call.args)}"
|
||||||
@ -410,6 +413,98 @@ def bpf_perf_event_output_handler(
|
|||||||
return result, None
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
def bpf_ringbuf_output_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_ringbuf_output helper function call.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if len(call.args) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
f"Ringbuf output expects exactly one argument, got {len(call.args)}"
|
||||||
|
)
|
||||||
|
data_arg = call.args[0]
|
||||||
|
data_ptr, size_val = get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab)
|
||||||
|
flags_val = ir.Constant(ir.IntType(64), 0)
|
||||||
|
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
data_void_ptr = builder.bitcast(data_ptr, ir.PointerType())
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.IntType(64),
|
||||||
|
[
|
||||||
|
ir.PointerType(),
|
||||||
|
ir.PointerType(),
|
||||||
|
ir.IntType(64),
|
||||||
|
ir.IntType(64),
|
||||||
|
],
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
# helper id
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_RINGBUF_OUTPUT.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
result = builder.call(
|
||||||
|
fn_ptr, [map_void_ptr, data_void_ptr, size_val, flags_val], tail=False
|
||||||
|
)
|
||||||
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register(
|
||||||
|
"output",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(8))],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
|
def handle_output_helper(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Route output helper to the appropriate emitter based on map type.
|
||||||
|
"""
|
||||||
|
match map_sym_tab[map_ptr.name].type:
|
||||||
|
case BPFMapType.PERF_EVENT_ARRAY:
|
||||||
|
return bpf_perf_event_output_handler(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
)
|
||||||
|
case BPFMapType.RINGBUF:
|
||||||
|
return bpf_ringbuf_output_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
)
|
||||||
|
case _:
|
||||||
|
logger.error("Unsupported map type for output helper.")
|
||||||
|
raise NotImplementedError("Output helper for this map type is not implemented.")
|
||||||
|
|
||||||
|
|
||||||
def emit_probe_read_kernel_str_call(builder, dst_ptr, dst_size, src_ptr):
|
def emit_probe_read_kernel_str_call(builder, dst_ptr, dst_size, src_ptr):
|
||||||
"""Emit LLVM IR call to bpf_probe_read_kernel_str"""
|
"""Emit LLVM IR call to bpf_probe_read_kernel_str"""
|
||||||
|
|
||||||
@ -711,7 +806,10 @@ def bpf_skb_store_bytes_emitter(
|
|||||||
flags_val = get_flags_val(call.args[3], builder, local_sym_tab)
|
flags_val = get_flags_val(call.args[3], builder, local_sym_tab)
|
||||||
else:
|
else:
|
||||||
flags_val = 0
|
flags_val = 0
|
||||||
|
if isinstance(flags_val, int):
|
||||||
flags = ir.Constant(ir.IntType(64), flags_val)
|
flags = ir.Constant(ir.IntType(64), flags_val)
|
||||||
|
else:
|
||||||
|
flags = flags_val
|
||||||
fn_type = ir.FunctionType(
|
fn_type = ir.FunctionType(
|
||||||
ir.IntType(64),
|
ir.IntType(64),
|
||||||
args_signature,
|
args_signature,
|
||||||
@ -736,6 +834,170 @@ def bpf_skb_store_bytes_emitter(
|
|||||||
return result, ir.IntType(64)
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register(
|
||||||
|
"reserve",
|
||||||
|
param_types=[ir.IntType(64)],
|
||||||
|
return_type=ir.PointerType(ir.IntType(8)),
|
||||||
|
)
|
||||||
|
def bpf_ringbuf_reserve_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_ringbuf_reserve helper function call.
|
||||||
|
Expected call signature: ringbuf.reserve(size)
|
||||||
|
"""
|
||||||
|
|
||||||
|
if len(call.args) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
f"ringbuf.reserve expects exactly one argument (size), got {len(call.args)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
size_val = get_int_value_from_arg(
|
||||||
|
call.args[0],
|
||||||
|
func,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
)
|
||||||
|
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
[ir.PointerType(), ir.IntType(64)],
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_RINGBUF_RESERVE.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
result = builder.call(fn_ptr, [map_void_ptr, size_val], tail=False)
|
||||||
|
|
||||||
|
return result, ir.PointerType(ir.IntType(8))
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register(
|
||||||
|
"submit",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(8)), ir.IntType(64)],
|
||||||
|
return_type=ir.VoidType(),
|
||||||
|
)
|
||||||
|
def bpf_ringbuf_submit_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_ringbuf_submit helper function call.
|
||||||
|
Expected call signature: ringbuf.submit(data, flags=0)
|
||||||
|
"""
|
||||||
|
|
||||||
|
if len(call.args) not in (1, 2):
|
||||||
|
raise ValueError(
|
||||||
|
f"ringbuf.submit expects 1 or 2 args (data, flags), got {len(call.args)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
data_arg = call.args[0]
|
||||||
|
flags_arg = call.args[1] if len(call.args) == 2 else None
|
||||||
|
|
||||||
|
data_ptr = get_or_create_ptr_from_arg(
|
||||||
|
func,
|
||||||
|
module,
|
||||||
|
data_arg,
|
||||||
|
builder,
|
||||||
|
local_sym_tab,
|
||||||
|
map_sym_tab,
|
||||||
|
struct_sym_tab,
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
)
|
||||||
|
|
||||||
|
flags_const = get_flags_val(flags_arg, builder, local_sym_tab)
|
||||||
|
if isinstance(flags_const, int):
|
||||||
|
flags_const = ir.Constant(ir.IntType(64), flags_const)
|
||||||
|
|
||||||
|
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.VoidType(),
|
||||||
|
[ir.PointerType(), ir.PointerType(), ir.IntType(64)],
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_RINGBUF_SUBMIT.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
|
||||||
|
result = builder.call(fn_ptr, [map_void_ptr, data_ptr, flags_const], tail=False)
|
||||||
|
|
||||||
|
return result, None
|
||||||
|
|
||||||
|
|
||||||
|
@HelperHandlerRegistry.register(
|
||||||
|
"get_stack",
|
||||||
|
param_types=[ir.PointerType(ir.IntType(8)), ir.IntType(64)],
|
||||||
|
return_type=ir.IntType(64),
|
||||||
|
)
|
||||||
|
def bpf_get_stack_emitter(
|
||||||
|
call,
|
||||||
|
map_ptr,
|
||||||
|
module,
|
||||||
|
builder,
|
||||||
|
func,
|
||||||
|
local_sym_tab=None,
|
||||||
|
struct_sym_tab=None,
|
||||||
|
map_sym_tab=None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Emit LLVM IR for bpf_get_stack helper function call.
|
||||||
|
"""
|
||||||
|
if len(call.args) not in (1, 2):
|
||||||
|
raise ValueError(
|
||||||
|
f"get_stack expects atmost two arguments (buf, flags), got {len(call.args)}"
|
||||||
|
)
|
||||||
|
ctx_ptr = func.args[0] # First argument to the function is ctx
|
||||||
|
buf_arg = call.args[0]
|
||||||
|
flags_arg = call.args[1] if len(call.args) == 2 else None
|
||||||
|
buf_ptr, buf_size = get_buffer_ptr_and_size(
|
||||||
|
buf_arg, builder, local_sym_tab, struct_sym_tab
|
||||||
|
)
|
||||||
|
flags_val = get_flags_val(flags_arg, builder, local_sym_tab)
|
||||||
|
if isinstance(flags_val, int):
|
||||||
|
flags_val = ir.Constant(ir.IntType(64), flags_val)
|
||||||
|
|
||||||
|
buf_void_ptr = builder.bitcast(buf_ptr, ir.PointerType())
|
||||||
|
fn_type = ir.FunctionType(
|
||||||
|
ir.IntType(64),
|
||||||
|
[
|
||||||
|
ir.PointerType(ir.IntType(8)),
|
||||||
|
ir.PointerType(),
|
||||||
|
ir.IntType(64),
|
||||||
|
ir.IntType(64),
|
||||||
|
],
|
||||||
|
var_arg=False,
|
||||||
|
)
|
||||||
|
fn_ptr_type = ir.PointerType(fn_type)
|
||||||
|
fn_addr = ir.Constant(ir.IntType(64), BPFHelperID.BPF_GET_STACK.value)
|
||||||
|
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
|
||||||
|
result = builder.call(
|
||||||
|
fn_ptr,
|
||||||
|
[ctx_ptr, buf_void_ptr, ir.Constant(ir.IntType(64), buf_size), flags_val],
|
||||||
|
tail=False,
|
||||||
|
)
|
||||||
|
return result, ir.IntType(64)
|
||||||
|
|
||||||
|
|
||||||
def handle_helper_call(
|
def handle_helper_call(
|
||||||
call,
|
call,
|
||||||
module,
|
module,
|
||||||
@ -790,6 +1052,6 @@ def handle_helper_call(
|
|||||||
if not map_sym_tab or map_name not in map_sym_tab:
|
if not map_sym_tab or map_name not in map_sym_tab:
|
||||||
raise ValueError(f"Map '{map_name}' not found in symbol table")
|
raise ValueError(f"Map '{map_name}' not found in symbol table")
|
||||||
|
|
||||||
return invoke_helper(method_name, map_sym_tab[map_name])
|
return invoke_helper(method_name, map_sym_tab[map_name].sym)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|||||||
@ -287,7 +287,10 @@ def get_char_array_ptr_and_size(buf_arg, builder, local_sym_tab, struct_sym_tab)
|
|||||||
|
|
||||||
field_type = struct_info.field_type(field_name)
|
field_type = struct_info.field_type(field_name)
|
||||||
if not _is_char_array(field_type):
|
if not _is_char_array(field_type):
|
||||||
raise ValueError("Expected char array field")
|
logger.info(
|
||||||
|
"Field is not a char array, falling back to int or ptr detection"
|
||||||
|
)
|
||||||
|
return None, 0
|
||||||
|
|
||||||
struct_ptr = local_sym_tab[var_name].var
|
struct_ptr = local_sym_tab[var_name].var
|
||||||
field_ptr = struct_info.gep(builder, struct_ptr, field_name)
|
field_ptr = struct_info.gep(builder, struct_ptr, field_name)
|
||||||
|
|||||||
@ -52,6 +52,11 @@ def skb_store_bytes(offset, from_buf, size, flags=0):
|
|||||||
return ctypes.c_int64(0)
|
return ctypes.c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
def get_stack(buf, flags=0):
|
||||||
|
"""get the current stack trace"""
|
||||||
|
return ctypes.c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
XDP_ABORTED = ctypes.c_int64(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)
|
||||||
|
|||||||
@ -220,6 +220,7 @@ def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_ta
|
|||||||
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
"""Evaluate and prepare an expression to use as an arg for bpf_printk."""
|
||||||
|
|
||||||
# Special case: struct field char array needs pointer to first element
|
# Special case: struct field char array needs pointer to first element
|
||||||
|
if isinstance(expr, ast.Attribute):
|
||||||
char_array_ptr, _ = get_char_array_ptr_and_size(
|
char_array_ptr, _ = get_char_array_ptr_and_size(
|
||||||
expr, builder, local_sym_tab, struct_sym_tab
|
expr, builder, local_sym_tab, struct_sym_tab
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
from .maps import HashMap, PerfEventArray, RingBuf
|
from .maps import HashMap, PerfEventArray, RingBuffer
|
||||||
from .maps_pass import maps_proc
|
from .maps_pass import maps_proc
|
||||||
|
from .map_types import BPFMapType
|
||||||
|
|
||||||
__all__ = ["HashMap", "PerfEventArray", "maps_proc", "RingBuf"]
|
__all__ = ["HashMap", "PerfEventArray", "maps_proc", "RingBuffer", "BPFMapType"]
|
||||||
|
|||||||
@ -2,7 +2,7 @@ from pythonbpf.debuginfo import DebugInfoGenerator
|
|||||||
from .map_types import BPFMapType
|
from .map_types import BPFMapType
|
||||||
|
|
||||||
|
|
||||||
def create_map_debug_info(module, map_global, map_name, map_params):
|
def create_map_debug_info(module, map_global, map_name, map_params, structs_sym_tab):
|
||||||
"""Generate debug info metadata for BPF maps HASH and PERF_EVENT_ARRAY"""
|
"""Generate debug info metadata for BPF maps HASH and PERF_EVENT_ARRAY"""
|
||||||
generator = DebugInfoGenerator(module)
|
generator = DebugInfoGenerator(module)
|
||||||
|
|
||||||
@ -64,7 +64,13 @@ def create_map_debug_info(module, map_global, map_name, map_params):
|
|||||||
return global_var
|
return global_var
|
||||||
|
|
||||||
|
|
||||||
def create_ringbuf_debug_info(module, map_global, map_name, map_params):
|
# TODO: This should not be exposed outside of the module.
|
||||||
|
# Ideally we should expose a single create_map_debug_info function that handles all map types.
|
||||||
|
# We can probably use a registry pattern to register different map types and their debug info generators.
|
||||||
|
# map_params["type"] will be used to determine which generator to use.
|
||||||
|
def create_ringbuf_debug_info(
|
||||||
|
module, map_global, map_name, map_params, structs_sym_tab
|
||||||
|
):
|
||||||
"""Generate debug information metadata for BPF RINGBUF map"""
|
"""Generate debug information metadata for BPF RINGBUF map"""
|
||||||
generator = DebugInfoGenerator(module)
|
generator = DebugInfoGenerator(module)
|
||||||
|
|
||||||
|
|||||||
@ -36,11 +36,14 @@ class PerfEventArray:
|
|||||||
pass # Placeholder for output method
|
pass # Placeholder for output method
|
||||||
|
|
||||||
|
|
||||||
class RingBuf:
|
class RingBuffer:
|
||||||
def __init__(self, max_entries):
|
def __init__(self, max_entries):
|
||||||
self.max_entries = max_entries
|
self.max_entries = max_entries
|
||||||
|
|
||||||
def reserve(self, size: int, flags=0):
|
def output(self, data, flags=0):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def reserve(self, size: int):
|
||||||
if size > self.max_entries:
|
if size > self.max_entries:
|
||||||
raise ValueError("size cannot be greater than set maximum entries")
|
raise ValueError("size cannot be greater than set maximum entries")
|
||||||
return 0
|
return 0
|
||||||
@ -48,4 +51,7 @@ class RingBuf:
|
|||||||
def submit(self, data, flags=0):
|
def submit(self, data, flags=0):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def discard(self, data, flags=0):
|
||||||
|
pass
|
||||||
|
|
||||||
# add discard, output and also give names to flags and stuff
|
# add discard, output and also give names to flags and stuff
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import logging
|
|||||||
from logging import Logger
|
from logging import Logger
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
|
|
||||||
from .maps_utils import MapProcessorRegistry
|
from .maps_utils import MapProcessorRegistry, MapSymbol
|
||||||
from .map_types import BPFMapType
|
from .map_types import BPFMapType
|
||||||
from .map_debug_info import create_map_debug_info, create_ringbuf_debug_info
|
from .map_debug_info import create_map_debug_info, create_ringbuf_debug_info
|
||||||
from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
|
from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
|
||||||
@ -12,13 +12,15 @@ from pythonbpf.expr.vmlinux_registry import VmlinuxHandlerRegistry
|
|||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def maps_proc(tree, module, chunks):
|
def maps_proc(tree, module, chunks, structs_sym_tab):
|
||||||
"""Process all functions decorated with @map to find BPF maps"""
|
"""Process all functions decorated with @map to find BPF maps"""
|
||||||
map_sym_tab = {}
|
map_sym_tab = {}
|
||||||
for func_node in chunks:
|
for func_node in chunks:
|
||||||
if is_map(func_node):
|
if is_map(func_node):
|
||||||
logger.info(f"Found BPF map: {func_node.name}")
|
logger.info(f"Found BPF map: {func_node.name}")
|
||||||
map_sym_tab[func_node.name] = process_bpf_map(func_node, module)
|
map_sym_tab[func_node.name] = process_bpf_map(
|
||||||
|
func_node, module, structs_sym_tab
|
||||||
|
)
|
||||||
return map_sym_tab
|
return map_sym_tab
|
||||||
|
|
||||||
|
|
||||||
@ -46,7 +48,7 @@ def create_bpf_map(module, map_name, map_params):
|
|||||||
map_global.align = 8
|
map_global.align = 8
|
||||||
|
|
||||||
logger.info(f"Created BPF map: {map_name} with params {map_params}")
|
logger.info(f"Created BPF map: {map_name} with params {map_params}")
|
||||||
return map_global
|
return MapSymbol(type=map_params["type"], sym=map_global)
|
||||||
|
|
||||||
|
|
||||||
def _parse_map_params(rval, expected_args=None):
|
def _parse_map_params(rval, expected_args=None):
|
||||||
@ -60,7 +62,8 @@ def _parse_map_params(rval, expected_args=None):
|
|||||||
if i < len(rval.args):
|
if i < len(rval.args):
|
||||||
arg = rval.args[i]
|
arg = rval.args[i]
|
||||||
if isinstance(arg, ast.Name):
|
if isinstance(arg, ast.Name):
|
||||||
params[arg_name] = arg.id
|
result = _get_vmlinux_enum(handler, arg.id)
|
||||||
|
params[arg_name] = result if result is not None else arg.id
|
||||||
elif isinstance(arg, ast.Constant):
|
elif isinstance(arg, ast.Constant):
|
||||||
params[arg_name] = arg.value
|
params[arg_name] = arg.value
|
||||||
|
|
||||||
@ -68,33 +71,46 @@ def _parse_map_params(rval, expected_args=None):
|
|||||||
for keyword in rval.keywords:
|
for keyword in rval.keywords:
|
||||||
if isinstance(keyword.value, ast.Name):
|
if isinstance(keyword.value, ast.Name):
|
||||||
name = keyword.value.id
|
name = keyword.value.id
|
||||||
if handler and handler.is_vmlinux_enum(name):
|
result = _get_vmlinux_enum(handler, name)
|
||||||
result = handler.get_vmlinux_enum_value(name)
|
|
||||||
params[keyword.arg] = result if result is not None else name
|
params[keyword.arg] = result if result is not None else name
|
||||||
else:
|
|
||||||
params[keyword.arg] = name
|
|
||||||
elif isinstance(keyword.value, ast.Constant):
|
elif isinstance(keyword.value, ast.Constant):
|
||||||
params[keyword.arg] = keyword.value.value
|
params[keyword.arg] = keyword.value.value
|
||||||
|
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
@MapProcessorRegistry.register("RingBuf")
|
def _get_vmlinux_enum(handler, name):
|
||||||
def process_ringbuf_map(map_name, rval, module):
|
if handler and handler.is_vmlinux_enum(name):
|
||||||
|
return handler.get_vmlinux_enum_value(name)
|
||||||
|
|
||||||
|
|
||||||
|
@MapProcessorRegistry.register("RingBuffer")
|
||||||
|
def process_ringbuf_map(map_name, rval, module, structs_sym_tab):
|
||||||
"""Process a BPF_RINGBUF map declaration"""
|
"""Process a BPF_RINGBUF map declaration"""
|
||||||
logger.info(f"Processing Ringbuf: {map_name}")
|
logger.info(f"Processing Ringbuf: {map_name}")
|
||||||
map_params = _parse_map_params(rval, expected_args=["max_entries"])
|
map_params = _parse_map_params(rval, expected_args=["max_entries"])
|
||||||
map_params["type"] = BPFMapType.RINGBUF
|
map_params["type"] = BPFMapType.RINGBUF
|
||||||
|
|
||||||
|
# NOTE: constraints borrowed from https://docs.ebpf.io/linux/map-type/BPF_MAP_TYPE_RINGBUF/
|
||||||
|
max_entries = map_params.get("max_entries")
|
||||||
|
if (
|
||||||
|
not isinstance(max_entries, int)
|
||||||
|
or max_entries < 4096
|
||||||
|
or (max_entries & (max_entries - 1)) != 0
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"Ringbuf max_entries must be a power of two greater than or equal to the page size (4096)"
|
||||||
|
)
|
||||||
|
|
||||||
logger.info(f"Ringbuf map parameters: {map_params}")
|
logger.info(f"Ringbuf map parameters: {map_params}")
|
||||||
|
|
||||||
map_global = create_bpf_map(module, map_name, map_params)
|
map_global = create_bpf_map(module, map_name, map_params)
|
||||||
create_ringbuf_debug_info(module, map_global, map_name, map_params)
|
create_ringbuf_debug_info(module, map_global.sym, map_name, map_params)
|
||||||
return map_global
|
return map_global
|
||||||
|
|
||||||
|
|
||||||
@MapProcessorRegistry.register("HashMap")
|
@MapProcessorRegistry.register("HashMap")
|
||||||
def process_hash_map(map_name, rval, module):
|
def process_hash_map(map_name, rval, module, structs_sym_tab):
|
||||||
"""Process a BPF_HASH map declaration"""
|
"""Process a BPF_HASH map declaration"""
|
||||||
logger.info(f"Processing HashMap: {map_name}")
|
logger.info(f"Processing HashMap: {map_name}")
|
||||||
map_params = _parse_map_params(rval, expected_args=["key", "value", "max_entries"])
|
map_params = _parse_map_params(rval, expected_args=["key", "value", "max_entries"])
|
||||||
@ -103,12 +119,12 @@ def process_hash_map(map_name, rval, module):
|
|||||||
logger.info(f"Map parameters: {map_params}")
|
logger.info(f"Map parameters: {map_params}")
|
||||||
map_global = create_bpf_map(module, map_name, map_params)
|
map_global = create_bpf_map(module, map_name, map_params)
|
||||||
# Generate debug info for BTF
|
# Generate debug info for BTF
|
||||||
create_map_debug_info(module, map_global, map_name, map_params)
|
create_map_debug_info(module, map_global.sym, map_name, map_params)
|
||||||
return map_global
|
return map_global
|
||||||
|
|
||||||
|
|
||||||
@MapProcessorRegistry.register("PerfEventArray")
|
@MapProcessorRegistry.register("PerfEventArray")
|
||||||
def process_perf_event_map(map_name, rval, module):
|
def process_perf_event_map(map_name, rval, module, structs_sym_tab):
|
||||||
"""Process a BPF_PERF_EVENT_ARRAY map declaration"""
|
"""Process a BPF_PERF_EVENT_ARRAY map declaration"""
|
||||||
logger.info(f"Processing PerfEventArray: {map_name}")
|
logger.info(f"Processing PerfEventArray: {map_name}")
|
||||||
map_params = _parse_map_params(rval, expected_args=["key_size", "value_size"])
|
map_params = _parse_map_params(rval, expected_args=["key_size", "value_size"])
|
||||||
@ -117,11 +133,11 @@ def process_perf_event_map(map_name, rval, module):
|
|||||||
logger.info(f"Map parameters: {map_params}")
|
logger.info(f"Map parameters: {map_params}")
|
||||||
map_global = create_bpf_map(module, map_name, map_params)
|
map_global = create_bpf_map(module, map_name, map_params)
|
||||||
# Generate debug info for BTF
|
# Generate debug info for BTF
|
||||||
create_map_debug_info(module, map_global, map_name, map_params)
|
create_map_debug_info(module, map_global.sym, map_name, map_params)
|
||||||
return map_global
|
return map_global
|
||||||
|
|
||||||
|
|
||||||
def process_bpf_map(func_node, module):
|
def process_bpf_map(func_node, module, structs_sym_tab):
|
||||||
"""Process a BPF map (a function decorated with @map)"""
|
"""Process a BPF map (a function decorated with @map)"""
|
||||||
map_name = func_node.name
|
map_name = func_node.name
|
||||||
logger.info(f"Processing BPF map: {map_name}")
|
logger.info(f"Processing BPF map: {map_name}")
|
||||||
@ -140,7 +156,7 @@ def process_bpf_map(func_node, module):
|
|||||||
if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name):
|
if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name):
|
||||||
handler = MapProcessorRegistry.get_processor(rval.func.id)
|
handler = MapProcessorRegistry.get_processor(rval.func.id)
|
||||||
if handler:
|
if handler:
|
||||||
return handler(map_name, rval, module)
|
return handler(map_name, rval, module, structs_sym_tab)
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Unknown map type {rval.func.id}, defaulting to HashMap")
|
logger.warning(f"Unknown map type {rval.func.id}, defaulting to HashMap")
|
||||||
return process_hash_map(map_name, rval, module)
|
return process_hash_map(map_name, rval, module)
|
||||||
|
|||||||
@ -1,5 +1,16 @@
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from llvmlite import ir
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from .map_types import BPFMapType
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MapSymbol:
|
||||||
|
"""Class representing a symbol on the map"""
|
||||||
|
|
||||||
|
type: BPFMapType
|
||||||
|
sym: ir.GlobalVariable
|
||||||
|
|
||||||
|
|
||||||
class MapProcessorRegistry:
|
class MapProcessorRegistry:
|
||||||
|
|||||||
Reference in New Issue
Block a user