ovs-vsctl: Support references among records at creation time.
[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_ssl,
1908      {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1909
1910     {&ovsrec_table_sflow,
1911      {{&ovsrec_table_bridge,
1912        &ovsrec_bridge_col_name,
1913        &ovsrec_bridge_col_sflow},
1914       {NULL, NULL, NULL}}},
1915
1916     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1917 };
1918
1919 static void
1920 die_if_error(char *error)
1921 {
1922     if (error) {
1923         vsctl_fatal("%s", error);
1924     }
1925 }
1926
1927 static int
1928 to_lower_and_underscores(unsigned c)
1929 {
1930     return c == '-' ? '_' : tolower(c);
1931 }
1932
1933 static unsigned int
1934 score_partial_match(const char *name, const char *s)
1935 {
1936     int score;
1937
1938     if (!strcmp(name, s)) {
1939         return UINT_MAX;
1940     }
1941     for (score = 0; ; score++, name++, s++) {
1942         if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1943             break;
1944         } else if (*name == '\0') {
1945             return UINT_MAX - 1;
1946         }
1947     }
1948     return *s == '\0' ? score : 0;
1949 }
1950
1951 static const struct vsctl_table_class *
1952 get_table(const char *table_name)
1953 {
1954     const struct vsctl_table_class *table;
1955     const struct vsctl_table_class *best_match = NULL;
1956     unsigned int best_score = 0;
1957
1958     for (table = tables; table->class; table++) {
1959         unsigned int score = score_partial_match(table->class->name,
1960                                                  table_name);
1961         if (score > best_score) {
1962             best_match = table;
1963             best_score = score;
1964         } else if (score == best_score) {
1965             best_match = NULL;
1966         }
1967     }
1968     if (best_match) {
1969         return best_match;
1970     } else if (best_score) {
1971         vsctl_fatal("multiple table names match \"%s\"", table_name);
1972     } else {
1973         vsctl_fatal("unknown table \"%s\"", table_name);
1974     }
1975 }
1976
1977 static const struct ovsdb_idl_row *
1978 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1979               const struct vsctl_row_id *id, const char *record_id)
1980 {
1981     const struct ovsdb_idl_row *referrer, *final;
1982
1983     if (!id->table) {
1984         return NULL;
1985     }
1986
1987     if (!id->name_column) {
1988         if (strcmp(record_id, ".")) {
1989             return NULL;
1990         }
1991         referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1992         if (!referrer || ovsdb_idl_next_row(referrer)) {
1993             return NULL;
1994         }
1995     } else {
1996         const struct ovsdb_idl_row *row;
1997         unsigned int best_score = 0;
1998
1999         /* It might make sense to relax this assertion. */
2000         assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
2001
2002         referrer = NULL;
2003         for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2004              row != NULL && best_score != UINT_MAX;
2005              row = ovsdb_idl_next_row(row))
2006         {
2007             struct ovsdb_datum name;
2008
2009             ovsdb_idl_txn_read(row, id->name_column, &name);
2010             if (name.n == 1) {
2011                 unsigned int score = score_partial_match(name.keys[0].string,
2012                                                          record_id);
2013                 if (score > best_score) {
2014                     referrer = row;
2015                     best_score = score;
2016                 } else if (score == best_score) {
2017                     referrer = NULL;
2018                 }
2019             }
2020             ovsdb_datum_destroy(&name, &id->name_column->type);
2021         }
2022         if (best_score && !referrer) {
2023             vsctl_fatal("multiple rows in %s match \"%s\"",
2024                         table->class->name, record_id);
2025         }
2026     }
2027     if (!referrer) {
2028         return NULL;
2029     }
2030
2031     final = NULL;
2032     if (id->uuid_column) {
2033         struct ovsdb_datum uuid;
2034
2035         assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
2036         assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
2037
2038         ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
2039         if (uuid.n == 1) {
2040             final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2041                                                &uuid.keys[0].uuid);
2042         }
2043         ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
2044     } else {
2045         final = referrer;
2046     }
2047
2048     return final;
2049 }
2050
2051 static const struct ovsdb_idl_row *
2052 get_row(struct vsctl_context *ctx,
2053         const struct vsctl_table_class *table, const char *record_id)
2054 {
2055     const struct ovsdb_idl_row *row;
2056     struct uuid uuid;
2057
2058     if (uuid_from_string(&uuid, record_id)) {
2059         row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2060     } else {
2061         int i;
2062
2063         for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2064             row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2065             if (row) {
2066                 break;
2067             }
2068         }
2069     }
2070     return row;
2071 }
2072
2073 static const struct ovsdb_idl_row *
2074 must_get_row(struct vsctl_context *ctx,
2075              const struct vsctl_table_class *table, const char *record_id)
2076 {
2077     const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2078     if (!row) {
2079         vsctl_fatal("no row \"%s\" in table %s",
2080                     record_id, table->class->name);
2081     }
2082     return row;
2083 }
2084
2085 static char *
2086 get_column(const struct vsctl_table_class *table, const char *column_name,
2087            const struct ovsdb_idl_column **columnp)
2088 {
2089     const struct ovsdb_idl_column *best_match = NULL;
2090     unsigned int best_score = 0;
2091     size_t i;
2092
2093     for (i = 0; i < table->class->n_columns; i++) {
2094         const struct ovsdb_idl_column *column = &table->class->columns[i];
2095         unsigned int score = score_partial_match(column->name, column_name);
2096         if (score > best_score) {
2097             best_match = column;
2098             best_score = score;
2099         } else if (score == best_score) {
2100             best_match = NULL;
2101         }
2102     }
2103
2104     *columnp = best_match;
2105     if (best_match) {
2106         return NULL;
2107     } else if (best_score) {
2108         return xasprintf("%s contains more than one column whose name "
2109                          "matches \"%s\"", table->class->name, column_name);
2110     } else {
2111         return xasprintf("%s does not contain a column whose name matches "
2112                          "\"%s\"", table->class->name, column_name);
2113     }
2114 }
2115
2116 static char * WARN_UNUSED_RESULT
2117 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
2118                        const struct ovsdb_idl_column **columnp,
2119                        char **keyp, char **valuep)
2120 {
2121     const char *p = arg;
2122     char *error;
2123
2124     assert(columnp || keyp);
2125     if (keyp) {
2126         *keyp = NULL;
2127     }
2128     if (valuep) {
2129         *valuep = NULL;
2130     }
2131
2132     /* Parse column name. */
2133     if (columnp) {
2134         char *column_name;
2135
2136         error = ovsdb_token_parse(&p, &column_name);
2137         if (error) {
2138             goto error;
2139         }
2140         if (column_name[0] == '\0') {
2141             free(column_name);
2142             error = xasprintf("%s: missing column name", arg);
2143             goto error;
2144         }
2145         error = get_column(table, column_name, columnp);
2146         free(column_name);
2147         if (error) {
2148             goto error;
2149         }
2150     }
2151
2152     /* Parse key string. */
2153     if (*p == ':' || !columnp) {
2154         if (columnp) {
2155             p++;
2156         } else if (!keyp) {
2157             error = xasprintf("%s: key not accepted here", arg);
2158             goto error;
2159         }
2160         error = ovsdb_token_parse(&p, keyp);
2161         if (error) {
2162             goto error;
2163         }
2164     } else if (keyp) {
2165         *keyp = NULL;
2166     }
2167
2168     /* Parse value string. */
2169     if (*p == '=') {
2170         if (!valuep) {
2171             error = xasprintf("%s: value not accepted here", arg);
2172             goto error;
2173         }
2174         *valuep = xstrdup(p + 1);
2175     } else {
2176         if (valuep) {
2177             *valuep = NULL;
2178         }
2179         if (*p != '\0') {
2180             error = xasprintf("%s: trailing garbage \"%s\" in argument",
2181                               arg, p);
2182             goto error;
2183         }
2184     }
2185     return NULL;
2186
2187 error:
2188     if (columnp) {
2189         *columnp = NULL;
2190     }
2191     if (keyp) {
2192         free(*keyp);
2193         *keyp = NULL;
2194     }
2195     if (valuep) {
2196         free(*valuep);
2197         *valuep = NULL;
2198     }
2199     return error;
2200 }
2201
2202 static void
2203 cmd_get(struct vsctl_context *ctx)
2204 {
2205     bool if_exists = shash_find(&ctx->options, "--if-exists");
2206     const char *table_name = ctx->argv[1];
2207     const char *record_id = ctx->argv[2];
2208     const struct vsctl_table_class *table;
2209     const struct ovsdb_idl_row *row;
2210     struct ds *out = &ctx->output;
2211     int i;
2212
2213     table = get_table(table_name);
2214     row = must_get_row(ctx, table, record_id);
2215     for (i = 3; i < ctx->argc; i++) {
2216         const struct ovsdb_idl_column *column;
2217         struct ovsdb_datum datum;
2218         char *key_string;
2219
2220         die_if_error(parse_column_key_value(ctx->argv[i], table,
2221                                             &column, &key_string, NULL));
2222
2223         ovsdb_idl_txn_read(row, column, &datum);
2224         if (key_string) {
2225             union ovsdb_atom key;
2226             unsigned int idx;
2227
2228             if (column->type.value.type == OVSDB_TYPE_VOID) {
2229                 vsctl_fatal("cannot specify key to get for non-map column %s",
2230                             column->name);
2231             }
2232
2233             die_if_error(ovsdb_atom_from_string(&key,
2234                                                 &column->type.key,
2235                                                 key_string, ctx->symtab));
2236
2237             idx = ovsdb_datum_find_key(&datum, &key,
2238                                        column->type.key.type);
2239             if (idx == UINT_MAX) {
2240                 if (!if_exists) {
2241                     vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2242                                 key_string, table->class->name, record_id,
2243                                 column->name);
2244                 }
2245             } else {
2246                 ovsdb_atom_to_string(&datum.values[idx],
2247                                      column->type.value.type, out);
2248             }
2249             ovsdb_atom_destroy(&key, column->type.key.type);
2250         } else {
2251             ovsdb_datum_to_string(&datum, &column->type, out);
2252         }
2253         ds_put_char(out, '\n');
2254         ovsdb_datum_destroy(&datum, &column->type);
2255
2256         free(key_string);
2257     }
2258 }
2259
2260 static void
2261 list_record(const struct vsctl_table_class *table,
2262             const struct ovsdb_idl_row *row, struct ds *out)
2263 {
2264     size_t i;
2265
2266     ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2267                   UUID_ARGS(&row->uuid));
2268     for (i = 0; i < table->class->n_columns; i++) {
2269         const struct ovsdb_idl_column *column = &table->class->columns[i];
2270         struct ovsdb_datum datum;
2271
2272         ovsdb_idl_txn_read(row, column, &datum);
2273
2274         ds_put_format(out, "%-20s: ", column->name);
2275         ovsdb_datum_to_string(&datum, &column->type, out);
2276         ds_put_char(out, '\n');
2277
2278         ovsdb_datum_destroy(&datum, &column->type);
2279     }
2280 }
2281
2282 static void
2283 cmd_list(struct vsctl_context *ctx)
2284 {
2285     const char *table_name = ctx->argv[1];
2286     const struct vsctl_table_class *table;
2287     struct ds *out = &ctx->output;
2288     int i;
2289
2290     table = get_table(table_name);
2291     if (ctx->argc > 2) {
2292         for (i = 2; i < ctx->argc; i++) {
2293             if (i > 2) {
2294                 ds_put_char(out, '\n');
2295             }
2296             list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2297         }
2298     } else {
2299         const struct ovsdb_idl_row *row;
2300         bool first;
2301
2302         for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2303              row != NULL;
2304              row = ovsdb_idl_next_row(row), first = false) {
2305             if (!first) {
2306                 ds_put_char(out, '\n');
2307             }
2308             list_record(table, row, out);
2309         }
2310     }
2311 }
2312
2313 static void
2314 set_column(const struct vsctl_table_class *table,
2315            const struct ovsdb_idl_row *row, const char *arg,
2316            struct ovsdb_symbol_table *symtab)
2317 {
2318     const struct ovsdb_idl_column *column;
2319     char *key_string, *value_string;
2320     char *error;
2321
2322     error = parse_column_key_value(arg, table, &column, &key_string,
2323                                    &value_string);
2324     die_if_error(error);
2325     if (!value_string) {
2326         vsctl_fatal("%s: missing value", arg);
2327     }
2328
2329     if (key_string) {
2330         union ovsdb_atom key, value;
2331         struct ovsdb_datum old, new;
2332
2333         if (column->type.value.type == OVSDB_TYPE_VOID) {
2334             vsctl_fatal("cannot specify key to set for non-map column %s",
2335                         column->name);
2336         }
2337
2338         die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2339                                             key_string, symtab));
2340         die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2341                                             value_string, symtab));
2342
2343         ovsdb_datum_init_empty(&new);
2344         ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2345
2346         ovsdb_atom_destroy(&key, column->type.key.type);
2347         ovsdb_atom_destroy(&value, column->type.value.type);
2348
2349         ovsdb_idl_txn_read(row, column, &old);
2350         ovsdb_datum_union(&old, &new, &column->type, true);
2351         ovsdb_idl_txn_write(row, column, &old);
2352
2353         ovsdb_datum_destroy(&new, &column->type);
2354     } else {
2355         struct ovsdb_datum datum;
2356
2357         die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2358                                              value_string, symtab));
2359         ovsdb_idl_txn_write(row, column, &datum);
2360     }
2361
2362     free(key_string);
2363     free(value_string);
2364 }
2365
2366 static void
2367 cmd_set(struct vsctl_context *ctx)
2368 {
2369     const char *table_name = ctx->argv[1];
2370     const char *record_id = ctx->argv[2];
2371     const struct vsctl_table_class *table;
2372     const struct ovsdb_idl_row *row;
2373     int i;
2374
2375     table = get_table(table_name);
2376     row = must_get_row(ctx, table, record_id);
2377     for (i = 3; i < ctx->argc; i++) {
2378         set_column(table, row, ctx->argv[i], ctx->symtab);
2379     }
2380 }
2381
2382 static void
2383 cmd_add(struct vsctl_context *ctx)
2384 {
2385     const char *table_name = ctx->argv[1];
2386     const char *record_id = ctx->argv[2];
2387     const char *column_name = ctx->argv[3];
2388     const struct vsctl_table_class *table;
2389     const struct ovsdb_idl_column *column;
2390     const struct ovsdb_idl_row *row;
2391     const struct ovsdb_type *type;
2392     struct ovsdb_datum old;
2393     int i;
2394
2395     table = get_table(table_name);
2396     row = must_get_row(ctx, table, record_id);
2397     die_if_error(get_column(table, column_name, &column));
2398
2399     type = &column->type;
2400     ovsdb_idl_txn_read(row, column, &old);
2401     for (i = 4; i < ctx->argc; i++) {
2402         struct ovsdb_type add_type;
2403         struct ovsdb_datum add;
2404
2405         add_type = *type;
2406         add_type.n_min = 1;
2407         add_type.n_max = UINT_MAX;
2408         die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2409                                              ctx->symtab));
2410         ovsdb_datum_union(&old, &add, type, false);
2411         ovsdb_datum_destroy(&add, type);
2412     }
2413     if (old.n > type->n_max) {
2414         vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2415                     "table %s but the maximum number is %u",
2416                     old.n,
2417                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2418                     column->name, table->class->name, type->n_max);
2419     }
2420     ovsdb_idl_txn_write(row, column, &old);
2421 }
2422
2423 static void
2424 cmd_remove(struct vsctl_context *ctx)
2425 {
2426     const char *table_name = ctx->argv[1];
2427     const char *record_id = ctx->argv[2];
2428     const char *column_name = ctx->argv[3];
2429     const struct vsctl_table_class *table;
2430     const struct ovsdb_idl_column *column;
2431     const struct ovsdb_idl_row *row;
2432     const struct ovsdb_type *type;
2433     struct ovsdb_datum old;
2434     int i;
2435
2436     table = get_table(table_name);
2437     row = must_get_row(ctx, table, record_id);
2438     die_if_error(get_column(table, column_name, &column));
2439
2440     type = &column->type;
2441     ovsdb_idl_txn_read(row, column, &old);
2442     for (i = 4; i < ctx->argc; i++) {
2443         struct ovsdb_type rm_type;
2444         struct ovsdb_datum rm;
2445         char *error;
2446
2447         rm_type = *type;
2448         rm_type.n_min = 1;
2449         rm_type.n_max = UINT_MAX;
2450         error = ovsdb_datum_from_string(&rm, &rm_type,
2451                                         ctx->argv[i], ctx->symtab);
2452         if (error && ovsdb_type_is_map(&rm_type)) {
2453             free(error);
2454             rm_type.value.type = OVSDB_TYPE_VOID;
2455             die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2456                                                  ctx->argv[i], ctx->symtab));
2457         }
2458         ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2459         ovsdb_datum_destroy(&rm, &rm_type);
2460     }
2461     if (old.n < type->n_min) {
2462         vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2463                     "table %s but the minimum number is %u",
2464                     old.n,
2465                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2466                     column->name, table->class->name, type->n_min);
2467     }
2468     ovsdb_idl_txn_write(row, column, &old);
2469 }
2470
2471 static void
2472 cmd_clear(struct vsctl_context *ctx)
2473 {
2474     const char *table_name = ctx->argv[1];
2475     const char *record_id = ctx->argv[2];
2476     const struct vsctl_table_class *table;
2477     const struct ovsdb_idl_row *row;
2478     int i;
2479
2480     table = get_table(table_name);
2481     row = must_get_row(ctx, table, record_id);
2482     for (i = 3; i < ctx->argc; i++) {
2483         const struct ovsdb_idl_column *column;
2484         const struct ovsdb_type *type;
2485         struct ovsdb_datum datum;
2486
2487         die_if_error(get_column(table, ctx->argv[i], &column));
2488
2489         type = &column->type;
2490         if (type->n_min > 0) {
2491             vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2492                         "of table %s, which is not allowed to be empty",
2493                         column->name, table->class->name);
2494         }
2495
2496         ovsdb_datum_init_empty(&datum);
2497         ovsdb_idl_txn_write(row, column, &datum);
2498     }
2499 }
2500
2501 static void
2502 cmd_create(struct vsctl_context *ctx)
2503 {
2504     const char *id = shash_find_data(&ctx->options, "--id");
2505     const char *table_name = ctx->argv[1];
2506     const struct vsctl_table_class *table;
2507     const struct ovsdb_idl_row *row;
2508     const struct uuid *uuid;
2509     int i;
2510
2511     if (id) {
2512         struct ovsdb_symbol *symbol;
2513
2514         if (id[0] != '@') {
2515             vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2516         }
2517
2518         symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2519         if (symbol->used) {
2520             vsctl_fatal("row id \"%s\" may only be used to insert a single "
2521                         "row", id);
2522         }
2523         symbol->used = true;
2524
2525         uuid = &symbol->uuid;
2526     } else {
2527         uuid = NULL;
2528     }
2529
2530     table = get_table(table_name);
2531     row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2532     for (i = 2; i < ctx->argc; i++) {
2533         set_column(table, row, ctx->argv[i], ctx->symtab);
2534     }
2535     ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2536 }
2537
2538 /* This function may be used as the 'postprocess' function for commands that
2539  * insert new rows into the database.  It expects that the command's 'run'
2540  * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2541  * sole output.  It replaces that output by the row's permanent UUID assigned
2542  * by the database server and appends a new-line.
2543  *
2544  * Currently we use this only for "create", because the higher-level commands
2545  * are supposed to be independent of the actual structure of the vswitch
2546  * configuration. */
2547 static void
2548 post_create(struct vsctl_context *ctx)
2549 {
2550     const struct uuid *real;
2551     struct uuid dummy;
2552
2553     uuid_from_string(&dummy, ds_cstr(&ctx->output));
2554     real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2555     if (real) {
2556         ds_clear(&ctx->output);
2557         ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2558     }
2559     ds_put_char(&ctx->output, '\n');
2560 }
2561
2562 static void
2563 cmd_destroy(struct vsctl_context *ctx)
2564 {
2565     bool must_exist = !shash_find(&ctx->options, "--if-exists");
2566     const char *table_name = ctx->argv[1];
2567     const struct vsctl_table_class *table;
2568     int i;
2569
2570     table = get_table(table_name);
2571     for (i = 2; i < ctx->argc; i++) {
2572         const struct ovsdb_idl_row *row;
2573
2574         row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2575         if (row) {
2576             ovsdb_idl_txn_delete(row);
2577         }
2578     }
2579 }
2580 \f
2581 static struct json *
2582 where_uuid_equals(const struct uuid *uuid)
2583 {
2584     return
2585         json_array_create_1(
2586             json_array_create_3(
2587                 json_string_create("_uuid"),
2588                 json_string_create("=="),
2589                 json_array_create_2(
2590                     json_string_create("uuid"),
2591                     json_string_create_nocopy(
2592                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2593 }
2594
2595 static void
2596 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2597                    struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2598                    const struct ovsrec_open_vswitch *ovs,
2599     struct ovsdb_symbol_table *symtab)
2600 {
2601     ctx->argc = command->argc;
2602     ctx->argv = command->argv;
2603     ctx->options = command->options;
2604
2605     ds_swap(&ctx->output, &command->output);
2606     ctx->idl = idl;
2607     ctx->txn = txn;
2608     ctx->ovs = ovs;
2609     ctx->symtab = symtab;
2610 }
2611
2612 static void
2613 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2614 {
2615     ds_swap(&ctx->output, &command->output);
2616 }
2617
2618 static void
2619 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2620          struct ovsdb_idl *idl)
2621 {
2622     struct ovsdb_idl_txn *txn;
2623     const struct ovsrec_open_vswitch *ovs;
2624     enum ovsdb_idl_txn_status status;
2625     struct ovsdb_symbol_table *symtab;
2626     const char *unused;
2627     struct vsctl_command *c;
2628     int64_t next_cfg = 0;
2629     char *error;
2630
2631     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2632     if (dry_run) {
2633         ovsdb_idl_txn_set_dry_run(txn);
2634     }
2635
2636     ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2637
2638     ovs = ovsrec_open_vswitch_first(idl);
2639     if (!ovs) {
2640         /* XXX add verification that table is empty */
2641         ovs = ovsrec_open_vswitch_insert(txn);
2642     }
2643
2644     if (wait_for_reload) {
2645         struct json *where = where_uuid_equals(&ovs->header_.uuid);
2646         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2647         json_destroy(where);
2648     }
2649
2650     symtab = ovsdb_symbol_table_create();
2651     for (c = commands; c < &commands[n_commands]; c++) {
2652         struct vsctl_context ctx;
2653
2654         ds_init(&c->output);
2655         vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2656         (c->syntax->run)(&ctx);
2657         vsctl_context_done(&ctx, c);
2658     }
2659
2660     status = ovsdb_idl_txn_commit_block(txn);
2661     if (wait_for_reload && status == TXN_SUCCESS) {
2662         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2663     }
2664     if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2665         for (c = commands; c < &commands[n_commands]; c++) {
2666             if (c->syntax->postprocess) {
2667                 struct vsctl_context ctx;
2668
2669                 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2670                 (c->syntax->postprocess)(&ctx);
2671                 vsctl_context_done(&ctx, c);
2672             }
2673         }
2674     }
2675     error = xstrdup(ovsdb_idl_txn_get_error(txn));
2676     ovsdb_idl_txn_destroy(txn);
2677     the_idl_txn = NULL;
2678
2679     unused = ovsdb_symbol_table_find_unused(symtab);
2680     if (unused) {
2681         vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2682                     "with \"-- --id=%s create ...\")", unused, unused);
2683     }
2684     ovsdb_symbol_table_destroy(symtab);
2685
2686     switch (status) {
2687     case TXN_INCOMPLETE:
2688         NOT_REACHED();
2689
2690     case TXN_ABORTED:
2691         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2692         vsctl_fatal("transaction aborted");
2693
2694     case TXN_UNCHANGED:
2695     case TXN_SUCCESS:
2696         break;
2697
2698     case TXN_TRY_AGAIN:
2699         for (c = commands; c < &commands[n_commands]; c++) {
2700             ds_destroy(&c->output);
2701         }
2702         free(error);
2703         return;
2704
2705     case TXN_ERROR:
2706         vsctl_fatal("transaction error: %s", error);
2707
2708     default:
2709         NOT_REACHED();
2710     }
2711     free(error);
2712
2713     for (c = commands; c < &commands[n_commands]; c++) {
2714         struct ds *ds = &c->output;
2715         struct shash_node *node;
2716
2717         if (oneline) {
2718             size_t j;
2719
2720             ds_chomp(ds, '\n');
2721             for (j = 0; j < ds->length; j++) {
2722                 int c = ds->string[j];
2723                 switch (c) {
2724                 case '\n':
2725                     fputs("\\n", stdout);
2726                     break;
2727
2728                 case '\\':
2729                     fputs("\\\\", stdout);
2730                     break;
2731
2732                 default:
2733                     putchar(c);
2734                 }
2735             }
2736             putchar('\n');
2737         } else {
2738             fputs(ds_cstr(ds), stdout);
2739         }
2740         ds_destroy(&c->output);
2741
2742         SHASH_FOR_EACH (node, &c->options) {
2743             free(node->data);
2744         }
2745         shash_destroy(&c->options);
2746     }
2747     free(commands);
2748
2749     if (wait_for_reload && status != TXN_UNCHANGED) {
2750         for (;;) {
2751             const struct ovsrec_open_vswitch *ovs;
2752
2753             ovsdb_idl_run(idl);
2754             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2755                 if (ovs->cur_cfg >= next_cfg) {
2756                     goto done;
2757                 }
2758             }
2759             ovsdb_idl_wait(idl);
2760             poll_block();
2761         }
2762     done: ;
2763     }
2764     ovsdb_idl_destroy(idl);
2765
2766     exit(EXIT_SUCCESS);
2767 }
2768
2769 static const struct vsctl_command_syntax all_commands[] = {
2770     /* Open vSwitch commands. */
2771     {"init", 0, 0, cmd_init, NULL, ""},
2772
2773     /* Bridge commands. */
2774     {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2775     {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2776     {"list-br", 0, 0, cmd_list_br, NULL, ""},
2777     {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2778     {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2779     {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2780     {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2781     {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2782
2783     /* Port commands. */
2784     {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2785     {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2786     {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2787     {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2788     {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2789
2790     /* Interface commands. */
2791     {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2792     {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2793
2794     /* Controller commands. */
2795     {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2796     {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2797     {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2798     {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2799     {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2800     {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2801
2802     /* SSL commands. */
2803     {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2804     {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2805     {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2806
2807     /* Switch commands. */
2808     {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2809
2810     /* Parameter commands. */
2811     {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2812     {"list", 1, INT_MAX, cmd_list, NULL, ""},
2813     {"set", 3, INT_MAX, cmd_set, NULL, ""},
2814     {"add", 4, INT_MAX, cmd_add, NULL, ""},
2815     {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2816     {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2817     {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2818     {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2819
2820     {NULL, 0, 0, NULL, NULL, NULL},
2821 };
2822