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