db-ctl-base: do not expose get_table() API
[cascardo/ovs.git] / utilities / ovs-vsctl.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <float.h>
22 #include <getopt.h>
23 #include <inttypes.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "db-ctl-base.h"
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "dynamic-string.h"
35 #include "fatal-signal.h"
36 #include "hash.h"
37 #include "json.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-idl.h"
40 #include "poll-loop.h"
41 #include "process.h"
42 #include "stream.h"
43 #include "stream-ssl.h"
44 #include "smap.h"
45 #include "sset.h"
46 #include "svec.h"
47 #include "lib/vswitch-idl.h"
48 #include "table.h"
49 #include "timeval.h"
50 #include "util.h"
51 #include "openvswitch/vconn.h"
52 #include "openvswitch/vlog.h"
53
54 VLOG_DEFINE_THIS_MODULE(vsctl);
55
56 struct vsctl_context;
57
58 /* --db: The database server to contact. */
59 static const char *db;
60
61 /* --oneline: Write each command's output as a single line? */
62 static bool oneline;
63
64 /* --dry-run: Do not commit any changes. */
65 static bool dry_run;
66
67 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
68 static bool wait_for_reload = true;
69
70 /* --timeout: Time to wait for a connection to 'db'. */
71 static int timeout;
72
73 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
74  * If false and --db says to use an active connection method (e.g. "unix:",
75  * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
76  * error if the database server cannot be contacted (e.g. ovsdb-server is not
77  * running).
78  *
79  * Regardless of this setting, --timeout always limits how long ovs-vsctl will
80  * wait. */
81 static bool retry;
82
83 /* Format for table output. */
84 static struct table_style table_style = TABLE_STYLE_DEFAULT;
85
86 static void vsctl_cmd_init(void);
87 OVS_NO_RETURN static void usage(void);
88 static void parse_options(int argc, char *argv[], struct shash *local_options);
89 static void run_prerequisites(struct ctl_command[], size_t n_commands,
90                               struct ovsdb_idl *);
91 static void do_vsctl(const char *args, struct ctl_command *, size_t n,
92                      struct ovsdb_idl *);
93
94 /* post_db_reload_check frame work is to allow ovs-vsctl to do additional
95  * checks after OVSDB transactions are successfully recorded and reload by
96  * ovs-vswitchd.
97  *
98  * For example, When a new interface is added to OVSDB, ovs-vswitchd will
99  * either store a positive values on successful implementing the new
100  * interface, or -1 on failure.
101  *
102  * Unless -no-wait command line option is specified,
103  * post_db_reload_do_checks() is called right after any configuration
104  * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
105  * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
106  * more detailed messages about those errors.
107  *
108  * Current implementation only check for Post OVSDB reload failures on new
109  * interface additions with 'add-br' and 'add-port' commands.
110  *
111  * post_db_reload_expect_iface()
112  *
113  * keep track of interfaces to be checked post OVSDB reload. */
114 static void post_db_reload_check_init(void);
115 static void post_db_reload_do_checks(const struct vsctl_context *);
116 static void post_db_reload_expect_iface(const struct ovsrec_interface *);
117
118 static struct uuid *neoteric_ifaces;
119 static size_t n_neoteric_ifaces;
120 static size_t allocated_neoteric_ifaces;
121
122 int
123 main(int argc, char *argv[])
124 {
125     extern struct vlog_module VLM_reconnect;
126     struct ovsdb_idl *idl;
127     struct ctl_command *commands;
128     struct shash local_options;
129     unsigned int seqno;
130     size_t n_commands;
131     char *args;
132
133     set_program_name(argv[0]);
134     fatal_ignore_sigpipe();
135     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
136     vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
137     ovsrec_init();
138
139     vsctl_cmd_init();
140
141     /* Log our arguments.  This is often valuable for debugging systems. */
142     args = process_escape_args(argv);
143     VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
144
145     /* Parse command line. */
146     shash_init(&local_options);
147     parse_options(argc, argv, &local_options);
148     commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
149                                   &n_commands);
150
151     if (timeout) {
152         time_alarm(timeout);
153     }
154
155     /* Initialize IDL. */
156     idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
157     run_prerequisites(commands, n_commands, idl);
158
159     /* Execute the commands.
160      *
161      * 'seqno' is the database sequence number for which we last tried to
162      * execute our transaction.  There's no point in trying to commit more than
163      * once for any given sequence number, because if the transaction fails
164      * it's because the database changed and we need to obtain an up-to-date
165      * view of the database before we try the transaction again. */
166     seqno = ovsdb_idl_get_seqno(idl);
167     for (;;) {
168         ovsdb_idl_run(idl);
169         if (!ovsdb_idl_is_alive(idl)) {
170             int retval = ovsdb_idl_get_last_error(idl);
171             ctl_fatal("%s: database connection failed (%s)",
172                         db, ovs_retval_to_string(retval));
173         }
174
175         if (seqno != ovsdb_idl_get_seqno(idl)) {
176             seqno = ovsdb_idl_get_seqno(idl);
177             do_vsctl(args, commands, n_commands, idl);
178         }
179
180         if (seqno == ovsdb_idl_get_seqno(idl)) {
181             ovsdb_idl_wait(idl);
182             poll_block();
183         }
184     }
185 }
186
187 static void
188 parse_options(int argc, char *argv[], struct shash *local_options)
189 {
190     enum {
191         OPT_DB = UCHAR_MAX + 1,
192         OPT_ONELINE,
193         OPT_NO_SYSLOG,
194         OPT_NO_WAIT,
195         OPT_DRY_RUN,
196         OPT_PEER_CA_CERT,
197         OPT_LOCAL,
198         OPT_RETRY,
199         OPT_COMMANDS,
200         OPT_OPTIONS,
201         VLOG_OPTION_ENUMS,
202         TABLE_OPTION_ENUMS
203     };
204     static const struct option global_long_options[] = {
205         {"db", required_argument, NULL, OPT_DB},
206         {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
207         {"no-wait", no_argument, NULL, OPT_NO_WAIT},
208         {"dry-run", no_argument, NULL, OPT_DRY_RUN},
209         {"oneline", no_argument, NULL, OPT_ONELINE},
210         {"timeout", required_argument, NULL, 't'},
211         {"retry", no_argument, NULL, OPT_RETRY},
212         {"help", no_argument, NULL, 'h'},
213         {"commands", no_argument, NULL, OPT_COMMANDS},
214         {"options", no_argument, NULL, OPT_OPTIONS},
215         {"version", no_argument, NULL, 'V'},
216         VLOG_LONG_OPTIONS,
217         TABLE_LONG_OPTIONS,
218         STREAM_SSL_LONG_OPTIONS,
219         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
220         {NULL, 0, NULL, 0},
221     };
222     const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
223     char *tmp, *short_options;
224
225     struct option *options;
226     size_t allocated_options;
227     size_t n_options;
228     size_t i;
229
230     tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
231     short_options = xasprintf("+%s", tmp);
232     free(tmp);
233
234     /* We want to parse both global and command-specific options here, but
235      * getopt_long() isn't too convenient for the job.  We copy our global
236      * options into a dynamic array, then append all of the command-specific
237      * options. */
238     options = xmemdup(global_long_options, sizeof global_long_options);
239     allocated_options = ARRAY_SIZE(global_long_options);
240     n_options = n_global_long_options;
241     ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
242     table_style.format = TF_LIST;
243
244     for (;;) {
245         int idx;
246         int c;
247
248         c = getopt_long(argc, argv, short_options, options, &idx);
249         if (c == -1) {
250             break;
251         }
252
253         switch (c) {
254         case OPT_DB:
255             db = optarg;
256             break;
257
258         case OPT_ONELINE:
259             oneline = true;
260             break;
261
262         case OPT_NO_SYSLOG:
263             vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
264             break;
265
266         case OPT_NO_WAIT:
267             wait_for_reload = false;
268             break;
269
270         case OPT_DRY_RUN:
271             dry_run = true;
272             break;
273
274         case OPT_LOCAL:
275             if (shash_find(local_options, options[idx].name)) {
276                 ctl_fatal("'%s' option specified multiple times",
277                             options[idx].name);
278             }
279             shash_add_nocopy(local_options,
280                              xasprintf("--%s", options[idx].name),
281                              optarg ? xstrdup(optarg) : NULL);
282             break;
283
284         case 'h':
285             usage();
286
287         case OPT_COMMANDS:
288             ctl_print_commands();
289
290         case OPT_OPTIONS:
291             ctl_print_options(global_long_options);
292
293         case 'V':
294             ovs_print_version(0, 0);
295             printf("DB Schema %s\n", ovsrec_get_db_version());
296             exit(EXIT_SUCCESS);
297
298         case 't':
299             timeout = strtoul(optarg, NULL, 10);
300             if (timeout < 0) {
301                 ctl_fatal("value %s on -t or --timeout is invalid",
302                             optarg);
303             }
304             break;
305
306         case OPT_RETRY:
307             retry = true;
308             break;
309
310         VLOG_OPTION_HANDLERS
311         TABLE_OPTION_HANDLERS(&table_style)
312
313         STREAM_SSL_OPTION_HANDLERS
314
315         case OPT_PEER_CA_CERT:
316             stream_ssl_set_peer_ca_cert_file(optarg);
317             break;
318
319         case '?':
320             exit(EXIT_FAILURE);
321
322         default:
323             abort();
324         }
325     }
326     free(short_options);
327
328     if (!db) {
329         db = ctl_default_db();
330     }
331
332     for (i = n_global_long_options; options[i].name; i++) {
333         free(CONST_CAST(char *, options[i].name));
334     }
335     free(options);
336 }
337
338 static void
339 usage(void)
340 {
341     printf("\
342 %s: ovs-vswitchd management utility\n\
343 usage: %s [OPTIONS] COMMAND [ARG...]\n\
344 \n\
345 Open vSwitch commands:\n\
346   init                        initialize database, if not yet initialized\n\
347   show                        print overview of database contents\n\
348   emer-reset                  reset configuration to clean state\n\
349 \n\
350 Bridge commands:\n\
351   add-br BRIDGE               create a new bridge named BRIDGE\n\
352   add-br BRIDGE PARENT VLAN   create new fake BRIDGE in PARENT on VLAN\n\
353   del-br BRIDGE               delete BRIDGE and all of its ports\n\
354   list-br                     print the names of all the bridges\n\
355   br-exists BRIDGE            exit 2 if BRIDGE does not exist\n\
356   br-to-vlan BRIDGE           print the VLAN which BRIDGE is on\n\
357   br-to-parent BRIDGE         print the parent of BRIDGE\n\
358   br-set-external-id BRIDGE KEY VALUE  set KEY on BRIDGE to VALUE\n\
359   br-set-external-id BRIDGE KEY  unset KEY on BRIDGE\n\
360   br-get-external-id BRIDGE KEY  print value of KEY on BRIDGE\n\
361   br-get-external-id BRIDGE  list key-value pairs on BRIDGE\n\
362 \n\
363 Port commands (a bond is considered to be a single port):\n\
364   list-ports BRIDGE           print the names of all the ports on BRIDGE\n\
365   add-port BRIDGE PORT        add network device PORT to BRIDGE\n\
366   add-bond BRIDGE PORT IFACE...  add bonded port PORT in BRIDGE from IFACES\n\
367   del-port [BRIDGE] PORT      delete PORT (which may be bonded) from BRIDGE\n\
368   port-to-br PORT             print name of bridge that contains PORT\n\
369 \n\
370 Interface commands (a bond consists of multiple interfaces):\n\
371   list-ifaces BRIDGE          print the names of all interfaces on BRIDGE\n\
372   iface-to-br IFACE           print name of bridge that contains IFACE\n\
373 \n\
374 Controller commands:\n\
375   get-controller BRIDGE      print the controllers for BRIDGE\n\
376   del-controller BRIDGE      delete the controllers for BRIDGE\n\
377   set-controller BRIDGE TARGET...  set the controllers for BRIDGE\n\
378   get-fail-mode BRIDGE       print the fail-mode for BRIDGE\n\
379   del-fail-mode BRIDGE       delete the fail-mode for BRIDGE\n\
380   set-fail-mode BRIDGE MODE  set the fail-mode for BRIDGE to MODE\n\
381 \n\
382 Manager commands:\n\
383   get-manager                print the managers\n\
384   del-manager                delete the managers\n\
385   set-manager TARGET...      set the list of managers to TARGET...\n\
386 \n\
387 SSL commands:\n\
388   get-ssl                     print the SSL configuration\n\
389   del-ssl                     delete the SSL configuration\n\
390   set-ssl PRIV-KEY CERT CA-CERT  set the SSL configuration\n\
391 \n\
392 Auto Attach commands:\n\
393   add-aa-mapping BRIDGE I-SID VLAN   add Auto Attach mapping to BRIDGE\n\
394   del-aa-mapping BRIDGE I-SID VLAN   delete Auto Attach mapping VLAN from BRIDGE\n\
395   get-aa-mapping BRIDGE              get Auto Attach mappings from BRIDGE\n\
396 \n\
397 Switch commands:\n\
398   emer-reset                  reset switch to known good state\n\
399 \n\
400 %s\
401 \n\
402 Options:\n\
403   --db=DATABASE               connect to DATABASE\n\
404                               (default: %s)\n\
405   --no-wait                   do not wait for ovs-vswitchd to reconfigure\n\
406   --retry                     keep trying to connect to server forever\n\
407   -t, --timeout=SECS          wait at most SECS seconds for ovs-vswitchd\n\
408   --dry-run                   do not commit changes to database\n\
409   --oneline                   print exactly one line of output per command\n",
410            program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
411     vlog_usage();
412     printf("\
413   --no-syslog             equivalent to --verbose=vsctl:syslog:warn\n");
414     stream_usage("database", true, true, false);
415     printf("\n\
416 Other options:\n\
417   -h, --help                  display this help message\n\
418   -V, --version               display version information\n");
419     exit(EXIT_SUCCESS);
420 }
421
422 \f
423 /* ovs-vsctl specific context.  Inherits the 'struct ctl_context' as base. */
424 struct vsctl_context {
425     struct ctl_context base;
426
427     /* Modifiable state. */
428     const struct ovsrec_open_vswitch *ovs;
429     bool verified_ports;
430
431     /* A cache of the contents of the database.
432      *
433      * A command that needs to use any of this information must first call
434      * vsctl_context_populate_cache().  A command that changes anything that
435      * could invalidate the cache must either call
436      * vsctl_context_invalidate_cache() or manually update the cache to
437      * maintain its correctness. */
438     bool cache_valid;
439     struct shash bridges;   /* Maps from bridge name to struct vsctl_bridge. */
440     struct shash ports;     /* Maps from port name to struct vsctl_port. */
441     struct shash ifaces;    /* Maps from port name to struct vsctl_iface. */
442 };
443
444 struct vsctl_bridge {
445     struct ovsrec_bridge *br_cfg;
446     char *name;
447     struct ovs_list ports;      /* Contains "struct vsctl_port"s. */
448
449     /* VLAN ("fake") bridge support.
450      *
451      * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
452      * in either case. */
453     struct hmap children;        /* VLAN bridges indexed by 'vlan'. */
454     struct hmap_node children_node; /* Node in parent's 'children' hmap. */
455     struct vsctl_bridge *parent; /* Real bridge, or NULL. */
456     int vlan;                    /* VLAN VID (0...4095), or 0. */
457 };
458
459 struct vsctl_port {
460     struct ovs_list ports_node;  /* In struct vsctl_bridge's 'ports' list. */
461     struct ovs_list ifaces;      /* Contains "struct vsctl_iface"s. */
462     struct ovsrec_port *port_cfg;
463     struct vsctl_bridge *bridge;
464 };
465
466 struct vsctl_iface {
467     struct ovs_list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
468     struct ovsrec_interface *iface_cfg;
469     struct vsctl_port *port;
470 };
471
472 /* Casts 'base' into 'strcut vsctl_context'. */
473 static struct vsctl_context *
474 vsctl_context_cast(struct ctl_context *base)
475 {
476     return CONTAINER_OF(base, struct vsctl_context, base);
477 }
478
479 static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
480                                              int vlan);
481
482 static char *
483 vsctl_context_to_string(const struct ctl_context *ctx)
484 {
485     const struct shash_node *node;
486     struct svec words;
487     char *s;
488     int i;
489
490     svec_init(&words);
491     SHASH_FOR_EACH (node, &ctx->options) {
492         svec_add(&words, node->name);
493     }
494     for (i = 0; i < ctx->argc; i++) {
495         svec_add(&words, ctx->argv[i]);
496     }
497     svec_terminate(&words);
498
499     s = process_escape_args(words.names);
500
501     svec_destroy(&words);
502
503     return s;
504 }
505
506 static void
507 verify_ports(struct vsctl_context *vsctl_ctx)
508 {
509     if (!vsctl_ctx->verified_ports) {
510         const struct ovsrec_bridge *bridge;
511         const struct ovsrec_port *port;
512
513         ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
514         OVSREC_BRIDGE_FOR_EACH (bridge, vsctl_ctx->base.idl) {
515             ovsrec_bridge_verify_ports(bridge);
516         }
517         OVSREC_PORT_FOR_EACH (port, vsctl_ctx->base.idl) {
518             ovsrec_port_verify_interfaces(port);
519         }
520
521         vsctl_ctx->verified_ports = true;
522     }
523 }
524
525 static struct vsctl_bridge *
526 add_bridge_to_cache(struct vsctl_context *vsctl_ctx,
527                     struct ovsrec_bridge *br_cfg, const char *name,
528                     struct vsctl_bridge *parent, int vlan)
529 {
530     struct vsctl_bridge *br = xmalloc(sizeof *br);
531     br->br_cfg = br_cfg;
532     br->name = xstrdup(name);
533     list_init(&br->ports);
534     br->parent = parent;
535     br->vlan = vlan;
536     hmap_init(&br->children);
537     if (parent) {
538         struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
539         if (conflict) {
540             VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
541                       "for VLAN %d, but only one is allowed",
542                       parent->name, name, conflict->name, vlan);
543         } else {
544             hmap_insert(&parent->children, &br->children_node,
545                         hash_int(vlan, 0));
546         }
547     }
548     shash_add(&vsctl_ctx->bridges, br->name, br);
549     return br;
550 }
551
552 static void
553 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
554                   struct ovsrec_bridge *bridge)
555 {
556     struct ovsrec_bridge **bridges;
557     size_t i, n;
558
559     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
560     for (i = n = 0; i < ovs->n_bridges; i++) {
561         if (ovs->bridges[i] != bridge) {
562             bridges[n++] = ovs->bridges[i];
563         }
564     }
565     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
566     free(bridges);
567 }
568
569 static void
570 del_cached_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
571 {
572     ovs_assert(list_is_empty(&br->ports));
573     ovs_assert(hmap_is_empty(&br->children));
574     if (br->parent) {
575         hmap_remove(&br->parent->children, &br->children_node);
576     }
577     if (br->br_cfg) {
578         ovsrec_bridge_delete(br->br_cfg);
579         ovs_delete_bridge(vsctl_ctx->ovs, br->br_cfg);
580     }
581     shash_find_and_delete(&vsctl_ctx->bridges, br->name);
582     hmap_destroy(&br->children);
583     free(br->name);
584     free(br);
585 }
586
587 static bool
588 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
589 {
590     return (port_cfg->fake_bridge
591             && port_cfg->tag
592             && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
593 }
594
595 static struct vsctl_bridge *
596 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
597 {
598     struct vsctl_bridge *child;
599
600     HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
601                              &parent->children) {
602         if (child->vlan == vlan) {
603             return child;
604         }
605     }
606
607     return NULL;
608 }
609
610 static struct vsctl_port *
611 add_port_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *parent,
612                   struct ovsrec_port *port_cfg)
613 {
614     struct vsctl_port *port;
615
616     if (port_cfg->tag
617         && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
618         struct vsctl_bridge *vlan_bridge;
619
620         vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
621         if (vlan_bridge) {
622             parent = vlan_bridge;
623         }
624     }
625
626     port = xmalloc(sizeof *port);
627     list_push_back(&parent->ports, &port->ports_node);
628     list_init(&port->ifaces);
629     port->port_cfg = port_cfg;
630     port->bridge = parent;
631     shash_add(&vsctl_ctx->ports, port_cfg->name, port);
632
633     return port;
634 }
635
636 static void
637 del_cached_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
638 {
639     ovs_assert(list_is_empty(&port->ifaces));
640     list_remove(&port->ports_node);
641     shash_find_and_delete(&vsctl_ctx->ports, port->port_cfg->name);
642     ovsrec_port_delete(port->port_cfg);
643     free(port);
644 }
645
646 static struct vsctl_iface *
647 add_iface_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_port *parent,
648                    struct ovsrec_interface *iface_cfg)
649 {
650     struct vsctl_iface *iface;
651
652     iface = xmalloc(sizeof *iface);
653     list_push_back(&parent->ifaces, &iface->ifaces_node);
654     iface->iface_cfg = iface_cfg;
655     iface->port = parent;
656     shash_add(&vsctl_ctx->ifaces, iface_cfg->name, iface);
657
658     return iface;
659 }
660
661 static void
662 del_cached_iface(struct vsctl_context *vsctl_ctx, struct vsctl_iface *iface)
663 {
664     list_remove(&iface->ifaces_node);
665     shash_find_and_delete(&vsctl_ctx->ifaces, iface->iface_cfg->name);
666     ovsrec_interface_delete(iface->iface_cfg);
667     free(iface);
668 }
669
670 static void
671 vsctl_context_invalidate_cache(struct ctl_context *ctx)
672 {
673     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
674     struct shash_node *node;
675
676     if (!vsctl_ctx->cache_valid) {
677         return;
678     }
679     vsctl_ctx->cache_valid = false;
680
681     SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
682         struct vsctl_bridge *bridge = node->data;
683         hmap_destroy(&bridge->children);
684         free(bridge->name);
685         free(bridge);
686     }
687     shash_destroy(&vsctl_ctx->bridges);
688
689     shash_destroy_free_data(&vsctl_ctx->ports);
690     shash_destroy_free_data(&vsctl_ctx->ifaces);
691 }
692
693 static void
694 pre_get_info(struct ctl_context *ctx)
695 {
696     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
697
698     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
699     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
700     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
701     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
702
703     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
704     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
705     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
706     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
707
708     ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
709
710     ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
711 }
712
713 static void
714 vsctl_context_populate_cache(struct ctl_context *ctx)
715 {
716     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
717     const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
718     struct sset bridges, ports;
719     size_t i;
720
721     if (vsctl_ctx->cache_valid) {
722         /* Cache is already populated. */
723         return;
724     }
725     vsctl_ctx->cache_valid = true;
726     shash_init(&vsctl_ctx->bridges);
727     shash_init(&vsctl_ctx->ports);
728     shash_init(&vsctl_ctx->ifaces);
729
730     sset_init(&bridges);
731     sset_init(&ports);
732     for (i = 0; i < ovs->n_bridges; i++) {
733         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
734         struct vsctl_bridge *br;
735         size_t j;
736
737         if (!sset_add(&bridges, br_cfg->name)) {
738             VLOG_WARN("%s: database contains duplicate bridge name",
739                       br_cfg->name);
740             continue;
741         }
742         br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
743         if (!br) {
744             continue;
745         }
746
747         for (j = 0; j < br_cfg->n_ports; j++) {
748             struct ovsrec_port *port_cfg = br_cfg->ports[j];
749
750             if (!sset_add(&ports, port_cfg->name)) {
751                 /* Duplicate port name.  (We will warn about that later.) */
752                 continue;
753             }
754
755             if (port_is_fake_bridge(port_cfg)
756                 && sset_add(&bridges, port_cfg->name)) {
757                 add_bridge_to_cache(vsctl_ctx, NULL, port_cfg->name, br,
758                                     *port_cfg->tag);
759             }
760         }
761     }
762     sset_destroy(&bridges);
763     sset_destroy(&ports);
764
765     sset_init(&bridges);
766     for (i = 0; i < ovs->n_bridges; i++) {
767         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
768         struct vsctl_bridge *br;
769         size_t j;
770
771         if (!sset_add(&bridges, br_cfg->name)) {
772             continue;
773         }
774         br = shash_find_data(&vsctl_ctx->bridges, br_cfg->name);
775         for (j = 0; j < br_cfg->n_ports; j++) {
776             struct ovsrec_port *port_cfg = br_cfg->ports[j];
777             struct vsctl_port *port;
778             size_t k;
779
780             port = shash_find_data(&vsctl_ctx->ports, port_cfg->name);
781             if (port) {
782                 if (port_cfg == port->port_cfg) {
783                     VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
784                               port_cfg->name, br->name, port->bridge->name);
785                 } else {
786                     /* Log as an error because this violates the database's
787                      * uniqueness constraints, so the database server shouldn't
788                      * have allowed it. */
789                     VLOG_ERR("%s: database contains duplicate port name",
790                              port_cfg->name);
791                 }
792                 continue;
793             }
794
795             if (port_is_fake_bridge(port_cfg)
796                 && !sset_add(&bridges, port_cfg->name)) {
797                 continue;
798             }
799
800             port = add_port_to_cache(vsctl_ctx, br, port_cfg);
801             for (k = 0; k < port_cfg->n_interfaces; k++) {
802                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
803                 struct vsctl_iface *iface;
804
805                 iface = shash_find_data(&vsctl_ctx->ifaces, iface_cfg->name);
806                 if (iface) {
807                     if (iface_cfg == iface->iface_cfg) {
808                         VLOG_WARN("%s: interface is in multiple ports "
809                                   "(%s and %s)",
810                                   iface_cfg->name,
811                                   iface->port->port_cfg->name,
812                                   port->port_cfg->name);
813                     } else {
814                         /* Log as an error because this violates the database's
815                          * uniqueness constraints, so the database server
816                          * shouldn't have allowed it. */
817                         VLOG_ERR("%s: database contains duplicate interface "
818                                  "name", iface_cfg->name);
819                     }
820                     continue;
821                 }
822
823                 add_iface_to_cache(vsctl_ctx, port, iface_cfg);
824             }
825         }
826     }
827     sset_destroy(&bridges);
828 }
829
830 static void
831 check_conflicts(struct vsctl_context *vsctl_ctx, const char *name,
832                 char *msg)
833 {
834     struct vsctl_iface *iface;
835     struct vsctl_port *port;
836
837     verify_ports(vsctl_ctx);
838
839     if (shash_find(&vsctl_ctx->bridges, name)) {
840         ctl_fatal("%s because a bridge named %s already exists",
841                     msg, name);
842     }
843
844     port = shash_find_data(&vsctl_ctx->ports, name);
845     if (port) {
846         ctl_fatal("%s because a port named %s already exists on "
847                     "bridge %s", msg, name, port->bridge->name);
848     }
849
850     iface = shash_find_data(&vsctl_ctx->ifaces, name);
851     if (iface) {
852         ctl_fatal("%s because an interface named %s already exists "
853                     "on bridge %s", msg, name, iface->port->bridge->name);
854     }
855
856     free(msg);
857 }
858
859 static struct vsctl_bridge *
860 find_bridge(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
861 {
862     struct vsctl_bridge *br;
863
864     ovs_assert(vsctl_ctx->cache_valid);
865
866     br = shash_find_data(&vsctl_ctx->bridges, name);
867     if (must_exist && !br) {
868         ctl_fatal("no bridge named %s", name);
869     }
870     ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
871     return br;
872 }
873
874 static struct vsctl_bridge *
875 find_real_bridge(struct vsctl_context *vsctl_ctx,
876                  const char *name, bool must_exist)
877 {
878     struct vsctl_bridge *br = find_bridge(vsctl_ctx, name, must_exist);
879     if (br && br->parent) {
880         ctl_fatal("%s is a fake bridge", name);
881     }
882     return br;
883 }
884
885 static struct vsctl_port *
886 find_port(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
887 {
888     struct vsctl_port *port;
889
890     ovs_assert(vsctl_ctx->cache_valid);
891
892     port = shash_find_data(&vsctl_ctx->ports, name);
893     if (port && !strcmp(name, port->bridge->name)) {
894         port = NULL;
895     }
896     if (must_exist && !port) {
897         ctl_fatal("no port named %s", name);
898     }
899     verify_ports(vsctl_ctx);
900     return port;
901 }
902
903 static struct vsctl_iface *
904 find_iface(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
905 {
906     struct vsctl_iface *iface;
907
908     ovs_assert(vsctl_ctx->cache_valid);
909
910     iface = shash_find_data(&vsctl_ctx->ifaces, name);
911     if (iface && !strcmp(name, iface->port->bridge->name)) {
912         iface = NULL;
913     }
914     if (must_exist && !iface) {
915         ctl_fatal("no interface named %s", name);
916     }
917     verify_ports(vsctl_ctx);
918     return iface;
919 }
920
921 static void
922 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
923 {
924     struct ovsrec_port **ports;
925     size_t i;
926
927     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
928     for (i = 0; i < br->n_ports; i++) {
929         ports[i] = br->ports[i];
930     }
931     ports[br->n_ports] = port;
932     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
933     free(ports);
934 }
935
936 static void
937 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
938 {
939     struct ovsrec_port **ports;
940     size_t i, n;
941
942     ports = xmalloc(sizeof *br->ports * br->n_ports);
943     for (i = n = 0; i < br->n_ports; i++) {
944         if (br->ports[i] != port) {
945             ports[n++] = br->ports[i];
946         }
947     }
948     ovsrec_bridge_set_ports(br, ports, n);
949     free(ports);
950 }
951
952 static void
953 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
954                   struct ovsrec_bridge *bridge)
955 {
956     struct ovsrec_bridge **bridges;
957     size_t i;
958
959     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
960     for (i = 0; i < ovs->n_bridges; i++) {
961         bridges[i] = ovs->bridges[i];
962     }
963     bridges[ovs->n_bridges] = bridge;
964     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
965     free(bridges);
966 }
967
968 static void
969 cmd_init(struct ctl_context *ctx OVS_UNUSED)
970 {
971 }
972
973 struct cmd_show_table cmd_show_tables[] = {
974     {&ovsrec_table_open_vswitch,
975      NULL,
976      {&ovsrec_open_vswitch_col_manager_options,
977       &ovsrec_open_vswitch_col_bridges,
978       &ovsrec_open_vswitch_col_ovs_version},
979      false},
980
981     {&ovsrec_table_bridge,
982      &ovsrec_bridge_col_name,
983      {&ovsrec_bridge_col_controller,
984       &ovsrec_bridge_col_fail_mode,
985       &ovsrec_bridge_col_ports},
986      false},
987
988     {&ovsrec_table_port,
989      &ovsrec_port_col_name,
990      {&ovsrec_port_col_tag,
991       &ovsrec_port_col_trunks,
992       &ovsrec_port_col_interfaces},
993      false},
994
995     {&ovsrec_table_interface,
996      &ovsrec_interface_col_name,
997      {&ovsrec_interface_col_type,
998       &ovsrec_interface_col_options,
999       &ovsrec_interface_col_error},
1000      false},
1001
1002     {&ovsrec_table_controller,
1003      &ovsrec_controller_col_target,
1004      {&ovsrec_controller_col_is_connected,
1005       NULL,
1006       NULL},
1007      false},
1008
1009     {&ovsrec_table_manager,
1010      &ovsrec_manager_col_target,
1011      {&ovsrec_manager_col_is_connected,
1012       NULL,
1013       NULL},
1014      false},
1015
1016     {NULL, NULL, {NULL, NULL, NULL}, false}
1017 };
1018
1019 static void
1020 pre_cmd_emer_reset(struct ctl_context *ctx)
1021 {
1022     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1023     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1024
1025     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1026     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1027     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1028     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1029     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1030     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ipfix);
1031     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1032     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1033
1034     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1035
1036     ovsdb_idl_add_column(ctx->idl,
1037                           &ovsrec_interface_col_ingress_policing_rate);
1038     ovsdb_idl_add_column(ctx->idl,
1039                           &ovsrec_interface_col_ingress_policing_burst);
1040 }
1041
1042 static void
1043 cmd_emer_reset(struct ctl_context *ctx)
1044 {
1045     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1046     const struct ovsdb_idl *idl = ctx->idl;
1047     const struct ovsrec_bridge *br;
1048     const struct ovsrec_port *port;
1049     const struct ovsrec_interface *iface;
1050     const struct ovsrec_mirror *mirror, *next_mirror;
1051     const struct ovsrec_controller *ctrl, *next_ctrl;
1052     const struct ovsrec_manager *mgr, *next_mgr;
1053     const struct ovsrec_netflow *nf, *next_nf;
1054     const struct ovsrec_ssl *ssl, *next_ssl;
1055     const struct ovsrec_sflow *sflow, *next_sflow;
1056     const struct ovsrec_ipfix *ipfix, *next_ipfix;
1057     const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1058
1059     /* Reset the Open_vSwitch table. */
1060     ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, NULL, 0);
1061     ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
1062
1063     OVSREC_BRIDGE_FOR_EACH (br, idl) {
1064         const char *hwaddr;
1065
1066         ovsrec_bridge_set_controller(br, NULL, 0);
1067         ovsrec_bridge_set_fail_mode(br, NULL);
1068         ovsrec_bridge_set_mirrors(br, NULL, 0);
1069         ovsrec_bridge_set_netflow(br, NULL);
1070         ovsrec_bridge_set_sflow(br, NULL);
1071         ovsrec_bridge_set_ipfix(br, NULL);
1072         ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1073
1074         /* We only want to save the "hwaddr" key from other_config. */
1075         hwaddr = smap_get(&br->other_config, "hwaddr");
1076         if (hwaddr) {
1077             struct smap smap = SMAP_INITIALIZER(&smap);
1078             smap_add(&smap, "hwaddr", hwaddr);
1079             ovsrec_bridge_set_other_config(br, &smap);
1080             smap_destroy(&smap);
1081         } else {
1082             ovsrec_bridge_set_other_config(br, NULL);
1083         }
1084     }
1085
1086     OVSREC_PORT_FOR_EACH (port, idl) {
1087         ovsrec_port_set_other_config(port, NULL);
1088     }
1089
1090     OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1091         /* xxx What do we do about gre/patch devices created by mgr? */
1092
1093         ovsrec_interface_set_ingress_policing_rate(iface, 0);
1094         ovsrec_interface_set_ingress_policing_burst(iface, 0);
1095     }
1096
1097     OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1098         ovsrec_mirror_delete(mirror);
1099     }
1100
1101     OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1102         ovsrec_controller_delete(ctrl);
1103     }
1104
1105     OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1106         ovsrec_manager_delete(mgr);
1107     }
1108
1109     OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1110         ovsrec_netflow_delete(nf);
1111     }
1112
1113     OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1114         ovsrec_ssl_delete(ssl);
1115     }
1116
1117     OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1118         ovsrec_sflow_delete(sflow);
1119     }
1120
1121     OVSREC_IPFIX_FOR_EACH_SAFE (ipfix, next_ipfix, idl) {
1122         ovsrec_ipfix_delete(ipfix);
1123     }
1124
1125     OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset, idl) {
1126         ovsrec_flow_sample_collector_set_delete(fscset);
1127     }
1128
1129     vsctl_context_invalidate_cache(ctx);
1130 }
1131
1132 static void
1133 cmd_add_br(struct ctl_context *ctx)
1134 {
1135     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1136     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1137     const char *br_name, *parent_name;
1138     struct ovsrec_interface *iface;
1139     int vlan;
1140
1141     br_name = ctx->argv[1];
1142     if (ctx->argc == 2) {
1143         parent_name = NULL;
1144         vlan = 0;
1145     } else if (ctx->argc == 4) {
1146         parent_name = ctx->argv[2];
1147         vlan = atoi(ctx->argv[3]);
1148         if (vlan < 0 || vlan > 4095) {
1149             ctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1150         }
1151     } else {
1152         ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1153                     ctx->argv[0]);
1154     }
1155
1156     vsctl_context_populate_cache(ctx);
1157     if (may_exist) {
1158         struct vsctl_bridge *br;
1159
1160         br = find_bridge(vsctl_ctx, br_name, false);
1161         if (br) {
1162             if (!parent_name) {
1163                 if (br->parent) {
1164                     ctl_fatal("\"--may-exist add-br %s\" but %s is "
1165                                 "a VLAN bridge for VLAN %d",
1166                                 br_name, br_name, br->vlan);
1167                 }
1168             } else {
1169                 if (!br->parent) {
1170                     ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1171                                 "is not a VLAN bridge",
1172                                 br_name, parent_name, vlan, br_name);
1173                 } else if (strcmp(br->parent->name, parent_name)) {
1174                     ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1175                                 "has the wrong parent %s",
1176                                 br_name, parent_name, vlan,
1177                                 br_name, br->parent->name);
1178                 } else if (br->vlan != vlan) {
1179                     ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1180                                 "is a VLAN bridge for the wrong VLAN %d",
1181                                 br_name, parent_name, vlan, br_name, br->vlan);
1182                 }
1183             }
1184             return;
1185         }
1186     }
1187     check_conflicts(vsctl_ctx, br_name,
1188                     xasprintf("cannot create a bridge named %s", br_name));
1189
1190     if (!parent_name) {
1191         struct ovsrec_port *port;
1192         struct ovsrec_bridge *br;
1193
1194         iface = ovsrec_interface_insert(ctx->txn);
1195         ovsrec_interface_set_name(iface, br_name);
1196         ovsrec_interface_set_type(iface, "internal");
1197
1198         port = ovsrec_port_insert(ctx->txn);
1199         ovsrec_port_set_name(port, br_name);
1200         ovsrec_port_set_interfaces(port, &iface, 1);
1201
1202         br = ovsrec_bridge_insert(ctx->txn);
1203         ovsrec_bridge_set_name(br, br_name);
1204         ovsrec_bridge_set_ports(br, &port, 1);
1205
1206         ovs_insert_bridge(vsctl_ctx->ovs, br);
1207     } else {
1208         struct vsctl_bridge *conflict;
1209         struct vsctl_bridge *parent;
1210         struct ovsrec_port *port;
1211         struct ovsrec_bridge *br;
1212         int64_t tag = vlan;
1213
1214         parent = find_bridge(vsctl_ctx, parent_name, false);
1215         if (parent && parent->parent) {
1216             ctl_fatal("cannot create bridge with fake bridge as parent");
1217         }
1218         if (!parent) {
1219             ctl_fatal("parent bridge %s does not exist", parent_name);
1220         }
1221         conflict = find_vlan_bridge(parent, vlan);
1222         if (conflict) {
1223             ctl_fatal("bridge %s already has a child VLAN bridge %s "
1224                         "on VLAN %d", parent_name, conflict->name, vlan);
1225         }
1226         br = parent->br_cfg;
1227
1228         iface = ovsrec_interface_insert(ctx->txn);
1229         ovsrec_interface_set_name(iface, br_name);
1230         ovsrec_interface_set_type(iface, "internal");
1231
1232         port = ovsrec_port_insert(ctx->txn);
1233         ovsrec_port_set_name(port, br_name);
1234         ovsrec_port_set_interfaces(port, &iface, 1);
1235         ovsrec_port_set_fake_bridge(port, true);
1236         ovsrec_port_set_tag(port, &tag, 1);
1237
1238         bridge_insert_port(br, port);
1239     }
1240
1241     post_db_reload_expect_iface(iface);
1242     vsctl_context_invalidate_cache(ctx);
1243 }
1244
1245 static void
1246 del_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
1247 {
1248     struct vsctl_iface *iface, *next_iface;
1249
1250     bridge_delete_port((port->bridge->parent
1251                         ? port->bridge->parent->br_cfg
1252                         : port->bridge->br_cfg), port->port_cfg);
1253
1254     LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1255         del_cached_iface(vsctl_ctx, iface);
1256     }
1257     del_cached_port(vsctl_ctx, port);
1258 }
1259
1260 static void
1261 del_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
1262 {
1263     struct vsctl_bridge *child, *next_child;
1264     struct vsctl_port *port, *next_port;
1265     const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1266
1267     HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1268         del_bridge(vsctl_ctx, child);
1269     }
1270
1271     LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1272         del_port(vsctl_ctx, port);
1273     }
1274
1275     OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset,
1276                                                     vsctl_ctx->base.idl) {
1277         if (fscset->bridge == br->br_cfg) {
1278             ovsrec_flow_sample_collector_set_delete(fscset);
1279         }
1280     }
1281
1282     del_cached_bridge(vsctl_ctx, br);
1283 }
1284
1285 static void
1286 cmd_del_br(struct ctl_context *ctx)
1287 {
1288     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1289     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1290     struct vsctl_bridge *bridge;
1291
1292     vsctl_context_populate_cache(ctx);
1293     bridge = find_bridge(vsctl_ctx, ctx->argv[1], must_exist);
1294     if (bridge) {
1295         del_bridge(vsctl_ctx, bridge);
1296     }
1297 }
1298
1299 static void
1300 output_sorted(struct svec *svec, struct ds *output)
1301 {
1302     const char *name;
1303     size_t i;
1304
1305     svec_sort(svec);
1306     SVEC_FOR_EACH (i, name, svec) {
1307         ds_put_format(output, "%s\n", name);
1308     }
1309 }
1310
1311 static void
1312 cmd_list_br(struct ctl_context *ctx)
1313 {
1314     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1315     struct shash_node *node;
1316     struct svec bridges;
1317     bool real = shash_find(&ctx->options, "--real");
1318     bool fake = shash_find(&ctx->options, "--fake");
1319
1320     /* If neither fake nor real were requested, return both. */
1321     if (!real && !fake) {
1322         real = fake = true;
1323     }
1324
1325     vsctl_context_populate_cache(ctx);
1326
1327     svec_init(&bridges);
1328     SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
1329         struct vsctl_bridge *br = node->data;
1330
1331         if (br->parent ? fake : real) {
1332             svec_add(&bridges, br->name);
1333         }
1334     }
1335     output_sorted(&bridges, &ctx->output);
1336     svec_destroy(&bridges);
1337 }
1338
1339 static void
1340 cmd_br_exists(struct ctl_context *ctx)
1341 {
1342     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1343
1344     vsctl_context_populate_cache(ctx);
1345     if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
1346         ctl_exit(2);
1347     }
1348 }
1349
1350 static void
1351 set_external_id(struct smap *old, struct smap *new,
1352                 char *key, char *value)
1353 {
1354     smap_clone(new, old);
1355
1356     if (value) {
1357         smap_replace(new, key, value);
1358     } else {
1359         smap_remove(new, key);
1360     }
1361 }
1362
1363 static void
1364 pre_cmd_br_set_external_id(struct ctl_context *ctx)
1365 {
1366     pre_get_info(ctx);
1367     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1368     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1369 }
1370
1371 static void
1372 cmd_br_set_external_id(struct ctl_context *ctx)
1373 {
1374     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1375     struct vsctl_bridge *bridge;
1376     struct smap new;
1377
1378     vsctl_context_populate_cache(ctx);
1379     bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1380     if (bridge->br_cfg) {
1381
1382         set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1383                         ctx->argc >= 4 ? ctx->argv[3] : NULL);
1384         ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1385         ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1386     } else {
1387         char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1388         struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1389                                                   ctx->argv[1]);
1390         set_external_id(&port->port_cfg->external_ids, &new,
1391                         key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1392         ovsrec_port_verify_external_ids(port->port_cfg);
1393         ovsrec_port_set_external_ids(port->port_cfg, &new);
1394         free(key);
1395     }
1396     smap_destroy(&new);
1397 }
1398
1399 static void
1400 get_external_id(struct smap *smap, const char *prefix, const char *key,
1401                 struct ds *output)
1402 {
1403     if (key) {
1404         char *prefix_key = xasprintf("%s%s", prefix, key);
1405         const char *value = smap_get(smap, prefix_key);
1406
1407         if (value) {
1408             ds_put_format(output, "%s\n", value);
1409         }
1410         free(prefix_key);
1411     } else {
1412         const struct smap_node **sorted = smap_sort(smap);
1413         size_t prefix_len = strlen(prefix);
1414         size_t i;
1415
1416         for (i = 0; i < smap_count(smap); i++) {
1417             const struct smap_node *node = sorted[i];
1418             if (!strncmp(node->key, prefix, prefix_len)) {
1419                 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1420                               node->value);
1421             }
1422         }
1423         free(sorted);
1424     }
1425 }
1426
1427 static void
1428 pre_cmd_br_get_external_id(struct ctl_context *ctx)
1429 {
1430     pre_cmd_br_set_external_id(ctx);
1431 }
1432
1433 static void
1434 cmd_br_get_external_id(struct ctl_context *ctx)
1435 {
1436     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1437     struct vsctl_bridge *bridge;
1438
1439     vsctl_context_populate_cache(ctx);
1440
1441     bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1442     if (bridge->br_cfg) {
1443         ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1444         get_external_id(&bridge->br_cfg->external_ids, "",
1445                         ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1446     } else {
1447         struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1448                                                   ctx->argv[1]);
1449         ovsrec_port_verify_external_ids(port->port_cfg);
1450         get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1451                         ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1452     }
1453 }
1454
1455 static void
1456 cmd_list_ports(struct ctl_context *ctx)
1457 {
1458     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1459     struct vsctl_bridge *br;
1460     struct vsctl_port *port;
1461     struct svec ports;
1462
1463     vsctl_context_populate_cache(ctx);
1464     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1465     ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1466
1467     svec_init(&ports);
1468     LIST_FOR_EACH (port, ports_node, &br->ports) {
1469         if (strcmp(port->port_cfg->name, br->name)) {
1470             svec_add(&ports, port->port_cfg->name);
1471         }
1472     }
1473     output_sorted(&ports, &ctx->output);
1474     svec_destroy(&ports);
1475 }
1476
1477 static void
1478 add_port(struct ctl_context *ctx,
1479          const char *br_name, const char *port_name,
1480          bool may_exist, bool fake_iface,
1481          char *iface_names[], int n_ifaces,
1482          char *settings[], int n_settings)
1483 {
1484     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1485     struct vsctl_port *vsctl_port;
1486     struct vsctl_bridge *bridge;
1487     struct ovsrec_interface **ifaces;
1488     struct ovsrec_port *port;
1489     size_t i;
1490
1491     vsctl_context_populate_cache(ctx);
1492     if (may_exist) {
1493         struct vsctl_port *vsctl_port;
1494
1495         vsctl_port = find_port(vsctl_ctx, port_name, false);
1496         if (vsctl_port) {
1497             struct svec want_names, have_names;
1498
1499             svec_init(&want_names);
1500             for (i = 0; i < n_ifaces; i++) {
1501                 svec_add(&want_names, iface_names[i]);
1502             }
1503             svec_sort(&want_names);
1504
1505             svec_init(&have_names);
1506             for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1507                 svec_add(&have_names,
1508                          vsctl_port->port_cfg->interfaces[i]->name);
1509             }
1510             svec_sort(&have_names);
1511
1512             if (strcmp(vsctl_port->bridge->name, br_name)) {
1513                 char *command = vsctl_context_to_string(ctx);
1514                 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1515                             command, port_name, vsctl_port->bridge->name);
1516             }
1517
1518             if (!svec_equal(&want_names, &have_names)) {
1519                 char *have_names_string = svec_join(&have_names, ", ", "");
1520                 char *command = vsctl_context_to_string(ctx);
1521
1522                 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1523                             command, port_name, have_names_string);
1524             }
1525
1526             svec_destroy(&want_names);
1527             svec_destroy(&have_names);
1528
1529             return;
1530         }
1531     }
1532     check_conflicts(vsctl_ctx, port_name,
1533                     xasprintf("cannot create a port named %s", port_name));
1534     for (i = 0; i < n_ifaces; i++) {
1535         check_conflicts(vsctl_ctx, iface_names[i],
1536                         xasprintf("cannot create an interface named %s",
1537                                   iface_names[i]));
1538     }
1539     bridge = find_bridge(vsctl_ctx, br_name, true);
1540
1541     ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1542     for (i = 0; i < n_ifaces; i++) {
1543         ifaces[i] = ovsrec_interface_insert(ctx->txn);
1544         ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1545         post_db_reload_expect_iface(ifaces[i]);
1546     }
1547
1548     port = ovsrec_port_insert(ctx->txn);
1549     ovsrec_port_set_name(port, port_name);
1550     ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1551     ovsrec_port_set_bond_fake_iface(port, fake_iface);
1552
1553     if (bridge->parent) {
1554         int64_t tag = bridge->vlan;
1555         ovsrec_port_set_tag(port, &tag, 1);
1556     }
1557
1558     for (i = 0; i < n_settings; i++) {
1559         ctl_set_column("Port", &port->header_, settings[i],
1560                        ctx->symtab);
1561     }
1562
1563     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1564                         : bridge->br_cfg), port);
1565
1566     vsctl_port = add_port_to_cache(vsctl_ctx, bridge, port);
1567     for (i = 0; i < n_ifaces; i++) {
1568         add_iface_to_cache(vsctl_ctx, vsctl_port, ifaces[i]);
1569     }
1570     free(ifaces);
1571 }
1572
1573 static void
1574 cmd_add_port(struct ctl_context *ctx)
1575 {
1576     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1577
1578     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1579              &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1580 }
1581
1582 static void
1583 cmd_add_bond(struct ctl_context *ctx)
1584 {
1585     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1586     bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1587     int n_ifaces;
1588     int i;
1589
1590     n_ifaces = ctx->argc - 3;
1591     for (i = 3; i < ctx->argc; i++) {
1592         if (strchr(ctx->argv[i], '=')) {
1593             n_ifaces = i - 3;
1594             break;
1595         }
1596     }
1597     if (n_ifaces < 2) {
1598         ctl_fatal("add-bond requires at least 2 interfaces, but only "
1599                     "%d were specified", n_ifaces);
1600     }
1601
1602     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1603              &ctx->argv[3], n_ifaces,
1604              &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1605 }
1606
1607 static void
1608 cmd_del_port(struct ctl_context *ctx)
1609 {
1610     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1611     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1612     bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1613     const char *target = ctx->argv[ctx->argc - 1];
1614     struct vsctl_port *port;
1615
1616     vsctl_context_populate_cache(ctx);
1617     if (find_bridge(vsctl_ctx, target, false)) {
1618         if (must_exist) {
1619             ctl_fatal("cannot delete port %s because it is the local port "
1620                         "for bridge %s (deleting this port requires deleting "
1621                         "the entire bridge)", target, target);
1622         }
1623         port = NULL;
1624     } else if (!with_iface) {
1625         port = find_port(vsctl_ctx, target, must_exist);
1626     } else {
1627         struct vsctl_iface *iface;
1628
1629         port = find_port(vsctl_ctx, target, false);
1630         if (!port) {
1631             iface = find_iface(vsctl_ctx, target, false);
1632             if (iface) {
1633                 port = iface->port;
1634             }
1635         }
1636         if (must_exist && !port) {
1637             ctl_fatal("no port or interface named %s", target);
1638         }
1639     }
1640
1641     if (port) {
1642         if (ctx->argc == 3) {
1643             struct vsctl_bridge *bridge;
1644
1645             bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1646             if (port->bridge != bridge) {
1647                 if (port->bridge->parent == bridge) {
1648                     ctl_fatal("bridge %s does not have a port %s (although "
1649                                 "its parent bridge %s does)",
1650                                 ctx->argv[1], ctx->argv[2],
1651                                 bridge->parent->name);
1652                 } else {
1653                     ctl_fatal("bridge %s does not have a port %s",
1654                                 ctx->argv[1], ctx->argv[2]);
1655                 }
1656             }
1657         }
1658
1659         del_port(vsctl_ctx, port);
1660     }
1661 }
1662
1663 static void
1664 cmd_port_to_br(struct ctl_context *ctx)
1665 {
1666     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1667     struct vsctl_port *port;
1668
1669     vsctl_context_populate_cache(ctx);
1670
1671     port = find_port(vsctl_ctx, ctx->argv[1], true);
1672     ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1673 }
1674
1675 static void
1676 cmd_br_to_vlan(struct ctl_context *ctx)
1677 {
1678     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1679     struct vsctl_bridge *bridge;
1680
1681     vsctl_context_populate_cache(ctx);
1682
1683     bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1684     ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1685 }
1686
1687 static void
1688 cmd_br_to_parent(struct ctl_context *ctx)
1689 {
1690     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1691     struct vsctl_bridge *bridge;
1692
1693     vsctl_context_populate_cache(ctx);
1694
1695     bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1696     if (bridge->parent) {
1697         bridge = bridge->parent;
1698     }
1699     ds_put_format(&ctx->output, "%s\n", bridge->name);
1700 }
1701
1702 static void
1703 cmd_list_ifaces(struct ctl_context *ctx)
1704 {
1705     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1706     struct vsctl_bridge *br;
1707     struct vsctl_port *port;
1708     struct svec ifaces;
1709
1710     vsctl_context_populate_cache(ctx);
1711
1712     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1713     verify_ports(vsctl_ctx);
1714
1715     svec_init(&ifaces);
1716     LIST_FOR_EACH (port, ports_node, &br->ports) {
1717         struct vsctl_iface *iface;
1718
1719         LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1720             if (strcmp(iface->iface_cfg->name, br->name)) {
1721                 svec_add(&ifaces, iface->iface_cfg->name);
1722             }
1723         }
1724     }
1725     output_sorted(&ifaces, &ctx->output);
1726     svec_destroy(&ifaces);
1727 }
1728
1729 static void
1730 cmd_iface_to_br(struct ctl_context *ctx)
1731 {
1732     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1733     struct vsctl_iface *iface;
1734
1735     vsctl_context_populate_cache(ctx);
1736
1737     iface = find_iface(vsctl_ctx, ctx->argv[1], true);
1738     ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1739 }
1740
1741 static void
1742 verify_controllers(struct ovsrec_bridge *bridge)
1743 {
1744     size_t i;
1745
1746     ovsrec_bridge_verify_controller(bridge);
1747     for (i = 0; i < bridge->n_controller; i++) {
1748         ovsrec_controller_verify_target(bridge->controller[i]);
1749     }
1750 }
1751
1752 static void
1753 pre_controller(struct ctl_context *ctx)
1754 {
1755     pre_get_info(ctx);
1756
1757     ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1758 }
1759
1760 static void
1761 cmd_get_controller(struct ctl_context *ctx)
1762 {
1763     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1764     struct vsctl_bridge *br;
1765     struct svec targets;
1766     size_t i;
1767
1768     vsctl_context_populate_cache(ctx);
1769
1770     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1771     if (br->parent) {
1772         br = br->parent;
1773     }
1774     verify_controllers(br->br_cfg);
1775
1776     /* Print the targets in sorted order for reproducibility. */
1777     svec_init(&targets);
1778     for (i = 0; i < br->br_cfg->n_controller; i++) {
1779         svec_add(&targets, br->br_cfg->controller[i]->target);
1780     }
1781
1782     svec_sort(&targets);
1783     for (i = 0; i < targets.n; i++) {
1784         ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1785     }
1786     svec_destroy(&targets);
1787 }
1788
1789 static void
1790 delete_controllers(struct ovsrec_controller **controllers,
1791                    size_t n_controllers)
1792 {
1793     size_t i;
1794
1795     for (i = 0; i < n_controllers; i++) {
1796         ovsrec_controller_delete(controllers[i]);
1797     }
1798 }
1799
1800 static void
1801 cmd_del_controller(struct ctl_context *ctx)
1802 {
1803     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1804     struct ovsrec_bridge *br;
1805
1806     vsctl_context_populate_cache(ctx);
1807
1808     br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1809     verify_controllers(br);
1810
1811     if (br->controller) {
1812         delete_controllers(br->controller, br->n_controller);
1813         ovsrec_bridge_set_controller(br, NULL, 0);
1814     }
1815 }
1816
1817 static struct ovsrec_controller **
1818 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1819 {
1820     struct ovsrec_controller **controllers;
1821     size_t i;
1822
1823     controllers = xmalloc(n * sizeof *controllers);
1824     for (i = 0; i < n; i++) {
1825         if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
1826             VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1827         }
1828         controllers[i] = ovsrec_controller_insert(txn);
1829         ovsrec_controller_set_target(controllers[i], targets[i]);
1830     }
1831
1832     return controllers;
1833 }
1834
1835 static void
1836 cmd_set_controller(struct ctl_context *ctx)
1837 {
1838     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1839     struct ovsrec_controller **controllers;
1840     struct ovsrec_bridge *br;
1841     size_t n;
1842
1843     vsctl_context_populate_cache(ctx);
1844
1845     br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1846     verify_controllers(br);
1847
1848     delete_controllers(br->controller, br->n_controller);
1849
1850     n = ctx->argc - 2;
1851     controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1852     ovsrec_bridge_set_controller(br, controllers, n);
1853     free(controllers);
1854 }
1855
1856 static void
1857 cmd_get_fail_mode(struct ctl_context *ctx)
1858 {
1859     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1860     struct vsctl_bridge *br;
1861     const char *fail_mode;
1862
1863     vsctl_context_populate_cache(ctx);
1864     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1865
1866     if (br->parent) {
1867         br = br->parent;
1868     }
1869     ovsrec_bridge_verify_fail_mode(br->br_cfg);
1870
1871     fail_mode = br->br_cfg->fail_mode;
1872     if (fail_mode && strlen(fail_mode)) {
1873         ds_put_format(&ctx->output, "%s\n", fail_mode);
1874     }
1875 }
1876
1877 static void
1878 cmd_del_fail_mode(struct ctl_context *ctx)
1879 {
1880     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1881     struct vsctl_bridge *br;
1882
1883     vsctl_context_populate_cache(ctx);
1884
1885     br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1886
1887     ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1888 }
1889
1890 static void
1891 cmd_set_fail_mode(struct ctl_context *ctx)
1892 {
1893     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1894     struct vsctl_bridge *br;
1895     const char *fail_mode = ctx->argv[2];
1896
1897     vsctl_context_populate_cache(ctx);
1898
1899     br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1900
1901     if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1902         ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1903     }
1904
1905     ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1906 }
1907
1908 static void
1909 verify_managers(const struct ovsrec_open_vswitch *ovs)
1910 {
1911     size_t i;
1912
1913     ovsrec_open_vswitch_verify_manager_options(ovs);
1914
1915     for (i = 0; i < ovs->n_manager_options; ++i) {
1916         const struct ovsrec_manager *mgr = ovs->manager_options[i];
1917
1918         ovsrec_manager_verify_target(mgr);
1919     }
1920 }
1921
1922 static void
1923 pre_manager(struct ctl_context *ctx)
1924 {
1925     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1926     ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1927 }
1928
1929 static void
1930 cmd_get_manager(struct ctl_context *ctx)
1931 {
1932     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1933     const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
1934     struct svec targets;
1935     size_t i;
1936
1937     verify_managers(ovs);
1938
1939     /* Print the targets in sorted order for reproducibility. */
1940     svec_init(&targets);
1941
1942     for (i = 0; i < ovs->n_manager_options; i++) {
1943         svec_add(&targets, ovs->manager_options[i]->target);
1944     }
1945
1946     svec_sort_unique(&targets);
1947     for (i = 0; i < targets.n; i++) {
1948         ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1949     }
1950     svec_destroy(&targets);
1951 }
1952
1953 static void
1954 delete_managers(const struct ovsrec_open_vswitch *ovs)
1955 {
1956     size_t i;
1957
1958     /* Delete Manager rows pointed to by 'manager_options' column. */
1959     for (i = 0; i < ovs->n_manager_options; i++) {
1960         ovsrec_manager_delete(ovs->manager_options[i]);
1961     }
1962
1963     /* Delete 'Manager' row refs in 'manager_options' column. */
1964     ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
1965 }
1966
1967 static void
1968 cmd_del_manager(struct ctl_context *ctx)
1969 {
1970     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1971     const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
1972
1973     verify_managers(ovs);
1974     delete_managers(ovs);
1975 }
1976
1977 static void
1978 insert_managers(struct vsctl_context *vsctl_ctx, char *targets[], size_t n)
1979 {
1980     struct ovsrec_manager **managers;
1981     size_t i;
1982
1983     /* Insert each manager in a new row in Manager table. */
1984     managers = xmalloc(n * sizeof *managers);
1985     for (i = 0; i < n; i++) {
1986         if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
1987             VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1988         }
1989         managers[i] = ovsrec_manager_insert(vsctl_ctx->base.txn);
1990         ovsrec_manager_set_target(managers[i], targets[i]);
1991     }
1992
1993     /* Store uuids of new Manager rows in 'manager_options' column. */
1994     ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, managers, n);
1995     free(managers);
1996 }
1997
1998 static void
1999 cmd_set_manager(struct ctl_context *ctx)
2000 {
2001     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2002     const size_t n = ctx->argc - 1;
2003
2004     verify_managers(vsctl_ctx->ovs);
2005     delete_managers(vsctl_ctx->ovs);
2006     insert_managers(vsctl_ctx, &ctx->argv[1], n);
2007 }
2008
2009 static void
2010 pre_cmd_get_ssl(struct ctl_context *ctx)
2011 {
2012     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2013
2014     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2015     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2016     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2017     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2018 }
2019
2020 static void
2021 cmd_get_ssl(struct ctl_context *ctx)
2022 {
2023     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2024     struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2025
2026     ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2027     if (ssl) {
2028         ovsrec_ssl_verify_private_key(ssl);
2029         ovsrec_ssl_verify_certificate(ssl);
2030         ovsrec_ssl_verify_ca_cert(ssl);
2031         ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2032
2033         ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2034         ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2035         ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2036         ds_put_format(&ctx->output, "Bootstrap: %s\n",
2037                 ssl->bootstrap_ca_cert ? "true" : "false");
2038     }
2039 }
2040
2041 static void
2042 pre_cmd_del_ssl(struct ctl_context *ctx)
2043 {
2044     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2045 }
2046
2047 static void
2048 cmd_del_ssl(struct ctl_context *ctx)
2049 {
2050     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2051     struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2052
2053     if (ssl) {
2054         ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2055         ovsrec_ssl_delete(ssl);
2056         ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
2057     }
2058 }
2059
2060 static void
2061 pre_cmd_set_ssl(struct ctl_context *ctx)
2062 {
2063     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2064 }
2065
2066 static void
2067 cmd_set_ssl(struct ctl_context *ctx)
2068 {
2069     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2070     bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2071     struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2072
2073     ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2074     if (ssl) {
2075         ovsrec_ssl_delete(ssl);
2076     }
2077     ssl = ovsrec_ssl_insert(ctx->txn);
2078
2079     ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2080     ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2081     ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2082
2083     ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2084
2085     ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, ssl);
2086 }
2087
2088 static void
2089 autoattach_insert_mapping(struct ovsrec_autoattach *aa,
2090                           int64_t isid,
2091                           int64_t vlan)
2092 {
2093     int64_t *key_mappings, *value_mappings;
2094     size_t i;
2095
2096     key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings + 1));
2097     value_mappings = xmalloc(sizeof *aa->value_mappings * (aa->n_mappings + 1));
2098
2099     for (i = 0; i < aa->n_mappings; i++) {
2100         key_mappings[i] = aa->key_mappings[i];
2101         value_mappings[i] = aa->value_mappings[i];
2102     }
2103     key_mappings[aa->n_mappings] = isid;
2104     value_mappings[aa->n_mappings] = vlan;
2105
2106     ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings,
2107                                    aa->n_mappings + 1);
2108
2109     free(key_mappings);
2110     free(value_mappings);
2111 }
2112
2113 static void
2114 cmd_add_aa_mapping(struct ctl_context *ctx)
2115 {
2116     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2117     struct vsctl_bridge *br;
2118     int64_t isid, vlan;
2119     char *nptr = NULL;
2120
2121     isid = strtoull(ctx->argv[2], &nptr, 10);
2122     if (nptr == ctx->argv[2] || nptr == NULL) {
2123         ctl_fatal("Invalid argument %s", ctx->argv[2]);
2124         return;
2125     }
2126
2127     vlan = strtoull(ctx->argv[3], &nptr, 10);
2128     if (nptr == ctx->argv[3] || nptr == NULL) {
2129         ctl_fatal("Invalid argument %s", ctx->argv[3]);
2130         return;
2131     }
2132
2133     vsctl_context_populate_cache(ctx);
2134
2135     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2136     if (br->parent) {
2137         br = br->parent;
2138     }
2139
2140     if (br && br->br_cfg) {
2141         if (!br->br_cfg->auto_attach) {
2142             struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
2143             ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
2144         }
2145         autoattach_insert_mapping(br->br_cfg->auto_attach, isid, vlan);
2146     }
2147 }
2148
2149 static void
2150 del_aa_mapping(struct ovsrec_autoattach *aa,
2151                int64_t isid,
2152                int64_t vlan)
2153 {
2154     int64_t *key_mappings, *value_mappings;
2155     size_t i, n;
2156
2157     key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings));
2158     value_mappings = xmalloc(sizeof *value_mappings * (aa->n_mappings));
2159
2160     for (i = n = 0; i < aa->n_mappings; i++) {
2161         if (aa->key_mappings[i] != isid && aa->value_mappings[i] != vlan) {
2162             key_mappings[n] = aa->key_mappings[i];
2163             value_mappings[n++] = aa->value_mappings[i];
2164         }
2165     }
2166
2167     ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings, n);
2168
2169     free(key_mappings);
2170     free(value_mappings);
2171 }
2172
2173 static void
2174 cmd_del_aa_mapping(struct ctl_context *ctx)
2175 {
2176     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2177     struct vsctl_bridge *br;
2178     int64_t isid, vlan;
2179     char *nptr = NULL;
2180
2181     isid = strtoull(ctx->argv[2], &nptr, 10);
2182     if (nptr == ctx->argv[2] || nptr == NULL) {
2183         ctl_fatal("Invalid argument %s", ctx->argv[2]);
2184         return;
2185     }
2186
2187     vlan = strtoull(ctx->argv[3], &nptr, 10);
2188     if (nptr == ctx->argv[3] || nptr == NULL) {
2189         ctl_fatal("Invalid argument %s", ctx->argv[3]);
2190         return;
2191     }
2192
2193     vsctl_context_populate_cache(ctx);
2194
2195     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2196     if (br->parent) {
2197         br = br->parent;
2198     }
2199
2200     if (br && br->br_cfg && br->br_cfg->auto_attach &&
2201         br->br_cfg->auto_attach->key_mappings &&
2202         br->br_cfg->auto_attach->value_mappings) {
2203         size_t i;
2204
2205         for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2206             if (br->br_cfg->auto_attach->key_mappings[i] == isid &&
2207                 br->br_cfg->auto_attach->value_mappings[i] == vlan) {
2208                 del_aa_mapping(br->br_cfg->auto_attach, isid, vlan);
2209                 break;
2210             }
2211         }
2212     }
2213 }
2214
2215 static void
2216 pre_aa_mapping(struct ctl_context *ctx)
2217 {
2218     pre_get_info(ctx);
2219
2220     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_auto_attach);
2221     ovsdb_idl_add_column(ctx->idl, &ovsrec_autoattach_col_mappings);
2222 }
2223
2224 static void
2225 verify_auto_attach(struct ovsrec_bridge *bridge)
2226 {
2227     if (bridge) {
2228         ovsrec_bridge_verify_auto_attach(bridge);
2229
2230         if (bridge->auto_attach) {
2231             ovsrec_autoattach_verify_mappings(bridge->auto_attach);
2232         }
2233     }
2234 }
2235
2236 static void
2237 cmd_get_aa_mapping(struct ctl_context *ctx)
2238 {
2239     struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2240     struct vsctl_bridge *br;
2241
2242     vsctl_context_populate_cache(ctx);
2243
2244     br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2245     if (br->parent) {
2246         br = br->parent;
2247     }
2248
2249     verify_auto_attach(br->br_cfg);
2250
2251     if (br && br->br_cfg && br->br_cfg->auto_attach &&
2252         br->br_cfg->auto_attach->key_mappings &&
2253         br->br_cfg->auto_attach->value_mappings) {
2254         size_t i;
2255
2256         for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2257             ds_put_format(&ctx->output, "%"PRId64" %"PRId64"\n",
2258                           br->br_cfg->auto_attach->key_mappings[i],
2259                           br->br_cfg->auto_attach->value_mappings[i]);
2260         }
2261     }
2262 }
2263
2264 \f
2265 const struct ctl_table_class tables[] = {
2266     {&ovsrec_table_bridge,
2267      {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2268       {&ovsrec_table_flow_sample_collector_set, NULL,
2269        &ovsrec_flow_sample_collector_set_col_bridge}}},
2270
2271     {&ovsrec_table_controller,
2272      {{&ovsrec_table_bridge,
2273        &ovsrec_bridge_col_name,
2274        &ovsrec_bridge_col_controller}}},
2275
2276     {&ovsrec_table_interface,
2277      {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2278       {NULL, NULL, NULL}}},
2279
2280     {&ovsrec_table_mirror,
2281      {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2282       {NULL, NULL, NULL}}},
2283
2284     {&ovsrec_table_manager,
2285      {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2286       {NULL, NULL, NULL}}},
2287
2288     {&ovsrec_table_netflow,
2289      {{&ovsrec_table_bridge,
2290        &ovsrec_bridge_col_name,
2291        &ovsrec_bridge_col_netflow},
2292       {NULL, NULL, NULL}}},
2293
2294     {&ovsrec_table_open_vswitch,
2295      {{&ovsrec_table_open_vswitch, NULL, NULL},
2296       {NULL, NULL, NULL}}},
2297
2298     {&ovsrec_table_port,
2299      {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2300       {NULL, NULL, NULL}}},
2301
2302     {&ovsrec_table_qos,
2303      {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2304       {NULL, NULL, NULL}}},
2305
2306     {&ovsrec_table_queue,
2307      {{NULL, NULL, NULL},
2308       {NULL, NULL, NULL}}},
2309
2310     {&ovsrec_table_ssl,
2311      {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2312
2313     {&ovsrec_table_sflow,
2314      {{&ovsrec_table_bridge,
2315        &ovsrec_bridge_col_name,
2316        &ovsrec_bridge_col_sflow},
2317       {NULL, NULL, NULL}}},
2318
2319     {&ovsrec_table_flow_table,
2320      {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2321       {NULL, NULL, NULL}}},
2322
2323     {&ovsrec_table_ipfix,
2324      {{&ovsrec_table_bridge,
2325        &ovsrec_bridge_col_name,
2326        &ovsrec_bridge_col_ipfix},
2327       {&ovsrec_table_flow_sample_collector_set, NULL,
2328        &ovsrec_flow_sample_collector_set_col_ipfix}}},
2329
2330     {&ovsrec_table_autoattach,
2331      {{&ovsrec_table_bridge,
2332        &ovsrec_bridge_col_name,
2333        &ovsrec_bridge_col_auto_attach},
2334       {NULL, NULL, NULL}}},
2335
2336     {&ovsrec_table_flow_sample_collector_set,
2337      {{NULL, NULL, NULL},
2338       {NULL, NULL, NULL}}},
2339
2340     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2341 };
2342
2343 static void
2344 post_db_reload_check_init(void)
2345 {
2346     n_neoteric_ifaces = 0;
2347 }
2348
2349 static void
2350 post_db_reload_expect_iface(const struct ovsrec_interface *iface)
2351 {
2352     if (n_neoteric_ifaces >= allocated_neoteric_ifaces) {
2353         neoteric_ifaces = x2nrealloc(neoteric_ifaces,
2354                                      &allocated_neoteric_ifaces,
2355                                      sizeof *neoteric_ifaces);
2356     }
2357     neoteric_ifaces[n_neoteric_ifaces++] = iface->header_.uuid;
2358 }
2359
2360 static void
2361 post_db_reload_do_checks(const struct vsctl_context *vsctl_ctx)
2362 {
2363     struct ds dead_ifaces = DS_EMPTY_INITIALIZER;
2364     size_t i;
2365
2366     for (i = 0; i < n_neoteric_ifaces; i++) {
2367         const struct uuid *uuid;
2368
2369         uuid = ovsdb_idl_txn_get_insert_uuid(vsctl_ctx->base.txn,
2370                                              &neoteric_ifaces[i]);
2371         if (uuid) {
2372             const struct ovsrec_interface *iface;
2373
2374             iface = ovsrec_interface_get_for_uuid(vsctl_ctx->base.idl, uuid);
2375             if (iface && (!iface->ofport || *iface->ofport == -1)) {
2376                 ds_put_format(&dead_ifaces, "'%s', ", iface->name);
2377             }
2378         }
2379     }
2380
2381     if (dead_ifaces.length) {
2382         dead_ifaces.length -= 2; /* Strip off trailing comma and space. */
2383         ovs_error(0, "Error detected while setting up %s.  See ovs-vswitchd "
2384                   "log for details.", ds_cstr(&dead_ifaces));
2385     }
2386
2387     ds_destroy(&dead_ifaces);
2388 }
2389
2390 \f
2391 static void
2392 vsctl_context_init_command(struct vsctl_context *vsctl_ctx,
2393                            struct ctl_command *command)
2394 {
2395     ctl_context_init_command(&vsctl_ctx->base, command);
2396     vsctl_ctx->verified_ports = false;
2397 }
2398
2399 static void
2400 vsctl_context_init(struct vsctl_context *vsctl_ctx,
2401                    struct ctl_command *command, struct ovsdb_idl *idl,
2402                    struct ovsdb_idl_txn *txn,
2403                    const struct ovsrec_open_vswitch *ovs,
2404                    struct ovsdb_symbol_table *symtab)
2405 {
2406     ctl_context_init(&vsctl_ctx->base, command, idl, txn, symtab,
2407                      vsctl_context_invalidate_cache);
2408     if (command) {
2409         vsctl_ctx->verified_ports = false;
2410     }
2411     vsctl_ctx->ovs = ovs;
2412     vsctl_ctx->cache_valid = false;
2413 }
2414
2415 static void
2416 vsctl_context_done_command(struct vsctl_context *vsctl_ctx,
2417                            struct ctl_command *command)
2418 {
2419     ctl_context_done_command(&vsctl_ctx->base, command);
2420 }
2421
2422 static void
2423 vsctl_context_done(struct vsctl_context *vsctl_ctx,
2424                    struct ctl_command *command)
2425 {
2426     ctl_context_done(&vsctl_ctx->base, command);
2427 }
2428
2429 static void
2430 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2431                   struct ovsdb_idl *idl)
2432 {
2433     struct ctl_command *c;
2434
2435     ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
2436     if (wait_for_reload) {
2437         ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
2438     }
2439     for (c = commands; c < &commands[n_commands]; c++) {
2440         if (c->syntax->prerequisites) {
2441             struct vsctl_context vsctl_ctx;
2442
2443             ds_init(&c->output);
2444             c->table = NULL;
2445
2446             vsctl_context_init(&vsctl_ctx, c, idl, NULL, NULL, NULL);
2447             (c->syntax->prerequisites)(&vsctl_ctx.base);
2448             vsctl_context_done(&vsctl_ctx, c);
2449
2450             ovs_assert(!c->output.string);
2451             ovs_assert(!c->table);
2452         }
2453     }
2454 }
2455
2456 static void
2457 do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
2458          struct ovsdb_idl *idl)
2459 {
2460     struct ovsdb_idl_txn *txn;
2461     const struct ovsrec_open_vswitch *ovs;
2462     enum ovsdb_idl_txn_status status;
2463     struct ovsdb_symbol_table *symtab;
2464     struct vsctl_context vsctl_ctx;
2465     struct ctl_command *c;
2466     struct shash_node *node;
2467     int64_t next_cfg = 0;
2468     char *error = NULL;
2469
2470     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2471     if (dry_run) {
2472         ovsdb_idl_txn_set_dry_run(txn);
2473     }
2474
2475     ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2476
2477     ovs = ovsrec_open_vswitch_first(idl);
2478     if (!ovs) {
2479         /* XXX add verification that table is empty */
2480         ovs = ovsrec_open_vswitch_insert(txn);
2481     }
2482
2483     if (wait_for_reload) {
2484         ovsdb_idl_txn_increment(txn, &ovs->header_,
2485                                 &ovsrec_open_vswitch_col_next_cfg);
2486     }
2487
2488     post_db_reload_check_init();
2489     symtab = ovsdb_symbol_table_create();
2490     for (c = commands; c < &commands[n_commands]; c++) {
2491         ds_init(&c->output);
2492         c->table = NULL;
2493     }
2494     vsctl_context_init(&vsctl_ctx, NULL, idl, txn, ovs, symtab);
2495     for (c = commands; c < &commands[n_commands]; c++) {
2496         vsctl_context_init_command(&vsctl_ctx, c);
2497         if (c->syntax->run) {
2498             (c->syntax->run)(&vsctl_ctx.base);
2499         }
2500         vsctl_context_done_command(&vsctl_ctx, c);
2501
2502         if (vsctl_ctx.base.try_again) {
2503             vsctl_context_done(&vsctl_ctx, NULL);
2504             goto try_again;
2505         }
2506     }
2507     vsctl_context_done(&vsctl_ctx, NULL);
2508
2509     SHASH_FOR_EACH (node, &symtab->sh) {
2510         struct ovsdb_symbol *symbol = node->data;
2511         if (!symbol->created) {
2512             ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2513                         "with \"-- --id=%s create ...\")",
2514                         node->name, node->name);
2515         }
2516         if (!symbol->strong_ref) {
2517             if (!symbol->weak_ref) {
2518                 VLOG_WARN("row id \"%s\" was created but no reference to it "
2519                           "was inserted, so it will not actually appear in "
2520                           "the database", node->name);
2521             } else {
2522                 VLOG_WARN("row id \"%s\" was created but only a weak "
2523                           "reference to it was inserted, so it will not "
2524                           "actually appear in the database", node->name);
2525             }
2526         }
2527     }
2528
2529     status = ovsdb_idl_txn_commit_block(txn);
2530     if (wait_for_reload && status == TXN_SUCCESS) {
2531         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2532     }
2533     if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2534         for (c = commands; c < &commands[n_commands]; c++) {
2535             if (c->syntax->postprocess) {
2536                 vsctl_context_init(&vsctl_ctx, c, idl, txn, ovs, symtab);
2537                 (c->syntax->postprocess)(&vsctl_ctx.base);
2538                 vsctl_context_done(&vsctl_ctx, c);
2539             }
2540         }
2541     }
2542     error = xstrdup(ovsdb_idl_txn_get_error(txn));
2543
2544     switch (status) {
2545     case TXN_UNCOMMITTED:
2546     case TXN_INCOMPLETE:
2547         OVS_NOT_REACHED();
2548
2549     case TXN_ABORTED:
2550         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2551         ctl_fatal("transaction aborted");
2552
2553     case TXN_UNCHANGED:
2554     case TXN_SUCCESS:
2555         break;
2556
2557     case TXN_TRY_AGAIN:
2558         goto try_again;
2559
2560     case TXN_ERROR:
2561         ctl_fatal("transaction error: %s", error);
2562
2563     case TXN_NOT_LOCKED:
2564         /* Should not happen--we never call ovsdb_idl_set_lock(). */
2565         ctl_fatal("database not locked");
2566
2567     default:
2568         OVS_NOT_REACHED();
2569     }
2570     free(error);
2571
2572     ovsdb_symbol_table_destroy(symtab);
2573
2574     for (c = commands; c < &commands[n_commands]; c++) {
2575         struct ds *ds = &c->output;
2576
2577         if (c->table) {
2578             table_print(c->table, &table_style);
2579         } else if (oneline) {
2580             size_t j;
2581
2582             ds_chomp(ds, '\n');
2583             for (j = 0; j < ds->length; j++) {
2584                 int ch = ds->string[j];
2585                 switch (ch) {
2586                 case '\n':
2587                     fputs("\\n", stdout);
2588                     break;
2589
2590                 case '\\':
2591                     fputs("\\\\", stdout);
2592                     break;
2593
2594                 default:
2595                     putchar(ch);
2596                 }
2597             }
2598             putchar('\n');
2599         } else {
2600             fputs(ds_cstr(ds), stdout);
2601         }
2602         ds_destroy(&c->output);
2603         table_destroy(c->table);
2604         free(c->table);
2605
2606         shash_destroy_free_data(&c->options);
2607     }
2608     free(commands);
2609
2610     if (wait_for_reload && status != TXN_UNCHANGED) {
2611         /* Even, if --retry flag was not specified, ovs-vsctl still
2612          * has to retry to establish OVSDB connection, if wait_for_reload
2613          * was set.  Otherwise, ovs-vsctl would end up waiting forever
2614          * until cur_cfg would be updated. */
2615         ovsdb_idl_enable_reconnect(idl);
2616         for (;;) {
2617             ovsdb_idl_run(idl);
2618             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2619                 if (ovs->cur_cfg >= next_cfg) {
2620                     post_db_reload_do_checks(&vsctl_ctx);
2621                     goto done;
2622                 }
2623             }
2624             ovsdb_idl_wait(idl);
2625             poll_block();
2626         }
2627     done: ;
2628     }
2629     ovsdb_idl_txn_destroy(txn);
2630     ovsdb_idl_destroy(idl);
2631
2632     exit(EXIT_SUCCESS);
2633
2634 try_again:
2635     /* Our transaction needs to be rerun, or a prerequisite was not met.  Free
2636      * resources and return so that the caller can try again. */
2637     if (txn) {
2638         ovsdb_idl_txn_abort(txn);
2639         ovsdb_idl_txn_destroy(txn);
2640         the_idl_txn = NULL;
2641     }
2642     ovsdb_symbol_table_destroy(symtab);
2643     for (c = commands; c < &commands[n_commands]; c++) {
2644         ds_destroy(&c->output);
2645         table_destroy(c->table);
2646         free(c->table);
2647     }
2648     free(error);
2649 }
2650
2651 /*
2652  * Developers who add new commands to the 'struct ctl_command_syntax' must
2653  * define the 'arguments' member of the struct.  The following keywords are
2654  * available for composing the argument format:
2655  *
2656  *    TABLE     RECORD       BRIDGE       PARENT         PORT
2657  *    KEY       VALUE        ARG          KEY=VALUE      ?KEY=VALUE
2658  *    IFACE     SYSIFACE     COLUMN       COLUMN?:KEY    COLUMN?:KEY=VALUE
2659  *    MODE      CA-CERT      CERTIFICATE  PRIVATE-KEY
2660  *    TARGET    NEW-* (e.g. NEW-PORT)
2661  *
2662  * For argument types not listed above, just uses 'ARG' as place holder.
2663  *
2664  * Encloses the keyword with '[]' if it is optional.  Appends '...' to
2665  * keyword or enclosed keyword to indicate that the argument can be specified
2666  * multiple times.
2667  *
2668  * */
2669 static const struct ctl_command_syntax vsctl_commands[] = {
2670     /* Open vSwitch commands. */
2671     {"init", 0, 0, "", NULL, cmd_init, NULL, "", RW},
2672
2673     /* Bridge commands. */
2674     {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info,
2675      cmd_add_br, NULL, "--may-exist", RW},
2676     {"del-br", 1, 1, "BRIDGE", pre_get_info, cmd_del_br,
2677      NULL, "--if-exists", RW},
2678     {"list-br", 0, 0, "", pre_get_info, cmd_list_br, NULL, "--real,--fake",
2679      RO},
2680     {"br-exists", 1, 1, "BRIDGE", pre_get_info, cmd_br_exists, NULL, "", RO},
2681     {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_vlan, NULL, "",
2682      RO},
2683     {"br-to-parent", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_parent, NULL,
2684      "", RO},
2685     {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2686      pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
2687     {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id,
2688      cmd_br_get_external_id, NULL, "", RO},
2689
2690     /* Port commands. */
2691     {"list-ports", 1, 1, "BRIDGE", pre_get_info, cmd_list_ports, NULL, "",
2692      RO},
2693     {"add-port", 2, INT_MAX, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2694      pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
2695     {"add-bond", 4, INT_MAX,
2696      "BRIDGE NEW-BOND-PORT SYSIFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info,
2697      cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
2698     {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info, cmd_del_port, NULL,
2699      "--if-exists,--with-iface", RW},
2700     {"port-to-br", 1, 1, "PORT", pre_get_info, cmd_port_to_br, NULL, "", RO},
2701
2702     /* Interface commands. */
2703     {"list-ifaces", 1, 1, "BRIDGE", pre_get_info, cmd_list_ifaces, NULL, "",
2704      RO},
2705     {"iface-to-br", 1, 1, "IFACE", pre_get_info, cmd_iface_to_br, NULL, "",
2706      RO},
2707
2708     /* Controller commands. */
2709     {"get-controller", 1, 1, "BRIDGE", pre_controller, cmd_get_controller,
2710      NULL, "", RO},
2711     {"del-controller", 1, 1, "BRIDGE", pre_controller, cmd_del_controller,
2712      NULL, "", RW},
2713     {"set-controller", 1, INT_MAX, "BRIDGE TARGET...", pre_controller,
2714      cmd_set_controller, NULL, "", RW},
2715     {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_get_fail_mode, NULL,
2716      "", RO},
2717     {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_del_fail_mode, NULL,
2718      "", RW},
2719     {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info, cmd_set_fail_mode,
2720      NULL, "", RW},
2721
2722     /* Manager commands. */
2723     {"get-manager", 0, 0, "", pre_manager, cmd_get_manager, NULL, "", RO},
2724     {"del-manager", 0, 0, "", pre_manager, cmd_del_manager, NULL, "", RW},
2725     {"set-manager", 1, INT_MAX, "TARGET...", pre_manager, cmd_set_manager,
2726      NULL, "", RW},
2727
2728     /* SSL commands. */
2729     {"get-ssl", 0, 0, "", pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
2730     {"del-ssl", 0, 0, "", pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
2731     {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl,
2732      cmd_set_ssl, NULL, "--bootstrap", RW},
2733
2734     /* Auto Attach commands. */
2735     {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_add_aa_mapping,
2736      NULL, "", RW},
2737     {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_del_aa_mapping,
2738      NULL, "", RW},
2739     {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping, cmd_get_aa_mapping,
2740      NULL, "", RO},
2741
2742     /* Switch commands. */
2743     {"emer-reset", 0, 0, "", pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
2744
2745     {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2746 };
2747
2748 /* Registers vsctl and common db commands. */
2749 static void
2750 vsctl_cmd_init(void)
2751 {
2752     ctl_init();
2753     ctl_register_commands(vsctl_commands);
2754 }