Blame man2/bpf.2

Packit 7cfc04
.\" Copyright (C) 2015 Alexei Starovoitov <ast@kernel.org>
Packit 7cfc04
.\" and Copyright (C) 2015 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH BPF 2 2018-02-02 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
bpf \- perform a command on an extended BPF map or program
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <linux/bpf.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int bpf(int " cmd ", union bpf_attr *" attr ", unsigned int " size ");
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
system call performs a range of operations related to extended
Packit 7cfc04
Berkeley Packet Filters.
Packit 7cfc04
Extended BPF (or eBPF) is similar to
Packit 7cfc04
the original ("classic") BPF (cBPF) used to filter network packets.
Packit 7cfc04
For both cBPF and eBPF programs,
Packit 7cfc04
the kernel statically analyzes the programs before loading them,
Packit 7cfc04
in order to ensure that they cannot harm the running system.
Packit 7cfc04
.PP
Packit 7cfc04
eBPF extends cBPF in multiple ways, including the ability to call
Packit 7cfc04
a fixed set of in-kernel helper functions
Packit 7cfc04
.\" See 'enum bpf_func_id' in include/uapi/linux/bpf.h
Packit 7cfc04
(via the
Packit 7cfc04
.B BPF_CALL
Packit 7cfc04
opcode extension provided by eBPF)
Packit 7cfc04
and access shared data structures such as eBPF maps.
Packit 7cfc04
.\"
Packit 7cfc04
.SS Extended BPF Design/Architecture
Packit 7cfc04
eBPF maps are a generic data structure for storage of different data types.
Packit 7cfc04
Data types are generally treated as binary blobs, so a user just specifies
Packit 7cfc04
the size of the key and the size of the value at map-creation time.
Packit 7cfc04
In other words, a key/value for a given map can have an arbitrary structure.
Packit 7cfc04
.PP
Packit 7cfc04
A user process can create multiple maps (with key/value-pairs being
Packit 7cfc04
opaque bytes of data) and access them via file descriptors.
Packit 7cfc04
Different eBPF programs can access the same maps in parallel.
Packit 7cfc04
It's up to the user process and eBPF program to decide what they store
Packit 7cfc04
inside maps.
Packit 7cfc04
.PP
Packit 7cfc04
There's one special map type, called a program array.
Packit 7cfc04
This type of map stores file descriptors referring to other eBPF programs.
Packit 7cfc04
When a lookup in the map is performed, the program flow is
Packit 7cfc04
redirected in-place to the beginning of another eBPF program and does not
Packit 7cfc04
return back to the calling program.
Packit 7cfc04
The level of nesting has a fixed limit of 32,
Packit 7cfc04
.\" Defined by the kernel constant MAX_TAIL_CALL_CNT in include/linux/bpf.h
Packit 7cfc04
so that infinite loops cannot be crafted.
Packit 7cfc04
At runtime, the program file descriptors stored in the map can be modified,
Packit 7cfc04
so program functionality can be altered based on specific requirements.
Packit 7cfc04
All programs referred to in a program-array map must
Packit 7cfc04
have been previously loaded into the kernel via
Packit 7cfc04
.BR bpf ().
Packit 7cfc04
If a map lookup fails, the current program continues its execution.
Packit 7cfc04
See
Packit 7cfc04
.B BPF_MAP_TYPE_PROG_ARRAY
Packit 7cfc04
below for further details.
Packit 7cfc04
.PP
Packit 7cfc04
Generally, eBPF programs are loaded by the user process and automatically
Packit 7cfc04
unloaded when the process exits.
Packit 7cfc04
In some cases, for example,
Packit 7cfc04
.BR tc-bpf (8),
Packit 7cfc04
the program will continue to stay alive inside the kernel even after the
Packit 7cfc04
process that loaded the program exits.
Packit 7cfc04
In that case,
Packit 7cfc04
the tc subsystem holds a reference to the eBPF program after the
Packit 7cfc04
file descriptor has been closed by the user-space program.
Packit 7cfc04
Thus, whether a specific program continues to live inside the kernel
Packit 7cfc04
depends on how it is further attached to a given kernel subsystem
Packit 7cfc04
after it was loaded via
Packit 7cfc04
.BR bpf ().
Packit 7cfc04
.PP
Packit 7cfc04
Each eBPF program is a set of instructions that is safe to run until
Packit 7cfc04
its completion.
Packit 7cfc04
An in-kernel verifier statically determines that the eBPF program
Packit 7cfc04
terminates and is safe to execute.
Packit 7cfc04
During verification, the kernel increments reference counts for each of
Packit 7cfc04
the maps that the eBPF program uses,
Packit 7cfc04
so that the attached maps can't be removed until the program is unloaded.
Packit 7cfc04
.PP
Packit 7cfc04
eBPF programs can be attached to different events.
Packit 7cfc04
These events can be the arrival of network packets, tracing
Packit 7cfc04
events, classification events by network queueing  disciplines
Packit 7cfc04
(for eBPF programs attached to a
Packit 7cfc04
.BR tc (8)
Packit 7cfc04
classifier), and other types that may be added in the future.
Packit 7cfc04
A new event triggers execution of the eBPF program, which
Packit 7cfc04
may store information about the event in eBPF maps.
Packit 7cfc04
Beyond storing data, eBPF programs may call a fixed set of
Packit 7cfc04
in-kernel helper functions.
Packit 7cfc04
.PP
Packit 7cfc04
The same eBPF program can be attached to multiple events and different
Packit 7cfc04
eBPF programs can access the same map:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
tracing     tracing    tracing    packet      packet     packet
Packit 7cfc04
event A     event B    event C    on eth0     on eth1    on eth2
Packit 7cfc04
 |             |         |          |           |          ^
Packit 7cfc04
 |             |         |          |           v          |
Packit 7cfc04
 --> tracing <--     tracing      socket    tc ingress   tc egress
Packit 7cfc04
      prog_1          prog_2      prog_3    classifier    action
Packit 7cfc04
      |  |              |           |         prog_4      prog_5
Packit 7cfc04
   |---  -----|  |------|          map_3        |           |
