bpf (2) Linux Manual Page
NAME
bpf – perform a command on an extended BPF map or program
SYNOPSIS
#include <linux/bpf.h>
int bpf(int cmd, union bpf_attr *attr, unsigned int size);
DESCRIPTION
The bpf() system call performs a range of operations related to extended Berkeley Packet Filters. Extended BPF (or eBPF) is similar to the original ("classic") BPF (cBPF) used to filter network packets. For both cBPF and eBPF programs, the kernel statically analyzes the programs before loading them, in order to ensure that they cannot harm the running system.
eBPF extends cBPF in multiple ways, including the ability to call a fixed set of in-kernel helper functions (via the BPF_CALL opcode extension provided by eBPF) and access shared data structures such as eBPF maps.
Extended BPF Design/Architecture
eBPF maps are a generic data structure for storage of different data types. Data types are generally treated as binary blobs, so a user just specifies the size of the key and the size of the value at map-creation time. In other words, a key/value for a given map can have an arbitrary structure.
A user process can create multiple maps (with key/value-pairs being opaque bytes of data) and access them via file descriptors. Different eBPF programs can access the same maps in parallel. It’s up to the user process and eBPF program to decide what they store inside maps.
There’s one special map type, called a program array. This type of map stores file descriptors referring to other eBPF programs. When a lookup in the map is performed, the program flow is redirected in-place to the beginning of another eBPF program and does not return back to the calling program. The level of nesting has a fixed limit of 32, so that infinite loops cannot be crafted. At run time, the program file descriptors stored in the map can be modified, so program functionality can be altered based on specific requirements. All programs referred to in a program-array map must have been previously loaded into the kernel via bpf(). If a map lookup fails, the current program continues its execution. See BPF_MAP_TYPE_PROG_ARRAY below for further details.
Generally, eBPF programs are loaded by the user process and automatically unloaded when the process exits. In some cases, for example, tc-bpf(8), the program will continue to stay alive inside the kernel even after the process that loaded the program exits. In that case, the tc subsystem holds a reference to the eBPF program after the file descriptor has been closed by the user-space program. Thus, whether a specific program continues to live inside the kernel depends on how it is further attached to a given kernel subsystem after it was loaded via bpf().
Each eBPF program is a set of instructions that is safe to run until its completion. An in-kernel verifier statically determines that the eBPF program terminates and is safe to execute. During verification, the kernel increments reference counts for each of the maps that the eBPF program uses, so that the attached maps can’t be removed until the program is unloaded.
eBPF programs can be attached to different events. These events can be the arrival of network packets, tracing events, classification events by network queueing disciplines (for eBPF programs attached to a tc(8) classifier), and other types that may be added in the future. A new event triggers execution of the eBPF program, which may store information about the event in eBPF maps. Beyond storing data, eBPF programs may call a fixed set of in-kernel helper functions.
The same eBPF program can be attached to multiple events and different eBPF programs can access the same map:
tracing tracing tracing packet packet packet event A event B event C on eth0 on eth1 on eth2
|
|
–> tracing <–
map_1
Arguments
The operation to be performed by the bpf() system call is determined by the cmd argument. Each operation takes an accompanying argument, provided via attr, which is a pointer to a union of type bpf_attr (see below). The size argument is the size of the union pointed to by attr.
The value provided in cmd is one of the following:
BPF_MAP_CREATE- Create a map and return a file descriptor that refers to the map. The close-on-exec file descriptor flag (see
fcntl(2)) is automatically enabled for the new file descriptor. BPF_MAP_LOOKUP_ELEM- Look up an element by key in a specified map and return its value.
BPF_MAP_UPDATE_ELEM- Create or update an element (key/value pair) in a specified map.
BPF_MAP_DELETE_ELEM- Look up and delete an element by key in a specified map.
BPF_MAP_GET_NEXT_KEY- Look up an element by key in a specified map and return the key of the next element.
BPF_PROG_LOAD- Verify and load an eBPF program, returning a new file descriptor associated with the program. The close-on-exec file descriptor flag (see
fcntl(2)) is automatically enabled for the new file descriptor. - The bpf_attr union consists of various anonymous structures that are used by different
bpf() commands:
union bpf_attr {
eBPF maps
Maps are a generic data structure for storage of different types of data. They allow sharing of data between eBPF kernel programs, and also between kernel and user-space applications.
Each map type has the following attributes:
- *
- type
- *
- maximum number of elements
- *
- key size in bytes
- *
- value size in bytes
The following wrapper functions demonstrate how various bpf() commands can be used to access the maps. The functions use the cmd argument to invoke different operations.
BPF_MAP_CREATE- The
BPF_MAP_CREATEcommand creates a new map, returning a new file descriptor that refers to the map. - int bpf_create_map(enum bpf_map_type map_type,
unsigned int key_size,
unsigned int value_size,
unsigned int max_entries) {
union bpf_attr attr = {
.map_type = map_type,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
}; return bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); } - The new map has the type specified by map_type, and attributes as specified in key_size, value_size, and max_entries. On success, this operation returns a file descriptor. On error, -1 is returned and errno is set to
EINVAL,EPERM, orENOMEM.- The key_size and value_size attributes will be used by the verifier during program loading to check that the program is calling
bpf_map_*_elem() helper functions with a correctly initialized key and to check that the program doesn’t access the map element value beyond the specified value_size. For example, when a map is created with a key_size of 8 and the eBPF program calls- bpf_map_lookup_elem(map_fd, fp – 4)
- the program will be rejected, since the in-kernel helper function
- bpf_map_lookup_elem(map_fd, void *key)
- expects to read 8 bytes from the location pointed to by key, but the fp – 4 (where fp is the top of the stack) starting address will cause out-of-bounds stack access.
- Similarly, when a map is created with a value_size of 1 and the eBPF program contains
- value = bpf_map_lookup_elem(…); *(u32 *) value = 1;
- the program will be rejected, since it accesses the value pointer beyond the specified 1 byte value_size limit.
- Currently, the following values are supported for map_type:
- enum bpf_map_type {
BPF_MAP_TYPE_UNSPEC, /* Reserve 0 as invalid map type */
BPF_MAP_TYPE_HASH,
BPF_MAP_TYPE_ARRAY,
BPF_MAP_TYPE_PROG_ARRAY,
BPF_MAP_TYPE_PERF_EVENT_ARRAY,
BPF_MAP_TYPE_PERCPU_HASH,
BPF_MAP_TYPE_PERCPU_ARRAY,
BPF_MAP_TYPE_STACK_TRACE,
BPF_MAP_TYPE_CGROUP_ARRAY,
BPF_MAP_TYPE_LRU_HASH,
BPF_MAP_TYPE_LRU_PERCPU_HASH,
BPF_MAP_TYPE_LPM_TRIE,
BPF_MAP_TYPE_ARRAY_OF_MAPS,
BPF_MAP_TYPE_HASH_OF_MAPS,
BPF_MAP_TYPE_DEVMAP,
BPF_MAP_TYPE_SOCKMAP,
BPF_MAP_TYPE_CPUMAP,
BPF_MAP_TYPE_XSKMAP,
BPF_MAP_TYPE_SOCKHASH,
BPF_MAP_TYPE_CGROUP_STORAGE,
BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE,
BPF_MAP_TYPE_QUEUE,
BPF_MAP_TYPE_STACK,
/* See /usr/include/linux/bpf.h for the full list. */ }; - map_type selects one of the available map implementations in the kernel. For all map types, eBPF programs access maps with the same
bpf_map_lookup_elem() andbpf_map_update_elem() helper functions. Further details of the various map types are given below.BPF_MAP_LOOKUP_ELEM- The
BPF_MAP_LOOKUP_ELEMcommand looks up an element with a given key in the map referred to by the file descriptor fd.- int bpf_lookup_elem(int fd, const void *key, void *value) {
union bpf_attr attr = {
.map_fd = fd,
.key = ptr_to_u64(key),
.value = ptr_to_u64(value),
}; return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); } - If an element is found, the operation returns zero and stores the element’s value into value, which must point to a buffer of value_size bytes.
- If no element is found, the operation returns -1 and sets errno to
ENOENT.BPF_MAP_UPDATE_ELEM- The
BPF_MAP_UPDATE_ELEMcommand creates or updates an element with a given key/value in the map referred to by the file descriptor fd.- int bpf_update_elem(int fd, const void *key, const void *value,
uint64_t flags) {
union bpf_attr attr = {
.map_fd = fd,
.key = ptr_to_u64(key),
.value = ptr_to_u64(value),
.flags = flags,
