ebb439
From 732b689c8ff0b4bb48f1e7b726bd113642440f8b Mon Sep 17 00:00:00 2001
ebb439
From: Justin Pettit <jpettit@ovn.org>
ebb439
Date: Wed, 28 Nov 2018 12:33:31 -0800
ebb439
Subject: [PATCH 02/10] northd: Move functions from ovn-northd.c into ovn-util.
ebb439
ebb439
The upcoming ddlog implementation of northd needs these functions too,
ebb439
so they should be in a common library.
ebb439
ebb439
Signed-off-by: Justin Pettit <jpettit@ovn.org>
ebb439
Signed-off-by: Ben Pfaff <blp@ovn.org>
ebb439
Acked-by: Numan Siddique <numans@ovn.org>
ebb439
---
ebb439
 lib/ovn-util.c      | 79 +++++++++++++++++++++++++++++++++++++++------
ebb439
 lib/ovn-util.h      | 11 +++++++
ebb439
 northd/ovn-northd.c | 50 ----------------------------
ebb439
 3 files changed, 80 insertions(+), 60 deletions(-)
ebb439
ebb439
diff --git a/lib/ovn-util.c b/lib/ovn-util.c
ebb439
index 47c7f57a0..8722f7a48 100644
ebb439
--- a/lib/ovn-util.c
ebb439
+++ b/lib/ovn-util.c
ebb439
@@ -13,17 +13,21 @@
ebb439
  */
ebb439
 
ebb439
 #include <config.h>
ebb439
+
ebb439
+#include "ovn-util.h"
ebb439
+
ebb439
+#include <ctype.h>
ebb439
 #include <unistd.h>
ebb439
 
ebb439
 #include "daemon.h"
ebb439
-#include "ovn-util.h"
ebb439
-#include "ovn-dirs.h"
ebb439
-#include "openvswitch/vlog.h"
ebb439
 #include "openvswitch/ofp-parse.h"
ebb439
+#include "openvswitch/vlog.h"
ebb439
+#include "ovn-dirs.h"
ebb439
 #include "ovn-nb-idl.h"
ebb439
 #include "ovn-sb-idl.h"
ebb439
+#include "socket-util.h"
ebb439
+#include "svec.h"
ebb439
 #include "unixctl.h"
ebb439
-#include <ctype.h>
ebb439
 
ebb439
 VLOG_DEFINE_THIS_MODULE(ovn_util);
ebb439
 
ebb439
@@ -240,27 +244,37 @@ extract_ip_addresses(const char *address, struct lport_addresses *laddrs)
ebb439
 bool
ebb439
 extract_lrp_networks(const struct nbrec_logical_router_port *lrp,
ebb439
                      struct lport_addresses *laddrs)
