diff mbox series

bpf, docs: document BPF_MAP_TYPE_HASH and variants

Message ID 20220713211612.84782-1-donald.hunter@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf, docs: document BPF_MAP_TYPE_HASH and variants | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch, async
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Donald Hunter July 13, 2022, 9:16 p.m. UTC
This commit adds documentation for BPF_MAP_TYPE_HASH including kernel
version introduced, usage and examples. It also documents
BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_LRU_HASH and
BPF_MAP_TYPE_LRU_PERCPU_HASH which are similar.

Note that this file is included in the BPF documentation by the glob in
Documentation/bpf/maps.rst

Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
---
 Documentation/bpf/map_hash.rst | 176 +++++++++++++++++++++++++++++++++
 1 file changed, 176 insertions(+)
 create mode 100644 Documentation/bpf/map_hash.rst

Comments

Stanislav Fomichev July 13, 2022, 11:30 p.m. UTC | #1
On 07/13, Donald Hunter wrote:
> This commit adds documentation for BPF_MAP_TYPE_HASH including kernel
> version introduced, usage and examples. It also documents
> BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_LRU_HASH and
> BPF_MAP_TYPE_LRU_PERCPU_HASH which are similar.

> Note that this file is included in the BPF documentation by the glob in
> Documentation/bpf/maps.rst

> Signed-off-by: Donald Hunter <donald.hunter@gmail.com>
> ---
>   Documentation/bpf/map_hash.rst | 176 +++++++++++++++++++++++++++++++++
>   1 file changed, 176 insertions(+)
>   create mode 100644 Documentation/bpf/map_hash.rst

> diff --git a/Documentation/bpf/map_hash.rst  
> b/Documentation/bpf/map_hash.rst
> new file mode 100644
> index 000000000000..991452e70cc9
> --- /dev/null
> +++ b/Documentation/bpf/map_hash.rst
> @@ -0,0 +1,176 @@
> +.. SPDX-License-Identifier: GPL-2.0-only
> +.. Copyright (C) 2021 Red Hat, Inc.
> +
> +===============================================
> +BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants
> +===============================================
> +
> +.. note::
> +   - ``BPF_MAP_TYPE_HASH`` was introduced in kernel version 3.19
> +   - ``BPF_MAP_TYPE_PERCPU_HASH`` was introduced in version 4.6
> +   - Both ``BPF_MAP_TYPE_LRU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
> +     were introduced in version 4.10
> +
> +``BPF_MAP_TYPE_HASH`` and ``BPF_MAP_TYPE_PERCPU_HASH`` provide general
> +purpose hash map storage. Both the key and the value can be structs,
> +allowing for composite keys and values. The maximum number of entries is
> +defined in max_entries and is limited to 2^32. The kernel is responsible

Do we really need to mention 2^32 limit here? It really depends on
the key/value sizes, right?

Instead, might be worth talking about how/when this memory is allocated and
mention BPF_F_NO_PREALLOC?

> +for allocating and freeing key/value pairs, up to the max_entries limit
> +that you specify. ``BPF_MAP_TYPE_PERCPU_HASH`` provides a separate hash
> +table per CPU.
> +
> +Values stored in ``BPF_MAP_TYPE_HASH`` can be accessed concurrently by
> +programs running on different CPUs.  Since Kernel version 5.1, the BPF
> +infrastructure provides ``struct bpf_spin_lock`` to synchronize access.
> +
> +The ``BPF_MAP_TYPE_LRU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
> +variants add LRU semantics to their respective hash tables. An LRU hash
> +will automatically evict the least recently used entries when the hash
> +table reaches capacity. An LRU hash maintains an internal LRU list that
> +is used to select elements for eviction. This internal LRU list is
> +shared across CPUs but it is possible to request a per CPU LRU list with
> +the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.
> +
> +Usage
> +=====
> +
> +.. c:function::
> +   long bpf_map_update_elem(struct bpf_map *map, const void *key, const  
> void *value, u60 flags)

s/u60/u64/

