Skip to content

Commit f7aa054

Browse files
get hashmap compiling
1 parent 1a82875 commit f7aa054

3 files changed

Lines changed: 56 additions & 50 deletions

File tree

examples/c-form/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ all: $(LL) $(OBJ)
1313
$(BPF_CLANG) -O2 -g -target bpf -c $< -o $@
1414

1515
%.bpf.ll: %.bpf.c
16-
$(BPF_CLANG) $(CFLAGS) -S $< -o $@
16+
$(BPF_CLANG) $(CFLAGS) -g -S $< -o $@
1717

1818
clean:
1919
rm -f $(LL) $(OBJ)

examples/c-form/ex2.bpf.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#include <linux/bpf.h>
22
#include <bpf/bpf_helpers.h>
33
#define u64 unsigned long long
4+
#define u32 unsigned int
45

56
struct {
67
__uint(type, BPF_MAP_TYPE_HASH);
78
__uint(max_entries, 1);
8-
__type(key, u64);
9+
__type(key, u32);
910
__type(value, u64);
1011
} last SEC(".maps");
1112

13+
struct {
14+
__uint(type, BPF_MAP_TYPE_HASH);
15+
__uint(max_entries, 1);
16+
__type(key, u64);
17+
__type(value, u64);
18+
} last2 SEC(".maps");
19+
1220
SEC("tracepoint/syscalls/sys_enter_execve")
1321
int hello(struct pt_regs *ctx) {
1422
bpf_printk("Hello, World!\n");

pythonbpf/maps_pass.py

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,56 @@ def maps_proc(tree, module, chunks):
1818

1919
def create_bpf_map(module, map_name, map_params):
2020
"""Create a BPF map in the module with the given parameters"""
21-
22-
key_type_str = map_params.get('key_type', 'c_uint32')
23-
value_type_str = map_params.get('value_type', 'c_uint32')
24-
25-
key_type = ctypes_to_ir(key_type_str)
26-
value_type = ctypes_to_ir(value_type_str)
27-
21+
22+
# Create the anonymous struct type for BPF map
2823
map_struct_type = ir.LiteralStructType([
29-
ir.PointerType(), # type
30-
ir.PointerType(), # max_entries
31-
ir.PointerType(), # key_type
32-
ir.PointerType() # value_type
24+
ir.PointerType(),
25+
ir.PointerType(),
26+
ir.PointerType(),
27+
ir.PointerType()
3328
])
34-
29+
30+
# Create the global variable
3531
map_global = ir.GlobalVariable(module, map_struct_type, name=map_name)
36-
map_global.linkage = 'external'
37-
map_global.initializer = ir.Constant( # type: ignore
38-
map_struct_type, [None, None, None, None])
32+
map_global.linkage = 'dso_local'
33+
map_global.global_constant = False
34+
35+
# Initialize with zeroinitializer (all null pointers)
36+
map_global.initializer = ir.Constant(map_struct_type, None) #type: ignore
37+
3938
map_global.section = ".maps"
40-
map_global.align = 8 # type: ignore
41-
42-
# TODO: Store map parameters in metadata or a suitable structure
43-
# maps[map_name] = {
44-
# 'global': map_global,
45-
# 'key_type': key_type,
46-
# 'value_type': value_type,
47-
# 'max_entries': map_params.get('max_entries', 1),
48-
# 'map_type': map_params.get('map_type', 'BPF_MAP_TYPE_HASH')
49-
# }
39+
map_global.align = 8 # type: ignore
5040

5141
print(f"Created BPF map: {map_name}")
5242
return map_global
5343

44+
def process_hash_map(map_name, rval, module):
45+
print(f"Creating HashMap map: {map_name}")
46+
map_params: dict[str, object] = {"map_type": "HASH"}
47+
48+
# Assuming order: key_type, value_type, max_entries
49+
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):
50+
map_params["key_type"] = rval.args[0].id
51+
if len(rval.args) >= 2 and isinstance(rval.args[1], ast.Name):
52+
map_params["value_type"] = rval.args[1].id
53+
if len(rval.args) >= 3 and isinstance(rval.args[2], ast.Constant):
54+
const_val = rval.args[2].value
55+
if isinstance(const_val, (int, str)): # safe check
56+
map_params["max_entries"] = const_val
57+
58+
for keyword in rval.keywords:
59+
if keyword.arg == "key_type" and isinstance(keyword.value, ast.Name):
60+
map_params["key_type"] = keyword.value.id
61+
elif keyword.arg == "value_type" and isinstance(keyword.value, ast.Name):
62+
map_params["value_type"] = keyword.value.id
63+
elif keyword.arg == "max_entries" and isinstance(keyword.value, ast.Constant):
64+
const_val = keyword.value.value
65+
if isinstance(const_val, (int, str)):
66+
map_params["max_entries"] = const_val
67+
68+
print(f"Map parameters: {map_params}")
69+
return create_bpf_map(module, map_name, map_params)
70+
5471

5572
def process_bpf_map(func_node, module):
5673
"""Process a BPF map (a function decorated with @map)"""
@@ -68,27 +85,8 @@ def process_bpf_map(func_node, module):
6885

6986
rval = return_stmt.value
7087

71-
# For now, just handle maps
88+
# Handle only HashMap maps
7289
if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name) and rval.func.id == "HashMap":
73-
print(f"Creating HashMap map: {map_name}")
74-
map_params = {'map_type': 'HASH'}
75-
# Handle positional arguments
76-
if rval.args:
77-
# Assuming order is: key_type, value_type, max_entries
78-
if len(rval.args) >= 1 and isinstance(rval.args[0], ast.Name):
79-
map_params['key_type'] = rval.args[0].id
80-
if len(rval.args) >= 2 and isinstance(rval.args[1], ast.Name):
81-
map_params['value_type'] = rval.args[1].id
82-
if len(rval.args) >= 3 and isinstance(rval.args[2], ast.Constant):
83-
map_params['max_entries'] = rval.args[2].value
84-
85-
# Handle keyword arguments (these will override any positional args)
86-
for keyword in rval.keywords:
87-
if keyword.arg == "key_type" and isinstance(keyword.value, ast.Name):
88-
map_params['key_type'] = keyword.value.id
89-
elif keyword.arg == "value_type" and isinstance(keyword.value, ast.Name):
90-
map_params['value_type'] = keyword.value.id
91-
elif keyword.arg == "max_entries" and isinstance(keyword.value, ast.Constant):
92-
map_params['max_entries'] = keyword.value.value
93-
print(f"Map parameters: {map_params}")
94-
print(create_bpf_map(module, map_name, map_params))
90+
process_hash_map(map_name, rval, module)
91+
else:
92+
raise ValueError("Function under @map must return a map")

0 commit comments

Comments
 (0)