A distributed load balancer delivers to the backends of the chassis the
traffic entered on, so that the reply leaves through the same chassis
and the per-backend routes keep their meaning. That is right for traffic
that arrives through the logical router port that advertised the VIP.
For a client on a logical switch port bound to a chassis, the reply
returns through the conntrack zone of its own switch on its own chassis,
whichever chassis the backend is on. A chassis without a healthy local
backend still drops the connection, but it does not have to. Serve such
a client from the backends of the other chassis instead.

Add a fallback keyword to ct_lb_mark_local. With it, a chassis that has
none of the backends selects among all of them instead of dropping the
traffic. northd sets it on the switch ingress flow of a distributed load
balancer, and keeps local-only delivery on the router flow, on switches
with a localnet, l2gateway, vtep or remote port and on transit switches,
whose traffic comes from outside the overlay and whose reply would leave
through the backend's chassis. The switch flow serves only traffic that
enters through a port of the switch: the pre-load-balancer stage skips
conntrack for the router ports, so traffic that a router forwards into
the switch is never load balanced by the switch, with or without the
fallback. A load balancer that must serve routed traffic is attached to
the logical router as well.

Such a fallback was suggested during the review of the distributed
load balancer RFC [0] and set aside over the reply path of the router
case, which stays local here. Nothing relies on the switch flow
dropping the traffic either:

- The feature was written for traffic that the fabric steers to a
  chassis with a backend [1].
- A load balancer attached to a logical switch was not a use case of
  its own [2].
- A deployment that lets workloads reach a VIP only where a local
  backend runs was not thought realistic during the review [3].

The switch flow never promised local-only delivery, so the fallback is
on by default.

[0] https://mail.openvswitch.org/pipermail/ovs-dev/2025-October/427029.html
[1] https://mail.openvswitch.org/pipermail/ovs-dev/2026-January/428912.html
[2] https://mail.openvswitch.org/pipermail/ovs-dev/2025-November/428115.html
[3] https://mail.openvswitch.org/pipermail/ovs-dev/2025-October/427053.html

Test the action parsing and encoding, the flows northd emits for a
plain, a localnet, a vtep and a transit switch, and the group the
controller builds on a switch before, after and without a local backend
binding. Trace also the same VIP packet entering from a client port,
which the switch load balances, and from a router port, which it does
not.

Signed-off-by: Dmitrii Shcherbakov <[email protected]>
---
 NEWS                    |   5 +
 include/ovn/actions.h   |   2 +
 lib/actions.c           | 133 ++++++++++++++---------
 lib/ovn-util.c          |   2 +-
 northd/northd.c         | 232 ++++++++++++++++++++++++++++++++--------
 northd/northd.h         |   5 +
 ovn-nb.xml              |  11 +-
 ovn-sb.xml              |  11 +-
 tests/multinode.at      |  50 +++++++++
 tests/ovn-controller.at | 200 ++++++++++++++++++++++++++++++++++
 tests/ovn-northd.at     | 117 ++++++++++++++++++++
 tests/ovn.at            |  79 ++++++++++++++
 12 files changed, 752 insertions(+), 95 deletions(-)

diff --git a/NEWS b/NEWS
index 490364f9d..2e1db57e1 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,11 @@ Post v26.09.0
    - Added a new "options:dynamic-routing-advertise-vips" key on the NB
      Load_Balancer table to limit route advertisement to a subset of the
      load balancer VIPs.
+   - A distributed load balancer now delivers traffic from a logical switch
+     port to any backend when the chassis has no local backend, instead of
+     dropping it.  Traffic that enters through a logical router port and
+     traffic on a logical switch with a localnet, l2gateway, vtep or remote
+     port keep local-only delivery.
    - Added a new "options:dynamic-routing-advertise-prefixes" key on the
      NB Logical_Router table, with a Logical_Router_Port override, to
      limit the advertisement of load balancer VIPs and NAT external IPs
diff --git a/include/ovn/actions.h b/include/ovn/actions.h
index 7def8917e..3053f628c 100644
--- a/include/ovn/actions.h
+++ b/include/ovn/actions.h
@@ -340,6 +340,8 @@ struct ovnact_ct_lb {
     uint8_t ltable;             /* Logical table ID of next table. */
     char *hash_fields;
     enum ovnact_ct_lb_flag ct_flag;
+    bool fallback;              /* ct_lb_mark_local: use every backend when
+                                 * none of them is local. */
 };
 
 struct ovnact_select_dst {
diff --git a/lib/actions.c b/lib/actions.c
index 0e95e2d70..7106cbf45 100644
--- a/lib/actions.c
+++ b/lib/actions.c
@@ -1218,6 +1218,7 @@ parse_ct_lb_action(struct action_context *ctx,
     struct vector dsts = VECTOR_EMPTY_INITIALIZER(struct ovnact_ct_lb_dst);
     char *hash_fields = NULL;
     enum ovnact_ct_lb_flag ct_flag = OVNACT_CT_LB_FLAG_NONE;
+    bool fallback = false;
 
     if (lexer_match(ctx->lexer, LEX_T_LPAREN) &&
         !lexer_match(ctx->lexer, LEX_T_RPAREN)) {
@@ -1326,6 +1327,12 @@ parse_ct_lb_action(struct action_context *ctx,
             ct_flag = OVNACT_CT_LB_FLAG_FORCE_SNAT;
             lexer_get(ctx->lexer);
         }
+
+        if (type == OVNACT_CT_LB_LOCAL_TYPE_MARK
+            && lexer_match_id(ctx->lexer, "fallback")) {
+            fallback = true;
+            lexer_get(ctx->lexer);
+        }
     }
 
     struct ovnact_ct_lb *cl;
@@ -1345,6 +1352,7 @@ parse_ct_lb_action(struct action_context *ctx,
     cl->dsts = vector_steal_array(&dsts);
     cl->hash_fields = hash_fields;
     cl->ct_flag = ct_flag;
+    cl->fallback = fallback;
 }
 
 static void
@@ -1399,6 +1407,9 @@ format_ct_lb(const struct ovnact_ct_lb *cl, struct ds *s,
                 /* None is the default value not shown in the output. */
                 break;
         }
+        if (cl->fallback) {
+            ds_put_cstr(s, "; fallback");
+        }
         ds_put_char(s, ')');
     }
 
@@ -1417,6 +1428,69 @@ format_CT_LB_MARK_LOCAL(const struct ovnact_ct_lb *cl, 
struct ds *s)
     format_ct_lb(cl, s, OVNACT_CT_LB_LOCAL_TYPE_MARK);
 }
 
