5f9769
From 97b58dde0f92fc83165a6db816456073f5ddf727 Mon Sep 17 00:00:00 2001
5f9769
Message-Id: <97b58dde0f92fc83165a6db816456073f5ddf727.1612349784.git.lorenzo.bianconi@redhat.com>
5f9769
From: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
5f9769
Date: Fri, 29 Jan 2021 23:45:19 +0100
5f9769
Subject: [PATCH] ovn-nbctl: add --bfd option to lr-route-add
5f9769
5f9769
Introduce the --bfd option to lr-route-add command.
5f9769
If the BFD session UUID is provided, it will be used for the OVN route
5f9769
otherwise the next-hop will be used to perform a lookup in the OVN BFD
5f9769
table.
5f9769
If the lookup fails and outport is specified, a new entry in the BFD table
5f9769
will be created using the nexthop as dst_ip and outport as logical_port.
5f9769
5f9769
Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
5f9769
Signed-off-by: Numan Siddique <numans@ovn.org>
5f9769
---
5f9769
 tests/ovn-northd.at       | 17 ++++++++++----
5f9769
 tests/system-ovn.at       |  5 ++--
5f9769
 utilities/ovn-nbctl.8.xml | 11 +++++++++
5f9769
 utilities/ovn-nbctl.c     | 49 ++++++++++++++++++++++++++++++++++++++-
5f9769
 4 files changed, 73 insertions(+), 9 deletions(-)
5f9769
5f9769
--- a/tests/ovn-northd.at
5f9769
+++ b/tests/ovn-northd.at
5f9769
@@ -2342,7 +2342,7 @@ AT_KEYWORDS([northd-bfd])
5f9769
 ovn_start
5f9769
 
5f9769
 check ovn-nbctl --wait=sb lr-add r0
5f9769
-for i in $(seq 1 4); do
5f9769
+for i in $(seq 1 5); do
5f9769
     check ovn-nbctl --wait=sb lrp-add r0 r0-sw$i 00:00:00:00:00:0$i 192.168.$i.1/24
5f9769
     check ovn-nbctl --wait=sb ls-add sw$i
5f9769
     check ovn-nbctl --wait=sb lsp-add sw$i sw$i-r0
5f9769
@@ -2387,17 +2387,24 @@ check_column 1000 bfd min_tx logical_por
5f9769
 check_column 1000 bfd min_rx logical_port=r0-sw1
5f9769
 check_column 100 bfd detect_mult logical_port=r0-sw1
5f9769
 
5f9769
-check ovn-nbctl lr-route-add r0 100.0.0.0/8 192.168.10.2
5f9769
-route_uuid=$(fetch_column nb:logical_router_static_route _uuid ip_prefix="100.0.0.0/8")
5f9769
-check ovn-nbctl set logical_router_static_route $route_uuid bfd=$uuid
5f9769
+check ovn-nbctl --bfd=$uuid lr-route-add r0 100.0.0.0/8 192.168.10.2
5f9769
 check_column down bfd status logical_port=r0-sw1
5f9769
 AT_CHECK([ovn-nbctl lr-route-list r0 | grep 192.168.10.2 | grep -q bfd],[0])
5f9769
 
5f9769
+check ovn-nbctl --bfd lr-route-add r0 200.0.0.0/8 192.168.20.2
5f9769
+check_column down bfd status logical_port=r0-sw2
5f9769
+AT_CHECK([ovn-nbctl lr-route-list r0 | grep 192.168.20.2 | grep -q bfd],[0])
5f9769
+
5f9769
+check ovn-nbctl --bfd lr-route-add r0 240.0.0.0/8 192.168.50.2 r0-sw5
5f9769
+check_column down bfd status logical_port=r0-sw5
5f9769
+AT_CHECK([ovn-nbctl lr-route-list r0 | grep 192.168.50.2 | grep -q bfd],[0])
5f9769
+
5f9769
+route_uuid=$(fetch_column nb:logical_router_static_route _uuid ip_prefix="100.0.0.0/8")
5f9769
 check ovn-nbctl clear logical_router_static_route $route_uuid bfd
5f9769
 check_column admin_down bfd status logical_port=r0-sw1
5f9769
 
5f9769
 ovn-nbctl destroy bfd $uuid
5f9769
-check_row_count bfd 2
5f9769
+check_row_count bfd 3
5f9769
 
5f9769
 AT_CLEANUP
5f9769
 
5f9769
--- a/tests/system-ovn.at
5f9769
+++ b/tests/system-ovn.at
5f9769
@@ -5606,10 +5606,9 @@ NS_CHECK_EXEC([server], [bfdd-control al
5f9769
 Allowing connections from 172.16.1.1
5f9769
 ])
5f9769
 
5f9769
-uuid=$(ovn-nbctl create bfd logical_port=rp-public dst_ip=172.16.1.50 min_tx=250 min_rx=250 detect_mult=10)
5f9769
-check ovn-nbctl lr-route-add R1 100.0.0.0/8 172.16.1.50
5f9769
+check ovn-nbctl --bfd lr-route-add R1 100.0.0.0/8 172.16.1.50 rp-public
5f9769
+uuid=$(fetch_column nb:bfd _uuid logical_port="rp-public")
5f9769
 route_uuid=$(fetch_column nb:logical_router_static_route _uuid ip_prefix="100.0.0.0/8")
5f9769
-check ovn-nbctl set logical_router_static_route $route_uuid bfd=$uuid
5f9769
 check ovn-nbctl --wait=hv sync
5f9769
 
5f9769
 wait_column "up" nb:bfd status logical_port=rp-public
