Skip to content

Commit 15970c4

Browse files
committed
Refactor program type handling in BTF parser and context code generation.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 1952309 commit 15970c4

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/btf_parser.ml

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ let get_kprobe_program_template target_function btf_path =
224224
in
225225

226226
{
227-
program_type = "kprobe";
227+
program_type = "probe";
228228
context_type = context_type;
229229
return_type = return_type;
230230
includes = ["linux/bpf.h"; "linux/pkt_cls.h"; "linux/if_ether.h"; "linux/ip.h"; "linux/tcp.h"; "linux/udp.h"];
@@ -358,21 +358,24 @@ let generate_kprobe_function_from_signature func_name signature =
358358

359359
(** Generate KernelScript source code from template *)
360360
let generate_kernelscript_source ?extra_param ?include_kfuncs template project_name =
361-
let context_comment = match template.program_type with
362-
| "xdp" -> "// XDP (eXpress Data Path) program for high-performance packet processing"
363-
| "tc" -> "// TC (Traffic Control) program for network traffic shaping and filtering"
364-
| "kprobe" -> "// Kprobe program for dynamic kernel tracing"
365-
| "uprobe" -> "// Uprobe program for userspace function tracing"
366-
| "tracepoint" -> "// Tracepoint program for static kernel tracing"
367-
| "lsm" -> "// LSM (Linux Security Module) program for security enforcement"
368-
| "cgroup_skb" -> "// Cgroup SKB program for cgroup-based packet filtering"
369-
| _ -> "// eBPF program"
370-
in
371-
372-
let return_values = match template.program_type with
373-
| "xdp" -> ["XDP_ABORTED"; "XDP_DROP"; "XDP_PASS"; "XDP_TX"; "XDP_REDIRECT"]
374-
| "tc" -> ["TC_ACT_OK"; "TC_ACT_SHOT"; "TC_ACT_STOLEN"; "TC_ACT_PIPE"; "TC_ACT_REDIRECT"]
375-
| _ -> ["0"; "-1"]
361+
(* Initialize context code generators to ensure they're available *)
362+
Kernelscript_context.Xdp_codegen.register ();
363+
Kernelscript_context.Tc_codegen.register ();
364+
Kernelscript_context.Kprobe_codegen.register ();
365+
Kernelscript_context.Tracepoint_codegen.register ();
366+
Kernelscript_context.Fprobe_codegen.register ();
367+
368+
(* Get program description from context codegen system *)
369+
let context_comment = "// " ^ (Kernelscript_context.Context_codegen.get_context_program_description template.program_type) in
370+
371+
(* Get return values from context codegen system if available *)
372+
let return_values =
373+
let action_constants = Kernelscript_context.Context_codegen.get_context_action_constants template.program_type in
374+
if action_constants <> [] then
375+
List.map fst action_constants (* Extract constant names *)
376+
else
377+
(* Fallback for program types without action constants *)
378+
["0"; "-1"]
376379
in
377380

378381
(* Helper function to generate type definition string *)
@@ -421,7 +424,7 @@ let generate_kernelscript_source ?extra_param ?include_kfuncs template project_n
421424

422425
(* Generate function signature comments and actual function definition for specific program types *)
423426
let (function_signatures_comment, target_function_name, function_definition, custom_attribute) =
424-
if template.program_type = "kprobe" && template.function_signatures <> [] then
427+
if template.program_type = "probe" && template.function_signatures <> [] then
425428
let signature_lines = List.map (fun (func_name, signature) ->
426429
sprintf "// Target function: %s -> %s" func_name signature
427430
) template.function_signatures in
@@ -462,23 +465,23 @@ let generate_kernelscript_source ?extra_param ?include_kfuncs template project_n
462465
| None -> "@" ^ template.program_type
463466
in
464467

465-
(* Customize attach call for kprobe/tracepoint *)
468+
(* Customize attach call for probe/tracepoint *)
466469
let attach_target =
467-
if template.program_type = "kprobe" then target_function_name
470+
if template.program_type = "probe" then target_function_name
468471
else if template.program_type = "tracepoint" then target_function_name
469472
else "eth0"
470473
in
471474
let attach_comment =
472-
if template.program_type = "kprobe" then
473-
" // Attach kprobe to target kernel function"
475+
if template.program_type = "probe" then
476+
" // Attach probe to target kernel function"
474477
else if template.program_type = "tracepoint" then
475478
" // Attach tracepoint to target kernel event"
476479
else
477480
" // TODO: Update interface name and attachment parameters"
478481
in
479482

480483
let function_name =
481-
if template.program_type = "kprobe" then target_function_name
484+
if template.program_type = "probe" then target_function_name
482485
else if template.program_type = "tracepoint" then
483486
String.map (function '/' -> '_' | c -> c) target_function_name ^ "_handler"
484487
else if template.program_type = "tc" && extra_param <> None then

src/context/context_codegen.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
This module defines the interface for context-specific code generators
1919
*)
2020

21+
open Printf
22+
2123
type context_field_access = {
2224
field_name: string;
2325
c_expression: string -> string; (* ctx_var -> C expression *)
@@ -115,6 +117,17 @@ let get_context_struct_fields ctx_type =
115117
) codegen.field_mappings
116118
| None -> []
117119

120+
(** Get program description for a context type *)
121+
let get_context_program_description ctx_type =
122+
match ctx_type with
123+
| "xdp" -> "XDP (eXpress Data Path) program for high-performance packet processing"
124+
| "tc" -> "TC (Traffic Control) program for network traffic shaping and filtering"
125+
| "probe" -> "Probe program for dynamic kernel tracing (fprobe/kprobe)"
126+
| "kprobe" -> "Kprobe program for dynamic kernel tracing with offset support"
127+
| "tracepoint" -> "Tracepoint program for static kernel tracing"
128+
| "fprobe" -> "Fprobe program for function entry/exit tracing"
129+
| _ -> sprintf "eBPF %s program" ctx_type
130+
118131
(** Get the C type string for a context field *)
119132
let get_context_field_c_type ctx_type field_name =
120133
match get_context_codegen ctx_type with

src/context/context_codegen.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ val generate_context_field_access : string -> string -> string -> string
5959
(** Get context-specific includes *)
6060
val get_context_includes : string -> string list
6161

62+
(** Get program description for a context type *)
63+
val get_context_program_description : string -> string
64+
6265
(** Map action constant for a context type *)
6366
val map_context_action_constant : string -> int -> string option
6467

0 commit comments

Comments
 (0)