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 #define THIS_MODULE VLM_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
;
64 struct vsctl_command
{
65 /* Data that remains constant after initialization. */
66 const struct vsctl_command_syntax
*syntax
;
71 /* Data modified by commands. */
75 /* --db: The database server to contact. */
76 static const char *db
;
78 /* --oneline: Write each command's output as a single line? */
81 /* --dry-run: Do not commit any changes. */
84 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
85 static bool wait_for_reload
= true;
87 /* --timeout: Time to wait for a connection to 'db'. */
88 static int timeout
= 5;
90 /* All supported commands. */
91 static const struct vsctl_command_syntax all_commands
[];
93 /* The IDL we're using and the current transaction, if any.
94 * This is for use by vsctl_exit() only, to allow it to clean up.
95 * Other code should use its context arguments. */
96 static struct ovsdb_idl
*the_idl
;
97 static struct ovsdb_idl_txn
*the_idl_txn
;
99 static void vsctl_exit(int status
) NO_RETURN
;
100 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN
;
101 static char *default_db(void);
102 static void usage(void) NO_RETURN
;
103 static void parse_options(int argc
, char *argv
[]);
105 static struct vsctl_command
*parse_commands(int argc
, char *argv
[],
106 size_t *n_commandsp
);
107 static void parse_command(int argc
, char *argv
[], struct vsctl_command
*);
108 static void do_vsctl(const char *args
,
109 struct vsctl_command
*, size_t n_commands
,
112 static const struct vsctl_table_class
*get_table(const char *table_name
);
113 static void set_column(const struct vsctl_table_class
*,
114 const struct ovsdb_idl_row
*, const char *arg
,
115 struct ovsdb_symbol_table
*);
119 main(int argc
, char *argv
[])
121 struct ovsdb_idl
*idl
;
123 struct vsctl_command
*commands
;
128 set_program_name(argv
[0]);
129 signal(SIGPIPE
, SIG_IGN
);
132 vlog_set_levels(VLM_ANY_MODULE
, VLF_CONSOLE
, VLL_WARN
);
133 vlog_set_levels(VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
136 /* Log our arguments. This is often valuable for debugging systems. */
137 args
= process_escape_args(argv
);
138 VLOG_INFO("Called as %s", args
);
140 /* Parse command line. */
141 parse_options(argc
, argv
);
142 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
148 /* Now execute the commands. */
149 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
);
150 seqno
= ovsdb_idl_get_seqno(idl
);
153 unsigned int new_seqno
;
156 new_seqno
= ovsdb_idl_get_seqno(idl
);
157 if (new_seqno
!= seqno
) {
159 vsctl_fatal("too many database inconsistency failures");
161 do_vsctl(args
, commands
, n_commands
, idl
);
171 parse_options(int argc
, char *argv
[])
174 OPT_DB
= UCHAR_MAX
+ 1,
182 static struct option long_options
[] = {
183 {"db", required_argument
, 0, OPT_DB
},
184 {"no-syslog", no_argument
, 0, OPT_NO_SYSLOG
},
185 {"no-wait", no_argument
, 0, OPT_NO_WAIT
},
186 {"dry-run", no_argument
, 0, OPT_DRY_RUN
},
187 {"oneline", no_argument
, 0, OPT_ONELINE
},
188 {"timeout", required_argument
, 0, 't'},
189 {"help", no_argument
, 0, 'h'},
190 {"version", no_argument
, 0, 'V'},
193 STREAM_SSL_LONG_OPTIONS
194 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
203 c
= getopt_long(argc
, argv
, "+v::hVt:", long_options
, NULL
);
218 vlog_set_levels(VLM_vsctl
, VLF_SYSLOG
, VLL_WARN
);
222 wait_for_reload
= false;
233 OVS_PRINT_VERSION(0, 0);
237 timeout
= strtoul(optarg
, NULL
, 10);
239 vsctl_fatal("value %s on -t or --timeout is invalid",
247 STREAM_SSL_OPTION_HANDLERS
249 case OPT_PEER_CA_CERT
:
250 stream_ssl_set_peer_ca_cert_file(optarg
);
267 static struct vsctl_command
*
268 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
270 struct vsctl_command
*commands
;
271 size_t n_commands
, allocated_commands
;
275 n_commands
= allocated_commands
= 0;
277 for (start
= i
= 0; i
<= argc
; i
++) {
278 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
280 if (n_commands
>= allocated_commands
) {
281 struct vsctl_command
*c
;
283 commands
= x2nrealloc(commands
, &allocated_commands
,
285 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
286 shash_moved(&c
->options
);
289 parse_command(i
- start
, &argv
[start
],
290 &commands
[n_commands
++]);
296 vsctl_fatal("missing command name (use --help for help)");
298 *n_commandsp
= n_commands
;
303 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
305 const struct vsctl_command_syntax
*p
;
308 shash_init(&command
->options
);
309 for (i
= 0; i
< argc
; i
++) {
310 const char *option
= argv
[i
];
314 if (option
[0] != '-') {
318 equals
= strchr(option
, '=');
320 key
= xmemdup0(option
, equals
- option
);
321 value
= xstrdup(equals
+ 1);
323 key
= xstrdup(option
);
327 if (shash_find(&command
->options
, key
)) {
328 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
330 shash_add_nocopy(&command
->options
, key
, value
);
333 vsctl_fatal("missing command name");
336 for (p
= all_commands
; p
->name
; p
++) {
337 if (!strcmp(p
->name
, argv
[i
])) {
338 struct shash_node
*node
;
341 SHASH_FOR_EACH (node
, &command
->options
) {
342 const char *s
= strstr(p
->options
, node
->name
);
343 int end
= s
? s
[strlen(node
->name
)] : EOF
;
345 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
346 vsctl_fatal("'%s' command has no '%s' option",
347 argv
[i
], node
->name
);
349 if ((end
== '=') != (node
->data
!= NULL
)) {
351 vsctl_fatal("missing argument to '%s' option on '%s' "
352 "command", node
->name
, argv
[i
]);
354 vsctl_fatal("'%s' option on '%s' does not accept an "
355 "argument", node
->name
, argv
[i
]);
360 n_arg
= argc
- i
- 1;
361 if (n_arg
< p
->min_args
) {
362 vsctl_fatal("'%s' command requires at least %d arguments",
363 p
->name
, p
->min_args
);
364 } else if (n_arg
> p
->max_args
) {
367 for (j
= i
+ 1; j
< argc
; j
++) {
368 if (argv
[j
][0] == '-') {
369 vsctl_fatal("'%s' command takes at most %d arguments "
370 "(note that options must precede command "
371 "names and follow a \"--\" argument)",
372 p
->name
, p
->max_args
);
376 vsctl_fatal("'%s' command takes at most %d arguments",
377 p
->name
, p
->max_args
);
380 command
->argc
= n_arg
+ 1;
381 command
->argv
= &argv
[i
];
387 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
391 vsctl_fatal(const char *format
, ...)
396 va_start(args
, format
);
397 message
= xvasprintf(format
, args
);
400 vlog_set_levels(VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
401 VLOG_ERR("%s", message
);
402 ovs_error(0, "%s", message
);
403 vsctl_exit(EXIT_FAILURE
);
406 /* Frees the current transaction and the underlying IDL and then calls
409 * Freeing the transaction and the IDL is not strictly necessary, but it makes
410 * for a clean memory leak report from valgrind in the normal case. That makes
411 * it easier to notice real memory leaks. */
413 vsctl_exit(int status
)
416 ovsdb_idl_txn_abort(the_idl_txn
);
417 ovsdb_idl_txn_destroy(the_idl_txn
);
419 ovsdb_idl_destroy(the_idl
);
427 %s: ovs-vswitchd management utility\n\
428 usage: %s [OPTIONS] COMMAND [ARG...]\n\
431 add-br BRIDGE create a new bridge named BRIDGE\n\
432 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
433 del-br BRIDGE delete BRIDGE and all of its ports\n\
434 list-br print the names of all the bridges\n\
435 br-exists BRIDGE test whether BRIDGE exists\n\
436 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
437 br-to-parent BRIDGE print the parent of BRIDGE\n\
438 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
439 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
440 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
441 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
444 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
445 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
446 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
447 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
448 port-to-br PORT print name of bridge that contains PORT\n\
449 A bond is considered to be a single port.\n\
451 Interface commands (a bond consists of multiple interfaces):\n\
452 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
453 iface-to-br IFACE print name of bridge that contains IFACE\n\
455 Controller commands:\n\
456 get-controller [BRIDGE] print the controller for BRIDGE\n\
457 del-controller [BRIDGE] delete the controller for BRIDGE\n\
458 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
459 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
460 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
461 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
464 get-ssl print the SSL configuration\n\
465 del-ssl delete the SSL configuration\n\
466 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
469 emer-reset reset switch to known good state\n\
471 Database commands:\n\
472 list TBL [REC] list RECord (or all records) in TBL\n\
473 get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
474 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
475 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
476 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
477 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
478 create TBL COL[:KEY]=VALUE create and initialize new record\n\
479 destroy TBL REC delete REC from TBL\n\
480 Potentially unsafe database commands require --force option.\n\
483 --db=DATABASE connect to DATABASE\n\
485 --oneline print exactly one line of output per command\n",
486 program_name
, program_name
, default_db());
490 -h, --help display this help message\n\
491 -V, --version display version information\n");
500 def
= xasprintf("unix:%s/db.sock", ovs_rundir
);
505 struct vsctl_context
{
509 struct shash options
;
511 /* Modifiable state. */
513 struct ovsdb_idl
*idl
;
514 struct ovsdb_idl_txn
*txn
;
515 struct ovsdb_symbol_table
*symtab
;
516 const struct ovsrec_open_vswitch
*ovs
;
519 struct vsctl_bridge
{
520 struct ovsrec_bridge
*br_cfg
;
522 struct ovsrec_controller
**ctrl
;
524 struct vsctl_bridge
*parent
;
529 struct ovsrec_port
*port_cfg
;
530 struct vsctl_bridge
*bridge
;
534 struct ovsrec_interface
*iface_cfg
;
535 struct vsctl_port
*port
;
539 struct shash bridges
;
542 struct ovsrec_controller
**ctrl
;
547 vsctl_context_to_string(const struct vsctl_context
*ctx
)
549 const struct shash_node
*node
;
555 SHASH_FOR_EACH (node
, &ctx
->options
) {
556 svec_add(&words
, node
->name
);
558 for (i
= 0; i
< ctx
->argc
; i
++) {
559 svec_add(&words
, ctx
->argv
[i
]);
561 svec_terminate(&words
);
563 s
= process_escape_args(words
.names
);
565 svec_destroy(&words
);
570 static struct vsctl_bridge
*
571 add_bridge(struct vsctl_info
*b
,
572 struct ovsrec_bridge
*br_cfg
, const char *name
,
573 struct vsctl_bridge
*parent
, int vlan
)
575 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
577 br
->name
= xstrdup(name
);
581 br
->ctrl
= parent
->br_cfg
->controller
;
582 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
584 br
->ctrl
= br_cfg
->controller
;
585 br
->n_ctrl
= br_cfg
->n_controller
;
587 shash_add(&b
->bridges
, br
->name
, br
);
592 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
594 return (port_cfg
->fake_bridge
596 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095);
599 static struct vsctl_bridge
*
600 find_vlan_bridge(struct vsctl_info
*info
,
601 struct vsctl_bridge
*parent
, int vlan
)
603 struct shash_node
*node
;
605 SHASH_FOR_EACH (node
, &info
->bridges
) {
606 struct vsctl_bridge
*br
= node
->data
;
607 if (br
->parent
== parent
&& br
->vlan
== vlan
) {
616 free_info(struct vsctl_info
*info
)
618 struct shash_node
*node
;
620 SHASH_FOR_EACH (node
, &info
->bridges
) {
621 struct vsctl_bridge
*bridge
= node
->data
;
625 shash_destroy(&info
->bridges
);
627 SHASH_FOR_EACH (node
, &info
->ports
) {
628 struct vsctl_port
*port
= node
->data
;
631 shash_destroy(&info
->ports
);
633 SHASH_FOR_EACH (node
, &info
->ifaces
) {
634 struct vsctl_iface
*iface
= node
->data
;
637 shash_destroy(&info
->ifaces
);
641 get_info(const struct ovsrec_open_vswitch
*ovs
, struct vsctl_info
*info
)
643 struct shash bridges
, ports
;
646 shash_init(&info
->bridges
);
647 shash_init(&info
->ports
);
648 shash_init(&info
->ifaces
);
650 info
->ctrl
= ovs
->controller
;
651 info
->n_ctrl
= ovs
->n_controller
;
653 shash_init(&bridges
);
655 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
656 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
657 struct vsctl_bridge
*br
;
660 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
661 VLOG_WARN("%s: database contains duplicate bridge name",
665 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
670 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
671 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
673 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
674 VLOG_WARN("%s: database contains duplicate port name",
679 if (port_is_fake_bridge(port_cfg
)
680 && shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
681 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
685 shash_destroy(&bridges
);
686 shash_destroy(&ports
);
688 shash_init(&bridges
);
690 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
691 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
692 struct vsctl_bridge
*br
;
695 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
698 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
699 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
700 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
701 struct vsctl_port
*port
;
704 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
708 if (port_is_fake_bridge(port_cfg
)
709 && !shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
713 port
= xmalloc(sizeof *port
);
714 port
->port_cfg
= port_cfg
;
716 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
717 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
724 shash_add(&info
->ports
, port_cfg
->name
, port
);
726 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
727 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
728 struct vsctl_iface
*iface
;
730 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
731 VLOG_WARN("%s: database contains duplicate interface name",
736 iface
= xmalloc(sizeof *iface
);
737 iface
->iface_cfg
= iface_cfg
;
739 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
743 shash_destroy(&bridges
);
744 shash_destroy(&ports
);
748 check_conflicts(struct vsctl_info
*info
, const char *name
,
751 struct vsctl_iface
*iface
;
752 struct vsctl_port
*port
;
754 if (shash_find(&info
->bridges
, name
)) {
755 vsctl_fatal("%s because a bridge named %s already exists",
759 port
= shash_find_data(&info
->ports
, name
);
761 vsctl_fatal("%s because a port named %s already exists on "
762 "bridge %s", msg
, name
, port
->bridge
->name
);
765 iface
= shash_find_data(&info
->ifaces
, name
);
767 vsctl_fatal("%s because an interface named %s already exists "
768 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
774 static struct vsctl_bridge
*
775 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
777 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
778 if (must_exist
&& !br
) {
779 vsctl_fatal("no bridge named %s", name
);
784 static struct vsctl_bridge
*
785 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
787 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
788 if (br
&& br
->parent
) {
789 vsctl_fatal("%s is a fake bridge", name
);
794 static struct vsctl_port
*
795 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
797 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
798 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
801 if (must_exist
&& !port
) {
802 vsctl_fatal("no port named %s", name
);
807 static struct vsctl_iface
*
808 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
810 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
811 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
814 if (must_exist
&& !iface
) {
815 vsctl_fatal("no interface named %s", name
);
821 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
823 struct ovsrec_port
**ports
;
826 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
827 for (i
= 0; i
< br
->n_ports
; i
++) {
828 ports
[i
] = br
->ports
[i
];
830 ports
[br
->n_ports
] = port
;
831 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
836 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
838 struct ovsrec_port
**ports
;
841 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
842 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
843 if (br
->ports
[i
] != port
) {
844 ports
[n
++] = br
->ports
[i
];
847 ovsrec_bridge_set_ports(br
, ports
, n
);
852 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
853 struct ovsrec_bridge
*bridge
)
855 struct ovsrec_bridge
**bridges
;
858 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
859 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
860 bridges
[i
] = ovs
->bridges
[i
];
862 bridges
[ovs
->n_bridges
] = bridge
;
863 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
868 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
869 struct ovsrec_bridge
*bridge
)
871 struct ovsrec_bridge
**bridges
;
874 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
875 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
876 if (ovs
->bridges
[i
] != bridge
) {
877 bridges
[n
++] = ovs
->bridges
[i
];
880 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
885 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
890 cmd_emer_reset(struct vsctl_context
*ctx
)
892 const struct ovsdb_idl
*idl
= ctx
->idl
;
893 const struct ovsrec_bridge
*br
;
894 const struct ovsrec_port
*port
;
895 const struct ovsrec_interface
*iface
;
896 const struct ovsrec_mirror
*mirror
, *next_mirror
;
897 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
898 const struct ovsrec_netflow
*nf
, *next_nf
;
899 const struct ovsrec_ssl
*ssl
, *next_ssl
;
900 const struct ovsrec_sflow
*sflow
, *next_sflow
;
903 /* Reset the Open_vSwitch table. */
904 ovsrec_open_vswitch_set_managers(ctx
->ovs
, NULL
, 0);
905 ovsrec_open_vswitch_set_controller(ctx
->ovs
, NULL
, 0);
906 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
908 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
910 char *hw_key
= "hwaddr";
913 ovsrec_bridge_set_controller(br
, NULL
, 0);
914 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
915 ovsrec_bridge_set_netflow(br
, NULL
);
916 ovsrec_bridge_set_sflow(br
, NULL
);
917 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
919 /* We only want to save the "hwaddr" key from other_config. */
920 for (i
=0; i
< br
->n_other_config
; i
++) {
921 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
922 hw_val
= br
->value_other_config
[i
];
927 char *val
= xstrdup(hw_val
);
928 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
931 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
935 OVSREC_PORT_FOR_EACH (port
, idl
) {
936 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
939 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
940 /* xxx What do we do about gre/patch devices created by mgr? */
942 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
943 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
946 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
947 ovsrec_mirror_delete(mirror
);
950 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
951 ovsrec_controller_delete(ctrl
);
954 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
955 ovsrec_netflow_delete(nf
);
958 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
959 ovsrec_ssl_delete(ssl
);
962 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
963 ovsrec_sflow_delete(sflow
);
968 cmd_add_br(struct vsctl_context
*ctx
)
970 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
971 const char *br_name
, *parent_name
;
972 struct vsctl_info info
;
975 br_name
= ctx
->argv
[1];
976 if (ctx
->argc
== 2) {
979 } else if (ctx
->argc
== 4) {
980 parent_name
= ctx
->argv
[2];
981 vlan
= atoi(ctx
->argv
[3]);
982 if (vlan
< 1 || vlan
> 4095) {
983 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
986 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
990 get_info(ctx
->ovs
, &info
);
992 struct vsctl_bridge
*br
;
994 br
= find_bridge(&info
, br_name
, false);
998 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
999 "a VLAN bridge for VLAN %d",
1000 br_name
, br_name
, br
->vlan
);
1004 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1005 "is not a VLAN bridge",
1006 br_name
, parent_name
, vlan
, br_name
);
1007 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1008 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1009 "has the wrong parent %s",
1010 br_name
, parent_name
, vlan
,
1011 br_name
, br
->parent
->name
);
1012 } else if (br
->vlan
!= vlan
) {
1013 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1014 "is a VLAN bridge for the wrong VLAN %d",
1015 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1021 check_conflicts(&info
, br_name
,
1022 xasprintf("cannot create a bridge named %s", br_name
));
1025 struct ovsrec_port
*port
;
1026 struct ovsrec_interface
*iface
;
1027 struct ovsrec_bridge
*br
;
1029 iface
= ovsrec_interface_insert(ctx
->txn
);
1030 ovsrec_interface_set_name(iface
, br_name
);
1032 port
= ovsrec_port_insert(ctx
->txn
);
1033 ovsrec_port_set_name(port
, br_name
);
1034 ovsrec_port_set_interfaces(port
, &iface
, 1);
1036 br
= ovsrec_bridge_insert(ctx
->txn
);
1037 ovsrec_bridge_set_name(br
, br_name
);
1038 ovsrec_bridge_set_ports(br
, &port
, 1);
1040 ovs_insert_bridge(ctx
->ovs
, br
);
1042 struct vsctl_bridge
*parent
;
1043 struct ovsrec_port
*port
;
1044 struct ovsrec_interface
*iface
;
1045 struct ovsrec_bridge
*br
;
1048 parent
= find_bridge(&info
, parent_name
, false);
1049 if (parent
&& parent
->vlan
) {
1050 vsctl_fatal("cannot create bridge with fake bridge as parent");
1053 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1055 br
= parent
->br_cfg
;
1057 iface
= ovsrec_interface_insert(ctx
->txn
);
1058 ovsrec_interface_set_name(iface
, br_name
);
1059 ovsrec_interface_set_type(iface
, "internal");
1061 port
= ovsrec_port_insert(ctx
->txn
);
1062 ovsrec_port_set_name(port
, br_name
);
1063 ovsrec_port_set_interfaces(port
, &iface
, 1);
1064 ovsrec_port_set_fake_bridge(port
, true);
1065 ovsrec_port_set_tag(port
, &tag
, 1);
1067 bridge_insert_port(br
, port
);
1074 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1076 struct shash_node
*node
;
1078 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1079 struct vsctl_iface
*iface
= node
->data
;
1080 if (iface
->port
== port
) {
1081 ovsrec_interface_delete(iface
->iface_cfg
);
1084 ovsrec_port_delete(port
->port_cfg
);
1086 bridge_delete_port((port
->bridge
->parent
1087 ? port
->bridge
->parent
->br_cfg
1088 : port
->bridge
->br_cfg
), port
->port_cfg
);
1092 cmd_del_br(struct vsctl_context
*ctx
)
1094 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1095 struct vsctl_bridge
*bridge
;
1096 struct vsctl_info info
;
1098 get_info(ctx
->ovs
, &info
);
1099 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1101 struct shash_node
*node
;
1103 SHASH_FOR_EACH (node
, &info
.ports
) {
1104 struct vsctl_port
*port
= node
->data
;
1105 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1106 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1107 del_port(&info
, port
);
1110 if (bridge
->br_cfg
) {
1111 ovsrec_bridge_delete(bridge
->br_cfg
);
1112 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1119 output_sorted(struct svec
*svec
, struct ds
*output
)
1125 SVEC_FOR_EACH (i
, name
, svec
) {
1126 ds_put_format(output
, "%s\n", name
);
1131 cmd_list_br(struct vsctl_context
*ctx
)
1133 struct shash_node
*node
;
1134 struct vsctl_info info
;
1135 struct svec bridges
;
1137 get_info(ctx
->ovs
, &info
);
1139 svec_init(&bridges
);
1140 SHASH_FOR_EACH (node
, &info
.bridges
) {
1141 struct vsctl_bridge
*br
= node
->data
;
1142 svec_add(&bridges
, br
->name
);
1144 output_sorted(&bridges
, &ctx
->output
);
1145 svec_destroy(&bridges
);
1151 cmd_br_exists(struct vsctl_context
*ctx
)
1153 struct vsctl_info info
;
1155 get_info(ctx
->ovs
, &info
);
1156 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1162 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1163 * equals 'a', false otherwise. */
1165 key_matches(const char *a
,
1166 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1168 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1172 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1173 char *key
, char *value
,
1174 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1181 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1182 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1184 for (i
= 0; i
< old_n
; i
++) {
1185 if (strcmp(key
, old_keys
[i
])) {
1186 new_keys
[new_n
] = old_keys
[i
];
1187 new_values
[new_n
] = old_values
[i
];
1192 new_keys
[new_n
] = key
;
1193 new_values
[new_n
] = value
;
1196 *new_keysp
= new_keys
;
1197 *new_valuesp
= new_values
;
1202 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1204 struct vsctl_info info
;
1205 struct vsctl_bridge
*bridge
;
1206 char **keys
, **values
;
1209 get_info(ctx
->ovs
, &info
);
1210 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1211 if (bridge
->br_cfg
) {
1212 set_external_id(bridge
->br_cfg
->key_external_ids
,
1213 bridge
->br_cfg
->value_external_ids
,
1214 bridge
->br_cfg
->n_external_ids
,
1215 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1216 &keys
, &values
, &n
);
1217 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1219 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1220 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1221 set_external_id(port
->port_cfg
->key_external_ids
,
1222 port
->port_cfg
->value_external_ids
,
1223 port
->port_cfg
->n_external_ids
,
1224 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1225 &keys
, &values
, &n
);
1226 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1236 get_external_id(char **keys
, char **values
, size_t n
,
1237 const char *prefix
, const char *key
,
1240 size_t prefix_len
= strlen(prefix
);
1245 for (i
= 0; i
< n
; i
++) {
1246 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1247 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1248 keys
[i
] + prefix_len
, values
[i
]));
1249 } else if (key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1250 svec_add(&svec
, values
[i
]);
1254 output_sorted(&svec
, output
);
1255 svec_destroy(&svec
);
1259 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1261 struct vsctl_info info
;
1262 struct vsctl_bridge
*bridge
;
1264 get_info(ctx
->ovs
, &info
);
1265 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1266 if (bridge
->br_cfg
) {
1267 get_external_id(bridge
->br_cfg
->key_external_ids
,
1268 bridge
->br_cfg
->value_external_ids
,
1269 bridge
->br_cfg
->n_external_ids
,
1270 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1273 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1274 get_external_id(port
->port_cfg
->key_external_ids
,
1275 port
->port_cfg
->value_external_ids
,
1276 port
->port_cfg
->n_external_ids
,
1277 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1284 cmd_list_ports(struct vsctl_context
*ctx
)
1286 struct vsctl_bridge
*br
;
1287 struct shash_node
*node
;
1288 struct vsctl_info info
;
1291 get_info(ctx
->ovs
, &info
);
1292 br
= find_bridge(&info
, ctx
->argv
[1], true);
1295 SHASH_FOR_EACH (node
, &info
.ports
) {
1296 struct vsctl_port
*port
= node
->data
;
1298 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1299 svec_add(&ports
, port
->port_cfg
->name
);
1302 output_sorted(&ports
, &ctx
->output
);
1303 svec_destroy(&ports
);
1309 add_port(struct vsctl_context
*ctx
,
1310 const char *br_name
, const char *port_name
,
1311 bool may_exist
, bool fake_iface
,
1312 char *iface_names
[], int n_ifaces
,
1313 char *settings
[], int n_settings
)
1315 struct vsctl_info info
;
1316 struct vsctl_bridge
*bridge
;
1317 struct ovsrec_interface
**ifaces
;
1318 struct ovsrec_port
*port
;
1321 get_info(ctx
->ovs
, &info
);
1323 struct vsctl_port
*port
;
1325 port
= find_port(&info
, port_name
, false);
1327 struct svec want_names
, have_names
;
1330 svec_init(&want_names
);
1331 for (i
= 0; i
< n_ifaces
; i
++) {
1332 svec_add(&want_names
, iface_names
[i
]);
1334 svec_sort(&want_names
);
1336 svec_init(&have_names
);
1337 for (i
= 0; i
< port
->port_cfg
->n_interfaces
; i
++) {
1338 svec_add(&have_names
, port
->port_cfg
->interfaces
[i
]->name
);
1340 svec_sort(&have_names
);
1342 if (strcmp(port
->bridge
->name
, br_name
)) {
1343 char *command
= vsctl_context_to_string(ctx
);
1344 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1345 command
, port_name
, port
->bridge
->name
);
1348 if (!svec_equal(&want_names
, &have_names
)) {
1349 char *have_names_string
= svec_join(&have_names
, ", ", "");
1350 char *command
= vsctl_context_to_string(ctx
);
1352 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1353 command
, port_name
, have_names_string
);
1356 svec_destroy(&want_names
);
1357 svec_destroy(&have_names
);
1362 check_conflicts(&info
, port_name
,
1363 xasprintf("cannot create a port named %s", port_name
));
1364 for (i
= 0; i
< n_ifaces
; i
++) {
1365 check_conflicts(&info
, iface_names
[i
],
1366 xasprintf("cannot create an interface named %s",
1369 bridge
= find_bridge(&info
, br_name
, true);
1371 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1372 for (i
= 0; i
< n_ifaces
; i
++) {
1373 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1374 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1377 port
= ovsrec_port_insert(ctx
->txn
);
1378 ovsrec_port_set_name(port
, port_name
);
1379 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1380 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1384 int64_t tag
= bridge
->vlan
;
1385 ovsrec_port_set_tag(port
, &tag
, 1);
1388 for (i
= 0; i
< n_settings
; i
++) {
1389 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1393 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1394 : bridge
->br_cfg
), port
);
1400 cmd_add_port(struct vsctl_context
*ctx
)
1402 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1404 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1405 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1409 cmd_add_bond(struct vsctl_context
*ctx
)
1411 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1412 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1416 n_ifaces
= ctx
->argc
- 3;
1417 for (i
= 3; i
< ctx
->argc
; i
++) {
1418 if (strchr(ctx
->argv
[i
], '=')) {
1424 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1425 "%d were specified", n_ifaces
);
1428 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1429 &ctx
->argv
[3], n_ifaces
,
1430 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1434 cmd_del_port(struct vsctl_context
*ctx
)
1436 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1437 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1438 struct vsctl_port
*port
;
1439 struct vsctl_info info
;
1441 get_info(ctx
->ovs
, &info
);
1443 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1445 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1446 struct vsctl_iface
*iface
;
1448 port
= find_port(&info
, target
, false);
1450 iface
= find_iface(&info
, target
, false);
1455 if (must_exist
&& !port
) {
1456 vsctl_fatal("no port or interface named %s", target
);
1461 if (ctx
->argc
== 3) {
1462 struct vsctl_bridge
*bridge
;
1464 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1465 if (port
->bridge
!= bridge
) {
1466 if (port
->bridge
->parent
== bridge
) {
1467 vsctl_fatal("bridge %s does not have a port %s (although "
1468 "its parent bridge %s does)",
1469 ctx
->argv
[1], ctx
->argv
[2],
1470 bridge
->parent
->name
);
1472 vsctl_fatal("bridge %s does not have a port %s",
1473 ctx
->argv
[1], ctx
->argv
[2]);
1478 del_port(&info
, port
);
1485 cmd_port_to_br(struct vsctl_context
*ctx
)
1487 struct vsctl_port
*port
;
1488 struct vsctl_info info
;
1490 get_info(ctx
->ovs
, &info
);
1491 port
= find_port(&info
, ctx
->argv
[1], true);
1492 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1497 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1499 struct vsctl_bridge
*bridge
;
1500 struct vsctl_info info
;
1502 get_info(ctx
->ovs
, &info
);
1503 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1504 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1509 cmd_br_to_parent(struct vsctl_context
*ctx
)
1511 struct vsctl_bridge
*bridge
;
1512 struct vsctl_info info
;
1514 get_info(ctx
->ovs
, &info
);
1515 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1516 if (bridge
->parent
) {
1517 bridge
= bridge
->parent
;
1519 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1524 cmd_list_ifaces(struct vsctl_context
*ctx
)
1526 struct vsctl_bridge
*br
;
1527 struct shash_node
*node
;
1528 struct vsctl_info info
;
1531 get_info(ctx
->ovs
, &info
);
1532 br
= find_bridge(&info
, ctx
->argv
[1], true);
1535 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1536 struct vsctl_iface
*iface
= node
->data
;
1538 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1539 && br
== iface
->port
->bridge
) {
1540 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1543 output_sorted(&ifaces
, &ctx
->output
);
1544 svec_destroy(&ifaces
);
1550 cmd_iface_to_br(struct vsctl_context
*ctx
)
1552 struct vsctl_iface
*iface
;
1553 struct vsctl_info info
;
1555 get_info(ctx
->ovs
, &info
);
1556 iface
= find_iface(&info
, ctx
->argv
[1], true);
1557 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1561 /* Print targets of the 'n_controllers' in 'controllers' on the output for
1564 print_controllers(struct vsctl_context
*ctx
,
1565 struct ovsrec_controller
**controllers
,
1566 size_t n_controllers
)
1568 /* Print the targets in sorted order for reproducibility. */
1569 struct svec targets
;
1572 svec_init(&targets
);
1573 for (i
= 0; i
< n_controllers
; i
++) {
1574 svec_add(&targets
, controllers
[i
]->target
);
1577 svec_sort(&targets
);
1578 for (i
= 0; i
< targets
.n
; i
++) {
1579 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1581 svec_destroy(&targets
);
1585 cmd_get_controller(struct vsctl_context
*ctx
)
1587 struct vsctl_info info
;
1589 get_info(ctx
->ovs
, &info
);
1591 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1592 print_controllers(ctx
, info
.ctrl
, info
.n_ctrl
);
1594 struct vsctl_bridge
*br
= find_bridge(&info
, ctx
->argv
[1], true);
1596 print_controllers(ctx
, br
->ctrl
, br
->n_ctrl
);
1598 print_controllers(ctx
, info
.ctrl
, info
.n_ctrl
);
1606 delete_controllers(struct ovsrec_controller
**controllers
,
1607 size_t n_controllers
)
1611 for (i
= 0; i
< n_controllers
; i
++) {
1612 ovsrec_controller_delete(controllers
[i
]);
1617 cmd_del_controller(struct vsctl_context
*ctx
)
1619 struct vsctl_info info
;
1621 get_info(ctx
->ovs
, &info
);
1623 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1625 delete_controllers(info
.ctrl
, info
.n_ctrl
);
1626 ovsrec_open_vswitch_set_controller(ctx
->ovs
, NULL
, 0);
1629 struct vsctl_bridge
*br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1631 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1632 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1639 static struct ovsrec_controller
**
1640 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1642 struct ovsrec_controller
**controllers
;
1645 controllers
= xmalloc(n
* sizeof *controllers
);
1646 for (i
= 0; i
< n
; i
++) {
1647 controllers
[i
] = ovsrec_controller_insert(txn
);
1648 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1655 set_default_controllers(struct vsctl_context
*ctx
, char *targets
[], size_t n
)
1657 struct ovsrec_controller
**controllers
;
1659 delete_controllers(ctx
->ovs
->controller
, ctx
->ovs
->n_controller
);
1661 controllers
= insert_controllers(ctx
->txn
, targets
, n
);
1662 ovsrec_open_vswitch_set_controller(ctx
->ovs
, controllers
, n
);
1667 cmd_set_controller(struct vsctl_context
*ctx
)
1669 struct vsctl_info info
;
1671 get_info(ctx
->ovs
, &info
);
1673 if (ctx
->argc
== 2) {
1674 /* Set one controller in the "Open_vSwitch" table. */
1675 set_default_controllers(ctx
, &ctx
->argv
[1], 1);
1676 } else if (!strcmp(ctx
->argv
[1], "default")) {
1677 /* Set one or more controllers in the "Open_vSwitch" table. */
1678 set_default_controllers(ctx
, &ctx
->argv
[2], ctx
->argc
- 2);
1680 /* Set one or more controllers for a particular bridge. */
1681 struct vsctl_bridge
*br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1682 struct ovsrec_controller
**controllers
;
1685 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1688 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1689 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1697 get_fail_mode(struct ovsrec_controller
**controllers
, size_t n_controllers
)
1699 const char *fail_mode
;
1703 for (i
= 0; i
< n_controllers
; i
++) {
1704 const char *s
= controllers
[i
]->fail_mode
;
1706 if (!strcmp(s
, "secure")) {
1718 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1720 struct vsctl_info info
;
1721 const char *fail_mode
= NULL
;
1723 get_info(ctx
->ovs
, &info
);
1725 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1726 /* Return the fail-mode from the "Open_vSwitch" table */
1727 fail_mode
= get_fail_mode(info
.ctrl
, info
.n_ctrl
);
1729 /* Return the fail-mode for a particular bridge. */
1730 struct vsctl_bridge
*br
= find_bridge(&info
, ctx
->argv
[1], true);
1732 /* If no controller is defined for the requested bridge, fallback to
1733 * the "Open_vSwitch" table's controller. */
1734 fail_mode
= (br
->n_ctrl
1735 ? get_fail_mode(br
->ctrl
, br
->n_ctrl
)
1736 : get_fail_mode(info
.ctrl
, info
.n_ctrl
));
1739 if (fail_mode
&& strlen(fail_mode
)) {
1740 ds_put_format(&ctx
->output
, "%s\n", fail_mode
);
1747 set_fail_mode(struct ovsrec_controller
**controllers
, size_t n_controllers
,
1748 const char *fail_mode
)
1752 for (i
= 0; i
< n_controllers
; i
++) {
1753 ovsrec_controller_set_fail_mode(controllers
[i
], fail_mode
);
1758 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1760 struct vsctl_info info
;
1762 get_info(ctx
->ovs
, &info
);
1764 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1765 set_fail_mode(info
.ctrl
, info
.n_ctrl
, NULL
);
1767 struct vsctl_bridge
*br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1769 set_fail_mode(br
->ctrl
, br
->n_ctrl
, NULL
);
1776 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1778 struct vsctl_info info
;
1780 const char *fail_mode
;
1782 get_info(ctx
->ovs
, &info
);
1784 if (ctx
->argc
== 2) {
1786 fail_mode
= ctx
->argv
[1];
1788 bridge
= ctx
->argv
[1];
1789 fail_mode
= ctx
->argv
[2];
1792 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1793 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1796 if (!strcmp(bridge
, "default")) {
1797 /* Set the fail-mode in the "Open_vSwitch" table. */
1799 vsctl_fatal("no controller declared");
1801 set_fail_mode(info
.ctrl
, info
.n_ctrl
, fail_mode
);
1803 struct vsctl_bridge
*br
= find_real_bridge(&info
, bridge
, true);
1806 vsctl_fatal("no controller declared for %s", br
->name
);
1808 set_fail_mode(br
->ctrl
, br
->n_ctrl
, fail_mode
);
1815 cmd_get_ssl(struct vsctl_context
*ctx
)
1817 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1820 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
1821 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
1822 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
1823 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
1824 ssl
->bootstrap_ca_cert
? "true" : "false");
1829 cmd_del_ssl(struct vsctl_context
*ctx
)
1831 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1834 ovsrec_ssl_delete(ssl
);
1835 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1840 cmd_set_ssl(struct vsctl_context
*ctx
)
1842 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
1843 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1846 ovsrec_ssl_delete(ssl
);
1848 ssl
= ovsrec_ssl_insert(ctx
->txn
);
1850 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
1851 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
1852 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
1854 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
1856 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
1859 /* Parameter commands. */
1861 struct vsctl_row_id
{
1862 const struct ovsdb_idl_table_class
*table
;
1863 const struct ovsdb_idl_column
*name_column
;
1864 const struct ovsdb_idl_column
*uuid_column
;
1867 struct vsctl_table_class
{
1868 struct ovsdb_idl_table_class
*class;
1869 struct vsctl_row_id row_ids
[2];
1872 static const struct vsctl_table_class tables
[] = {
1873 {&ovsrec_table_bridge
,
1874 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
1875 {NULL
, NULL
, NULL
}}},
1877 {&ovsrec_table_controller
,
1878 {{&ovsrec_table_bridge
,
1879 &ovsrec_bridge_col_name
,
1880 &ovsrec_bridge_col_controller
},
1881 {&ovsrec_table_open_vswitch
,
1883 &ovsrec_open_vswitch_col_controller
}}},
1885 {&ovsrec_table_interface
,
1886 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
1887 {NULL
, NULL
, NULL
}}},
1889 {&ovsrec_table_mirror
,
1890 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
1891 {NULL
, NULL
, NULL
}}},
1893 {&ovsrec_table_netflow
,
1894 {{&ovsrec_table_bridge
,
1895 &ovsrec_bridge_col_name
,
1896 &ovsrec_bridge_col_netflow
},
1897 {NULL
, NULL
, NULL
}}},
1899 {&ovsrec_table_open_vswitch
,
1900 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
1901 {NULL
, NULL
, NULL
}}},
1903 {&ovsrec_table_port
,
1904 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
1905 {NULL
, NULL
, NULL
}}},
1908 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
1909 {NULL
, NULL
, NULL
}}},
1911 {&ovsrec_table_queue
,
1912 {{NULL
, NULL
, NULL
},
1913 {NULL
, NULL
, NULL
}}},
1916 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
1918 {&ovsrec_table_sflow
,
1919 {{&ovsrec_table_bridge
,
1920 &ovsrec_bridge_col_name
,
1921 &ovsrec_bridge_col_sflow
},
1922 {NULL
, NULL
, NULL
}}},
1924 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
1928 die_if_error(char *error
)
1931 vsctl_fatal("%s", error
);
1936 to_lower_and_underscores(unsigned c
)
1938 return c
== '-' ? '_' : tolower(c
);
1942 score_partial_match(const char *name
, const char *s
)
1946 if (!strcmp(name
, s
)) {
1949 for (score
= 0; ; score
++, name
++, s
++) {
1950 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
1952 } else if (*name
== '\0') {
1953 return UINT_MAX
- 1;
1956 return *s
== '\0' ? score
: 0;
1959 static const struct vsctl_table_class
*
1960 get_table(const char *table_name
)
1962 const struct vsctl_table_class
*table
;
1963 const struct vsctl_table_class
*best_match
= NULL
;
1964 unsigned int best_score
= 0;
1966 for (table
= tables
; table
->class; table
++) {
1967 unsigned int score
= score_partial_match(table
->class->name
,
1969 if (score
> best_score
) {
1972 } else if (score
== best_score
) {
1978 } else if (best_score
) {
1979 vsctl_fatal("multiple table names match \"%s\"", table_name
);
1981 vsctl_fatal("unknown table \"%s\"", table_name
);
1985 static const struct ovsdb_idl_row
*
1986 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
1987 const struct vsctl_row_id
*id
, const char *record_id
)
1989 const struct ovsdb_idl_row
*referrer
, *final
;
1995 if (!id
->name_column
) {
1996 if (strcmp(record_id
, ".")) {
1999 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2000 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
2004 const struct ovsdb_idl_row
*row
;
2005 unsigned int best_score
= 0;
2007 /* It might make sense to relax this assertion. */
2008 assert(id
->name_column
->type
.key
.type
== OVSDB_TYPE_STRING
);
2011 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2012 row
!= NULL
&& best_score
!= UINT_MAX
;
2013 row
= ovsdb_idl_next_row(row
))
2015 struct ovsdb_datum name
;
2017 ovsdb_idl_txn_read(row
, id
->name_column
, &name
);
2019 unsigned int score
= score_partial_match(name
.keys
[0].string
,
2021 if (score
> best_score
) {
2024 } else if (score
== best_score
) {
2028 ovsdb_datum_destroy(&name
, &id
->name_column
->type
);
2030 if (best_score
&& !referrer
) {
2031 vsctl_fatal("multiple rows in %s match \"%s\"",
2032 table
->class->name
, record_id
);
2040 if (id
->uuid_column
) {
2041 struct ovsdb_datum uuid
;
2043 assert(id
->uuid_column
->type
.key
.type
== OVSDB_TYPE_UUID
);
2044 assert(id
->uuid_column
->type
.value
.type
== OVSDB_TYPE_VOID
);
2046 ovsdb_idl_txn_read(referrer
, id
->uuid_column
, &uuid
);
2048 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
2049 &uuid
.keys
[0].uuid
);
2051 ovsdb_datum_destroy(&uuid
, &id
->uuid_column
->type
);
2059 static const struct ovsdb_idl_row
*
2060 get_row(struct vsctl_context
*ctx
,
2061 const struct vsctl_table_class
*table
, const char *record_id
)
2063 const struct ovsdb_idl_row
*row
;
2066 if (uuid_from_string(&uuid
, record_id
)) {
2067 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
2071 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
2072 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
2081 static const struct ovsdb_idl_row
*
2082 must_get_row(struct vsctl_context
*ctx
,
2083 const struct vsctl_table_class
*table
, const char *record_id
)
2085 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
2087 vsctl_fatal("no row \"%s\" in table %s",
2088 record_id
, table
->class->name
);
2094 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
2095 const struct ovsdb_idl_column
**columnp
)
2097 const struct ovsdb_idl_column
*best_match
= NULL
;
2098 unsigned int best_score
= 0;
2101 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2102 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2103 unsigned int score
= score_partial_match(column
->name
, column_name
);
2104 if (score
> best_score
) {
2105 best_match
= column
;
2107 } else if (score
== best_score
) {
2112 *columnp
= best_match
;
2115 } else if (best_score
) {
2116 return xasprintf("%s contains more than one column whose name "
2117 "matches \"%s\"", table
->class->name
, column_name
);
2119 return xasprintf("%s does not contain a column whose name matches "
2120 "\"%s\"", table
->class->name
, column_name
);
2124 static char * WARN_UNUSED_RESULT
2125 parse_column_key_value(const char *arg
, const struct vsctl_table_class
*table
,
2126 const struct ovsdb_idl_column
**columnp
,
2127 char **keyp
, char **valuep
)
2129 const char *p
= arg
;
2132 assert(columnp
|| keyp
);
2140 /* Parse column name. */
2144 error
= ovsdb_token_parse(&p
, &column_name
);
2148 if (column_name
[0] == '\0') {
2150 error
= xasprintf("%s: missing column name", arg
);
2153 error
= get_column(table
, column_name
, columnp
);
2160 /* Parse key string. */
2161 if (*p
== ':' || !columnp
) {
2165 error
= xasprintf("%s: key not accepted here", arg
);
2168 error
= ovsdb_token_parse(&p
, keyp
);
2176 /* Parse value string. */
2179 error
= xasprintf("%s: value not accepted here", arg
);
2182 *valuep
= xstrdup(p
+ 1);
2188 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2211 cmd_get(struct vsctl_context
*ctx
)
2213 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2214 const char *table_name
= ctx
->argv
[1];
2215 const char *record_id
= ctx
->argv
[2];
2216 const struct vsctl_table_class
*table
;
2217 const struct ovsdb_idl_row
*row
;
2218 struct ds
*out
= &ctx
->output
;
2221 table
= get_table(table_name
);
2222 row
= must_get_row(ctx
, table
, record_id
);
2223 for (i
= 3; i
< ctx
->argc
; i
++) {
2224 const struct ovsdb_idl_column
*column
;
2225 struct ovsdb_datum datum
;
2228 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2229 &column
, &key_string
, NULL
));
2231 ovsdb_idl_txn_read(row
, column
, &datum
);
2233 union ovsdb_atom key
;
2236 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2237 vsctl_fatal("cannot specify key to get for non-map column %s",
2241 die_if_error(ovsdb_atom_from_string(&key
,
2243 key_string
, ctx
->symtab
));
2245 idx
= ovsdb_datum_find_key(&datum
, &key
,
2246 column
->type
.key
.type
);
2247 if (idx
== UINT_MAX
) {
2249 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2250 key_string
, table
->class->name
, record_id
,
2254 ovsdb_atom_to_string(&datum
.values
[idx
],
2255 column
->type
.value
.type
, out
);
2257 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2259 ovsdb_datum_to_string(&datum
, &column
->type
, out
);
2261 ds_put_char(out
, '\n');
2262 ovsdb_datum_destroy(&datum
, &column
->type
);
2269 list_record(const struct vsctl_table_class
*table
,
2270 const struct ovsdb_idl_row
*row
, struct ds
*out
)
2274 ds_put_format(out
, "%-20s: "UUID_FMT
"\n", "_uuid",
2275 UUID_ARGS(&row
->uuid
));
2276 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2277 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2278 struct ovsdb_datum datum
;
2280 ovsdb_idl_txn_read(row
, column
, &datum
);
2282 ds_put_format(out
, "%-20s: ", column
->name
);
2283 ovsdb_datum_to_string(&datum
, &column
->type
, out
);
2284 ds_put_char(out
, '\n');
2286 ovsdb_datum_destroy(&datum
, &column
->type
);
2291 cmd_list(struct vsctl_context
*ctx
)
2293 const char *table_name
= ctx
->argv
[1];
2294 const struct vsctl_table_class
*table
;
2295 struct ds
*out
= &ctx
->output
;
2298 table
= get_table(table_name
);
2299 if (ctx
->argc
> 2) {
2300 for (i
= 2; i
< ctx
->argc
; i
++) {
2302 ds_put_char(out
, '\n');
2304 list_record(table
, must_get_row(ctx
, table
, ctx
->argv
[i
]), out
);
2307 const struct ovsdb_idl_row
*row
;
2310 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class), first
= true;
2312 row
= ovsdb_idl_next_row(row
), first
= false) {
2314 ds_put_char(out
, '\n');
2316 list_record(table
, row
, out
);
2322 set_column(const struct vsctl_table_class
*table
,
2323 const struct ovsdb_idl_row
*row
, const char *arg
,
2324 struct ovsdb_symbol_table
*symtab
)
2326 const struct ovsdb_idl_column
*column
;
2327 char *key_string
, *value_string
;
2330 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2332 die_if_error(error
);
2333 if (!value_string
) {
2334 vsctl_fatal("%s: missing value", arg
);
2338 union ovsdb_atom key
, value
;
2339 struct ovsdb_datum old
, new;
2341 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2342 vsctl_fatal("cannot specify key to set for non-map column %s",
2346 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2347 key_string
, symtab
));
2348 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2349 value_string
, symtab
));
2351 ovsdb_datum_init_empty(&new);
2352 ovsdb_datum_add_unsafe(&new, &key
, &value
, &column
->type
);
2354 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2355 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2357 ovsdb_idl_txn_read(row
, column
, &old
);
2358 ovsdb_datum_union(&old
, &new, &column
->type
, true);
2359 ovsdb_idl_txn_write(row
, column
, &old
);
2361 ovsdb_datum_destroy(&new, &column
->type
);
2363 struct ovsdb_datum datum
;
2365 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2366 value_string
, symtab
));
2367 ovsdb_idl_txn_write(row
, column
, &datum
);
2375 cmd_set(struct vsctl_context
*ctx
)
2377 const char *table_name
= ctx
->argv
[1];
2378 const char *record_id
= ctx
->argv
[2];
2379 const struct vsctl_table_class
*table
;
2380 const struct ovsdb_idl_row
*row
;
2383 table
= get_table(table_name
);
2384 row
= must_get_row(ctx
, table
, record_id
);
2385 for (i
= 3; i
< ctx
->argc
; i
++) {
2386 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2391 cmd_add(struct vsctl_context
*ctx
)
2393 const char *table_name
= ctx
->argv
[1];
2394 const char *record_id
= ctx
->argv
[2];
2395 const char *column_name
= ctx
->argv
[3];
2396 const struct vsctl_table_class
*table
;
2397 const struct ovsdb_idl_column
*column
;
2398 const struct ovsdb_idl_row
*row
;
2399 const struct ovsdb_type
*type
;
2400 struct ovsdb_datum old
;
2403 table
= get_table(table_name
);
2404 row
= must_get_row(ctx
, table
, record_id
);
2405 die_if_error(get_column(table
, column_name
, &column
));
2407 type
= &column
->type
;
2408 ovsdb_idl_txn_read(row
, column
, &old
);
2409 for (i
= 4; i
< ctx
->argc
; i
++) {
2410 struct ovsdb_type add_type
;
2411 struct ovsdb_datum add
;
2415 add_type
.n_max
= UINT_MAX
;
2416 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2418 ovsdb_datum_union(&old
, &add
, type
, false);
2419 ovsdb_datum_destroy(&add
, type
);
2421 if (old
.n
> type
->n_max
) {
2422 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2423 "table %s but the maximum number is %u",
2425 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2426 column
->name
, table
->class->name
, type
->n_max
);
2428 ovsdb_idl_txn_write(row
, column
, &old
);
2432 cmd_remove(struct vsctl_context
*ctx
)
2434 const char *table_name
= ctx
->argv
[1];
2435 const char *record_id
= ctx
->argv
[2];
2436 const char *column_name
= ctx
->argv
[3];
2437 const struct vsctl_table_class
*table
;
2438 const struct ovsdb_idl_column
*column
;
2439 const struct ovsdb_idl_row
*row
;
2440 const struct ovsdb_type
*type
;
2441 struct ovsdb_datum old
;
2444 table
= get_table(table_name
);
2445 row
= must_get_row(ctx
, table
, record_id
);
2446 die_if_error(get_column(table
, column_name
, &column
));
2448 type
= &column
->type
;
2449 ovsdb_idl_txn_read(row
, column
, &old
);
2450 for (i
= 4; i
< ctx
->argc
; i
++) {
2451 struct ovsdb_type rm_type
;
2452 struct ovsdb_datum rm
;
2457 rm_type
.n_max
= UINT_MAX
;
2458 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
2459 ctx
->argv
[i
], ctx
->symtab
);
2460 if (error
&& ovsdb_type_is_map(&rm_type
)) {
2462 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
2463 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
2464 ctx
->argv
[i
], ctx
->symtab
));
2466 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
2467 ovsdb_datum_destroy(&rm
, &rm_type
);
2469 if (old
.n
< type
->n_min
) {
2470 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2471 "table %s but the minimum number is %u",
2473 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2474 column
->name
, table
->class->name
, type
->n_min
);
2476 ovsdb_idl_txn_write(row
, column
, &old
);
2480 cmd_clear(struct vsctl_context
*ctx
)
2482 const char *table_name
= ctx
->argv
[1];
2483 const char *record_id
= ctx
->argv
[2];
2484 const struct vsctl_table_class
*table
;
2485 const struct ovsdb_idl_row
*row
;
2488 table
= get_table(table_name
);
2489 row
= must_get_row(ctx
, table
, record_id
);
2490 for (i
= 3; i
< ctx
->argc
; i
++) {
2491 const struct ovsdb_idl_column
*column
;
2492 const struct ovsdb_type
*type
;
2493 struct ovsdb_datum datum
;
2495 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
2497 type
= &column
->type
;
2498 if (type
->n_min
> 0) {
2499 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2500 "of table %s, which is not allowed to be empty",
2501 column
->name
, table
->class->name
);
2504 ovsdb_datum_init_empty(&datum
);
2505 ovsdb_idl_txn_write(row
, column
, &datum
);
2510 cmd_create(struct vsctl_context
*ctx
)
2512 const char *id
= shash_find_data(&ctx
->options
, "--id");
2513 const char *table_name
= ctx
->argv
[1];
2514 const struct vsctl_table_class
*table
;
2515 const struct ovsdb_idl_row
*row
;
2516 const struct uuid
*uuid
;
2520 struct ovsdb_symbol
*symbol
;
2523 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2526 symbol
= ovsdb_symbol_table_insert(ctx
->symtab
, id
);
2528 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2531 symbol
->used
= true;
2533 uuid
= &symbol
->uuid
;
2538 table
= get_table(table_name
);
2539 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
2540 for (i
= 2; i
< ctx
->argc
; i
++) {
2541 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2543 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
2546 /* This function may be used as the 'postprocess' function for commands that
2547 * insert new rows into the database. It expects that the command's 'run'
2548 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2549 * sole output. It replaces that output by the row's permanent UUID assigned
2550 * by the database server and appends a new-line.
2552 * Currently we use this only for "create", because the higher-level commands
2553 * are supposed to be independent of the actual structure of the vswitch
2556 post_create(struct vsctl_context
*ctx
)
2558 const struct uuid
*real
;
2561 uuid_from_string(&dummy
, ds_cstr(&ctx
->output
));
2562 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
2564 ds_clear(&ctx
->output
);
2565 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
2567 ds_put_char(&ctx
->output
, '\n');
2571 cmd_destroy(struct vsctl_context
*ctx
)
2573 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
2574 const char *table_name
= ctx
->argv
[1];
2575 const struct vsctl_table_class
*table
;
2578 table
= get_table(table_name
);
2579 for (i
= 2; i
< ctx
->argc
; i
++) {
2580 const struct ovsdb_idl_row
*row
;
2582 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
2584 ovsdb_idl_txn_delete(row
);
2589 static struct json
*
2590 where_uuid_equals(const struct uuid
*uuid
)
2593 json_array_create_1(
2594 json_array_create_3(
2595 json_string_create("_uuid"),
2596 json_string_create("=="),
2597 json_array_create_2(
2598 json_string_create("uuid"),
2599 json_string_create_nocopy(
2600 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
2604 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
2605 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2606 const struct ovsrec_open_vswitch
*ovs
,
2607 struct ovsdb_symbol_table
*symtab
)
2609 ctx
->argc
= command
->argc
;
2610 ctx
->argv
= command
->argv
;
2611 ctx
->options
= command
->options
;
2613 ds_swap(&ctx
->output
, &command
->output
);
2617 ctx
->symtab
= symtab
;
2621 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
2623 ds_swap(&ctx
->output
, &command
->output
);
2627 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
2628 struct ovsdb_idl
*idl
)
2630 struct ovsdb_idl_txn
*txn
;
2631 const struct ovsrec_open_vswitch
*ovs
;
2632 enum ovsdb_idl_txn_status status
;
2633 struct ovsdb_symbol_table
*symtab
;
2635 struct vsctl_command
*c
;
2636 int64_t next_cfg
= 0;
2639 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2641 ovsdb_idl_txn_set_dry_run(txn
);
2644 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2646 ovs
= ovsrec_open_vswitch_first(idl
);
2648 /* XXX add verification that table is empty */
2649 ovs
= ovsrec_open_vswitch_insert(txn
);
2652 if (wait_for_reload
) {
2653 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
2654 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
2655 json_destroy(where
);
2658 symtab
= ovsdb_symbol_table_create();
2659 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2660 struct vsctl_context ctx
;
2662 ds_init(&c
->output
);
2663 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2664 (c
->syntax
->run
)(&ctx
);
2665 vsctl_context_done(&ctx
, c
);
2668 status
= ovsdb_idl_txn_commit_block(txn
);
2669 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2670 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2672 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2673 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2674 if (c
->syntax
->postprocess
) {
2675 struct vsctl_context ctx
;
2677 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2678 (c
->syntax
->postprocess
)(&ctx
);
2679 vsctl_context_done(&ctx
, c
);
2683 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2684 ovsdb_idl_txn_destroy(txn
);
2687 unused
= ovsdb_symbol_table_find_unused(symtab
);
2689 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2690 "with \"-- --id=%s create ...\")", unused
, unused
);
2692 ovsdb_symbol_table_destroy(symtab
);
2695 case TXN_INCOMPLETE
:
2699 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2700 vsctl_fatal("transaction aborted");
2707 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2708 ds_destroy(&c
->output
);
2714 vsctl_fatal("transaction error: %s", error
);
2721 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2722 struct ds
*ds
= &c
->output
;
2723 struct shash_node
*node
;
2729 for (j
= 0; j
< ds
->length
; j
++) {
2730 int c
= ds
->string
[j
];
2733 fputs("\\n", stdout
);
2737 fputs("\\\\", stdout
);
2746 fputs(ds_cstr(ds
), stdout
);
2748 ds_destroy(&c
->output
);
2750 SHASH_FOR_EACH (node
, &c
->options
) {
2753 shash_destroy(&c
->options
);
2757 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2759 const struct ovsrec_open_vswitch
*ovs
;
2762 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2763 if (ovs
->cur_cfg
>= next_cfg
) {
2767 ovsdb_idl_wait(idl
);
2772 ovsdb_idl_destroy(idl
);
2777 static const struct vsctl_command_syntax all_commands
[] = {
2778 /* Open vSwitch commands. */
2779 {"init", 0, 0, cmd_init
, NULL
, ""},
2781 /* Bridge commands. */
2782 {"add-br", 1, 3, cmd_add_br
, NULL
, "--may-exist"},
2783 {"del-br", 1, 1, cmd_del_br
, NULL
, "--if-exists"},
2784 {"list-br", 0, 0, cmd_list_br
, NULL
, ""},
2785 {"br-exists", 1, 1, cmd_br_exists
, NULL
, ""},
2786 {"br-to-vlan", 1, 1, cmd_br_to_vlan
, NULL
, ""},
2787 {"br-to-parent", 1, 1, cmd_br_to_parent
, NULL
, ""},
2788 {"br-set-external-id", 2, 3, cmd_br_set_external_id
, NULL
, ""},
2789 {"br-get-external-id", 1, 2, cmd_br_get_external_id
, NULL
, ""},
2791 /* Port commands. */
2792 {"list-ports", 1, 1, cmd_list_ports
, NULL
, ""},
2793 {"add-port", 2, INT_MAX
, cmd_add_port
, NULL
, "--may-exist"},
2794 {"add-bond", 4, INT_MAX
, cmd_add_bond
, NULL
, "--may-exist,--fake-iface"},
2795 {"del-port", 1, 2, cmd_del_port
, NULL
, "--if-exists,--with-iface"},
2796 {"port-to-br", 1, 1, cmd_port_to_br
, NULL
, ""},
2798 /* Interface commands. */
2799 {"list-ifaces", 1, 1, cmd_list_ifaces
, NULL
, ""},
2800 {"iface-to-br", 1, 1, cmd_iface_to_br
, NULL
, ""},
2802 /* Controller commands. */
2803 {"get-controller", 0, 1, cmd_get_controller
, NULL
, ""},
2804 {"del-controller", 0, 1, cmd_del_controller
, NULL
, ""},
2805 {"set-controller", 1, INT_MAX
, cmd_set_controller
, NULL
, ""},
2806 {"get-fail-mode", 0, 1, cmd_get_fail_mode
, NULL
, ""},
2807 {"del-fail-mode", 0, 1, cmd_del_fail_mode
, NULL
, ""},
2808 {"set-fail-mode", 1, 2, cmd_set_fail_mode
, NULL
, ""},
2811 {"get-ssl", 0, 0, cmd_get_ssl
, NULL
, ""},
2812 {"del-ssl", 0, 0, cmd_del_ssl
, NULL
, ""},
2813 {"set-ssl", 3, 3, cmd_set_ssl
, NULL
, "--bootstrap"},
2815 /* Switch commands. */
2816 {"emer-reset", 0, 0, cmd_emer_reset
, NULL
, ""},
2818 /* Parameter commands. */
2819 {"get", 3, INT_MAX
, cmd_get
, NULL
, "--if-exists"},
2820 {"list", 1, INT_MAX
, cmd_list
, NULL
, ""},
2821 {"set", 3, INT_MAX
, cmd_set
, NULL
, ""},
2822 {"add", 4, INT_MAX
, cmd_add
, NULL
, ""},
2823 {"remove", 4, INT_MAX
, cmd_remove
, NULL
, ""},
2824 {"clear", 3, INT_MAX
, cmd_clear
, NULL
, ""},
2825 {"create", 2, INT_MAX
, cmd_create
, post_create
, "--id="},
2826 {"destroy", 1, INT_MAX
, cmd_destroy
, NULL
, "--if-exists"},
2828 {NULL
, 0, 0, NULL
, NULL
, NULL
},