11import ast
22from llvmlite import ir
3- from pythonbpf .type_deducer import ctypes_to_ir
43from pythonbpf import dwarf_constants as dc
54from enum import Enum
65from .maps_utils import MapProcessorRegistry
76
8- map_sym_tab = {}
9-
107
118def maps_proc (tree , module , chunks ):
9+ """ Process all functions decorated with @map to find BPF maps """
10+ map_sym_tab = {}
1211 for func_node in chunks :
1312 if is_map (func_node ):
1413 print (f"Found BPF map: { func_node .name } " )
15- process_bpf_map (func_node , module )
16- continue
14+ map_sym_tab [func_node .name ] = process_bpf_map (func_node , module )
1715 return map_sym_tab
1816
1917
@@ -30,9 +28,7 @@ class BPFMapType(Enum):
3028
3129
3230def create_bpf_map (module , map_name , map_params ):
33- """Create a BPF map in the module with the given parameters and debug info"""
34-
35- map_type = map_params .get ("type" , BPFMapType .HASH ).value
31+ """Create a BPF map in the module with given parameters and debug info"""
3632
3733 # Create the anonymous struct type for BPF map
3834 map_struct_type = ir .LiteralStructType (
@@ -43,15 +39,14 @@ def create_bpf_map(module, map_name, map_params):
4339 map_global .linkage = 'dso_local'
4440 map_global .global_constant = False
4541 map_global .initializer = ir .Constant (
46- map_struct_type , None ) # type: ignore
42+ map_struct_type , None )
4743 map_global .section = ".maps"
48- map_global .align = 8 # type: ignore
44+ map_global .align = 8
4945
5046 # Generate debug info for BTF
5147 create_map_debug_info (module , map_global , map_name , map_params )
5248
5349 print (f"Created BPF map: { map_name } " )
54- map_sym_tab [map_name ] = map_global
5550 return map_global
5651
5752
@@ -186,6 +181,7 @@ def create_map_debug_info(module, map_global, map_name, map_params):
186181
187182@MapProcessorRegistry .register ("HashMap" )
188183def process_hash_map (map_name , rval , module ):
184+ """Process a BPF_HASH map declaration"""
189185 print (f"Creating HashMap map: { map_name } " )
190186 map_params = {"type" : BPFMapType .HASH }
191187
@@ -204,7 +200,8 @@ def process_hash_map(map_name, rval, module):
204200 map_params ["key" ] = keyword .value .id
205201 elif keyword .arg == "value" and isinstance (keyword .value , ast .Name ):
206202 map_params ["value" ] = keyword .value .id
207- elif keyword .arg == "max_entries" and isinstance (keyword .value , ast .Constant ):
203+ elif keyword .arg == "max_entries" and \
204+ isinstance (keyword .value , ast .Constant ):
208205 const_val = keyword .value .value
209206 if isinstance (const_val , (int , str )):
210207 map_params ["max_entries" ] = const_val
@@ -215,6 +212,7 @@ def process_hash_map(map_name, rval, module):
215212
216213@MapProcessorRegistry .register ("PerfEventArray" )
217214def process_perf_event_map (map_name , rval , module ):
215+ """Process a BPF_PERF_EVENT_ARRAY map declaration"""
218216 print (f"Creating PerfEventArray map: { map_name } " )
219217 map_params = {"type" : BPFMapType .PERF_EVENT_ARRAY }
220218
@@ -226,7 +224,8 @@ def process_perf_event_map(map_name, rval, module):
226224 for keyword in rval .keywords :
227225 if keyword .arg == "key_size" and isinstance (keyword .value , ast .Name ):
228226 map_params ["key_size" ] = keyword .value .id
229- elif keyword .arg == "value_size" and isinstance (keyword .value , ast .Name ):
227+ elif keyword .arg == "value_size" and \
228+ isinstance (keyword .value , ast .Name ):
230229 map_params ["value_size" ] = keyword .value .id
231230
232231 print (f"Map parameters: { map_params } " )
@@ -249,11 +248,10 @@ def process_bpf_map(func_node, module):
249248
250249 rval = return_stmt .value
251250
252- # Handle only HashMap maps
253251 if isinstance (rval , ast .Call ) and isinstance (rval .func , ast .Name ):
254252 handler = MapProcessorRegistry .get_processor (rval .func .id )
255253 if handler :
256- handler (map_name , rval , module )
254+ return handler (map_name , rval , module )
257255 else :
258256 print (f"Unknown map type { rval .func .id } , defaulting to HashMap" )
259257 process_hash_map (map_name , rval , module )
0 commit comments