Packit 7cfc04
 map_1       map_2                              --| map_4 |--
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.\"
Packit 7cfc04
.SS Arguments
Packit 7cfc04
The operation to be performed by the
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
system call is determined by the
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument.
Packit 7cfc04
Each operation takes an accompanying argument,
Packit 7cfc04
provided via
Packit 7cfc04
.IR attr ,
Packit 7cfc04
which is a pointer to a union of type
Packit 7cfc04
.IR bpf_attr
Packit 7cfc04
(see below).
Packit 7cfc04
The
Packit 7cfc04
.I size
Packit 7cfc04
argument is the size of the union pointed to by
Packit 7cfc04
.IR attr .
Packit 7cfc04
.PP
Packit 7cfc04
The value provided in
Packit 7cfc04
.IR cmd
Packit 7cfc04
is one of the following:
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_CREATE
Packit 7cfc04
Create a map and return a file descriptor that refers to the map.
Packit 7cfc04
The close-on-exec file descriptor flag (see
Packit 7cfc04
.BR fcntl (2))
Packit 7cfc04
is automatically enabled for the new file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_LOOKUP_ELEM
Packit 7cfc04
Look up an element by key in a specified map and return its value.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_UPDATE_ELEM
Packit 7cfc04
Create or update an element (key/value pair) in a specified map.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_DELETE_ELEM
Packit 7cfc04
Look up and delete an element by key in a specified map.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_GET_NEXT_KEY
Packit 7cfc04
Look up an element by key in a specified map and return the key
Packit 7cfc04
of the next element.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_PROG_LOAD
Packit 7cfc04
Verify and load an eBPF program,
Packit 7cfc04
returning a new file descriptor associated with the program.
Packit 7cfc04
The close-on-exec file descriptor flag (see
Packit 7cfc04
.BR fcntl (2))
Packit 7cfc04
is automatically enabled for the new file descriptor.
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I bpf_attr
Packit 7cfc04
union consists of various anonymous structures that are used by different
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
commands:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
union bpf_attr {
Packit 7cfc04
    struct {    /* Used by BPF_MAP_CREATE */
Packit 7cfc04
        __u32         map_type;
Packit 7cfc04
        __u32         key_size;    /* size of key in bytes */
Packit 7cfc04
        __u32         value_size;  /* size of value in bytes */
Packit 7cfc04
        __u32         max_entries; /* maximum number of entries
Packit 7cfc04
                                      in a map */
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    struct {    /* Used by BPF_MAP_*_ELEM and BPF_MAP_GET_NEXT_KEY
Packit 7cfc04
                   commands */
Packit 7cfc04
        __u32         map_fd;
Packit 7cfc04
        __aligned_u64 key;
Packit 7cfc04
        union {
Packit 7cfc04
            __aligned_u64 value;
Packit 7cfc04
            __aligned_u64 next_key;
Packit 7cfc04
        };
Packit 7cfc04
        __u64         flags;
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    struct {    /* Used by BPF_PROG_LOAD */
Packit 7cfc04
        __u32         prog_type;
Packit 7cfc04
        __u32         insn_cnt;
Packit 7cfc04
        __aligned_u64 insns;      /* 'const struct bpf_insn *' */
Packit 7cfc04
        __aligned_u64 license;    /* 'const char *' */
Packit 7cfc04
        __u32         log_level;  /* verbosity level of verifier */
Packit 7cfc04
        __u32         log_size;   /* size of user buffer */
Packit 7cfc04
        __aligned_u64 log_buf;    /* user supplied 'char *'
Packit 7cfc04
                                     buffer */
Packit 7cfc04
        __u32         kern_version;
Packit 7cfc04
                                  /* checked when prog_type=kprobe
Packit 7cfc04
                                     (since Linux 4.1) */
Packit 7cfc04
.\"                 commit 2541517c32be2531e0da59dfd7efc1ce844644f5
Packit 7cfc04
    };
Packit 7cfc04
} __attribute__((aligned(8)));
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.\"
Packit 7cfc04
.SS eBPF maps
Packit 7cfc04
Maps are a generic data structure for storage of different types of data.
Packit 7cfc04
They allow sharing of data between eBPF kernel programs,
Packit 7cfc04
and also between kernel and user-space applications.
Packit 7cfc04
.PP
Packit 7cfc04
Each map type has the following attributes:
Packit 7cfc04
.IP * 3
Packit 7cfc04
type
Packit 7cfc04
.IP *
Packit 7cfc04
maximum number of elements
Packit 7cfc04
.IP *
Packit 7cfc04
key size in bytes
Packit 7cfc04
.IP *
Packit 7cfc04
value size in bytes
Packit 7cfc04
.PP
Packit 7cfc04
The following wrapper functions demonstrate how various
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
commands can be used to access the maps.
Packit 7cfc04
The functions use the
Packit 7cfc04
.IR cmd
Packit 7cfc04
argument to invoke different operations.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_CREATE
Packit 7cfc04
The
Packit 7cfc04
.B BPF_MAP_CREATE
Packit 7cfc04
command creates a new map,
Packit 7cfc04
returning a new file descriptor that refers to the map.
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int
Packit 7cfc04
bpf_create_map(enum bpf_map_type map_type,
Packit 7cfc04
               unsigned int key_size,
Packit 7cfc04
               unsigned int value_size,
Packit 7cfc04
               unsigned int max_entries)
Packit 7cfc04
{
Packit 7cfc04
    union bpf_attr attr = {
Packit 7cfc04
        .map_type    = map_type,
Packit 7cfc04
        .key_size    = key_size,
Packit 7cfc04
        .value_size  = value_size,
Packit 7cfc04
        .max_entries = max_entries
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The new map has the type specified by
Packit 7cfc04
.IR map_type ,
Packit 7cfc04
and attributes as specified in
Packit 7cfc04
.IR key_size ,
Packit 7cfc04
.IR value_size ,
Packit 7cfc04
and
Packit 7cfc04
.IR max_entries .
Packit 7cfc04
On success, this operation returns a file descriptor.
Packit 7cfc04
On error, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR EINVAL ,
Packit 7cfc04
.BR EPERM ,
Packit 7cfc04
or
Packit 7cfc04
.BR ENOMEM .
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I key_size
Packit 7cfc04
and
Packit 7cfc04
.I value_size
Packit 7cfc04
attributes will be used by the verifier during program loading
Packit 7cfc04
to check that the program is calling
Packit 7cfc04
.BR bpf_map_*_elem ()
Packit 7cfc04
helper functions with a correctly initialized
Packit 7cfc04
.I key
Packit 7cfc04
and to check that the program doesn't access the map element
Packit 7cfc04
.I value
Packit 7cfc04
beyond the specified
Packit 7cfc04
.IR value_size .
Packit 7cfc04
For example, when a map is created with a
Packit 7cfc04
.IR key_size
Packit 7cfc04
of 8 and the eBPF program calls
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
bpf_map_lookup_elem(map_fd, fp - 4)
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
the program will be rejected,
Packit 7cfc04
since the in-kernel helper function
Packit 7cfc04
.IP
Packit 7cfc04
.EX
Packit 7cfc04
    bpf_map_lookup_elem(map_fd, void *key)
Packit 7cfc04
.EE
Packit 7cfc04
.IP
Packit 7cfc04
expects to read 8 bytes from the location pointed to by
Packit 7cfc04
.IR key ,
Packit 7cfc04
but the
Packit 7cfc04
.IR "fp\ -\ 4"
Packit 7cfc04
(where
Packit 7cfc04
.I fp
Packit 7cfc04
is the top of the stack)
Packit 7cfc04
starting address will cause out-of-bounds stack access.
Packit 7cfc04
.IP
Packit 7cfc04
Similarly, when a map is created with a
Packit 7cfc04
.I value_size
Packit 7cfc04
of 1 and the eBPF program contains
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
value = bpf_map_lookup_elem(...);
Packit 7cfc04
*(u32 *) value = 1;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
the program will be rejected, since it accesses the
Packit 7cfc04
.I value
Packit 7cfc04
pointer beyond the specified 1 byte
Packit 7cfc04
.I value_size
Packit 7cfc04
limit.
Packit 7cfc04
.IP
Packit 7cfc04
Currently, the following values are supported for
Packit 7cfc04
.IR map_type :
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
enum bpf_map_type {
Packit 7cfc04
    BPF_MAP_TYPE_UNSPEC,  /* Reserve 0 as invalid map type */
Packit 7cfc04
    BPF_MAP_TYPE_HASH,
Packit 7cfc04
    BPF_MAP_TYPE_ARRAY,
Packit 7cfc04
    BPF_MAP_TYPE_PROG_ARRAY,
Packit 7cfc04
    BPF_MAP_TYPE_PERF_EVENT_ARRAY,
Packit 7cfc04
    BPF_MAP_TYPE_PERCPU_HASH,
Packit 7cfc04
    BPF_MAP_TYPE_PERCPU_ARRAY,
Packit 7cfc04
    BPF_MAP_TYPE_STACK_TRACE,
Packit 7cfc04
    BPF_MAP_TYPE_CGROUP_ARRAY,
Packit 7cfc04
    BPF_MAP_TYPE_LRU_HASH,
Packit 7cfc04
    BPF_MAP_TYPE_LRU_PERCPU_HASH,
Packit 7cfc04
    BPF_MAP_TYPE_LPM_TRIE,
Packit 7cfc04
    BPF_MAP_TYPE_ARRAY_OF_MAPS,
Packit 7cfc04
    BPF_MAP_TYPE_HASH_OF_MAPS,
Packit 7cfc04
    BPF_MAP_TYPE_DEVMAP,
Packit 7cfc04
    BPF_MAP_TYPE_SOCKMAP,
Packit 7cfc04
    BPF_MAP_TYPE_CPUMAP,
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
.I map_type
Packit 7cfc04
selects one of the available map implementations in the kernel.
Packit 7cfc04
.\" FIXME We need an explanation of why one might choose each of
Packit 7cfc04
.\" these map implementations
Packit 7cfc04
For all map types,
Packit 7cfc04
eBPF programs access maps with the same
Packit 7cfc04
.BR bpf_map_lookup_elem ()
Packit 7cfc04
and
Packit 7cfc04
.BR bpf_map_update_elem ()
Packit 7cfc04
helper functions.
Packit 7cfc04
Further details of the various map types are given below.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_LOOKUP_ELEM
Packit 7cfc04
The
Packit 7cfc04
.B BPF_MAP_LOOKUP_ELEM
Packit 7cfc04
command looks up an element with a given
Packit 7cfc04
.I key
Packit 7cfc04
in the map referred to by the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int
Packit 7cfc04
bpf_lookup_elem(int fd, const void *key, void *value)
Packit 7cfc04
{
Packit 7cfc04
    union bpf_attr attr = {
Packit 7cfc04
        .map_fd = fd,
Packit 7cfc04
        .key    = ptr_to_u64(key),
Packit 7cfc04
        .value  = ptr_to_u64(value),
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
If an element is found,
Packit 7cfc04
the operation returns zero and stores the element's value into
Packit 7cfc04
.IR value ,
Packit 7cfc04
which must point to a buffer of
Packit 7cfc04
.I value_size
Packit 7cfc04
bytes.
Packit 7cfc04
.IP
Packit 7cfc04
If no element is found, the operation returns \-1 and sets
Packit 7cfc04
.I errno
Packit 7cfc04
to
Packit 7cfc04
.BR ENOENT .
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_UPDATE_ELEM
Packit 7cfc04
The
Packit 7cfc04
.B BPF_MAP_UPDATE_ELEM
Packit 7cfc04
command
Packit 7cfc04
creates or updates an element with a given
Packit 7cfc04
.I key/value
Packit 7cfc04
in the map referred to by the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int
Packit 7cfc04
bpf_update_elem(int fd, const void *key, const void *value,
Packit 7cfc04
                uint64_t flags)
Packit 7cfc04
{
Packit 7cfc04
    union bpf_attr attr = {
Packit 7cfc04
        .map_fd = fd,
Packit 7cfc04
        .key    = ptr_to_u64(key),
Packit 7cfc04
        .value  = ptr_to_u64(value),
Packit 7cfc04
        .flags  = flags,
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument should be specified as one of the following:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_ANY
Packit 7cfc04
Create a new element or update an existing element.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_NOEXIST
Packit 7cfc04
Create a new element only if it did not exist.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_EXIST
Packit 7cfc04
Update an existing element.
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
On success, the operation returns zero.
Packit 7cfc04
On error, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR EINVAL ,
Packit 7cfc04
.BR EPERM ,
Packit 7cfc04
.BR ENOMEM ,
Packit 7cfc04
or
Packit 7cfc04
.BR E2BIG .
Packit 7cfc04
.B E2BIG
Packit 7cfc04
indicates that the number of elements in the map reached the
Packit 7cfc04
.I max_entries
Packit 7cfc04
limit specified at map creation time.
Packit 7cfc04
.B EEXIST
Packit 7cfc04
will be returned if
Packit 7cfc04
.I flags
Packit 7cfc04
specifies
Packit 7cfc04
.B BPF_NOEXIST
Packit 7cfc04
and the element with
Packit 7cfc04
.I key
Packit 7cfc04
already exists in the map.
Packit 7cfc04
.B ENOENT
Packit 7cfc04
will be returned if
Packit 7cfc04
.I flags
Packit 7cfc04
specifies
Packit 7cfc04
.B BPF_EXIST
Packit 7cfc04
and the element with
Packit 7cfc04
.I key
Packit 7cfc04
doesn't exist in the map.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_DELETE_ELEM
Packit 7cfc04
The
Packit 7cfc04
.B BPF_MAP_DELETE_ELEM
Packit 7cfc04
command
Packit 7cfc04
deleted the element whose key is
Packit 7cfc04
.I key
Packit 7cfc04
from the map referred to by the file descriptor
Packit 7cfc04
.IR fd .
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int
Packit 7cfc04
bpf_delete_elem(int fd, const void *key)
Packit 7cfc04
{
Packit 7cfc04
    union bpf_attr attr = {
Packit 7cfc04
        .map_fd = fd,
Packit 7cfc04
        .key    = ptr_to_u64(key),
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    return bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
On success, zero is returned.
Packit 7cfc04
If the element is not found, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR ENOENT .
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_GET_NEXT_KEY
Packit 7cfc04
The
Packit 7cfc04
.B BPF_MAP_GET_NEXT_KEY
Packit 7cfc04
command looks up an element by
Packit 7cfc04
.I key
Packit 7cfc04
in the map referred to by the file descriptor
Packit 7cfc04
.IR fd
Packit 7cfc04
and sets the
Packit 7cfc04
.I next_key
Packit 7cfc04
pointer to the key of the next element.
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int
Packit 7cfc04
bpf_get_next_key(int fd, const void *key, void *next_key)
Packit 7cfc04
{
Packit 7cfc04
    union bpf_attr attr = {
Packit 7cfc04
        .map_fd   = fd,
Packit 7cfc04
        .key      = ptr_to_u64(key),
Packit 7cfc04
        .next_key = ptr_to_u64(next_key),
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
If
Packit 7cfc04
.I key
Packit 7cfc04
is found, the operation returns zero and sets the
Packit 7cfc04
.I next_key
Packit 7cfc04
pointer to the key of the next element.
Packit 7cfc04
If
Packit 7cfc04
.I key
Packit 7cfc04
is not found, the operation returns zero and sets the
Packit 7cfc04
.I next_key
Packit 7cfc04
pointer to the key of the first element.
Packit 7cfc04
If
Packit 7cfc04
.I key
Packit 7cfc04
is the last element, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR ENOENT .
Packit 7cfc04
Other possible
Packit 7cfc04
.I errno
Packit 7cfc04
values are
Packit 7cfc04
.BR ENOMEM ,
Packit 7cfc04
.BR EFAULT ,
Packit 7cfc04
.BR EPERM ,
Packit 7cfc04
and
Packit 7cfc04
.BR EINVAL .
Packit 7cfc04
This method can be used to iterate over all elements in the map.
Packit 7cfc04
.TP
Packit 7cfc04
.B close(map_fd)
Packit 7cfc04
Delete the map referred to by the file descriptor
Packit 7cfc04
.IR map_fd .
Packit 7cfc04
When the user-space program that created a map exits, all maps will
Packit 7cfc04
be deleted automatically (but see NOTES).
Packit 7cfc04
.\"
Packit 7cfc04
.SS eBPF map types
Packit 7cfc04
The following map types are supported:
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_TYPE_HASH
Packit 7cfc04
.\" commit 0f8e4bd8a1fc8c4185f1630061d0a1f2d197a475
Packit 7cfc04
Hash-table maps have the following characteristics:
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
Maps are created and destroyed by user-space programs.
Packit 7cfc04
Both user-space and eBPF programs
Packit 7cfc04
can perform lookup, update, and delete operations.
Packit 7cfc04
.IP *
Packit 7cfc04
The kernel takes care of allocating and freeing key/value pairs.
Packit 7cfc04
.IP *
Packit 7cfc04
The
Packit 7cfc04
.BR map_update_elem ()
Packit 7cfc04
helper will fail to insert new element when the
Packit 7cfc04
.I max_entries
Packit 7cfc04
limit is reached.
Packit 7cfc04
(This ensures that eBPF programs cannot exhaust memory.)
Packit 7cfc04
.IP *
Packit 7cfc04
.BR map_update_elem ()
Packit 7cfc04
replaces existing elements atomically.
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
Hash-table maps are
Packit 7cfc04
optimized for speed of lookup.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_TYPE_ARRAY
Packit 7cfc04
.\" commit 28fbcfa08d8ed7c5a50d41a0433aad222835e8e3
Packit 7cfc04
Array maps have the following characteristics:
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
Optimized for fastest possible lookup.
Packit 7cfc04
In the future the verifier/JIT compiler
Packit 7cfc04
may recognize lookup() operations that employ a constant key
Packit 7cfc04
and optimize it into constant pointer.
Packit 7cfc04
It is possible to optimize a non-constant
Packit 7cfc04
key into direct pointer arithmetic as well, since pointers and
Packit 7cfc04
.I value_size
Packit 7cfc04
are constant for the life of the eBPF program.
Packit 7cfc04
In other words,
Packit 7cfc04
.BR array_map_lookup_elem ()
Packit 7cfc04
may be 'inlined' by the verifier/JIT compiler
Packit 7cfc04
while preserving concurrent access to this map from user space.
Packit 7cfc04
.IP *
Packit 7cfc04
All array elements pre-allocated and zero initialized at init time
Packit 7cfc04
.IP *
Packit 7cfc04
The key is an array index, and must be exactly four bytes.
Packit 7cfc04
.IP *
Packit 7cfc04
.BR map_delete_elem ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR EINVAL ,
Packit 7cfc04
since elements cannot be deleted.
Packit 7cfc04
.IP *
Packit 7cfc04
.BR map_update_elem ()
Packit 7cfc04
replaces elements in a
Packit 7cfc04
.B nonatomic
Packit 7cfc04
fashion;
Packit 7cfc04
for atomic updates, a hash-table map should be used instead.
Packit 7cfc04
There is however one special case that can also be used with arrays:
Packit 7cfc04
the atomic built-in
Packit 7cfc04
.BR __sync_fetch_and_add()
Packit 7cfc04
can be used on 32 and 64 bit atomic counters.
Packit 7cfc04
For example, it can be
Packit 7cfc04
applied on the whole value itself if it represents a single counter,
Packit 7cfc04
or in case of a structure containing multiple counters, it could be
Packit 7cfc04
used on individual counters.
Packit 7cfc04
This is quite often useful for aggregation and accounting of events.
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
Among the uses for array maps are the following:
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
As "global" eBPF variables: an array of 1 element whose key is (index) 0
Packit 7cfc04
and where the value is a collection of 'global' variables which
Packit 7cfc04
eBPF programs can use to keep state between events.
Packit 7cfc04
.IP *
Packit 7cfc04
Aggregation of tracing events into a fixed set of buckets.
Packit 7cfc04
.IP *
Packit 7cfc04
Accounting of networking events, for example, number of packets and packet
Packit 7cfc04
sizes.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.BR BPF_MAP_TYPE_PROG_ARRAY " (since Linux 4.2)"
Packit 7cfc04
A program array map is a special kind of array map whose map values
Packit 7cfc04
contain only file descriptors referring to other eBPF programs.
Packit 7cfc04
Thus, both the
Packit 7cfc04
.I key_size
Packit 7cfc04
and
Packit 7cfc04
.I value_size
Packit 7cfc04
must be exactly four bytes.
Packit 7cfc04
This map is used in conjunction with the
Packit 7cfc04
.BR bpf_tail_call ()
Packit 7cfc04
helper.
Packit 7cfc04
.IP
Packit 7cfc04
This means that an eBPF program with a program array map attached to it
Packit 7cfc04
can call from kernel side into
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
void bpf_tail_call(void *context, void *prog_map,
Packit 7cfc04
                   unsigned int index);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
and therefore replace its own program flow with the one from the program
Packit 7cfc04
at the given program array slot, if present.
Packit 7cfc04
This can be regarded as kind of a jump table to a different eBPF program.
Packit 7cfc04
The invoked program will then reuse the same stack.
Packit 7cfc04
When a jump into the new program has been performed,
Packit 7cfc04
it won't return to the old program anymore.
Packit 7cfc04
.IP
Packit 7cfc04
If no eBPF program is found at the given index of the program array
Packit 7cfc04
(because the map slot doesn't contain a valid program file descriptor,
Packit 7cfc04
the specified lookup index/key is out of bounds,
Packit 7cfc04
or the limit of 32
Packit 7cfc04
.\" MAX_TAIL_CALL_CNT
Packit 7cfc04
nested calls has been exceed),
Packit 7cfc04
execution continues with the current eBPF program.
Packit 7cfc04
This can be used as a fall-through for default cases.
Packit 7cfc04
.IP
Packit 7cfc04
A program array map is useful, for example, in tracing or networking, to
Packit 7cfc04
handle individual system calls or protocols in their own subprograms and
Packit 7cfc04
use their identifiers as an individual map index.
Packit 7cfc04
This approach may result in performance benefits,
Packit 7cfc04
and also makes it possible to overcome the maximum
Packit 7cfc04
instruction limit of a single eBPF program.
Packit 7cfc04
In dynamic environments,
Packit 7cfc04
a user-space daemon might atomically replace individual subprograms
Packit 7cfc04
at run-time with newer versions to alter overall program behavior,
Packit 7cfc04
for instance, if global policies change.
Packit 7cfc04
.\"
Packit 7cfc04
.SS eBPF programs
Packit 7cfc04
The
Packit 7cfc04
.B BPF_PROG_LOAD
Packit 7cfc04
command is used to load an eBPF program into the kernel.
Packit 7cfc04
The return value for this command is a new file descriptor associated
Packit 7cfc04
with this eBPF program.
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
char bpf_log_buf[LOG_BUF_SIZE];
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
bpf_prog_load(enum bpf_prog_type type,
Packit 7cfc04
              const struct bpf_insn *insns, int insn_cnt,
Packit 7cfc04
              const char *license)
Packit 7cfc04
{
Packit 7cfc04
    union bpf_attr attr = {
Packit 7cfc04
        .prog_type = type,
Packit 7cfc04
        .insns     = ptr_to_u64(insns),
Packit 7cfc04
        .insn_cnt  = insn_cnt,
Packit 7cfc04
        .license   = ptr_to_u64(license),
Packit 7cfc04
        .log_buf   = ptr_to_u64(bpf_log_buf),
Packit 7cfc04
        .log_size  = LOG_BUF_SIZE,
Packit 7cfc04
        .log_level = 1,
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.I prog_type
Packit 7cfc04
is one of the available program types:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
enum bpf_prog_type {
Packit 7cfc04
    BPF_PROG_TYPE_UNSPEC,        /* Reserve 0 as invalid
Packit 7cfc04
                                    program type */
Packit 7cfc04
    BPF_PROG_TYPE_SOCKET_FILTER,
Packit 7cfc04
    BPF_PROG_TYPE_KPROBE,
Packit 7cfc04
    BPF_PROG_TYPE_SCHED_CLS,
Packit 7cfc04
    BPF_PROG_TYPE_SCHED_ACT,
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
For further details of eBPF program types, see below.
Packit 7cfc04
.PP
Packit 7cfc04
The remaining fields of
Packit 7cfc04
.I bpf_attr
Packit 7cfc04
are set as follows:
Packit 7cfc04
.IP * 3
Packit 7cfc04
.I insns
Packit 7cfc04
is an array of
Packit 7cfc04
.I "struct bpf_insn"
Packit 7cfc04
instructions.
Packit 7cfc04
.IP *
Packit 7cfc04
.I insn_cnt
Packit 7cfc04
is the number of instructions in the program referred to by
Packit 7cfc04
.IR insns .
Packit 7cfc04
.IP *
Packit 7cfc04
.I license
Packit 7cfc04
is a license string, which must be GPL compatible to call helper functions
Packit 7cfc04
marked
Packit 7cfc04
.IR gpl_only .
Packit 7cfc04
(The licensing rules are the same as for kernel modules,
Packit 7cfc04
so that also dual licenses, such as "Dual BSD/GPL", may be used.)
Packit 7cfc04
.IP *
Packit 7cfc04
.I log_buf
Packit 7cfc04
is a pointer to a caller-allocated buffer in which the in-kernel
Packit 7cfc04
verifier can store the verification log.
Packit 7cfc04
This log is a multi-line string that can be checked by
Packit 7cfc04
the program author in order to understand how the verifier came to
Packit 7cfc04
the conclusion that the eBPF program is unsafe.
Packit 7cfc04
The format of the output can change at any time as the verifier evolves.
Packit 7cfc04
.IP *
Packit 7cfc04
.I log_size
Packit 7cfc04
size of the buffer pointed to by
Packit 7cfc04
.IR log_buf .
Packit 7cfc04
If the size of the buffer is not large enough to store all
Packit 7cfc04
verifier messages, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR ENOSPC .
Packit 7cfc04
.IP *
Packit 7cfc04
.I log_level
Packit 7cfc04
verbosity level of the verifier.
Packit 7cfc04
A value of zero means that the verifier will not provide a log;
Packit 7cfc04
in this case,
Packit 7cfc04
.I log_buf
Packit 7cfc04
must be a NULL pointer, and
Packit 7cfc04
.I log_size
Packit 7cfc04
must be zero.
Packit 7cfc04
.PP
Packit 7cfc04
Applying
Packit 7cfc04
.BR close (2)
Packit 7cfc04
to the file descriptor returned by
Packit 7cfc04
.B BPF_PROG_LOAD
Packit 7cfc04
will unload the eBPF program (but see NOTES).
Packit 7cfc04
.PP
Packit 7cfc04
Maps are accessible from eBPF programs and are used to exchange data between
Packit 7cfc04
eBPF programs and between eBPF programs and user-space programs.
Packit 7cfc04
For example,
Packit 7cfc04
eBPF programs can process various events (like kprobe, packets) and
Packit 7cfc04
store their data into a map,
Packit 7cfc04
and user-space programs can then fetch data from the map.
Packit 7cfc04
Conversely, user-space programs can use a map as a configuration mechanism,
Packit 7cfc04
populating the map with values checked by the eBPF program,
Packit 7cfc04
which then modifies its behavior on the fly according to those values.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS eBPF program types
Packit 7cfc04
The eBPF program type
Packit 7cfc04
.RI ( prog_type )
Packit 7cfc04
determines the subset of kernel helper functions that the program
Packit 7cfc04
may call.
Packit 7cfc04
The program type also determines the program input (context)\(emthe
Packit 7cfc04
format of
Packit 7cfc04
.I "struct bpf_context"
Packit 7cfc04
(which is the data blob passed into the eBPF program as the first argument).
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME
Packit 7cfc04
.\" Somewhere in this page we need a general introduction to the
Packit 7cfc04
.\" bpf_context. For example, how does a BPF program access the
Packit 7cfc04
.\" context?
Packit 7cfc04
.PP
Packit 7cfc04
For example, a tracing program does not have the exact same
Packit 7cfc04
subset of helper functions as a socket filter program
Packit 7cfc04
(though they may have some helpers in common).
Packit 7cfc04
Similarly,
Packit 7cfc04
the input (context) for a tracing program is a set of register values,
Packit 7cfc04
while for a socket filter it is a network packet.
Packit 7cfc04
.PP
Packit 7cfc04
The set of functions available to eBPF programs of a given type may increase
Packit 7cfc04
in the future.
Packit 7cfc04
.PP
Packit 7cfc04
The following program types are supported:
Packit 7cfc04
.TP
Packit 7cfc04
.BR BPF_PROG_TYPE_SOCKET_FILTER " (since Linux 3.19)"
Packit 7cfc04
Currently, the set of functions for
Packit 7cfc04
.B BPF_PROG_TYPE_SOCKET_FILTER
Packit 7cfc04
is:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
bpf_map_lookup_elem(map_fd, void *key)
Packit 7cfc04
                    /* look up key in a map_fd */
Packit 7cfc04
bpf_map_update_elem(map_fd, void *key, void *value)
Packit 7cfc04
                    /* update key/value */
Packit 7cfc04
bpf_map_delete_elem(map_fd, void *key)
Packit 7cfc04
                    /* delete key in a map_fd */
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I bpf_context
Packit 7cfc04
argument is a pointer to a
Packit 7cfc04
.IR "struct __sk_buff" .
Packit 7cfc04
.\" FIXME: We need some text here to explain how the program
Packit 7cfc04
.\" accesses __sk_buff.
Packit 7cfc04
.\" See 'struct __sk_buff' and commit 9bac3d6d548e5
Packit 7cfc04
.\"
Packit 7cfc04
.\" Alexei commented:
Packit 7cfc04
.\" Actually now in case of SOCKET_FILTER, SCHED_CLS, SCHED_ACT
Packit 7cfc04
.\" the program can now access skb fields.
Packit 7cfc04
.\"
Packit 7cfc04
.TP
Packit 7cfc04
.BR BPF_PROG_TYPE_KPROBE " (since Linux 4.1)
Packit 7cfc04
.\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
Packit 7cfc04
[To be documented]
Packit 7cfc04
.\" FIXME Document this program type
Packit 7cfc04
.\"	  Describe allowed helper functions for this program type
Packit 7cfc04
.\"	  Describe bpf_context for this program type
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME We need text here to describe 'kern_version'
Packit 7cfc04
.TP
Packit 7cfc04
.BR BPF_PROG_TYPE_SCHED_CLS " (since Linux 4.1)
Packit 7cfc04
.\" commit 96be4325f443dbbfeb37d2a157675ac0736531a1
Packit 7cfc04
.\" commit e2e9b6541dd4b31848079da80fe2253daaafb549
Packit 7cfc04
[To be documented]
Packit 7cfc04
.\" FIXME Document this program type
Packit 7cfc04
.\"	  Describe allowed helper functions for this program type
Packit 7cfc04
.\"	  Describe bpf_context for this program type
Packit 7cfc04
.TP
Packit 7cfc04
.BR BPF_PROG_TYPE_SCHED_ACT " (since Linux 4.1)
Packit 7cfc04
.\" commit 94caee8c312d96522bcdae88791aaa9ebcd5f22c
Packit 7cfc04
.\" commit a8cb5f556b567974d75ea29c15181c445c541b1f
Packit 7cfc04
[To be documented]
Packit 7cfc04
.\" FIXME Document this program type
Packit 7cfc04
.\"	  Describe allowed helper functions for this program type
Packit 7cfc04
.\"	  Describe bpf_context for this program type
Packit 7cfc04
.SS Events
Packit 7cfc04
Once a program is loaded, it can be attached to an event.
Packit 7cfc04
Various kernel subsystems have different ways to do so.
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 3.19,
Packit 7cfc04
.\" commit 89aa075832b0da4402acebd698d0411dcc82d03e
Packit 7cfc04
the following call will attach the program
Packit 7cfc04
.I prog_fd
Packit 7cfc04
to the socket
Packit 7cfc04
.IR sockfd ,
Packit 7cfc04
which was created by an earlier call to
Packit 7cfc04
.BR socket (2):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_BPF,
Packit 7cfc04
           &prog_fd, sizeof(prog_fd));
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 4.1,
Packit 7cfc04
.\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
Packit 7cfc04
the following call may be used to attach
Packit 7cfc04
the eBPF program referred to by the file descriptor
Packit 7cfc04
.I prog_fd
Packit 7cfc04
to a perf event file descriptor,
Packit 7cfc04
.IR event_fd ,
Packit 7cfc04
that was created by a previous call to
Packit 7cfc04
.BR perf_event_open (2):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
ioctl(event_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
.EX
Packit 7cfc04
/* bpf+sockets example:
Packit 7cfc04
 * 1. create array map of 256 elements
Packit 7cfc04
 * 2. load program that counts number of packets received
Packit 7cfc04
 *    r0 = skb->data[ETH_HLEN + offsetof(struct iphdr, protocol)]
Packit 7cfc04
 *    map[r0]++
Packit 7cfc04
 * 3. attach prog_fd to raw socket via setsockopt()
Packit 7cfc04
 * 4. print number of received TCP/UDP packets every second
Packit 7cfc04
 */
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char **argv)
Packit 7cfc04
{
Packit 7cfc04
    int sock, map_fd, prog_fd, key;
Packit 7cfc04
    long long value = 0, tcp_cnt, udp_cnt;
Packit 7cfc04
Packit 7cfc04
    map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key),
Packit 7cfc04
                            sizeof(value), 256);
Packit 7cfc04
    if (map_fd < 0) {
Packit 7cfc04
        printf("failed to create map '%s'\\n", strerror(errno));
Packit 7cfc04
        /* likely not run as root */
Packit 7cfc04
        return 1;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    struct bpf_insn prog[] = {
Packit 7cfc04
        BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),        /* r6 = r1 */
Packit 7cfc04
        BPF_LD_ABS(BPF_B, ETH_HLEN + offsetof(struct iphdr, protocol)),
Packit 7cfc04
                                /* r0 = ip->proto */
Packit 7cfc04
        BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
Packit 7cfc04
                                /* *(u32 *)(fp - 4) = r0 */
Packit 7cfc04
        BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),       /* r2 = fp */
Packit 7cfc04
        BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),      /* r2 = r2 - 4 */
Packit 7cfc04
        BPF_LD_MAP_FD(BPF_REG_1, map_fd),           /* r1 = map_fd */
Packit 7cfc04
        BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
Packit 7cfc04
                                /* r0 = map_lookup(r1, r2) */
Packit 7cfc04
        BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
Packit 7cfc04
                                /* if (r0 == 0) goto pc+2 */
Packit 7cfc04
        BPF_MOV64_IMM(BPF_REG_1, 1),                /* r1 = 1 */
Packit 7cfc04
        BPF_XADD(BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0),
Packit 7cfc04
                                /* lock *(u64 *) r0 += r1 */
Packit 7cfc04
.\"                                == atomic64_add
Packit 7cfc04
        BPF_MOV64_IMM(BPF_REG_0, 0),                /* r0 = 0 */
Packit 7cfc04
        BPF_EXIT_INSN(),                            /* return r0 */
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog,
Packit 7cfc04
                            sizeof(prog), "GPL");
Packit 7cfc04
Packit 7cfc04
    sock = open_raw_sock("lo");
Packit 7cfc04
Packit 7cfc04
    assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
Packit 7cfc04
                      sizeof(prog_fd)) == 0);
Packit 7cfc04
Packit 7cfc04
    for (;;) {
Packit 7cfc04
        key = IPPROTO_TCP;
Packit 7cfc04
        assert(bpf_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
Packit 7cfc04
        key = IPPROTO_UDP;
Packit 7cfc04
        assert(bpf_lookup_elem(map_fd, &key, &udp_cnt) == 0);
Packit 7cfc04
        printf("TCP %lld UDP %lld packets\\n", tcp_cnt, udp_cnt);
Packit 7cfc04
        sleep(1);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    return 0;
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.PP
Packit 7cfc04
Some complete working code can be found in the
Packit 7cfc04
.IR samples/bpf
Packit 7cfc04
directory in the kernel source tree.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
For a successful call, the return value depends on the operation:
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_MAP_CREATE
Packit 7cfc04
The new file descriptor associated with the eBPF map.
Packit 7cfc04
.TP
Packit 7cfc04
.B BPF_PROG_LOAD
Packit 7cfc04
The new file descriptor associated with the eBPF program.
Packit 7cfc04
.TP
Packit 7cfc04
All other commands
Packit 7cfc04
Zero.
Packit 7cfc04
.PP
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.BR E2BIG
Packit 7cfc04
The eBPF program is too large or a map reached the
Packit 7cfc04
.I max_entries
Packit 7cfc04
limit (maximum number of elements).
Packit 7cfc04
.TP
Packit 7cfc04
.BR EACCES
Packit 7cfc04
For
Packit 7cfc04
.BR BPF_PROG_LOAD,
Packit 7cfc04
even though all program instructions are valid, the program has been
Packit 7cfc04
rejected because it was deemed unsafe.
Packit 7cfc04
This may be because it may have
Packit 7cfc04
accessed a disallowed memory region or an uninitialized stack/register or
Packit 7cfc04
because the function constraints don't match the actual types or because
Packit 7cfc04
there was a misaligned memory access.
Packit 7cfc04
In this case, it is recommended to call
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
again with
Packit 7cfc04
.I log_level = 1
Packit 7cfc04
and examine
Packit 7cfc04
.I log_buf
Packit 7cfc04
for the specific reason provided by the verifier.
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I fd
Packit 7cfc04
is not an open file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
One of the pointers
Packit 7cfc04
.RI ( key
Packit 7cfc04
or
Packit 7cfc04
.I value
Packit 7cfc04
or
Packit 7cfc04
.I log_buf
Packit 7cfc04
or
Packit 7cfc04
.IR insns )
Packit 7cfc04
is outside the accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The value specified in
Packit 7cfc04
.I cmd
Packit 7cfc04
is not recognized by this kernel.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
For
Packit 7cfc04
.BR BPF_MAP_CREATE ,
Packit 7cfc04
either
Packit 7cfc04
.I map_type
Packit 7cfc04
or attributes are invalid.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
For
Packit 7cfc04
.BR BPF_MAP_*_ELEM
Packit 7cfc04
commands,
Packit 7cfc04
some of the fields of
Packit 7cfc04
.I "union bpf_attr"
Packit 7cfc04
that are not used by this command
Packit 7cfc04
are not set to zero.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
For
Packit 7cfc04
.BR BPF_PROG_LOAD,
Packit 7cfc04
indicates an attempt to load an invalid program.
Packit 7cfc04
eBPF programs can be deemed
Packit 7cfc04
invalid due to unrecognized instructions, the use of reserved fields, jumps
Packit 7cfc04
out of range, infinite loops or calls of unknown functions.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOENT
Packit 7cfc04
For
Packit 7cfc04
.B BPF_MAP_LOOKUP_ELEM
Packit 7cfc04
or
Packit 7cfc04
.BR BPF_MAP_DELETE_ELEM ,
Packit 7cfc04
indicates that the element with the given
Packit 7cfc04
.I key
Packit 7cfc04
was not found.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Cannot allocate sufficient memory.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The call was made without sufficient privilege
Packit 7cfc04
(without the
Packit 7cfc04
.B CAP_SYS_ADMIN
Packit 7cfc04
capability).
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
system call first appeared in Linux 3.18.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
The
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
system call is Linux-specific.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
In the current implementation, all
Packit 7cfc04
.BR bpf ()
Packit 7cfc04
commands require the caller to have the
Packit 7cfc04
.B CAP_SYS_ADMIN
Packit 7cfc04
capability.
Packit 7cfc04
.PP
Packit 7cfc04
eBPF objects (maps and programs) can be shared between processes.
Packit 7cfc04
For example, after
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
the child inherits file descriptors referring to the same eBPF objects.
Packit 7cfc04
In addition, file descriptors referring to eBPF objects can be
Packit 7cfc04
transferred over UNIX domain sockets.
Packit 7cfc04
File descriptors referring to eBPF objects can be duplicated
Packit 7cfc04
in the usual way, using
Packit 7cfc04
.BR dup (2)
Packit 7cfc04
and similar calls.
Packit 7cfc04
An eBPF object is deallocated only after all file descriptors
Packit 7cfc04
referring to the object have been closed.
Packit 7cfc04
.PP
Packit 7cfc04
eBPF programs can be written in a restricted C that is compiled (using the
Packit 7cfc04
.B clang
Packit 7cfc04
compiler) into eBPF bytecode.
Packit 7cfc04
Various features are omitted from this restricted C, such as loops,
Packit 7cfc04
global variables, variadic functions, floating-point numbers,
Packit 7cfc04
and passing structures as function arguments.
Packit 7cfc04
Some examples can be found in the
Packit 7cfc04
.I samples/bpf/*_kern.c
Packit 7cfc04
files in the kernel source tree.
Packit 7cfc04
.\" There are also examples for the tc classifier, in the iproute2
Packit 7cfc04
.\" project, in examples/bpf
Packit 7cfc04
.PP
Packit 7cfc04
The kernel contains a just-in-time (JIT) compiler that translates
Packit 7cfc04
eBPF bytecode into native machine code for better performance.
Packit 7cfc04
The JIT compiler is disabled by default,
Packit 7cfc04
but its operation can be controlled by writing one of the
Packit 7cfc04
following integer strings to the file
Packit 7cfc04
.IR /proc/sys/net/core/bpf_jit_enable :
Packit 7cfc04
.IP 0 3
Packit 7cfc04
Disable JIT compilation (default).
Packit 7cfc04
.IP 1
Packit 7cfc04
Normal compilation.
Packit 7cfc04
.IP 2
Packit 7cfc04
Debugging mode.
Packit 7cfc04
The generated opcodes are dumped in hexadecimal into the kernel log.
Packit 7cfc04
These opcodes can then be disassembled using the program
Packit 7cfc04
.IR tools/net/bpf_jit_disasm.c
Packit 7cfc04
provided in the kernel source tree.
Packit 7cfc04
.PP
Packit 7cfc04
JIT compiler for eBPF is currently available for the x86-64, arm64,
Packit 7cfc04
and s390 architectures.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR seccomp (2),
Packit 7cfc04
.BR socket (7),
Packit 7cfc04
.BR tc (8),
Packit 7cfc04
.BR tc-bpf (8)
Packit 7cfc04
.PP
Packit 7cfc04
Both classic and extended BPF are explained in the kernel source file
Packit 7cfc04
.IR Documentation/networking/filter.txt .
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.