datapath: Add missing definitions for building GRE on older kernels
[openvswitch] / utilities / ovs-vsctl.c
1 /*
2  * Copyright (c) 2009 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 <errno.h>
21 #include <getopt.h>
22 #include <inttypes.h>
23 #include <signal.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "command-line.h"
29 #include "compiler.h"
30 #include "dirs.h"
31 #include "dynamic-string.h"
32 #include "json.h"
33 #include "ovsdb-idl.h"
34 #include "poll-loop.h"
35 #include "svec.h"
36 #include "vswitchd/vswitch-idl.h"
37 #include "timeval.h"
38 #include "util.h"
39
40 #include "vlog.h"
41 #define THIS_MODULE VLM_vsctl
42
43 /* --db: The database server to contact. */
44 static const char *db;
45
46 /* --oneline: Write each command's output as a single line? */
47 static bool oneline;
48
49 /* --dry-run: Do not commit any changes. */
50 static bool dry_run;
51
52 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
53 static bool wait_for_reload = true;
54
55 /* --timeout: Time to wait for a connection to 'db'. */
56 static int timeout = 5;
57
58 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
59 static char *default_db(void);
60 static void usage(void) NO_RETURN;
61 static void parse_options(int argc, char *argv[]);
62
63 static void check_vsctl_command(int argc, char *argv[]);
64 static void do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl);
65
66 int
67 main(int argc, char *argv[])
68 {
69     struct ovsdb_idl *idl;
70     unsigned int seqno;
71     struct ds args;
72     int start, n_commands;
73     int trials;
74     int i;
75
76     set_program_name(argv[0]);
77     signal(SIGPIPE, SIG_IGN);
78     time_init();
79     vlog_init();
80     vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
81     vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
82     parse_options(argc, argv);
83
84     if (timeout) {
85         time_alarm(timeout);
86     }
87
88     /* Log our arguments.  This is often valuable for debugging systems. */
89     ds_init(&args);
90     for (i = 1; i < argc; i++) {
91         ds_put_format(&args, " %s", argv[i]);
92     }
93     VLOG_INFO("Called as%s", ds_cstr(&args));
94     ds_destroy(&args);
95
96     /* Do basic command syntax checking. */
97     n_commands = 0;
98     for (start = i = optind; i <= argc; i++) {
99         if (i == argc || !strcmp(argv[i], "--")) {
100             if (i > start) {
101                 check_vsctl_command(i - start, &argv[start]);
102                 n_commands++;
103             }
104             start = i + 1;
105         }
106     }
107     if (!n_commands) {
108         vsctl_fatal("missing command name (use --help for help)");
109     }
110
111     /* Now execute the commands. */
112     idl = ovsdb_idl_create(db, &ovsrec_idl_class);
113     seqno = ovsdb_idl_get_seqno(idl);
114     trials = 0;
115     for (;;) {
116         unsigned int new_seqno;
117
118         ovsdb_idl_run(idl);
119         new_seqno = ovsdb_idl_get_seqno(idl);
120         if (new_seqno != seqno) {
121             if (++trials > 5) {
122                 vsctl_fatal("too many database inconsistency failures");
123             }
124             do_vsctl(argc - optind, argv + optind, idl);
125             seqno = new_seqno;
126         }
127
128         ovsdb_idl_wait(idl);
129         poll_block();
130     }
131 }
132
133 static void
134 vsctl_fatal(const char *format, ...)
135 {
136     char *message;
137     va_list args;
138
139     va_start(args, format);
140     message = xvasprintf(format, args);
141     va_end(args);
142
143     vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
144     VLOG_ERR("%s", message);
145     ovs_fatal(0, "%s", message);
146 }
147
148 static void
149 parse_options(int argc, char *argv[])
150 {
151     enum {
152         OPT_DB = UCHAR_MAX + 1,
153         OPT_ONELINE,
154         OPT_NO_SYSLOG,
155         OPT_NO_WAIT,
156         OPT_DRY_RUN
157     };
158     static struct option long_options[] = {
159         {"db", required_argument, 0, OPT_DB},
160         {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
161         {"no-wait", no_argument, 0, OPT_NO_WAIT},
162         {"dry-run", no_argument, 0, OPT_DRY_RUN},
163         {"oneline", no_argument, 0, OPT_ONELINE},
164         {"timeout", required_argument, 0, 't'},
165         {"verbose", optional_argument, 0, 'v'},
166         {"help", no_argument, 0, 'h'},
167         {"version", no_argument, 0, 'V'},
168         {0, 0, 0, 0},
169     };
170
171
172     for (;;) {
173         unsigned long int timeout;
174         int c;
175
176         c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
177         if (c == -1) {
178             break;
179         }
180
181         switch (c) {
182         case OPT_DB:
183             db = optarg;
184             break;
185
186         case OPT_ONELINE:
187             oneline = true;
188             break;
189
190         case OPT_NO_SYSLOG:
191             vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
192             break;
193
194         case OPT_NO_WAIT:
195             wait_for_reload = false;
196             break;
197
198         case OPT_DRY_RUN:
199             dry_run = true;
200             break;
201
202         case 'h':
203             usage();
204
205         case 'V':
206             OVS_PRINT_VERSION(0, 0);
207             exit(EXIT_SUCCESS);
208
209         case 't':
210             timeout = strtoul(optarg, NULL, 10);
211             if (timeout < 0) {
212                 ovs_fatal(0, "value %s on -t or --timeout is invalid",
213                           optarg);
214             }
215             break;
216
217         case 'v':
218             vlog_set_verbosity(optarg);
219             break;
220
221         case '?':
222             exit(EXIT_FAILURE);
223
224         default:
225             abort();
226         }
227     }
228
229     if (!db) {
230         db = default_db();
231     }
232 }
233
234 static void
235 usage(void)
236 {
237     printf("%s: ovs-vswitchd management utility\n"
238            "usage: %s [OPTIONS] COMMAND [ARG...]\n",
239            program_name, program_name);
240     printf("\nBridge commands:\n"
241            "  add-br BRIDGE               "
242            "create a new bridge named BRIDGE\n"
243            "  add-br BRIDGE PARENT VLAN   "
244            "create new fake bridge BRIDGE in PARENT on VLAN\n"
245            "  del-br BRIDGE               "
246            "delete BRIDGE and all of its ports\n"
247            "  list-br                     "
248            "print the names of all the bridges\n"
249            "  br-exists BRIDGE            "
250            "test whether BRIDGE exists\n"
251            "  br-to-vlan BRIDGE           "
252            "print the VLAN which BRIDGE is on\n"
253            "  br-to-parent BRIDGE         "
254            "print the parent of BRIDGE\n"
255            "  br-set-external-id BRIDGE KEY VALUE"
256            "  set KEY on BRIDGE to VALUE\n"
257            "  br-set-external-id BRIDGE KEY"
258            "  unset KEY on BRIDGE\n"
259            "  br-get-external-id BRIDGE KEY"
260            "  print value of KEY on BRIDGE\n"
261            "  br-get-external-id BRIDGE"
262            "  list key-value pairs on BRIDGE\n"
263         );
264     printf("\nPort commands:\n"
265            "  list-ports BRIDGE           "
266            "print the names of all the ports on BRIDGE\n"
267            "  add-port BRIDGE PORT        "
268            "add network device PORT to BRIDGE\n"
269            "  add-bond BRIDGE PORT IFACE...  "
270            "add new bonded port PORT in BRIDGE from IFACES\n"
271            "  del-port [BRIDGE] PORT      "
272            "delete PORT (which may be bonded) from BRIDGE\n"
273            "  port-to-br PORT             "
274            "print name of bridge that contains PORT\n"
275            "  port-set-external-id PORT KEY VALUE"
276            "  set KEY on PORT to VALUE\n"
277            "  port-set-external-id PORT KEY"
278            "  unset KEY on PORT\n"
279            "  port-get-external-id PORT KEY"
280            "  print value of KEY on PORT\n"
281            "  port-get-external-id PORT"
282            "  list key-value pairs on PORT\n"
283            "A bond is considered to be a single port.\n"
284         );
285     printf("\nInterface commands (a bond consists of multiple interfaces):\n"
286            "  list-ifaces BRIDGE          "
287            "print the names of all the interfaces on BRIDGE\n"
288            "  iface-to-br IFACE           "
289            "print name of bridge that contains IFACE\n"
290            "  iface-set-external-id IFACE KEY VALUE"
291            "  set KEY on IFACE to VALUE\n"
292            "  iface-set-external-id IFACE KEY"
293            "  unset KEY on IFACE\n"
294            "  iface-get-external-id IFACE KEY"
295            "  print value of KEY on IFACE\n"
296            "  iface-get-external-id IFACE"
297            "  list key-value pairs on IFACE\n"
298         );
299     printf("\nController commands:\n"
300            "  get-controller [BRIDGE]     "
301            "print the controller for BRIDGE\n"
302            "  del-controller [BRIDGE]     "
303            "delete the controller for BRIDGE\n"
304            "  set-controller [BRIDGE] TARGET  "
305            "set the controller for BRIDGE to TARGET\n"
306            "  get-fail-mode [BRIDGE]     "
307            "print the fail-mode for BRIDGE\n"
308            "  del-fail-mode [BRIDGE]     "
309            "delete the fail-mode for BRIDGE\n"
310            "  set-fail-mode [BRIDGE] MODE  "
311            "set the fail-mode for BRIDGE to MODE\n"
312         );
313     printf("\nSSL commands:\n"
314            "  get-ssl              "
315            "print the SSL configuration\n"
316            "  del-ssl              "
317            "delete the SSL configuration\n"
318            "  set-ssl PRIV-KEY CERT CA-CERT  "
319            "set the SSL configuration\n"
320         );
321     printf("\nOptions:\n"
322            "  --db=DATABASE               "
323            "connect to DATABASE\n"
324            "                              "
325            "(default: %s)\n"
326            "  --oneline                   "
327            "print exactly one line of output per command\n",
328            default_db());
329     vlog_usage();
330     printf("\nOther options:\n"
331            "  -h, --help                  "
332            "display this help message\n"
333            "  -V, --version               "
334            "display version information\n");
335     exit(EXIT_SUCCESS);
336 }
337
338 static char *
339 default_db(void)
340 {
341     static char *def;
342     if (!def) {
343         def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
344     }
345     return def;
346 }
347 \f
348 struct vsctl_context {
349     int argc;
350     char **argv;
351     const struct ovsrec_open_vswitch *ovs;
352     struct ds output;
353     struct shash options;
354 };
355
356 struct vsctl_bridge {
357     struct ovsrec_bridge *br_cfg;
358     char *name;
359     struct ovsrec_controller *ctrl;
360     struct vsctl_bridge *parent;
361     int vlan;
362 };
363
364 struct vsctl_port {
365     struct ovsrec_port *port_cfg;
366     struct vsctl_bridge *bridge;
367 };
368
369 struct vsctl_iface {
370     struct ovsrec_interface *iface_cfg;
371     struct vsctl_port *port;
372 };
373
374 struct vsctl_info {
375     struct shash bridges;
376     struct shash ports;
377     struct shash ifaces;
378     struct ovsrec_controller *ctrl;
379 };
380
381 static struct ovsdb_idl_txn *
382 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
383 {
384     return ovsdb_idl_txn_get(&ovs->header_);
385 }
386
387 static struct vsctl_bridge *
388 add_bridge(struct vsctl_info *b,
389            struct ovsrec_bridge *br_cfg, const char *name,
390            struct vsctl_bridge *parent, int vlan)
391 {
392     struct vsctl_bridge *br = xmalloc(sizeof *br);
393     br->br_cfg = br_cfg;
394     br->name = xstrdup(name);
395     br->parent = parent;
396     br->vlan = vlan;
397     br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
398     shash_add(&b->bridges, br->name, br);
399     return br;
400 }
401
402 static bool
403 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
404 {
405     return (port_cfg->fake_bridge
406             && port_cfg->tag
407             && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
408 }
409
410 static struct vsctl_bridge *
411 find_vlan_bridge(struct vsctl_info *info,
412                  struct vsctl_bridge *parent, int vlan)
413 {
414     struct shash_node *node;
415
416     SHASH_FOR_EACH (node, &info->bridges) {
417         struct vsctl_bridge *br = node->data;
418         if (br->parent == parent && br->vlan == vlan) {
419             return br;
420         }
421     }
422
423     return NULL;
424 }
425
426 static void
427 free_info(struct vsctl_info *info)
428 {
429     struct shash_node *node;
430
431     SHASH_FOR_EACH (node, &info->bridges) {
432         struct vsctl_bridge *bridge = node->data;
433         free(bridge->name);
434         free(bridge);
435     }
436     shash_destroy(&info->bridges);
437
438     SHASH_FOR_EACH (node, &info->ports) {
439         struct vsctl_port *port = node->data;
440         free(port);
441     }
442     shash_destroy(&info->ports);
443
444     SHASH_FOR_EACH (node, &info->ifaces) {
445         struct vsctl_iface *iface = node->data;
446         free(iface);
447     }
448     shash_destroy(&info->ifaces);
449 }
450
451 static void
452 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
453 {
454     struct shash bridges, ports;
455     size_t i;
456
457     shash_init(&info->bridges);
458     shash_init(&info->ports);
459     shash_init(&info->ifaces);
460
461     info->ctrl = ovs->controller;
462
463     shash_init(&bridges);
464     shash_init(&ports);
465     for (i = 0; i < ovs->n_bridges; i++) {
466         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
467         struct vsctl_bridge *br;
468         size_t j;
469
470         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
471             VLOG_WARN("%s: database contains duplicate bridge name",
472                       br_cfg->name);
473             continue;
474         }
475         br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
476         if (!br) {
477             continue;
478         }
479
480         for (j = 0; j < br_cfg->n_ports; j++) {
481             struct ovsrec_port *port_cfg = br_cfg->ports[j];
482
483             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
484                 VLOG_WARN("%s: database contains duplicate port name",
485                           port_cfg->name);
486                 continue;
487             }
488
489             if (port_is_fake_bridge(port_cfg)
490                 && shash_add_once(&bridges, port_cfg->name, NULL)) {
491                 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
492             }
493         }
494     }
495     shash_destroy(&bridges);
496     shash_destroy(&ports);
497
498     shash_init(&bridges);
499     shash_init(&ports);
500     for (i = 0; i < ovs->n_bridges; i++) {
501         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
502         struct vsctl_bridge *br;
503         size_t j;
504
505         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
506             continue;
507         }
508         br = shash_find_data(&info->bridges, br_cfg->name);
509         for (j = 0; j < br_cfg->n_ports; j++) {
510             struct ovsrec_port *port_cfg = br_cfg->ports[j];
511             struct vsctl_port *port;
512             size_t k;
513
514             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
515                 continue;
516             }
517
518             if (port_is_fake_bridge(port_cfg)
519                 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
520                 continue;
521             }
522
523             port = xmalloc(sizeof *port);
524             port->port_cfg = port_cfg;
525             if (port_cfg->tag
526                 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
527                 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
528                 if (!port->bridge) {
529                     port->bridge = br;
530                 }
531             } else {
532                 port->bridge = br;
533             }
534             shash_add(&info->ports, port_cfg->name, port);
535
536             for (k = 0; k < port_cfg->n_interfaces; k++) {
537                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
538                 struct vsctl_iface *iface;
539
540                 if (shash_find(&info->ifaces, iface_cfg->name)) {
541                     VLOG_WARN("%s: database contains duplicate interface name",
542                               iface_cfg->name);
543                     continue;
544                 }
545
546                 iface = xmalloc(sizeof *iface);
547                 iface->iface_cfg = iface_cfg;
548                 iface->port = port;
549                 shash_add(&info->ifaces, iface_cfg->name, iface);
550             }
551         }
552     }
553     shash_destroy(&bridges);
554     shash_destroy(&ports);
555 }
556
557 static void
558 check_conflicts(struct vsctl_info *info, const char *name,
559                 char *msg)
560 {
561     struct vsctl_iface *iface;
562     struct vsctl_port *port;
563
564     if (shash_find(&info->bridges, name)) {
565         vsctl_fatal("%s because a bridge named %s already exists",
566                     msg, name);
567     }
568
569     port = shash_find_data(&info->ports, name);
570     if (port) {
571         vsctl_fatal("%s because a port named %s already exists on "
572                     "bridge %s", msg, name, port->bridge->name);
573     }
574
575     iface = shash_find_data(&info->ifaces, name);
576     if (iface) {
577         vsctl_fatal("%s because an interface named %s already exists "
578                     "on bridge %s", msg, name, iface->port->bridge->name);
579     }
580
581     free(msg);
582 }
583
584 static struct vsctl_bridge *
585 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
586 {
587     struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
588     if (must_exist && !br) {
589         vsctl_fatal("no bridge named %s", name);
590     }
591     return br;
592 }
593
594 static struct vsctl_bridge *
595 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
596 {
597     struct vsctl_bridge *br = find_bridge(info, name, must_exist);
598     if (br && br->parent) {
599         vsctl_fatal("%s is a fake bridge", name);
600     }
601     return br;
602 }
603
604 static struct vsctl_port *
605 find_port(struct vsctl_info *info, const char *name, bool must_exist)
606 {
607     struct vsctl_port *port = shash_find_data(&info->ports, name);
608     if (port && !strcmp(name, port->bridge->name)) {
609         port = NULL;
610     }
611     if (must_exist && !port) {
612         vsctl_fatal("no port named %s", name);
613     }
614     return port;
615 }
616
617 static struct vsctl_iface *
618 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
619 {
620     struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
621     if (iface && !strcmp(name, iface->port->bridge->name)) {
622         iface = NULL;
623     }
624     if (must_exist && !iface) {
625         vsctl_fatal("no interface named %s", name);
626     }
627     return iface;
628 }
629
630 static void
631 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
632 {
633     struct ovsrec_port **ports;
634     size_t i;
635
636     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
637     for (i = 0; i < br->n_ports; i++) {
638         ports[i] = br->ports[i];
639     }
640     ports[br->n_ports] = port;
641     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
642     free(ports);
643 }
644
645 static void
646 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
647 {
648     struct ovsrec_port **ports;
649     size_t i, n;
650
651     ports = xmalloc(sizeof *br->ports * br->n_ports);
652     for (i = n = 0; i < br->n_ports; i++) {
653         if (br->ports[i] != port) {
654             ports[n++] = br->ports[i];
655         }
656     }
657     ovsrec_bridge_set_ports(br, ports, n);
658     free(ports);
659 }
660
661 static void
662 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
663                   struct ovsrec_bridge *bridge)
664 {
665     struct ovsrec_bridge **bridges;
666     size_t i;
667
668     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
669     for (i = 0; i < ovs->n_bridges; i++) {
670         bridges[i] = ovs->bridges[i];
671     }
672     bridges[ovs->n_bridges] = bridge;
673     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
674     free(bridges);
675 }
676
677 static void
678 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
679                   struct ovsrec_bridge *bridge)
680 {
681     struct ovsrec_bridge **bridges;
682     size_t i, n;
683
684     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
685     for (i = n = 0; i < ovs->n_bridges; i++) {
686         if (ovs->bridges[i] != bridge) {
687             bridges[n++] = ovs->bridges[i];
688         }
689     }
690     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
691     free(bridges);
692 }
693
694 static void
695 cmd_init(struct vsctl_context *ctx UNUSED)
696 {
697 }
698
699 static void
700 cmd_add_br(struct vsctl_context *ctx)
701 {
702     const char *br_name = ctx->argv[1];
703     struct vsctl_info info;
704
705     get_info(ctx->ovs, &info);
706     check_conflicts(&info, br_name,
707                     xasprintf("cannot create a bridge named %s", br_name));
708
709     if (ctx->argc == 2) {
710         struct ovsrec_bridge *br;
711         struct ovsrec_port *port;
712         struct ovsrec_interface *iface;
713
714         iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
715         ovsrec_interface_set_name(iface, br_name);
716
717         port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
718         ovsrec_port_set_name(port, br_name);
719         ovsrec_port_set_interfaces(port, &iface, 1);
720
721         br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs));
722         ovsrec_bridge_set_name(br, br_name);
723         ovsrec_bridge_set_ports(br, &port, 1);
724
725         ovs_insert_bridge(ctx->ovs, br);
726     } else if (ctx->argc == 3) {
727         vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
728                     ctx->argv[0]);
729     } else if (ctx->argc == 4) {
730         const char *parent_name = ctx->argv[2];
731         int vlan = atoi(ctx->argv[3]);
732         struct ovsrec_bridge *br;
733         struct vsctl_bridge *parent;
734         struct ovsrec_port *port;
735         struct ovsrec_interface *iface;
736         int64_t tag = vlan;
737
738         if (vlan < 1 || vlan > 4095) {
739             vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
740         }
741
742         parent = find_bridge(&info, parent_name, false);
743         if (parent && parent->vlan) {
744             vsctl_fatal("cannot create bridge with fake bridge as parent");
745         }
746         if (!parent) {
747             vsctl_fatal("parent bridge %s does not exist", parent_name);
748         }
749         br = parent->br_cfg;
750
751         iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
752         ovsrec_interface_set_name(iface, br_name);
753         ovsrec_interface_set_type(iface, "internal");
754
755         port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
756         ovsrec_port_set_name(port, br_name);
757         ovsrec_port_set_interfaces(port, &iface, 1);
758         ovsrec_port_set_fake_bridge(port, true);
759         ovsrec_port_set_tag(port, &tag, 1);
760
761         bridge_insert_port(br, port);
762     } else {
763         NOT_REACHED();
764     }
765
766     free_info(&info);
767 }
768
769 static void
770 del_port(struct vsctl_info *info, struct vsctl_port *port)
771 {
772     struct shash_node *node;
773
774     SHASH_FOR_EACH (node, &info->ifaces) {
775         struct vsctl_iface *iface = node->data;
776         if (iface->port == port) {
777             ovsrec_interface_delete(iface->iface_cfg);
778         }
779     }
780     ovsrec_port_delete(port->port_cfg);
781
782     bridge_delete_port((port->bridge->parent
783                         ? port->bridge->parent->br_cfg
784                         : port->bridge->br_cfg), port->port_cfg);
785 }
786
787 static void
788 cmd_del_br(struct vsctl_context *ctx)
789 {
790     bool must_exist = !shash_find(&ctx->options, "--if-exists");
791     struct vsctl_bridge *bridge;
792     struct vsctl_info info;
793
794     get_info(ctx->ovs, &info);
795     bridge = find_bridge(&info, ctx->argv[1], must_exist);
796     if (bridge) {
797         struct shash_node *node;
798
799         SHASH_FOR_EACH (node, &info.ports) {
800             struct vsctl_port *port = node->data;
801             if (port->bridge == bridge
802                 || !strcmp(port->port_cfg->name, bridge->name)) {
803                 del_port(&info, port);
804             }
805         }
806         if (bridge->br_cfg) {
807             ovsrec_bridge_delete(bridge->br_cfg);
808             ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
809         }
810     }
811     free_info(&info);
812 }
813
814 static void
815 output_sorted(struct svec *svec, struct ds *output)
816 {
817     const char *name;
818     size_t i;
819
820     svec_sort(svec);
821     SVEC_FOR_EACH (i, name, svec) {
822         ds_put_format(output, "%s\n", name);
823     }
824 }
825
826 static void
827 cmd_list_br(struct vsctl_context *ctx)
828 {
829     struct shash_node *node;
830     struct vsctl_info info;
831     struct svec bridges;
832
833     get_info(ctx->ovs, &info);
834
835     svec_init(&bridges);
836     SHASH_FOR_EACH (node, &info.bridges) {
837         struct vsctl_bridge *br = node->data;
838         svec_add(&bridges, br->name);
839     }
840     output_sorted(&bridges, &ctx->output);
841     svec_destroy(&bridges);
842
843     free_info(&info);
844 }
845
846 static void
847 cmd_br_exists(struct vsctl_context *ctx)
848 {
849     struct vsctl_info info;
850
851     get_info(ctx->ovs, &info);
852     if (!find_bridge(&info, ctx->argv[1], false)) {
853         exit(2);
854     }
855     free_info(&info);
856 }
857
858 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
859  * equals 'a', false otherwise. */
860 static bool
861 key_matches(const char *a,
862             const char *b_prefix, size_t b_prefix_len, const char *b)
863 {
864     return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
865 }
866
867 static void
868 set_external_id(char **old_keys, char **old_values, size_t old_n,
869                 char *key, char *value,
870                 char ***new_keysp, char ***new_valuesp, size_t *new_np)
871 {
872     char **new_keys;
873     char **new_values;
874     size_t new_n;
875     size_t i;
876
877     new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
878     new_values = xmalloc(sizeof *new_values * (old_n + 1));
879     new_n = 0;
880     for (i = 0; i < old_n; i++) {
881         if (strcmp(key, old_keys[i])) {
882             new_keys[new_n] = old_keys[i];
883             new_values[new_n] = old_values[i];
884             new_n++;
885         }
886     }
887     if (value) {
888         new_keys[new_n] = key;
889         new_values[new_n] = value;
890         new_n++;
891     }
892     *new_keysp = new_keys;
893     *new_valuesp = new_values;
894     *new_np = new_n;
895 }
896
897 static void
898 cmd_br_set_external_id(struct vsctl_context *ctx)
899 {
900     struct vsctl_info info;
901     struct vsctl_bridge *bridge;
902     char **keys, **values;
903     size_t n;
904
905     get_info(ctx->ovs, &info);
906     bridge = find_bridge(&info, ctx->argv[1], true);
907     if (bridge->br_cfg) {
908         set_external_id(bridge->br_cfg->key_external_ids,
909                         bridge->br_cfg->value_external_ids,
910                         bridge->br_cfg->n_external_ids,
911                         ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
912                         &keys, &values, &n);
913         ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
914     } else {
915         char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
916         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
917         set_external_id(port->port_cfg->key_external_ids,
918                         port->port_cfg->value_external_ids,
919                         port->port_cfg->n_external_ids,
920                         key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
921                         &keys, &values, &n);
922         ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
923         free(key);
924     }
925     free(keys);
926     free(values);
927
928     free_info(&info);
929 }
930
931 static void
932 get_external_id(char **keys, char **values, size_t n,
933                 const char *prefix, const char *key,
934                 struct ds *output)
935 {
936     size_t prefix_len = strlen(prefix);
937     struct svec svec;
938     size_t i;
939
940     svec_init(&svec);
941     for (i = 0; i < n; i++) {
942         if (!key && !strncmp(keys[i], prefix, prefix_len)) {
943             svec_add_nocopy(&svec, xasprintf("%s=%s",
944                                              keys[i] + prefix_len, values[i]));
945         } else if (key_matches(keys[i], prefix, prefix_len, key)) {
946             svec_add(&svec, values[i]);
947             break;
948         }
949     }
950     output_sorted(&svec, output);
951     svec_destroy(&svec);
952 }
953
954 static void
955 cmd_br_get_external_id(struct vsctl_context *ctx)
956 {
957     struct vsctl_info info;
958     struct vsctl_bridge *bridge;
959
960     get_info(ctx->ovs, &info);
961     bridge = find_bridge(&info, ctx->argv[1], true);
962     if (bridge->br_cfg) {
963         get_external_id(bridge->br_cfg->key_external_ids,
964                         bridge->br_cfg->value_external_ids,
965                         bridge->br_cfg->n_external_ids,
966                         "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
967                         &ctx->output);
968     } else {
969         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
970         get_external_id(port->port_cfg->key_external_ids,
971                         port->port_cfg->value_external_ids,
972                         port->port_cfg->n_external_ids,
973                         "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
974     }
975     free_info(&info);
976 }
977
978
979 static void
980 cmd_list_ports(struct vsctl_context *ctx)
981 {
982     struct vsctl_bridge *br;
983     struct shash_node *node;
984     struct vsctl_info info;
985     struct svec ports;
986
987     get_info(ctx->ovs, &info);
988     br = find_bridge(&info, ctx->argv[1], true);
989
990     svec_init(&ports);
991     SHASH_FOR_EACH (node, &info.ports) {
992         struct vsctl_port *port = node->data;
993
994         if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
995             svec_add(&ports, port->port_cfg->name);
996         }
997     }
998     output_sorted(&ports, &ctx->output);
999     svec_destroy(&ports);
1000
1001     free_info(&info);
1002 }
1003
1004 static void
1005 add_port(const struct ovsrec_open_vswitch *ovs,
1006          const char *br_name, const char *port_name,
1007          char *iface_names[], int n_ifaces)
1008 {
1009     struct vsctl_info info;
1010     struct vsctl_bridge *bridge;
1011     struct ovsrec_interface **ifaces;
1012     struct ovsrec_port *port;
1013     size_t i;
1014
1015     get_info(ovs, &info);
1016     check_conflicts(&info, port_name,
1017                     xasprintf("cannot create a port named %s", port_name));
1018     /* XXX need to check for conflicts on interfaces too */
1019     bridge = find_bridge(&info, br_name, true);
1020
1021     ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1022     for (i = 0; i < n_ifaces; i++) {
1023         ifaces[i] = ovsrec_interface_insert(txn_from_openvswitch(ovs));
1024         ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1025     }
1026
1027     port = ovsrec_port_insert(txn_from_openvswitch(ovs));
1028     ovsrec_port_set_name(port, port_name);
1029     ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1030     free(ifaces);
1031
1032     if (bridge->vlan) {
1033         int64_t tag = bridge->vlan;
1034         ovsrec_port_set_tag(port, &tag, 1);
1035     }
1036
1037     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1038                         : bridge->br_cfg), port);
1039
1040     free_info(&info);
1041 }
1042
1043 static void
1044 cmd_add_port(struct vsctl_context *ctx)
1045 {
1046     add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[2], 1);
1047 }
1048
1049 static void
1050 cmd_add_bond(struct vsctl_context *ctx)
1051 {
1052     add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[3], ctx->argc - 3);
1053 }
1054
1055 static void
1056 cmd_del_port(struct vsctl_context *ctx)
1057 {
1058     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1059     struct vsctl_info info;
1060
1061     get_info(ctx->ovs, &info);
1062     if (ctx->argc == 2) {
1063         struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1064         if (port) {
1065             del_port(&info, port);
1066         }
1067     } else if (ctx->argc == 3) {
1068         struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1069         struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1070
1071         if (port) {
1072             if (port->bridge == bridge) {
1073                 del_port(&info, port);
1074             } else if (port->bridge->parent == bridge) {
1075                 vsctl_fatal("bridge %s does not have a port %s (although its "
1076                             "parent bridge %s does)",
1077                             ctx->argv[1], ctx->argv[2], bridge->parent->name);
1078             } else {
1079                 vsctl_fatal("bridge %s does not have a port %s",
1080                             ctx->argv[1], ctx->argv[2]);
1081             }
1082         }
1083     }
1084     free_info(&info);
1085 }
1086
1087 static void
1088 cmd_port_to_br(struct vsctl_context *ctx)
1089 {
1090     struct vsctl_port *port;
1091     struct vsctl_info info;
1092
1093     get_info(ctx->ovs, &info);
1094     port = find_port(&info, ctx->argv[1], true);
1095     ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1096     free_info(&info);
1097 }
1098
1099 static void
1100 cmd_port_set_external_id(struct vsctl_context *ctx)
1101 {
1102     struct vsctl_info info;
1103     struct vsctl_port *port;
1104     char **keys, **values;
1105     size_t n;
1106
1107     get_info(ctx->ovs, &info);
1108     port = find_port(&info, ctx->argv[1], true);
1109     set_external_id(port->port_cfg->key_external_ids,
1110                     port->port_cfg->value_external_ids,
1111                     port->port_cfg->n_external_ids,
1112                     ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1113                     &keys, &values, &n);
1114     ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1115     free(keys);
1116     free(values);
1117
1118     free_info(&info);
1119 }
1120
1121 static void
1122 cmd_port_get_external_id(struct vsctl_context *ctx)
1123 {
1124     struct vsctl_info info;
1125     struct vsctl_port *port;
1126
1127     get_info(ctx->ovs, &info);
1128     port = find_port(&info, ctx->argv[1], true);
1129     get_external_id(port->port_cfg->key_external_ids,
1130                     port->port_cfg->value_external_ids,
1131                     port->port_cfg->n_external_ids,
1132                     "",  ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1133     free_info(&info);
1134 }
1135
1136 static void
1137 cmd_br_to_vlan(struct vsctl_context *ctx)
1138 {
1139     struct vsctl_bridge *bridge;
1140     struct vsctl_info info;
1141
1142     get_info(ctx->ovs, &info);
1143     bridge = find_bridge(&info, ctx->argv[1], true);
1144     ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1145     free_info(&info);
1146 }
1147
1148 static void
1149 cmd_br_to_parent(struct vsctl_context *ctx)
1150 {
1151     struct vsctl_bridge *bridge;
1152     struct vsctl_info info;
1153
1154     get_info(ctx->ovs, &info);
1155     bridge = find_bridge(&info, ctx->argv[1], true);
1156     if (bridge->parent) {
1157         bridge = bridge->parent;
1158     }
1159     ds_put_format(&ctx->output, "%s\n", bridge->name);
1160     free_info(&info);
1161 }
1162
1163 static void
1164 cmd_list_ifaces(struct vsctl_context *ctx)
1165 {
1166     struct vsctl_bridge *br;
1167     struct shash_node *node;
1168     struct vsctl_info info;
1169     struct svec ifaces;
1170
1171     get_info(ctx->ovs, &info);
1172     br = find_bridge(&info, ctx->argv[1], true);
1173
1174     svec_init(&ifaces);
1175     SHASH_FOR_EACH (node, &info.ifaces) {
1176         struct vsctl_iface *iface = node->data;
1177
1178         if (strcmp(iface->iface_cfg->name, br->name)
1179             && br == iface->port->bridge) {
1180             svec_add(&ifaces, iface->iface_cfg->name);
1181         }
1182     }
1183     output_sorted(&ifaces, &ctx->output);
1184     svec_destroy(&ifaces);
1185
1186     free_info(&info);
1187 }
1188
1189 static void
1190 cmd_iface_to_br(struct vsctl_context *ctx)
1191 {
1192     struct vsctl_iface *iface;
1193     struct vsctl_info info;
1194
1195     get_info(ctx->ovs, &info);
1196     iface = find_iface(&info, ctx->argv[1], true);
1197     ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1198     free_info(&info);
1199 }
1200
1201 static void
1202 cmd_iface_set_external_id(struct vsctl_context *ctx)
1203 {
1204     struct vsctl_info info;
1205     struct vsctl_iface *iface;
1206     char **keys, **values;
1207     size_t n;
1208
1209     get_info(ctx->ovs, &info);
1210     iface = find_iface(&info, ctx->argv[1], true);
1211     set_external_id(iface->iface_cfg->key_external_ids,
1212                     iface->iface_cfg->value_external_ids,
1213                     iface->iface_cfg->n_external_ids,
1214                     ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1215                     &keys, &values, &n);
1216     ovsrec_interface_set_external_ids(iface->iface_cfg, keys, values, n);
1217     free(keys);
1218     free(values);
1219
1220     free_info(&info);
1221 }
1222
1223 static void
1224 cmd_iface_get_external_id(struct vsctl_context *ctx)
1225 {
1226     struct vsctl_info info;
1227     struct vsctl_iface *iface;
1228
1229     get_info(ctx->ovs, &info);
1230     iface = find_iface(&info, ctx->argv[1], true);
1231     get_external_id(iface->iface_cfg->key_external_ids,
1232                     iface->iface_cfg->value_external_ids,
1233                     iface->iface_cfg->n_external_ids,
1234                     "",  ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1235     free_info(&info);
1236 }
1237
1238 static void
1239 cmd_get_controller(struct vsctl_context *ctx)
1240 {
1241     struct vsctl_info info;
1242
1243     get_info(ctx->ovs, &info);
1244
1245     if (ctx->argc == 1) {
1246         /* Return the controller from the "Open_vSwitch" table */
1247         if (info.ctrl) {
1248             ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1249         }
1250     } else {
1251         /* Return the controller for a particular bridge. */
1252         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1253
1254         /* If no controller is explicitly defined for the requested
1255          * bridge, fallback to the "Open_vSwitch" table's controller. */
1256         if (br->ctrl) {
1257             ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1258         } else if (info.ctrl) {
1259             ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1260         }
1261     }
1262
1263     free_info(&info);
1264 }
1265
1266 static void
1267 cmd_del_controller(struct vsctl_context *ctx)
1268 {
1269     struct vsctl_info info;
1270
1271     get_info(ctx->ovs, &info);
1272
1273     if (ctx->argc == 1) {
1274         if (info.ctrl) {
1275             ovsrec_controller_delete(info.ctrl);
1276             ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1277         }
1278     } else {
1279         struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1280
1281         if (br->ctrl) {
1282             ovsrec_controller_delete(br->ctrl);
1283             ovsrec_bridge_set_controller(br->br_cfg, NULL);
1284         }
1285     }
1286
1287     free_info(&info);
1288 }
1289
1290 static void
1291 cmd_set_controller(struct vsctl_context *ctx)
1292 {
1293     struct vsctl_info info;
1294     struct ovsrec_controller *ctrl;
1295
1296     get_info(ctx->ovs, &info);
1297
1298     if (ctx->argc == 2) {
1299         /* Set the controller in the "Open_vSwitch" table. */
1300         if (info.ctrl) {
1301             ovsrec_controller_delete(info.ctrl);
1302         }
1303         ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1304         ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1305         ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1306     } else {
1307         /* Set the controller for a particular bridge. */
1308         struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1309
1310         if (br->ctrl) {
1311             ovsrec_controller_delete(br->ctrl);
1312         }
1313         ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1314         ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1315         ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1316     }
1317
1318     free_info(&info);
1319 }
1320
1321 static void
1322 cmd_get_fail_mode(struct vsctl_context *ctx)
1323 {
1324     struct vsctl_info info;
1325     const char *fail_mode = NULL;
1326
1327     get_info(ctx->ovs, &info);
1328
1329     if (ctx->argc == 1) {
1330         /* Return the fail-mode from the "Open_vSwitch" table */
1331         if (info.ctrl && info.ctrl->fail_mode) {
1332             fail_mode = info.ctrl->fail_mode;
1333         }
1334     } else {
1335         /* Return the fail-mode for a particular bridge. */
1336         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1337
1338         /* If no controller or fail-mode is explicitly defined for the 
1339          * requested bridge, fallback to the "Open_vSwitch" table's 
1340          * setting. */
1341         if (br->ctrl && br->ctrl->fail_mode) {
1342             fail_mode = br->ctrl->fail_mode;
1343         } else if (info.ctrl && info.ctrl->fail_mode) {
1344             fail_mode = info.ctrl->fail_mode;
1345         }
1346     }
1347
1348     if (fail_mode && strlen(fail_mode)) {
1349         ds_put_format(&ctx->output, "%s\n", info.ctrl->fail_mode);
1350     }
1351
1352     free_info(&info);
1353 }
1354
1355 static void
1356 cmd_del_fail_mode(struct vsctl_context *ctx)
1357 {
1358     struct vsctl_info info;
1359
1360     get_info(ctx->ovs, &info);
1361
1362     if (ctx->argc == 1) {
1363         if (info.ctrl && info.ctrl->fail_mode) {
1364             ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1365         }
1366     } else {
1367         struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1368
1369         if (br->ctrl && br->ctrl->fail_mode) {
1370             ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1371         }
1372     }
1373
1374     free_info(&info);
1375 }
1376
1377 static void
1378 cmd_set_fail_mode(struct vsctl_context *ctx)
1379 {
1380     struct vsctl_info info;
1381     const char *fail_mode;
1382
1383     get_info(ctx->ovs, &info);
1384
1385     fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1386
1387     if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1388         vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1389     }
1390
1391     if (ctx->argc == 2) {
1392         /* Set the fail-mode in the "Open_vSwitch" table. */
1393         if (!info.ctrl) {
1394             vsctl_fatal("no controller declared");
1395         }
1396         ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1397     } else {
1398         struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1399
1400         if (!br->ctrl) {
1401             vsctl_fatal("no controller declared for %s", br->name);
1402         }
1403         ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1404     }
1405
1406     free_info(&info);
1407 }
1408
1409 static void
1410 cmd_get_ssl(struct vsctl_context *ctx)
1411 {
1412     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1413
1414     if (ssl) {
1415         ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1416         ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1417         ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1418         ds_put_format(&ctx->output, "Bootstrap: %s\n",
1419                 ssl->bootstrap_ca_cert ? "true" : "false");
1420     }
1421 }
1422
1423 static void
1424 cmd_del_ssl(struct vsctl_context *ctx)
1425 {
1426     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1427
1428     if (ssl) {
1429         ovsrec_ssl_delete(ssl);
1430         ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1431     }
1432 }
1433
1434 static void
1435 cmd_set_ssl(struct vsctl_context *ctx)
1436 {
1437     bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1438     struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1439
1440     if (ssl) {
1441         ovsrec_ssl_delete(ssl);
1442     }
1443     ssl = ovsrec_ssl_insert(txn_from_openvswitch(ctx->ovs));
1444
1445     ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1446     ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1447     ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1448
1449     ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1450
1451     ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1452 }
1453 \f
1454 typedef void vsctl_handler_func(struct vsctl_context *);
1455
1456 struct vsctl_command {
1457     const char *name;
1458     int min_args;
1459     int max_args;
1460     vsctl_handler_func *handler;
1461     const char *options;
1462 };
1463
1464 static void run_vsctl_command(int argc, char *argv[],
1465                               const struct ovsrec_open_vswitch *ovs,
1466                               struct ds *output);
1467
1468 static struct json *
1469 where_uuid_equals(const struct uuid *uuid)
1470 {
1471     return
1472         json_array_create_1(
1473             json_array_create_3(
1474                 json_string_create("_uuid"),
1475                 json_string_create("=="),
1476                 json_array_create_2(
1477                     json_string_create("uuid"),
1478                     json_string_create_nocopy(
1479                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1480 }
1481
1482 static void
1483 do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl)
1484 {
1485     struct ovsdb_idl_txn *txn;
1486     const struct ovsrec_open_vswitch *ovs;
1487     enum ovsdb_idl_txn_status status;
1488     struct ds comment, *output;
1489     int64_t next_cfg;
1490     int n_output;
1491     int i, start;
1492
1493     txn = ovsdb_idl_txn_create(idl);
1494     if (dry_run) {
1495         ovsdb_idl_txn_set_dry_run(txn);
1496     }
1497
1498     ds_init(&comment);
1499     ds_put_cstr(&comment, "ovs-vsctl:");
1500     for (i = 0; i < argc; i++) {
1501         ds_put_format(&comment, " %s", argv[i]);
1502     }
1503     ovsdb_idl_txn_add_comment(txn, ds_cstr(&comment));
1504     ds_destroy(&comment);
1505
1506     ovs = ovsrec_open_vswitch_first(idl);
1507     if (!ovs) {
1508         /* XXX add verification that table is empty */
1509         ovs = ovsrec_open_vswitch_insert(txn);
1510     }
1511
1512     if (wait_for_reload) {
1513         struct json *where = where_uuid_equals(&ovs->header_.uuid);
1514         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg",
1515                                 where);
1516         json_destroy(where);
1517     }
1518
1519     output = xmalloc(argc * sizeof *output);
1520     n_output = 0;
1521     for (start = i = 0; i <= argc; i++) {
1522         if (i == argc || !strcmp(argv[i], "--")) {
1523             if (i > start) {
1524                 ds_init(&output[n_output]);
1525                 run_vsctl_command(i - start, &argv[start], ovs,
1526                                   &output[n_output++]);
1527             }
1528             start = i + 1;
1529         }
1530     }
1531
1532     while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1533         ovsdb_idl_run(idl);
1534         ovsdb_idl_wait(idl);
1535         ovsdb_idl_txn_wait(txn);
1536         poll_block();
1537     }
1538     if (wait_for_reload && status == TXN_SUCCESS) {
1539         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
1540     }
1541     ovsdb_idl_txn_destroy(txn);
1542
1543     switch (status) {
1544     case TXN_INCOMPLETE:
1545         NOT_REACHED();
1546
1547     case TXN_ABORTED:
1548         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1549         vsctl_fatal("transaction aborted");
1550
1551     case TXN_UNCHANGED:
1552     case TXN_SUCCESS:
1553         break;
1554
1555     case TXN_TRY_AGAIN:
1556         for (i = 0; i < n_output; i++) {
1557             ds_destroy(&output[i]);
1558         }
1559         return;
1560
1561     case TXN_ERROR:
1562         vsctl_fatal("transaction error");
1563
1564     default:
1565         NOT_REACHED();
1566     }
1567
1568     for (i = 0; i < n_output; i++) {
1569         struct ds *ds = &output[i];
1570         if (oneline) {
1571             size_t j;
1572
1573             ds_chomp(ds, '\n');
1574             for (j = 0; j < ds->length; j++) {
1575                 int c = ds->string[j];
1576                 switch (c) {
1577                 case '\n':
1578                     fputs("\\n", stdout);
1579                     break;
1580
1581                 case '\\':
1582                     fputs("\\\\", stdout);
1583                     break;
1584
1585                 default:
1586                     putchar(c);
1587                 }
1588             }
1589             putchar('\n');
1590         } else {
1591             fputs(ds_cstr(ds), stdout);
1592         }
1593     }
1594
1595     if (wait_for_reload && status != TXN_UNCHANGED) {
1596         for (;;) {
1597             const struct ovsrec_open_vswitch *ovs;
1598
1599             ovsdb_idl_run(idl);
1600             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
1601                 if (ovs->cur_cfg >= next_cfg) {
1602                     goto done;
1603                 }
1604             }
1605             ovsdb_idl_wait(idl);
1606             poll_block();
1607         }
1608     done: ;
1609     }
1610
1611     exit(EXIT_SUCCESS);
1612 }
1613
1614 static vsctl_handler_func *
1615 get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx)
1616 {
1617     static const struct vsctl_command all_commands[] = {
1618         /* Open vSwitch commands. */
1619         {"init", 0, 0, cmd_init, ""},
1620
1621         /* Bridge commands. */
1622         {"add-br", 1, 3, cmd_add_br, ""},
1623         {"del-br", 1, 1, cmd_del_br, "--if-exists"},
1624         {"list-br", 0, 0, cmd_list_br, ""},
1625         {"br-exists", 1, 1, cmd_br_exists, ""},
1626         {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""},
1627         {"br-to-parent", 1, 1, cmd_br_to_parent, ""},
1628         {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""},
1629         {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""},
1630
1631         /* Port commands. */
1632         {"list-ports", 1, 1, cmd_list_ports, ""},
1633         {"add-port", 2, 2, cmd_add_port, ""},
1634         {"add-bond", 4, INT_MAX, cmd_add_bond, ""},
1635         {"del-port", 1, 2, cmd_del_port, "--if-exists"},
1636         {"port-to-br", 1, 1, cmd_port_to_br, ""},
1637         {"port-set-external-id", 2, 3, cmd_port_set_external_id, ""},
1638         {"port-get-external-id", 1, 2, cmd_port_get_external_id, ""},
1639
1640         /* Interface commands. */
1641         {"list-ifaces", 1, 1, cmd_list_ifaces, ""},
1642         {"iface-to-br", 1, 1, cmd_iface_to_br, ""},
1643         {"iface-set-external-id", 2, 3, cmd_iface_set_external_id, ""},
1644         {"iface-get-external-id", 1, 2, cmd_iface_get_external_id, ""},
1645
1646         /* Controller commands. */
1647         {"get-controller", 0, 1, cmd_get_controller, ""},
1648         {"del-controller", 0, 1, cmd_del_controller, ""},
1649         {"set-controller", 1, 2, cmd_set_controller, ""},
1650         {"get-fail-mode", 0, 1, cmd_get_fail_mode, ""},
1651         {"del-fail-mode", 0, 1, cmd_del_fail_mode, ""},
1652         {"set-fail-mode", 1, 2, cmd_set_fail_mode, ""},
1653
1654         /* SSL commands. */
1655         {"get-ssl", 0, 0, cmd_get_ssl, ""},
1656         {"del-ssl", 0, 0, cmd_del_ssl, ""},
1657         {"set-ssl", 3, 3, cmd_set_ssl, "--bootstrap"},
1658     };
1659
1660     const struct vsctl_command *p;
1661     int i;
1662
1663     shash_init(&ctx->options);
1664     for (i = 0; i < argc; i++) {
1665         if (argv[i][0] != '-') {
1666             break;
1667         }
1668         if (!shash_add_once(&ctx->options, argv[i], NULL)) {
1669             vsctl_fatal("'%s' option specified multiple times", argv[i]);
1670         }
1671     }
1672     if (i == argc) {
1673         vsctl_fatal("missing command name");
1674     }
1675
1676     for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) {
1677         if (!strcmp(p->name, argv[i])) {
1678             struct shash_node *node;
1679             int n_arg;
1680
1681             SHASH_FOR_EACH (node, &ctx->options) {
1682                 const char *s = strstr(p->options, node->name);
1683                 int end = s ? s[strlen(node->name)] : EOF;
1684                 if (end != ',' && end != ' ' && end != '\0') {
1685                     vsctl_fatal("'%s' command has no '%s' option",
1686                                 argv[i], node->name);
1687                 }
1688             }
1689
1690             n_arg = argc - i - 1;
1691             if (n_arg < p->min_args) {
1692                 vsctl_fatal("'%s' command requires at least %d arguments",
1693                             p->name, p->min_args);
1694             } else if (n_arg > p->max_args) {
1695                 vsctl_fatal("'%s' command takes at most %d arguments",
1696                             p->name, p->max_args);
1697             } else {
1698                 ctx->argc = n_arg + 1;
1699                 ctx->argv = &argv[i];
1700                 return p->handler;
1701             }
1702         }
1703     }
1704
1705     vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
1706 }
1707
1708 static void
1709 check_vsctl_command(int argc, char *argv[])
1710 {
1711     struct vsctl_context ctx;
1712
1713     get_vsctl_handler(argc, argv, &ctx);
1714     shash_destroy(&ctx.options);
1715 }
1716
1717 static void
1718 run_vsctl_command(int argc, char *argv[],
1719                   const struct ovsrec_open_vswitch *ovs, struct ds *output)
1720 {
1721     vsctl_handler_func *function;
1722     struct vsctl_context ctx;
1723
1724     function = get_vsctl_handler(argc, argv, &ctx);
1725     ctx.ovs = ovs;
1726     ds_init(&ctx.output);
1727     function(&ctx);
1728     *output = ctx.output;
1729     shash_destroy(&ctx.options);
1730 }