Skip to content

Commit 80cfed1

Browse files
committed
Enhance eBPF C code generation by incorporating context-specific includes for kprobe programs while maintaining vmlinux.h usage. Update TC-specific includes to define action constants inline, avoiding header conflicts.
1 parent 3786f00 commit 80cfed1

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/context/tc_codegen.ml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,19 @@ let tc_field_mappings = [
6868

6969
(** Generate TC-specific includes *)
7070
let generate_tc_includes () = [
71-
"#include <linux/bpf.h>";
72-
"#include <bpf/bpf_helpers.h>";
73-
"#include <linux/if_ether.h>";
74-
"#include <linux/ip.h>";
75-
"#include <linux/in.h>";
76-
"#include <linux/pkt_cls.h>";
71+
"/* TC action constants - defined inline to avoid header conflicts with vmlinux.h */";
72+
"#ifndef TC_ACT_UNSPEC";
73+
"#define TC_ACT_UNSPEC (-1)";
74+
"#define TC_ACT_OK 0";
75+
"#define TC_ACT_RECLASSIFY 1";
76+
"#define TC_ACT_SHOT 2";
77+
"#define TC_ACT_PIPE 3";
78+
"#define TC_ACT_STOLEN 4";
79+
"#define TC_ACT_QUEUED 5";
80+
"#define TC_ACT_REPEAT 6";
81+
"#define TC_ACT_REDIRECT 7";
82+
"#define TC_ACT_TRAP 8";
83+
"#endif";
7784
]
7885

7986
(** Generate field access for TC context *)

src/ebpf_c_codegen.ml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,32 +1119,42 @@ let generate_includes ctx ?(program_types=[]) () =
11191119
"#include <bpf/bpf_helpers.h>";
11201120
] in
11211121

1122-
(* When using vmlinux.h, we don't need context-specific kernel headers *)
1123-
(* vmlinux.h contains all kernel types, so we only need eBPF helpers *)
1124-
let context_includes = [] in (* No additional kernel headers needed with vmlinux.h *)
1122+
(* Get context-specific includes for macros not in vmlinux.h *)
1123+
let context_includes = List.fold_left (fun acc prog_type ->
1124+
let context_type = match prog_type with
1125+
| Ast.Tc -> Some "tc"
1126+
| _ -> None
1127+
in
1128+
match context_type with
1129+
| Some ctx_type ->
1130+
let includes = Kernelscript_context.Context_codegen.get_context_includes ctx_type in
1131+
acc @ includes
1132+
| None -> acc
1133+
) [] program_types in
11251134

11261135
(* Remove duplicates between all include sets *)
11271136
let all_base_includes = vmlinux_includes @ standard_includes in
11281137
let unique_context_includes = List.filter (fun inc ->
11291138
not (List.mem inc all_base_includes)) context_includes in
11301139

1131-
(* For kprobe programs, only emit kprobe includes which contain everything needed *)
1140+
(* For kprobe programs, still use vmlinux.h but include context-specific macro headers *)
11321141
let has_kprobe = List.exists (function Ast.Kprobe -> true | _ -> false) program_types in
11331142
if has_kprobe then (
1134-
(* With vmlinux.h, we only need it and eBPF helpers, no additional kernel headers *)
1143+
(* Use vmlinux.h and context-specific headers for macros *)
11351144
let vmlinux_and_helpers = [
11361145
"#include \"vmlinux.h\"";
11371146
"#include <bpf/bpf_helpers.h>";
11381147
] in
11391148

11401149
List.iter (emit_line ctx) vmlinux_and_helpers;
1150+
List.iter (emit_line ctx) unique_context_includes;
11411151
emit_blank_line ctx
11421152
) else (
11431153
(* For non-kprobe programs, use vmlinux.h and standard processing *)
11441154
let all_includes = vmlinux_includes @ standard_includes @ unique_context_includes in
11451155
List.iter (emit_line ctx) all_includes;
11461156
emit_blank_line ctx;
1147-
1157+
11481158
(* Use proper kernel implementation: extern declarations and macros *)
11491159
emit_line ctx "extern void *bpf_obj_new_impl(__u64 local_type_id__k, void *meta__ign) __ksym;";
11501160
emit_line ctx "extern void bpf_obj_drop_impl(void *p__alloc, void *meta__ign) __ksym;";

0 commit comments

Comments
 (0)