ovs-vsctl: Add --columns options to "list" command.
[cascardo/ovs.git] / utilities / ovs-vsctl.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <float.h>
23 #include <getopt.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "command-line.h"
31 #include "compiler.h"
32 #include "dirs.h"
33 #include "dynamic-string.h"
34 #include "json.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
38 #include "process.h"
39 #include "stream-ssl.h"
40 #include "svec.h"
41 #include "vswitchd/vswitch-idl.h"
42 #include "timeval.h"
43 #include "util.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(vsctl);
47
48 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
49 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
50
51 struct vsctl_context;
52
53 /* A command supported by ovs-vsctl. */
54 struct vsctl_command_syntax {
55     const char *name;           /* e.g. "add-br" */
56     int min_args;               /* Min number of arguments following name. */
57     int max_args;               /* Max number of arguments following name. */
58
59     /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
60      * each column or table in ctx->idl that it uses. */
61     void (*prerequisites)(struct vsctl_context *ctx);
62
63     /* Does the actual work of the command and puts the command's output, if
64      * any, in ctx->output.
65      *
66      * Alternatively, if some prerequisite of the command is not met and the
67      * caller should wait for something to change and then retry, it may set
68      * ctx->try_again to true.  (Only the "wait-until" command currently does
69      * this.) */
70     void (*run)(struct vsctl_context *ctx);
71
72     /* If nonnull, called after the transaction has been successfully
73      * committed.  ctx->output is the output from the "run" function, which
74      * this function may modify and otherwise postprocess as needed.  (Only the
75      * "create" command currently does any postprocessing.) */
76     void (*postprocess)(struct vsctl_context *ctx);
77
78     /* A comma-separated list of supported options, e.g. "--a,--b", or the
79      * empty string if the command does not support any options. */
80     const char *options;
81     enum { RO, RW } mode;       /* Does this command modify the database? */
82 };
83
84 struct vsctl_command {
85     /* Data that remains constant after initialization. */
86     const struct vsctl_command_syntax *syntax;
87     int argc;
88     char **argv;
89     struct shash options;
90
91     /* Data modified by commands. */
92     struct ds output;
93 };
94
95 /* --db: The database server to contact. */
96 static const char *db;
97
98 /* --oneline: Write each command's output as a single line? */
99 static bool oneline;
100
101 /* --dry-run: Do not commit any changes. */
102 static bool dry_run;
103
104 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
105 static bool wait_for_reload = true;
106
107 /* --timeout: Time to wait for a connection to 'db'. */
108 static int timeout;
109
110 /* All supported commands. */
111 static const struct vsctl_command_syntax all_commands[];
112
113 /* The IDL we're using and the current transaction, if any.
114  * This is for use by vsctl_exit() only, to allow it to clean up.
115  * Other code should use its context arguments. */
116 static struct ovsdb_idl *the_idl;
117 static struct ovsdb_idl_txn *the_idl_txn;
118
119 static void vsctl_exit(int status) NO_RETURN;
120 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
121 static char *default_db(void);
122 static void usage(void) NO_RETURN;
123 static void parse_options(int argc, char *argv[]);
124 static bool might_write_to_db(char **argv);
125
126 static struct vsctl_command *parse_commands(int argc, char *argv[],
127                                             size_t *n_commandsp);
128 static void parse_command(int argc, char *argv[], struct vsctl_command *);
129 static const struct vsctl_command_syntax *find_command(const char *name);
130 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
131                               struct ovsdb_idl *);
132 static void do_vsctl(const char *args,
133                      struct vsctl_command *, size_t n_commands,
134                      struct ovsdb_idl *);
135
136 static const struct vsctl_table_class *get_table(const char *table_name);
137 static void set_column(const struct vsctl_table_class *,
138                        const struct ovsdb_idl_row *, const char *arg,
139                        struct ovsdb_symbol_table *);
140
141 int
142 main(int argc, char *argv[])
143 {
144     extern struct vlog_module VLM_reconnect;
145     struct ovsdb_idl *idl;
146     struct vsctl_command *commands;
147     size_t n_commands;
148     char *args;
149
150     set_program_name(argv[0]);
151     signal(SIGPIPE, SIG_IGN);
152     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
153     vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
154     ovsrec_init();
155
156     /* Log our arguments.  This is often valuable for debugging systems. */
157     args = process_escape_args(argv);
158     VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
159
160     /* Parse command line. */
161     parse_options(argc, argv);
162     commands = parse_commands(argc - optind, argv + optind, &n_commands);
163
164     if (timeout) {
165         time_alarm(timeout);
166     }
167
168     /* Initialize IDL. */
169     idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
170     run_prerequisites(commands, n_commands, idl);
171
172     /* Now execute the commands. */
173     for (;;) {
174         if (ovsdb_idl_run(idl)) {
175             do_vsctl(args, commands, n_commands, idl);
176         }
177
178         ovsdb_idl_wait(idl);
179         poll_block();
180     }
181 }
182
183 static void
184 parse_options(int argc, char *argv[])
185 {
186     enum {
187         OPT_DB = UCHAR_MAX + 1,
188         OPT_ONELINE,
189         OPT_NO_SYSLOG,
190         OPT_NO_WAIT,
191         OPT_DRY_RUN,
192         OPT_PEER_CA_CERT,
193         VLOG_OPTION_ENUMS
194     };
195     static struct option long_options[] = {
196         {"db", required_argument, 0, OPT_DB},
197         {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
198         {"no-wait", no_argument, 0, OPT_NO_WAIT},
199         {"dry-run", no_argument, 0, OPT_DRY_RUN},
200         {"oneline", no_argument, 0, OPT_ONELINE},
201         {"timeout", required_argument, 0, 't'},
202         {"help", no_argument, 0, 'h'},
203         {"version", no_argument, 0, 'V'},
204         VLOG_LONG_OPTIONS,
205 #ifdef HAVE_OPENSSL
206         STREAM_SSL_LONG_OPTIONS
207         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
208 #endif
209         {0, 0, 0, 0},
210     };
211     char *tmp, *short_options;
212
213     tmp = long_options_to_short_options(long_options);
214     short_options = xasprintf("+%s", tmp);
215     free(tmp);
216
217     for (;;) {
218         int c;
219
220         c = getopt_long(argc, argv, short_options, long_options, NULL);
221         if (c == -1) {
222             break;
223         }
224
225         switch (c) {
226         case OPT_DB:
227             db = optarg;
228             break;
229
230         case OPT_ONELINE:
231             oneline = true;
232             break;
233
234         case OPT_NO_SYSLOG:
235             vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
236             break;
237
238         case OPT_NO_WAIT:
239             wait_for_reload = false;
240             break;
241
242         case OPT_DRY_RUN:
243             dry_run = true;
244             break;
245
246         case 'h':
247             usage();
248
249         case 'V':
250             OVS_PRINT_VERSION(0, 0);
251             exit(EXIT_SUCCESS);
252
253         case 't':
254             timeout = strtoul(optarg, NULL, 10);
255             if (timeout < 0) {
256                 vsctl_fatal("value %s on -t or --timeout is invalid",
257                             optarg);
258             }
259             break;
260
261         VLOG_OPTION_HANDLERS
262
263 #ifdef HAVE_OPENSSL
264         STREAM_SSL_OPTION_HANDLERS
265
266         case OPT_PEER_CA_CERT:
267             stream_ssl_set_peer_ca_cert_file(optarg);
268             break;
269 #endif
270
271         case '?':
272             exit(EXIT_FAILURE);
273
274         default:
275             abort();
276         }
277     }
278     free(short_options);
279
280     if (!db) {
281         db = default_db();
282     }
283 }
284
285 static struct vsctl_command *
286 parse_commands(int argc, char *argv[], size_t *n_commandsp)
287 {
288     struct vsctl_command *commands;
289     size_t n_commands, allocated_commands;
290     int i, start;
291
292     commands = NULL;
293     n_commands = allocated_commands = 0;
294
295     for (start = i = 0; i <= argc; i++) {
296         if (i == argc || !strcmp(argv[i], "--")) {
297             if (i > start) {
298                 if (n_commands >= allocated_commands) {
299                     struct vsctl_command *c;
300
301                     commands = x2nrealloc(commands, &allocated_commands,
302                                           sizeof *commands);
303                     for (c = commands; c < &commands[n_commands]; c++) {
304                         shash_moved(&c->options);
305                     }
306                 }
307                 parse_command(i - start, &argv[start],
308                               &commands[n_commands++]);
309             }
310             start = i + 1;
311         }
312     }
313     if (!n_commands) {
314         vsctl_fatal("missing command name (use --help for help)");
315     }
316     *n_commandsp = n_commands;
317     return commands;
318 }
319
320 static void
321 parse_command(int argc, char *argv[], struct vsctl_command *command)
322 {
323     const struct vsctl_command_syntax *p;
324     struct shash_node *node;
325     int n_arg;
326     int i;
327
328     shash_init(&command->options);
329     for (i = 0; i < argc; i++) {
330         const char *option = argv[i];
331         const char *equals;
332         char *key, *value;
333
334         if (option[0] != '-') {
335             break;
336         }
337
338         equals = strchr(option, '=');
339         if (equals) {
340             key = xmemdup0(option, equals - option);
341             value = xstrdup(equals + 1);
342         } else {
343             key = xstrdup(option);
344             value = NULL;
345         }
346
347         if (shash_find(&command->options, key)) {
348             vsctl_fatal("'%s' option specified multiple times", argv[i]);
349         }
350         shash_add_nocopy(&command->options, key, value);
351     }
352     if (i == argc) {
353         vsctl_fatal("missing command name");
354     }
355
356     p = find_command(argv[i]);
357     if (!p) {
358         vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
359     }
360
361     SHASH_FOR_EACH (node, &command->options) {
362         const char *s = strstr(p->options, node->name);
363         int end = s ? s[strlen(node->name)] : EOF;
364
365         if (end != '=' && end != ',' && end != ' ' && end != '\0') {
366             vsctl_fatal("'%s' command has no '%s' option",
367                         argv[i], node->name);
368         }
369         if ((end == '=') != (node->data != NULL)) {
370             if (end == '=') {
371                 vsctl_fatal("missing argument to '%s' option on '%s' "
372                             "command", node->name, argv[i]);
373             } else {
374                 vsctl_fatal("'%s' option on '%s' does not accept an "
375                             "argument", node->name, argv[i]);
376             }
377         }
378     }
379
380     n_arg = argc - i - 1;
381     if (n_arg < p->min_args) {
382         vsctl_fatal("'%s' command requires at least %d arguments",
383                     p->name, p->min_args);
384     } else if (n_arg > p->max_args) {
385         int j;
386
387         for (j = i + 1; j < argc; j++) {
388             if (argv[j][0] == '-') {
389                 vsctl_fatal("'%s' command takes at most %d arguments "
390                             "(note that options must precede command "
391                             "names and follow a \"--\" argument)",
392                             p->name, p->max_args);
393             }
394         }
395
396         vsctl_fatal("'%s' command takes at most %d arguments",
397                     p->name, p->max_args);
398     }
399
400     command->syntax = p;
401     command->argc = n_arg + 1;
402     command->argv = &argv[i];
403 }
404
405 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
406  * null pointer if there is none. */
407 static const struct vsctl_command_syntax *
408 find_command(const char *name)
409 {
410     static struct shash commands = SHASH_INITIALIZER(&commands);
411
412     if (shash_is_empty(&commands)) {
413         const struct vsctl_command_syntax *p;
414
415         for (p = all_commands; p->name; p++) {
416             shash_add_assert(&commands, p->name, p);
417         }
418     }
419
420     return shash_find_data(&commands, name);
421 }
422
423 static void
424 vsctl_fatal(const char *format, ...)
425 {
426     char *message;
427     va_list args;
428
429     va_start(args, format);
430     message = xvasprintf(format, args);
431     va_end(args);
432
433     vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
434     VLOG_ERR("%s", message);
435     ovs_error(0, "%s", message);
436     vsctl_exit(EXIT_FAILURE);
437 }
438
439 /* Frees the current transaction and the underlying IDL and then calls
440  * exit(status).
441  *
442  * Freeing the transaction and the IDL is not strictly necessary, but it makes
443  * for a clean memory leak report from valgrind in the normal case.  That makes
444  * it easier to notice real memory leaks. */
445 static void
446 vsctl_exit(int status)
447 {
448     if (the_idl_txn) {
449         ovsdb_idl_txn_abort(the_idl_txn);
450         ovsdb_idl_txn_destroy(the_idl_txn);
451     }
452     ovsdb_idl_destroy(the_idl);
453     exit(status);
454 }
455
456 static void
457 usage(void)
458 {
459     printf("\
460 %s: ovs-vswitchd management utility\n\
461 usage: %s [OPTIONS] COMMAND [ARG...]\n\
462 \n\
463 Bridge commands:\n\
464   add-br BRIDGE               create a new bridge named BRIDGE\n\
465   add-br BRIDGE PARENT VLAN   create new fake BRIDGE in PARENT on VLAN\n\
466   del-br BRIDGE               delete BRIDGE and all of its ports\n\
467   list-br                     print the names of all the bridges\n\
468   br-exists BRIDGE            test whether BRIDGE exists\n\
469   br-to-vlan BRIDGE           print the VLAN which BRIDGE is on\n\
470   br-to-parent BRIDGE         print the parent of BRIDGE\n\
471   br-set-external-id BRIDGE KEY VALUE  set KEY on BRIDGE to VALUE\n\
472   br-set-external-id BRIDGE KEY  unset KEY on BRIDGE\n\
473   br-get-external-id BRIDGE KEY  print value of KEY on BRIDGE\n\
474   br-get-external-id BRIDGE  list key-value pairs on BRIDGE\n\
475 \n\
476 Port commands:\n\
477   list-ports BRIDGE           print the names of all the ports on BRIDGE\n\
478   add-port BRIDGE PORT        add network device PORT to BRIDGE\n\
479   add-bond BRIDGE PORT IFACE...  add bonded port PORT in BRIDGE from IFACES\n\
480   del-port [BRIDGE] PORT      delete PORT (which may be bonded) from BRIDGE\n\
481   port-to-br PORT             print name of bridge that contains PORT\n\
482 A bond is considered to be a single port.\n\
483 \n\
484 Interface commands (a bond consists of multiple interfaces):\n\
485   list-ifaces BRIDGE          print the names of all interfaces on BRIDGE\n\
486   iface-to-br IFACE           print name of bridge that contains IFACE\n\
487 \n\
488 Controller commands:\n\
489   get-controller BRIDGE      print the controller for BRIDGE\n\
490   del-controller BRIDGE      delete the controller for BRIDGE\n\
491   set-controller BRIDGE TARGET  set the controller for BRIDGE to TARGET\n\
492   get-fail-mode BRIDGE       print the fail-mode for BRIDGE\n\
493   del-fail-mode BRIDGE       delete the fail-mode for BRIDGE\n\
494   set-fail-mode BRIDGE MODE  set the fail-mode for BRIDGE to MODE\n\
495 \n\
496 Manager commands:\n\
497   get-manager                print all manager(s)\n\
498   del-manager                delete all manager(s)\n\
499   set-manager TARGET...      set the list of manager(s) to TARGET(s)\n\
500 \n\
501 SSL commands:\n\
502   get-ssl                     print the SSL configuration\n\
503   del-ssl                     delete the SSL configuration\n\
504   set-ssl PRIV-KEY CERT CA-CERT  set the SSL configuration\n\
505 \n\
506 Switch commands:\n\
507   emer-reset                  reset switch to known good state\n\
508 \n\
509 Database commands:\n\
510   list TBL [REC]              list RECord (or all records) in TBL\n\
511   get TBL REC COL[:KEY]       print values of COLumns in RECord in TBL\n\
512   set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
513   add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
514   remove TBL REC COL [KEY=]VALUE  remove (KEY=)VALUE from COLumn\n\
515   clear TBL REC COL           clear values from COLumn in RECord in TBL\n\
516   create TBL COL[:KEY]=VALUE  create and initialize new record\n\
517   destroy TBL REC             delete RECord from TBL\n\
518   wait-until TBL REC [COL[:KEY]=VALUE]  wait until condition is true\n\
519 Potentially unsafe database commands require --force option.\n\
520 \n\
521 Options:\n\
522   --db=DATABASE               connect to DATABASE\n\
523                               (default: %s)\n\
524   --oneline                   print exactly one line of output per command\n",
525            program_name, program_name, default_db());
526     vlog_usage();
527     printf("\n\
528 Other options:\n\
529   -h, --help                  display this help message\n\
530   -V, --version               display version information\n");
531     exit(EXIT_SUCCESS);
532 }
533
534 static char *
535 default_db(void)
536 {
537     static char *def;
538     if (!def) {
539         def = xasprintf("unix:%s/db.sock", ovs_rundir());
540     }
541     return def;
542 }
543
544 /* Returns true if it looks like this set of arguments might modify the
545  * database, otherwise false.  (Not very smart, so it's prone to false
546  * positives.) */
547 static bool
548 might_write_to_db(char **argv)
549 {
550     for (; *argv; argv++) {
551         const struct vsctl_command_syntax *p = find_command(*argv);
552         if (p && p->mode == RW) {
553             return true;
554         }
555     }
556     return false;
557 }
558 \f
559 struct vsctl_context {
560     /* Read-only. */
561     int argc;
562     char **argv;
563     struct shash options;
564
565     /* Modifiable state. */
566     struct ds output;
567     struct ovsdb_idl *idl;
568     struct ovsdb_idl_txn *txn;
569     struct ovsdb_symbol_table *symtab;
570     const struct ovsrec_open_vswitch *ovs;
571     bool verified_ports;
572
573     /* A command may set this member to true if some prerequisite is not met
574      * and the caller should wait for something to change and then retry. */
575     bool try_again;
576 };
577
578 struct vsctl_bridge {
579     struct ovsrec_bridge *br_cfg;
580     char *name;
581     struct ovsrec_controller **ctrl;
582     char *fail_mode;
583     size_t n_ctrl;
584     struct vsctl_bridge *parent;
585     int vlan;
586 };
587
588 struct vsctl_port {
589     struct ovsrec_port *port_cfg;
590     struct vsctl_bridge *bridge;
591 };
592
593 struct vsctl_iface {
594     struct ovsrec_interface *iface_cfg;
595     struct vsctl_port *port;
596 };
597
598 struct vsctl_info {
599     struct vsctl_context *ctx;
600     struct shash bridges;   /* Maps from bridge name to struct vsctl_bridge. */
601     struct shash ports;     /* Maps from port name to struct vsctl_port. */
602     struct shash ifaces;    /* Maps from port name to struct vsctl_iface. */
603 };
604
605 static char *
606 vsctl_context_to_string(const struct vsctl_context *ctx)
607 {
608     const struct shash_node *node;
609     struct svec words;
610     char *s;
611     int i;
612
613     svec_init(&words);
614     SHASH_FOR_EACH (node, &ctx->options) {
615         svec_add(&words, node->name);
616     }
617     for (i = 0; i < ctx->argc; i++) {
618         svec_add(&words, ctx->argv[i]);
619     }
620     svec_terminate(&words);
621
622     s = process_escape_args(words.names);
623
624     svec_destroy(&words);
625
626     return s;
627 }
628
629 static void
630 verify_ports(struct vsctl_context *ctx)
631 {
632     if (!ctx->verified_ports) {
633         const struct ovsrec_bridge *bridge;
634         const struct ovsrec_port *port;
635
636         ovsrec_open_vswitch_verify_bridges(ctx->ovs);
637         OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
638             ovsrec_bridge_verify_ports(bridge);
639         }
640         OVSREC_PORT_FOR_EACH (port, ctx->idl) {
641             ovsrec_port_verify_interfaces(port);
642         }
643
644         ctx->verified_ports = true;
645     }
646 }
647
648 static struct vsctl_bridge *
649 add_bridge(struct vsctl_info *b,
650            struct ovsrec_bridge *br_cfg, const char *name,
651            struct vsctl_bridge *parent, int vlan)
652 {
653     struct vsctl_bridge *br = xmalloc(sizeof *br);
654     br->br_cfg = br_cfg;
655     br->name = xstrdup(name);
656     br->parent = parent;
657     br->vlan = vlan;
658     if (parent) {
659         br->ctrl = parent->br_cfg->controller;
660         br->n_ctrl = parent->br_cfg->n_controller;
661         br->fail_mode = parent->br_cfg->fail_mode;
662     } else {
663         br->ctrl = br_cfg->controller;
664         br->n_ctrl = br_cfg->n_controller;
665         br->fail_mode = br_cfg->fail_mode;
666     }
667     shash_add(&b->bridges, br->name, br);
668     return br;
669 }
670
671 static bool
672 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
673 {
674     return (port_cfg->fake_bridge
675             && port_cfg->tag
676             && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
677 }
678
679 static struct vsctl_bridge *
680 find_vlan_bridge(struct vsctl_info *info,
681                  struct vsctl_bridge *parent, int vlan)
682 {
683     struct shash_node *node;
684
685     SHASH_FOR_EACH (node, &info->bridges) {
686         struct vsctl_bridge *br = node->data;
687         if (br->parent == parent && br->vlan == vlan) {
688             return br;
689         }
690     }
691
692     return NULL;
693 }
694
695 static void
696 free_info(struct vsctl_info *info)
697 {
698     struct shash_node *node;
699
700     SHASH_FOR_EACH (node, &info->bridges) {
701         struct vsctl_bridge *bridge = node->data;
702         free(bridge->name);
703         free(bridge);
704     }
705     shash_destroy(&info->bridges);
706
707     shash_destroy_free_data(&info->ports);
708     shash_destroy_free_data(&info->ifaces);
709 }
710
711 static void
712 pre_get_info(struct vsctl_context *ctx)
713 {
714     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
715
716     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
717     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
718     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
719     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
720
721     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
722     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
723     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
724     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
725
726     ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
727 }
728
729 static void
730 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
731 {
732     const struct ovsrec_open_vswitch *ovs = ctx->ovs;
733     struct shash bridges, ports;
734     size_t i;
735
736     info->ctx = ctx;
737     shash_init(&info->bridges);
738     shash_init(&info->ports);
739     shash_init(&info->ifaces);
740
741     shash_init(&bridges);
742     shash_init(&ports);
743     for (i = 0; i < ovs->n_bridges; i++) {
744         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
745         struct vsctl_bridge *br;
746         size_t j;
747
748         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
749             VLOG_WARN("%s: database contains duplicate bridge name",
750                       br_cfg->name);
751             continue;
752         }
753         br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
754         if (!br) {
755             continue;
756         }
757
758         for (j = 0; j < br_cfg->n_ports; j++) {
759             struct ovsrec_port *port_cfg = br_cfg->ports[j];
760
761             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
762                 VLOG_WARN("%s: database contains duplicate port name",
763                           port_cfg->name);
764                 continue;
765             }
766
767             if (port_is_fake_bridge(port_cfg)
768                 && shash_add_once(&bridges, port_cfg->name, NULL)) {
769                 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
770             }
771         }
772     }
773     shash_destroy(&bridges);
774     shash_destroy(&ports);
775
776     shash_init(&bridges);
777     shash_init(&ports);
778     for (i = 0; i < ovs->n_bridges; i++) {
779         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
780         struct vsctl_bridge *br;
781         size_t j;
782
783         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
784             continue;
785         }
786         br = shash_find_data(&info->bridges, br_cfg->name);
787         for (j = 0; j < br_cfg->n_ports; j++) {
788             struct ovsrec_port *port_cfg = br_cfg->ports[j];
789             struct vsctl_port *port;
790             size_t k;
791
792             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
793                 continue;
794             }
795
796             if (port_is_fake_bridge(port_cfg)
797                 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
798                 continue;
799             }
800
801             port = xmalloc(sizeof *port);
802             port->port_cfg = port_cfg;
803             if (port_cfg->tag
804                 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
805                 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
806                 if (!port->bridge) {
807                     port->bridge = br;
808                 }
809             } else {
810                 port->bridge = br;
811             }
812             shash_add(&info->ports, port_cfg->name, port);
813
814             for (k = 0; k < port_cfg->n_interfaces; k++) {
815                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
816                 struct vsctl_iface *iface;
817
818                 if (shash_find(&info->ifaces, iface_cfg->name)) {
819                     VLOG_WARN("%s: database contains duplicate interface name",
820                               iface_cfg->name);
821                     continue;
822                 }
823
824                 iface = xmalloc(sizeof *iface);
825                 iface->iface_cfg = iface_cfg;
826                 iface->port = port;
827                 shash_add(&info->ifaces, iface_cfg->name, iface);
828             }
829         }
830     }
831     shash_destroy(&bridges);
832     shash_destroy(&ports);
833 }
834
835 static void
836 check_conflicts(struct vsctl_info *info, const char *name,
837                 char *msg)
838 {
839     struct vsctl_iface *iface;
840     struct vsctl_port *port;
841
842     verify_ports(info->ctx);
843
844     if (shash_find(&info->bridges, name)) {
845         vsctl_fatal("%s because a bridge named %s already exists",
846                     msg, name);
847     }
848
849     port = shash_find_data(&info->ports, name);
850     if (port) {
851         vsctl_fatal("%s because a port named %s already exists on "
852                     "bridge %s", msg, name, port->bridge->name);
853     }
854
855     iface = shash_find_data(&info->ifaces, name);
856     if (iface) {
857         vsctl_fatal("%s because an interface named %s already exists "
858                     "on bridge %s", msg, name, iface->port->bridge->name);
859     }
860
861     free(msg);
862 }
863
864 static struct vsctl_bridge *
865 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
866 {
867     struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
868     if (must_exist && !br) {
869         vsctl_fatal("no bridge named %s", name);
870     }
871     ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
872     return br;
873 }
874
875 static struct vsctl_bridge *
876 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
877 {
878     struct vsctl_bridge *br = find_bridge(info, name, must_exist);
879     if (br && br->parent) {
880         vsctl_fatal("%s is a fake bridge", name);
881     }
882     return br;
883 }
884
885 static struct vsctl_port *
886 find_port(struct vsctl_info *info, const char *name, bool must_exist)
887 {
888     struct vsctl_port *port = shash_find_data(&info->ports, name);
889     if (port && !strcmp(name, port->bridge->name)) {
890         port = NULL;
891     }
892     if (must_exist && !port) {
893         vsctl_fatal("no port named %s", name);
894     }
895     verify_ports(info->ctx);
896     return port;
897 }
898
899 static struct vsctl_iface *
900 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
901 {
902     struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
903     if (iface && !strcmp(name, iface->port->bridge->name)) {
904         iface = NULL;
905     }
906     if (must_exist && !iface) {
907         vsctl_fatal("no interface named %s", name);
908     }
909     verify_ports(info->ctx);
910     return iface;
911 }
912
913 static void
914 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
915 {
916     struct ovsrec_port **ports;
917     size_t i;
918
919     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
920     for (i = 0; i < br->n_ports; i++) {
921         ports[i] = br->ports[i];
922     }
923     ports[br->n_ports] = port;
924     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
925     free(ports);
926 }
927
928 static void
929 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
930 {
931     struct ovsrec_port **ports;
932     size_t i, n;
933
934     ports = xmalloc(sizeof *br->ports * br->n_ports);
935     for (i = n = 0; i < br->n_ports; i++) {
936         if (br->ports[i] != port) {
937             ports[n++] = br->ports[i];
938         }
939     }
940     ovsrec_bridge_set_ports(br, ports, n);
941     free(ports);
942 }
943
944 static void
945 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
946                   struct ovsrec_bridge *bridge)
947 {
948     struct ovsrec_bridge **bridges;
949     size_t i;
950
951     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
952     for (i = 0; i < ovs->n_bridges; i++) {
953         bridges[i] = ovs->bridges[i];
954     }
955     bridges[ovs->n_bridges] = bridge;
956     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
957     free(bridges);
958 }
959
960 static void
961 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
962                   struct ovsrec_bridge *bridge)
963 {
964     struct ovsrec_bridge **bridges;
965     size_t i, n;
966
967     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
968     for (i = n = 0; i < ovs->n_bridges; i++) {
969         if (ovs->bridges[i] != bridge) {
970             bridges[n++] = ovs->bridges[i];
971         }
972     }
973     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
974     free(bridges);
975 }
976
977 static void
978 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
979 {
980 }
981
982 static void
983 pre_cmd_emer_reset(struct vsctl_context *ctx)
984 {
985     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers);
986     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
987     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
988
989     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
990     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
991     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
992     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
993     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
994     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
995     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
996
997     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
998
999     ovsdb_idl_add_column(ctx->idl,
1000                           &ovsrec_interface_col_ingress_policing_rate);
1001     ovsdb_idl_add_column(ctx->idl,
1002                           &ovsrec_interface_col_ingress_policing_burst);
1003 }
1004
1005 static void
1006 cmd_emer_reset(struct vsctl_context *ctx)
1007 {
1008     const struct ovsdb_idl *idl = ctx->idl;
1009     const struct ovsrec_bridge *br;
1010     const struct ovsrec_port *port;
1011     const struct ovsrec_interface *iface;
1012     const struct ovsrec_mirror *mirror, *next_mirror;
1013     const struct ovsrec_controller *ctrl, *next_ctrl;
1014     const struct ovsrec_manager *mgr, *next_mgr;
1015     const struct ovsrec_netflow *nf, *next_nf;
1016     const struct ovsrec_ssl *ssl, *next_ssl;
1017     const struct ovsrec_sflow *sflow, *next_sflow;
1018
1019     /* Reset the Open_vSwitch table. */
1020     ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
1021     ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1022     ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1023
1024     OVSREC_BRIDGE_FOR_EACH (br, idl) {
1025         int i;
1026         char *hw_key = "hwaddr";
1027         char *hw_val = NULL;
1028
1029         ovsrec_bridge_set_controller(br, NULL, 0);
1030         ovsrec_bridge_set_fail_mode(br, NULL);
1031         ovsrec_bridge_set_mirrors(br, NULL, 0);
1032         ovsrec_bridge_set_netflow(br, NULL);
1033         ovsrec_bridge_set_sflow(br, NULL);
1034         ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1035
1036         /* We only want to save the "hwaddr" key from other_config. */
1037         for (i=0; i < br->n_other_config; i++) {
1038             if (!strcmp(br->key_other_config[i], hw_key)) {
1039                 hw_val = br->value_other_config[i];
1040                 break;
1041             }
1042         }
1043         if (hw_val) {
1044             char *val = xstrdup(hw_val);
1045             ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1046             free(val);
1047         } else {
1048             ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1049         }
1050     }
1051
1052     OVSREC_PORT_FOR_EACH (port, idl) {
1053         ovsrec_port_set_other_config(port, NULL, NULL, 0);
1054     }
1055
1056     OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1057         /* xxx What do we do about gre/patch devices created by mgr? */
1058
1059         ovsrec_interface_set_ingress_policing_rate(iface, 0);
1060         ovsrec_interface_set_ingress_policing_burst(iface, 0);
1061     }
1062
1063     OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1064         ovsrec_mirror_delete(mirror);
1065     }
1066
1067     OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1068         ovsrec_controller_delete(ctrl);
1069     }
1070
1071     OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1072         ovsrec_manager_delete(mgr);
1073     }
1074
1075     OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1076         ovsrec_netflow_delete(nf);
1077     }
1078
1079     OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1080         ovsrec_ssl_delete(ssl);
1081     }
1082
1083     OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1084         ovsrec_sflow_delete(sflow);
1085     }
1086 }
1087
1088 static void
1089 cmd_add_br(struct vsctl_context *ctx)
1090 {
1091     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1092     const char *br_name, *parent_name;
1093     struct vsctl_info info;
1094     int vlan;
1095
1096     br_name = ctx->argv[1];
1097     if (ctx->argc == 2) {
1098         parent_name = NULL;
1099         vlan = 0;
1100     } else if (ctx->argc == 4) {
1101         parent_name = ctx->argv[2];
1102         vlan = atoi(ctx->argv[3]);
1103         if (vlan < 1 || vlan > 4095) {
1104             vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1105         }
1106     } else {
1107         vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1108                     ctx->argv[0]);
1109     }
1110
1111     get_info(ctx, &info);
1112     if (may_exist) {
1113         struct vsctl_bridge *br;
1114
1115         br = find_bridge(&info, br_name, false);
1116         if (br) {
1117             if (!parent_name) {
1118                 if (br->parent) {
1119                     vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1120                                 "a VLAN bridge for VLAN %d",
1121                                 br_name, br_name, br->vlan);
1122                 }
1123             } else {
1124                 if (!br->parent) {
1125                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1126                                 "is not a VLAN bridge",
1127                                 br_name, parent_name, vlan, br_name);
1128                 } else if (strcmp(br->parent->name, parent_name)) {
1129                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1130                                 "has the wrong parent %s",
1131                                 br_name, parent_name, vlan,
1132                                 br_name, br->parent->name);
1133                 } else if (br->vlan != vlan) {
1134                     vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1135                                 "is a VLAN bridge for the wrong VLAN %d",
1136                                 br_name, parent_name, vlan, br_name, br->vlan);
1137                 }
1138             }
1139             return;
1140         }
1141     }
1142     check_conflicts(&info, br_name,
1143                     xasprintf("cannot create a bridge named %s", br_name));
1144
1145     if (!parent_name) {
1146         struct ovsrec_port *port;
1147         struct ovsrec_interface *iface;
1148         struct ovsrec_bridge *br;
1149
1150         iface = ovsrec_interface_insert(ctx->txn);
1151         ovsrec_interface_set_name(iface, br_name);
1152         ovsrec_interface_set_type(iface, "internal");
1153
1154         port = ovsrec_port_insert(ctx->txn);
1155         ovsrec_port_set_name(port, br_name);
1156         ovsrec_port_set_interfaces(port, &iface, 1);
1157
1158         br = ovsrec_bridge_insert(ctx->txn);
1159         ovsrec_bridge_set_name(br, br_name);
1160         ovsrec_bridge_set_ports(br, &port, 1);
1161
1162         ovs_insert_bridge(ctx->ovs, br);
1163     } else {
1164         struct vsctl_bridge *parent;
1165         struct ovsrec_port *port;
1166         struct ovsrec_interface *iface;
1167         struct ovsrec_bridge *br;
1168         int64_t tag = vlan;
1169
1170         parent = find_bridge(&info, parent_name, false);
1171         if (parent && parent->vlan) {
1172             vsctl_fatal("cannot create bridge with fake bridge as parent");
1173         }
1174         if (!parent) {
1175             vsctl_fatal("parent bridge %s does not exist", parent_name);
1176         }
1177         br = parent->br_cfg;
1178
1179         iface = ovsrec_interface_insert(ctx->txn);
1180         ovsrec_interface_set_name(iface, br_name);
1181         ovsrec_interface_set_type(iface, "internal");
1182
1183         port = ovsrec_port_insert(ctx->txn);
1184         ovsrec_port_set_name(port, br_name);
1185         ovsrec_port_set_interfaces(port, &iface, 1);
1186         ovsrec_port_set_fake_bridge(port, true);
1187         ovsrec_port_set_tag(port, &tag, 1);
1188
1189         bridge_insert_port(br, port);
1190     }
1191
1192     free_info(&info);
1193 }
1194
1195 static void
1196 del_port(struct vsctl_info *info, struct vsctl_port *port)
1197 {
1198     struct shash_node *node;
1199
1200     SHASH_FOR_EACH (node, &info->ifaces) {
1201         struct vsctl_iface *iface = node->data;
1202         if (iface->port == port) {
1203             ovsrec_interface_delete(iface->iface_cfg);
1204         }
1205     }
1206     ovsrec_port_delete(port->port_cfg);
1207
1208     bridge_delete_port((port->bridge->parent
1209                         ? port->bridge->parent->br_cfg
1210                         : port->bridge->br_cfg), port->port_cfg);
1211 }
1212
1213 static void
1214 cmd_del_br(struct vsctl_context *ctx)
1215 {
1216     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1217     struct vsctl_bridge *bridge;
1218     struct vsctl_info info;
1219
1220     get_info(ctx, &info);
1221     bridge = find_bridge(&info, ctx->argv[1], must_exist);
1222     if (bridge) {
1223         struct shash_node *node;
1224
1225         SHASH_FOR_EACH (node, &info.ports) {
1226             struct vsctl_port *port = node->data;
1227             if (port->bridge == bridge || port->bridge->parent == bridge
1228                 || !strcmp(port->port_cfg->name, bridge->name)) {
1229                 del_port(&info, port);
1230             }
1231         }
1232         if (bridge->br_cfg) {
1233             ovsrec_bridge_delete(bridge->br_cfg);
1234             ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1235         }
1236     }
1237     free_info(&info);
1238 }
1239
1240 static void
1241 output_sorted(struct svec *svec, struct ds *output)
1242 {
1243     const char *name;
1244     size_t i;
1245
1246     svec_sort(svec);
1247     SVEC_FOR_EACH (i, name, svec) {
1248         ds_put_format(output, "%s\n", name);
1249     }
1250 }
1251
1252 static void
1253 cmd_list_br(struct vsctl_context *ctx)
1254 {
1255     struct shash_node *node;
1256     struct vsctl_info info;
1257     struct svec bridges;
1258
1259     get_info(ctx, &info);
1260
1261     svec_init(&bridges);
1262     SHASH_FOR_EACH (node, &info.bridges) {
1263         struct vsctl_bridge *br = node->data;
1264         svec_add(&bridges, br->name);
1265     }
1266     output_sorted(&bridges, &ctx->output);
1267     svec_destroy(&bridges);
1268
1269     free_info(&info);
1270 }
1271
1272 static void
1273 cmd_br_exists(struct vsctl_context *ctx)
1274 {
1275     struct vsctl_info info;
1276
1277     get_info(ctx, &info);
1278     if (!find_bridge(&info, ctx->argv[1], false)) {
1279         vsctl_exit(2);
1280     }
1281     free_info(&info);
1282 }
1283
1284 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1285  * equals 'a', false otherwise. */
1286 static bool
1287 key_matches(const char *a,
1288             const char *b_prefix, size_t b_prefix_len, const char *b)
1289 {
1290     return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1291 }
1292
1293 static void
1294 set_external_id(char **old_keys, char **old_values, size_t old_n,
1295                 char *key, char *value,
1296                 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1297 {
1298     char **new_keys;
1299     char **new_values;
1300     size_t new_n;
1301     size_t i;
1302
1303     new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1304     new_values = xmalloc(sizeof *new_values * (old_n + 1));
1305     new_n = 0;
1306     for (i = 0; i < old_n; i++) {
1307         if (strcmp(key, old_keys[i])) {
1308             new_keys[new_n] = old_keys[i];
1309             new_values[new_n] = old_values[i];
1310             new_n++;
1311         }
1312     }
1313     if (value) {
1314         new_keys[new_n] = key;
1315         new_values[new_n] = value;
1316         new_n++;
1317     }
1318     *new_keysp = new_keys;
1319     *new_valuesp = new_values;
1320     *new_np = new_n;
1321 }
1322
1323 static void
1324 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1325 {
1326     pre_get_info(ctx);
1327     ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1328     ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1329 }
1330
1331 static void
1332 cmd_br_set_external_id(struct vsctl_context *ctx)
1333 {
1334     struct vsctl_info info;
1335     struct vsctl_bridge *bridge;
1336     char **keys, **values;
1337     size_t n;
1338
1339     get_info(ctx, &info);
1340     bridge = find_bridge(&info, ctx->argv[1], true);
1341     if (bridge->br_cfg) {
1342         set_external_id(bridge->br_cfg->key_external_ids,
1343                         bridge->br_cfg->value_external_ids,
1344                         bridge->br_cfg->n_external_ids,
1345                         ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1346                         &keys, &values, &n);
1347         ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1348         ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1349     } else {
1350         char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1351         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1352         set_external_id(port->port_cfg->key_external_ids,
1353                         port->port_cfg->value_external_ids,
1354                         port->port_cfg->n_external_ids,
1355                         key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1356                         &keys, &values, &n);
1357         ovsrec_port_verify_external_ids(port->port_cfg);
1358         ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1359         free(key);
1360     }
1361     free(keys);
1362     free(values);
1363
1364     free_info(&info);
1365 }
1366
1367 static void
1368 get_external_id(char **keys, char **values, size_t n,
1369                 const char *prefix, const char *key,
1370                 struct ds *output)
1371 {
1372     size_t prefix_len = strlen(prefix);
1373     struct svec svec;
1374     size_t i;
1375
1376     svec_init(&svec);
1377     for (i = 0; i < n; i++) {
1378         if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1379             svec_add_nocopy(&svec, xasprintf("%s=%s",
1380                                              keys[i] + prefix_len, values[i]));
1381         } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1382             svec_add(&svec, values[i]);
1383             break;
1384         }
1385     }
1386     output_sorted(&svec, output);
1387     svec_destroy(&svec);
1388 }
1389
1390 static void
1391 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1392 {
1393     pre_cmd_br_set_external_id(ctx);
1394 }
1395
1396 static void
1397 cmd_br_get_external_id(struct vsctl_context *ctx)
1398 {
1399     struct vsctl_info info;
1400     struct vsctl_bridge *bridge;
1401
1402     get_info(ctx, &info);
1403     bridge = find_bridge(&info, ctx->argv[1], true);
1404     if (bridge->br_cfg) {
1405         ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1406         get_external_id(bridge->br_cfg->key_external_ids,
1407                         bridge->br_cfg->value_external_ids,
1408                         bridge->br_cfg->n_external_ids,
1409                         "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1410                         &ctx->output);
1411     } else {
1412         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1413         ovsrec_port_verify_external_ids(port->port_cfg);
1414         get_external_id(port->port_cfg->key_external_ids,
1415                         port->port_cfg->value_external_ids,
1416                         port->port_cfg->n_external_ids,
1417                         "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1418     }
1419     free_info(&info);
1420 }
1421
1422
1423 static void
1424 cmd_list_ports(struct vsctl_context *ctx)
1425 {
1426     struct vsctl_bridge *br;
1427     struct shash_node *node;
1428     struct vsctl_info info;
1429     struct svec ports;
1430
1431     get_info(ctx, &info);
1432     br = find_bridge(&info, ctx->argv[1], true);
1433     ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1434
1435     svec_init(&ports);
1436     SHASH_FOR_EACH (node, &info.ports) {
1437         struct vsctl_port *port = node->data;
1438
1439         if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1440             svec_add(&ports, port->port_cfg->name);
1441         }
1442     }
1443     output_sorted(&ports, &ctx->output);
1444     svec_destroy(&ports);
1445
1446     free_info(&info);
1447 }
1448
1449 static void
1450 add_port(struct vsctl_context *ctx,
1451          const char *br_name, const char *port_name,
1452          bool may_exist, bool fake_iface,
1453          char *iface_names[], int n_ifaces,
1454          char *settings[], int n_settings)
1455 {
1456     struct vsctl_info info;
1457     struct vsctl_bridge *bridge;
1458     struct ovsrec_interface **ifaces;
1459     struct ovsrec_port *port;
1460     size_t i;
1461
1462     get_info(ctx, &info);
1463     if (may_exist) {
1464         struct vsctl_port *vsctl_port;
1465
1466         vsctl_port = find_port(&info, port_name, false);
1467         if (vsctl_port) {
1468             struct svec want_names, have_names;
1469
1470             svec_init(&want_names);
1471             for (i = 0; i < n_ifaces; i++) {
1472                 svec_add(&want_names, iface_names[i]);
1473             }
1474             svec_sort(&want_names);
1475
1476             svec_init(&have_names);
1477             for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1478                 svec_add(&have_names,
1479                          vsctl_port->port_cfg->interfaces[i]->name);
1480             }
1481             svec_sort(&have_names);
1482
1483             if (strcmp(vsctl_port->bridge->name, br_name)) {
1484                 char *command = vsctl_context_to_string(ctx);
1485                 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1486                             command, port_name, vsctl_port->bridge->name);
1487             }
1488
1489             if (!svec_equal(&want_names, &have_names)) {
1490                 char *have_names_string = svec_join(&have_names, ", ", "");
1491                 char *command = vsctl_context_to_string(ctx);
1492
1493                 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1494                             command, port_name, have_names_string);
1495             }
1496
1497             svec_destroy(&want_names);
1498             svec_destroy(&have_names);
1499
1500             return;
1501         }
1502     }
1503     check_conflicts(&info, port_name,
1504                     xasprintf("cannot create a port named %s", port_name));
1505     for (i = 0; i < n_ifaces; i++) {
1506         check_conflicts(&info, iface_names[i],
1507                         xasprintf("cannot create an interface named %s",
1508                                   iface_names[i]));
1509     }
1510     bridge = find_bridge(&info, br_name, true);
1511
1512     ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1513     for (i = 0; i < n_ifaces; i++) {
1514         ifaces[i] = ovsrec_interface_insert(ctx->txn);
1515         ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1516     }
1517
1518     port = ovsrec_port_insert(ctx->txn);
1519     ovsrec_port_set_name(port, port_name);
1520     ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1521     ovsrec_port_set_bond_fake_iface(port, fake_iface);
1522     free(ifaces);
1523
1524     if (bridge->vlan) {
1525         int64_t tag = bridge->vlan;
1526         ovsrec_port_set_tag(port, &tag, 1);
1527     }
1528
1529     for (i = 0; i < n_settings; i++) {
1530         set_column(get_table("Port"), &port->header_, settings[i],
1531                    ctx->symtab);
1532     }
1533
1534     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1535                         : bridge->br_cfg), port);
1536
1537     free_info(&info);
1538 }
1539
1540 static void
1541 cmd_add_port(struct vsctl_context *ctx)
1542 {
1543     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1544
1545     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1546              &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1547 }
1548
1549 static void
1550 cmd_add_bond(struct vsctl_context *ctx)
1551 {
1552     bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1553     bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1554     int n_ifaces;
1555     int i;
1556
1557     n_ifaces = ctx->argc - 3;
1558     for (i = 3; i < ctx->argc; i++) {
1559         if (strchr(ctx->argv[i], '=')) {
1560             n_ifaces = i - 3;
1561             break;
1562         }
1563     }
1564     if (n_ifaces < 2) {
1565         vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1566                     "%d were specified", n_ifaces);
1567     }
1568
1569     add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1570              &ctx->argv[3], n_ifaces,
1571              &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1572 }
1573
1574 static void
1575 cmd_del_port(struct vsctl_context *ctx)
1576 {
1577     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1578     bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1579     struct vsctl_port *port;
1580     struct vsctl_info info;
1581
1582     get_info(ctx, &info);
1583     if (!with_iface) {
1584         port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1585     } else {
1586         const char *target = ctx->argv[ctx->argc - 1];
1587         struct vsctl_iface *iface;
1588
1589         port = find_port(&info, target, false);
1590         if (!port) {
1591             iface = find_iface(&info, target, false);
1592             if (iface) {
1593                 port = iface->port;
1594             }
1595         }
1596         if (must_exist && !port) {
1597             vsctl_fatal("no port or interface named %s", target);
1598         }
1599     }
1600
1601     if (port) {
1602         if (ctx->argc == 3) {
1603             struct vsctl_bridge *bridge;
1604
1605             bridge = find_bridge(&info, ctx->argv[1], true);
1606             if (port->bridge != bridge) {
1607                 if (port->bridge->parent == bridge) {
1608                     vsctl_fatal("bridge %s does not have a port %s (although "
1609                                 "its parent bridge %s does)",
1610                                 ctx->argv[1], ctx->argv[2],
1611                                 bridge->parent->name);
1612                 } else {
1613                     vsctl_fatal("bridge %s does not have a port %s",
1614                                 ctx->argv[1], ctx->argv[2]);
1615                 }
1616             }
1617         }
1618
1619         del_port(&info, port);
1620     }
1621
1622     free_info(&info);
1623 }
1624
1625 static void
1626 cmd_port_to_br(struct vsctl_context *ctx)
1627 {
1628     struct vsctl_port *port;
1629     struct vsctl_info info;
1630
1631     get_info(ctx, &info);
1632     port = find_port(&info, ctx->argv[1], true);
1633     ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1634     free_info(&info);
1635 }
1636
1637 static void
1638 cmd_br_to_vlan(struct vsctl_context *ctx)
1639 {
1640     struct vsctl_bridge *bridge;
1641     struct vsctl_info info;
1642
1643     get_info(ctx, &info);
1644     bridge = find_bridge(&info, ctx->argv[1], true);
1645     ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1646     free_info(&info);
1647 }
1648
1649 static void
1650 cmd_br_to_parent(struct vsctl_context *ctx)
1651 {
1652     struct vsctl_bridge *bridge;
1653     struct vsctl_info info;
1654
1655     get_info(ctx, &info);
1656     bridge = find_bridge(&info, ctx->argv[1], true);
1657     if (bridge->parent) {
1658         bridge = bridge->parent;
1659     }
1660     ds_put_format(&ctx->output, "%s\n", bridge->name);
1661     free_info(&info);
1662 }
1663
1664 static void
1665 cmd_list_ifaces(struct vsctl_context *ctx)
1666 {
1667     struct vsctl_bridge *br;
1668     struct shash_node *node;
1669     struct vsctl_info info;
1670     struct svec ifaces;
1671
1672     get_info(ctx, &info);
1673     br = find_bridge(&info, ctx->argv[1], true);
1674     verify_ports(ctx);
1675
1676     svec_init(&ifaces);
1677     SHASH_FOR_EACH (node, &info.ifaces) {
1678         struct vsctl_iface *iface = node->data;
1679
1680         if (strcmp(iface->iface_cfg->name, br->name)
1681             && br == iface->port->bridge) {
1682             svec_add(&ifaces, iface->iface_cfg->name);
1683         }
1684     }
1685     output_sorted(&ifaces, &ctx->output);
1686     svec_destroy(&ifaces);
1687
1688     free_info(&info);
1689 }
1690
1691 static void
1692 cmd_iface_to_br(struct vsctl_context *ctx)
1693 {
1694     struct vsctl_iface *iface;
1695     struct vsctl_info info;
1696
1697     get_info(ctx, &info);
1698     iface = find_iface(&info, ctx->argv[1], true);
1699     ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1700     free_info(&info);
1701 }
1702
1703 static void
1704 verify_controllers(struct ovsrec_bridge *bridge)
1705 {
1706     if (bridge) {
1707         size_t i;
1708
1709         ovsrec_bridge_verify_controller(bridge);
1710         for (i = 0; i < bridge->n_controller; i++) {
1711             ovsrec_controller_verify_target(bridge->controller[i]);
1712         }
1713     }
1714 }
1715
1716 static void
1717 pre_controller(struct vsctl_context *ctx)
1718 {
1719     pre_get_info(ctx);
1720
1721     ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1722 }
1723
1724 static void
1725 cmd_get_controller(struct vsctl_context *ctx)
1726 {
1727     struct vsctl_info info;
1728     struct vsctl_bridge *br;
1729     struct svec targets;
1730     size_t i;
1731
1732     get_info(ctx, &info);
1733     br = find_bridge(&info, ctx->argv[1], true);
1734     verify_controllers(br->br_cfg);
1735
1736     /* Print the targets in sorted order for reproducibility. */
1737     svec_init(&targets);
1738     for (i = 0; i < br->n_ctrl; i++) {
1739         svec_add(&targets, br->ctrl[i]->target);
1740     }
1741
1742     svec_sort(&targets);
1743     for (i = 0; i < targets.n; i++) {
1744         ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1745     }
1746     svec_destroy(&targets);
1747
1748     free_info(&info);
1749 }
1750
1751 static void
1752 delete_controllers(struct ovsrec_controller **controllers,
1753                    size_t n_controllers)
1754 {
1755     size_t i;
1756
1757     for (i = 0; i < n_controllers; i++) {
1758         ovsrec_controller_delete(controllers[i]);
1759     }
1760 }
1761
1762 static void
1763 cmd_del_controller(struct vsctl_context *ctx)
1764 {
1765     struct vsctl_info info;
1766     struct vsctl_bridge *br;
1767
1768     get_info(ctx, &info);
1769     br = find_real_bridge(&info, ctx->argv[1], true);
1770     verify_controllers(br->br_cfg);
1771
1772     if (br->ctrl) {
1773         delete_controllers(br->ctrl, br->n_ctrl);
1774         ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1775     }
1776
1777     free_info(&info);
1778 }
1779
1780 static struct ovsrec_controller **
1781 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1782 {
1783     struct ovsrec_controller **controllers;
1784     size_t i;
1785
1786     controllers = xmalloc(n * sizeof *controllers);
1787     for (i = 0; i < n; i++) {
1788         controllers[i] = ovsrec_controller_insert(txn);
1789         ovsrec_controller_set_target(controllers[i], targets[i]);
1790     }
1791
1792     return controllers;
1793 }
1794
1795 static void
1796 cmd_set_controller(struct vsctl_context *ctx)
1797 {
1798     struct vsctl_info info;
1799     struct vsctl_bridge *br;
1800     struct ovsrec_controller **controllers;
1801     size_t n;
1802
1803     get_info(ctx, &info);
1804     br = find_real_bridge(&info, ctx->argv[1], true);
1805     verify_controllers(br->br_cfg);
1806
1807     delete_controllers(br->ctrl, br->n_ctrl);
1808
1809     n = ctx->argc - 2;
1810     controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1811     ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1812     free(controllers);
1813
1814     free_info(&info);
1815 }
1816
1817 static void
1818 cmd_get_fail_mode(struct vsctl_context *ctx)
1819 {
1820     struct vsctl_info info;
1821     struct vsctl_bridge *br;
1822
1823     get_info(ctx, &info);
1824     br = find_bridge(&info, ctx->argv[1], true);
1825
1826     if (br->br_cfg) {
1827         ovsrec_bridge_verify_fail_mode(br->br_cfg);
1828     }
1829     if (br->fail_mode && strlen(br->fail_mode)) {
1830         ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1831     }
1832
1833     free_info(&info);
1834 }
1835
1836 static void
1837 cmd_del_fail_mode(struct vsctl_context *ctx)
1838 {
1839     struct vsctl_info info;
1840     struct vsctl_bridge *br;
1841
1842     get_info(ctx, &info);
1843     br = find_real_bridge(&info, ctx->argv[1], true);
1844
1845     ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1846
1847     free_info(&info);
1848 }
1849
1850 static void
1851 cmd_set_fail_mode(struct vsctl_context *ctx)
1852 {
1853     struct vsctl_info info;
1854     struct vsctl_bridge *br;
1855     const char *fail_mode = ctx->argv[2];
1856
1857     get_info(ctx, &info);
1858     br = find_real_bridge(&info, ctx->argv[1], true);
1859
1860     if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1861         vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1862     }
1863
1864     ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1865
1866     free_info(&info);
1867 }
1868
1869 static void
1870 verify_managers(const struct ovsrec_open_vswitch *ovs)
1871 {
1872     size_t i;
1873
1874     ovsrec_open_vswitch_verify_managers(ovs);
1875     ovsrec_open_vswitch_verify_manager_options(ovs);
1876
1877     for (i = 0; i < ovs->n_manager_options; ++i) {
1878         const struct ovsrec_manager *mgr = ovs->manager_options[i];
1879
1880         ovsrec_manager_verify_target(mgr);
1881     }
1882 }
1883
1884 static void
1885 pre_manager(struct vsctl_context *ctx)
1886 {
1887     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers);
1888     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1889     ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1890 }
1891
1892 static void
1893 cmd_get_manager(struct vsctl_context *ctx)
1894 {
1895     const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1896     struct svec targets;
1897     size_t i;
1898
1899     verify_managers(ovs);
1900
1901     /* Print the targets in sorted order for reproducibility. */
1902     svec_init(&targets);
1903
1904     /* First, add all targets found in deprecated 'managers' column. */
1905     for (i = 0; i < ovs->n_managers; i++) {
1906         svec_add(&targets, ovs->managers[i]);
1907     }
1908
1909     /* Second, add all targets pointed to by 'manager_options' column. */
1910     for (i = 0; i < ovs->n_manager_options; i++) {
1911         svec_add(&targets, ovs->manager_options[i]->target);
1912     }
1913
1914     svec_sort_unique(&targets);
1915     for (i = 0; i < targets.n; i++) {
1916         ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1917     }
1918     svec_destroy(&targets);
1919 }
1920
1921 static void
1922 delete_managers(const struct vsctl_context *ctx)
1923 {
1924     const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1925     size_t i;
1926
1927     /* Delete manager targets in deprecated 'managers' column. */
1928     ovsrec_open_vswitch_set_managers(ovs, NULL, 0);
1929
1930     /* Delete Manager rows pointed to by 'manager_options' column. */
1931     for (i = 0; i < ovs->n_manager_options; i++) {
1932         ovsrec_manager_delete(ovs->manager_options[i]);
1933     }
1934
1935     /* Delete 'Manager' row refs in 'manager_options' column. */
1936     ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
1937 }
1938
1939 static void
1940 cmd_del_manager(struct vsctl_context *ctx)
1941 {
1942     const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1943
1944     verify_managers(ovs);
1945     delete_managers(ctx);
1946 }
1947
1948 static void
1949 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
1950 {
1951     struct ovsrec_manager **managers;
1952     size_t i;
1953
1954     /* Store in deprecated 'manager' column. */
1955     ovsrec_open_vswitch_set_managers(ctx->ovs, targets, n);
1956
1957     /* Insert each manager in a new row in Manager table. */
1958     managers = xmalloc(n * sizeof *managers);
1959     for (i = 0; i < n; i++) {
1960         managers[i] = ovsrec_manager_insert(ctx->txn);
1961         ovsrec_manager_set_target(managers[i], targets[i]);
1962     }
1963
1964     /* Store uuids of new Manager rows in 'manager_options' column. */
1965     ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
1966     free(managers);
1967 }
1968
1969 static void
1970 cmd_set_manager(struct vsctl_context *ctx)
1971 {
1972     const size_t n = ctx->argc - 1;
1973
1974     verify_managers(ctx->ovs);
1975     delete_managers(ctx);
1976     insert_managers(ctx, &ctx->argv[1], n);
1977 }
1978
1979 static void
1980 pre_cmd_get_ssl(struct vsctl_context *ctx)
1981 {
1982     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1983
1984     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
1985     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
1986     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
1987     ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
1988 }
1989
1990 static void
1991 cmd_get_ssl(struct vsctl_context *ctx)
1992 {
1993     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1994
1995     ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1996     if (ssl) {
1997         ovsrec_ssl_verify_private_key(ssl);
1998         ovsrec_ssl_verify_certificate(ssl);
1999         ovsrec_ssl_verify_ca_cert(ssl);
2000         ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2001
2002         ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2003         ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2004         ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2005         ds_put_format(&ctx->output, "Bootstrap: %s\n",
2006                 ssl->bootstrap_ca_cert ? "true" : "false");
2007     }
2008 }
2009
2010 static void
2011 pre_cmd_del_ssl(struct vsctl_context *ctx)
2012 {
2013     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2014 }
2015
2016 static void
2017 cmd_del_ssl(struct vsctl_context *ctx)
2018 {
2019     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2020
2021     if (ssl) {
2022         ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2023         ovsrec_ssl_delete(ssl);
2024         ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2025     }
2026 }
2027
2028 static void
2029 pre_cmd_set_ssl(struct vsctl_context *ctx)
2030 {
2031     ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2032 }
2033
2034 static void
2035 cmd_set_ssl(struct vsctl_context *ctx)
2036 {
2037     bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2038     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2039
2040     ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2041     if (ssl) {
2042         ovsrec_ssl_delete(ssl);
2043     }
2044     ssl = ovsrec_ssl_insert(ctx->txn);
2045
2046     ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2047     ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2048     ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2049
2050     ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2051
2052     ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2053 }
2054 \f
2055 /* Parameter commands. */
2056
2057 struct vsctl_row_id {
2058     const struct ovsdb_idl_table_class *table;
2059     const struct ovsdb_idl_column *name_column;
2060     const struct ovsdb_idl_column *uuid_column;
2061 };
2062
2063 struct vsctl_table_class {
2064     struct ovsdb_idl_table_class *class;
2065     struct vsctl_row_id row_ids[2];
2066 };
2067
2068 static const struct vsctl_table_class tables[] = {
2069     {&ovsrec_table_bridge,
2070      {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2071       {NULL, NULL, NULL}}},
2072
2073     {&ovsrec_table_controller,
2074      {{&ovsrec_table_bridge,
2075        &ovsrec_bridge_col_name,
2076        &ovsrec_bridge_col_controller}}},
2077
2078     {&ovsrec_table_interface,
2079      {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2080       {NULL, NULL, NULL}}},
2081
2082     {&ovsrec_table_mirror,
2083      {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2084       {NULL, NULL, NULL}}},
2085
2086     {&ovsrec_table_manager,
2087      {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2088       {NULL, NULL, NULL}}},
2089
2090     {&ovsrec_table_netflow,
2091      {{&ovsrec_table_bridge,
2092        &ovsrec_bridge_col_name,
2093        &ovsrec_bridge_col_netflow},
2094       {NULL, NULL, NULL}}},
2095
2096     {&ovsrec_table_open_vswitch,
2097      {{&ovsrec_table_open_vswitch, NULL, NULL},
2098       {NULL, NULL, NULL}}},
2099
2100     {&ovsrec_table_port,
2101      {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2102       {NULL, NULL, NULL}}},
2103
2104     {&ovsrec_table_qos,
2105      {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2106       {NULL, NULL, NULL}}},
2107
2108     {&ovsrec_table_monitor,
2109      {{&ovsrec_table_interface,
2110        &ovsrec_interface_col_name,
2111        &ovsrec_interface_col_monitor},
2112       {NULL, NULL, NULL}}},
2113
2114     {&ovsrec_table_maintenance_point,
2115      {{NULL, NULL, NULL},
2116       {NULL, NULL, NULL}}},
2117
2118     {&ovsrec_table_queue,
2119      {{NULL, NULL, NULL},
2120       {NULL, NULL, NULL}}},
2121
2122     {&ovsrec_table_ssl,
2123      {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2124
2125     {&ovsrec_table_sflow,
2126      {{&ovsrec_table_bridge,
2127        &ovsrec_bridge_col_name,
2128        &ovsrec_bridge_col_sflow},
2129       {NULL, NULL, NULL}}},
2130
2131     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2132 };
2133
2134 static void
2135 die_if_error(char *error)
2136 {
2137     if (error) {
2138         vsctl_fatal("%s", error);
2139     }
2140 }
2141
2142 static int
2143 to_lower_and_underscores(unsigned c)
2144 {
2145     return c == '-' ? '_' : tolower(c);
2146 }
2147
2148 static unsigned int
2149 score_partial_match(const char *name, const char *s)
2150 {
2151     int score;
2152
2153     if (!strcmp(name, s)) {
2154         return UINT_MAX;
2155     }
2156     for (score = 0; ; score++, name++, s++) {
2157         if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2158             break;
2159         } else if (*name == '\0') {
2160             return UINT_MAX - 1;
2161         }
2162     }
2163     return *s == '\0' ? score : 0;
2164 }
2165
2166 static const struct vsctl_table_class *
2167 get_table(const char *table_name)
2168 {
2169     const struct vsctl_table_class *table;
2170     const struct vsctl_table_class *best_match = NULL;
2171     unsigned int best_score = 0;
2172
2173     for (table = tables; table->class; table++) {
2174         unsigned int score = score_partial_match(table->class->name,
2175                                                  table_name);
2176         if (score > best_score) {
2177             best_match = table;
2178             best_score = score;
2179         } else if (score == best_score) {
2180             best_match = NULL;
2181         }
2182     }
2183     if (best_match) {
2184         return best_match;
2185     } else if (best_score) {
2186         vsctl_fatal("multiple table names match \"%s\"", table_name);
2187     } else {
2188         vsctl_fatal("unknown table \"%s\"", table_name);
2189     }
2190 }
2191
2192 static const struct vsctl_table_class *
2193 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2194 {
2195     const struct vsctl_table_class *table_class;
2196     int i;
2197
2198     table_class = get_table(table_name);
2199     ovsdb_idl_add_table(ctx->idl, table_class->class);
2200
2201     for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2202         const struct vsctl_row_id *id = &table_class->row_ids[i];
2203         if (id->table) {
2204             ovsdb_idl_add_table(ctx->idl, id->table);
2205         }
2206         if (id->name_column) {
2207             ovsdb_idl_add_column(ctx->idl, id->name_column);
2208         }
2209         if (id->uuid_column) {
2210             ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2211         }
2212     }
2213
2214     return table_class;
2215 }
2216
2217 static const struct ovsdb_idl_row *
2218 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2219               const struct vsctl_row_id *id, const char *record_id)
2220 {
2221     const struct ovsdb_idl_row *referrer, *final;
2222
2223     if (!id->table) {
2224         return NULL;
2225     }
2226
2227     if (!id->name_column) {
2228         if (strcmp(record_id, ".")) {
2229             return NULL;
2230         }
2231         referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2232         if (!referrer || ovsdb_idl_next_row(referrer)) {
2233             return NULL;
2234         }
2235     } else {
2236         const struct ovsdb_idl_row *row;
2237
2238         referrer = NULL;
2239         for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2240              row != NULL;
2241              row = ovsdb_idl_next_row(row))
2242         {
2243             const struct ovsdb_datum *name;
2244
2245             name = ovsdb_idl_get(row, id->name_column,
2246                                  OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2247             if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2248                 if (referrer) {
2249                     vsctl_fatal("multiple rows in %s match \"%s\"",
2250                                 table->class->name, record_id);
2251                 }
2252                 referrer = row;
2253             }
2254         }
2255     }
2256     if (!referrer) {
2257         return NULL;
2258     }
2259
2260     final = NULL;
2261     if (id->uuid_column) {
2262         const struct ovsdb_datum *uuid;
2263
2264         ovsdb_idl_txn_verify(referrer, id->uuid_column);
2265         uuid = ovsdb_idl_get(referrer, id->uuid_column,
2266                              OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2267         if (uuid->n == 1) {
2268             final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2269                                                &uuid->keys[0].uuid);
2270         }
2271     } else {
2272         final = referrer;
2273     }
2274
2275     return final;
2276 }
2277
2278 static const struct ovsdb_idl_row *
2279 get_row (struct vsctl_context *ctx,
2280          const struct vsctl_table_class *table, const char *record_id)
2281 {
2282     const struct ovsdb_idl_row *row;
2283     struct uuid uuid;
2284
2285     if (uuid_from_string(&uuid, record_id)) {
2286         row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2287     } else {
2288         int i;
2289
2290         for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2291             row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2292             if (row) {
2293                 break;
2294             }
2295         }
2296     }
2297     return row;
2298 }
2299
2300 static const struct ovsdb_idl_row *
2301 must_get_row(struct vsctl_context *ctx,
2302              const struct vsctl_table_class *table, const char *record_id)
2303 {
2304     const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2305     if (!row) {
2306         vsctl_fatal("no row \"%s\" in table %s",
2307                     record_id, table->class->name);
2308     }
2309     return row;
2310 }
2311
2312 static char *
2313 get_column(const struct vsctl_table_class *table, const char *column_name,
2314            const struct ovsdb_idl_column **columnp)
2315 {
2316     const struct ovsdb_idl_column *best_match = NULL;
2317     unsigned int best_score = 0;
2318     size_t i;
2319
2320     for (i = 0; i < table->class->n_columns; i++) {
2321         const struct ovsdb_idl_column *column = &table->class->columns[i];
2322         unsigned int score = score_partial_match(column->name, column_name);
2323         if (score > best_score) {
2324             best_match = column;
2325             best_score = score;
2326         } else if (score == best_score) {
2327             best_match = NULL;
2328         }
2329     }
2330
2331     *columnp = best_match;
2332     if (best_match) {
2333         return NULL;
2334     } else if (best_score) {
2335         return xasprintf("%s contains more than one column whose name "
2336                          "matches \"%s\"", table->class->name, column_name);
2337     } else {
2338         return xasprintf("%s does not contain a column whose name matches "
2339                          "\"%s\"", table->class->name, column_name);
2340     }
2341 }
2342
2343 static struct uuid *
2344 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2345 {
2346     struct ovsdb_symbol *symbol;
2347
2348     if (id[0] != '@') {
2349         vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2350     }
2351
2352     if (newp) {
2353         *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2354     }
2355
2356     symbol = ovsdb_symbol_table_insert(symtab, id);
2357     if (symbol->used) {
2358         vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2359                     id);
2360     }
2361     symbol->used = true;
2362     return &symbol->uuid;
2363 }
2364
2365 static void
2366 pre_get_column(struct vsctl_context *ctx,
2367                const struct vsctl_table_class *table, const char *column_name,
2368                const struct ovsdb_idl_column **columnp)
2369 {
2370     die_if_error(get_column(table, column_name, columnp));
2371     ovsdb_idl_add_column(ctx->idl, *columnp);
2372 }
2373
2374 static char *
2375 missing_operator_error(const char *arg, const char **allowed_operators,
2376                        size_t n_allowed)
2377 {
2378     struct ds s;
2379
2380     ds_init(&s);
2381     ds_put_format(&s, "%s: argument does not end in ", arg);
2382     ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2383     if (n_allowed == 2) {
2384         ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2385     } else if (n_allowed > 2) {
2386         size_t i;
2387
2388         for (i = 1; i < n_allowed - 1; i++) {
2389             ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2390         }
2391         ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2392     }
2393     ds_put_format(&s, " followed by a value.");
2394
2395     return ds_steal_cstr(&s);
2396 }
2397
2398 /* Breaks 'arg' apart into a number of fields in the following order:
2399  *
2400  *      - If 'columnp' is nonnull, the name of a column in 'table'.  The column
2401  *        is stored into '*columnp'.  The column name may be abbreviated.
2402  *
2403  *      - If 'keyp' is nonnull, optionally a key string.  (If both 'columnp'
2404  *        and 'keyp' are nonnull, then the column and key names are expected to
2405  *        be separated by ':').  The key is stored as a malloc()'d string into
2406  *        '*keyp', or NULL if no key is present in 'arg'.
2407  *
2408  *      - If 'valuep' is nonnull, an operator followed by a value string.  The
2409  *        allowed operators are the 'n_allowed' string in 'allowed_operators',
2410  *        or just "=" if 'n_allowed' is 0.  If 'operatorp' is nonnull, then the
2411  *        operator is stored into '*operatorp' (one of the pointers from
2412  *        'allowed_operators' is stored; nothing is malloc()'d).  The value is
2413  *        stored as a malloc()'d string into '*valuep', or NULL if no value is
2414  *        present in 'arg'.
2415  *
2416  * At least 'columnp' or 'keyp' must be nonnull.
2417  *
2418  * On success, returns NULL.  On failure, returned a malloc()'d string error
2419  * message and stores NULL into all of the nonnull output arguments. */
2420 static char * WARN_UNUSED_RESULT
2421 parse_column_key_value(const char *arg,
2422                        const struct vsctl_table_class *table,
2423                        const struct ovsdb_idl_column **columnp, char **keyp,
2424                        const char **operatorp,
2425                        const char **allowed_operators, size_t n_allowed,
2426                        char **valuep)
2427 {
2428     const char *p = arg;
2429     char *error;
2430
2431     assert(columnp || keyp);
2432     assert(!(operatorp && !valuep));
2433     if (keyp) {
2434         *keyp = NULL;
2435     }
2436     if (valuep) {
2437         *valuep = NULL;
2438     }
2439
2440     /* Parse column name. */
2441     if (columnp) {
2442         char *column_name;
2443
2444         error = ovsdb_token_parse(&p, &column_name);
2445         if (error) {
2446             goto error;
2447         }
2448         if (column_name[0] == '\0') {
2449             free(column_name);
2450             error = xasprintf("%s: missing column name", arg);
2451             goto error;
2452         }
2453         error = get_column(table, column_name, columnp);
2454         free(column_name);
2455         if (error) {
2456             goto error;
2457         }
2458     }
2459
2460     /* Parse key string. */
2461     if (*p == ':' || !columnp) {
2462         if (columnp) {
2463             p++;
2464         } else if (!keyp) {
2465             error = xasprintf("%s: key not accepted here", arg);
2466             goto error;
2467         }
2468         error = ovsdb_token_parse(&p, keyp);
2469         if (error) {
2470             goto error;
2471         }
2472     } else if (keyp) {
2473         *keyp = NULL;
2474     }
2475
2476     /* Parse value string. */
2477     if (valuep) {
2478         const char *best;
2479         size_t best_len;
2480         size_t i;
2481
2482         if (!allowed_operators) {
2483             static const char *equals = "=";
2484             allowed_operators = &equals;
2485             n_allowed = 1;
2486         }
2487
2488         best = NULL;
2489         best_len = 0;
2490         for (i = 0; i < n_allowed; i++) {
2491             const char *op = allowed_operators[i];
2492             size_t op_len = strlen(op);
2493
2494             if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2495                 best_len = op_len;
2496                 best = op;
2497             }
2498         }
2499         if (!best) {
2500             error = missing_operator_error(arg, allowed_operators, n_allowed);
2501             goto error;
2502         }
2503
2504         if (operatorp) {
2505             *operatorp = best;
2506         }
2507         *valuep = xstrdup(p + best_len);
2508     } else {
2509         if (valuep) {
2510             *valuep = NULL;
2511         }
2512         if (*p != '\0') {
2513             error = xasprintf("%s: trailing garbage \"%s\" in argument",
2514                               arg, p);
2515             goto error;
2516         }
2517     }
2518     return NULL;
2519
2520 error:
2521     if (columnp) {
2522         *columnp = NULL;
2523     }
2524     if (keyp) {
2525         free(*keyp);
2526         *keyp = NULL;
2527     }
2528     if (valuep) {
2529         free(*valuep);
2530         *valuep = NULL;
2531         if (operatorp) {
2532             *operatorp = NULL;
2533         }
2534     }
2535     return error;
2536 }
2537
2538 static void
2539 pre_parse_column_key_value(struct vsctl_context *ctx,
2540                            const char *arg,
2541                            const struct vsctl_table_class *table)
2542 {
2543     const struct ovsdb_idl_column *column;
2544     const char *p;
2545     char *column_name;
2546
2547     p = arg;
2548     die_if_error(ovsdb_token_parse(&p, &column_name));
2549     if (column_name[0] == '\0') {
2550         vsctl_fatal("%s: missing column name", arg);
2551     }
2552
2553     pre_get_column(ctx, table, column_name, &column);
2554     free(column_name);
2555 }
2556
2557 static void
2558 pre_cmd_get(struct vsctl_context *ctx)
2559 {
2560     const char *table_name = ctx->argv[1];
2561     const struct vsctl_table_class *table;
2562     int i;
2563
2564     table = pre_get_table(ctx, table_name);
2565     for (i = 3; i < ctx->argc; i++) {
2566         if (!strcasecmp(ctx->argv[i], "_uuid")
2567             || !strcasecmp(ctx->argv[i], "-uuid")) {
2568             continue;
2569         }
2570
2571         pre_parse_column_key_value(ctx, ctx->argv[i], table);
2572     }
2573 }
2574
2575 static void
2576 cmd_get(struct vsctl_context *ctx)
2577 {
2578     const char *id = shash_find_data(&ctx->options, "--id");
2579     bool if_exists = shash_find(&ctx->options, "--if-exists");
2580     const char *table_name = ctx->argv[1];
2581     const char *record_id = ctx->argv[2];
2582     const struct vsctl_table_class *table;
2583     const struct ovsdb_idl_row *row;
2584     struct ds *out = &ctx->output;
2585     int i;
2586
2587     table = get_table(table_name);
2588     row = must_get_row(ctx, table, record_id);
2589     if (id) {
2590         bool new;
2591
2592         *create_symbol(ctx->symtab, id, &new) = row->uuid;
2593         if (!new) {
2594             vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2595                         "before it was defined", id);
2596         }
2597     }
2598     for (i = 3; i < ctx->argc; i++) {
2599         const struct ovsdb_idl_column *column;
2600         const struct ovsdb_datum *datum;
2601         char *key_string;
2602
2603         /* Special case for obtaining the UUID of a row.  We can't just do this
2604          * through parse_column_key_value() below since it returns a "struct
2605          * ovsdb_idl_column" and the UUID column doesn't have one. */
2606         if (!strcasecmp(ctx->argv[i], "_uuid")
2607             || !strcasecmp(ctx->argv[i], "-uuid")) {
2608             ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2609             continue;
2610         }
2611
2612         die_if_error(parse_column_key_value(ctx->argv[i], table,
2613                                             &column, &key_string,
2614                                             NULL, NULL, 0, NULL));
2615
2616         ovsdb_idl_txn_verify(row, column);
2617         datum = ovsdb_idl_read(row, column);
2618         if (key_string) {
2619             union ovsdb_atom key;
2620             unsigned int idx;
2621
2622             if (column->type.value.type == OVSDB_TYPE_VOID) {
2623                 vsctl_fatal("cannot specify key to get for non-map column %s",
2624                             column->name);
2625             }
2626
2627             die_if_error(ovsdb_atom_from_string(&key,
2628                                                 &column->type.key,
2629                                                 key_string, ctx->symtab));
2630
2631             idx = ovsdb_datum_find_key(datum, &key,
2632                                        column->type.key.type);
2633             if (idx == UINT_MAX) {
2634                 if (!if_exists) {
2635                     vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2636                                 key_string, table->class->name, record_id,
2637                                 column->name);
2638                 }
2639             } else {
2640                 ovsdb_atom_to_string(&datum->values[idx],
2641                                      column->type.value.type, out);
2642             }
2643             ovsdb_atom_destroy(&key, column->type.key.type);
2644         } else {
2645             ovsdb_datum_to_string(datum, &column->type, out);
2646         }
2647         ds_put_char(out, '\n');
2648
2649         free(key_string);
2650     }
2651 }
2652
2653 static void
2654 parse_column_names(const char *column_names,
2655                    const struct vsctl_table_class *table,
2656                    const struct ovsdb_idl_column ***columnsp,
2657                    size_t *n_columnsp)
2658 {
2659     const struct ovsdb_idl_column **columns;
2660     size_t n_columns;
2661
2662     if (!column_names) {
2663         size_t i;
2664
2665         n_columns = table->class->n_columns + 1;
2666         columns = xmalloc(n_columns * sizeof *columns);
2667         columns[0] = NULL;
2668         for (i = 0; i < table->class->n_columns; i++) {
2669             columns[i + 1] = &table->class->columns[i];
2670         }
2671     } else {
2672         char *s = xstrdup(column_names);
2673         size_t allocated_columns;
2674         char *save_ptr = NULL;
2675         char *column_name;
2676
2677         columns = NULL;
2678         allocated_columns = n_columns = 0;
2679         for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2680              column_name = strtok_r(NULL, ", ", &save_ptr)) {
2681             const struct ovsdb_idl_column *column;
2682
2683             if (!strcasecmp(column_name, "_uuid")) {
2684                 column = NULL;
2685             } else {
2686                 die_if_error(get_column(table, column_name, &column));
2687             }
2688             if (n_columns >= allocated_columns) {
2689                 columns = x2nrealloc(columns, &allocated_columns,
2690                                      sizeof *columns);
2691             }
2692             columns[n_columns++] = column;
2693         }
2694         free(s);
2695
2696         if (!n_columns) {
2697             vsctl_fatal("must specify at least one column name");
2698         }
2699     }
2700     *columnsp = columns;
2701     *n_columnsp = n_columns;
2702 }
2703
2704
2705 static void
2706 pre_list_columns(struct vsctl_context *ctx,
2707                  const struct vsctl_table_class *table,
2708                  const char *column_names)
2709 {
2710     const struct ovsdb_idl_column **columns;
2711     size_t n_columns;
2712     size_t i;
2713
2714     parse_column_names(column_names, table, &columns, &n_columns);
2715     for (i = 0; i < n_columns; i++) {
2716         if (columns[i]) {
2717             ovsdb_idl_add_column(ctx->idl, columns[i]);
2718         }
2719     }
2720     free(columns);
2721 }
2722
2723 static void
2724 pre_cmd_list(struct vsctl_context *ctx)
2725 {
2726     const char *column_names = shash_find_data(&ctx->options, "--columns");
2727     const char *table_name = ctx->argv[1];
2728     const struct vsctl_table_class *table;
2729
2730     table = pre_get_table(ctx, table_name);
2731     pre_list_columns(ctx, table, column_names);
2732 }
2733
2734 static void
2735 list_record(const struct ovsdb_idl_row *row,
2736             const struct ovsdb_idl_column **columns, size_t n_columns,
2737             struct ds *out)
2738 {
2739     size_t i;
2740
2741     for (i = 0; i < n_columns; i++) {
2742         const struct ovsdb_idl_column *column = columns[i];
2743
2744         if (!column) {
2745             ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2746                           UUID_ARGS(&row->uuid));
2747         } else {
2748             const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2749             ds_put_format(out, "%-20s: ", column->name);
2750             ovsdb_datum_to_string(datum, &column->type, out);
2751             ds_put_char(out, '\n');
2752         }
2753     }
2754 }
2755
2756 static void
2757 cmd_list(struct vsctl_context *ctx)
2758 {
2759     const char *column_names = shash_find_data(&ctx->options, "--columns");
2760     const struct ovsdb_idl_column **columns;
2761     const char *table_name = ctx->argv[1];
2762     const struct vsctl_table_class *table;
2763     struct ds *out = &ctx->output;
2764     size_t n_columns;
2765     int i;
2766
2767     table = get_table(table_name);
2768     parse_column_names(column_names, table, &columns, &n_columns);
2769     if (ctx->argc > 2) {
2770         for (i = 2; i < ctx->argc; i++) {
2771             if (i > 2) {
2772                 ds_put_char(out, '\n');
2773             }
2774             list_record(must_get_row(ctx, table, ctx->argv[i]),
2775                         columns, n_columns, out);
2776         }
2777     } else {
2778         const struct ovsdb_idl_row *row;
2779         bool first;
2780
2781         for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2782              row != NULL;
2783              row = ovsdb_idl_next_row(row), first = false) {
2784             if (!first) {
2785                 ds_put_char(out, '\n');
2786             }
2787             list_record(row, columns, n_columns, out);
2788         }
2789     }
2790     free(columns);
2791 }
2792
2793 static void
2794 pre_cmd_set(struct vsctl_context *ctx)
2795 {
2796     const char *table_name = ctx->argv[1];
2797     const struct vsctl_table_class *table;
2798     int i;
2799
2800     table = pre_get_table(ctx, table_name);
2801     for (i = 3; i < ctx->argc; i++) {
2802         pre_parse_column_key_value(ctx, ctx->argv[i], table);
2803     }
2804 }
2805
2806 static void
2807 set_column(const struct vsctl_table_class *table,
2808            const struct ovsdb_idl_row *row, const char *arg,
2809            struct ovsdb_symbol_table *symtab)
2810 {
2811     const struct ovsdb_idl_column *column;
2812     char *key_string, *value_string;
2813     char *error;
2814
2815     error = parse_column_key_value(arg, table, &column, &key_string,
2816                                    NULL, NULL, 0, &value_string);
2817     die_if_error(error);
2818     if (!value_string) {
2819         vsctl_fatal("%s: missing value", arg);
2820     }
2821
2822     if (key_string) {
2823         union ovsdb_atom key, value;
2824         struct ovsdb_datum datum;
2825
2826         if (column->type.value.type == OVSDB_TYPE_VOID) {
2827             vsctl_fatal("cannot specify key to set for non-map column %s",
2828                         column->name);
2829         }
2830
2831         die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2832                                             key_string, symtab));
2833         die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2834                                             value_string, symtab));
2835
2836         ovsdb_datum_init_empty(&datum);
2837         ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2838
2839         ovsdb_atom_destroy(&key, column->type.key.type);
2840         ovsdb_atom_destroy(&value, column->type.value.type);
2841
2842         ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2843                           &column->type, false);
2844         ovsdb_idl_txn_write(row, column, &datum);
2845     } else {
2846         struct ovsdb_datum datum;
2847
2848         die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2849                                              value_string, symtab));
2850         ovsdb_idl_txn_write(row, column, &datum);
2851     }
2852
2853     free(key_string);
2854     free(value_string);
2855 }
2856
2857 static void
2858 cmd_set(struct vsctl_context *ctx)
2859 {
2860     const char *table_name = ctx->argv[1];
2861     const char *record_id = ctx->argv[2];
2862     const struct vsctl_table_class *table;
2863     const struct ovsdb_idl_row *row;
2864     int i;
2865
2866     table = get_table(table_name);
2867     row = must_get_row(ctx, table, record_id);
2868     for (i = 3; i < ctx->argc; i++) {
2869         set_column(table, row, ctx->argv[i], ctx->symtab);
2870     }
2871 }
2872
2873 static void
2874 pre_cmd_add(struct vsctl_context *ctx)
2875 {
2876     const char *table_name = ctx->argv[1];
2877     const char *column_name = ctx->argv[3];
2878     const struct vsctl_table_class *table;
2879     const struct ovsdb_idl_column *column;
2880
2881     table = pre_get_table(ctx, table_name);
2882     pre_get_column(ctx, table, column_name, &column);
2883 }
2884
2885 static void
2886 cmd_add(struct vsctl_context *ctx)
2887 {
2888     const char *table_name = ctx->argv[1];
2889     const char *record_id = ctx->argv[2];
2890     const char *column_name = ctx->argv[3];
2891     const struct vsctl_table_class *table;
2892     const struct ovsdb_idl_column *column;
2893     const struct ovsdb_idl_row *row;
2894     const struct ovsdb_type *type;
2895     struct ovsdb_datum old;
2896     int i;
2897
2898     table = get_table(table_name);
2899     row = must_get_row(ctx, table, record_id);
2900     die_if_error(get_column(table, column_name, &column));
2901
2902     type = &column->type;
2903     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2904     for (i = 4; i < ctx->argc; i++) {
2905         struct ovsdb_type add_type;
2906         struct ovsdb_datum add;
2907
2908         add_type = *type;
2909         add_type.n_min = 1;
2910         add_type.n_max = UINT_MAX;
2911         die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2912                                              ctx->symtab));
2913         ovsdb_datum_union(&old, &add, type, false);
2914         ovsdb_datum_destroy(&add, type);
2915     }
2916     if (old.n > type->n_max) {
2917         vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2918                     "table %s but the maximum number is %u",
2919                     old.n,
2920                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2921                     column->name, table->class->name, type->n_max);
2922     }
2923     ovsdb_idl_txn_verify(row, column);
2924     ovsdb_idl_txn_write(row, column, &old);
2925 }
2926
2927 static void
2928 pre_cmd_remove(struct vsctl_context *ctx)
2929 {
2930     const char *table_name = ctx->argv[1];
2931     const char *column_name = ctx->argv[3];
2932     const struct vsctl_table_class *table;
2933     const struct ovsdb_idl_column *column;
2934
2935     table = pre_get_table(ctx, table_name);
2936     pre_get_column(ctx, table, column_name, &column);
2937 }
2938
2939 static void
2940 cmd_remove(struct vsctl_context *ctx)
2941 {
2942     const char *table_name = ctx->argv[1];
2943     const char *record_id = ctx->argv[2];
2944     const char *column_name = ctx->argv[3];
2945     const struct vsctl_table_class *table;
2946     const struct ovsdb_idl_column *column;
2947     const struct ovsdb_idl_row *row;
2948     const struct ovsdb_type *type;
2949     struct ovsdb_datum old;
2950     int i;
2951
2952     table = get_table(table_name);
2953     row = must_get_row(ctx, table, record_id);
2954     die_if_error(get_column(table, column_name, &column));
2955
2956     type = &column->type;
2957     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2958     for (i = 4; i < ctx->argc; i++) {
2959         struct ovsdb_type rm_type;
2960         struct ovsdb_datum rm;
2961         char *error;
2962
2963         rm_type = *type;
2964         rm_type.n_min = 1;
2965         rm_type.n_max = UINT_MAX;
2966         error = ovsdb_datum_from_string(&rm, &rm_type,
2967                                         ctx->argv[i], ctx->symtab);
2968         if (error && ovsdb_type_is_map(&rm_type)) {
2969             free(error);
2970             rm_type.value.type = OVSDB_TYPE_VOID;
2971             die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2972                                                  ctx->argv[i], ctx->symtab));
2973         }
2974         ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2975         ovsdb_datum_destroy(&rm, &rm_type);
2976     }
2977     if (old.n < type->n_min) {
2978         vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2979                     "table %s but the minimum number is %u",
2980                     old.n,
2981                     type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2982                     column->name, table->class->name, type->n_min);
2983     }
2984     ovsdb_idl_txn_verify(row, column);
2985     ovsdb_idl_txn_write(row, column, &old);
2986 }
2987
2988 static void
2989 pre_cmd_clear(struct vsctl_context *ctx)
2990 {
2991     const char *table_name = ctx->argv[1];
2992     const struct vsctl_table_class *table;
2993     int i;
2994
2995     table = pre_get_table(ctx, table_name);
2996     for (i = 3; i < ctx->argc; i++) {
2997         const struct ovsdb_idl_column *column;
2998
2999         pre_get_column(ctx, table, ctx->argv[i], &column);
3000     }
3001 }
3002
3003 static void
3004 cmd_clear(struct vsctl_context *ctx)
3005 {
3006     const char *table_name = ctx->argv[1];
3007     const char *record_id = ctx->argv[2];
3008     const struct vsctl_table_class *table;
3009     const struct ovsdb_idl_row *row;
3010     int i;
3011
3012     table = get_table(table_name);
3013     row = must_get_row(ctx, table, record_id);
3014     for (i = 3; i < ctx->argc; i++) {
3015         const struct ovsdb_idl_column *column;
3016         const struct ovsdb_type *type;
3017         struct ovsdb_datum datum;
3018
3019         die_if_error(get_column(table, ctx->argv[i], &column));
3020
3021         type = &column->type;
3022         if (type->n_min > 0) {
3023             vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3024                         "of table %s, which is not allowed to be empty",
3025                         column->name, table->class->name);
3026         }
3027
3028         ovsdb_datum_init_empty(&datum);
3029         ovsdb_idl_txn_write(row, column, &datum);
3030     }
3031 }
3032
3033 static void
3034 cmd_create(struct vsctl_context *ctx)
3035 {
3036     const char *id = shash_find_data(&ctx->options, "--id");
3037     const char *table_name = ctx->argv[1];
3038     const struct vsctl_table_class *table;
3039     const struct ovsdb_idl_row *row;
3040     const struct uuid *uuid;
3041     int i;
3042
3043     uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL;
3044
3045     table = get_table(table_name);
3046     row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3047     for (i = 2; i < ctx->argc; i++) {
3048         set_column(table, row, ctx->argv[i], ctx->symtab);
3049     }
3050     ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3051 }
3052
3053 /* This function may be used as the 'postprocess' function for commands that
3054  * insert new rows into the database.  It expects that the command's 'run'
3055  * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3056  * sole output.  It replaces that output by the row's permanent UUID assigned
3057  * by the database server and appends a new-line.
3058  *
3059  * Currently we use this only for "create", because the higher-level commands
3060  * are supposed to be independent of the actual structure of the vswitch
3061  * configuration. */
3062 static void
3063 post_create(struct vsctl_context *ctx)
3064 {
3065     const struct uuid *real;
3066     struct uuid dummy;
3067
3068     uuid_from_string(&dummy, ds_cstr(&ctx->output));
3069     real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3070     if (real) {
3071         ds_clear(&ctx->output);
3072         ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3073     }
3074     ds_put_char(&ctx->output, '\n');
3075 }
3076
3077 static void
3078 pre_cmd_destroy(struct vsctl_context *ctx)
3079 {
3080     const char *table_name = ctx->argv[1];
3081
3082     pre_get_table(ctx, table_name);
3083 }
3084
3085 static void
3086 cmd_destroy(struct vsctl_context *ctx)
3087 {
3088     bool must_exist = !shash_find(&ctx->options, "--if-exists");
3089     const char *table_name = ctx->argv[1];
3090     const struct vsctl_table_class *table;
3091     int i;
3092
3093     table = get_table(table_name);
3094     for (i = 2; i < ctx->argc; i++) {
3095         const struct ovsdb_idl_row *row;
3096
3097         row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3098         if (row) {
3099             ovsdb_idl_txn_delete(row);
3100         }
3101     }
3102 }
3103
3104 static bool
3105 is_condition_satisfied(const struct vsctl_table_class *table,
3106                        const struct ovsdb_idl_row *row, const char *arg,
3107                        struct ovsdb_symbol_table *symtab)
3108 {
3109     static const char *operators[] = {
3110         "=", "!=", "<", ">", "<=", ">="
3111     };
3112
3113     const struct ovsdb_idl_column *column;
3114     const struct ovsdb_datum *have_datum;
3115     char *key_string, *value_string;
3116     const char *operator;
3117     unsigned int idx;
3118     char *error;
3119     int cmp = 0;
3120
3121     error = parse_column_key_value(arg, table, &column, &key_string,
3122                                    &operator, operators, ARRAY_SIZE(operators),
3123                                    &value_string);
3124     die_if_error(error);
3125     if (!value_string) {
3126         vsctl_fatal("%s: missing value", arg);
3127     }
3128
3129     have_datum = ovsdb_idl_read(row, column);
3130     if (key_string) {
3131         union ovsdb_atom want_key, want_value;
3132
3133         if (column->type.value.type == OVSDB_TYPE_VOID) {
3134             vsctl_fatal("cannot specify key to check for non-map column %s",
3135                         column->name);
3136         }
3137
3138         die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3139                                             key_string, symtab));
3140         die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
3141                                             value_string, symtab));
3142
3143         idx = ovsdb_datum_find_key(have_datum,
3144                                    &want_key, column->type.key.type);
3145         if (idx != UINT_MAX) {
3146             cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
3147                                           &want_value,
3148                                           column->type.value.type);
3149         }
3150
3151         ovsdb_atom_destroy(&want_key, column->type.key.type);
3152         ovsdb_atom_destroy(&want_value, column->type.value.type);
3153     } else {
3154         struct ovsdb_datum want_datum;
3155
3156         die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3157                                              value_string, symtab));
3158         idx = 0;
3159         cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
3160                                        &column->type);
3161         ovsdb_datum_destroy(&want_datum, &column->type);
3162     }
3163
3164     free(key_string);
3165     free(value_string);
3166
3167     return (idx == UINT_MAX ? false
3168             : !strcmp(operator, "=") ? cmp == 0
3169             : !strcmp(operator, "!=") ? cmp != 0
3170             : !strcmp(operator, "<") ? cmp < 0
3171             : !strcmp(operator, ">") ? cmp > 0
3172             : !strcmp(operator, "<=") ? cmp <= 0
3173             : !strcmp(operator, ">=") ? cmp >= 0
3174             : (abort(), 0));
3175 }
3176
3177 static void
3178 pre_cmd_wait_until(struct vsctl_context *ctx)
3179 {
3180     const char *table_name = ctx->argv[1];
3181     const struct vsctl_table_class *table;
3182     int i;
3183
3184     table = pre_get_table(ctx, table_name);
3185
3186     for (i = 3; i < ctx->argc; i++) {
3187         pre_parse_column_key_value(ctx, ctx->argv[i], table);
3188     }
3189 }
3190
3191 static void
3192 cmd_wait_until(struct vsctl_context *ctx)
3193 {
3194     const char *table_name = ctx->argv[1];
3195     const char *record_id = ctx->argv[2];
3196     const struct vsctl_table_class *table;
3197     const struct ovsdb_idl_row *row;
3198     int i;
3199
3200     table = get_table(table_name);
3201
3202     row = get_row(ctx, table, record_id);
3203     if (!row) {
3204         ctx->try_again = true;
3205         return;
3206     }
3207
3208     for (i = 3; i < ctx->argc; i++) {
3209         if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3210             ctx->try_again = true;
3211             return;
3212         }
3213     }
3214 }
3215 \f
3216 static struct json *
3217 where_uuid_equals(const struct uuid *uuid)
3218 {
3219     return
3220         json_array_create_1(
3221             json_array_create_3(
3222                 json_string_create("_uuid"),
3223                 json_string_create("=="),
3224                 json_array_create_2(
3225                     json_string_create("uuid"),
3226                     json_string_create_nocopy(
3227                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3228 }
3229
3230 static void
3231 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3232                    struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3233                    const struct ovsrec_open_vswitch *ovs,
3234                    struct ovsdb_symbol_table *symtab)
3235 {
3236     ctx->argc = command->argc;
3237     ctx->argv = command->argv;
3238     ctx->options = command->options;
3239
3240     ds_swap(&ctx->output, &command->output);
3241     ctx->idl = idl;
3242     ctx->txn = txn;
3243     ctx->ovs = ovs;
3244     ctx->symtab = symtab;
3245     ctx->verified_ports = false;
3246
3247     ctx->try_again = false;
3248 }
3249
3250 static void
3251 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3252 {
3253     ds_swap(&ctx->output, &command->output);
3254 }
3255
3256 static void
3257 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3258                   struct ovsdb_idl *idl)
3259 {
3260     struct vsctl_command *c;
3261
3262     ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3263     if (wait_for_reload) {
3264         ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3265     }
3266     for (c = commands; c < &commands[n_commands]; c++) {
3267         if (c->syntax->prerequisites) {
3268             struct vsctl_context ctx;
3269
3270             ds_init(&c->output);
3271
3272             vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3273             (c->syntax->prerequisites)(&ctx);
3274             vsctl_context_done(&ctx, c);
3275
3276             assert(!c->output.string);
3277         }
3278     }
3279 }
3280
3281 static void
3282 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3283          struct ovsdb_idl *idl)
3284 {
3285     struct ovsdb_idl_txn *txn;
3286     const struct ovsrec_open_vswitch *ovs;
3287     enum ovsdb_idl_txn_status status;
3288     struct ovsdb_symbol_table *symtab;
3289     const char *unused;
3290     struct vsctl_command *c;
3291     int64_t next_cfg = 0;
3292     char *error = NULL;
3293
3294     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3295     if (dry_run) {
3296         ovsdb_idl_txn_set_dry_run(txn);
3297     }
3298
3299     ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3300
3301     ovs = ovsrec_open_vswitch_first(idl);
3302     if (!ovs) {
3303         /* XXX add verification that table is empty */
3304         ovs = ovsrec_open_vswitch_insert(txn);
3305     }
3306
3307     if (wait_for_reload) {
3308         struct json *where = where_uuid_equals(&ovs->header_.uuid);
3309         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
3310         json_destroy(where);
3311     }
3312
3313     symtab = ovsdb_symbol_table_create();
3314     for (c = commands; c < &commands[n_commands]; c++) {
3315         ds_init(&c->output);
3316     }
3317     for (c = commands; c < &commands[n_commands]; c++) {
3318         struct vsctl_context ctx;
3319
3320         vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3321         (c->syntax->run)(&ctx);
3322         vsctl_context_done(&ctx, c);
3323
3324         if (ctx.try_again) {
3325             goto try_again;
3326         }
3327     }
3328
3329     status = ovsdb_idl_txn_commit_block(txn);
3330     if (wait_for_reload && status == TXN_SUCCESS) {
3331         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3332     }
3333     if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3334         for (c = commands; c < &commands[n_commands]; c++) {
3335             if (c->syntax->postprocess) {
3336                 struct vsctl_context ctx;
3337
3338                 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3339                 (c->syntax->postprocess)(&ctx);
3340                 vsctl_context_done(&ctx, c);
3341             }
3342         }
3343     }
3344     error = xstrdup(ovsdb_idl_txn_get_error(txn));
3345     ovsdb_idl_txn_destroy(txn);
3346     txn = the_idl_txn = NULL;
3347
3348     unused = ovsdb_symbol_table_find_unused(symtab);
3349     if (unused) {
3350         vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3351                     "with \"-- --id=%s create ...\")", unused, unused);
3352     }
3353
3354     switch (status) {
3355     case TXN_INCOMPLETE:
3356         NOT_REACHED();
3357
3358     case TXN_ABORTED:
3359         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3360         vsctl_fatal("transaction aborted");
3361
3362     case TXN_UNCHANGED:
3363     case TXN_SUCCESS:
3364         break;
3365
3366     case TXN_TRY_AGAIN:
3367         goto try_again;
3368
3369     case TXN_ERROR:
3370         vsctl_fatal("transaction error: %s", error);
3371
3372     default:
3373         NOT_REACHED();
3374     }
3375     free(error);
3376
3377     ovsdb_symbol_table_destroy(symtab);
3378
3379     for (c = commands; c < &commands[n_commands]; c++) {
3380         struct ds *ds = &c->output;
3381
3382         if (oneline) {
3383             size_t j;
3384
3385             ds_chomp(ds, '\n');
3386             for (j = 0; j < ds->length; j++) {
3387                 int ch = ds->string[j];
3388                 switch (ch) {
3389                 case '\n':
3390                     fputs("\\n", stdout);
3391                     break;
3392
3393                 case '\\':
3394                     fputs("\\\\", stdout);
3395                     break;
3396
3397                 default:
3398                     putchar(ch);
3399                 }
3400             }
3401             putchar('\n');
3402         } else {
3403             fputs(ds_cstr(ds), stdout);
3404         }
3405         ds_destroy(&c->output);
3406
3407         smap_destroy(&c->options);
3408     }
3409     free(commands);
3410
3411     if (wait_for_reload && status != TXN_UNCHANGED) {
3412         for (;;) {
3413             ovsdb_idl_run(idl);
3414             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3415                 if (ovs->cur_cfg >= next_cfg) {
3416                     goto done;
3417                 }
3418             }
3419             ovsdb_idl_wait(idl);
3420             poll_block();
3421         }
3422     done: ;
3423     }
3424     ovsdb_idl_destroy(idl);
3425
3426     exit(EXIT_SUCCESS);
3427
3428 try_again:
3429     /* Our transaction needs to be rerun, or a prerequisite was not met.  Free
3430      * resources and return so that the caller can try again. */
3431     if (txn) {
3432         ovsdb_idl_txn_abort(txn);
3433         ovsdb_idl_txn_destroy(txn);
3434     }
3435     ovsdb_symbol_table_destroy(symtab);
3436     for (c = commands; c < &commands[n_commands]; c++) {
3437         ds_destroy(&c->output);
3438     }
3439     free(error);
3440 }
3441
3442 static const struct vsctl_command_syntax all_commands[] = {
3443     /* Open vSwitch commands. */
3444     {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3445
3446     /* Bridge commands. */
3447     {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3448     {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3449     {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3450     {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3451     {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3452     {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3453     {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3454      cmd_br_set_external_id, NULL, "", RW},
3455     {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3456      cmd_br_get_external_id, NULL, "", RO},
3457
3458     /* Port commands. */
3459     {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3460     {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3461      RW},
3462     {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3463      "--may-exist,--fake-iface", RW},
3464     {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3465      "--if-exists,--with-iface", RW},
3466     {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3467
3468     /* Interface commands. */
3469     {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3470     {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3471
3472     /* Controller commands. */
3473     {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3474     {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3475     {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3476      "", RW},
3477     {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3478     {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3479     {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
3480
3481     /* Manager commands. */
3482     {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3483     {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
3484     {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3485
3486     /* SSL commands. */
3487     {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3488     {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3489     {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
3490
3491     /* Switch commands. */
3492     {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
3493
3494     /* Parameter commands. */
3495     {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3496     {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3497     {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3498     {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3499     {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3500     {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3501     {"create", 2, INT_MAX, NULL, cmd_create, post_create, "--id=", RW},
3502     {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3503      RW},
3504     {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3505      RO},
3506
3507     {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},
3508 };
3509