+/* Writes the select group that load balances over the destinations of 'cl'
+ * into 'ds' and returns the number of buckets.  With 'local_only' set, only
+ * the destinations whose logical port is local to this chassis get a
+ * bucket. */
+static size_t
+encode_ct_lb_group(const struct ovnact_ct_lb *cl,
+                   const struct ovnact_encode_params *ep,
+                   bool local_only, uint8_t recirc_table, uint32_t zone_reg,
+                   const char *ct_flag_value, struct ds *ds)
+{
+    ds_clear(ds);
+    ds_put_format(ds, "type=select,selection_method=%s",
+                  cl->hash_fields ? "hash": "dp_hash");
+    if (cl->hash_fields) {
+        ds_put_format(ds, ",fields(%s)", cl->hash_fields);
+    }
+
+    size_t n_active_backends = 0;
+    for (size_t bucket_id = 0; bucket_id < cl->n_dsts; bucket_id++) {
+        const struct ovnact_ct_lb_dst *dst = &cl->dsts[bucket_id];
+        char ip_addr[INET6_ADDRSTRLEN];
+
+        if (local_only && !ep->lookup_local_port(ep->aux, dst->port_name)) {
+            continue;
+        }
+
+        if (dst->family == AF_INET) {
+            inet_ntop(AF_INET, &dst->ipv4, ip_addr, sizeof ip_addr);
+        } else {
+            inet_ntop(AF_INET6, &dst->ipv6, ip_addr, sizeof ip_addr);
+        }
+
+        ds_put_format(ds, ",bucket=bucket_id=%"PRIuSIZE",weight:100,actions=",
+                      bucket_id);
+
+        bool is_ipv6_address = (dst->family == AF_INET6 && dst->port);
+        ds_put_format(ds, "ct(nat(dst=");
+        if (is_ipv6_address) {
+            ds_put_format(ds, "[%s]", ip_addr);
+        } else {
+            ds_put_format(ds, "%s", ip_addr);
+        }
+
+        if (dst->port) {
+            ds_put_format(ds, ":%"PRIu16, dst->port);
+        }
+        ds_put_format(ds, "),commit,table=%d,zone=NXM_NX_REG%d[0..15],"
+                      "exec(set_field:"
+                        OVN_CT_MASKED_STR(OVN_CT_NATTED)
+                      "->ct_mark",
+                      recirc_table, zone_reg);
+        if (ct_flag_value) {
+            ds_put_format(ds, ",set_field:%s->ct_mark", ct_flag_value);
+        }
+
+        ds_put_cstr(ds, "))");
+
+        n_active_backends++;
+    }
+
+    return n_active_backends;
+}
+
 static void
 encode_ct_lb(const struct ovnact_ct_lb *cl,
              const struct ovnact_encode_params *ep,
@@ -1468,58 +1542,19 @@ encode_ct_lb(const struct ovnact_ct_lb *cl,
             break;
     }
 
-    struct ds ds = DS_EMPTY_INITIALIZER;
-    ds_put_format(&ds, "type=select,selection_method=%s",
-                  cl->hash_fields ? "hash": "dp_hash");
-    if (cl->hash_fields) {
-        ds_put_format(&ds, ",fields(%s)", cl->hash_fields);
-    }
-
     /* Make sure that all the used registers are within the NXM_NX class. */
     BUILD_ASSERT(OVN_FLOW_N_REGS_SUPPORTED == 16);
 
-    size_t n_active_backends = 0;
-    for (size_t bucket_id = 0; bucket_id < cl->n_dsts; bucket_id++) {
-        const struct ovnact_ct_lb_dst *dst = &cl->dsts[bucket_id];
-        char ip_addr[INET6_ADDRSTRLEN];
-
-        if (type == OVNACT_CT_LB_LOCAL_TYPE_MARK
-            && !ep->lookup_local_port(ep->aux, dst->port_name)) {
-            continue;
-        }
-
-        if (dst->family == AF_INET) {
-            inet_ntop(AF_INET, &dst->ipv4, ip_addr, sizeof ip_addr);
-        } else {
-            inet_ntop(AF_INET6, &dst->ipv6, ip_addr, sizeof ip_addr);
-        }
-
-        ds_put_format(&ds, ",bucket=bucket_id=%"PRIuSIZE",weight:100,actions=",
-                      bucket_id);
-
-        bool is_ipv6_address = (dst->family == AF_INET6 && dst->port);
-        ds_put_format(&ds, "ct(nat(dst=");
-        if (is_ipv6_address) {
-            ds_put_format(&ds, "[%s]", ip_addr);
-        } else {
-            ds_put_format(&ds, "%s", ip_addr);
-        }
-
-        if (dst->port) {
-            ds_put_format(&ds, ":%"PRIu16, dst->port);
-        }
-        ds_put_format(&ds, "),commit,table=%d,zone=NXM_NX_REG%d[0..15],"
-                      "exec(set_field:"
-                        OVN_CT_MASKED_STR(OVN_CT_NATTED)
-                      "->ct_mark",
-                      recirc_table, zone_reg);
-        if (ct_flag_value) {
-            ds_put_format(&ds, ",set_field:%s->ct_mark", ct_flag_value);
-        }
-
-        ds_put_cstr(&ds, "))");
-
-        n_active_backends++;
+    struct ds ds = DS_EMPTY_INITIALIZER;
+    bool local_only = type == OVNACT_CT_LB_LOCAL_TYPE_MARK;
+    size_t n_active_backends = encode_ct_lb_group(cl, ep, local_only,
+                                                  recirc_table, zone_reg,
+                                                  ct_flag_value, &ds);
+    if (!n_active_backends && local_only && cl->fallback) {
+        /* No backend is local to this chassis and the action asked to use
+         * every backend in that case instead of dropping the traffic. */
+        n_active_backends = encode_ct_lb_group(cl, ep, false, recirc_table,
+                                               zone_reg, ct_flag_value, &ds);
     }
 
     if (!n_active_backends) {
diff --git a/lib/ovn-util.c b/lib/ovn-util.c
index b184e1770..82a56c062 100644
--- a/lib/ovn-util.c
+++ b/lib/ovn-util.c
@@ -1008,7 +1008,7 @@ ip_address_and_port_from_lb_key(const char *key, char 
**ip_address,
  * NOTE: If OVN_NORTHD_PIPELINE_CSUM is updated make sure to double check
  * whether an update of OVN_INTERNAL_MINOR_VER is required. */
 #define OVN_NORTHD_PIPELINE_CSUM "3980195012 11262"
-#define OVN_INTERNAL_MINOR_VER 16
+#define OVN_INTERNAL_MINOR_VER 17
 
 /* Returns the OVN version. The caller must free the returned value. */
 char *
diff --git a/northd/northd.c b/northd/northd.c
index c4a38f38d..b31e969b4 100644
--- a/northd/northd.c
+++ b/northd/northd.c
@@ -985,6 +985,14 @@ init_ls_other_config(struct ovn_datapath *od)
     int64_t vni = ovn_smap_get_llong(&od->nbs->other_config,
                                      "dynamic-routing-vni", -1);
     od->has_evpn_vni = ovn_is_valid_vni(vni);
+
+    /* A transit switch carries traffic from other zones through remote
+     * ports that arrive after its creation and come and go with the
+     * interconnection, so the switch itself, not the ports, marks the
+     * traffic as not entering through a port bound to one chassis. */
+    if (smap_get(&od->nbs->other_config, "interconn-ts")) {
+        od->has_non_overlay_lports = true;
+    }
 }
 
 /* Initializes 'ls_datapaths' to contain a "struct ovn_datapath" for every
@@ -1303,6 +1311,56 @@ lsp_is_vtep(const struct nbrec_logical_switch_port *nbsp)
     return !strcmp(nbsp->type, "vtep");
 }
 
+/* True for a port whose traffic enters or leaves this deployment
+ * itself, not one chassis of it. */
+static bool
+lsp_is_non_overlay(const struct nbrec_logical_switch_port *nbsp)
+{
+    return lsp_is_localnet(nbsp) || lsp_is_vtep(nbsp) || lsp_is_remote(nbsp)
+           || !strcmp(nbsp->type, "l2gateway");
+}
+
+/* True if 'od' has load balancer flows: a load balancer or group of its
+ * own, or one of a neighbouring router when install_ls_lb_from_router is
+ * set. */
+static bool
+ls_has_lb(const struct ovn_datapath *od)
+{
+    if (od->nbs->n_load_balancer || od->nbs->n_load_balancer_group) {
+        return true;
+    }
+    if (!install_ls_lb_from_router) {
+        return false;
+    }
+    const struct ovn_port *op;
+    VECTOR_FOR_EACH (&od->router_ports, op) {
+        const struct ovn_datapath *lr = op->peer ? op->peer->od : NULL;
+        if (lr && lr->nbr
+            && (lr->nbr->n_load_balancer || lr->nbr->n_load_balancer_group)) {
+            return true;
+        }
+    }
+    return false;
+}
+
+/* True if 'od' is a transit switch or has a non-overlay port other than
+ * 'skip', which may be NULL. */
+static bool
+ls_has_non_overlay_lport_other_than(const struct ovn_datapath *od,
+                                    const struct ovn_port *skip)
+{
+    if (smap_get(&od->nbs->other_config, "interconn-ts")) {
+        return true;
+    }
+    const struct ovn_port *op;
+    HMAP_FOR_EACH (op, dp_node, &od->ports) {
+        if (op != skip && op->nbsp && lsp_is_non_overlay(op->nbsp)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 static bool
 localnet_can_learn_mac(const struct nbrec_logical_switch_port *nbsp)
 {
@@ -1695,6 +1753,10 @@ join_logical_ports_lsp(struct hmap *ports,
         od->has_vtep_lports = true;
     }
 
+    if (lsp_is_non_overlay(nbsp)) {
+        od->has_non_overlay_lports = true;
+    }
+
     parse_lsp_addrs(op);
 
     op->od = od;
@@ -3654,7 +3716,7 @@ build_lb_vip_actions(const struct ovn_northd_lb *lb,
                      struct ds *skip_snat_action,
                      struct ds *force_snat_action,
                      const struct svc_monitors_map_data *svc_mons_data,
-                     bool ls_dp)
+                     bool ls_dp, bool fallback)
 {
     bool reject =
         vector_is_empty(&lb_vip->backends) && lb_vip->empty_backend_rej;
@@ -3734,6 +3796,9 @@ build_lb_vip_actions(const struct ovn_northd_lb *lb,
     }
 
     bool is_lb_action = !(reject || drop);
+    if (is_lb_action && fallback) {
+        ds_put_cstr(action, "; fallback");
+    }
     const char *enclose = is_lb_action ? ");" : "";
 
     if (!ls_dp) {
@@ -5129,6 +5194,14 @@ ls_handle_lsp_changes(struct ovsdb_idl_txn 
*ovnsb_idl_txn,
                 if (!lsp_can_be_inc_processed(new_nbsp)) {
                     goto fail;
                 }
+                if (lsp_is_remote(new_nbsp) && !od->has_non_overlay_lports) {
+                    /* The first non-overlay port changes the form of the
+                     * load balancer flows of the switch. */
+                    if (ls_has_lb(od)) {
+                        goto fail;
+                    }
+                    od->has_non_overlay_lports = true;
+                }
                 op = ls_port_create(ovnsb_idl_txn, &nd->ls_ports,
                                     new_nbsp->name, new_nbsp, od,
                                     ni->sbrec_mirror_table,
@@ -5219,6 +5292,15 @@ ls_handle_lsp_changes(struct ovsdb_idl_txn 
*ovnsb_idl_txn,
                  * resolved; fall back to recompute. */
                 goto fail;
             }
+            if (lsp_is_remote(op->nbsp)
+                && !ls_has_non_overlay_lport_other_than(od, op)) {
+                /* The last non-overlay port changes the form of the load
+                 * balancer flows of the switch. */
+                if (ls_has_lb(od)) {
+                    goto fail;
+                }
+                od->has_non_overlay_lports = false;
+            }
             add_op_to_northd_tracked_ports(&trk_lsps->deleted, op);
             hmap_remove(&nd->ls_ports, &op->key_node);
             hmap_remove(&od->ports, &op->dp_node);
@@ -9789,6 +9871,51 @@ build_lb_health_check_response_lflows(
     ds_destroy(&probe_match);
 }
 
+/* Adds the ls_in_lb flow of one VIP to the switches in 'dp_map'. */
+static void
+build_lb_rules_for_dps(struct lflow_table *lflows,
+                       struct ovn_lb_datapaths *lb_dps,
+                       const struct ovn_datapaths *ls_datapaths,
+                       const unsigned long *dp_map,
+                       int priority, bool reject,
+                       struct ds *match, struct ds *action,
+                       const struct shash *meter_groups)
+{
+    const struct ovn_northd_lb *lb = lb_dps->lb;
+    unsigned long *dp_non_meter = NULL;
+    bool build_non_meter = false;
+    if (reject) {
+        size_t index;
+
+        dp_non_meter = bitmap_clone(dp_map, ods_size(ls_datapaths));
+        BITMAP_FOR_EACH_1 (index, ods_size(ls_datapaths), dp_map) {
+            struct ovn_datapath *od = sparse_array_get(&ls_datapaths->dps,
+                                                       index);
+
+            const char *meter = copp_meter_get(COPP_REJECT, od->nbs->copp,
+                                               meter_groups);
+            if (!meter) {
+                build_non_meter = true;
+                continue;
+            }
+            bitmap_set0(dp_non_meter, index);
+            ovn_lflow_add(lflows, od, S_SWITCH_IN_LB, priority,
+                          ds_cstr(match), ds_cstr(action),
+                          lb_dps->lflow_ref, WITH_CTRL_METER(meter),
+                          WITH_HINT(&lb->nlb->header_));
+        }
+    }
+    if (!reject || build_non_meter) {
+        ovn_lflow_add_with_dp_group(lflows,
+                                    dp_non_meter ? dp_non_meter : dp_map,
+                                    ods_size(ls_datapaths), S_SWITCH_IN_LB,
+                                    priority, ds_cstr(match),
+                                    ds_cstr(action), lb_dps->lflow_ref,
+                                    WITH_HINT(&lb->nlb->header_));
+    }
+    bitmap_free(dp_non_meter);
+}
+
 static void
 build_lb_rules(struct lflow_table *lflows, struct ovn_lb_datapaths *lb_dps,
                const struct ovn_datapaths *ls_datapaths,
@@ -9797,22 +9924,39 @@ build_lb_rules(struct lflow_table *lflows, struct 
ovn_lb_datapaths *lb_dps,
                const struct svc_monitors_map_data *svc_mons_data)
 {
     const struct ovn_northd_lb *lb = lb_dps->lb;
+
+    /* A distributed load balancer delivers to a local backend when the
+     * chassis has one and to any backend otherwise: the reply of a client
+     * on a port bound to a chassis returns through the conntrack zone of
+     * its own switch on its own chassis, whichever chassis the backend is
+     * on.  Traffic from outside the overlay, through a localnet, l2gateway,
+     * vtep or remote port or over a transit switch, has no such return:
+     * its reply would leave through the chassis of the backend, so its
+     * switch keeps local-only delivery. */
+    unsigned long *dp_fallback = NULL;
+    unsigned long *dp_local = NULL;
+    if (lb->is_distributed) {
+        size_t n_dps = ods_size(ls_datapaths);
+        dp_fallback = dynamic_bitmap_clone_map(&lb_dps->nb_ls_map);
+        dp_local = bitmap_allocate(n_dps);
+        size_t index;
+        DYNAMIC_BITMAP_FOR_EACH_1 (index, &lb_dps->nb_ls_map) {
+            struct ovn_datapath *od = sparse_array_get(&ls_datapaths->dps,
+                                                       index);
+            if (od->has_non_overlay_lports) {
+                bitmap_set0(dp_fallback, index);
+                bitmap_set1(dp_local, index);
+            }
+        }
+    }
+
     for (size_t i = 0; i < lb->n_vips; i++) {
         struct ovn_lb_vip *lb_vip = &lb->vips[i];
         struct ovn_northd_lb_vip *lb_vip_nb = &lb->vips_nb[i];
         const char *ip_match =
             lb_vip->address_family == AF_INET ? "ip4" : "ip6";
 
-        ds_clear(action);
         ds_clear(match);
-
-        /* New connections in Ingress table. */
-        const char *meter = NULL;
-        bool reject = build_lb_vip_actions(lb, lb_vip, lb_vip_nb, action,
-                                           lb->selection_fields,
-                                           NULL, NULL,
-                                           svc_mons_data, true);
-
         ds_put_format(match, "ct.new && %s.dst == %s", ip_match,
                       lb_vip->vip_str);
         int priority = 110;
@@ -9826,41 +9970,43 @@ build_lb_rules(struct lflow_table *lflows, struct 
ovn_lb_datapaths *lb_dps,
         build_lb_affinity_ls_flows(lflows, lb_dps, lb_vip, ls_datapaths,
                                    lb_dps->lflow_ref);
 
-        unsigned long *dp_non_meter = NULL;
-        bool build_non_meter = false;
-        if (reject) {
-            size_t index;
-
-            dp_non_meter = dynamic_bitmap_clone_map(&lb_dps->nb_ls_map);
-            DYNAMIC_BITMAP_FOR_EACH_1 (index, &lb_dps->nb_ls_map) {
-                struct ovn_datapath *od = sparse_array_get(&ls_datapaths->dps,
-                                                           index);
-
-                meter = copp_meter_get(COPP_REJECT, od->nbs->copp,
-                                       meter_groups);
-                if (!meter) {
-                    build_non_meter = true;
-                    continue;
-                }
-                bitmap_set0(dp_non_meter, index);
-                ovn_lflow_add(lflows, od, S_SWITCH_IN_LB, priority,
-                              ds_cstr(match), ds_cstr(action),
-                              lb_dps->lflow_ref, WITH_CTRL_METER(meter),
-                              WITH_HINT(&lb->nlb->header_));
-            }
+        /* New connections in Ingress table. */
+        if (!lb->is_distributed) {
+            ds_clear(action);
+            bool reject = build_lb_vip_actions(lb, lb_vip, lb_vip_nb, action,
+                                               lb->selection_fields,
+                                               NULL, NULL, svc_mons_data,
+                                               true, false);
+            build_lb_rules_for_dps(lflows, lb_dps, ls_datapaths,
+                                   lb_dps->nb_ls_map.map, priority, reject,
+                                   match, action, meter_groups);
+            continue;
         }
-        if (!reject || build_non_meter) {
-            ovn_lflow_add_with_dp_group(lflows,
-                                        dp_non_meter
-                                            ? dp_non_meter
-                                            : lb_dps->nb_ls_map.map,
-                                        ods_size(ls_datapaths), S_SWITCH_IN_LB,
-                                        priority, ds_cstr(match),
-                                        ds_cstr(action), lb_dps->lflow_ref,
-                                        WITH_HINT(&lb->nlb->header_));
+
+        if (!bitmap_is_all_zeros(dp_fallback, ods_size(ls_datapaths))) {
+            ds_clear(action);
+            bool reject = build_lb_vip_actions(lb, lb_vip, lb_vip_nb, action,
+                                               lb->selection_fields,
+                                               NULL, NULL, svc_mons_data,
+                                               true, true);
+            build_lb_rules_for_dps(lflows, lb_dps, ls_datapaths, dp_fallback,
+                                   priority, reject, match, action,
+                                   meter_groups);
+        }
+        if (!bitmap_is_all_zeros(dp_local, ods_size(ls_datapaths))) {
+            ds_clear(action);
+            bool reject = build_lb_vip_actions(lb, lb_vip, lb_vip_nb, action,
+                                               lb->selection_fields,
+                                               NULL, NULL, svc_mons_data,
+                                               true, false);
+            build_lb_rules_for_dps(lflows, lb_dps, ls_datapaths, dp_local,
+                                   priority, reject, match, action,
+                                   meter_groups);
         }
-        bitmap_free(dp_non_meter);
     }
+
+    bitmap_free(dp_fallback);
+    bitmap_free(dp_local);
 }
 
 static void
@@ -14121,7 +14267,7 @@ build_lrouter_nat_flows_for_lb(
                                        &skip_snat_act,
                                        &force_snat_act,
                                        svc_mons_data,
-                                       false);
+                                       false, false);
 
     /* Higher priority rules are added for load-balancing in DNAT
      * table.  For every match (on a VIP[:port]), we add two flows.
diff --git a/northd/northd.h b/northd/northd.h
index 442a90481..e887ce309 100644
--- a/northd/northd.h
+++ b/northd/northd.h
@@ -414,6 +414,11 @@ struct ovn_datapath {
     bool has_unknown;
     bool has_vtep_lports;
     bool has_arp_proxy_port;
+    /* True if a localnet, l2gateway, vtep or remote port carries traffic of
+     * this switch, or if it is a transit switch, that is, if traffic does
+     * not enter and leave the switch through a port bound to one chassis of
+     * this deployment. */
+    bool has_non_overlay_lports;
 
     /* Set to true if the option 'enable-stateless-acl-with-lb' is enabled
      * on the logical switch. */
diff --git a/ovn-nb.xml b/ovn-nb.xml
index ef2eea6fa..4aa01a8c3 100644
--- a/ovn-nb.xml
+++ b/ovn-nb.xml
@@ -2853,7 +2853,16 @@ or
       <column name="options" key="distributed">
         Option enables distributed load balancing across compute nodes,
         ensuring traffic is always routed to local backends, eliminating
-        east-west traffic between nodes.
+        east-west traffic between nodes.  Traffic that enters through a
+        logical router port is delivered to the backends of the chassis
+        it entered on only.  Traffic from a logical switch port is
+        delivered to a backend of the same chassis when there is one and
+        to any backend otherwise, unless the logical switch has a localnet,
+        l2gateway, vtep or remote port, in which case it is delivered to
+        local backends only as well.
+        Traffic that a logical router forwards into the switch is not
+        load balanced by the logical switch.  A load balancer that must
+        serve such traffic is attached to the logical router as well.
         Required configuration: <ref column="ip_port_mappings"/>.
         NOTE: The addressing of the underlay network must not overlap with the
         addressing of Load Balancer VIP. If the Load Balancer is attached to a
diff --git a/ovn-sb.xml b/ovn-sb.xml
index a05c043f6..cf2c52c81 100644
--- a/ovn-sb.xml
+++ b/ovn-sb.xml
@@ -2279,7 +2279,7 @@
         </dd>
 
         <dt><code>ct_lb_mark_local;</code></dt>
-        
<dt><code>ct_lb_mark_local(backends=<var>lport_name</var>[<var>ip</var>[:<var>port</var>][,...][;
 hash_fields=<var>field1</var>,<var>field2</var>,...][; ct_flag]);</code></dt>
+        
<dt><code>ct_lb_mark_local(backends=<var>lport_name</var>[<var>ip</var>[:<var>port</var>][,...][;
 hash_fields=<var>field1</var>,<var>field2</var>,...][; ct_flag][; 
fallback]);</code></dt>
         <dd>
           <p>
               Same as <code>ct_lb_mark</code>, with the key difference that it
@@ -2287,6 +2287,15 @@
               only from those running on the current chassis, preventing
               traffic from being forwarded to backends on remote nodes.
           </p>
+
+          <p>
+              With <code>fallback</code>, a chassis that has none of the
+              backends selects among all of them instead of dropping the
+              traffic.  This is meant for traffic whose reply returns through
+              the chassis that applied the action whichever chassis the
+              backend is on, such as traffic from a logical switch port of
+              the same chassis.
+          </p>
         </dd>
 
         <dt>
diff --git a/tests/multinode.at b/tests/multinode.at
index 6e5ead52d..b59fa7d11 100644
--- a/tests/multinode.at
+++ b/tests/multinode.at
@@ -5885,4 +5885,54 @@ AT_CHECK([cat ch2_genev.tcpdump], [0], [dnl
 AT_CHECK([cat ch2_eth2.tcpdump], [0], [dnl
 ])
 
+# An overlay client on a chassis without a backend.  With the load balancer
+# on the switch, the chassis falls back to the backends of the other
+# chassis and the reply returns through the client's chassis.
+check multinode_nbctl lsp-add ls1 ls1c3
+check multinode_nbctl lsp-set-addresses ls1c3 "00:00:00:01:04:02 192.168.1.4"
+m_as ovn-chassis-3 /data/create_fake_vm.sh ls1c3 ls1c3 00:00:00:01:04:02 1500 
192.168.1.4 24 192.168.1.254 2001::4/64 2001::a
+m_wait_for_ports_up
+check multinode_nbctl ls-lb-add ls1 lb1
+check multinode_nbctl --wait=hv sync
+
+get_lb_backends_ch3() {
+    m_as ovn-chassis-3 ovs-ofctl dump-groups br-int > groups || return 1
+    tr ')' '\n' < groups | sed -n 's/.*nat(dst=//p' | sort
+}
+OVS_WAIT_FOR_OUTPUT([get_lb_backends_ch3], [0], [dnl
+192.168.1.1:10880
+192.168.1.2:10880
+192.168.1.3:10880
+])
+
+AT_CHECK([m_as ovn-chassis-3 ip netns exec ls1c3 /bin/bash -c '
+    for i in $(seq 20); do
+        reply=$(echo "" | timeout 5 nc -w 3 172.31.0.1 80) || exit 1
+        case "$reply" in
+            ls1p1|ls1p2|ls1p3) ;;
+            *) exit 1 ;;
+        esac
+    done
+'], [0], [])
+
+# A backend bound on the client chassis takes over and the group shrinks
+# to it.  The router flow, local-only, now has a local backend as well and
+# gets a group of its own, so the backend shows up twice.
+check m_as ovn-chassis-2 ovs-vsctl del-port br-int ls1p3-p
+m_wait_row_count nb:Logical_Switch_Port 1 name=ls1p3 up=false
+check m_as ovn-chassis-3 /data/create_fake_vm.sh ls1p3 ls1p3 00:00:00:01:03:02 
1500 192.168.1.3 24 192.168.1.254 2001::3/64 2001::a
+m_wait_row_count nb:Logical_Switch_Port 1 name=ls1p3 up=true
+check multinode_nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends_ch3], [0], [dnl
+192.168.1.3:10880
+192.168.1.3:10880
+])
+M_START_L4_SERVER([ovn-chassis-3], [ls1p3], [192.168.1.3], [10880], 
[ls1p3-ch3], [ls1p3-ch3.pid])
+AT_CHECK([m_as ovn-chassis-3 ip netns exec ls1c3 /bin/bash -c '
+    for i in $(seq 10); do
+        reply=$(echo "" | timeout 5 nc -w 3 172.31.0.1 80) || exit 1
+        test "$reply" = "ls1p3-ch3" || exit 1
+    done
+'], [0], [])
+
 AT_CLEANUP
diff --git a/tests/ovn-controller.at b/tests/ovn-controller.at
index 45146535b..addc341d3 100644
--- a/tests/ovn-controller.at
+++ b/tests/ovn-controller.at
@@ -4749,3 +4749,203 @@ OVS_WAIT_FOR_OUTPUT([get_lb_backends], [0], [dnl
 OVN_CLEANUP([hv1])
 AT_CLEANUP
 ])
+
+OVN_FOR_EACH_NORTHD([
+AT_SETUP([ovn-controller - distributed LB fallback on a logical switch])
+AT_KEYWORDS([ovn lb])
+ovn_start
+net_add n1
+sim_add hv1
+as hv1
+check ovs-vsctl add-br br-phys
+ovn_attach n1 br-phys 192.168.0.1
+check ovs-vsctl set open . external-ids:ovn-bridge-mappings=phys:br-phys
+
+AT_CAPTURE_FILE([groups])
+get_lb_backends() {
+    ovs-ofctl dump-groups br-int > groups || return 1
+    tr ')' '\n' < groups | sed -n 's/.*nat(dst=//p' | sort
+}
+
+# A client port makes the switch an overlay one.  While no backend is
+# bound on this chassis, the switch flow of a distributed load balancer
+# selects among every backend, and it goes back to the local backend
+# once one is bound.
+check ovn-nbctl ls-add be
+check ovn-nbctl lsp-add be be-client
+check ovn-nbctl lsp-set-addresses be-client "00:00:00:00:01:00 192.168.1.5"
+check ovn-nbctl lsp-add be be-vm1
+check ovn-nbctl lsp-set-addresses be-vm1 "00:00:00:00:01:01 192.168.1.10"
+check ovn-nbctl lsp-add be be-vm2
+check ovn-nbctl lsp-set-addresses be-vm2 "00:00:00:00:01:02 192.168.1.11"
+check ovn-nbctl \
+    -- lb-add lb0 172.16.1.10:80 192.168.1.10:80,192.168.1.11:80 \
+    -- set Load_Balancer lb0 options:distributed=true \
+        ip_port_mappings:192.168.1.10="be-vm1" \
+        ip_port_mappings:192.168.1.11="be-vm2"
+check ovn-nbctl ls-lb-add be lb0
+check ovn-nbctl --wait=hv sync
+
+check ovs-vsctl add-port br-int vif0 \
+    -- set Interface vif0 external_ids:iface-id=be-client
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-client up) = 
xtrue])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends], [0], [dnl
+192.168.1.10:80
+192.168.1.11:80
+])
+
+check ovs-vsctl add-port br-int vif1 \
+    -- set Interface vif1 external_ids:iface-id=be-vm1
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm1 up) = xtrue])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends], [0], [dnl
+192.168.1.10:80
+])
+CHECK_AFTER_RECOMPUTE([hv1], [hv1])
+
+check ovs-vsctl del-port br-int vif1
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm1 up) = xfalse])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends], [0], [dnl
+192.168.1.10:80
+192.168.1.11:80
+])
+CHECK_AFTER_RECOMPUTE([hv1], [hv1])
+
+# A switch with a localnet port keeps local-only delivery, so without a
+# local backend its flow encodes to no group at all.
+check ovn-nbctl ls-add ext
+check ovn-nbctl lsp-add ext ext-ln \
+    -- lsp-set-type ext-ln localnet \
+    -- lsp-set-addresses ext-ln unknown \
+    -- lsp-set-options ext-ln network_name=phys
+check ovn-nbctl lsp-add ext ext-client
+check ovn-nbctl lsp-set-addresses ext-client "00:00:00:00:02:00 192.168.2.5"
+check ovn-nbctl ls-lb-del be lb0 -- ls-lb-add ext lb0
+check ovn-nbctl --wait=hv sync
+check ovs-vsctl add-port br-int vif2 \
+    -- set Interface vif2 external_ids:iface-id=ext-client
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port ext-client up) = 
xtrue])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends], [0], [dnl
+])
+CHECK_AFTER_RECOMPUTE([hv1], [hv1])
+
+# With a local backend the localnet switch keeps that backend only.
+check ovs-vsctl add-port br-int vif1 \
+    -- set Interface vif1 external_ids:iface-id=be-vm1
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm1 up) = xtrue])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends], [0], [dnl
+192.168.1.10:80
+])
+
+OVN_CLEANUP([hv1])
+AT_CLEANUP
+])
+
+OVN_FOR_EACH_NORTHD([
+AT_SETUP([ovn-controller - distributed LB fallback with backends on another 
chassis])
+AT_KEYWORDS([ovn lb])
+ovn_start
+net_add n1
+sim_add hv1
+as hv1
+check ovs-vsctl add-br br-phys
+ovn_attach n1 br-phys 192.168.0.11
+sim_add hv2
+as hv2
+check ovs-vsctl add-br br-phys
+ovn_attach n1 br-phys 192.168.0.12
+
+AT_CAPTURE_FILE([groups])
+get_lb_backends() {
+    as $1 ovs-ofctl dump-groups br-int > groups || return 1
+    tr ')' '\n' < groups | sed -n 's/.*nat(dst=//p' | sort
+}
+
+# A client on each chassis, the backends on hv2.  hv1 falls back to every
+# backend while hv2 keeps its local ones, and each follows the bindings as
+# backends move.
+check ovn-nbctl ls-add be
+check ovn-nbctl lsp-add be be-client
+check ovn-nbctl lsp-set-addresses be-client "00:00:00:00:01:00 192.168.1.5"
+check ovn-nbctl lsp-add be be-client2
+check ovn-nbctl lsp-set-addresses be-client2 "00:00:00:00:01:05 192.168.1.6"
+check ovn-nbctl lsp-add be be-vm1
+check ovn-nbctl lsp-set-addresses be-vm1 "00:00:00:00:01:01 192.168.1.10"
+check ovn-nbctl lsp-add be be-vm2
+check ovn-nbctl lsp-set-addresses be-vm2 "00:00:00:00:01:02 192.168.1.11"
+check ovn-nbctl \
+    -- lb-add lb0 172.16.1.10:80 192.168.1.10:80,192.168.1.11:80 \
+    -- set Load_Balancer lb0 options:distributed=true \
+        ip_port_mappings:192.168.1.10="be-vm1" \
+        ip_port_mappings:192.168.1.11="be-vm2"
+check ovn-nbctl ls-lb-add be lb0
+check ovn-nbctl --wait=hv sync
+
+as hv1 check ovs-vsctl add-port br-int vif0 \
+    -- set Interface vif0 external_ids:iface-id=be-client
+as hv2 check ovs-vsctl add-port br-int vif3 \
+    -- set Interface vif3 external_ids:iface-id=be-client2
+as hv2 check ovs-vsctl add-port br-int vif1 \
+    -- set Interface vif1 external_ids:iface-id=be-vm1
+as hv2 check ovs-vsctl add-port br-int vif2 \
+    -- set Interface vif2 external_ids:iface-id=be-vm2
+wait_for_ports_up
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv1], [0], [dnl
+192.168.1.10:80
+192.168.1.11:80
+])
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv2], [0], [dnl
+192.168.1.10:80
+192.168.1.11:80
+])
+
+# One backend leaves hv2: hv2 keeps the other, hv1 still has both.
+as hv2 check ovs-vsctl del-port br-int vif2
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm2 up) = xfalse])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv2], [0], [dnl
+192.168.1.10:80
+])
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv1], [0], [dnl
+192.168.1.10:80
+192.168.1.11:80
+])
+
+# The remaining backend moves to hv1: hv1 becomes local-only and hv2, left
+# with a client and no backend, falls back to every backend.
+as hv2 check ovs-vsctl del-port br-int vif1
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm1 up) = xfalse])
+as hv1 check ovs-vsctl add-port br-int vif1 \
+    -- set Interface vif1 external_ids:iface-id=be-vm1
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm1 up) = xtrue])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv1], [0], [dnl
+192.168.1.10:80
+])
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv2], [0], [dnl
+192.168.1.10:80
+192.168.1.11:80
+])
+
+# A backend bound on hv2 again gives hv2 a local-only group.
+as hv2 check ovs-vsctl add-port br-int vif2 \
+    -- set Interface vif2 external_ids:iface-id=be-vm2
+OVS_WAIT_UNTIL([test x$(ovn-nbctl get logical_switch_port be-vm2 up) = xtrue])
+check ovn-nbctl --wait=hv sync
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv2], [0], [dnl
+192.168.1.11:80
+])
+OVS_WAIT_FOR_OUTPUT([get_lb_backends hv1], [0], [dnl
+192.168.1.10:80
+])
+CHECK_AFTER_RECOMPUTE([hv1], [hv1])
+CHECK_AFTER_RECOMPUTE([hv2], [hv2])
+
+OVN_CLEANUP([hv1], [hv2])
+AT_CLEANUP
+])
diff --git a/tests/ovn-northd.at b/tests/ovn-northd.at
index 739808caa..828b6b4c0 100644
--- a/tests/ovn-northd.at
+++ b/tests/ovn-northd.at
@@ -24871,8 +24871,125 @@ AT_CHECK([cat lr1_lflows_after | grep lr_in_dnat | 
grep priority=120 | ovn_strip
   table=??(lr_in_dnat         ), priority=120  , match=(ct.new && !ct.rel && 
ip6 && ip6.dst == 2000::1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80 && 
is_chassis_resident("cr-lr1-up")), 
action=(ct_lb_mark(backends=[[2001:db8:abcd:1::2]]:10882);)
 ])
 
