A redirect policy is similar to a reroute policy, except that an output
port is required, and no nexthop is specified.

Whatever nexthop IP address is determined in the IP routing stage of the
ingress pipeline of the logical router is maintained, but the packet is
redirected out the output port of the redirect policy.

Signed-off-by: Mark Michelson <[email protected]>
---
 northd/en-route-policies.c | 32 +++++++++++++-
 northd/en-route-policies.h |  4 ++
 northd/northd.c            | 63 +++++++++++++++++++++++---
 northd/northd.h            |  3 ++
 ovn-nb.ovsschema           |  7 +--
 ovn-nb.xml                 | 27 +++++++++---
 tests/ovn-nbctl.at         | 26 ++++++++++-
 tests/ovn-northd.at        | 90 ++++++++++++++++++++++++++++++++++++++
 utilities/ovn-nbctl.c      | 45 +++++++++++--------
 9 files changed, 258 insertions(+), 39 deletions(-)

diff --git a/northd/en-route-policies.c b/northd/en-route-policies.c
index 3bb658cb8..ccdf7ded0 100644
--- a/northd/en-route-policies.c
+++ b/northd/en-route-policies.c
@@ -188,6 +188,7 @@ build_route_policies(struct ovn_datapath *od,
 
         uint32_t chain_id = 0;
         uint32_t jump_chain_id = 0;
+        const char *redirect_port_key = NULL;
 
         /* Skip policy if chain name is set but id was not created above */
         if (policy_chain_id(chain_ids, rule->chain, &chain_id)
@@ -195,6 +196,26 @@ build_route_policies(struct ovn_datapath *od,
             continue;
         }
 
+        if (!strcmp(rule->action, "redirect")) {
+            if (!rule->output_port) {
+                static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
+                VLOG_WARN_RL(&rl, "Router policies of type 'redirect' must "
+                                  "specify an output_port");
+                continue;
+            }
+            /* Verify that the port belongs to the router that the chain is
+             * attached to.
+             */
+            if (!ovn_port_find_in_datapath(od, rule->output_port->name)) {
+                static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
+                VLOG_WARN_RL(&rl, "Router policy output_port '%s' does not 
belong "
+                                  "to attached Logical Router '%s'",
+                             rule->output_port->name, od->nbr->name);
+                continue;
+            }
+            redirect_port_key = rule->output_port->name;
+        }
+
         if (!strcmp(rule->action, "jump")) {
             /* Skip policy if action is 'jump' but no target chain is set */
             if (!policy_chain_id(chain_ids, rule->jump_chain,
@@ -329,6 +350,7 @@ build_route_policies(struct ovn_datapath *od,
             .chain_id = chain_id,
             .jump_chain_id = jump_chain_id,
             .ecmp_group_id = ecmp_group_id,
+            .redirect_port_key = redirect_port_key,
         };
         hmap_insert(route_policies, &new_rp->key_node, hash);
     }
@@ -580,17 +602,23 @@ static bool
 logical_router_ports_updated(const struct nbrec_logical_router *lr)
 {
     /* We only care if ports change if the lr has policies with a
-     * "reroute" action. Otherwise, we don't care about the ports.
+     * "reroute" or "redirect" action. Otherwise, we don't care about
+     * the ports.
      */
     bool has_reroute = false;
+    bool has_redirect = false;
     for (size_t i = 0; i < lr->n_policies; i++) {
         const struct nbrec_logical_router_policy *rule = lr->policies[i];
         if (!strcmp(rule->action, "reroute")) {
             has_reroute = true;
             break;
         }
+        if (!strcmp(rule->action, "redirect")) {
+            has_redirect = true;
+            break;
+        }
     }
-    if (!has_reroute) {
+    if (!has_reroute && !has_redirect) {
         return false;
     }
 
diff --git a/northd/en-route-policies.h b/northd/en-route-policies.h
index 347227803..ed88c4188 100644
--- a/northd/en-route-policies.h
+++ b/northd/en-route-policies.h
@@ -52,6 +52,10 @@ struct route_policy {
      * If the policy is not ECMP, then this is 0.
      */
     uint32_t ecmp_group_id;
+    /* For policies with "redirect" action, this is the key for the
+     * port where the matching packet should be sent out.
+     */
+    const char *redirect_port_key;
 };
 
 struct datapath_route_policies {
diff --git a/northd/northd.c b/northd/northd.c
index f0fdead6a..13b043371 100644
--- a/northd/northd.c
+++ b/northd/northd.c
@@ -4759,7 +4759,7 @@ ls_port_has_changed(const struct 
nbrec_logical_switch_port *new)
                                 OVSDB_IDL_CHANGE_MODIFY) > 0);
 }
 
-static struct ovn_port *
+struct ovn_port *
 ovn_port_find_in_datapath(const struct ovn_datapath *od, const char *name)
 {
     struct ovn_port *op;
@@ -12368,6 +12368,13 @@ build_routing_policy_flow(struct lflow_table *lflows, 
struct ovn_datapath *od,
     struct ds match = DS_EMPTY_INITIALIZER;
     struct ds actions = DS_EMPTY_INITIALIZER;
 
+    if (rp->chain_id == -1) {
+        ds_put_format(&match, "%s", rule->match);
+    } else {
+        ds_put_format(&match, REG_POLICY_CHAIN_ID" == %d && (%s)",
+                      rp->chain_id, rule->match);
+    }
+
     if (!strcmp(rule->action, "reroute")) {
         ovs_assert(vector_len(&rp->valid_nexthops) <= 1);
 
@@ -12420,13 +12427,55 @@ build_routing_policy_flow(struct lflow_table *lflows, 
struct ovn_datapath *od,
             ds_put_format(&actions, "pkt.mark = %u; ", pkt_mark);
         }
         ds_put_cstr(&actions, REG_ECMP_GROUP_ID" = 0; next;");
-    }
+    } else if (!strcmp(rule->action, "redirect")) {
+        ovs_assert(rp->redirect_port_key);
+        struct ovn_port *out_port;
+        out_port = ovn_port_find_in_datapath(od, rp->redirect_port_key);
+        if (!out_port) {
+            return;
+        }
 
-    if (rp->chain_id == -1) {
-        ds_put_format(&match, "%s", rule->match);
-    } else {
-        ds_put_format(&match, REG_POLICY_CHAIN_ID" == %d && (%s)",
-                      rp->chain_id, rule->match);
+        size_t match_len = match.length;
+
+        ds_put_format(&actions, "eth.src = %s; "
+                                "outport = %s; "
+                                "flags.loopback = 1; ",
+                      out_port->lrp_networks.ea_s, out_port->json_key);
+        size_t actions_len = actions.length;
+
+        if (out_port->lrp_networks.n_ipv4_addrs) {
+            /* Use the first IPv4 address available. We assume this is capable
+             * of reaching the nexthop.
+             */
+            ds_put_cstr(&match, " && "REGBIT_NEXTHOP_IS_IPV4" == 1");
+            ds_put_format(&actions, REG_SRC_IPV4 " = %s; next;",
+                          out_port->lrp_networks.ipv4_addrs[0].addr_s);
+            ovn_lflow_add(lflows, od, S_ROUTER_IN_POLICY, rule->priority,
+                          ds_cstr(&match), ds_cstr(&actions),
+                          lflow_ref, WITH_HINT(stage_hint));
+        }
+        ds_truncate(&match, match_len);
+        ds_truncate(&actions, actions_len);
+        if (out_port->lrp_networks.n_ipv6_addrs) {
+            /* Use the first non-link-local IPv6 address available. We assume
+             * this is capable of reaching the nexthop.
+             */
+            for (size_t i = 0; i < out_port->lrp_networks.n_ipv6_addrs; i++) {
+                if (in6_is_lla(&out_port->lrp_networks.ipv6_addrs[i].addr)) {
+                    continue;
+                }
+                ds_put_cstr(&match, " && "REGBIT_NEXTHOP_IS_IPV4" == 0");
+                ds_put_format(&actions, REG_SRC_IPV6 " = %s; next;",
+                              out_port->lrp_networks.ipv6_addrs[0].addr_s);
+                ovn_lflow_add(lflows, od, S_ROUTER_IN_POLICY, rule->priority,
+                              ds_cstr(&match), ds_cstr(&actions),
+                              lflow_ref, WITH_HINT(stage_hint));
+                break;
+            }
+        }
+        ds_destroy(&match);
+        ds_destroy(&actions);
+        return;
     }
 
     ovn_lflow_add(lflows, od, S_ROUTER_IN_POLICY, rule->priority,
diff --git a/northd/northd.h b/northd/northd.h
index 8c9b76446..f2cbd72f6 100644
--- a/northd/northd.h
+++ b/northd/northd.h
@@ -1242,4 +1242,7 @@ struct ovn_port_routable_addresses get_op_addresses(
 
 void destroy_routable_addresses(struct ovn_port_routable_addresses *ra);
 
+struct ovn_port *ovn_port_find_in_datapath(const struct ovn_datapath *,
+                                           const char *);
+
 #endif /* NORTHD_H */
diff --git a/ovn-nb.ovsschema b/ovn-nb.ovsschema
index a29245115..45f84c03c 100644
--- a/ovn-nb.ovsschema
+++ b/ovn-nb.ovsschema
@@ -1,7 +1,7 @@
 {
     "name": "OVN_Northbound",
-    "version": "7.19.0",
-    "cksum": "2631744256 45474",
+    "version": "7.19.1",
+    "cksum": "3930301753 45523",
     "tables": {
         "NB_Global": {
             "columns": {
@@ -651,7 +651,8 @@
                 "action": {"type": {
                     "key": {"type": "string",
                             "enum": ["set",
-                                    ["allow", "drop", "reroute", "jump"]]}}},
+                                    ["allow", "drop", "reroute", "jump",
+                                     "redirect"]]}}},
                 "jump_chain": {"type": {"key": "string", "min": 0, "max": 1}},
                 "nexthop": {"type": {"key": "string", "min": 0, "max": 1}},
                 "nexthops": {"type": {
diff --git a/ovn-nb.xml b/ovn-nb.xml
index 8b74ed84e..f8883602c 100644
--- a/ovn-nb.xml
+++ b/ovn-nb.xml
@@ -5521,6 +5521,12 @@ or
           <ref column="chain"/> value as specified in
           <ref column="jump_chain"/>.
         </li>
+
+        <li>
+          <code>redirect</code>: Redirect the packet out a specific output
+          <ref table="Logical_Router_Port"/>. This type of policy does not
+          alter the nexthop IP address.
+        </li>
       </ul>
     </column>
 
@@ -5556,16 +5562,23 @@ or
     <column name="output_port">
       <p>
         The <ref table="Logical_Router_Port"/> via which the packet
-        needs to be sent out.  This is optional and when not specified,
-        OVN will automatically figure this out based on the
-        <ref column="nexthops"/> column.  When this is specified and there are
-        multiple IP addresses on the router port and none of them are in the
-        same subnet of <ref column="nexthops"/>, OVN chooses the first IP
-        address as the one via which the <ref column="nexthops"/> are
+        needs to be sent out.
+
+        For policies whose action is <code>reroute</code>, this is optional
+        and when not specified, OVN will automatically figure this out based
+        on the <ref column="nexthops"/> column.  When this is specified and
+        there are multiple IP addresses on the router port and none of them
+        are in the same subnet of <ref column="nexthops"/>, OVN chooses the
+        first IP address as the one via which the <ref column="nexthops"/> are
         reachable.
 
+        For policies whose action is <code>redirect</code>, this value is
+        required. OVN will send the packet out the configured
+        <code>output_port</code> without altering the nexthop IP address
+        that was determined during the IP routing stage.
+
         NOTE: for now OVN does not support configuring the output port on
-        ECMP reroute policies (with more than one value in the
+        ECMP <code>reroute</code> policies (with more than one value in the
         <ref column="nexthops"/> column).
       </p>
     </column>
diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index d3a7619b8..f97af79c1 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -2517,7 +2517,7 @@ AT_CHECK([ovn-nbctl lr-policy-add lr0 100 "ip4.src == 
2.1.1.0/24" jump], [1], []
 ])
 
 AT_CHECK([ovn-nbctl --output-port=lrp0 lr-policy-add lr0 101 "ip4.src == 
3.1.1.0/24" drop], [1], [],
-  [ovn-nbctl: Specifying output-port is only allowed for reroute policies.
+  [ovn-nbctl: Specifying output-port is only allowed for reroute and redirect 
policies.
 ])
 
 AT_CHECK([ovn-nbctl --output-port=lrp0 lr-policy-add lr0 101 "ip4.src == 
3.1.1.0/24" reroute 192.168.0.10,192.168.0.11], [1], [],
@@ -2540,7 +2540,7 @@ AT_CHECK([ovn-nbctl --may-exist lr-policy-add lr0 100 
"ip4.src == 1.1.1.0/24" dr
 
 dnl Add duplicated policy
 AT_CHECK([ovn-nbctl lr-policy-add lr0 103 "ip4.src == 1.1.1.0/24" deny], [1], 
[],
-  [ovn-nbctl: deny: action must be one of "allow", "drop", "reroute", "jump"
+  [ovn-nbctl: deny: action must be one of "allow", "drop", "reroute", "jump", 
"redirect"
 ])
 
 dnl Delete by priority and match string
@@ -3797,3 +3797,25 @@ check ovn-nbctl lr-policy-del lr0 100
 # Double check it was actually deleted.
 wait_row_count nb:Logical_Router_Policy 0
 ])
+
+OVN_NBCTL_TEST([route_policy_redirect], [Router policy with redirect action], [
+check ovn-nbctl lr-add lr
+check ovn-nbctl lrp-add lr lrp 00:00:00:00:00:01 192.168.0.1/24 fd01::1/64
+
+# Base test. Add a redirect router policy and specify a valid port.
+check ovn-nbctl --output-port=lrp lr-policy-add lr 100 "ip4" redirect
+
+# Ensure the policy was inserted
+check_row_count nb:Logical_Router_Policy 1
+
+# Try adding a redirect policy without a port.
+AT_CHECK([ovn-nbctl lr-policy-add lr 101 "ip4" redirect], [1], [], [dnl
+ovn-nbctl: An output-port must be specified for redirect policies
+])
+check_row_count nb:Logical_Router_Policy 1
+
+# Try adding a redirect policy with a non-existent port.
+AT_CHECK([ovn-nbctl --output-port=lrp0 lr-policy-add lr 101 "ip4" redirect], 
[1], [], [dnl
+ovn-nbctl: unknown output-port: lrp0
+])
+])
diff --git a/tests/ovn-northd.at b/tests/ovn-northd.at
index b459b3a7b..1dcc2971f 100644
--- a/tests/ovn-northd.at
+++ b/tests/ovn-northd.at
@@ -26045,6 +26045,30 @@ check_engine_stats bfd_sync recompute nocompute
 check_engine_stats lflow recompute nocompute
 CHECK_NO_CHANGE_AFTER_RECOMPUTE
 
+# Add a redirect policy to lr1. This should be handled incrementally.
+check as northd ovn-appctl -t ovn-northd inc-engine/clear-stats
+check ovn-nbctl --wait=sb --output-port=newer_port lr-policy-add lr1 102 "ip4" 
redirect
+
+# Like with any other type of router policy, this should be handled 
incrementally.
+check_engine_stats northd norecompute compute
+check_engine_stats route_policies norecompute compute
+check_engine_stats bfd_sync recompute nocompute
+check_engine_stats lflow norecompute compute
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
+# Add another port to lr1 just to ensure that with the redirect policy,
+# we still see the same results as before with the reroute policy.
+check as northd ovn-appctl -t ovn-northd inc-engine/clear-stats
+check ovn-nbctl --wait=sb lrp-add lr1 newest_port 00:00:00:00:00:07 40.0.0.1/24
+
+check_engine_stats northd recompute compute
+check_engine_stats route_policies norecompute compute
+# bfd_sync has to recompute because northd recomputed.
+# Since bfd_sync recomputes, so does lflow.
+check_engine_stats bfd_sync recompute nocompute
+check_engine_stats lflow recompute nocompute
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
 # Delete the logical router that has policies on it.
 # This should also not require a recompute in route_policies
 check as northd ovn-appctl -t ovn-northd inc-engine/clear-stats
@@ -26356,3 +26380,69 @@ AT_CHECK([grep "lr_out_undnat" lr0flows | 
ovn_strip_lflows | sort], [0], [dnl
 OVN_CLEANUP_NORTHD
 AT_CLEANUP
 ])
+
+OVN_FOR_EACH_NORTHD_NO_HV([
+AT_SETUP([Router policy with redirect action])
+ovn_start
+
+check ovn-nbctl lr-add lr
+# Add three logical router ports. One is IPv4 only. One is IPv6 only.
+# And one has addresses with both families.
+check ovn-nbctl lrp-add lr lrp-v4 00:00:00:00:00:01 192.168.0.1/24
+check ovn-nbctl lrp-add lr lrp-v6 00:00:00:00:01:01 fd01::1/64
+check ovn-nbctl lrp-add lr lrp-mixed 00:00:00:00:02:01 192.168.1.1/24 
fd02::1/64
+
+# Add a redirect policy for lrp-v4
+check ovn-nbctl --wait=sb --output-port=lrp-v4 lr-policy-add lr 100 "ip4" 
redirect
+
+# Make sure that we see one policy flow added.
+AT_CHECK([ovn-sbctl lflow-list lr | grep "lr_in_policy" | grep "priority=100" 
| ovn_strip_lflows], [0], [dnl
+  table=??(lr_in_policy       ), priority=100  , match=(ip4 && reg9[[9]] == 
1), action=(eth.src = 00:00:00:00:00:01; outport = "lrp-v4"; flags.loopback = 
1; reg5 = 192.168.0.1; next;)
+])
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
+# Remove the policy for lrp-v4
+check ovn-nbctl --wait=sb lr-policy-del lr 100
+
+# Make sure the policy flow is no longer there.
+AT_CHECK([ovn-sbctl lflow-list lr | grep "lr_in_policy" | grep "priority=100" 
| ovn_strip_lflows], [0], [dnl
+])
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
+# Add a redirect policy for lrp-v6
+check ovn-nbctl --wait=sb --output-port=lrp-v6 lr-policy-add lr 100 "ip6" 
redirect
+
+# Make sure that we see one policy flow added.
+AT_CHECK([ovn-sbctl lflow-list lr | grep "lr_in_policy" | grep "priority=100" 
| ovn_strip_lflows], [0], [dnl
+  table=??(lr_in_policy       ), priority=100  , match=(ip6 && reg9[[9]] == 
0), action=(eth.src = 00:00:00:00:01:01; outport = "lrp-v6"; flags.loopback = 
1; xxreg1 = fd01::1; next;)
+])
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
+# Remove the policy for lrp-v6
+check ovn-nbctl --wait=sb lr-policy-del lr 100
+
+# Make sure the policy flow is no longer there.
+AT_CHECK([ovn-sbctl lflow-list lr | grep "lr_in_policy" | grep "priority=100" 
| ovn_strip_lflows], [0], [dnl
+])
+
+# Add redirect policy for lrp-mixed
+check ovn-nbctl --wait=sb --output-port=lrp-mixed lr-policy-add lr 100 "ip" 
redirect
+
+# Make sure that we see two policy flows added, since we support both address 
families.
+AT_CHECK([ovn-sbctl lflow-list lr | grep "lr_in_policy" | grep "priority=100" 
| ovn_strip_lflows], [0], [dnl
+  table=??(lr_in_policy       ), priority=100  , match=(ip && reg9[[9]] == 0), 
action=(eth.src = 00:00:00:00:02:01; outport = "lrp-mixed"; flags.loopback = 1; 
xxreg1 = fd02::1; next;)
+  table=??(lr_in_policy       ), priority=100  , match=(ip && reg9[[9]] == 1), 
action=(eth.src = 00:00:00:00:02:01; outport = "lrp-mixed"; flags.loopback = 1; 
reg5 = 192.168.1.1; next;)
+])
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
+# Remove the policy for lrp-mixed
+check ovn-nbctl --wait=sb lr-policy-del lr 100
+
+# Make sure the policy flows are no longer there.
+AT_CHECK([ovn-sbctl lflow-list lr | grep "lr_in_policy" | grep "priority=100" 
| ovn_strip_lflows], [0], [dnl
+])
+CHECK_NO_CHANGE_AFTER_RECOMPUTE
+
+OVN_CLEANUP_NORTHD
+AT_CLEANUP
+])
diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 6f918f55d..b5defafa4 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -4676,9 +4676,10 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
 
     /* Validate action. */
     if (strcmp(action, "allow") && strcmp(action, "drop")
-        && strcmp(action, "jump") && strcmp(action, "reroute")) {
+        && strcmp(action, "jump") && strcmp(action, "reroute")
+        && strcmp(action, "redirect")) {
         ctl_error(ctx, "%s: action must be one of \"allow\", \"drop\", "
-                  "\"reroute\", \"jump\"", action);
+                  "\"reroute\", \"jump\", \"redirect\"", action);
         return;
     }
     if (!strcmp(action, "reroute")) {
@@ -4687,9 +4688,16 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
             return;
         }
         reroute = true;
-    } else if (output_port) {
-        ctl_error(ctx, "Specifying output-port is only allowed for "
-                       "reroute policies.");
+    }
+    if (output_port) {
+        if (strcmp(action, "reroute") && strcmp(action, "redirect")) {
+            ctl_error(ctx, "Specifying output-port is only allowed for "
+                           "reroute and redirect policies.");
+            return;
+        }
+    } else if (!strcmp(action, "redirect")) {
+        ctl_error(ctx, "An output-port must be specified for redirect "
+                       "policies");
         return;
     }
 
@@ -4751,19 +4759,19 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
 
         free(nexthops_arg);
 
-        if (output_port) {
-            if (vector_len(&nexthops) != 1) {
-                ctl_error(ctx, "output-port option is supported only for "
-                               "policies with a single reroute next hop");
-                goto free_nexthops;
-            }
+        if (output_port && vector_len(&nexthops) != 1) {
+            ctl_error(ctx, "output-port option is supported only for "
+                           "policies with a single reroute next hop");
+            goto free_nexthops;
+        }
+    }
 
-            error = lrp_by_name_or_uuid(ctx, output_port, true, &out_lrp);
-            if (error) {
-                ctl_error(ctx, "unknown output-port: %s", output_port);
-                free(error);
-                goto free_nexthops;
-            }
+    if (output_port) {
+        error = lrp_by_name_or_uuid(ctx, output_port, true, &out_lrp);
+        if (error) {
+            ctl_error(ctx, "unknown output-port: %s", output_port);
+            free(error);
+            goto free_nexthops;
         }
     }
 
@@ -4784,9 +4792,10 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
     if (reroute) {
         nbrec_logical_router_policy_set_nexthops(
             policy, vector_get_array(&nexthops), vector_len(&nexthops));
-        nbrec_logical_router_policy_set_output_port(policy, out_lrp);
     }
 
+    nbrec_logical_router_policy_set_output_port(policy, out_lrp);
+
     /* Parse the options. */
     struct smap options = SMAP_INITIALIZER(&options);
     for (i = (reroute || jump) ? 6 : 5; i < ctx->argc; i++) {
-- 
2.55.0

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

Reply via email to