[ovs-dev] [PATCH v3 ovn 6/6] Cleanup `ovn-nbctl lr-route-add` IP normalization logic.

Mark Michelson mmichels at redhat.com
Wed Jun 24 16:12:11 UTC 2020


Use newer IP normalization routines to make things much cleaner.

Signed-off-by: Mark Michelson <mmichels at redhat.com>
---
 utilities/ovn-nbctl.c | 47 ++++++++++++++++---------------------------
 1 file changed, 17 insertions(+), 30 deletions(-)

diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 410596bcd..459cd5627 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -3815,7 +3815,7 @@ nbctl_lr_route_add(struct ctl_context *ctx)
         ctx->error = error;
         return;
     }
-    char *prefix, *next_hop;
+    char *prefix = NULL, *next_hop = NULL;
 
     const char *policy = shash_find_data(&ctx->options, "--policy");
     bool is_src_route = false;
@@ -3828,35 +3828,24 @@ nbctl_lr_route_add(struct ctl_context *ctx)
         }
     }
 
-    prefix = normalize_prefix_str(ctx->argv[2]);
+    bool v6_prefix = false;
+    prefix = normalize_ipv4_prefix_str(ctx->argv[2]);
+    if (!prefix) {
+        prefix = normalize_ipv6_prefix_str(ctx->argv[2]);
+        v6_prefix = true;
+    }
     if (!prefix) {
         ctl_error(ctx, "bad prefix argument: %s", ctx->argv[2]);
         return;
     }
 
-    next_hop = normalize_prefix_str(ctx->argv[3]);
+    next_hop = v6_prefix
+        ? normalize_ipv6_addr_str(ctx->argv[3])
+        : normalize_ipv4_addr_str(ctx->argv[3]);
     if (!next_hop) {
-        free(prefix);
-        ctl_error(ctx, "bad next hop argument: %s", ctx->argv[3]);
-        return;
-    }
-
-    if (strchr(prefix, '.')) {
-        ovs_be32 hop_ipv4;
-        if (!ip_parse(ctx->argv[3], &hop_ipv4)) {
-            free(prefix);
-            free(next_hop);
-            ctl_error(ctx, "bad IPv4 nexthop argument: %s", ctx->argv[3]);
-            return;
-        }
-    } else {
-        struct in6_addr hop_ipv6;
-        if (!ipv6_parse(ctx->argv[3], &hop_ipv6)) {
-            free(prefix);
-            free(next_hop);
-            ctl_error(ctx, "bad IPv6 nexthop argument: %s", ctx->argv[3]);
-            return;
-        }
+        ctl_error(ctx, "bad %s nexthop argument: %s",
+                  v6_prefix ? "IPv6" : "IPv4", ctx->argv[3]);
+        goto cleanup;
     }
 
     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
@@ -3892,10 +3881,8 @@ nbctl_lr_route_add(struct ctl_context *ctx)
             if (!may_exist) {
                 ctl_error(ctx, "duplicate prefix: %s (policy: %s)",
                           prefix, is_src_route ? "src-ip" : "dst-ip");
-                free(next_hop);
                 free(rt_prefix);
-                free(prefix);
-                return;
+                goto cleanup;
             }
 
             /* Update the next hop for an existing route. */
@@ -3912,9 +3899,7 @@ nbctl_lr_route_add(struct ctl_context *ctx)
                  nbrec_logical_router_static_route_set_policy(route, policy);
             }
             free(rt_prefix);
-            free(next_hop);
-            free(prefix);
-            return;
+            goto cleanup;
         }
     }
 
@@ -3938,6 +3923,8 @@ nbctl_lr_route_add(struct ctl_context *ctx)
     nbrec_logical_router_set_static_routes(lr, new_routes,
                                            lr->n_static_routes + 1);
     free(new_routes);
+
+cleanup:
     free(next_hop);
     free(prefix);
 }
-- 
2.25.4



More information about the dev mailing list