Skip to content

Commit 1952309

Browse files
committed
Implement caching for BTF-extracted kernel types and update related functions for improved type detection. Remove deprecated kernel_types.ml file.
1 parent 869d270 commit 1952309

File tree

4 files changed

+52
-94
lines changed

4 files changed

+52
-94
lines changed

src/btf_parser.ml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,45 @@ type program_template = {
3737

3838

3939

40-
(** Check if a type name is a well-known kernel type *)
41-
let is_well_known_kernel_type ?btf_path = Kernel_types.is_well_known_ebpf_type ?btf_path
40+
(** Cache for BTF-extracted kernel types to avoid re-parsing *)
41+
let kernel_types_cache : (string, string list) Hashtbl.t = Hashtbl.create 16
42+
43+
(** Extract kernel types from BTF file with caching *)
44+
let get_kernel_types_from_btf btf_path =
45+
match Hashtbl.find_opt kernel_types_cache btf_path with
46+
| Some cached_types -> cached_types
47+
| None ->
48+
let kernel_types = Btf_binary_parser.extract_all_kernel_struct_and_enum_names btf_path in
49+
Hashtbl.add kernel_types_cache btf_path kernel_types;
50+
kernel_types
51+
52+
(** Check if a type name is a well-known eBPF kernel type using BTF. *)
53+
let is_well_known_ebpf_type ?btf_path type_name =
54+
match btf_path with
55+
| Some path when Sys.file_exists path ->
56+
let kernel_types = get_kernel_types_from_btf path in
57+
List.mem type_name kernel_types
58+
| Some path ->
59+
failwith (sprintf "BTF file not found: %s" path)
60+
| None ->
61+
failwith "BTF file path is required for kernel type detection. Use --btf-vmlinux-path option."
62+
63+
(** Clear the kernel types cache (useful for testing or when BTF file changes) *)
64+
let clear_kernel_types_cache () =
65+
Hashtbl.clear kernel_types_cache
66+
67+
(** Get all known kernel types for the given BTF file (for debugging/inspection) *)
68+
let get_all_kernel_types ?btf_path () =
69+
match btf_path with
70+
| Some path when Sys.file_exists path ->
71+
get_kernel_types_from_btf path
72+
| Some path ->
73+
failwith (sprintf "BTF file not found: %s" path)
74+
| None ->
75+
failwith "BTF file path is required for kernel type inspection. Use --btf-vmlinux-path option."
76+
77+
(** Check if a type name is a well-known kernel type (alias for compatibility) *)
78+
let is_well_known_kernel_type ?btf_path = is_well_known_ebpf_type ?btf_path
4279

4380
(** Create hardcoded enum definitions for constants that can't be extracted from BTF *)
4481
let create_hardcoded_tc_action_enum () = {
@@ -82,7 +119,7 @@ let get_program_template prog_type btf_path =
82119
kind = bt.Btf_binary_parser.kind;
83120
size = bt.Btf_binary_parser.size;
84121
members = bt.Btf_binary_parser.members;
85-
kernel_defined = is_well_known_kernel_type ?btf_path bt.Btf_binary_parser.name;
122+
kernel_defined = is_well_known_ebpf_type ?btf_path bt.Btf_binary_parser.name;
86123
}) binary_types
87124
| Some path -> failwith (sprintf "BTF file not found: %s" path)
88125
| None -> failwith "BTF file path is required. Use --btf-vmlinux-path option."
@@ -147,7 +184,7 @@ let get_tracepoint_program_template category_event btf_path =
147184
kind = bt.Btf_binary_parser.kind;
148185
size = bt.Btf_binary_parser.size;
149186
members = bt.Btf_binary_parser.members;
150-
kernel_defined = is_well_known_kernel_type ?btf_path bt.Btf_binary_parser.name;
187+
kernel_defined = is_well_known_ebpf_type ?btf_path bt.Btf_binary_parser.name;
151188
}) binary_types
152189
| Some path -> failwith (sprintf "BTF file not found: %s" path)
153190
| None -> failwith "BTF file path is required for tracepoint extraction. Use --btf-vmlinux-path option."

src/btf_parser.mli

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ val get_kprobe_program_template : string -> string option -> program_template
4242
(** Get tracepoint program template for a specific target function *)
4343
val get_tracepoint_program_template : string -> string option -> program_template
4444

45-
(** Check if a type name is a well-known eBPF kernel type *)
45+
(** Check if a type name is a well-known eBPF kernel type using BTF *)
4646
val is_well_known_kernel_type : ?btf_path:string -> string -> bool
4747

48+
(** Check if a type name is a well-known eBPF kernel type using BTF (main function) *)
49+
val is_well_known_ebpf_type : ?btf_path:string -> string -> bool
50+
51+
(** Clear the kernel types cache (useful for testing or when BTF file changes) *)
52+
val clear_kernel_types_cache : unit -> unit
53+
54+
(** Get all known kernel types for the given BTF file (for debugging/inspection) *)
55+
val get_all_kernel_types : ?btf_path:string -> unit -> string list
56+
4857
(** Extract struct_ops definitions from BTF and generate KernelScript code *)
4958
val extract_struct_ops_definitions : string option -> string list -> string list
5059

src/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
multi_program_analyzer multi_program_ir_optimizer ebpf_c_codegen
66
userspace_codegen evaluator safety_checker stdlib test_codegen
77
tail_call_analyzer kernel_module_codegen dynptr_bridge
8-
btf_parser btf_binary_parser kernel_types struct_ops_registry
8+
btf_parser btf_binary_parser struct_ops_registry
99
import_resolver include_resolver python_bridge kernelscript_bridge
1010
)
1111
(libraries unix str kernelscript_context)

src/kernel_types.ml

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)