ebb439
+{
ebb439
+    return extract_lrp_networks__(lrp->mac, lrp->networks, lrp->n_networks,
ebb439
+                                  laddrs);
ebb439
+}
ebb439
+
ebb439
+/* Separate out the body of 'extract_lrp_networks()' for use from DDlog,
ebb439
+ * which does not know the 'nbrec_logical_router_port' type. */
ebb439
+bool
ebb439
+extract_lrp_networks__(char *mac, char **networks, size_t n_networks,
ebb439
+                       struct lport_addresses *laddrs)
ebb439
 {
ebb439
     memset(laddrs, 0, sizeof *laddrs);
ebb439
 
ebb439
-    if (!eth_addr_from_string(lrp->mac, &laddrs->ea)) {
ebb439
+    if (!eth_addr_from_string(mac, &laddrs->ea)) {
ebb439
         laddrs->ea = eth_addr_zero;
ebb439
         return false;
ebb439
     }
ebb439
     snprintf(laddrs->ea_s, sizeof laddrs->ea_s, ETH_ADDR_FMT,
ebb439
              ETH_ADDR_ARGS(laddrs->ea));
ebb439
 
ebb439
-    for (int i = 0; i < lrp->n_networks; i++) {
ebb439
+    for (int i = 0; i < n_networks; i++) {
ebb439
         ovs_be32 ip4;
ebb439
         struct in6_addr ip6;
ebb439
         unsigned int plen;
ebb439
         char *error;
ebb439
 
ebb439
-        error = ip_parse_cidr(lrp->networks[i], &ip4, &plen);
ebb439
+        error = ip_parse_cidr(networks[i], &ip4, &plen);
ebb439
         if (!error) {
ebb439
             if (!ip4) {
ebb439
                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
ebb439
-                VLOG_WARN_RL(&rl, "bad 'networks' %s", lrp->networks[i]);
ebb439
+                VLOG_WARN_RL(&rl, "bad 'networks' %s", networks[i]);
ebb439
                 continue;
ebb439
             }
ebb439
 
ebb439
@@ -269,13 +283,13 @@ extract_lrp_networks(const struct nbrec_logical_router_port *lrp,
ebb439
         }
ebb439
         free(error);
ebb439
 
ebb439
-        error = ipv6_parse_cidr(lrp->networks[i], &ip6, &plen);
ebb439
+        error = ipv6_parse_cidr(networks[i], &ip6, &plen);
ebb439
         if (!error) {
ebb439
             add_ipv6_netaddr(laddrs, ip6, plen);
ebb439
         } else {
ebb439
             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
ebb439
             VLOG_INFO_RL(&rl, "invalid syntax '%s' in networks",
ebb439
-                         lrp->networks[i]);
ebb439
+                         networks[i]);
ebb439
             free(error);
ebb439
         }
ebb439
     }
ebb439
@@ -327,6 +341,23 @@ destroy_lport_addresses(struct lport_addresses *laddrs)
ebb439
     free(laddrs->ipv6_addrs);
ebb439
 }
ebb439
 
ebb439
+/* Go through 'addresses' and add found IPv4 addresses to 'ipv4_addrs' and
ebb439
+ * IPv6 addresses to 'ipv6_addrs'. */
ebb439
+void
ebb439
+split_addresses(const char *addresses, struct svec *ipv4_addrs,
ebb439
+                struct svec *ipv6_addrs)
ebb439
+{
ebb439
+    struct lport_addresses laddrs;
ebb439
+    extract_lsp_addresses(addresses, &laddrs);
ebb439
+    for (size_t k = 0; k < laddrs.n_ipv4_addrs; k++) {
ebb439
+        svec_add(ipv4_addrs, laddrs.ipv4_addrs[k].addr_s);
ebb439
+    }
ebb439
+    for (size_t k = 0; k < laddrs.n_ipv6_addrs; k++) {
ebb439
+        svec_add(ipv6_addrs, laddrs.ipv6_addrs[k].addr_s);
ebb439
+    }
ebb439
+    destroy_lport_addresses(&laddrs);
ebb439
+}
ebb439
+
ebb439
 /* Allocates a key for NAT conntrack zone allocation for a provided
ebb439
  * 'key' record and a 'type'.
ebb439
  *
ebb439
@@ -661,3 +692,31 @@ ovn_smap_get_uint(const struct smap *smap, const char *key, unsigned int def)
ebb439
 
ebb439
     return u_value;
ebb439
 }
ebb439
+
ebb439
+/* For a 'key' of the form "IP:port" or just "IP", sets 'port' and
ebb439
+ * 'ip_address'.  The caller must free() the memory allocated for
ebb439
+ * 'ip_address'.
ebb439
+ * Returns true if parsing of 'key' was successful, false otherwise.
ebb439
+ */
ebb439
+bool
ebb439
+ip_address_and_port_from_lb_key(const char *key, char **ip_address,
ebb439
+                                uint16_t *port, int *addr_family)
ebb439
+{
ebb439
+    struct sockaddr_storage ss;
ebb439
+    if (!inet_parse_active(key, 0, &ss, false)) {
ebb439
+        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
ebb439
+        VLOG_WARN_RL(&rl, "bad ip address or port for load balancer key %s",
ebb439
+                     key);
ebb439
+        *ip_address = NULL;
ebb439
+        *port = 0;
ebb439
+        *addr_family = 0;
ebb439
+        return false;
ebb439
+    }
ebb439
+
ebb439
+    struct ds s = DS_EMPTY_INITIALIZER;
ebb439
+    ss_format_address_nobracks(&ss, &s);
ebb439
+    *ip_address = ds_steal_cstr(&s);
ebb439
+    *port = ss_get_port(&ss);
ebb439
+    *addr_family = ss.ss_family;
ebb439
+    return true;
ebb439
+}
ebb439
diff --git a/lib/ovn-util.h b/lib/ovn-util.h
ebb439
index a597efb50..f72a801df 100644
ebb439
--- a/lib/ovn-util.h
ebb439
+++ b/lib/ovn-util.h
ebb439
@@ -27,6 +27,7 @@
ebb439
 
ebb439
 struct nbrec_logical_router_port;
ebb439
 struct sbrec_logical_flow;
ebb439
+struct svec;
ebb439
 struct uuid;
ebb439
 struct eth_addr;
ebb439
 struct sbrec_port_binding;
ebb439
@@ -76,8 +77,15 @@ bool extract_lrp_networks(const struct nbrec_logical_router_port *,
ebb439
 bool extract_sbrec_binding_first_mac(const struct sbrec_port_binding *binding,
ebb439
                                      struct eth_addr *ea);
ebb439
 
ebb439
+bool extract_lrp_networks__(char *mac, char **networks, size_t n_networks,
ebb439
+                            struct lport_addresses *laddrs);
ebb439
+
ebb439
+bool lport_addresses_is_empty(struct lport_addresses *);
ebb439
 void destroy_lport_addresses(struct lport_addresses *);
ebb439
 
ebb439
+void split_addresses(const char *addresses, struct svec *ipv4_addrs,
ebb439
+                     struct svec *ipv6_addrs);
ebb439
+
ebb439
 char *alloc_nat_zone_key(const struct uuid *key, const char *type);
ebb439
 
ebb439
 const char *default_nb_db(void);
ebb439
@@ -220,4 +228,7 @@ char *str_tolower(const char *orig);
ebb439
         case OVN_OPT_MONITOR:                       \
ebb439
         case OVN_OPT_USER_GROUP:
ebb439
 
ebb439
+bool ip_address_and_port_from_lb_key(const char *key, char **ip_address,
ebb439
+                                     uint16_t *port, int *addr_family);
ebb439
+
ebb439
 #endif
ebb439
diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c
ebb439
index 13fa0ebef..38074e7f4 100644
ebb439
--- a/northd/ovn-northd.c
ebb439
+++ b/northd/ovn-northd.c
ebb439
@@ -2494,10 +2494,6 @@ join_logical_ports(struct northd_context *ctx,
ebb439
     }
ebb439
 }
ebb439
 
ebb439
-static bool
ebb439
-ip_address_and_port_from_lb_key(const char *key, char **ip_address,
ebb439
-                                uint16_t *port, int *addr_family);
ebb439
-
ebb439
 static void
ebb439
 get_router_load_balancer_ips(const struct ovn_datapath *od,
ebb439
                              struct sset *all_ips_v4, struct sset *all_ips_v6)
ebb439
@@ -5061,34 +5057,6 @@ build_pre_acls(struct ovn_datapath *od, struct hmap *lflows)
ebb439
     }
ebb439
 }