> +
> +Hash entries can be added or updated using the ``bpf_map_update_elem()``
> +helper. This helper replaces existing elements atomically. The ``flags``
> +parameter can be used to control the update behaviour:
> +
> +- ``BPF_ANY`` will create a new element or update an existing element
> +- ``BPF_NOTEXIST`` will create a new element only if one did not already
> +  exist
> +- ``BPF_EXIST`` will update an existing element
> +
> +``bpf_map_update_elem()`` returns 0 on success, or negative error in
> +case of failure.
> +
> +.. c:function::
> +   void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
> +
> +Hash entries can be retrieved using the ``bpf_map_lookup_elem()``
> +helper. This helper returns a pointer to the value associated with
> +``key``, or ``NULL`` if no entry was found.
> +
> +.. c:function::
> +   long bpf_map_delete_elem(struct bpf_map *map, const void *key)
> +
> +Hash entries can be deleted using the ``bpf_map_delete_elem()``
> +helper. This helper will return 0 on success, or negative error in case
> +of failure.
> +
> +Per CPU Hashes
> +--------------
> +
> +For ``BPF_MAP_TYPE_PERCPU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
> +the ``bpf_map_update_elem()`` and ``bpf_map_lookup_elem()`` helpers
> +automatically access the hash slot for the current CPU.
> +
> +.. c:function::
> +   void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void  
> *key, u32 cpu)
> +
> +The ``bpf_map_lookup_percpu_elem()`` helper can be used to lookup the
> +value in the hash slot for a specific CPU. Returns value associated with
> +``key`` on ``cpu`` , or ``NULL`` if no entry was found or ``cpu`` is
> +invalid.
> +
> +Userspace
> +---------
> +
> +.. c:function::
> +   int bpf_map_get_next_key (int fd, const void *cur_key, void *next_key)
> +
> +In userspace, is possible to iterate through the keys of a hash using
> +the ``bpf_map_get_next_key()`` function. The first key can be fetched by
> +calling ``bpf_map_get_next_key()`` with ``cur_key`` set to
> +``NULL``. Subsequent calls will fetch the next key that follows the
> +current key. ``bpf_map_get_next_key()`` returns 0 on success, -ENOENT if
> +cur_key is the last key in the hash, or negative error in case of
> +failure.
> +
> +Examples
> +========
> +
> +Please see the ``tools/testing/selftests/bpf`` directory for functional
> +examples.  This sample code demonstrates API usage.

[..]

