Skip to content

Commit ff76638

Browse files
committed
implement bpf map lookup emission
1 parent 414db12 commit ff76638

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

pythonbpf/bpf_helper_handler.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ def bpf_ktime_get_ns_emitter(call, module, builder, func):
66
pass
77

88

9+
def bpf_map_lookup_elem_emitter(map_ptr, key_ptr, module, builder):
10+
"""
11+
Emit LLVM IR for bpf_map_lookup_elem helper function call.
12+
"""
13+
# Cast pointers to void*
14+
map_void_ptr = builder.bitcast(map_ptr, ir.PointerType())
15+
16+
# Define function type for bpf_map_lookup_elem
17+
# The function takes two void* arguments and returns void*
18+
fn_type = ir.FunctionType(
19+
ir.PointerType(), # Return type: void*
20+
[ir.PointerType(), ir.PointerType()], # Args: (void*, void*)
21+
var_arg=False
22+
)
23+
fn_ptr_type = ir.PointerType(fn_type)
24+
25+
# Helper ID 1 is bpf_map_lookup_elem
26+
fn_addr = ir.Constant(ir.IntType(64), 1)
27+
fn_ptr = builder.inttoptr(fn_addr, fn_ptr_type)
28+
29+
# Call the helper function
30+
result = builder.call(fn_ptr, [map_void_ptr, key_ptr], tail=False)
31+
32+
return result
33+
34+
935
def bpf_printk_emitter(call, module, builder, func):
1036
if not hasattr(func, "_fmt_counter"):
1137
func._fmt_counter = 0

pythonbpf/functions_pass.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from llvmlite import ir
22
import ast
33

4-
from .bpf_helper_handler import bpf_printk_emitter, bpf_ktime_get_ns_emitter
4+
from .bpf_helper_handler import bpf_printk_emitter, bpf_ktime_get_ns_emitter, bpf_map_lookup_elem_emitter
55
from .type_deducer import ctypes_to_ir
66

77

@@ -79,28 +79,31 @@ def handle_assign(module, builder, stmt, map_sym_tab, local_sym_tab):
7979
key_type = ir.IntType(64)
8080
print(f"Key type: {key_type}")
8181
print(f"Key val: {key_val}")
82+
key_var = builder.alloca(key_type)
83+
key_var.align = key_type // 8
84+
builder.store(ir.Constant(
85+
key_type, key_val), key_var)
8286
elif isinstance(key_arg, ast.Name):
8387
# Check in local symtab first
8488
if key_arg.id in local_sym_tab:
8589
key_var = local_sym_tab[key_arg.id]
8690
key_type = key_var.type.pointee
87-
key_val = builder.load(key_var)
8891
elif key_arg.id in map_sym_tab:
8992
key_var = map_sym_tab[key_arg.id]
9093
key_type = key_var.type.pointee
91-
key_val = builder.load(key_var)
9294
else:
9395
print("Key variable "
9496
f"{key_arg.id} not found in symtabs")
9597
return
9698
print(f"Found key variable {key_arg.id} in symtab")
9799
print(f"Key type: {key_type}")
98-
print(f"Key val: {key_val}")
99100
else:
100101
print("Unsupported lookup key arg")
101102
return
102103

103104
# TODO: generate call to bpf_map_lookup_elem
105+
result_ptr = bpf_map_lookup_elem_emitter(
106+
map_global, key_var, module, builder)
104107

105108
else:
106109
print(f"Map {map_name} not found in symbol table")

0 commit comments

Comments
 (0)