map_array.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. ================================================
  4. BPF_MAP_TYPE_ARRAY and BPF_MAP_TYPE_PERCPU_ARRAY
  5. ================================================
  6. .. note::
  7. - ``BPF_MAP_TYPE_ARRAY`` was introduced in kernel version 3.19
  8. - ``BPF_MAP_TYPE_PERCPU_ARRAY`` was introduced in version 4.6
  9. ``BPF_MAP_TYPE_ARRAY`` and ``BPF_MAP_TYPE_PERCPU_ARRAY`` provide generic array
  10. storage. The key type is an unsigned 32-bit integer (4 bytes) and the map is
  11. of constant size. The size of the array is defined in ``max_entries`` at
  12. creation time. All array elements are pre-allocated and zero initialized when
  13. created. ``BPF_MAP_TYPE_PERCPU_ARRAY`` uses a different memory region for each
  14. CPU whereas ``BPF_MAP_TYPE_ARRAY`` uses the same memory region. The value
  15. stored can be of any size, however, all array elements are aligned to 8
  16. bytes.
  17. Since kernel 5.5, memory mapping may be enabled for ``BPF_MAP_TYPE_ARRAY`` by
  18. setting the flag ``BPF_F_MMAPABLE``. The map definition is page-aligned and
  19. starts on the first page. Sufficient page-sized and page-aligned blocks of
  20. memory are allocated to store all array values, starting on the second page,
  21. which in some cases will result in over-allocation of memory. The benefit of
  22. using this is increased performance and ease of use since userspace programs
  23. would not be required to use helper functions to access and mutate data.
  24. Usage
  25. =====
  26. Kernel BPF
  27. ----------
  28. bpf_map_lookup_elem()
  29. ~~~~~~~~~~~~~~~~~~~~~
  30. .. code-block:: c
  31. void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
  32. Array elements can be retrieved using the ``bpf_map_lookup_elem()`` helper.
  33. This helper returns a pointer into the array element, so to avoid data races
  34. with userspace reading the value, the user must use primitives like
  35. ``__sync_fetch_and_add()`` when updating the value in-place.
  36. bpf_map_update_elem()
  37. ~~~~~~~~~~~~~~~~~~~~~
  38. .. code-block:: c
  39. long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
  40. Array elements can be updated using the ``bpf_map_update_elem()`` helper.
  41. ``bpf_map_update_elem()`` returns 0 on success, or negative error in case of
  42. failure.
  43. Since the array is of constant size, ``bpf_map_delete_elem()`` is not supported.
  44. To clear an array element, you may use ``bpf_map_update_elem()`` to insert a
  45. zero value to that index.
  46. Per CPU Array
  47. -------------
  48. Values stored in ``BPF_MAP_TYPE_ARRAY`` can be accessed by multiple programs
  49. across different CPUs. To restrict storage to a single CPU, you may use a
  50. ``BPF_MAP_TYPE_PERCPU_ARRAY``.
  51. When using a ``BPF_MAP_TYPE_PERCPU_ARRAY`` the ``bpf_map_update_elem()`` and
  52. ``bpf_map_lookup_elem()`` helpers automatically access the slot for the current
  53. CPU.
  54. bpf_map_lookup_percpu_elem()
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. .. code-block:: c
  57. void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, u32 cpu)
  58. The ``bpf_map_lookup_percpu_elem()`` helper can be used to lookup the array
  59. value for a specific CPU. Returns value on success , or ``NULL`` if no entry was
  60. found or ``cpu`` is invalid.
  61. Concurrency
  62. -----------
  63. Since kernel version 5.1, the BPF infrastructure provides ``struct bpf_spin_lock``
  64. to synchronize access.
  65. Userspace
  66. ---------
  67. Access from userspace uses libbpf APIs with the same names as above, with
  68. the map identified by its ``fd``.
  69. Examples
  70. ========
  71. Please see the ``tools/testing/selftests/bpf`` directory for functional
  72. examples. The code samples below demonstrate API usage.
  73. Kernel BPF
  74. ----------
  75. This snippet shows how to declare an array in a BPF program.
  76. .. code-block:: c
  77. struct {
  78. __uint(type, BPF_MAP_TYPE_ARRAY);
  79. __type(key, u32);
  80. __type(value, long);
  81. __uint(max_entries, 256);
  82. } my_map SEC(".maps");
  83. This example BPF program shows how to access an array element.
  84. .. code-block:: c
  85. int bpf_prog(struct __sk_buff *skb)
  86. {
  87. struct iphdr ip;
  88. int index;
  89. long *value;
  90. if (bpf_skb_load_bytes(skb, ETH_HLEN, &ip, sizeof(ip)) < 0)
  91. return 0;
  92. index = ip.protocol;
  93. value = bpf_map_lookup_elem(&my_map, &index);
  94. if (value)
  95. __sync_fetch_and_add(value, skb->len);
  96. return 0;
  97. }
  98. Userspace
  99. ---------
  100. BPF_MAP_TYPE_ARRAY
  101. ~~~~~~~~~~~~~~~~~~
  102. This snippet shows how to create an array, using ``bpf_map_create_opts`` to
  103. set flags.
  104. .. code-block:: c
  105. #include <bpf/libbpf.h>
  106. #include <bpf/bpf.h>
  107. int create_array()
  108. {
  109. int fd;
  110. LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
  111. fd = bpf_map_create(BPF_MAP_TYPE_ARRAY,
  112. "example_array", /* name */
  113. sizeof(__u32), /* key size */
  114. sizeof(long), /* value size */
  115. 256, /* max entries */
  116. &opts); /* create opts */
  117. return fd;
  118. }
  119. This snippet shows how to initialize the elements of an array.
  120. .. code-block:: c
  121. int initialize_array(int fd)
  122. {
  123. __u32 i;
  124. long value;
  125. int ret;
  126. for (i = 0; i < 256; i++) {
  127. value = i;
  128. ret = bpf_map_update_elem(fd, &i, &value, BPF_ANY);
  129. if (ret < 0)
  130. return ret;
  131. }
  132. return ret;
  133. }
  134. This snippet shows how to retrieve an element value from an array.
  135. .. code-block:: c
  136. int lookup(int fd)
  137. {
  138. __u32 index = 42;
  139. long value;
  140. int ret;
  141. ret = bpf_map_lookup_elem(fd, &index, &value);
  142. if (ret < 0)
  143. return ret;
  144. /* use value here */
  145. assert(value == 42);
  146. return ret;
  147. }
  148. BPF_MAP_TYPE_PERCPU_ARRAY
  149. ~~~~~~~~~~~~~~~~~~~~~~~~~
  150. This snippet shows how to initialize the elements of a per CPU array.
  151. .. code-block:: c
  152. int initialize_array(int fd)
  153. {
  154. int ncpus = libbpf_num_possible_cpus();
  155. long values[ncpus];
  156. __u32 i, j;
  157. int ret;
  158. for (i = 0; i < 256 ; i++) {
  159. for (j = 0; j < ncpus; j++)
  160. values[j] = i;
  161. ret = bpf_map_update_elem(fd, &i, &values, BPF_ANY);
  162. if (ret < 0)
  163. return ret;
  164. }
  165. return ret;
  166. }
  167. This snippet shows how to access the per CPU elements of an array value.
  168. .. code-block:: c
  169. int lookup(int fd)
  170. {
  171. int ncpus = libbpf_num_possible_cpus();
  172. __u32 index = 42, j;
  173. long values[ncpus];
  174. int ret;
  175. ret = bpf_map_lookup_elem(fd, &index, &values);
  176. if (ret < 0)
  177. return ret;
  178. for (j = 0; j < ncpus; j++) {
  179. /* Use per CPU value here */
  180. assert(values[j] == 42);
  181. }
  182. return ret;
  183. }
  184. Semantics
  185. =========
  186. As shown in the example above, when accessing a ``BPF_MAP_TYPE_PERCPU_ARRAY``
  187. in userspace, each value is an array with ``ncpus`` elements.
  188. When calling ``bpf_map_update_elem()`` the flag ``BPF_NOEXIST`` can not be used
  189. for these maps.