[ovs-dev] [PATCH ovn v2 1/3] Add IP address normalization to ovn-utils.

Mark Michelson mmichels at redhat.com
Thu Jul 23 19:36:49 UTC 2020


This moves a couple of existing IP address normalization routines from
ovn-nbctl.c to ovn-util.c. It also adds a new normalization function for
a v46_address. This new function is not used in this commit but will be
used in a future commit in this series.

Signed-off-by: Mark Michelson <mmichels at redhat.com>
---
 lib/ovn-util.c        | 39 +++++++++++++++++++++++++++++++++++++++
 lib/ovn-util.h        |  4 ++++
 utilities/ovn-nbctl.c | 29 -----------------------------
 3 files changed, 43 insertions(+), 29 deletions(-)

diff --git a/lib/ovn-util.c b/lib/ovn-util.c
index f09fdaffe..cdb5e18fb 100644
--- a/lib/ovn-util.c
+++ b/lib/ovn-util.c
@@ -589,6 +589,45 @@ ip46_equals(const struct v46_ip *addr1, const struct v46_ip *addr2)
              IN6_ARE_ADDR_EQUAL(&addr1->ipv6, &addr2->ipv6)));
 }
 
+/* The caller must free the returned string. */
+char *
+normalize_ipv4_prefix(ovs_be32 ipv4, unsigned int plen)
+{
+    ovs_be32 network = ipv4 & be32_prefix_mask(plen);
+    if (plen == 32) {
+        return xasprintf(IP_FMT, IP_ARGS(network));
+    } else {
+        return xasprintf(IP_FMT "/%d", IP_ARGS(network), plen);
+    }
+}
+
+/* The caller must free the returned string. */
+char *
+normalize_ipv6_prefix(struct in6_addr ipv6, unsigned int plen)
+{
+    char network_s[INET6_ADDRSTRLEN];
+
+    struct in6_addr mask = ipv6_create_mask(plen);
+    struct in6_addr network = ipv6_addr_bitand(&ipv6, &mask);
+
+    inet_ntop(AF_INET6, &network, network_s, INET6_ADDRSTRLEN);
+    if (plen == 128) {
+        return xasprintf("%s", network_s);
+    } else {
+        return xasprintf("%s/%d", network_s, plen);
+    }
+}
+
+char *
+normalize_v46_prefix(const struct v46_ip *prefix, unsigned int plen)
+{
+    if (prefix->family == AF_INET) {
+        return normalize_ipv4_prefix(prefix->ipv4, plen);
+    } else {
+        return normalize_ipv6_prefix(prefix->ipv6, plen);
+    }
+}
+
 char *
 str_tolower(const char *orig)
 {
diff --git a/lib/ovn-util.h b/lib/ovn-util.h
index 4e08ee01e..0f7b501f1 100644
--- a/lib/ovn-util.h
+++ b/lib/ovn-util.h
@@ -144,6 +144,10 @@ bool ip46_parse_cidr(const char *str, struct v46_ip *prefix,
                      unsigned int *plen);
 bool ip46_equals(const struct v46_ip *addr1, const struct v46_ip *addr2);
 
+char *normalize_ipv4_prefix(ovs_be32 ipv4, unsigned int plen);
+char *normalize_ipv6_prefix(struct in6_addr ipv6, unsigned int plen);
+char *normalize_v46_prefix(const struct v46_ip *prefix, unsigned int plen);
+
 /* Returns a lowercase copy of orig.
  * Caller must free the returned string.
  */
diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 7578b9928..0079ad5a6 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -3482,35 +3482,6 @@ nbctl_dhcp_options_list(struct ctl_context *ctx)
     free(nodes);
 }
 
-/* The caller must free the returned string. */
-static char *
-normalize_ipv4_prefix(ovs_be32 ipv4, unsigned int plen)
-{
-    ovs_be32 network = ipv4 & be32_prefix_mask(plen);
-    if (plen == 32) {
-        return xasprintf(IP_FMT, IP_ARGS(network));
-    } else {
-        return xasprintf(IP_FMT"/%d", IP_ARGS(network), plen);
-    }
-}
-
-/* The caller must free the returned string. */
-static char *
-normalize_ipv6_prefix(struct in6_addr ipv6, unsigned int plen)
-{
-    char network_s[INET6_ADDRSTRLEN];
-
-    struct in6_addr mask = ipv6_create_mask(plen);
-    struct in6_addr network = ipv6_addr_bitand(&ipv6, &mask);
-
-    inet_ntop(AF_INET6, &network, network_s, INET6_ADDRSTRLEN);
-    if (plen == 128) {
-        return xasprintf("%s", network_s);
-    } else {
-        return xasprintf("%s/%d", network_s, plen);
-    }
-}
-
 static char *
 normalize_ipv4_prefix_str(const char *orig_prefix)
 {
-- 
2.25.4



More information about the dev mailing list