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