Skip to content

Commit e267910

Browse files
committed
Fix syntax errors in packet_matching.ks.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 06d2e0a commit e267910

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

examples/packet_matching.ks

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,46 +252,59 @@ fn packet_monitor(ctx: *xdp_md) -> xdp_action {
252252
var dst_ip = get_dst_ip(ctx)
253253

254254
// Categorize and log based on protocol
255-
var log_category = match (protocol) {
255+
match (protocol) {
256256
TCP: {
257257
var tcp_port = get_tcp_dest_port(ctx)
258-
return match (tcp_port) {
259-
HTTP: "web_traffic",
260-
HTTPS: "secure_web",
261-
SSH: "admin_access",
262-
default: "tcp_other"
258+
match (tcp_port) {
259+
HTTP: {
260+
print("PKT: web_traffic %u->%u\n", src_ip, dst_ip)
261+
},
262+
HTTPS: {
263+
print("PKT: secure_web %u->%u\n", src_ip, dst_ip)
264+
},
265+
SSH: {
266+
print("PKT: admin_access %u->%u\n", src_ip, dst_ip)
267+
},
268+
default: {
269+
print("PKT: tcp_other %u->%u\n", src_ip, dst_ip)
270+
}
263271
}
264272
},
265273

266274
UDP: {
267275
var udp_port = get_udp_dest_port(ctx)
268-
return match (udp_port) {
269-
DNS: "dns_query",
270-
default: "udp_other"
276+
match (udp_port) {
277+
DNS: {
278+
print("PKT: dns_query %u->%u\n", src_ip, dst_ip)
279+
},
280+
default: {
281+
print("PKT: udp_other %u->%u\n", src_ip, dst_ip)
282+
}
271283
}
272284
},
273285

274-
ICMP: "icmp_traffic",
275-
default: "unknown_protocol"
286+
ICMP: {
287+
print("PKT: icmp_traffic %u->%u\n", src_ip, dst_ip)
288+
},
289+
default: {
290+
print("PKT: unknown_protocol %u->%u\n", src_ip, dst_ip)
291+
}
276292
}
277293

278-
// Log the packet with category
279-
print("PKT: %s %u->%u\n", log_category, src_ip, dst_ip)
280-
281294
return XDP_PASS
282295
}
283296

284297
// Quality of Service (QoS) packet marking
285298
// Demonstrates match for traffic prioritization
286299
@tc("ingress")
287-
fn qos_packet_marker(ctx: *__sk_buff) -> int {
300+
fn qos_packet_marker(ctx: *__sk_buff) -> i32 {
288301
var protocol = get_ip_protocol_tc(ctx)
289302

290303
// Set QoS markings based on traffic type
291304
var qos_class = match (protocol) {
292305
TCP: {
293306
var tcp_port = get_tcp_dest_port_tc(ctx)
294-
return match (tcp_port) {
307+
match (tcp_port) {
295308
SSH: "high_priority", // Admin traffic
296309
HTTPS: "medium_priority", // Web traffic
297310
HTTP: "medium_priority", // Web traffic
@@ -301,7 +314,7 @@ fn qos_packet_marker(ctx: *__sk_buff) -> int {
301314

302315
UDP: {
303316
var udp_port = get_udp_dest_port_tc(ctx)
304-
return match (udp_port) {
317+
match (udp_port) {
305318
DNS: "high_priority", // DNS is critical
306319
default: "low_priority"
307320
}

0 commit comments

Comments
 (0)