BPF_MAP_TYPE_PERCPU_ARRAY
is a type of BPF
map that allows each CPU to have its own instance of the map's values. This means that updates to the map are done on a per-CPU basis. BPF_MAP_TYPE_PERCPU_ARRAY
uses a different memory region for each CPU. To restrict storage to a single CPU, you may use a BPF_MAP_TYPE_PERCPU_ARRAY
. When a program reads from or writes to a BPF_MAP_TYPE_PERCPU_ARRAY
map, it accesses the version specific to the CPU it is running on. This makes it efficient for operations that are frequently updated, like packet counters, because it reduces the overhead associated with synchronization across multiple CPUs.
This XDP
program defines a map to record statistics of packets processed by XDP actions. The map, xdp_stats_map
, is a per-CPU array that stores packet and byte counts for up to five XDP actions. The program includes three XDP functions: xdp_pass_func
, xdp_drop_func
, and xdp_abort_func
, which are triggered for corresponding actions (XDP_PASS, XDP_DROP, XDP_ABORTED). Each function calls xdp_stats_record_action
to update the statistics in the map for the specific action, incrementing the packet count and adding the packet's byte size. Additionally, an user_space_prog in Go is used for logging packet counts and sizes to the console.
Firstly you have to navigate to tasks/per-cpu-stats/user-space-code
this directory. After doing all the necessary steps from the prerequisite. You can proceed with following procedure :
- Download the necessary modules
go mod tidy
- Generate necessary files using
bpf2go
go generate
- Build the go binary. It will generate a file in the same directory called
xdp-abort
go build
- Run the binary with
-iface <interface name> -prog <xdp_pass, xdp_drop, or xdp_abort>
withsudo
privileges
sudo ./xdp-per-cpu-stats -iface veth-h -prog xdp_drop
Note: I have used veth-h
as interface name. Because I am working on the default
environment setup.
- Pinging and Tracing for
xdp_pass
- Pinging and Tracing for
xdp_drop
- Pinging and Tracing for
xdp_abort