BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH
BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH are BPF maps primarily used as backend maps for the XDP BPF helper call bpf_redirect_map(). BPF_MAP_TYPE_DEVMAP is backed by an array that uses the key as the index to lookup a reference to a net device. While BPF_MAP_TYPE_DEVMAP_HASH is backed by a hash table that uses a key to lookup a reference to a net device. The user provides either <key/ ifindex> or <key/ struct bpf_devmap_val> pairs to update the maps with new net devices.
The setup and packet enqueue/send code is shared between the two types of devmap; only the lookup and insertion is different.
Usage #
Kernel BPF #
bpf_redirect_map() #
long Redirect the packet to the endpoint referenced by map at index key. For BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH this map contains references to net devices (for forwarding packets through other ports).
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. The higher bits of flags can be set to BPF_F_BROADCAST or BPF_F_EXCLUDE_INGRESS as defined below.
With BPF_F_BROADCAST the packet will be broadcast to all the interfaces in the map, with BPF_F_EXCLUDE_INGRESS the ingress interface will be excluded from the broadcast.
This helper will return XDP_REDIRECT on success, or the value of the two lower bits of the flags argument if the map lookup fails.
More information about redirection can be found Redirect
bpf_map_lookup_elem() #
void *Net device entries can be retrieved using the bpf_map_lookup_elem() helper.
User space #
bpf_map_update_elem() #
int ;
Net device 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_devmap_val or a simple int ifindex for backwards compatibility.
;
-
The
flagsargument 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.
-
DEVMAPs can associate a program with a device entry by adding a bpf_prog.fd to struct bpf_devmap_val. Programs are run after XDP_REDIRECT and have access to both Rx device and Tx device. The program associated with the fd must have type XDP with expected attach type xdp_devmap. When a program is associated with a device index, the program is run on an XDP_REDIRECT and before the buffer is added to the per-cpu queue. Examples of how to attach/use xdp_devmap progs can be found in the kernel selftests:
-
tools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c -
tools/testing/selftests/bpf/progs/test_xdp_with_devmap_helpers.c
bpf_map_lookup_elem() #
int ;
Net device entries can be retrieved using the bpf_map_lookup_elem() helper.
bpf_map_delete_elem() #
int ;
Net device 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 BPF #
The following code snippet shows how to declare a BPF_MAP_TYPE_DEVMAP called tx_port.
tx_port ;
The following code snippet shows how to declare a BPF_MAP_TYPE_DEVMAP_HASH called forward_map.
forward_map ;
The following code snippet shows a simple xdp_redirect_map program. This program would work with a user space program that populates the devmap forward_map based on ingress ifindexes. The BPF program (below) is redirecting packets using the ingress ifindex as the key.
int
The following code snippet shows a BPF program that is broadcasting packets to all the interfaces in the tx_port devmap.
int
User space #
The following code snippet shows how to update a devmap called tx_port.
int
The following code snippet shows how to update a hash_devmap called forward_map.
int