> +Kernel
> +------
> +
> +.. code-block:: c
> +
> +    #include <linux/bpf.h>
> +    #include <bpf/bpf_helpers.h>
> +
> +    struct key {
> +        __u32 srcip;
> +    };
> +
> +    struct value {
> +        __u64 packets;
> +        __u64 bytes;
> +    };
> +
> +    struct {
> +            __uint(type, BPF_MAP_TYPE_LRU_HASH);
> +            __uint(max_entries, 32);
> +            __type(key, struct key);
> +            __type(value, struct value);
> +    } packet_stats SEC(".maps");
> +
> +    static inline void count_by_srcip(__u32 srcip, int bytes)
> +    {
> +            struct key key = {
> +                    .srcip = srcip
> +            };
> +            struct value *value = bpf_map_lookup_elem(&packet_stats,  
> &key);
> +            if (value) {
> +                    __sync_fetch_and_add(&value->packets, 1);
> +                    __sync_fetch_and_add(&value->bytes, bytes);
> +            } else {
> +                    struct value newval = { 1, bytes };
> +                    bpf_map_update_elem(&packet_stats, &key, &newval,  
> BPF_NOEXIST);
> +            }
> +    }
> +
> +Userspace
> +---------
> +
> +.. code-block:: c
> +
> +    #include <bpf/libbpf.h>
> +    #include <bpf/bpf.h>
> +
> +    static void print_values(int map_fd)
> +    {
> +            struct key *cur_key = NULL;
> +            struct key next_key;
> +            int next;
> +            do {
> +                    next = bpf_map_get_next_key(stats_fd, cur_key,  
> &next_key);
> +                    if (next == -ENOENT)
> +                            break;
> +                    if (next < 0) {
> +                            fprintf(stderr, "bpf_map_get_next_key %d  
> returned %s\n", stats_fd, strerror(-next));
> +                            break;
> +                    }
> +
> +                    struct in_addr src_addr = {
> +                            .s_addr = next_key.srcip
> +                    };
> +                    char *src_ip = inet_ntoa(src_addr);
> +
> +                    struct value value;
> +                    int ret = bpf_map_lookup_elem(stats_fd, &next_key,  
> &value);
> +                    if (ret < 0) {
> +                            fprintf(stderr, "Failed to lookup elem with  
> key %s: %s\n", src_ip, strerror(-ret));
> +                            break;
> +                    }
> +                    printf("%s: %lld packets, %lld bytes\n", src_ip,  
> value.packets, value.bytes);
> +                    cur_key = &next_key;
> +            } while (next == 0);
> +    }

Instead of adding c code, maybe add pointers to specific file within
tools/testing/selftests/bpf/progs ? That's what we've done for
prog_cgroup_sockopt; the actual tests are a bit more maintained than
the doc :-)
Bagas Sanjaya July 14, 2022, 1:12 a.m. UTC | #2
On 7/14/22 04:16, Donald Hunter wrote:
> This commit adds documentation for BPF_MAP_TYPE_HASH including kernel
> version introduced, usage and examples. It also documents
> BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_LRU_HASH and
> BPF_MAP_TYPE_LRU_PERCPU_HASH which are similar.
> 

Please, please use imperative mood instead for patch description
(that is, better write like "document BPF_MAP_TYPE_* types").
Daniel Müller July 14, 2022, 5:51 a.m. UTC | #3
On Thu, Jul 14, 2022 at 08:12:57AM +0700, Bagas Sanjaya wrote:
> On 7/14/22 04:16, Donald Hunter wrote:
> > This commit adds documentation for BPF_MAP_TYPE_HASH including kernel
> > version introduced, usage and examples. It also documents
> > BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_LRU_HASH and
> > BPF_MAP_TYPE_LRU_PERCPU_HASH which are similar.
> > 
> 
> Please, please use imperative mood instead for patch description
> (that is, better write like "document BPF_MAP_TYPE_* types").

Can you elaborate why you make that recommendation, please?

Thanks,
Daniel
Bagas Sanjaya July 14, 2022, 8:10 a.m. UTC | #4
On 7/14/22 12:51, Daniel Müller wrote:
> On Thu, Jul 14, 2022 at 08:12:57AM +0700, Bagas Sanjaya wrote:
>> On 7/14/22 04:16, Donald Hunter wrote:
>>> This commit adds documentation for BPF_MAP_TYPE_HASH including kernel
>>> version introduced, usage and examples. It also documents
>>> BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_LRU_HASH and
>>> BPF_MAP_TYPE_LRU_PERCPU_HASH which are similar.
>>>
>>
>> Please, please use imperative mood instead for patch description
>> (that is, better write like "document BPF_MAP_TYPE_* types").
> 
> Can you elaborate why you make that recommendation, please?
> 

From Documentation/process/submitting-patches.rst:

> Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
> instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
> to do frotz", as if you are giving orders to the codebase to change
> its behaviour.

The recommendation above is from commit 74a475acea4945
("SubmittingPatches: add style recommendation to use imperative descriptions")

Thanks.
Donald Hunter July 14, 2022, 9:53 p.m. UTC | #5
Bagas Sanjaya <bagasdotme@gmail.com> writes:
>
> From Documentation/process/submitting-patches.rst:
>
>> Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
>> instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
>> to do frotz", as if you are giving orders to the codebase to change
>> its behaviour.
>
> The recommendation above is from commit 74a475acea4945
> ("SubmittingPatches: add style recommendation to use imperative descriptions")
>
> Thanks.

Thanks for the pointer. I will follow this guidance for v2.

Donald.
Donald Hunter July 14, 2022, 10:20 p.m. UTC | #6
sdf@google.com writes:
>> +``BPF_MAP_TYPE_HASH`` and ``BPF_MAP_TYPE_PERCPU_HASH`` provide general
>> +purpose hash map storage. Both the key and the value can be structs,
>> +allowing for composite keys and values. The maximum number of entries is
>> +defined in max_entries and is limited to 2^32. The kernel is responsible
>
> Do we really need to mention 2^32 limit here? It really depends on
> the key/value sizes, right?
>
> Instead, might be worth talking about how/when this memory is allocated and
> mention BPF_F_NO_PREALLOC?

Good suggestion. I'll incorporate this into v2.

>> +.. c:function::
>> +   long bpf_map_update_elem(struct bpf_map *map, const void *key, const  void *value, u60
>> flags)
>
> s/u60/u64/

Good catch, thanks.

>> +Kernel
>> +------
>> +
>> +.. code-block:: c
>> +
>> +    #include <linux/bpf.h>
>> +    #include <bpf/bpf_helpers.h>
>> +
>> +    struct key {
>> +        __u32 srcip;
>> +    };
>> +
>> +    struct value {
>> +        __u64 packets;
>> +        __u64 bytes;
>> +    };
>> +
>> +    struct {
>> +            __uint(type, BPF_MAP_TYPE_LRU_HASH);
>> +            __uint(max_entries, 32);
>> +            __type(key, struct key);
>> +            __type(value, struct value);
>> +    } packet_stats SEC(".maps");
>> +
>> +    static inline void count_by_srcip(__u32 srcip, int bytes)
>> +    {
>> +            struct key key = {
>> +                    .srcip = srcip
>> +            };
>> +            struct value *value = bpf_map_lookup_elem(&packet_stats,  &key);
>> +            if (value) {
>> +                    __sync_fetch_and_add(&value->packets, 1);
>> +                    __sync_fetch_and_add(&value->bytes, bytes);
>> +            } else {
>> +                    struct value newval = { 1, bytes };
>> +                    bpf_map_update_elem(&packet_stats, &key, &newval,  BPF_NOEXIST);
>> +            }
>> +    }
>> +
>> +Userspace
>> +---------
>> +
>> +.. code-block:: c
>> +
>> +    #include <bpf/libbpf.h>
>> +    #include <bpf/bpf.h>
>> +
>> +    static void print_values(int map_fd)
>> +    {
>> +            struct key *cur_key = NULL;
>> +            struct key next_key;
>> +            int next;
>> +            do {
>> +                    next = bpf_map_get_next_key(stats_fd, cur_key,  &next_key);
>> +                    if (next == -ENOENT)
>> +                            break;
>> +                    if (next < 0) {
>> +                            fprintf(stderr, "bpf_map_get_next_key %d  returned %s\n",
>> stats_fd, strerror(-next));
>> +                            break;
>> +                    }
>> +
>> +                    struct in_addr src_addr = {
>> +                            .s_addr = next_key.srcip
>> +                    };
>> +                    char *src_ip = inet_ntoa(src_addr);
>> +
>> +                    struct value value;
>> +                    int ret = bpf_map_lookup_elem(stats_fd, &next_key,  &value);
>> +                    if (ret < 0) {
>> +                            fprintf(stderr, "Failed to lookup elem with  key %s: %s\n",
>> src_ip, strerror(-ret));
>> +                            break;
>> +                    }
>> +                    printf("%s: %lld packets, %lld bytes\n", src_ip,  value.packets,
>> value.bytes);
>> +                    cur_key = &next_key;
>> +            } while (next == 0);
>> +    }
>
> Instead of adding c code, maybe add pointers to specific file within
> tools/testing/selftests/bpf/progs ? That's what we've done for
> prog_cgroup_sockopt; the actual tests are a bit more maintained than
> the doc :-)

