forked from multikernel/kernelscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_programs.ks
More file actions
75 lines (64 loc) · 1.4 KB
/
Copy pathmulti_programs.ks
File metadata and controls
75 lines (64 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// XDP context struct (from BTF)
struct xdp_md {
data: u64,
data_end: u64,
data_meta: u64,
ingress_ifindex: u32,
rx_queue_index: u32,
egress_ifindex: u32,
}
// XDP action enum (from BTF)
enum xdp_action {
XDP_ABORTED = 0,
XDP_DROP = 1,
XDP_PASS = 2,
XDP_REDIRECT = 3,
XDP_TX = 4,
}
// TC context struct (from BTF)
struct __sk_buff {
data: u64,
data_end: u64,
len: u32,
ifindex: u32,
protocol: u32,
mark: u32,
}
// TC action constants
enum tc_action {
TC_ACT_UNSPEC = 255,
TC_ACT_OK = 0,
TC_ACT_RECLASSIFY = 1,
TC_ACT_SHOT = 2,
TC_ACT_PIPE = 3,
TC_ACT_STOLEN = 4,
TC_ACT_QUEUED = 5,
TC_ACT_REPEAT = 6,
TC_ACT_REDIRECT = 7,
}
pin var shared_counter : hash<u32, u32>(1024)
// First eBPF program - packet counter
@xdp fn packet_counter(ctx: *xdp_md) -> xdp_action {
shared_counter[1] = 100
return XDP_PASS
}
@tc fn packet_filter(ctx: *__sk_buff) -> i32 {
shared_counter[2] = 200
return TC_ACT_OK
}
// Userspace coordination (outside program blocks)
fn main() -> i32 {
shared_counter[1] = 0
shared_counter[2] = 0
var prog1 = load(packet_counter)
var prog2 = load(packet_filter)
attach(prog1, "eth0", 0)
attach(prog2, "eth0", 0)
print("Multiple XDP programs attached to eth0")
print("Counter and filter working together...")
// Detach in reverse order (good practice)
detach(prog2)
detach(prog1)
print("All programs detached")
return 0
}