Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>
Link: https://lore.kernel.org/r/20221012152715.25073-1-donald.hunter@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Donald Hunter and committed by
Alexei Starovoitov
fb73a20e 04a8f9d7

+69 -40
+69 -40
Documentation/bpf/maps.rst
··· 1 1 2 - ========= 3 - eBPF maps 4 - ========= 2 + ======== 3 + BPF maps 4 + ======== 5 5 6 - 'maps' is a generic storage of different types for sharing data between kernel 7 - and userspace. 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)`_. 8 12 9 - The maps are accessed from user space via BPF syscall, which has commands: 10 - 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 15 - 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 20 - 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 25 - 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 29 - 30 - - to delete map: close(fd) 31 - Exiting process will delete maps automatically 32 - 33 - userspace programs use this syscall to create/access maps that eBPF programs 34 - are concurrently updating. 35 - 36 - maps can have different types: hash, array, bloom filter, radix-tree, etc. 37 - 38 - The map is defined by: 39 - 40 - - type 41 - - max number of elements 42 - - key size in bytes 43 - - value size in bytes 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)`_. 44 17 45 18 Map Types 46 19 ========= ··· 23 50 :glob: 24 51 25 52 map_* 53 + 54 + Usage Notes 55 + =========== 56 + 57 + .. c:function:: 58 + int bpf(int command, union bpf_attr *attr, u32 size) 59 + 60 + Use the ``bpf()`` system call to perform the operation specified by 61 + ``command``. The operation takes parameters provided in ``attr``. The ``size`` 62 + argument is the size of the ``union bpf_attr`` in ``attr``. 63 + 64 + **BPF_MAP_CREATE** 65 + 66 + Create a map with the desired type and attributes in ``attr``: 67 + 68 + .. code-block:: c 69 + 70 + int fd; 71 + union bpf_attr attr = { 72 + .map_type = BPF_MAP_TYPE_ARRAY; /* mandatory */ 73 + .key_size = sizeof(__u32); /* mandatory */ 74 + .value_size = sizeof(__u32); /* mandatory */ 75 + .max_entries = 256; /* mandatory */ 76 + .map_flags = BPF_F_MMAPABLE; 77 + .map_name = "example_array"; 78 + }; 79 + 80 + fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); 81 + 82 + Returns a process-local file descriptor on success, or negative error in case of 83 + failure. The map can be deleted by calling ``close(fd)``. Maps held by open 84 + file descriptors will be deleted automatically when a process exits. 85 + 86 + .. note:: Valid characters for ``map_name`` are ``A-Z``, ``a-z``, ``0-9``, 87 + ``'_'`` and ``'.'``. 88 + 89 + **BPF_MAP_LOOKUP_ELEM** 90 + 91 + Lookup key in a given map using ``attr->map_fd``, ``attr->key``, 92 + ``attr->value``. Returns zero and stores found elem into ``attr->value`` on 93 + success, or negative error on failure. 94 + 95 + **BPF_MAP_UPDATE_ELEM** 96 + 97 + Create or update key/value pair in a given map using ``attr->map_fd``, ``attr->key``, 98 + ``attr->value``. Returns zero on success or negative error on failure. 99 + 100 + **BPF_MAP_DELETE_ELEM** 101 + 102 + Find and delete element by key in a given map using ``attr->map_fd``, 103 + ``attr->key``. Returns zero on success or negative error on failure. 104 + 105 + .. Links: 106 + .. _man-pages: https://www.kernel.org/doc/man-pages/ 107 + .. _bpf(2): https://man7.org/linux/man-pages/man2/bpf.2.html 108 + .. _bpf-helpers(7): https://man7.org/linux/man-pages/man7/bpf-helpers.7.html