2 * Copyright (c) 2009, 2010, 2011 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"
40 #include "stream-ssl.h"
42 #include "vswitchd/vswitch-idl.h"
48 VLOG_DEFINE_THIS_MODULE(vsctl
);
50 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
51 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
55 /* A command supported by ovs-vsctl. */
56 struct vsctl_command_syntax
{
57 const char *name
; /* e.g. "add-br" */
58 int min_args
; /* Min number of arguments following name. */
59 int max_args
; /* Max number of arguments following name. */
61 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
62 * each column or table in ctx->idl that it uses. */
63 void (*prerequisites
)(struct vsctl_context
*ctx
);
65 /* Does the actual work of the command and puts the command's output, if
66 * any, in ctx->output or ctx->table.
68 * Alternatively, if some prerequisite of the command is not met and the
69 * caller should wait for something to change and then retry, it may set
70 * ctx->try_again to true. (Only the "wait-until" command currently does
72 void (*run
)(struct vsctl_context
*ctx
);
74 /* If nonnull, called after the transaction has been successfully
75 * committed. ctx->output is the output from the "run" function, which
76 * this function may modify and otherwise postprocess as needed. (Only the
77 * "create" command currently does any postprocessing.) */
78 void (*postprocess
)(struct vsctl_context
*ctx
);
80 /* A comma-separated list of supported options, e.g. "--a,--b", or the
81 * empty string if the command does not support any options. */
83 enum { RO
, RW
} mode
; /* Does this command modify the database? */
86 struct vsctl_command
{
87 /* Data that remains constant after initialization. */
88 const struct vsctl_command_syntax
*syntax
;
93 /* Data modified by commands. */
98 /* --db: The database server to contact. */
99 static const char *db
;
101 /* --oneline: Write each command's output as a single line? */
104 /* --dry-run: Do not commit any changes. */
107 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
108 static bool wait_for_reload
= true;
110 /* --timeout: Time to wait for a connection to 'db'. */
113 /* Format for table output. */
114 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
116 /* All supported commands. */
117 static const struct vsctl_command_syntax all_commands
[];
119 /* The IDL we're using and the current transaction, if any.
120 * This is for use by vsctl_exit() only, to allow it to clean up.
121 * Other code should use its context arguments. */
122 static struct ovsdb_idl
*the_idl
;
123 static struct ovsdb_idl_txn
*the_idl_txn
;
125 static void vsctl_exit(int status
) NO_RETURN
;
126 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN
;
127 static char *default_db(void);
128 static void usage(void) NO_RETURN
;
129 static void parse_options(int argc
, char *argv
[]);
130 static bool might_write_to_db(char **argv
);
132 static struct vsctl_command
*parse_commands(int argc
, char *argv
[],
133 size_t *n_commandsp
);
134 static void parse_command(int argc
, char *argv
[], struct vsctl_command
*);
135 static const struct vsctl_command_syntax
*find_command(const char *name
);
136 static void run_prerequisites(struct vsctl_command
[], size_t n_commands
,
138 static void do_vsctl(const char *args
,
139 struct vsctl_command
*, size_t n_commands
,
142 static const struct vsctl_table_class
*get_table(const char *table_name
);
143 static void set_column(const struct vsctl_table_class
*,
144 const struct ovsdb_idl_row
*, const char *arg
,
145 struct ovsdb_symbol_table
*);
147 static bool is_condition_satisfied(const struct vsctl_table_class
*,
148 const struct ovsdb_idl_row
*,
150 struct ovsdb_symbol_table
*);
153 main(int argc
, char *argv
[])
155 extern struct vlog_module VLM_reconnect
;
156 struct ovsdb_idl
*idl
;
157 struct vsctl_command
*commands
;
161 set_program_name(argv
[0]);
162 signal(SIGPIPE
, SIG_IGN
);
163 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
164 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
167 /* Log our arguments. This is often valuable for debugging systems. */
168 args
= process_escape_args(argv
);
169 VLOG(might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
171 /* Parse command line. */
172 parse_options(argc
, argv
);
173 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
179 /* Initialize IDL. */
180 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
, false);
181 run_prerequisites(commands
, n_commands
, idl
);
183 /* Now execute the commands. */
185 if (ovsdb_idl_run(idl
)) {
186 do_vsctl(args
, commands
, n_commands
, idl
);
195 parse_options(int argc
, char *argv
[])
198 OPT_DB
= UCHAR_MAX
+ 1,
207 static struct option long_options
[] = {
208 {"db", required_argument
, 0, OPT_DB
},
209 {"no-syslog", no_argument
, 0, OPT_NO_SYSLOG
},
210 {"no-wait", no_argument
, 0, OPT_NO_WAIT
},
211 {"dry-run", no_argument
, 0, OPT_DRY_RUN
},
212 {"oneline", no_argument
, 0, OPT_ONELINE
},
213 {"timeout", required_argument
, 0, 't'},
214 {"help", no_argument
, 0, 'h'},
215 {"version", no_argument
, 0, 'V'},
219 STREAM_SSL_LONG_OPTIONS
220 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
224 char *tmp
, *short_options
;
226 tmp
= long_options_to_short_options(long_options
);
227 short_options
= xasprintf("+%s", tmp
);
230 table_style
.format
= TF_LIST
;
235 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
250 vlog_set_levels(&VLM_vsctl
, VLF_SYSLOG
, VLL_WARN
);
254 wait_for_reload
= false;
265 OVS_PRINT_VERSION(0, 0);
269 timeout
= strtoul(optarg
, NULL
, 10);
271 vsctl_fatal("value %s on -t or --timeout is invalid",
277 TABLE_OPTION_HANDLERS(&table_style
)
280 STREAM_SSL_OPTION_HANDLERS
282 case OPT_PEER_CA_CERT
:
283 stream_ssl_set_peer_ca_cert_file(optarg
);
301 static struct vsctl_command
*
302 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
304 struct vsctl_command
*commands
;
305 size_t n_commands
, allocated_commands
;
309 n_commands
= allocated_commands
= 0;
311 for (start
= i
= 0; i
<= argc
; i
++) {
312 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
314 if (n_commands
>= allocated_commands
) {
315 struct vsctl_command
*c
;
317 commands
= x2nrealloc(commands
, &allocated_commands
,
319 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
320 shash_moved(&c
->options
);
323 parse_command(i
- start
, &argv
[start
],
324 &commands
[n_commands
++]);
330 vsctl_fatal("missing command name (use --help for help)");
332 *n_commandsp
= n_commands
;
337 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
339 const struct vsctl_command_syntax
*p
;
340 struct shash_node
*node
;
344 shash_init(&command
->options
);
345 for (i
= 0; i
< argc
; i
++) {
346 const char *option
= argv
[i
];
350 if (option
[0] != '-') {
354 equals
= strchr(option
, '=');
356 key
= xmemdup0(option
, equals
- option
);
357 value
= xstrdup(equals
+ 1);
359 key
= xstrdup(option
);
363 if (shash_find(&command
->options
, key
)) {
364 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
366 shash_add_nocopy(&command
->options
, key
, value
);
369 vsctl_fatal("missing command name");
372 p
= find_command(argv
[i
]);
374 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
377 SHASH_FOR_EACH (node
, &command
->options
) {
378 const char *s
= strstr(p
->options
, node
->name
);
379 int end
= s
? s
[strlen(node
->name
)] : EOF
;
381 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
382 vsctl_fatal("'%s' command has no '%s' option",
383 argv
[i
], node
->name
);
385 if ((end
== '=') != (node
->data
!= NULL
)) {
387 vsctl_fatal("missing argument to '%s' option on '%s' "
388 "command", node
->name
, argv
[i
]);
390 vsctl_fatal("'%s' option on '%s' does not accept an "
391 "argument", node
->name
, argv
[i
]);
396 n_arg
= argc
- i
- 1;
397 if (n_arg
< p
->min_args
) {
398 vsctl_fatal("'%s' command requires at least %d arguments",
399 p
->name
, p
->min_args
);
400 } else if (n_arg
> p
->max_args
) {
403 for (j
= i
+ 1; j
< argc
; j
++) {
404 if (argv
[j
][0] == '-') {
405 vsctl_fatal("'%s' command takes at most %d arguments "
406 "(note that options must precede command "
407 "names and follow a \"--\" argument)",
408 p
->name
, p
->max_args
);
412 vsctl_fatal("'%s' command takes at most %d arguments",
413 p
->name
, p
->max_args
);
417 command
->argc
= n_arg
+ 1;
418 command
->argv
= &argv
[i
];
421 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
422 * null pointer if there is none. */
423 static const struct vsctl_command_syntax
*
424 find_command(const char *name
)
426 static struct shash commands
= SHASH_INITIALIZER(&commands
);
428 if (shash_is_empty(&commands
)) {
429 const struct vsctl_command_syntax
*p
;
431 for (p
= all_commands
; p
->name
; p
++) {
432 shash_add_assert(&commands
, p
->name
, p
);
436 return shash_find_data(&commands
, name
);
440 vsctl_fatal(const char *format
, ...)
445 va_start(args
, format
);
446 message
= xvasprintf(format
, args
);
449 vlog_set_levels(&VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
450 VLOG_ERR("%s", message
);
451 ovs_error(0, "%s", message
);
452 vsctl_exit(EXIT_FAILURE
);
455 /* Frees the current transaction and the underlying IDL and then calls
458 * Freeing the transaction and the IDL is not strictly necessary, but it makes
459 * for a clean memory leak report from valgrind in the normal case. That makes
460 * it easier to notice real memory leaks. */
462 vsctl_exit(int status
)
465 ovsdb_idl_txn_abort(the_idl_txn
);
466 ovsdb_idl_txn_destroy(the_idl_txn
);
468 ovsdb_idl_destroy(the_idl
);
476 %s: ovs-vswitchd management utility\n\
477 usage: %s [OPTIONS] COMMAND [ARG...]\n\
479 Open vSwitch commands:\n\
480 init initialize database, if not yet initialized\n\
481 emer-reset reset configuration to clean state\n\
484 add-br BRIDGE create a new bridge named BRIDGE\n\
485 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
486 del-br BRIDGE delete BRIDGE and all of its ports\n\
487 list-br print the names of all the bridges\n\
488 br-exists BRIDGE test whether BRIDGE exists\n\
489 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
490 br-to-parent BRIDGE print the parent of BRIDGE\n\
491 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
492 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
493 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
494 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
496 Port commands (a bond is considered to be a single port):\n\
497 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
498 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
499 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
500 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
501 port-to-br PORT print name of bridge that contains PORT\n\
503 Interface commands (a bond consists of multiple interfaces):\n\
504 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
505 iface-to-br IFACE print name of bridge that contains IFACE\n\
507 Controller commands:\n\
508 get-controller BRIDGE print the controller for BRIDGE\n\
509 del-controller BRIDGE delete the controller for BRIDGE\n\
510 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
511 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
512 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
513 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
516 get-manager print all manager(s)\n\
517 del-manager delete all manager(s)\n\
518 set-manager TARGET... set the list of manager(s) to TARGET(s)\n\
521 get-ssl print the SSL configuration\n\
522 del-ssl delete the SSL configuration\n\
523 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
526 emer-reset reset switch to known good state\n\
528 Database commands:\n\
529 list TBL [REC] list RECord (or all records) in TBL\n\
530 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
531 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
532 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
533 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
534 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
535 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
536 create TBL COL[:KEY]=VALUE create and initialize new record\n\
537 destroy TBL REC delete RECord from TBL\n\
538 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
539 Potentially unsafe database commands require --force option.\n\
542 --db=DATABASE connect to DATABASE\n\
544 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
545 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
546 --dry-run do not commit changes to database\n\
547 --oneline print exactly one line of output per command\n",
548 program_name
, program_name
, default_db());
551 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
552 stream_usage("database", true, true, false);
555 -h, --help display this help message\n\
556 -V, --version display version information\n");
565 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
570 /* Returns true if it looks like this set of arguments might modify the
571 * database, otherwise false. (Not very smart, so it's prone to false
574 might_write_to_db(char **argv
)
576 for (; *argv
; argv
++) {
577 const struct vsctl_command_syntax
*p
= find_command(*argv
);
578 if (p
&& p
->mode
== RW
) {
585 struct vsctl_context
{
589 struct shash options
;
591 /* Modifiable state. */
594 struct ovsdb_idl
*idl
;
595 struct ovsdb_idl_txn
*txn
;
596 struct ovsdb_symbol_table
*symtab
;
597 const struct ovsrec_open_vswitch
*ovs
;
600 /* A command may set this member to true if some prerequisite is not met
601 * and the caller should wait for something to change and then retry. */
605 struct vsctl_bridge
{
606 struct ovsrec_bridge
*br_cfg
;
608 struct ovsrec_controller
**ctrl
;
611 struct vsctl_bridge
*parent
;
616 struct ovsrec_port
*port_cfg
;
617 struct vsctl_bridge
*bridge
;
621 struct ovsrec_interface
*iface_cfg
;
622 struct vsctl_port
*port
;
626 struct vsctl_context
*ctx
;
627 struct shash bridges
; /* Maps from bridge name to struct vsctl_bridge. */
628 struct shash ports
; /* Maps from port name to struct vsctl_port. */
629 struct shash ifaces
; /* Maps from port name to struct vsctl_iface. */
633 vsctl_context_to_string(const struct vsctl_context
*ctx
)
635 const struct shash_node
*node
;
641 SHASH_FOR_EACH (node
, &ctx
->options
) {
642 svec_add(&words
, node
->name
);
644 for (i
= 0; i
< ctx
->argc
; i
++) {
645 svec_add(&words
, ctx
->argv
[i
]);
647 svec_terminate(&words
);
649 s
= process_escape_args(words
.names
);
651 svec_destroy(&words
);
657 verify_ports(struct vsctl_context
*ctx
)
659 if (!ctx
->verified_ports
) {
660 const struct ovsrec_bridge
*bridge
;
661 const struct ovsrec_port
*port
;
663 ovsrec_open_vswitch_verify_bridges(ctx
->ovs
);
664 OVSREC_BRIDGE_FOR_EACH (bridge
, ctx
->idl
) {
665 ovsrec_bridge_verify_ports(bridge
);
667 OVSREC_PORT_FOR_EACH (port
, ctx
->idl
) {
668 ovsrec_port_verify_interfaces(port
);
671 ctx
->verified_ports
= true;
675 static struct vsctl_bridge
*
676 add_bridge(struct vsctl_info
*b
,
677 struct ovsrec_bridge
*br_cfg
, const char *name
,
678 struct vsctl_bridge
*parent
, int vlan
)
680 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
682 br
->name
= xstrdup(name
);
686 br
->ctrl
= parent
->br_cfg
->controller
;
687 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
688 br
->fail_mode
= parent
->br_cfg
->fail_mode
;
690 br
->ctrl
= br_cfg
->controller
;
691 br
->n_ctrl
= br_cfg
->n_controller
;
692 br
->fail_mode
= br_cfg
->fail_mode
;
694 shash_add(&b
->bridges
, br
->name
, br
);
699 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
701 return (port_cfg
->fake_bridge
703 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095);
706 static struct vsctl_bridge
*
707 find_vlan_bridge(struct vsctl_info
*info
,
708 struct vsctl_bridge
*parent
, int vlan
)
710 struct shash_node
*node
;
712 SHASH_FOR_EACH (node
, &info
->bridges
) {
713 struct vsctl_bridge
*br
= node
->data
;
714 if (br
->parent
== parent
&& br
->vlan
== vlan
) {
723 free_info(struct vsctl_info
*info
)
725 struct shash_node
*node
;
727 SHASH_FOR_EACH (node
, &info
->bridges
) {
728 struct vsctl_bridge
*bridge
= node
->data
;
732 shash_destroy(&info
->bridges
);
734 shash_destroy_free_data(&info
->ports
);
735 shash_destroy_free_data(&info
->ifaces
);
739 pre_get_info(struct vsctl_context
*ctx
)
741 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_bridges
);
743 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_name
);
744 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
745 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
746 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ports
);
748 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_name
);
749 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_fake_bridge
);
750 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_tag
);
751 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_interfaces
);
753 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_name
);
757 get_info(struct vsctl_context
*ctx
, struct vsctl_info
*info
)
759 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
760 struct shash bridges
, ports
;
764 shash_init(&info
->bridges
);
765 shash_init(&info
->ports
);
766 shash_init(&info
->ifaces
);
768 shash_init(&bridges
);
770 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
771 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
772 struct vsctl_bridge
*br
;
775 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
776 VLOG_WARN("%s: database contains duplicate bridge name",
780 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
785 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
786 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
788 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
789 VLOG_WARN("%s: database contains duplicate port name",
794 if (port_is_fake_bridge(port_cfg
)
795 && shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
796 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
800 shash_destroy(&bridges
);
801 shash_destroy(&ports
);
803 shash_init(&bridges
);
805 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
806 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
807 struct vsctl_bridge
*br
;
810 if (!shash_add_once(&bridges
, br_cfg
->name
, NULL
)) {
813 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
814 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
815 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
816 struct vsctl_port
*port
;
819 if (!shash_add_once(&ports
, port_cfg
->name
, NULL
)) {
823 if (port_is_fake_bridge(port_cfg
)
824 && !shash_add_once(&bridges
, port_cfg
->name
, NULL
)) {
828 port
= xmalloc(sizeof *port
);
829 port
->port_cfg
= port_cfg
;
831 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
832 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
839 shash_add(&info
->ports
, port_cfg
->name
, port
);
841 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
842 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
843 struct vsctl_iface
*iface
;
845 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
846 VLOG_WARN("%s: database contains duplicate interface name",
851 iface
= xmalloc(sizeof *iface
);
852 iface
->iface_cfg
= iface_cfg
;
854 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
858 shash_destroy(&bridges
);
859 shash_destroy(&ports
);
863 check_conflicts(struct vsctl_info
*info
, const char *name
,
866 struct vsctl_iface
*iface
;
867 struct vsctl_port
*port
;
869 verify_ports(info
->ctx
);
871 if (shash_find(&info
->bridges
, name
)) {
872 vsctl_fatal("%s because a bridge named %s already exists",
876 port
= shash_find_data(&info
->ports
, name
);
878 vsctl_fatal("%s because a port named %s already exists on "
879 "bridge %s", msg
, name
, port
->bridge
->name
);
882 iface
= shash_find_data(&info
->ifaces
, name
);
884 vsctl_fatal("%s because an interface named %s already exists "
885 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
891 static struct vsctl_bridge
*
892 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
894 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
895 if (must_exist
&& !br
) {
896 vsctl_fatal("no bridge named %s", name
);
898 ovsrec_open_vswitch_verify_bridges(info
->ctx
->ovs
);
902 static struct vsctl_bridge
*
903 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
905 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
906 if (br
&& br
->parent
) {
907 vsctl_fatal("%s is a fake bridge", name
);
912 static struct vsctl_port
*
913 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
915 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
916 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
919 if (must_exist
&& !port
) {
920 vsctl_fatal("no port named %s", name
);
922 verify_ports(info
->ctx
);
926 static struct vsctl_iface
*
927 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
929 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
930 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
933 if (must_exist
&& !iface
) {
934 vsctl_fatal("no interface named %s", name
);
936 verify_ports(info
->ctx
);
941 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
943 struct ovsrec_port
**ports
;
946 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
947 for (i
= 0; i
< br
->n_ports
; i
++) {
948 ports
[i
] = br
->ports
[i
];
950 ports
[br
->n_ports
] = port
;
951 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
956 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
958 struct ovsrec_port
**ports
;
961 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
962 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
963 if (br
->ports
[i
] != port
) {
964 ports
[n
++] = br
->ports
[i
];
967 ovsrec_bridge_set_ports(br
, ports
, n
);
972 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
973 struct ovsrec_bridge
*bridge
)
975 struct ovsrec_bridge
**bridges
;
978 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
979 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
980 bridges
[i
] = ovs
->bridges
[i
];
982 bridges
[ovs
->n_bridges
] = bridge
;
983 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
988 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
989 struct ovsrec_bridge
*bridge
)
991 struct ovsrec_bridge
**bridges
;
994 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
995 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
996 if (ovs
->bridges
[i
] != bridge
) {
997 bridges
[n
++] = ovs
->bridges
[i
];
1000 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
1005 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
1010 pre_cmd_emer_reset(struct vsctl_context
*ctx
)
1012 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1013 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1015 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
1016 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
1017 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_mirrors
);
1018 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_netflow
);
1019 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_sflow
);
1020 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_flood_vlans
);
1021 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_other_config
);
1023 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_other_config
);
1025 ovsdb_idl_add_column(ctx
->idl
,
1026 &ovsrec_interface_col_ingress_policing_rate
);
1027 ovsdb_idl_add_column(ctx
->idl
,
1028 &ovsrec_interface_col_ingress_policing_burst
);
1032 cmd_emer_reset(struct vsctl_context
*ctx
)
1034 const struct ovsdb_idl
*idl
= ctx
->idl
;
1035 const struct ovsrec_bridge
*br
;
1036 const struct ovsrec_port
*port
;
1037 const struct ovsrec_interface
*iface
;
1038 const struct ovsrec_mirror
*mirror
, *next_mirror
;
1039 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
1040 const struct ovsrec_manager
*mgr
, *next_mgr
;
1041 const struct ovsrec_netflow
*nf
, *next_nf
;
1042 const struct ovsrec_ssl
*ssl
, *next_ssl
;
1043 const struct ovsrec_sflow
*sflow
, *next_sflow
;
1045 /* Reset the Open_vSwitch table. */
1046 ovsrec_open_vswitch_set_manager_options(ctx
->ovs
, NULL
, 0);
1047 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1049 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
1051 char *hw_key
= "hwaddr";
1052 char *hw_val
= NULL
;
1054 ovsrec_bridge_set_controller(br
, NULL
, 0);
1055 ovsrec_bridge_set_fail_mode(br
, NULL
);
1056 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
1057 ovsrec_bridge_set_netflow(br
, NULL
);
1058 ovsrec_bridge_set_sflow(br
, NULL
);
1059 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
1061 /* We only want to save the "hwaddr" key from other_config. */
1062 for (i
=0; i
< br
->n_other_config
; i
++) {
1063 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
1064 hw_val
= br
->value_other_config
[i
];
1069 char *val
= xstrdup(hw_val
);
1070 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
1073 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
1077 OVSREC_PORT_FOR_EACH (port
, idl
) {
1078 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
1081 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
1082 /* xxx What do we do about gre/patch devices created by mgr? */
1084 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
1085 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
1088 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
1089 ovsrec_mirror_delete(mirror
);
1092 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
1093 ovsrec_controller_delete(ctrl
);
1096 OVSREC_MANAGER_FOR_EACH_SAFE (mgr
, next_mgr
, idl
) {
1097 ovsrec_manager_delete(mgr
);
1100 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
1101 ovsrec_netflow_delete(nf
);
1104 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
1105 ovsrec_ssl_delete(ssl
);
1108 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
1109 ovsrec_sflow_delete(sflow
);
1114 cmd_add_br(struct vsctl_context
*ctx
)
1116 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1117 const char *br_name
, *parent_name
;
1118 struct vsctl_info info
;
1121 br_name
= ctx
->argv
[1];
1122 if (ctx
->argc
== 2) {
1125 } else if (ctx
->argc
== 4) {
1126 parent_name
= ctx
->argv
[2];
1127 vlan
= atoi(ctx
->argv
[3]);
1128 if (vlan
< 1 || vlan
> 4095) {
1129 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
1132 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1136 get_info(ctx
, &info
);
1138 struct vsctl_bridge
*br
;
1140 br
= find_bridge(&info
, br_name
, false);
1144 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1145 "a VLAN bridge for VLAN %d",
1146 br_name
, br_name
, br
->vlan
);
1150 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1151 "is not a VLAN bridge",
1152 br_name
, parent_name
, vlan
, br_name
);
1153 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1154 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1155 "has the wrong parent %s",
1156 br_name
, parent_name
, vlan
,
1157 br_name
, br
->parent
->name
);
1158 } else if (br
->vlan
!= vlan
) {
1159 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1160 "is a VLAN bridge for the wrong VLAN %d",
1161 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1167 check_conflicts(&info
, br_name
,
1168 xasprintf("cannot create a bridge named %s", br_name
));
1171 struct ovsrec_port
*port
;
1172 struct ovsrec_interface
*iface
;
1173 struct ovsrec_bridge
*br
;
1175 iface
= ovsrec_interface_insert(ctx
->txn
);
1176 ovsrec_interface_set_name(iface
, br_name
);
1177 ovsrec_interface_set_type(iface
, "internal");
1179 port
= ovsrec_port_insert(ctx
->txn
);
1180 ovsrec_port_set_name(port
, br_name
);
1181 ovsrec_port_set_interfaces(port
, &iface
, 1);
1183 br
= ovsrec_bridge_insert(ctx
->txn
);
1184 ovsrec_bridge_set_name(br
, br_name
);
1185 ovsrec_bridge_set_ports(br
, &port
, 1);
1187 ovs_insert_bridge(ctx
->ovs
, br
);
1189 struct vsctl_bridge
*parent
;
1190 struct ovsrec_port
*port
;
1191 struct ovsrec_interface
*iface
;
1192 struct ovsrec_bridge
*br
;
1195 parent
= find_bridge(&info
, parent_name
, false);
1196 if (parent
&& parent
->vlan
) {
1197 vsctl_fatal("cannot create bridge with fake bridge as parent");
1200 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1202 br
= parent
->br_cfg
;
1204 iface
= ovsrec_interface_insert(ctx
->txn
);
1205 ovsrec_interface_set_name(iface
, br_name
);
1206 ovsrec_interface_set_type(iface
, "internal");
1208 port
= ovsrec_port_insert(ctx
->txn
);
1209 ovsrec_port_set_name(port
, br_name
);
1210 ovsrec_port_set_interfaces(port
, &iface
, 1);
1211 ovsrec_port_set_fake_bridge(port
, true);
1212 ovsrec_port_set_tag(port
, &tag
, 1);
1214 bridge_insert_port(br
, port
);
1221 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1223 struct shash_node
*node
;
1225 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1226 struct vsctl_iface
*iface
= node
->data
;
1227 if (iface
->port
== port
) {
1228 ovsrec_interface_delete(iface
->iface_cfg
);
1231 ovsrec_port_delete(port
->port_cfg
);
1233 bridge_delete_port((port
->bridge
->parent
1234 ? port
->bridge
->parent
->br_cfg
1235 : port
->bridge
->br_cfg
), port
->port_cfg
);
1239 cmd_del_br(struct vsctl_context
*ctx
)
1241 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1242 struct vsctl_bridge
*bridge
;
1243 struct vsctl_info info
;
1245 get_info(ctx
, &info
);
1246 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1248 struct shash_node
*node
;
1250 SHASH_FOR_EACH (node
, &info
.ports
) {
1251 struct vsctl_port
*port
= node
->data
;
1252 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1253 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1254 del_port(&info
, port
);
1257 if (bridge
->br_cfg
) {
1258 ovsrec_bridge_delete(bridge
->br_cfg
);
1259 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1266 output_sorted(struct svec
*svec
, struct ds
*output
)
1272 SVEC_FOR_EACH (i
, name
, svec
) {
1273 ds_put_format(output
, "%s\n", name
);
1278 cmd_list_br(struct vsctl_context
*ctx
)
1280 struct shash_node
*node
;
1281 struct vsctl_info info
;
1282 struct svec bridges
;
1284 get_info(ctx
, &info
);
1286 svec_init(&bridges
);
1287 SHASH_FOR_EACH (node
, &info
.bridges
) {
1288 struct vsctl_bridge
*br
= node
->data
;
1289 svec_add(&bridges
, br
->name
);
1291 output_sorted(&bridges
, &ctx
->output
);
1292 svec_destroy(&bridges
);
1298 cmd_br_exists(struct vsctl_context
*ctx
)
1300 struct vsctl_info info
;
1302 get_info(ctx
, &info
);
1303 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1309 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1310 * equals 'a', false otherwise. */
1312 key_matches(const char *a
,
1313 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1315 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1319 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1320 char *key
, char *value
,
1321 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1328 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1329 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1331 for (i
= 0; i
< old_n
; i
++) {
1332 if (strcmp(key
, old_keys
[i
])) {
1333 new_keys
[new_n
] = old_keys
[i
];
1334 new_values
[new_n
] = old_values
[i
];
1339 new_keys
[new_n
] = key
;
1340 new_values
[new_n
] = value
;
1343 *new_keysp
= new_keys
;
1344 *new_valuesp
= new_values
;
1349 pre_cmd_br_set_external_id(struct vsctl_context
*ctx
)
1352 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_external_ids
);
1353 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_external_ids
);
1357 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1359 struct vsctl_info info
;
1360 struct vsctl_bridge
*bridge
;
1361 char **keys
, **values
;
1364 get_info(ctx
, &info
);
1365 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1366 if (bridge
->br_cfg
) {
1367 set_external_id(bridge
->br_cfg
->key_external_ids
,
1368 bridge
->br_cfg
->value_external_ids
,
1369 bridge
->br_cfg
->n_external_ids
,
1370 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1371 &keys
, &values
, &n
);
1372 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1373 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1375 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1376 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1377 set_external_id(port
->port_cfg
->key_external_ids
,
1378 port
->port_cfg
->value_external_ids
,
1379 port
->port_cfg
->n_external_ids
,
1380 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1381 &keys
, &values
, &n
);
1382 ovsrec_port_verify_external_ids(port
->port_cfg
);
1383 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1393 get_external_id(char **keys
, char **values
, size_t n
,
1394 const char *prefix
, const char *key
,
1397 size_t prefix_len
= strlen(prefix
);
1402 for (i
= 0; i
< n
; i
++) {
1403 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1404 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1405 keys
[i
] + prefix_len
, values
[i
]));
1406 } else if (key
&& key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1407 svec_add(&svec
, values
[i
]);
1411 output_sorted(&svec
, output
);
1412 svec_destroy(&svec
);
1416 pre_cmd_br_get_external_id(struct vsctl_context
*ctx
)
1418 pre_cmd_br_set_external_id(ctx
);
1422 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1424 struct vsctl_info info
;
1425 struct vsctl_bridge
*bridge
;
1427 get_info(ctx
, &info
);
1428 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1429 if (bridge
->br_cfg
) {
1430 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1431 get_external_id(bridge
->br_cfg
->key_external_ids
,
1432 bridge
->br_cfg
->value_external_ids
,
1433 bridge
->br_cfg
->n_external_ids
,
1434 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1437 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1438 ovsrec_port_verify_external_ids(port
->port_cfg
);
1439 get_external_id(port
->port_cfg
->key_external_ids
,
1440 port
->port_cfg
->value_external_ids
,
1441 port
->port_cfg
->n_external_ids
,
1442 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1449 cmd_list_ports(struct vsctl_context
*ctx
)
1451 struct vsctl_bridge
*br
;
1452 struct shash_node
*node
;
1453 struct vsctl_info info
;
1456 get_info(ctx
, &info
);
1457 br
= find_bridge(&info
, ctx
->argv
[1], true);
1458 ovsrec_bridge_verify_ports(br
->br_cfg
? br
->br_cfg
: br
->parent
->br_cfg
);
1461 SHASH_FOR_EACH (node
, &info
.ports
) {
1462 struct vsctl_port
*port
= node
->data
;
1464 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1465 svec_add(&ports
, port
->port_cfg
->name
);
1468 output_sorted(&ports
, &ctx
->output
);
1469 svec_destroy(&ports
);
1475 add_port(struct vsctl_context
*ctx
,
1476 const char *br_name
, const char *port_name
,
1477 bool may_exist
, bool fake_iface
,
1478 char *iface_names
[], int n_ifaces
,
1479 char *settings
[], int n_settings
)
1481 struct vsctl_info info
;
1482 struct vsctl_bridge
*bridge
;
1483 struct ovsrec_interface
**ifaces
;
1484 struct ovsrec_port
*port
;
1487 get_info(ctx
, &info
);
1489 struct vsctl_port
*vsctl_port
;
1491 vsctl_port
= find_port(&info
, port_name
, false);
1493 struct svec want_names
, have_names
;
1495 svec_init(&want_names
);
1496 for (i
= 0; i
< n_ifaces
; i
++) {
1497 svec_add(&want_names
, iface_names
[i
]);
1499 svec_sort(&want_names
);
1501 svec_init(&have_names
);
1502 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1503 svec_add(&have_names
,
1504 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1506 svec_sort(&have_names
);
1508 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1509 char *command
= vsctl_context_to_string(ctx
);
1510 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1511 command
, port_name
, vsctl_port
->bridge
->name
);
1514 if (!svec_equal(&want_names
, &have_names
)) {
1515 char *have_names_string
= svec_join(&have_names
, ", ", "");
1516 char *command
= vsctl_context_to_string(ctx
);
1518 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1519 command
, port_name
, have_names_string
);
1522 svec_destroy(&want_names
);
1523 svec_destroy(&have_names
);
1528 check_conflicts(&info
, port_name
,
1529 xasprintf("cannot create a port named %s", port_name
));
1530 for (i
= 0; i
< n_ifaces
; i
++) {
1531 check_conflicts(&info
, iface_names
[i
],
1532 xasprintf("cannot create an interface named %s",
1535 bridge
= find_bridge(&info
, br_name
, true);
1537 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1538 for (i
= 0; i
< n_ifaces
; i
++) {
1539 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1540 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1543 port
= ovsrec_port_insert(ctx
->txn
);
1544 ovsrec_port_set_name(port
, port_name
);
1545 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1546 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1550 int64_t tag
= bridge
->vlan
;
1551 ovsrec_port_set_tag(port
, &tag
, 1);
1554 for (i
= 0; i
< n_settings
; i
++) {
1555 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1559 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1560 : bridge
->br_cfg
), port
);
1566 cmd_add_port(struct vsctl_context
*ctx
)
1568 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1570 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1571 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1575 cmd_add_bond(struct vsctl_context
*ctx
)
1577 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != 0;
1578 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1582 n_ifaces
= ctx
->argc
- 3;
1583 for (i
= 3; i
< ctx
->argc
; i
++) {
1584 if (strchr(ctx
->argv
[i
], '=')) {
1590 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1591 "%d were specified", n_ifaces
);
1594 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1595 &ctx
->argv
[3], n_ifaces
,
1596 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1600 cmd_del_port(struct vsctl_context
*ctx
)
1602 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1603 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1604 struct vsctl_port
*port
;
1605 struct vsctl_info info
;
1607 get_info(ctx
, &info
);
1609 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1611 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1612 struct vsctl_iface
*iface
;
1614 port
= find_port(&info
, target
, false);
1616 iface
= find_iface(&info
, target
, false);
1621 if (must_exist
&& !port
) {
1622 vsctl_fatal("no port or interface named %s", target
);
1627 if (ctx
->argc
== 3) {
1628 struct vsctl_bridge
*bridge
;
1630 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1631 if (port
->bridge
!= bridge
) {
1632 if (port
->bridge
->parent
== bridge
) {
1633 vsctl_fatal("bridge %s does not have a port %s (although "
1634 "its parent bridge %s does)",
1635 ctx
->argv
[1], ctx
->argv
[2],
1636 bridge
->parent
->name
);
1638 vsctl_fatal("bridge %s does not have a port %s",
1639 ctx
->argv
[1], ctx
->argv
[2]);
1644 del_port(&info
, port
);
1651 cmd_port_to_br(struct vsctl_context
*ctx
)
1653 struct vsctl_port
*port
;
1654 struct vsctl_info info
;
1656 get_info(ctx
, &info
);
1657 port
= find_port(&info
, ctx
->argv
[1], true);
1658 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1663 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1665 struct vsctl_bridge
*bridge
;
1666 struct vsctl_info info
;
1668 get_info(ctx
, &info
);
1669 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1670 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1675 cmd_br_to_parent(struct vsctl_context
*ctx
)
1677 struct vsctl_bridge
*bridge
;
1678 struct vsctl_info info
;
1680 get_info(ctx
, &info
);
1681 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1682 if (bridge
->parent
) {
1683 bridge
= bridge
->parent
;
1685 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1690 cmd_list_ifaces(struct vsctl_context
*ctx
)
1692 struct vsctl_bridge
*br
;
1693 struct shash_node
*node
;
1694 struct vsctl_info info
;
1697 get_info(ctx
, &info
);
1698 br
= find_bridge(&info
, ctx
->argv
[1], true);
1702 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1703 struct vsctl_iface
*iface
= node
->data
;
1705 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1706 && br
== iface
->port
->bridge
) {
1707 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1710 output_sorted(&ifaces
, &ctx
->output
);
1711 svec_destroy(&ifaces
);
1717 cmd_iface_to_br(struct vsctl_context
*ctx
)
1719 struct vsctl_iface
*iface
;
1720 struct vsctl_info info
;
1722 get_info(ctx
, &info
);
1723 iface
= find_iface(&info
, ctx
->argv
[1], true);
1724 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1729 verify_controllers(struct ovsrec_bridge
*bridge
)
1734 ovsrec_bridge_verify_controller(bridge
);
1735 for (i
= 0; i
< bridge
->n_controller
; i
++) {
1736 ovsrec_controller_verify_target(bridge
->controller
[i
]);
1742 pre_controller(struct vsctl_context
*ctx
)
1746 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_controller_col_target
);
1750 cmd_get_controller(struct vsctl_context
*ctx
)
1752 struct vsctl_info info
;
1753 struct vsctl_bridge
*br
;
1754 struct svec targets
;
1757 get_info(ctx
, &info
);
1758 br
= find_bridge(&info
, ctx
->argv
[1], true);
1759 verify_controllers(br
->br_cfg
);
1761 /* Print the targets in sorted order for reproducibility. */
1762 svec_init(&targets
);
1763 for (i
= 0; i
< br
->n_ctrl
; i
++) {
1764 svec_add(&targets
, br
->ctrl
[i
]->target
);
1767 svec_sort(&targets
);
1768 for (i
= 0; i
< targets
.n
; i
++) {
1769 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1771 svec_destroy(&targets
);
1777 delete_controllers(struct ovsrec_controller
**controllers
,
1778 size_t n_controllers
)
1782 for (i
= 0; i
< n_controllers
; i
++) {
1783 ovsrec_controller_delete(controllers
[i
]);
1788 cmd_del_controller(struct vsctl_context
*ctx
)
1790 struct vsctl_info info
;
1791 struct vsctl_bridge
*br
;
1793 get_info(ctx
, &info
);
1795 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1796 verify_controllers(br
->br_cfg
);
1799 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1800 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1806 static struct ovsrec_controller
**
1807 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1809 struct ovsrec_controller
**controllers
;
1812 controllers
= xmalloc(n
* sizeof *controllers
);
1813 for (i
= 0; i
< n
; i
++) {
1814 controllers
[i
] = ovsrec_controller_insert(txn
);
1815 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1822 cmd_set_controller(struct vsctl_context
*ctx
)
1824 struct vsctl_info info
;
1825 struct vsctl_bridge
*br
;
1826 struct ovsrec_controller
**controllers
;
1829 get_info(ctx
, &info
);
1830 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1831 verify_controllers(br
->br_cfg
);
1833 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1836 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1837 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1844 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1846 struct vsctl_info info
;
1847 struct vsctl_bridge
*br
;
1849 get_info(ctx
, &info
);
1850 br
= find_bridge(&info
, ctx
->argv
[1], true);
1853 ovsrec_bridge_verify_fail_mode(br
->br_cfg
);
1855 if (br
->fail_mode
&& strlen(br
->fail_mode
)) {
1856 ds_put_format(&ctx
->output
, "%s\n", br
->fail_mode
);
1863 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1865 struct vsctl_info info
;
1866 struct vsctl_bridge
*br
;
1868 get_info(ctx
, &info
);
1869 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1871 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1877 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1879 struct vsctl_info info
;
1880 struct vsctl_bridge
*br
;
1881 const char *fail_mode
= ctx
->argv
[2];
1883 get_info(ctx
, &info
);
1884 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1886 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1887 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1890 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1896 verify_managers(const struct ovsrec_open_vswitch
*ovs
)
1900 ovsrec_open_vswitch_verify_manager_options(ovs
);
1902 for (i
= 0; i
< ovs
->n_manager_options
; ++i
) {
1903 const struct ovsrec_manager
*mgr
= ovs
->manager_options
[i
];
1905 ovsrec_manager_verify_target(mgr
);
1910 pre_manager(struct vsctl_context
*ctx
)
1912 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1913 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_manager_col_target
);
1917 cmd_get_manager(struct vsctl_context
*ctx
)
1919 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
1920 struct svec targets
;
1923 verify_managers(ovs
);
1925 /* Print the targets in sorted order for reproducibility. */
1926 svec_init(&targets
);
1928 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1929 svec_add(&targets
, ovs
->manager_options
[i
]->target
);
1932 svec_sort_unique(&targets
);
1933 for (i
= 0; i
< targets
.n
; i
++) {
1934 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1936 svec_destroy(&targets
);
1940 delete_managers(const struct vsctl_context
*ctx
)
1942 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
1945 /* Delete Manager rows pointed to by 'manager_options' column. */
1946 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1947 ovsrec_manager_delete(ovs
->manager_options
[i
]);
1950 /* Delete 'Manager' row refs in 'manager_options' column. */
1951 ovsrec_open_vswitch_set_manager_options(ovs
, NULL
, 0);
1955 cmd_del_manager(struct vsctl_context
*ctx
)
1957 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
1959 verify_managers(ovs
);
1960 delete_managers(ctx
);
1964 insert_managers(struct vsctl_context
*ctx
, char *targets
[], size_t n
)
1966 struct ovsrec_manager
**managers
;
1969 /* Insert each manager in a new row in Manager table. */
1970 managers
= xmalloc(n
* sizeof *managers
);
1971 for (i
= 0; i
< n
; i
++) {
1972 managers
[i
] = ovsrec_manager_insert(ctx
->txn
);
1973 ovsrec_manager_set_target(managers
[i
], targets
[i
]);
1976 /* Store uuids of new Manager rows in 'manager_options' column. */
1977 ovsrec_open_vswitch_set_manager_options(ctx
->ovs
, managers
, n
);
1982 cmd_set_manager(struct vsctl_context
*ctx
)
1984 const size_t n
= ctx
->argc
- 1;
1986 verify_managers(ctx
->ovs
);
1987 delete_managers(ctx
);
1988 insert_managers(ctx
, &ctx
->argv
[1], n
);
1992 pre_cmd_get_ssl(struct vsctl_context
*ctx
)
1994 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1996 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_private_key
);
1997 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_certificate
);
1998 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_ca_cert
);
1999 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_bootstrap_ca_cert
);
2003 cmd_get_ssl(struct vsctl_context
*ctx
)
2005 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
2007 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
2009 ovsrec_ssl_verify_private_key(ssl
);
2010 ovsrec_ssl_verify_certificate(ssl
);
2011 ovsrec_ssl_verify_ca_cert(ssl
);
2012 ovsrec_ssl_verify_bootstrap_ca_cert(ssl
);
2014 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
2015 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
2016 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
2017 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
2018 ssl
->bootstrap_ca_cert
? "true" : "false");
2023 pre_cmd_del_ssl(struct vsctl_context
*ctx
)
2025 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2029 cmd_del_ssl(struct vsctl_context
*ctx
)
2031 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
2034 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
2035 ovsrec_ssl_delete(ssl
);
2036 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
2041 pre_cmd_set_ssl(struct vsctl_context
*ctx
)
2043 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2047 cmd_set_ssl(struct vsctl_context
*ctx
)
2049 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
2050 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
2052 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
2054 ovsrec_ssl_delete(ssl
);
2056 ssl
= ovsrec_ssl_insert(ctx
->txn
);
2058 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
2059 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
2060 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
2062 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
2064 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
2067 /* Parameter commands. */
2069 struct vsctl_row_id
{
2070 const struct ovsdb_idl_table_class
*table
;
2071 const struct ovsdb_idl_column
*name_column
;
2072 const struct ovsdb_idl_column
*uuid_column
;
2075 struct vsctl_table_class
{
2076 struct ovsdb_idl_table_class
*class;
2077 struct vsctl_row_id row_ids
[2];
2080 static const struct vsctl_table_class tables
[] = {
2081 {&ovsrec_table_bridge
,
2082 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
2083 {NULL
, NULL
, NULL
}}},
2085 {&ovsrec_table_controller
,
2086 {{&ovsrec_table_bridge
,
2087 &ovsrec_bridge_col_name
,
2088 &ovsrec_bridge_col_controller
}}},
2090 {&ovsrec_table_interface
,
2091 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
2092 {NULL
, NULL
, NULL
}}},
2094 {&ovsrec_table_mirror
,
2095 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
2096 {NULL
, NULL
, NULL
}}},
2098 {&ovsrec_table_manager
,
2099 {{&ovsrec_table_manager
, &ovsrec_manager_col_target
, NULL
},
2100 {NULL
, NULL
, NULL
}}},
2102 {&ovsrec_table_netflow
,
2103 {{&ovsrec_table_bridge
,
2104 &ovsrec_bridge_col_name
,
2105 &ovsrec_bridge_col_netflow
},
2106 {NULL
, NULL
, NULL
}}},
2108 {&ovsrec_table_open_vswitch
,
2109 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
2110 {NULL
, NULL
, NULL
}}},
2112 {&ovsrec_table_port
,
2113 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
2114 {NULL
, NULL
, NULL
}}},
2117 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
2118 {NULL
, NULL
, NULL
}}},
2120 {&ovsrec_table_monitor
,
2121 {{&ovsrec_table_interface
,
2122 &ovsrec_interface_col_name
,
2123 &ovsrec_interface_col_monitor
},
2124 {NULL
, NULL
, NULL
}}},
2126 {&ovsrec_table_maintenance_point
,
2127 {{NULL
, NULL
, NULL
},
2128 {NULL
, NULL
, NULL
}}},
2130 {&ovsrec_table_queue
,
2131 {{NULL
, NULL
, NULL
},
2132 {NULL
, NULL
, NULL
}}},
2135 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
2137 {&ovsrec_table_sflow
,
2138 {{&ovsrec_table_bridge
,
2139 &ovsrec_bridge_col_name
,
2140 &ovsrec_bridge_col_sflow
},
2141 {NULL
, NULL
, NULL
}}},
2143 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
2147 die_if_error(char *error
)
2150 vsctl_fatal("%s", error
);
2155 to_lower_and_underscores(unsigned c
)
2157 return c
== '-' ? '_' : tolower(c
);
2161 score_partial_match(const char *name
, const char *s
)
2165 if (!strcmp(name
, s
)) {
2168 for (score
= 0; ; score
++, name
++, s
++) {
2169 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
2171 } else if (*name
== '\0') {
2172 return UINT_MAX
- 1;
2175 return *s
== '\0' ? score
: 0;
2178 static const struct vsctl_table_class
*
2179 get_table(const char *table_name
)
2181 const struct vsctl_table_class
*table
;
2182 const struct vsctl_table_class
*best_match
= NULL
;
2183 unsigned int best_score
= 0;
2185 for (table
= tables
; table
->class; table
++) {
2186 unsigned int score
= score_partial_match(table
->class->name
,
2188 if (score
> best_score
) {
2191 } else if (score
== best_score
) {
2197 } else if (best_score
) {
2198 vsctl_fatal("multiple table names match \"%s\"", table_name
);
2200 vsctl_fatal("unknown table \"%s\"", table_name
);
2204 static const struct vsctl_table_class
*
2205 pre_get_table(struct vsctl_context
*ctx
, const char *table_name
)
2207 const struct vsctl_table_class
*table_class
;
2210 table_class
= get_table(table_name
);
2211 ovsdb_idl_add_table(ctx
->idl
, table_class
->class);
2213 for (i
= 0; i
< ARRAY_SIZE(table_class
->row_ids
); i
++) {
2214 const struct vsctl_row_id
*id
= &table_class
->row_ids
[i
];
2216 ovsdb_idl_add_table(ctx
->idl
, id
->table
);
2218 if (id
->name_column
) {
2219 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
2221 if (id
->uuid_column
) {
2222 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
2229 static const struct ovsdb_idl_row
*
2230 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
2231 const struct vsctl_row_id
*id
, const char *record_id
)
2233 const struct ovsdb_idl_row
*referrer
, *final
;
2239 if (!id
->name_column
) {
2240 if (strcmp(record_id
, ".")) {
2243 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2244 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
2248 const struct ovsdb_idl_row
*row
;
2251 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2253 row
= ovsdb_idl_next_row(row
))
2255 const struct ovsdb_datum
*name
;
2257 name
= ovsdb_idl_get(row
, id
->name_column
,
2258 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
2259 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
2261 vsctl_fatal("multiple rows in %s match \"%s\"",
2262 table
->class->name
, record_id
);
2273 if (id
->uuid_column
) {
2274 const struct ovsdb_datum
*uuid
;
2276 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
2277 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
2278 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
2280 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
2281 &uuid
->keys
[0].uuid
);
2290 static const struct ovsdb_idl_row
*
2291 get_row (struct vsctl_context
*ctx
,
2292 const struct vsctl_table_class
*table
, const char *record_id
)
2294 const struct ovsdb_idl_row
*row
;
2297 if (uuid_from_string(&uuid
, record_id
)) {
2298 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
2302 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
2303 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
2312 static const struct ovsdb_idl_row
*
2313 must_get_row(struct vsctl_context
*ctx
,
2314 const struct vsctl_table_class
*table
, const char *record_id
)
2316 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
2318 vsctl_fatal("no row \"%s\" in table %s",
2319 record_id
, table
->class->name
);
2325 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
2326 const struct ovsdb_idl_column
**columnp
)
2328 const struct ovsdb_idl_column
*best_match
= NULL
;
2329 unsigned int best_score
= 0;
2332 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2333 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2334 unsigned int score
= score_partial_match(column
->name
, column_name
);
2335 if (score
> best_score
) {
2336 best_match
= column
;
2338 } else if (score
== best_score
) {
2343 *columnp
= best_match
;
2346 } else if (best_score
) {
2347 return xasprintf("%s contains more than one column whose name "
2348 "matches \"%s\"", table
->class->name
, column_name
);
2350 return xasprintf("%s does not contain a column whose name matches "
2351 "\"%s\"", table
->class->name
, column_name
);
2355 static struct ovsdb_symbol
*
2356 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
2358 struct ovsdb_symbol
*symbol
;
2361 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2365 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
2368 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
2369 if (symbol
->created
) {
2370 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2373 symbol
->created
= true;
2378 pre_get_column(struct vsctl_context
*ctx
,
2379 const struct vsctl_table_class
*table
, const char *column_name
,
2380 const struct ovsdb_idl_column
**columnp
)
2382 die_if_error(get_column(table
, column_name
, columnp
));
2383 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
2387 missing_operator_error(const char *arg
, const char **allowed_operators
,
2393 ds_put_format(&s
, "%s: argument does not end in ", arg
);
2394 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
2395 if (n_allowed
== 2) {
2396 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
2397 } else if (n_allowed
> 2) {
2400 for (i
= 1; i
< n_allowed
- 1; i
++) {
2401 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
2403 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
2405 ds_put_format(&s
, " followed by a value.");
2407 return ds_steal_cstr(&s
);
2410 /* Breaks 'arg' apart into a number of fields in the following order:
2412 * - The name of a column in 'table', stored into '*columnp'. The column
2413 * name may be abbreviated.
2415 * - Optionally ':' followed by a key string. The key is stored as a
2416 * malloc()'d string into '*keyp', or NULL if no key is present in
2419 * - If 'valuep' is nonnull, an operator followed by a value string. The
2420 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2421 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2422 * operator is stored into '*operatorp' (one of the pointers from
2423 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2424 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2427 * On success, returns NULL. On failure, returned a malloc()'d string error
2428 * message and stores NULL into all of the nonnull output arguments. */
2429 static char * WARN_UNUSED_RESULT
2430 parse_column_key_value(const char *arg
,
2431 const struct vsctl_table_class
*table
,
2432 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2433 const char **operatorp
,
2434 const char **allowed_operators
, size_t n_allowed
,
2437 const char *p
= arg
;
2441 assert(!(operatorp
&& !valuep
));
2447 /* Parse column name. */
2448 error
= ovsdb_token_parse(&p
, &column_name
);
2452 if (column_name
[0] == '\0') {
2454 error
= xasprintf("%s: missing column name", arg
);
2457 error
= get_column(table
, column_name
, columnp
);
2463 /* Parse key string. */
2466 error
= ovsdb_token_parse(&p
, keyp
);
2472 /* Parse value string. */
2478 if (!allowed_operators
) {
2479 static const char *equals
= "=";
2480 allowed_operators
= &equals
;
2486 for (i
= 0; i
< n_allowed
; i
++) {
2487 const char *op
= allowed_operators
[i
];
2488 size_t op_len
= strlen(op
);
2490 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2496 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2503 *valuep
= xstrdup(p
+ best_len
);
2506 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2528 pre_parse_column_key_value(struct vsctl_context
*ctx
,
2530 const struct vsctl_table_class
*table
)
2532 const struct ovsdb_idl_column
*column
;
2537 die_if_error(ovsdb_token_parse(&p
, &column_name
));
2538 if (column_name
[0] == '\0') {
2539 vsctl_fatal("%s: missing column name", arg
);
2542 pre_get_column(ctx
, table
, column_name
, &column
);
2547 pre_cmd_get(struct vsctl_context
*ctx
)
2549 const char *table_name
= ctx
->argv
[1];
2550 const struct vsctl_table_class
*table
;
2553 table
= pre_get_table(ctx
, table_name
);
2554 for (i
= 3; i
< ctx
->argc
; i
++) {
2555 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2556 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2560 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2565 cmd_get(struct vsctl_context
*ctx
)
2567 const char *id
= shash_find_data(&ctx
->options
, "--id");
2568 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2569 const char *table_name
= ctx
->argv
[1];
2570 const char *record_id
= ctx
->argv
[2];
2571 const struct vsctl_table_class
*table
;
2572 const struct ovsdb_idl_row
*row
;
2573 struct ds
*out
= &ctx
->output
;
2576 table
= get_table(table_name
);
2577 row
= must_get_row(ctx
, table
, record_id
);
2579 struct ovsdb_symbol
*symbol
;
2582 symbol
= create_symbol(ctx
->symtab
, id
, &new);
2584 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2585 "before it was defined", id
);
2587 symbol
->uuid
= row
->uuid
;
2589 /* This symbol refers to a row that already exists, so disable warnings
2590 * about it being unreferenced. */
2591 symbol
->strong_ref
= true;
2593 for (i
= 3; i
< ctx
->argc
; i
++) {
2594 const struct ovsdb_idl_column
*column
;
2595 const struct ovsdb_datum
*datum
;
2598 /* Special case for obtaining the UUID of a row. We can't just do this
2599 * through parse_column_key_value() below since it returns a "struct
2600 * ovsdb_idl_column" and the UUID column doesn't have one. */
2601 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2602 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2603 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2607 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2608 &column
, &key_string
,
2609 NULL
, NULL
, 0, NULL
));
2611 ovsdb_idl_txn_verify(row
, column
);
2612 datum
= ovsdb_idl_read(row
, column
);
2614 union ovsdb_atom key
;
2617 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2618 vsctl_fatal("cannot specify key to get for non-map column %s",
2622 die_if_error(ovsdb_atom_from_string(&key
,
2624 key_string
, ctx
->symtab
));
2626 idx
= ovsdb_datum_find_key(datum
, &key
,
2627 column
->type
.key
.type
);
2628 if (idx
== UINT_MAX
) {
2630 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2631 key_string
, table
->class->name
, record_id
,
2635 ovsdb_atom_to_string(&datum
->values
[idx
],
2636 column
->type
.value
.type
, out
);
2638 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2640 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2642 ds_put_char(out
, '\n');
2649 parse_column_names(const char *column_names
,
2650 const struct vsctl_table_class
*table
,
2651 const struct ovsdb_idl_column
***columnsp
,
2654 const struct ovsdb_idl_column
**columns
;
2657 if (!column_names
) {
2660 n_columns
= table
->class->n_columns
+ 1;
2661 columns
= xmalloc(n_columns
* sizeof *columns
);
2663 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2664 columns
[i
+ 1] = &table
->class->columns
[i
];
2667 char *s
= xstrdup(column_names
);
2668 size_t allocated_columns
;
2669 char *save_ptr
= NULL
;
2673 allocated_columns
= n_columns
= 0;
2674 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
2675 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
2676 const struct ovsdb_idl_column
*column
;
2678 if (!strcasecmp(column_name
, "_uuid")) {
2681 die_if_error(get_column(table
, column_name
, &column
));
2683 if (n_columns
>= allocated_columns
) {
2684 columns
= x2nrealloc(columns
, &allocated_columns
,
2687 columns
[n_columns
++] = column
;
2692 vsctl_fatal("must specify at least one column name");
2695 *columnsp
= columns
;
2696 *n_columnsp
= n_columns
;
2701 pre_list_columns(struct vsctl_context
*ctx
,
2702 const struct vsctl_table_class
*table
,
2703 const char *column_names
)
2705 const struct ovsdb_idl_column
**columns
;
2709 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2710 for (i
= 0; i
< n_columns
; i
++) {
2712 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
2719 pre_cmd_list(struct vsctl_context
*ctx
)
2721 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2722 const char *table_name
= ctx
->argv
[1];
2723 const struct vsctl_table_class
*table
;
2725 table
= pre_get_table(ctx
, table_name
);
2726 pre_list_columns(ctx
, table
, column_names
);
2729 static struct table
*
2730 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
2735 out
= xmalloc(sizeof *out
);
2738 for (i
= 0; i
< n_columns
; i
++) {
2739 const struct ovsdb_idl_column
*column
= columns
[i
];
2740 const char *column_name
= column
? column
->name
: "_uuid";
2742 table_add_column(out
, "%s", column_name
);
2749 list_record(const struct ovsdb_idl_row
*row
,
2750 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
2756 for (i
= 0; i
< n_columns
; i
++) {
2757 const struct ovsdb_idl_column
*column
= columns
[i
];
2758 struct cell
*cell
= table_add_cell(out
);
2761 struct ovsdb_datum datum
;
2762 union ovsdb_atom atom
;
2764 atom
.uuid
= row
->uuid
;
2767 datum
.values
= NULL
;
2770 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
2771 cell
->type
= &ovsdb_type_uuid
;
2773 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
2775 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
2776 cell
->type
= &column
->type
;
2782 cmd_list(struct vsctl_context
*ctx
)
2784 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2785 const struct ovsdb_idl_column
**columns
;
2786 const char *table_name
= ctx
->argv
[1];
2787 const struct vsctl_table_class
*table
;
2792 table
= get_table(table_name
);
2793 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2794 out
= ctx
->table
= list_make_table(columns
, n_columns
);
2795 if (ctx
->argc
> 2) {
2796 for (i
= 2; i
< ctx
->argc
; i
++) {
2797 list_record(must_get_row(ctx
, table
, ctx
->argv
[i
]),
2798 columns
, n_columns
, out
);
2801 const struct ovsdb_idl_row
*row
;
2803 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
!= NULL
;
2804 row
= ovsdb_idl_next_row(row
)) {
2805 list_record(row
, columns
, n_columns
, out
);
2812 pre_cmd_find(struct vsctl_context
*ctx
)
2814 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2815 const char *table_name
= ctx
->argv
[1];
2816 const struct vsctl_table_class
*table
;
2819 table
= pre_get_table(ctx
, table_name
);
2820 pre_list_columns(ctx
, table
, column_names
);
2821 for (i
= 2; i
< ctx
->argc
; i
++) {
2822 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2827 cmd_find(struct vsctl_context
*ctx
)
2829 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2830 const struct ovsdb_idl_column
**columns
;
2831 const char *table_name
= ctx
->argv
[1];
2832 const struct vsctl_table_class
*table
;
2833 const struct ovsdb_idl_row
*row
;
2837 table
= get_table(table_name
);
2838 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2839 out
= ctx
->table
= list_make_table(columns
, n_columns
);
2840 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
;
2841 row
= ovsdb_idl_next_row(row
)) {
2844 for (i
= 2; i
< ctx
->argc
; i
++) {
2845 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
],
2850 list_record(row
, columns
, n_columns
, out
);
2858 pre_cmd_set(struct vsctl_context
*ctx
)
2860 const char *table_name
= ctx
->argv
[1];
2861 const struct vsctl_table_class
*table
;
2864 table
= pre_get_table(ctx
, table_name
);
2865 for (i
= 3; i
< ctx
->argc
; i
++) {
2866 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2871 set_column(const struct vsctl_table_class
*table
,
2872 const struct ovsdb_idl_row
*row
, const char *arg
,
2873 struct ovsdb_symbol_table
*symtab
)
2875 const struct ovsdb_idl_column
*column
;
2876 char *key_string
, *value_string
;
2879 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2880 NULL
, NULL
, 0, &value_string
);
2881 die_if_error(error
);
2882 if (!value_string
) {
2883 vsctl_fatal("%s: missing value", arg
);
2887 union ovsdb_atom key
, value
;
2888 struct ovsdb_datum datum
;
2890 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2891 vsctl_fatal("cannot specify key to set for non-map column %s",
2895 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2896 key_string
, symtab
));
2897 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2898 value_string
, symtab
));
2900 ovsdb_datum_init_empty(&datum
);
2901 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
2903 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2904 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2906 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
2907 &column
->type
, false);
2908 ovsdb_idl_txn_write(row
, column
, &datum
);
2910 struct ovsdb_datum datum
;
2912 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2913 value_string
, symtab
));
2914 ovsdb_idl_txn_write(row
, column
, &datum
);
2922 cmd_set(struct vsctl_context
*ctx
)
2924 const char *table_name
= ctx
->argv
[1];
2925 const char *record_id
= ctx
->argv
[2];
2926 const struct vsctl_table_class
*table
;
2927 const struct ovsdb_idl_row
*row
;
2930 table
= get_table(table_name
);
2931 row
= must_get_row(ctx
, table
, record_id
);
2932 for (i
= 3; i
< ctx
->argc
; i
++) {
2933 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2938 pre_cmd_add(struct vsctl_context
*ctx
)
2940 const char *table_name
= ctx
->argv
[1];
2941 const char *column_name
= ctx
->argv
[3];
2942 const struct vsctl_table_class
*table
;
2943 const struct ovsdb_idl_column
*column
;
2945 table
= pre_get_table(ctx
, table_name
);
2946 pre_get_column(ctx
, table
, column_name
, &column
);
2950 cmd_add(struct vsctl_context
*ctx
)
2952 const char *table_name
= ctx
->argv
[1];
2953 const char *record_id
= ctx
->argv
[2];
2954 const char *column_name
= ctx
->argv
[3];
2955 const struct vsctl_table_class
*table
;
2956 const struct ovsdb_idl_column
*column
;
2957 const struct ovsdb_idl_row
*row
;
2958 const struct ovsdb_type
*type
;
2959 struct ovsdb_datum old
;
2962 table
= get_table(table_name
);
2963 row
= must_get_row(ctx
, table
, record_id
);
2964 die_if_error(get_column(table
, column_name
, &column
));
2966 type
= &column
->type
;
2967 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2968 for (i
= 4; i
< ctx
->argc
; i
++) {
2969 struct ovsdb_type add_type
;
2970 struct ovsdb_datum add
;
2974 add_type
.n_max
= UINT_MAX
;
2975 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2977 ovsdb_datum_union(&old
, &add
, type
, false);
2978 ovsdb_datum_destroy(&add
, type
);
2980 if (old
.n
> type
->n_max
) {
2981 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2982 "table %s but the maximum number is %u",
2984 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2985 column
->name
, table
->class->name
, type
->n_max
);
2987 ovsdb_idl_txn_verify(row
, column
);
2988 ovsdb_idl_txn_write(row
, column
, &old
);
2992 pre_cmd_remove(struct vsctl_context
*ctx
)
2994 const char *table_name
= ctx
->argv
[1];
2995 const char *column_name
= ctx
->argv
[3];
2996 const struct vsctl_table_class
*table
;
2997 const struct ovsdb_idl_column
*column
;
2999 table
= pre_get_table(ctx
, table_name
);
3000 pre_get_column(ctx
, table
, column_name
, &column
);
3004 cmd_remove(struct vsctl_context
*ctx
)
3006 const char *table_name
= ctx
->argv
[1];
3007 const char *record_id
= ctx
->argv
[2];
3008 const char *column_name
= ctx
->argv
[3];
3009 const struct vsctl_table_class
*table
;
3010 const struct ovsdb_idl_column
*column
;
3011 const struct ovsdb_idl_row
*row
;
3012 const struct ovsdb_type
*type
;
3013 struct ovsdb_datum old
;
3016 table
= get_table(table_name
);
3017 row
= must_get_row(ctx
, table
, record_id
);
3018 die_if_error(get_column(table
, column_name
, &column
));
3020 type
= &column
->type
;
3021 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
3022 for (i
= 4; i
< ctx
->argc
; i
++) {
3023 struct ovsdb_type rm_type
;
3024 struct ovsdb_datum rm
;
3029 rm_type
.n_max
= UINT_MAX
;
3030 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
3031 ctx
->argv
[i
], ctx
->symtab
);
3032 if (error
&& ovsdb_type_is_map(&rm_type
)) {
3034 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
3035 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
3036 ctx
->argv
[i
], ctx
->symtab
));
3038 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
3039 ovsdb_datum_destroy(&rm
, &rm_type
);
3041 if (old
.n
< type
->n_min
) {
3042 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3043 "table %s but the minimum number is %u",
3045 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
3046 column
->name
, table
->class->name
, type
->n_min
);
3048 ovsdb_idl_txn_verify(row
, column
);
3049 ovsdb_idl_txn_write(row
, column
, &old
);
3053 pre_cmd_clear(struct vsctl_context
*ctx
)
3055 const char *table_name
= ctx
->argv
[1];
3056 const struct vsctl_table_class
*table
;
3059 table
= pre_get_table(ctx
, table_name
);
3060 for (i
= 3; i
< ctx
->argc
; i
++) {
3061 const struct ovsdb_idl_column
*column
;
3063 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
3068 cmd_clear(struct vsctl_context
*ctx
)
3070 const char *table_name
= ctx
->argv
[1];
3071 const char *record_id
= ctx
->argv
[2];
3072 const struct vsctl_table_class
*table
;
3073 const struct ovsdb_idl_row
*row
;
3076 table
= get_table(table_name
);
3077 row
= must_get_row(ctx
, table
, record_id
);
3078 for (i
= 3; i
< ctx
->argc
; i
++) {
3079 const struct ovsdb_idl_column
*column
;
3080 const struct ovsdb_type
*type
;
3081 struct ovsdb_datum datum
;
3083 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
3085 type
= &column
->type
;
3086 if (type
->n_min
> 0) {
3087 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3088 "of table %s, which is not allowed to be empty",
3089 column
->name
, table
->class->name
);
3092 ovsdb_datum_init_empty(&datum
);
3093 ovsdb_idl_txn_write(row
, column
, &datum
);
3098 pre_create(struct vsctl_context
*ctx
)
3100 const char *id
= shash_find_data(&ctx
->options
, "--id");
3101 const char *table_name
= ctx
->argv
[1];
3102 const struct vsctl_table_class
*table
;
3104 table
= get_table(table_name
);
3105 if (!id
&& !table
->class->is_root
) {
3106 VLOG_WARN("applying \"create\" command to table %s without --id "
3107 "option will have no effect", table
->class->name
);
3112 cmd_create(struct vsctl_context
*ctx
)
3114 const char *id
= shash_find_data(&ctx
->options
, "--id");
3115 const char *table_name
= ctx
->argv
[1];
3116 const struct vsctl_table_class
*table
= get_table(table_name
);
3117 const struct ovsdb_idl_row
*row
;
3118 const struct uuid
*uuid
;
3122 struct ovsdb_symbol
*symbol
= create_symbol(ctx
->symtab
, id
, NULL
);
3123 if (table
->class->is_root
) {
3124 /* This table is in the root set, meaning that rows created in it
3125 * won't disappear even if they are unreferenced, so disable
3126 * warnings about that by pretending that there is a reference. */
3127 symbol
->strong_ref
= true;
3129 uuid
= &symbol
->uuid
;
3134 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
3135 for (i
= 2; i
< ctx
->argc
; i
++) {
3136 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
3138 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
3141 /* This function may be used as the 'postprocess' function for commands that
3142 * insert new rows into the database. It expects that the command's 'run'
3143 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3144 * sole output. It replaces that output by the row's permanent UUID assigned
3145 * by the database server and appends a new-line.
3147 * Currently we use this only for "create", because the higher-level commands
3148 * are supposed to be independent of the actual structure of the vswitch
3151 post_create(struct vsctl_context
*ctx
)
3153 const struct uuid
*real
;
3156 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
3159 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
3161 ds_clear(&ctx
->output
);
3162 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
3164 ds_put_char(&ctx
->output
, '\n');
3168 pre_cmd_destroy(struct vsctl_context
*ctx
)
3170 const char *table_name
= ctx
->argv
[1];
3172 pre_get_table(ctx
, table_name
);
3176 cmd_destroy(struct vsctl_context
*ctx
)
3178 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
3179 const char *table_name
= ctx
->argv
[1];
3180 const struct vsctl_table_class
*table
;
3183 table
= get_table(table_name
);
3184 for (i
= 2; i
< ctx
->argc
; i
++) {
3185 const struct ovsdb_idl_row
*row
;
3187 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
3189 ovsdb_idl_txn_delete(row
);
3195 is_condition_satisfied(const struct vsctl_table_class
*table
,
3196 const struct ovsdb_idl_row
*row
, const char *arg
,
3197 struct ovsdb_symbol_table
*symtab
)
3199 static const char *operators
[] = {
3200 "=", "!=", "<", ">", "<=", ">="
3203 const struct ovsdb_idl_column
*column
;
3204 const struct ovsdb_datum
*have_datum
;
3205 char *key_string
, *value_string
;
3206 const char *operator;
3211 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
3212 &operator, operators
, ARRAY_SIZE(operators
),
3214 die_if_error(error
);
3215 if (!value_string
) {
3216 vsctl_fatal("%s: missing value", arg
);
3219 have_datum
= ovsdb_idl_read(row
, column
);
3221 union ovsdb_atom want_key
, want_value
;
3223 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
3224 vsctl_fatal("cannot specify key to check for non-map column %s",
3228 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
3229 key_string
, symtab
));
3230 die_if_error(ovsdb_atom_from_string(&want_value
, &column
->type
.value
,
3231 value_string
, symtab
));
3233 idx
= ovsdb_datum_find_key(have_datum
,
3234 &want_key
, column
->type
.key
.type
);
3235 if (idx
!= UINT_MAX
) {
3236 cmp
= ovsdb_atom_compare_3way(&have_datum
->values
[idx
],
3238 column
->type
.value
.type
);
3241 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
3242 ovsdb_atom_destroy(&want_value
, column
->type
.value
.type
);
3244 struct ovsdb_datum want_datum
;
3246 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
3247 value_string
, symtab
));
3249 cmp
= ovsdb_datum_compare_3way(have_datum
, &want_datum
,
3251 ovsdb_datum_destroy(&want_datum
, &column
->type
);
3257 return (idx
== UINT_MAX
? false
3258 : !strcmp(operator, "=") ? cmp
== 0
3259 : !strcmp(operator, "!=") ? cmp
!= 0
3260 : !strcmp(operator, "<") ? cmp
< 0
3261 : !strcmp(operator, ">") ? cmp
> 0
3262 : !strcmp(operator, "<=") ? cmp
<= 0
3263 : !strcmp(operator, ">=") ? cmp
>= 0
3268 pre_cmd_wait_until(struct vsctl_context
*ctx
)
3270 const char *table_name
= ctx
->argv
[1];
3271 const struct vsctl_table_class
*table
;
3274 table
= pre_get_table(ctx
, table_name
);
3276 for (i
= 3; i
< ctx
->argc
; i
++) {
3277 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
3282 cmd_wait_until(struct vsctl_context
*ctx
)
3284 const char *table_name
= ctx
->argv
[1];
3285 const char *record_id
= ctx
->argv
[2];
3286 const struct vsctl_table_class
*table
;
3287 const struct ovsdb_idl_row
*row
;
3290 table
= get_table(table_name
);
3292 row
= get_row(ctx
, table
, record_id
);
3294 ctx
->try_again
= true;
3298 for (i
= 3; i
< ctx
->argc
; i
++) {
3299 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
3300 ctx
->try_again
= true;
3306 static struct json
*
3307 where_uuid_equals(const struct uuid
*uuid
)
3310 json_array_create_1(
3311 json_array_create_3(
3312 json_string_create("_uuid"),
3313 json_string_create("=="),
3314 json_array_create_2(
3315 json_string_create("uuid"),
3316 json_string_create_nocopy(
3317 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
3321 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
3322 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
3323 const struct ovsrec_open_vswitch
*ovs
,
3324 struct ovsdb_symbol_table
*symtab
)
3326 ctx
->argc
= command
->argc
;
3327 ctx
->argv
= command
->argv
;
3328 ctx
->options
= command
->options
;
3330 ds_swap(&ctx
->output
, &command
->output
);
3331 ctx
->table
= command
->table
;
3335 ctx
->symtab
= symtab
;
3336 ctx
->verified_ports
= false;
3338 ctx
->try_again
= false;
3342 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
3344 ds_swap(&ctx
->output
, &command
->output
);
3345 command
->table
= ctx
->table
;
3349 run_prerequisites(struct vsctl_command
*commands
, size_t n_commands
,
3350 struct ovsdb_idl
*idl
)
3352 struct vsctl_command
*c
;
3354 ovsdb_idl_add_table(idl
, &ovsrec_table_open_vswitch
);
3355 if (wait_for_reload
) {
3356 ovsdb_idl_add_column(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
3358 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3359 if (c
->syntax
->prerequisites
) {
3360 struct vsctl_context ctx
;
3362 ds_init(&c
->output
);
3365 vsctl_context_init(&ctx
, c
, idl
, NULL
, NULL
, NULL
);
3366 (c
->syntax
->prerequisites
)(&ctx
);
3367 vsctl_context_done(&ctx
, c
);
3369 assert(!c
->output
.string
);
3376 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
3377 struct ovsdb_idl
*idl
)
3379 struct ovsdb_idl_txn
*txn
;
3380 const struct ovsrec_open_vswitch
*ovs
;
3381 enum ovsdb_idl_txn_status status
;
3382 struct ovsdb_symbol_table
*symtab
;
3383 struct vsctl_command
*c
;
3384 struct shash_node
*node
;
3385 int64_t next_cfg
= 0;
3388 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
3390 ovsdb_idl_txn_set_dry_run(txn
);
3393 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
3395 ovs
= ovsrec_open_vswitch_first(idl
);
3397 /* XXX add verification that table is empty */
3398 ovs
= ovsrec_open_vswitch_insert(txn
);
3401 if (wait_for_reload
) {
3402 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
3403 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
3404 json_destroy(where
);
3407 symtab
= ovsdb_symbol_table_create();
3408 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3409 ds_init(&c
->output
);
3412 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3413 struct vsctl_context ctx
;
3415 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
3416 (c
->syntax
->run
)(&ctx
);
3417 vsctl_context_done(&ctx
, c
);
3419 if (ctx
.try_again
) {
3424 SHASH_FOR_EACH (node
, &symtab
->sh
) {
3425 struct ovsdb_symbol
*symbol
= node
->data
;
3426 if (!symbol
->created
) {
3427 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3428 "with \"-- --id=%s create ...\")",
3429 node
->name
, node
->name
);
3431 if (!symbol
->strong_ref
) {
3432 if (!symbol
->weak_ref
) {
3433 VLOG_WARN("row id \"%s\" was created but no reference to it "
3434 "was inserted, so it will not actually appear in "
3435 "the database", node
->name
);
3437 VLOG_WARN("row id \"%s\" was created but only a weak "
3438 "reference to it was inserted, so it will not "
3439 "actually appear in the database", node
->name
);
3444 status
= ovsdb_idl_txn_commit_block(txn
);
3445 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
3446 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
3448 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
3449 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3450 if (c
->syntax
->postprocess
) {
3451 struct vsctl_context ctx
;
3453 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
3454 (c
->syntax
->postprocess
)(&ctx
);
3455 vsctl_context_done(&ctx
, c
);
3459 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
3460 ovsdb_idl_txn_destroy(txn
);
3461 txn
= the_idl_txn
= NULL
;
3464 case TXN_INCOMPLETE
:
3468 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3469 vsctl_fatal("transaction aborted");
3479 vsctl_fatal("transaction error: %s", error
);
3486 ovsdb_symbol_table_destroy(symtab
);
3488 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3489 struct ds
*ds
= &c
->output
;
3492 table_print(c
->table
, &table_style
);
3493 } else if (oneline
) {
3497 for (j
= 0; j
< ds
->length
; j
++) {
3498 int ch
= ds
->string
[j
];
3501 fputs("\\n", stdout
);
3505 fputs("\\\\", stdout
);
3514 fputs(ds_cstr(ds
), stdout
);
3516 ds_destroy(&c
->output
);
3517 table_destroy(c
->table
);
3520 smap_destroy(&c
->options
);
3524 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
3527 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
3528 if (ovs
->cur_cfg
>= next_cfg
) {
3532 ovsdb_idl_wait(idl
);
3537 ovsdb_idl_destroy(idl
);
3542 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3543 * resources and return so that the caller can try again. */
3545 ovsdb_idl_txn_abort(txn
);
3546 ovsdb_idl_txn_destroy(txn
);
3548 ovsdb_symbol_table_destroy(symtab
);
3549 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3550 ds_destroy(&c
->output
);
3551 table_destroy(c
->table
);
3557 static const struct vsctl_command_syntax all_commands
[] = {
3558 /* Open vSwitch commands. */
3559 {"init", 0, 0, NULL
, cmd_init
, NULL
, "", RW
},
3561 /* Bridge commands. */
3562 {"add-br", 1, 3, pre_get_info
, cmd_add_br
, NULL
, "--may-exist", RW
},
3563 {"del-br", 1, 1, pre_get_info
, cmd_del_br
, NULL
, "--if-exists", RW
},
3564 {"list-br", 0, 0, pre_get_info
, cmd_list_br
, NULL
, "", RO
},
3565 {"br-exists", 1, 1, pre_get_info
, cmd_br_exists
, NULL
, "", RO
},
3566 {"br-to-vlan", 1, 1, pre_get_info
, cmd_br_to_vlan
, NULL
, "", RO
},
3567 {"br-to-parent", 1, 1, pre_get_info
, cmd_br_to_parent
, NULL
, "", RO
},
3568 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id
,
3569 cmd_br_set_external_id
, NULL
, "", RW
},
3570 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id
,
3571 cmd_br_get_external_id
, NULL
, "", RO
},
3573 /* Port commands. */
3574 {"list-ports", 1, 1, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
3575 {"add-port", 2, INT_MAX
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
3577 {"add-bond", 4, INT_MAX
, pre_get_info
, cmd_add_bond
, NULL
,
3578 "--may-exist,--fake-iface", RW
},
3579 {"del-port", 1, 2, pre_get_info
, cmd_del_port
, NULL
,
3580 "--if-exists,--with-iface", RW
},
3581 {"port-to-br", 1, 1, pre_get_info
, cmd_port_to_br
, NULL
, "", RO
},
3583 /* Interface commands. */
3584 {"list-ifaces", 1, 1, pre_get_info
, cmd_list_ifaces
, NULL
, "", RO
},
3585 {"iface-to-br", 1, 1, pre_get_info
, cmd_iface_to_br
, NULL
, "", RO
},
3587 /* Controller commands. */
3588 {"get-controller", 1, 1, pre_controller
, cmd_get_controller
, NULL
, "", RO
},
3589 {"del-controller", 1, 1, pre_controller
, cmd_del_controller
, NULL
, "", RW
},
3590 {"set-controller", 1, INT_MAX
, pre_controller
, cmd_set_controller
, NULL
,
3592 {"get-fail-mode", 1, 1, pre_get_info
, cmd_get_fail_mode
, NULL
, "", RO
},
3593 {"del-fail-mode", 1, 1, pre_get_info
, cmd_del_fail_mode
, NULL
, "", RW
},
3594 {"set-fail-mode", 2, 2, pre_get_info
, cmd_set_fail_mode
, NULL
, "", RW
},
3596 /* Manager commands. */
3597 {"get-manager", 0, 0, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
3598 {"del-manager", 0, INT_MAX
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
3599 {"set-manager", 1, INT_MAX
, pre_manager
, cmd_set_manager
, NULL
, "", RW
},
3602 {"get-ssl", 0, 0, pre_cmd_get_ssl
, cmd_get_ssl
, NULL
, "", RO
},
3603 {"del-ssl", 0, 0, pre_cmd_del_ssl
, cmd_del_ssl
, NULL
, "", RW
},
3604 {"set-ssl", 3, 3, pre_cmd_set_ssl
, cmd_set_ssl
, NULL
, "--bootstrap", RW
},
3606 /* Switch commands. */
3607 {"emer-reset", 0, 0, pre_cmd_emer_reset
, cmd_emer_reset
, NULL
, "", RW
},
3609 /* Parameter commands. */
3610 {"get", 2, INT_MAX
, pre_cmd_get
, cmd_get
, NULL
, "--if-exists,--id=", RO
},
3611 {"list", 1, INT_MAX
, pre_cmd_list
, cmd_list
, NULL
, "--columns=", RO
},
3612 {"find", 1, INT_MAX
, pre_cmd_find
, cmd_find
, NULL
, "--columns=", RO
},
3613 {"set", 3, INT_MAX
, pre_cmd_set
, cmd_set
, NULL
, "", RW
},
3614 {"add", 4, INT_MAX
, pre_cmd_add
, cmd_add
, NULL
, "", RW
},
3615 {"remove", 4, INT_MAX
, pre_cmd_remove
, cmd_remove
, NULL
, "", RW
},
3616 {"clear", 3, INT_MAX
, pre_cmd_clear
, cmd_clear
, NULL
, "", RW
},
3617 {"create", 2, INT_MAX
, pre_create
, cmd_create
, post_create
, "--id=", RW
},
3618 {"destroy", 1, INT_MAX
, pre_cmd_destroy
, cmd_destroy
, NULL
, "--if-exists",
3620 {"wait-until", 2, INT_MAX
, pre_cmd_wait_until
, cmd_wait_until
, NULL
, "",
3623 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, RO
},