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