[ovs-dev] [PATCH 10/30] smap: New function smap_get_def().

Ben Pfaff blp at ovn.org
Mon Aug 8 07:44:49 UTC 2016


Signed-off-by: Ben Pfaff <blp at ovn.org>
---
 lib/bfd.c                       | 60 +++++++++++++----------------------------
 lib/smap.c                      | 25 +++++++++--------
 lib/smap.h                      |  4 ++-
 ovn/controller/chassis.c        |  7 +++--
 ovn/controller/encaps.c         |  5 ++--
 ovn/controller/ovn-controller.c |  6 ++---
 ovn/controller/patch.c          |  6 ++---
 ovn/northd/ovn-northd.c         |  7 ++---
 ovn/utilities/ovn-sbctl.c       |  4 +--
 9 files changed, 49 insertions(+), 75 deletions(-)

diff --git a/lib/bfd.c b/lib/bfd.c
index 7a34e89..fcb6f64 100644
--- a/lib/bfd.c
+++ b/lib/bfd.c
@@ -235,7 +235,7 @@ static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
 static struct hmap all_bfds__ = HMAP_INITIALIZER(&all_bfds__);
 static struct hmap *const all_bfds OVS_GUARDED_BY(mutex) = &all_bfds__;
 
-static bool bfd_lookup_ip(const char *host_name, struct in_addr *)
+static void bfd_lookup_ip(const char *host_name, ovs_be32 def, ovs_be32 *ip)
     OVS_REQUIRES(mutex);
 static bool bfd_forwarding__(struct bfd *) OVS_REQUIRES(mutex);
 static bool bfd_in_poll(const struct bfd *) OVS_REQUIRES(mutex);
@@ -354,9 +354,6 @@ bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
     bool need_poll = false;
     bool cfg_min_rx_changed = false;
     bool cpath_down, forwarding_if_rx;
-    const char *hwaddr, *ip_src, *ip_dst;
-    struct in_addr in_addr;
-    struct eth_addr ea;
 
     if (!cfg || !smap_get_bool(cfg, "enable", false)) {
         bfd_unref(bfd);
@@ -441,40 +438,17 @@ bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
         need_poll = true;
     }
 
-    hwaddr = smap_get(cfg, "bfd_local_src_mac");
-    if (hwaddr && eth_addr_from_string(hwaddr, &ea)) {
-        bfd->local_eth_src = ea;
-    } else {
-        bfd->local_eth_src = eth_addr_zero;
-    }
-
-    hwaddr = smap_get(cfg, "bfd_local_dst_mac");
-    if (hwaddr && eth_addr_from_string(hwaddr, &ea)) {
-        bfd->local_eth_dst = ea;
-    } else {
-        bfd->local_eth_dst = eth_addr_zero;
-    }
-
-    hwaddr = smap_get(cfg, "bfd_remote_dst_mac");
-    if (hwaddr && eth_addr_from_string(hwaddr, &ea)) {
-        bfd->rmt_eth_dst = ea;
-    } else {
-        bfd->rmt_eth_dst = eth_addr_zero;
-    }
+    eth_addr_from_string(smap_get_def(cfg, "bfd_local_src_mac", ""),
+                         &bfd->local_eth_src);
+    eth_addr_from_string(smap_get_def(cfg, "bfd_local_dst_mac", ""),
+                         &bfd->local_eth_dst);
+    eth_addr_from_string(smap_get_def(cfg, "bfd_remote_dst_mac", ""),
+                         &bfd->rmt_eth_dst);
 
-    ip_src = smap_get(cfg, "bfd_src_ip");
-    if (ip_src && bfd_lookup_ip(ip_src, &in_addr)) {
-        memcpy(&bfd->ip_src, &in_addr, sizeof in_addr);
-    } else {
-        bfd->ip_src = htonl(0xA9FE0101); /* 169.254.1.1. */
-    }
-
-    ip_dst = smap_get(cfg, "bfd_dst_ip");
-    if (ip_dst && bfd_lookup_ip(ip_dst, &in_addr)) {
-        memcpy(&bfd->ip_dst, &in_addr, sizeof in_addr);
-    } else {
-        bfd->ip_dst = htonl(0xA9FE0100); /* 169.254.1.0. */
-    }
+    bfd_lookup_ip(smap_get_def(cfg, "bfd_src_ip", ""),
+                  htonl(0xA9FE0101) /* 169.254.1.1 */, &bfd->ip_src);
+    bfd_lookup_ip(smap_get_def(cfg, "bfd_dst_ip", ""),
+                  htonl(0xA9FE0100) /* 169.254.1.0 */, &bfd->ip_dst);
 
     forwarding_if_rx = smap_get_bool(cfg, "forwarding_if_rx", false);
     if (bfd->forwarding_if_rx != forwarding_if_rx) {
@@ -942,14 +916,16 @@ bfd_forwarding__(struct bfd *bfd) OVS_REQUIRES(mutex)
 }
 
 /* Helpers. */
