ovs-vsctl: Fix segfault with fake bridges.
[sliver-openvswitch.git] / 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("\nOptions:\n"
314            "  --db=DATABASE               "
315            "connect to DATABASE\n"
316            "                              "
317            "(default: %s)\n"
318            "  --oneline                   "
319            "print exactly one line of output per command\n",
320            default_db());
321     vlog_usage();
322     printf("\nOther options:\n"
323            "  -h, --help                  "
324            "display this help message\n"
325            "  -V, --version               "
326            "display version information\n");
327     exit(EXIT_SUCCESS);
328 }
329
330 static char *
331 default_db(void)
332 {
333     static char *def;
334     if (!def) {
335         def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
336     }
337     return def;
338 }
339 \f
340 struct vsctl_context {
341     int argc;
342     char **argv;
343     const struct ovsrec_open_vswitch *ovs;
344     struct ds output;
345     struct shash options;
346 };
347
348 struct vsctl_bridge {
349     struct ovsrec_bridge *br_cfg;
350     char *name;
351     struct ovsrec_controller *ctrl;
352     struct vsctl_bridge *parent;
353     int vlan;
354 };
355
356 struct vsctl_port {
357     struct ovsrec_port *port_cfg;
358     struct vsctl_bridge *bridge;
359 };
360
361 struct vsctl_iface {
362     struct ovsrec_interface *iface_cfg;
363     struct vsctl_port *port;
364 };
365
366 struct vsctl_info {
367     struct shash bridges;
368     struct shash ports;
369     struct shash ifaces;
370     struct ovsrec_controller *ctrl;
371 };
372
373 static struct ovsdb_idl_txn *
374 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
375 {
376     return ovsdb_idl_txn_get(&ovs->header_);
377 }
378
379 static struct vsctl_bridge *
380 add_bridge(struct vsctl_info *b,
381            struct ovsrec_bridge *br_cfg, const char *name,
382            struct vsctl_bridge *parent, int vlan)
383 {
384     struct vsctl_bridge *br = xmalloc(sizeof *br);
385     br->br_cfg = br_cfg;
386     br->name = xstrdup(name);
387     br->parent = parent;
388     br->vlan = vlan;
389     br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
390     shash_add(&b->bridges, br->name, br);
391     return br;
392 }
393
394 static bool
395 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
396 {
397     return (port_cfg->fake_bridge
398             && port_cfg->tag
399             && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
400 }
401
402 static struct vsctl_bridge *
403 find_vlan_bridge(struct vsctl_info *info,
404                  struct vsctl_bridge *parent, int vlan)
405 {
406     struct shash_node *node;
407
408     SHASH_FOR_EACH (node, &info->bridges) {
409         struct vsctl_bridge *br = node->data;
410         if (br->parent == parent && br->vlan == vlan) {
411             return br;
412         }
413     }
414
415     return NULL;
416 }
417
418 static void
419 free_info(struct vsctl_info *info)
420 {
421     struct shash_node *node;
422
423     SHASH_FOR_EACH (node, &info->bridges) {
424         struct vsctl_bridge *bridge = node->data;
425         free(bridge->name);
426         free(bridge);
427     }
428     shash_destroy(&info->bridges);
429
430     SHASH_FOR_EACH (node, &info->ports) {
431         struct vsctl_port *port = node->data;
432         free(port);
433     }
434     shash_destroy(&info->ports);
435
436     SHASH_FOR_EACH (node, &info->ifaces) {
437         struct vsctl_iface *iface = node->data;
438         free(iface);
439     }
440     shash_destroy(&info->ifaces);
441 }
442
443 static void
444 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
445 {
446     struct shash bridges, ports;
447     size_t i;
448
449     shash_init(&info->bridges);
450     shash_init(&info->ports);
451     shash_init(&info->ifaces);
452
453     info->ctrl = ovs->controller;
454
455     shash_init(&bridges);
456     shash_init(&ports);
457     for (i = 0; i < ovs->n_bridges; i++) {
458         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
459         struct vsctl_bridge *br;
460         size_t j;
461
462         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
463             VLOG_WARN("%s: database contains duplicate bridge name",
464                       br_cfg->name);
465             continue;
466         }
467         br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
468         if (!br) {
469             continue;
470         }
471
472         for (j = 0; j < br_cfg->n_ports; j++) {
473             struct ovsrec_port *port_cfg = br_cfg->ports[j];
474
475             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
476                 VLOG_WARN("%s: database contains duplicate port name",
477                           port_cfg->name);
478                 continue;
479             }
480
481             if (port_is_fake_bridge(port_cfg)
482                 && shash_add_once(&bridges, port_cfg->name, NULL)) {
483                 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
484             }
485         }
486     }
487     shash_destroy(&bridges);
488     shash_destroy(&ports);
489
490     shash_init(&bridges);
491     shash_init(&ports);
492     for (i = 0; i < ovs->n_bridges; i++) {
493         struct ovsrec_bridge *br_cfg = ovs->bridges[i];
494         struct vsctl_bridge *br;
495         size_t j;
496
497         if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
498             continue;
499         }
500         br = shash_find_data(&info->bridges, br_cfg->name);
501         for (j = 0; j < br_cfg->n_ports; j++) {
502             struct ovsrec_port *port_cfg = br_cfg->ports[j];
503             struct vsctl_port *port;
504             size_t k;
505
506             if (!shash_add_once(&ports, port_cfg->name, NULL)) {
507                 continue;
508             }
509
510             if (port_is_fake_bridge(port_cfg)
511                 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
512                 continue;
513             }
514
515             port = xmalloc(sizeof *port);
516             port->port_cfg = port_cfg;
517             if (port_cfg->tag
518                 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
519                 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
520                 if (!port->bridge) {
521                     port->bridge = br;
522                 }
523             } else {
524                 port->bridge = br;
525             }
526             shash_add(&info->ports, port_cfg->name, port);
527
528             for (k = 0; k < port_cfg->n_interfaces; k++) {
529                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
530                 struct vsctl_iface *iface;
531
532                 if (shash_find(&info->ifaces, iface_cfg->name)) {
533                     VLOG_WARN("%s: database contains duplicate interface name",
534                               iface_cfg->name);
535                     continue;
536                 }
537
538                 iface = xmalloc(sizeof *iface);
539                 iface->iface_cfg = iface_cfg;
540                 iface->port = port;
541                 shash_add(&info->ifaces, iface_cfg->name, iface);
542             }
543         }
544     }
545     shash_destroy(&bridges);
546     shash_destroy(&ports);
547 }
548
549 static void
550 check_conflicts(struct vsctl_info *info, const char *name,
551                 char *msg)
552 {
553     struct vsctl_iface *iface;
554     struct vsctl_port *port;
555
556     if (shash_find(&info->bridges, name)) {
557         vsctl_fatal("%s because a bridge named %s already exists",
558                     msg, name);
559     }
560
561     port = shash_find_data(&info->ports, name);
562     if (port) {
563         vsctl_fatal("%s because a port named %s already exists on "
564                     "bridge %s", msg, name, port->bridge->name);
565     }
566
567     iface = shash_find_data(&info->ifaces, name);
568     if (iface) {
569         vsctl_fatal("%s because an interface named %s already exists "
570                     "on bridge %s", msg, name, iface->port->bridge->name);
571     }
572
573     free(msg);
574 }
575
576 static struct vsctl_bridge *
577 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
578 {
579     struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
580     if (must_exist && !br) {
581         vsctl_fatal("no bridge named %s", name);
582     }
583     return br;
584 }
585
586 static struct vsctl_port *
587 find_port(struct vsctl_info *info, const char *name, bool must_exist)
588 {
589     struct vsctl_port *port = shash_find_data(&info->ports, name);
590     if (port && !strcmp(name, port->bridge->name)) {
591         port = NULL;
592     }
593     if (must_exist && !port) {
594         vsctl_fatal("no port named %s", name);
595     }
596     return port;
597 }
598
599 static struct vsctl_iface *
600 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
601 {
602     struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
603     if (iface && !strcmp(name, iface->port->bridge->name)) {
604         iface = NULL;
605     }
606     if (must_exist && !iface) {
607         vsctl_fatal("no interface named %s", name);
608     }
609     return iface;
610 }
611
612 static void
613 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
614 {
615     struct ovsrec_port **ports;
616     size_t i;
617
618     ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
619     for (i = 0; i < br->n_ports; i++) {
620         ports[i] = br->ports[i];
621     }
622     ports[br->n_ports] = port;
623     ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
624     free(ports);
625 }
626
627 static void
628 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
629 {
630     struct ovsrec_port **ports;
631     size_t i, n;
632
633     ports = xmalloc(sizeof *br->ports * br->n_ports);
634     for (i = n = 0; i < br->n_ports; i++) {
635         if (br->ports[i] != port) {
636             ports[n++] = br->ports[i];
637         }
638     }
639     ovsrec_bridge_set_ports(br, ports, n);
640     free(ports);
641 }
642
643 static void
644 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
645                   struct ovsrec_bridge *bridge)
646 {
647     struct ovsrec_bridge **bridges;
648     size_t i;
649
650     bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
651     for (i = 0; i < ovs->n_bridges; i++) {
652         bridges[i] = ovs->bridges[i];
653     }
654     bridges[ovs->n_bridges] = bridge;
655     ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
656     free(bridges);
657 }
658
659 static void
660 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
661                   struct ovsrec_bridge *bridge)
662 {
663     struct ovsrec_bridge **bridges;
664     size_t i, n;
665
666     bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
667     for (i = n = 0; i < ovs->n_bridges; i++) {
668         if (ovs->bridges[i] != bridge) {
669             bridges[n++] = ovs->bridges[i];
670         }
671     }
672     ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
673     free(bridges);
674 }
675
676 static void
677 cmd_init(struct vsctl_context *ctx UNUSED)
678 {
679 }
680
681 static void
682 cmd_add_br(struct vsctl_context *ctx)
683 {
684     const char *br_name = ctx->argv[1];
685     struct vsctl_info info;
686
687     get_info(ctx->ovs, &info);
688     check_conflicts(&info, br_name,
689                     xasprintf("cannot create a bridge named %s", br_name));
690
691     if (ctx->argc == 2) {
692         struct ovsrec_bridge *br;
693         struct ovsrec_port *port;
694         struct ovsrec_interface *iface;
695
696         iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
697         ovsrec_interface_set_name(iface, br_name);
698
699         port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
700         ovsrec_port_set_name(port, br_name);
701         ovsrec_port_set_interfaces(port, &iface, 1);
702
703         br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs));
704         ovsrec_bridge_set_name(br, br_name);
705         ovsrec_bridge_set_ports(br, &port, 1);
706
707         ovs_insert_bridge(ctx->ovs, br);
708     } else if (ctx->argc == 3) {
709         vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
710                     ctx->argv[0]);
711     } else if (ctx->argc == 4) {
712         const char *parent_name = ctx->argv[2];
713         int vlan = atoi(ctx->argv[3]);
714         struct ovsrec_bridge *br;
715         struct vsctl_bridge *parent;
716         struct ovsrec_port *port;
717         struct ovsrec_interface *iface;
718         int64_t tag = vlan;
719
720         if (vlan < 1 || vlan > 4095) {
721             vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
722         }
723
724         parent = find_bridge(&info, parent_name, false);
725         if (parent && parent->vlan) {
726             vsctl_fatal("cannot create brdige with fake bridge as parent");
727         }
728         if (!parent) {
729             vsctl_fatal("parent bridge %s does not exist", parent_name);
730         }
731         br = parent->br_cfg;
732
733         iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
734         ovsrec_interface_set_name(iface, br_name);
735         ovsrec_interface_set_type(iface, "internal");
736
737         port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
738         ovsrec_port_set_name(port, br_name);
739         ovsrec_port_set_interfaces(port, &iface, 1);
740         ovsrec_port_set_fake_bridge(port, true);
741         ovsrec_port_set_tag(port, &tag, 1);
742
743         bridge_insert_port(br, port);
744     } else {
745         NOT_REACHED();
746     }
747
748     free_info(&info);
749 }
750
751 static void
752 del_port(struct vsctl_info *info, struct vsctl_port *port)
753 {
754     struct shash_node *node;
755
756     SHASH_FOR_EACH (node, &info->ifaces) {
757         struct vsctl_iface *iface = node->data;
758         if (iface->port == port) {
759             ovsrec_interface_delete(iface->iface_cfg);
760         }
761     }
762     ovsrec_port_delete(port->port_cfg);
763
764     bridge_delete_port((port->bridge->parent
765                         ? port->bridge->parent->br_cfg
766                         : port->bridge->br_cfg), port->port_cfg);
767 }
768
769 static void
770 cmd_del_br(struct vsctl_context *ctx)
771 {
772     bool must_exist = !shash_find(&ctx->options, "--if-exists");
773     struct vsctl_bridge *bridge;
774     struct vsctl_info info;
775
776     get_info(ctx->ovs, &info);
777     bridge = find_bridge(&info, ctx->argv[1], must_exist);
778     if (bridge) {
779         struct shash_node *node;
780
781         SHASH_FOR_EACH (node, &info.ports) {
782             struct vsctl_port *port = node->data;
783             if (port->bridge == bridge
784                 || !strcmp(port->port_cfg->name, bridge->name)) {
785                 del_port(&info, port);
786             }
787         }
788         if (bridge->br_cfg) {
789             ovsrec_bridge_delete(bridge->br_cfg);
790             ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
791         }
792     }
793     free_info(&info);
794 }
795
796 static void
797 output_sorted(struct svec *svec, struct ds *output)
798 {
799     const char *name;
800     size_t i;
801
802     svec_sort(svec);
803     SVEC_FOR_EACH (i, name, svec) {
804         ds_put_format(output, "%s\n", name);
805     }
806 }
807
808 static void
809 cmd_list_br(struct vsctl_context *ctx)
810 {
811     struct shash_node *node;
812     struct vsctl_info info;
813     struct svec bridges;
814
815     get_info(ctx->ovs, &info);
816
817     svec_init(&bridges);
818     SHASH_FOR_EACH (node, &info.bridges) {
819         struct vsctl_bridge *br = node->data;
820         svec_add(&bridges, br->name);
821     }
822     output_sorted(&bridges, &ctx->output);
823     svec_destroy(&bridges);
824
825     free_info(&info);
826 }
827
828 static void
829 cmd_br_exists(struct vsctl_context *ctx)
830 {
831     struct vsctl_info info;
832
833     get_info(ctx->ovs, &info);
834     if (!find_bridge(&info, ctx->argv[1], false)) {
835         exit(2);
836     }
837     free_info(&info);
838 }
839
840 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
841  * equals 'a', false otherwise. */
842 static bool
843 key_matches(const char *a,
844             const char *b_prefix, size_t b_prefix_len, const char *b)
845 {
846     return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
847 }
848
849 static void
850 set_external_id(char **old_keys, char **old_values, size_t old_n,
851                 char *key, char *value,
852                 char ***new_keysp, char ***new_valuesp, size_t *new_np)
853 {
854     char **new_keys;
855     char **new_values;
856     size_t new_n;
857     size_t i;
858
859     new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
860     new_values = xmalloc(sizeof *new_values * (old_n + 1));
861     new_n = 0;
862     for (i = 0; i < old_n; i++) {
863         if (strcmp(key, old_keys[i])) {
864             new_keys[new_n] = old_keys[i];
865             new_values[new_n] = old_values[i];
866             new_n++;
867         }
868     }
869     if (value) {
870         new_keys[new_n] = key;
871         new_values[new_n] = value;
872         new_n++;
873     }
874     *new_keysp = new_keys;
875     *new_valuesp = new_values;
876     *new_np = new_n;
877 }
878
879 static void
880 cmd_br_set_external_id(struct vsctl_context *ctx)
881 {
882     struct vsctl_info info;
883     struct vsctl_bridge *bridge;
884     char **keys, **values;
885     size_t n;
886
887     get_info(ctx->ovs, &info);
888     bridge = find_bridge(&info, ctx->argv[1], true);
889     if (bridge->br_cfg) {
890         set_external_id(bridge->br_cfg->key_external_ids,
891                         bridge->br_cfg->value_external_ids,
892                         bridge->br_cfg->n_external_ids,
893                         ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
894                         &keys, &values, &n);
895         ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
896     } else {
897         char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
898         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
899         set_external_id(port->port_cfg->key_external_ids,
900                         port->port_cfg->value_external_ids,
901                         port->port_cfg->n_external_ids,
902                         key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
903                         &keys, &values, &n);
904         ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
905         free(key);
906     }
907     free(keys);
908     free(values);
909
910     free_info(&info);
911 }
912
913 static void
914 get_external_id(char **keys, char **values, size_t n,
915                 const char *prefix, const char *key,
916                 struct ds *output)
917 {
918     size_t prefix_len = strlen(prefix);
919     struct svec svec;
920     size_t i;
921
922     svec_init(&svec);
923     for (i = 0; i < n; i++) {
924         if (!key && !strncmp(keys[i], prefix, prefix_len)) {
925             svec_add_nocopy(&svec, xasprintf("%s=%s",
926                                              keys[i] + prefix_len, values[i]));
927         } else if (key_matches(keys[i], prefix, prefix_len, key)) {
928             svec_add(&svec, values[i]);
929             break;
930         }
931     }
932     output_sorted(&svec, output);
933     svec_destroy(&svec);
934 }
935
936 static void
937 cmd_br_get_external_id(struct vsctl_context *ctx)
938 {
939     struct vsctl_info info;
940     struct vsctl_bridge *bridge;
941
942     get_info(ctx->ovs, &info);
943     bridge = find_bridge(&info, ctx->argv[1], true);
944     if (bridge->br_cfg) {
945         get_external_id(bridge->br_cfg->key_external_ids,
946                         bridge->br_cfg->value_external_ids,
947                         bridge->br_cfg->n_external_ids,
948                         "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
949                         &ctx->output);
950     } else {
951         struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
952         get_external_id(port->port_cfg->key_external_ids,
953                         port->port_cfg->value_external_ids,
954                         port->port_cfg->n_external_ids,
955                         "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
956     }
957     free_info(&info);
958 }
959
960
961 static void
962 cmd_list_ports(struct vsctl_context *ctx)
963 {
964     struct vsctl_bridge *br;
965     struct shash_node *node;
966     struct vsctl_info info;
967     struct svec ports;
968
969     get_info(ctx->ovs, &info);
970     br = find_bridge(&info, ctx->argv[1], true);
971
972     svec_init(&ports);
973     SHASH_FOR_EACH (node, &info.ports) {
974         struct vsctl_port *port = node->data;
975
976         if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
977             svec_add(&ports, port->port_cfg->name);
978         }
979     }
980     output_sorted(&ports, &ctx->output);
981     svec_destroy(&ports);
982
983     free_info(&info);
984 }
985
986 static void
987 add_port(const struct ovsrec_open_vswitch *ovs,
988          const char *br_name, const char *port_name,
989          char *iface_names[], int n_ifaces)
990 {
991     struct vsctl_info info;
992     struct vsctl_bridge *bridge;
993     struct ovsrec_interface **ifaces;
994     struct ovsrec_port *port;
995     size_t i;
996
997     get_info(ovs, &info);
998     check_conflicts(&info, port_name,
999                     xasprintf("cannot create a port named %s", port_name));
1000     /* XXX need to check for conflicts on interfaces too */
1001     bridge = find_bridge(&info, br_name, true);
1002
1003     ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1004     for (i = 0; i < n_ifaces; i++) {
1005         ifaces[i] = ovsrec_interface_insert(txn_from_openvswitch(ovs));
1006         ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1007     }
1008
1009     port = ovsrec_port_insert(txn_from_openvswitch(ovs));
1010     ovsrec_port_set_name(port, port_name);
1011     ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1012     free(ifaces);
1013
1014     if (bridge->vlan) {
1015         int64_t tag = bridge->vlan;
1016         ovsrec_port_set_tag(port, &tag, 1);
1017     }
1018
1019     bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1020                         : bridge->br_cfg), port);
1021
1022     free_info(&info);
1023 }
1024
1025 static void
1026 cmd_add_port(struct vsctl_context *ctx)
1027 {
1028     add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[2], 1);
1029 }
1030
1031 static void
1032 cmd_add_bond(struct vsctl_context *ctx)
1033 {
1034     add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[3], ctx->argc - 3);
1035 }
1036
1037 static void
1038 cmd_del_port(struct vsctl_context *ctx)
1039 {
1040     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1041     struct vsctl_info info;
1042
1043     get_info(ctx->ovs, &info);
1044     if (ctx->argc == 2) {
1045         struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1046         if (port) {
1047             del_port(&info, port);
1048         }
1049     } else if (ctx->argc == 3) {
1050         struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1051         struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1052
1053         if (port) {
1054             if (port->bridge == bridge) {
1055                 del_port(&info, port);
1056             } else if (port->bridge->parent == bridge) {
1057                 vsctl_fatal("bridge %s does not have a port %s (although its "
1058                             "parent bridge %s does)",
1059                             ctx->argv[1], ctx->argv[2], bridge->parent->name);
1060             } else {
1061                 vsctl_fatal("bridge %s does not have a port %s",
1062                             ctx->argv[1], ctx->argv[2]);
1063             }
1064         }
1065     }
1066     free_info(&info);
1067 }
1068
1069 static void
1070 cmd_port_to_br(struct vsctl_context *ctx)
1071 {
1072     struct vsctl_port *port;
1073     struct vsctl_info info;
1074
1075     get_info(ctx->ovs, &info);
1076     port = find_port(&info, ctx->argv[1], true);
1077     ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1078     free_info(&info);
1079 }
1080
1081 static void
1082 cmd_port_set_external_id(struct vsctl_context *ctx)
1083 {
1084     struct vsctl_info info;
1085     struct vsctl_port *port;
1086     char **keys, **values;
1087     size_t n;
1088
1089     get_info(ctx->ovs, &info);
1090     port = find_port(&info, ctx->argv[1], true);
1091     set_external_id(port->port_cfg->key_external_ids,
1092                     port->port_cfg->value_external_ids,
1093                     port->port_cfg->n_external_ids,
1094                     ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1095                     &keys, &values, &n);
1096     ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1097     free(keys);
1098     free(values);
1099
1100     free_info(&info);
1101 }
1102
1103 static void
1104 cmd_port_get_external_id(struct vsctl_context *ctx)
1105 {
1106     struct vsctl_info info;
1107     struct vsctl_port *port;
1108
1109     get_info(ctx->ovs, &info);
1110     port = find_port(&info, ctx->argv[1], true);
1111     get_external_id(port->port_cfg->key_external_ids,
1112                     port->port_cfg->value_external_ids,
1113                     port->port_cfg->n_external_ids,
1114                     "",  ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1115     free_info(&info);
1116 }
1117
1118 static void
1119 cmd_br_to_vlan(struct vsctl_context *ctx)
1120 {
1121     struct vsctl_bridge *bridge;
1122     struct vsctl_info info;
1123
1124     get_info(ctx->ovs, &info);
1125     bridge = find_bridge(&info, ctx->argv[1], true);
1126     ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1127     free_info(&info);
1128 }
1129
1130 static void
1131 cmd_br_to_parent(struct vsctl_context *ctx)
1132 {
1133     struct vsctl_bridge *bridge;
1134     struct vsctl_info info;
1135
1136     get_info(ctx->ovs, &info);
1137     bridge = find_bridge(&info, ctx->argv[1], true);
1138     if (bridge->parent) {
1139         bridge = bridge->parent;
1140     }
1141     ds_put_format(&ctx->output, "%s\n", bridge->name);
1142     free_info(&info);
1143 }
1144
1145 static void
1146 cmd_list_ifaces(struct vsctl_context *ctx)
1147 {
1148     struct vsctl_bridge *br;
1149     struct shash_node *node;
1150     struct vsctl_info info;
1151     struct svec ifaces;
1152
1153     get_info(ctx->ovs, &info);
1154     br = find_bridge(&info, ctx->argv[1], true);
1155
1156     svec_init(&ifaces);
1157     SHASH_FOR_EACH (node, &info.ifaces) {
1158         struct vsctl_iface *iface = node->data;
1159
1160         if (strcmp(iface->iface_cfg->name, br->name)
1161             && br == iface->port->bridge) {
1162             svec_add(&ifaces, iface->iface_cfg->name);
1163         }
1164     }
1165     output_sorted(&ifaces, &ctx->output);
1166     svec_destroy(&ifaces);
1167
1168     free_info(&info);
1169 }
1170
1171 static void
1172 cmd_iface_to_br(struct vsctl_context *ctx)
1173 {
1174     struct vsctl_iface *iface;
1175     struct vsctl_info info;
1176
1177     get_info(ctx->ovs, &info);
1178     iface = find_iface(&info, ctx->argv[1], true);
1179     ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1180     free_info(&info);
1181 }
1182
1183 static void
1184 cmd_iface_set_external_id(struct vsctl_context *ctx)
1185 {
1186     struct vsctl_info info;
1187     struct vsctl_iface *iface;
1188     char **keys, **values;
1189     size_t n;
1190
1191     get_info(ctx->ovs, &info);
1192     iface = find_iface(&info, ctx->argv[1], true);
1193     set_external_id(iface->iface_cfg->key_external_ids,
1194                     iface->iface_cfg->value_external_ids,
1195                     iface->iface_cfg->n_external_ids,
1196                     ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1197                     &keys, &values, &n);
1198     ovsrec_interface_set_external_ids(iface->iface_cfg, keys, values, n);
1199     free(keys);
1200     free(values);
1201
1202     free_info(&info);
1203 }
1204
1205 static void
1206 cmd_iface_get_external_id(struct vsctl_context *ctx)
1207 {
1208     struct vsctl_info info;
1209     struct vsctl_iface *iface;
1210
1211     get_info(ctx->ovs, &info);
1212     iface = find_iface(&info, ctx->argv[1], true);
1213     get_external_id(iface->iface_cfg->key_external_ids,
1214                     iface->iface_cfg->value_external_ids,
1215                     iface->iface_cfg->n_external_ids,
1216                     "",  ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1217     free_info(&info);
1218 }
1219
1220 static void
1221 cmd_get_controller(struct vsctl_context *ctx)
1222 {
1223     struct vsctl_info info;
1224
1225     get_info(ctx->ovs, &info);
1226
1227     if (ctx->argc == 1) {
1228         /* Return the controller from the "Open_vSwitch" table */
1229         if (info.ctrl) {
1230             ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1231         }
1232     } else {
1233         /* Return the controller for a particular bridge. */
1234         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1235
1236         /* If no controller is explicitly defined for the requested
1237          * bridge, fallback to the "Open_vSwitch" table's controller. */
1238         if (br->ctrl) {
1239             ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1240         } else if (info.ctrl) {
1241             ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1242         }
1243     }
1244
1245     free_info(&info);
1246 }
1247
1248 static void
1249 cmd_del_controller(struct vsctl_context *ctx)
1250 {
1251     struct vsctl_info info;
1252
1253     get_info(ctx->ovs, &info);
1254
1255     if (ctx->argc == 1) {
1256         if (info.ctrl) {
1257             ovsrec_controller_delete(info.ctrl);
1258             ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1259         }
1260     } else {
1261         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1262
1263         if (br->ctrl) {
1264             ovsrec_controller_delete(br->ctrl);
1265             ovsrec_bridge_set_controller(br->br_cfg, NULL);
1266         }
1267     }
1268
1269     free_info(&info);
1270 }
1271
1272 static void
1273 cmd_set_controller(struct vsctl_context *ctx)
1274 {
1275     struct vsctl_info info;
1276     struct ovsrec_controller *ctrl;
1277
1278     get_info(ctx->ovs, &info);
1279
1280     if (ctx->argc == 2) {
1281         /* Set the controller in the "Open_vSwitch" table. */
1282         if (info.ctrl) {
1283             ovsrec_controller_delete(info.ctrl);
1284         }
1285         ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1286         ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1287         ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1288     } else {
1289         /* Set the controller for a particular bridge. */
1290         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1291
1292         if (br->ctrl) {
1293             ovsrec_controller_delete(br->ctrl);
1294         }
1295         ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1296         ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1297         ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1298     }
1299
1300     free_info(&info);
1301 }
1302
1303 static void
1304 cmd_get_fail_mode(struct vsctl_context *ctx)
1305 {
1306     struct vsctl_info info;
1307     const char *fail_mode = NULL;
1308
1309     get_info(ctx->ovs, &info);
1310
1311     if (ctx->argc == 1) {
1312         /* Return the fail-mode from the "Open_vSwitch" table */
1313         if (info.ctrl && info.ctrl->fail_mode) {
1314             fail_mode = info.ctrl->fail_mode;
1315         }
1316     } else {
1317         /* Return the fail-mode for a particular bridge. */
1318         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1319
1320         /* If no controller or fail-mode is explicitly defined for the 
1321          * requested bridge, fallback to the "Open_vSwitch" table's 
1322          * setting. */
1323         if (br->ctrl && br->ctrl->fail_mode) {
1324             fail_mode = br->ctrl->fail_mode;
1325         } else if (info.ctrl && info.ctrl->fail_mode) {
1326             fail_mode = info.ctrl->fail_mode;
1327         }
1328     }
1329
1330     if (fail_mode && strlen(fail_mode)) {
1331         ds_put_format(&ctx->output, "%s\n", info.ctrl->fail_mode);
1332     }
1333
1334     free_info(&info);
1335 }
1336
1337 static void
1338 cmd_del_fail_mode(struct vsctl_context *ctx)
1339 {
1340     struct vsctl_info info;
1341
1342     get_info(ctx->ovs, &info);
1343
1344     if (ctx->argc == 1) {
1345         if (info.ctrl && info.ctrl->fail_mode) {
1346             ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1347         }
1348     } else {
1349         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1350
1351         if (br->ctrl && br->ctrl->fail_mode) {
1352             ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1353         }
1354     }
1355
1356     free_info(&info);
1357 }
1358
1359 static void
1360 cmd_set_fail_mode(struct vsctl_context *ctx)
1361 {
1362     struct vsctl_info info;
1363     const char *fail_mode;
1364
1365     get_info(ctx->ovs, &info);
1366
1367     fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1368
1369     if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1370         vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1371     }
1372
1373     if (ctx->argc == 2) {
1374         /* Set the fail-mode in the "Open_vSwitch" table. */
1375         if (!info.ctrl) {
1376             vsctl_fatal("no controller declared");
1377         }
1378         ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1379     } else {
1380         struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1381
1382         if (!br->ctrl) {
1383             vsctl_fatal("no controller declared for %s", br->name);
1384         }
1385         ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1386     }
1387
1388     free_info(&info);
1389 }
1390 \f
1391 typedef void vsctl_handler_func(struct vsctl_context *);
1392
1393 struct vsctl_command {
1394     const char *name;
1395     int min_args;
1396     int max_args;
1397     vsctl_handler_func *handler;
1398     const char *options;
1399 };
1400
1401 static void run_vsctl_command(int argc, char *argv[],
1402                               const struct ovsrec_open_vswitch *ovs,
1403                               struct ds *output);
1404
1405 static struct json *
1406 where_uuid_equals(const struct uuid *uuid)
1407 {
1408     return
1409         json_array_create_1(
1410             json_array_create_3(
1411                 json_string_create("_uuid"),
1412                 json_string_create("=="),
1413                 json_array_create_2(
1414                     json_string_create("uuid"),
1415                     json_string_create_nocopy(
1416                         xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1417 }
1418
1419 static void
1420 do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl)
1421 {
1422     struct ovsdb_idl_txn *txn;
1423     const struct ovsrec_open_vswitch *ovs;
1424     enum ovsdb_idl_txn_status status;
1425     struct ds comment, *output;
1426     int64_t next_cfg;
1427     int n_output;
1428     int i, start;
1429
1430     txn = ovsdb_idl_txn_create(idl);
1431     if (dry_run) {
1432         ovsdb_idl_txn_set_dry_run(txn);
1433     }
1434
1435     ds_init(&comment);
1436     ds_put_cstr(&comment, "ovs-vsctl:");
1437     for (i = 0; i < argc; i++) {
1438         ds_put_format(&comment, " %s", argv[i]);
1439     }
1440     ovsdb_idl_txn_add_comment(txn, ds_cstr(&comment));
1441     ds_destroy(&comment);
1442
1443     ovs = ovsrec_open_vswitch_first(idl);
1444     if (!ovs) {
1445         /* XXX add verification that table is empty */
1446         ovs = ovsrec_open_vswitch_insert(txn);
1447     }
1448
1449     if (wait_for_reload) {
1450         struct json *where = where_uuid_equals(&ovs->header_.uuid);
1451         ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg",
1452                                 where);
1453         json_destroy(where);
1454     }
1455
1456     output = xmalloc(argc * sizeof *output);
1457     n_output = 0;
1458     for (start = i = 0; i <= argc; i++) {
1459         if (i == argc || !strcmp(argv[i], "--")) {
1460             if (i > start) {
1461                 ds_init(&output[n_output]);
1462                 run_vsctl_command(i - start, &argv[start], ovs,
1463                                   &output[n_output++]);
1464             }
1465             start = i + 1;
1466         }
1467     }
1468
1469     while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1470         ovsdb_idl_run(idl);
1471         ovsdb_idl_wait(idl);
1472         ovsdb_idl_txn_wait(txn);
1473         poll_block();
1474     }
1475     if (wait_for_reload && status == TXN_SUCCESS) {
1476         next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
1477     }
1478     ovsdb_idl_txn_destroy(txn);
1479
1480     switch (status) {
1481     case TXN_INCOMPLETE:
1482         NOT_REACHED();
1483
1484     case TXN_ABORTED:
1485         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1486         vsctl_fatal("transaction aborted");
1487
1488     case TXN_UNCHANGED:
1489     case TXN_SUCCESS:
1490         break;
1491
1492     case TXN_TRY_AGAIN:
1493         for (i = 0; i < n_output; i++) {
1494             ds_destroy(&output[i]);
1495         }
1496         return;
1497
1498     case TXN_ERROR:
1499         vsctl_fatal("transaction error");
1500
1501     default:
1502         NOT_REACHED();
1503     }
1504
1505     for (i = 0; i < n_output; i++) {
1506         struct ds *ds = &output[i];
1507         if (oneline) {
1508             size_t j;
1509
1510             ds_chomp(ds, '\n');
1511             for (j = 0; j < ds->length; j++) {
1512                 int c = ds->string[j];
1513                 switch (c) {
1514                 case '\n':
1515                     fputs("\\n", stdout);
1516                     break;
1517
1518                 case '\\':
1519                     fputs("\\\\", stdout);
1520                     break;
1521
1522                 default:
1523                     putchar(c);
1524                 }
1525             }
1526             putchar('\n');
1527         } else {
1528             fputs(ds_cstr(ds), stdout);
1529         }
1530     }
1531
1532     if (wait_for_reload && status != TXN_UNCHANGED) {
1533         for (;;) {
1534             const struct ovsrec_open_vswitch *ovs;
1535
1536             ovsdb_idl_run(idl);
1537             OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
1538                 if (ovs->cur_cfg >= next_cfg) {
1539                     goto done;
1540                 }
1541             }
1542             ovsdb_idl_wait(idl);
1543             poll_block();
1544         }
1545     done: ;
1546     }
1547
1548     exit(EXIT_SUCCESS);
1549 }
1550
1551 static vsctl_handler_func *
1552 get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx)
1553 {
1554     static const struct vsctl_command all_commands[] = {
1555         /* Open vSwitch commands. */
1556         {"init", 0, 0, cmd_init, ""},
1557
1558         /* Bridge commands. */
1559         {"add-br", 1, 3, cmd_add_br, ""},
1560         {"del-br", 1, 1, cmd_del_br, "--if-exists"},
1561         {"list-br", 0, 0, cmd_list_br, ""},
1562         {"br-exists", 1, 1, cmd_br_exists, ""},
1563         {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""},
1564         {"br-to-parent", 1, 1, cmd_br_to_parent, ""},
1565         {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""},
1566         {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""},
1567
1568         /* Port commands. */
1569         {"list-ports", 1, 1, cmd_list_ports, ""},
1570         {"add-port", 2, 2, cmd_add_port, ""},
1571         {"add-bond", 4, INT_MAX, cmd_add_bond, ""},
1572         {"del-port", 1, 2, cmd_del_port, "--if-exists"},
1573         {"port-to-br", 1, 1, cmd_port_to_br, ""},
1574         {"port-set-external-id", 2, 3, cmd_port_set_external_id, ""},
1575         {"port-get-external-id", 1, 2, cmd_port_get_external_id, ""},
1576
1577         /* Interface commands. */
1578         {"list-ifaces", 1, 1, cmd_list_ifaces, ""},
1579         {"iface-to-br", 1, 1, cmd_iface_to_br, ""},
1580         {"iface-set-external-id", 2, 3, cmd_iface_set_external_id, ""},
1581         {"iface-get-external-id", 1, 2, cmd_iface_get_external_id, ""},
1582
1583         /* Controller commands. */
1584         {"get-controller", 0, 1, cmd_get_controller, ""},
1585         {"del-controller", 0, 1, cmd_del_controller, ""},
1586         {"set-controller", 1, 2, cmd_set_controller, ""},
1587         {"get-fail-mode", 0, 1, cmd_get_fail_mode, ""},
1588         {"del-fail-mode", 0, 1, cmd_del_fail_mode, ""},
1589         {"set-fail-mode", 1, 2, cmd_set_fail_mode, ""},
1590     };
1591
1592     const struct vsctl_command *p;
1593     int i;
1594
1595     shash_init(&ctx->options);
1596     for (i = 0; i < argc; i++) {
1597         if (argv[i][0] != '-') {
1598             break;
1599         }
1600         if (!shash_add_once(&ctx->options, argv[i], NULL)) {
1601             vsctl_fatal("'%s' option specified multiple times", argv[i]);
1602         }
1603     }
1604     if (i == argc) {
1605         vsctl_fatal("missing command name");
1606     }
1607
1608     for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) {
1609         if (!strcmp(p->name, argv[i])) {
1610             struct shash_node *node;
1611             int n_arg;
1612
1613             SHASH_FOR_EACH (node, &ctx->options) {
1614                 const char *s = strstr(p->options, node->name);
1615                 int end = s ? s[strlen(node->name)] : EOF;
1616                 if (end != ',' && end != ' ' && end != '\0') {
1617                     vsctl_fatal("'%s' command has no '%s' option",
1618                                 argv[i], node->name);
1619                 }
1620             }
1621
1622             n_arg = argc - i - 1;
1623             if (n_arg < p->min_args) {
1624                 vsctl_fatal("'%s' command requires at least %d arguments",
1625                             p->name, p->min_args);
1626             } else if (n_arg > p->max_args) {
1627                 vsctl_fatal("'%s' command takes at most %d arguments",
1628                             p->name, p->max_args);
1629             } else {
1630                 ctx->argc = n_arg + 1;
1631                 ctx->argv = &argv[i];
1632                 return p->handler;
1633             }
1634         }
1635     }
1636
1637     vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
1638 }
1639
1640 static void
1641 check_vsctl_command(int argc, char *argv[])
1642 {
1643     struct vsctl_context ctx;
1644
1645     get_vsctl_handler(argc, argv, &ctx);
1646     shash_destroy(&ctx.options);
1647 }
1648
1649 static void
1650 run_vsctl_command(int argc, char *argv[],
1651                   const struct ovsrec_open_vswitch *ovs, struct ds *output)
1652 {
1653     vsctl_handler_func *function;
1654     struct vsctl_context ctx;
1655
1656     function = get_vsctl_handler(argc, argv, &ctx);
1657     ctx.ovs = ovs;
1658     ds_init(&ctx.output);
1659     function(&ctx);
1660     *output = ctx.output;
1661     shash_destroy(&ctx.options);
1662 }