ebb439
 
ebb439
-/* For a 'key' of the form "IP:port" or just "IP", sets 'port' and
ebb439
- * 'ip_address'.  The caller must free() the memory allocated for
ebb439
- * 'ip_address'.
ebb439
- * Returns true if parsing of 'key' was successful, false otherwise.
ebb439
- */
ebb439
-static bool
ebb439
-ip_address_and_port_from_lb_key(const char *key, char **ip_address,
ebb439
-                                uint16_t *port, int *addr_family)
ebb439
-{
ebb439
-    struct sockaddr_storage ss;
ebb439
-    if (!inet_parse_active(key, 0, &ss, false)) {
ebb439
-        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
ebb439
-        VLOG_WARN_RL(&rl, "bad ip address or port for load balancer key %s",
ebb439
-                     key);
ebb439
-        *ip_address = NULL;
ebb439
-        *port = 0;
ebb439
-        *addr_family = 0;
ebb439
-        return false;
ebb439
-    }
ebb439
-
ebb439
-    struct ds s = DS_EMPTY_INITIALIZER;
ebb439
-    ss_format_address_nobracks(&ss, &s);
ebb439
-    *ip_address = ds_steal_cstr(&s);
ebb439
-    *port = ss_get_port(&ss);
ebb439
-    *addr_family = ss.ss_family;
ebb439
-    return true;
ebb439
-}
ebb439
-
ebb439
 /*
ebb439
  * Returns true if logical switch is configured with DNS records, false
ebb439
  * otherwise.
ebb439
@@ -11482,24 +11450,6 @@ sync_address_set(struct northd_context *ctx, const char *name,
ebb439
                                     addrs, n_addrs);
ebb439
 }
ebb439
 
ebb439
-/* Go through 'addresses' and add found IPv4 addresses to 'ipv4_addrs' and IPv6
ebb439
- * addresses to 'ipv6_addrs'.
ebb439
- */
ebb439
-static void
ebb439
-split_addresses(const char *addresses, struct svec *ipv4_addrs,
ebb439
-                struct svec *ipv6_addrs)
ebb439
-{
ebb439
-    struct lport_addresses laddrs;
ebb439
-    extract_lsp_addresses(addresses, &laddrs);
ebb439
-    for (size_t k = 0; k < laddrs.n_ipv4_addrs; k++) {
ebb439
-        svec_add(ipv4_addrs, laddrs.ipv4_addrs[k].addr_s);
ebb439
-    }
ebb439
-    for (size_t k = 0; k < laddrs.n_ipv6_addrs; k++) {
ebb439
-        svec_add(ipv6_addrs, laddrs.ipv6_addrs[k].addr_s);
ebb439
-    }
ebb439
-    destroy_lport_addresses(&laddrs);
ebb439
-}
ebb439
-
ebb439
 /* OVN_Southbound Address_Set table contains same records as in north
ebb439
  * bound, plus the records generated from Port_Group table in north bound.
ebb439
  *
ebb439
-- 
ebb439
2.28.0
ebb439