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
;
122 struct vsctl_command
*commands
;
126 set_program_name(argv
[0]);
127 signal(SIGPIPE
, SIG_IGN
);
130 vlog_set_levels(VLM_ANY_MODULE
, VLF_CONSOLE
, VLL_WARN
);
131 vlog_set_levels(VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
134 /* Log our arguments. This is often valuable for debugging systems. */
135 args
= process_escape_args(argv
);
136 VLOG_INFO("Called as %s", args
);
138 /* Parse command line. */
139 parse_options(argc
, argv
);
140 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
146 /* Now execute the commands. */
147 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
);
149 if (ovsdb_idl_run(idl
)) {
150 do_vsctl(args
, commands
, n_commands
, idl
);
159 parse_options(int argc
, char *argv
[])
162 OPT_DB
= UCHAR_MAX
+ 1,
170 static struct option long_options
[] = {
171 {"db", required_argument
, 0, OPT_DB
},
172 {"no-syslog", no_argument
, 0, OPT_NO_SYSLOG
},
173 {"no-wait", no_argument
, 0, OPT_NO_WAIT
},
174 {"dry-run", no_argument
, 0, OPT_DRY_RUN
},
175 {"oneline", no_argument
, 0, OPT_ONELINE
},
176 {"timeout", required_argument
, 0, 't'},
177 {"help", no_argument
, 0, 'h'},
178 {"version", no_argument
, 0, 'V'},
181 STREAM_SSL_LONG_OPTIONS
182 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
191 c
= getopt_long(argc
, argv
, "+v::hVt:", long_options
, NULL
);
206 vlog_set_levels(VLM_vsctl
, VLF_SYSLOG
, VLL_WARN
);
210 wait_for_reload
= false;
221 OVS_PRINT_VERSION(0, 0);
225 timeout
= strtoul(optarg
, NULL
, 10);
227 vsctl_fatal("value %s on -t or --timeout is invalid",
235 STREAM_SSL_OPTION_HANDLERS
237 case OPT_PEER_CA_CERT
:
238 stream_ssl_set_peer_ca_cert_file(optarg
);
255 static struct vsctl_command
*
256 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
258 struct vsctl_command
*commands
;
259 size_t n_commands
, allocated_commands
;
263 n_commands
= allocated_commands
= 0;
265 for (start
= i
= 0; i
<= argc
; i
++) {
266 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
268 if (n_commands
>= allocated_commands
) {
269 struct vsctl_command
*c
;
271 commands
= x2nrealloc(commands
, &allocated_commands
,
273 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
274 shash_moved(&c
->options
);
277 parse_command(i
- start
, &argv
[start
],
278 &commands
[n_commands
++]);
284 vsctl_fatal("missing command name (use --help for help)");
286 *n_commandsp
= n_commands
;
291 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
293 const struct vsctl_command_syntax
*p
;
296 shash_init(&command
->options
);
297 for (i
= 0; i
< argc
; i
++) {
298 const char *option
= argv
[i
];
302 if (option
[0] != '-') {
306 equals
= strchr(option
, '=');
308 key
= xmemdup0(option
, equals
- option
);
309 value
= xstrdup(equals
+ 1);
311 key
= xstrdup(option
);
315 if (shash_find(&command
->options
, key
)) {
316 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
318 shash_add_nocopy(&command
->options
, key
, value
);
321 vsctl_fatal("missing command name");
324 for (p
= all_commands
; p
->name
; p
++) {
325 if (!strcmp(p
->name
, argv
[i
])) {
326 struct shash_node
*node
;
329 SHASH_FOR_EACH (node
, &command
->options
) {
330 const char *s
= strstr(p
->options
, node
->name
);
331 int end
= s
? s
[strlen(node
->name
)] : EOF
;
333 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
334 vsctl_fatal("'%s' command has no '%s' option",
335 argv
[i
], node
->name
);
337 if ((end
== '=') != (node
->data
!= NULL
)) {
339 vsctl_fatal("missing argument to '%s' option on '%s' "
340 "command", node
->name
, argv
[i
]);
342 vsctl_fatal("'%s' option on '%s' does not accept an "
343 "argument", node
->name
, argv
[i
]);
348 n_arg
= argc
- i
- 1;
349 if (n_arg
< p
->min_args
) {
350 vsctl_fatal("'%s' command requires at least %d arguments",
351 p
->name
, p
->min_args
);
352 } else if (n_arg
> p
->max_args
) {
355 for (j
= i
+ 1; j
< argc
; j
++) {
356 if (argv
[j
][0] == '-') {
357 vsctl_fatal("'%s' command takes at most %d arguments "
358 "(note that options must precede command "
359 "names and follow a \"--\" argument)",
360 p
->name
, p
->max_args
);
364 vsctl_fatal("'%s' command takes at most %d arguments",
365 p
->name
, p
->max_args
);
368 command
->argc
= n_arg
+ 1;
369 command
->argv
= &argv
[i
];
375 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
379 vsctl_fatal(const char *format
, ...)
384 va_start(args
, format
);
385 message
= xvasprintf(format
, args
);
388 vlog_set_levels(VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
389 VLOG_ERR("%s", message
);
390 ovs_error(0, "%s", message
);
391 vsctl_exit(EXIT_FAILURE
);
394 /* Frees the current transaction and the underlying IDL and then calls
397 * Freeing the transaction and the IDL is not strictly necessary, but it makes
398 * for a clean memory leak report from valgrind in the normal case. That makes
399 * it easier to notice real memory leaks. */
401 vsctl_exit(int status
)
404 ovsdb_idl_txn_abort(the_idl_txn
);
405 ovsdb_idl_txn_destroy(the_idl_txn
);
407 ovsdb_idl_destroy(the_idl
);
415 %s: ovs-vswitchd management utility\n\
416 usage: %s [OPTIONS] COMMAND [ARG...]\n\
419 add-br BRIDGE create a new bridge named BRIDGE\n\
420 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
421 del-br BRIDGE delete BRIDGE and all of its ports\n\
422 list-br print the names of all the bridges\n\
423 br-exists BRIDGE test whether BRIDGE exists\n\
424 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
425 br-to-parent BRIDGE print the parent of BRIDGE\n\
426 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
427 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
428 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
429 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
432 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
433 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
434 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
435 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
436 port-to-br PORT print name of bridge that contains PORT\n\
437 A bond is considered to be a single port.\n\
439 Interface commands (a bond consists of multiple interfaces):\n\
440 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
441 iface-to-br IFACE print name of bridge that contains IFACE\n\
443 Controller commands:\n\
444 get-controller [BRIDGE] print the controller for BRIDGE\n\
445 del-controller [BRIDGE] delete the controller for BRIDGE\n\
446 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
447 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
448 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
449 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
452 get-ssl print the SSL configuration\n\
453 del-ssl delete the SSL configuration\n\
454 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
457 emer-reset reset switch to known good state\n\
459 Database commands:\n\
460 list TBL [REC] list RECord (or all records) in TBL\n\
461 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
462 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
463 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
464 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
465 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
466 create TBL COL[:KEY]=VALUE create and initialize new record\n\
467 destroy TBL REC delete RECord from TBL\n\
468 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
469 Potentially unsafe database commands require --force option.\n\
472 --db=DATABASE connect to DATABASE\n\
474 --oneline print exactly one line of output per command\n",
475 program_name
, program_name
, default_db());
479 -h, --help display this help message\n\
480 -V, --version display version information\n");
489 def
= xasprintf("unix:%s/db.sock", ovs_rundir
);
494 struct vsctl_context
{
498 struct shash options
;
500 /* Modifiable state. */
502 struct ovsdb_idl
*idl
;
503 struct ovsdb_idl_txn
*txn
;
504 struct ovsdb_symbol_table
*symtab
;
505 const struct ovsrec_open_vswitch
*ovs
;
507 /* A command may set this member to true if some prerequisite is not met
508 * and the caller should wait for something to change and then retry. */
512 struct vsctl_bridge
{
513 struct ovsrec_bridge
*br_cfg
;
515 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
;
535 struct ovsrec_controller
**ctrl
;
540 vsctl_context_to_string(const struct vsctl_context
*ctx
)
542 const struct shash_node
*node
;
548 SHASH_FOR_EACH (node
, &ctx
->options
) {
549 svec_add(&words
, node
->name
);
551 for (i
= 0; i
< ctx
->argc
; i
++) {
552 svec_add(&words
, ctx
->argv
[i
]);
554 svec_terminate(&words
);
556 s
= process_escape_args(words
.names
);
558 svec_destroy(&words
);
563 static struct vsctl_bridge
*
564 add_bridge(struct vsctl_info
*b
,
565 struct ovsrec_bridge
*br_cfg
, const char *name
,
566 struct vsctl_bridge
*parent
, int vlan
)
568 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
570 br
->name
= xstrdup(name
);
574 br
->ctrl
= parent
->br_cfg
->controller
;
575 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
577 br
->ctrl
= br_cfg
->controller
;
578 br
->n_ctrl
= br_cfg
->n_controller
;
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 info
->ctrl
= ovs
->controller
;
635 info
->n_ctrl
= ovs
->n_controller
;
637 shash_init(&bridges
);
639 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
640 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
641 struct vsctl_bridge
*br
;
644 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
645 VLOG_WARN("%s: database contains duplicate bridge name",
649 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
654 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
655 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
657 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
658 VLOG_WARN("%s: database contains duplicate port name",
663 if (port_is_fake_bridge(port_cfg
)
664 && shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
665 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
669 shash_destroy(&bridges
);
670 shash_destroy(&ports
);
672 shash_init(&bridges
);
674 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
675 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
676 struct vsctl_bridge
*br
;
679 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
682 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
683 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
684 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
685 struct vsctl_port
*port
;
688 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
692 if (port_is_fake_bridge(port_cfg
)
693 && !shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
697 port
= xmalloc(sizeof *port
);
698 port
->port_cfg
= port_cfg
;
700 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
701 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
708 shash_add(&info
->ports
, port_cfg
->name
, port
);
710 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
711 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
712 struct vsctl_iface
*iface
;
714 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
715 VLOG_WARN("%s: database contains duplicate interface name",
720 iface
= xmalloc(sizeof *iface
);
721 iface
->iface_cfg
= iface_cfg
;
723 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
727 shash_destroy(&bridges
);
728 shash_destroy(&ports
);
732 check_conflicts(struct vsctl_info
*info
, const char *name
,
735 struct vsctl_iface
*iface
;
736 struct vsctl_port
*port
;
738 if (shash_find(&info
->bridges
, name
)) {
739 vsctl_fatal("%s because a bridge named %s already exists",
743 port
= shash_find_data(&info
->ports
, name
);
745 vsctl_fatal("%s because a port named %s already exists on "
746 "bridge %s", msg
, name
, port
->bridge
->name
);
749 iface
= shash_find_data(&info
->ifaces
, name
);
751 vsctl_fatal("%s because an interface named %s already exists "
752 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
758 static struct vsctl_bridge
*
759 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
761 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
762 if (must_exist
&& !br
) {
763 vsctl_fatal("no bridge named %s", name
);
768 static struct vsctl_bridge
*
769 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
771 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
772 if (br
&& br
->parent
) {
773 vsctl_fatal("%s is a fake bridge", name
);
778 static struct vsctl_port
*
779 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
781 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
782 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
785 if (must_exist
&& !port
) {
786 vsctl_fatal("no port named %s", name
);
791 static struct vsctl_iface
*
792 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
794 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
795 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
798 if (must_exist
&& !iface
) {
799 vsctl_fatal("no interface named %s", name
);
805 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
807 struct ovsrec_port
**ports
;
810 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
811 for (i
= 0; i
< br
->n_ports
; i
++) {
812 ports
[i
] = br
->ports
[i
];
814 ports
[br
->n_ports
] = port
;
815 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
820 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
822 struct ovsrec_port
**ports
;
825 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
826 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
827 if (br
->ports
[i
] != port
) {
828 ports
[n
++] = br
->ports
[i
];
831 ovsrec_bridge_set_ports(br
, ports
, n
);
836 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
837 struct ovsrec_bridge
*bridge
)
839 struct ovsrec_bridge
**bridges
;
842 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
843 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
844 bridges
[i
] = ovs
->bridges
[i
];
846 bridges
[ovs
->n_bridges
] = bridge
;
847 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
852 ovs_delete_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
);
859 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
860 if (ovs
->bridges
[i
] != bridge
) {
861 bridges
[n
++] = ovs
->bridges
[i
];
864 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
869 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
874 cmd_emer_reset(struct vsctl_context
*ctx
)
876 const struct ovsdb_idl
*idl
= ctx
->idl
;
877 const struct ovsrec_bridge
*br
;
878 const struct ovsrec_port
*port
;
879 const struct ovsrec_interface
*iface
;
880 const struct ovsrec_mirror
*mirror
, *next_mirror
;
881 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
882 const struct ovsrec_netflow
*nf
, *next_nf
;
883 const struct ovsrec_ssl
*ssl
, *next_ssl
;
884 const struct ovsrec_sflow
*sflow
, *next_sflow
;
887 /* Reset the Open_vSwitch table. */
888 ovsrec_open_vswitch_set_managers(ctx
->ovs
, NULL
, 0);
889 ovsrec_open_vswitch_set_controller(ctx
->ovs
, NULL
, 0);
890 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
892 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
894 char *hw_key
= "hwaddr";
897 ovsrec_bridge_set_controller(br
, NULL
, 0);
898 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
899 ovsrec_bridge_set_netflow(br
, NULL
);
900 ovsrec_bridge_set_sflow(br
, NULL
);
901 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
903 /* We only want to save the "hwaddr" key from other_config. */
904 for (i
=0; i
< br
->n_other_config
; i
++) {
905 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
906 hw_val
= br
->value_other_config
[i
];
911 char *val
= xstrdup(hw_val
);
912 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
915 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
919 OVSREC_PORT_FOR_EACH (port
, idl
) {
920 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
923 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
924 /* xxx What do we do about gre/patch devices created by mgr? */
926 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
927 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
930 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
931 ovsrec_mirror_delete(mirror
);
934 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
935 ovsrec_controller_delete(ctrl
);
938 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
939 ovsrec_netflow_delete(nf
);
942 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
943 ovsrec_ssl_delete(ssl
);
946 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
947 ovsrec_sflow_delete(sflow
);
952 cmd_add_br(struct vsctl_context
*ctx
)
954 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
955 const char *br_name
, *parent_name
;
956 struct vsctl_info info
;
959 br_name
= ctx
->argv
[1];
960 if (ctx
->argc
== 2) {
963 } else if (ctx
->argc
== 4) {
964 parent_name
= ctx
->argv
[2];
965 vlan
= atoi(ctx
->argv
[3]);
966 if (vlan
< 1 || vlan
> 4095) {
967 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
970 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
974 get_info(ctx
->ovs
, &info
);
976 struct vsctl_bridge
*br
;
978 br
= find_bridge(&info
, br_name
, false);
982 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
983 "a VLAN bridge for VLAN %d",
984 br_name
, br_name
, br
->vlan
);
988 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
989 "is not a VLAN bridge",
990 br_name
, parent_name
, vlan
, br_name
);
991 } else if (strcmp(br
->parent
->name
, parent_name
)) {
992 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
993 "has the wrong parent %s",
994 br_name
, parent_name
, vlan
,
995 br_name
, br
->parent
->name
);
996 } else if (br
->vlan
!= vlan
) {
997 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
998 "is a VLAN bridge for the wrong VLAN %d",
999 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1005 check_conflicts(&info
, br_name
,
1006 xasprintf("cannot create a bridge named %s", br_name
));
1009 struct ovsrec_port
*port
;
1010 struct ovsrec_interface
*iface
;
1011 struct ovsrec_bridge
*br
;
1013 iface
= ovsrec_interface_insert(ctx
->txn
);
1014 ovsrec_interface_set_name(iface
, br_name
);
1016 port
= ovsrec_port_insert(ctx
->txn
);
1017 ovsrec_port_set_name(port
, br_name
);
1018 ovsrec_port_set_interfaces(port
, &iface
, 1);
1020 br
= ovsrec_bridge_insert(ctx
->txn
);
1021 ovsrec_bridge_set_name(br
, br_name
);
1022 ovsrec_bridge_set_ports(br
, &port
, 1);
1024 ovs_insert_bridge(ctx
->ovs
, br
);
1026 struct vsctl_bridge
*parent
;
1027 struct ovsrec_port
*port
;
1028 struct ovsrec_interface
*iface
;
1029 struct ovsrec_bridge
*br
;
1032 parent
= find_bridge(&info
, parent_name
, false);
1033 if (parent
&& parent
->vlan
) {
1034 vsctl_fatal("cannot create bridge with fake bridge as parent");
1037 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1039 br
= parent
->br_cfg
;
1041 iface
= ovsrec_interface_insert(ctx
->txn
);
1042 ovsrec_interface_set_name(iface
, br_name
);
1043 ovsrec_interface_set_type(iface
, "internal");
1045 port
= ovsrec_port_insert(ctx
->txn
);
1046 ovsrec_port_set_name(port
, br_name
);
1047 ovsrec_port_set_interfaces(port
, &iface
, 1);
1048 ovsrec_port_set_fake_bridge(port
, true);
1049 ovsrec_port_set_tag(port
, &tag
, 1);
1051 bridge_insert_port(br
, port
);
1058 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1060 struct shash_node
*node
;
1062 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1063 struct vsctl_iface
*iface
= node
->data
;
1064 if (iface
->port
== port
) {
1065 ovsrec_interface_delete(iface
->iface_cfg
);
1068 ovsrec_port_delete(port
->port_cfg
);
1070 bridge_delete_port((port
->bridge
->parent
1071 ? port
->bridge
->parent
->br_cfg
1072 : port
->bridge
->br_cfg
), port
->port_cfg
);
1076 cmd_del_br(struct vsctl_context
*ctx
)
1078 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1079 struct vsctl_bridge
*bridge
;
1080 struct vsctl_info info
;
1082 get_info(ctx
->ovs
, &info
);
1083 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1085 struct shash_node
*node
;
1087 SHASH_FOR_EACH (node
, &info
.ports
) {
1088 struct vsctl_port
*port
= node
->data
;
1089 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1090 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1091 del_port(&info
, port
);
1094 if (bridge
->br_cfg
) {
1095 ovsrec_bridge_delete(bridge
->br_cfg
);
1096 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1103 output_sorted(struct svec
*svec
, struct ds
*output
)
1109 SVEC_FOR_EACH (i
, name
, svec
) {
1110 ds_put_format(output
, "%s\n", name
);
1115 cmd_list_br(struct vsctl_context
*ctx
)
1117 struct shash_node
*node
;
1118 struct vsctl_info info
;
1119 struct svec bridges
;
1121 get_info(ctx
->ovs
, &info
);
1123 svec_init(&bridges
);
1124 SHASH_FOR_EACH (node
, &info
.bridges
) {
1125 struct vsctl_bridge
*br
= node
->data
;
1126 svec_add(&bridges
, br
->name
);
1128 output_sorted(&bridges
, &ctx
->output
);
1129 svec_destroy(&bridges
);
1135 cmd_br_exists(struct vsctl_context
*ctx
)
1137 struct vsctl_info info
;
1139 get_info(ctx
->ovs
, &info
);
1140 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1146 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1147 * equals 'a', false otherwise. */
1149 key_matches(const char *a
,
1150 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1152 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1156 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1157 char *key
, char *value
,
1158 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1165 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1166 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1168 for (i
= 0; i
< old_n
; i
++) {
1169 if (strcmp(key
, old_keys
[i
])) {
1170 new_keys
[new_n
] = old_keys
[i
];
1171 new_values
[new_n
] = old_values
[i
];
1176 new_keys
[new_n
] = key
;
1177 new_values
[new_n
] = value
;
1180 *new_keysp
= new_keys
;
1181 *new_valuesp
= new_values
;
1186 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1188 struct vsctl_info info
;
1189 struct vsctl_bridge
*bridge
;
1190 char **keys
, **values
;
1193 get_info(ctx
->ovs
, &info
);
1194 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1195 if (bridge
->br_cfg
) {
1196 set_external_id(bridge
->br_cfg
->key_external_ids
,
1197 bridge
->br_cfg
->value_external_ids
,
1198 bridge
->br_cfg
->n_external_ids
,
1199 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1200 &keys
, &values
, &n
);
1201 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1203 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1204 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1205 set_external_id(port
->port_cfg
->key_external_ids
,
1206 port
->port_cfg
->value_external_ids
,
1207 port
->port_cfg
->n_external_ids
,
1208 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1209 &keys
, &values
, &n
);
1210 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1220 get_external_id(char **keys
, char **values
, size_t n
,
1221 const char *prefix
, const char *key
,
1224 size_t prefix_len
= strlen(prefix
);
1229 for (i
= 0; i
< n
; i
++) {
1230 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1231 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1232 keys
[i
] + prefix_len
, values
[i
]));
1233 } else if (key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1234 svec_add(&svec
, values
[i
]);
1238 output_sorted(&svec
, output
);
1239 svec_destroy(&svec
);
1243 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1245 struct vsctl_info info
;
1246 struct vsctl_bridge
*bridge
;
1248 get_info(ctx
->ovs
, &info
);
1249 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1250 if (bridge
->br_cfg
) {
1251 get_external_id(bridge
->br_cfg
->key_external_ids
,
1252 bridge
->br_cfg
->value_external_ids
,
1253 bridge
->br_cfg
->n_external_ids
,
1254 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1257 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1258 get_external_id(port
->port_cfg
->key_external_ids
,
1259 port
->port_cfg
->value_external_ids
,
1260 port
->port_cfg
->n_external_ids
,
1261 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1268 cmd_list_ports(struct vsctl_context
*ctx
)
1270 struct vsctl_bridge
*br
;
1271 struct shash_node
*node
;
1272 struct vsctl_info info
;
1275 get_info(ctx
->ovs
, &info
);
1276 br
= find_bridge(&info
, ctx
->argv
[1], true);
1279 SHASH_FOR_EACH (node
, &info
.ports
) {
1280 struct vsctl_port
*port
= node
->data
;
1282 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1283 svec_add(&ports
, port
->port_cfg
->name
);
1286 output_sorted(&ports
, &ctx
->output
);
1287 svec_destroy(&ports
);
1293 add_port(struct vsctl_context
*ctx
,
1294 const char *br_name
, const char *port_name
,
1295 bool may_exist
, bool fake_iface
,
1296 char *iface_names
[], int n_ifaces
,
1297 char *settings
[], int n_settings
)
1299 struct vsctl_info info
;
1300 struct vsctl_bridge
*bridge
;
1301 struct ovsrec_interface
**ifaces
;
1302 struct ovsrec_port
*port
;
1305 get_info(ctx
->ovs
, &info
);
1307 struct vsctl_port
*port
;
1309 port
= find_port(&info
, port_name
, false);
1311 struct svec want_names
, have_names
;
1314 svec_init(&want_names
);
1315 for (i
= 0; i
< n_ifaces
; i
++) {
1316 svec_add(&want_names
, iface_names
[i
]);
1318 svec_sort(&want_names
);
1320 svec_init(&have_names
);
1321 for (i
= 0; i
< port
->port_cfg
->n_interfaces
; i
++) {
1322 svec_add(&have_names
, port
->port_cfg
->interfaces
[i
]->name
);
1324 svec_sort(&have_names
);
1326 if (strcmp(port
->bridge
->name
, br_name
)) {
1327 char *command
= vsctl_context_to_string(ctx
);
1328 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1329 command
, port_name
, port
->bridge
->name
);
1332 if (!svec_equal(&want_names
, &have_names
)) {
1333 char *have_names_string
= svec_join(&have_names
, ", ", "");
1334 char *command
= vsctl_context_to_string(ctx
);
1336 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1337 command
, port_name
, have_names_string
);
1340 svec_destroy(&want_names
);
1341 svec_destroy(&have_names
);
1346 check_conflicts(&info
, port_name
,
1347 xasprintf("cannot create a port named %s", port_name
));
1348 for (i
= 0; i
< n_ifaces
; i
++) {
1349 check_conflicts(&info
, iface_names
[i
],
1350 xasprintf("cannot create an interface named %s",
1353 bridge
= find_bridge(&info
, br_name
, true);
1355 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1356 for (i
= 0; i
< n_ifaces
; i
++) {
1357 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1358 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1361 port
= ovsrec_port_insert(ctx
->txn
);
1362 ovsrec_port_set_name(port
, port_name
);
1363 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1364 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1368 int64_t tag
= bridge
->vlan
;
1369 ovsrec_port_set_tag(port
, &tag
, 1);
1372 for (i
= 0; i
< n_settings
; i
++) {
1373 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1377 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1378 : bridge
->br_cfg
), port
);
1384 cmd_add_port(struct vsctl_context
*ctx
)
1386 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1388 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1389 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1393 cmd_add_bond(struct vsctl_context
*ctx
)
1395 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1396 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1400 n_ifaces
= ctx
->argc
- 3;
1401 for (i
= 3; i
< ctx
->argc
; i
++) {
1402 if (strchr(ctx
->argv
[i
], '=')) {
1408 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1409 "%d were specified", n_ifaces
);
1412 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1413 &ctx
->argv
[3], n_ifaces
,
1414 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1418 cmd_del_port(struct vsctl_context
*ctx
)
1420 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1421 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1422 struct vsctl_port
*port
;
1423 struct vsctl_info info
;
1425 get_info(ctx
->ovs
, &info
);
1427 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1429 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1430 struct vsctl_iface
*iface
;
1432 port
= find_port(&info
, target
, false);
1434 iface
= find_iface(&info
, target
, false);
1439 if (must_exist
&& !port
) {
1440 vsctl_fatal("no port or interface named %s", target
);
1445 if (ctx
->argc
== 3) {
1446 struct vsctl_bridge
*bridge
;
1448 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1449 if (port
->bridge
!= bridge
) {
1450 if (port
->bridge
->parent
== bridge
) {
1451 vsctl_fatal("bridge %s does not have a port %s (although "
1452 "its parent bridge %s does)",
1453 ctx
->argv
[1], ctx
->argv
[2],
1454 bridge
->parent
->name
);
1456 vsctl_fatal("bridge %s does not have a port %s",
1457 ctx
->argv
[1], ctx
->argv
[2]);
1462 del_port(&info
, port
);
1469 cmd_port_to_br(struct vsctl_context
*ctx
)
1471 struct vsctl_port
*port
;
1472 struct vsctl_info info
;
1474 get_info(ctx
->ovs
, &info
);
1475 port
= find_port(&info
, ctx
->argv
[1], true);
1476 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1481 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1483 struct vsctl_bridge
*bridge
;
1484 struct vsctl_info info
;
1486 get_info(ctx
->ovs
, &info
);
1487 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1488 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1493 cmd_br_to_parent(struct vsctl_context
*ctx
)
1495 struct vsctl_bridge
*bridge
;
1496 struct vsctl_info info
;
1498 get_info(ctx
->ovs
, &info
);
1499 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1500 if (bridge
->parent
) {
1501 bridge
= bridge
->parent
;
1503 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1508 cmd_list_ifaces(struct vsctl_context
*ctx
)
1510 struct vsctl_bridge
*br
;
1511 struct shash_node
*node
;
1512 struct vsctl_info info
;
1515 get_info(ctx
->ovs
, &info
);
1516 br
= find_bridge(&info
, ctx
->argv
[1], true);
1519 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1520 struct vsctl_iface
*iface
= node
->data
;
1522 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1523 && br
== iface
->port
->bridge
) {
1524 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1527 output_sorted(&ifaces
, &ctx
->output
);
1528 svec_destroy(&ifaces
);
1534 cmd_iface_to_br(struct vsctl_context
*ctx
)
1536 struct vsctl_iface
*iface
;
1537 struct vsctl_info info
;
1539 get_info(ctx
->ovs
, &info
);
1540 iface
= find_iface(&info
, ctx
->argv
[1], true);
1541 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1545 /* Print targets of the 'n_controllers' in 'controllers' on the output for
1548 print_controllers(struct vsctl_context
*ctx
,
1549 struct ovsrec_controller
**controllers
,
1550 size_t n_controllers
)
1552 /* Print the targets in sorted order for reproducibility. */
1553 struct svec targets
;
1556 svec_init(&targets
);
1557 for (i
= 0; i
< n_controllers
; i
++) {
1558 svec_add(&targets
, controllers
[i
]->target
);
1561 svec_sort(&targets
);
1562 for (i
= 0; i
< targets
.n
; i
++) {
1563 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1565 svec_destroy(&targets
);
1569 cmd_get_controller(struct vsctl_context
*ctx
)
1571 struct vsctl_info info
;
1573 get_info(ctx
->ovs
, &info
);
1575 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1576 print_controllers(ctx
, info
.ctrl
, info
.n_ctrl
);
1578 struct vsctl_bridge
*br
= find_bridge(&info
, ctx
->argv
[1], true);
1580 print_controllers(ctx
, br
->ctrl
, br
->n_ctrl
);
1582 print_controllers(ctx
, info
.ctrl
, info
.n_ctrl
);
1590 delete_controllers(struct ovsrec_controller
**controllers
,
1591 size_t n_controllers
)
1595 for (i
= 0; i
< n_controllers
; i
++) {
1596 ovsrec_controller_delete(controllers
[i
]);
1601 cmd_del_controller(struct vsctl_context
*ctx
)
1603 struct vsctl_info info
;
1605 get_info(ctx
->ovs
, &info
);
1607 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1609 delete_controllers(info
.ctrl
, info
.n_ctrl
);
1610 ovsrec_open_vswitch_set_controller(ctx
->ovs
, NULL
, 0);
1613 struct vsctl_bridge
*br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1615 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1616 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1623 static struct ovsrec_controller
**
1624 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1626 struct ovsrec_controller
**controllers
;
1629 controllers
= xmalloc(n
* sizeof *controllers
);
1630 for (i
= 0; i
< n
; i
++) {
1631 controllers
[i
] = ovsrec_controller_insert(txn
);
1632 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1639 set_default_controllers(struct vsctl_context
*ctx
, char *targets
[], size_t n
)
1641 struct ovsrec_controller
**controllers
;
1643 delete_controllers(ctx
->ovs
->controller
, ctx
->ovs
->n_controller
);
1645 controllers
= insert_controllers(ctx
->txn
, targets
, n
);
1646 ovsrec_open_vswitch_set_controller(ctx
->ovs
, controllers
, n
);
1651 cmd_set_controller(struct vsctl_context
*ctx
)
1653 struct vsctl_info info
;
1655 get_info(ctx
->ovs
, &info
);
1657 if (ctx
->argc
== 2) {
1658 /* Set one controller in the "Open_vSwitch" table. */
1659 set_default_controllers(ctx
, &ctx
->argv
[1], 1);
1660 } else if (!strcmp(ctx
->argv
[1], "default")) {
1661 /* Set one or more controllers in the "Open_vSwitch" table. */
1662 set_default_controllers(ctx
, &ctx
->argv
[2], ctx
->argc
- 2);
1664 /* Set one or more controllers for a particular bridge. */
1665 struct vsctl_bridge
*br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1666 struct ovsrec_controller
**controllers
;
1669 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1672 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1673 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1681 get_fail_mode(struct ovsrec_controller
**controllers
, size_t n_controllers
)
1683 const char *fail_mode
;
1687 for (i
= 0; i
< n_controllers
; i
++) {
1688 const char *s
= controllers
[i
]->fail_mode
;
1690 if (!strcmp(s
, "secure")) {
1702 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1704 struct vsctl_info info
;
1705 const char *fail_mode
= NULL
;
1707 get_info(ctx
->ovs
, &info
);
1709 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1710 /* Return the fail-mode from the "Open_vSwitch" table */
1711 fail_mode
= get_fail_mode(info
.ctrl
, info
.n_ctrl
);
1713 /* Return the fail-mode for a particular bridge. */
1714 struct vsctl_bridge
*br
= find_bridge(&info
, ctx
->argv
[1], true);
1716 /* If no controller is defined for the requested bridge, fallback to
1717 * the "Open_vSwitch" table's controller. */
1718 fail_mode
= (br
->n_ctrl
1719 ? get_fail_mode(br
->ctrl
, br
->n_ctrl
)
1720 : get_fail_mode(info
.ctrl
, info
.n_ctrl
));
1723 if (fail_mode
&& strlen(fail_mode
)) {
1724 ds_put_format(&ctx
->output
, "%s\n", fail_mode
);
1731 set_fail_mode(struct ovsrec_controller
**controllers
, size_t n_controllers
,
1732 const char *fail_mode
)
1736 for (i
= 0; i
< n_controllers
; i
++) {
1737 ovsrec_controller_set_fail_mode(controllers
[i
], fail_mode
);
1742 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1744 struct vsctl_info info
;
1746 get_info(ctx
->ovs
, &info
);
1748 if (ctx
->argc
== 1 || !strcmp(ctx
->argv
[1], "default")) {
1749 set_fail_mode(info
.ctrl
, info
.n_ctrl
, NULL
);
1751 struct vsctl_bridge
*br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1753 set_fail_mode(br
->ctrl
, br
->n_ctrl
, NULL
);
1760 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1762 struct vsctl_info info
;
1764 const char *fail_mode
;
1766 get_info(ctx
->ovs
, &info
);
1768 if (ctx
->argc
== 2) {
1770 fail_mode
= ctx
->argv
[1];
1772 bridge
= ctx
->argv
[1];
1773 fail_mode
= ctx
->argv
[2];
1776 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1777 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1780 if (!strcmp(bridge
, "default")) {
1781 /* Set the fail-mode in the "Open_vSwitch" table. */
1783 vsctl_fatal("no controller declared");
1785 set_fail_mode(info
.ctrl
, info
.n_ctrl
, fail_mode
);
1787 struct vsctl_bridge
*br
= find_real_bridge(&info
, bridge
, true);
1790 vsctl_fatal("no controller declared for %s", br
->name
);
1792 set_fail_mode(br
->ctrl
, br
->n_ctrl
, fail_mode
);
1799 cmd_get_ssl(struct vsctl_context
*ctx
)
1801 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1804 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
1805 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
1806 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
1807 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
1808 ssl
->bootstrap_ca_cert
? "true" : "false");
1813 cmd_del_ssl(struct vsctl_context
*ctx
)
1815 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1818 ovsrec_ssl_delete(ssl
);
1819 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1824 cmd_set_ssl(struct vsctl_context
*ctx
)
1826 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
1827 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
1830 ovsrec_ssl_delete(ssl
);
1832 ssl
= ovsrec_ssl_insert(ctx
->txn
);
1834 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
1835 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
1836 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
1838 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
1840 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
1843 /* Parameter commands. */
1845 struct vsctl_row_id
{
1846 const struct ovsdb_idl_table_class
*table
;
1847 const struct ovsdb_idl_column
*name_column
;
1848 const struct ovsdb_idl_column
*uuid_column
;
1851 struct vsctl_table_class
{
1852 struct ovsdb_idl_table_class
*class;
1853 struct vsctl_row_id row_ids
[2];
1856 static const struct vsctl_table_class tables
[] = {
1857 {&ovsrec_table_bridge
,
1858 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
1859 {NULL
, NULL
, NULL
}}},
1861 {&ovsrec_table_controller
,
1862 {{&ovsrec_table_bridge
,
1863 &ovsrec_bridge_col_name
,
1864 &ovsrec_bridge_col_controller
},
1865 {&ovsrec_table_open_vswitch
,
1867 &ovsrec_open_vswitch_col_controller
}}},
1869 {&ovsrec_table_interface
,
1870 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
1871 {NULL
, NULL
, NULL
}}},
1873 {&ovsrec_table_mirror
,
1874 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
1875 {NULL
, NULL
, NULL
}}},
1877 {&ovsrec_table_netflow
,
1878 {{&ovsrec_table_bridge
,
1879 &ovsrec_bridge_col_name
,
1880 &ovsrec_bridge_col_netflow
},
1881 {NULL
, NULL
, NULL
}}},
1883 {&ovsrec_table_open_vswitch
,
1884 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
1885 {NULL
, NULL
, NULL
}}},
1887 {&ovsrec_table_port
,
1888 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
1889 {NULL
, NULL
, NULL
}}},
1892 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
1893 {NULL
, NULL
, NULL
}}},
1895 {&ovsrec_table_queue
,
1896 {{NULL
, NULL
, NULL
},
1897 {NULL
, NULL
, NULL
}}},
1900 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
1902 {&ovsrec_table_sflow
,
1903 {{&ovsrec_table_bridge
,
1904 &ovsrec_bridge_col_name
,
1905 &ovsrec_bridge_col_sflow
},
1906 {NULL
, NULL
, NULL
}}},
1908 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
1912 die_if_error(char *error
)
1915 vsctl_fatal("%s", error
);
1920 to_lower_and_underscores(unsigned c
)
1922 return c
== '-' ? '_' : tolower(c
);
1926 score_partial_match(const char *name
, const char *s
)
1930 if (!strcmp(name
, s
)) {
1933 for (score
= 0; ; score
++, name
++, s
++) {
1934 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
1936 } else if (*name
== '\0') {
1937 return UINT_MAX
- 1;
1940 return *s
== '\0' ? score
: 0;
1943 static const struct vsctl_table_class
*
1944 get_table(const char *table_name
)
1946 const struct vsctl_table_class
*table
;
1947 const struct vsctl_table_class
*best_match
= NULL
;
1948 unsigned int best_score
= 0;
1950 for (table
= tables
; table
->class; table
++) {
1951 unsigned int score
= score_partial_match(table
->class->name
,
1953 if (score
> best_score
) {
1956 } else if (score
== best_score
) {
1962 } else if (best_score
) {
1963 vsctl_fatal("multiple table names match \"%s\"", table_name
);
1965 vsctl_fatal("unknown table \"%s\"", table_name
);
1969 static const struct ovsdb_idl_row
*
1970 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
1971 const struct vsctl_row_id
*id
, const char *record_id
)
1973 const struct ovsdb_idl_row
*referrer
, *final
;
1979 if (!id
->name_column
) {
1980 if (strcmp(record_id
, ".")) {
1983 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
1984 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
1988 const struct ovsdb_idl_row
*row
;
1991 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
1993 row
= ovsdb_idl_next_row(row
))
1995 const struct ovsdb_datum
*name
;
1997 name
= ovsdb_idl_get(row
, id
->name_column
,
1998 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
1999 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
2001 vsctl_fatal("multiple rows in %s match \"%s\"",
2002 table
->class->name
, record_id
);
2013 if (id
->uuid_column
) {
2014 const struct ovsdb_datum
*uuid
;
2016 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
2017 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
2019 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
2020 &uuid
->keys
[0].uuid
);
2029 static const struct ovsdb_idl_row
*
2030 get_row (struct vsctl_context
*ctx
,
2031 const struct vsctl_table_class
*table
, const char *record_id
)
2033 const struct ovsdb_idl_row
*row
;
2036 if (uuid_from_string(&uuid
, record_id
)) {
2037 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
2041 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
2042 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
2051 static const struct ovsdb_idl_row
*
2052 must_get_row(struct vsctl_context
*ctx
,
2053 const struct vsctl_table_class
*table
, const char *record_id
)
2055 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
2057 vsctl_fatal("no row \"%s\" in table %s",
2058 record_id
, table
->class->name
);
2064 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
2065 const struct ovsdb_idl_column
**columnp
)
2067 const struct ovsdb_idl_column
*best_match
= NULL
;
2068 unsigned int best_score
= 0;
2071 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2072 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2073 unsigned int score
= score_partial_match(column
->name
, column_name
);
2074 if (score
> best_score
) {
2075 best_match
= column
;
2077 } else if (score
== best_score
) {
2082 *columnp
= best_match
;
2085 } else if (best_score
) {
2086 return xasprintf("%s contains more than one column whose name "
2087 "matches \"%s\"", table
->class->name
, column_name
);
2089 return xasprintf("%s does not contain a column whose name matches "
2090 "\"%s\"", table
->class->name
, column_name
);
2095 missing_operator_error(const char *arg
, const char **allowed_operators
,
2101 ds_put_format(&s
, "%s: argument does not end in ", arg
);
2102 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
2103 if (n_allowed
== 2) {
2104 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
2105 } else if (n_allowed
> 2) {
2108 for (i
= 1; i
< n_allowed
- 1; i
++) {
2109 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
2111 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
2113 ds_put_format(&s
, " followed by a value.");
2115 return ds_steal_cstr(&s
);
2118 /* Breaks 'arg' apart into a number of fields in the following order:
2120 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2121 * is stored into '*columnp'. The column name may be abbreviated.
2123 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2124 * and 'keyp' are nonnull, then the column and key names are expected to
2125 * be separated by ':'). The key is stored as a malloc()'d string into
2126 * '*keyp', or NULL if no key is present in 'arg'.
2128 * - If 'valuep' is nonnull, an operator followed by a value string. The
2129 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2130 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2131 * operator is stored into '*operatorp' (one of the pointers from
2132 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2133 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2136 * At least 'columnp' or 'keyp' must be nonnull.
2138 * On success, returns NULL. On failure, returned a malloc()'d string error
2139 * message and stores NULL into all of the nonnull output arguments. */
2140 static char * WARN_UNUSED_RESULT
2141 parse_column_key_value(const char *arg
,
2142 const struct vsctl_table_class
*table
,
2143 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2144 const char **operatorp
,
2145 const char **allowed_operators
, size_t n_allowed
,
2148 const char *p
= arg
;
2151 assert(columnp
|| keyp
);
2152 assert(!(operatorp
&& !valuep
));
2160 /* Parse column name. */
2164 error
= ovsdb_token_parse(&p
, &column_name
);
2168 if (column_name
[0] == '\0') {
2170 error
= xasprintf("%s: missing column name", arg
);
2173 error
= get_column(table
, column_name
, columnp
);
2180 /* Parse key string. */
2181 if (*p
== ':' || !columnp
) {
2185 error
= xasprintf("%s: key not accepted here", arg
);
2188 error
= ovsdb_token_parse(&p
, keyp
);
2196 /* Parse value string. */
2202 if (!allowed_operators
) {
2203 static const char *equals
= "=";
2204 allowed_operators
= &equals
;
2210 for (i
= 0; i
< n_allowed
; i
++) {
2211 const char *op
= allowed_operators
[i
];
2212 size_t op_len
= strlen(op
);
2214 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2220 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2227 *valuep
= xstrdup(p
+ best_len
);
2233 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2259 cmd_get(struct vsctl_context
*ctx
)
2261 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2262 const char *table_name
= ctx
->argv
[1];
2263 const char *record_id
= ctx
->argv
[2];
2264 const struct vsctl_table_class
*table
;
2265 const struct ovsdb_idl_row
*row
;
2266 struct ds
*out
= &ctx
->output
;
2269 table
= get_table(table_name
);
2270 row
= must_get_row(ctx
, table
, record_id
);
2271 for (i
= 3; i
< ctx
->argc
; i
++) {
2272 const struct ovsdb_idl_column
*column
;
2273 const struct ovsdb_datum
*datum
;
2276 /* Special case for obtaining the UUID of a row. We can't just do this
2277 * through parse_column_key_value() below since it returns a "struct
2278 * ovsdb_idl_column" and the UUID column doesn't have one. */
2279 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2280 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2281 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2285 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2286 &column
, &key_string
,
2287 NULL
, NULL
, 0, NULL
));
2289 datum
= ovsdb_idl_read(row
, column
);
2291 union ovsdb_atom key
;
2294 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2295 vsctl_fatal("cannot specify key to get for non-map column %s",
2299 die_if_error(ovsdb_atom_from_string(&key
,
2301 key_string
, ctx
->symtab
));
2303 idx
= ovsdb_datum_find_key(datum
, &key
,
2304 column
->type
.key
.type
);
2305 if (idx
== UINT_MAX
) {
2307 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2308 key_string
, table
->class->name
, record_id
,
2312 ovsdb_atom_to_string(&datum
->values
[idx
],
2313 column
->type
.value
.type
, out
);
2315 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2317 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2319 ds_put_char(out
, '\n');
2326 list_record(const struct vsctl_table_class
*table
,
2327 const struct ovsdb_idl_row
*row
, struct ds
*out
)
2331 ds_put_format(out
, "%-20s: "UUID_FMT
"\n", "_uuid",
2332 UUID_ARGS(&row
->uuid
));
2333 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2334 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2335 const struct ovsdb_datum
*datum
;
2337 datum
= ovsdb_idl_read(row
, column
);
2339 ds_put_format(out
, "%-20s: ", column
->name
);
2340 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2341 ds_put_char(out
, '\n');
2346 cmd_list(struct vsctl_context
*ctx
)
2348 const char *table_name
= ctx
->argv
[1];
2349 const struct vsctl_table_class
*table
;
2350 struct ds
*out
= &ctx
->output
;
2353 table
= get_table(table_name
);
2354 if (ctx
->argc
> 2) {
2355 for (i
= 2; i
< ctx
->argc
; i
++) {
2357 ds_put_char(out
, '\n');
2359 list_record(table
, must_get_row(ctx
, table
, ctx
->argv
[i
]), out
);
2362 const struct ovsdb_idl_row
*row
;
2365 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class), first
= true;
2367 row
= ovsdb_idl_next_row(row
), first
= false) {
2369 ds_put_char(out
, '\n');
2371 list_record(table
, row
, out
);
2377 set_column(const struct vsctl_table_class
*table
,
2378 const struct ovsdb_idl_row
*row
, const char *arg
,
2379 struct ovsdb_symbol_table
*symtab
)
2381 const struct ovsdb_idl_column
*column
;
2382 char *key_string
, *value_string
;
2385 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2386 NULL
, NULL
, 0, &value_string
);
2387 die_if_error(error
);
2388 if (!value_string
) {
2389 vsctl_fatal("%s: missing value", arg
);
2393 union ovsdb_atom key
, value
;
2394 struct ovsdb_datum datum
;
2396 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2397 vsctl_fatal("cannot specify key to set for non-map column %s",
2401 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2402 key_string
, symtab
));
2403 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2404 value_string
, symtab
));
2406 ovsdb_datum_init_empty(&datum
);
2407 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
2409 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2410 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2412 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
2413 &column
->type
, false);
2414 ovsdb_idl_txn_write(row
, column
, &datum
);
2416 struct ovsdb_datum datum
;
2418 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2419 value_string
, symtab
));
2420 ovsdb_idl_txn_write(row
, column
, &datum
);
2428 cmd_set(struct vsctl_context
*ctx
)
2430 const char *table_name
= ctx
->argv
[1];
2431 const char *record_id
= ctx
->argv
[2];
2432 const struct vsctl_table_class
*table
;
2433 const struct ovsdb_idl_row
*row
;
2436 table
= get_table(table_name
);
2437 row
= must_get_row(ctx
, table
, record_id
);
2438 for (i
= 3; i
< ctx
->argc
; i
++) {
2439 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2444 cmd_add(struct vsctl_context
*ctx
)
2446 const char *table_name
= ctx
->argv
[1];
2447 const char *record_id
= ctx
->argv
[2];
2448 const char *column_name
= ctx
->argv
[3];
2449 const struct vsctl_table_class
*table
;
2450 const struct ovsdb_idl_column
*column
;
2451 const struct ovsdb_idl_row
*row
;
2452 const struct ovsdb_type
*type
;
2453 struct ovsdb_datum old
;
2456 table
= get_table(table_name
);
2457 row
= must_get_row(ctx
, table
, record_id
);
2458 die_if_error(get_column(table
, column_name
, &column
));
2460 type
= &column
->type
;
2461 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2462 for (i
= 4; i
< ctx
->argc
; i
++) {
2463 struct ovsdb_type add_type
;
2464 struct ovsdb_datum add
;
2468 add_type
.n_max
= UINT_MAX
;
2469 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2471 ovsdb_datum_union(&old
, &add
, type
, false);
2472 ovsdb_datum_destroy(&add
, type
);
2474 if (old
.n
> type
->n_max
) {
2475 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2476 "table %s but the maximum number is %u",
2478 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2479 column
->name
, table
->class->name
, type
->n_max
);
2481 ovsdb_idl_txn_write(row
, column
, &old
);
2485 cmd_remove(struct vsctl_context
*ctx
)
2487 const char *table_name
= ctx
->argv
[1];
2488 const char *record_id
= ctx
->argv
[2];
2489 const char *column_name
= ctx
->argv
[3];
2490 const struct vsctl_table_class
*table
;
2491 const struct ovsdb_idl_column
*column
;
2492 const struct ovsdb_idl_row
*row
;
2493 const struct ovsdb_type
*type
;
2494 struct ovsdb_datum old
;
2497 table
= get_table(table_name
);
2498 row
= must_get_row(ctx
, table
, record_id
);
2499 die_if_error(get_column(table
, column_name
, &column
));
2501 type
= &column
->type
;
2502 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2503 for (i
= 4; i
< ctx
->argc
; i
++) {
2504 struct ovsdb_type rm_type
;
2505 struct ovsdb_datum rm
;
2510 rm_type
.n_max
= UINT_MAX
;
2511 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
2512 ctx
->argv
[i
], ctx
->symtab
);
2513 if (error
&& ovsdb_type_is_map(&rm_type
)) {
2515 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
2516 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
2517 ctx
->argv
[i
], ctx
->symtab
));
2519 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
2520 ovsdb_datum_destroy(&rm
, &rm_type
);
2522 if (old
.n
< type
->n_min
) {
2523 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2524 "table %s but the minimum number is %u",
2526 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2527 column
->name
, table
->class->name
, type
->n_min
);
2529 ovsdb_idl_txn_write(row
, column
, &old
);
2533 cmd_clear(struct vsctl_context
*ctx
)
2535 const char *table_name
= ctx
->argv
[1];
2536 const char *record_id
= ctx
->argv
[2];
2537 const struct vsctl_table_class
*table
;
2538 const struct ovsdb_idl_row
*row
;
2541 table
= get_table(table_name
);
2542 row
= must_get_row(ctx
, table
, record_id
);
2543 for (i
= 3; i
< ctx
->argc
; i
++) {
2544 const struct ovsdb_idl_column
*column
;
2545 const struct ovsdb_type
*type
;
2546 struct ovsdb_datum datum
;
2548 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
2550 type
= &column
->type
;
2551 if (type
->n_min
> 0) {
2552 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2553 "of table %s, which is not allowed to be empty",
2554 column
->name
, table
->class->name
);
2557 ovsdb_datum_init_empty(&datum
);
2558 ovsdb_idl_txn_write(row
, column
, &datum
);
2563 cmd_create(struct vsctl_context
*ctx
)
2565 const char *id
= shash_find_data(&ctx
->options
, "--id");
2566 const char *table_name
= ctx
->argv
[1];
2567 const struct vsctl_table_class
*table
;
2568 const struct ovsdb_idl_row
*row
;
2569 const struct uuid
*uuid
;
2573 struct ovsdb_symbol
*symbol
;
2576 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2579 symbol
= ovsdb_symbol_table_insert(ctx
->symtab
, id
);
2581 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2584 symbol
->used
= true;
2586 uuid
= &symbol
->uuid
;
2591 table
= get_table(table_name
);
2592 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
2593 for (i
= 2; i
< ctx
->argc
; i
++) {
2594 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2596 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
2599 /* This function may be used as the 'postprocess' function for commands that
2600 * insert new rows into the database. It expects that the command's 'run'
2601 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2602 * sole output. It replaces that output by the row's permanent UUID assigned
2603 * by the database server and appends a new-line.
2605 * Currently we use this only for "create", because the higher-level commands
2606 * are supposed to be independent of the actual structure of the vswitch
2609 post_create(struct vsctl_context
*ctx
)
2611 const struct uuid
*real
;
2614 uuid_from_string(&dummy
, ds_cstr(&ctx
->output
));
2615 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
2617 ds_clear(&ctx
->output
);
2618 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
2620 ds_put_char(&ctx
->output
, '\n');
2624 cmd_destroy(struct vsctl_context
*ctx
)
2626 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
2627 const char *table_name
= ctx
->argv
[1];
2628 const struct vsctl_table_class
*table
;
2631 table
= get_table(table_name
);
2632 for (i
= 2; i
< ctx
->argc
; i
++) {
2633 const struct ovsdb_idl_row
*row
;
2635 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
2637 ovsdb_idl_txn_delete(row
);
2643 is_condition_satified(const struct vsctl_table_class
*table
,
2644 const struct ovsdb_idl_row
*row
, const char *arg
,
2645 struct ovsdb_symbol_table
*symtab
)
2647 static const char *operators
[] = {
2648 "=", "!=", "<", ">", "<=", ">="
2651 const struct ovsdb_idl_column
*column
;
2652 const struct ovsdb_datum
*have_datum
;
2653 char *key_string
, *value_string
;
2654 const char *operator;
2659 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2660 &operator, operators
, ARRAY_SIZE(operators
),
2662 die_if_error(error
);
2663 if (!value_string
) {
2664 vsctl_fatal("%s: missing value", arg
);
2667 have_datum
= ovsdb_idl_read(row
, column
);
2669 union ovsdb_atom want_key
, want_value
;
2671 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2672 vsctl_fatal("cannot specify key to check for non-map column %s",
2676 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
2677 key_string
, symtab
));
2678 die_if_error(ovsdb_atom_from_string(&want_value
, &column
->type
.value
,
2679 value_string
, symtab
));
2681 idx
= ovsdb_datum_find_key(have_datum
,
2682 &want_key
, column
->type
.key
.type
);
2683 if (idx
!= UINT_MAX
) {
2684 cmp
= ovsdb_atom_compare_3way(&have_datum
->values
[idx
],
2686 column
->type
.value
.type
);
2689 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
2690 ovsdb_atom_destroy(&want_value
, column
->type
.value
.type
);
2692 struct ovsdb_datum want_datum
;
2694 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
2695 value_string
, symtab
));
2697 cmp
= ovsdb_datum_compare_3way(have_datum
, &want_datum
,
2699 ovsdb_datum_destroy(&want_datum
, &column
->type
);
2705 return (idx
== UINT_MAX
? false
2706 : !strcmp(operator, "=") ? cmp
== 0
2707 : !strcmp(operator, "!=") ? cmp
!= 0
2708 : !strcmp(operator, "<") ? cmp
< 0
2709 : !strcmp(operator, ">") ? cmp
> 0
2710 : !strcmp(operator, "<=") ? cmp
<= 0
2711 : !strcmp(operator, ">=") ? cmp
>= 0
2716 cmd_wait_until(struct vsctl_context
*ctx
)
2718 const char *table_name
= ctx
->argv
[1];
2719 const char *record_id
= ctx
->argv
[2];
2720 const struct vsctl_table_class
*table
;
2721 const struct ovsdb_idl_row
*row
;
2724 table
= get_table(table_name
);
2726 row
= get_row(ctx
, table
, record_id
);
2728 ctx
->try_again
= true;
2732 for (i
= 3; i
< ctx
->argc
; i
++) {
2733 if (!is_condition_satified(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
2734 ctx
->try_again
= true;
2740 static struct json
*
2741 where_uuid_equals(const struct uuid
*uuid
)
2744 json_array_create_1(
2745 json_array_create_3(
2746 json_string_create("_uuid"),
2747 json_string_create("=="),
2748 json_array_create_2(
2749 json_string_create("uuid"),
2750 json_string_create_nocopy(
2751 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
2755 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
2756 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2757 const struct ovsrec_open_vswitch
*ovs
,
2758 struct ovsdb_symbol_table
*symtab
)
2760 ctx
->argc
= command
->argc
;
2761 ctx
->argv
= command
->argv
;
2762 ctx
->options
= command
->options
;
2764 ds_swap(&ctx
->output
, &command
->output
);
2768 ctx
->symtab
= symtab
;
2770 ctx
->try_again
= false;
2774 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
2776 ds_swap(&ctx
->output
, &command
->output
);
2780 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
2781 struct ovsdb_idl
*idl
)
2783 struct ovsdb_idl_txn
*txn
;
2784 const struct ovsrec_open_vswitch
*ovs
;
2785 enum ovsdb_idl_txn_status status
;
2786 struct ovsdb_symbol_table
*symtab
;
2788 struct vsctl_command
*c
;
2789 int64_t next_cfg
= 0;
2792 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2794 ovsdb_idl_txn_set_dry_run(txn
);
2797 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2799 ovs
= ovsrec_open_vswitch_first(idl
);
2801 /* XXX add verification that table is empty */
2802 ovs
= ovsrec_open_vswitch_insert(txn
);
2805 if (wait_for_reload
) {
2806 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
2807 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
2808 json_destroy(where
);
2811 symtab
= ovsdb_symbol_table_create();
2812 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2813 ds_init(&c
->output
);
2815 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2816 struct vsctl_context ctx
;
2818 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2819 (c
->syntax
->run
)(&ctx
);
2820 vsctl_context_done(&ctx
, c
);
2822 if (ctx
.try_again
) {
2827 status
= ovsdb_idl_txn_commit_block(txn
);
2828 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2829 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2831 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2832 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2833 if (c
->syntax
->postprocess
) {
2834 struct vsctl_context ctx
;
2836 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
2837 (c
->syntax
->postprocess
)(&ctx
);
2838 vsctl_context_done(&ctx
, c
);
2842 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2843 ovsdb_idl_txn_destroy(txn
);
2846 unused
= ovsdb_symbol_table_find_unused(symtab
);
2848 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2849 "with \"-- --id=%s create ...\")", unused
, unused
);
2853 case TXN_INCOMPLETE
:
2857 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2858 vsctl_fatal("transaction aborted");
2868 vsctl_fatal("transaction error: %s", error
);
2875 ovsdb_symbol_table_destroy(symtab
);
2877 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2878 struct ds
*ds
= &c
->output
;
2879 struct shash_node
*node
;
2885 for (j
= 0; j
< ds
->length
; j
++) {
2886 int c
= ds
->string
[j
];
2889 fputs("\\n", stdout
);
2893 fputs("\\\\", stdout
);
2902 fputs(ds_cstr(ds
), stdout
);
2904 ds_destroy(&c
->output
);
2906 SHASH_FOR_EACH (node
, &c
->options
) {
2909 shash_destroy(&c
->options
);
2913 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2915 const struct ovsrec_open_vswitch
*ovs
;
2918 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2919 if (ovs
->cur_cfg
>= next_cfg
) {
2923 ovsdb_idl_wait(idl
);
2928 ovsdb_idl_destroy(idl
);
2933 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2934 * resources and return so that the caller can try again. */
2935 ovsdb_idl_txn_abort(txn
);
2936 ovsdb_idl_txn_destroy(txn
);
2937 ovsdb_symbol_table_destroy(symtab
);
2938 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2939 ds_destroy(&c
->output
);
2944 static const struct vsctl_command_syntax all_commands
[] = {
2945 /* Open vSwitch commands. */
2946 {"init", 0, 0, cmd_init
, NULL
, ""},
2948 /* Bridge commands. */
2949 {"add-br", 1, 3, cmd_add_br
, NULL
, "--may-exist"},
2950 {"del-br", 1, 1, cmd_del_br
, NULL
, "--if-exists"},
2951 {"list-br", 0, 0, cmd_list_br
, NULL
, ""},
2952 {"br-exists", 1, 1, cmd_br_exists
, NULL
, ""},
2953 {"br-to-vlan", 1, 1, cmd_br_to_vlan
, NULL
, ""},
2954 {"br-to-parent", 1, 1, cmd_br_to_parent
, NULL
, ""},
2955 {"br-set-external-id", 2, 3, cmd_br_set_external_id
, NULL
, ""},
2956 {"br-get-external-id", 1, 2, cmd_br_get_external_id
, NULL
, ""},
2958 /* Port commands. */
2959 {"list-ports", 1, 1, cmd_list_ports
, NULL
, ""},
2960 {"add-port", 2, INT_MAX
, cmd_add_port
, NULL
, "--may-exist"},
2961 {"add-bond", 4, INT_MAX
, cmd_add_bond
, NULL
, "--may-exist,--fake-iface"},
2962 {"del-port", 1, 2, cmd_del_port
, NULL
, "--if-exists,--with-iface"},
2963 {"port-to-br", 1, 1, cmd_port_to_br
, NULL
, ""},
2965 /* Interface commands. */
2966 {"list-ifaces", 1, 1, cmd_list_ifaces
, NULL
, ""},
2967 {"iface-to-br", 1, 1, cmd_iface_to_br
, NULL
, ""},
2969 /* Controller commands. */
2970 {"get-controller", 0, 1, cmd_get_controller
, NULL
, ""},
2971 {"del-controller", 0, 1, cmd_del_controller
, NULL
, ""},
2972 {"set-controller", 1, INT_MAX
, cmd_set_controller
, NULL
, ""},
2973 {"get-fail-mode", 0, 1, cmd_get_fail_mode
, NULL
, ""},
2974 {"del-fail-mode", 0, 1, cmd_del_fail_mode
, NULL
, ""},
2975 {"set-fail-mode", 1, 2, cmd_set_fail_mode
, NULL
, ""},
2978 {"get-ssl", 0, 0, cmd_get_ssl
, NULL
, ""},
2979 {"del-ssl", 0, 0, cmd_del_ssl
, NULL
, ""},
2980 {"set-ssl", 3, 3, cmd_set_ssl
, NULL
, "--bootstrap"},
2982 /* Switch commands. */
2983 {"emer-reset", 0, 0, cmd_emer_reset
, NULL
, ""},
2985 /* Parameter commands. */
2986 {"get", 3, INT_MAX
, cmd_get
, NULL
, "--if-exists"},
2987 {"list", 1, INT_MAX
, cmd_list
, NULL
, ""},
2988 {"set", 3, INT_MAX
, cmd_set
, NULL
, ""},
2989 {"add", 4, INT_MAX
, cmd_add
, NULL
, ""},
2990 {"remove", 4, INT_MAX
, cmd_remove
, NULL
, ""},
2991 {"clear", 3, INT_MAX
, cmd_clear
, NULL
, ""},
2992 {"create", 2, INT_MAX
, cmd_create
, post_create
, "--id="},
2993 {"destroy", 1, INT_MAX
, cmd_destroy
, NULL
, "--if-exists"},
2994 {"wait-until", 2, INT_MAX
, cmd_wait_until
, NULL
, ""},
2996 {NULL
, 0, 0, NULL
, NULL
, NULL
},