On 18 Nov 2026, at 22:55, Aaron Conole wrote:
> This series reworks the userspace connection tracker in Open vSwitch in > order to introduce an infrastructure that future offload providers could > use to offload connections, similar to facilities provided in the TC offload > path. The goal is to enable userspace datapath offloads to provide > additional connection tracking offload support. > > Patch 1 introduces a new conn private storage and a common initializer that > will be used throughout the connection lifecycle for more modular metadata. > Patch 2 then adds a priority-ordered observer infrastructure for monitoring, > and optionally handling connection state transitions. Patches 3 + 4 are > restructures of existing code into separate files and prep work for > offloading in the TCP module. > > Patches 5-10 are the meat of the offload infrastructure. They provide the > basic support - add/del/est/update indicators that give enough context into > the userspace connection tracking layers to actually inform the hardware and > keep the userspace updated. Additionally, we automatically disable tcp > sequence number checking on connections that are 'tracked'. > > Patch 11 provides a dummy offloading implementation. > > Patch 12 is Documentation / NEWS updates > > This work stops at the connection lifecycle boundary, rather than adding > additional APIs for other types of CT queries. For example, the dpif layer > may need assistance querying whether a particular match can be offloaded. > That API work is still in progress (although we do provide some assistance > with specific querying). > > NOTE: Some in-flight patches will conflict with this, notably the ALG > refactor, and (if they get posted) Felix's ct optimization. > > RFC: > https://mail.openvswitch.org/pipermail/ovs-dev/2026-April/431605.html > > RFC->v1: > https://mail.openvswitch.org/pipermail/ovs-dev/2026-May/432353.html > Fixed a number of freebsd / compiler issues > Fixed calling offload conn_del in the conn_clean case > Fixed an incorrect gate in the sweeping code > > v1->v2: > https://mail.openvswitch.org/pipermail/ovs-dev/2026-July/434631.html > Fixed up spelling mistakes, null checks, and nits. > Changed ct_private_id to a 32-bit unsigned type > Updated example > Reworked comments > > v2->v3: > BIG NOTE: this does a big rework of the series based on all the feedback. > I'm hoping that I caught it all. It also means a large class > of comments got invalidated. There is a bigger design change, > so it needs some careful analysis from that perspective, but > I believe it should offer all the mechanisms needed to > implement conntrack offloading. > > Fixed up some header consistency > Dictionary entries > Updates from Eelco's feedback (ex: error sentinels, spacing, etc) > 'Tighter' coupling to dpif-offload layer. > Redesigned the API based on feedback from Eelco to drop the individual > api call sites > Redesigned the sync between HW and SW to fix up weird edge cases like > Flush during add, signals during updates, etc. > Disambiguated 'established' from 'tracked' and 'hw_confirmed' states. > Time extension needs to be tightly checked. > Updated the documentation to describe the sync and integration better. Hi Aaron, I’ve left my comments on the individual patches. To cut down on the back-and-forth, I’ve attached a diff with some API changes that I hope make the direction I’m suggesting clearer. The diff is meant to illustrate the open/close API and doesn’t address all of my review comments. The series defines a separate ct-offload class. Based on the existing DOCA offload code, I expect that class to be a thin layer, with most, if not all, of the resources managed by dpif-offload-doca or its CT implementation. Let me know what you think. It might be worth a quick call to discuss the API. I’ve copied Gaetan, since he’ll likely be the first to use it and may have useful insights from the existing implementation. Cheers, Eelco ---------- PATCH ---------- diff --git a/lib/automake.mk b/lib/automake.mk index b22b69d01..300c679b2 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -102,6 +102,7 @@ lib_libopenvswitch_la_SOURCES = \ lib/dpif-offload.c \ lib/dpif-offload.h \ lib/dpif-offload-dummy.c \ + lib/dpif-offload-dummy-private.h \ lib/dpif-offload-provider.h \ lib/dpif-provider.h \ lib/dpif.c \ diff --git a/lib/conntrack-tcp.c b/lib/conntrack-tcp.c index d98856368..3d6f25cb8 100644 --- a/lib/conntrack-tcp.c +++ b/lib/conntrack-tcp.c @@ -159,6 +159,7 @@ tcp_conn_update(struct conntrack *ct, struct conn *conn_, if (!conn) { return CT_UPDATE_INVALID; } + struct tcp_header *tcp = dp_packet_l4(pkt); /* The peer that sent 'pkt' */ struct tcp_peer *src = &conn->peer[reply ? 1 : 0]; @@ -434,10 +435,10 @@ tcp_new_conn(struct conntrack *ct, struct dp_packet *pkt, long long now, uint32_t tp_id) { struct tcp_header *tcp = dp_packet_l4(pkt); + uint16_t tcp_flags = TCP_FLAGS(tcp->tcp_ctl); struct conn_tcp_state *tcp_state; - struct conn *newconn; struct tcp_peer *src, *dst; - uint16_t tcp_flags = TCP_FLAGS(tcp->tcp_ctl); + struct conn *newconn; tcp_state = xzalloc(sizeof *tcp_state); src = &tcp_state->peer[0]; @@ -500,6 +501,7 @@ tcp_conn_get_protoinfo(const struct conn *conn_, OVS_REQUIRES(conn_->lock) { const struct conn_tcp_state *conn = conn_tcp_state_get(conn_); + if (!conn) { return; } diff --git a/lib/conntrack.c b/lib/conntrack.c index 96ef51351..47be10107 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -514,6 +514,7 @@ conn_clean(struct conntrack *ct, struct conn *conn) OVS_EXCLUDED(conn->lock, ct->ct_lock) { bool reclaimed = false; + if (!atomic_compare_exchange_strong(&conn->reclaimed, &reclaimed, true)) { return; @@ -2847,7 +2848,7 @@ conn_to_ct_dpif_entry(const struct conn *conn, struct ct_dpif_entry *entry, ovs_mutex_lock(&conn->lock); entry->mark = conn->mark; memcpy(&entry->labels, &conn->label, sizeof entry->labels); - entry->offloaded = ct_offload_conn_is_tracked(conn); + entry->offloaded = ct_offload_conn_is_hw_confirmed(conn); long long expiration = conn_expiration(conn) - now; diff --git a/lib/ct-dpif.h b/lib/ct-dpif.h index 6d8a0f08c..81b55b3a7 100644 --- a/lib/ct-dpif.h +++ b/lib/ct-dpif.h @@ -213,9 +213,7 @@ struct ct_dpif_entry { uint32_t timeout; uint32_t mark; uint32_t bkt; /* CT bucket number. */ - /* Set by userspace datapaths when the connection is tracked by a CT - * offload provider. Never set for kernel datapaths. */ - bool offloaded; + bool offloaded; /* True if a userspace CT entry is hardware-confirmed. */ }; enum { diff --git a/lib/ct-offload-dummy.c b/lib/ct-offload-dummy.c index 85514d9cd..cfecf28e4 100644 --- a/lib/ct-offload-dummy.c +++ b/lib/ct-offload-dummy.c @@ -16,16 +16,15 @@ #include <config.h> -#include <errno.h> - #include "ct-offload-dummy.h" +#include <errno.h> + #include "conntrack-private.h" #include "coverage.h" #include "ct-offload.h" -#include "dummy.h" +#include "dpif-offload-dummy-private.h" #include "netdev.h" -#include "netdev-provider.h" #include "openvswitch/list.h" #include "openvswitch/vlog.h" #include "ovs-atomic.h" @@ -42,24 +41,18 @@ COVERAGE_DEFINE(ct_offload_dummy_del); COVERAGE_DEFINE(ct_offload_dummy_upd); COVERAGE_DEFINE(ct_offload_dummy_est); -/* Test knobs for UPD reporting: "active" (default) reports a fresh - * last_used (connections never expire in software), "idle" reports no - * activity (normal expiry), "evicted" reports ENOENT (demotion). */ +/* Test controls for UPD responses: active (default) reports a current + * timestamp, idle reports no activity, and evicted reports ENOENT. */ static atomic_bool dummy_hw_idle; static atomic_bool dummy_hw_evicted; -/* Per-connection state. Port names are copies: ctx.netdev_in is only - * valid during the submit, and ports may be deleted while tracked. */ struct ct_dummy_entry { struct ovs_list list_node; const struct conn *conn; - char *fwd_in_name; /* Input port of the original - * direction. */ - char *rev_in_name; /* Input port of the reply - * direction. */ + char *fwd_in_name; /* Input port of the original direction. */ + char *rev_in_name; /* Input port of the reply direction. */ }; -/* Frees 'e' and the strings it owns. */ static void dummy_entry_destroy(struct ct_dummy_entry *e) { @@ -73,7 +66,6 @@ static struct ovs_mutex dummy_mutex = OVS_MUTEX_INITIALIZER; static struct ovs_list dummy_conns OVS_GUARDED_BY(dummy_mutex) = OVS_LIST_INITIALIZER(&dummy_conns); -/* Lookup must be called with dummy_mutex held. */ static struct ct_dummy_entry * dummy_find__(const struct conn *conn) OVS_REQUIRES(dummy_mutex) @@ -88,35 +80,29 @@ dummy_find__(const struct conn *conn) return NULL; } -/* Returns 'name', or "(nil)" if it is NULL. */ static const char * dummy_netdev_name(const char *name) { return name ? name : "(nil)"; } -/* Only dummy-netdev ports, mirroring the dpif-offload-dummy - * can_offload check. */ static bool -dummy_netdev_offloadable(const struct netdev *netdev) +dummy_flow_might_offload(void *instance_ctx, + const struct ct_offload_flow_ctx *flow_ctx) { - return netdev && is_dummy_netdev_class(netdev_get_class(netdev)); -} + const struct dummy_offload *offload = instance_ctx; -static bool -dummy_flow_might_offload(const struct ct_offload_flow_ctx *ctx) -{ - return dummy_netdev_offloadable(ctx->netdev_in); + return dpif_offload_dummy_owns_netdev(offload, flow_ctx->netdev_in); } static void -dummy_op_add(struct ct_offload_op *op) +dummy_op_add(const struct dummy_offload *offload, struct ct_offload_op *op) { const struct ct_offload_ctx *ctx = &op->ctx; ovs_assert(ctx->conn); - if (!dummy_netdev_offloadable(ctx->netdev_in)) { + if (!dpif_offload_dummy_owns_netdev(offload, ctx->netdev_in)) { /* Decline: leave the pre-filled result untouched. */ return; } @@ -134,7 +120,7 @@ dummy_op_add(struct ct_offload_op *op) if (dummy_find__(ctx->conn)) { ovs_mutex_unlock(&dummy_mutex); dummy_entry_destroy(e); - VLOG_WARN_RL(&rl, "conn %p added twice", ctx->conn); + VLOG_WARN_RL(&rl, "conn %p already exists", ctx->conn); op->error = EEXIST; return; } @@ -152,95 +138,91 @@ static void dummy_op_del(struct ct_offload_op *op) { const struct ct_offload_ctx *ctx = &op->ctx; + struct ct_dummy_entry *e; ovs_assert(ctx->conn); ovs_mutex_lock(&dummy_mutex); - struct ct_dummy_entry *e = dummy_find__(ctx->conn); - + e = dummy_find__(ctx->conn); if (e) { ovs_list_remove(&e->list_node); dummy_entry_destroy(e); + op->error = 0; + COVERAGE_INC(ct_offload_dummy_del); + VLOG_DBG("conn del: conn=%p", ctx->conn); } ovs_mutex_unlock(&dummy_mutex); - - op->error = 0; - COVERAGE_INC(ct_offload_dummy_del); - VLOG_DBG("conn del: conn=%p", ctx->conn); } static void -dummy_op_establish(struct ct_offload_op *op) +dummy_op_establish(const struct dummy_offload *offload, + struct ct_offload_op *op) { const struct ct_offload_ctx *ctx = &op->ctx; + struct ct_dummy_entry *e; ovs_assert(ctx->conn); - char *rev_in_name = ctx->netdev_in - ? xstrdup(netdev_get_name(ctx->netdev_in)) : NULL; - char *log_fwd_name = NULL; - char *log_rev_name = NULL; - ovs_mutex_lock(&dummy_mutex); - struct ct_dummy_entry *e = dummy_find__(ctx->conn); - - if (e) { - if (!e->rev_in_name) { - e->rev_in_name = rev_in_name; - rev_in_name = NULL; /* Ownership transferred to 'e'. */ - op->hw_confirmed = true; - COVERAGE_INC(ct_offload_dummy_est); - } - log_fwd_name = e->fwd_in_name ? xstrdup(e->fwd_in_name) : NULL; - log_rev_name = e->rev_in_name ? xstrdup(e->rev_in_name) : NULL; + e = dummy_find__(ctx->conn); + if (e && !e->rev_in_name + && dpif_offload_dummy_owns_netdev(offload, ctx->netdev_in)) { + e->rev_in_name = xstrdup(netdev_get_name(ctx->netdev_in)); + op->error = 0; + op->hw_confirmed = true; + COVERAGE_INC(ct_offload_dummy_est); + VLOG_DBG("conn established: conn=%p fwd_in=%s rev_in=%s", + ctx->conn, dummy_netdev_name(e->fwd_in_name), + e->rev_in_name); } ovs_mutex_unlock(&dummy_mutex); - - VLOG_DBG("conn established: conn=%p fwd_in=%s rev_in=%s", - ctx->conn, dummy_netdev_name(log_fwd_name), - dummy_netdev_name(log_rev_name)); - free(log_fwd_name); - free(log_rev_name); - free(rev_in_name); } static void dummy_op_upd(struct ct_offload_op *op) { const struct ct_offload_ctx *ctx = &op->ctx; + struct ct_dummy_entry *e; ovs_assert(ctx->conn); ovs_mutex_lock(&dummy_mutex); - bool found = dummy_find__(ctx->conn) != NULL; - ovs_mutex_unlock(&dummy_mutex); - - if (found) { + e = dummy_find__(ctx->conn); + if (e) { bool hw_evicted, hw_idle; atomic_read_relaxed(&dummy_hw_evicted, &hw_evicted); atomic_read_relaxed(&dummy_hw_idle, &hw_idle); COVERAGE_INC(ct_offload_dummy_upd); if (hw_evicted) { + ovs_list_remove(&e->list_node); + dummy_entry_destroy(e); op->error = ENOENT; } else { op->error = 0; op->last_used = hw_idle ? 0 : time_msec(); } + VLOG_DBG("conn update: conn=%p last_used=%lld", + ctx->conn, op->last_used); } - VLOG_DBG("conn update: conn=%p last_used=%lld", ctx->conn, - op->last_used); + ovs_mutex_unlock(&dummy_mutex); + } static void -dummy_batch_submit(const struct ct_offload_op_batch *batch) +dummy_batch_submit(void *instance_ctx, const struct ct_offload_op_batch *batch) { + const struct dummy_offload *offload = instance_ctx; struct ct_offload_op *op; CT_OFFLOAD_BATCH_OP_FOR_EACH (batch, i, op) { + if (op->error != EOPNOTSUPP) { + continue; + } + switch (op->type) { case CT_OFFLOAD_OP_ADD: - dummy_op_add(op); + dummy_op_add(offload, op); break; case CT_OFFLOAD_OP_DEL: dummy_op_del(op); @@ -249,15 +231,14 @@ dummy_batch_submit(const struct ct_offload_op_batch *batch) dummy_op_upd(op); break; case CT_OFFLOAD_OP_ESTABLISHED: - dummy_op_establish(op); + dummy_op_establish(offload, op); break; } } } -/* Drops all tracked connections on last close. */ static void -dummy_close(void) +dummy_close(void *instance_ctx OVS_UNUSED) { struct ct_dummy_entry *e; @@ -268,7 +249,6 @@ dummy_close(void) ovs_mutex_unlock(&dummy_mutex); } -/* "ct-offload-dummy/set-hw-activity" (see the knobs above). */ static void ct_offload_dummy_set_hw_activity(struct unixctl_conn *conn, int argc OVS_UNUSED, @@ -290,16 +270,18 @@ ct_offload_dummy_set_hw_activity(struct unixctl_conn *conn, unixctl_command_reply(conn, NULL); } -void -ct_offload_dummy_init(void) +static int +dummy_init(void) { unixctl_command_register("ct-offload-dummy/set-hw-activity", "active|idle|evicted", 1, 1, ct_offload_dummy_set_hw_activity, NULL); + return 0; } const struct ct_offload_class ct_offload_dummy_class = { .name = "dummy", + .init = dummy_init, .open = NULL, .close = dummy_close, .batch_submit = dummy_batch_submit, diff --git a/lib/ct-offload-dummy.h b/lib/ct-offload-dummy.h index fbe0c601c..774557313 100644 --- a/lib/ct-offload-dummy.h +++ b/lib/ct-offload-dummy.h @@ -21,12 +21,8 @@ /* Software-only CT offload provider used together with the dpif-offload * dummy provider. It is registered when the matching "dummy" dpif offload - * class is registered, and activated or deactivated by that provider - * through ct_offload_open() and ct_offload_close(). */ + * class is registered, and opened or closed by that provider through + * ct_offload_open() and ct_offload_close(). */ extern const struct ct_offload_class ct_offload_dummy_class; -/* Registers the ct-offload-dummy unixctl commands; called from - * ct_offload_module_init(). */ -void ct_offload_dummy_init(void); - #endif /* CT_OFFLOAD_DUMMY_H */ diff --git a/lib/ct-offload.c b/lib/ct-offload.c index f4451e0e1..863d65942 100644 --- a/lib/ct-offload.c +++ b/lib/ct-offload.c @@ -37,7 +37,7 @@ VLOG_DEFINE_THIS_MODULE(ct_offload); * registered dpif offload class are registered by ct_offload_module_init(), * tying CT offload to the active hardware offload provider. Registration * only makes the provider known; the matching dpif-offload provider - * activates it through ct_offload_open(). */ + * opens it for use through ct_offload_open(). */ static const struct ct_offload_class *base_ct_offload_classes[] = { &ct_offload_dummy_class, }; @@ -67,18 +67,13 @@ struct ct_offload_data { /* Node in the registered-provider list. */ struct ct_offload_class_node { const struct ct_offload_class *class; - int refcnt; /* Number of open users; providers with - * a zero refcnt are registered but not - * active and do not see operations. - * Written under the write lock, read - * under the read lock. */ + void *instance_ctx; /* Open instance context, or NULL when closed. */ struct ovs_list list_node; }; /* Global list of registered CT offload classes and a rwlock to protect it. - * Write lock is held only during register/unregister/open/close; fast-path - * operations hold the read lock so multiple PMD threads can iterate - * concurrently. */ + * Write lock is held during register/open/close; fast-path operations hold the + * read lock so multiple PMD threads can iterate concurrently. */ static struct ovs_rwlock ct_offload_rwlock = OVS_RWLOCK_INITIALIZER; static struct ovs_list ct_offload_classes OVS_GUARDED_BY(ct_offload_rwlock) @@ -102,9 +97,9 @@ ct_offload_class_find__(const char *name) /* ct_offload_register() - register a CT offload provider class. * - * Calls class->init() if provided. Registration does not activate the - * provider; the dpif-offload provider must call ct_offload_open() for - * that. Returns 0 on success or a positive errno value on failure. + * Calls class->init() if provided. The dpif-offload provider must call + * ct_offload_open() before using the registered provider. Returns 0 on + * success or a positive errno value on failure. * Attempting to register the same class name twice returns EEXIST. */ int ct_offload_register(const struct ct_offload_class *class) @@ -114,6 +109,7 @@ ct_offload_register(const struct ct_offload_class *class) ovs_assert(class); ovs_assert(class->name); + ovs_assert(!class->open || class->close); ovs_assert(class->batch_submit); ovs_assert(class->flow_might_offload); @@ -140,7 +136,7 @@ ct_offload_register(const struct ct_offload_class *class) node = xmalloc(sizeof *node); node->class = class; - node->refcnt = 0; + node->instance_ctx = NULL; ovs_list_push_back(&ct_offload_classes, &node->list_node); VLOG_DBG("registered ct offload class: %s", class->name); @@ -149,49 +145,29 @@ out: return error; } -/* ct_offload_unregister() - unregister a previously registered class. - * - * Safe to call even if the class was never registered (no-op in that - * case). */ -void -ct_offload_unregister(const struct ct_offload_class *class) -{ - struct ct_offload_class_node *node; - - ovs_assert(class); - - ovs_rwlock_wrlock(&ct_offload_rwlock); - LIST_FOR_EACH (node, list_node, &ct_offload_classes) { - if (node->class == class) { - if (node->refcnt > 0) { - VLOG_WARN("unregistering still-open ct offload class: %s", - class->name); - } - ovs_list_remove(&node->list_node); - free(node); - VLOG_DBG("unregistered ct offload class: %s", class->name); - goto out; - } - } - VLOG_WARN("attempted to unregister unknown ct offload class: %s", - class->name); - -out: - ovs_rwlock_unlock(&ct_offload_rwlock); -} - -/* ct_offload_open() - activate the named provider for use. +/* ct_offload_open() - open the named provider for use. * * Called by the dpif-offload provider that owns the ports and hardware * resources when it needs conntrack offload from the named provider. - * Opens are refcounted; class->open() runs on the first open. Returns 0 - * on success or a positive errno value on failure. */ + * + * A dpif-offload class can serve multiple datapaths. Each dpif-offload + * instance could open the same CT offload class with its own context, + * sharing callbacks while using separate resources. Currently, only + * dpif-netdev uses the CT offload infrastructure. To keep the initial + * implementation simple, we support one open instance per CT offload + * class and store its context directly in the registry entry. */ int -ct_offload_open(const char *name) +ct_offload_open(const char *name, void *instance_ctx) { struct ct_offload_class_node *node; int error = 0; + ovs_assert(name); + + if (!instance_ctx) { + return EINVAL; + } + ovs_rwlock_wrlock(&ct_offload_rwlock); node = ct_offload_class_find__(name); if (!node) { @@ -200,15 +176,18 @@ ct_offload_open(const char *name) goto out; } - if (node->refcnt == 0 && node->class->open) { - error = node->class->open(); - if (error) { - VLOG_WARN("failed to open ct offload class %s: %s", - name, ovs_strerror(error)); - goto out; - } + if (node->instance_ctx) { + error = EBUSY; + goto out; + } + + error = node->class->open ? node->class->open(instance_ctx) : 0; + if (error) { + VLOG_WARN("failed to open ct offload class %s: %s", + name, ovs_strerror(error)); + goto out; } - node->refcnt++; + node->instance_ctx = instance_ctx; VLOG_DBG("opened ct offload class: %s", name); out: @@ -216,25 +195,30 @@ out: return error; } -/* ct_offload_close() - release a previously opened provider. +/* ct_offload_close() - close a previously opened provider. * - * Paired with an earlier successful ct_offload_open(). class->close() - * runs when the last opener releases the provider. */ + * Should be paired with an earlier successful ct_offload_open() with the same + * instance context. */ void -ct_offload_close(const char *name) +ct_offload_close(const char *name, void *instance_ctx) { struct ct_offload_class_node *node; + ovs_assert(name); + ovs_assert(instance_ctx); + ovs_rwlock_wrlock(&ct_offload_rwlock); node = ct_offload_class_find__(name); - if (!node || node->refcnt == 0) { + if (!node || node->instance_ctx != instance_ctx) { VLOG_WARN("attempted to close non-open ct offload class: %s", name); goto out; } - if (--node->refcnt == 0 && node->class->close) { - node->class->close(); + if (node->class->close) { + node->class->close(instance_ctx); } + + node->instance_ctx = NULL; VLOG_DBG("closed ct offload class: %s", name); out: @@ -251,8 +235,6 @@ ct_offload_module_init(void) { size_t n_classes = ARRAY_SIZE(base_ct_offload_classes); - ct_offload_dummy_init(); - for (size_t i = 0; i < n_classes; i++) { const struct ct_offload_class *class = base_ct_offload_classes[i]; @@ -307,8 +289,9 @@ ct_offload_op_batch_add(struct ct_offload_op_batch *batch, op->type = type; op->ctx = *ctx; - op->error = 0; + op->error = EOPNOTSUPP; op->last_used = 0; + op->hw_confirmed = false; } /* Marks the connection hw-confirmed. last_extend_ts is stored before @@ -398,19 +381,22 @@ ct_offload_op_batch_submit(struct ct_offload_op_batch *batch) { struct ct_offload_class_node *node; + // Moved all of this to the add operation to save one iteration loop. /* EOPNOTSUPP = "no provider claimed this op". */ + /* for (size_t i = 0; i < batch->n_ops; i++) { batch->ops[i].error = EOPNOTSUPP; batch->ops[i].last_used = 0; batch->ops[i].hw_confirmed = false; } + */ ovs_rwlock_rdlock(&ct_offload_rwlock); LIST_FOR_EACH (node, list_node, &ct_offload_classes) { - if (node->refcnt == 0) { + if (!node->instance_ctx) { continue; } - node->class->batch_submit(batch); + node->class->batch_submit(node->instance_ctx, batch); } ovs_rwlock_unlock(&ct_offload_rwlock); @@ -422,7 +408,11 @@ ct_offload_op_batch_submit(struct ct_offload_op_batch *batch) void ct_offload_conn_add(const struct ct_offload_ctx *ctx) { - struct ct_offload_op op = { .type = CT_OFFLOAD_OP_ADD, .ctx = *ctx }; + struct ct_offload_op op = { + .type = CT_OFFLOAD_OP_ADD, + .ctx = *ctx, + .error = EOPNOTSUPP, + }; struct ct_offload_op_batch batch = { .ops = &op, .n_ops = 1, .allocated = 1 }; @@ -448,7 +438,11 @@ ct_offload_conn_del(const struct ct_offload_ctx *ctx) return; } - struct ct_offload_op op = { .type = CT_OFFLOAD_OP_DEL, .ctx = *ctx }; + struct ct_offload_op op = { + .type = CT_OFFLOAD_OP_DEL, + .ctx = *ctx, + .error = EOPNOTSUPP, + }; struct ct_offload_op_batch batch = { .ops = &op, .n_ops = 1, .allocated = 1 }; @@ -459,7 +453,11 @@ ct_offload_conn_del(const struct ct_offload_ctx *ctx) long long ct_offload_conn_update(const struct ct_offload_ctx *ctx) { - struct ct_offload_op op = { .type = CT_OFFLOAD_OP_UPD, .ctx = *ctx }; + struct ct_offload_op op = { + .type = CT_OFFLOAD_OP_UPD, + .ctx = *ctx, + .error = EOPNOTSUPP, + }; struct ct_offload_op_batch batch = { .ops = &op, .n_ops = 1, .allocated = 1 }; @@ -487,7 +485,9 @@ ct_offload_conn_notify_established(const struct ct_offload_ctx *ctx) } struct ct_offload_op op = { - .type = CT_OFFLOAD_OP_ESTABLISHED, .ctx = *ctx + .type = CT_OFFLOAD_OP_ESTABLISHED, + .ctx = *ctx, + .error = EOPNOTSUPP, }; struct ct_offload_op_batch batch = { .ops = &op, .n_ops = 1, .allocated = 1 @@ -496,13 +496,17 @@ ct_offload_conn_notify_established(const struct ct_offload_ctx *ctx) ct_offload_op_batch_submit(&batch); } -/* ct_offload_flow_offloadable() - may a flow containing the ct() - * action 'ct_action' with flow key 'match' on 'netdev_in' be fully - * offloaded? Returns true if any open provider claims it. */ -bool -ct_offload_flow_offloadable(struct netdev *netdev_in, - const struct match *match, - const struct nlattr *ct_action) +/* Checks whether an active provider potentially supports offloading the + * CT requirements described by 'match' and 'ct_action' on 'netdev_in'. + * Returns true if any active provider reports support, false otherwise. + * + * Called during flow installation; no connection object is supplied. + * A true result does not guarantee that the complete flow or a specific + * connection can be offloaded. The dpif-offload provider must validate + * the remaining actions; connection offload is handled separately. */ +bool ct_offload_flow_might_offload(struct netdev *netdev_in, + const struct match *match, + const struct nlattr *ct_action) { struct ct_offload_flow_ctx ctx = { .netdev_in = netdev_in, @@ -514,10 +518,10 @@ ct_offload_flow_offloadable(struct netdev *netdev_in, ovs_rwlock_rdlock(&ct_offload_rwlock); LIST_FOR_EACH (node, list_node, &ct_offload_classes) { - if (node->refcnt == 0) { + if (!node->instance_ctx) { continue; } - if (node->class->flow_might_offload(&ctx)) { + if (node->class->flow_might_offload(node->instance_ctx, &ctx)) { result = true; break; } diff --git a/lib/ct-offload.h b/lib/ct-offload.h index 3dce6585e..860a1252b 100644 --- a/lib/ct-offload.h +++ b/lib/ct-offload.h @@ -47,49 +47,55 @@ struct ct_offload_ctx { * packet that triggered the op. */ }; -/* Operations delivered to a provider's batch_submit() callback. Each op - * carries its own 'ctx'; the fields each type uses are noted below. The - * provider fills in the op result fields ('error', 'last_used', - * 'hw_confirmed'). At most one provider handles a given connection -- - * the one whose ports the connection flows through; every other - * provider leaves the result fields untouched: +/* Operations passed to a provider's batch_submit() callback. Each + * operation carries its own 'ctx'. The descriptions below list the + * context fields it uses and the result fields the provider may set. + * + * Before dispatch, the framework sets op->error to EOPNOTSUPP to mark + * each operation as unclaimed. Unless noted below, a provider must + * skip an operation if op->error is no longer EOPNOTSUPP. A provider + * that skips or declines an operation leaves all result fields untouched. + * + * A provider that claims an operation sets op->error to 0 on success + * or a positive errno other than EOPNOTSUPP on failure. EOPNOTSUPP is + * reserved for unclaimed operations. * * CT_OFFLOAD_OP_ADD (uses conn, netdev_in, input_port_id, dir): - * Sets op->error to 0 if accepted, another positive errno on - * failure, and op->hw_confirmed if the entry is fully programmed - * at add time; leave the result fields untouched when not - * handling the connection. + * Handle this operation only if the provider serves the port and + * can take responsibility for the connection. Set op->error to + * 0 if the ADD is accepted, even if hardware programming is still + * pending. Set op->hw_confirmed only if the entry is already + * fully programmed. If the provider claims the ADD but fails to + * accept it, report an error as described above. * * CT_OFFLOAD_OP_DEL (uses conn, dir): - * Removes any offloaded state; providers must tolerate unknown - * connections. Sets op->error to 0. + * Handle this operation only if the provider accepted the ADD for + * the connection. Remove any remaining state and set op->error + * to 0 on success, even if the hardware entry is already gone. + * Leave the result fields untouched if the provider does not + * recognize the connection. * * CT_OFFLOAD_OP_UPD (uses conn, dir): - * Sets op->last_used to the hardware last-used time in ms since - * epoch (0 if none), and op->error to 0, or ENOENT if the - * provider previously accepted the connection and has since - * evicted it -- the framework then demotes it to software-only - * tracking. A provider that never accepted the connection must - * leave the result fields untouched. May set - * op->hw_confirmed if the provider noticed the connection active - * on its own. Leave the result fields untouched when not - * holding the connection. + * Handle this operation only if the provider accepted the ADD for + * the connection. On success, set op->error to 0 and report + * op->last_used in the same monotonic millisecond time base as + * time_msec(), or 0 if no timestamp is available. Set + * op->hw_confirmed only if all required hardware state is in + * place. Report ENOENT only if the provider knows the hardware + * entry was evicted. * - * CT_OFFLOAD_OP_ESTABLISHED (uses conn, input_port_id, dir): - * Software observed the connection transition to established - * (reply-direction packet); carries the reverse-direction input - * port. A signal only: providers may act on it or ignore it; - * it requests nothing. A provider confirming an entry in - * hardware does so via op->hw_confirmed, which it may set on any - * op it handles. op->error may report a failure acting on the - * signal (diagnostics only). Leave the result fields untouched - * otherwise. */ + * CT_OFFLOAD_OP_ESTABLISHED (uses conn, netdev_in, input_port_id, dir): + * Software observed reply traffic. The provider may act on this + * notification only if it accepted the ADD for the connection. + * If it acts on the notification, set op->error as described above + * and set op->hw_confirmed only if all required hardware state + * is in place. The notification may be skipped if the entry is + * already hardware-confirmed. */ enum ct_offload_op_type { - CT_OFFLOAD_OP_ADD, /* Add operation. */ - CT_OFFLOAD_OP_DEL, /* Del operation. */ - CT_OFFLOAD_OP_UPD, /* Update (timestamp) operation. */ - CT_OFFLOAD_OP_ESTABLISHED, /* SW observed the established - * transition. */ + CT_OFFLOAD_OP_ADD, + CT_OFFLOAD_OP_DEL, + CT_OFFLOAD_OP_UPD, + CT_OFFLOAD_OP_ESTABLISHED, }; /* A single offload operation. The 'ctx' is embedded by value, but no @@ -153,54 +159,72 @@ void ct_offload_op_batch_submit(struct ct_offload_op_batch *); /* CT offload class describes a conntrack offload provider implementation. * - * Providers register globally at module init and become active when - * opened through ct_offload_open() (mirroring the init/open/close split - * of struct dpif_offload_class). Opens are refcounted; close() runs - * when the last user releases it. */ + * Providers register globally at module init and become active when opened + * through ct_offload_open() (mirroring the init/open/close split of struct + * dpif_offload_class). For now each class supports one active opener. + * Except for init(), all callbacks receive the context supplied at open. */ struct ct_offload_class { /* Unique provider name, e.g. "dummy". */ const char *name; - /* Optional; called at registration. Per-port or per-instance - * hardware resources belong in open(). Returns 0 or a positive - * errno to abort registration. */ + /* Called at registration. Per-port or per-instance hardware + * resources belong in open(). Returns 0 on success or a positive + * errno value to abort registration. This callback is optional. */ int (*init)(void); - /* Called by ct_offload_open() on the first open. Returns 0 or - * errno. May be NULL. */ - int (*open)(void); + /* Called by ct_offload_open() with the instance context. Returns 0 on + * success or a positive errno value on failure. This callback is + * optional. */ + int (*open)(void *instance_ctx); - /* Called by ct_offload_close() on the last release; must undo - * open(). May be NULL if open() is. */ - void (*close)(void); + /* Called by ct_offload_close() with the context from the matching + * successful ct_offload_open() to release provider resources. This + * callback is required if open() is implemented; otherwise, it is + * optional. */ + void (*close)(void *instance_ctx); /* Processes the ops in 'batch'; the only per-connection entry * point. Fill in each op's result fields per enum * ct_offload_op_type; ops in a batch are independent. Called with * only ct_offload_rwlock (read side) held -- no conn->lock -- and * must not block for extended periods; take conn->lock yourself to - * access conn state. Mandatory. */ - void (*batch_submit)(const struct ct_offload_op_batch *batch); - - /* May a flow containing the ct() action 'ct_action' with flow key - * 'match' on this input netdev be fully offloaded? Called at - * flow-put time through ct_offload_flow_offloadable(); there is no - * connection yet. Providers parse only the attributes they care - * about (commit, NAT configuration, protocol, helper, zone, ...). - * Mandatory. */ - bool (*flow_might_offload)(const struct ct_offload_flow_ctx *); + * access conn state. This callback is mandatory. */ + void (*batch_submit)(void *instance_ctx, + const struct ct_offload_op_batch *batch); + + /* Checks whether this provider potentially supports offloading the + * CT requirements described by flow_ctx->match and flow_ctx->ct_action + * on flow_ctx->netdev_in. + * + * Returns true if the supplied CT requirements are supported. + * Returns false if they are unsupported or the supplied information + * is insufficient to establish support. + * + * Called during flow installation; no connection object is supplied. + * Returning true permits further offload consideration; it neither + * programs hardware nor guarantees that the complete flow or a + * specific connection can be offloaded. + * + * This callback is mandatory. */ + bool (*flow_might_offload)(void *instance_ctx, + const struct ct_offload_flow_ctx *flow_ctx); }; -/* Register/unregister a provider class. Must be called at module init, - * before any connections are created; registration alone does not activate - * the provider. batch_submit and flow_might_offload must be non-NULL. */ +/* Register a CT offload provider class during module initialization, + * before any conntrack connections are created. The provider becomes + * active only after a successful ct_offload_open() call. */ int ct_offload_register(const struct ct_offload_class *); -void ct_offload_unregister(const struct ct_offload_class *); -/* Activate/deactivate a provider by name. Refcounted; pair close() - * with a successful open(). Returns 0 on success or a positive errno. */ -int ct_offload_open(const char *name); -void ct_offload_close(const char *name); +/* Open the named provider for use with a non-NULL 'instance_ctx'. The pair + * ('name', 'instance_ctx') identifies the open instance. Returns 0 on + * success, EINVAL if 'instance_ctx' is NULL, EBUSY if the named provider + * is already open, or another positive errno on failure. Keep + * 'instance_ctx' valid until the matching ct_offload_close() returns. */ +int ct_offload_open(const char *name, void *instance_ctx); + +/* Close the named provider. The caller must pass the same 'name' and + * 'instance_ctx' as in the matching successful ct_offload_open(). */ +void ct_offload_close(const char *name, void *instance_ctx); /* Module initialization (register built-in providers). Must run before * any connections are created, as it allocates the per-connection private @@ -249,12 +273,16 @@ long long ct_offload_conn_update(const struct ct_offload_ctx *); * packet); a signal only. */ void ct_offload_conn_notify_established(const struct ct_offload_ctx *); -/* May a flow containing the ct() action 'ct_action' with flow key - * 'match' on 'netdev_in' be fully offloaded? Returns true if any open - * provider claims it. Called by dpif offload providers at flow-put - * time; there is no connection yet. */ -bool ct_offload_flow_offloadable(struct netdev *netdev_in, - const struct match *match, - const struct nlattr *ct_action); +/* Checks whether an active provider potentially supports offloading the + * CT requirements described by 'match' and 'ct_action' on 'netdev_in'. + * Returns true if any active provider reports support, false otherwise. + * + * Called during flow installation; no connection object is supplied. + * A true result does not guarantee that the complete flow or a specific + * connection can be offloaded. The dpif-offload provider must validate + * the remaining actions; connection offload is handled separately. */ +bool ct_offload_flow_might_offload(struct netdev *, + const struct match *, + const struct nlattr *ct_action); #endif /* CT_OFFLOAD_H */ diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 7ff97034d..6b61dee6a 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -8257,6 +8257,7 @@ dp_execute_cb(void *aux_, struct dp_packet_batch *packets_, const struct ovs_key_ct_labels *setlabel = NULL; struct nat_action_info_t nat_action_info; struct nat_action_info_t *nat_action_info_ref = NULL; + struct netdev *in_netdev = NULL; bool nat_config = false; NL_ATTR_FOR_EACH_UNSAFE (b, left, nl_attr_get(a), @@ -8382,17 +8383,18 @@ dp_execute_cb(void *aux_, struct dp_packet_batch *packets_, * output action: a direct dp_netdev_lookup_port() would race * hmap_expand() on port addition. NULL when unknown to this * PMD; offload providers tolerate it. */ - struct netdev *in_netdev = NULL; if (!dp_packet_batch_is_empty(packets_)) { - odp_port_t query_port = - packets_->packets[0]->md.orig_in_port; - struct tx_port *tx = - pmd_send_port_cache_lookup(pmd, query_port); - if (!tx) { - tx = pmd_tnl_port_cache_lookup(pmd, query_port); + struct tx_port *tx_port; + odp_port_t query_port; + + query_port = packets_->packets[0]->md.orig_in_port; + tx_port = pmd_send_port_cache_lookup(pmd, query_port); + if (!tx_port) { + tx_port = pmd_tnl_port_cache_lookup(pmd, query_port); } - if (tx) { - in_netdev = tx->port->netdev; + + if (tx_port) { + in_netdev = tx_port->port->netdev; } } diff --git a/lib/dpif-offload-dummy-private.h b/lib/dpif-offload-dummy-private.h new file mode 100755 index 000000000..c0686f204 --- /dev/null +++ b/lib/dpif-offload-dummy-private.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2026 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DPIF_OFFLOAD_DUMMY_PRIVATE_H +#define DPIF_OFFLOAD_DUMMY_PRIVATE_H + +#include <stdbool.h> + +struct dummy_offload; +struct netdev; + +bool dpif_offload_dummy_owns_netdev(const struct dummy_offload *, + const struct netdev *); + +#endif /* DPIF_OFFLOAD_DUMMY_PRIVATE_H */ diff --git a/lib/dpif-offload-dummy.c b/lib/dpif-offload-dummy.c index 53ea71113..862ea1955 100644 --- a/lib/dpif-offload-dummy.c +++ b/lib/dpif-offload-dummy.c @@ -17,10 +17,11 @@ #include <config.h> #include <errno.h> -#include "dpif.h" #include "ct-offload.h" #include "ct-offload-dummy.h" +#include "dpif.h" #include "dpif-offload.h" +#include "dpif-offload-dummy-private.h" #include "dpif-offload-provider.h" #include "dummy.h" #include "id-fpool.h" @@ -59,6 +60,7 @@ struct dummy_offload { struct dpif_offload offload; struct id_fpool *flow_mark_pool; dpif_offload_flow_unreference_cb *unreference_cb; + bool ct_opened; /* Configuration specific variables. */ struct ovsthread_once once_enable; /* Track first-time enablement. */ @@ -126,6 +128,18 @@ dummy_offload_cast(const struct dpif_offload *offload) return CONTAINER_OF(offload, struct dummy_offload, offload); } +bool +dpif_offload_dummy_owns_netdev(const struct dummy_offload *offload, + const struct netdev *netdev) +{ + if (!netdev || !offload) { + return false; + } + + return netdev && &offload->offload == ovsrcu_get( + const struct dpif_offload *, &netdev->dpif_offload); +} + static uint32_t dummy_flow_hash(const ovs_u128 *ufid) { @@ -423,16 +437,9 @@ dummy_offload_open(const struct dpif_offload_class *offload_class, offload->once_enable = (struct ovsthread_once) OVSTHREAD_ONCE_INITIALIZER; offload->flow_mark_pool = NULL; offload->unreference_cb = NULL; + offload->ct_opened = false; *dpif_offload = &offload->offload; - - /* Activate the matching CT offload provider. Failure is not fatal: - * flow offload still works, only conntrack offload stays inactive. */ - int error = ct_offload_open(ct_offload_dummy_class.name); - if (error) { - VLOG_WARN("failed to open ct-offload provider %s: %s", - ct_offload_dummy_class.name, ovs_strerror(error)); - } return 0; } @@ -442,7 +449,9 @@ dummy_offload_close(struct dpif_offload *dpif_offload) struct dummy_offload *offload = dummy_offload_cast(dpif_offload); struct dpif_offload_port *port; - ct_offload_close(ct_offload_dummy_class.name); + if (offload->ct_opened) { + ct_offload_close(ct_offload_dummy_class.name, offload); + } /* The ofproto layer may not call dpif_port_del() for all ports, * especially internal ones, so we need to clean up any remaining ports. */ @@ -468,6 +477,21 @@ dummy_offload_set_config(struct dpif_offload *dpif_offload, if (ovsthread_once_start(&offload->once_enable)) { struct dpif_offload_port *port; + /* Only open CT offload for the dummy provider, not dummy_x. */ + if (!strcmp(dpif_offload_type(dpif_offload), + ct_offload_dummy_class.name)) { + int error; + + error = ct_offload_open(ct_offload_dummy_class.name, offload); + if (!error) { + offload->ct_opened = true; + } else { + VLOG_WARN("failed to open ct-offload provider %s: %s", + ct_offload_dummy_class.name, + ovs_strerror(error)); + } + } + DPIF_OFFLOAD_PORT_FOR_EACH (port, dpif_offload) { dummy_offload_enable(dpif_offload, port); } @@ -705,7 +729,7 @@ dummy_offload_are_all_actions_supported(const struct dpif_offload *offload_, dummy_offload_get_port_by_odp_port(offload_, in_odp); struct netdev *netdev = ct_port ? ct_port->pm_port.netdev : NULL; - if (!ct_offload_flow_offloadable(netdev, match, nla)) { + if (!ct_offload_flow_might_offload(netdev, match, nla)) { return false; } break; diff --git a/lib/dpif-offload.c b/lib/dpif-offload.c index a040e6053..c95bd5c10 100644 --- a/lib/dpif-offload.c +++ b/lib/dpif-offload.c @@ -17,8 +17,8 @@ #include <config.h> #include <errno.h> -#include "dpif-offload.h" #include "ct-offload.h" +#include "dpif-offload.h" #include "dpif-offload-provider.h" #include "dpif-provider.h" #include "netdev-provider.h" diff --git a/utilities/checkpatch_dict.txt b/utilities/checkpatch_dict.txt index f4ff3ccdf..6212ffee4 100644 --- a/utilities/checkpatch_dict.txt +++ b/utilities/checkpatch_dict.txt @@ -72,6 +72,7 @@ enums eol epasv epsv +errno erspan ether ethernet _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
