* [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP
@ 2022-11-07 16:52 mtahhan
2022-11-07 16:52 ` [PATCH bpf-next v3 1/1] " mtahhan
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: mtahhan @ 2022-11-07 16:52 UTC (permalink / raw)
To: bpf, linux-doc
Cc: jbrouer, thoiland, donhunte, Maryam Tahhan, Lorenzo Bianconi
From: Maryam Tahhan <mtahhan@redhat.com>
Add documentation for BPF_MAP_TYPE_CPUMAP including
kernel version introduced, usage and examples.
Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
v3:
- Updated introduction to use cpumap definition from kernel/bpf/cpumap.c
- Separated examples and APIs under Kernel and Userspace headings.
- Updated Userspace function signatures.
- Fixed typos.
- Migrated the use of u32 types to __u32.
v2:
- Removed TMI.
- Updated example to use a round robin scheme.
Maryam Tahhan (1):
docs: BPF_MAP_TYPE_CPUMAP
Documentation/bpf/map_cpumap.rst | 166 +++++++++++++++++++++++++++++++
kernel/bpf/cpumap.c | 9 +-
2 files changed, 172 insertions(+), 3 deletions(-)
create mode 100644 Documentation/bpf/map_cpumap.rst
--
2.35.3
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next v3 1/1] docs: BPF_MAP_TYPE_CPUMAP
2022-11-07 16:52 [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP mtahhan
@ 2022-11-07 16:52 ` mtahhan
2022-11-07 20:17 ` Yonghong Song
2022-11-13 0:19 ` Akira Yokosawa
2022-11-11 19:29 ` [PATCH bpf-next v3 0/1] " Andrii Nakryiko
2022-11-11 19:30 ` patchwork-bot+netdevbpf
2 siblings, 2 replies; 6+ messages in thread
From: mtahhan @ 2022-11-07 16:52 UTC (permalink / raw)
To: bpf, linux-doc
Cc: jbrouer, thoiland, donhunte, Maryam Tahhan, Lorenzo Bianconi
From: Maryam Tahhan <mtahhan@redhat.com>
Add documentation for BPF_MAP_TYPE_CPUMAP including
kernel version introduced, usage and examples.
Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
Documentation/bpf/map_cpumap.rst | 166 +++++++++++++++++++++++++++++++
kernel/bpf/cpumap.c | 9 +-
2 files changed, 172 insertions(+), 3 deletions(-)
create mode 100644 Documentation/bpf/map_cpumap.rst
diff --git a/Documentation/bpf/map_cpumap.rst b/Documentation/bpf/map_cpumap.rst
new file mode 100644
index 000000000000..eaf57b38cafd
--- /dev/null
+++ b/Documentation/bpf/map_cpumap.rst
@@ -0,0 +1,166 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+.. Copyright (C) 2022 Red Hat, Inc.
+
+===================
+BPF_MAP_TYPE_CPUMAP
+===================
+
+.. note::
+ - ``BPF_MAP_TYPE_CPUMAP`` was introduced in kernel version 4.15
+
+.. kernel-doc:: kernel/bpf/cpumap.c
+ :doc: cpu map
+
+An example use-case for this map type is software based Receive Side Scaling (RSS).
+
+The CPUMAP represents the CPUs in the system indexed as the map-key, and the
+map-value is the config setting (per CPUMAP entry). Each CPUMAP entry has a dedicated
+kernel thread bound to the given CPU to represent the remote CPU execution unit.
+
+Starting from Linux kernel version 5.9 the CPUMAP can run a second XDP program
+on the remote CPU. This allows an XDP program to split its processing across
+multiple CPUs. For example, a scenario where the initial CPU (that sees/receives
+the packets) needs to do minimal packet processing and the remote CPU (to which
+the packet is directed) can afford to spend more cycles processing the frame. The
+initial CPU is where the XDP redirect program is executed. The remote CPU
+receives raw ``xdp_frame`` objects.
+
+Usage
+=====
+
+Kernel BPF
+----------
+.. c:function::
+ long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
+
+ Redirect the packet to the endpoint referenced by ``map`` at index ``key``.
+ For ``BPF_MAP_TYPE_CPUMAP`` this map contains references to CPUs.
+
+ The lower two bits of ``flags`` are used as the return code if the map lookup
+ fails. This is so that the return value can be one of the XDP program return
+ codes up to ``XDP_TX``, as chosen by the caller.
+
+Userspace
+---------
+.. note::
+ CPUMAP entries can only be updated/looked up/deleted from user space and not
+ from an eBPF program. Trying to call these functions from a kernel eBPF
+ program will result in the program failing to load and a verifier warning.
+
+.. c:function::
+ int bpf_map_update_elem(int fd, const void *key, const void *value,
+ __u64 flags);
+
+ CPU entries can be added or updated using the ``bpf_map_update_elem()``
+ helper. This helper replaces existing elements atomically. The ``value`` parameter
+ can be ``struct bpf_cpumap_val``.
+
+ .. code-block:: c
+
+ struct bpf_cpumap_val {
+ __u32 qsize; /* queue size to remote target CPU */
+ union {
+ int fd; /* prog fd on map write */
+ __u32 id; /* prog id on map read */
+ } bpf_prog;
+ };
+
+ The flags argument can be one of the following:
+ - BPF_ANY: Create a new element or update an existing element.
+ - BPF_NOEXIST: Create a new element only if it did not exist.
+ - BPF_EXIST: Update an existing element.
+
+.. c:function::
+ int bpf_map_lookup_elem(int fd, const void *key, void *value);
+
+ CPU entries can be retrieved using the ``bpf_map_lookup_elem()``
+ helper.
+
+.. c:function::
+ int bpf_map_delete_elem(int fd, const void *key);
+
+ CPU 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.
+
+Examples
+========
+Kernel
+------
+
+The following code snippet shows how to declare a ``BPF_MAP_TYPE_CPUMAP`` called
+``cpu_map`` and how to redirect packets to a remote CPU using a round robin scheme.
+
+.. code-block:: c
+
+ struct {
+ __uint(type, BPF_MAP_TYPE_CPUMAP);
+ __type(key, __u32);
+ __type(value, struct bpf_cpumap_val);
+ __uint(max_entries, 12);
+ } cpu_map SEC(".maps");
+
+ struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __type(key, __u32);
+ __type(value, __u32);
+ __uint(max_entries, 12);
+ } cpus_available SEC(".maps");
+
+ struct {
+ __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+ __type(key, __u32);
+ __type(value, __u32);
+ __uint(max_entries, 1);
+ } cpus_iterator SEC(".maps");
+
+ SEC("xdp")
+ int xdp_redir_cpu_round_robin(struct xdp_md *ctx)
+ {
+ __u32 key = 0;
+ __u32 cpu_dest = 0;
+ __u32 *cpu_selected, *cpu_iterator;
+ __u32 cpu_idx;
+
+ cpu_iterator = bpf_map_lookup_elem(&cpus_iterator, &key);
+ if (!cpu_iterator)
+ return XDP_ABORTED;
+ cpu_idx = *cpu_iterator;
+
+ *cpu_iterator += 1;
+ if (*cpu_iterator == bpf_num_possible_cpus())
+ *cpu_iterator = 0;
+
+ cpu_selected = bpf_map_lookup_elem(&cpus_available, &cpu_idx);
+ if (!cpu_selected)
+ return XDP_ABORTED;
+ cpu_dest = *cpu_selected;
+
+ if (cpu_dest >= bpf_num_possible_cpus())
+ return XDP_ABORTED;
+
+ return bpf_redirect_map(&cpu_map, cpu_dest, 0);
+ }
+
+Userspace
+---------
+
+The following code snippet shows how to dynamically set the max_entries for a
+CPUMAP to the max number of cpus available on the system.
+
+.. code-block:: c
+
+ int set_max_cpu_entries(struct bpf_map *cpu_map)
+ {
+ if (bpf_map__set_max_entries(cpu_map, libbpf_num_possible_cpus()) < 0) {
+ fprintf(stderr, "Failed to set max entries for cpu_map map: %s",
+ strerror(errno));
+ return -1;
+ }
+ return 0;
+ }
+
+References
+===========
+
+- https://developers.redhat.com/blog/2021/05/13/receive-side-scaling-rss-with-ebpf-and-cpumap#redirecting_into_a_cpumap
diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c
index b5ba34ddd4b6..9747550c9088 100644
--- a/kernel/bpf/cpumap.c
+++ b/kernel/bpf/cpumap.c
@@ -4,13 +4,16 @@
* Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
*/
-/* The 'cpumap' is primarily used as a backend map for XDP BPF helper
+/**
+ * DOC: cpu map
+ * The 'cpumap' is primarily used as a backend map for XDP BPF helper
* call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
*
- * Unlike devmap which redirects XDP frames out another NIC device,
+ * Unlike devmap which redirects XDP frames out to another NIC device,
* this map type redirects raw XDP frames to another CPU. The remote
* CPU will do SKB-allocation and call the normal network stack.
- *
+ */
+/*
* This is a scalability and isolation mechanism, that allow
* separating the early driver network XDP layer, from the rest of the
* netstack, and assigning dedicated CPUs for this stage. This
--
2.35.3
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 1/1] docs: BPF_MAP_TYPE_CPUMAP
2022-11-07 16:52 ` [PATCH bpf-next v3 1/1] " mtahhan
@ 2022-11-07 20:17 ` Yonghong Song
2022-11-13 0:19 ` Akira Yokosawa
1 sibling, 0 replies; 6+ messages in thread
From: Yonghong Song @ 2022-11-07 20:17 UTC (permalink / raw)
To: mtahhan, bpf, linux-doc; +Cc: jbrouer, thoiland, donhunte, Lorenzo Bianconi
On 11/7/22 8:52 AM, mtahhan@redhat.com wrote:
> From: Maryam Tahhan <mtahhan@redhat.com>
>
> Add documentation for BPF_MAP_TYPE_CPUMAP including
> kernel version introduced, usage and examples.
>
> Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
> Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP
2022-11-07 16:52 [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP mtahhan
2022-11-07 16:52 ` [PATCH bpf-next v3 1/1] " mtahhan
@ 2022-11-11 19:29 ` Andrii Nakryiko
2022-11-11 19:30 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2022-11-11 19:29 UTC (permalink / raw)
To: mtahhan; +Cc: bpf, linux-doc, jbrouer, thoiland, donhunte, Lorenzo Bianconi
On Mon, Nov 7, 2022 at 8:06 AM <mtahhan@redhat.com> wrote:
>
> From: Maryam Tahhan <mtahhan@redhat.com>
>
> Add documentation for BPF_MAP_TYPE_CPUMAP including
> kernel version introduced, usage and examples.
>
> Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
> Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
>
> v3:
> - Updated introduction to use cpumap definition from kernel/bpf/cpumap.c
> - Separated examples and APIs under Kernel and Userspace headings.
> - Updated Userspace function signatures.
> - Fixed typos.
> - Migrated the use of u32 types to __u32.
>
> v2:
> - Removed TMI.
> - Updated example to use a round robin scheme.
>
> Maryam Tahhan (1):
> docs: BPF_MAP_TYPE_CPUMAP
>
> Documentation/bpf/map_cpumap.rst | 166 +++++++++++++++++++++++++++++++
> kernel/bpf/cpumap.c | 9 +-
> 2 files changed, 172 insertions(+), 3 deletions(-)
> create mode 100644 Documentation/bpf/map_cpumap.rst
>
> --
> 2.35.3
>
Similar to Donald's patch, there is no need for a cover letter for
single patch submission. Applied to bpf-next, thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP
2022-11-07 16:52 [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP mtahhan
2022-11-07 16:52 ` [PATCH bpf-next v3 1/1] " mtahhan
2022-11-11 19:29 ` [PATCH bpf-next v3 0/1] " Andrii Nakryiko
@ 2022-11-11 19:30 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-11-11 19:30 UTC (permalink / raw)
To: Maryam Tahhan; +Cc: bpf, linux-doc, jbrouer, thoiland, donhunte, lorenzo
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Mon, 7 Nov 2022 11:52:06 -0500 you wrote:
> From: Maryam Tahhan <mtahhan@redhat.com>
>
> Add documentation for BPF_MAP_TYPE_CPUMAP including
> kernel version introduced, usage and examples.
>
> Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
> Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
>
> [...]
Here is the summary with links:
- [bpf-next,v3,1/1] docs: BPF_MAP_TYPE_CPUMAP
https://git.kernel.org/bpf/bpf-next/c/d9c982d25c47
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 1/1] docs: BPF_MAP_TYPE_CPUMAP
2022-11-07 16:52 ` [PATCH bpf-next v3 1/1] " mtahhan
2022-11-07 20:17 ` Yonghong Song
@ 2022-11-13 0:19 ` Akira Yokosawa
1 sibling, 0 replies; 6+ messages in thread
From: Akira Yokosawa @ 2022-11-13 0:19 UTC (permalink / raw)
To: mtahhan
Cc: bpf, donhunte, jbrouer, linux-doc, lorenzo, thoiland,
Akira Yokosawa
Hi Maryam,
I know this has already been applied, but I see warnings from
"make htmldocs" on pbf-next caused by this. See inline comment
below.
On Mon, 7 Nov 2022 11:52:07 -0500, mtahhan@redhat.com wrote:
> From: Maryam Tahhan <mtahhan@redhat.com>
>
> Add documentation for BPF_MAP_TYPE_CPUMAP including
> kernel version introduced, usage and examples.
>
> Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
> Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
> ---
> Documentation/bpf/map_cpumap.rst | 166 +++++++++++++++++++++++++++++++
> kernel/bpf/cpumap.c | 9 +-
> 2 files changed, 172 insertions(+), 3 deletions(-)
> create mode 100644 Documentation/bpf/map_cpumap.rst
>
> diff --git a/Documentation/bpf/map_cpumap.rst b/Documentation/bpf/map_cpumap.rst
> new file mode 100644
> index 000000000000..eaf57b38cafd
> --- /dev/null
> +++ b/Documentation/bpf/map_cpumap.rst
> @@ -0,0 +1,166 @@
> +.. SPDX-License-Identifier: GPL-2.0-only
> +.. Copyright (C) 2022 Red Hat, Inc.
> +
> +===================
> +BPF_MAP_TYPE_CPUMAP
> +===================
> +
> +.. note::
> + - ``BPF_MAP_TYPE_CPUMAP`` was introduced in kernel version 4.15
> +
> +.. kernel-doc:: kernel/bpf/cpumap.c
> + :doc: cpu map
> +
> +An example use-case for this map type is software based Receive Side Scaling (RSS).
> +
> +The CPUMAP represents the CPUs in the system indexed as the map-key, and the
> +map-value is the config setting (per CPUMAP entry). Each CPUMAP entry has a dedicated
> +kernel thread bound to the given CPU to represent the remote CPU execution unit.
> +
> +Starting from Linux kernel version 5.9 the CPUMAP can run a second XDP program
> +on the remote CPU. This allows an XDP program to split its processing across
> +multiple CPUs. For example, a scenario where the initial CPU (that sees/receives
> +the packets) needs to do minimal packet processing and the remote CPU (to which
> +the packet is directed) can afford to spend more cycles processing the frame. The
> +initial CPU is where the XDP redirect program is executed. The remote CPU
> +receives raw ``xdp_frame`` objects.
> +
> +Usage
> +=====
> +
> +Kernel BPF
> +----------
> +.. c:function::
> + long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
> +
> + Redirect the packet to the endpoint referenced by ``map`` at index ``key``.
> + For ``BPF_MAP_TYPE_CPUMAP`` this map contains references to CPUs.
> +
> + The lower two bits of ``flags`` are used as the return code if the map lookup
> + fails. This is so that the return value can be one of the XDP program return
> + codes up to ``XDP_TX``, as chosen by the caller.
> +
> +Userspace
> +---------
> +.. note::
> + CPUMAP entries can only be updated/looked up/deleted from user space and not
> + from an eBPF program. Trying to call these functions from a kernel eBPF
> + program will result in the program failing to load and a verifier warning.
> +
> +.. c:function::
> + int bpf_map_update_elem(int fd, const void *key, const void *value,
> + __u64 flags);
Sphinx's domain directives assumes single-line declarations [1].
Hence "make htmldocs" with Sphinx >= 3.1 emits warnings like:
/linux/Documentation/bpf/map_cpumap.rst:50: WARNING: Error in declarator or parameters
Invalid C declaration: Expected identifier in nested name. [error at 67]
int bpf_map_update_elem(int fd, const void *key, const void *value,
-------------------------------------------------------------------^
/linux/Documentation/bpf/map_cpumap.rst:50: WARNING: Error in declarator or parameters
Invalid C declaration: Expecting "(" in parameters. [error at 11]
__u64 flags);
-----------^
This can be fixed by using reST's continuation line as follows:
.. c:function::
int bpf_map_update_elem(int fd, const void *key, const void *value, \
__u64 flags);
, or by permitting a somewhat long declaration:
.. c:function::
int bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags);
Can you please fix it?
[1]: https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#basic-markup
Thanks, Akira
[...]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-11-13 0:19 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-07 16:52 [PATCH bpf-next v3 0/1] docs: BPF_MAP_TYPE_CPUMAP mtahhan
2022-11-07 16:52 ` [PATCH bpf-next v3 1/1] " mtahhan
2022-11-07 20:17 ` Yonghong Song
2022-11-13 0:19 ` Akira Yokosawa
2022-11-11 19:29 ` [PATCH bpf-next v3 0/1] " Andrii Nakryiko
2022-11-11 19:30 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).