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