map_queue_stack.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. =========================================
  4. BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK
  5. =========================================
  6. .. note::
  7. - ``BPF_MAP_TYPE_QUEUE`` and ``BPF_MAP_TYPE_STACK`` were introduced
  8. in kernel version 4.20
  9. ``BPF_MAP_TYPE_QUEUE`` provides FIFO storage and ``BPF_MAP_TYPE_STACK``
  10. provides LIFO storage for BPF programs. These maps support peek, pop and
  11. push operations that are exposed to BPF programs through the respective
  12. helpers. These operations are exposed to userspace applications using
  13. the existing ``bpf`` syscall in the following way:
  14. - ``BPF_MAP_LOOKUP_ELEM`` -> peek
  15. - ``BPF_MAP_LOOKUP_AND_DELETE_ELEM`` -> pop
  16. - ``BPF_MAP_UPDATE_ELEM`` -> push
  17. ``BPF_MAP_TYPE_QUEUE`` and ``BPF_MAP_TYPE_STACK`` do not support
  18. ``BPF_F_NO_PREALLOC``.
  19. Usage
  20. =====
  21. Kernel BPF
  22. ----------
  23. bpf_map_push_elem()
  24. ~~~~~~~~~~~~~~~~~~~
  25. .. code-block:: c
  26. long bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags)
  27. An element ``value`` can be added to a queue or stack using the
  28. ``bpf_map_push_elem`` helper. The ``flags`` parameter must be set to
  29. ``BPF_ANY`` or ``BPF_EXIST``. If ``flags`` is set to ``BPF_EXIST`` then,
  30. when the queue or stack is full, the oldest element will be removed to
  31. make room for ``value`` to be added. Returns ``0`` on success, or
  32. negative error in case of failure.
  33. bpf_map_peek_elem()
  34. ~~~~~~~~~~~~~~~~~~~
  35. .. code-block:: c
  36. long bpf_map_peek_elem(struct bpf_map *map, void *value)
  37. This helper fetches an element ``value`` from a queue or stack without
  38. removing it. Returns ``0`` on success, or negative error in case of
  39. failure.
  40. bpf_map_pop_elem()
  41. ~~~~~~~~~~~~~~~~~~
  42. .. code-block:: c
  43. long bpf_map_pop_elem(struct bpf_map *map, void *value)
  44. This helper removes an element into ``value`` from a queue or
  45. stack. Returns ``0`` on success, or negative error in case of failure.
  46. Userspace
  47. ---------
  48. bpf_map_update_elem()
  49. ~~~~~~~~~~~~~~~~~~~~~
  50. .. code-block:: c
  51. int bpf_map_update_elem (int fd, const void *key, const void *value, __u64 flags)
  52. A userspace program can push ``value`` onto a queue or stack using libbpf's
  53. ``bpf_map_update_elem`` function. The ``key`` parameter must be set to
  54. ``NULL`` and ``flags`` must be set to ``BPF_ANY`` or ``BPF_EXIST``, with the
  55. same semantics as the ``bpf_map_push_elem`` kernel helper. Returns ``0`` on
  56. success, or negative error in case of failure.
  57. bpf_map_lookup_elem()
  58. ~~~~~~~~~~~~~~~~~~~~~
  59. .. code-block:: c
  60. int bpf_map_lookup_elem (int fd, const void *key, void *value)
  61. A userspace program can peek at the ``value`` at the head of a queue or stack
  62. using the libbpf ``bpf_map_lookup_elem`` function. The ``key`` parameter must be
  63. set to ``NULL``. Returns ``0`` on success, or negative error in case of
  64. failure.
  65. bpf_map_lookup_and_delete_elem()
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. .. code-block:: c
  68. int bpf_map_lookup_and_delete_elem (int fd, const void *key, void *value)
  69. A userspace program can pop a ``value`` from the head of a queue or stack using
  70. the libbpf ``bpf_map_lookup_and_delete_elem`` function. The ``key`` parameter
  71. must be set to ``NULL``. Returns ``0`` on success, or negative error in case of
  72. failure.
  73. Examples
  74. ========
  75. Kernel BPF
  76. ----------
  77. This snippet shows how to declare a queue in a BPF program:
  78. .. code-block:: c
  79. struct {
  80. __uint(type, BPF_MAP_TYPE_QUEUE);
  81. __type(value, __u32);
  82. __uint(max_entries, 10);
  83. } queue SEC(".maps");
  84. Userspace
  85. ---------
  86. This snippet shows how to use libbpf's low-level API to create a queue from
  87. userspace:
  88. .. code-block:: c
  89. int create_queue()
  90. {
  91. return bpf_map_create(BPF_MAP_TYPE_QUEUE,
  92. "sample_queue", /* name */
  93. 0, /* key size, must be zero */
  94. sizeof(__u32), /* value size */
  95. 10, /* max entries */
  96. NULL); /* create options */
  97. }
  98. References
  99. ==========
  100. https://lwn.net/ml/netdev/153986858555.9127.14517764371945179514.stgit@kernel/