ovs-vsctl: Try connecting only once for active connections by default.
[sliver-openvswitch.git] / utilities / ovs-vsctl.c
index bbaaa1b..18ace60 100644 (file)
@@ -16,7 +16,6 @@
 
 #include <config.h>
 
-#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <float.h>
@@ -115,6 +114,16 @@ static bool wait_for_reload = true;
 /* --timeout: Time to wait for a connection to 'db'. */
 static int timeout;
 
+/* --retry: If true, ovs-vsctl will retry connecting to the database forever.
+ * If false and --db says to use an active connection method (e.g. "unix:",
+ * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
+ * error if the database server cannot be contacted (e.g. ovsdb-server is not
+ * running).
+ *
+ * Regardless of this setting, --timeout always limits how long ovs-vsctl will
+ * wait. */
+static bool retry;
+
 /* Format for table output. */
 static struct table_style table_style = TABLE_STYLE_DEFAULT;
 
@@ -187,7 +196,7 @@ main(int argc, char *argv[])
     }
 
     /* Initialize IDL. */
-    idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
+    idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
     run_prerequisites(commands, n_commands, idl);
 
     /* Execute the commands.
@@ -200,6 +209,11 @@ main(int argc, char *argv[])
     seqno = ovsdb_idl_get_seqno(idl);
     for (;;) {
         ovsdb_idl_run(idl);
+        if (!ovsdb_idl_is_alive(idl)) {
+            int retval = ovsdb_idl_get_last_error(idl);
+            vsctl_fatal("%s: database connection failed (%s)",
+                        db, ovs_retval_to_string(retval));
+        }
 
         if (seqno != ovsdb_idl_get_seqno(idl)) {
             seqno = ovsdb_idl_get_seqno(idl);
@@ -248,6 +262,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
         OPT_DRY_RUN,
         OPT_PEER_CA_CERT,
         OPT_LOCAL,
+        OPT_RETRY,
         VLOG_OPTION_ENUMS,
         TABLE_OPTION_ENUMS
     };
@@ -258,6 +273,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
         {"dry-run", no_argument, NULL, OPT_DRY_RUN},
         {"oneline", no_argument, NULL, OPT_ONELINE},
         {"timeout", required_argument, NULL, 't'},
+        {"retry", no_argument, NULL, OPT_RETRY},
         {"help", no_argument, NULL, 'h'},
         {"version", no_argument, NULL, 'V'},
         VLOG_LONG_OPTIONS,
@@ -298,7 +314,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
                 char *equals;
                 int has_arg;
 
-                assert(name[0] == '-' && name[1] == '-' && name[2]);
+                ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
                 name += 2;
 
                 equals = strchr(name, '=');
@@ -311,8 +327,8 @@ parse_options(int argc, char *argv[], struct shash *local_options)
 
                 o = find_option(name, options, n_options);
                 if (o) {
-                    assert(o - options >= n_global_long_options);
-                    assert(o->has_arg == has_arg);
+                    ovs_assert(o - options >= n_global_long_options);
+                    ovs_assert(o->has_arg == has_arg);
                 } else {
                     o = add_option(&options, &n_options, &allocated_options);
                     o->name = xstrdup(name);
@@ -385,6 +401,10 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             }
             break;
 
+        case OPT_RETRY:
+            retry = true;
+            break;
+
         VLOG_OPTION_HANDLERS
         TABLE_OPTION_HANDLERS(&table_style)
 
@@ -663,6 +683,7 @@ Options:\n\
   --db=DATABASE               connect to DATABASE\n\
                               (default: %s)\n\
   --no-wait                   do not wait for ovs-vswitchd to reconfigure\n\
+  --retry                     keep trying to connect to server forever\n\
   -t, --timeout=SECS          wait at most SECS seconds for ovs-vswitchd\n\
   --dry-run                   do not commit changes to database\n\
   --oneline                   print exactly one line of output per command\n",
@@ -845,8 +866,8 @@ ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
 static void
 del_cached_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
 {
-    assert(list_is_empty(&br->ports));
-    assert(hmap_is_empty(&br->children));
+    ovs_assert(list_is_empty(&br->ports));
+    ovs_assert(hmap_is_empty(&br->children));
     if (br->parent) {
         hmap_remove(&br->parent->children, &br->children_node);
     }
@@ -912,7 +933,7 @@ add_port_to_cache(struct vsctl_context *ctx, struct vsctl_bridge *parent,
 static void
 del_cached_port(struct vsctl_context *ctx, struct vsctl_port *port)
 {
-    assert(list_is_empty(&port->ifaces));
+    ovs_assert(list_is_empty(&port->ifaces));
     list_remove(&port->ports_node);
     shash_find_and_delete(&ctx->ports, port->port_cfg->name);
     ovsrec_port_delete(port->port_cfg);
@@ -1133,7 +1154,7 @@ find_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
 {
     struct vsctl_bridge *br;
 
-    assert(ctx->cache_valid);
+    ovs_assert(ctx->cache_valid);
 
     br = shash_find_data(&ctx->bridges, name);
     if (must_exist && !br) {
@@ -1158,7 +1179,7 @@ find_port(struct vsctl_context *ctx, const char *name, bool must_exist)
 {
     struct vsctl_port *port;
 
-    assert(ctx->cache_valid);
+    ovs_assert(ctx->cache_valid);
 
     port = shash_find_data(&ctx->ports, name);
     if (port && !strcmp(name, port->bridge->name)) {
@@ -1176,7 +1197,7 @@ find_iface(struct vsctl_context *ctx, const char *name, bool must_exist)
 {
     struct vsctl_iface *iface;
 
-    assert(ctx->cache_valid);
+    ovs_assert(ctx->cache_valid);
 
     iface = shash_find_data(&ctx->ifaces, name);
     if (iface && !strcmp(name, iface->port->bridge->name)) {
@@ -2787,7 +2808,7 @@ parse_column_key_value(const char *arg,
     char *column_name;
     char *error;
 
-    assert(!(operatorp && !valuep));
+    ovs_assert(!(operatorp && !valuep));
     *keyp = NULL;
     if (valuep) {
         *valuep = NULL;
@@ -3898,8 +3919,8 @@ run_prerequisites(struct vsctl_command *commands, size_t n_commands,
             (c->syntax->prerequisites)(&ctx);
             vsctl_context_done(&ctx, c);
 
-            assert(!c->output.string);
-            assert(!c->table);
+            ovs_assert(!c->output.string);
+            ovs_assert(!c->table);
         }
     }
 }