2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "command-line.h"
33 #include "dynamic-string.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
39 #include "stream-ssl.h"
41 #include "vswitchd/vswitch-idl.h"
46 VLOG_DEFINE_THIS_MODULE(vsctl
);
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
53 /* A command supported by ovs-vsctl. */
54 struct vsctl_command_syntax
{
55 const char *name
; /* e.g. "add-br" */
56 int min_args
; /* Min number of arguments following name. */
57 int max_args
; /* Max number of arguments following name. */
59 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
60 * each column or table in ctx->idl that it uses. */
61 void (*prerequisites
)(struct vsctl_context
*ctx
);
63 /* Does the actual work of the command and puts the command's output, if
64 * any, in ctx->output.
66 * Alternatively, if some prerequisite of the command is not met and the
67 * caller should wait for something to change and then retry, it may set
68 * ctx->try_again to true. (Only the "wait-until" command currently does
70 void (*run
)(struct vsctl_context
*ctx
);
72 /* If nonnull, called after the transaction has been successfully
73 * committed. ctx->output is the output from the "run" function, which
74 * this function may modify and otherwise postprocess as needed. (Only the
75 * "create" command currently does any postprocessing.) */
76 void (*postprocess
)(struct vsctl_context
*ctx
);
78 /* A comma-separated list of supported options, e.g. "--a,--b", or the
79 * empty string if the command does not support any options. */
81 enum { RO
, RW
} mode
; /* Does this command modify the database? */
84 struct vsctl_command
{
85 /* Data that remains constant after initialization. */
86 const struct vsctl_command_syntax
*syntax
;
91 /* Data modified by commands. */
95 /* --db: The database server to contact. */
96 static const char *db
;
98 /* --oneline: Write each command's output as a single line? */
101 /* --dry-run: Do not commit any changes. */
104 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
105 static bool wait_for_reload
= true;
107 /* --timeout: Time to wait for a connection to 'db'. */
110 /* All supported commands. */
111 static const struct vsctl_command_syntax all_commands
[];
113 /* The IDL we're using and the current transaction, if any.
114 * This is for use by vsctl_exit() only, to allow it to clean up.
115 * Other code should use its context arguments. */
116 static struct ovsdb_idl
*the_idl
;
117 static struct ovsdb_idl_txn
*the_idl_txn
;
119 static void vsctl_exit(int status
) NO_RETURN
;
120 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN
;
121 static char *default_db(void);
122 static void usage(void) NO_RETURN
;
123 static void parse_options(int argc
, char *argv
[]);
124 static bool might_write_to_db(char **argv
);
126 static struct vsctl_command
*parse_commands(int argc
, char *argv
[],
127 size_t *n_commandsp
);
128 static void parse_command(int argc
, char *argv
[], struct vsctl_command
*);
129 static const struct vsctl_command_syntax
*find_command(const char *name
);
130 static void run_prerequisites(struct vsctl_command
[], size_t n_commands
,
132 static void do_vsctl(const char *args
,
133 struct vsctl_command
*, size_t n_commands
,
136 static const struct vsctl_table_class
*get_table(const char *table_name
);
137 static void set_column(const struct vsctl_table_class
*,
138 const struct ovsdb_idl_row
*, const char *arg
,
139 struct ovsdb_symbol_table
*);
142 main(int argc
, char *argv
[])
144 extern struct vlog_module VLM_reconnect
;
145 struct ovsdb_idl
*idl
;
146 struct vsctl_command
*commands
;
150 set_program_name(argv
[0]);
151 signal(SIGPIPE
, SIG_IGN
);
152 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
153 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
156 /* Log our arguments. This is often valuable for debugging systems. */
157 args
= process_escape_args(argv
);
158 VLOG(might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
160 /* Parse command line. */
161 parse_options(argc
, argv
);
162 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
168 /* Initialize IDL. */
169 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
, false);
170 run_prerequisites(commands
, n_commands
, idl
);
172 /* Now execute the commands. */
174 if (ovsdb_idl_run(idl
)) {
175 do_vsctl(args
, commands
, n_commands
, idl
);
184 parse_options(int argc
, char *argv
[])
187 OPT_DB
= UCHAR_MAX
+ 1,
195 static struct option long_options
[] = {
196 {"db", required_argument
, 0, OPT_DB
},
197 {"no-syslog", no_argument
, 0, OPT_NO_SYSLOG
},
198 {"no-wait", no_argument
, 0, OPT_NO_WAIT
},
199 {"dry-run", no_argument
, 0, OPT_DRY_RUN
},
200 {"oneline", no_argument
, 0, OPT_ONELINE
},
201 {"timeout", required_argument
, 0, 't'},
202 {"help", no_argument
, 0, 'h'},
203 {"version", no_argument
, 0, 'V'},
206 STREAM_SSL_LONG_OPTIONS
207 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
211 char *tmp
, *short_options
;
213 tmp
= long_options_to_short_options(long_options
);
214 short_options
= xasprintf("+%s", tmp
);
220 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
235 vlog_set_levels(&VLM_vsctl
, VLF_SYSLOG
, VLL_WARN
);
239 wait_for_reload
= false;
250 OVS_PRINT_VERSION(0, 0);
254 timeout
= strtoul(optarg
, NULL
, 10);
256 vsctl_fatal("value %s on -t or --timeout is invalid",
264 STREAM_SSL_OPTION_HANDLERS
266 case OPT_PEER_CA_CERT
:
267 stream_ssl_set_peer_ca_cert_file(optarg
);
285 static struct vsctl_command
*
286 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
288 struct vsctl_command
*commands
;
289 size_t n_commands
, allocated_commands
;
293 n_commands
= allocated_commands
= 0;
295 for (start
= i
= 0; i
<= argc
; i
++) {
296 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
298 if (n_commands
>= allocated_commands
) {
299 struct vsctl_command
*c
;
301 commands
= x2nrealloc(commands
, &allocated_commands
,
303 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
304 shash_moved(&c
->options
);
307 parse_command(i
- start
, &argv
[start
],
308 &commands
[n_commands
++]);
314 vsctl_fatal("missing command name (use --help for help)");
316 *n_commandsp
= n_commands
;
321 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
323 const struct vsctl_command_syntax
*p
;
324 struct shash_node
*node
;
328 shash_init(&command
->options
);
329 for (i
= 0; i
< argc
; i
++) {
330 const char *option
= argv
[i
];
334 if (option
[0] != '-') {
338 equals
= strchr(option
, '=');
340 key
= xmemdup0(option
, equals
- option
);
341 value
= xstrdup(equals
+ 1);
343 key
= xstrdup(option
);
347 if (shash_find(&command
->options
, key
)) {
348 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
350 shash_add_nocopy(&command
->options
, key
, value
);
353 vsctl_fatal("missing command name");
356 p
= find_command(argv
[i
]);
358 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
361 SHASH_FOR_EACH (node
, &command
->options
) {
362 const char *s
= strstr(p
->options
, node
->name
);
363 int end
= s
? s
[strlen(node
->name
)] : EOF
;
365 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
366 vsctl_fatal("'%s' command has no '%s' option",
367 argv
[i
], node
->name
);
369 if ((end
== '=') != (node
->data
!= NULL
)) {
371 vsctl_fatal("missing argument to '%s' option on '%s' "
372 "command", node
->name
, argv
[i
]);
374 vsctl_fatal("'%s' option on '%s' does not accept an "
375 "argument", node
->name
, argv
[i
]);
380 n_arg
= argc
- i
- 1;
381 if (n_arg
< p
->min_args
) {
382 vsctl_fatal("'%s' command requires at least %d arguments",
383 p
->name
, p
->min_args
);
384 } else if (n_arg
> p
->max_args
) {
387 for (j
= i
+ 1; j
< argc
; j
++) {
388 if (argv
[j
][0] == '-') {
389 vsctl_fatal("'%s' command takes at most %d arguments "
390 "(note that options must precede command "
391 "names and follow a \"--\" argument)",
392 p
->name
, p
->max_args
);
396 vsctl_fatal("'%s' command takes at most %d arguments",
397 p
->name
, p
->max_args
);
401 command
->argc
= n_arg
+ 1;
402 command
->argv
= &argv
[i
];
405 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
406 * null pointer if there is none. */
407 static const struct vsctl_command_syntax
*
408 find_command(const char *name
)
410 static struct shash commands
= SHASH_INITIALIZER(&commands
);
412 if (shash_is_empty(&commands
)) {
413 const struct vsctl_command_syntax
*p
;
415 for (p
= all_commands
; p
->name
; p
++) {
416 shash_add_assert(&commands
, p
->name
, p
);
420 return shash_find_data(&commands
, name
);
424 vsctl_fatal(const char *format
, ...)
429 va_start(args
, format
);
430 message
= xvasprintf(format
, args
);
433 vlog_set_levels(&VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
434 VLOG_ERR("%s", message
);
435 ovs_error(0, "%s", message
);
436 vsctl_exit(EXIT_FAILURE
);
439 /* Frees the current transaction and the underlying IDL and then calls
442 * Freeing the transaction and the IDL is not strictly necessary, but it makes
443 * for a clean memory leak report from valgrind in the normal case. That makes
444 * it easier to notice real memory leaks. */
446 vsctl_exit(int status
)
449 ovsdb_idl_txn_abort(the_idl_txn
);
450 ovsdb_idl_txn_destroy(the_idl_txn
);
452 ovsdb_idl_destroy(the_idl
);
460 %s: ovs-vswitchd management utility\n\
461 usage: %s [OPTIONS] COMMAND [ARG...]\n\
464 add-br BRIDGE create a new bridge named BRIDGE\n\
465 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
466 del-br BRIDGE delete BRIDGE and all of its ports\n\
467 list-br print the names of all the bridges\n\
468 br-exists BRIDGE test whether BRIDGE exists\n\
469 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
470 br-to-parent BRIDGE print the parent of BRIDGE\n\
471 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
472 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
473 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
474 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
477 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
478 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
479 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
480 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
481 port-to-br PORT print name of bridge that contains PORT\n\
482 A bond is considered to be a single port.\n\
484 Interface commands (a bond consists of multiple interfaces):\n\
485 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
486 iface-to-br IFACE print name of bridge that contains IFACE\n\
488 Controller commands:\n\
489 get-controller BRIDGE print the controller for BRIDGE\n\
490 del-controller BRIDGE delete the controller for BRIDGE\n\
491 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
492 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
493 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
494 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
497 get-ssl print the SSL configuration\n\
498 del-ssl delete the SSL configuration\n\
499 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
502 emer-reset reset switch to known good state\n\
504 Database commands:\n\
505 list TBL [REC] list RECord (or all records) in TBL\n\
506 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
507 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
508 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
509 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
510 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
511 create TBL COL[:KEY]=VALUE create and initialize new record\n\
512 destroy TBL REC delete RECord from TBL\n\
513 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
514 Potentially unsafe database commands require --force option.\n\
517 --db=DATABASE connect to DATABASE\n\
519 --oneline print exactly one line of output per command\n",
520 program_name
, program_name
, default_db());
524 -h, --help display this help message\n\
525 -V, --version display version information\n");
534 def
= xasprintf("unix:%s/db.sock", ovs_rundir
);
539 /* Returns true if it looks like this set of arguments might modify the
540 * database, otherwise false. (Not very smart, so it's prone to false
543 might_write_to_db(char **argv
)
545 for (; *argv
; argv
++) {
546 const struct vsctl_command_syntax
*p
= find_command(*argv
);
547 if (p
&& p
->mode
== RW
) {
554 struct vsctl_context
{
558 struct shash options
;
560 /* Modifiable state. */
562 struct ovsdb_idl
*idl
;
563 struct ovsdb_idl_txn
*txn
;
564 struct ovsdb_symbol_table
*symtab
;
565 const struct ovsrec_open_vswitch
*ovs
;
568 /* A command may set this member to true if some prerequisite is not met
569 * and the caller should wait for something to change and then retry. */
573 struct vsctl_bridge
{
574 struct ovsrec_bridge
*br_cfg
;
576 struct ovsrec_controller
**ctrl
;
579 struct vsctl_bridge
*parent
;
584 struct ovsrec_port
*port_cfg
;
585 struct vsctl_bridge
*bridge
;
589 struct ovsrec_interface
*iface_cfg
;
590 struct vsctl_port
*port
;
594 struct vsctl_context
*ctx
;
595 struct shash bridges
; /* Maps from bridge name to struct vsctl_bridge. */
596 struct shash ports
; /* Maps from port name to struct vsctl_port. */
597 struct shash ifaces
; /* Maps from port name to struct vsctl_iface. */
601 vsctl_context_to_string(const struct vsctl_context
*ctx
)
603 const struct shash_node
*node
;
609 SHASH_FOR_EACH (node
, &ctx
->options
) {
610 svec_add(&words
, node
->name
);
612 for (i
= 0; i
< ctx
->argc
; i
++) {
613 svec_add(&words
, ctx
->argv
[i
]);
615 svec_terminate(&words
);
617 s
= process_escape_args(words
.names
);
619 svec_destroy(&words
);
625 verify_ports(struct vsctl_context
*ctx
)
627 if (!ctx
->verified_ports
) {
628 const struct ovsrec_bridge
*bridge
;
629 const struct ovsrec_port
*port
;
631 ovsrec_open_vswitch_verify_bridges(ctx
->ovs
);
632 OVSREC_BRIDGE_FOR_EACH (bridge
, ctx
->idl
) {
633 ovsrec_bridge_verify_ports(bridge
);
635 OVSREC_PORT_FOR_EACH (port
, ctx
->idl
) {
636 ovsrec_port_verify_interfaces(port
);
639 ctx
->verified_ports
= true;
643 static struct vsctl_bridge
*
644 add_bridge(struct vsctl_info
*b
,
645 struct ovsrec_bridge
*br_cfg
, const char *name
,
646 struct vsctl_bridge
*parent
, int vlan
)
648 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
650 br
->name
= xstrdup(name
);
654 br
->ctrl
= parent
->br_cfg
->controller
;
655 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
656 br
->fail_mode
= parent
->br_cfg
->fail_mode
;
658 br
->ctrl
= br_cfg
->controller
;
659 br
->n_ctrl
= br_cfg
->n_controller
;
660 br
->fail_mode
= br_cfg
->fail_mode
;
662 shash_add(&b
->bridges
, br
->name
, br
);
667 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
669 return (port_cfg
->fake_bridge
671 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095);
674 static struct vsctl_bridge
*
675 find_vlan_bridge(struct vsctl_info
*info
,
676 struct vsctl_bridge
*parent
, int vlan
)
678 struct shash_node
*node
;
680 SHASH_FOR_EACH (node
, &info
->bridges
) {
681 struct vsctl_bridge
*br
= node
->data
;
682 if (br
->parent
== parent
&& br
->vlan
== vlan
) {
691 free_info(struct vsctl_info
*info
)
693 struct shash_node
*node
;
695 SHASH_FOR_EACH (node
, &info
->bridges
) {
696 struct vsctl_bridge
*bridge
= node
->data
;
700 shash_destroy(&info
->bridges
);
702 shash_destroy_free_data(&info
->ports
);
703 shash_destroy_free_data(&info
->ifaces
);
707 pre_get_info(struct vsctl_context
*ctx
)
709 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_bridges
);
711 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_name
);
712 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
713 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
714 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ports
);
716 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_name
);
717 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_fake_bridge
);
718 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_tag
);
719 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_interfaces
);
721 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_name
);
725 get_info(struct vsctl_context
*ctx
, struct vsctl_info
*info
)
727 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
728 struct shash bridges
, ports
;
732 shash_init(&info
->bridges
);
733 shash_init(&info
->ports
);
734 shash_init(&info
->ifaces
);
736 shash_init(&bridges
);
738 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
739 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
740 struct vsctl_bridge
*br
;
743 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
744 VLOG_WARN("%s: database contains duplicate bridge name",
748 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
753 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
754 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
756 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
757 VLOG_WARN("%s: database contains duplicate port name",
762 if (port_is_fake_bridge(port_cfg
)
763 && shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
764 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
768 shash_destroy(&bridges
);
769 shash_destroy(&ports
);
771 shash_init(&bridges
);
773 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
774 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
775 struct vsctl_bridge
*br
;
778 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
781 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
782 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
783 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
784 struct vsctl_port
*port
;
787 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
791 if (port_is_fake_bridge(port_cfg
)
792 && !shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
796 port
= xmalloc(sizeof *port
);
797 port
->port_cfg
= port_cfg
;
799 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
800 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
807 shash_add(&info
->ports
, port_cfg
->name
, port
);
809 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
810 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
811 struct vsctl_iface
*iface
;
813 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
814 VLOG_WARN("%s: database contains duplicate interface name",
819 iface
= xmalloc(sizeof *iface
);
820 iface
->iface_cfg
= iface_cfg
;
822 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
826 shash_destroy(&bridges
);
827 shash_destroy(&ports
);
831 check_conflicts(struct vsctl_info
*info
, const char *name
,
834 struct vsctl_iface
*iface
;
835 struct vsctl_port
*port
;
837 verify_ports(info
->ctx
);
839 if (shash_find(&info
->bridges
, name
)) {
840 vsctl_fatal("%s because a bridge named %s already exists",
844 port
= shash_find_data(&info
->ports
, name
);
846 vsctl_fatal("%s because a port named %s already exists on "
847 "bridge %s", msg
, name
, port
->bridge
->name
);
850 iface
= shash_find_data(&info
->ifaces
, name
);
852 vsctl_fatal("%s because an interface named %s already exists "
853 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
859 static struct vsctl_bridge
*
860 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
862 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
863 if (must_exist
&& !br
) {
864 vsctl_fatal("no bridge named %s", name
);
866 ovsrec_open_vswitch_verify_bridges(info
->ctx
->ovs
);
870 static struct vsctl_bridge
*
871 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
873 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
874 if (br
&& br
->parent
) {
875 vsctl_fatal("%s is a fake bridge", name
);
880 static struct vsctl_port
*
881 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
883 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
884 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
887 if (must_exist
&& !port
) {
888 vsctl_fatal("no port named %s", name
);
890 verify_ports(info
->ctx
);
894 static struct vsctl_iface
*
895 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
897 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
898 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
901 if (must_exist
&& !iface
) {
902 vsctl_fatal("no interface named %s", name
);
904 verify_ports(info
->ctx
);
909 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
911 struct ovsrec_port
**ports
;
914 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
915 for (i
= 0; i
< br
->n_ports
; i
++) {
916 ports
[i
] = br
->ports
[i
];
918 ports
[br
->n_ports
] = port
;
919 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
924 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
926 struct ovsrec_port
**ports
;
929 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
930 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
931 if (br
->ports
[i
] != port
) {
932 ports
[n
++] = br
->ports
[i
];
935 ovsrec_bridge_set_ports(br
, ports
, n
);
940 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
941 struct ovsrec_bridge
*bridge
)
943 struct ovsrec_bridge
**bridges
;
946 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
947 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
948 bridges
[i
] = ovs
->bridges
[i
];
950 bridges
[ovs
->n_bridges
] = bridge
;
951 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
956 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
957 struct ovsrec_bridge
*bridge
)
959 struct ovsrec_bridge
**bridges
;
962 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
963 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
964 if (ovs
->bridges
[i
] != bridge
) {
965 bridges
[n
++] = ovs
->bridges
[i
];
968 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
973 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
978 pre_cmd_emer_reset(struct vsctl_context
*ctx
)
980 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_managers
);
981 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
983 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
984 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_mirrors
);
985 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_netflow
);
986 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_sflow
);
987 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_flood_vlans
);
988 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_other_config
);
990 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_other_config
);
992 ovsdb_idl_add_column(ctx
->idl
,
993 &ovsrec_interface_col_ingress_policing_rate
);
994 ovsdb_idl_add_column(ctx
->idl
,
995 &ovsrec_interface_col_ingress_policing_burst
);
999 cmd_emer_reset(struct vsctl_context
*ctx
)
1001 const struct ovsdb_idl
*idl
= ctx
->idl
;
1002 const struct ovsrec_bridge
*br
;
1003 const struct ovsrec_port
*port
;
1004 const struct ovsrec_interface
*iface
;
1005 const struct ovsrec_mirror
*mirror
, *next_mirror
;
1006 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
1007 const struct ovsrec_netflow
*nf
, *next_nf
;
1008 const struct ovsrec_ssl
*ssl
, *next_ssl
;
1009 const struct ovsrec_sflow
*sflow
, *next_sflow
;
1011 /* Reset the Open_vSwitch table. */
1012 ovsrec_open_vswitch_set_managers(ctx
->ovs
, NULL
, 0);
1013 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1015 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
1017 char *hw_key
= "hwaddr";
1018 char *hw_val
= NULL
;
1020 ovsrec_bridge_set_controller(br
, NULL
, 0);
1021 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
1022 ovsrec_bridge_set_netflow(br
, NULL
);
1023 ovsrec_bridge_set_sflow(br
, NULL
);
1024 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
1026 /* We only want to save the "hwaddr" key from other_config. */
1027 for (i
=0; i
< br
->n_other_config
; i
++) {
1028 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
1029 hw_val
= br
->value_other_config
[i
];
1034 char *val
= xstrdup(hw_val
);
1035 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
1038 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
1042 OVSREC_PORT_FOR_EACH (port
, idl
) {
1043 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
1046 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
1047 /* xxx What do we do about gre/patch devices created by mgr? */
1049 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
1050 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
1053 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
1054 ovsrec_mirror_delete(mirror
);
1057 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
1058 ovsrec_controller_delete(ctrl
);
1061 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
1062 ovsrec_netflow_delete(nf
);
1065 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
1066 ovsrec_ssl_delete(ssl
);
1069 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
1070 ovsrec_sflow_delete(sflow
);
1075 cmd_add_br(struct vsctl_context
*ctx
)
1077 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1078 const char *br_name
, *parent_name
;
1079 struct vsctl_info info
;
1082 br_name
= ctx
->argv
[1];
1083 if (ctx
->argc
== 2) {
1086 } else if (ctx
->argc
== 4) {
1087 parent_name
= ctx
->argv
[2];
1088 vlan
= atoi(ctx
->argv
[3]);
1089 if (vlan
< 1 || vlan
> 4095) {
1090 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
1093 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1097 get_info(ctx
, &info
);
1099 struct vsctl_bridge
*br
;
1101 br
= find_bridge(&info
, br_name
, false);
1105 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1106 "a VLAN bridge for VLAN %d",
1107 br_name
, br_name
, br
->vlan
);
1111 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1112 "is not a VLAN bridge",
1113 br_name
, parent_name
, vlan
, br_name
);
1114 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1115 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1116 "has the wrong parent %s",
1117 br_name
, parent_name
, vlan
,
1118 br_name
, br
->parent
->name
);
1119 } else if (br
->vlan
!= vlan
) {
1120 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1121 "is a VLAN bridge for the wrong VLAN %d",
1122 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1128 check_conflicts(&info
, br_name
,
1129 xasprintf("cannot create a bridge named %s", br_name
));
1132 struct ovsrec_port
*port
;
1133 struct ovsrec_interface
*iface
;
1134 struct ovsrec_bridge
*br
;
1136 iface
= ovsrec_interface_insert(ctx
->txn
);
1137 ovsrec_interface_set_name(iface
, br_name
);
1138 ovsrec_interface_set_type(iface
, "internal");
1140 port
= ovsrec_port_insert(ctx
->txn
);
1141 ovsrec_port_set_name(port
, br_name
);
1142 ovsrec_port_set_interfaces(port
, &iface
, 1);
1144 br
= ovsrec_bridge_insert(ctx
->txn
);
1145 ovsrec_bridge_set_name(br
, br_name
);
1146 ovsrec_bridge_set_ports(br
, &port
, 1);
1148 ovs_insert_bridge(ctx
->ovs
, br
);
1150 struct vsctl_bridge
*parent
;
1151 struct ovsrec_port
*port
;
1152 struct ovsrec_interface
*iface
;
1153 struct ovsrec_bridge
*br
;
1156 parent
= find_bridge(&info
, parent_name
, false);
1157 if (parent
&& parent
->vlan
) {
1158 vsctl_fatal("cannot create bridge with fake bridge as parent");
1161 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1163 br
= parent
->br_cfg
;
1165 iface
= ovsrec_interface_insert(ctx
->txn
);
1166 ovsrec_interface_set_name(iface
, br_name
);
1167 ovsrec_interface_set_type(iface
, "internal");
1169 port
= ovsrec_port_insert(ctx
->txn
);
1170 ovsrec_port_set_name(port
, br_name
);
1171 ovsrec_port_set_interfaces(port
, &iface
, 1);
1172 ovsrec_port_set_fake_bridge(port
, true);
1173 ovsrec_port_set_tag(port
, &tag
, 1);
1175 bridge_insert_port(br
, port
);
1182 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1184 struct shash_node
*node
;
1186 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1187 struct vsctl_iface
*iface
= node
->data
;
1188 if (iface
->port
== port
) {
1189 ovsrec_interface_delete(iface
->iface_cfg
);
1192 ovsrec_port_delete(port
->port_cfg
);
1194 bridge_delete_port((port
->bridge
->parent
1195 ? port
->bridge
->parent
->br_cfg
1196 : port
->bridge
->br_cfg
), port
->port_cfg
);
1200 cmd_del_br(struct vsctl_context
*ctx
)
1202 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1203 struct vsctl_bridge
*bridge
;
1204 struct vsctl_info info
;
1206 get_info(ctx
, &info
);
1207 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1209 struct shash_node
*node
;
1211 SHASH_FOR_EACH (node
, &info
.ports
) {
1212 struct vsctl_port
*port
= node
->data
;
1213 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1214 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1215 del_port(&info
, port
);
1218 if (bridge
->br_cfg
) {
1219 ovsrec_bridge_delete(bridge
->br_cfg
);
1220 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1227 output_sorted(struct svec
*svec
, struct ds
*output
)
1233 SVEC_FOR_EACH (i
, name
, svec
) {
1234 ds_put_format(output
, "%s\n", name
);
1239 cmd_list_br(struct vsctl_context
*ctx
)
1241 struct shash_node
*node
;
1242 struct vsctl_info info
;
1243 struct svec bridges
;
1245 get_info(ctx
, &info
);
1247 svec_init(&bridges
);
1248 SHASH_FOR_EACH (node
, &info
.bridges
) {
1249 struct vsctl_bridge
*br
= node
->data
;
1250 svec_add(&bridges
, br
->name
);
1252 output_sorted(&bridges
, &ctx
->output
);
1253 svec_destroy(&bridges
);
1259 cmd_br_exists(struct vsctl_context
*ctx
)
1261 struct vsctl_info info
;
1263 get_info(ctx
, &info
);
1264 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1270 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1271 * equals 'a', false otherwise. */
1273 key_matches(const char *a
,
1274 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1276 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1280 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1281 char *key
, char *value
,
1282 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1289 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1290 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1292 for (i
= 0; i
< old_n
; i
++) {
1293 if (strcmp(key
, old_keys
[i
])) {
1294 new_keys
[new_n
] = old_keys
[i
];
1295 new_values
[new_n
] = old_values
[i
];
1300 new_keys
[new_n
] = key
;
1301 new_values
[new_n
] = value
;
1304 *new_keysp
= new_keys
;
1305 *new_valuesp
= new_values
;
1310 pre_cmd_br_set_external_id(struct vsctl_context
*ctx
)
1313 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_external_ids
);
1314 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_external_ids
);
1318 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1320 struct vsctl_info info
;
1321 struct vsctl_bridge
*bridge
;
1322 char **keys
, **values
;
1325 get_info(ctx
, &info
);
1326 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1327 if (bridge
->br_cfg
) {
1328 set_external_id(bridge
->br_cfg
->key_external_ids
,
1329 bridge
->br_cfg
->value_external_ids
,
1330 bridge
->br_cfg
->n_external_ids
,
1331 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1332 &keys
, &values
, &n
);
1333 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1334 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1336 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1337 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1338 set_external_id(port
->port_cfg
->key_external_ids
,
1339 port
->port_cfg
->value_external_ids
,
1340 port
->port_cfg
->n_external_ids
,
1341 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1342 &keys
, &values
, &n
);
1343 ovsrec_port_verify_external_ids(port
->port_cfg
);
1344 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1354 get_external_id(char **keys
, char **values
, size_t n
,
1355 const char *prefix
, const char *key
,
1358 size_t prefix_len
= strlen(prefix
);
1363 for (i
= 0; i
< n
; i
++) {
1364 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1365 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1366 keys
[i
] + prefix_len
, values
[i
]));
1367 } else if (key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1368 svec_add(&svec
, values
[i
]);
1372 output_sorted(&svec
, output
);
1373 svec_destroy(&svec
);
1377 pre_cmd_br_get_external_id(struct vsctl_context
*ctx
)
1379 pre_cmd_br_set_external_id(ctx
);
1383 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1385 struct vsctl_info info
;
1386 struct vsctl_bridge
*bridge
;
1388 get_info(ctx
, &info
);
1389 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1390 if (bridge
->br_cfg
) {
1391 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1392 get_external_id(bridge
->br_cfg
->key_external_ids
,
1393 bridge
->br_cfg
->value_external_ids
,
1394 bridge
->br_cfg
->n_external_ids
,
1395 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1398 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1399 ovsrec_port_verify_external_ids(port
->port_cfg
);
1400 get_external_id(port
->port_cfg
->key_external_ids
,
1401 port
->port_cfg
->value_external_ids
,
1402 port
->port_cfg
->n_external_ids
,
1403 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1410 cmd_list_ports(struct vsctl_context
*ctx
)
1412 struct vsctl_bridge
*br
;
1413 struct shash_node
*node
;
1414 struct vsctl_info info
;
1417 get_info(ctx
, &info
);
1418 br
= find_bridge(&info
, ctx
->argv
[1], true);
1419 ovsrec_bridge_verify_ports(br
->br_cfg
? br
->br_cfg
: br
->parent
->br_cfg
);
1422 SHASH_FOR_EACH (node
, &info
.ports
) {
1423 struct vsctl_port
*port
= node
->data
;
1425 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1426 svec_add(&ports
, port
->port_cfg
->name
);
1429 output_sorted(&ports
, &ctx
->output
);
1430 svec_destroy(&ports
);
1436 add_port(struct vsctl_context
*ctx
,
1437 const char *br_name
, const char *port_name
,
1438 bool may_exist
, bool fake_iface
,
1439 char *iface_names
[], int n_ifaces
,
1440 char *settings
[], int n_settings
)
1442 struct vsctl_info info
;
1443 struct vsctl_bridge
*bridge
;
1444 struct ovsrec_interface
**ifaces
;
1445 struct ovsrec_port
*port
;
1448 get_info(ctx
, &info
);
1450 struct vsctl_port
*vsctl_port
;
1452 vsctl_port
= find_port(&info
, port_name
, false);
1454 struct svec want_names
, have_names
;
1456 svec_init(&want_names
);
1457 for (i
= 0; i
< n_ifaces
; i
++) {
1458 svec_add(&want_names
, iface_names
[i
]);
1460 svec_sort(&want_names
);
1462 svec_init(&have_names
);
1463 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1464 svec_add(&have_names
,
1465 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1467 svec_sort(&have_names
);
1469 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1470 char *command
= vsctl_context_to_string(ctx
);
1471 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1472 command
, port_name
, vsctl_port
->bridge
->name
);
1475 if (!svec_equal(&want_names
, &have_names
)) {
1476 char *have_names_string
= svec_join(&have_names
, ", ", "");
1477 char *command
= vsctl_context_to_string(ctx
);
1479 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1480 command
, port_name
, have_names_string
);
1483 svec_destroy(&want_names
);
1484 svec_destroy(&have_names
);
1489 check_conflicts(&info
, port_name
,
1490 xasprintf("cannot create a port named %s", port_name
));
1491 for (i
= 0; i
< n_ifaces
; i
++) {
1492 check_conflicts(&info
, iface_names
[i
],
1493 xasprintf("cannot create an interface named %s",
1496 bridge
= find_bridge(&info
, br_name
, true);
1498 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1499 for (i
= 0; i
< n_ifaces
; i
++) {
1500 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1501 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1504 port
= ovsrec_port_insert(ctx
->txn
);
1505 ovsrec_port_set_name(port
, port_name
);
1506 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1507 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1511 int64_t tag
= bridge
->vlan
;
1512 ovsrec_port_set_tag(port
, &tag
, 1);
1515 for (i
= 0; i
< n_settings
; i
++) {
1516 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1520 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1521 : bridge
->br_cfg
), port
);
1527 cmd_add_port(struct vsctl_context
*ctx
)
1529 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1531 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1532 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1536 cmd_add_bond(struct vsctl_context
*ctx
)
1538 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1539 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1543 n_ifaces
= ctx
->argc
- 3;
1544 for (i
= 3; i
< ctx
->argc
; i
++) {
1545 if (strchr(ctx
->argv
[i
], '=')) {
1551 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1552 "%d were specified", n_ifaces
);
1555 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1556 &ctx
->argv
[3], n_ifaces
,
1557 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1561 cmd_del_port(struct vsctl_context
*ctx
)
1563 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1564 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1565 struct vsctl_port
*port
;
1566 struct vsctl_info info
;
1568 get_info(ctx
, &info
);
1570 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1572 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1573 struct vsctl_iface
*iface
;
1575 port
= find_port(&info
, target
, false);
1577 iface
= find_iface(&info
, target
, false);
1582 if (must_exist
&& !port
) {
1583 vsctl_fatal("no port or interface named %s", target
);
1588 if (ctx
->argc
== 3) {
1589 struct vsctl_bridge
*bridge
;
1591 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1592 if (port
->bridge
!= bridge
) {
1593 if (port
->bridge
->parent
== bridge
) {
1594 vsctl_fatal("bridge %s does not have a port %s (although "
1595 "its parent bridge %s does)",
1596 ctx
->argv
[1], ctx
->argv
[2],
1597 bridge
->parent
->name
);
1599 vsctl_fatal("bridge %s does not have a port %s",
1600 ctx
->argv
[1], ctx
->argv
[2]);
1605 del_port(&info
, port
);
1612 cmd_port_to_br(struct vsctl_context
*ctx
)
1614 struct vsctl_port
*port
;
1615 struct vsctl_info info
;
1617 get_info(ctx
, &info
);
1618 port
= find_port(&info
, ctx
->argv
[1], true);
1619 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1624 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1626 struct vsctl_bridge
*bridge
;
1627 struct vsctl_info info
;
1629 get_info(ctx
, &info
);
1630 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1631 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1636 cmd_br_to_parent(struct vsctl_context
*ctx
)
1638 struct vsctl_bridge
*bridge
;
1639 struct vsctl_info info
;
1641 get_info(ctx
, &info
);
1642 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1643 if (bridge
->parent
) {
1644 bridge
= bridge
->parent
;
1646 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1651 cmd_list_ifaces(struct vsctl_context
*ctx
)
1653 struct vsctl_bridge
*br
;
1654 struct shash_node
*node
;
1655 struct vsctl_info info
;
1658 get_info(ctx
, &info
);
1659 br
= find_bridge(&info
, ctx
->argv
[1], true);
1663 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1664 struct vsctl_iface
*iface
= node
->data
;
1666 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1667 && br
== iface
->port
->bridge
) {
1668 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1671 output_sorted(&ifaces
, &ctx
->output
);
1672 svec_destroy(&ifaces
);
1678 cmd_iface_to_br(struct vsctl_context
*ctx
)
1680 struct vsctl_iface
*iface
;
1681 struct vsctl_info info
;
1683 get_info(ctx
, &info
);
1684 iface
= find_iface(&info
, ctx
->argv
[1], true);
1685 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1690 verify_controllers(struct ovsrec_bridge
*bridge
)
1695 ovsrec_bridge_verify_controller(bridge
);
1696 for (i
= 0; i
< bridge
->n_controller
; i
++) {
1697 ovsrec_controller_verify_target(bridge
->controller
[i
]);
1703 cmd_get_controller(struct vsctl_context
*ctx
)
1705 struct vsctl_info info
;
1706 struct vsctl_bridge
*br
;
1707 struct svec targets
;
1710 get_info(ctx
, &info
);
1711 br
= find_bridge(&info
, ctx
->argv
[1], true);
1712 verify_controllers(br
->br_cfg
);
1714 /* Print the targets in sorted order for reproducibility. */
1715 svec_init(&targets
);
1716 for (i
= 0; i
< br
->n_ctrl
; i
++) {
1717 svec_add(&targets
, br
->ctrl
[i
]->target
);
1720 svec_sort(&targets
);
1721 for (i
= 0; i
< targets
.n
; i
++) {
1722 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1724 svec_destroy(&targets
);
1730 delete_controllers(struct ovsrec_controller
**controllers
,
1731 size_t n_controllers
)
1735 for (i
= 0; i
< n_controllers
; i
++) {
1736 ovsrec_controller_delete(controllers
[i
]);
1741 cmd_del_controller(struct vsctl_context
*ctx
)
1743 struct vsctl_info info
;
1744 struct vsctl_bridge
*br
;
1746 get_info(ctx
, &info
);
1747 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1748 verify_controllers(br
->br_cfg
);
1751 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1752 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1758 static struct ovsrec_controller
**
1759 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1761 struct ovsrec_controller
**controllers
;
1764 controllers
= xmalloc(n
* sizeof *controllers
);
1765 for (i
= 0; i
< n
; i
++) {
1766 controllers
[i
] = ovsrec_controller_insert(txn
);
1767 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1774 cmd_set_controller(struct vsctl_context
*ctx
)
1776 struct vsctl_info info
;
1777 struct vsctl_bridge
*br
;
1778 struct ovsrec_controller
**controllers
;
1781 get_info(ctx
, &info
);
1782 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1783 verify_controllers(br
->br_cfg
);
1785 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1788 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1789 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1796 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1798 struct vsctl_info info
;
1799 struct vsctl_bridge
*br
;
1801 get_info(ctx
, &info
);
1802 br
= find_bridge(&info
, ctx
->argv
[1], true);
1805 ovsrec_bridge_verify_fail_mode(br
->br_cfg
);
1807 if (br
->fail_mode
&& strlen(br
->fail_mode
)) {
1808 ds_put_format(&ctx
->output
, "%s\n", br
->fail_mode
);
1815 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1817 struct vsctl_info info
;
1818 struct vsctl_bridge
*br
;
1820 get_info(ctx
, &info
);
1821 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1823 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1829 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1831 struct vsctl_info info
;
1832 struct vsctl_bridge
*br
;
1833 const char *fail_mode
= ctx
->argv
[2];
1835 get_info(ctx
, &info
);
1836 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1838 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1839 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1842 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1848 pre_cmd_get_ssl(struct vsctl_context
*ctx
)
1850 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1852 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_private_key
);
1853 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_certificate
);
1854 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_ca_cert
);
1855 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_bootstrap_ca_cert
);
1859 cmd_get_ssl(struct vsctl_context
*ctx
)
1861 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1863 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
1865 ovsrec_ssl_verify_private_key(ssl
);
1866 ovsrec_ssl_verify_certificate(ssl
);
1867 ovsrec_ssl_verify_ca_cert(ssl
);
1868 ovsrec_ssl_verify_bootstrap_ca_cert(ssl
);
1870 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
1871 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
1872 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
1873 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
1874 ssl
->bootstrap_ca_cert
? "true" : "false");
1879 pre_cmd_del_ssl(struct vsctl_context
*ctx
)
1881 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1885 cmd_del_ssl(struct vsctl_context
*ctx
)
1887 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1890 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
1891 ovsrec_ssl_delete(ssl
);
1892 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1897 pre_cmd_set_ssl(struct vsctl_context
*ctx
)
1899 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1903 cmd_set_ssl(struct vsctl_context
*ctx
)
1905 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
1906 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1908 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
1910 ovsrec_ssl_delete(ssl
);
1912 ssl
= ovsrec_ssl_insert(ctx
->txn
);
1914 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
1915 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
1916 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
1918 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
1920 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
1923 /* Parameter commands. */
1925 struct vsctl_row_id
{
1926 const struct ovsdb_idl_table_class
*table
;
1927 const struct ovsdb_idl_column
*name_column
;
1928 const struct ovsdb_idl_column
*uuid_column
;
1931 struct vsctl_table_class
{
1932 struct ovsdb_idl_table_class
*class;
1933 struct vsctl_row_id row_ids
[2];
1936 static const struct vsctl_table_class tables
[] = {
1937 {&ovsrec_table_bridge
,
1938 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
1939 {NULL
, NULL
, NULL
}}},
1941 {&ovsrec_table_controller
,
1942 {{&ovsrec_table_bridge
,
1943 &ovsrec_bridge_col_name
,
1944 &ovsrec_bridge_col_controller
}}},
1946 {&ovsrec_table_interface
,
1947 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
1948 {NULL
, NULL
, NULL
}}},
1950 {&ovsrec_table_mirror
,
1951 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
1952 {NULL
, NULL
, NULL
}}},
1954 {&ovsrec_table_manager
,
1955 {{&ovsrec_table_manager
, &ovsrec_manager_col_target
, NULL
},
1956 {NULL
, NULL
, NULL
}}},
1958 {&ovsrec_table_netflow
,
1959 {{&ovsrec_table_bridge
,
1960 &ovsrec_bridge_col_name
,
1961 &ovsrec_bridge_col_netflow
},
1962 {NULL
, NULL
, NULL
}}},
1964 {&ovsrec_table_open_vswitch
,
1965 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
1966 {NULL
, NULL
, NULL
}}},
1968 {&ovsrec_table_port
,
1969 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
1970 {NULL
, NULL
, NULL
}}},
1973 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
1974 {NULL
, NULL
, NULL
}}},
1976 {&ovsrec_table_monitor
,
1977 {{&ovsrec_table_interface
,
1978 &ovsrec_interface_col_name
,
1979 &ovsrec_interface_col_monitor
},
1980 {NULL
, NULL
, NULL
}}},
1982 {&ovsrec_table_maintenance_point
,
1983 {{NULL
, NULL
, NULL
},
1984 {NULL
, NULL
, NULL
}}},
1986 {&ovsrec_table_queue
,
1987 {{NULL
, NULL
, NULL
},
1988 {NULL
, NULL
, NULL
}}},
1991 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
1993 {&ovsrec_table_sflow
,
1994 {{&ovsrec_table_bridge
,
1995 &ovsrec_bridge_col_name
,
1996 &ovsrec_bridge_col_sflow
},
1997 {NULL
, NULL
, NULL
}}},
1999 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
2003 die_if_error(char *error
)
2006 vsctl_fatal("%s", error
);
2011 to_lower_and_underscores(unsigned c
)
2013 return c
== '-' ? '_' : tolower(c
);
2017 score_partial_match(const char *name
, const char *s
)
2021 if (!strcmp(name
, s
)) {
2024 for (score
= 0; ; score
++, name
++, s
++) {
2025 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
2027 } else if (*name
== '\0') {
2028 return UINT_MAX
- 1;
2031 return *s
== '\0' ? score
: 0;
2034 static const struct vsctl_table_class
*
2035 get_table(const char *table_name
)
2037 const struct vsctl_table_class
*table
;
2038 const struct vsctl_table_class
*best_match
= NULL
;
2039 unsigned int best_score
= 0;
2041 for (table
= tables
; table
->class; table
++) {
2042 unsigned int score
= score_partial_match(table
->class->name
,
2044 if (score
> best_score
) {
2047 } else if (score
== best_score
) {
2053 } else if (best_score
) {
2054 vsctl_fatal("multiple table names match \"%s\"", table_name
);
2056 vsctl_fatal("unknown table \"%s\"", table_name
);
2060 static const struct vsctl_table_class
*
2061 pre_get_table(struct vsctl_context
*ctx
, const char *table_name
)
2063 const struct vsctl_table_class
*table_class
;
2066 table_class
= get_table(table_name
);
2067 ovsdb_idl_add_table(ctx
->idl
, table_class
->class);
2069 for (i
= 0; i
< ARRAY_SIZE(table_class
->row_ids
); i
++) {
2070 const struct vsctl_row_id
*id
= &table_class
->row_ids
[i
];
2072 ovsdb_idl_add_table(ctx
->idl
, id
->table
);
2074 if (id
->name_column
) {
2075 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
2077 if (id
->uuid_column
) {
2078 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
2085 static const struct ovsdb_idl_row
*
2086 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
2087 const struct vsctl_row_id
*id
, const char *record_id
)
2089 const struct ovsdb_idl_row
*referrer
, *final
;
2095 if (!id
->name_column
) {
2096 if (strcmp(record_id
, ".")) {
2099 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2100 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
2104 const struct ovsdb_idl_row
*row
;
2107 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2109 row
= ovsdb_idl_next_row(row
))
2111 const struct ovsdb_datum
*name
;
2113 name
= ovsdb_idl_get(row
, id
->name_column
,
2114 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
2115 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
2117 vsctl_fatal("multiple rows in %s match \"%s\"",
2118 table
->class->name
, record_id
);
2129 if (id
->uuid_column
) {
2130 const struct ovsdb_datum
*uuid
;
2132 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
2133 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
2134 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
2136 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
2137 &uuid
->keys
[0].uuid
);
2146 static const struct ovsdb_idl_row
*
2147 get_row (struct vsctl_context
*ctx
,
2148 const struct vsctl_table_class
*table
, const char *record_id
)
2150 const struct ovsdb_idl_row
*row
;
2153 if (uuid_from_string(&uuid
, record_id
)) {
2154 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
2158 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
2159 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
2168 static const struct ovsdb_idl_row
*
2169 must_get_row(struct vsctl_context
*ctx
,
2170 const struct vsctl_table_class
*table
, const char *record_id
)
2172 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
2174 vsctl_fatal("no row \"%s\" in table %s",
2175 record_id
, table
->class->name
);
2181 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
2182 const struct ovsdb_idl_column
**columnp
)
2184 const struct ovsdb_idl_column
*best_match
= NULL
;
2185 unsigned int best_score
= 0;
2188 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2189 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2190 unsigned int score
= score_partial_match(column
->name
, column_name
);
2191 if (score
> best_score
) {
2192 best_match
= column
;
2194 } else if (score
== best_score
) {
2199 *columnp
= best_match
;
2202 } else if (best_score
) {
2203 return xasprintf("%s contains more than one column whose name "
2204 "matches \"%s\"", table
->class->name
, column_name
);
2206 return xasprintf("%s does not contain a column whose name matches "
2207 "\"%s\"", table
->class->name
, column_name
);
2211 static struct uuid
*
2212 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
2214 struct ovsdb_symbol
*symbol
;
2217 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2221 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
2224 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
2226 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2229 symbol
->used
= true;
2230 return &symbol
->uuid
;
2234 pre_get_column(struct vsctl_context
*ctx
,
2235 const struct vsctl_table_class
*table
, const char *column_name
,
2236 const struct ovsdb_idl_column
**columnp
)
2238 die_if_error(get_column(table
, column_name
, columnp
));
2239 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
2243 missing_operator_error(const char *arg
, const char **allowed_operators
,
2249 ds_put_format(&s
, "%s: argument does not end in ", arg
);
2250 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
2251 if (n_allowed
== 2) {
2252 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
2253 } else if (n_allowed
> 2) {
2256 for (i
= 1; i
< n_allowed
- 1; i
++) {
2257 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
2259 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
2261 ds_put_format(&s
, " followed by a value.");
2263 return ds_steal_cstr(&s
);
2266 /* Breaks 'arg' apart into a number of fields in the following order:
2268 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2269 * is stored into '*columnp'. The column name may be abbreviated.
2271 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2272 * and 'keyp' are nonnull, then the column and key names are expected to
2273 * be separated by ':'). The key is stored as a malloc()'d string into
2274 * '*keyp', or NULL if no key is present in 'arg'.
2276 * - If 'valuep' is nonnull, an operator followed by a value string. The
2277 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2278 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2279 * operator is stored into '*operatorp' (one of the pointers from
2280 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2281 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2284 * At least 'columnp' or 'keyp' must be nonnull.
2286 * On success, returns NULL. On failure, returned a malloc()'d string error
2287 * message and stores NULL into all of the nonnull output arguments. */
2288 static char * WARN_UNUSED_RESULT
2289 parse_column_key_value(const char *arg
,
2290 const struct vsctl_table_class
*table
,
2291 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2292 const char **operatorp
,
2293 const char **allowed_operators
, size_t n_allowed
,
2296 const char *p
= arg
;
2299 assert(columnp
|| keyp
);
2300 assert(!(operatorp
&& !valuep
));
2308 /* Parse column name. */
2312 error
= ovsdb_token_parse(&p
, &column_name
);
2316 if (column_name
[0] == '\0') {
2318 error
= xasprintf("%s: missing column name", arg
);
2321 error
= get_column(table
, column_name
, columnp
);
2328 /* Parse key string. */
2329 if (*p
== ':' || !columnp
) {
2333 error
= xasprintf("%s: key not accepted here", arg
);
2336 error
= ovsdb_token_parse(&p
, keyp
);
2344 /* Parse value string. */
2350 if (!allowed_operators
) {
2351 static const char *equals
= "=";
2352 allowed_operators
= &equals
;
2358 for (i
= 0; i
< n_allowed
; i
++) {
2359 const char *op
= allowed_operators
[i
];
2360 size_t op_len
= strlen(op
);
2362 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2368 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2375 *valuep
= xstrdup(p
+ best_len
);
2381 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2407 pre_parse_column_key_value(struct vsctl_context
*ctx
,
2409 const struct vsctl_table_class
*table
)
2411 const struct ovsdb_idl_column
*column
;
2416 die_if_error(ovsdb_token_parse(&p
, &column_name
));
2417 if (column_name
[0] == '\0') {
2418 vsctl_fatal("%s: missing column name", arg
);
2421 pre_get_column(ctx
, table
, column_name
, &column
);
2426 pre_cmd_get(struct vsctl_context
*ctx
)
2428 const char *table_name
= ctx
->argv
[1];
2429 const struct vsctl_table_class
*table
;
2432 table
= pre_get_table(ctx
, table_name
);
2433 for (i
= 3; i
< ctx
->argc
; i
++) {
2434 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2435 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2439 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2444 cmd_get(struct vsctl_context
*ctx
)
2446 const char *id
= shash_find_data(&ctx
->options
, "--id");
2447 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2448 const char *table_name
= ctx
->argv
[1];
2449 const char *record_id
= ctx
->argv
[2];
2450 const struct vsctl_table_class
*table
;
2451 const struct ovsdb_idl_row
*row
;
2452 struct ds
*out
= &ctx
->output
;
2455 table
= get_table(table_name
);
2456 row
= must_get_row(ctx
, table
, record_id
);
2460 *create_symbol(ctx
->symtab
, id
, &new) = row
->uuid
;
2462 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2463 "before it was defined", id
);
2466 for (i
= 3; i
< ctx
->argc
; i
++) {
2467 const struct ovsdb_idl_column
*column
;
2468 const struct ovsdb_datum
*datum
;
2471 /* Special case for obtaining the UUID of a row. We can't just do this
2472 * through parse_column_key_value() below since it returns a "struct
2473 * ovsdb_idl_column" and the UUID column doesn't have one. */
2474 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2475 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2476 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2480 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2481 &column
, &key_string
,
2482 NULL
, NULL
, 0, NULL
));
2484 ovsdb_idl_txn_verify(row
, column
);
2485 datum
= ovsdb_idl_read(row
, column
);
2487 union ovsdb_atom key
;
2490 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2491 vsctl_fatal("cannot specify key to get for non-map column %s",
2495 die_if_error(ovsdb_atom_from_string(&key
,
2497 key_string
, ctx
->symtab
));
2499 idx
= ovsdb_datum_find_key(datum
, &key
,
2500 column
->type
.key
.type
);
2501 if (idx
== UINT_MAX
) {
2503 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2504 key_string
, table
->class->name
, record_id
,
2508 ovsdb_atom_to_string(&datum
->values
[idx
],
2509 column
->type
.value
.type
, out
);
2511 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2513 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2515 ds_put_char(out
, '\n');
2522 pre_cmd_list(struct vsctl_context
*ctx
)
2524 const char *table_name
= ctx
->argv
[1];
2525 const struct vsctl_table_class
*table
;
2528 table
= pre_get_table(ctx
, table_name
);
2529 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2530 ovsdb_idl_add_column(ctx
->idl
, &table
->class->columns
[i
]);
2535 list_record(const struct vsctl_table_class
*table
,
2536 const struct ovsdb_idl_row
*row
, struct ds
*out
)
2540 ds_put_format(out
, "%-20s: "UUID_FMT
"\n", "_uuid",
2541 UUID_ARGS(&row
->uuid
));
2542 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2543 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2544 const struct ovsdb_datum
*datum
;
2546 datum
= ovsdb_idl_read(row
, column
);
2548 ds_put_format(out
, "%-20s: ", column
->name
);
2549 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2550 ds_put_char(out
, '\n');
2555 cmd_list(struct vsctl_context
*ctx
)
2557 const char *table_name
= ctx
->argv
[1];
2558 const struct vsctl_table_class
*table
;
2559 struct ds
*out
= &ctx
->output
;
2562 table
= get_table(table_name
);
2563 if (ctx
->argc
> 2) {
2564 for (i
= 2; i
< ctx
->argc
; i
++) {
2566 ds_put_char(out
, '\n');
2568 list_record(table
, must_get_row(ctx
, table
, ctx
->argv
[i
]), out
);
2571 const struct ovsdb_idl_row
*row
;
2574 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class), first
= true;
2576 row
= ovsdb_idl_next_row(row
), first
= false) {
2578 ds_put_char(out
, '\n');
2580 list_record(table
, row
, out
);
2586 pre_cmd_set(struct vsctl_context
*ctx
)
2588 const char *table_name
= ctx
->argv
[1];
2589 const struct vsctl_table_class
*table
;
2592 table
= pre_get_table(ctx
, table_name
);
2593 for (i
= 3; i
< ctx
->argc
; i
++) {
2594 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2599 set_column(const struct vsctl_table_class
*table
,
2600 const struct ovsdb_idl_row
*row
, const char *arg
,
2601 struct ovsdb_symbol_table
*symtab
)
2603 const struct ovsdb_idl_column
*column
;
2604 char *key_string
, *value_string
;
2607 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2608 NULL
, NULL
, 0, &value_string
);
2609 die_if_error(error
);
2610 if (!value_string
) {
2611 vsctl_fatal("%s: missing value", arg
);
2615 union ovsdb_atom key
, value
;
2616 struct ovsdb_datum datum
;
2618 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2619 vsctl_fatal("cannot specify key to set for non-map column %s",
2623 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2624 key_string
, symtab
));
2625 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2626 value_string
, symtab
));
2628 ovsdb_datum_init_empty(&datum
);
2629 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
2631 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2632 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2634 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
2635 &column
->type
, false);
2636 ovsdb_idl_txn_write(row
, column
, &datum
);
2638 struct ovsdb_datum datum
;
2640 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2641 value_string
, symtab
));
2642 ovsdb_idl_txn_write(row
, column
, &datum
);
2650 cmd_set(struct vsctl_context
*ctx
)
2652 const char *table_name
= ctx
->argv
[1];
2653 const char *record_id
= ctx
->argv
[2];
2654 const struct vsctl_table_class
*table
;
2655 const struct ovsdb_idl_row
*row
;
2658 table
= get_table(table_name
);
2659 row
= must_get_row(ctx
, table
, record_id
);
2660 for (i
= 3; i
< ctx
->argc
; i
++) {
2661 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2666 pre_cmd_add(struct vsctl_context
*ctx
)
2668 const char *table_name
= ctx
->argv
[1];
2669 const char *column_name
= ctx
->argv
[3];
2670 const struct vsctl_table_class
*table
;
2671 const struct ovsdb_idl_column
*column
;
2673 table
= pre_get_table(ctx
, table_name
);
2674 pre_get_column(ctx
, table
, column_name
, &column
);
2678 cmd_add(struct vsctl_context
*ctx
)
2680 const char *table_name
= ctx
->argv
[1];
2681 const char *record_id
= ctx
->argv
[2];
2682 const char *column_name
= ctx
->argv
[3];
2683 const struct vsctl_table_class
*table
;
2684 const struct ovsdb_idl_column
*column
;
2685 const struct ovsdb_idl_row
*row
;
2686 const struct ovsdb_type
*type
;
2687 struct ovsdb_datum old
;
2690 table
= get_table(table_name
);
2691 row
= must_get_row(ctx
, table
, record_id
);
2692 die_if_error(get_column(table
, column_name
, &column
));
2694 type
= &column
->type
;
2695 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2696 for (i
= 4; i
< ctx
->argc
; i
++) {
2697 struct ovsdb_type add_type
;
2698 struct ovsdb_datum add
;
2702 add_type
.n_max
= UINT_MAX
;
2703 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2705 ovsdb_datum_union(&old
, &add
, type
, false);
2706 ovsdb_datum_destroy(&add
, type
);
2708 if (old
.n
> type
->n_max
) {
2709 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2710 "table %s but the maximum number is %u",
2712 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2713 column
->name
, table
->class->name
, type
->n_max
);
2715 ovsdb_idl_txn_verify(row
, column
);
2716 ovsdb_idl_txn_write(row
, column
, &old
);
2720 pre_cmd_remove(struct vsctl_context
*ctx
)
2722 const char *table_name
= ctx
->argv
[1];
2723 const char *column_name
= ctx
->argv
[3];
2724 const struct vsctl_table_class
*table
;
2725 const struct ovsdb_idl_column
*column
;
2727 table
= pre_get_table(ctx
, table_name
);
2728 pre_get_column(ctx
, table
, column_name
, &column
);
2732 cmd_remove(struct vsctl_context
*ctx
)
2734 const char *table_name
= ctx
->argv
[1];
2735 const char *record_id
= ctx
->argv
[2];
2736 const char *column_name
= ctx
->argv
[3];
2737 const struct vsctl_table_class
*table
;
2738 const struct ovsdb_idl_column
*column
;
2739 const struct ovsdb_idl_row
*row
;
2740 const struct ovsdb_type
*type
;
2741 struct ovsdb_datum old
;
2744 table
= get_table(table_name
);
2745 row
= must_get_row(ctx
, table
, record_id
);
2746 die_if_error(get_column(table
, column_name
, &column
));
2748 type
= &column
->type
;
2749 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2750 for (i
= 4; i
< ctx
->argc
; i
++) {
2751 struct ovsdb_type rm_type
;
2752 struct ovsdb_datum rm
;
2757 rm_type
.n_max
= UINT_MAX
;
2758 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
2759 ctx
->argv
[i
], ctx
->symtab
);
2760 if (error
&& ovsdb_type_is_map(&rm_type
)) {
2762 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
2763 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
2764 ctx
->argv
[i
], ctx
->symtab
));
2766 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
2767 ovsdb_datum_destroy(&rm
, &rm_type
);
2769 if (old
.n
< type
->n_min
) {
2770 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2771 "table %s but the minimum number is %u",
2773 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2774 column
->name
, table
->class->name
, type
->n_min
);
2776 ovsdb_idl_txn_verify(row
, column
);
2777 ovsdb_idl_txn_write(row
, column
, &old
);
2781 pre_cmd_clear(struct vsctl_context
*ctx
)
2783 const char *table_name
= ctx
->argv
[1];
2784 const struct vsctl_table_class
*table
;
2787 table
= pre_get_table(ctx
, table_name
);
2788 for (i
= 3; i
< ctx
->argc
; i
++) {
2789 const struct ovsdb_idl_column
*column
;
2791 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
2796 cmd_clear(struct vsctl_context
*ctx
)
2798 const char *table_name
= ctx
->argv
[1];
2799 const char *record_id
= ctx
->argv
[2];
2800 const struct vsctl_table_class
*table
;
2801 const struct ovsdb_idl_row
*row
;
2804 table
= get_table(table_name
);
2805 row
= must_get_row(ctx
, table
, record_id
);
2806 for (i
= 3; i
< ctx
->argc
; i
++) {
2807 const struct ovsdb_idl_column
*column
;
2808 const struct ovsdb_type
*type
;
2809 struct ovsdb_datum datum
;
2811 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
2813 type
= &column
->type
;
2814 if (type
->n_min
> 0) {
2815 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2816 "of table %s, which is not allowed to be empty",
2817 column
->name
, table
->class->name
);
2820 ovsdb_datum_init_empty(&datum
);
2821 ovsdb_idl_txn_write(row
, column
, &datum
);
2826 cmd_create(struct vsctl_context
*ctx
)
2828 const char *id
= shash_find_data(&ctx
->options
, "--id");
2829 const char *table_name
= ctx
->argv
[1];
2830 const struct vsctl_table_class
*table
;
2831 const struct ovsdb_idl_row
*row
;
2832 const struct uuid
*uuid
;
2835 uuid
= id
? create_symbol(ctx
->symtab
, id
, NULL
) : NULL
;
2837 table
= get_table(table_name
);
2838 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
2839 for (i
= 2; i
< ctx
->argc
; i
++) {
2840 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2842 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
2845 /* This function may be used as the 'postprocess' function for commands that
2846 * insert new rows into the database. It expects that the command's 'run'
2847 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2848 * sole output. It replaces that output by the row's permanent UUID assigned
2849 * by the database server and appends a new-line.
2851 * Currently we use this only for "create", because the higher-level commands
2852 * are supposed to be independent of the actual structure of the vswitch
2855 post_create(struct vsctl_context
*ctx
)
2857 const struct uuid
*real
;
2860 uuid_from_string(&dummy
, ds_cstr(&ctx
->output
));
2861 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
2863 ds_clear(&ctx
->output
);
2864 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
2866 ds_put_char(&ctx
->output
, '\n');
2870 pre_cmd_destroy(struct vsctl_context
*ctx
)
2872 const char *table_name
= ctx
->argv
[1];
2874 pre_get_table(ctx
, table_name
);
2878 cmd_destroy(struct vsctl_context
*ctx
)
2880 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
2881 const char *table_name
= ctx
->argv
[1];
2882 const struct vsctl_table_class
*table
;
2885 table
= get_table(table_name
);
2886 for (i
= 2; i
< ctx
->argc
; i
++) {
2887 const struct ovsdb_idl_row
*row
;
2889 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
2891 ovsdb_idl_txn_delete(row
);
2897 is_condition_satified(const struct vsctl_table_class
*table
,
2898 const struct ovsdb_idl_row
*row
, const char *arg
,
2899 struct ovsdb_symbol_table
*symtab
)
2901 static const char *operators
[] = {
2902 "=", "!=", "<", ">", "<=", ">="
2905 const struct ovsdb_idl_column
*column
;
2906 const struct ovsdb_datum
*have_datum
;
2907 char *key_string
, *value_string
;
2908 const char *operator;
2913 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2914 &operator, operators
, ARRAY_SIZE(operators
),
2916 die_if_error(error
);
2917 if (!value_string
) {
2918 vsctl_fatal("%s: missing value", arg
);
2921 have_datum
= ovsdb_idl_read(row
, column
);
2923 union ovsdb_atom want_key
, want_value
;
2925 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2926 vsctl_fatal("cannot specify key to check for non-map column %s",
2930 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
2931 key_string
, symtab
));
2932 die_if_error(ovsdb_atom_from_string(&want_value
, &column
->type
.value
,
2933 value_string
, symtab
));
2935 idx
= ovsdb_datum_find_key(have_datum
,
2936 &want_key
, column
->type
.key
.type
);
2937 if (idx
!= UINT_MAX
) {
2938 cmp
= ovsdb_atom_compare_3way(&have_datum
->values
[idx
],
2940 column
->type
.value
.type
);
2943 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
2944 ovsdb_atom_destroy(&want_value
, column
->type
.value
.type
);
2946 struct ovsdb_datum want_datum
;
2948 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
2949 value_string
, symtab
));
2951 cmp
= ovsdb_datum_compare_3way(have_datum
, &want_datum
,
2953 ovsdb_datum_destroy(&want_datum
, &column
->type
);
2959 return (idx
== UINT_MAX
? false
2960 : !strcmp(operator, "=") ? cmp
== 0
2961 : !strcmp(operator, "!=") ? cmp
!= 0
2962 : !strcmp(operator, "<") ? cmp
< 0
2963 : !strcmp(operator, ">") ? cmp
> 0
2964 : !strcmp(operator, "<=") ? cmp
<= 0
2965 : !strcmp(operator, ">=") ? cmp
>= 0
2970 pre_cmd_wait_until(struct vsctl_context
*ctx
)
2972 const char *table_name
= ctx
->argv
[1];
2973 const struct vsctl_table_class
*table
;
2976 table
= pre_get_table(ctx
, table_name
);
2978 for (i
= 3; i
< ctx
->argc
; i
++) {
2979 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2984 cmd_wait_until(struct vsctl_context
*ctx
)
2986 const char *table_name
= ctx
->argv
[1];
2987 const char *record_id
= ctx
->argv
[2];
2988 const struct vsctl_table_class
*table
;
2989 const struct ovsdb_idl_row
*row
;
2992 table
= get_table(table_name
);
2994 row
= get_row(ctx
, table
, record_id
);
2996 ctx
->try_again
= true;
3000 for (i
= 3; i
< ctx
->argc
; i
++) {
3001 if (!is_condition_satified(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
3002 ctx
->try_again
= true;
3008 static struct json
*
3009 where_uuid_equals(const struct uuid
*uuid
)
3012 json_array_create_1(
3013 json_array_create_3(
3014 json_string_create("_uuid"),
3015 json_string_create("=="),
3016 json_array_create_2(
3017 json_string_create("uuid"),
3018 json_string_create_nocopy(
3019 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
3023 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
3024 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
3025 const struct ovsrec_open_vswitch
*ovs
,
3026 struct ovsdb_symbol_table
*symtab
)
3028 ctx
->argc
= command
->argc
;
3029 ctx
->argv
= command
->argv
;
3030 ctx
->options
= command
->options
;
3032 ds_swap(&ctx
->output
, &command
->output
);
3036 ctx
->symtab
= symtab
;
3037 ctx
->verified_ports
= false;
3039 ctx
->try_again
= false;
3043 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
3045 ds_swap(&ctx
->output
, &command
->output
);
3049 run_prerequisites(struct vsctl_command
*commands
, size_t n_commands
,
3050 struct ovsdb_idl
*idl
)
3052 struct vsctl_command
*c
;
3054 ovsdb_idl_add_table(idl
, &ovsrec_table_open_vswitch
);
3055 if (wait_for_reload
) {
3056 ovsdb_idl_add_column(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
3058 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3059 if (c
->syntax
->prerequisites
) {
3060 struct vsctl_context ctx
;
3062 ds_init(&c
->output
);
3064 vsctl_context_init(&ctx
, c
, idl
, NULL
, NULL
, NULL
);
3065 (c
->syntax
->prerequisites
)(&ctx
);
3066 vsctl_context_done(&ctx
, c
);
3068 assert(!c
->output
.string
);
3074 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
3075 struct ovsdb_idl
*idl
)
3077 struct ovsdb_idl_txn
*txn
;
3078 const struct ovsrec_open_vswitch
*ovs
;
3079 enum ovsdb_idl_txn_status status
;
3080 struct ovsdb_symbol_table
*symtab
;
3082 struct vsctl_command
*c
;
3083 int64_t next_cfg
= 0;
3086 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
3088 ovsdb_idl_txn_set_dry_run(txn
);
3091 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
3093 ovs
= ovsrec_open_vswitch_first(idl
);
3095 /* XXX add verification that table is empty */
3096 ovs
= ovsrec_open_vswitch_insert(txn
);
3099 if (wait_for_reload
) {
3100 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
3101 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
3102 json_destroy(where
);
3105 symtab
= ovsdb_symbol_table_create();
3106 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3107 ds_init(&c
->output
);
3109 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3110 struct vsctl_context ctx
;
3112 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
3113 (c
->syntax
->run
)(&ctx
);
3114 vsctl_context_done(&ctx
, c
);
3116 if (ctx
.try_again
) {
3121 status
= ovsdb_idl_txn_commit_block(txn
);
3122 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
3123 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
3125 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
3126 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3127 if (c
->syntax
->postprocess
) {
3128 struct vsctl_context ctx
;
3130 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
3131 (c
->syntax
->postprocess
)(&ctx
);
3132 vsctl_context_done(&ctx
, c
);
3136 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
3137 ovsdb_idl_txn_destroy(txn
);
3138 txn
= the_idl_txn
= NULL
;
3140 unused
= ovsdb_symbol_table_find_unused(symtab
);
3142 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3143 "with \"-- --id=%s create ...\")", unused
, unused
);
3147 case TXN_INCOMPLETE
:
3151 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3152 vsctl_fatal("transaction aborted");
3162 vsctl_fatal("transaction error: %s", error
);
3169 ovsdb_symbol_table_destroy(symtab
);
3171 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3172 struct ds
*ds
= &c
->output
;
3173 struct shash_node
*node
;
3179 for (j
= 0; j
< ds
->length
; j
++) {
3180 int ch
= ds
->string
[j
];
3183 fputs("\\n", stdout
);
3187 fputs("\\\\", stdout
);
3196 fputs(ds_cstr(ds
), stdout
);
3198 ds_destroy(&c
->output
);
3200 SHASH_FOR_EACH (node
, &c
->options
) {
3203 shash_destroy(&c
->options
);
3207 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
3210 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
3211 if (ovs
->cur_cfg
>= next_cfg
) {
3215 ovsdb_idl_wait(idl
);
3220 ovsdb_idl_destroy(idl
);
3225 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3226 * resources and return so that the caller can try again. */
3228 ovsdb_idl_txn_abort(txn
);
3229 ovsdb_idl_txn_destroy(txn
);
3231 ovsdb_symbol_table_destroy(symtab
);
3232 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3233 ds_destroy(&c
->output
);
3238 static const struct vsctl_command_syntax all_commands
[] = {
3239 /* Open vSwitch commands. */
3240 {"init", 0, 0, NULL
, cmd_init
, NULL
, "", RW
},
3242 /* Bridge commands. */
3243 {"add-br", 1, 3, pre_get_info
, cmd_add_br
, NULL
, "--may-exist", RW
},
3244 {"del-br", 1, 1, pre_get_info
, cmd_del_br
, NULL
, "--if-exists", RW
},
3245 {"list-br", 0, 0, pre_get_info
, cmd_list_br
, NULL
, "", RO
},
3246 {"br-exists", 1, 1, pre_get_info
, cmd_br_exists
, NULL
, "", RO
},
3247 {"br-to-vlan", 1, 1, pre_get_info
, cmd_br_to_vlan
, NULL
, "", RO
},
3248 {"br-to-parent", 1, 1, pre_get_info
, cmd_br_to_parent
, NULL
, "", RO
},
3249 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id
,
3250 cmd_br_set_external_id
, NULL
, "", RW
},
3251 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id
,
3252 cmd_br_get_external_id
, NULL
, "", RO
},
3254 /* Port commands. */
3255 {"list-ports", 1, 1, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
3256 {"add-port", 2, INT_MAX
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
3258 {"add-bond", 4, INT_MAX
, pre_get_info
, cmd_add_bond
, NULL
,
3259 "--may-exist,--fake-iface", RW
},
3260 {"del-port", 1, 2, pre_get_info
, cmd_del_port
, NULL
,
3261 "--if-exists,--with-iface", RW
},
3262 {"port-to-br", 1, 1, pre_get_info
, cmd_port_to_br
, NULL
, "", RO
},
3264 /* Interface commands. */
3265 {"list-ifaces", 1, 1, pre_get_info
, cmd_list_ifaces
, NULL
, "", RO
},
3266 {"iface-to-br", 1, 1, pre_get_info
, cmd_iface_to_br
, NULL
, "", RO
},
3268 /* Controller commands. */
3269 {"get-controller", 1, 1, pre_get_info
, cmd_get_controller
, NULL
, "", RO
},
3270 {"del-controller", 1, 1, pre_get_info
, cmd_del_controller
, NULL
, "", RW
},
3271 {"set-controller", 1, INT_MAX
, pre_get_info
, cmd_set_controller
, NULL
, "",
3273 {"get-fail-mode", 1, 1, pre_get_info
, cmd_get_fail_mode
, NULL
, "", RO
},
3274 {"del-fail-mode", 1, 1, pre_get_info
, cmd_del_fail_mode
, NULL
, "", RW
},
3275 {"set-fail-mode", 2, 2, pre_get_info
, cmd_set_fail_mode
, NULL
, "", RW
},
3278 {"get-ssl", 0, 0, pre_cmd_get_ssl
, cmd_get_ssl
, NULL
, "", RO
},
3279 {"del-ssl", 0, 0, pre_cmd_del_ssl
, cmd_del_ssl
, NULL
, "", RW
},
3280 {"set-ssl", 3, 3, pre_cmd_set_ssl
, cmd_set_ssl
, NULL
, "--bootstrap", RW
},
3282 /* Switch commands. */
3283 {"emer-reset", 0, 0, pre_cmd_emer_reset
, cmd_emer_reset
, NULL
, "", RW
},
3285 /* Parameter commands. */
3286 {"get", 2, INT_MAX
, pre_cmd_get
, cmd_get
, NULL
, "--if-exists,--id=", RO
},
3287 {"list", 1, INT_MAX
, pre_cmd_list
, cmd_list
, NULL
, "", RO
},
3288 {"set", 3, INT_MAX
, pre_cmd_set
, cmd_set
, NULL
, "", RW
},
3289 {"add", 4, INT_MAX
, pre_cmd_add
, cmd_add
, NULL
, "", RW
},
3290 {"remove", 4, INT_MAX
, pre_cmd_remove
, cmd_remove
, NULL
, "", RW
},
3291 {"clear", 3, INT_MAX
, pre_cmd_clear
, cmd_clear
, NULL
, "", RW
},
3292 {"create", 2, INT_MAX
, NULL
, cmd_create
, post_create
, "--id=", RW
},
3293 {"destroy", 1, INT_MAX
, pre_cmd_destroy
, cmd_destroy
, NULL
, "--if-exists",
3295 {"wait-until", 2, INT_MAX
, pre_cmd_wait_until
, cmd_wait_until
, NULL
, "",
3298 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, RO
},