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