+# On a logical switch the distributed load balancer falls back to any
+# backend when the chassis has no local one.  A switch with a localnet port,
+# or any other port that is not bound to one chassis, keeps local-only
+# delivery.
+check ovn-nbctl ls-add vt
+check ovn-nbctl lsp-add vt vt-p -- lsp-set-type vt-p vtep
+check ovn-nbctl ls-add ts -- set logical_switch ts other_config:interconn-ts=ts
+check ovn-nbctl ls-lb-add ls1 lb1_ipv4
+check ovn-nbctl ls-lb-add vt lb1_ipv4
+check ovn-nbctl ls-lb-add ts lb1_ipv4
+check ovn-nbctl --wait=sb ls-lb-add outside lb1_ipv4
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(reg4 = 
172.31.0.1; reg2[[0..15]] = 80; 
ct_lb_mark_local(backends="lport1":192.168.0.101:10880,"lport2":192.168.0.102:10880;
 fallback);)
+])
+AT_CHECK([ovn-sbctl lflow-list outside | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(reg4 = 
172.31.0.1; reg2[[0..15]] = 80; 
ct_lb_mark_local(backends="lport1":192.168.0.101:10880,"lport2":192.168.0.102:10880);)
+])
+AT_CHECK([ovn-sbctl lflow-list vt | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(reg4 = 
172.31.0.1; reg2[[0..15]] = 80; 
ct_lb_mark_local(backends="lport1":192.168.0.101:10880,"lport2":192.168.0.102:10880);)
+])
+AT_CHECK([ovn-sbctl lflow-list ts | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(reg4 = 
172.31.0.1; reg2[[0..15]] = 80; 
ct_lb_mark_local(backends="lport1":192.168.0.101:10880,"lport2":192.168.0.102:10880);)
+])
+
+# A localnet port added to ls1 turns its flow strict, removing it turns it
+# back.  A remote port, which northd processes incrementally, does the
+# same.
+check ovn-nbctl --wait=sb lsp-add-localnet-port ls1 ls1-ln phnet
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [1], [0
+])
+check ovn-nbctl --wait=sb lsp-del ls1-ln
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [0], [1
+])
+check ovn-nbctl --wait=sb lsp-add ls1 ls1-remote -- lsp-set-type ls1-remote 
remote
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [1], [0
+])
+check ovn-nbctl --wait=sb lsp-del ls1-remote
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [0], [1
+])
+# The same on a switch without a load balancer is handled incrementally and
+# a load balancer attached afterwards gets the right form.
+check ovn-nbctl ls-add ls2 -- lsp-add ls2 ls2-p1 -- lsp-set-addresses ls2-p1 
"00:00:00:00:00:21 192.168.0.121"
+check ovn-nbctl --wait=sb sync
+check as northd ovn-appctl -t ovn-northd inc-engine/clear-stats
+check ovn-nbctl --wait=sb lsp-add ls2 ls2-remote -- lsp-set-type ls2-remote 
remote
+check_engine_stats northd norecompute compute
+check ovn-nbctl --wait=sb ls-lb-add ls2 lb1_ipv4
+AT_CHECK([ovn-sbctl lflow-list ls2 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [1], [0
+])
+# Detaching the load balancer recomputes on its own, so clear the stats
+# after it.
+check ovn-nbctl --wait=sb ls-lb-del ls2 lb1_ipv4
+check as northd ovn-appctl -t ovn-northd inc-engine/clear-stats
+check ovn-nbctl --wait=sb lsp-del ls2-remote
+check_engine_stats northd norecompute compute
+check ovn-nbctl --wait=sb ls-lb-add ls2 lb1_ipv4
+AT_CHECK([ovn-sbctl lflow-list ls2 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [0], [1
+])
+check ovn-nbctl --wait=sb ls-del ls2
+
+# The fallback set is the set northd already filtered by health, so a
+# backend whose monitor is offline is left out of it.  northd creates the
+# monitors offline, the controller would set them online.
+check_uuid ovn-nbctl --wait=sb -- --id=@hc create Load_Balancer_Health_Check \
+    vip="172.31.0.1\:80" -- add Load_Balancer lb1_ipv4 health_check @hc
+check_row_count sb:Service_Monitor 2
+mon1=$(fetch_column sb:Service_Monitor _uuid logical_port=lport1)
+mon2=$(fetch_column sb:Service_Monitor _uuid logical_port=lport2)
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(drop;)
+])
+check ovn-sbctl set Service_Monitor $mon1 status=online
+check ovn-nbctl --wait=sb sync
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(reg4 = 
172.31.0.1; reg2[[0..15]] = 80; 
ct_lb_mark_local(backends="lport1":192.168.0.101:10880; fallback);)
+])
+check ovn-sbctl set Service_Monitor $mon2 status=online
+check ovn-nbctl --wait=sb sync
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c '"lport1":192.168.0.101:10880,"lport2":192.168.0.102:10880; fallback'], [0], 
[1
+])
+check ovn-nbctl --wait=sb clear Load_Balancer lb1_ipv4 health_check
+check_row_count sb:Service_Monitor 0
+
+# Without a port mapping no backend is distributed, so the switch flow
+# drops and there is nothing to fall back to.
+check ovn-nbctl lb-add lb_nomap 172.31.0.2:80 
192.168.0.101:10880,192.168.0.102:10880
+check ovn-nbctl set Load_Balancer lb_nomap options:distributed=true
+check ovn-nbctl --wait=sb ls-lb-add ls1 lb_nomap
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep 172.31.0.2 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip4.dst == 
172.31.0.2 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(drop;)
+])
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+check ovn-nbctl --wait=sb lb-del lb_nomap
+check ovn-nbctl ls-lb-del ls1 lb1_ipv4 -- ls-lb-del outside lb1_ipv4 -- ls-del 
vt -- ls-del ts
+check ovn-nbctl --wait=sb sync
+
+# With install_ls_lb_from_router the switch inherits the load balancer of
+# lr1 without an attachment of its own, and a remote port added or removed
+# incrementally still changes the form of its flow.
+check ovn-nbctl --wait=sb set NB_Global . 
options:install_ls_lb_from_router=true
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [0], [1
+])
+check ovn-nbctl --wait=sb lsp-add ls1 ls1-remote -- lsp-set-type ls1-remote 
remote
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [1], [0
+])
+check ovn-nbctl --wait=sb lsp-del ls1-remote
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | grep 
-c fallback], [0], [1
+])
+check ovn-nbctl --wait=sb remove NB_Global . options install_ls_lb_from_router
+check ovn-nbctl --wait=sb sync
+
 check ovn-nbctl --wait=sb set load_balancer lb1_ipv6 options:distributed=true
 
