|
1 | 1 |
|
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 |
4 | 19 | =========
|
5 | 20 |
|
6 |
| -'maps' is a generic storage of different types for sharing data between kernel |
7 |
| -and userspace. |
| 21 | +.. toctree:: |
| 22 | + :maxdepth: 1 |
| 23 | + :glob: |
8 | 24 |
|
9 |
| -The maps are accessed from user space via BPF syscall, which has commands: |
| 25 | + map_* |
10 | 26 |
|
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 | +=========== |
15 | 29 |
|
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) |
20 | 32 |
|
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``. |
25 | 36 |
|
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** |
29 | 38 |
|
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``: |
32 | 40 |
|
33 |
| -userspace programs use this syscall to create/access maps that eBPF programs |
34 |
| -are concurrently updating. |
| 41 | +.. code-block:: c |
35 | 42 |
|
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 | + }; |
37 | 52 |
|
38 |
| -The map is defined by: |
| 53 | + fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); |
39 | 54 |
|
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. |
44 | 58 |
|
45 |
| -Map Types |
46 |
| -========= |
| 59 | +.. note:: Valid characters for ``map_name`` are ``A-Z``, ``a-z``, ``0-9``, |
| 60 | + ``'_'`` and ``'.'``. |
47 | 61 |
|
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. |
51 | 77 |
|
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