Examples
Explore example Solnix programs and common use cases.
Tracepoint Example
This example demonstrates a simple tracepoint program that performs arithmetic operations and stores the result in a map.
map results { // (1)
type: .array;
key: u32;
value: u64;
max: 8;
}
unit sample_tracepoint {
section: "tracepoint/syscalls/sys_enter_execve"; // (2)
license: "GPL";
reg a = 10;
reg b = 20;
reg c = a + b; // (3)
imm k0 = 0; heap p0 = results.lookup(k0); *p0 = c; // (4)
return 0;
}
- Defines an array map
resultswith 8 entries. - Attaches to the
sys_enter_execvetracepoint. - Performs addition of two register variables.
- Stores the result in the map at index 0.
XDP Packet Filter Example
This example shows an XDP program that counts packets per source IP address using a hash map.
map connection_counter {
type: .hash;
key: u32;
value: u64;
max: 1024;
}
unit filter_packets {
section: "xdp";
license: "GPL";
reg src_ip = ctx.load_u32(26);
heap count_ptr = connection_counter.lookup(src_ip);
if guard(count_ptr) {
*count_ptr += 1;
}
return 1;
}
This program: - Defines a hash map to track connection counts per IP address - Attaches to the XDP hook for network packet processing - Loads the source IP address from the packet context - Increments a counter for each source IP if an entry exists - Returns 1 to pass the packet