This adds the provider class, registration, and the reference-counted open/close lifecycle that conntrack offload providers build on, along with the flow-level eligibility query used at flow-put time.
The public conntrack.h API surface is widened as well: struct ct_endpoint and struct conn_key move there from conntrack-private.h, joined by enum key_dir, so the provider-facing ct-offload.h can stay free of private internals. Signed-off-by: Aaron Conole <[email protected]> --- lib/automake.mk | 2 + lib/conntrack-private.h | 33 ----- lib/conntrack.h | 37 ++++++ lib/ct-offload.c | 241 ++++++++++++++++++++++++++++++++++ lib/ct-offload.h | 104 +++++++++++++++ utilities/checkpatch_dict.txt | 2 + 6 files changed, 386 insertions(+), 33 deletions(-) create mode 100644 lib/ct-offload.c create mode 100644 lib/ct-offload.h diff --git a/lib/automake.mk b/lib/automake.mk index 8051362354..dc165f4153 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -57,6 +57,8 @@ lib_libopenvswitch_la_SOURCES = \ lib/conntrack-other.c \ lib/conntrack.c \ lib/conntrack.h \ + lib/ct-offload.c \ + lib/ct-offload.h \ lib/cooperative-multitasking.c \ lib/cooperative-multitasking.h \ lib/cooperative-multitasking-private.h \ diff --git a/lib/conntrack-private.h b/lib/conntrack-private.h index 534e00a204..f5bcd5d828 100644 --- a/lib/conntrack-private.h +++ b/lib/conntrack-private.h @@ -33,39 +33,6 @@ #include "unaligned.h" #include "dp-packet.h" -struct ct_endpoint { - union ct_addr addr; - union { - ovs_be16 port; - struct { - ovs_be16 icmp_id; - uint8_t icmp_type; - uint8_t icmp_code; - }; - }; -}; - -/* Verify that there is no padding in struct ct_endpoint, to facilitate - * hashing in ct_endpoint_hash_add(). */ -BUILD_ASSERT_DECL(sizeof(struct ct_endpoint) == sizeof(union ct_addr) + 4); - -enum key_dir { - CT_DIR_FWD = 0, - CT_DIR_REV, - CT_DIRS, -}; - -/* Changes to this structure need to be reflected in conn_key_hash() - * and conn_key_cmp(). */ -struct conn_key { - struct ct_endpoint src; - struct ct_endpoint dst; - - ovs_be16 dl_type; - uint16_t zone; - uint8_t nw_proto; -}; - /* Verify that nw_proto stays uint8_t as it's used to index into l4_protos[] */ BUILD_ASSERT_DECL(MEMBER_SIZEOF(struct conn_key, nw_proto) == sizeof(uint8_t)); diff --git a/lib/conntrack.h b/lib/conntrack.h index 08a9c36d0a..2ba6e303a1 100644 --- a/lib/conntrack.h +++ b/lib/conntrack.h @@ -70,6 +70,43 @@ union ct_addr { struct in6_addr ipv6; }; +/* One endpoint (address + L4 port or ICMP id/type/code) of a connection. */ +struct ct_endpoint { + union ct_addr addr; + union { + ovs_be16 port; + struct { + ovs_be16 icmp_id; + uint8_t icmp_type; + uint8_t icmp_code; + }; + }; +}; + +/* Verify that there is no padding in struct ct_endpoint, to facilitate + * hashing. */ +BUILD_ASSERT_DECL(sizeof(struct ct_endpoint) == sizeof(union ct_addr) + 4); + +/* Connection 5-tuple: forward-direction match key. + * Changes to this structure must be reflected in conn_key_hash() + * and conn_key_cmp(). */ +struct conn_key { + struct ct_endpoint src; + struct ct_endpoint dst; + + ovs_be16 dl_type; + uint16_t zone; + uint8_t nw_proto; +}; + +/* Direction of a connection key: a connection tracks both the + * forward-direction tuple and its reply. */ +enum key_dir { + CT_DIR_FWD = 0, + CT_DIR_REV, + CT_DIRS, +}; + enum nat_action_e { NAT_ACTION_SRC = 1 << 0, NAT_ACTION_SRC_PORT = 1 << 1, diff --git a/lib/ct-offload.c b/lib/ct-offload.c new file mode 100644 index 0000000000..833e9078b1 --- /dev/null +++ b/lib/ct-offload.c @@ -0,0 +1,241 @@ +/* + * 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. + */ + +#include <config.h> + +#include "ct-offload.h" + +#include <errno.h> + +#include "ovs-thread.h" +#include "util.h" + +#include "openvswitch/list.h" +#include "openvswitch/vlog.h" + +VLOG_DEFINE_THIS_MODULE(ct_offload); + +/* 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. */ + 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. */ +static struct ovs_rwlock ct_offload_rwlock = OVS_RWLOCK_INITIALIZER; +static struct ovs_list ct_offload_classes + OVS_GUARDED_BY(ct_offload_rwlock) + = OVS_LIST_INITIALIZER(&ct_offload_classes); + +/* Looks up 'name' in the registered class list. The caller must hold + * ct_offload_rwlock. */ +static struct ct_offload_class_node * +ct_offload_class_find__(const char *name) + OVS_REQUIRES(ct_offload_rwlock) +{ + struct ct_offload_class_node *node; + + LIST_FOR_EACH (node, list_node, &ct_offload_classes) { + if (!strcmp(node->class->name, name)) { + return node; + } + } + return NULL; +} + +/* 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. + * Attempting to register the same class name twice returns EEXIST. */ +int +ct_offload_register(const struct ct_offload_class *class) +{ + struct ct_offload_class_node *node; + int error = 0; + + ovs_assert(class); + ovs_assert(class->name); + ovs_assert(class->flow_might_offload); + + ovs_rwlock_wrlock(&ct_offload_rwlock); + + if (ct_offload_class_find__(class->name)) { + VLOG_WARN("attempted to register duplicate ct offload class: %s", + class->name); + error = EEXIST; + goto out; + } + + error = class->init ? class->init() : 0; + if (error) { + VLOG_WARN("failed to initialize ct offload class %s: %s", + class->name, ovs_strerror(error)); + goto out; + } + + node = xmalloc(sizeof *node); + node->class = class; + node->refcnt = 0; + ovs_list_push_back(&ct_offload_classes, &node->list_node); + VLOG_DBG("registered ct offload class: %s", class->name); + +out: + ovs_rwlock_unlock(&ct_offload_rwlock); + 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. + * + * 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. */ +int +ct_offload_open(const char *name) +{ + struct ct_offload_class_node *node; + int error = 0; + + ovs_rwlock_wrlock(&ct_offload_rwlock); + node = ct_offload_class_find__(name); + if (!node) { + VLOG_INFO("attempted to open unknown ct offload class: %s", name); + error = ENOENT; + 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; + } + } + node->refcnt++; + VLOG_DBG("opened ct offload class: %s", name); + +out: + ovs_rwlock_unlock(&ct_offload_rwlock); + return error; +} + +/* ct_offload_close() - release a previously opened provider. + * + * Paired with an earlier successful ct_offload_open(). class->close() + * runs when the last opener releases the provider. */ +void +ct_offload_close(const char *name) +{ + struct ct_offload_class_node *node; + + ovs_rwlock_wrlock(&ct_offload_rwlock); + node = ct_offload_class_find__(name); + if (!node || node->refcnt == 0) { + VLOG_WARN("attempted to close non-open ct offload class: %s", name); + goto out; + } + + if (--node->refcnt == 0 && node->class->close) { + node->class->close(); + } + VLOG_DBG("closed ct offload class: %s", name); + +out: + ovs_rwlock_unlock(&ct_offload_rwlock); +} + +/* ct_offload_module_init() - register built-in CT offload providers. + * + * Must be called once before any connections are created. */ +void +ct_offload_module_init(void) +{ + /* No built-in providers yet; third parties call ct_offload_register() + * directly from their own module-init routines. */ +} + +/* 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) +{ + struct ct_offload_flow_ctx ctx = { + .netdev_in = netdev_in, + .match = match, + .ct_action = ct_action, + }; + struct ct_offload_class_node *node; + bool result = false; + + ovs_rwlock_rdlock(&ct_offload_rwlock); + LIST_FOR_EACH (node, list_node, &ct_offload_classes) { + if (node->refcnt == 0) { + continue; + } + if (node->class->flow_might_offload(&ctx)) { + result = true; + break; + } + } + ovs_rwlock_unlock(&ct_offload_rwlock); + + return result; +} diff --git a/lib/ct-offload.h b/lib/ct-offload.h new file mode 100644 index 0000000000..c8eddb2f3a --- /dev/null +++ b/lib/ct-offload.h @@ -0,0 +1,104 @@ +/* + * 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 CT_OFFLOAD_H +#define CT_OFFLOAD_H + +#include "conntrack.h" +#include "openvswitch/types.h" + +struct conn; +struct match; +struct nlattr; +struct netdev; + +/* Context for flow-level ct offload eligibility, queried at flow-put + * time (before any connection exists). 'ct_action' holds the nested + * OVS_CT_ATTR_* attributes of the ct() action being installed. */ +struct ct_offload_flow_ctx { + struct netdev *netdev_in; + const struct match *match; + const struct nlattr *ct_action; +}; + +/* Context for offload as part of the callbacks that all connection + * offload APIs receive. The direction identifies which of the + * connection's keys is the packet's match key; providers holding + * conn->lock can derive either direction's key via + * conn->key_node[dir].key. */ +struct ct_offload_ctx { + struct conn *conn; /* Connection object being offloaded. */ + struct netdev *netdev_in; /* Input netdev (may be NULL). */ + odp_port_t input_port_id; /* ODP port number. */ + enum key_dir dir; /* Direction of 'conn' relative to the + * packet that triggered the op. */ +}; + +/* 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. */ +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. */ + 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_close() on the last release; must undo + * open(). May be NULL if open() is. */ + void (*close)(void); + + /* 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 *); +}; + +/* Register/unregister a provider class. Must be called at module init, + * before any connections are created; registration alone does not activate + * the provider. flow_might_offload must be non-NULL. */ +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); + +/* Module initialization (register built-in providers). */ +void ct_offload_module_init(void); + +/* 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); + +#endif /* CT_OFFLOAD_H */ diff --git a/utilities/checkpatch_dict.txt b/utilities/checkpatch_dict.txt index 7fb56a9183..b7313b807d 100644 --- a/utilities/checkpatch_dict.txt +++ b/utilities/checkpatch_dict.txt @@ -250,6 +250,7 @@ rip rss rsslim rst +rwlock rx rxq sbrec @@ -309,6 +310,7 @@ unassociated unclosed unicast unixctl +unregister untrusted upcall upcalls -- 2.56.1 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
