Skip to content

Commit 2d43949

Browse files
committed
Fix ring buffer and map setup code to accept customizable object variable names.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 177a9e3 commit 2d43949

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

src/userspace_codegen.ml

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,18 +2673,18 @@ static int %s_event_handler(void *ctx, void *data, size_t data_sz) {
26732673
final_event_handlers ^ combined_rb_declaration
26742674

26752675
(** Generate ring buffer setup code from centralized registry *)
2676-
let generate_ringbuf_setup_code_from_registry (registry : Ir.ir_ring_buffer_registry) ~dispatch_used =
2676+
let generate_ringbuf_setup_code_from_registry ?(obj_var="obj->obj") (registry : Ir.ir_ring_buffer_registry) ~dispatch_used =
26772677
if List.length registry.ring_buffer_declarations = 0 then ""
26782678
else
26792679
let fd_setup_code = List.map (fun rb_decl ->
26802680
let ringbuf_name = rb_decl.rb_name in
26812681
sprintf {| // Get ring buffer map FD for %s
2682-
int %s_map_fd = bpf_object__find_map_fd_by_name(obj->obj, "%s");
2682+
int %s_map_fd = bpf_object__find_map_fd_by_name(%s, "%s");
26832683
if (%s_map_fd < 0) {
26842684
fprintf(stderr, "Failed to find %s ring buffer map\n");
26852685
return 1;
26862686
}|}
2687-
ringbuf_name ringbuf_name ringbuf_name ringbuf_name ringbuf_name
2687+
ringbuf_name ringbuf_name obj_var ringbuf_name ringbuf_name ringbuf_name
26882688
) registry.ring_buffer_declarations in
26892689

26902690
let combined_rb_setup = if List.length registry.ring_buffer_declarations > 0 && dispatch_used then
@@ -2781,7 +2781,7 @@ int %s_get_next_key(%s *key, %s *next_key) {
27812781
String.concat "\n" (regular_map_ops @ [ringbuf_handlers])
27822782

27832783
(** Generate unified map setup code - handle both regular and pinned maps *)
2784-
let generate_unified_map_setup_code maps =
2784+
let generate_unified_map_setup_code ?(obj_var="obj->obj") maps =
27852785
(* Remove duplicates first *)
27862786
let deduplicated_maps = List.fold_left (fun acc map ->
27872787
if List.exists (fun existing -> existing.map_name = map.map_name) acc
@@ -2812,15 +2812,15 @@ let generate_unified_map_setup_code maps =
28122812
%s_fd = bpf_map__fd(%s_map);|} map.map_name map.map_name
28132813
in
28142814
Printf.sprintf {| // Load map %s from eBPF object
2815-
struct bpf_map *%s_map = bpf_object__find_map_by_name(obj->obj, "%s");
2815+
struct bpf_map *%s_map = bpf_object__find_map_by_name(%s, "%s");
28162816
if (!%s_map) {
28172817
fprintf(stderr, "Failed to find %s map in eBPF object\n");
28182818
return 1;
28192819
}%s
28202820
if (%s_fd < 0) {
28212821
fprintf(stderr, "Failed to get fd for %s map\n");
28222822
return 1;
2823-
}|} map.map_name map.map_name map.map_name map.map_name map.map_name pin_logic map.map_name map.map_name
2823+
}|} map.map_name map.map_name obj_var map.map_name map.map_name map.map_name pin_logic map.map_name map.map_name
28242824
) deduplicated_maps |> String.concat "\n"
28252825

28262826
(** Generate config struct definition from config declaration - reusing eBPF logic *)
@@ -3372,19 +3372,22 @@ let generate_complete_userspace_program_from_ir ?(config_declarations = []) ?(ty
33723372
generate_pinned_globals_support project_name ir_multi_prog.global_variables in
33733373

33743374
(* Generate config map setup code - load from eBPF object and initialize with defaults *)
3375-
let config_setup_code = if List.length config_declarations > 0 then
3376-
List.map (fun config_decl ->
3377-
let config_name = config_decl.Ast.config_name in
3378-
let load_code = sprintf {| /* Load %s config map from eBPF object */
3379-
%s_config_map_fd = bpf_object__find_map_fd_by_name(obj->obj, "%s_config_map");
3375+
let generate_config_setup_code ?(obj_var="obj->obj") config_declarations =
3376+
if List.length config_declarations > 0 then
3377+
List.map (fun config_decl ->
3378+
let config_name = config_decl.Ast.config_name in
3379+
let load_code = sprintf {| /* Load %s config map from eBPF object */
3380+
%s_config_map_fd = bpf_object__find_map_fd_by_name(%s, "%s_config_map");
33803381
if (%s_config_map_fd < 0) {
33813382
fprintf(stderr, "Failed to find %s config map in eBPF object\n");
33823383
return -1;
3383-
}|} config_name config_name config_name config_name config_name in
3384-
let init_code = generate_config_initialization config_decl in
3385-
load_code ^ "\n" ^ init_code
3386-
) config_declarations |> String.concat "\n"
3387-
else "" in
3384+
}|} config_name config_name obj_var config_name config_name config_name in
3385+
let init_code = generate_config_initialization config_decl in
3386+
load_code ^ "\n" ^ init_code
3387+
) config_declarations |> String.concat "\n"
3388+
else "" in
3389+
3390+
let config_setup_code = generate_config_setup_code config_declarations in
33883391

33893392
(* Generate struct_ops registration code *)
33903393
let struct_ops_registration_code = generate_struct_ops_registration_code ir_multi_prog in
@@ -3462,6 +3465,11 @@ let generate_complete_userspace_program_from_ir ?(config_declarations = []) ?(ty
34623465
(* Generate automatic BPF object initialization when maps are used but load is not called *)
34633466
let needs_auto_bpf_init = all_usage.uses_map_operations && not all_usage.uses_load in
34643467
let auto_bpf_init_code = if needs_auto_bpf_init && all_setup_code <> "" then
3468+
let auto_map_setup_code = generate_unified_map_setup_code ~obj_var:"bpf_obj" used_global_maps_with_exec in
3469+
let auto_config_setup_code = generate_config_setup_code ~obj_var:"bpf_obj" config_declarations in
3470+
let auto_ringbuf_setup_code = generate_ringbuf_setup_code_from_registry ~obj_var:"bpf_obj" ir_multi_prog.ring_buffer_registry ~dispatch_used:(List.length all_usage.used_dispatch_functions > 0) in
3471+
let auto_setup_parts = [auto_map_setup_code; auto_config_setup_code; auto_ringbuf_setup_code] in
3472+
let auto_setup_code = String.concat "\n" (List.filter (fun s -> s <> "") auto_setup_parts) in
34653473
sprintf {|
34663474
/* Auto-generated BPF object initialization */
34673475
static struct bpf_object *bpf_obj = NULL;
@@ -3489,7 +3497,7 @@ void cleanup_bpf_maps(void) {
34893497
bpf_obj = NULL;
34903498
}
34913499
}
3492-
|} base_name all_setup_code
3500+
|} base_name auto_setup_code
34933501
else "" in
34943502

34953503
(* Only generate BPF helper functions when they're actually used *)

0 commit comments

Comments
 (0)