+check ovn-nbctl --wait=sb ls-lb-add ls1 lb1_ipv6
+AT_CHECK([ovn-sbctl lflow-list ls1 | grep ls_in_lb | grep priority=120 | 
ovn_strip_lflows], [0], [dnl
+  table=??(ls_in_lb           ), priority=120  , match=(ct.new && ip6.dst == 
2000::1 && reg1[[16..23]] == 6 && reg1[[0..15]] == 80), action=(xxreg1 = 
2000::1; reg2[[0..15]] = 80; 
ct_lb_mark_local(backends="lport1":[[2001:db8:abcd:1::2]]:10882; fallback);)
+])
+check ovn-nbctl --wait=sb ls-lb-del ls1 lb1_ipv6
+
 ovn-sbctl lflow-list outside > outside_lflows_after
 ovn-sbctl lflow-list lr1 > lr1_lflows_after
 
diff --git a/tests/ovn.at b/tests/ovn.at
index 6b191624c..d57d9f2b9 100644
--- a/tests/ovn.at
+++ b/tests/ovn.at
@@ -2424,6 +2424,42 @@ ct_lb_mark_local(backends="lsp1":[[fd0f::2]]:80);
     uses group: id(23), 
name(type=select,selection_method=dp_hash,bucket=bucket_id=0,weight:100,actions=ct(nat(dst=[[fd0f::2]]:80),commit,table=oflow_in_table,zone=NXM_NX_REG13[[0..15]],exec(set_field:2/2->ct_mark)))
     has prereqs ip
 
