Add a private data slot allocated from the connection's private store
to track offload state per connection.  Three states are defined:
none, added, and hw-confirmed.  The framework updates the state after
each submitted batch.

Two public helpers, ct_offload_conn_is_tracked() and
ct_offload_conn_is_hw_confirmed(), expose the current state.

A hw-confirmed connection means a provider acknowledged the entry
in hardware; software notifies providers of the established
transition, but providers may confirm independently via
op->hw_confirmed.

Signed-off-by: Aaron Conole <[email protected]>
---
 lib/conntrack-tcp.c |  13 ++++-
 lib/ct-offload.c    | 134 +++++++++++++++++++++++++++++++++++++++++++-
 lib/ct-offload.h    |  39 +++++++++----
 3 files changed, 169 insertions(+), 17 deletions(-)

diff --git a/lib/conntrack-tcp.c b/lib/conntrack-tcp.c
index 5c6485fdc0..4788b5a4e6 100644
--- a/lib/conntrack-tcp.c
+++ b/lib/conntrack-tcp.c
@@ -44,6 +44,7 @@
 #include "conntrack-tp.h"
 #include "coverage.h"
 #include "ct-dpif.h"
+#include "ct-offload.h"
 #include "dp-packet.h"
 #include "util.h"
 
@@ -133,9 +134,15 @@ tcp_get_wscale(const struct tcp_header *tcp)
 }
 
 static bool
