Commit fb73a20e authored by Donald Hunter's avatar Donald Hunter Committed by Alexei Starovoitov

bpf, docs: Reformat BPF maps page to be more readable

Add a more complete introduction, with links to man pages.
Move toctree of map types above usage notes.
Format usage notes to improve readability.
Signed-off-by: default avatarDonald Hunter <donald.hunter@gmail.com>
Link: https://lore.kernel.org/r/20221012152715.25073-1-donald.hunter@gmail.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 04a8f9d7
========= ========
eBPF maps BPF maps
========
BPF 'maps' provide generic storage of different types for sharing data between
kernel and user space. There are several storage types available, including
hash, array, bloom filter and radix-tree. Several of the map types exist to
support specific BPF helpers that perform actions based on the map contents. The
maps are accessed from BPF programs via BPF helpers which are documented in the
`man-pages`_ for `bpf-helpers(7)`_.
BPF maps are accessed from user space via the ``bpf`` syscall, which provides
commands to create maps, lookup elements, update elements and delete
elements. More details of the BPF syscall are available in
:doc:`/userspace-api/ebpf/syscall` and in the `man-pages`_ for `bpf(2)`_.
Map Types
========= =========
'maps' is a generic storage of different types for sharing data between kernel .. toctree::
and userspace. :maxdepth: 1
:glob:
The maps are accessed from user space via BPF syscall, which has commands: map_*
- create a map with given type and attributes Usage Notes
``map_fd = bpf(BPF_MAP_CREATE, union bpf_attr *attr, u32 size)`` ===========
using attr->map_type, attr->key_size, attr->value_size, attr->max_entries
returns process-local file descriptor or negative error
- lookup key in a given map .. c:function::
``err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)`` int bpf(int command, union bpf_attr *attr, u32 size)
using attr->map_fd, attr->key, attr->value
returns zero and stores found elem into value or negative error
- create or update key/value pair in a given map Use the ``bpf()`` system call to perform the operation specified by
``err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)`` ``command``. The operation takes parameters provided in ``attr``. The ``size``
using attr->map_fd, attr->key, attr->value argument is the size of the ``union bpf_attr`` in ``attr``.
returns zero or negative error
- find and delete element by key in a given map **BPF_MAP_CREATE**
``err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)``
using attr->map_fd, attr->key
- to delete map: close(fd) Create a map with the desired type and attributes in ``attr``:
Exiting process will delete maps automatically
userspace programs use this syscall to create/access maps that eBPF programs .. code-block:: c
are concurrently updating.
maps can have different types: hash, array, bloom filter, radix-tree, etc. int fd;
union bpf_attr attr = {
.map_type = BPF_MAP_TYPE_ARRAY; /* mandatory */
.key_size = sizeof(__u32); /* mandatory */
.value_size = sizeof(__u32); /* mandatory */
.max_entries = 256; /* mandatory */
.map_flags = BPF_F_MMAPABLE;
.map_name = "example_array";
};
The map is defined by: fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
- type Returns a process-local file descriptor on success, or negative error in case of
- max number of elements failure. The map can be deleted by calling ``close(fd)``. Maps held by open
- key size in bytes file descriptors will be deleted automatically when a process exits.
- value size in bytes
Map Types .. note:: Valid characters for ``map_name`` are ``A-Z``, ``a-z``, ``0-9``,
========= ``'_'`` and ``'.'``.
.. toctree:: **BPF_MAP_LOOKUP_ELEM**
:maxdepth: 1
:glob: Lookup key in a given map using ``attr->map_fd``, ``attr->key``,
``attr->value``. Returns zero and stores found elem into ``attr->value`` on
success, or negative error on failure.
**BPF_MAP_UPDATE_ELEM**
Create or update key/value pair in a given map using ``attr->map_fd``, ``attr->key``,
``attr->value``. Returns zero on success or negative error on failure.
**BPF_MAP_DELETE_ELEM**
Find and delete element by key in a given map using ``attr->map_fd``,
``attr->key``. Returns zero on success or negative error on failure.
map_* .. Links:
\ No newline at end of file .. _man-pages: https://www.kernel.org/doc/man-pages/
.. _bpf(2): https://man7.org/linux/man-pages/man2/bpf.2.html
.. _bpf-helpers(7): https://man7.org/linux/man-pages/man7/bpf-helpers.7.html
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment