d09cf7460b0f645f8cd94e50f644962d34ae7fb9
[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 shash bridges;
538     struct shash ports;
539     struct shash ifaces;
540 };
541
542 static char *
543 vsctl_context_to_string(const struct vsctl_context *ctx)
544 {
545     const struct shash_node *node;
546     struct svec words;
547     char *s;
548     int i;
549
550     svec_init(&words);
551     SHASH_FOR_EACH (node, &ctx->options) {
552         svec_add(&words, node->name);
553     }
554     for (i = 0; i < ctx->argc; i++) {
555         svec_add(&words, ctx->argv[i]);
556     }
557     svec_terminate(&words);
558
559     s = process_escape_args(words.names);
560
561     svec_destroy(&words);
562
563     return s;
564 }
565
566 static struct vsctl_bridge *
567 add_bridge(struct vsctl_info *b,
568            struct ovsrec_bridge *br_cfg, const char *name,
569            struct vsctl_bridge *parent, int vlan)
570 {
571     struct vsctl_bridge *br = xmalloc(sizeof *br);
572     br->br_cfg = br_cfg;
573     br->name = xstrdup(name);
574     br->parent = parent;
575     br->vlan = vlan;
576     if (parent) {
577         br->ctrl = parent->br_cfg->controller;
578         br->n_ctrl = parent->br_cfg->n_controller;
579         br->fail_mode = parent->br_cfg->fail_mode;
580     } else {
581         br->ctrl = br_cfg->controller;
582         br->n_ctrl = br_cfg->n_controller;
583         br->fail_mode = br_cfg->fail_mode;
584     }
585     shash_add(&b->bridges, br->name, br);
586     return br;
587 }
588
589 static bool
590 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
591 {
592     return (port_cfg->fake_bridge
593             && port_cfg->tag
594             && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
595 }
596
597 static struct vsctl_bridge *
598 find_vlan_bridge(struct vsctl_info *info,
599                  struct vsctl_bridge *parent, int vlan)
600 {
601     struct shash_node *node;
602
603     SHASH_FOR_EACH (node, &info->bridges) {
604         struct vsctl_bridge *br = node->data;
605         if (br->parent == parent && br->vlan == vlan) {
606             return br;
607         }
608     }
609
610     return NULL;
611 }
612
613 static void
614 free_info(struct vsctl_info *info)
615 {
616     struct shash_node *node;
617
618     SHASH_FOR_EACH (node, &info->bridges) {
619         struct vsctl_bridge *bridge = node->data;
620         free(bridge->name);
621         free(bridge);
622     }
623     shash_destroy(&info->bridges);
624
625     shash_destroy_free_data(&info->ports);
626     shash_destroy_free_data(&info->ifaces);
627 }
628
629 static void
630 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
631 {
632     struct shash bridges, ports;
633     size_t i;
634
635     shash_init(&info->bridges);
636     shash_init(&info->ports);
637     shash_init(&info->ifaces);
638
639     shash_init(&bridges);
640     shash_init(&ports);
641     for (i = 0; i < ovs->n_bridges; i++) {
642         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
643         struct vsctl_bridge *br;
644         size_t j;
645
646         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
647             VLOG_WARN("%s: database contains duplicate bridge name",
648                       br_cfg->name);
649             continue;
650         }
651         br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
652         if (!br) {
653             continue;
654         }
655
656         for (j = 0; j < br_cfg->n_ports; j++) {
657             struct ovsrec_port *port_cfg = br_cfg->ports[j];
658
659             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
660                 VLOG_WARN("%s: database contains duplicate port name",
661                           port_cfg->name);
662                 continue;
663             }
664
665             if (port_is_fake_bridge(port_cfg)
666                 && shash_add_once(&bridges, port_cfg->name, NULL)) {
667                 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
668             }
669         }
670     }
671     shash_destroy(&bridges);
672     shash_destroy(&ports);
673
674     shash_init(&bridges);
675     shash_init(&ports);
676     for (i = 0; i < ovs->n_bridges; i++) {
677         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
678         struct vsctl_bridge *br;
679         size_t j;
680
681         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
682             continue;
683         }
684         br = shash_find_data(&info->bridges, br_cfg->name);
685         for (j = 0; j < br_cfg->n_ports; j++) {
686             struct ovsrec_port *port_cfg = br_cfg->ports[j];
687             struct vsctl_port *port;
688             size_t k;
689
690             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
691                 continue;
692             }
693
694             if (port_is_fake_bridge(port_cfg)
695                 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
696                 continue;
697             }
698
699             port = xmalloc(sizeof *port);
700             port->port_cfg = port_cfg;
701             if (port_cfg->tag
702                 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
703                 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
704                 if (!port->bridge) {
705                     port->bridge = br;
706                 }
707             } else {
708                 port->bridge = br;
709             }
710             shash_add(&info->ports, port_cfg->name, port);
711
712             for (k = 0; k < port_cfg->n_interfaces; k++) {
713                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
714                 struct vsctl_iface *iface;
715
716                 if (shash_find(&info->ifaces, iface_cfg->name)) {
717                     VLOG_WARN("%s: database contains duplicate interface name",
718                               iface_cfg->name);
719                     continue;
720                 }
721
722                 iface = xmalloc(sizeof *iface);
723                 iface->iface_cfg = iface_cfg;
724                 iface->port = port;
725                 shash_add(&info->ifaces, iface_cfg->name, iface);
726             }
727         }
728     }
729     shash_destroy(&bridges);
730     shash_destroy(&ports);
731 }
732
733 static void
734 check_conflicts(struct vsctl_info *info, const char *name,
735                 char *msg)
736 {
737     struct vsctl_iface *iface;
738     struct vsctl_port *port;
739
740     if (shash_find(&info->bridges, name)) {
741         vsctl_fatal("%s because a bridge named %s already exists",
742                     msg, name);
743     }
744
745     port = shash_find_data(&info->ports, name);
746     if (port) {
747         vsctl_fatal("%s because a port named %s already exists on "
748                     "bridge %s", msg, name, port->bridge->name);
749     }
750
751     iface = shash_find_data(&info->ifaces, name);
752     if (iface) {
753         vsctl_fatal("%s because an interface named %s already exists "
754                     "on bridge %s", msg, name, iface->port->bridge->name);
755     }
756
757     free(msg);
758 }
759
760 static struct vsctl_bridge *
761 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
762 {
763     struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
764     if (must_exist && !br) {
765         vsctl_fatal("no bridge named %s", name);
766     }
767     return br;
768 }
769
770 static struct vsctl_bridge *
771 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
772 {
773     struct vsctl_bridge *br = find_bridge(info, name, must_exist);
774     if (br && br->parent) {
775         vsctl_fatal("%s is a fake bridge", name);
776     }
777     return br;
778 }
779
780 static struct vsctl_port *
781 find_port(struct vsctl_info *info, const char *name, bool must_exist)
782 {
783     struct vsctl_port *port = shash_find_data(&info->ports, name);
784     if (port && !strcmp(name, port->bridge->name)) {
785         port = NULL;
786     }
787     if (must_exist && !port) {
788         vsctl_fatal("no port named %s", name);
789     }
790     return port;
791 }
792
793 static struct vsctl_iface *
794 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
795 {
796     struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
797     if (iface && !strcmp(name, iface->port->bridge->name)) {
798         iface = NULL;
799     }
800     if (must_exist && !iface) {
801         vsctl_fatal("no interface named %s", name);
802     }
803     return iface;
804 }
805
806 static void
807 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
808 {
809     struct ovsrec_port **ports;
810     size_t i;
811
812     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
813     for (i = 0; i < br->n_ports; i++) {
814         ports[i] = br->ports[i];
815     }
816     ports[br->n_ports] = port;
817     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
818     free(ports);
819 }
820
821 static void
822 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
823 {
824     struct ovsrec_port **ports;
825     size_t i, n;
826
827     ports = xmalloc(sizeof *br->ports * br->n_ports);
828     for (i = n = 0; i < br->n_ports; i++) {
829         if (br->ports[i] != port) {
830             ports[n++] = br->ports[i];
831         }
832     }
833     ovsrec_bridge_set_ports(br, ports, n);
834     free(ports);
835 }
836
837 static void
838 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
839                   struct ovsrec_bridge *bridge)
840 {
841     struct ovsrec_bridge **bridges;
842     size_t i;
843
844     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
845     for (i = 0; i < ovs->n_bridges; i++) {
846         bridges[i] = ovs->bridges[i];
847     }
848     bridges[ovs->n_bridges] = bridge;
849     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
850     free(bridges);
851 }
852
853 static void
854 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
855                   struct ovsrec_bridge *bridge)
856 {
857     struct ovsrec_bridge **bridges;
858     size_t i, n;
859
860     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
861     for (i = n = 0; i < ovs->n_bridges; i++) {
862         if (ovs->bridges[i] != bridge) {
863             bridges[n++] = ovs->bridges[i];
864         }
865     }
866     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
867     free(bridges);
868 }
869
870 static void
871 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
872 {
873 }
874
875 static void
876 cmd_emer_reset(struct vsctl_context *ctx)
877 {
878     const struct ovsdb_idl *idl = ctx->idl;
879     const struct ovsrec_bridge *br;
880     const struct ovsrec_port *port;
881     const struct ovsrec_interface *iface;
882     const struct ovsrec_mirror *mirror, *next_mirror;
883     const struct ovsrec_controller *ctrl, *next_ctrl;
884     const struct ovsrec_netflow *nf, *next_nf;
885     const struct ovsrec_ssl *ssl, *next_ssl;
886     const struct ovsrec_sflow *sflow, *next_sflow;
887
888
889     /* Reset the Open_vSwitch table. */
890     ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
891     ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
892
893     OVSREC_BRIDGE_FOR_EACH (br, idl) {
894         int i;
895         char *hw_key = "hwaddr";
896         char *hw_val = NULL;
897
898         ovsrec_bridge_set_controller(br, NULL, 0);
899         ovsrec_bridge_set_mirrors(br, NULL, 0);
900         ovsrec_bridge_set_netflow(br, NULL);
901         ovsrec_bridge_set_sflow(br, NULL);
902         ovsrec_bridge_set_flood_vlans(br, NULL, 0);
903
904         /* We only want to save the "hwaddr" key from other_config. */
905         for (i=0; i < br->n_other_config; i++) {
906             if (!strcmp(br->key_other_config[i], hw_key)) {
907                 hw_val = br->value_other_config[i];
908                 break;
909             }
910         }
911         if (hw_val) {
912             char *val = xstrdup(hw_val);
913             ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
914             free(val);
915         } else {
916             ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
917         }
918     }
919
920     OVSREC_PORT_FOR_EACH (port, idl) {
921         ovsrec_port_set_other_config(port, NULL, NULL, 0);
922     }
923
924     OVSREC_INTERFACE_FOR_EACH (iface, idl) {
925         /* xxx What do we do about gre/patch devices created by mgr? */
926
927         ovsrec_interface_set_ingress_policing_rate(iface, 0);
928         ovsrec_interface_set_ingress_policing_burst(iface, 0);
929     }
930
931     OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
932         ovsrec_mirror_delete(mirror);
933     }
934
935     OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
936         ovsrec_controller_delete(ctrl);
937     }
938
939     OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
940         ovsrec_netflow_delete(nf);
941     }
942
943     OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
944         ovsrec_ssl_delete(ssl);
945     }
946
947     OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
948         ovsrec_sflow_delete(sflow);
949     }
950 }
951
952 static void
953 cmd_add_br(struct vsctl_context *ctx)
954 {
955     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
956     const char *br_name, *parent_name;
957     struct vsctl_info info;
958     int vlan;
959
960     br_name = ctx->argv[1];
961     if (ctx->argc == 2) {
962         parent_name = NULL;
963         vlan = 0;
964     } else if (ctx->argc == 4) {
965         parent_name = ctx->argv[2];
966         vlan = atoi(ctx->argv[3]);
967         if (vlan < 1 || vlan > 4095) {
968             vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
969         }
970     } else {
971         vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
972                     ctx->argv[0]);
973     }
974
975     get_info(ctx->ovs, &info);
976     if (may_exist) {
977         struct vsctl_bridge *br;
978
979         br = find_bridge(&info, br_name, false);
980         if (br) {
981             if (!parent_name) {
982                 if (br->parent) {
983                     vsctl_fatal("\"--may-exist add-br %s\" but %s is "
984                                 "a VLAN bridge for VLAN %d",
985                                 br_name, br_name, br->vlan);
986                 }
987             } else {
988                 if (!br->parent) {
989                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
990                                 "is not a VLAN bridge",
991                                 br_name, parent_name, vlan, br_name);
992                 } else if (strcmp(br->parent->name, parent_name)) {
993                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
994                                 "has the wrong parent %s",
995                                 br_name, parent_name, vlan,
996                                 br_name, br->parent->name);
997                 } else if (br->vlan != vlan) {
998                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
999                                 "is a VLAN bridge for the wrong VLAN %d",
1000                                 br_name, parent_name, vlan, br_name, br->vlan);
1001                 }
1002             }
1003             return;
1004         }
1005     }
1006     check_conflicts(&info, br_name,
1007                     xasprintf("cannot create a bridge named %s", br_name));
1008
1009     if (!parent_name) {
1010         struct ovsrec_port *port;
1011         struct ovsrec_interface *iface;
1012         struct ovsrec_bridge *br;
1013
1014         iface = ovsrec_interface_insert(ctx->txn);
1015         ovsrec_interface_set_name(iface, br_name);
1016
1017         port = ovsrec_port_insert(ctx->txn);
1018         ovsrec_port_set_name(port, br_name);
1019         ovsrec_port_set_interfaces(port, &iface, 1);
1020
1021         br = ovsrec_bridge_insert(ctx->txn);
1022         ovsrec_bridge_set_name(br, br_name);
1023         ovsrec_bridge_set_ports(br, &port, 1);
1024
1025         ovs_insert_bridge(ctx->ovs, br);
1026     } else {
1027         struct vsctl_bridge *parent;
1028         struct ovsrec_port *port;
1029         struct ovsrec_interface *iface;
1030         struct ovsrec_bridge *br;
1031         int64_t tag = vlan;
1032
1033         parent = find_bridge(&info, parent_name, false);
1034         if (parent && parent->vlan) {
1035             vsctl_fatal("cannot create bridge with fake bridge as parent");
1036         }
1037         if (!parent) {
1038             vsctl_fatal("parent bridge %s does not exist", parent_name);
1039         }
1040         br = parent->br_cfg;
1041
1042         iface = ovsrec_interface_insert(ctx->txn);
1043         ovsrec_interface_set_name(iface, br_name);
1044         ovsrec_interface_set_type(iface, "internal");
1045
1046         port = ovsrec_port_insert(ctx->txn);
1047         ovsrec_port_set_name(port, br_name);
1048         ovsrec_port_set_interfaces(port, &iface, 1);
1049         ovsrec_port_set_fake_bridge(port, true);
1050         ovsrec_port_set_tag(port, &tag, 1);
1051
1052         bridge_insert_port(br, port);
1053     }
1054
1055     free_info(&info);
1056 }
1057
1058 static void
1059 del_port(struct vsctl_info *info, struct vsctl_port *port)
1060 {
1061     struct shash_node *node;
1062
1063     SHASH_FOR_EACH (node, &info->ifaces) {
1064         struct vsctl_iface *iface = node->data;
1065         if (iface->port == port) {
1066             ovsrec_interface_delete(iface->iface_cfg);
1067         }
1068     }
1069     ovsrec_port_delete(port->port_cfg);
1070
1071     bridge_delete_port((port->bridge->parent
1072                         ? port->bridge->parent->br_cfg
1073                         : port->bridge->br_cfg), port->port_cfg);
1074 }
1075
1076 static void
1077 cmd_del_br(struct vsctl_context *ctx)
1078 {
1079     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1080     struct vsctl_bridge *bridge;
1081     struct vsctl_info info;
1082
1083     get_info(ctx->ovs, &info);
1084     bridge = find_bridge(&info, ctx->argv[1], must_exist);
1085     if (bridge) {
1086         struct shash_node *node;
1087
1088         SHASH_FOR_EACH (node, &info.ports) {
1089             struct vsctl_port *port = node->data;
1090             if (port->bridge == bridge || port->bridge->parent == bridge
1091                 || !strcmp(port->port_cfg->name, bridge->name)) {
1092                 del_port(&info, port);
1093             }
1094         }
1095         if (bridge->br_cfg) {
1096             ovsrec_bridge_delete(bridge->br_cfg);
1097             ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1098         }
1099     }
1100     free_info(&info);
1101 }
1102
1103 static void
1104 output_sorted(struct svec *svec, struct ds *output)
1105 {
1106     const char *name;
1107     size_t i;
1108
1109     svec_sort(svec);
1110     SVEC_FOR_EACH (i, name, svec) {
1111         ds_put_format(output, "%s\n", name);
1112     }
1113 }
1114
1115 static void
1116 cmd_list_br(struct vsctl_context *ctx)
1117 {
1118     struct shash_node *node;
1119     struct vsctl_info info;
1120     struct svec bridges;
1121
1122     get_info(ctx->ovs, &info);
1123
1124     svec_init(&bridges);
1125     SHASH_FOR_EACH (node, &info.bridges) {
1126         struct vsctl_bridge *br = node->data;
1127         svec_add(&bridges, br->name);
1128     }
1129     output_sorted(&bridges, &ctx->output);
1130     svec_destroy(&bridges);
1131
1132     free_info(&info);
1133 }
1134
1135 static void
1136 cmd_br_exists(struct vsctl_context *ctx)
1137 {
1138     struct vsctl_info info;
1139
1140     get_info(ctx->ovs, &info);
1141     if (!find_bridge(&info, ctx->argv[1], false)) {
1142         vsctl_exit(2);
1143     }
1144     free_info(&info);
1145 }
1146
1147 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1148  * equals 'a', false otherwise. */
1149 static bool
1150 key_matches(const char *a,
1151             const char *b_prefix, size_t b_prefix_len, const char *b)
1152 {
1153     return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1154 }
1155
1156 static void
1157 set_external_id(char **old_keys, char **old_values, size_t old_n,
1158                 char *key, char *value,
1159                 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1160 {
1161     char **new_keys;
1162     char **new_values;
1163     size_t new_n;
1164     size_t i;
1165
1166     new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1167     new_values = xmalloc(sizeof *new_values * (old_n + 1));
1168     new_n = 0;
1169     for (i = 0; i < old_n; i++) {
1170         if (strcmp(key, old_keys[i])) {
1171             new_keys[new_n] = old_keys[i];
1172             new_values[new_n] = old_values[i];
1173             new_n++;
1174         }
1175     }
1176     if (value) {
1177         new_keys[new_n] = key;
1178         new_values[new_n] = value;
1179         new_n++;
1180     }
1181     *new_keysp = new_keys;
1182     *new_valuesp = new_values;
1183     *new_np = new_n;
1184 }
1185
1186 static void
1187 cmd_br_set_external_id(struct vsctl_context *ctx)
1188 {
1189     struct vsctl_info info;
1190     struct vsctl_bridge *bridge;
1191     char **keys, **values;
1192     size_t n;
1193
1194     get_info(ctx->ovs, &info);
1195     bridge = find_bridge(&info, ctx->argv[1], true);
1196     if (bridge->br_cfg) {
1197         set_external_id(bridge->br_cfg->key_external_ids,
1198                         bridge->br_cfg->value_external_ids,
1199                         bridge->br_cfg->n_external_ids,
1200                         ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1201                         &keys, &values, &n);
1202         ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1203     } else {
1204         char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1205         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1206         set_external_id(port->port_cfg->key_external_ids,
1207                         port->port_cfg->value_external_ids,
1208                         port->port_cfg->n_external_ids,
1209                         key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1210                         &keys, &values, &n);
1211         ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1212         free(key);
1213     }
1214     free(keys);
1215     free(values);
1216
1217     free_info(&info);
1218 }
1219
1220 static void
1221 get_external_id(char **keys, char **values, size_t n,
1222                 const char *prefix, const char *key,
1223                 struct ds *output)
1224 {
1225     size_t prefix_len = strlen(prefix);
1226     struct svec svec;
1227     size_t i;
1228
1229     svec_init(&svec);
1230     for (i = 0; i < n; i++) {
1231         if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1232             svec_add_nocopy(&svec, xasprintf("%s=%s",
1233                                              keys[i] + prefix_len, values[i]));
1234         } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1235             svec_add(&svec, values[i]);
1236             break;
1237         }
1238     }
1239     output_sorted(&svec, output);
1240     svec_destroy(&svec);
1241 }
1242
1243 static void
1244 cmd_br_get_external_id(struct vsctl_context *ctx)
1245 {
1246     struct vsctl_info info;
1247     struct vsctl_bridge *bridge;
1248
1249     get_info(ctx->ovs, &info);
1250     bridge = find_bridge(&info, ctx->argv[1], true);
1251     if (bridge->br_cfg) {
1252         get_external_id(bridge->br_cfg->key_external_ids,
1253                         bridge->br_cfg->value_external_ids,
1254                         bridge->br_cfg->n_external_ids,
1255                         "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1256                         &ctx->output);
1257     } else {
1258         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1259         get_external_id(port->port_cfg->key_external_ids,
1260                         port->port_cfg->value_external_ids,
1261                         port->port_cfg->n_external_ids,
1262                         "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1263     }
1264     free_info(&info);
1265 }
1266
1267
1268 static void
1269 cmd_list_ports(struct vsctl_context *ctx)
1270 {
1271     struct vsctl_bridge *br;
1272     struct shash_node *node;
1273     struct vsctl_info info;
1274     struct svec ports;
1275
1276     get_info(ctx->ovs, &info);
1277     br = find_bridge(&info, ctx->argv[1], true);
1278
1279     svec_init(&ports);
1280     SHASH_FOR_EACH (node, &info.ports) {
1281         struct vsctl_port *port = node->data;
1282
1283         if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1284             svec_add(&ports, port->port_cfg->name);
1285         }
1286     }
1287     output_sorted(&ports, &ctx->output);
1288     svec_destroy(&ports);
1289
1290     free_info(&info);
1291 }
1292
1293 static void
1294 add_port(struct vsctl_context *ctx,
1295          const char *br_name, const char *port_name,
1296          bool may_exist, bool fake_iface,
1297          char *iface_names[], int n_ifaces,
1298          char *settings[], int n_settings)
1299 {
1300     struct vsctl_info info;
1301     struct vsctl_bridge *bridge;
1302     struct ovsrec_interface **ifaces;
1303     struct ovsrec_port *port;
1304     size_t i;
1305
1306     get_info(ctx->ovs, &info);
1307     if (may_exist) {
1308         struct vsctl_port *vsctl_port;
1309
1310         vsctl_port = find_port(&info, port_name, false);
1311         if (vsctl_port) {
1312             struct svec want_names, have_names;
1313
1314             svec_init(&want_names);
1315             for (i = 0; i < n_ifaces; i++) {
1316                 svec_add(&want_names, iface_names[i]);
1317             }
1318             svec_sort(&want_names);
1319
1320             svec_init(&have_names);
1321             for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1322                 svec_add(&have_names,
1323                          vsctl_port->port_cfg->interfaces[i]->name);
1324             }
1325             svec_sort(&have_names);
1326
1327             if (strcmp(vsctl_port->bridge->name, br_name)) {
1328                 char *command = vsctl_context_to_string(ctx);
1329                 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1330                             command, port_name, vsctl_port->bridge->name);
1331             }
1332
1333             if (!svec_equal(&want_names, &have_names)) {
1334                 char *have_names_string = svec_join(&have_names, ", ", "");
1335                 char *command = vsctl_context_to_string(ctx);
1336
1337                 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1338                             command, port_name, have_names_string);
1339             }
1340
1341             svec_destroy(&want_names);
1342             svec_destroy(&have_names);
1343
1344             return;
1345         }
1346     }
1347     check_conflicts(&info, port_name,
1348                     xasprintf("cannot create a port named %s", port_name));
1349     for (i = 0; i < n_ifaces; i++) {
1350         check_conflicts(&info, iface_names[i],
1351                         xasprintf("cannot create an interface named %s",
1352                                   iface_names[i]));
1353     }
1354     bridge = find_bridge(&info, br_name, true);
1355
1356     ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1357     for (i = 0; i < n_ifaces; i++) {
1358         ifaces[i] = ovsrec_interface_insert(ctx->txn);
1359         ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1360     }
1361
1362     port = ovsrec_port_insert(ctx->txn);
1363     ovsrec_port_set_name(port, port_name);
1364     ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1365     ovsrec_port_set_bond_fake_iface(port, fake_iface);
1366     free(ifaces);
1367
1368     if (bridge->vlan) {
1369         int64_t tag = bridge->vlan;
1370         ovsrec_port_set_tag(port, &tag, 1);
1371     }
1372
1373     for (i = 0; i < n_settings; i++) {
1374         set_column(get_table("Port"), &port->header_, settings[i],
1375                    ctx->symtab);
1376     }
1377
1378     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1379                         : bridge->br_cfg), port);
1380
1381     free_info(&info);
1382 }
1383
1384 static void
1385 cmd_add_port(struct vsctl_context *ctx)
1386 {
1387     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1388
1389     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1390              &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1391 }
1392
1393 static void
1394 cmd_add_bond(struct vsctl_context *ctx)
1395 {
1396     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1397     bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1398     int n_ifaces;
1399     int i;
1400
1401     n_ifaces = ctx->argc - 3;
1402     for (i = 3; i < ctx->argc; i++) {
1403         if (strchr(ctx->argv[i], '=')) {
1404             n_ifaces = i - 3;
1405             break;
1406         }
1407     }
1408     if (n_ifaces < 2) {
1409         vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1410                     "%d were specified", n_ifaces);
1411     }
1412
1413     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1414              &ctx->argv[3], n_ifaces,
1415              &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1416 }
1417
1418 static void
1419 cmd_del_port(struct vsctl_context *ctx)
1420 {
1421     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1422     bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1423     struct vsctl_port *port;
1424     struct vsctl_info info;
1425
1426     get_info(ctx->ovs, &info);
1427     if (!with_iface) {
1428         port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1429     } else {
1430         const char *target = ctx->argv[ctx->argc - 1];
1431         struct vsctl_iface *iface;
1432
1433         port = find_port(&info, target, false);
1434         if (!port) {
1435             iface = find_iface(&info, target, false);
1436             if (iface) {
1437                 port = iface->port;
1438             }
1439         }
1440         if (must_exist && !port) {
1441             vsctl_fatal("no port or interface named %s", target);
1442         }
1443     }
1444
1445     if (port) {
1446         if (ctx->argc == 3) {
1447             struct vsctl_bridge *bridge;
1448
1449             bridge = find_bridge(&info, ctx->argv[1], true);
1450             if (port->bridge != bridge) {
1451                 if (port->bridge->parent == bridge) {
1452                     vsctl_fatal("bridge %s does not have a port %s (although "
1453                                 "its parent bridge %s does)",
1454                                 ctx->argv[1], ctx->argv[2],
1455                                 bridge->parent->name);
1456                 } else {
1457                     vsctl_fatal("bridge %s does not have a port %s",
1458                                 ctx->argv[1], ctx->argv[2]);
1459                 }
1460             }
1461         }
1462
1463         del_port(&info, port);
1464     }
1465
1466     free_info(&info);
1467 }
1468
1469 static void
1470 cmd_port_to_br(struct vsctl_context *ctx)
1471 {
1472     struct vsctl_port *port;
1473     struct vsctl_info info;
1474
1475     get_info(ctx->ovs, &info);
1476     port = find_port(&info, ctx->argv[1], true);
1477     ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1478     free_info(&info);
1479 }
1480
1481 static void
1482 cmd_br_to_vlan(struct vsctl_context *ctx)
1483 {
1484     struct vsctl_bridge *bridge;
1485     struct vsctl_info info;
1486
1487     get_info(ctx->ovs, &info);
1488     bridge = find_bridge(&info, ctx->argv[1], true);
1489     ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1490     free_info(&info);
1491 }
1492
1493 static void
1494 cmd_br_to_parent(struct vsctl_context *ctx)
1495 {
1496     struct vsctl_bridge *bridge;
1497     struct vsctl_info info;
1498
1499     get_info(ctx->ovs, &info);
1500     bridge = find_bridge(&info, ctx->argv[1], true);
1501     if (bridge->parent) {
1502         bridge = bridge->parent;
1503     }
1504     ds_put_format(&ctx->output, "%s\n", bridge->name);
1505     free_info(&info);
1506 }
1507
1508 static void
1509 cmd_list_ifaces(struct vsctl_context *ctx)
1510 {
1511     struct vsctl_bridge *br;
1512     struct shash_node *node;
1513     struct vsctl_info info;
1514     struct svec ifaces;
1515
1516     get_info(ctx->ovs, &info);
1517     br = find_bridge(&info, ctx->argv[1], true);
1518
1519     svec_init(&ifaces);
1520     SHASH_FOR_EACH (node, &info.ifaces) {
1521         struct vsctl_iface *iface = node->data;
1522
1523         if (strcmp(iface->iface_cfg->name, br->name)
1524             && br == iface->port->bridge) {
1525             svec_add(&ifaces, iface->iface_cfg->name);
1526         }
1527     }
1528     output_sorted(&ifaces, &ctx->output);
1529     svec_destroy(&ifaces);
1530
1531     free_info(&info);
1532 }
1533
1534 static void
1535 cmd_iface_to_br(struct vsctl_context *ctx)
1536 {
1537     struct vsctl_iface *iface;
1538     struct vsctl_info info;
1539
1540     get_info(ctx->ovs, &info);
1541     iface = find_iface(&info, ctx->argv[1], true);
1542     ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1543     free_info(&info);
1544 }
1545
1546 static void
1547 cmd_get_controller(struct vsctl_context *ctx)
1548 {
1549     struct vsctl_info info;
1550     struct vsctl_bridge *br;
1551     struct svec targets;
1552     size_t i;
1553
1554     get_info(ctx->ovs, &info);
1555     br = find_bridge(&info, ctx->argv[1], true);
1556
1557     /* Print the targets in sorted order for reproducibility. */
1558     svec_init(&targets);
1559     for (i = 0; i < br->n_ctrl; i++) {
1560         svec_add(&targets, br->ctrl[i]->target);
1561     }
1562
1563     svec_sort(&targets);
1564     for (i = 0; i < targets.n; i++) {
1565         ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1566     }
1567     svec_destroy(&targets);
1568
1569     free_info(&info);
1570 }
1571
1572 static void
1573 delete_controllers(struct ovsrec_controller **controllers,
1574                    size_t n_controllers)
1575 {
1576     size_t i;
1577
1578     for (i = 0; i < n_controllers; i++) {
1579         ovsrec_controller_delete(controllers[i]);
1580     }
1581 }
1582
1583 static void
1584 cmd_del_controller(struct vsctl_context *ctx)
1585 {
1586     struct vsctl_info info;
1587     struct vsctl_bridge *br;
1588
1589     get_info(ctx->ovs, &info);
1590     br = find_real_bridge(&info, ctx->argv[1], true);
1591
1592     if (br->ctrl) {
1593         delete_controllers(br->ctrl, br->n_ctrl);
1594         ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1595     }
1596
1597     free_info(&info);
1598 }
1599
1600 static struct ovsrec_controller **
1601 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1602 {
1603     struct ovsrec_controller **controllers;
1604     size_t i;
1605
1606     controllers = xmalloc(n * sizeof *controllers);
1607     for (i = 0; i < n; i++) {
1608         controllers[i] = ovsrec_controller_insert(txn);
1609         ovsrec_controller_set_target(controllers[i], targets[i]);
1610     }
1611
1612     return controllers;
1613 }
1614
1615 static void
1616 cmd_set_controller(struct vsctl_context *ctx)
1617 {
1618     struct vsctl_info info;
1619     struct vsctl_bridge *br;
1620     struct ovsrec_controller **controllers;
1621     size_t n;
1622
1623     get_info(ctx->ovs, &info);
1624     br = find_real_bridge(&info, ctx->argv[1], true);
1625
1626     delete_controllers(br->ctrl, br->n_ctrl);
1627
1628     n = ctx->argc - 2;
1629     controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1630     ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1631     free(controllers);
1632
1633     free_info(&info);
1634 }
1635
1636 static void
1637 cmd_get_fail_mode(struct vsctl_context *ctx)
1638 {
1639     struct vsctl_info info;
1640     struct vsctl_bridge *br;
1641
1642     get_info(ctx->ovs, &info);
1643     br = find_bridge(&info, ctx->argv[1], true);
1644
1645     if (br->fail_mode && strlen(br->fail_mode)) {
1646         ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1647     }
1648
1649     free_info(&info);
1650 }
1651
1652 static void
1653 cmd_del_fail_mode(struct vsctl_context *ctx)
1654 {
1655     struct vsctl_info info;
1656     struct vsctl_bridge *br;
1657
1658     get_info(ctx->ovs, &info);
1659     br = find_real_bridge(&info, ctx->argv[1], true);
1660
1661     ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1662
1663     free_info(&info);
1664 }
1665
1666 static void
1667 cmd_set_fail_mode(struct vsctl_context *ctx)
1668 {
1669     struct vsctl_info info;
1670     struct vsctl_bridge *br;
1671     const char *fail_mode = ctx->argv[2];
1672
1673     get_info(ctx->ovs, &info);
1674     br = find_real_bridge(&info, ctx->argv[1], true);
1675
1676     if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1677         vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1678     }
1679
1680     ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1681
1682     free_info(&info);
1683 }
1684
1685 static void
1686 cmd_get_ssl(struct vsctl_context *ctx)
1687 {
1688     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1689
1690     if (ssl) {
1691         ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1692         ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1693         ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1694         ds_put_format(&ctx->output, "Bootstrap: %s\n",
1695                 ssl->bootstrap_ca_cert ? "true" : "false");
1696     }
1697 }
1698
1699 static void
1700 cmd_del_ssl(struct vsctl_context *ctx)
1701 {
1702     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1703
1704     if (ssl) {
1705         ovsrec_ssl_delete(ssl);
1706         ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1707     }
1708 }
1709
1710 static void
1711 cmd_set_ssl(struct vsctl_context *ctx)
1712 {
1713     bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1714     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1715
1716     if (ssl) {
1717         ovsrec_ssl_delete(ssl);
1718     }
1719     ssl = ovsrec_ssl_insert(ctx->txn);
1720
1721     ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1722     ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1723     ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1724
1725     ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1726
1727     ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1728 }
1729 \f
1730 /* Parameter commands. */
1731
1732 struct vsctl_row_id {
1733     const struct ovsdb_idl_table_class *table;
1734     const struct ovsdb_idl_column *name_column;
1735     const struct ovsdb_idl_column *uuid_column;
1736 };
1737
1738 struct vsctl_table_class {
1739     struct ovsdb_idl_table_class *class;
1740     struct vsctl_row_id row_ids[2];
1741 };
1742
1743 static const struct vsctl_table_class tables[] = {
1744     {&ovsrec_table_bridge,
1745      {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1746       {NULL, NULL, NULL}}},
1747
1748     {&ovsrec_table_controller,
1749      {{&ovsrec_table_bridge,
1750        &ovsrec_bridge_col_name,
1751        &ovsrec_bridge_col_controller}}},
1752
1753     {&ovsrec_table_interface,
1754      {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1755       {NULL, NULL, NULL}}},
1756
1757     {&ovsrec_table_mirror,
1758      {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1759       {NULL, NULL, NULL}}},
1760
1761     {&ovsrec_table_netflow,
1762      {{&ovsrec_table_bridge,
1763        &ovsrec_bridge_col_name,
1764        &ovsrec_bridge_col_netflow},
1765       {NULL, NULL, NULL}}},
1766
1767     {&ovsrec_table_open_vswitch,
1768      {{&ovsrec_table_open_vswitch, NULL, NULL},
1769       {NULL, NULL, NULL}}},
1770
1771     {&ovsrec_table_port,
1772      {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1773       {NULL, NULL, NULL}}},
1774
1775     {&ovsrec_table_qos,
1776      {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1777       {NULL, NULL, NULL}}},
1778
1779     {&ovsrec_table_queue,
1780      {{NULL, NULL, NULL},
1781       {NULL, NULL, NULL}}},
1782
1783     {&ovsrec_table_ssl,
1784      {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1785
1786     {&ovsrec_table_sflow,
1787      {{&ovsrec_table_bridge,
1788        &ovsrec_bridge_col_name,
1789        &ovsrec_bridge_col_sflow},
1790       {NULL, NULL, NULL}}},
1791
1792     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1793 };
1794
1795 static void
1796 die_if_error(char *error)
1797 {
1798     if (error) {
1799         vsctl_fatal("%s", error);
1800     }
1801 }
1802
1803 static int
1804 to_lower_and_underscores(unsigned c)
1805 {
1806     return c == '-' ? '_' : tolower(c);
1807 }
1808
1809 static unsigned int
1810 score_partial_match(const char *name, const char *s)
1811 {
1812     int score;
1813
1814     if (!strcmp(name, s)) {
1815         return UINT_MAX;
1816     }
1817     for (score = 0; ; score++, name++, s++) {
1818         if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1819             break;
1820         } else if (*name == '\0') {
1821             return UINT_MAX - 1;
1822         }
1823     }
1824     return *s == '\0' ? score : 0;
1825 }
1826
1827 static const struct vsctl_table_class *
1828 get_table(const char *table_name)
1829 {
1830     const struct vsctl_table_class *table;
1831     const struct vsctl_table_class *best_match = NULL;
1832     unsigned int best_score = 0;
1833
1834     for (table = tables; table->class; table++) {
1835         unsigned int score = score_partial_match(table->class->name,
1836                                                  table_name);
1837         if (score > best_score) {
1838             best_match = table;
1839             best_score = score;
1840         } else if (score == best_score) {
1841             best_match = NULL;
1842         }
1843     }
1844     if (best_match) {
1845         return best_match;
1846     } else if (best_score) {
1847         vsctl_fatal("multiple table names match \"%s\"", table_name);
1848     } else {
1849         vsctl_fatal("unknown table \"%s\"", table_name);
1850     }
1851 }
1852
1853 static const struct ovsdb_idl_row *
1854 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1855               const struct vsctl_row_id *id, const char *record_id)
1856 {
1857     const struct ovsdb_idl_row *referrer, *final;
1858
1859     if (!id->table) {
1860         return NULL;
1861     }
1862
1863     if (!id->name_column) {
1864         if (strcmp(record_id, ".")) {
1865             return NULL;
1866         }
1867         referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1868         if (!referrer || ovsdb_idl_next_row(referrer)) {
1869             return NULL;
1870         }
1871     } else {
1872         const struct ovsdb_idl_row *row;
1873
1874         referrer = NULL;
1875         for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1876              row != NULL;
1877              row = ovsdb_idl_next_row(row))
1878         {
1879             const struct ovsdb_datum *name;
1880
1881             name = ovsdb_idl_get(row, id->name_column,
1882                                  OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1883             if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1884                 if (referrer) {
1885                     vsctl_fatal("multiple rows in %s match \"%s\"",
1886                                 table->class->name, record_id);
1887                 }
1888                 referrer = row;
1889             }
1890         }
1891     }
1892     if (!referrer) {
1893         return NULL;
1894     }
1895
1896     final = NULL;
1897     if (id->uuid_column) {
1898         const struct ovsdb_datum *uuid;
1899
1900         uuid = ovsdb_idl_get(referrer, id->uuid_column,
1901                              OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1902         if (uuid->n == 1) {
1903             final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1904                                                &uuid->keys[0].uuid);
1905         }
1906     } else {
1907         final = referrer;
1908     }
1909
1910     return final;
1911 }
1912
1913 static const struct ovsdb_idl_row *
1914 get_row (struct vsctl_context *ctx,
1915          const struct vsctl_table_class *table, const char *record_id)
1916 {
1917     const struct ovsdb_idl_row *row;
1918     struct uuid uuid;
1919
1920     if (uuid_from_string(&uuid, record_id)) {
1921         row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1922     } else {
1923         int i;
1924
1925         for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1926             row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1927             if (row) {
1928                 break;
1929             }
1930         }
1931     }
1932     return row;
1933 }
1934
1935 static const struct ovsdb_idl_row *
1936 must_get_row(struct vsctl_context *ctx,
1937              const struct vsctl_table_class *table, const char *record_id)
1938 {
1939     const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1940     if (!row) {
1941         vsctl_fatal("no row \"%s\" in table %s",
1942                     record_id, table->class->name);
1943     }
1944     return row;
1945 }
1946
1947 static char *
1948 get_column(const struct vsctl_table_class *table, const char *column_name,
1949            const struct ovsdb_idl_column **columnp)
1950 {
1951     const struct ovsdb_idl_column *best_match = NULL;
1952     unsigned int best_score = 0;
1953     size_t i;
1954
1955     for (i = 0; i < table->class->n_columns; i++) {
1956         const struct ovsdb_idl_column *column = &table->class->columns[i];
1957         unsigned int score = score_partial_match(column->name, column_name);
1958         if (score > best_score) {
1959             best_match = column;
1960             best_score = score;
1961         } else if (score == best_score) {
1962             best_match = NULL;
1963         }
1964     }
1965
1966     *columnp = best_match;
1967     if (best_match) {
1968         return NULL;
1969     } else if (best_score) {
1970         return xasprintf("%s contains more than one column whose name "
1971                          "matches \"%s\"", table->class->name, column_name);
1972     } else {
1973         return xasprintf("%s does not contain a column whose name matches "
1974                          "\"%s\"", table->class->name, column_name);
1975     }
1976 }
1977
1978 static struct uuid *
1979 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
1980 {
1981     struct ovsdb_symbol *symbol;
1982
1983     if (id[0] != '@') {
1984         vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
1985     }
1986
1987     if (newp) {
1988         *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
1989     }
1990
1991     symbol = ovsdb_symbol_table_insert(symtab, id);
1992     if (symbol->used) {
1993         vsctl_fatal("row id \"%s\" may only be specified on one --id option",
1994                     id);
1995     }
1996     symbol->used = true;
1997     return &symbol->uuid;
1998 }
1999
2000 static char *
2001 missing_operator_error(const char *arg, const char **allowed_operators,
2002                        size_t n_allowed)
2003 {
2004     struct ds s;
2005
2006     ds_init(&s);
2007     ds_put_format(&s, "%s: argument does not end in ", arg);
2008     ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2009     if (n_allowed == 2) {
2010         ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2011     } else if (n_allowed > 2) {
2012         size_t i;
2013
2014         for (i = 1; i < n_allowed - 1; i++) {
2015             ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2016         }
2017         ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2018     }
2019     ds_put_format(&s, " followed by a value.");
2020
2021     return ds_steal_cstr(&s);
2022 }
2023
2024 /* Breaks 'arg' apart into a number of fields in the following order:
2025  *
2026  *      - If 'columnp' is nonnull, the name of a column in 'table'.  The column
2027  *        is stored into '*columnp'.  The column name may be abbreviated.
2028  *
2029  *      - If 'keyp' is nonnull, optionally a key string.  (If both 'columnp'
2030  *        and 'keyp' are nonnull, then the column and key names are expected to
2031  *        be separated by ':').  The key is stored as a malloc()'d string into
2032  *        '*keyp', or NULL if no key is present in 'arg'.
2033  *
2034  *      - If 'valuep' is nonnull, an operator followed by a value string.  The
2035  *        allowed operators are the 'n_allowed' string in 'allowed_operators',
2036  *        or just "=" if 'n_allowed' is 0.  If 'operatorp' is nonnull, then the
2037  *        operator is stored into '*operatorp' (one of the pointers from
2038  *        'allowed_operators' is stored; nothing is malloc()'d).  The value is
2039  *        stored as a malloc()'d string into '*valuep', or NULL if no value is
2040  *        present in 'arg'.
2041  *
2042  * At least 'columnp' or 'keyp' must be nonnull.
2043  *
2044  * On success, returns NULL.  On failure, returned a malloc()'d string error
2045  * message and stores NULL into all of the nonnull output arguments. */
2046 static char * WARN_UNUSED_RESULT
2047 parse_column_key_value(const char *arg,
2048                        const struct vsctl_table_class *table,
2049                        const struct ovsdb_idl_column **columnp, char **keyp,
2050                        const char **operatorp,
2051                        const char **allowed_operators, size_t n_allowed,
2052                        char **valuep)
2053 {
2054     const char *p = arg;
2055     char *error;
2056
2057     assert(columnp || keyp);
2058     assert(!(operatorp && !valuep));
2059     if (keyp) {
2060         *keyp = NULL;
2061     }
2062     if (valuep) {
2063         *valuep = NULL;
2064     }
2065
2066     /* Parse column name. */
2067     if (columnp) {
2068         char *column_name;
2069
2070         error = ovsdb_token_parse(&p, &column_name);
2071         if (error) {
2072             goto error;
2073         }
2074         if (column_name[0] == '\0') {
2075             free(column_name);
2076             error = xasprintf("%s: missing column name", arg);
2077             goto error;
2078         }
2079         error = get_column(table, column_name, columnp);
2080         free(column_name);
2081         if (error) {
2082             goto error;
2083         }
2084     }
2085
2086     /* Parse key string. */
2087     if (*p == ':' || !columnp) {
2088         if (columnp) {
2089             p++;
2090         } else if (!keyp) {
2091             error = xasprintf("%s: key not accepted here", arg);
2092             goto error;
2093         }
2094         error = ovsdb_token_parse(&p, keyp);
2095         if (error) {
2096             goto error;
2097         }
2098     } else if (keyp) {
2099         *keyp = NULL;
2100     }
2101
2102     /* Parse value string. */
2103     if (valuep) {
2104         const char *best;
2105         size_t best_len;
2106         size_t i;
2107
2108         if (!allowed_operators) {
2109             static const char *equals = "=";
2110             allowed_operators = &equals;
2111             n_allowed = 1;
2112         }
2113
2114         best = NULL;
2115         best_len = 0;
2116         for (i = 0; i < n_allowed; i++) {
2117             const char *op = allowed_operators[i];
2118             size_t op_len = strlen(op);
2119
2120             if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2121                 best_len = op_len;
2122                 best = op;
2123             }
2124         }
2125         if (!best) {
2126             error = missing_operator_error(arg, allowed_operators, n_allowed);
2127             goto error;
2128         }
2129
2130         if (operatorp) {
2131             *operatorp = best;
2132         }
2133         *valuep = xstrdup(p + best_len);
2134     } else {
2135         if (valuep) {
2136             *valuep = NULL;
2137         }
2138         if (*p != '\0') {
2139             error = xasprintf("%s: trailing garbage \"%s\" in argument",
2140                               arg, p);
2141             goto error;
2142         }
2143     }
2144     return NULL;
2145
2146 error:
2147     if (columnp) {
2148         *columnp = NULL;
2149     }
2150     if (keyp) {
2151         free(*keyp);
2152         *keyp = NULL;
2153     }
2154     if (valuep) {
2155         free(*valuep);
2156         *valuep = NULL;
2157         if (operatorp) {
2158             *operatorp = NULL;
2159         }
2160     }
2161     return error;
2162 }
2163
2164 static void
2165 cmd_get(struct vsctl_context *ctx)
2166 {
2167     const char *id = shash_find_data(&ctx->options, "--id");
2168     bool if_exists = shash_find(&ctx->options, "--if-exists");
2169     const char *table_name = ctx->argv[1];
2170     const char *record_id = ctx->argv[2];
2171     const struct vsctl_table_class *table;
2172     const struct ovsdb_idl_row *row;
2173     struct ds *out = &ctx->output;
2174     int i;
2175
2176     table = get_table(table_name);
2177     row = must_get_row(ctx, table, record_id);
2178     if (id) {
2179         bool new;
2180
2181         *create_symbol(ctx->symtab, id, &new) = row->uuid;
2182         if (!new) {
2183             vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2184                         "before it was defined", id);
2185         }
2186     }
2187     for (i = 3; i < ctx->argc; i++) {
2188         const struct ovsdb_idl_column *column;
2189         const struct ovsdb_datum *datum;
2190         char *key_string;
2191
2192         /* Special case for obtaining the UUID of a row.  We can't just do this
2193          * through parse_column_key_value() below since it returns a "struct
2194          * ovsdb_idl_column" and the UUID column doesn't have one. */
2195         if (!strcasecmp(ctx->argv[i], "_uuid")
2196             || !strcasecmp(ctx->argv[i], "-uuid")) {
2197             ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2198             continue;
2199         }
2200
2201         die_if_error(parse_column_key_value(ctx->argv[i], table,
2202                                             &column, &key_string,
2203                                             NULL, NULL, 0, NULL));
2204
2205         datum = ovsdb_idl_read(row, column);
2206         if (key_string) {
2207             union ovsdb_atom key;
2208             unsigned int idx;
2209
2210             if (column->type.value.type == OVSDB_TYPE_VOID) {
2211                 vsctl_fatal("cannot specify key to get for non-map column %s",
2212                             column->name);
2213             }
2214
2215             die_if_error(ovsdb_atom_from_string(&key,
2216                                                 &column->type.key,
2217                                                 key_string, ctx->symtab));
2218
2219             idx = ovsdb_datum_find_key(datum, &key,
2220                                        column->type.key.type);
2221             if (idx == UINT_MAX) {
2222                 if (!if_exists) {
2223                     vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2224                                 key_string, table->class->name, record_id,
2225                                 column->name);
2226                 }
2227             } else {
2228                 ovsdb_atom_to_string(&datum->values[idx],
2229                                      column->type.value.type, out);
2230             }
2231             ovsdb_atom_destroy(&key, column->type.key.type);
2232         } else {
2233             ovsdb_datum_to_string(datum, &column->type, out);
2234         }
2235         ds_put_char(out, '\n');
2236
2237         free(key_string);
2238     }
2239 }
2240
2241 static void
2242 list_record(const struct vsctl_table_class *table,
2243             const struct ovsdb_idl_row *row, struct ds *out)
2244 {
2245     size_t i;
2246
2247     ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2248                   UUID_ARGS(&row->uuid));
2249     for (i = 0; i < table->class->n_columns; i++) {
2250         const struct ovsdb_idl_column *column = &table->class->columns[i];
2251         const struct ovsdb_datum *datum;
2252
2253         datum = ovsdb_idl_read(row, column);
2254
2255         ds_put_format(out, "%-20s: ", column->name);
2256         ovsdb_datum_to_string(datum, &column->type, out);
2257         ds_put_char(out, '\n');
2258     }
2259 }
2260
2261 static void
2262 cmd_list(struct vsctl_context *ctx)
2263 {
2264     const char *table_name = ctx->argv[1];
2265     const struct vsctl_table_class *table;
2266     struct ds *out = &ctx->output;
2267     int i;
2268
2269     table = get_table(table_name);
2270     if (ctx->argc > 2) {
2271         for (i = 2; i < ctx->argc; i++) {
2272             if (i > 2) {
2273                 ds_put_char(out, '\n');
2274             }
2275             list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2276         }
2277     } else {
2278         const struct ovsdb_idl_row *row;
2279         bool first;
2280
2281         for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2282              row != NULL;
2283              row = ovsdb_idl_next_row(row), first = false) {
2284             if (!first) {
2285                 ds_put_char(out, '\n');
2286             }
2287             list_record(table, row, out);
2288         }
2289     }
2290 }
2291
2292 static void
2293 set_column(const struct vsctl_table_class *table,
2294            const struct ovsdb_idl_row *row, const char *arg,
2295            struct ovsdb_symbol_table *symtab)
2296 {
2297     const struct ovsdb_idl_column *column;
2298     char *key_string, *value_string;
2299     char *error;
2300
2301     error = parse_column_key_value(arg, table, &column, &key_string,
2302                                    NULL, NULL, 0, &value_string);
2303     die_if_error(error);
2304     if (!value_string) {
2305         vsctl_fatal("%s: missing value", arg);
2306     }
2307
2308     if (key_string) {
2309         union ovsdb_atom key, value;
2310         struct ovsdb_datum datum;
2311
2312         if (column->type.value.type == OVSDB_TYPE_VOID) {
2313             vsctl_fatal("cannot specify key to set for non-map column %s",
2314                         column->name);
2315         }
2316
2317         die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2318                                             key_string, symtab));
2319         die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2320                                             value_string, symtab));
2321
2322         ovsdb_datum_init_empty(&datum);
2323         ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2324
2325         ovsdb_atom_destroy(&key, column->type.key.type);
2326         ovsdb_atom_destroy(&value, column->type.value.type);
2327
2328         ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2329                           &column->type, false);
2330         ovsdb_idl_txn_write(row, column, &datum);
2331     } else {
2332         struct ovsdb_datum datum;
2333
2334         die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2335                                              value_string, symtab));
2336         ovsdb_idl_txn_write(row, column, &datum);
2337     }
2338
2339     free(key_string);
2340     free(value_string);
2341 }
2342
2343 static void
2344 cmd_set(struct vsctl_context *ctx)
2345 {
2346     const char *table_name = ctx->argv[1];
2347     const char *record_id = ctx->argv[2];
2348     const struct vsctl_table_class *table;
2349     const struct ovsdb_idl_row *row;
2350     int i;
2351
2352     table = get_table(table_name);
2353     row = must_get_row(ctx, table, record_id);
2354     for (i = 3; i < ctx->argc; i++) {
2355         set_column(table, row, ctx->argv[i], ctx->symtab);
2356     }
2357 }
2358
2359 static void
2360 cmd_add(struct vsctl_context *ctx)
2361 {
2362     const char *table_name = ctx->argv[1];
2363     const char *record_id = ctx->argv[2];
2364     const char *column_name = ctx->argv[3];
2365     const struct vsctl_table_class *table;
2366     const struct ovsdb_idl_column *column;
2367     const struct ovsdb_idl_row *row;
2368     const struct ovsdb_type *type;
2369     struct ovsdb_datum old;
2370     int i;
2371
2372     table = get_table(table_name);
2373     row = must_get_row(ctx, table, record_id);
2374     die_if_error(get_column(table, column_name, &column));
2375
2376     type = &column->type;
2377     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2378     for (i = 4; i < ctx->argc; i++) {
2379         struct ovsdb_type add_type;
2380         struct ovsdb_datum add;
2381
2382         add_type = *type;
2383         add_type.n_min = 1;
2384         add_type.n_max = UINT_MAX;
2385         die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2386                                              ctx->symtab));
2387         ovsdb_datum_union(&old, &add, type, false);
2388         ovsdb_datum_destroy(&add, type);
2389     }
2390     if (old.n > type->n_max) {
2391         vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2392                     "table %s but the maximum number is %u",
2393                     old.n,
2394                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2395                     column->name, table->class->name, type->n_max);
2396     }
2397     ovsdb_idl_txn_write(row, column, &old);
2398 }
2399
2400 static void
2401 cmd_remove(struct vsctl_context *ctx)
2402 {
2403     const char *table_name = ctx->argv[1];
2404     const char *record_id = ctx->argv[2];
2405     const char *column_name = ctx->argv[3];
2406     const struct vsctl_table_class *table;
2407     const struct ovsdb_idl_column *column;
2408     const struct ovsdb_idl_row *row;
2409     const struct ovsdb_type *type;
2410     struct ovsdb_datum old;
2411     int i;
2412
2413     table = get_table(table_name);
2414     row = must_get_row(ctx, table, record_id);
2415     die_if_error(get_column(table, column_name, &column));
2416
2417     type = &column->type;
2418     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2419     for (i = 4; i < ctx->argc; i++) {
2420         struct ovsdb_type rm_type;
2421         struct ovsdb_datum rm;
2422         char *error;
2423
2424         rm_type = *type;
2425         rm_type.n_min = 1;
2426         rm_type.n_max = UINT_MAX;
2427         error = ovsdb_datum_from_string(&rm, &rm_type,
2428                                         ctx->argv[i], ctx->symtab);
2429         if (error && ovsdb_type_is_map(&rm_type)) {
2430             free(error);
2431             rm_type.value.type = OVSDB_TYPE_VOID;
2432             die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2433                                                  ctx->argv[i], ctx->symtab));
2434         }
2435         ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2436         ovsdb_datum_destroy(&rm, &rm_type);
2437     }
2438     if (old.n < type->n_min) {
2439         vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2440                     "table %s but the minimum number is %u",
2441                     old.n,
2442                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2443                     column->name, table->class->name, type->n_min);
2444     }
2445     ovsdb_idl_txn_write(row, column, &old);
2446 }
2447
2448 static void
2449 cmd_clear(struct vsctl_context *ctx)
2450 {
2451     const char *table_name = ctx->argv[1];
2452     const char *record_id = ctx->argv[2];
2453     const struct vsctl_table_class *table;
2454     const struct ovsdb_idl_row *row;
2455     int i;
2456
2457     table = get_table(table_name);
2458     row = must_get_row(ctx, table, record_id);
2459     for (i = 3; i < ctx->argc; i++) {
2460         const struct ovsdb_idl_column *column;
2461         const struct ovsdb_type *type;
2462         struct ovsdb_datum datum;
2463
2464         die_if_error(get_column(table, ctx->argv[i], &column));
2465
2466         type = &column->type;
2467         if (type->n_min > 0) {
2468             vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2469                         "of table %s, which is not allowed to be empty",
2470                         column->name, table->class->name);
2471         }
2472
2473         ovsdb_datum_init_empty(&datum);
2474         ovsdb_idl_txn_write(row, column, &datum);
2475     }
2476 }
2477
2478 static void
2479 cmd_create(struct vsctl_context *ctx)
2480 {
2481     const char *id = shash_find_data(&ctx->options, "--id");
2482     const char *table_name = ctx->argv[1];
2483     const struct vsctl_table_class *table;
2484     const struct ovsdb_idl_row *row;
2485     const struct uuid *uuid;
2486     int i;
2487
2488     uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL;
2489
2490     table = get_table(table_name);
2491     row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2492     for (i = 2; i < ctx->argc; i++) {
2493         set_column(table, row, ctx->argv[i], ctx->symtab);
2494     }
2495     ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2496 }
2497
2498 /* This function may be used as the 'postprocess' function for commands that
2499  * insert new rows into the database.  It expects that the command's 'run'
2500  * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2501  * sole output.  It replaces that output by the row's permanent UUID assigned
2502  * by the database server and appends a new-line.
2503  *
2504  * Currently we use this only for "create", because the higher-level commands
2505  * are supposed to be independent of the actual structure of the vswitch
2506  * configuration. */
2507 static void
2508 post_create(struct vsctl_context *ctx)
2509 {
2510     const struct uuid *real;
2511     struct uuid dummy;
2512
2513     uuid_from_string(&dummy, ds_cstr(&ctx->output));
2514     real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2515     if (real) {
2516         ds_clear(&ctx->output);
2517         ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2518     }
2519     ds_put_char(&ctx->output, '\n');
2520 }
2521
2522 static void
2523 cmd_destroy(struct vsctl_context *ctx)
2524 {
2525     bool must_exist = !shash_find(&ctx->options, "--if-exists");
2526     const char *table_name = ctx->argv[1];
2527     const struct vsctl_table_class *table;
2528     int i;
2529
2530     table = get_table(table_name);
2531     for (i = 2; i < ctx->argc; i++) {
2532         const struct ovsdb_idl_row *row;
2533
2534         row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2535         if (row) {
2536             ovsdb_idl_txn_delete(row);
2537         }
2538     }
2539 }
2540
2541 static bool
2542 is_condition_satified(const struct vsctl_table_class *table,
2543                       const struct ovsdb_idl_row *row, const char *arg,
2544                       struct ovsdb_symbol_table *symtab)
2545 {
2546     static const char *operators[] = {
2547         "=", "!=", "<", ">", "<=", ">="
2548     };
2549
2550     const struct ovsdb_idl_column *column;
2551     const struct ovsdb_datum *have_datum;
2552     char *key_string, *value_string;
2553     const char *operator;
2554     unsigned int idx;
2555     char *error;
2556     int cmp = 0;
2557
2558     error = parse_column_key_value(arg, table, &column, &key_string,
2559                                    &operator, operators, ARRAY_SIZE(operators),
2560                                    &value_string);
2561     die_if_error(error);
2562     if (!value_string) {
2563         vsctl_fatal("%s: missing value", arg);
2564     }
2565
2566     have_datum = ovsdb_idl_read(row, column);
2567     if (key_string) {
2568         union ovsdb_atom want_key, want_value;
2569
2570         if (column->type.value.type == OVSDB_TYPE_VOID) {
2571             vsctl_fatal("cannot specify key to check for non-map column %s",
2572                         column->name);
2573         }
2574
2575         die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2576                                             key_string, symtab));
2577         die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2578                                             value_string, symtab));
2579
2580         idx = ovsdb_datum_find_key(have_datum,
2581                                    &want_key, column->type.key.type);
2582         if (idx != UINT_MAX) {
2583             cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2584                                           &want_value,
2585                                           column->type.value.type);
2586         }
2587
2588         ovsdb_atom_destroy(&want_key, column->type.key.type);
2589         ovsdb_atom_destroy(&want_value, column->type.value.type);
2590     } else {
2591         struct ovsdb_datum want_datum;
2592
2593         die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2594                                              value_string, symtab));
2595         idx = 0;
2596         cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2597                                        &column->type);
2598         ovsdb_datum_destroy(&want_datum, &column->type);
2599     }
2600
2601     free(key_string);
2602     free(value_string);
2603
2604     return (idx == UINT_MAX ? false
2605             : !strcmp(operator, "=") ? cmp == 0
2606             : !strcmp(operator, "!=") ? cmp != 0
2607             : !strcmp(operator, "<") ? cmp < 0
2608             : !strcmp(operator, ">") ? cmp > 0
2609             : !strcmp(operator, "<=") ? cmp <= 0
2610             : !strcmp(operator, ">=") ? cmp >= 0
2611             : (abort(), 0));
2612 }
2613
2614 static void
2615 cmd_wait_until(struct vsctl_context *ctx)
2616 {
2617     const char *table_name = ctx->argv[1];
2618     const char *record_id = ctx->argv[2];
2619     const struct vsctl_table_class *table;
2620     const struct ovsdb_idl_row *row;
2621     int i;
2622
2623     table = get_table(table_name);
2624
2625     row = get_row(ctx, table, record_id);
2626     if (!row) {
2627         ctx->try_again = true;
2628         return;
2629     }
2630
2631     for (i = 3; i < ctx->argc; i++) {
2632         if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2633             ctx->try_again = true;
2634             return;
2635         }
2636     }
2637 }
2638 \f
2639 static struct json *
2640 where_uuid_equals(const struct uuid *uuid)
2641 {
2642     return
2643         json_array_create_1(
2644             json_array_create_3(
2645                 json_string_create("_uuid"),
2646                 json_string_create("=="),
2647                 json_array_create_2(
2648                     json_string_create("uuid"),
2649                     json_string_create_nocopy(
2650                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2651 }
2652
2653 static void
2654 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2655                    struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2656                    const struct ovsrec_open_vswitch *ovs,
2657     struct ovsdb_symbol_table *symtab)
2658 {
2659     ctx->argc = command->argc;
2660     ctx->argv = command->argv;
2661     ctx->options = command->options;
2662
2663     ds_swap(&ctx->output, &command->output);
2664     ctx->idl = idl;
2665     ctx->txn = txn;
2666     ctx->ovs = ovs;
2667     ctx->symtab = symtab;
2668
2669     ctx->try_again = false;
2670 }
2671
2672 static void
2673 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2674 {
2675     ds_swap(&ctx->output, &command->output);
2676 }
2677
2678 static void
2679 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2680          struct ovsdb_idl *idl)
2681 {
2682     struct ovsdb_idl_txn *txn;
2683     const struct ovsrec_open_vswitch *ovs;
2684     enum ovsdb_idl_txn_status status;
2685     struct ovsdb_symbol_table *symtab;
2686     const char *unused;
2687     struct vsctl_command *c;
2688     int64_t next_cfg = 0;
2689     char *error = NULL;
2690
2691     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2692     if (dry_run) {
2693         ovsdb_idl_txn_set_dry_run(txn);
2694     }
2695
2696     ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2697
2698     ovs = ovsrec_open_vswitch_first(idl);
2699     if (!ovs) {
2700         /* XXX add verification that table is empty */
2701         ovs = ovsrec_open_vswitch_insert(txn);
2702     }
2703
2704     if (wait_for_reload) {
2705         struct json *where = where_uuid_equals(&ovs->header_.uuid);
2706         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2707         json_destroy(where);
2708     }
2709
2710     symtab = ovsdb_symbol_table_create();
2711     for (c = commands; c < &commands[n_commands]; c++) {
2712         ds_init(&c->output);
2713     }
2714     for (c = commands; c < &commands[n_commands]; c++) {
2715         struct vsctl_context ctx;
2716
2717         vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2718         (c->syntax->run)(&ctx);
2719         vsctl_context_done(&ctx, c);
2720
2721         if (ctx.try_again) {
2722             goto try_again;
2723         }
2724     }
2725
2726     status = ovsdb_idl_txn_commit_block(txn);
2727     if (wait_for_reload && status == TXN_SUCCESS) {
2728         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2729     }
2730     if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2731         for (c = commands; c < &commands[n_commands]; c++) {
2732             if (c->syntax->postprocess) {
2733                 struct vsctl_context ctx;
2734
2735                 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2736                 (c->syntax->postprocess)(&ctx);
2737                 vsctl_context_done(&ctx, c);
2738             }
2739         }
2740     }
2741     error = xstrdup(ovsdb_idl_txn_get_error(txn));
2742     ovsdb_idl_txn_destroy(txn);
2743     the_idl_txn = NULL;
2744
2745     unused = ovsdb_symbol_table_find_unused(symtab);
2746     if (unused) {
2747         vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2748                     "with \"-- --id=%s create ...\")", unused, unused);
2749     }
2750
2751     switch (status) {
2752     case TXN_INCOMPLETE:
2753         NOT_REACHED();
2754
2755     case TXN_ABORTED:
2756         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2757         vsctl_fatal("transaction aborted");
2758
2759     case TXN_UNCHANGED:
2760     case TXN_SUCCESS:
2761         break;
2762
2763     case TXN_TRY_AGAIN:
2764         goto try_again;
2765
2766     case TXN_ERROR:
2767         vsctl_fatal("transaction error: %s", error);
2768
2769     default:
2770         NOT_REACHED();
2771     }
2772     free(error);
2773
2774     ovsdb_symbol_table_destroy(symtab);
2775
2776     for (c = commands; c < &commands[n_commands]; c++) {
2777         struct ds *ds = &c->output;
2778         struct shash_node *node;
2779
2780         if (oneline) {
2781             size_t j;
2782
2783             ds_chomp(ds, '\n');
2784             for (j = 0; j < ds->length; j++) {
2785                 int ch = ds->string[j];
2786                 switch (ch) {
2787                 case '\n':
2788                     fputs("\\n", stdout);
2789                     break;
2790
2791                 case '\\':
2792                     fputs("\\\\", stdout);
2793                     break;
2794
2795                 default:
2796                     putchar(ch);
2797                 }
2798             }
2799             putchar('\n');
2800         } else {
2801             fputs(ds_cstr(ds), stdout);
2802         }
2803         ds_destroy(&c->output);
2804
2805         SHASH_FOR_EACH (node, &c->options) {
2806             free(node->data);
2807         }
2808         shash_destroy(&c->options);
2809     }
2810     free(commands);
2811
2812     if (wait_for_reload && status != TXN_UNCHANGED) {
2813         for (;;) {
2814             ovsdb_idl_run(idl);
2815             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2816                 if (ovs->cur_cfg >= next_cfg) {
2817                     goto done;
2818                 }
2819             }
2820             ovsdb_idl_wait(idl);
2821             poll_block();
2822         }
2823     done: ;
2824     }
2825     ovsdb_idl_destroy(idl);
2826
2827     exit(EXIT_SUCCESS);
2828
2829 try_again:
2830     /* Our transaction needs to be rerun, or a prerequisite was not met.  Free
2831      * resources and return so that the caller can try again. */
2832     ovsdb_idl_txn_abort(txn);
2833     ovsdb_idl_txn_destroy(txn);
2834     ovsdb_symbol_table_destroy(symtab);
2835     for (c = commands; c < &commands[n_commands]; c++) {
2836         ds_destroy(&c->output);
2837     }
2838     free(error);
2839 }
2840
2841 static const struct vsctl_command_syntax all_commands[] = {
2842     /* Open vSwitch commands. */
2843     {"init", 0, 0, cmd_init, NULL, ""},
2844
2845     /* Bridge commands. */
2846     {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2847     {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2848     {"list-br", 0, 0, cmd_list_br, NULL, ""},
2849     {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2850     {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2851     {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2852     {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2853     {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2854
2855     /* Port commands. */
2856     {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2857     {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2858     {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2859     {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2860     {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2861
2862     /* Interface commands. */
2863     {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2864     {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2865
2866     /* Controller commands. */
2867     {"get-controller", 1, 1, cmd_get_controller, NULL, ""},
2868     {"del-controller", 1, 1, cmd_del_controller, NULL, ""},
2869     {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2870     {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, ""},
2871     {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, ""},
2872     {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, ""},
2873
2874     /* SSL commands. */
2875     {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2876     {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2877     {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2878
2879     /* Switch commands. */
2880     {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2881
2882     /* Parameter commands. */
2883     {"get", 2, INT_MAX, cmd_get, NULL, "--if-exists,--id="},
2884     {"list", 1, INT_MAX, cmd_list, NULL, ""},
2885     {"set", 3, INT_MAX, cmd_set, NULL, ""},
2886     {"add", 4, INT_MAX, cmd_add, NULL, ""},
2887     {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2888     {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2889     {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2890     {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2891     {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2892
2893     {NULL, 0, 0, NULL, NULL, NULL},
2894 };
2895