5f9769
--- a/utilities/ovn-nbctl.8.xml
5f9769
+++ b/utilities/ovn-nbctl.8.xml
5f9769
@@ -659,6 +659,7 @@
5f9769
     
5f9769
       
[--may-exist] [--policy=POLICY]
5f9769
         [--ecmp] [--ecmp-symmetric-reply]
5f9769
+        [--bfd[=UUID]]
5f9769
         lr-route-add router
5f9769
         prefix nexthop [port]
5f9769
       
5f9769
@@ -696,6 +697,16 @@
5f9769
         

5f9769
 
5f9769
         

5f9769
+          --bfd option is used to link a BFD session to the
5f9769
+          OVN route. If the BFD session UUID is provided, it will be used
5f9769
+          for the OVN route otherwise the next-hop will be used to perform
5f9769
+          a lookup in the OVN BFD table.
5f9769
+          If the lookup fails and port is specified, a new entry
5f9769
+          in the BFD table will be created using the nexthop as
5f9769
+          dst_ip and port as logical_port.
5f9769
+        

5f9769
+
5f9769
+        

5f9769
           It is an error if a route with prefix and
5f9769
           POLICY already exists, unless --may-exist,
5f9769
           --ecmp, or --ecmp-symmetric-reply is
5f9769
--- a/utilities/ovn-nbctl.c
5f9769
+++ b/utilities/ovn-nbctl.c
5f9769
@@ -3957,6 +3957,29 @@ nbctl_lr_route_add(struct ctl_context *c
5f9769
         goto cleanup;
5f9769
     }
5f9769
 
5f9769
+    struct shash_node *bfd = shash_find(&ctx->options, "--bfd");
5f9769
+    const struct nbrec_bfd *nb_bt = NULL;
5f9769
+    if (bfd) {
5f9769
+        if (bfd->data) {
5f9769
+            struct uuid bfd_uuid;
5f9769
+            if (uuid_from_string(&bfd_uuid, bfd->data)) {
5f9769
+                nb_bt = nbrec_bfd_get_for_uuid(ctx->idl, &bfd_uuid);
5f9769
+            }
5f9769
+            if (!nb_bt) {
5f9769
+                ctl_error(ctx, "no entry found in the BFD table");
5f9769
+                goto cleanup;
5f9769
+            }
5f9769
+        } else {
5f9769
+            const struct nbrec_bfd *iter;
5f9769
+            NBREC_BFD_FOR_EACH (iter, ctx->idl) {
5f9769
+                if (!strcmp(iter->dst_ip, next_hop)) {
5f9769
+                    nb_bt = iter;
5f9769
+                    break;
5f9769
+                }
5f9769
+            }
5f9769
+        }
5f9769
+    }
5f9769
+
5f9769
     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
5f9769
     bool ecmp_symmetric_reply = shash_find(&ctx->options,
5f9769
                                            "--ecmp-symmetric-reply") != NULL;
5f9769
@@ -4011,6 +4034,18 @@ nbctl_lr_route_add(struct ctl_context *c
5f9769
             if (policy) {
5f9769
                  nbrec_logical_router_static_route_set_policy(route, policy);
5f9769
             }
5f9769
+            if (bfd) {
5f9769
+                if (!nb_bt) {
5f9769
+                    if (ctx->argc != 5) {
5f9769
+                        ctl_error(ctx, "insert entry in the BFD table failed");
5f9769
+                        goto cleanup;
5f9769
+                    }
5f9769
+                    nb_bt = nbrec_bfd_insert(ctx->txn);
5f9769
+                    nbrec_bfd_set_dst_ip(nb_bt, next_hop);
5f9769
+                    nbrec_bfd_set_logical_port(nb_bt, ctx->argv[4]);
5f9769
+                }
5f9769
+                nbrec_logical_router_static_route_set_bfd(route, nb_bt);
5f9769
+            }
5f9769
             free(rt_prefix);
5f9769
             goto cleanup;
5f9769
         }
5f9769
@@ -4035,6 +4070,18 @@ nbctl_lr_route_add(struct ctl_context *c
5f9769
     }
5f9769
 
5f9769
     nbrec_logical_router_update_static_routes_addvalue(lr, route);
5f9769
+    if (bfd) {
5f9769
+        if (!nb_bt) {
5f9769
+            if (ctx->argc != 5) {
5f9769
+                ctl_error(ctx, "insert entry in the BFD table failed");
5f9769
+                goto cleanup;
5f9769
+            }
5f9769
+            nb_bt = nbrec_bfd_insert(ctx->txn);
5f9769
+            nbrec_bfd_set_dst_ip(nb_bt, next_hop);
5f9769
+            nbrec_bfd_set_logical_port(nb_bt, ctx->argv[4]);
5f9769
+        }
5f9769
+        nbrec_logical_router_static_route_set_bfd(route, nb_bt);
5f9769
+    }
5f9769
 
5f9769
 cleanup:
5f9769
     free(next_hop);
5f9769
@@ -6548,7 +6595,7 @@ static const struct ctl_command_syntax n
5f9769
     /* logical router route commands. */
5f9769
     { "lr-route-add", 3, 4, "ROUTER PREFIX NEXTHOP [PORT]", NULL,
5f9769
       nbctl_lr_route_add, NULL, "--may-exist,--ecmp,--ecmp-symmetric-reply,"
5f9769
-      "--policy=", RW },
5f9769
+      "--policy=,--bfd?", RW },
5f9769
     { "lr-route-del", 1, 4, "ROUTER [PREFIX [NEXTHOP [PORT]]]", NULL,
5f9769
       nbctl_lr_route_del, NULL, "--if-exists,--policy=", RW },
5f9769
     { "lr-route-list", 1, 1, "ROUTER", NULL, nbctl_lr_route_list, NULL,