Skip to content

Commit 6f7c09b

Browse files
committed
Implement program-type specific header generation and improve struct_ops template handling.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent e8deaf9 commit 6f7c09b

75 files changed

Lines changed: 972 additions & 1219 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/basic_match.ks

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
// Basic Match Construct Demo for KernelScript
22
// Demonstrates packet matching with the new match construct
33

4-
// XDP context struct (from BTF)
5-
struct xdp_md {
6-
data: u64,
7-
data_end: u64,
8-
data_meta: u64,
9-
ingress_ifindex: u32,
10-
rx_queue_index: u32,
11-
egress_ifindex: u32,
12-
}
13-
14-
// XDP action enum (from BTF)
15-
enum xdp_action {
16-
XDP_ABORTED = 0,
17-
XDP_DROP = 1,
18-
XDP_PASS = 2,
19-
XDP_REDIRECT = 3,
20-
XDP_TX = 4,
21-
}
4+
include "xdp.kh"
225

236
// Protocol constants
247
enum IpProtocol {

examples/break_continue_unbound.ks

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
// XDP context struct (from BTF)
2-
struct xdp_md {
3-
data: u64,
4-
data_end: u64,
5-
data_meta: u64,
6-
ingress_ifindex: u32,
7-
rx_queue_index: u32,
8-
egress_ifindex: u32,
9-
}
10-
11-
// XDP action enum (from BTF)
12-
enum xdp_action {
13-
XDP_ABORTED = 0,
14-
XDP_DROP = 1,
15-
XDP_PASS = 2,
16-
XDP_REDIRECT = 3,
17-
XDP_TX = 4,
18-
}
19-
201
// Example demonstrating break and continue in truly unbound loops
212
// This should force bpf_loop() usage
223

4+
include "xdp.kh"
5+
236
var counter_map : hash<u32, u32>(10)
247

258
@xdp fn packet_filter(ctx: *xdp_md) -> xdp_action {

examples/dynptr.ks

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
1-
// XDP context struct (from BTF)
2-
struct xdp_md {
3-
data: u64,
4-
data_end: u64,
5-
data_meta: u64,
6-
ingress_ifindex: u32,
7-
rx_queue_index: u32,
8-
egress_ifindex: u32,
9-
}
10-
11-
// XDP action enum (from BTF)
12-
enum xdp_action {
13-
XDP_ABORTED = 0,
14-
XDP_DROP = 1,
15-
XDP_PASS = 2,
16-
XDP_REDIRECT = 3,
17-
XDP_TX = 4,
18-
}
19-
201
// Dynptr showcase - compiler should transparently use dynptr APIs for packet access
212
// Example to demonstrate bpf_dynptr_from_mem usage
223
// This would be for accessing memory buffers, not packet data
234

5+
include "xdp.kh"
6+
247
struct DataBuffer {
258
data: u8[32],
269
size: u32

examples/error_handling_demo.ks

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
// XDP context struct (from BTF)
2-
struct xdp_md {
3-
data: u64,
4-
data_end: u64,
5-
data_meta: u64,
6-
ingress_ifindex: u32,
7-
rx_queue_index: u32,
8-
egress_ifindex: u32,
9-
}
10-
11-
// XDP action enum (from BTF)
12-
enum xdp_action {
13-
XDP_ABORTED = 0,
14-
XDP_DROP = 1,
15-
XDP_PASS = 2,
16-
XDP_REDIRECT = 3,
17-
XDP_TX = 4,
18-
}
19-
201
// Minimal error handling demo
2+
include "xdp.kh"
3+
214
var counters : hash<u32, u64>(1024)
225

236
@xdp fn error_demo(ctx: *xdp_md) -> xdp_action {

examples/extern_kfunc_demo.ks

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include "xdp.kh"
2+
13
// External kfunc declarations - these would typically be imported from kernel BTF
24
extern bpf_ktime_get_ns() -> u64
35
extern bpf_trace_printk(fmt: *u8, fmt_size: u32) -> i32

examples/functions.ks

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
// XDP context struct (from BTF)
2-
struct xdp_md {
3-
data: u64,
4-
data_end: u64,
5-
data_meta: u64,
6-
ingress_ifindex: u32,
7-
rx_queue_index: u32,
8-
egress_ifindex: u32,
9-
}
10-
11-
// XDP action enum (from BTF)
12-
enum xdp_action {
13-
XDP_ABORTED = 0,
14-
XDP_DROP = 1,
15-
XDP_PASS = 2,
16-
XDP_REDIRECT = 3,
17-
XDP_TX = 4,
18-
}
1+
include "xdp.kh"
192

203
type IpAddress = u32
214

examples/import_demo.ks

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import utils from "./import/simple_utils.ks"
66
// Import Python module (uses Python bridge)
77
import network_utils from "./import/network_utils.py"
88

9+
include "xdp.kh"
10+
911
config network {
1012
enable_filtering: bool = false,
1113
status_code: u32 = 0,

examples/include_demo.ks

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// Include declarations from header files
55
include "common_kfuncs.kh"
66
include "xdp_kfuncs.kh"
7+
include "xdp.kh"
8+
79

810
// XDP program that uses included kfunc declarations
911
@xdp

examples/local_global_vars.ks

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
1-
// XDP context struct (from BTF)
2-
struct xdp_md {
3-
data: u64,
4-
data_end: u64,
5-
data_meta: u64,
6-
ingress_ifindex: u32,
7-
rx_queue_index: u32,
8-
egress_ifindex: u32,
9-
}
10-
11-
// XDP action enum (from BTF)
12-
enum xdp_action {
13-
XDP_ABORTED = 0,
14-
XDP_DROP = 1,
15-
XDP_PASS = 2,
16-
XDP_REDIRECT = 3,
17-
XDP_TX = 4,
18-
}
19-
201
// Example demonstrating local vs shared global variables
212
//
223
// This example shows the difference between:
234
// - Regular global variables (shared with userspace via skeleton)
245
// - Local global variables (kernel-only, not accessible from userspace)
256

267
// Shared global variables - accessible from userspace via skeleton
8+
include "xdp.kh"
9+
2710
var packet_count: u64 = 0
2811
var debug_enabled: bool = true
2912

examples/map_operations_demo.ks

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,9 @@
44
//
55
// NOTE: This file uses advanced language features not yet implemented in KernelScript.
66

7-
// XDP context struct (from BTF)
8-
struct xdp_md {
9-
data: u64,
10-
data_end: u64,
11-
data_meta: u64,
12-
ingress_ifindex: u32,
13-
rx_queue_index: u32,
14-
egress_ifindex: u32,
15-
}
16-
17-
// XDP action enum (from BTF)
18-
enum xdp_action {
19-
XDP_ABORTED = 0,
20-
XDP_DROP = 1,
21-
XDP_PASS = 2,
22-
XDP_REDIRECT = 3,
23-
XDP_TX = 4,
24-
}
25-
26-
// TC context struct (from BTF)
27-
struct __sk_buff {
28-
data: u64,
29-
data_end: u64,
30-
len: u32,
31-
ifindex: u32,
32-
protocol: u32,
33-
mark: u32,
34-
}
35-
36-
// TC action constants
37-
enum tc_action {
38-
TC_ACT_UNSPEC = 255,
39-
TC_ACT_OK = 0,
40-
TC_ACT_RECLASSIFY = 1,
41-
TC_ACT_SHOT = 2,
42-
TC_ACT_PIPE = 3,
43-
TC_ACT_STOLEN = 4,
44-
TC_ACT_QUEUED = 5,
45-
TC_ACT_REPEAT = 6,
46-
TC_ACT_REDIRECT = 7,
47-
}
48-
49-
// Base trace entry struct
50-
struct trace_entry {
51-
entry_type: u16,
52-
flags: u8,
53-
preempt_count: u8,
54-
pid: i32,
55-
}
56-
57-
// Tracepoint context struct (from BTF) - sys_enter structure
58-
struct trace_event_raw_sys_enter {
59-
ent: trace_entry,
60-
id: i64,
61-
args: u64[6],
62-
}
7+
include "xdp.kh"
8+
include "tc.kh"
9+
include "tracepoint.kh"
6310

6411
// This example demonstrates comprehensive map operations with multi-program analysis
6512
// It shows various access patterns and concurrent access scenarios

0 commit comments

Comments
 (0)