maps.rst 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ========
  2. BPF maps
  3. ========
  4. BPF 'maps' provide generic storage of different types for sharing data between
  5. kernel and user space. There are several storage types available, including
  6. hash, array, bloom filter and radix-tree. Several of the map types exist to
  7. support specific BPF helpers that perform actions based on the map contents. The
  8. maps are accessed from BPF programs via BPF helpers which are documented in the
  9. `man-pages`_ for `bpf-helpers(7)`_.
  10. BPF maps are accessed from user space via the ``bpf`` syscall, which provides
  11. commands to create maps, lookup elements, update elements and delete elements.
  12. More details of the BPF syscall are available in `ebpf-syscall`_ and in the
  13. `man-pages`_ for `bpf(2)`_.
  14. Map Types
  15. =========
  16. .. toctree::
  17. :maxdepth: 1
  18. :glob:
  19. map_*
  20. Usage Notes
  21. ===========
  22. .. c:function::
  23. int bpf(int command, union bpf_attr *attr, u32 size)
  24. Use the ``bpf()`` system call to perform the operation specified by
  25. ``command``. The operation takes parameters provided in ``attr``. The ``size``
  26. argument is the size of the ``union bpf_attr`` in ``attr``.
  27. **BPF_MAP_CREATE**
  28. Create a map with the desired type and attributes in ``attr``:
  29. .. code-block:: c
  30. int fd;
  31. union bpf_attr attr = {
  32. .map_type = BPF_MAP_TYPE_ARRAY; /* mandatory */
  33. .key_size = sizeof(__u32); /* mandatory */
  34. .value_size = sizeof(__u32); /* mandatory */
  35. .max_entries = 256; /* mandatory */
  36. .map_flags = BPF_F_MMAPABLE;
  37. .map_name = "example_array";
  38. };
  39. fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  40. Returns a process-local file descriptor on success, or negative error in case of
  41. failure. The map can be deleted by calling ``close(fd)``. Maps held by open
  42. file descriptors will be deleted automatically when a process exits.
  43. .. note:: Valid characters for ``map_name`` are ``A-Z``, ``a-z``, ``0-9``,
  44. ``'_'`` and ``'.'``.
  45. **BPF_MAP_LOOKUP_ELEM**
  46. Lookup key in a given map using ``attr->map_fd``, ``attr->key``,
  47. ``attr->value``. Returns zero and stores found elem into ``attr->value`` on
  48. success, or negative error on failure.
  49. **BPF_MAP_UPDATE_ELEM**
  50. Create or update key/value pair in a given map using ``attr->map_fd``, ``attr->key``,
  51. ``attr->value``. Returns zero on success or negative error on failure.
  52. **BPF_MAP_DELETE_ELEM**
  53. Find and delete element by key in a given map using ``attr->map_fd``,
  54. ``attr->key``. Returns zero on success or negative error on failure.
  55. .. Links:
  56. .. _man-pages: https://www.kernel.org/doc/man-pages/
  57. .. _bpf(2): https://man7.org/linux/man-pages/man2/bpf.2.html
  58. .. _bpf-helpers(7): https://man7.org/linux/man-pages/man7/bpf-helpers.7.html
  59. .. _ebpf-syscall: https://docs.kernel.org/userspace-api/ebpf/syscall.html