ovs-vsctl: Add context pointer to struct vsctl_info.
[sliver-openvswitch.git] / utilities / ovs-vsctl.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
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 <assert.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <float.h>
23 #include <getopt.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "command-line.h"
31 #include "compiler.h"
32 #include "dirs.h"
33 #include "dynamic-string.h"
34 #include "json.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
38 #include "process.h"
39 #include "stream-ssl.h"
40 #include "svec.h"
41 #include "vswitchd/vswitch-idl.h"
42 #include "timeval.h"
43 #include "util.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(vsctl)
47
48 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
49 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
50
51 struct vsctl_context;
52
53 typedef void vsctl_handler_func(struct vsctl_context *);
54
55 struct vsctl_command_syntax {
56     const char *name;
57     int min_args;
58     int max_args;
59     vsctl_handler_func *run;
60     vsctl_handler_func *postprocess;
61     const char *options;
62 };
63
64 struct vsctl_command {
65     /* Data that remains constant after initialization. */
66     const struct vsctl_command_syntax *syntax;
67     int argc;
68     char **argv;
69     struct shash options;
70
71     /* Data modified by commands. */
72     struct ds output;
73 };
74
75 /* --db: The database server to contact. */
76 static const char *db;
77
78 /* --oneline: Write each command's output as a single line? */
79 static bool oneline;
80
81 /* --dry-run: Do not commit any changes. */
82 static bool dry_run;
83
84 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
85 static bool wait_for_reload = true;
86
87 /* --timeout: Time to wait for a connection to 'db'. */
88 static int timeout;
89
90 /* All supported commands. */
91 static const struct vsctl_command_syntax all_commands[];
92
93 /* The IDL we're using and the current transaction, if any.
94  * This is for use by vsctl_exit() only, to allow it to clean up.
95  * Other code should use its context arguments. */
96 static struct ovsdb_idl *the_idl;
97 static struct ovsdb_idl_txn *the_idl_txn;
98
99 static void vsctl_exit(int status) NO_RETURN;
100 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
101 static char *default_db(void);
102 static void usage(void) NO_RETURN;
103 static void parse_options(int argc, char *argv[]);
104
105 static struct vsctl_command *parse_commands(int argc, char *argv[],
106                                             size_t *n_commandsp);
107 static void parse_command(int argc, char *argv[], struct vsctl_command *);
108 static void do_vsctl(const char *args,
109                      struct vsctl_command *, size_t n_commands,
110                      struct ovsdb_idl *);
111
112 static const struct vsctl_table_class *get_table(const char *table_name);
113 static void set_column(const struct vsctl_table_class *,
114                        const struct ovsdb_idl_row *, const char *arg,
115                        struct ovsdb_symbol_table *);
116
117
118 int
119 main(int argc, char *argv[])
120 {
121     extern struct vlog_module VLM_reconnect;
122     struct ovsdb_idl *idl;
123     struct vsctl_command *commands;
124     size_t n_commands;
125     char *args;
126
127     set_program_name(argv[0]);
128     signal(SIGPIPE, SIG_IGN);
129     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
130     vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
131     ovsrec_init();
132
133     /* Log our arguments.  This is often valuable for debugging systems. */
134     args = process_escape_args(argv);
135     VLOG_INFO("Called as %s", args);
136
137     /* Parse command line. */
138     parse_options(argc, argv);
139     commands = parse_commands(argc - optind, argv + optind, &n_commands);
140
141     if (timeout) {
142         time_alarm(timeout);
143     }
144
145     /* Now execute the commands. */
146     idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
147     for (;;) {
148         if (ovsdb_idl_run(idl)) {
149             do_vsctl(args, commands, n_commands, idl);
150         }
151
152         ovsdb_idl_wait(idl);
153         poll_block();
154     }
155 }
156
157 static void
158 parse_options(int argc, char *argv[])
159 {
160     enum {
161         OPT_DB = UCHAR_MAX + 1,
162         OPT_ONELINE,
163         OPT_NO_SYSLOG,
164         OPT_NO_WAIT,
165         OPT_DRY_RUN,
166         OPT_PEER_CA_CERT,
167         VLOG_OPTION_ENUMS
168     };
169     static struct option long_options[] = {
170         {"db", required_argument, 0, OPT_DB},
171         {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
172         {"no-wait", no_argument, 0, OPT_NO_WAIT},
173         {"dry-run", no_argument, 0, OPT_DRY_RUN},
174         {"oneline", no_argument, 0, OPT_ONELINE},
175         {"timeout", required_argument, 0, 't'},
176         {"help", no_argument, 0, 'h'},
177         {"version", no_argument, 0, 'V'},
178         VLOG_LONG_OPTIONS,
179 #ifdef HAVE_OPENSSL
180         STREAM_SSL_LONG_OPTIONS
181         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
182 #endif
183         {0, 0, 0, 0},
184     };
185     char *tmp, *short_options;
186
187     tmp = long_options_to_short_options(long_options);
188     short_options = xasprintf("+%s", tmp);
189     free(tmp);
190
191     for (;;) {
192         int c;
193
194         c = getopt_long(argc, argv, short_options, long_options, NULL);
195         if (c == -1) {
196             break;
197         }
198
199         switch (c) {
200         case OPT_DB:
201             db = optarg;
202             break;
203
204         case OPT_ONELINE:
205             oneline = true;
206             break;
207
208         case OPT_NO_SYSLOG:
209             vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
210             break;
211
212         case OPT_NO_WAIT:
213             wait_for_reload = false;
214             break;
215
216         case OPT_DRY_RUN:
217             dry_run = true;
218             break;
219
220         case 'h':
221             usage();
222
223         case 'V':
224             OVS_PRINT_VERSION(0, 0);
225             exit(EXIT_SUCCESS);
226
227         case 't':
228             timeout = strtoul(optarg, NULL, 10);
229             if (timeout < 0) {
230                 vsctl_fatal("value %s on -t or --timeout is invalid",
231                             optarg);
232             }
233             break;
234
235         VLOG_OPTION_HANDLERS
236
237 #ifdef HAVE_OPENSSL
238         STREAM_SSL_OPTION_HANDLERS
239
240         case OPT_PEER_CA_CERT:
241             stream_ssl_set_peer_ca_cert_file(optarg);
242             break;
243 #endif
244
245         case '?':
246             exit(EXIT_FAILURE);
247
248         default:
249             abort();
250         }
251     }
252     free(short_options);
253
254     if (!db) {
255         db = default_db();
256     }
257 }
258
259 static struct vsctl_command *
260 parse_commands(int argc, char *argv[], size_t *n_commandsp)
261 {
262     struct vsctl_command *commands;
263     size_t n_commands, allocated_commands;
264     int i, start;
265
266     commands = NULL;
267     n_commands = allocated_commands = 0;
268
269     for (start = i = 0; i <= argc; i++) {
270         if (i == argc || !strcmp(argv[i], "--")) {
271             if (i > start) {
272                 if (n_commands >= allocated_commands) {
273                     struct vsctl_command *c;
274
275                     commands = x2nrealloc(commands, &allocated_commands,
276                                           sizeof *commands);
277                     for (c = commands; c < &commands[n_commands]; c++) {
278                         shash_moved(&c->options);
279                     }
280                 }
281                 parse_command(i - start, &argv[start],
282                               &commands[n_commands++]);
283             }
284             start = i + 1;
285         }
286     }
287     if (!n_commands) {
288         vsctl_fatal("missing command name (use --help for help)");
289     }
290     *n_commandsp = n_commands;
291     return commands;
292 }
293
294 static void
295 parse_command(int argc, char *argv[], struct vsctl_command *command)
296 {
297     const struct vsctl_command_syntax *p;
298     int i;
299
300     shash_init(&command->options);
301     for (i = 0; i < argc; i++) {
302         const char *option = argv[i];
303         const char *equals;
304         char *key, *value;
305
306         if (option[0] != '-') {
307             break;
308         }
309
310         equals = strchr(option, '=');
311         if (equals) {
312             key = xmemdup0(option, equals - option);
313             value = xstrdup(equals + 1);
314         } else {
315             key = xstrdup(option);
316             value = NULL;
317         }
318
319         if (shash_find(&command->options, key)) {
320             vsctl_fatal("'%s' option specified multiple times", argv[i]);
321         }
322         shash_add_nocopy(&command->options, key, value);
323     }
324     if (i == argc) {
325         vsctl_fatal("missing command name");
326     }
327
328     for (p = all_commands; p->name; p++) {
329         if (!strcmp(p->name, argv[i])) {
330             struct shash_node *node;
331             int n_arg;
332
333             SHASH_FOR_EACH (node, &command->options) {
334                 const char *s = strstr(p->options, node->name);
335                 int end = s ? s[strlen(node->name)] : EOF;
336
337                 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
338                     vsctl_fatal("'%s' command has no '%s' option",
339                                 argv[i], node->name);
340                 }
341                 if ((end == '=') != (node->data != NULL)) {
342                     if (end == '=') {
343                         vsctl_fatal("missing argument to '%s' option on '%s' "
344                                     "command", node->name, argv[i]);
345                     } else {
346                         vsctl_fatal("'%s' option on '%s' does not accept an "
347                                     "argument", node->name, argv[i]);
348                     }
349                 }
350             }
351
352             n_arg = argc - i - 1;
353             if (n_arg < p->min_args) {
354                 vsctl_fatal("'%s' command requires at least %d arguments",
355                             p->name, p->min_args);
356             } else if (n_arg > p->max_args) {
357                 int j;
358
359                 for (j = i + 1; j < argc; j++) {
360                     if (argv[j][0] == '-') {
361                         vsctl_fatal("'%s' command takes at most %d arguments "
362                                     "(note that options must precede command "
363                                     "names and follow a \"--\" argument)",
364                                     p->name, p->max_args);
365                     }
366                 }
367
368                 vsctl_fatal("'%s' command takes at most %d arguments",
369                             p->name, p->max_args);
370             } else {
371                 command->syntax = p;
372                 command->argc = n_arg + 1;
373                 command->argv = &argv[i];
374                 return;
375             }
376         }
377     }
378
379     vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
380 }
381
382 static void
383 vsctl_fatal(const char *format, ...)
384 {
385     char *message;
386     va_list args;
387
388     va_start(args, format);
389     message = xvasprintf(format, args);
390     va_end(args);
391
392     vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
393     VLOG_ERR("%s", message);
394     ovs_error(0, "%s", message);
395     vsctl_exit(EXIT_FAILURE);
396 }
397
398 /* Frees the current transaction and the underlying IDL and then calls
399  * exit(status).
400  *
401  * Freeing the transaction and the IDL is not strictly necessary, but it makes
402  * for a clean memory leak report from valgrind in the normal case.  That makes
403  * it easier to notice real memory leaks. */
404 static void
405 vsctl_exit(int status)
406 {
407     if (the_idl_txn) {
408         ovsdb_idl_txn_abort(the_idl_txn);
409         ovsdb_idl_txn_destroy(the_idl_txn);
410     }
411     ovsdb_idl_destroy(the_idl);
412     exit(status);
413 }
414
415 static void
416 usage(void)
417 {
418     printf("\
419 %s: ovs-vswitchd management utility\n\
420 usage: %s [OPTIONS] COMMAND [ARG...]\n\
421 \n\
422 Bridge commands:\n\
423   add-br BRIDGE               create a new bridge named BRIDGE\n\
424   add-br BRIDGE PARENT VLAN   create new fake BRIDGE in PARENT on VLAN\n\
425   del-br BRIDGE               delete BRIDGE and all of its ports\n\
426   list-br                     print the names of all the bridges\n\
427   br-exists BRIDGE            test whether BRIDGE exists\n\
428   br-to-vlan BRIDGE           print the VLAN which BRIDGE is on\n\
429   br-to-parent BRIDGE         print the parent of BRIDGE\n\
430   br-set-external-id BRIDGE KEY VALUE  set KEY on BRIDGE to VALUE\n\
431   br-set-external-id BRIDGE KEY  unset KEY on BRIDGE\n\
432   br-get-external-id BRIDGE KEY  print value of KEY on BRIDGE\n\
433   br-get-external-id BRIDGE  list key-value pairs on BRIDGE\n\
434 \n\
435 Port commands:\n\
436   list-ports BRIDGE           print the names of all the ports on BRIDGE\n\
437   add-port BRIDGE PORT        add network device PORT to BRIDGE\n\
438   add-bond BRIDGE PORT IFACE...  add bonded port PORT in BRIDGE from IFACES\n\
439   del-port [BRIDGE] PORT      delete PORT (which may be bonded) from BRIDGE\n\
440   port-to-br PORT             print name of bridge that contains PORT\n\
441 A bond is considered to be a single port.\n\
442 \n\
443 Interface commands (a bond consists of multiple interfaces):\n\
444   list-ifaces BRIDGE          print the names of all interfaces on BRIDGE\n\
445   iface-to-br IFACE           print name of bridge that contains IFACE\n\
446 \n\
447 Controller commands:\n\
448   get-controller BRIDGE      print the controller for BRIDGE\n\
449   del-controller BRIDGE      delete the controller for BRIDGE\n\
450   set-controller BRIDGE TARGET  set the controller for BRIDGE to TARGET\n\
451   get-fail-mode BRIDGE       print the fail-mode for BRIDGE\n\
452   del-fail-mode BRIDGE       delete the fail-mode for BRIDGE\n\
453   set-fail-mode BRIDGE MODE  set the fail-mode for BRIDGE to MODE\n\
454 \n\
455 SSL commands:\n\
456   get-ssl                     print the SSL configuration\n\
457   del-ssl                     delete the SSL configuration\n\
458   set-ssl PRIV-KEY CERT CA-CERT  set the SSL configuration\n\
459 \n\
460 Switch commands:\n\
461   emer-reset                  reset switch to known good state\n\
462 \n\
463 Database commands:\n\
464   list TBL [REC]              list RECord (or all records) in TBL\n\
465   get TBL REC COL[:KEY]       print values of COLumns in RECord in TBL\n\
466   set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
467   add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
468   remove TBL REC COL [KEY=]VALUE  remove (KEY=)VALUE from COLumn\n\
469   clear TBL REC COL           clear values from COLumn in RECord in TBL\n\
470   create TBL COL[:KEY]=VALUE  create and initialize new record\n\
471   destroy TBL REC             delete RECord from TBL\n\
472   wait-until TBL REC [COL[:KEY]=VALUE]  wait until condition is true\n\
473 Potentially unsafe database commands require --force option.\n\
474 \n\
475 Options:\n\
476   --db=DATABASE               connect to DATABASE\n\
477                               (default: %s)\n\
478   --oneline                   print exactly one line of output per command\n",
479            program_name, program_name, default_db());
480     vlog_usage();
481     printf("\n\
482 Other options:\n\
483   -h, --help                  display this help message\n\
484   -V, --version               display version information\n");
485     exit(EXIT_SUCCESS);
486 }
487
488 static char *
489 default_db(void)
490 {
491     static char *def;
492     if (!def) {
493         def = xasprintf("unix:%s/db.sock", ovs_rundir);
494     }
495     return def;
496 }
497 \f
498 struct vsctl_context {
499     /* Read-only. */
500     int argc;
501     char **argv;
502     struct shash options;
503
504     /* Modifiable state. */
505     struct ds output;
506     struct ovsdb_idl *idl;
507     struct ovsdb_idl_txn *txn;
508     struct ovsdb_symbol_table *symtab;
509     const struct ovsrec_open_vswitch *ovs;
510
511     /* A command may set this member to true if some prerequisite is not met
512      * and the caller should wait for something to change and then retry. */
513     bool try_again;
514 };
515
516 struct vsctl_bridge {
517     struct ovsrec_bridge *br_cfg;
518     char *name;
519     struct ovsrec_controller **ctrl;
520     char *fail_mode;
521     size_t n_ctrl;
522     struct vsctl_bridge *parent;
523     int vlan;
524 };
525
526 struct vsctl_port {
527     struct ovsrec_port *port_cfg;
528     struct vsctl_bridge *bridge;
529 };
530
531 struct vsctl_iface {
532     struct ovsrec_interface *iface_cfg;
533     struct vsctl_port *port;
534 };
535
536 struct vsctl_info {
537     struct vsctl_context *ctx;
538     struct shash bridges;
539     struct shash ports;
540     struct shash ifaces;
541 };
542
543 static char *
544 vsctl_context_to_string(const struct vsctl_context *ctx)
545 {
546     const struct shash_node *node;
547     struct svec words;
548     char *s;
549     int i;
550
551     svec_init(&words);
552     SHASH_FOR_EACH (node, &ctx->options) {
553         svec_add(&words, node->name);
554     }
555     for (i = 0; i < ctx->argc; i++) {
556         svec_add(&words, ctx->argv[i]);
557     }
558     svec_terminate(&words);
559
560     s = process_escape_args(words.names);
561
562     svec_destroy(&words);
563
564     return s;
565 }
566
567 static struct vsctl_bridge *
568 add_bridge(struct vsctl_info *b,
569            struct ovsrec_bridge *br_cfg, const char *name,
570            struct vsctl_bridge *parent, int vlan)
571 {
572     struct vsctl_bridge *br = xmalloc(sizeof *br);
573     br->br_cfg = br_cfg;
574     br->name = xstrdup(name);
575     br->parent = parent;
576     br->vlan = vlan;
577     if (parent) {
578         br->ctrl = parent->br_cfg->controller;
579         br->n_ctrl = parent->br_cfg->n_controller;
580         br->fail_mode = parent->br_cfg->fail_mode;
581     } else {
582         br->ctrl = br_cfg->controller;
583         br->n_ctrl = br_cfg->n_controller;
584         br->fail_mode = br_cfg->fail_mode;
585     }
586     shash_add(&b->bridges, br->name, br);
587     return br;
588 }
589
590 static bool
591 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
592 {
593     return (port_cfg->fake_bridge
594             && port_cfg->tag
595             && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
596 }
597
598 static struct vsctl_bridge *
599 find_vlan_bridge(struct vsctl_info *info,
600                  struct vsctl_bridge *parent, int vlan)
601 {
602     struct shash_node *node;
603
604     SHASH_FOR_EACH (node, &info->bridges) {
605         struct vsctl_bridge *br = node->data;
606         if (br->parent == parent && br->vlan == vlan) {
607             return br;
608         }
609     }
610
611     return NULL;
612 }
613
614 static void
615 free_info(struct vsctl_info *info)
616 {
617     struct shash_node *node;
618
619     SHASH_FOR_EACH (node, &info->bridges) {
620         struct vsctl_bridge *bridge = node->data;
621         free(bridge->name);
622         free(bridge);
623     }
624     shash_destroy(&info->bridges);
625
626     shash_destroy_free_data(&info->ports);
627     shash_destroy_free_data(&info->ifaces);
628 }
629
630 static void
631 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
632 {
633     const struct ovsrec_open_vswitch *ovs = ctx->ovs;
634     struct shash bridges, ports;
635     size_t i;
636
637     info->ctx = ctx;
638     shash_init(&info->bridges);
639     shash_init(&info->ports);
640     shash_init(&info->ifaces);
641
642     shash_init(&bridges);
643     shash_init(&ports);
644     for (i = 0; i < ovs->n_bridges; i++) {
645         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
646         struct vsctl_bridge *br;
647         size_t j;
648
649         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
650             VLOG_WARN("%s: database contains duplicate bridge name",
651                       br_cfg->name);
652             continue;
653         }
654         br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
655         if (!br) {
656             continue;
657         }
658
659         for (j = 0; j < br_cfg->n_ports; j++) {
660             struct ovsrec_port *port_cfg = br_cfg->ports[j];
661
662             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
663                 VLOG_WARN("%s: database contains duplicate port name",
664                           port_cfg->name);
665                 continue;
666             }
667
668             if (port_is_fake_bridge(port_cfg)
669                 && shash_add_once(&bridges, port_cfg->name, NULL)) {
670                 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
671             }
672         }
673     }
674     shash_destroy(&bridges);
675     shash_destroy(&ports);
676
677     shash_init(&bridges);
678     shash_init(&ports);
679     for (i = 0; i < ovs->n_bridges; i++) {
680         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
681         struct vsctl_bridge *br;
682         size_t j;
683
684         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
685             continue;
686         }
687         br = shash_find_data(&info->bridges, br_cfg->name);
688         for (j = 0; j < br_cfg->n_ports; j++) {
689             struct ovsrec_port *port_cfg = br_cfg->ports[j];
690             struct vsctl_port *port;
691             size_t k;
692
693             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
694                 continue;
695             }
696
697             if (port_is_fake_bridge(port_cfg)
698                 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
699                 continue;
700             }
701
702             port = xmalloc(sizeof *port);
703             port->port_cfg = port_cfg;
704             if (port_cfg->tag
705                 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
706                 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
707                 if (!port->bridge) {
708                     port->bridge = br;
709                 }
710             } else {
711                 port->bridge = br;
712             }
713             shash_add(&info->ports, port_cfg->name, port);
714
715             for (k = 0; k < port_cfg->n_interfaces; k++) {
716                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
717                 struct vsctl_iface *iface;
718
719                 if (shash_find(&info->ifaces, iface_cfg->name)) {
720                     VLOG_WARN("%s: database contains duplicate interface name",
721                               iface_cfg->name);
722                     continue;
723                 }
724
725                 iface = xmalloc(sizeof *iface);
726                 iface->iface_cfg = iface_cfg;
727                 iface->port = port;
728                 shash_add(&info->ifaces, iface_cfg->name, iface);
729             }
730         }
731     }
732     shash_destroy(&bridges);
733     shash_destroy(&ports);
734 }
735
736 static void
737 check_conflicts(struct vsctl_info *info, const char *name,
738                 char *msg)
739 {
740     struct vsctl_iface *iface;
741     struct vsctl_port *port;
742
743     if (shash_find(&info->bridges, name)) {
744         vsctl_fatal("%s because a bridge named %s already exists",
745                     msg, name);
746     }
747
748     port = shash_find_data(&info->ports, name);
749     if (port) {
750         vsctl_fatal("%s because a port named %s already exists on "
751                     "bridge %s", msg, name, port->bridge->name);
752     }
753
754     iface = shash_find_data(&info->ifaces, name);
755     if (iface) {
756         vsctl_fatal("%s because an interface named %s already exists "
757                     "on bridge %s", msg, name, iface->port->bridge->name);
758     }
759
760     free(msg);
761 }
762
763 static struct vsctl_bridge *
764 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
765 {
766     struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
767     if (must_exist && !br) {
768         vsctl_fatal("no bridge named %s", name);
769     }
770     return br;
771 }
772
773 static struct vsctl_bridge *
774 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
775 {
776     struct vsctl_bridge *br = find_bridge(info, name, must_exist);
777     if (br && br->parent) {
778         vsctl_fatal("%s is a fake bridge", name);
779     }
780     return br;
781 }
782
783 static struct vsctl_port *
784 find_port(struct vsctl_info *info, const char *name, bool must_exist)
785 {
786     struct vsctl_port *port = shash_find_data(&info->ports, name);
787     if (port && !strcmp(name, port->bridge->name)) {
788         port = NULL;
789     }
790     if (must_exist && !port) {
791         vsctl_fatal("no port named %s", name);
792     }
793     return port;
794 }
795
796 static struct vsctl_iface *
797 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
798 {
799     struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
800     if (iface && !strcmp(name, iface->port->bridge->name)) {
801         iface = NULL;
802     }
803     if (must_exist && !iface) {
804         vsctl_fatal("no interface named %s", name);
805     }
806     return iface;
807 }
808
809 static void
810 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
811 {
812     struct ovsrec_port **ports;
813     size_t i;
814
815     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
816     for (i = 0; i < br->n_ports; i++) {
817         ports[i] = br->ports[i];
818     }
819     ports[br->n_ports] = port;
820     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
821     free(ports);
822 }
823
824 static void
825 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
826 {
827     struct ovsrec_port **ports;
828     size_t i, n;
829
830     ports = xmalloc(sizeof *br->ports * br->n_ports);
831     for (i = n = 0; i < br->n_ports; i++) {
832         if (br->ports[i] != port) {
833             ports[n++] = br->ports[i];
834         }
835     }
836     ovsrec_bridge_set_ports(br, ports, n);
837     free(ports);
838 }
839
840 static void
841 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
842                   struct ovsrec_bridge *bridge)
843 {
844     struct ovsrec_bridge **bridges;
845     size_t i;
846
847     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
848     for (i = 0; i < ovs->n_bridges; i++) {
849         bridges[i] = ovs->bridges[i];
850     }
851     bridges[ovs->n_bridges] = bridge;
852     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
853     free(bridges);
854 }
855
856 static void
857 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
858                   struct ovsrec_bridge *bridge)
859 {
860     struct ovsrec_bridge **bridges;
861     size_t i, n;
862
863     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
864     for (i = n = 0; i < ovs->n_bridges; i++) {
865         if (ovs->bridges[i] != bridge) {
866             bridges[n++] = ovs->bridges[i];
867         }
868     }
869     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
870     free(bridges);
871 }
872
873 static void
874 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
875 {
876 }
877
878 static void
879 cmd_emer_reset(struct vsctl_context *ctx)
880 {
881     const struct ovsdb_idl *idl = ctx->idl;
882     const struct ovsrec_bridge *br;
883     const struct ovsrec_port *port;
884     const struct ovsrec_interface *iface;
885     const struct ovsrec_mirror *mirror, *next_mirror;
886     const struct ovsrec_controller *ctrl, *next_ctrl;
887     const struct ovsrec_netflow *nf, *next_nf;
888     const struct ovsrec_ssl *ssl, *next_ssl;
889     const struct ovsrec_sflow *sflow, *next_sflow;
890
891
892     /* Reset the Open_vSwitch table. */
893     ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
894     ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
895
896     OVSREC_BRIDGE_FOR_EACH (br, idl) {
897         int i;
898         char *hw_key = "hwaddr";
899         char *hw_val = NULL;
900
901         ovsrec_bridge_set_controller(br, NULL, 0);
902         ovsrec_bridge_set_mirrors(br, NULL, 0);
903         ovsrec_bridge_set_netflow(br, NULL);
904         ovsrec_bridge_set_sflow(br, NULL);
905         ovsrec_bridge_set_flood_vlans(br, NULL, 0);
906
907         /* We only want to save the "hwaddr" key from other_config. */
908         for (i=0; i < br->n_other_config; i++) {
909             if (!strcmp(br->key_other_config[i], hw_key)) {
910                 hw_val = br->value_other_config[i];
911                 break;
912             }
913         }
914         if (hw_val) {
915             char *val = xstrdup(hw_val);
916             ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
917             free(val);
918         } else {
919             ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
920         }
921     }
922
923     OVSREC_PORT_FOR_EACH (port, idl) {
924         ovsrec_port_set_other_config(port, NULL, NULL, 0);
925     }
926
927     OVSREC_INTERFACE_FOR_EACH (iface, idl) {
928         /* xxx What do we do about gre/patch devices created by mgr? */
929
930         ovsrec_interface_set_ingress_policing_rate(iface, 0);
931         ovsrec_interface_set_ingress_policing_burst(iface, 0);
932     }
933
934     OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
935         ovsrec_mirror_delete(mirror);
936     }
937
938     OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
939         ovsrec_controller_delete(ctrl);
940     }
941
942     OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
943         ovsrec_netflow_delete(nf);
944     }
945
946     OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
947         ovsrec_ssl_delete(ssl);
948     }
949
950     OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
951         ovsrec_sflow_delete(sflow);
952     }
953 }
954
955 static void
956 cmd_add_br(struct vsctl_context *ctx)
957 {
958     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
959     const char *br_name, *parent_name;
960     struct vsctl_info info;
961     int vlan;
962
963     br_name = ctx->argv[1];
964     if (ctx->argc == 2) {
965         parent_name = NULL;
966         vlan = 0;
967     } else if (ctx->argc == 4) {
968         parent_name = ctx->argv[2];
969         vlan = atoi(ctx->argv[3]);
970         if (vlan < 1 || vlan > 4095) {
971             vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
972         }
973     } else {
974         vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
975                     ctx->argv[0]);
976     }
977
978     get_info(ctx, &info);
979     if (may_exist) {
980         struct vsctl_bridge *br;
981
982         br = find_bridge(&info, br_name, false);
983         if (br) {
984             if (!parent_name) {
985                 if (br->parent) {
986                     vsctl_fatal("\"--may-exist add-br %s\" but %s is "
987                                 "a VLAN bridge for VLAN %d",
988                                 br_name, br_name, br->vlan);
989                 }
990             } else {
991                 if (!br->parent) {
992                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
993                                 "is not a VLAN bridge",
994                                 br_name, parent_name, vlan, br_name);
995                 } else if (strcmp(br->parent->name, parent_name)) {
996                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
997                                 "has the wrong parent %s",
998                                 br_name, parent_name, vlan,
999                                 br_name, br->parent->name);
1000                 } else if (br->vlan != vlan) {
1001                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1002                                 "is a VLAN bridge for the wrong VLAN %d",
1003                                 br_name, parent_name, vlan, br_name, br->vlan);
1004                 }
1005             }
1006             return;
1007         }
1008     }
1009     check_conflicts(&info, br_name,
1010                     xasprintf("cannot create a bridge named %s", br_name));
1011
1012     if (!parent_name) {
1013         struct ovsrec_port *port;
1014         struct ovsrec_interface *iface;
1015         struct ovsrec_bridge *br;
1016
1017         iface = ovsrec_interface_insert(ctx->txn);
1018         ovsrec_interface_set_name(iface, br_name);
1019
1020         port = ovsrec_port_insert(ctx->txn);
1021         ovsrec_port_set_name(port, br_name);
1022         ovsrec_port_set_interfaces(port, &iface, 1);
1023
1024         br = ovsrec_bridge_insert(ctx->txn);
1025         ovsrec_bridge_set_name(br, br_name);
1026         ovsrec_bridge_set_ports(br, &port, 1);
1027
1028         ovs_insert_bridge(ctx->ovs, br);
1029     } else {
1030         struct vsctl_bridge *parent;
1031         struct ovsrec_port *port;
1032         struct ovsrec_interface *iface;
1033         struct ovsrec_bridge *br;
1034         int64_t tag = vlan;
1035
1036         parent = find_bridge(&info, parent_name, false);
1037         if (parent && parent->vlan) {
1038             vsctl_fatal("cannot create bridge with fake bridge as parent");
1039         }
1040         if (!parent) {
1041             vsctl_fatal("parent bridge %s does not exist", parent_name);
1042         }
1043         br = parent->br_cfg;
1044
1045         iface = ovsrec_interface_insert(ctx->txn);
1046         ovsrec_interface_set_name(iface, br_name);
1047         ovsrec_interface_set_type(iface, "internal");
1048
1049         port = ovsrec_port_insert(ctx->txn);
1050         ovsrec_port_set_name(port, br_name);
1051         ovsrec_port_set_interfaces(port, &iface, 1);
1052         ovsrec_port_set_fake_bridge(port, true);
1053         ovsrec_port_set_tag(port, &tag, 1);
1054
1055         bridge_insert_port(br, port);
1056     }
1057
1058     free_info(&info);
1059 }
1060
1061 static void
1062 del_port(struct vsctl_info *info, struct vsctl_port *port)
1063 {
1064     struct shash_node *node;
1065
1066     SHASH_FOR_EACH (node, &info->ifaces) {
1067         struct vsctl_iface *iface = node->data;
1068         if (iface->port == port) {
1069             ovsrec_interface_delete(iface->iface_cfg);
1070         }
1071     }
1072     ovsrec_port_delete(port->port_cfg);
1073
1074     bridge_delete_port((port->bridge->parent
1075                         ? port->bridge->parent->br_cfg
1076                         : port->bridge->br_cfg), port->port_cfg);
1077 }
1078
1079 static void
1080 cmd_del_br(struct vsctl_context *ctx)
1081 {
1082     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1083     struct vsctl_bridge *bridge;
1084     struct vsctl_info info;
1085
1086     get_info(ctx, &info);
1087     bridge = find_bridge(&info, ctx->argv[1], must_exist);
1088     if (bridge) {
1089         struct shash_node *node;
1090
1091         SHASH_FOR_EACH (node, &info.ports) {
1092             struct vsctl_port *port = node->data;
1093             if (port->bridge == bridge || port->bridge->parent == bridge
1094                 || !strcmp(port->port_cfg->name, bridge->name)) {
1095                 del_port(&info, port);
1096             }
1097         }
1098         if (bridge->br_cfg) {
1099             ovsrec_bridge_delete(bridge->br_cfg);
1100             ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1101         }
1102     }
1103     free_info(&info);
1104 }
1105
1106 static void
1107 output_sorted(struct svec *svec, struct ds *output)
1108 {
1109     const char *name;
1110     size_t i;
1111
1112     svec_sort(svec);
1113     SVEC_FOR_EACH (i, name, svec) {
1114         ds_put_format(output, "%s\n", name);
1115     }
1116 }
1117
1118 static void
1119 cmd_list_br(struct vsctl_context *ctx)
1120 {
1121     struct shash_node *node;
1122     struct vsctl_info info;
1123     struct svec bridges;
1124
1125     get_info(ctx, &info);
1126
1127     svec_init(&bridges);
1128     SHASH_FOR_EACH (node, &info.bridges) {
1129         struct vsctl_bridge *br = node->data;
1130         svec_add(&bridges, br->name);
1131     }
1132     output_sorted(&bridges, &ctx->output);
1133     svec_destroy(&bridges);
1134
1135     free_info(&info);
1136 }
1137
1138 static void
1139 cmd_br_exists(struct vsctl_context *ctx)
1140 {
1141     struct vsctl_info info;
1142
1143     get_info(ctx, &info);
1144     if (!find_bridge(&info, ctx->argv[1], false)) {
1145         vsctl_exit(2);
1146     }
1147     free_info(&info);
1148 }
1149
1150 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1151  * equals 'a', false otherwise. */
1152 static bool
1153 key_matches(const char *a,
1154             const char *b_prefix, size_t b_prefix_len, const char *b)
1155 {
1156     return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1157 }
1158
1159 static void
1160 set_external_id(char **old_keys, char **old_values, size_t old_n,
1161                 char *key, char *value,
1162                 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1163 {
1164     char **new_keys;
1165     char **new_values;
1166     size_t new_n;
1167     size_t i;
1168
1169     new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1170     new_values = xmalloc(sizeof *new_values * (old_n + 1));
1171     new_n = 0;
1172     for (i = 0; i < old_n; i++) {
1173         if (strcmp(key, old_keys[i])) {
1174             new_keys[new_n] = old_keys[i];
1175             new_values[new_n] = old_values[i];
1176             new_n++;
1177         }
1178     }
1179     if (value) {
1180         new_keys[new_n] = key;
1181         new_values[new_n] = value;
1182         new_n++;
1183     }
1184     *new_keysp = new_keys;
1185     *new_valuesp = new_values;
1186     *new_np = new_n;
1187 }
1188
1189 static void
1190 cmd_br_set_external_id(struct vsctl_context *ctx)
1191 {
1192     struct vsctl_info info;
1193     struct vsctl_bridge *bridge;
1194     char **keys, **values;
1195     size_t n;
1196
1197     get_info(ctx, &info);
1198     bridge = find_bridge(&info, ctx->argv[1], true);
1199     if (bridge->br_cfg) {
1200         set_external_id(bridge->br_cfg->key_external_ids,
1201                         bridge->br_cfg->value_external_ids,
1202                         bridge->br_cfg->n_external_ids,
1203                         ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1204                         &keys, &values, &n);
1205         ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1206     } else {
1207         char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1208         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1209         set_external_id(port->port_cfg->key_external_ids,
1210                         port->port_cfg->value_external_ids,
1211                         port->port_cfg->n_external_ids,
1212                         key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1213                         &keys, &values, &n);
1214         ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1215         free(key);
1216     }
1217     free(keys);
1218     free(values);
1219
1220     free_info(&info);
1221 }
1222
1223 static void
1224 get_external_id(char **keys, char **values, size_t n,
1225                 const char *prefix, const char *key,
1226                 struct ds *output)
1227 {
1228     size_t prefix_len = strlen(prefix);
1229     struct svec svec;
1230     size_t i;
1231
1232     svec_init(&svec);
1233     for (i = 0; i < n; i++) {
1234         if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1235             svec_add_nocopy(&svec, xasprintf("%s=%s",
1236                                              keys[i] + prefix_len, values[i]));
1237         } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1238             svec_add(&svec, values[i]);
1239             break;
1240         }
1241     }
1242     output_sorted(&svec, output);
1243     svec_destroy(&svec);
1244 }
1245
1246 static void
1247 cmd_br_get_external_id(struct vsctl_context *ctx)
1248 {
1249     struct vsctl_info info;
1250     struct vsctl_bridge *bridge;
1251
1252     get_info(ctx, &info);
1253     bridge = find_bridge(&info, ctx->argv[1], true);
1254     if (bridge->br_cfg) {
1255         get_external_id(bridge->br_cfg->key_external_ids,
1256                         bridge->br_cfg->value_external_ids,
1257                         bridge->br_cfg->n_external_ids,
1258                         "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1259                         &ctx->output);
1260     } else {
1261         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1262         get_external_id(port->port_cfg->key_external_ids,
1263                         port->port_cfg->value_external_ids,
1264                         port->port_cfg->n_external_ids,
1265                         "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1266     }
1267     free_info(&info);
1268 }
1269
1270
1271 static void
1272 cmd_list_ports(struct vsctl_context *ctx)
1273 {
1274     struct vsctl_bridge *br;
1275     struct shash_node *node;
1276     struct vsctl_info info;
1277     struct svec ports;
1278
1279     get_info(ctx, &info);
1280     br = find_bridge(&info, ctx->argv[1], true);
1281
1282     svec_init(&ports);
1283     SHASH_FOR_EACH (node, &info.ports) {
1284         struct vsctl_port *port = node->data;
1285
1286         if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1287             svec_add(&ports, port->port_cfg->name);
1288         }
1289     }
1290     output_sorted(&ports, &ctx->output);
1291     svec_destroy(&ports);
1292
1293     free_info(&info);
1294 }
1295
1296 static void
1297 add_port(struct vsctl_context *ctx,
1298          const char *br_name, const char *port_name,
1299          bool may_exist, bool fake_iface,
1300          char *iface_names[], int n_ifaces,
1301          char *settings[], int n_settings)
1302 {
1303     struct vsctl_info info;
1304     struct vsctl_bridge *bridge;
1305     struct ovsrec_interface **ifaces;
1306     struct ovsrec_port *port;
1307     size_t i;
1308
1309     get_info(ctx, &info);
1310     if (may_exist) {
1311         struct vsctl_port *port;
1312
1313         port = find_port(&info, port_name, false);
1314         if (port) {
1315             struct svec want_names, have_names;
1316             size_t i;
1317
1318             svec_init(&want_names);
1319             for (i = 0; i < n_ifaces; i++) {
1320                 svec_add(&want_names, iface_names[i]);
1321             }
1322             svec_sort(&want_names);
1323
1324             svec_init(&have_names);
1325             for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1326                 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1327             }
1328             svec_sort(&have_names);
1329
1330             if (strcmp(port->bridge->name, br_name)) {
1331                 char *command = vsctl_context_to_string(ctx);
1332                 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1333                             command, port_name, port->bridge->name);
1334             }
1335
1336             if (!svec_equal(&want_names, &have_names)) {
1337                 char *have_names_string = svec_join(&have_names, ", ", "");
1338                 char *command = vsctl_context_to_string(ctx);
1339
1340                 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1341                             command, port_name, have_names_string);
1342             }
1343
1344             svec_destroy(&want_names);
1345             svec_destroy(&have_names);
1346
1347             return;
1348         }
1349     }
1350     check_conflicts(&info, port_name,
1351                     xasprintf("cannot create a port named %s", port_name));
1352     for (i = 0; i < n_ifaces; i++) {
1353         check_conflicts(&info, iface_names[i],
1354                         xasprintf("cannot create an interface named %s",
1355                                   iface_names[i]));
1356     }
1357     bridge = find_bridge(&info, br_name, true);
1358
1359     ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1360     for (i = 0; i < n_ifaces; i++) {
1361         ifaces[i] = ovsrec_interface_insert(ctx->txn);
1362         ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1363     }
1364
1365     port = ovsrec_port_insert(ctx->txn);
1366     ovsrec_port_set_name(port, port_name);
1367     ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1368     ovsrec_port_set_bond_fake_iface(port, fake_iface);
1369     free(ifaces);
1370
1371     if (bridge->vlan) {
1372         int64_t tag = bridge->vlan;
1373         ovsrec_port_set_tag(port, &tag, 1);
1374     }
1375
1376     for (i = 0; i < n_settings; i++) {
1377         set_column(get_table("Port"), &port->header_, settings[i],
1378                    ctx->symtab);
1379     }
1380
1381     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1382                         : bridge->br_cfg), port);
1383
1384     free_info(&info);
1385 }
1386
1387 static void
1388 cmd_add_port(struct vsctl_context *ctx)
1389 {
1390     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1391
1392     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1393              &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1394 }
1395
1396 static void
1397 cmd_add_bond(struct vsctl_context *ctx)
1398 {
1399     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1400     bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1401     int n_ifaces;
1402     int i;
1403
1404     n_ifaces = ctx->argc - 3;
1405     for (i = 3; i < ctx->argc; i++) {
1406         if (strchr(ctx->argv[i], '=')) {
1407             n_ifaces = i - 3;
1408             break;
1409         }
1410     }
1411     if (n_ifaces < 2) {
1412         vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1413                     "%d were specified", n_ifaces);
1414     }
1415
1416     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1417              &ctx->argv[3], n_ifaces,
1418              &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1419 }
1420
1421 static void
1422 cmd_del_port(struct vsctl_context *ctx)
1423 {
1424     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1425     bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1426     struct vsctl_port *port;
1427     struct vsctl_info info;
1428
1429     get_info(ctx, &info);
1430     if (!with_iface) {
1431         port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1432     } else {
1433         const char *target = ctx->argv[ctx->argc - 1];
1434         struct vsctl_iface *iface;
1435
1436         port = find_port(&info, target, false);
1437         if (!port) {
1438             iface = find_iface(&info, target, false);
1439             if (iface) {
1440                 port = iface->port;
1441             }
1442         }
1443         if (must_exist && !port) {
1444             vsctl_fatal("no port or interface named %s", target);
1445         }
1446     }
1447
1448     if (port) {
1449         if (ctx->argc == 3) {
1450             struct vsctl_bridge *bridge;
1451
1452             bridge = find_bridge(&info, ctx->argv[1], true);
1453             if (port->bridge != bridge) {
1454                 if (port->bridge->parent == bridge) {
1455                     vsctl_fatal("bridge %s does not have a port %s (although "
1456                                 "its parent bridge %s does)",
1457                                 ctx->argv[1], ctx->argv[2],
1458                                 bridge->parent->name);
1459                 } else {
1460                     vsctl_fatal("bridge %s does not have a port %s",
1461                                 ctx->argv[1], ctx->argv[2]);
1462                 }
1463             }
1464         }
1465
1466         del_port(&info, port);
1467     }
1468
1469     free_info(&info);
1470 }
1471
1472 static void
1473 cmd_port_to_br(struct vsctl_context *ctx)
1474 {
1475     struct vsctl_port *port;
1476     struct vsctl_info info;
1477
1478     get_info(ctx, &info);
1479     port = find_port(&info, ctx->argv[1], true);
1480     ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1481     free_info(&info);
1482 }
1483
1484 static void
1485 cmd_br_to_vlan(struct vsctl_context *ctx)
1486 {
1487     struct vsctl_bridge *bridge;
1488     struct vsctl_info info;
1489
1490     get_info(ctx, &info);
1491     bridge = find_bridge(&info, ctx->argv[1], true);
1492     ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1493     free_info(&info);
1494 }
1495
1496 static void
1497 cmd_br_to_parent(struct vsctl_context *ctx)
1498 {
1499     struct vsctl_bridge *bridge;
1500     struct vsctl_info info;
1501
1502     get_info(ctx, &info);
1503     bridge = find_bridge(&info, ctx->argv[1], true);
1504     if (bridge->parent) {
1505         bridge = bridge->parent;
1506     }
1507     ds_put_format(&ctx->output, "%s\n", bridge->name);
1508     free_info(&info);
1509 }
1510
1511 static void
1512 cmd_list_ifaces(struct vsctl_context *ctx)
1513 {
1514     struct vsctl_bridge *br;
1515     struct shash_node *node;
1516     struct vsctl_info info;
1517     struct svec ifaces;
1518
1519     get_info(ctx, &info);
1520     br = find_bridge(&info, ctx->argv[1], true);
1521
1522     svec_init(&ifaces);
1523     SHASH_FOR_EACH (node, &info.ifaces) {
1524         struct vsctl_iface *iface = node->data;
1525
1526         if (strcmp(iface->iface_cfg->name, br->name)
1527             && br == iface->port->bridge) {
1528             svec_add(&ifaces, iface->iface_cfg->name);
1529         }
1530     }
1531     output_sorted(&ifaces, &ctx->output);
1532     svec_destroy(&ifaces);
1533
1534     free_info(&info);
1535 }
1536
1537 static void
1538 cmd_iface_to_br(struct vsctl_context *ctx)
1539 {
1540     struct vsctl_iface *iface;
1541     struct vsctl_info info;
1542
1543     get_info(ctx, &info);
1544     iface = find_iface(&info, ctx->argv[1], true);
1545     ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1546     free_info(&info);
1547 }
1548
1549 static void
1550 cmd_get_controller(struct vsctl_context *ctx)
1551 {
1552     struct vsctl_info info;
1553     struct vsctl_bridge *br;
1554     struct svec targets;
1555     size_t i;
1556
1557     get_info(ctx, &info);
1558     br = find_bridge(&info, ctx->argv[1], true);
1559
1560     /* Print the targets in sorted order for reproducibility. */
1561     svec_init(&targets);
1562     for (i = 0; i < br->n_ctrl; i++) {
1563         svec_add(&targets, br->ctrl[i]->target);
1564     }
1565
1566     svec_sort(&targets);
1567     for (i = 0; i < targets.n; i++) {
1568         ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1569     }
1570     svec_destroy(&targets);
1571
1572     free_info(&info);
1573 }
1574
1575 static void
1576 delete_controllers(struct ovsrec_controller **controllers,
1577                    size_t n_controllers)
1578 {
1579     size_t i;
1580
1581     for (i = 0; i < n_controllers; i++) {
1582         ovsrec_controller_delete(controllers[i]);
1583     }
1584 }
1585
1586 static void
1587 cmd_del_controller(struct vsctl_context *ctx)
1588 {
1589     struct vsctl_info info;
1590     struct vsctl_bridge *br;
1591
1592     get_info(ctx, &info);
1593     br = find_real_bridge(&info, ctx->argv[1], true);
1594
1595     if (br->ctrl) {
1596         delete_controllers(br->ctrl, br->n_ctrl);
1597         ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1598     }
1599
1600     free_info(&info);
1601 }
1602
1603 static struct ovsrec_controller **
1604 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1605 {
1606     struct ovsrec_controller **controllers;
1607     size_t i;
1608
1609     controllers = xmalloc(n * sizeof *controllers);
1610     for (i = 0; i < n; i++) {
1611         controllers[i] = ovsrec_controller_insert(txn);
1612         ovsrec_controller_set_target(controllers[i], targets[i]);
1613     }
1614
1615     return controllers;
1616 }
1617
1618 static void
1619 cmd_set_controller(struct vsctl_context *ctx)
1620 {
1621     struct vsctl_info info;
1622     struct vsctl_bridge *br;
1623     struct ovsrec_controller **controllers;
1624     size_t n;
1625
1626     get_info(ctx, &info);
1627     br = find_real_bridge(&info, ctx->argv[1], true);
1628
1629     delete_controllers(br->ctrl, br->n_ctrl);
1630
1631     n = ctx->argc - 2;
1632     controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1633     ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1634     free(controllers);
1635
1636     free_info(&info);
1637 }
1638
1639 static void
1640 cmd_get_fail_mode(struct vsctl_context *ctx)
1641 {
1642     struct vsctl_info info;
1643     struct vsctl_bridge *br;
1644
1645     get_info(ctx, &info);
1646     br = find_bridge(&info, ctx->argv[1], true);
1647
1648     if (br->fail_mode && strlen(br->fail_mode)) {
1649         ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1650     }
1651
1652     free_info(&info);
1653 }
1654
1655 static void
1656 cmd_del_fail_mode(struct vsctl_context *ctx)
1657 {
1658     struct vsctl_info info;
1659     struct vsctl_bridge *br;
1660
1661     get_info(ctx, &info);
1662     br = find_real_bridge(&info, ctx->argv[1], true);
1663
1664     ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1665
1666     free_info(&info);
1667 }
1668
1669 static void
1670 cmd_set_fail_mode(struct vsctl_context *ctx)
1671 {
1672     struct vsctl_info info;
1673     struct vsctl_bridge *br;
1674     const char *fail_mode = ctx->argv[2];
1675
1676     get_info(ctx, &info);
1677     br = find_real_bridge(&info, ctx->argv[1], true);
1678
1679     if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1680         vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1681     }
1682
1683     ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1684
1685     free_info(&info);
1686 }
1687
1688 static void
1689 cmd_get_ssl(struct vsctl_context *ctx)
1690 {
1691     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1692
1693     if (ssl) {
1694         ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1695         ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1696         ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1697         ds_put_format(&ctx->output, "Bootstrap: %s\n",
1698                 ssl->bootstrap_ca_cert ? "true" : "false");
1699     }
1700 }
1701
1702 static void
1703 cmd_del_ssl(struct vsctl_context *ctx)
1704 {
1705     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1706
1707     if (ssl) {
1708         ovsrec_ssl_delete(ssl);
1709         ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1710     }
1711 }
1712
1713 static void
1714 cmd_set_ssl(struct vsctl_context *ctx)
1715 {
1716     bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1717     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1718
1719     if (ssl) {
1720         ovsrec_ssl_delete(ssl);
1721     }
1722     ssl = ovsrec_ssl_insert(ctx->txn);
1723
1724     ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1725     ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1726     ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1727
1728     ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1729
1730     ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1731 }
1732 \f
1733 /* Parameter commands. */
1734
1735 struct vsctl_row_id {
1736     const struct ovsdb_idl_table_class *table;
1737     const struct ovsdb_idl_column *name_column;
1738     const struct ovsdb_idl_column *uuid_column;
1739 };
1740
1741 struct vsctl_table_class {
1742     struct ovsdb_idl_table_class *class;
1743     struct vsctl_row_id row_ids[2];
1744 };
1745
1746 static const struct vsctl_table_class tables[] = {
1747     {&ovsrec_table_bridge,
1748      {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1749       {NULL, NULL, NULL}}},
1750
1751     {&ovsrec_table_controller,
1752      {{&ovsrec_table_bridge,
1753        &ovsrec_bridge_col_name,
1754        &ovsrec_bridge_col_controller}}},
1755
1756     {&ovsrec_table_interface,
1757      {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1758       {NULL, NULL, NULL}}},
1759
1760     {&ovsrec_table_mirror,
1761      {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1762       {NULL, NULL, NULL}}},
1763
1764     {&ovsrec_table_netflow,
1765      {{&ovsrec_table_bridge,
1766        &ovsrec_bridge_col_name,
1767        &ovsrec_bridge_col_netflow},
1768       {NULL, NULL, NULL}}},
1769
1770     {&ovsrec_table_open_vswitch,
1771      {{&ovsrec_table_open_vswitch, NULL, NULL},
1772       {NULL, NULL, NULL}}},
1773
1774     {&ovsrec_table_port,
1775      {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1776       {NULL, NULL, NULL}}},
1777
1778     {&ovsrec_table_qos,
1779      {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1780       {NULL, NULL, NULL}}},
1781
1782     {&ovsrec_table_queue,
1783      {{NULL, NULL, NULL},
1784       {NULL, NULL, NULL}}},
1785
1786     {&ovsrec_table_ssl,
1787      {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1788
1789     {&ovsrec_table_sflow,
1790      {{&ovsrec_table_bridge,
1791        &ovsrec_bridge_col_name,
1792        &ovsrec_bridge_col_sflow},
1793       {NULL, NULL, NULL}}},
1794
1795     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1796 };
1797
1798 static void
1799 die_if_error(char *error)
1800 {
1801     if (error) {
1802         vsctl_fatal("%s", error);
1803     }
1804 }
1805
1806 static int
1807 to_lower_and_underscores(unsigned c)
1808 {
1809     return c == '-' ? '_' : tolower(c);
1810 }
1811
1812 static unsigned int
1813 score_partial_match(const char *name, const char *s)
1814 {
1815     int score;
1816
1817     if (!strcmp(name, s)) {
1818         return UINT_MAX;
1819     }
1820     for (score = 0; ; score++, name++, s++) {
1821         if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1822             break;
1823         } else if (*name == '\0') {
1824             return UINT_MAX - 1;
1825         }
1826     }
1827     return *s == '\0' ? score : 0;
1828 }
1829
1830 static const struct vsctl_table_class *
1831 get_table(const char *table_name)
1832 {
1833     const struct vsctl_table_class *table;
1834     const struct vsctl_table_class *best_match = NULL;
1835     unsigned int best_score = 0;
1836
1837     for (table = tables; table->class; table++) {
1838         unsigned int score = score_partial_match(table->class->name,
1839                                                  table_name);
1840         if (score > best_score) {
1841             best_match = table;
1842             best_score = score;
1843         } else if (score == best_score) {
1844             best_match = NULL;
1845         }
1846     }
1847     if (best_match) {
1848         return best_match;
1849     } else if (best_score) {
1850         vsctl_fatal("multiple table names match \"%s\"", table_name);
1851     } else {
1852         vsctl_fatal("unknown table \"%s\"", table_name);
1853     }
1854 }
1855
1856 static const struct ovsdb_idl_row *
1857 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1858               const struct vsctl_row_id *id, const char *record_id)
1859 {
1860     const struct ovsdb_idl_row *referrer, *final;
1861
1862     if (!id->table) {
1863         return NULL;
1864     }
1865
1866     if (!id->name_column) {
1867         if (strcmp(record_id, ".")) {
1868             return NULL;
1869         }
1870         referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1871         if (!referrer || ovsdb_idl_next_row(referrer)) {
1872             return NULL;
1873         }
1874     } else {
1875         const struct ovsdb_idl_row *row;
1876
1877         referrer = NULL;
1878         for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1879              row != NULL;
1880              row = ovsdb_idl_next_row(row))
1881         {
1882             const struct ovsdb_datum *name;
1883
1884             name = ovsdb_idl_get(row, id->name_column,
1885                                  OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1886             if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1887                 if (referrer) {
1888                     vsctl_fatal("multiple rows in %s match \"%s\"",
1889                                 table->class->name, record_id);
1890                 }
1891                 referrer = row;
1892             }
1893         }
1894     }
1895     if (!referrer) {
1896         return NULL;
1897     }
1898
1899     final = NULL;
1900     if (id->uuid_column) {
1901         const struct ovsdb_datum *uuid;
1902
1903         uuid = ovsdb_idl_get(referrer, id->uuid_column,
1904                              OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1905         if (uuid->n == 1) {
1906             final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1907                                                &uuid->keys[0].uuid);
1908         }
1909     } else {
1910         final = referrer;
1911     }
1912
1913     return final;
1914 }
1915
1916 static const struct ovsdb_idl_row *
1917 get_row (struct vsctl_context *ctx,
1918          const struct vsctl_table_class *table, const char *record_id)
1919 {
1920     const struct ovsdb_idl_row *row;
1921     struct uuid uuid;
1922
1923     if (uuid_from_string(&uuid, record_id)) {
1924         row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1925     } else {
1926         int i;
1927
1928         for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1929             row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1930             if (row) {
1931                 break;
1932             }
1933         }
1934     }
1935     return row;
1936 }
1937
1938 static const struct ovsdb_idl_row *
1939 must_get_row(struct vsctl_context *ctx,
1940              const struct vsctl_table_class *table, const char *record_id)
1941 {
1942     const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1943     if (!row) {
1944         vsctl_fatal("no row \"%s\" in table %s",
1945                     record_id, table->class->name);
1946     }
1947     return row;
1948 }
1949
1950 static char *
1951 get_column(const struct vsctl_table_class *table, const char *column_name,
1952            const struct ovsdb_idl_column **columnp)
1953 {
1954     const struct ovsdb_idl_column *best_match = NULL;
1955     unsigned int best_score = 0;
1956     size_t i;
1957
1958     for (i = 0; i < table->class->n_columns; i++) {
1959         const struct ovsdb_idl_column *column = &table->class->columns[i];
1960         unsigned int score = score_partial_match(column->name, column_name);
1961         if (score > best_score) {
1962             best_match = column;
1963             best_score = score;
1964         } else if (score == best_score) {
1965             best_match = NULL;
1966         }
1967     }
1968
1969     *columnp = best_match;
1970     if (best_match) {
1971         return NULL;
1972     } else if (best_score) {
1973         return xasprintf("%s contains more than one column whose name "
1974                          "matches \"%s\"", table->class->name, column_name);
1975     } else {
1976         return xasprintf("%s does not contain a column whose name matches "
1977                          "\"%s\"", table->class->name, column_name);
1978     }
1979 }
1980
1981 static char *
1982 missing_operator_error(const char *arg, const char **allowed_operators,
1983                        size_t n_allowed)
1984 {
1985     struct ds s;
1986
1987     ds_init(&s);
1988     ds_put_format(&s, "%s: argument does not end in ", arg);
1989     ds_put_format(&s, "\"%s\"", allowed_operators[0]);
1990     if (n_allowed == 2) {
1991         ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
1992     } else if (n_allowed > 2) {
1993         size_t i;
1994
1995         for (i = 1; i < n_allowed - 1; i++) {
1996             ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
1997         }
1998         ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
1999     }
2000     ds_put_format(&s, " followed by a value.");
2001
2002     return ds_steal_cstr(&s);
2003 }
2004
2005 /* Breaks 'arg' apart into a number of fields in the following order:
2006  *
2007  *      - If 'columnp' is nonnull, the name of a column in 'table'.  The column
2008  *        is stored into '*columnp'.  The column name may be abbreviated.
2009  *
2010  *      - If 'keyp' is nonnull, optionally a key string.  (If both 'columnp'
2011  *        and 'keyp' are nonnull, then the column and key names are expected to
2012  *        be separated by ':').  The key is stored as a malloc()'d string into
2013  *        '*keyp', or NULL if no key is present in 'arg'.
2014  *
2015  *      - If 'valuep' is nonnull, an operator followed by a value string.  The
2016  *        allowed operators are the 'n_allowed' string in 'allowed_operators',
2017  *        or just "=" if 'n_allowed' is 0.  If 'operatorp' is nonnull, then the
2018  *        operator is stored into '*operatorp' (one of the pointers from
2019  *        'allowed_operators' is stored; nothing is malloc()'d).  The value is
2020  *        stored as a malloc()'d string into '*valuep', or NULL if no value is
2021  *        present in 'arg'.
2022  *
2023  * At least 'columnp' or 'keyp' must be nonnull.
2024  *
2025  * On success, returns NULL.  On failure, returned a malloc()'d string error
2026  * message and stores NULL into all of the nonnull output arguments. */
2027 static char * WARN_UNUSED_RESULT
2028 parse_column_key_value(const char *arg,
2029                        const struct vsctl_table_class *table,
2030                        const struct ovsdb_idl_column **columnp, char **keyp,
2031                        const char **operatorp,
2032                        const char **allowed_operators, size_t n_allowed,
2033                        char **valuep)
2034 {
2035     const char *p = arg;
2036     char *error;
2037
2038     assert(columnp || keyp);
2039     assert(!(operatorp && !valuep));
2040     if (keyp) {
2041         *keyp = NULL;
2042     }
2043     if (valuep) {
2044         *valuep = NULL;
2045     }
2046
2047     /* Parse column name. */
2048     if (columnp) {
2049         char *column_name;
2050
2051         error = ovsdb_token_parse(&p, &column_name);
2052         if (error) {
2053             goto error;
2054         }
2055         if (column_name[0] == '\0') {
2056             free(column_name);
2057             error = xasprintf("%s: missing column name", arg);
2058             goto error;
2059         }
2060         error = get_column(table, column_name, columnp);
2061         free(column_name);
2062         if (error) {
2063             goto error;
2064         }
2065     }
2066
2067     /* Parse key string. */
2068     if (*p == ':' || !columnp) {
2069         if (columnp) {
2070             p++;
2071         } else if (!keyp) {
2072             error = xasprintf("%s: key not accepted here", arg);
2073             goto error;
2074         }
2075         error = ovsdb_token_parse(&p, keyp);
2076         if (error) {
2077             goto error;
2078         }
2079     } else if (keyp) {
2080         *keyp = NULL;
2081     }
2082
2083     /* Parse value string. */
2084     if (valuep) {
2085         const char *best;
2086         size_t best_len;
2087         size_t i;
2088
2089         if (!allowed_operators) {
2090             static const char *equals = "=";
2091             allowed_operators = &equals;
2092             n_allowed = 1;
2093         }
2094
2095         best = NULL;
2096         best_len = 0;
2097         for (i = 0; i < n_allowed; i++) {
2098             const char *op = allowed_operators[i];
2099             size_t op_len = strlen(op);
2100
2101             if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2102                 best_len = op_len;
2103                 best = op;
2104             }
2105         }
2106         if (!best) {
2107             error = missing_operator_error(arg, allowed_operators, n_allowed);
2108             goto error;
2109         }
2110
2111         if (operatorp) {
2112             *operatorp = best;
2113         }
2114         *valuep = xstrdup(p + best_len);
2115     } else {
2116         if (valuep) {
2117             *valuep = NULL;
2118         }
2119         if (*p != '\0') {
2120             error = xasprintf("%s: trailing garbage \"%s\" in argument",
2121                               arg, p);
2122             goto error;
2123         }
2124     }
2125     return NULL;
2126
2127 error:
2128     if (columnp) {
2129         *columnp = NULL;
2130     }
2131     if (keyp) {
2132         free(*keyp);
2133         *keyp = NULL;
2134     }
2135     if (valuep) {
2136         free(*valuep);
2137         *valuep = NULL;
2138         if (operatorp) {
2139             *operatorp = NULL;
2140         }
2141     }
2142     return error;
2143 }
2144
2145 static void
2146 cmd_get(struct vsctl_context *ctx)
2147 {
2148     bool if_exists = shash_find(&ctx->options, "--if-exists");
2149     const char *table_name = ctx->argv[1];
2150     const char *record_id = ctx->argv[2];
2151     const struct vsctl_table_class *table;
2152     const struct ovsdb_idl_row *row;
2153     struct ds *out = &ctx->output;
2154     int i;
2155
2156     table = get_table(table_name);
2157     row = must_get_row(ctx, table, record_id);
2158     for (i = 3; i < ctx->argc; i++) {
2159         const struct ovsdb_idl_column *column;
2160         const struct ovsdb_datum *datum;
2161         char *key_string;
2162
2163         /* Special case for obtaining the UUID of a row.  We can't just do this
2164          * through parse_column_key_value() below since it returns a "struct
2165          * ovsdb_idl_column" and the UUID column doesn't have one. */
2166         if (!strcasecmp(ctx->argv[i], "_uuid")
2167             || !strcasecmp(ctx->argv[i], "-uuid")) {
2168             ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2169             continue;
2170         }
2171
2172         die_if_error(parse_column_key_value(ctx->argv[i], table,
2173                                             &column, &key_string,
2174                                             NULL, NULL, 0, NULL));
2175
2176         datum = ovsdb_idl_read(row, column);
2177         if (key_string) {
2178             union ovsdb_atom key;
2179             unsigned int idx;
2180
2181             if (column->type.value.type == OVSDB_TYPE_VOID) {
2182                 vsctl_fatal("cannot specify key to get for non-map column %s",
2183                             column->name);
2184             }
2185
2186             die_if_error(ovsdb_atom_from_string(&key,
2187                                                 &column->type.key,
2188                                                 key_string, ctx->symtab));
2189
2190             idx = ovsdb_datum_find_key(datum, &key,
2191                                        column->type.key.type);
2192             if (idx == UINT_MAX) {
2193                 if (!if_exists) {
2194                     vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2195                                 key_string, table->class->name, record_id,
2196                                 column->name);
2197                 }
2198             } else {
2199                 ovsdb_atom_to_string(&datum->values[idx],
2200                                      column->type.value.type, out);
2201             }
2202             ovsdb_atom_destroy(&key, column->type.key.type);
2203         } else {
2204             ovsdb_datum_to_string(datum, &column->type, out);
2205         }
2206         ds_put_char(out, '\n');
2207
2208         free(key_string);
2209     }
2210 }
2211
2212 static void
2213 list_record(const struct vsctl_table_class *table,
2214             const struct ovsdb_idl_row *row, struct ds *out)
2215 {
2216     size_t i;
2217
2218     ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2219                   UUID_ARGS(&row->uuid));
2220     for (i = 0; i < table->class->n_columns; i++) {
2221         const struct ovsdb_idl_column *column = &table->class->columns[i];
2222         const struct ovsdb_datum *datum;
2223
2224         datum = ovsdb_idl_read(row, column);
2225
2226         ds_put_format(out, "%-20s: ", column->name);
2227         ovsdb_datum_to_string(datum, &column->type, out);
2228         ds_put_char(out, '\n');
2229     }
2230 }
2231
2232 static void
2233 cmd_list(struct vsctl_context *ctx)
2234 {
2235     const char *table_name = ctx->argv[1];
2236     const struct vsctl_table_class *table;
2237     struct ds *out = &ctx->output;
2238     int i;
2239
2240     table = get_table(table_name);
2241     if (ctx->argc > 2) {
2242         for (i = 2; i < ctx->argc; i++) {
2243             if (i > 2) {
2244                 ds_put_char(out, '\n');
2245             }
2246             list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2247         }
2248     } else {
2249         const struct ovsdb_idl_row *row;
2250         bool first;
2251
2252         for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2253              row != NULL;
2254              row = ovsdb_idl_next_row(row), first = false) {
2255             if (!first) {
2256                 ds_put_char(out, '\n');
2257             }
2258             list_record(table, row, out);
2259         }
2260     }
2261 }
2262
2263 static void
2264 set_column(const struct vsctl_table_class *table,
2265            const struct ovsdb_idl_row *row, const char *arg,
2266            struct ovsdb_symbol_table *symtab)
2267 {
2268     const struct ovsdb_idl_column *column;
2269     char *key_string, *value_string;
2270     char *error;
2271
2272     error = parse_column_key_value(arg, table, &column, &key_string,
2273                                    NULL, NULL, 0, &value_string);
2274     die_if_error(error);
2275     if (!value_string) {
2276         vsctl_fatal("%s: missing value", arg);
2277     }
2278
2279     if (key_string) {
2280         union ovsdb_atom key, value;
2281         struct ovsdb_datum datum;
2282
2283         if (column->type.value.type == OVSDB_TYPE_VOID) {
2284             vsctl_fatal("cannot specify key to set for non-map column %s",
2285                         column->name);
2286         }
2287
2288         die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2289                                             key_string, symtab));
2290         die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2291                                             value_string, symtab));
2292
2293         ovsdb_datum_init_empty(&datum);
2294         ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2295
2296         ovsdb_atom_destroy(&key, column->type.key.type);
2297         ovsdb_atom_destroy(&value, column->type.value.type);
2298
2299         ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2300                           &column->type, false);
2301         ovsdb_idl_txn_write(row, column, &datum);
2302     } else {
2303         struct ovsdb_datum datum;
2304
2305         die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2306                                              value_string, symtab));
2307         ovsdb_idl_txn_write(row, column, &datum);
2308     }
2309
2310     free(key_string);
2311     free(value_string);
2312 }
2313
2314 static void
2315 cmd_set(struct vsctl_context *ctx)
2316 {
2317     const char *table_name = ctx->argv[1];
2318     const char *record_id = ctx->argv[2];
2319     const struct vsctl_table_class *table;
2320     const struct ovsdb_idl_row *row;
2321     int i;
2322
2323     table = get_table(table_name);
2324     row = must_get_row(ctx, table, record_id);
2325     for (i = 3; i < ctx->argc; i++) {
2326         set_column(table, row, ctx->argv[i], ctx->symtab);
2327     }
2328 }
2329
2330 static void
2331 cmd_add(struct vsctl_context *ctx)
2332 {
2333     const char *table_name = ctx->argv[1];
2334     const char *record_id = ctx->argv[2];
2335     const char *column_name = ctx->argv[3];
2336     const struct vsctl_table_class *table;
2337     const struct ovsdb_idl_column *column;
2338     const struct ovsdb_idl_row *row;
2339     const struct ovsdb_type *type;
2340     struct ovsdb_datum old;
2341     int i;
2342
2343     table = get_table(table_name);
2344     row = must_get_row(ctx, table, record_id);
2345     die_if_error(get_column(table, column_name, &column));
2346
2347     type = &column->type;
2348     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2349     for (i = 4; i < ctx->argc; i++) {
2350         struct ovsdb_type add_type;
2351         struct ovsdb_datum add;
2352
2353         add_type = *type;
2354         add_type.n_min = 1;
2355         add_type.n_max = UINT_MAX;
2356         die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2357                                              ctx->symtab));
2358         ovsdb_datum_union(&old, &add, type, false);
2359         ovsdb_datum_destroy(&add, type);
2360     }
2361     if (old.n > type->n_max) {
2362         vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2363                     "table %s but the maximum number is %u",
2364                     old.n,
2365                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2366                     column->name, table->class->name, type->n_max);
2367     }
2368     ovsdb_idl_txn_write(row, column, &old);
2369 }
2370
2371 static void
2372 cmd_remove(struct vsctl_context *ctx)
2373 {
2374     const char *table_name = ctx->argv[1];
2375     const char *record_id = ctx->argv[2];
2376     const char *column_name = ctx->argv[3];
2377     const struct vsctl_table_class *table;
2378     const struct ovsdb_idl_column *column;
2379     const struct ovsdb_idl_row *row;
2380     const struct ovsdb_type *type;
2381     struct ovsdb_datum old;
2382     int i;
2383
2384     table = get_table(table_name);
2385     row = must_get_row(ctx, table, record_id);
2386     die_if_error(get_column(table, column_name, &column));
2387
2388     type = &column->type;
2389     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2390     for (i = 4; i < ctx->argc; i++) {
2391         struct ovsdb_type rm_type;
2392         struct ovsdb_datum rm;
2393         char *error;
2394
2395         rm_type = *type;
2396         rm_type.n_min = 1;
2397         rm_type.n_max = UINT_MAX;
2398         error = ovsdb_datum_from_string(&rm, &rm_type,
2399                                         ctx->argv[i], ctx->symtab);
2400         if (error && ovsdb_type_is_map(&rm_type)) {
2401             free(error);
2402             rm_type.value.type = OVSDB_TYPE_VOID;
2403             die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2404                                                  ctx->argv[i], ctx->symtab));
2405         }
2406         ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2407         ovsdb_datum_destroy(&rm, &rm_type);
2408     }
2409     if (old.n < type->n_min) {
2410         vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2411                     "table %s but the minimum number is %u",
2412                     old.n,
2413                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2414                     column->name, table->class->name, type->n_min);
2415     }
2416     ovsdb_idl_txn_write(row, column, &old);
2417 }
2418
2419 static void
2420 cmd_clear(struct vsctl_context *ctx)
2421 {
2422     const char *table_name = ctx->argv[1];
2423     const char *record_id = ctx->argv[2];
2424     const struct vsctl_table_class *table;
2425     const struct ovsdb_idl_row *row;
2426     int i;
2427
2428     table = get_table(table_name);
2429     row = must_get_row(ctx, table, record_id);
2430     for (i = 3; i < ctx->argc; i++) {
2431         const struct ovsdb_idl_column *column;
2432         const struct ovsdb_type *type;
2433         struct ovsdb_datum datum;
2434
2435         die_if_error(get_column(table, ctx->argv[i], &column));
2436
2437         type = &column->type;
2438         if (type->n_min > 0) {
2439             vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2440                         "of table %s, which is not allowed to be empty",
2441                         column->name, table->class->name);
2442         }
2443
2444         ovsdb_datum_init_empty(&datum);
2445         ovsdb_idl_txn_write(row, column, &datum);
2446     }
2447 }
2448
2449 static void
2450 cmd_create(struct vsctl_context *ctx)
2451 {
2452     const char *id = shash_find_data(&ctx->options, "--id");
2453     const char *table_name = ctx->argv[1];
2454     const struct vsctl_table_class *table;
2455     const struct ovsdb_idl_row *row;
2456     const struct uuid *uuid;
2457     int i;
2458
2459     if (id) {
2460         struct ovsdb_symbol *symbol;
2461
2462         if (id[0] != '@') {
2463             vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2464         }
2465
2466         symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2467         if (symbol->used) {
2468             vsctl_fatal("row id \"%s\" may only be used to insert a single "
2469                         "row", id);
2470         }
2471         symbol->used = true;
2472
2473         uuid = &symbol->uuid;
2474     } else {
2475         uuid = NULL;
2476     }
2477
2478     table = get_table(table_name);
2479     row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2480     for (i = 2; i < ctx->argc; i++) {
2481         set_column(table, row, ctx->argv[i], ctx->symtab);
2482     }
2483     ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2484 }
2485
2486 /* This function may be used as the 'postprocess' function for commands that
2487  * insert new rows into the database.  It expects that the command's 'run'
2488  * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2489  * sole output.  It replaces that output by the row's permanent UUID assigned
2490  * by the database server and appends a new-line.
2491  *
2492  * Currently we use this only for "create", because the higher-level commands
2493  * are supposed to be independent of the actual structure of the vswitch
2494  * configuration. */
2495 static void
2496 post_create(struct vsctl_context *ctx)
2497 {
2498     const struct uuid *real;
2499     struct uuid dummy;
2500
2501     uuid_from_string(&dummy, ds_cstr(&ctx->output));
2502     real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2503     if (real) {
2504         ds_clear(&ctx->output);
2505         ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2506     }
2507     ds_put_char(&ctx->output, '\n');
2508 }
2509
2510 static void
2511 cmd_destroy(struct vsctl_context *ctx)
2512 {
2513     bool must_exist = !shash_find(&ctx->options, "--if-exists");
2514     const char *table_name = ctx->argv[1];
2515     const struct vsctl_table_class *table;
2516     int i;
2517
2518     table = get_table(table_name);
2519     for (i = 2; i < ctx->argc; i++) {
2520         const struct ovsdb_idl_row *row;
2521
2522         row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2523         if (row) {
2524             ovsdb_idl_txn_delete(row);
2525         }
2526     }
2527 }
2528
2529 static bool
2530 is_condition_satified(const struct vsctl_table_class *table,
2531                       const struct ovsdb_idl_row *row, const char *arg,
2532                       struct ovsdb_symbol_table *symtab)
2533 {
2534     static const char *operators[] = {
2535         "=", "!=", "<", ">", "<=", ">="
2536     };
2537
2538     const struct ovsdb_idl_column *column;
2539     const struct ovsdb_datum *have_datum;
2540     char *key_string, *value_string;
2541     const char *operator;
2542     unsigned int idx;
2543     char *error;
2544     int cmp = 0;
2545
2546     error = parse_column_key_value(arg, table, &column, &key_string,
2547                                    &operator, operators, ARRAY_SIZE(operators),
2548                                    &value_string);
2549     die_if_error(error);
2550     if (!value_string) {
2551         vsctl_fatal("%s: missing value", arg);
2552     }
2553
2554     have_datum = ovsdb_idl_read(row, column);
2555     if (key_string) {
2556         union ovsdb_atom want_key, want_value;
2557
2558         if (column->type.value.type == OVSDB_TYPE_VOID) {
2559             vsctl_fatal("cannot specify key to check for non-map column %s",
2560                         column->name);
2561         }
2562
2563         die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2564                                             key_string, symtab));
2565         die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2566                                             value_string, symtab));
2567
2568         idx = ovsdb_datum_find_key(have_datum,
2569                                    &want_key, column->type.key.type);
2570         if (idx != UINT_MAX) {
2571             cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2572                                           &want_value,
2573                                           column->type.value.type);
2574         }
2575
2576         ovsdb_atom_destroy(&want_key, column->type.key.type);
2577         ovsdb_atom_destroy(&want_value, column->type.value.type);
2578     } else {
2579         struct ovsdb_datum want_datum;
2580
2581         die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2582                                              value_string, symtab));
2583         idx = 0;
2584         cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2585                                        &column->type);
2586         ovsdb_datum_destroy(&want_datum, &column->type);
2587     }
2588
2589     free(key_string);
2590     free(value_string);
2591
2592     return (idx == UINT_MAX ? false
2593             : !strcmp(operator, "=") ? cmp == 0
2594             : !strcmp(operator, "!=") ? cmp != 0
2595             : !strcmp(operator, "<") ? cmp < 0
2596             : !strcmp(operator, ">") ? cmp > 0
2597             : !strcmp(operator, "<=") ? cmp <= 0
2598             : !strcmp(operator, ">=") ? cmp >= 0
2599             : (abort(), 0));
2600 }
2601
2602 static void
2603 cmd_wait_until(struct vsctl_context *ctx)
2604 {
2605     const char *table_name = ctx->argv[1];
2606     const char *record_id = ctx->argv[2];
2607     const struct vsctl_table_class *table;
2608     const struct ovsdb_idl_row *row;
2609     int i;
2610
2611     table = get_table(table_name);
2612
2613     row = get_row(ctx, table, record_id);
2614     if (!row) {
2615         ctx->try_again = true;
2616         return;
2617     }
2618
2619     for (i = 3; i < ctx->argc; i++) {
2620         if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2621             ctx->try_again = true;
2622             return;
2623         }
2624     }
2625 }
2626 \f
2627 static struct json *
2628 where_uuid_equals(const struct uuid *uuid)
2629 {
2630     return
2631         json_array_create_1(
2632             json_array_create_3(
2633                 json_string_create("_uuid"),
2634                 json_string_create("=="),
2635                 json_array_create_2(
2636                     json_string_create("uuid"),
2637                     json_string_create_nocopy(
2638                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2639 }
2640
2641 static void
2642 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2643                    struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2644                    const struct ovsrec_open_vswitch *ovs,
2645     struct ovsdb_symbol_table *symtab)
2646 {
2647     ctx->argc = command->argc;
2648     ctx->argv = command->argv;
2649     ctx->options = command->options;
2650
2651     ds_swap(&ctx->output, &command->output);
2652     ctx->idl = idl;
2653     ctx->txn = txn;
2654     ctx->ovs = ovs;
2655     ctx->symtab = symtab;
2656
2657     ctx->try_again = false;
2658 }
2659
2660 static void
2661 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2662 {
2663     ds_swap(&ctx->output, &command->output);
2664 }
2665
2666 static void
2667 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2668          struct ovsdb_idl *idl)
2669 {
2670     struct ovsdb_idl_txn *txn;
2671     const struct ovsrec_open_vswitch *ovs;
2672     enum ovsdb_idl_txn_status status;
2673     struct ovsdb_symbol_table *symtab;
2674     const char *unused;
2675     struct vsctl_command *c;
2676     int64_t next_cfg = 0;
2677     char *error = NULL;
2678
2679     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2680     if (dry_run) {
2681         ovsdb_idl_txn_set_dry_run(txn);
2682     }
2683
2684     ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2685
2686     ovs = ovsrec_open_vswitch_first(idl);
2687     if (!ovs) {
2688         /* XXX add verification that table is empty */
2689         ovs = ovsrec_open_vswitch_insert(txn);
2690     }
2691
2692     if (wait_for_reload) {
2693         struct json *where = where_uuid_equals(&ovs->header_.uuid);
2694         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2695         json_destroy(where);
2696     }
2697
2698     symtab = ovsdb_symbol_table_create();
2699     for (c = commands; c < &commands[n_commands]; c++) {
2700         ds_init(&c->output);
2701     }
2702     for (c = commands; c < &commands[n_commands]; c++) {
2703         struct vsctl_context ctx;
2704
2705         vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2706         (c->syntax->run)(&ctx);
2707         vsctl_context_done(&ctx, c);
2708
2709         if (ctx.try_again) {
2710             goto try_again;
2711         }
2712     }
2713
2714     status = ovsdb_idl_txn_commit_block(txn);
2715     if (wait_for_reload && status == TXN_SUCCESS) {
2716         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2717     }
2718     if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2719         for (c = commands; c < &commands[n_commands]; c++) {
2720             if (c->syntax->postprocess) {
2721                 struct vsctl_context ctx;
2722
2723                 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2724                 (c->syntax->postprocess)(&ctx);
2725                 vsctl_context_done(&ctx, c);
2726             }
2727         }
2728     }
2729     error = xstrdup(ovsdb_idl_txn_get_error(txn));
2730     ovsdb_idl_txn_destroy(txn);
2731     the_idl_txn = NULL;
2732
2733     unused = ovsdb_symbol_table_find_unused(symtab);
2734     if (unused) {
2735         vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2736                     "with \"-- --id=%s create ...\")", unused, unused);
2737     }
2738
2739     switch (status) {
2740     case TXN_INCOMPLETE:
2741         NOT_REACHED();
2742
2743     case TXN_ABORTED:
2744         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2745         vsctl_fatal("transaction aborted");
2746
2747     case TXN_UNCHANGED:
2748     case TXN_SUCCESS:
2749         break;
2750
2751     case TXN_TRY_AGAIN:
2752         goto try_again;
2753
2754     case TXN_ERROR:
2755         vsctl_fatal("transaction error: %s", error);
2756
2757     default:
2758         NOT_REACHED();
2759     }
2760     free(error);
2761
2762     ovsdb_symbol_table_destroy(symtab);
2763
2764     for (c = commands; c < &commands[n_commands]; c++) {
2765         struct ds *ds = &c->output;
2766         struct shash_node *node;
2767
2768         if (oneline) {
2769             size_t j;
2770
2771             ds_chomp(ds, '\n');
2772             for (j = 0; j < ds->length; j++) {
2773                 int c = ds->string[j];
2774                 switch (c) {
2775                 case '\n':
2776                     fputs("\\n", stdout);
2777                     break;
2778
2779                 case '\\':
2780                     fputs("\\\\", stdout);
2781                     break;
2782
2783                 default:
2784                     putchar(c);
2785                 }
2786             }
2787             putchar('\n');
2788         } else {
2789             fputs(ds_cstr(ds), stdout);
2790         }
2791         ds_destroy(&c->output);
2792
2793         SHASH_FOR_EACH (node, &c->options) {
2794             free(node->data);
2795         }
2796         shash_destroy(&c->options);
2797     }
2798     free(commands);
2799
2800     if (wait_for_reload && status != TXN_UNCHANGED) {
2801         for (;;) {
2802             const struct ovsrec_open_vswitch *ovs;
2803
2804             ovsdb_idl_run(idl);
2805             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2806                 if (ovs->cur_cfg >= next_cfg) {
2807                     goto done;
2808                 }
2809             }
2810             ovsdb_idl_wait(idl);
2811             poll_block();
2812         }
2813     done: ;
2814     }
2815     ovsdb_idl_destroy(idl);
2816
2817     exit(EXIT_SUCCESS);
2818
2819 try_again:
2820     /* Our transaction needs to be rerun, or a prerequisite was not met.  Free
2821      * resources and return so that the caller can try again. */
2822     ovsdb_idl_txn_abort(txn);
2823     ovsdb_idl_txn_destroy(txn);
2824     ovsdb_symbol_table_destroy(symtab);
2825     for (c = commands; c < &commands[n_commands]; c++) {
2826         ds_destroy(&c->output);
2827     }
2828     free(error);
2829 }
2830
2831 static const struct vsctl_command_syntax all_commands[] = {
2832     /* Open vSwitch commands. */
2833     {"init", 0, 0, cmd_init, NULL, ""},
2834
2835     /* Bridge commands. */
2836     {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2837     {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2838     {"list-br", 0, 0, cmd_list_br, NULL, ""},
2839     {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2840     {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2841     {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2842     {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2843     {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2844
2845     /* Port commands. */
2846     {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2847     {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2848     {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2849     {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2850     {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2851
2852     /* Interface commands. */
2853     {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2854     {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2855
2856     /* Controller commands. */
2857     {"get-controller", 1, 1, cmd_get_controller, NULL, ""},
2858     {"del-controller", 1, 1, cmd_del_controller, NULL, ""},
2859     {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2860     {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, ""},
2861     {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, ""},
2862     {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, ""},
2863
2864     /* SSL commands. */
2865     {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2866     {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2867     {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2868
2869     /* Switch commands. */
2870     {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2871
2872     /* Parameter commands. */
2873     {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2874     {"list", 1, INT_MAX, cmd_list, NULL, ""},
2875     {"set", 3, INT_MAX, cmd_set, NULL, ""},
2876     {"add", 4, INT_MAX, cmd_add, NULL, ""},
2877     {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2878     {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2879     {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2880     {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2881     {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2882
2883     {NULL, 0, 0, NULL, NULL, NULL},
2884 };
2885