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