Skip to content

Commit 168c090

Browse files
donaldhKernel Patches Daemon
authored andcommitted
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: Donald Hunter <donald.hunter@gmail.com>
1 parent 3ad9227 commit 168c090

File tree

1 file changed

+65
-36
lines changed

1 file changed

+65
-36
lines changed

Documentation/bpf/maps.rst

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,81 @@
11

2-
=========
3-
eBPF maps
2+
========
3+
BPF maps
4+
========
5+
6+
BPF 'maps' provide generic storage of different types for sharing data between
7+
kernel and user space. There are several storage types available, including
8+
hash, array, bloom filter and radix-tree. Several of the map types exist to
9+
support specific BPF helpers that perform actions based on the map contents. The
10+
maps are accessed from BPF programs via BPF helpers which are documented in the
11+
`man-pages`_ for `bpf-helpers(7)`_.
12+
13+
BPF maps are accessed from user space via the ``bpf`` syscall, which provides
14+
commands to create maps, lookup elements, update elements and delete
15+
elements. More details of the BPF syscall are available in
16+
:doc:`/userspace-api/ebpf/syscall` and in the `man-pages`_ for `bpf(2)`_.
17+
18+
Map Types
419
=========
520

6-
'maps' is a generic storage of different types for sharing data between kernel
7-
and userspace.
21+
.. toctree::
22+
:maxdepth: 1
23+
:glob:
824

9-
The maps are accessed from user space via BPF syscall, which has commands:
25+
map_*
1026

11-
- create a map with given type and attributes
12-
``map_fd = bpf(BPF_MAP_CREATE, union bpf_attr *attr, u32 size)``
13-
using attr->map_type, attr->key_size, attr->value_size, attr->max_entries
14-
returns process-local file descriptor or negative error
27+
Usage Notes
28+
===========
1529

16-
- lookup key in a given map
17-
``err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)``
18-
using attr->map_fd, attr->key, attr->value
19-
returns zero and stores found elem into value or negative error
30+
.. c:function::
31+
int bpf(int command, union bpf_attr *attr, u32 size)
2032
21-
- create or update key/value pair in a given map
22-
``err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)``
23-
using attr->map_fd, attr->key, attr->value
24-
returns zero or negative error
33+
Use the ``bpf()`` system call to perform the operation specified by
34+
``command``. The operation takes parameters provided in ``attr``. The ``size``
35+
argument is the size of the ``union bpf_attr`` in ``attr``.
2536

26-
- find and delete element by key in a given map
27-
``err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)``
28-
using attr->map_fd, attr->key
37+
**BPF_MAP_CREATE**
2938

30-
- to delete map: close(fd)
31-
Exiting process will delete maps automatically
39+
Create a map with the desired type and attributes in ``attr``:
3240

33-
userspace programs use this syscall to create/access maps that eBPF programs
34-
are concurrently updating.
41+
.. code-block:: c
3542
36-
maps can have different types: hash, array, bloom filter, radix-tree, etc.
43+
int fd;
44+
union bpf_attr attr = {
45+
.map_type = BPF_MAP_TYPE_ARRAY; /* mandatory */
46+
.key_size = sizeof(__u32); /* mandatory */
47+
.value_size = sizeof(__u32); /* mandatory */
48+
.max_entries = 256; /* mandatory */
49+
.map_flags = BPF_F_MMAPABLE;
50+
.map_name = "example_array";
51+
};
3752
38-
The map is defined by:
53+
fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
3954
40-
- type
41-
- max number of elements
42-
- key size in bytes
43-
- value size in bytes
55+
Returns a process-local file descriptor on success, or negative error in case of
56+
failure. The map can be deleted by calling ``close(fd)``. Maps held by open
57+
file descriptors will be deleted automatically when a process exits.
4458

45-
Map Types
46-
=========
59+
.. note:: Valid characters for ``map_name`` are ``A-Z``, ``a-z``, ``0-9``,
60+
``'_'`` and ``'.'``.
4761

48-
.. toctree::
49-
:maxdepth: 1
50-
:glob:
62+
**BPF_MAP_LOOKUP_ELEM**
63+
64+
Lookup key in a given map using ``attr->map_fd``, ``attr->key``,
65+
``attr->value``. Returns zero and stores found elem into ``attr->value`` on
66+
success, or negative error on failure.
67+
68+
**BPF_MAP_UPDATE_ELEM**
69+
70+
Create or update key/value pair in a given map using ``attr->map_fd``, ``attr->key``,
71+
``attr->value``. Returns zero on success or negative error on failure.
72+
73+
**BPF_MAP_DELETE_ELEM**
74+
75+
Find and delete element by key in a given map using ``attr->map_fd``,
76+
``attr->key``. Returns zero on success or negative error on failure.
5177

52-
map_*
78+
.. Links:
79+
.. _man-pages: https://www.kernel.org/doc/man-pages/
80+
.. _bpf(2): https://man7.org/linux/man-pages/man2/bpf.2.html
81+
.. _bpf-helpers(7): https://man7.org/linux/man-pages/man7/bpf-helpers.7.html

0 commit comments

Comments
 (0)