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 typedef void vsctl_handler_func(struct vsctl_context
*);
55 struct vsctl_command_syntax
{
59 vsctl_handler_func
*run
;
60 vsctl_handler_func
*postprocess
;
62 enum { RO
, RW
} mode
; /* Does this command modify the database? */
65 struct vsctl_command
{
66 /* Data that remains constant after initialization. */
67 const struct vsctl_command_syntax
*syntax
;
72 /* Data modified by commands. */
76 /* --db: The database server to contact. */
77 static const char *db
;
79 /* --oneline: Write each command's output as a single line? */
82 /* --dry-run: Do not commit any changes. */
85 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
86 static bool wait_for_reload
= true;
88 /* --timeout: Time to wait for a connection to 'db'. */
91 /* All supported commands. */
92 static const struct vsctl_command_syntax all_commands
[];
94 /* The IDL we're using and the current transaction, if any.
95 * This is for use by vsctl_exit() only, to allow it to clean up.
96 * Other code should use its context arguments. */
97 static struct ovsdb_idl
*the_idl
;
98 static struct ovsdb_idl_txn
*the_idl_txn
;
100 static void vsctl_exit(int status
) NO_RETURN
;
101 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN
;
102 static char *default_db(void);
103 static void usage(void) NO_RETURN
;
104 static void parse_options(int argc
, char *argv
[]);
105 static bool might_write_to_db(char **argv
);
107 static struct vsctl_command
*parse_commands(int argc
, char *argv
[],
108 size_t *n_commandsp
);
109 static void parse_command(int argc
, char *argv
[], struct vsctl_command
*);
110 static const struct vsctl_command_syntax
*find_command(const char *name
);
111 static void do_vsctl(const char *args
,
112 struct vsctl_command
*, size_t n_commands
,
115 static const struct vsctl_table_class
*get_table(const char *table_name
);
116 static void set_column(const struct vsctl_table_class
*,
117 const struct ovsdb_idl_row
*, const char *arg
,
118 struct ovsdb_symbol_table
*);
121 main(int argc
, char *argv
[])
123 extern struct vlog_module VLM_reconnect
;
124 struct ovsdb_idl
*idl
;
125 struct vsctl_command
*commands
;
129 set_program_name(argv
[0]);
130 signal(SIGPIPE
, SIG_IGN
);
131 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
132 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
135 /* Log our arguments. This is often valuable for debugging systems. */
136 args
= process_escape_args(argv
);
137 VLOG(might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
139 /* Parse command line. */
140 parse_options(argc
, argv
);
141 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
147 /* Now execute the commands. */
148 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
);
150 if (ovsdb_idl_run(idl
)) {
151 do_vsctl(args
, commands
, n_commands
, idl
);
160 parse_options(int argc
, char *argv
[])
163 OPT_DB
= UCHAR_MAX
+ 1,
171 static struct option long_options
[] = {
172 {"db", required_argument
, 0, OPT_DB
},
173 {"no-syslog", no_argument
, 0, OPT_NO_SYSLOG
},
174 {"no-wait", no_argument
, 0, OPT_NO_WAIT
},
175 {"dry-run", no_argument
, 0, OPT_DRY_RUN
},
176 {"oneline", no_argument
, 0, OPT_ONELINE
},
177 {"timeout", required_argument
, 0, 't'},
178 {"help", no_argument
, 0, 'h'},
179 {"version", no_argument
, 0, 'V'},
182 STREAM_SSL_LONG_OPTIONS
183 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
187 char *tmp
, *short_options
;
189 tmp
= long_options_to_short_options(long_options
);
190 short_options
= xasprintf("+%s", tmp
);
196 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
211 vlog_set_levels(&VLM_vsctl
, VLF_SYSLOG
, VLL_WARN
);
215 wait_for_reload
= false;
226 OVS_PRINT_VERSION(0, 0);
230 timeout
= strtoul(optarg
, NULL
, 10);
232 vsctl_fatal("value %s on -t or --timeout is invalid",
240 STREAM_SSL_OPTION_HANDLERS
242 case OPT_PEER_CA_CERT
:
243 stream_ssl_set_peer_ca_cert_file(optarg
);
261 static struct vsctl_command
*
262 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
264 struct vsctl_command
*commands
;
265 size_t n_commands
, allocated_commands
;
269 n_commands
= allocated_commands
= 0;
271 for (start
= i
= 0; i
<= argc
; i
++) {
272 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
274 if (n_commands
>= allocated_commands
) {
275 struct vsctl_command
*c
;
277 commands
= x2nrealloc(commands
, &allocated_commands
,
279 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
280 shash_moved(&c
->options
);
283 parse_command(i
- start
, &argv
[start
],
284 &commands
[n_commands
++]);
290 vsctl_fatal("missing command name (use --help for help)");
292 *n_commandsp
= n_commands
;
297 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
299 const struct vsctl_command_syntax
*p
;
300 struct shash_node
*node
;
304 shash_init(&command
->options
);
305 for (i
= 0; i
< argc
; i
++) {
306 const char *option
= argv
[i
];
310 if (option
[0] != '-') {
314 equals
= strchr(option
, '=');
316 key
= xmemdup0(option
, equals
- option
);
317 value
= xstrdup(equals
+ 1);
319 key
= xstrdup(option
);
323 if (shash_find(&command
->options
, key
)) {
324 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
326 shash_add_nocopy(&command
->options
, key
, value
);
329 vsctl_fatal("missing command name");
332 p
= find_command(argv
[i
]);
334 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
337 SHASH_FOR_EACH (node
, &command
->options
) {
338 const char *s
= strstr(p
->options
, node
->name
);
339 int end
= s
? s
[strlen(node
->name
)] : EOF
;
341 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
342 vsctl_fatal("'%s' command has no '%s' option",
343 argv
[i
], node
->name
);
345 if ((end
== '=') != (node
->data
!= NULL
)) {
347 vsctl_fatal("missing argument to '%s' option on '%s' "
348 "command", node
->name
, argv
[i
]);
350 vsctl_fatal("'%s' option on '%s' does not accept an "
351 "argument", node
->name
, argv
[i
]);
356 n_arg
= argc
- i
- 1;
357 if (n_arg
< p
->min_args
) {
358 vsctl_fatal("'%s' command requires at least %d arguments",
359 p
->name
, p
->min_args
);
360 } else if (n_arg
> p
->max_args
) {
363 for (j
= i
+ 1; j
< argc
; j
++) {
364 if (argv
[j
][0] == '-') {
365 vsctl_fatal("'%s' command takes at most %d arguments "
366 "(note that options must precede command "
367 "names and follow a \"--\" argument)",
368 p
->name
, p
->max_args
);
372 vsctl_fatal("'%s' command takes at most %d arguments",
373 p
->name
, p
->max_args
);
377 command
->argc
= n_arg
+ 1;
378 command
->argv
= &argv
[i
];
381 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
382 * null pointer if there is none. */
383 static const struct vsctl_command_syntax
*
384 find_command(const char *name
)
386 static struct shash commands
= SHASH_INITIALIZER(&commands
);
388 if (shash_is_empty(&commands
)) {
389 const struct vsctl_command_syntax
*p
;
391 for (p
= all_commands
; p
->name
; p
++) {
392 shash_add_assert(&commands
, p
->name
, p
);
396 return shash_find_data(&commands
, name
);
400 vsctl_fatal(const char *format
, ...)
405 va_start(args
, format
);
406 message
= xvasprintf(format
, args
);
409 vlog_set_levels(&VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
410 VLOG_ERR("%s", message
);
411 ovs_error(0, "%s", message
);
412 vsctl_exit(EXIT_FAILURE
);
415 /* Frees the current transaction and the underlying IDL and then calls
418 * Freeing the transaction and the IDL is not strictly necessary, but it makes
419 * for a clean memory leak report from valgrind in the normal case. That makes
420 * it easier to notice real memory leaks. */
422 vsctl_exit(int status
)
425 ovsdb_idl_txn_abort(the_idl_txn
);
426 ovsdb_idl_txn_destroy(the_idl_txn
);
428 ovsdb_idl_destroy(the_idl
);
436 %s: ovs-vswitchd management utility\n\
437 usage: %s [OPTIONS] COMMAND [ARG...]\n\
440 add-br BRIDGE create a new bridge named BRIDGE\n\
441 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
442 del-br BRIDGE delete BRIDGE and all of its ports\n\
443 list-br print the names of all the bridges\n\
444 br-exists BRIDGE test whether BRIDGE exists\n\
445 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
446 br-to-parent BRIDGE print the parent of BRIDGE\n\
447 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
448 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
449 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
450 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
453 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
454 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
455 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
456 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
457 port-to-br PORT print name of bridge that contains PORT\n\
458 A bond is considered to be a single port.\n\
460 Interface commands (a bond consists of multiple interfaces):\n\
461 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
462 iface-to-br IFACE print name of bridge that contains IFACE\n\
464 Controller commands:\n\
465 get-controller BRIDGE print the controller for BRIDGE\n\
466 del-controller BRIDGE delete the controller for BRIDGE\n\
467 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
468 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
469 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
470 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
473 get-ssl print the SSL configuration\n\
474 del-ssl delete the SSL configuration\n\
475 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
478 emer-reset reset switch to known good state\n\
480 Database commands:\n\
481 list TBL [REC] list RECord (or all records) in TBL\n\
482 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
483 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
484 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
485 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
486 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
487 create TBL COL[:KEY]=VALUE create and initialize new record\n\
488 destroy TBL REC delete RECord from TBL\n\
489 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
490 Potentially unsafe database commands require --force option.\n\
493 --db=DATABASE connect to DATABASE\n\
495 --oneline print exactly one line of output per command\n",
496 program_name
, program_name
, default_db());
500 -h, --help display this help message\n\
501 -V, --version display version information\n");
510 def
= xasprintf("unix:%s/db.sock", ovs_rundir
);
515 /* Returns true if it looks like this set of arguments might modify the
516 * database, otherwise false. (Not very smart, so it's prone to false
519 might_write_to_db(char **argv
)
521 for (; *argv
; argv
++) {
522 const struct vsctl_command_syntax
*p
= find_command(*argv
);
523 if (p
&& p
->mode
== RW
) {
530 struct vsctl_context
{
534 struct shash options
;
536 /* Modifiable state. */
538 struct ovsdb_idl
*idl
;
539 struct ovsdb_idl_txn
*txn
;
540 struct ovsdb_symbol_table
*symtab
;
541 const struct ovsrec_open_vswitch
*ovs
;
543 /* A command may set this member to true if some prerequisite is not met
544 * and the caller should wait for something to change and then retry. */
548 struct vsctl_bridge
{
549 struct ovsrec_bridge
*br_cfg
;
551 struct ovsrec_controller
**ctrl
;
554 struct vsctl_bridge
*parent
;
559 struct ovsrec_port
*port_cfg
;
560 struct vsctl_bridge
*bridge
;
564 struct ovsrec_interface
*iface_cfg
;
565 struct vsctl_port
*port
;
569 struct vsctl_context
*ctx
;
570 struct shash bridges
;
576 vsctl_context_to_string(const struct vsctl_context
*ctx
)
578 const struct shash_node
*node
;
584 SHASH_FOR_EACH (node
, &ctx
->options
) {
585 svec_add(&words
, node
->name
);
587 for (i
= 0; i
< ctx
->argc
; i
++) {
588 svec_add(&words
, ctx
->argv
[i
]);
590 svec_terminate(&words
);
592 s
= process_escape_args(words
.names
);
594 svec_destroy(&words
);
599 static struct vsctl_bridge
*
600 add_bridge(struct vsctl_info
*b
,
601 struct ovsrec_bridge
*br_cfg
, const char *name
,
602 struct vsctl_bridge
*parent
, int vlan
)
604 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
606 br
->name
= xstrdup(name
);
610 br
->ctrl
= parent
->br_cfg
->controller
;
611 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
612 br
->fail_mode
= parent
->br_cfg
->fail_mode
;
614 br
->ctrl
= br_cfg
->controller
;
615 br
->n_ctrl
= br_cfg
->n_controller
;
616 br
->fail_mode
= br_cfg
->fail_mode
;
618 shash_add(&b
->bridges
, br
->name
, br
);
623 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
625 return (port_cfg
->fake_bridge
627 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095);
630 static struct vsctl_bridge
*
631 find_vlan_bridge(struct vsctl_info
*info
,
632 struct vsctl_bridge
*parent
, int vlan
)
634 struct shash_node
*node
;
636 SHASH_FOR_EACH (node
, &info
->bridges
) {
637 struct vsctl_bridge
*br
= node
->data
;
638 if (br
->parent
== parent
&& br
->vlan
== vlan
) {
647 free_info(struct vsctl_info
*info
)
649 struct shash_node
*node
;
651 SHASH_FOR_EACH (node
, &info
->bridges
) {
652 struct vsctl_bridge
*bridge
= node
->data
;
656 shash_destroy(&info
->bridges
);
658 shash_destroy_free_data(&info
->ports
);
659 shash_destroy_free_data(&info
->ifaces
);
663 get_info(struct vsctl_context
*ctx
, struct vsctl_info
*info
)
665 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
666 struct shash bridges
, ports
;
670 shash_init(&info
->bridges
);
671 shash_init(&info
->ports
);
672 shash_init(&info
->ifaces
);
674 shash_init(&bridges
);
676 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
677 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
678 struct vsctl_bridge
*br
;
681 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
682 VLOG_WARN("%s: database contains duplicate bridge name",
686 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
691 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
692 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
694 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
695 VLOG_WARN("%s: database contains duplicate port name",
700 if (port_is_fake_bridge(port_cfg
)
701 && shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
702 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
706 shash_destroy(&bridges
);
707 shash_destroy(&ports
);
709 shash_init(&bridges
);
711 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
712 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
713 struct vsctl_bridge
*br
;
716 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
719 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
720 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
721 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
722 struct vsctl_port
*port
;
725 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
729 if (port_is_fake_bridge(port_cfg
)
730 && !shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
734 port
= xmalloc(sizeof *port
);
735 port
->port_cfg
= port_cfg
;
737 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
738 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
745 shash_add(&info
->ports
, port_cfg
->name
, port
);
747 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
748 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
749 struct vsctl_iface
*iface
;
751 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
752 VLOG_WARN("%s: database contains duplicate interface name",
757 iface
= xmalloc(sizeof *iface
);
758 iface
->iface_cfg
= iface_cfg
;
760 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
764 shash_destroy(&bridges
);
765 shash_destroy(&ports
);
769 check_conflicts(struct vsctl_info
*info
, const char *name
,
772 struct vsctl_iface
*iface
;
773 struct vsctl_port
*port
;
775 if (shash_find(&info
->bridges
, name
)) {
776 vsctl_fatal("%s because a bridge named %s already exists",
780 port
= shash_find_data(&info
->ports
, name
);
782 vsctl_fatal("%s because a port named %s already exists on "
783 "bridge %s", msg
, name
, port
->bridge
->name
);
786 iface
= shash_find_data(&info
->ifaces
, name
);
788 vsctl_fatal("%s because an interface named %s already exists "
789 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
795 static struct vsctl_bridge
*
796 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
798 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
799 if (must_exist
&& !br
) {
800 vsctl_fatal("no bridge named %s", name
);
805 static struct vsctl_bridge
*
806 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
808 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
809 if (br
&& br
->parent
) {
810 vsctl_fatal("%s is a fake bridge", name
);
815 static struct vsctl_port
*
816 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
818 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
819 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
822 if (must_exist
&& !port
) {
823 vsctl_fatal("no port named %s", name
);
828 static struct vsctl_iface
*
829 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
831 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
832 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
835 if (must_exist
&& !iface
) {
836 vsctl_fatal("no interface named %s", name
);
842 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
844 struct ovsrec_port
**ports
;
847 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
848 for (i
= 0; i
< br
->n_ports
; i
++) {
849 ports
[i
] = br
->ports
[i
];
851 ports
[br
->n_ports
] = port
;
852 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
857 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
859 struct ovsrec_port
**ports
;
862 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
863 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
864 if (br
->ports
[i
] != port
) {
865 ports
[n
++] = br
->ports
[i
];
868 ovsrec_bridge_set_ports(br
, ports
, n
);
873 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
874 struct ovsrec_bridge
*bridge
)
876 struct ovsrec_bridge
**bridges
;
879 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
880 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
881 bridges
[i
] = ovs
->bridges
[i
];
883 bridges
[ovs
->n_bridges
] = bridge
;
884 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
889 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
890 struct ovsrec_bridge
*bridge
)
892 struct ovsrec_bridge
**bridges
;
895 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
896 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
897 if (ovs
->bridges
[i
] != bridge
) {
898 bridges
[n
++] = ovs
->bridges
[i
];
901 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
906 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
911 cmd_emer_reset(struct vsctl_context
*ctx
)
913 const struct ovsdb_idl
*idl
= ctx
->idl
;
914 const struct ovsrec_bridge
*br
;
915 const struct ovsrec_port
*port
;
916 const struct ovsrec_interface
*iface
;
917 const struct ovsrec_mirror
*mirror
, *next_mirror
;
918 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
919 const struct ovsrec_netflow
*nf
, *next_nf
;
920 const struct ovsrec_ssl
*ssl
, *next_ssl
;
921 const struct ovsrec_sflow
*sflow
, *next_sflow
;
924 /* Reset the Open_vSwitch table. */
925 ovsrec_open_vswitch_set_managers(ctx
->ovs
, NULL
, 0);
926 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
928 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
930 char *hw_key
= "hwaddr";
933 ovsrec_bridge_set_controller(br
, NULL
, 0);
934 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
935 ovsrec_bridge_set_netflow(br
, NULL
);
936 ovsrec_bridge_set_sflow(br
, NULL
);
937 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
939 /* We only want to save the "hwaddr" key from other_config. */
940 for (i
=0; i
< br
->n_other_config
; i
++) {
941 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
942 hw_val
= br
->value_other_config
[i
];
947 char *val
= xstrdup(hw_val
);
948 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
951 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
955 OVSREC_PORT_FOR_EACH (port
, idl
) {
956 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
959 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
960 /* xxx What do we do about gre/patch devices created by mgr? */
962 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
963 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
966 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
967 ovsrec_mirror_delete(mirror
);
970 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
971 ovsrec_controller_delete(ctrl
);
974 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
975 ovsrec_netflow_delete(nf
);
978 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
979 ovsrec_ssl_delete(ssl
);
982 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
983 ovsrec_sflow_delete(sflow
);
988 cmd_add_br(struct vsctl_context
*ctx
)
990 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
991 const char *br_name
, *parent_name
;
992 struct vsctl_info info
;
995 br_name
= ctx
->argv
[1];
996 if (ctx
->argc
== 2) {
999 } else if (ctx
->argc
== 4) {
1000 parent_name
= ctx
->argv
[2];
1001 vlan
= atoi(ctx
->argv
[3]);
1002 if (vlan
< 1 || vlan
> 4095) {
1003 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
1006 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1010 get_info(ctx
, &info
);
1012 struct vsctl_bridge
*br
;
1014 br
= find_bridge(&info
, br_name
, false);
1018 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1019 "a VLAN bridge for VLAN %d",
1020 br_name
, br_name
, br
->vlan
);
1024 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1025 "is not a VLAN bridge",
1026 br_name
, parent_name
, vlan
, br_name
);
1027 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1028 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1029 "has the wrong parent %s",
1030 br_name
, parent_name
, vlan
,
1031 br_name
, br
->parent
->name
);
1032 } else if (br
->vlan
!= vlan
) {
1033 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1034 "is a VLAN bridge for the wrong VLAN %d",
1035 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1041 check_conflicts(&info
, br_name
,
1042 xasprintf("cannot create a bridge named %s", br_name
));
1045 struct ovsrec_port
*port
;
1046 struct ovsrec_interface
*iface
;
1047 struct ovsrec_bridge
*br
;
1049 iface
= ovsrec_interface_insert(ctx
->txn
);
1050 ovsrec_interface_set_name(iface
, br_name
);
1052 port
= ovsrec_port_insert(ctx
->txn
);
1053 ovsrec_port_set_name(port
, br_name
);
1054 ovsrec_port_set_interfaces(port
, &iface
, 1);
1056 br
= ovsrec_bridge_insert(ctx
->txn
);
1057 ovsrec_bridge_set_name(br
, br_name
);
1058 ovsrec_bridge_set_ports(br
, &port
, 1);
1060 ovs_insert_bridge(ctx
->ovs
, br
);
1062 struct vsctl_bridge
*parent
;
1063 struct ovsrec_port
*port
;
1064 struct ovsrec_interface
*iface
;
1065 struct ovsrec_bridge
*br
;
1068 parent
= find_bridge(&info
, parent_name
, false);
1069 if (parent
&& parent
->vlan
) {
1070 vsctl_fatal("cannot create bridge with fake bridge as parent");
1073 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1075 br
= parent
->br_cfg
;
1077 iface
= ovsrec_interface_insert(ctx
->txn
);
1078 ovsrec_interface_set_name(iface
, br_name
);
1079 ovsrec_interface_set_type(iface
, "internal");
1081 port
= ovsrec_port_insert(ctx
->txn
);
1082 ovsrec_port_set_name(port
, br_name
);
1083 ovsrec_port_set_interfaces(port
, &iface
, 1);
1084 ovsrec_port_set_fake_bridge(port
, true);
1085 ovsrec_port_set_tag(port
, &tag
, 1);
1087 bridge_insert_port(br
, port
);
1094 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1096 struct shash_node
*node
;
1098 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1099 struct vsctl_iface
*iface
= node
->data
;
1100 if (iface
->port
== port
) {
1101 ovsrec_interface_delete(iface
->iface_cfg
);
1104 ovsrec_port_delete(port
->port_cfg
);
1106 bridge_delete_port((port
->bridge
->parent
1107 ? port
->bridge
->parent
->br_cfg
1108 : port
->bridge
->br_cfg
), port
->port_cfg
);
1112 cmd_del_br(struct vsctl_context
*ctx
)
1114 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1115 struct vsctl_bridge
*bridge
;
1116 struct vsctl_info info
;
1118 get_info(ctx
, &info
);
1119 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1121 struct shash_node
*node
;
1123 SHASH_FOR_EACH (node
, &info
.ports
) {
1124 struct vsctl_port
*port
= node
->data
;
1125 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1126 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1127 del_port(&info
, port
);
1130 if (bridge
->br_cfg
) {
1131 ovsrec_bridge_delete(bridge
->br_cfg
);
1132 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1139 output_sorted(struct svec
*svec
, struct ds
*output
)
1145 SVEC_FOR_EACH (i
, name
, svec
) {
1146 ds_put_format(output
, "%s\n", name
);
1151 cmd_list_br(struct vsctl_context
*ctx
)
1153 struct shash_node
*node
;
1154 struct vsctl_info info
;
1155 struct svec bridges
;
1157 get_info(ctx
, &info
);
1159 svec_init(&bridges
);
1160 SHASH_FOR_EACH (node
, &info
.bridges
) {
1161 struct vsctl_bridge
*br
= node
->data
;
1162 svec_add(&bridges
, br
->name
);
1164 output_sorted(&bridges
, &ctx
->output
);
1165 svec_destroy(&bridges
);
1171 cmd_br_exists(struct vsctl_context
*ctx
)
1173 struct vsctl_info info
;
1175 get_info(ctx
, &info
);
1176 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1182 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1183 * equals 'a', false otherwise. */
1185 key_matches(const char *a
,
1186 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1188 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1192 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1193 char *key
, char *value
,
1194 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1201 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1202 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1204 for (i
= 0; i
< old_n
; i
++) {
1205 if (strcmp(key
, old_keys
[i
])) {
1206 new_keys
[new_n
] = old_keys
[i
];
1207 new_values
[new_n
] = old_values
[i
];
1212 new_keys
[new_n
] = key
;
1213 new_values
[new_n
] = value
;
1216 *new_keysp
= new_keys
;
1217 *new_valuesp
= new_values
;
1222 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1224 struct vsctl_info info
;
1225 struct vsctl_bridge
*bridge
;
1226 char **keys
, **values
;
1229 get_info(ctx
, &info
);
1230 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1231 if (bridge
->br_cfg
) {
1232 set_external_id(bridge
->br_cfg
->key_external_ids
,
1233 bridge
->br_cfg
->value_external_ids
,
1234 bridge
->br_cfg
->n_external_ids
,
1235 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1236 &keys
, &values
, &n
);
1237 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1239 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1240 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1241 set_external_id(port
->port_cfg
->key_external_ids
,
1242 port
->port_cfg
->value_external_ids
,
1243 port
->port_cfg
->n_external_ids
,
1244 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1245 &keys
, &values
, &n
);
1246 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1256 get_external_id(char **keys
, char **values
, size_t n
,
1257 const char *prefix
, const char *key
,
1260 size_t prefix_len
= strlen(prefix
);
1265 for (i
= 0; i
< n
; i
++) {
1266 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1267 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1268 keys
[i
] + prefix_len
, values
[i
]));
1269 } else if (key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1270 svec_add(&svec
, values
[i
]);
1274 output_sorted(&svec
, output
);
1275 svec_destroy(&svec
);
1279 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1281 struct vsctl_info info
;
1282 struct vsctl_bridge
*bridge
;
1284 get_info(ctx
, &info
);
1285 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1286 if (bridge
->br_cfg
) {
1287 get_external_id(bridge
->br_cfg
->key_external_ids
,
1288 bridge
->br_cfg
->value_external_ids
,
1289 bridge
->br_cfg
->n_external_ids
,
1290 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1293 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1294 get_external_id(port
->port_cfg
->key_external_ids
,
1295 port
->port_cfg
->value_external_ids
,
1296 port
->port_cfg
->n_external_ids
,
1297 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1304 cmd_list_ports(struct vsctl_context
*ctx
)
1306 struct vsctl_bridge
*br
;
1307 struct shash_node
*node
;
1308 struct vsctl_info info
;
1311 get_info(ctx
, &info
);
1312 br
= find_bridge(&info
, ctx
->argv
[1], true);
1315 SHASH_FOR_EACH (node
, &info
.ports
) {
1316 struct vsctl_port
*port
= node
->data
;
1318 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1319 svec_add(&ports
, port
->port_cfg
->name
);
1322 output_sorted(&ports
, &ctx
->output
);
1323 svec_destroy(&ports
);
1329 add_port(struct vsctl_context
*ctx
,
1330 const char *br_name
, const char *port_name
,
1331 bool may_exist
, bool fake_iface
,
1332 char *iface_names
[], int n_ifaces
,
1333 char *settings
[], int n_settings
)
1335 struct vsctl_info info
;
1336 struct vsctl_bridge
*bridge
;
1337 struct ovsrec_interface
**ifaces
;
1338 struct ovsrec_port
*port
;
1341 get_info(ctx
, &info
);
1343 struct vsctl_port
*vsctl_port
;
1345 vsctl_port
= find_port(&info
, port_name
, false);
1347 struct svec want_names
, have_names
;
1349 svec_init(&want_names
);
1350 for (i
= 0; i
< n_ifaces
; i
++) {
1351 svec_add(&want_names
, iface_names
[i
]);
1353 svec_sort(&want_names
);
1355 svec_init(&have_names
);
1356 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1357 svec_add(&have_names
,
1358 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1360 svec_sort(&have_names
);
1362 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1363 char *command
= vsctl_context_to_string(ctx
);
1364 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1365 command
, port_name
, vsctl_port
->bridge
->name
);
1368 if (!svec_equal(&want_names
, &have_names
)) {
1369 char *have_names_string
= svec_join(&have_names
, ", ", "");
1370 char *command
= vsctl_context_to_string(ctx
);
1372 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1373 command
, port_name
, have_names_string
);
1376 svec_destroy(&want_names
);
1377 svec_destroy(&have_names
);
1382 check_conflicts(&info
, port_name
,
1383 xasprintf("cannot create a port named %s", port_name
));
1384 for (i
= 0; i
< n_ifaces
; i
++) {
1385 check_conflicts(&info
, iface_names
[i
],
1386 xasprintf("cannot create an interface named %s",
1389 bridge
= find_bridge(&info
, br_name
, true);
1391 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1392 for (i
= 0; i
< n_ifaces
; i
++) {
1393 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1394 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1397 port
= ovsrec_port_insert(ctx
->txn
);
1398 ovsrec_port_set_name(port
, port_name
);
1399 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1400 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1404 int64_t tag
= bridge
->vlan
;
1405 ovsrec_port_set_tag(port
, &tag
, 1);
1408 for (i
= 0; i
< n_settings
; i
++) {
1409 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1413 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1414 : bridge
->br_cfg
), port
);
1420 cmd_add_port(struct vsctl_context
*ctx
)
1422 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1424 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1425 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1429 cmd_add_bond(struct vsctl_context
*ctx
)
1431 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1432 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1436 n_ifaces
= ctx
->argc
- 3;
1437 for (i
= 3; i
< ctx
->argc
; i
++) {
1438 if (strchr(ctx
->argv
[i
], '=')) {
1444 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1445 "%d were specified", n_ifaces
);
1448 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1449 &ctx
->argv
[3], n_ifaces
,
1450 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1454 cmd_del_port(struct vsctl_context
*ctx
)
1456 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1457 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1458 struct vsctl_port
*port
;
1459 struct vsctl_info info
;
1461 get_info(ctx
, &info
);
1463 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1465 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1466 struct vsctl_iface
*iface
;
1468 port
= find_port(&info
, target
, false);
1470 iface
= find_iface(&info
, target
, false);
1475 if (must_exist
&& !port
) {
1476 vsctl_fatal("no port or interface named %s", target
);
1481 if (ctx
->argc
== 3) {
1482 struct vsctl_bridge
*bridge
;
1484 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1485 if (port
->bridge
!= bridge
) {
1486 if (port
->bridge
->parent
== bridge
) {
1487 vsctl_fatal("bridge %s does not have a port %s (although "
1488 "its parent bridge %s does)",
1489 ctx
->argv
[1], ctx
->argv
[2],
1490 bridge
->parent
->name
);
1492 vsctl_fatal("bridge %s does not have a port %s",
1493 ctx
->argv
[1], ctx
->argv
[2]);
1498 del_port(&info
, port
);
1505 cmd_port_to_br(struct vsctl_context
*ctx
)
1507 struct vsctl_port
*port
;
1508 struct vsctl_info info
;
1510 get_info(ctx
, &info
);
1511 port
= find_port(&info
, ctx
->argv
[1], true);
1512 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1517 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1519 struct vsctl_bridge
*bridge
;
1520 struct vsctl_info info
;
1522 get_info(ctx
, &info
);
1523 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1524 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1529 cmd_br_to_parent(struct vsctl_context
*ctx
)
1531 struct vsctl_bridge
*bridge
;
1532 struct vsctl_info info
;
1534 get_info(ctx
, &info
);
1535 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1536 if (bridge
->parent
) {
1537 bridge
= bridge
->parent
;
1539 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1544 cmd_list_ifaces(struct vsctl_context
*ctx
)
1546 struct vsctl_bridge
*br
;
1547 struct shash_node
*node
;
1548 struct vsctl_info info
;
1551 get_info(ctx
, &info
);
1552 br
= find_bridge(&info
, ctx
->argv
[1], true);
1555 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1556 struct vsctl_iface
*iface
= node
->data
;
1558 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1559 && br
== iface
->port
->bridge
) {
1560 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1563 output_sorted(&ifaces
, &ctx
->output
);
1564 svec_destroy(&ifaces
);
1570 cmd_iface_to_br(struct vsctl_context
*ctx
)
1572 struct vsctl_iface
*iface
;
1573 struct vsctl_info info
;
1575 get_info(ctx
, &info
);
1576 iface
= find_iface(&info
, ctx
->argv
[1], true);
1577 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1582 cmd_get_controller(struct vsctl_context
*ctx
)
1584 struct vsctl_info info
;
1585 struct vsctl_bridge
*br
;
1586 struct svec targets
;
1589 get_info(ctx
, &info
);
1590 br
= find_bridge(&info
, ctx
->argv
[1], true);
1592 /* Print the targets in sorted order for reproducibility. */
1593 svec_init(&targets
);
1594 for (i
= 0; i
< br
->n_ctrl
; i
++) {
1595 svec_add(&targets
, br
->ctrl
[i
]->target
);
1598 svec_sort(&targets
);
1599 for (i
= 0; i
< targets
.n
; i
++) {
1600 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1602 svec_destroy(&targets
);
1608 delete_controllers(struct ovsrec_controller
**controllers
,
1609 size_t n_controllers
)
1613 for (i
= 0; i
< n_controllers
; i
++) {
1614 ovsrec_controller_delete(controllers
[i
]);
1619 cmd_del_controller(struct vsctl_context
*ctx
)
1621 struct vsctl_info info
;
1622 struct vsctl_bridge
*br
;
1624 get_info(ctx
, &info
);
1625 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1628 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1629 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1635 static struct ovsrec_controller
**
1636 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1638 struct ovsrec_controller
**controllers
;
1641 controllers
= xmalloc(n
* sizeof *controllers
);
1642 for (i
= 0; i
< n
; i
++) {
1643 controllers
[i
] = ovsrec_controller_insert(txn
);
1644 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1651 cmd_set_controller(struct vsctl_context
*ctx
)
1653 struct vsctl_info info
;
1654 struct vsctl_bridge
*br
;
1655 struct ovsrec_controller
**controllers
;
1658 get_info(ctx
, &info
);
1659 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1661 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1664 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1665 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1672 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1674 struct vsctl_info info
;
1675 struct vsctl_bridge
*br
;
1677 get_info(ctx
, &info
);
1678 br
= find_bridge(&info
, ctx
->argv
[1], true);
1680 if (br
->fail_mode
&& strlen(br
->fail_mode
)) {
1681 ds_put_format(&ctx
->output
, "%s\n", br
->fail_mode
);
1688 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1690 struct vsctl_info info
;
1691 struct vsctl_bridge
*br
;
1693 get_info(ctx
, &info
);
1694 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1696 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1702 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1704 struct vsctl_info info
;
1705 struct vsctl_bridge
*br
;
1706 const char *fail_mode
= ctx
->argv
[2];
1708 get_info(ctx
, &info
);
1709 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1711 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1712 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1715 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1721 cmd_get_ssl(struct vsctl_context
*ctx
)
1723 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1726 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
1727 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
1728 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
1729 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
1730 ssl
->bootstrap_ca_cert
? "true" : "false");
1735 cmd_del_ssl(struct vsctl_context
*ctx
)
1737 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1740 ovsrec_ssl_delete(ssl
);
1741 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1746 cmd_set_ssl(struct vsctl_context
*ctx
)
1748 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
1749 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1752 ovsrec_ssl_delete(ssl
);
1754 ssl
= ovsrec_ssl_insert(ctx
->txn
);
1756 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
1757 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
1758 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
1760 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
1762 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
1765 /* Parameter commands. */
1767 struct vsctl_row_id
{
1768 const struct ovsdb_idl_table_class
*table
;
1769 const struct ovsdb_idl_column
*name_column
;
1770 const struct ovsdb_idl_column
*uuid_column
;
1773 struct vsctl_table_class
{
1774 struct ovsdb_idl_table_class
*class;
1775 struct vsctl_row_id row_ids
[2];
1778 static const struct vsctl_table_class tables
[] = {
1779 {&ovsrec_table_bridge
,
1780 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
1781 {NULL
, NULL
, NULL
}}},
1783 {&ovsrec_table_controller
,
1784 {{&ovsrec_table_bridge
,
1785 &ovsrec_bridge_col_name
,
1786 &ovsrec_bridge_col_controller
}}},
1788 {&ovsrec_table_interface
,
1789 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
1790 {NULL
, NULL
, NULL
}}},
1792 {&ovsrec_table_mirror
,
1793 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
1794 {NULL
, NULL
, NULL
}}},
1796 {&ovsrec_table_netflow
,
1797 {{&ovsrec_table_bridge
,
1798 &ovsrec_bridge_col_name
,
1799 &ovsrec_bridge_col_netflow
},
1800 {NULL
, NULL
, NULL
}}},
1802 {&ovsrec_table_open_vswitch
,
1803 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
1804 {NULL
, NULL
, NULL
}}},
1806 {&ovsrec_table_port
,
1807 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
1808 {NULL
, NULL
, NULL
}}},
1811 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
1812 {NULL
, NULL
, NULL
}}},
1814 {&ovsrec_table_queue
,
1815 {{NULL
, NULL
, NULL
},
1816 {NULL
, NULL
, NULL
}}},
1819 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
1821 {&ovsrec_table_sflow
,
1822 {{&ovsrec_table_bridge
,
1823 &ovsrec_bridge_col_name
,
1824 &ovsrec_bridge_col_sflow
},
1825 {NULL
, NULL
, NULL
}}},
1827 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
1831 die_if_error(char *error
)
1834 vsctl_fatal("%s", error
);
1839 to_lower_and_underscores(unsigned c
)
1841 return c
== '-' ? '_' : tolower(c
);
1845 score_partial_match(const char *name
, const char *s
)
1849 if (!strcmp(name
, s
)) {
1852 for (score
= 0; ; score
++, name
++, s
++) {
1853 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
1855 } else if (*name
== '\0') {
1856 return UINT_MAX
- 1;
1859 return *s
== '\0' ? score
: 0;
1862 static const struct vsctl_table_class
*
1863 get_table(const char *table_name
)
1865 const struct vsctl_table_class
*table
;
1866 const struct vsctl_table_class
*best_match
= NULL
;
1867 unsigned int best_score
= 0;
1869 for (table
= tables
; table
->class; table
++) {
1870 unsigned int score
= score_partial_match(table
->class->name
,
1872 if (score
> best_score
) {
1875 } else if (score
== best_score
) {
1881 } else if (best_score
) {
1882 vsctl_fatal("multiple table names match \"%s\"", table_name
);
1884 vsctl_fatal("unknown table \"%s\"", table_name
);
1888 static const struct ovsdb_idl_row
*
1889 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
1890 const struct vsctl_row_id
*id
, const char *record_id
)
1892 const struct ovsdb_idl_row
*referrer
, *final
;
1898 if (!id
->name_column
) {
1899 if (strcmp(record_id
, ".")) {
1902 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
1903 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
1907 const struct ovsdb_idl_row
*row
;
1910 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
1912 row
= ovsdb_idl_next_row(row
))
1914 const struct ovsdb_datum
*name
;
1916 name
= ovsdb_idl_get(row
, id
->name_column
,
1917 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
1918 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
1920 vsctl_fatal("multiple rows in %s match \"%s\"",
1921 table
->class->name
, record_id
);
1932 if (id
->uuid_column
) {
1933 const struct ovsdb_datum
*uuid
;
1935 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
1936 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
1938 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
1939 &uuid
->keys
[0].uuid
);
1948 static const struct ovsdb_idl_row
*
1949 get_row (struct vsctl_context
*ctx
,
1950 const struct vsctl_table_class
*table
, const char *record_id
)
1952 const struct ovsdb_idl_row
*row
;
1955 if (uuid_from_string(&uuid
, record_id
)) {
1956 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
1960 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
1961 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
1970 static const struct ovsdb_idl_row
*
1971 must_get_row(struct vsctl_context
*ctx
,
1972 const struct vsctl_table_class
*table
, const char *record_id
)
1974 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
1976 vsctl_fatal("no row \"%s\" in table %s",
1977 record_id
, table
->class->name
);
1983 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
1984 const struct ovsdb_idl_column
**columnp
)
1986 const struct ovsdb_idl_column
*best_match
= NULL
;
1987 unsigned int best_score
= 0;
1990 for (i
= 0; i
< table
->class->n_columns
; i
++) {
1991 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
1992 unsigned int score
= score_partial_match(column
->name
, column_name
);
1993 if (score
> best_score
) {
1994 best_match
= column
;
1996 } else if (score
== best_score
) {
2001 *columnp
= best_match
;
2004 } else if (best_score
) {
2005 return xasprintf("%s contains more than one column whose name "
2006 "matches \"%s\"", table
->class->name
, column_name
);
2008 return xasprintf("%s does not contain a column whose name matches "
2009 "\"%s\"", table
->class->name
, column_name
);
2013 static struct uuid
*
2014 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
2016 struct ovsdb_symbol
*symbol
;
2019 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2023 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
2026 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
2028 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2031 symbol
->used
= true;
2032 return &symbol
->uuid
;
2036 missing_operator_error(const char *arg
, const char **allowed_operators
,
2042 ds_put_format(&s
, "%s: argument does not end in ", arg
);
2043 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
2044 if (n_allowed
== 2) {
2045 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
2046 } else if (n_allowed
> 2) {
2049 for (i
= 1; i
< n_allowed
- 1; i
++) {
2050 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
2052 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
2054 ds_put_format(&s
, " followed by a value.");
2056 return ds_steal_cstr(&s
);
2059 /* Breaks 'arg' apart into a number of fields in the following order:
2061 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2062 * is stored into '*columnp'. The column name may be abbreviated.
2064 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2065 * and 'keyp' are nonnull, then the column and key names are expected to
2066 * be separated by ':'). The key is stored as a malloc()'d string into
2067 * '*keyp', or NULL if no key is present in 'arg'.
2069 * - If 'valuep' is nonnull, an operator followed by a value string. The
2070 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2071 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2072 * operator is stored into '*operatorp' (one of the pointers from
2073 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2074 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2077 * At least 'columnp' or 'keyp' must be nonnull.
2079 * On success, returns NULL. On failure, returned a malloc()'d string error
2080 * message and stores NULL into all of the nonnull output arguments. */
2081 static char * WARN_UNUSED_RESULT
2082 parse_column_key_value(const char *arg
,
2083 const struct vsctl_table_class
*table
,
2084 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2085 const char **operatorp
,
2086 const char **allowed_operators
, size_t n_allowed
,
2089 const char *p
= arg
;
2092 assert(columnp
|| keyp
);
2093 assert(!(operatorp
&& !valuep
));
2101 /* Parse column name. */
2105 error
= ovsdb_token_parse(&p
, &column_name
);
2109 if (column_name
[0] == '\0') {
2111 error
= xasprintf("%s: missing column name", arg
);
2114 error
= get_column(table
, column_name
, columnp
);
2121 /* Parse key string. */
2122 if (*p
== ':' || !columnp
) {
2126 error
= xasprintf("%s: key not accepted here", arg
);
2129 error
= ovsdb_token_parse(&p
, keyp
);
2137 /* Parse value string. */
2143 if (!allowed_operators
) {
2144 static const char *equals
= "=";
2145 allowed_operators
= &equals
;
2151 for (i
= 0; i
< n_allowed
; i
++) {
2152 const char *op
= allowed_operators
[i
];
2153 size_t op_len
= strlen(op
);
2155 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2161 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2168 *valuep
= xstrdup(p
+ best_len
);
2174 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2200 cmd_get(struct vsctl_context
*ctx
)
2202 const char *id
= shash_find_data(&ctx
->options
, "--id");
2203 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2204 const char *table_name
= ctx
->argv
[1];
2205 const char *record_id
= ctx
->argv
[2];
2206 const struct vsctl_table_class
*table
;
2207 const struct ovsdb_idl_row
*row
;
2208 struct ds
*out
= &ctx
->output
;
2211 table
= get_table(table_name
);
2212 row
= must_get_row(ctx
, table
, record_id
);
2216 *create_symbol(ctx
->symtab
, id
, &new) = row
->uuid
;
2218 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2219 "before it was defined", id
);
2222 for (i
= 3; i
< ctx
->argc
; i
++) {
2223 const struct ovsdb_idl_column
*column
;
2224 const struct ovsdb_datum
*datum
;
2227 /* Special case for obtaining the UUID of a row. We can't just do this
2228 * through parse_column_key_value() below since it returns a "struct
2229 * ovsdb_idl_column" and the UUID column doesn't have one. */
2230 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2231 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2232 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2236 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2237 &column
, &key_string
,
2238 NULL
, NULL
, 0, NULL
));
2240 datum
= ovsdb_idl_read(row
, column
);
2242 union ovsdb_atom key
;
2245 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2246 vsctl_fatal("cannot specify key to get for non-map column %s",
2250 die_if_error(ovsdb_atom_from_string(&key
,
2252 key_string
, ctx
->symtab
));
2254 idx
= ovsdb_datum_find_key(datum
, &key
,
2255 column
->type
.key
.type
);
2256 if (idx
== UINT_MAX
) {
2258 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2259 key_string
, table
->class->name
, record_id
,
2263 ovsdb_atom_to_string(&datum
->values
[idx
],
2264 column
->type
.value
.type
, out
);
2266 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2268 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2270 ds_put_char(out
, '\n');
2277 list_record(const struct vsctl_table_class
*table
,
2278 const struct ovsdb_idl_row
*row
, struct ds
*out
)
2282 ds_put_format(out
, "%-20s: "UUID_FMT
"\n", "_uuid",
2283 UUID_ARGS(&row
->uuid
));
2284 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2285 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2286 const struct ovsdb_datum
*datum
;
2288 datum
= ovsdb_idl_read(row
, column
);
2290 ds_put_format(out
, "%-20s: ", column
->name
);
2291 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2292 ds_put_char(out
, '\n');
2297 cmd_list(struct vsctl_context
*ctx
)
2299 const char *table_name
= ctx
->argv
[1];
2300 const struct vsctl_table_class
*table
;
2301 struct ds
*out
= &ctx
->output
;
2304 table
= get_table(table_name
);
2305 if (ctx
->argc
> 2) {
2306 for (i
= 2; i
< ctx
->argc
; i
++) {
2308 ds_put_char(out
, '\n');
2310 list_record(table
, must_get_row(ctx
, table
, ctx
->argv
[i
]), out
);
2313 const struct ovsdb_idl_row
*row
;
2316 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class), first
= true;
2318 row
= ovsdb_idl_next_row(row
), first
= false) {
2320 ds_put_char(out
, '\n');
2322 list_record(table
, row
, out
);
2328 set_column(const struct vsctl_table_class
*table
,
2329 const struct ovsdb_idl_row
*row
, const char *arg
,
2330 struct ovsdb_symbol_table
*symtab
)
2332 const struct ovsdb_idl_column
*column
;
2333 char *key_string
, *value_string
;
2336 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2337 NULL
, NULL
, 0, &value_string
);
2338 die_if_error(error
);
2339 if (!value_string
) {
2340 vsctl_fatal("%s: missing value", arg
);
2344 union ovsdb_atom key
, value
;
2345 struct ovsdb_datum datum
;
2347 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2348 vsctl_fatal("cannot specify key to set for non-map column %s",
2352 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2353 key_string
, symtab
));
2354 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2355 value_string
, symtab
));
2357 ovsdb_datum_init_empty(&datum
);
2358 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
2360 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2361 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2363 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
2364 &column
->type
, false);
2365 ovsdb_idl_txn_write(row
, column
, &datum
);
2367 struct ovsdb_datum datum
;
2369 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2370 value_string
, symtab
));
2371 ovsdb_idl_txn_write(row
, column
, &datum
);
2379 cmd_set(struct vsctl_context
*ctx
)
2381 const char *table_name
= ctx
->argv
[1];
2382 const char *record_id
= ctx
->argv
[2];
2383 const struct vsctl_table_class
*table
;
2384 const struct ovsdb_idl_row
*row
;
2387 table
= get_table(table_name
);
2388 row
= must_get_row(ctx
, table
, record_id
);
2389 for (i
= 3; i
< ctx
->argc
; i
++) {
2390 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2395 cmd_add(struct vsctl_context
*ctx
)
2397 const char *table_name
= ctx
->argv
[1];
2398 const char *record_id
= ctx
->argv
[2];
2399 const char *column_name
= ctx
->argv
[3];
2400 const struct vsctl_table_class
*table
;
2401 const struct ovsdb_idl_column
*column
;
2402 const struct ovsdb_idl_row
*row
;
2403 const struct ovsdb_type
*type
;
2404 struct ovsdb_datum old
;
2407 table
= get_table(table_name
);
2408 row
= must_get_row(ctx
, table
, record_id
);
2409 die_if_error(get_column(table
, column_name
, &column
));
2411 type
= &column
->type
;
2412 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2413 for (i
= 4; i
< ctx
->argc
; i
++) {
2414 struct ovsdb_type add_type
;
2415 struct ovsdb_datum add
;
2419 add_type
.n_max
= UINT_MAX
;
2420 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2422 ovsdb_datum_union(&old
, &add
, type
, false);
2423 ovsdb_datum_destroy(&add
, type
);
2425 if (old
.n
> type
->n_max
) {
2426 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2427 "table %s but the maximum number is %u",
2429 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2430 column
->name
, table
->class->name
, type
->n_max
);
2432 ovsdb_idl_txn_write(row
, column
, &old
);
2436 cmd_remove(struct vsctl_context
*ctx
)
2438 const char *table_name
= ctx
->argv
[1];
2439 const char *record_id
= ctx
->argv
[2];
2440 const char *column_name
= ctx
->argv
[3];
2441 const struct vsctl_table_class
*table
;
2442 const struct ovsdb_idl_column
*column
;
2443 const struct ovsdb_idl_row
*row
;
2444 const struct ovsdb_type
*type
;
2445 struct ovsdb_datum old
;
2448 table
= get_table(table_name
);
2449 row
= must_get_row(ctx
, table
, record_id
);
2450 die_if_error(get_column(table
, column_name
, &column
));
2452 type
= &column
->type
;
2453 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2454 for (i
= 4; i
< ctx
->argc
; i
++) {
2455 struct ovsdb_type rm_type
;
2456 struct ovsdb_datum rm
;
2461 rm_type
.n_max
= UINT_MAX
;
2462 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
2463 ctx
->argv
[i
], ctx
->symtab
);
2464 if (error
&& ovsdb_type_is_map(&rm_type
)) {
2466 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
2467 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
2468 ctx
->argv
[i
], ctx
->symtab
));
2470 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
2471 ovsdb_datum_destroy(&rm
, &rm_type
);
2473 if (old
.n
< type
->n_min
) {
2474 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2475 "table %s but the minimum number is %u",
2477 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2478 column
->name
, table
->class->name
, type
->n_min
);
2480 ovsdb_idl_txn_write(row
, column
, &old
);
2484 cmd_clear(struct vsctl_context
*ctx
)
2486 const char *table_name
= ctx
->argv
[1];
2487 const char *record_id
= ctx
->argv
[2];
2488 const struct vsctl_table_class
*table
;
2489 const struct ovsdb_idl_row
*row
;
2492 table
= get_table(table_name
);
2493 row
= must_get_row(ctx
, table
, record_id
);
2494 for (i
= 3; i
< ctx
->argc
; i
++) {
2495 const struct ovsdb_idl_column
*column
;
2496 const struct ovsdb_type
*type
;
2497 struct ovsdb_datum datum
;
2499 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
2501 type
= &column
->type
;
2502 if (type
->n_min
> 0) {
2503 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2504 "of table %s, which is not allowed to be empty",
2505 column
->name
, table
->class->name
);
2508 ovsdb_datum_init_empty(&datum
);
2509 ovsdb_idl_txn_write(row
, column
, &datum
);
2514 cmd_create(struct vsctl_context
*ctx
)
2516 const char *id
= shash_find_data(&ctx
->options
, "--id");
2517 const char *table_name
= ctx
->argv
[1];
2518 const struct vsctl_table_class
*table
;
2519 const struct ovsdb_idl_row
*row
;
2520 const struct uuid
*uuid
;
2523 uuid
= id
? create_symbol(ctx
->symtab
, id
, NULL
) : NULL
;
2525 table
= get_table(table_name
);
2526 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
2527 for (i
= 2; i
< ctx
->argc
; i
++) {
2528 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2530 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
2533 /* This function may be used as the 'postprocess' function for commands that
2534 * insert new rows into the database. It expects that the command's 'run'
2535 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2536 * sole output. It replaces that output by the row's permanent UUID assigned
2537 * by the database server and appends a new-line.
2539 * Currently we use this only for "create", because the higher-level commands
2540 * are supposed to be independent of the actual structure of the vswitch
2543 post_create(struct vsctl_context
*ctx
)
2545 const struct uuid
*real
;
2548 uuid_from_string(&dummy
, ds_cstr(&ctx
->output
));
2549 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
2551 ds_clear(&ctx
->output
);
2552 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
2554 ds_put_char(&ctx
->output
, '\n');
2558 cmd_destroy(struct vsctl_context
*ctx
)
2560 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
2561 const char *table_name
= ctx
->argv
[1];
2562 const struct vsctl_table_class
*table
;
2565 table
= get_table(table_name
);
2566 for (i
= 2; i
< ctx
->argc
; i
++) {
2567 const struct ovsdb_idl_row
*row
;
2569 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
2571 ovsdb_idl_txn_delete(row
);
2577 is_condition_satified(const struct vsctl_table_class
*table
,
2578 const struct ovsdb_idl_row
*row
, const char *arg
,
2579 struct ovsdb_symbol_table
*symtab
)
2581 static const char *operators
[] = {
2582 "=", "!=", "<", ">", "<=", ">="
2585 const struct ovsdb_idl_column
*column
;
2586 const struct ovsdb_datum
*have_datum
;
2587 char *key_string
, *value_string
;
2588 const char *operator;
2593 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2594 &operator, operators
, ARRAY_SIZE(operators
),
2596 die_if_error(error
);
2597 if (!value_string
) {
2598 vsctl_fatal("%s: missing value", arg
);
2601 have_datum
= ovsdb_idl_read(row
, column
);
2603 union ovsdb_atom want_key
, want_value
;
2605 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2606 vsctl_fatal("cannot specify key to check for non-map column %s",
2610 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
2611 key_string
, symtab
));
2612 die_if_error(ovsdb_atom_from_string(&want_value
, &column
->type
.value
,
2613 value_string
, symtab
));
2615 idx
= ovsdb_datum_find_key(have_datum
,
2616 &want_key
, column
->type
.key
.type
);
2617 if (idx
!= UINT_MAX
) {
2618 cmp
= ovsdb_atom_compare_3way(&have_datum
->values
[idx
],
2620 column
->type
.value
.type
);
2623 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
2624 ovsdb_atom_destroy(&want_value
, column
->type
.value
.type
);
2626 struct ovsdb_datum want_datum
;
2628 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
2629 value_string
, symtab
));
2631 cmp
= ovsdb_datum_compare_3way(have_datum
, &want_datum
,
2633 ovsdb_datum_destroy(&want_datum
, &column
->type
);
2639 return (idx
== UINT_MAX
? false
2640 : !strcmp(operator, "=") ? cmp
== 0
2641 : !strcmp(operator, "!=") ? cmp
!= 0
2642 : !strcmp(operator, "<") ? cmp
< 0
2643 : !strcmp(operator, ">") ? cmp
> 0
2644 : !strcmp(operator, "<=") ? cmp
<= 0
2645 : !strcmp(operator, ">=") ? cmp
>= 0
2650 cmd_wait_until(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
);
2660 row
= get_row(ctx
, table
, record_id
);
2662 ctx
->try_again
= true;
2666 for (i
= 3; i
< ctx
->argc
; i
++) {
2667 if (!is_condition_satified(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
2668 ctx
->try_again
= true;
2674 static struct json
*
2675 where_uuid_equals(const struct uuid
*uuid
)
2678 json_array_create_1(
2679 json_array_create_3(
2680 json_string_create("_uuid"),
2681 json_string_create("=="),
2682 json_array_create_2(
2683 json_string_create("uuid"),
2684 json_string_create_nocopy(
2685 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
2689 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
2690 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2691 const struct ovsrec_open_vswitch
*ovs
,
2692 struct ovsdb_symbol_table
*symtab
)
2694 ctx
->argc
= command
->argc
;
2695 ctx
->argv
= command
->argv
;
2696 ctx
->options
= command
->options
;
2698 ds_swap(&ctx
->output
, &command
->output
);
2702 ctx
->symtab
= symtab
;
2704 ctx
->try_again
= false;
2708 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
2710 ds_swap(&ctx
->output
, &command
->output
);
2714 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
2715 struct ovsdb_idl
*idl
)
2717 struct ovsdb_idl_txn
*txn
;
2718 const struct ovsrec_open_vswitch
*ovs
;
2719 enum ovsdb_idl_txn_status status
;
2720 struct ovsdb_symbol_table
*symtab
;
2722 struct vsctl_command
*c
;
2723 int64_t next_cfg
= 0;
2726 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2728 ovsdb_idl_txn_set_dry_run(txn
);
2731 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2733 ovs
= ovsrec_open_vswitch_first(idl
);
2735 /* XXX add verification that table is empty */
2736 ovs
= ovsrec_open_vswitch_insert(txn
);
2739 if (wait_for_reload
) {
2740 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
2741 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
2742 json_destroy(where
);
2745 symtab
= ovsdb_symbol_table_create();
2746 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2747 ds_init(&c
->output
);
2749 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2750 struct vsctl_context ctx
;
2752 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2753 (c
->syntax
->run
)(&ctx
);
2754 vsctl_context_done(&ctx
, c
);
2756 if (ctx
.try_again
) {
2761 status
= ovsdb_idl_txn_commit_block(txn
);
2762 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2763 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2765 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2766 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2767 if (c
->syntax
->postprocess
) {
2768 struct vsctl_context ctx
;
2770 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2771 (c
->syntax
->postprocess
)(&ctx
);
2772 vsctl_context_done(&ctx
, c
);
2776 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2777 ovsdb_idl_txn_destroy(txn
);
2780 unused
= ovsdb_symbol_table_find_unused(symtab
);
2782 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2783 "with \"-- --id=%s create ...\")", unused
, unused
);
2787 case TXN_INCOMPLETE
:
2791 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2792 vsctl_fatal("transaction aborted");
2802 vsctl_fatal("transaction error: %s", error
);
2809 ovsdb_symbol_table_destroy(symtab
);
2811 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2812 struct ds
*ds
= &c
->output
;
2813 struct shash_node
*node
;
2819 for (j
= 0; j
< ds
->length
; j
++) {
2820 int ch
= ds
->string
[j
];
2823 fputs("\\n", stdout
);
2827 fputs("\\\\", stdout
);
2836 fputs(ds_cstr(ds
), stdout
);
2838 ds_destroy(&c
->output
);
2840 SHASH_FOR_EACH (node
, &c
->options
) {
2843 shash_destroy(&c
->options
);
2847 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2850 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2851 if (ovs
->cur_cfg
>= next_cfg
) {
2855 ovsdb_idl_wait(idl
);
2860 ovsdb_idl_destroy(idl
);
2865 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2866 * resources and return so that the caller can try again. */
2867 ovsdb_idl_txn_abort(txn
);
2868 ovsdb_idl_txn_destroy(txn
);
2869 ovsdb_symbol_table_destroy(symtab
);
2870 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2871 ds_destroy(&c
->output
);
2876 static const struct vsctl_command_syntax all_commands
[] = {
2877 /* Open vSwitch commands. */
2878 {"init", 0, 0, cmd_init
, NULL
, "", RW
},
2880 /* Bridge commands. */
2881 {"add-br", 1, 3, cmd_add_br
, NULL
, "--may-exist", RW
},
2882 {"del-br", 1, 1, cmd_del_br
, NULL
, "--if-exists", RW
},
2883 {"list-br", 0, 0, cmd_list_br
, NULL
, "", RO
},
2884 {"br-exists", 1, 1, cmd_br_exists
, NULL
, "", RO
},
2885 {"br-to-vlan", 1, 1, cmd_br_to_vlan
, NULL
, "", RO
},
2886 {"br-to-parent", 1, 1, cmd_br_to_parent
, NULL
, "", RO
},
2887 {"br-set-external-id", 2, 3, cmd_br_set_external_id
, NULL
, "", RW
},
2888 {"br-get-external-id", 1, 2, cmd_br_get_external_id
, NULL
, "", RO
},
2890 /* Port commands. */
2891 {"list-ports", 1, 1, cmd_list_ports
, NULL
, "", RO
},
2892 {"add-port", 2, INT_MAX
, cmd_add_port
, NULL
, "--may-exist", RW
},
2893 {"add-bond", 4, INT_MAX
, cmd_add_bond
, NULL
, "--may-exist,--fake-iface", RW
},
2894 {"del-port", 1, 2, cmd_del_port
, NULL
, "--if-exists,--with-iface", RW
},
2895 {"port-to-br", 1, 1, cmd_port_to_br
, NULL
, "", RO
},
2897 /* Interface commands. */
2898 {"list-ifaces", 1, 1, cmd_list_ifaces
, NULL
, "", RO
},
2899 {"iface-to-br", 1, 1, cmd_iface_to_br
, NULL
, "", RO
},
2901 /* Controller commands. */
2902 {"get-controller", 1, 1, cmd_get_controller
, NULL
, "", RO
},
2903 {"del-controller", 1, 1, cmd_del_controller
, NULL
, "", RW
},
2904 {"set-controller", 1, INT_MAX
, cmd_set_controller
, NULL
, "", RW
},
2905 {"get-fail-mode", 1, 1, cmd_get_fail_mode
, NULL
, "", RO
},
2906 {"del-fail-mode", 1, 1, cmd_del_fail_mode
, NULL
, "", RW
},
2907 {"set-fail-mode", 2, 2, cmd_set_fail_mode
, NULL
, "", RW
},
2910 {"get-ssl", 0, 0, cmd_get_ssl
, NULL
, "", RO
},
2911 {"del-ssl", 0, 0, cmd_del_ssl
, NULL
, "", RW
},
2912 {"set-ssl", 3, 3, cmd_set_ssl
, NULL
, "--bootstrap", RW
},
2914 /* Switch commands. */
2915 {"emer-reset", 0, 0, cmd_emer_reset
, NULL
, "", RW
},
2917 /* Parameter commands. */
2918 {"get", 2, INT_MAX
, cmd_get
, NULL
, "--if-exists,--id=", RO
},
2919 {"list", 1, INT_MAX
, cmd_list
, NULL
, "", RO
},
2920 {"set", 3, INT_MAX
, cmd_set
, NULL
, "", RW
},
2921 {"add", 4, INT_MAX
, cmd_add
, NULL
, "", RW
},
2922 {"remove", 4, INT_MAX
, cmd_remove
, NULL
, "", RW
},
2923 {"clear", 3, INT_MAX
, cmd_clear
, NULL
, "", RW
},
2924 {"create", 2, INT_MAX
, cmd_create
, post_create
, "--id=", RW
},
2925 {"destroy", 1, INT_MAX
, cmd_destroy
, NULL
, "--if-exists", RW
},
2926 {"wait-until", 2, INT_MAX
, cmd_wait_until
, NULL
, "", RO
},
2928 {NULL
, 0, 0, NULL
, NULL
, NULL
, RO
},