map_hash.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. ===============================================
  4. BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants
  5. ===============================================
  6. .. note::
  7. - ``BPF_MAP_TYPE_HASH`` was introduced in kernel version 3.19
  8. - ``BPF_MAP_TYPE_PERCPU_HASH`` was introduced in version 4.6
  9. - Both ``BPF_MAP_TYPE_LRU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
  10. were introduced in version 4.10
  11. ``BPF_MAP_TYPE_HASH`` and ``BPF_MAP_TYPE_PERCPU_HASH`` provide general
  12. purpose hash map storage. Both the key and the value can be structs,
  13. allowing for composite keys and values.
  14. The kernel is responsible for allocating and freeing key/value pairs, up
  15. to the max_entries limit that you specify. Hash maps use pre-allocation
  16. of hash table elements by default. The ``BPF_F_NO_PREALLOC`` flag can be
  17. used to disable pre-allocation when it is too memory expensive.
  18. ``BPF_MAP_TYPE_PERCPU_HASH`` provides a separate value slot per
  19. CPU. The per-cpu values are stored internally in an array.
  20. The ``BPF_MAP_TYPE_LRU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
  21. variants add LRU semantics to their respective hash tables. An LRU hash
  22. will automatically evict the least recently used entries when the hash
  23. table reaches capacity. An LRU hash maintains an internal LRU list that
  24. is used to select elements for eviction. This internal LRU list is
  25. shared across CPUs but it is possible to request a per CPU LRU list with
  26. the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.
  27. Usage
  28. =====
  29. .. c:function::
  30. long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
  31. Hash entries can be added or updated using the ``bpf_map_update_elem()``
  32. helper. This helper replaces existing elements atomically. The ``flags``
  33. parameter can be used to control the update behaviour:
  34. - ``BPF_ANY`` will create a new element or update an existing element
  35. - ``BPF_NOEXIST`` will create a new element only if one did not already
  36. exist
  37. - ``BPF_EXIST`` will update an existing element
  38. ``bpf_map_update_elem()`` returns 0 on success, or negative error in
  39. case of failure.
  40. .. c:function::
  41. void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
  42. Hash entries can be retrieved using the ``bpf_map_lookup_elem()``
  43. helper. This helper returns a pointer to the value associated with
  44. ``key``, or ``NULL`` if no entry was found.
  45. .. c:function::
  46. long bpf_map_delete_elem(struct bpf_map *map, const void *key)
  47. Hash entries can be deleted using the ``bpf_map_delete_elem()``
  48. helper. This helper will return 0 on success, or negative error in case
  49. of failure.
  50. Per CPU Hashes
  51. --------------
  52. For ``BPF_MAP_TYPE_PERCPU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
  53. the ``bpf_map_update_elem()`` and ``bpf_map_lookup_elem()`` helpers
  54. automatically access the hash slot for the current CPU.
  55. .. c:function::
  56. void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, u32 cpu)
  57. The ``bpf_map_lookup_percpu_elem()`` helper can be used to lookup the
  58. value in the hash slot for a specific CPU. Returns value associated with
  59. ``key`` on ``cpu`` , or ``NULL`` if no entry was found or ``cpu`` is
  60. invalid.
  61. Concurrency
  62. -----------
  63. Values stored in ``BPF_MAP_TYPE_HASH`` can be accessed concurrently by
  64. programs running on different CPUs. Since Kernel version 5.1, the BPF
  65. infrastructure provides ``struct bpf_spin_lock`` to synchronise access.
  66. See ``tools/testing/selftests/bpf/progs/test_spin_lock.c``.
  67. Userspace
  68. ---------
  69. .. c:function::
  70. int bpf_map_get_next_key(int fd, const void *cur_key, void *next_key)
  71. In userspace, it is possible to iterate through the keys of a hash using
  72. libbpf's ``bpf_map_get_next_key()`` function. The first key can be fetched by
  73. calling ``bpf_map_get_next_key()`` with ``cur_key`` set to
  74. ``NULL``. Subsequent calls will fetch the next key that follows the
  75. current key. ``bpf_map_get_next_key()`` returns 0 on success, -ENOENT if
  76. cur_key is the last key in the hash, or negative error in case of
  77. failure.
  78. Note that if ``cur_key`` gets deleted then ``bpf_map_get_next_key()``
  79. will instead return the *first* key in the hash table which is
  80. undesirable. It is recommended to use batched lookup if there is going
  81. to be key deletion intermixed with ``bpf_map_get_next_key()``.
  82. Examples
  83. ========
  84. Please see the ``tools/testing/selftests/bpf`` directory for functional
  85. examples. The code snippets below demonstrates API usage.
  86. This example shows how to declare an LRU Hash with a struct key and a
  87. struct value.
  88. .. code-block:: c
  89. #include <linux/bpf.h>
  90. #include <bpf/bpf_helpers.h>
  91. struct key {
  92. __u32 srcip;
  93. };
  94. struct value {
  95. __u64 packets;
  96. __u64 bytes;
  97. };
  98. struct {
  99. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  100. __uint(max_entries, 32);
  101. __type(key, struct key);
  102. __type(value, struct value);
  103. } packet_stats SEC(".maps");
  104. This example shows how to create or update hash values using atomic
  105. instructions:
  106. .. code-block:: c
  107. static void update_stats(__u32 srcip, int bytes)
  108. {
  109. struct key key = {
  110. .srcip = srcip,
  111. };
  112. struct value *value = bpf_map_lookup_elem(&packet_stats, &key);
  113. if (value) {
  114. __sync_fetch_and_add(&value->packets, 1);
  115. __sync_fetch_and_add(&value->bytes, bytes);
  116. } else {
  117. struct value newval = { 1, bytes };
  118. bpf_map_update_elem(&packet_stats, &key, &newval, BPF_NOEXIST);
  119. }
  120. }
  121. Userspace walking the map elements from the map declared above:
  122. .. code-block:: c
  123. #include <bpf/libbpf.h>
  124. #include <bpf/bpf.h>
  125. static void walk_hash_elements(int map_fd)
  126. {
  127. struct key *cur_key = NULL;
  128. struct key next_key;
  129. struct value value;
  130. int err;
  131. for (;;) {
  132. err = bpf_map_get_next_key(map_fd, cur_key, &next_key);
  133. if (err)
  134. break;
  135. bpf_map_lookup_elem(map_fd, &next_key, &value);
  136. // Use key and value here
  137. cur_key = &next_key;
  138. }
  139. }