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