Skip to content

Commit b899f12

Browse files
committed
Fix packet and security event handling in ring buffer demo. Inline event data population replaces separate event construction function, improving clarity and performance.
1 parent 42fe0c2 commit b899f12

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

examples/ringbuf_demo.ks

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,6 @@ fn get_timestamp() -> u64 {
5757
return 1234567890 // Demo timestamp - would be bpf_ktime_get_ns() in real code
5858
}
5959

60-
@helper
61-
fn get_packet_info(ctx: *xdp_md) -> NetworkEvent {
62-
var event = NetworkEvent {
63-
timestamp: get_timestamp(),
64-
event_type: 1, // PACKET_RECEIVED
65-
src_ip: 0x7f000001, // 127.0.0.1
66-
dst_ip: 0x7f000002, // 127.0.0.2
67-
port: 80,
68-
protocol: 6, // TCP
69-
packet_size: 64,
70-
}
71-
return event
72-
}
73-
7460
// XDP program that generates network events
7561
@xdp fn network_monitor(ctx: *xdp_md) -> xdp_action {
7662
var key: u32 = 0
@@ -84,8 +70,16 @@ fn get_packet_info(ctx: *xdp_md) -> NetworkEvent {
8470
// Try to reserve space in ring buffer
8571
var reserved = network_events.reserve()
8672
if (reserved != null) {
87-
// Successfully reserved space - build event and submit
88-
// For now, just submit the reserved space
73+
// Successfully reserved space - populate event data inline
74+
reserved->timestamp = get_timestamp()
75+
reserved->event_type = 1 // PACKET_RECEIVED
76+
reserved->src_ip = 0x7f000001 // 127.0.0.1
77+
reserved->dst_ip = 0x7f000002 // 127.0.0.2
78+
reserved->port = 80
79+
reserved->protocol = 6 // TCP
80+
reserved->packet_size = 64
81+
82+
// Submit the populated event
8983
network_events.submit(reserved)
9084
stat.events_submitted = stat.events_submitted + 1
9185
} else {
@@ -101,7 +95,14 @@ fn get_packet_info(ctx: *xdp_md) -> NetworkEvent {
10195
@kprobe("sys_openat") fn security_monitor(dfd: i32, filename: *u8, flags: i32, mode: u16) -> i32 {
10296
var reserved = security_events.reserve()
10397
if (reserved != null) {
104-
// Successfully reserved space - submit the event
98+
// Successfully reserved space - populate security event inline
99+
reserved->timestamp = get_timestamp()
100+
reserved->severity = 2 // Medium severity
101+
reserved->event_id = 1001 // FILE_OPEN event
102+
reserved->pid = 1234 // Demo PID
103+
// Note: In real code, would copy actual message data
104+
105+
// Submit the populated event
105106
security_events.submit(reserved)
106107
} else {
107108
// Handle full buffer - could discard or try alternative logging
@@ -116,23 +117,23 @@ fn get_packet_info(ctx: *xdp_md) -> NetworkEvent {
116117
// Event handler for network events
117118
fn network_event_handler(event: *NetworkEvent) -> i32 {
118119
print("Network Event:")
119-
print(" Timestamp: ", event->timestamp)
120-
print(" Type: ", event->event_type)
121-
print(" Source IP: ", event->src_ip)
122-
print(" Destination IP: ", event->dst_ip)
123-
print(" Port: ", event->port)
124-
print(" Protocol: ", event->protocol)
125-
print(" Packet Size: ", event->packet_size)
120+
print(" Timestamp: %llu", event->timestamp)
121+
print(" Type: %u", event->event_type)
122+
print(" Source IP: %u", event->src_ip)
123+
print(" Destination IP: %u", event->dst_ip)
124+
print(" Port: %u", event->port)
125+
print(" Protocol: %u", event->protocol)
126+
print(" Packet Size: %u", event->packet_size)
126127
return 0
127128
}
128129

129130
// Event handler for security events
130131
fn security_event_handler(event: *SecurityEvent) -> i32 {
131132
print("Security Event:")
132-
print(" Timestamp: ", event->timestamp)
133-
print(" Severity: ", event->severity)
134-
print(" Event ID: ", event->event_id)
135-
print(" PID: ", event->pid)
133+
print(" Timestamp: %llu", event->timestamp)
134+
print(" Severity: %u", event->severity)
135+
print(" Event ID: %u", event->event_id)
136+
print(" PID: %u", event->pid)
136137
print(" Message: [security event]")
137138
return 0
138139
}

0 commit comments

Comments
 (0)