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