get_smp_processor_id (v4.1)
Summary
The get_smp_processor_id
helper function returns the ID of the CPU core on which the eBPF program is currently running. This can be useful for debugging, load balancing, or ensuring that certain operations are performed on specific cores.
Kernel Code
Summary:
The eBPF program retrieves the ID of the CPU core on which it is running using
bpf_get_smp_processor_id
.The CPU ID 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_smp_processor_id
helper function to retrieve the ID of the CPU core on which the eBPF program is running.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