A conntrack flush that matches on anything but a full 5-tuple (zone
with mark, labels or a partial tuple) is done by ct_dpif_flush_tuple():
it dumps the zone and deletes the matching entries one by one.  If one
of these deletes fails, the loop breaks and all the matching entries
that come later in the dump are left in the connection tracker.

The dump is a snapshot.  With the kernel datapath an entry can expire,
or be removed by someone else, after it was dumped and before its
delete is sent.  That delete fails with ENOENT, so a single short-lived
entry is enough to cut the flush short.  The window grows with the size
of the conntrack hash table, because the kernel walks all the buckets
while it fills a dump message.

OVN relies on this path to remove the connections of an ECMP next hop
that went away (match on ct_label).  The entries that survive keep
established connections pointed at the removed next hop.

That this loop should not bail out on the first failure was already
noted when the flush learned to delete SCTP entries [0], but only that
one cause of a failed delete was removed.

An entry that is gone is what the flush wants, so do not stop on
ENOENT and do not report it.  Any other error still ends the flush and
is returned, as before: there is no point in asking the datapath for
more deletes that it cannot do.

Neither datapath can be made to lose an entry between the dump and the
delete at a chosen moment: the userspace connection tracker skips
expired connections in both, and a kernel entry expires when it
expires.  So the test brings a datapath of its own, which dumps a
fixed set of connections and fails the deletes that the test picks.

Also reproduced with the kernel datapath in a separate network
namespace: 60000 entries with a 15 second timeout and 2000 long-lived
ones in one zone, all with the same mark, and 'ovs-dpctl
flush-conntrack zone=N mark=M' started while the short-lived ones
expire.  Without the fix the command fails with "No such file or
directory" and most of the long-lived entries remain (5 runs out of
5).  With the fix all the entries are removed (5 runs out of 5).

[0] https://mail.openvswitch.org/pipermail/ovs-dev/2023-September/407696.html

Fixes: a9ae73b916ba ("ofp, dpif: Allow CT flush based on partial match.")
Assisted-by: Claude Fable 5.2, Claude Code
Signed-off-by: Bekei Park <[email protected]>
---
v3:
 - Added the include of <sys/types.h> to the test, which
   <netinet/in.h> needs in a build with sparse.  The CI build of v2
   failed without it.
v2:
 - Only ENOENT keeps the flush going.  Any other error ends it and is
   returned, as before the patch (v1 carried on after any error and
   logged it).
 - No new log message in the loop.
 - Added a test.
 - The part about NXT_CT_FLUSH losing the error moved to patch 3.
 - Changed the subject: was "ct-dpif: Do not stop a partial match
   flush on a failed delete."

 lib/ct-dpif.c        |   6 +-
 tests/automake.mk    |   1 +
 tests/library.at     |  12 +++
 tests/test-ct-dpif.c | 196 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 214 insertions(+), 1 deletion(-)
 create mode 100644 tests/test-ct-dpif.c

