brintos

brintos / linux-shallow public Read only

0
0
Text · 4.1 KiB · 8d14ed4 Raw
147 lines · plain
1.. SPDX-License-Identifier: GPL-2.0-only2.. Copyright (C) 2022 Red Hat, Inc.3 4=========================================5BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK6=========================================7 8.. note::9   - ``BPF_MAP_TYPE_QUEUE`` and ``BPF_MAP_TYPE_STACK`` were introduced10     in kernel version 4.2011 12``BPF_MAP_TYPE_QUEUE`` provides FIFO storage and ``BPF_MAP_TYPE_STACK``13provides LIFO storage for BPF programs. These maps support peek, pop and14push operations that are exposed to BPF programs through the respective15helpers. These operations are exposed to userspace applications using16the existing ``bpf`` syscall in the following way:17 18- ``BPF_MAP_LOOKUP_ELEM`` -> peek19- ``BPF_MAP_LOOKUP_AND_DELETE_ELEM`` -> pop20- ``BPF_MAP_UPDATE_ELEM`` -> push21 22``BPF_MAP_TYPE_QUEUE`` and ``BPF_MAP_TYPE_STACK`` do not support23``BPF_F_NO_PREALLOC``.24 25Usage26=====27 28Kernel BPF29----------30 31bpf_map_push_elem()32~~~~~~~~~~~~~~~~~~~33 34.. code-block:: c35 36   long bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags)37 38An element ``value`` can be added to a queue or stack using the39``bpf_map_push_elem`` helper. The ``flags`` parameter must be set to40``BPF_ANY`` or ``BPF_EXIST``. If ``flags`` is set to ``BPF_EXIST`` then,41when the queue or stack is full, the oldest element will be removed to42make room for ``value`` to be added. Returns ``0`` on success, or43negative error in case of failure.44 45bpf_map_peek_elem()46~~~~~~~~~~~~~~~~~~~47 48.. code-block:: c49 50   long bpf_map_peek_elem(struct bpf_map *map, void *value)51 52This helper fetches an element ``value`` from a queue or stack without53removing it. Returns ``0`` on success, or negative error in case of54failure.55 56bpf_map_pop_elem()57~~~~~~~~~~~~~~~~~~58 59.. code-block:: c60 61   long bpf_map_pop_elem(struct bpf_map *map, void *value)62 63This helper removes an element into ``value`` from a queue or64stack. Returns ``0`` on success, or negative error in case of failure.65 66 67Userspace68---------69 70bpf_map_update_elem()71~~~~~~~~~~~~~~~~~~~~~72 73.. code-block:: c74 75   int bpf_map_update_elem (int fd, const void *key, const void *value, __u64 flags)76 77A userspace program can push ``value`` onto a queue or stack using libbpf's78``bpf_map_update_elem`` function. The ``key`` parameter must be set to79``NULL`` and ``flags`` must be set to ``BPF_ANY`` or ``BPF_EXIST``, with the80same semantics as the ``bpf_map_push_elem`` kernel helper. Returns ``0`` on81success, or negative error in case of failure.82 83bpf_map_lookup_elem()84~~~~~~~~~~~~~~~~~~~~~85 86.. code-block:: c87 88   int bpf_map_lookup_elem (int fd, const void *key, void *value)89 90A userspace program can peek at the ``value`` at the head of a queue or stack91using the libbpf ``bpf_map_lookup_elem`` function. The ``key`` parameter must be92set to ``NULL``.  Returns ``0`` on success, or negative error in case of93failure.94 95bpf_map_lookup_and_delete_elem()96~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~97 98.. code-block:: c99 100   int bpf_map_lookup_and_delete_elem (int fd, const void *key, void *value)101 102A userspace program can pop a ``value`` from the head of a queue or stack using103the libbpf ``bpf_map_lookup_and_delete_elem`` function. The ``key`` parameter104must be set to ``NULL``. Returns ``0`` on success, or negative error in case of105failure.106 107Examples108========109 110Kernel BPF111----------112 113This snippet shows how to declare a queue in a BPF program:114 115.. code-block:: c116 117    struct {118            __uint(type, BPF_MAP_TYPE_QUEUE);119            __type(value, __u32);120            __uint(max_entries, 10);121    } queue SEC(".maps");122 123 124Userspace125---------126 127This snippet shows how to use libbpf's low-level API to create a queue from128userspace:129 130.. code-block:: c131 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 143References144==========145 146https://lwn.net/ml/netdev/153986858555.9127.14517764371945179514.stgit@kernel/147