I tried to cut the examples to the minimum that was still complete
enough to show use in context. Happy to try cutting the examples down to
a sequence of shorter snippets and and links to samples/bpf and
tools/testing/selftests/bpf/progs. I'll need to reference samples/bpf
because there are no bpf_map_get_next_key examples in
tools/testing/selftests/bpf/progs.

Donald.
diff mbox series

Patch

diff --git a/Documentation/bpf/map_hash.rst b/Documentation/bpf/map_hash.rst
new file mode 100644
index 000000000000..991452e70cc9
--- /dev/null
+++ b/Documentation/bpf/map_hash.rst
@@ -0,0 +1,176 @@ 
+.. SPDX-License-Identifier: GPL-2.0-only
+.. Copyright (C) 2021 Red Hat, Inc.
+
+===============================================
+BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants
+===============================================
+
+.. note::
+   - ``BPF_MAP_TYPE_HASH`` was introduced in kernel version 3.19
+   - ``BPF_MAP_TYPE_PERCPU_HASH`` was introduced in version 4.6
+   - Both ``BPF_MAP_TYPE_LRU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
+     were introduced in version 4.10
+
+``BPF_MAP_TYPE_HASH`` and ``BPF_MAP_TYPE_PERCPU_HASH`` provide general
+purpose hash map storage. Both the key and the value can be structs,
+allowing for composite keys and values. The maximum number of entries is
+defined in max_entries and is limited to 2^32. The kernel is responsible
+for allocating and freeing key/value pairs, up to the max_entries limit
+that you specify. ``BPF_MAP_TYPE_PERCPU_HASH`` provides a separate hash
+table per CPU.
+
+Values stored in ``BPF_MAP_TYPE_HASH`` can be accessed concurrently by
+programs running on different CPUs.  Since Kernel version 5.1, the BPF
+infrastructure provides ``struct bpf_spin_lock`` to synchronize access.
+
+The ``BPF_MAP_TYPE_LRU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
+variants add LRU semantics to their respective hash tables. An LRU hash
+will automatically evict the least recently used entries when the hash
+table reaches capacity. An LRU hash maintains an internal LRU list that
+is used to select elements for eviction. This internal LRU list is
+shared across CPUs but it is possible to request a per CPU LRU list with
+the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.
+
+Usage
+=====
+
+.. c:function::
+   long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u60 flags)
+
+Hash entries can be added or updated using the ``bpf_map_update_elem()``
+helper. This helper replaces existing elements atomically. The ``flags``
+parameter can be used to control the update behaviour:
+
+- ``BPF_ANY`` will create a new element or update an existing element
+- ``BPF_NOTEXIST`` will create a new element only if one did not already
+  exist
+- ``BPF_EXIST`` will update an existing element
+
+``bpf_map_update_elem()`` returns 0 on success, or negative error in
+case of failure.
+
+.. c:function::
+   void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
+
+Hash entries can be retrieved using the ``bpf_map_lookup_elem()``
+helper. This helper returns a pointer to the value associated with
+``key``, or ``NULL`` if no entry was found.
+
+.. c:function::
+   long bpf_map_delete_elem(struct bpf_map *map, const void *key)
+
+Hash entries can be deleted using the ``bpf_map_delete_elem()``
+helper. This helper will return 0 on success, or negative error in case
+of failure.
+
+Per CPU Hashes
+--------------
+
+For ``BPF_MAP_TYPE_PERCPU_HASH`` and ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
+the ``bpf_map_update_elem()`` and ``bpf_map_lookup_elem()`` helpers
+automatically access the hash slot for the current CPU.
+
+.. c:function::
+   void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, u32 cpu)
+
+The ``bpf_map_lookup_percpu_elem()`` helper can be used to lookup the
+value in the hash slot for a specific CPU. Returns value associated with
+``key`` on ``cpu`` , or ``NULL`` if no entry was found or ``cpu`` is
+invalid.
+
+Userspace
+---------
+
+.. c:function::
+   int bpf_map_get_next_key (int fd, const void *cur_key, void *next_key)
+
+In userspace, is possible to iterate through the keys of a hash using
+the ``bpf_map_get_next_key()`` function. The first key can be fetched by
+calling ``bpf_map_get_next_key()`` with ``cur_key`` set to
+``NULL``. Subsequent calls will fetch the next key that follows the
+current key. ``bpf_map_get_next_key()`` returns 0 on success, -ENOENT if
+cur_key is the last key in the hash, or negative error in case of
+failure.
+
+Examples
+========
+
+Please see the ``tools/testing/selftests/bpf`` directory for functional
+examples.  This sample code demonstrates API usage.
+
+Kernel
+------
+
+.. code-block:: c
+
+    #include <linux/bpf.h>
+    #include <bpf/bpf_helpers.h>
+
+    struct key {
+        __u32 srcip;
+    };
+
+    struct value {
+        __u64 packets;
+        __u64 bytes;
+    };
+
+    struct {
+            __uint(type, BPF_MAP_TYPE_LRU_HASH);
+            __uint(max_entries, 32);
+            __type(key, struct key);
+            __type(value, struct value);
+    } packet_stats SEC(".maps");
+
+    static inline void count_by_srcip(__u32 srcip, int bytes)
+    {
+            struct key key = {
+                    .srcip = srcip
+            };
+            struct value *value = bpf_map_lookup_elem(&packet_stats, &key);
+            if (value) {
+                    __sync_fetch_and_add(&value->packets, 1);
+                    __sync_fetch_and_add(&value->bytes, bytes);
+            } else {
+                    struct value newval = { 1, bytes };
+                    bpf_map_update_elem(&packet_stats, &key, &newval, BPF_NOEXIST);
+            }
+    }
+
+Userspace
+---------
+
+.. code-block:: c
+
+    #include <bpf/libbpf.h>
+    #include <bpf/bpf.h>
+
+    static void print_values(int map_fd)
+    {
+            struct key *cur_key = NULL;
+            struct key next_key;
+            int next;
+            do {
+                    next = bpf_map_get_next_key(stats_fd, cur_key, &next_key);
+                    if (next == -ENOENT)
+                            break;
+                    if (next < 0) {
+                            fprintf(stderr, "bpf_map_get_next_key %d returned %s\n", stats_fd, strerror(-next));
+                            break;
+                    }
+
+                    struct in_addr src_addr = {
+                            .s_addr = next_key.srcip
+                    };
+                    char *src_ip = inet_ntoa(src_addr);
+
+                    struct value value;
+                    int ret = bpf_map_lookup_elem(stats_fd, &next_key, &value);
+                    if (ret < 0) {
+                            fprintf(stderr, "Failed to lookup elem with key %s: %s\n", src_ip, strerror(-ret));
+                            break;
+                    }
+                    printf("%s: %lld packets, %lld bytes\n", src_ip, value.packets, value.bytes);
+                    cur_key = &next_key;
+            } while (next == 0);
+    }