+# A backend whose port is not local gets no bucket, the action encodes
+# to nothing without a fallback and to the full set with one.
+ct_lb_mark_local(backends="lsp2":192.168.0.102:80);
+    encodes as drop
+    has prereqs ip
+
+ct_lb_mark_local(backends="lsp2":192.168.0.102:80; fallback);
+    encodes as group:24
+    uses group: id(24), 
name(type=select,selection_method=dp_hash,bucket=bucket_id=0,weight:100,actions=ct(nat(dst=192.168.0.102:80),commit,table=oflow_in_table,zone=NXM_NX_REG13[[0..15]],exec(set_field:2/2->ct_mark)))
+    has prereqs ip
+
+ct_lb_mark_local(backends="lsp1":192.168.0.101:80,"lsp2":192.168.0.102:80; 
fallback);
+    encodes as group:25
+    uses group: id(25), 
name(type=select,selection_method=dp_hash,bucket=bucket_id=0,weight:100,actions=ct(nat(dst=192.168.0.101:80),commit,table=oflow_in_table,zone=NXM_NX_REG13[[0..15]],exec(set_field:2/2->ct_mark)))
+    has prereqs ip
+
+ct_lb_mark_local(backends="lsp2":192.168.0.102:80; 
hash_fields="ip_src,ip_dst"; skip_snat; fallback);
+    encodes as group:26
+    uses group: id(26), 
name(type=select,selection_method=hash,fields(ip_src,ip_dst),bucket=bucket_id=0,weight:100,actions=ct(nat(dst=192.168.0.102:80),commit,table=oflow_in_table,zone=NXM_NX_REG13[[0..15]],exec(set_field:2/2->ct_mark,set_field:4/4->ct_mark)))
+    has prereqs ip
+
+ct_lb_mark(backends=192.168.0.102:80; fallback);
+    Syntax error at `fallback' expecting `;'.
+
+ct_lb_mark_local(backends="lsp2":192.168.0.102:80; 
hash_fields="ip_src,ip_dst"; fallback);
+    encodes as group:27
+    uses group: id(27), 
name(type=select,selection_method=hash,fields(ip_src,ip_dst),bucket=bucket_id=0,weight:100,actions=ct(nat(dst=192.168.0.102:80),commit,table=oflow_in_table,zone=NXM_NX_REG13[[0..15]],exec(set_field:2/2->ct_mark)))
+    has prereqs ip
+
+# The keyword comes last and only once.
+ct_lb_mark_local(backends="lsp2":192.168.0.102:80; fallback; skip_snat);
+    Syntax error at `skip_snat' expecting `;'.
+
+ct_lb_mark_local(backends="lsp2":192.168.0.102:80; fallback; fallback);
+    Syntax error at `fallback' expecting `;'.
+
 # Miscellaneous negative tests.
 ;
     Syntax error at `;'.
@@ -48872,3 +48908,46 @@ AT_CHECK([ovs-ofctl dump-flows br-int 
table=OFTABLE_PHY_TO_LOG \
 OVN_CLEANUP([hv1])
 AT_CLEANUP
 ])
+
+OVN_FOR_EACH_NORTHD_NO_HV([
+AT_SETUP([ovn-trace - distributed LB ingress from a client and a router port])
+AT_KEYWORDS([ovn-trace])
+ovn_start
+
+# The load balancer is attached to the switch only.  The same VIP packet
+# enters once from a logical switch port, which the switch load balances,
+# and once from the logical router port, which the pre-load-balancer
+# stage skips conntrack for, so the switch never load balances it.
+check ovn-nbctl lr-add lr0
+check ovn-nbctl lrp-add lr0 lr0-be 00:00:00:00:00:01 172.31.0.254/24
+check ovn-nbctl ls-add be
+check ovn-nbctl lsp-add-router-port be be-lr0 lr0-be
+check ovn-nbctl lsp-add be be-client
+check ovn-nbctl lsp-set-addresses be-client "00:00:00:00:01:10 172.31.0.100"
+check ovn-nbctl lsp-add be be-vm1
+check ovn-nbctl lsp-set-addresses be-vm1 "00:00:00:00:01:01 172.31.0.10"
+check ovn-nbctl lsp-add be be-vm2
+check ovn-nbctl lsp-set-addresses be-vm2 "00:00:00:00:01:02 172.31.0.11"
+check ovn-nbctl lb-add lb0 172.31.0.1:80 172.31.0.10:80,172.31.0.11:80
+check ovn-nbctl set Load_Balancer lb0 options:distributed=true \
+    ip_port_mappings:172.31.0.10=be-vm1 \
+    ip_port_mappings:172.31.0.11=be-vm2
+check ovn-nbctl --wait=sb ls-lb-add be lb0
+
+AT_CHECK([ovn-trace be 'inport == "be-client" && eth.src == 00:00:00:00:01:10 
&& eth.dst == 00:00:00:00:01:01 && ip4.src == 172.31.0.100 && ip4.dst == 
172.31.0.1 && tcp && tcp.src == 40000 && tcp.dst == 80' > client.out], [0], 
[ignore])
+AT_CHECK([ovn-trace be 'inport == "be-lr0" && eth.src == 00:00:00:00:00:01 && 
eth.dst == 00:00:00:00:01:01 && ip4.src == 172.31.0.100 && ip4.dst == 
172.31.0.1 && tcp && tcp.src == 40000 && tcp.dst == 80' > router.out], [0], 
[ignore])
+
+# The same packet from a logical switch port is load balanced: the
+# pre-stateful stage sends it to conntrack and commits the load
+# balancing for the VIP, and the connection is tracked from then on.
+AT_CHECK([grep 'ip4.dst == 172.31.0.1 && tcp.dst == 80' client.out], [0], 
[ignore])
+AT_CHECK([grep 'ls_in_acl_hint.*!ct.new && ct.est' client.out], [0], [ignore])
+# The packet from the router port is not: the pre-load-balancer stage
+# skips conntrack for the router ports, the load balancing flow cannot
+# match, and the packet stays untracked.
+AT_CHECK([! grep -q 'ip4.dst == 172.31.0.1 && tcp.dst == 80' router.out])
+AT_CHECK([grep 'ls_in_acl_hint.*!ct.trk' router.out], [0], [ignore])
+AT_CHECK([grep 'ls_in_pre_lb.*ip && inport == "be-lr0", priority 110' 
router.out], [0], [ignore])
+OVN_CLEANUP_NORTHD
+AT_CLEANUP
+])
base-commit: 2cf772348fc572c68e9bd70652f3b0ece267a9cd
-- 
2.53.0

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

Reply via email to