get_prandom_u32 (v4.1)
Summary
The get_prandom_u32
helper function generates a pseudo-random 32-bit unsigned integer. This can be useful for various purposes, such as generating random keys, randomizing behavior, or creating unique identifiers within an eBPF program.
Kernel Code
Summary:
The eBPF program generates a pseudo-random 32-bit unsigned integer using
bpf_get_prandom_u32
.The generated random value is printed to the BPF trace pipe using
bpf_printk
.
Userland Code
Summary:
The userland program loads the BPF object file using
bpf_object__open
andbpf_object__load
.It retrieves the file descriptor for the BPF program using
bpf_program__fd
.A raw socket is created and the BPF program is attached to it using
setsockopt
.The program waits for user input to keep the BPF program running, allowing it to execute.
Resources are cleaned up when the program exits.
Conclusions
The example demonstrates the use of the
get_prandom_u32
helper function to generate a pseudo-random 32-bit unsigned integer within an eBPF program.The userland code uses full libbpf functions to load, attach programs, and manage resources without using the skeleton generated by
bpftool
.The output from
bpf_printk
can be viewed usingbpftool
by runningbpftool prog trace
.
Last updated