-tcp_bypass_seq_chk(struct conntrack *ct)
+tcp_bypass_seq_chk(struct conntrack *ct, struct conn *conn)
+    OVS_REQUIRES(conn->lock)
 {
-    if (!conntrack_get_tcp_seq_chk(ct)) {
+    /* While hardware-confirmed, not every segment is visible to
+     * software, so validation is skipped.  Segments that do reach
+     * software still update the baselines, so validation resumes
+     * correctly after demotion. */
+    if (!conntrack_get_tcp_seq_chk(ct) ||
+        ct_offload_conn_is_hw_confirmed(conn)) {
         COVERAGE_INC(conntrack_tcp_seq_chk_bypass);
         return true;
     }
@@ -291,7 +298,7 @@ tcp_conn_update(struct conntrack *ct, struct conn *conn_,
         /* Acking not more than one window forward */
         && ((tcp_flags & TCP_RST) == 0 || orig_seq == src->seqlo
             || (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo)))
-        || tcp_bypass_seq_chk(ct)) {
+        || tcp_bypass_seq_chk(ct, conn_)) {
         /* Require an exact/+1 sequence match on resets when possible */
 
         /* update max window */
diff --git a/lib/ct-offload.c b/lib/ct-offload.c
index 111bf37f06..ca682f329f 100644
--- a/lib/ct-offload.c
+++ b/lib/ct-offload.c
@@ -20,7 +20,9 @@
 
 #include <errno.h>
 
+#include "conntrack-private.h"
 #include "ovs-thread.h"
+#include "timeval.h"
 #include "util.h"
 
 #include "openvswitch/list.h"
@@ -28,6 +30,28 @@
 
 VLOG_DEFINE_THIS_MODULE(ct_offload);
 
+/* Private slot for storing per-connection offload state.  Allocated when
+ * the first provider class is registered, which must happen before any
+ * connections are created. */
+static ct_private_id_t ct_offload_private_id = CT_PRIVATE_ID_INVALID;
+
+enum ct_offload_state {
+    CT_OFFLOAD_STATE_NONE,          /* Not offloaded. */
+    CT_OFFLOAD_STATE_ADDED,         /* Offload pending. */
+    CT_OFFLOAD_STATE_HW_CONFIRMED   /* Provider acknowledged the entry. */
+};
+
+/* Per-connection offload data stored in the private slot; NULL (the
+ * initial slot value) means never offloaded.  All members are written
+ * under conn->lock; last_extend_ts is written at hw-confirmation time
+ * (by PMD threads) and by the expiration sweeper, both under
+ * conn->lock. */
+struct ct_offload_data {
+    enum ct_offload_state state;
+    long long last_extend_ts;       /* Last hardware timestamp used to
+                                     * extend the connection's expiration. */
+};
+
 /* Node in the registered-provider list. */
 struct ct_offload_class_node {
     const struct ct_offload_class *class;
@@ -90,6 +114,11 @@ ct_offload_register(const struct ct_offload_class *class)
         goto out;
     }
 
+    /* Must be allocated before any connections exist. */
+    if (ct_offload_private_id == CT_PRIVATE_ID_INVALID) {
+        ct_offload_private_id = conn_private_id_alloc(free);
+    }
+
     error = class->init ? class->init() : 0;
     if (error) {
         VLOG_WARN("failed to initialize ct offload class %s: %s",
@@ -243,11 +272,75 @@ ct_offload_op_batch_add(struct ct_offload_op_batch *batch,
     op->last_used = 0;
 }
 
+/* Marks the connection hw-confirmed.  last_extend_ts is stored before
+ * the state so that observing HW_CONFIRMED implies it is set. */
+static void
+ct_offload_data_hw_confirm(struct ct_offload_data *data)
+{
+    if (data->state != CT_OFFLOAD_STATE_HW_CONFIRMED) {
+        data->last_extend_ts = time_msec();
+        data->state = CT_OFFLOAD_STATE_HW_CONFIRMED;
+    }
+}
+
+/* Updates the per-connection offload state from the results of 'op'.
+ * Called with ct_offload_rwlock released, taking conn->lock by itself. */
+static void
+ct_offload_op_mark(struct ct_offload_op *op)
+{
+    struct conn *conn = op->ctx.conn;
+    struct ct_offload_data *data;
+
+    if (ct_offload_private_id == CT_PRIVATE_ID_INVALID) {
+        return;
+    }
+
+    ovs_mutex_lock(&conn->lock);
+    data = conn_private_get(conn, ct_offload_private_id);
+
+    switch (op->type) {
+    case CT_OFFLOAD_OP_ADD:
+        if (!op->error && !data) {
+            data = xzalloc(sizeof *data);
+            data->state = CT_OFFLOAD_STATE_ADDED;
+            conn_private_set(conn, ct_offload_private_id, data);
+        }
+        if (data && op->hw_confirmed) {
+            ct_offload_data_hw_confirm(data);
+        }
+        break;
+    case CT_OFFLOAD_OP_ESTABLISHED:
+    case CT_OFFLOAD_OP_UPD:
+        if (data && op->hw_confirmed) {
+            ct_offload_data_hw_confirm(data);
+        }
+        break;
+    case CT_OFFLOAD_OP_DEL:
+        if (op->error && op->error != ENOENT && op->error != EOPNOTSUPP) {
+            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10);
+
+            VLOG_WARN_RL(&rl, "ct offload provider failed to delete "
+                         "connection: %s", ovs_strerror(op->error));
+        }
+        conn_private_set(conn, ct_offload_private_id, NULL);
+        /* RCU-deferred: readers of the private slot hold conn->lock, so
+         * the postponement is belt-and-suspenders. */
+        ovsrcu_postpone(free, data);
+        break;
+    }
+    ovs_mutex_unlock(&conn->lock);
+}
+
 /* ct_offload_op_batch_submit() - submit 'batch' to every open provider.
  *
  * Result fields are pre-filled (EOPNOTSUPP = "no provider claimed this
  * op"); providers must leave them untouched when not handling a
- * connection (see ct-offload.h). */
+ * connection (see ct-offload.h).  After the providers run, the
+ * per-connection state is updated from the op results, with
+ * ct_offload_rwlock released to keep the lock order strict:
+ * ct_offload_rwlock always nests outside conn->lock, so code must
+ * never call into ct-offload with conn->lock held.  Providers needing
+ * conn state inside batch_submit() take conn->lock themselves. */
 void
 ct_offload_op_batch_submit(struct ct_offload_op_batch *batch)
 {
@@ -257,6 +350,7 @@ ct_offload_op_batch_submit(struct ct_offload_op_batch 
*batch)
     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);
@@ -267,6 +361,10 @@ ct_offload_op_batch_submit(struct ct_offload_op_batch 
*batch)
         node->class->batch_submit(batch);
     }
     ovs_rwlock_unlock(&ct_offload_rwlock);
+
+    for (size_t i = 0; i < batch->n_ops; i++) {
+        ct_offload_op_mark(&batch->ops[i]);
+    }
 }
 
 void
@@ -310,7 +408,8 @@ ct_offload_conn_update(const struct ct_offload_ctx *ctx)
 }
 
 /* Notifies providers that software observed the connection transition
- * to established (reply-direction packet).  A signal only. */
+ * to established (reply-direction packet).  A signal only; providers
+ * confirm via op->hw_confirmed. */
 void
 ct_offload_conn_notify_established(const struct ct_offload_ctx *ctx)
 {
@@ -354,3 +453,34 @@ ct_offload_flow_offloadable(struct netdev *netdev_in,
 
     return result;
 }
+
+/* ct_offload_conn_is_tracked() - returns true if a provider accepted the
+ * connection (state ADDED or HW_CONFIRMED).  Note that this does not
+ * mean hardware has confirmed the connection; use
+ * ct_offload_conn_is_hw_confirmed() for that. */
+bool
+ct_offload_conn_is_tracked(const struct conn *conn)
+    OVS_REQUIRES(conn->lock)
+{
+    if (ct_offload_private_id == CT_PRIVATE_ID_INVALID) {
+        return false;
+    }
+
+    return conn_private_get(conn, ct_offload_private_id) != NULL;
+}
+
+/* ct_offload_conn_is_hw_confirmed() - returns true if a provider
+ * acknowledged the entry in hardware (state HW_CONFIRMED). */
+bool
+ct_offload_conn_is_hw_confirmed(const struct conn *conn)
+    OVS_REQUIRES(conn->lock)
+{
+    if (ct_offload_private_id == CT_PRIVATE_ID_INVALID) {
+        return false;
+    }
+
+    struct ct_offload_data *data =
+        conn_private_get(conn, ct_offload_private_id);
+
+    return data && data->state == CT_OFFLOAD_STATE_HW_CONFIRMED;
+}
diff --git a/lib/ct-offload.h b/lib/ct-offload.h
index 7b35b35cef..1157b7edfe 100644
--- a/lib/ct-offload.h
+++ b/lib/ct-offload.h
@@ -49,15 +49,16 @@ struct ct_offload_ctx {
 
 /* 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').
- * At most one provider handles a given connection -- the one whose
- * ports the connection flows through; every other provider leaves
- * the result fields untouched:
+ * 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:
  *
  *   CT_OFFLOAD_OP_ADD (uses conn, netdev_in, input_port_id, dir):
  *       Sets op->error to 0 if accepted, another positive errno on
- *       failure; leave the result fields untouched when not handling
- *       the connection.
+ *       failure, and op->hw_confirmed if the entry is fully programmed
+ *       at add time; leave the result fields untouched when not
+ *       handling the connection.
  *
  *   CT_OFFLOAD_OP_DEL (uses conn, dir):
  *       Removes any offloaded state; providers must tolerate unknown
@@ -65,16 +66,20 @@ struct ct_offload_ctx {
  *
  *   CT_OFFLOAD_OP_UPD (uses conn, dir):
  *       Sets op->last_used to the hardware last-used time in ms since
- *       epoch (0 if none).  Sets op->error to 0.  Leave the result
- *       fields untouched when not holding the connection.
+ *       epoch (0 if none).  Sets op->error to 0.  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.
  *
  *   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.  op->error may report a failure acting
- *       on the signal (diagnostics only).  Leave the result fields
- *       untouched otherwise. */
+ *       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. */
 enum ct_offload_op_type {
     CT_OFFLOAD_OP_ADD,              /* Add operation. */
     CT_OFFLOAD_OP_DEL,              /* Del operation. */
@@ -93,6 +98,8 @@ struct ct_offload_op {
     /* Result fields, filled in by the provider as documented above. */
     int error;
     long long last_used;
+    bool hw_confirmed;              /* Provider acknowledged the entry
+                                     * in hardware. */
 };
 
 /* Ops embedded directly in struct ct_offload_op_batch; larger batches
@@ -191,9 +198,17 @@ void ct_offload_unregister(const struct ct_offload_class 
*);
 int  ct_offload_open(const char *name);
 void ct_offload_close(const char *name);
 
-/* Module initialization (register built-in providers). */
+/* Module initialization (register built-in providers).  Must run before
+ * any connections are created, as it allocates the per-connection private
+ * slot used to track offload state. */
 void ct_offload_module_init(void);
 
+/* Offload state of a connection, tracked in a conn-private slot:
+ * tracked (a provider accepted the ADD op) or hw-confirmed (a provider
+ * acknowledged the entry in hardware).  Both require conn->lock. */
+bool ct_offload_conn_is_tracked(const struct conn *);
+bool ct_offload_conn_is_hw_confirmed(const struct conn *);
+
 /* Single-operation convenience wrappers around ct_offload_op_batch_*(),
  * each submitting a batch of one op to every open provider.  Callers
  * must not hold conn->lock; see the locking notes in ct-offload.c. */
-- 
2.56.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to