~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/bpf/map_queue_stack.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php