diff --git a/lib/ct-dpif.c b/lib/ct-dpif.c
index 5a836b668..9a51964c2 100644
--- a/lib/ct-dpif.c
+++ b/lib/ct-dpif.c
@@ -330,7 +330,11 @@ ct_dpif_flush_tuple(struct dpif *dpif, const uint16_t 
*zone,
         if (ct_dpif_entry_cmp(&cte, match)) {
             error = dpif->dpif_class->ct_flush(dpif, &cte.zone,
                                                &cte.tuple_orig);
-            if (error) {
+            /* The dump is a snapshot.  A connection that expired or was
+             * removed after it was dumped is already gone, which is what
+             * the flush wants, so ENOENT does not end the flush.  Any other
+             * error does. */
+            if (error && error != ENOENT) {
                 break;
             }
         }
diff --git a/tests/automake.mk b/tests/automake.mk
index 2f3e4f7ef..4fffb0596 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -474,6 +474,7 @@ tests_ovstest_SOURCES = \
        tests/test-conntrack.c \
        tests/test-cooperative-multitasking.c \
        tests/test-csum.c \
+       tests/test-ct-dpif.c \
        tests/test-flows.c \
        tests/test-hash.c \
        tests/test-heap.c \
diff --git a/tests/library.at b/tests/library.at
index 80ebe6ed8..8acd01ec5 100644
--- a/tests/library.at
+++ b/tests/library.at
@@ -296,3 +296,15 @@ AT_CLEANUP
 AT_SETUP([Conntrack Library - FTP ALG parsing])
 AT_CHECK([ovstest test-conntrack ftp-alg-large-payload])
 AT_CLEANUP
+
+AT_SETUP([ct-dpif - partial match flush])
+AT_KEYWORDS([conntrack])
+AT_CHECK([ovstest test-ct-dpif], [0], [dnl
+all deleted: ok
+gone in the middle: ok
+gone at the end: ok
+all gone: ok
+not supported: ok
+gone, then no buffers: ok
+])
+AT_CLEANUP
diff --git a/tests/test-ct-dpif.c b/tests/test-ct-dpif.c
new file mode 100644
index 000000000..c7e0037af
--- /dev/null
+++ b/tests/test-ct-dpif.c
@@ -0,0 +1,196 @@
+/*
+ * Copyright (c) 2026 Bekei Park.
+ *
+ * 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>
+#undef NDEBUG
+#include <errno.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <string.h>
+#include "ct-dpif.h"
+#include "dpif-provider.h"
+#include "openvswitch/ofp-ct.h"
+#include "ovstest.h"
+#include "util.h"
+
+/* A datapath that implements only what a partial match conntrack flush
+ * needs: a dump of a fixed set of connections and a per-connection delete.
+ *
+ * The dump is a snapshot and the result of each delete is set by the test,
+ * so a test can make a connection go away between the moment it is dumped
+ * and the moment it is deleted, which no real datapath does on demand. */
+
+#define N_CONNS 5
+#define TEST_ZONE 7
+#define FLUSH_MARK 0x1234
+#define OTHER_MARK 0x4321
+#define BASE_PORT 1000
+
+struct test_conn {
+    uint32_t mark;
+    int flush_error;            /* Result of deleting this connection. */
+    int n_flush;                /* Number of deletes of this connection. */
+};
+
+static struct test_conn conns[N_CONNS];
+
+struct test_dump {
+    struct ct_dpif_dump_state up;
+    size_t pos;
+};
+
+static int
+test_ct_dump_start(struct dpif *dpif OVS_UNUSED,
+                   struct ct_dpif_dump_state **statep,
+                   const uint16_t *zone, int *ptot_bkts OVS_UNUSED)
+{
+    struct test_dump *dump = xzalloc(sizeof *dump);
+
+    ovs_assert(zone && *zone == TEST_ZONE);
+    *statep = &dump->up;
+    return 0;
+}
+
+static int
+test_ct_dump_next(struct dpif *dpif OVS_UNUSED,
+                  struct ct_dpif_dump_state *state,
+                  struct ct_dpif_entry *entry)
+{
+    struct test_dump *dump = CONTAINER_OF(state, struct test_dump, up);
+
+    if (dump->pos >= N_CONNS) {
+        return EOF;
+    }
+
+    memset(entry, 0, sizeof *entry);
+    entry->zone = TEST_ZONE;
+    entry->mark = conns[dump->pos].mark;
+    entry->tuple_orig.l3_type = AF_INET;
+    entry->tuple_orig.ip_proto = IPPROTO_TCP;
+    entry->tuple_orig.src.ip = htonl(0x0a000001);
+    entry->tuple_orig.dst.ip = htonl(0x0a000002);
+    entry->tuple_orig.src_port = htons(BASE_PORT + dump->pos);
+    entry->tuple_orig.dst_port = htons(80);
+    dump->pos++;
+
+    return 0;
+}
+
+static int
+test_ct_dump_done(struct dpif *dpif OVS_UNUSED,
+                  struct ct_dpif_dump_state *state)
+{
+    free(CONTAINER_OF(state, struct test_dump, up));
+    return 0;
+}
+
+static int
+test_ct_flush(struct dpif *dpif OVS_UNUSED, const uint16_t *zone,
+              const struct ct_dpif_tuple *tuple)
+{
+    size_t i;
+
+    ovs_assert(zone && *zone == TEST_ZONE && tuple);
+    i = ntohs(tuple->src_port) - BASE_PORT;
+    ovs_assert(i < N_CONNS);
+
+    conns[i].n_flush++;
+    return conns[i].flush_error;
+}
+
+static const struct dpif_class test_dpif_class = {
+    .type = "test-ct-dpif",
+    .ct_dump_start = test_ct_dump_start,
+    .ct_dump_next = test_ct_dump_next,
+    .ct_dump_done = test_ct_dump_done,
+    .ct_flush = test_ct_flush,
+};
+
+/* Flushes the connections with FLUSH_MARK in TEST_ZONE.  The delete of
+ * connection 'i' returns 'flush_errors[i]'.  Checks that the flush returns
+ * 'expected_error' and that the delete of connection 'i' was called
+ * 'expected_flushes[i]' times.
+ *
+ * Connection 1 has another mark, so it must never be deleted. */
+static void
+check_flush(const char *name, const int flush_errors[N_CONNS],
+            int expected_error, const int expected_flushes[N_CONNS])
+{
+    struct ofp_ct_match match = {
+        .mark = FLUSH_MARK,
+        .mark_mask = UINT32_MAX,
+    };
+    uint16_t zone = TEST_ZONE;
+    struct dpif dpif;
+    int error;
+
+    for (size_t i = 0; i < N_CONNS; i++) {
+        conns[i] = (struct test_conn) {
+            .mark = i == 1 ? OTHER_MARK : FLUSH_MARK,
+            .flush_error = flush_errors[i],
+        };
+    }
+
+    dpif_init(&dpif, &test_dpif_class, "test", 0, 0);
+    error = ct_dpif_flush(&dpif, &zone, &match);
+    dpif_uninit(&dpif, false);
+
+    if (error != expected_error) {
+        ovs_fatal(0, "%s: flush returned %d (%s), expected %d (%s)", name,
+                  error, ovs_strerror(error),
+                  expected_error, ovs_strerror(expected_error));
+    }
+    for (size_t i = 0; i < N_CONNS; i++) {
+        if (conns[i].n_flush != expected_flushes[i]) {
+            ovs_fatal(0, "%s: connection %"PRIuSIZE" deleted %d times, "
+                      "expected %d", name, i, conns[i].n_flush,
+                      expected_flushes[i]);
+        }
+    }
+    printf("%s: ok\n", name);
+}
+
+static void
+test_ct_dpif_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+    /* Every delete works: all the matching connections are deleted. */
+    check_flush("all deleted",
+                (const int[N_CONNS]) { 0, 0, 0, 0, 0 }, 0,
+                (const int[N_CONNS]) { 1, 0, 1, 1, 1 });
+
+    /* A connection that is gone by the time it is deleted does not stop the
+     * flush and is not an error. */
+    check_flush("gone in the middle",
+                (const int[N_CONNS]) { 0, 0, ENOENT, 0, 0 }, 0,
+                (const int[N_CONNS]) { 1, 0, 1, 1, 1 });
+    check_flush("gone at the end",
+                (const int[N_CONNS]) { 0, 0, 0, 0, ENOENT }, 0,
+                (const int[N_CONNS]) { 1, 0, 1, 1, 1 });
+    check_flush("all gone",
+                (const int[N_CONNS]) { ENOENT, 0, ENOENT, ENOENT, ENOENT }, 0,
+                (const int[N_CONNS]) { 1, 0, 1, 1, 1 });
+
+    /* Any other error ends the flush and is returned. */
+    check_flush("not supported",
+                (const int[N_CONNS]) { 0, 0, EOPNOTSUPP, 0, 0 }, EOPNOTSUPP,
+                (const int[N_CONNS]) { 1, 0, 1, 0, 0 });
+    check_flush("gone, then no buffers",
+                (const int[N_CONNS]) { ENOENT, 0, 0, ENOBUFS, 0 }, ENOBUFS,
+                (const int[N_CONNS]) { 1, 0, 1, 1, 0 });
+}
+
+OVSTEST_REGISTER("test-ct-dpif", test_ct_dpif_main);
-- 
2.43.0

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

Reply via email to