From: Marat Khalili <qm2k21@gmail.com>
To: Konstantin Ananyev <konstantin.ananyev@huawei.com>,
Marat Khalili <qm2k21@gmail.com>
Cc: dev@dpdk.org, stephen@networkplumber.org
Subject: [PATCH 1/9] bpf: document packet callback unload sequence
Date: Sun, 11 Oct 2026 18:21:49 +0000 [thread overview]
Message-ID: <20261011182158.124246-2-qm2k21@gmail.com> (raw)
In-Reply-To: <20261011182158.124246-1-qm2k21@gmail.com>
Add a detailed comment with the sequence diagram to bpf_pkt.c describing
the current de-facto synchronization protocol on BPF program unload
between the control plane and the data plane.
Signed-off-by: Marat Khalili <qm2k21@gmail.com>
---
lib/bpf/bpf_pkt.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 64 insertions(+), 1 deletion(-)
diff --git a/lib/bpf/bpf_pkt.c b/lib/bpf/bpf_pkt.c
index f072fdaaed..4223ef624e 100644
--- a/lib/bpf/bpf_pkt.c
+++ b/lib/bpf/bpf_pkt.c
@@ -21,9 +21,72 @@
#include "bpf_impl.h"
/*
- * information about installed BPF rx/tx callback
+ * CP-DP synchronization mechanism
+ *
+ * After the callback is installed to a port/queue, removing it safely is a non-trivial task.
+ * The `rte_bpf` object can only be destroyed while the callback is not being executed,
+ * while the number of synchronization calls in the packet callback must be minimized.
+ *
+ * APIs `rte_eth_remove_rx_callback` and `rte_eth_remove_tx_callback` remove the callback from
+ * the callback list and upon return guarantee that it will not be called again,
+ * but not that any existing invocation of the callback has completed.
+ *
+ * Current synchronization mechanism relies on the following per-port/queue entities:
+ * 1. List of installed port/queue callbacks ("cbs" on the diagram);
+ * 2. Member `cb` of struct `bpf_eth_cbi`;
+ * 3. Member `use` of struct `bpf_eth_cbi`;
+ *
+ * BPF program is only executed if the first two are set, and increments `use` at the start and
+ * end of the callback execution so that in the safe state its value is even.
+ * This relies on only one thread calling the per-port/queue callback at a time.
+ *
+ * Unload processing starts with clearing the first two entities,
+ * then waits for `use` to become even before destroying the BPF program.
+ * The whole operation is protected by a control-plane-only spinlock (not shown on the diagram).
+ *
+ * Typical sequence of synchronization actions between CP and DP on unload is shown below:
+ * ```
+ * +----+ +-----+ +-----+ +----+ +-----+ +----+
+ * | CP | | cbs | | use | | cb | | bpf | | DP |
+ * +----+ +-----+ +-----+ +----+ +-----+ +----+
+ * | | | | | |
+ * | |<---------------------- fetch callback -----------------------|
+ * | | | | | |
+ * | | |<------------- increment to odd --------------|
+ * | | | | | |
+ * |-- remove callback -->| | | | rte_smp_mb()
+ * | | | | | |
+ * | | | |<------ fetch non-NULL --------|
+ * | | | | | |
+ * |------------------- store NULL --------------------->| | |
+ * | | | | | |
+ * rte_smp_mb() | | | | |
+ * | | | | | |
+ * rte_smp_mb() | | | | |
+ * | | | | | |
+ * +-if use is odd | | | | if cb not NULL-+
+ * | | | | | | | |
+ * | +------------ fetch odd ------------>| | |<-- execute --+ |
+ * | | | | | | BPF program | |
+ * | +-while use is same | | | | +-+
+ * | | | | | | | |
+ * | | +---------- fetch odd ------------>| | | rte_smp_rmb()
+ * | | | | | | | |
+ * +-+-+ | |<------------- increment to even -------------|
+ * | | | | | |
+ * |------------- fetch even ------------>| | | |
+ * | | | | | |
+ * |----------------- destroy BPF program and cleanup ----------------->| |
+ * | | | | | |
+ * +----+ +-----+ +-----+ +----+ +-----+ +----+
+ * | CP | | cbs | | use | | cb | | bpf | | DP |
+ * +----+ +-----+ +-----+ +----+ +-----+ +----+
+ * ```
*/
+/*
+ * information about installed BPF rx/tx callback
+ */
struct __rte_cache_aligned bpf_eth_cbi {
/* used by both data & control path */
RTE_ATOMIC(uint32_t) use; /*usage counter */
--
2.43.0
next prev parent reply other threads:[~2026-10-11 18:22 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-11 18:21 [PATCH 0/9] bpf: clean up and document ethdev callback synchronization Marat Khalili
2026-10-11 18:21 ` Marat Khalili [this message]
2026-10-11 18:21 ` [PATCH 2/9] bpf: document ethdev callback threading limitations Marat Khalili
2026-10-11 18:21 ` [PATCH 3/9] bpf: reject Tx callback installation with MT_LOCKFREE Marat Khalili
2026-10-11 18:21 ` [PATCH 4/9] bpf: add callback flag to skip multi-thread check Marat Khalili
2026-10-11 19:48 ` Stephen Hemminger
2026-10-11 20:12 ` Marat Khalili
2026-10-11 18:21 ` [PATCH 5/9] bpf: remove redundant memory barrier on unload Marat Khalili
2026-10-11 19:50 ` Stephen Hemminger
2026-10-11 20:14 ` Marat Khalili
2026-10-11 18:21 ` [PATCH 6/9] bpf: simplify callback inuse tracking to boolean flag Marat Khalili
2026-10-11 19:51 ` Stephen Hemminger
2026-10-11 20:17 ` Marat Khalili
2026-10-11 18:21 ` [PATCH 7/9] bpf: reformat packet callback unload diagram Marat Khalili
2026-10-11 18:21 ` [PATCH 8/9] bpf: use relaxed stores for callback inuse tracking Marat Khalili
2026-10-11 19:52 ` Stephen Hemminger
2026-10-11 20:24 ` Marat Khalili
2026-10-11 18:21 ` [PATCH 9/9] bpf: make callback handle atomic with relaxed access Marat Khalili
2026-10-11 19:54 ` Stephen Hemminger
2026-10-11 20:31 ` Marat Khalili
2026-10-11 20:29 ` [RFC] bpf: fix callback unload with concurrent Tx burst Stephen Hemminger
2026-10-12 21:09 ` [PATCH v2 0/9] bpf: clean up and document ethdev callback synchronization Marat Khalili
2026-10-12 21:09 ` [PATCH v2 1/9] bpf: document packet callback unload sequence Marat Khalili
2026-10-12 21:09 ` [PATCH v2 2/9] bpf: document ethdev callback threading limitations Marat Khalili
2026-10-12 21:44 ` Stephen Hemminger
2026-10-12 21:09 ` [PATCH v2 3/9] bpf: reject Tx callback installation with MT_LOCKFREE Marat Khalili
2026-10-12 21:09 ` [PATCH v2 4/9] bpf: remove redundant memory barrier on unload Marat Khalili
2026-10-12 21:09 ` [PATCH v2 5/9] bpf: simplify callback inuse tracking to boolean flag Marat Khalili
2026-10-12 21:09 ` [PATCH v2 6/9] bpf: reformat packet callback unload diagram Marat Khalili
2026-10-12 21:09 ` [PATCH v2 7/9] bpf: use relaxed accesses for callback inuse tracking Marat Khalili
2026-10-12 21:09 ` [PATCH v2 8/9] bpf: make callback handle atomic with relaxed access Marat Khalili
2026-10-12 21:09 ` [PATCH v2 9/9] bpf: add callback flag to skip multi-thread check Marat Khalili
2026-10-13 16:55 ` Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261011182158.124246-2-qm2k21@gmail.com \
--to=qm2k21@gmail.com \
--cc=dev@dpdk.org \
--cc=konstantin.ananyev@huawei.com \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox