Makefile.am: Clean flake8-check too.
[cascardo/ovs.git] / utilities / ovs-vsctl.c
index 6bc6818..eeb711f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 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.
@@ -84,6 +84,14 @@ static bool retry;
 static struct table_style table_style = TABLE_STYLE_DEFAULT;
 
 static void vsctl_cmd_init(void);
+
+/* The IDL we're using and the current transaction, if any.
+ * This is for use by vsctl_exit() only, to allow it to clean up.
+ * Other code should use its context arguments. */
+static struct ovsdb_idl *the_idl;
+static struct ovsdb_idl_txn *the_idl_txn;
+OVS_NO_RETURN static void vsctl_exit(int status);
+
 OVS_NO_RETURN static void usage(void);
 static void parse_options(int argc, char *argv[], struct shash *local_options);
 static void run_prerequisites(struct ctl_command[], size_t n_commands,
@@ -122,7 +130,6 @@ static size_t allocated_neoteric_ifaces;
 int
 main(int argc, char *argv[])
 {
-    extern struct vlog_module VLM_reconnect;
     struct ovsdb_idl *idl;
     struct ctl_command *commands;
     struct shash local_options;
@@ -133,7 +140,7 @@ main(int argc, char *argv[])
     set_program_name(argv[0]);
     fatal_ignore_sigpipe();
     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
-    vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
+    vlog_set_levels_from_string_assert("reconnect:warn");
     ovsrec_init();
 
     vsctl_cmd_init();
@@ -193,6 +200,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
         OPT_NO_SYSLOG,
         OPT_NO_WAIT,
         OPT_DRY_RUN,
+        OPT_BOOTSTRAP_CA_CERT,
         OPT_PEER_CA_CERT,
         OPT_LOCAL,
         OPT_RETRY,
@@ -216,6 +224,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
         VLOG_LONG_OPTIONS,
         TABLE_LONG_OPTIONS,
         STREAM_SSL_LONG_OPTIONS,
+        {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
         {NULL, 0, NULL, 0},
     };
@@ -260,7 +269,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             break;
 
         case OPT_NO_SYSLOG:
-            vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
+            vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
             break;
 
         case OPT_NO_WAIT:
@@ -316,6 +325,10 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             stream_ssl_set_peer_ca_cert_file(optarg);
             break;
 
+        case OPT_BOOTSTRAP_CA_CERT:
+            stream_ssl_set_ca_cert_file(optarg, true);
+            break;
+
         case '?':
             exit(EXIT_FAILURE);
 
@@ -469,7 +482,7 @@ struct vsctl_iface {
     struct vsctl_port *port;
 };
 
-/* Casts 'base' into 'strcut vsctl_context'. */
+/* Casts 'base' into 'struct vsctl_context'. */
 static struct vsctl_context *
 vsctl_context_cast(struct ctl_context *base)
 {
@@ -740,9 +753,6 @@ vsctl_context_populate_cache(struct ctl_context *ctx)
             continue;
         }
         br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
-        if (!br) {
-            continue;
-        }
 
         for (j = 0; j < br_cfg->n_ports; j++) {
             struct ovsrec_port *port_cfg = br_cfg->ports[j];
@@ -970,185 +980,57 @@ cmd_init(struct ctl_context *ctx OVS_UNUSED)
 {
 }
 
-struct cmd_show_table {
-    const struct ovsdb_idl_table_class *table;
-    const struct ovsdb_idl_column *name_column;
-    const struct ovsdb_idl_column *columns[3];
-    bool recurse;
-};
-
 static struct cmd_show_table cmd_show_tables[] = {
     {&ovsrec_table_open_vswitch,
      NULL,
      {&ovsrec_open_vswitch_col_manager_options,
       &ovsrec_open_vswitch_col_bridges,
       &ovsrec_open_vswitch_col_ovs_version},
-     false},
+     {NULL, NULL, NULL}
+    },
 
     {&ovsrec_table_bridge,
      &ovsrec_bridge_col_name,
      {&ovsrec_bridge_col_controller,
       &ovsrec_bridge_col_fail_mode,
       &ovsrec_bridge_col_ports},
-     false},
+     {NULL, NULL, NULL}
+    },
 
     {&ovsrec_table_port,
      &ovsrec_port_col_name,
      {&ovsrec_port_col_tag,
       &ovsrec_port_col_trunks,
       &ovsrec_port_col_interfaces},
-     false},
+     {NULL, NULL, NULL}
+    },
 
     {&ovsrec_table_interface,
      &ovsrec_interface_col_name,
      {&ovsrec_interface_col_type,
       &ovsrec_interface_col_options,
       &ovsrec_interface_col_error},
-     false},
+     {NULL, NULL, NULL}
+    },
 
     {&ovsrec_table_controller,
      &ovsrec_controller_col_target,
      {&ovsrec_controller_col_is_connected,
       NULL,
       NULL},
-     false},
+     {NULL, NULL, NULL}
+    },
 
     {&ovsrec_table_manager,
      &ovsrec_manager_col_target,
      {&ovsrec_manager_col_is_connected,
       NULL,
       NULL},
-     false},
-};
-
-static void
-pre_cmd_show(struct ctl_context *ctx)
-{
-    struct cmd_show_table *show;
-
-    for (show = cmd_show_tables;
-         show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
-         show++) {
-        size_t i;
-
-        ovsdb_idl_add_table(ctx->idl, show->table);
-        if (show->name_column) {
-            ovsdb_idl_add_column(ctx->idl, show->name_column);
-        }
-        for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
-            const struct ovsdb_idl_column *column = show->columns[i];
-            if (column) {
-                ovsdb_idl_add_column(ctx->idl, column);
-            }
-        }
-    }
-}
-
-static struct cmd_show_table *
-cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
-{
-    struct cmd_show_table *show;
-
-    for (show = cmd_show_tables;
-         show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
-         show++) {
-        if (show->table == row->table->class) {
-            return show;
-        }
-    }
-    return NULL;
-}
-
-static struct cmd_show_table *
-cmd_show_find_table_by_name(const char *name)
-{
-    struct cmd_show_table *show;
-
-    for (show = cmd_show_tables;
-         show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
-         show++) {
-        if (!strcmp(show->table->name, name)) {
-            return show;
-        }
-    }
-    return NULL;
-}
-
-static void
-cmd_show_row(struct ctl_context *ctx, const struct ovsdb_idl_row *row,
-             int level)
-{
-    struct cmd_show_table *show = cmd_show_find_table_by_row(row);
-    size_t i;
-
-    ds_put_char_multiple(&ctx->output, ' ', level * 4);
-    if (show && show->name_column) {
-        const struct ovsdb_datum *datum;
-
-        ds_put_format(&ctx->output, "%s ", show->table->name);
-        datum = ovsdb_idl_read(row, show->name_column);
-        ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
-    } else {
-        ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
-    }
-    ds_put_char(&ctx->output, '\n');
-
-    if (!show || show->recurse) {
-        return;
-    }
-
-    show->recurse = true;
-    for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
-        const struct ovsdb_idl_column *column = show->columns[i];
-        const struct ovsdb_datum *datum;
+     {NULL, NULL, NULL}
+    },
 
-        if (!column) {
-            break;
-        }
-
-        datum = ovsdb_idl_read(row, column);
-        if (column->type.key.type == OVSDB_TYPE_UUID &&
-            column->type.key.u.uuid.refTableName) {
-            struct cmd_show_table *ref_show;
-            size_t j;
-
-            ref_show = cmd_show_find_table_by_name(
-                column->type.key.u.uuid.refTableName);
-            if (ref_show) {
-                for (j = 0; j < datum->n; j++) {
-                    const struct ovsdb_idl_row *ref_row;
-
-                    ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
-                                                         ref_show->table,
-                                                         &datum->keys[j].uuid);
-                    if (ref_row) {
-                        cmd_show_row(ctx, ref_row, level + 1);
-                    }
-                }
-                continue;
-            }
-        }
-
-        if (!ovsdb_datum_is_default(datum, &column->type)) {
-            ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
-            ds_put_format(&ctx->output, "%s: ", column->name);
-            ovsdb_datum_to_string(datum, &column->type, &ctx->output);
-            ds_put_char(&ctx->output, '\n');
-        }
-    }
-    show->recurse = false;
-}
-
-static void
-cmd_show(struct ctl_context *ctx)
-{
-    const struct ovsdb_idl_row *row;
-
-    for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
-         row; row = ovsdb_idl_next_row(row)) {
-        cmd_show_row(ctx, row, 0);
-    }
-}
+    {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
+};
 
 static void
 pre_cmd_emer_reset(struct ctl_context *ctx)
@@ -1208,10 +1090,8 @@ cmd_emer_reset(struct ctl_context *ctx)
         /* We only want to save the "hwaddr" key from other_config. */
         hwaddr = smap_get(&br->other_config, "hwaddr");
         if (hwaddr) {
-            struct smap smap = SMAP_INITIALIZER(&smap);
-            smap_add(&smap, "hwaddr", hwaddr);
+            const struct smap smap = SMAP_CONST1(&smap, "hwaddr", hwaddr);
             ovsrec_bridge_set_other_config(br, &smap);
-            smap_destroy(&smap);
         } else {
             ovsrec_bridge_set_other_config(br, NULL);
         }
@@ -1477,7 +1357,7 @@ cmd_br_exists(struct ctl_context *ctx)
 
     vsctl_context_populate_cache(ctx);
     if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
-        ctl_exit(2);
+        vsctl_exit(2);
     }
 }
 
@@ -1690,8 +1570,8 @@ add_port(struct ctl_context *ctx,
     }
 
     for (i = 0; i < n_settings; i++) {
-        set_column(get_table("Port"), &port->header_, settings[i],
-                   ctx->symtab);
+        ctl_set_column("Port", &port->header_, settings[i],
+                       ctx->symtab);
     }
 
     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
@@ -2271,7 +2151,7 @@ cmd_add_aa_mapping(struct ctl_context *ctx)
         br = br->parent;
     }
 
-    if (br && br->br_cfg) {
+    if (br->br_cfg) {
         if (!br->br_cfg->auto_attach) {
             struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
             ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
@@ -2331,7 +2211,7 @@ cmd_del_aa_mapping(struct ctl_context *ctx)
         br = br->parent;
     }
 
-    if (br && br->br_cfg && br->br_cfg->auto_attach &&
+    if (br->br_cfg && br->br_cfg->auto_attach &&
         br->br_cfg->auto_attach->key_mappings &&
         br->br_cfg->auto_attach->value_mappings) {
         size_t i;
@@ -2382,7 +2262,7 @@ cmd_get_aa_mapping(struct ctl_context *ctx)
 
     verify_auto_attach(br->br_cfg);
 
-    if (br && br->br_cfg && br->br_cfg->auto_attach &&
+    if (br->br_cfg && br->br_cfg->auto_attach &&
         br->br_cfg->auto_attach->key_mappings &&
         br->br_cfg->auto_attach->value_mappings) {
         size_t i;
@@ -2396,7 +2276,7 @@ cmd_get_aa_mapping(struct ctl_context *ctx)
 }
 
 \f
-const struct ctl_table_class tables[] = {
+static const struct ctl_table_class tables[] = {
     {&ovsrec_table_bridge,
      {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
       {&ovsrec_table_flow_sample_collector_set, NULL,
@@ -2587,6 +2467,44 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands,
     }
 }
 
+static char *
+vsctl_parent_process_info(void)
+{
+#ifdef __linux__
+    pid_t parent_pid;
+    char *procfile;
+    struct ds s;
+    FILE *f;
+
+    parent_pid = getppid();
+    procfile = xasprintf("/proc/%d/cmdline", parent_pid);
+
+    f = fopen(procfile, "r");
+    if (!f) {
+        VLOG_WARN("%s: open failed (%s)", procfile, ovs_strerror(errno));
+        free(procfile);
+        return NULL;
+    }
+    free(procfile);
+
+    ds_init(&s);
+    for (;;) {
+        int c = getc(f);
+        if (!c || c == EOF) {
+            break;
+        }
+        ds_put_char(&s, c);
+    }
+    fclose(f);
+
+    ds_put_format(&s, " (pid %d)", parent_pid);
+
+    return ds_steal_cstr(&s);
+#else
+    return NULL;
+#endif
+}
+
 static void
 do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
          struct ovsdb_idl *idl)
@@ -2600,13 +2518,21 @@ do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
     struct shash_node *node;
     int64_t next_cfg = 0;
     char *error = NULL;
+    char *ppid_info = NULL;
 
     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
     if (dry_run) {
         ovsdb_idl_txn_set_dry_run(txn);
     }
 
-    ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
+    ppid_info = vsctl_parent_process_info();
+    if (ppid_info) {
+        ovsdb_idl_txn_add_comment(txn, "ovs-vsctl (invoked by %s): %s",
+                                  ppid_info, args);
+        free(ppid_info);
+    } else {
+        ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
+    }
 
     ovs = ovsrec_open_vswitch_first(idl);
     if (!ovs) {
@@ -2782,6 +2708,23 @@ try_again:
     free(error);
 }
 
+/* Frees the current transaction and the underlying IDL and then calls
+ * exit(status).
+ *
+ * Freeing the transaction and the IDL is not strictly necessary, but it makes
+ * for a clean memory leak report from valgrind in the normal case.  That makes
+ * it easier to notice real memory leaks. */
+static void
+vsctl_exit(int status)
+{
+    if (the_idl_txn) {
+        ovsdb_idl_txn_abort(the_idl_txn);
+        ovsdb_idl_txn_destroy(the_idl_txn);
+    }
+    ovsdb_idl_destroy(the_idl);
+    exit(status);
+}
+
 /*
  * Developers who add new commands to the 'struct ctl_command_syntax' must
  * define the 'arguments' member of the struct.  The following keywords are
@@ -2803,7 +2746,6 @@ try_again:
 static const struct ctl_command_syntax vsctl_commands[] = {
     /* Open vSwitch commands. */
     {"init", 0, 0, "", NULL, cmd_init, NULL, "", RW},
-    {"show", 0, 0, "", pre_cmd_show, cmd_show, NULL, "", RO},
 
     /* Bridge commands. */
     {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info,
@@ -2884,6 +2826,6 @@ static const struct ctl_command_syntax vsctl_commands[] = {
 static void
 vsctl_cmd_init(void)
 {
-    ctl_init();
+    ctl_init(tables, cmd_show_tables, vsctl_exit);
     ctl_register_commands(vsctl_commands);
 }