@@ -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 *)
4481let 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."
0 commit comments