BPF_MAP_TYPE_CPUMAP
The 'cpumap' is primarily used as a backend map for XDP BPF helper
call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
Unlike devmap which redirects XDP frames out to another NIC device, this map type redirects raw XDP frames to another CPU. The remote CPU will do SKB-allocation and call the normal network stack.
An example use-case for this map type is software based Receive Side Scaling (RSS).
The CPUMAP represents the CPUs in the system indexed as the map-key, and the map-value is the config setting (per CPUMAP entry). Each CPUMAP entry has a dedicated kernel thread bound to the given CPU to represent the remote CPU execution unit.
Starting from Linux kernel version 5.9 the CPUMAP can run a second XDP program on the remote CPU. This allows an XDP program to split its processing across multiple CPUs. For example, a scenario where the initial CPU (that sees/receives the packets) needs to do minimal packet processing and the remote CPU (to which the packet is directed) can afford to spend more cycles processing the frame. The initial CPU is where the XDP redirect program is executed. The remote CPU receives raw xdp_frame objects.
Usage #
Kernel BPF #
bpf_redirect_map() #
long Redirect the packet to the endpoint referenced by map at index key. For BPF_MAP_TYPE_CPUMAP this map contains references to CPUs.
The lower two bits of flags are used as the return code if the map lookup fails. This is so that the return value can be one of the XDP program return codes up to XDP_TX, as chosen by the caller.
User space #
bpf_map_update_elem() #
int ;
CPU entries can be added or updated using the bpf_map_update_elem() helper. This helper replaces existing elements atomically. The value parameter can be struct bpf_cpumap_val.
;
-
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
-
bpf_map_lookup_elem() #
int ;
CPU entries can be retrieved using the bpf_map_lookup_elem() helper.
bpf_map_delete_elem() #
int ;
CPU entries can be deleted using the bpf_map_delete_elem() helper. This helper will return 0 on success, or negative error in case of failure.
Examples #
Kernel #
The following code snippet shows how to declare a BPF_MAP_TYPE_CPUMAP called cpu_map and how to redirect packets to a remote CPU using a round robin scheme.
cpu_map ;
cpus_available ;
cpus_iterator ;
int
User space #
The following code snippet shows how to dynamically set the max_entries for a CPUMAP to the max number of cpus available on the system.
int