-static bool
-bfd_lookup_ip(const char *host_name, struct in_addr *addr)
+static void
+bfd_lookup_ip(const char *host_name, ovs_be32 def, ovs_be32 *addr)
 {
-    if (!ip_parse(host_name, &addr->s_addr)) {
+    if (host_name[0]) {
+        if (ip_parse(host_name, addr)) {
+            return;
+        }
         VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
-        return false;
     }
-    return true;
+    *addr = def;
 }
 
 static bool
diff --git a/lib/smap.c b/lib/smap.c
index aff7eb0..7b35da6 100644
--- a/lib/smap.c
+++ b/lib/smap.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012, 2014, 2015 Nicira, Inc.
+/* Copyright (c) 2012, 2014, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -182,12 +182,21 @@ smap_clear(struct smap *smap)
     }
 }
 
-/* Returns the value associated with 'key' in 'smap', or NULL. */
+/* Returns the value associated with 'key' in 'smap'.
+ * If 'smap' does not contain 'key', returns NULL. */
 const char *
 smap_get(const struct smap *smap, const char *key)
 {
+    return smap_get_def(smap, key, NULL);
+}
+
+/* Returns the value associated with 'key' in 'smap'.
+ * If 'smap' does not contain 'key', returns 'def'. */
+const char *
+smap_get_def(const struct smap *smap, const char *key, const char *def)
+{
     struct smap_node *node = smap_get_node(smap, key);
-    return node ? node->value : NULL;
+    return node ? node->value : def;
 }
 
 /* Returns the node associated with 'key' in 'smap', or NULL. */
@@ -204,12 +213,7 @@ smap_get_node(const struct smap *smap, const char *key)
 bool
 smap_get_bool(const struct smap *smap, const char *key, bool def)
 {
-    const char *value = smap_get(smap, key);
-
-    if (!value) {
-        return def;
-    }
-
+    const char *value = smap_get_def(smap, key, "");
     if (def) {
         return strcasecmp("false", value) != 0;
     } else {
@@ -233,8 +237,7 @@ smap_get_int(const struct smap *smap, const char *key, int def)
 bool
 smap_get_uuid(const struct smap *smap, const char *key, struct uuid *uuid)
 {
-    const char *value = smap_get(smap, key);
-    return value && uuid_from_string(uuid, value);
+    return uuid_from_string(uuid, smap_get_def(smap, key, ""));
 }
 
 /* Returns true of there are no elements in 'smap'. */
diff --git a/lib/smap.h b/lib/smap.h
index 29f99e6..9ad71e7 100644
--- a/lib/smap.h
+++ b/lib/smap.h
@@ -82,7 +82,9 @@ void smap_remove_node(struct smap *, struct smap_node *);
 void smap_steal(struct smap *, struct smap_node *, char **keyp, char **valuep);
 void smap_clear(struct smap *);
 
-const char *smap_get(const struct smap *, const char *);
+const char *smap_get(const struct smap *, const char *key);
+const char *smap_get_def(const struct smap *, const char *key,
+                         const char *def);
 struct smap_node *smap_get_node(const struct smap *, const char *);
 bool smap_get_bool(const struct smap *smap, const char *key, bool def);
 int smap_get_int(const struct smap *smap, const char *key, int def);
diff --git a/ovn/controller/chassis.c b/ovn/controller/chassis.c
index a1545ec..b5f022e 100644
--- a/ovn/controller/chassis.c
+++ b/ovn/controller/chassis.c
@@ -60,8 +60,7 @@ pop_tunnel_name(uint32_t *type)
 static const char *
 get_bridge_mappings(const struct smap *ext_ids)
 {
-    const char *bridge_mappings = smap_get(ext_ids, "ovn-bridge-mappings");
-    return bridge_mappings ? bridge_mappings : "";
+    return smap_get_def(ext_ids, "ovn-bridge-mappings", "");
 }
 
 /* Returns this chassis's Chassis record, if it is available and is currently
@@ -104,9 +103,9 @@ chassis_run(struct controller_ctx *ctx, const char *chassis_id)
     }
     free(tokstr);
 
-    const char *hostname = smap_get(&cfg->external_ids, "hostname");
+    const char *hostname = smap_get_def(&cfg->external_ids, "hostname", "");
     char hostname_[HOST_NAME_MAX + 1];
-    if (!hostname || !hostname[0]) {
+    if (!hostname[0]) {
         if (gethostname(hostname_, sizeof hostname_)) {
             hostname_[0] = '\0';
         }
diff --git a/ovn/controller/encaps.c b/ovn/controller/encaps.c
index 977cc3a..3bb8ae4 100644
--- a/ovn/controller/encaps.c
+++ b/ovn/controller/encaps.c
@@ -327,8 +327,9 @@ check_and_update_tunnel(const struct sbrec_chassis *chassis_rec)
             smap_destroy(&options);
         }
 
-        const char *chassis = smap_get(&port->external_ids, "ovn-chassis-id");
-        if (!chassis || strcmp(chassis_rec->name, chassis)) {
+        const char *chassis = smap_get_def(&port->external_ids,
+                                           "ovn-chassis-id", "");
+        if (strcmp(chassis_rec->name, chassis)) {
             const struct smap id = SMAP_CONST1(&id, "ovn-chassis-id",
                                                chassis_rec->name);
             ovsrec_port_set_external_ids(port, &id);
diff --git a/ovn/controller/ovn-controller.c b/ovn/controller/ovn-controller.c
index 2ca0295..126e471 100644
--- a/ovn/controller/ovn-controller.c
+++ b/ovn/controller/ovn-controller.c
@@ -178,10 +178,8 @@ get_br_int(struct controller_ctx *ctx)
         return NULL;
     }
 
-    const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
-    if (!br_int_name) {
-        br_int_name = DEFAULT_BRIDGE_NAME;
-    }
+    const char *br_int_name = smap_get_def(&cfg->external_ids, "ovn-bridge",
+                                           DEFAULT_BRIDGE_NAME);
 
     const struct ovsrec_bridge *br;
     br = get_bridge(ctx->ovs_idl, br_int_name);
diff --git a/ovn/controller/patch.c b/ovn/controller/patch.c
index 012e6ba..f9efc4c 100644
--- a/ovn/controller/patch.c
+++ b/ovn/controller/patch.c
@@ -150,10 +150,8 @@ add_bridge_mappings(struct controller_ctx *ctx,
     const struct ovsrec_open_vswitch *cfg;
     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
     if (cfg) {
-        mappings_cfg = smap_get(&cfg->external_ids, "ovn-bridge-mappings");
-        if (!mappings_cfg) {
-            mappings_cfg = "";
-        }
+        mappings_cfg = smap_get_def(&cfg->external_ids, "ovn-bridge-mappings",
+                                    "");
     }
 
     /* Parse bridge mappings. */
diff --git a/ovn/northd/ovn-northd.c b/ovn/northd/ovn-northd.c
index d6c14cf..861f872 100644
--- a/ovn/northd/ovn-northd.c
+++ b/ovn/northd/ovn-northd.c
@@ -1178,11 +1178,8 @@ ovn_port_update_sbrec(const struct ovn_port *op)
                 sbrec_port_binding_set_type(op->sb, "patch");
             }
 
-            const char *router_port = smap_get(&op->nbsp->options,
-                                               "router-port");
-            if (!router_port) {
-                router_port = "<error>";
-            }
+            const char *router_port = smap_get_def(&op->nbsp->options,
+                                                   "router-port", "<error>");
             struct smap new;
             smap_init(&new);
             smap_add(&new, "peer", router_port);
diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
index dfac8ea..50c9f17 100644
--- a/ovn/utilities/ovn-sbctl.c
+++ b/ovn/utilities/ovn-sbctl.c
@@ -736,10 +736,10 @@ cmd_lflow_list(struct ctl_context *ctx)
             cur_pipeline = lflow->pipeline;
         }
 
-        const char *table_name = smap_get(&lflow->external_ids, "stage-name");
         printf("  table=%-2" PRId64 "(%-19s), priority=%-5" PRId64
                ", match=(%s), action=(%s)\n",
-               lflow->table_id, table_name ? table_name : "",
+               lflow->table_id,
+               smap_get_def(&lflow->external_ids, "stage-name", ""),
                lflow->priority, lflow->match, lflow->actions);
     }
 
-- 
2.1.3




More information about the dev mailing list