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
;
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 extern struct vlog_module VLM_reconnect
;
122 struct ovsdb_idl
*idl
;
123 struct vsctl_command
*commands
;
127 set_program_name(argv
[0]);
128 signal(SIGPIPE
, SIG_IGN
);
129 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
130 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
133 /* Log our arguments. This is often valuable for debugging systems. */
134 args
= process_escape_args(argv
);
135 VLOG_INFO("Called as %s", args
);
137 /* Parse command line. */
138 parse_options(argc
, argv
);
139 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
145 /* Now execute the commands. */
146 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
);
148 if (ovsdb_idl_run(idl
)) {
149 do_vsctl(args
, commands
, n_commands
, idl
);
158 parse_options(int argc
, char *argv
[])
161 OPT_DB
= UCHAR_MAX
+ 1,
169 static struct option long_options
[] = {
170 {"db", required_argument
, 0, OPT_DB
},
171 {"no-syslog", no_argument
, 0, OPT_NO_SYSLOG
},
172 {"no-wait", no_argument
, 0, OPT_NO_WAIT
},
173 {"dry-run", no_argument
, 0, OPT_DRY_RUN
},
174 {"oneline", no_argument
, 0, OPT_ONELINE
},
175 {"timeout", required_argument
, 0, 't'},
176 {"help", no_argument
, 0, 'h'},
177 {"version", no_argument
, 0, 'V'},
180 STREAM_SSL_LONG_OPTIONS
181 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
190 c
= getopt_long(argc
, argv
, "+v::hVt:", long_options
, NULL
);
205 vlog_set_levels(&VLM_vsctl
, VLF_SYSLOG
, VLL_WARN
);
209 wait_for_reload
= false;
220 OVS_PRINT_VERSION(0, 0);
224 timeout
= strtoul(optarg
, NULL
, 10);
226 vsctl_fatal("value %s on -t or --timeout is invalid",
234 STREAM_SSL_OPTION_HANDLERS
236 case OPT_PEER_CA_CERT
:
237 stream_ssl_set_peer_ca_cert_file(optarg
);
254 static struct vsctl_command
*
255 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
257 struct vsctl_command
*commands
;
258 size_t n_commands
, allocated_commands
;
262 n_commands
= allocated_commands
= 0;
264 for (start
= i
= 0; i
<= argc
; i
++) {
265 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
267 if (n_commands
>= allocated_commands
) {
268 struct vsctl_command
*c
;
270 commands
= x2nrealloc(commands
, &allocated_commands
,
272 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
273 shash_moved(&c
->options
);
276 parse_command(i
- start
, &argv
[start
],
277 &commands
[n_commands
++]);
283 vsctl_fatal("missing command name (use --help for help)");
285 *n_commandsp
= n_commands
;
290 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
292 const struct vsctl_command_syntax
*p
;
295 shash_init(&command
->options
);
296 for (i
= 0; i
< argc
; i
++) {
297 const char *option
= argv
[i
];
301 if (option
[0] != '-') {
305 equals
= strchr(option
, '=');
307 key
= xmemdup0(option
, equals
- option
);
308 value
= xstrdup(equals
+ 1);
310 key
= xstrdup(option
);
314 if (shash_find(&command
->options
, key
)) {
315 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
317 shash_add_nocopy(&command
->options
, key
, value
);
320 vsctl_fatal("missing command name");
323 for (p
= all_commands
; p
->name
; p
++) {
324 if (!strcmp(p
->name
, argv
[i
])) {
325 struct shash_node
*node
;
328 SHASH_FOR_EACH (node
, &command
->options
) {
329 const char *s
= strstr(p
->options
, node
->name
);
330 int end
= s
? s
[strlen(node
->name
)] : EOF
;
332 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
333 vsctl_fatal("'%s' command has no '%s' option",
334 argv
[i
], node
->name
);
336 if ((end
== '=') != (node
->data
!= NULL
)) {
338 vsctl_fatal("missing argument to '%s' option on '%s' "
339 "command", node
->name
, argv
[i
]);
341 vsctl_fatal("'%s' option on '%s' does not accept an "
342 "argument", node
->name
, argv
[i
]);
347 n_arg
= argc
- i
- 1;
348 if (n_arg
< p
->min_args
) {
349 vsctl_fatal("'%s' command requires at least %d arguments",
350 p
->name
, p
->min_args
);
351 } else if (n_arg
> p
->max_args
) {
354 for (j
= i
+ 1; j
< argc
; j
++) {
355 if (argv
[j
][0] == '-') {
356 vsctl_fatal("'%s' command takes at most %d arguments "
357 "(note that options must precede command "
358 "names and follow a \"--\" argument)",
359 p
->name
, p
->max_args
);
363 vsctl_fatal("'%s' command takes at most %d arguments",
364 p
->name
, p
->max_args
);
367 command
->argc
= n_arg
+ 1;
368 command
->argv
= &argv
[i
];
374 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
378 vsctl_fatal(const char *format
, ...)
383 va_start(args
, format
);
384 message
= xvasprintf(format
, args
);
387 vlog_set_levels(&VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
388 VLOG_ERR("%s", message
);
389 ovs_error(0, "%s", message
);
390 vsctl_exit(EXIT_FAILURE
);
393 /* Frees the current transaction and the underlying IDL and then calls
396 * Freeing the transaction and the IDL is not strictly necessary, but it makes
397 * for a clean memory leak report from valgrind in the normal case. That makes
398 * it easier to notice real memory leaks. */
400 vsctl_exit(int status
)
403 ovsdb_idl_txn_abort(the_idl_txn
);
404 ovsdb_idl_txn_destroy(the_idl_txn
);
406 ovsdb_idl_destroy(the_idl
);
414 %s: ovs-vswitchd management utility\n\
415 usage: %s [OPTIONS] COMMAND [ARG...]\n\
418 add-br BRIDGE create a new bridge named BRIDGE\n\
419 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
420 del-br BRIDGE delete BRIDGE and all of its ports\n\
421 list-br print the names of all the bridges\n\
422 br-exists BRIDGE test whether BRIDGE exists\n\
423 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
424 br-to-parent BRIDGE print the parent of BRIDGE\n\
425 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
426 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
427 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
428 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
431 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
432 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
433 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
434 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
435 port-to-br PORT print name of bridge that contains PORT\n\
436 A bond is considered to be a single port.\n\
438 Interface commands (a bond consists of multiple interfaces):\n\
439 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
440 iface-to-br IFACE print name of bridge that contains IFACE\n\
442 Controller commands:\n\
443 get-controller BRIDGE print the controller for BRIDGE\n\
444 del-controller BRIDGE delete the controller for BRIDGE\n\
445 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
446 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
447 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
448 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
451 get-ssl print the SSL configuration\n\
452 del-ssl delete the SSL configuration\n\
453 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
456 emer-reset reset switch to known good state\n\
458 Database commands:\n\
459 list TBL [REC] list RECord (or all records) in TBL\n\
460 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
461 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
462 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
463 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
464 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
465 create TBL COL[:KEY]=VALUE create and initialize new record\n\
466 destroy TBL REC delete RECord from TBL\n\
467 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
468 Potentially unsafe database commands require --force option.\n\
471 --db=DATABASE connect to DATABASE\n\
473 --oneline print exactly one line of output per command\n",
474 program_name
, program_name
, default_db());
478 -h, --help display this help message\n\
479 -V, --version display version information\n");
488 def
= xasprintf("unix:%s/db.sock", ovs_rundir
);
493 struct vsctl_context
{
497 struct shash options
;
499 /* Modifiable state. */
501 struct ovsdb_idl
*idl
;
502 struct ovsdb_idl_txn
*txn
;
503 struct ovsdb_symbol_table
*symtab
;
504 const struct ovsrec_open_vswitch
*ovs
;
506 /* A command may set this member to true if some prerequisite is not met
507 * and the caller should wait for something to change and then retry. */
511 struct vsctl_bridge
{
512 struct ovsrec_bridge
*br_cfg
;
514 struct ovsrec_controller
**ctrl
;
517 struct vsctl_bridge
*parent
;
522 struct ovsrec_port
*port_cfg
;
523 struct vsctl_bridge
*bridge
;
527 struct ovsrec_interface
*iface_cfg
;
528 struct vsctl_port
*port
;
532 struct shash bridges
;
538 vsctl_context_to_string(const struct vsctl_context
*ctx
)
540 const struct shash_node
*node
;
546 SHASH_FOR_EACH (node
, &ctx
->options
) {
547 svec_add(&words
, node
->name
);
549 for (i
= 0; i
< ctx
->argc
; i
++) {
550 svec_add(&words
, ctx
->argv
[i
]);
552 svec_terminate(&words
);
554 s
= process_escape_args(words
.names
);
556 svec_destroy(&words
);
561 static struct vsctl_bridge
*
562 add_bridge(struct vsctl_info
*b
,
563 struct ovsrec_bridge
*br_cfg
, const char *name
,
564 struct vsctl_bridge
*parent
, int vlan
)
566 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
568 br
->name
= xstrdup(name
);
572 br
->ctrl
= parent
->br_cfg
->controller
;
573 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
574 br
->fail_mode
= parent
->br_cfg
->fail_mode
;
576 br
->ctrl
= br_cfg
->controller
;
577 br
->n_ctrl
= br_cfg
->n_controller
;
578 br
->fail_mode
= br_cfg
->fail_mode
;
580 shash_add(&b
->bridges
, br
->name
, br
);
585 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
587 return (port_cfg
->fake_bridge
589 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095);
592 static struct vsctl_bridge
*
593 find_vlan_bridge(struct vsctl_info
*info
,
594 struct vsctl_bridge
*parent
, int vlan
)
596 struct shash_node
*node
;
598 SHASH_FOR_EACH (node
, &info
->bridges
) {
599 struct vsctl_bridge
*br
= node
->data
;
600 if (br
->parent
== parent
&& br
->vlan
== vlan
) {
609 free_info(struct vsctl_info
*info
)
611 struct shash_node
*node
;
613 SHASH_FOR_EACH (node
, &info
->bridges
) {
614 struct vsctl_bridge
*bridge
= node
->data
;
618 shash_destroy(&info
->bridges
);
620 shash_destroy_free_data(&info
->ports
);
621 shash_destroy_free_data(&info
->ifaces
);
625 get_info(const struct ovsrec_open_vswitch
*ovs
, struct vsctl_info
*info
)
627 struct shash bridges
, ports
;
630 shash_init(&info
->bridges
);
631 shash_init(&info
->ports
);
632 shash_init(&info
->ifaces
);
634 shash_init(&bridges
);
636 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
637 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
638 struct vsctl_bridge
*br
;
641 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
642 VLOG_WARN("%s: database contains duplicate bridge name",
646 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
651 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
652 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
654 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
655 VLOG_WARN("%s: database contains duplicate port name",
660 if (port_is_fake_bridge(port_cfg
)
661 && shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
662 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
666 shash_destroy(&bridges
);
667 shash_destroy(&ports
);
669 shash_init(&bridges
);
671 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
672 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
673 struct vsctl_bridge
*br
;
676 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
679 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
680 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
681 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
682 struct vsctl_port
*port
;
685 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
689 if (port_is_fake_bridge(port_cfg
)
690 && !shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
694 port
= xmalloc(sizeof *port
);
695 port
->port_cfg
= port_cfg
;
697 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
698 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
705 shash_add(&info
->ports
, port_cfg
->name
, port
);
707 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
708 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
709 struct vsctl_iface
*iface
;
711 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
712 VLOG_WARN("%s: database contains duplicate interface name",
717 iface
= xmalloc(sizeof *iface
);
718 iface
->iface_cfg
= iface_cfg
;
720 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
724 shash_destroy(&bridges
);
725 shash_destroy(&ports
);
729 check_conflicts(struct vsctl_info
*info
, const char *name
,
732 struct vsctl_iface
*iface
;
733 struct vsctl_port
*port
;
735 if (shash_find(&info
->bridges
, name
)) {
736 vsctl_fatal("%s because a bridge named %s already exists",
740 port
= shash_find_data(&info
->ports
, name
);
742 vsctl_fatal("%s because a port named %s already exists on "
743 "bridge %s", msg
, name
, port
->bridge
->name
);
746 iface
= shash_find_data(&info
->ifaces
, name
);
748 vsctl_fatal("%s because an interface named %s already exists "
749 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
755 static struct vsctl_bridge
*
756 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
758 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
759 if (must_exist
&& !br
) {
760 vsctl_fatal("no bridge named %s", name
);
765 static struct vsctl_bridge
*
766 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
768 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
769 if (br
&& br
->parent
) {
770 vsctl_fatal("%s is a fake bridge", name
);
775 static struct vsctl_port
*
776 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
778 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
779 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
782 if (must_exist
&& !port
) {
783 vsctl_fatal("no port named %s", name
);
788 static struct vsctl_iface
*
789 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
791 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
792 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
795 if (must_exist
&& !iface
) {
796 vsctl_fatal("no interface named %s", name
);
802 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
804 struct ovsrec_port
**ports
;
807 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
808 for (i
= 0; i
< br
->n_ports
; i
++) {
809 ports
[i
] = br
->ports
[i
];
811 ports
[br
->n_ports
] = port
;
812 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
817 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
819 struct ovsrec_port
**ports
;
822 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
823 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
824 if (br
->ports
[i
] != port
) {
825 ports
[n
++] = br
->ports
[i
];
828 ovsrec_bridge_set_ports(br
, ports
, n
);
833 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
834 struct ovsrec_bridge
*bridge
)
836 struct ovsrec_bridge
**bridges
;
839 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
840 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
841 bridges
[i
] = ovs
->bridges
[i
];
843 bridges
[ovs
->n_bridges
] = bridge
;
844 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
849 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
850 struct ovsrec_bridge
*bridge
)
852 struct ovsrec_bridge
**bridges
;
855 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
856 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
857 if (ovs
->bridges
[i
] != bridge
) {
858 bridges
[n
++] = ovs
->bridges
[i
];
861 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
866 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
871 cmd_emer_reset(struct vsctl_context
*ctx
)
873 const struct ovsdb_idl
*idl
= ctx
->idl
;
874 const struct ovsrec_bridge
*br
;
875 const struct ovsrec_port
*port
;
876 const struct ovsrec_interface
*iface
;
877 const struct ovsrec_mirror
*mirror
, *next_mirror
;
878 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
879 const struct ovsrec_netflow
*nf
, *next_nf
;
880 const struct ovsrec_ssl
*ssl
, *next_ssl
;
881 const struct ovsrec_sflow
*sflow
, *next_sflow
;
884 /* Reset the Open_vSwitch table. */
885 ovsrec_open_vswitch_set_managers(ctx
->ovs
, NULL
, 0);
886 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
888 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
890 char *hw_key
= "hwaddr";
893 ovsrec_bridge_set_controller(br
, NULL
, 0);
894 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
895 ovsrec_bridge_set_netflow(br
, NULL
);
896 ovsrec_bridge_set_sflow(br
, NULL
);
897 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
899 /* We only want to save the "hwaddr" key from other_config. */
900 for (i
=0; i
< br
->n_other_config
; i
++) {
901 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
902 hw_val
= br
->value_other_config
[i
];
907 char *val
= xstrdup(hw_val
);
908 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
911 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
915 OVSREC_PORT_FOR_EACH (port
, idl
) {
916 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
919 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
920 /* xxx What do we do about gre/patch devices created by mgr? */
922 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
923 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
926 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
927 ovsrec_mirror_delete(mirror
);
930 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
931 ovsrec_controller_delete(ctrl
);
934 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
935 ovsrec_netflow_delete(nf
);
938 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
939 ovsrec_ssl_delete(ssl
);
942 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
943 ovsrec_sflow_delete(sflow
);
948 cmd_add_br(struct vsctl_context
*ctx
)
950 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
951 const char *br_name
, *parent_name
;
952 struct vsctl_info info
;
955 br_name
= ctx
->argv
[1];
956 if (ctx
->argc
== 2) {
959 } else if (ctx
->argc
== 4) {
960 parent_name
= ctx
->argv
[2];
961 vlan
= atoi(ctx
->argv
[3]);
962 if (vlan
< 1 || vlan
> 4095) {
963 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
966 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
970 get_info(ctx
->ovs
, &info
);
972 struct vsctl_bridge
*br
;
974 br
= find_bridge(&info
, br_name
, false);
978 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
979 "a VLAN bridge for VLAN %d",
980 br_name
, br_name
, br
->vlan
);
984 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
985 "is not a VLAN bridge",
986 br_name
, parent_name
, vlan
, br_name
);
987 } else if (strcmp(br
->parent
->name
, parent_name
)) {
988 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
989 "has the wrong parent %s",
990 br_name
, parent_name
, vlan
,
991 br_name
, br
->parent
->name
);
992 } else if (br
->vlan
!= vlan
) {
993 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
994 "is a VLAN bridge for the wrong VLAN %d",
995 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1001 check_conflicts(&info
, br_name
,
1002 xasprintf("cannot create a bridge named %s", br_name
));
1005 struct ovsrec_port
*port
;
1006 struct ovsrec_interface
*iface
;
1007 struct ovsrec_bridge
*br
;
1009 iface
= ovsrec_interface_insert(ctx
->txn
);
1010 ovsrec_interface_set_name(iface
, br_name
);
1012 port
= ovsrec_port_insert(ctx
->txn
);
1013 ovsrec_port_set_name(port
, br_name
);
1014 ovsrec_port_set_interfaces(port
, &iface
, 1);
1016 br
= ovsrec_bridge_insert(ctx
->txn
);
1017 ovsrec_bridge_set_name(br
, br_name
);
1018 ovsrec_bridge_set_ports(br
, &port
, 1);
1020 ovs_insert_bridge(ctx
->ovs
, br
);
1022 struct vsctl_bridge
*parent
;
1023 struct ovsrec_port
*port
;
1024 struct ovsrec_interface
*iface
;
1025 struct ovsrec_bridge
*br
;
1028 parent
= find_bridge(&info
, parent_name
, false);
1029 if (parent
&& parent
->vlan
) {
1030 vsctl_fatal("cannot create bridge with fake bridge as parent");
1033 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1035 br
= parent
->br_cfg
;
1037 iface
= ovsrec_interface_insert(ctx
->txn
);
1038 ovsrec_interface_set_name(iface
, br_name
);
1039 ovsrec_interface_set_type(iface
, "internal");
1041 port
= ovsrec_port_insert(ctx
->txn
);
1042 ovsrec_port_set_name(port
, br_name
);
1043 ovsrec_port_set_interfaces(port
, &iface
, 1);
1044 ovsrec_port_set_fake_bridge(port
, true);
1045 ovsrec_port_set_tag(port
, &tag
, 1);
1047 bridge_insert_port(br
, port
);
1054 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1056 struct shash_node
*node
;
1058 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1059 struct vsctl_iface
*iface
= node
->data
;
1060 if (iface
->port
== port
) {
1061 ovsrec_interface_delete(iface
->iface_cfg
);
1064 ovsrec_port_delete(port
->port_cfg
);
1066 bridge_delete_port((port
->bridge
->parent
1067 ? port
->bridge
->parent
->br_cfg
1068 : port
->bridge
->br_cfg
), port
->port_cfg
);
1072 cmd_del_br(struct vsctl_context
*ctx
)
1074 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1075 struct vsctl_bridge
*bridge
;
1076 struct vsctl_info info
;
1078 get_info(ctx
->ovs
, &info
);
1079 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1081 struct shash_node
*node
;
1083 SHASH_FOR_EACH (node
, &info
.ports
) {
1084 struct vsctl_port
*port
= node
->data
;
1085 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1086 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1087 del_port(&info
, port
);
1090 if (bridge
->br_cfg
) {
1091 ovsrec_bridge_delete(bridge
->br_cfg
);
1092 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1099 output_sorted(struct svec
*svec
, struct ds
*output
)
1105 SVEC_FOR_EACH (i
, name
, svec
) {
1106 ds_put_format(output
, "%s\n", name
);
1111 cmd_list_br(struct vsctl_context
*ctx
)
1113 struct shash_node
*node
;
1114 struct vsctl_info info
;
1115 struct svec bridges
;
1117 get_info(ctx
->ovs
, &info
);
1119 svec_init(&bridges
);
1120 SHASH_FOR_EACH (node
, &info
.bridges
) {
1121 struct vsctl_bridge
*br
= node
->data
;
1122 svec_add(&bridges
, br
->name
);
1124 output_sorted(&bridges
, &ctx
->output
);
1125 svec_destroy(&bridges
);
1131 cmd_br_exists(struct vsctl_context
*ctx
)
1133 struct vsctl_info info
;
1135 get_info(ctx
->ovs
, &info
);
1136 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1142 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1143 * equals 'a', false otherwise. */
1145 key_matches(const char *a
,
1146 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1148 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1152 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1153 char *key
, char *value
,
1154 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1161 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1162 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1164 for (i
= 0; i
< old_n
; i
++) {
1165 if (strcmp(key
, old_keys
[i
])) {
1166 new_keys
[new_n
] = old_keys
[i
];
1167 new_values
[new_n
] = old_values
[i
];
1172 new_keys
[new_n
] = key
;
1173 new_values
[new_n
] = value
;
1176 *new_keysp
= new_keys
;
1177 *new_valuesp
= new_values
;
1182 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1184 struct vsctl_info info
;
1185 struct vsctl_bridge
*bridge
;
1186 char **keys
, **values
;
1189 get_info(ctx
->ovs
, &info
);
1190 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1191 if (bridge
->br_cfg
) {
1192 set_external_id(bridge
->br_cfg
->key_external_ids
,
1193 bridge
->br_cfg
->value_external_ids
,
1194 bridge
->br_cfg
->n_external_ids
,
1195 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1196 &keys
, &values
, &n
);
1197 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1199 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1200 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1201 set_external_id(port
->port_cfg
->key_external_ids
,
1202 port
->port_cfg
->value_external_ids
,
1203 port
->port_cfg
->n_external_ids
,
1204 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1205 &keys
, &values
, &n
);
1206 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1216 get_external_id(char **keys
, char **values
, size_t n
,
1217 const char *prefix
, const char *key
,
1220 size_t prefix_len
= strlen(prefix
);
1225 for (i
= 0; i
< n
; i
++) {
1226 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1227 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1228 keys
[i
] + prefix_len
, values
[i
]));
1229 } else if (key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1230 svec_add(&svec
, values
[i
]);
1234 output_sorted(&svec
, output
);
1235 svec_destroy(&svec
);
1239 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1241 struct vsctl_info info
;
1242 struct vsctl_bridge
*bridge
;
1244 get_info(ctx
->ovs
, &info
);
1245 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1246 if (bridge
->br_cfg
) {
1247 get_external_id(bridge
->br_cfg
->key_external_ids
,
1248 bridge
->br_cfg
->value_external_ids
,
1249 bridge
->br_cfg
->n_external_ids
,
1250 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1253 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1254 get_external_id(port
->port_cfg
->key_external_ids
,
1255 port
->port_cfg
->value_external_ids
,
1256 port
->port_cfg
->n_external_ids
,
1257 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1264 cmd_list_ports(struct vsctl_context
*ctx
)
1266 struct vsctl_bridge
*br
;
1267 struct shash_node
*node
;
1268 struct vsctl_info info
;
1271 get_info(ctx
->ovs
, &info
);
1272 br
= find_bridge(&info
, ctx
->argv
[1], true);
1275 SHASH_FOR_EACH (node
, &info
.ports
) {
1276 struct vsctl_port
*port
= node
->data
;
1278 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1279 svec_add(&ports
, port
->port_cfg
->name
);
1282 output_sorted(&ports
, &ctx
->output
);
1283 svec_destroy(&ports
);
1289 add_port(struct vsctl_context
*ctx
,
1290 const char *br_name
, const char *port_name
,
1291 bool may_exist
, bool fake_iface
,
1292 char *iface_names
[], int n_ifaces
,
1293 char *settings
[], int n_settings
)
1295 struct vsctl_info info
;
1296 struct vsctl_bridge
*bridge
;
1297 struct ovsrec_interface
**ifaces
;
1298 struct ovsrec_port
*port
;
1301 get_info(ctx
->ovs
, &info
);
1303 struct vsctl_port
*port
;
1305 port
= find_port(&info
, port_name
, false);
1307 struct svec want_names
, have_names
;
1310 svec_init(&want_names
);
1311 for (i
= 0; i
< n_ifaces
; i
++) {
1312 svec_add(&want_names
, iface_names
[i
]);
1314 svec_sort(&want_names
);
1316 svec_init(&have_names
);
1317 for (i
= 0; i
< port
->port_cfg
->n_interfaces
; i
++) {
1318 svec_add(&have_names
, port
->port_cfg
->interfaces
[i
]->name
);
1320 svec_sort(&have_names
);
1322 if (strcmp(port
->bridge
->name
, br_name
)) {
1323 char *command
= vsctl_context_to_string(ctx
);
1324 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1325 command
, port_name
, port
->bridge
->name
);
1328 if (!svec_equal(&want_names
, &have_names
)) {
1329 char *have_names_string
= svec_join(&have_names
, ", ", "");
1330 char *command
= vsctl_context_to_string(ctx
);
1332 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1333 command
, port_name
, have_names_string
);
1336 svec_destroy(&want_names
);
1337 svec_destroy(&have_names
);
1342 check_conflicts(&info
, port_name
,
1343 xasprintf("cannot create a port named %s", port_name
));
1344 for (i
= 0; i
< n_ifaces
; i
++) {
1345 check_conflicts(&info
, iface_names
[i
],
1346 xasprintf("cannot create an interface named %s",
1349 bridge
= find_bridge(&info
, br_name
, true);
1351 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1352 for (i
= 0; i
< n_ifaces
; i
++) {
1353 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1354 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1357 port
= ovsrec_port_insert(ctx
->txn
);
1358 ovsrec_port_set_name(port
, port_name
);
1359 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1360 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1364 int64_t tag
= bridge
->vlan
;
1365 ovsrec_port_set_tag(port
, &tag
, 1);
1368 for (i
= 0; i
< n_settings
; i
++) {
1369 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1373 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1374 : bridge
->br_cfg
), port
);
1380 cmd_add_port(struct vsctl_context
*ctx
)
1382 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1384 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1385 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1389 cmd_add_bond(struct vsctl_context
*ctx
)
1391 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1392 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1396 n_ifaces
= ctx
->argc
- 3;
1397 for (i
= 3; i
< ctx
->argc
; i
++) {
1398 if (strchr(ctx
->argv
[i
], '=')) {
1404 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1405 "%d were specified", n_ifaces
);
1408 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1409 &ctx
->argv
[3], n_ifaces
,
1410 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1414 cmd_del_port(struct vsctl_context
*ctx
)
1416 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1417 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1418 struct vsctl_port
*port
;
1419 struct vsctl_info info
;
1421 get_info(ctx
->ovs
, &info
);
1423 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1425 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1426 struct vsctl_iface
*iface
;
1428 port
= find_port(&info
, target
, false);
1430 iface
= find_iface(&info
, target
, false);
1435 if (must_exist
&& !port
) {
1436 vsctl_fatal("no port or interface named %s", target
);
1441 if (ctx
->argc
== 3) {
1442 struct vsctl_bridge
*bridge
;
1444 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1445 if (port
->bridge
!= bridge
) {
1446 if (port
->bridge
->parent
== bridge
) {
1447 vsctl_fatal("bridge %s does not have a port %s (although "
1448 "its parent bridge %s does)",
1449 ctx
->argv
[1], ctx
->argv
[2],
1450 bridge
->parent
->name
);
1452 vsctl_fatal("bridge %s does not have a port %s",
1453 ctx
->argv
[1], ctx
->argv
[2]);
1458 del_port(&info
, port
);
1465 cmd_port_to_br(struct vsctl_context
*ctx
)
1467 struct vsctl_port
*port
;
1468 struct vsctl_info info
;
1470 get_info(ctx
->ovs
, &info
);
1471 port
= find_port(&info
, ctx
->argv
[1], true);
1472 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1477 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1479 struct vsctl_bridge
*bridge
;
1480 struct vsctl_info info
;
1482 get_info(ctx
->ovs
, &info
);
1483 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1484 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1489 cmd_br_to_parent(struct vsctl_context
*ctx
)
1491 struct vsctl_bridge
*bridge
;
1492 struct vsctl_info info
;
1494 get_info(ctx
->ovs
, &info
);
1495 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1496 if (bridge
->parent
) {
1497 bridge
= bridge
->parent
;
1499 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1504 cmd_list_ifaces(struct vsctl_context
*ctx
)
1506 struct vsctl_bridge
*br
;
1507 struct shash_node
*node
;
1508 struct vsctl_info info
;
1511 get_info(ctx
->ovs
, &info
);
1512 br
= find_bridge(&info
, ctx
->argv
[1], true);
1515 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1516 struct vsctl_iface
*iface
= node
->data
;
1518 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1519 && br
== iface
->port
->bridge
) {
1520 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1523 output_sorted(&ifaces
, &ctx
->output
);
1524 svec_destroy(&ifaces
);
1530 cmd_iface_to_br(struct vsctl_context
*ctx
)
1532 struct vsctl_iface
*iface
;
1533 struct vsctl_info info
;
1535 get_info(ctx
->ovs
, &info
);
1536 iface
= find_iface(&info
, ctx
->argv
[1], true);
1537 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1542 cmd_get_controller(struct vsctl_context
*ctx
)
1544 struct vsctl_info info
;
1545 struct vsctl_bridge
*br
;
1546 struct svec targets
;
1549 get_info(ctx
->ovs
, &info
);
1550 br
= find_bridge(&info
, ctx
->argv
[1], true);
1552 /* Print the targets in sorted order for reproducibility. */
1553 svec_init(&targets
);
1554 for (i
= 0; i
< br
->n_ctrl
; i
++) {
1555 svec_add(&targets
, br
->ctrl
[i
]->target
);
1558 svec_sort(&targets
);
1559 for (i
= 0; i
< targets
.n
; i
++) {
1560 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1562 svec_destroy(&targets
);
1568 delete_controllers(struct ovsrec_controller
**controllers
,
1569 size_t n_controllers
)
1573 for (i
= 0; i
< n_controllers
; i
++) {
1574 ovsrec_controller_delete(controllers
[i
]);
1579 cmd_del_controller(struct vsctl_context
*ctx
)
1581 struct vsctl_info info
;
1582 struct vsctl_bridge
*br
;
1584 get_info(ctx
->ovs
, &info
);
1585 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1588 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1589 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1595 static struct ovsrec_controller
**
1596 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1598 struct ovsrec_controller
**controllers
;
1601 controllers
= xmalloc(n
* sizeof *controllers
);
1602 for (i
= 0; i
< n
; i
++) {
1603 controllers
[i
] = ovsrec_controller_insert(txn
);
1604 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1611 cmd_set_controller(struct vsctl_context
*ctx
)
1613 struct vsctl_info info
;
1614 struct vsctl_bridge
*br
;
1615 struct ovsrec_controller
**controllers
;
1618 get_info(ctx
->ovs
, &info
);
1619 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1621 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1624 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1625 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1632 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1634 struct vsctl_info info
;
1635 struct vsctl_bridge
*br
;
1637 get_info(ctx
->ovs
, &info
);
1638 br
= find_bridge(&info
, ctx
->argv
[1], true);
1640 if (br
->fail_mode
&& strlen(br
->fail_mode
)) {
1641 ds_put_format(&ctx
->output
, "%s\n", br
->fail_mode
);
1648 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1650 struct vsctl_info info
;
1651 struct vsctl_bridge
*br
;
1653 get_info(ctx
->ovs
, &info
);
1654 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1656 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1662 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1664 struct vsctl_info info
;
1665 struct vsctl_bridge
*br
;
1666 const char *fail_mode
= ctx
->argv
[2];
1668 get_info(ctx
->ovs
, &info
);
1669 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1671 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1672 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1675 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1681 cmd_get_ssl(struct vsctl_context
*ctx
)
1683 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1686 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
1687 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
1688 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
1689 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
1690 ssl
->bootstrap_ca_cert
? "true" : "false");
1695 cmd_del_ssl(struct vsctl_context
*ctx
)
1697 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1700 ovsrec_ssl_delete(ssl
);
1701 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1706 cmd_set_ssl(struct vsctl_context
*ctx
)
1708 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
1709 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1712 ovsrec_ssl_delete(ssl
);
1714 ssl
= ovsrec_ssl_insert(ctx
->txn
);
1716 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
1717 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
1718 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
1720 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
1722 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
1725 /* Parameter commands. */
1727 struct vsctl_row_id
{
1728 const struct ovsdb_idl_table_class
*table
;
1729 const struct ovsdb_idl_column
*name_column
;
1730 const struct ovsdb_idl_column
*uuid_column
;
1733 struct vsctl_table_class
{
1734 struct ovsdb_idl_table_class
*class;
1735 struct vsctl_row_id row_ids
[2];
1738 static const struct vsctl_table_class tables
[] = {
1739 {&ovsrec_table_bridge
,
1740 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
1741 {NULL
, NULL
, NULL
}}},
1743 {&ovsrec_table_controller
,
1744 {{&ovsrec_table_bridge
,
1745 &ovsrec_bridge_col_name
,
1746 &ovsrec_bridge_col_controller
}}},
1748 {&ovsrec_table_interface
,
1749 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
1750 {NULL
, NULL
, NULL
}}},
1752 {&ovsrec_table_mirror
,
1753 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
1754 {NULL
, NULL
, NULL
}}},
1756 {&ovsrec_table_netflow
,
1757 {{&ovsrec_table_bridge
,
1758 &ovsrec_bridge_col_name
,
1759 &ovsrec_bridge_col_netflow
},
1760 {NULL
, NULL
, NULL
}}},
1762 {&ovsrec_table_open_vswitch
,
1763 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
1764 {NULL
, NULL
, NULL
}}},
1766 {&ovsrec_table_port
,
1767 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
1768 {NULL
, NULL
, NULL
}}},
1771 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
1772 {NULL
, NULL
, NULL
}}},
1774 {&ovsrec_table_queue
,
1775 {{NULL
, NULL
, NULL
},
1776 {NULL
, NULL
, NULL
}}},
1779 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
1781 {&ovsrec_table_sflow
,
1782 {{&ovsrec_table_bridge
,
1783 &ovsrec_bridge_col_name
,
1784 &ovsrec_bridge_col_sflow
},
1785 {NULL
, NULL
, NULL
}}},
1787 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
1791 die_if_error(char *error
)
1794 vsctl_fatal("%s", error
);
1799 to_lower_and_underscores(unsigned c
)
1801 return c
== '-' ? '_' : tolower(c
);
1805 score_partial_match(const char *name
, const char *s
)
1809 if (!strcmp(name
, s
)) {
1812 for (score
= 0; ; score
++, name
++, s
++) {
1813 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
1815 } else if (*name
== '\0') {
1816 return UINT_MAX
- 1;
1819 return *s
== '\0' ? score
: 0;
1822 static const struct vsctl_table_class
*
1823 get_table(const char *table_name
)
1825 const struct vsctl_table_class
*table
;
1826 const struct vsctl_table_class
*best_match
= NULL
;
1827 unsigned int best_score
= 0;
1829 for (table
= tables
; table
->class; table
++) {
1830 unsigned int score
= score_partial_match(table
->class->name
,
1832 if (score
> best_score
) {
1835 } else if (score
== best_score
) {
1841 } else if (best_score
) {
1842 vsctl_fatal("multiple table names match \"%s\"", table_name
);
1844 vsctl_fatal("unknown table \"%s\"", table_name
);
1848 static const struct ovsdb_idl_row
*
1849 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
1850 const struct vsctl_row_id
*id
, const char *record_id
)
1852 const struct ovsdb_idl_row
*referrer
, *final
;
1858 if (!id
->name_column
) {
1859 if (strcmp(record_id
, ".")) {
1862 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
1863 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
1867 const struct ovsdb_idl_row
*row
;
1870 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
1872 row
= ovsdb_idl_next_row(row
))
1874 const struct ovsdb_datum
*name
;
1876 name
= ovsdb_idl_get(row
, id
->name_column
,
1877 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
1878 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
1880 vsctl_fatal("multiple rows in %s match \"%s\"",
1881 table
->class->name
, record_id
);
1892 if (id
->uuid_column
) {
1893 const struct ovsdb_datum
*uuid
;
1895 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
1896 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
1898 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
1899 &uuid
->keys
[0].uuid
);
1908 static const struct ovsdb_idl_row
*
1909 get_row (struct vsctl_context
*ctx
,
1910 const struct vsctl_table_class
*table
, const char *record_id
)
1912 const struct ovsdb_idl_row
*row
;
1915 if (uuid_from_string(&uuid
, record_id
)) {
1916 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
1920 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
1921 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
1930 static const struct ovsdb_idl_row
*
1931 must_get_row(struct vsctl_context
*ctx
,
1932 const struct vsctl_table_class
*table
, const char *record_id
)
1934 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
1936 vsctl_fatal("no row \"%s\" in table %s",
1937 record_id
, table
->class->name
);
1943 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
1944 const struct ovsdb_idl_column
**columnp
)
1946 const struct ovsdb_idl_column
*best_match
= NULL
;
1947 unsigned int best_score
= 0;
1950 for (i
= 0; i
< table
->class->n_columns
; i
++) {
1951 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
1952 unsigned int score
= score_partial_match(column
->name
, column_name
);
1953 if (score
> best_score
) {
1954 best_match
= column
;
1956 } else if (score
== best_score
) {
1961 *columnp
= best_match
;
1964 } else if (best_score
) {
1965 return xasprintf("%s contains more than one column whose name "
1966 "matches \"%s\"", table
->class->name
, column_name
);
1968 return xasprintf("%s does not contain a column whose name matches "
1969 "\"%s\"", table
->class->name
, column_name
);
1974 missing_operator_error(const char *arg
, const char **allowed_operators
,
1980 ds_put_format(&s
, "%s: argument does not end in ", arg
);
1981 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
1982 if (n_allowed
== 2) {
1983 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
1984 } else if (n_allowed
> 2) {
1987 for (i
= 1; i
< n_allowed
- 1; i
++) {
1988 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
1990 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
1992 ds_put_format(&s
, " followed by a value.");
1994 return ds_steal_cstr(&s
);
1997 /* Breaks 'arg' apart into a number of fields in the following order:
1999 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2000 * is stored into '*columnp'. The column name may be abbreviated.
2002 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2003 * and 'keyp' are nonnull, then the column and key names are expected to
2004 * be separated by ':'). The key is stored as a malloc()'d string into
2005 * '*keyp', or NULL if no key is present in 'arg'.
2007 * - If 'valuep' is nonnull, an operator followed by a value string. The
2008 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2009 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2010 * operator is stored into '*operatorp' (one of the pointers from
2011 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2012 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2015 * At least 'columnp' or 'keyp' must be nonnull.
2017 * On success, returns NULL. On failure, returned a malloc()'d string error
2018 * message and stores NULL into all of the nonnull output arguments. */
2019 static char * WARN_UNUSED_RESULT
2020 parse_column_key_value(const char *arg
,
2021 const struct vsctl_table_class
*table
,
2022 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2023 const char **operatorp
,
2024 const char **allowed_operators
, size_t n_allowed
,
2027 const char *p
= arg
;
2030 assert(columnp
|| keyp
);
2031 assert(!(operatorp
&& !valuep
));
2039 /* Parse column name. */
2043 error
= ovsdb_token_parse(&p
, &column_name
);
2047 if (column_name
[0] == '\0') {
2049 error
= xasprintf("%s: missing column name", arg
);
2052 error
= get_column(table
, column_name
, columnp
);
2059 /* Parse key string. */
2060 if (*p
== ':' || !columnp
) {
2064 error
= xasprintf("%s: key not accepted here", arg
);
2067 error
= ovsdb_token_parse(&p
, keyp
);
2075 /* Parse value string. */
2081 if (!allowed_operators
) {
2082 static const char *equals
= "=";
2083 allowed_operators
= &equals
;
2089 for (i
= 0; i
< n_allowed
; i
++) {
2090 const char *op
= allowed_operators
[i
];
2091 size_t op_len
= strlen(op
);
2093 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2099 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2106 *valuep
= xstrdup(p
+ best_len
);
2112 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2138 cmd_get(struct vsctl_context
*ctx
)
2140 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2141 const char *table_name
= ctx
->argv
[1];
2142 const char *record_id
= ctx
->argv
[2];
2143 const struct vsctl_table_class
*table
;
2144 const struct ovsdb_idl_row
*row
;
2145 struct ds
*out
= &ctx
->output
;
2148 table
= get_table(table_name
);
2149 row
= must_get_row(ctx
, table
, record_id
);
2150 for (i
= 3; i
< ctx
->argc
; i
++) {
2151 const struct ovsdb_idl_column
*column
;
2152 const struct ovsdb_datum
*datum
;
2155 /* Special case for obtaining the UUID of a row. We can't just do this
2156 * through parse_column_key_value() below since it returns a "struct
2157 * ovsdb_idl_column" and the UUID column doesn't have one. */
2158 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2159 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2160 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2164 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2165 &column
, &key_string
,
2166 NULL
, NULL
, 0, NULL
));
2168 datum
= ovsdb_idl_read(row
, column
);
2170 union ovsdb_atom key
;
2173 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2174 vsctl_fatal("cannot specify key to get for non-map column %s",
2178 die_if_error(ovsdb_atom_from_string(&key
,
2180 key_string
, ctx
->symtab
));
2182 idx
= ovsdb_datum_find_key(datum
, &key
,
2183 column
->type
.key
.type
);
2184 if (idx
== UINT_MAX
) {
2186 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2187 key_string
, table
->class->name
, record_id
,
2191 ovsdb_atom_to_string(&datum
->values
[idx
],
2192 column
->type
.value
.type
, out
);
2194 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2196 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2198 ds_put_char(out
, '\n');
2205 list_record(const struct vsctl_table_class
*table
,
2206 const struct ovsdb_idl_row
*row
, struct ds
*out
)
2210 ds_put_format(out
, "%-20s: "UUID_FMT
"\n", "_uuid",
2211 UUID_ARGS(&row
->uuid
));
2212 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2213 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2214 const struct ovsdb_datum
*datum
;
2216 datum
= ovsdb_idl_read(row
, column
);
2218 ds_put_format(out
, "%-20s: ", column
->name
);
2219 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2220 ds_put_char(out
, '\n');
2225 cmd_list(struct vsctl_context
*ctx
)
2227 const char *table_name
= ctx
->argv
[1];
2228 const struct vsctl_table_class
*table
;
2229 struct ds
*out
= &ctx
->output
;
2232 table
= get_table(table_name
);
2233 if (ctx
->argc
> 2) {
2234 for (i
= 2; i
< ctx
->argc
; i
++) {
2236 ds_put_char(out
, '\n');
2238 list_record(table
, must_get_row(ctx
, table
, ctx
->argv
[i
]), out
);
2241 const struct ovsdb_idl_row
*row
;
2244 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class), first
= true;
2246 row
= ovsdb_idl_next_row(row
), first
= false) {
2248 ds_put_char(out
, '\n');
2250 list_record(table
, row
, out
);
2256 set_column(const struct vsctl_table_class
*table
,
2257 const struct ovsdb_idl_row
*row
, const char *arg
,
2258 struct ovsdb_symbol_table
*symtab
)
2260 const struct ovsdb_idl_column
*column
;
2261 char *key_string
, *value_string
;
2264 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2265 NULL
, NULL
, 0, &value_string
);
2266 die_if_error(error
);
2267 if (!value_string
) {
2268 vsctl_fatal("%s: missing value", arg
);
2272 union ovsdb_atom key
, value
;
2273 struct ovsdb_datum datum
;
2275 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2276 vsctl_fatal("cannot specify key to set for non-map column %s",
2280 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2281 key_string
, symtab
));
2282 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2283 value_string
, symtab
));
2285 ovsdb_datum_init_empty(&datum
);
2286 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
2288 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2289 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2291 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
2292 &column
->type
, false);
2293 ovsdb_idl_txn_write(row
, column
, &datum
);
2295 struct ovsdb_datum datum
;
2297 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2298 value_string
, symtab
));
2299 ovsdb_idl_txn_write(row
, column
, &datum
);
2307 cmd_set(struct vsctl_context
*ctx
)
2309 const char *table_name
= ctx
->argv
[1];
2310 const char *record_id
= ctx
->argv
[2];
2311 const struct vsctl_table_class
*table
;
2312 const struct ovsdb_idl_row
*row
;
2315 table
= get_table(table_name
);
2316 row
= must_get_row(ctx
, table
, record_id
);
2317 for (i
= 3; i
< ctx
->argc
; i
++) {
2318 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2323 cmd_add(struct vsctl_context
*ctx
)
2325 const char *table_name
= ctx
->argv
[1];
2326 const char *record_id
= ctx
->argv
[2];
2327 const char *column_name
= ctx
->argv
[3];
2328 const struct vsctl_table_class
*table
;
2329 const struct ovsdb_idl_column
*column
;
2330 const struct ovsdb_idl_row
*row
;
2331 const struct ovsdb_type
*type
;
2332 struct ovsdb_datum old
;
2335 table
= get_table(table_name
);
2336 row
= must_get_row(ctx
, table
, record_id
);
2337 die_if_error(get_column(table
, column_name
, &column
));
2339 type
= &column
->type
;
2340 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2341 for (i
= 4; i
< ctx
->argc
; i
++) {
2342 struct ovsdb_type add_type
;
2343 struct ovsdb_datum add
;
2347 add_type
.n_max
= UINT_MAX
;
2348 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2350 ovsdb_datum_union(&old
, &add
, type
, false);
2351 ovsdb_datum_destroy(&add
, type
);
2353 if (old
.n
> type
->n_max
) {
2354 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2355 "table %s but the maximum number is %u",
2357 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2358 column
->name
, table
->class->name
, type
->n_max
);
2360 ovsdb_idl_txn_write(row
, column
, &old
);
2364 cmd_remove(struct vsctl_context
*ctx
)
2366 const char *table_name
= ctx
->argv
[1];
2367 const char *record_id
= ctx
->argv
[2];
2368 const char *column_name
= ctx
->argv
[3];
2369 const struct vsctl_table_class
*table
;
2370 const struct ovsdb_idl_column
*column
;
2371 const struct ovsdb_idl_row
*row
;
2372 const struct ovsdb_type
*type
;
2373 struct ovsdb_datum old
;
2376 table
= get_table(table_name
);
2377 row
= must_get_row(ctx
, table
, record_id
);
2378 die_if_error(get_column(table
, column_name
, &column
));
2380 type
= &column
->type
;
2381 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2382 for (i
= 4; i
< ctx
->argc
; i
++) {
2383 struct ovsdb_type rm_type
;
2384 struct ovsdb_datum rm
;
2389 rm_type
.n_max
= UINT_MAX
;
2390 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
2391 ctx
->argv
[i
], ctx
->symtab
);
2392 if (error
&& ovsdb_type_is_map(&rm_type
)) {
2394 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
2395 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
2396 ctx
->argv
[i
], ctx
->symtab
));
2398 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
2399 ovsdb_datum_destroy(&rm
, &rm_type
);
2401 if (old
.n
< type
->n_min
) {
2402 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2403 "table %s but the minimum number is %u",
2405 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2406 column
->name
, table
->class->name
, type
->n_min
);
2408 ovsdb_idl_txn_write(row
, column
, &old
);
2412 cmd_clear(struct vsctl_context
*ctx
)
2414 const char *table_name
= ctx
->argv
[1];
2415 const char *record_id
= ctx
->argv
[2];
2416 const struct vsctl_table_class
*table
;
2417 const struct ovsdb_idl_row
*row
;
2420 table
= get_table(table_name
);
2421 row
= must_get_row(ctx
, table
, record_id
);
2422 for (i
= 3; i
< ctx
->argc
; i
++) {
2423 const struct ovsdb_idl_column
*column
;
2424 const struct ovsdb_type
*type
;
2425 struct ovsdb_datum datum
;
2427 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
2429 type
= &column
->type
;
2430 if (type
->n_min
> 0) {
2431 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2432 "of table %s, which is not allowed to be empty",
2433 column
->name
, table
->class->name
);
2436 ovsdb_datum_init_empty(&datum
);
2437 ovsdb_idl_txn_write(row
, column
, &datum
);
2442 cmd_create(struct vsctl_context
*ctx
)
2444 const char *id
= shash_find_data(&ctx
->options
, "--id");
2445 const char *table_name
= ctx
->argv
[1];
2446 const struct vsctl_table_class
*table
;
2447 const struct ovsdb_idl_row
*row
;
2448 const struct uuid
*uuid
;
2452 struct ovsdb_symbol
*symbol
;
2455 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2458 symbol
= ovsdb_symbol_table_insert(ctx
->symtab
, id
);
2460 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2463 symbol
->used
= true;
2465 uuid
= &symbol
->uuid
;
2470 table
= get_table(table_name
);
2471 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
2472 for (i
= 2; i
< ctx
->argc
; i
++) {
2473 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2475 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
2478 /* This function may be used as the 'postprocess' function for commands that
2479 * insert new rows into the database. It expects that the command's 'run'
2480 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2481 * sole output. It replaces that output by the row's permanent UUID assigned
2482 * by the database server and appends a new-line.
2484 * Currently we use this only for "create", because the higher-level commands
2485 * are supposed to be independent of the actual structure of the vswitch
2488 post_create(struct vsctl_context
*ctx
)
2490 const struct uuid
*real
;
2493 uuid_from_string(&dummy
, ds_cstr(&ctx
->output
));
2494 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
2496 ds_clear(&ctx
->output
);
2497 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
2499 ds_put_char(&ctx
->output
, '\n');
2503 cmd_destroy(struct vsctl_context
*ctx
)
2505 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
2506 const char *table_name
= ctx
->argv
[1];
2507 const struct vsctl_table_class
*table
;
2510 table
= get_table(table_name
);
2511 for (i
= 2; i
< ctx
->argc
; i
++) {
2512 const struct ovsdb_idl_row
*row
;
2514 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
2516 ovsdb_idl_txn_delete(row
);
2522 is_condition_satified(const struct vsctl_table_class
*table
,
2523 const struct ovsdb_idl_row
*row
, const char *arg
,
2524 struct ovsdb_symbol_table
*symtab
)
2526 static const char *operators
[] = {
2527 "=", "!=", "<", ">", "<=", ">="
2530 const struct ovsdb_idl_column
*column
;
2531 const struct ovsdb_datum
*have_datum
;
2532 char *key_string
, *value_string
;
2533 const char *operator;
2538 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2539 &operator, operators
, ARRAY_SIZE(operators
),
2541 die_if_error(error
);
2542 if (!value_string
) {
2543 vsctl_fatal("%s: missing value", arg
);
2546 have_datum
= ovsdb_idl_read(row
, column
);
2548 union ovsdb_atom want_key
, want_value
;
2550 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2551 vsctl_fatal("cannot specify key to check for non-map column %s",
2555 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
2556 key_string
, symtab
));
2557 die_if_error(ovsdb_atom_from_string(&want_value
, &column
->type
.value
,
2558 value_string
, symtab
));
2560 idx
= ovsdb_datum_find_key(have_datum
,
2561 &want_key
, column
->type
.key
.type
);
2562 if (idx
!= UINT_MAX
) {
2563 cmp
= ovsdb_atom_compare_3way(&have_datum
->values
[idx
],
2565 column
->type
.value
.type
);
2568 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
2569 ovsdb_atom_destroy(&want_value
, column
->type
.value
.type
);
2571 struct ovsdb_datum want_datum
;
2573 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
2574 value_string
, symtab
));
2576 cmp
= ovsdb_datum_compare_3way(have_datum
, &want_datum
,
2578 ovsdb_datum_destroy(&want_datum
, &column
->type
);
2584 return (idx
== UINT_MAX
? false
2585 : !strcmp(operator, "=") ? cmp
== 0
2586 : !strcmp(operator, "!=") ? cmp
!= 0
2587 : !strcmp(operator, "<") ? cmp
< 0
2588 : !strcmp(operator, ">") ? cmp
> 0
2589 : !strcmp(operator, "<=") ? cmp
<= 0
2590 : !strcmp(operator, ">=") ? cmp
>= 0
2595 cmd_wait_until(struct vsctl_context
*ctx
)
2597 const char *table_name
= ctx
->argv
[1];
2598 const char *record_id
= ctx
->argv
[2];
2599 const struct vsctl_table_class
*table
;
2600 const struct ovsdb_idl_row
*row
;
2603 table
= get_table(table_name
);
2605 row
= get_row(ctx
, table
, record_id
);
2607 ctx
->try_again
= true;
2611 for (i
= 3; i
< ctx
->argc
; i
++) {
2612 if (!is_condition_satified(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
2613 ctx
->try_again
= true;
2619 static struct json
*
2620 where_uuid_equals(const struct uuid
*uuid
)
2623 json_array_create_1(
2624 json_array_create_3(
2625 json_string_create("_uuid"),
2626 json_string_create("=="),
2627 json_array_create_2(
2628 json_string_create("uuid"),
2629 json_string_create_nocopy(
2630 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
2634 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
2635 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2636 const struct ovsrec_open_vswitch
*ovs
,
2637 struct ovsdb_symbol_table
*symtab
)
2639 ctx
->argc
= command
->argc
;
2640 ctx
->argv
= command
->argv
;
2641 ctx
->options
= command
->options
;
2643 ds_swap(&ctx
->output
, &command
->output
);
2647 ctx
->symtab
= symtab
;
2649 ctx
->try_again
= false;
2653 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
2655 ds_swap(&ctx
->output
, &command
->output
);
2659 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
2660 struct ovsdb_idl
*idl
)
2662 struct ovsdb_idl_txn
*txn
;
2663 const struct ovsrec_open_vswitch
*ovs
;
2664 enum ovsdb_idl_txn_status status
;
2665 struct ovsdb_symbol_table
*symtab
;
2667 struct vsctl_command
*c
;
2668 int64_t next_cfg
= 0;
2671 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2673 ovsdb_idl_txn_set_dry_run(txn
);
2676 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2678 ovs
= ovsrec_open_vswitch_first(idl
);
2680 /* XXX add verification that table is empty */
2681 ovs
= ovsrec_open_vswitch_insert(txn
);
2684 if (wait_for_reload
) {
2685 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
2686 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
2687 json_destroy(where
);
2690 symtab
= ovsdb_symbol_table_create();
2691 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2692 ds_init(&c
->output
);
2694 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2695 struct vsctl_context ctx
;
2697 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2698 (c
->syntax
->run
)(&ctx
);
2699 vsctl_context_done(&ctx
, c
);
2701 if (ctx
.try_again
) {
2706 status
= ovsdb_idl_txn_commit_block(txn
);
2707 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2708 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2710 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2711 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2712 if (c
->syntax
->postprocess
) {
2713 struct vsctl_context ctx
;
2715 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2716 (c
->syntax
->postprocess
)(&ctx
);
2717 vsctl_context_done(&ctx
, c
);
2721 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2722 ovsdb_idl_txn_destroy(txn
);
2725 unused
= ovsdb_symbol_table_find_unused(symtab
);
2727 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2728 "with \"-- --id=%s create ...\")", unused
, unused
);
2732 case TXN_INCOMPLETE
:
2736 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2737 vsctl_fatal("transaction aborted");
2747 vsctl_fatal("transaction error: %s", error
);
2754 ovsdb_symbol_table_destroy(symtab
);
2756 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2757 struct ds
*ds
= &c
->output
;
2758 struct shash_node
*node
;
2764 for (j
= 0; j
< ds
->length
; j
++) {
2765 int c
= ds
->string
[j
];
2768 fputs("\\n", stdout
);
2772 fputs("\\\\", stdout
);
2781 fputs(ds_cstr(ds
), stdout
);
2783 ds_destroy(&c
->output
);
2785 SHASH_FOR_EACH (node
, &c
->options
) {
2788 shash_destroy(&c
->options
);
2792 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2794 const struct ovsrec_open_vswitch
*ovs
;
2797 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2798 if (ovs
->cur_cfg
>= next_cfg
) {
2802 ovsdb_idl_wait(idl
);
2807 ovsdb_idl_destroy(idl
);
2812 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2813 * resources and return so that the caller can try again. */
2814 ovsdb_idl_txn_abort(txn
);
2815 ovsdb_idl_txn_destroy(txn
);
2816 ovsdb_symbol_table_destroy(symtab
);
2817 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2818 ds_destroy(&c
->output
);
2823 static const struct vsctl_command_syntax all_commands
[] = {
2824 /* Open vSwitch commands. */
2825 {"init", 0, 0, cmd_init
, NULL
, ""},
2827 /* Bridge commands. */
2828 {"add-br", 1, 3, cmd_add_br
, NULL
, "--may-exist"},
2829 {"del-br", 1, 1, cmd_del_br
, NULL
, "--if-exists"},
2830 {"list-br", 0, 0, cmd_list_br
, NULL
, ""},
2831 {"br-exists", 1, 1, cmd_br_exists
, NULL
, ""},
2832 {"br-to-vlan", 1, 1, cmd_br_to_vlan
, NULL
, ""},
2833 {"br-to-parent", 1, 1, cmd_br_to_parent
, NULL
, ""},
2834 {"br-set-external-id", 2, 3, cmd_br_set_external_id
, NULL
, ""},
2835 {"br-get-external-id", 1, 2, cmd_br_get_external_id
, NULL
, ""},
2837 /* Port commands. */
2838 {"list-ports", 1, 1, cmd_list_ports
, NULL
, ""},
2839 {"add-port", 2, INT_MAX
, cmd_add_port
, NULL
, "--may-exist"},
2840 {"add-bond", 4, INT_MAX
, cmd_add_bond
, NULL
, "--may-exist,--fake-iface"},
2841 {"del-port", 1, 2, cmd_del_port
, NULL
, "--if-exists,--with-iface"},
2842 {"port-to-br", 1, 1, cmd_port_to_br
, NULL
, ""},
2844 /* Interface commands. */
2845 {"list-ifaces", 1, 1, cmd_list_ifaces
, NULL
, ""},
2846 {"iface-to-br", 1, 1, cmd_iface_to_br
, NULL
, ""},
2848 /* Controller commands. */
2849 {"get-controller", 1, 1, cmd_get_controller
, NULL
, ""},
2850 {"del-controller", 1, 1, cmd_del_controller
, NULL
, ""},
2851 {"set-controller", 1, INT_MAX
, cmd_set_controller
, NULL
, ""},
2852 {"get-fail-mode", 1, 1, cmd_get_fail_mode
, NULL
, ""},
2853 {"del-fail-mode", 1, 1, cmd_del_fail_mode
, NULL
, ""},
2854 {"set-fail-mode", 2, 2, cmd_set_fail_mode
, NULL
, ""},
2857 {"get-ssl", 0, 0, cmd_get_ssl
, NULL
, ""},
2858 {"del-ssl", 0, 0, cmd_del_ssl
, NULL
, ""},
2859 {"set-ssl", 3, 3, cmd_set_ssl
, NULL
, "--bootstrap"},
2861 /* Switch commands. */
2862 {"emer-reset", 0, 0, cmd_emer_reset
, NULL
, ""},
2864 /* Parameter commands. */
2865 {"get", 3, INT_MAX
, cmd_get
, NULL
, "--if-exists"},
2866 {"list", 1, INT_MAX
, cmd_list
, NULL
, ""},
2867 {"set", 3, INT_MAX
, cmd_set
, NULL
, ""},
2868 {"add", 4, INT_MAX
, cmd_add
, NULL
, ""},
2869 {"remove", 4, INT_MAX
, cmd_remove
, NULL
, ""},
2870 {"clear", 3, INT_MAX
, cmd_clear
, NULL
, ""},
2871 {"create", 2, INT_MAX
, cmd_create
, post_create
, "--id="},
2872 {"destroy", 1, INT_MAX
, cmd_destroy
, NULL
, "--if-exists"},
2873 {"wait-until", 2, INT_MAX
, cmd_wait_until
, NULL
, ""},
2875 {NULL
, 0, 0, NULL
, NULL
, NULL
},