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.
31 #include "command-line.h"
34 #include "dynamic-string.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-idl.h"
38 #include "poll-loop.h"
41 #include "stream-ssl.h"
44 #include "vswitchd/vswitch-idl.h"
50 VLOG_DEFINE_THIS_MODULE(vsctl
);
52 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
53 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
57 /* A command supported by ovs-vsctl. */
58 struct vsctl_command_syntax
{
59 const char *name
; /* e.g. "add-br" */
60 int min_args
; /* Min number of arguments following name. */
61 int max_args
; /* Max number of arguments following name. */
63 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
64 * each column or table in ctx->idl that it uses. */
65 void (*prerequisites
)(struct vsctl_context
*ctx
);
67 /* Does the actual work of the command and puts the command's output, if
68 * any, in ctx->output or ctx->table.
70 * Alternatively, if some prerequisite of the command is not met and the
71 * caller should wait for something to change and then retry, it may set
72 * ctx->try_again to true. (Only the "wait-until" command currently does
74 void (*run
)(struct vsctl_context
*ctx
);
76 /* If nonnull, called after the transaction has been successfully
77 * committed. ctx->output is the output from the "run" function, which
78 * this function may modify and otherwise postprocess as needed. (Only the
79 * "create" command currently does any postprocessing.) */
80 void (*postprocess
)(struct vsctl_context
*ctx
);
82 /* A comma-separated list of supported options, e.g. "--a,--b", or the
83 * empty string if the command does not support any options. */
85 enum { RO
, RW
} mode
; /* Does this command modify the database? */
88 struct vsctl_command
{
89 /* Data that remains constant after initialization. */
90 const struct vsctl_command_syntax
*syntax
;
95 /* Data modified by commands. */
100 /* --db: The database server to contact. */
101 static const char *db
;
103 /* --oneline: Write each command's output as a single line? */
106 /* --dry-run: Do not commit any changes. */
109 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
110 static bool wait_for_reload
= true;
112 /* --timeout: Time to wait for a connection to 'db'. */
115 /* Format for table output. */
116 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
118 /* All supported commands. */
119 static const struct vsctl_command_syntax all_commands
[];
121 /* The IDL we're using and the current transaction, if any.
122 * This is for use by vsctl_exit() only, to allow it to clean up.
123 * Other code should use its context arguments. */
124 static struct ovsdb_idl
*the_idl
;
125 static struct ovsdb_idl_txn
*the_idl_txn
;
127 static void vsctl_exit(int status
) NO_RETURN
;
128 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN
;
129 static char *default_db(void);
130 static void usage(void) NO_RETURN
;
131 static void parse_options(int argc
, char *argv
[]);
132 static bool might_write_to_db(char **argv
);
134 static struct vsctl_command
*parse_commands(int argc
, char *argv
[],
135 size_t *n_commandsp
);
136 static void parse_command(int argc
, char *argv
[], struct vsctl_command
*);
137 static const struct vsctl_command_syntax
*find_command(const char *name
);
138 static void run_prerequisites(struct vsctl_command
[], size_t n_commands
,
140 static void do_vsctl(const char *args
,
141 struct vsctl_command
*, size_t n_commands
,
144 static const struct vsctl_table_class
*get_table(const char *table_name
);
145 static void set_column(const struct vsctl_table_class
*,
146 const struct ovsdb_idl_row
*, const char *arg
,
147 struct ovsdb_symbol_table
*);
149 static bool is_condition_satisfied(const struct vsctl_table_class
*,
150 const struct ovsdb_idl_row
*,
152 struct ovsdb_symbol_table
*);
155 main(int argc
, char *argv
[])
157 extern struct vlog_module VLM_reconnect
;
158 struct ovsdb_idl
*idl
;
159 struct vsctl_command
*commands
;
163 set_program_name(argv
[0]);
164 signal(SIGPIPE
, SIG_IGN
);
165 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
166 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
169 /* Log our arguments. This is often valuable for debugging systems. */
170 args
= process_escape_args(argv
);
171 VLOG(might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
173 /* Parse command line. */
174 parse_options(argc
, argv
);
175 commands
= parse_commands(argc
- optind
, argv
+ optind
, &n_commands
);
181 /* Initialize IDL. */
182 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
, false);
183 run_prerequisites(commands
, n_commands
, idl
);
185 /* Now execute the commands. */
187 if (ovsdb_idl_run(idl
)) {
188 do_vsctl(args
, commands
, n_commands
, idl
);
197 parse_options(int argc
, char *argv
[])
200 OPT_DB
= UCHAR_MAX
+ 1,
209 static struct option long_options
[] = {
210 {"db", required_argument
, NULL
, OPT_DB
},
211 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
212 {"no-wait", no_argument
, NULL
, OPT_NO_WAIT
},
213 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
214 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
215 {"timeout", required_argument
, NULL
, 't'},
216 {"help", no_argument
, NULL
, 'h'},
217 {"version", no_argument
, NULL
, 'V'},
220 STREAM_SSL_LONG_OPTIONS
,
221 {"peer-ca-cert", required_argument
, NULL
, 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
)
279 STREAM_SSL_OPTION_HANDLERS
281 case OPT_PEER_CA_CERT
:
282 stream_ssl_set_peer_ca_cert_file(optarg
);
299 static struct vsctl_command
*
300 parse_commands(int argc
, char *argv
[], size_t *n_commandsp
)
302 struct vsctl_command
*commands
;
303 size_t n_commands
, allocated_commands
;
307 n_commands
= allocated_commands
= 0;
309 for (start
= i
= 0; i
<= argc
; i
++) {
310 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
312 if (n_commands
>= allocated_commands
) {
313 struct vsctl_command
*c
;
315 commands
= x2nrealloc(commands
, &allocated_commands
,
317 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
318 shash_moved(&c
->options
);
321 parse_command(i
- start
, &argv
[start
],
322 &commands
[n_commands
++]);
328 vsctl_fatal("missing command name (use --help for help)");
330 *n_commandsp
= n_commands
;
335 parse_command(int argc
, char *argv
[], struct vsctl_command
*command
)
337 const struct vsctl_command_syntax
*p
;
338 struct shash_node
*node
;
342 shash_init(&command
->options
);
343 for (i
= 0; i
< argc
; i
++) {
344 const char *option
= argv
[i
];
348 if (option
[0] != '-') {
352 equals
= strchr(option
, '=');
354 key
= xmemdup0(option
, equals
- option
);
355 value
= xstrdup(equals
+ 1);
357 key
= xstrdup(option
);
361 if (shash_find(&command
->options
, key
)) {
362 vsctl_fatal("'%s' option specified multiple times", argv
[i
]);
364 shash_add_nocopy(&command
->options
, key
, value
);
367 vsctl_fatal("missing command name");
370 p
= find_command(argv
[i
]);
372 vsctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
375 SHASH_FOR_EACH (node
, &command
->options
) {
376 const char *s
= strstr(p
->options
, node
->name
);
377 int end
= s
? s
[strlen(node
->name
)] : EOF
;
379 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
380 vsctl_fatal("'%s' command has no '%s' option",
381 argv
[i
], node
->name
);
383 if ((end
== '=') != (node
->data
!= NULL
)) {
385 vsctl_fatal("missing argument to '%s' option on '%s' "
386 "command", node
->name
, argv
[i
]);
388 vsctl_fatal("'%s' option on '%s' does not accept an "
389 "argument", node
->name
, argv
[i
]);
394 n_arg
= argc
- i
- 1;
395 if (n_arg
< p
->min_args
) {
396 vsctl_fatal("'%s' command requires at least %d arguments",
397 p
->name
, p
->min_args
);
398 } else if (n_arg
> p
->max_args
) {
401 for (j
= i
+ 1; j
< argc
; j
++) {
402 if (argv
[j
][0] == '-') {
403 vsctl_fatal("'%s' command takes at most %d arguments "
404 "(note that options must precede command "
405 "names and follow a \"--\" argument)",
406 p
->name
, p
->max_args
);
410 vsctl_fatal("'%s' command takes at most %d arguments",
411 p
->name
, p
->max_args
);
415 command
->argc
= n_arg
+ 1;
416 command
->argv
= &argv
[i
];
419 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
420 * null pointer if there is none. */
421 static const struct vsctl_command_syntax
*
422 find_command(const char *name
)
424 static struct shash commands
= SHASH_INITIALIZER(&commands
);
426 if (shash_is_empty(&commands
)) {
427 const struct vsctl_command_syntax
*p
;
429 for (p
= all_commands
; p
->name
; p
++) {
430 shash_add_assert(&commands
, p
->name
, p
);
434 return shash_find_data(&commands
, name
);
438 vsctl_fatal(const char *format
, ...)
443 va_start(args
, format
);
444 message
= xvasprintf(format
, args
);
447 vlog_set_levels(&VLM_vsctl
, VLF_CONSOLE
, VLL_EMER
);
448 VLOG_ERR("%s", message
);
449 ovs_error(0, "%s", message
);
450 vsctl_exit(EXIT_FAILURE
);
453 /* Frees the current transaction and the underlying IDL and then calls
456 * Freeing the transaction and the IDL is not strictly necessary, but it makes
457 * for a clean memory leak report from valgrind in the normal case. That makes
458 * it easier to notice real memory leaks. */
460 vsctl_exit(int status
)
463 ovsdb_idl_txn_abort(the_idl_txn
);
464 ovsdb_idl_txn_destroy(the_idl_txn
);
466 ovsdb_idl_destroy(the_idl
);
474 %s: ovs-vswitchd management utility\n\
475 usage: %s [OPTIONS] COMMAND [ARG...]\n\
477 Open vSwitch commands:\n\
478 init initialize database, if not yet initialized\n\
479 emer-reset reset configuration to clean state\n\
482 add-br BRIDGE create a new bridge named BRIDGE\n\
483 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
484 del-br BRIDGE delete BRIDGE and all of its ports\n\
485 list-br print the names of all the bridges\n\
486 br-exists BRIDGE test whether BRIDGE exists\n\
487 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
488 br-to-parent BRIDGE print the parent of BRIDGE\n\
489 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
490 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
491 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
492 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
494 Port commands (a bond is considered to be a single port):\n\
495 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
496 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
497 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
498 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
499 port-to-br PORT print name of bridge that contains PORT\n\
501 Interface commands (a bond consists of multiple interfaces):\n\
502 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
503 iface-to-br IFACE print name of bridge that contains IFACE\n\
505 Controller commands:\n\
506 get-controller BRIDGE print the controller for BRIDGE\n\
507 del-controller BRIDGE delete the controller for BRIDGE\n\
508 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
509 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
510 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
511 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
514 get-manager print all manager(s)\n\
515 del-manager delete all manager(s)\n\
516 set-manager TARGET... set the list of manager(s) to TARGET(s)\n\
519 get-ssl print the SSL configuration\n\
520 del-ssl delete the SSL configuration\n\
521 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
524 emer-reset reset switch to known good state\n\
526 Database commands:\n\
527 list TBL [REC] list RECord (or all records) in TBL\n\
528 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
529 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
530 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
531 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
532 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
533 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
534 create TBL COL[:KEY]=VALUE create and initialize new record\n\
535 destroy TBL REC delete RECord from TBL\n\
536 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
537 Potentially unsafe database commands require --force option.\n\
540 --db=DATABASE connect to DATABASE\n\
542 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
543 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
544 --dry-run do not commit changes to database\n\
545 --oneline print exactly one line of output per command\n",
546 program_name
, program_name
, default_db());
549 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
550 stream_usage("database", true, true, false);
553 -h, --help display this help message\n\
554 -V, --version display version information\n");
563 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
568 /* Returns true if it looks like this set of arguments might modify the
569 * database, otherwise false. (Not very smart, so it's prone to false
572 might_write_to_db(char **argv
)
574 for (; *argv
; argv
++) {
575 const struct vsctl_command_syntax
*p
= find_command(*argv
);
576 if (p
&& p
->mode
== RW
) {
583 struct vsctl_context
{
587 struct shash options
;
589 /* Modifiable state. */
592 struct ovsdb_idl
*idl
;
593 struct ovsdb_idl_txn
*txn
;
594 struct ovsdb_symbol_table
*symtab
;
595 const struct ovsrec_open_vswitch
*ovs
;
598 /* A command may set this member to true if some prerequisite is not met
599 * and the caller should wait for something to change and then retry. */
603 struct vsctl_bridge
{
604 struct ovsrec_bridge
*br_cfg
;
606 struct ovsrec_controller
**ctrl
;
609 struct vsctl_bridge
*parent
;
614 struct ovsrec_port
*port_cfg
;
615 struct vsctl_bridge
*bridge
;
619 struct ovsrec_interface
*iface_cfg
;
620 struct vsctl_port
*port
;
624 struct vsctl_context
*ctx
;
625 struct shash bridges
; /* Maps from bridge name to struct vsctl_bridge. */
626 struct shash ports
; /* Maps from port name to struct vsctl_port. */
627 struct shash ifaces
; /* Maps from port name to struct vsctl_iface. */
631 vsctl_context_to_string(const struct vsctl_context
*ctx
)
633 const struct shash_node
*node
;
639 SHASH_FOR_EACH (node
, &ctx
->options
) {
640 svec_add(&words
, node
->name
);
642 for (i
= 0; i
< ctx
->argc
; i
++) {
643 svec_add(&words
, ctx
->argv
[i
]);
645 svec_terminate(&words
);
647 s
= process_escape_args(words
.names
);
649 svec_destroy(&words
);
655 verify_ports(struct vsctl_context
*ctx
)
657 if (!ctx
->verified_ports
) {
658 const struct ovsrec_bridge
*bridge
;
659 const struct ovsrec_port
*port
;
661 ovsrec_open_vswitch_verify_bridges(ctx
->ovs
);
662 OVSREC_BRIDGE_FOR_EACH (bridge
, ctx
->idl
) {
663 ovsrec_bridge_verify_ports(bridge
);
665 OVSREC_PORT_FOR_EACH (port
, ctx
->idl
) {
666 ovsrec_port_verify_interfaces(port
);
669 ctx
->verified_ports
= true;
673 static struct vsctl_bridge
*
674 add_bridge(struct vsctl_info
*b
,
675 struct ovsrec_bridge
*br_cfg
, const char *name
,
676 struct vsctl_bridge
*parent
, int vlan
)
678 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
680 br
->name
= xstrdup(name
);
684 br
->ctrl
= parent
->br_cfg
->controller
;
685 br
->n_ctrl
= parent
->br_cfg
->n_controller
;
686 br
->fail_mode
= parent
->br_cfg
->fail_mode
;
688 br
->ctrl
= br_cfg
->controller
;
689 br
->n_ctrl
= br_cfg
->n_controller
;
690 br
->fail_mode
= br_cfg
->fail_mode
;
692 shash_add(&b
->bridges
, br
->name
, br
);
697 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
699 return (port_cfg
->fake_bridge
701 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095);
704 static struct vsctl_bridge
*
705 find_vlan_bridge(struct vsctl_info
*info
,
706 struct vsctl_bridge
*parent
, int vlan
)
708 struct shash_node
*node
;
710 SHASH_FOR_EACH (node
, &info
->bridges
) {
711 struct vsctl_bridge
*br
= node
->data
;
712 if (br
->parent
== parent
&& br
->vlan
== vlan
) {
721 free_info(struct vsctl_info
*info
)
723 struct shash_node
*node
;
725 SHASH_FOR_EACH (node
, &info
->bridges
) {
726 struct vsctl_bridge
*bridge
= node
->data
;
730 shash_destroy(&info
->bridges
);
732 shash_destroy_free_data(&info
->ports
);
733 shash_destroy_free_data(&info
->ifaces
);
737 pre_get_info(struct vsctl_context
*ctx
)
739 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_bridges
);
741 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_name
);
742 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
743 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
744 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ports
);
746 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_name
);
747 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_fake_bridge
);
748 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_tag
);
749 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_interfaces
);
751 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_name
);
755 get_info(struct vsctl_context
*ctx
, struct vsctl_info
*info
)
757 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
758 struct sset bridges
, ports
;
762 shash_init(&info
->bridges
);
763 shash_init(&info
->ports
);
764 shash_init(&info
->ifaces
);
768 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
769 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
770 struct vsctl_bridge
*br
;
773 if (!sset_add(&bridges
, br_cfg
->name
)) {
774 VLOG_WARN("%s: database contains duplicate bridge name",
778 br
= add_bridge(info
, br_cfg
, br_cfg
->name
, NULL
, 0);
783 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
784 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
786 if (!sset_add(&ports
, port_cfg
->name
)) {
787 VLOG_WARN("%s: database contains duplicate port name",
792 if (port_is_fake_bridge(port_cfg
)
793 && sset_add(&bridges
, port_cfg
->name
)) {
794 add_bridge(info
, NULL
, port_cfg
->name
, br
, *port_cfg
->tag
);
798 sset_destroy(&bridges
);
799 sset_destroy(&ports
);
803 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
804 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
805 struct vsctl_bridge
*br
;
808 if (!sset_add(&bridges
, br_cfg
->name
)) {
811 br
= shash_find_data(&info
->bridges
, br_cfg
->name
);
812 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
813 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
814 struct vsctl_port
*port
;
817 if (!sset_add(&ports
, port_cfg
->name
)) {
821 if (port_is_fake_bridge(port_cfg
)
822 && !sset_add(&bridges
, port_cfg
->name
)) {
826 port
= xmalloc(sizeof *port
);
827 port
->port_cfg
= port_cfg
;
829 && *port_cfg
->tag
>= 1 && *port_cfg
->tag
<= 4095) {
830 port
->bridge
= find_vlan_bridge(info
, br
, *port_cfg
->tag
);
837 shash_add(&info
->ports
, port_cfg
->name
, port
);
839 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
840 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
841 struct vsctl_iface
*iface
;
843 if (shash_find(&info
->ifaces
, iface_cfg
->name
)) {
844 VLOG_WARN("%s: database contains duplicate interface name",
849 iface
= xmalloc(sizeof *iface
);
850 iface
->iface_cfg
= iface_cfg
;
852 shash_add(&info
->ifaces
, iface_cfg
->name
, iface
);
856 sset_destroy(&bridges
);
857 sset_destroy(&ports
);
861 check_conflicts(struct vsctl_info
*info
, const char *name
,
864 struct vsctl_iface
*iface
;
865 struct vsctl_port
*port
;
867 verify_ports(info
->ctx
);
869 if (shash_find(&info
->bridges
, name
)) {
870 vsctl_fatal("%s because a bridge named %s already exists",
874 port
= shash_find_data(&info
->ports
, name
);
876 vsctl_fatal("%s because a port named %s already exists on "
877 "bridge %s", msg
, name
, port
->bridge
->name
);
880 iface
= shash_find_data(&info
->ifaces
, name
);
882 vsctl_fatal("%s because an interface named %s already exists "
883 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
889 static struct vsctl_bridge
*
890 find_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
892 struct vsctl_bridge
*br
= shash_find_data(&info
->bridges
, name
);
893 if (must_exist
&& !br
) {
894 vsctl_fatal("no bridge named %s", name
);
896 ovsrec_open_vswitch_verify_bridges(info
->ctx
->ovs
);
900 static struct vsctl_bridge
*
901 find_real_bridge(struct vsctl_info
*info
, const char *name
, bool must_exist
)
903 struct vsctl_bridge
*br
= find_bridge(info
, name
, must_exist
);
904 if (br
&& br
->parent
) {
905 vsctl_fatal("%s is a fake bridge", name
);
910 static struct vsctl_port
*
911 find_port(struct vsctl_info
*info
, const char *name
, bool must_exist
)
913 struct vsctl_port
*port
= shash_find_data(&info
->ports
, name
);
914 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
917 if (must_exist
&& !port
) {
918 vsctl_fatal("no port named %s", name
);
920 verify_ports(info
->ctx
);
924 static struct vsctl_iface
*
925 find_iface(struct vsctl_info
*info
, const char *name
, bool must_exist
)
927 struct vsctl_iface
*iface
= shash_find_data(&info
->ifaces
, name
);
928 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
931 if (must_exist
&& !iface
) {
932 vsctl_fatal("no interface named %s", name
);
934 verify_ports(info
->ctx
);
939 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
941 struct ovsrec_port
**ports
;
944 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
945 for (i
= 0; i
< br
->n_ports
; i
++) {
946 ports
[i
] = br
->ports
[i
];
948 ports
[br
->n_ports
] = port
;
949 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
954 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
956 struct ovsrec_port
**ports
;
959 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
960 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
961 if (br
->ports
[i
] != port
) {
962 ports
[n
++] = br
->ports
[i
];
965 ovsrec_bridge_set_ports(br
, ports
, n
);
970 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
971 struct ovsrec_bridge
*bridge
)
973 struct ovsrec_bridge
**bridges
;
976 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
977 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
978 bridges
[i
] = ovs
->bridges
[i
];
980 bridges
[ovs
->n_bridges
] = bridge
;
981 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
986 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
987 struct ovsrec_bridge
*bridge
)
989 struct ovsrec_bridge
**bridges
;
992 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
993 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
994 if (ovs
->bridges
[i
] != bridge
) {
995 bridges
[n
++] = ovs
->bridges
[i
];
998 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
1003 cmd_init(struct vsctl_context
*ctx OVS_UNUSED
)
1008 pre_cmd_emer_reset(struct vsctl_context
*ctx
)
1010 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1011 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1013 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
1014 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
1015 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_mirrors
);
1016 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_netflow
);
1017 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_sflow
);
1018 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_flood_vlans
);
1019 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_other_config
);
1021 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_other_config
);
1023 ovsdb_idl_add_column(ctx
->idl
,
1024 &ovsrec_interface_col_ingress_policing_rate
);
1025 ovsdb_idl_add_column(ctx
->idl
,
1026 &ovsrec_interface_col_ingress_policing_burst
);
1030 cmd_emer_reset(struct vsctl_context
*ctx
)
1032 const struct ovsdb_idl
*idl
= ctx
->idl
;
1033 const struct ovsrec_bridge
*br
;
1034 const struct ovsrec_port
*port
;
1035 const struct ovsrec_interface
*iface
;
1036 const struct ovsrec_mirror
*mirror
, *next_mirror
;
1037 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
1038 const struct ovsrec_manager
*mgr
, *next_mgr
;
1039 const struct ovsrec_netflow
*nf
, *next_nf
;
1040 const struct ovsrec_ssl
*ssl
, *next_ssl
;
1041 const struct ovsrec_sflow
*sflow
, *next_sflow
;
1043 /* Reset the Open_vSwitch table. */
1044 ovsrec_open_vswitch_set_manager_options(ctx
->ovs
, NULL
, 0);
1045 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
1047 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
1049 char *hw_key
= "hwaddr";
1050 char *hw_val
= NULL
;
1052 ovsrec_bridge_set_controller(br
, NULL
, 0);
1053 ovsrec_bridge_set_fail_mode(br
, NULL
);
1054 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
1055 ovsrec_bridge_set_netflow(br
, NULL
);
1056 ovsrec_bridge_set_sflow(br
, NULL
);
1057 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
1059 /* We only want to save the "hwaddr" key from other_config. */
1060 for (i
=0; i
< br
->n_other_config
; i
++) {
1061 if (!strcmp(br
->key_other_config
[i
], hw_key
)) {
1062 hw_val
= br
->value_other_config
[i
];
1067 char *val
= xstrdup(hw_val
);
1068 ovsrec_bridge_set_other_config(br
, &hw_key
, &val
, 1);
1071 ovsrec_bridge_set_other_config(br
, NULL
, NULL
, 0);
1075 OVSREC_PORT_FOR_EACH (port
, idl
) {
1076 ovsrec_port_set_other_config(port
, NULL
, NULL
, 0);
1079 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
1080 /* xxx What do we do about gre/patch devices created by mgr? */
1082 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
1083 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
1086 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
1087 ovsrec_mirror_delete(mirror
);
1090 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
1091 ovsrec_controller_delete(ctrl
);
1094 OVSREC_MANAGER_FOR_EACH_SAFE (mgr
, next_mgr
, idl
) {
1095 ovsrec_manager_delete(mgr
);
1098 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
1099 ovsrec_netflow_delete(nf
);
1102 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
1103 ovsrec_ssl_delete(ssl
);
1106 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
1107 ovsrec_sflow_delete(sflow
);
1112 cmd_add_br(struct vsctl_context
*ctx
)
1114 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1115 const char *br_name
, *parent_name
;
1116 struct vsctl_info info
;
1119 br_name
= ctx
->argv
[1];
1120 if (ctx
->argc
== 2) {
1123 } else if (ctx
->argc
== 4) {
1124 parent_name
= ctx
->argv
[2];
1125 vlan
= atoi(ctx
->argv
[3]);
1126 if (vlan
< 1 || vlan
> 4095) {
1127 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx
->argv
[0]);
1130 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1134 get_info(ctx
, &info
);
1136 struct vsctl_bridge
*br
;
1138 br
= find_bridge(&info
, br_name
, false);
1142 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1143 "a VLAN bridge for VLAN %d",
1144 br_name
, br_name
, br
->vlan
);
1148 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1149 "is not a VLAN bridge",
1150 br_name
, parent_name
, vlan
, br_name
);
1151 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1152 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1153 "has the wrong parent %s",
1154 br_name
, parent_name
, vlan
,
1155 br_name
, br
->parent
->name
);
1156 } else if (br
->vlan
!= vlan
) {
1157 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1158 "is a VLAN bridge for the wrong VLAN %d",
1159 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1165 check_conflicts(&info
, br_name
,
1166 xasprintf("cannot create a bridge named %s", br_name
));
1169 struct ovsrec_port
*port
;
1170 struct ovsrec_interface
*iface
;
1171 struct ovsrec_bridge
*br
;
1173 iface
= ovsrec_interface_insert(ctx
->txn
);
1174 ovsrec_interface_set_name(iface
, br_name
);
1175 ovsrec_interface_set_type(iface
, "internal");
1177 port
= ovsrec_port_insert(ctx
->txn
);
1178 ovsrec_port_set_name(port
, br_name
);
1179 ovsrec_port_set_interfaces(port
, &iface
, 1);
1181 br
= ovsrec_bridge_insert(ctx
->txn
);
1182 ovsrec_bridge_set_name(br
, br_name
);
1183 ovsrec_bridge_set_ports(br
, &port
, 1);
1185 ovs_insert_bridge(ctx
->ovs
, br
);
1187 struct vsctl_bridge
*parent
;
1188 struct ovsrec_port
*port
;
1189 struct ovsrec_interface
*iface
;
1190 struct ovsrec_bridge
*br
;
1193 parent
= find_bridge(&info
, parent_name
, false);
1194 if (parent
&& parent
->vlan
) {
1195 vsctl_fatal("cannot create bridge with fake bridge as parent");
1198 vsctl_fatal("parent bridge %s does not exist", parent_name
);
1200 br
= parent
->br_cfg
;
1202 iface
= ovsrec_interface_insert(ctx
->txn
);
1203 ovsrec_interface_set_name(iface
, br_name
);
1204 ovsrec_interface_set_type(iface
, "internal");
1206 port
= ovsrec_port_insert(ctx
->txn
);
1207 ovsrec_port_set_name(port
, br_name
);
1208 ovsrec_port_set_interfaces(port
, &iface
, 1);
1209 ovsrec_port_set_fake_bridge(port
, true);
1210 ovsrec_port_set_tag(port
, &tag
, 1);
1212 bridge_insert_port(br
, port
);
1219 del_port(struct vsctl_info
*info
, struct vsctl_port
*port
)
1221 struct shash_node
*node
;
1223 SHASH_FOR_EACH (node
, &info
->ifaces
) {
1224 struct vsctl_iface
*iface
= node
->data
;
1225 if (iface
->port
== port
) {
1226 ovsrec_interface_delete(iface
->iface_cfg
);
1229 ovsrec_port_delete(port
->port_cfg
);
1231 bridge_delete_port((port
->bridge
->parent
1232 ? port
->bridge
->parent
->br_cfg
1233 : port
->bridge
->br_cfg
), port
->port_cfg
);
1237 cmd_del_br(struct vsctl_context
*ctx
)
1239 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1240 struct vsctl_bridge
*bridge
;
1241 struct vsctl_info info
;
1243 get_info(ctx
, &info
);
1244 bridge
= find_bridge(&info
, ctx
->argv
[1], must_exist
);
1246 struct shash_node
*node
;
1248 SHASH_FOR_EACH (node
, &info
.ports
) {
1249 struct vsctl_port
*port
= node
->data
;
1250 if (port
->bridge
== bridge
|| port
->bridge
->parent
== bridge
1251 || !strcmp(port
->port_cfg
->name
, bridge
->name
)) {
1252 del_port(&info
, port
);
1255 if (bridge
->br_cfg
) {
1256 ovsrec_bridge_delete(bridge
->br_cfg
);
1257 ovs_delete_bridge(ctx
->ovs
, bridge
->br_cfg
);
1264 output_sorted(struct svec
*svec
, struct ds
*output
)
1270 SVEC_FOR_EACH (i
, name
, svec
) {
1271 ds_put_format(output
, "%s\n", name
);
1276 cmd_list_br(struct vsctl_context
*ctx
)
1278 struct shash_node
*node
;
1279 struct vsctl_info info
;
1280 struct svec bridges
;
1282 get_info(ctx
, &info
);
1284 svec_init(&bridges
);
1285 SHASH_FOR_EACH (node
, &info
.bridges
) {
1286 struct vsctl_bridge
*br
= node
->data
;
1287 svec_add(&bridges
, br
->name
);
1289 output_sorted(&bridges
, &ctx
->output
);
1290 svec_destroy(&bridges
);
1296 cmd_br_exists(struct vsctl_context
*ctx
)
1298 struct vsctl_info info
;
1300 get_info(ctx
, &info
);
1301 if (!find_bridge(&info
, ctx
->argv
[1], false)) {
1307 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1308 * equals 'a', false otherwise. */
1310 key_matches(const char *a
,
1311 const char *b_prefix
, size_t b_prefix_len
, const char *b
)
1313 return !strncmp(a
, b_prefix
, b_prefix_len
) && !strcmp(a
+ b_prefix_len
, b
);
1317 set_external_id(char **old_keys
, char **old_values
, size_t old_n
,
1318 char *key
, char *value
,
1319 char ***new_keysp
, char ***new_valuesp
, size_t *new_np
)
1326 new_keys
= xmalloc(sizeof *new_keys
* (old_n
+ 1));
1327 new_values
= xmalloc(sizeof *new_values
* (old_n
+ 1));
1329 for (i
= 0; i
< old_n
; i
++) {
1330 if (strcmp(key
, old_keys
[i
])) {
1331 new_keys
[new_n
] = old_keys
[i
];
1332 new_values
[new_n
] = old_values
[i
];
1337 new_keys
[new_n
] = key
;
1338 new_values
[new_n
] = value
;
1341 *new_keysp
= new_keys
;
1342 *new_valuesp
= new_values
;
1347 pre_cmd_br_set_external_id(struct vsctl_context
*ctx
)
1350 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_external_ids
);
1351 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_external_ids
);
1355 cmd_br_set_external_id(struct vsctl_context
*ctx
)
1357 struct vsctl_info info
;
1358 struct vsctl_bridge
*bridge
;
1359 char **keys
, **values
;
1362 get_info(ctx
, &info
);
1363 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1364 if (bridge
->br_cfg
) {
1365 set_external_id(bridge
->br_cfg
->key_external_ids
,
1366 bridge
->br_cfg
->value_external_ids
,
1367 bridge
->br_cfg
->n_external_ids
,
1368 ctx
->argv
[2], ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1369 &keys
, &values
, &n
);
1370 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1371 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, keys
, values
, n
);
1373 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1374 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1375 set_external_id(port
->port_cfg
->key_external_ids
,
1376 port
->port_cfg
->value_external_ids
,
1377 port
->port_cfg
->n_external_ids
,
1378 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
,
1379 &keys
, &values
, &n
);
1380 ovsrec_port_verify_external_ids(port
->port_cfg
);
1381 ovsrec_port_set_external_ids(port
->port_cfg
, keys
, values
, n
);
1391 get_external_id(char **keys
, char **values
, size_t n
,
1392 const char *prefix
, const char *key
,
1395 size_t prefix_len
= strlen(prefix
);
1400 for (i
= 0; i
< n
; i
++) {
1401 if (!key
&& !strncmp(keys
[i
], prefix
, prefix_len
)) {
1402 svec_add_nocopy(&svec
, xasprintf("%s=%s",
1403 keys
[i
] + prefix_len
, values
[i
]));
1404 } else if (key
&& key_matches(keys
[i
], prefix
, prefix_len
, key
)) {
1405 svec_add(&svec
, values
[i
]);
1409 output_sorted(&svec
, output
);
1410 svec_destroy(&svec
);
1414 pre_cmd_br_get_external_id(struct vsctl_context
*ctx
)
1416 pre_cmd_br_set_external_id(ctx
);
1420 cmd_br_get_external_id(struct vsctl_context
*ctx
)
1422 struct vsctl_info info
;
1423 struct vsctl_bridge
*bridge
;
1425 get_info(ctx
, &info
);
1426 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1427 if (bridge
->br_cfg
) {
1428 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1429 get_external_id(bridge
->br_cfg
->key_external_ids
,
1430 bridge
->br_cfg
->value_external_ids
,
1431 bridge
->br_cfg
->n_external_ids
,
1432 "", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
,
1435 struct vsctl_port
*port
= shash_find_data(&info
.ports
, ctx
->argv
[1]);
1436 ovsrec_port_verify_external_ids(port
->port_cfg
);
1437 get_external_id(port
->port_cfg
->key_external_ids
,
1438 port
->port_cfg
->value_external_ids
,
1439 port
->port_cfg
->n_external_ids
,
1440 "fake-bridge-", ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1447 cmd_list_ports(struct vsctl_context
*ctx
)
1449 struct vsctl_bridge
*br
;
1450 struct shash_node
*node
;
1451 struct vsctl_info info
;
1454 get_info(ctx
, &info
);
1455 br
= find_bridge(&info
, ctx
->argv
[1], true);
1456 ovsrec_bridge_verify_ports(br
->br_cfg
? br
->br_cfg
: br
->parent
->br_cfg
);
1459 SHASH_FOR_EACH (node
, &info
.ports
) {
1460 struct vsctl_port
*port
= node
->data
;
1462 if (strcmp(port
->port_cfg
->name
, br
->name
) && br
== port
->bridge
) {
1463 svec_add(&ports
, port
->port_cfg
->name
);
1466 output_sorted(&ports
, &ctx
->output
);
1467 svec_destroy(&ports
);
1473 add_port(struct vsctl_context
*ctx
,
1474 const char *br_name
, const char *port_name
,
1475 bool may_exist
, bool fake_iface
,
1476 char *iface_names
[], int n_ifaces
,
1477 char *settings
[], int n_settings
)
1479 struct vsctl_info info
;
1480 struct vsctl_bridge
*bridge
;
1481 struct ovsrec_interface
**ifaces
;
1482 struct ovsrec_port
*port
;
1485 get_info(ctx
, &info
);
1487 struct vsctl_port
*vsctl_port
;
1489 vsctl_port
= find_port(&info
, port_name
, false);
1491 struct svec want_names
, have_names
;
1493 svec_init(&want_names
);
1494 for (i
= 0; i
< n_ifaces
; i
++) {
1495 svec_add(&want_names
, iface_names
[i
]);
1497 svec_sort(&want_names
);
1499 svec_init(&have_names
);
1500 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1501 svec_add(&have_names
,
1502 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1504 svec_sort(&have_names
);
1506 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1507 char *command
= vsctl_context_to_string(ctx
);
1508 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1509 command
, port_name
, vsctl_port
->bridge
->name
);
1512 if (!svec_equal(&want_names
, &have_names
)) {
1513 char *have_names_string
= svec_join(&have_names
, ", ", "");
1514 char *command
= vsctl_context_to_string(ctx
);
1516 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1517 command
, port_name
, have_names_string
);
1520 svec_destroy(&want_names
);
1521 svec_destroy(&have_names
);
1526 check_conflicts(&info
, port_name
,
1527 xasprintf("cannot create a port named %s", port_name
));
1528 for (i
= 0; i
< n_ifaces
; i
++) {
1529 check_conflicts(&info
, iface_names
[i
],
1530 xasprintf("cannot create an interface named %s",
1533 bridge
= find_bridge(&info
, br_name
, true);
1535 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1536 for (i
= 0; i
< n_ifaces
; i
++) {
1537 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1538 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1541 port
= ovsrec_port_insert(ctx
->txn
);
1542 ovsrec_port_set_name(port
, port_name
);
1543 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1544 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1548 int64_t tag
= bridge
->vlan
;
1549 ovsrec_port_set_tag(port
, &tag
, 1);
1552 for (i
= 0; i
< n_settings
; i
++) {
1553 set_column(get_table("Port"), &port
->header_
, settings
[i
],
1557 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1558 : bridge
->br_cfg
), port
);
1564 cmd_add_port(struct vsctl_context
*ctx
)
1566 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1568 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1569 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1573 cmd_add_bond(struct vsctl_context
*ctx
)
1575 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1576 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1580 n_ifaces
= ctx
->argc
- 3;
1581 for (i
= 3; i
< ctx
->argc
; i
++) {
1582 if (strchr(ctx
->argv
[i
], '=')) {
1588 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1589 "%d were specified", n_ifaces
);
1592 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1593 &ctx
->argv
[3], n_ifaces
,
1594 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1598 cmd_del_port(struct vsctl_context
*ctx
)
1600 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1601 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1602 struct vsctl_port
*port
;
1603 struct vsctl_info info
;
1605 get_info(ctx
, &info
);
1607 port
= find_port(&info
, ctx
->argv
[ctx
->argc
- 1], must_exist
);
1609 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1610 struct vsctl_iface
*iface
;
1612 port
= find_port(&info
, target
, false);
1614 iface
= find_iface(&info
, target
, false);
1619 if (must_exist
&& !port
) {
1620 vsctl_fatal("no port or interface named %s", target
);
1625 if (ctx
->argc
== 3) {
1626 struct vsctl_bridge
*bridge
;
1628 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1629 if (port
->bridge
!= bridge
) {
1630 if (port
->bridge
->parent
== bridge
) {
1631 vsctl_fatal("bridge %s does not have a port %s (although "
1632 "its parent bridge %s does)",
1633 ctx
->argv
[1], ctx
->argv
[2],
1634 bridge
->parent
->name
);
1636 vsctl_fatal("bridge %s does not have a port %s",
1637 ctx
->argv
[1], ctx
->argv
[2]);
1642 del_port(&info
, port
);
1649 cmd_port_to_br(struct vsctl_context
*ctx
)
1651 struct vsctl_port
*port
;
1652 struct vsctl_info info
;
1654 get_info(ctx
, &info
);
1655 port
= find_port(&info
, ctx
->argv
[1], true);
1656 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1661 cmd_br_to_vlan(struct vsctl_context
*ctx
)
1663 struct vsctl_bridge
*bridge
;
1664 struct vsctl_info info
;
1666 get_info(ctx
, &info
);
1667 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1668 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1673 cmd_br_to_parent(struct vsctl_context
*ctx
)
1675 struct vsctl_bridge
*bridge
;
1676 struct vsctl_info info
;
1678 get_info(ctx
, &info
);
1679 bridge
= find_bridge(&info
, ctx
->argv
[1], true);
1680 if (bridge
->parent
) {
1681 bridge
= bridge
->parent
;
1683 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1688 cmd_list_ifaces(struct vsctl_context
*ctx
)
1690 struct vsctl_bridge
*br
;
1691 struct shash_node
*node
;
1692 struct vsctl_info info
;
1695 get_info(ctx
, &info
);
1696 br
= find_bridge(&info
, ctx
->argv
[1], true);
1700 SHASH_FOR_EACH (node
, &info
.ifaces
) {
1701 struct vsctl_iface
*iface
= node
->data
;
1703 if (strcmp(iface
->iface_cfg
->name
, br
->name
)
1704 && br
== iface
->port
->bridge
) {
1705 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1708 output_sorted(&ifaces
, &ctx
->output
);
1709 svec_destroy(&ifaces
);
1715 cmd_iface_to_br(struct vsctl_context
*ctx
)
1717 struct vsctl_iface
*iface
;
1718 struct vsctl_info info
;
1720 get_info(ctx
, &info
);
1721 iface
= find_iface(&info
, ctx
->argv
[1], true);
1722 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1727 verify_controllers(struct ovsrec_bridge
*bridge
)
1732 ovsrec_bridge_verify_controller(bridge
);
1733 for (i
= 0; i
< bridge
->n_controller
; i
++) {
1734 ovsrec_controller_verify_target(bridge
->controller
[i
]);
1740 pre_controller(struct vsctl_context
*ctx
)
1744 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_controller_col_target
);
1748 cmd_get_controller(struct vsctl_context
*ctx
)
1750 struct vsctl_info info
;
1751 struct vsctl_bridge
*br
;
1752 struct svec targets
;
1755 get_info(ctx
, &info
);
1756 br
= find_bridge(&info
, ctx
->argv
[1], true);
1757 verify_controllers(br
->br_cfg
);
1759 /* Print the targets in sorted order for reproducibility. */
1760 svec_init(&targets
);
1761 for (i
= 0; i
< br
->n_ctrl
; i
++) {
1762 svec_add(&targets
, br
->ctrl
[i
]->target
);
1765 svec_sort(&targets
);
1766 for (i
= 0; i
< targets
.n
; i
++) {
1767 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1769 svec_destroy(&targets
);
1775 delete_controllers(struct ovsrec_controller
**controllers
,
1776 size_t n_controllers
)
1780 for (i
= 0; i
< n_controllers
; i
++) {
1781 ovsrec_controller_delete(controllers
[i
]);
1786 cmd_del_controller(struct vsctl_context
*ctx
)
1788 struct vsctl_info info
;
1789 struct vsctl_bridge
*br
;
1791 get_info(ctx
, &info
);
1793 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1794 verify_controllers(br
->br_cfg
);
1797 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1798 ovsrec_bridge_set_controller(br
->br_cfg
, NULL
, 0);
1804 static struct ovsrec_controller
**
1805 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1807 struct ovsrec_controller
**controllers
;
1810 controllers
= xmalloc(n
* sizeof *controllers
);
1811 for (i
= 0; i
< n
; i
++) {
1812 controllers
[i
] = ovsrec_controller_insert(txn
);
1813 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1820 cmd_set_controller(struct vsctl_context
*ctx
)
1822 struct vsctl_info info
;
1823 struct vsctl_bridge
*br
;
1824 struct ovsrec_controller
**controllers
;
1827 get_info(ctx
, &info
);
1828 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1829 verify_controllers(br
->br_cfg
);
1831 delete_controllers(br
->ctrl
, br
->n_ctrl
);
1834 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1835 ovsrec_bridge_set_controller(br
->br_cfg
, controllers
, n
);
1842 cmd_get_fail_mode(struct vsctl_context
*ctx
)
1844 struct vsctl_info info
;
1845 struct vsctl_bridge
*br
;
1847 get_info(ctx
, &info
);
1848 br
= find_bridge(&info
, ctx
->argv
[1], true);
1851 ovsrec_bridge_verify_fail_mode(br
->br_cfg
);
1853 if (br
->fail_mode
&& strlen(br
->fail_mode
)) {
1854 ds_put_format(&ctx
->output
, "%s\n", br
->fail_mode
);
1861 cmd_del_fail_mode(struct vsctl_context
*ctx
)
1863 struct vsctl_info info
;
1864 struct vsctl_bridge
*br
;
1866 get_info(ctx
, &info
);
1867 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1869 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1875 cmd_set_fail_mode(struct vsctl_context
*ctx
)
1877 struct vsctl_info info
;
1878 struct vsctl_bridge
*br
;
1879 const char *fail_mode
= ctx
->argv
[2];
1881 get_info(ctx
, &info
);
1882 br
= find_real_bridge(&info
, ctx
->argv
[1], true);
1884 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1885 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1888 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1894 verify_managers(const struct ovsrec_open_vswitch
*ovs
)
1898 ovsrec_open_vswitch_verify_manager_options(ovs
);
1900 for (i
= 0; i
< ovs
->n_manager_options
; ++i
) {
1901 const struct ovsrec_manager
*mgr
= ovs
->manager_options
[i
];
1903 ovsrec_manager_verify_target(mgr
);
1908 pre_manager(struct vsctl_context
*ctx
)
1910 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1911 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_manager_col_target
);
1915 cmd_get_manager(struct vsctl_context
*ctx
)
1917 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
1918 struct svec targets
;
1921 verify_managers(ovs
);
1923 /* Print the targets in sorted order for reproducibility. */
1924 svec_init(&targets
);
1926 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1927 svec_add(&targets
, ovs
->manager_options
[i
]->target
);
1930 svec_sort_unique(&targets
);
1931 for (i
= 0; i
< targets
.n
; i
++) {
1932 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1934 svec_destroy(&targets
);
1938 delete_managers(const struct vsctl_context
*ctx
)
1940 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
1943 /* Delete Manager rows pointed to by 'manager_options' column. */
1944 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1945 ovsrec_manager_delete(ovs
->manager_options
[i
]);
1948 /* Delete 'Manager' row refs in 'manager_options' column. */
1949 ovsrec_open_vswitch_set_manager_options(ovs
, NULL
, 0);
1953 cmd_del_manager(struct vsctl_context
*ctx
)
1955 const struct ovsrec_open_vswitch
*ovs
= ctx
->ovs
;
1957 verify_managers(ovs
);
1958 delete_managers(ctx
);
1962 insert_managers(struct vsctl_context
*ctx
, char *targets
[], size_t n
)
1964 struct ovsrec_manager
**managers
;
1967 /* Insert each manager in a new row in Manager table. */
1968 managers
= xmalloc(n
* sizeof *managers
);
1969 for (i
= 0; i
< n
; i
++) {
1970 managers
[i
] = ovsrec_manager_insert(ctx
->txn
);
1971 ovsrec_manager_set_target(managers
[i
], targets
[i
]);
1974 /* Store uuids of new Manager rows in 'manager_options' column. */
1975 ovsrec_open_vswitch_set_manager_options(ctx
->ovs
, managers
, n
);
1980 cmd_set_manager(struct vsctl_context
*ctx
)
1982 const size_t n
= ctx
->argc
- 1;
1984 verify_managers(ctx
->ovs
);
1985 delete_managers(ctx
);
1986 insert_managers(ctx
, &ctx
->argv
[1], n
);
1990 pre_cmd_get_ssl(struct vsctl_context
*ctx
)
1992 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1994 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_private_key
);
1995 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_certificate
);
1996 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_ca_cert
);
1997 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_bootstrap_ca_cert
);
2001 cmd_get_ssl(struct vsctl_context
*ctx
)
2003 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
2005 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
2007 ovsrec_ssl_verify_private_key(ssl
);
2008 ovsrec_ssl_verify_certificate(ssl
);
2009 ovsrec_ssl_verify_ca_cert(ssl
);
2010 ovsrec_ssl_verify_bootstrap_ca_cert(ssl
);
2012 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
2013 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
2014 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
2015 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
2016 ssl
->bootstrap_ca_cert
? "true" : "false");
2021 pre_cmd_del_ssl(struct vsctl_context
*ctx
)
2023 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2027 cmd_del_ssl(struct vsctl_context
*ctx
)
2029 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
2032 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
2033 ovsrec_ssl_delete(ssl
);
2034 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, NULL
);
2039 pre_cmd_set_ssl(struct vsctl_context
*ctx
)
2041 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2045 cmd_set_ssl(struct vsctl_context
*ctx
)
2047 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
2048 struct ovsrec_ssl
*ssl
= ctx
->ovs
->ssl
;
2050 ovsrec_open_vswitch_verify_ssl(ctx
->ovs
);
2052 ovsrec_ssl_delete(ssl
);
2054 ssl
= ovsrec_ssl_insert(ctx
->txn
);
2056 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
2057 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
2058 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
2060 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
2062 ovsrec_open_vswitch_set_ssl(ctx
->ovs
, ssl
);
2065 /* Parameter commands. */
2067 struct vsctl_row_id
{
2068 const struct ovsdb_idl_table_class
*table
;
2069 const struct ovsdb_idl_column
*name_column
;
2070 const struct ovsdb_idl_column
*uuid_column
;
2073 struct vsctl_table_class
{
2074 struct ovsdb_idl_table_class
*class;
2075 struct vsctl_row_id row_ids
[2];
2078 static const struct vsctl_table_class tables
[] = {
2079 {&ovsrec_table_bridge
,
2080 {{&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
2081 {NULL
, NULL
, NULL
}}},
2083 {&ovsrec_table_controller
,
2084 {{&ovsrec_table_bridge
,
2085 &ovsrec_bridge_col_name
,
2086 &ovsrec_bridge_col_controller
}}},
2088 {&ovsrec_table_interface
,
2089 {{&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
2090 {NULL
, NULL
, NULL
}}},
2092 {&ovsrec_table_mirror
,
2093 {{&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
2094 {NULL
, NULL
, NULL
}}},
2096 {&ovsrec_table_manager
,
2097 {{&ovsrec_table_manager
, &ovsrec_manager_col_target
, NULL
},
2098 {NULL
, NULL
, NULL
}}},
2100 {&ovsrec_table_netflow
,
2101 {{&ovsrec_table_bridge
,
2102 &ovsrec_bridge_col_name
,
2103 &ovsrec_bridge_col_netflow
},
2104 {NULL
, NULL
, NULL
}}},
2106 {&ovsrec_table_open_vswitch
,
2107 {{&ovsrec_table_open_vswitch
, NULL
, NULL
},
2108 {NULL
, NULL
, NULL
}}},
2110 {&ovsrec_table_port
,
2111 {{&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
2112 {NULL
, NULL
, NULL
}}},
2115 {{&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
2116 {NULL
, NULL
, NULL
}}},
2118 {&ovsrec_table_queue
,
2119 {{NULL
, NULL
, NULL
},
2120 {NULL
, NULL
, NULL
}}},
2123 {{&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
}}},
2125 {&ovsrec_table_sflow
,
2126 {{&ovsrec_table_bridge
,
2127 &ovsrec_bridge_col_name
,
2128 &ovsrec_bridge_col_sflow
},
2129 {NULL
, NULL
, NULL
}}},
2131 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
2135 die_if_error(char *error
)
2138 vsctl_fatal("%s", error
);
2143 to_lower_and_underscores(unsigned c
)
2145 return c
== '-' ? '_' : tolower(c
);
2149 score_partial_match(const char *name
, const char *s
)
2153 if (!strcmp(name
, s
)) {
2156 for (score
= 0; ; score
++, name
++, s
++) {
2157 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
2159 } else if (*name
== '\0') {
2160 return UINT_MAX
- 1;
2163 return *s
== '\0' ? score
: 0;
2166 static const struct vsctl_table_class
*
2167 get_table(const char *table_name
)
2169 const struct vsctl_table_class
*table
;
2170 const struct vsctl_table_class
*best_match
= NULL
;
2171 unsigned int best_score
= 0;
2173 for (table
= tables
; table
->class; table
++) {
2174 unsigned int score
= score_partial_match(table
->class->name
,
2176 if (score
> best_score
) {
2179 } else if (score
== best_score
) {
2185 } else if (best_score
) {
2186 vsctl_fatal("multiple table names match \"%s\"", table_name
);
2188 vsctl_fatal("unknown table \"%s\"", table_name
);
2192 static const struct vsctl_table_class
*
2193 pre_get_table(struct vsctl_context
*ctx
, const char *table_name
)
2195 const struct vsctl_table_class
*table_class
;
2198 table_class
= get_table(table_name
);
2199 ovsdb_idl_add_table(ctx
->idl
, table_class
->class);
2201 for (i
= 0; i
< ARRAY_SIZE(table_class
->row_ids
); i
++) {
2202 const struct vsctl_row_id
*id
= &table_class
->row_ids
[i
];
2204 ovsdb_idl_add_table(ctx
->idl
, id
->table
);
2206 if (id
->name_column
) {
2207 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
2209 if (id
->uuid_column
) {
2210 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
2217 static const struct ovsdb_idl_row
*
2218 get_row_by_id(struct vsctl_context
*ctx
, const struct vsctl_table_class
*table
,
2219 const struct vsctl_row_id
*id
, const char *record_id
)
2221 const struct ovsdb_idl_row
*referrer
, *final
;
2227 if (!id
->name_column
) {
2228 if (strcmp(record_id
, ".")) {
2231 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2232 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
2236 const struct ovsdb_idl_row
*row
;
2239 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2241 row
= ovsdb_idl_next_row(row
))
2243 const struct ovsdb_datum
*name
;
2245 name
= ovsdb_idl_get(row
, id
->name_column
,
2246 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
2247 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
2249 vsctl_fatal("multiple rows in %s match \"%s\"",
2250 table
->class->name
, record_id
);
2261 if (id
->uuid_column
) {
2262 const struct ovsdb_datum
*uuid
;
2264 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
2265 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
2266 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
2268 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
2269 &uuid
->keys
[0].uuid
);
2278 static const struct ovsdb_idl_row
*
2279 get_row (struct vsctl_context
*ctx
,
2280 const struct vsctl_table_class
*table
, const char *record_id
)
2282 const struct ovsdb_idl_row
*row
;
2285 if (uuid_from_string(&uuid
, record_id
)) {
2286 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
2290 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
2291 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
2300 static const struct ovsdb_idl_row
*
2301 must_get_row(struct vsctl_context
*ctx
,
2302 const struct vsctl_table_class
*table
, const char *record_id
)
2304 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
2306 vsctl_fatal("no row \"%s\" in table %s",
2307 record_id
, table
->class->name
);
2313 get_column(const struct vsctl_table_class
*table
, const char *column_name
,
2314 const struct ovsdb_idl_column
**columnp
)
2316 const struct ovsdb_idl_column
*best_match
= NULL
;
2317 unsigned int best_score
= 0;
2320 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2321 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2322 unsigned int score
= score_partial_match(column
->name
, column_name
);
2323 if (score
> best_score
) {
2324 best_match
= column
;
2326 } else if (score
== best_score
) {
2331 *columnp
= best_match
;
2334 } else if (best_score
) {
2335 return xasprintf("%s contains more than one column whose name "
2336 "matches \"%s\"", table
->class->name
, column_name
);
2338 return xasprintf("%s does not contain a column whose name matches "
2339 "\"%s\"", table
->class->name
, column_name
);
2343 static struct ovsdb_symbol
*
2344 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
2346 struct ovsdb_symbol
*symbol
;
2349 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2353 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
2356 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
2357 if (symbol
->created
) {
2358 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2361 symbol
->created
= true;
2366 pre_get_column(struct vsctl_context
*ctx
,
2367 const struct vsctl_table_class
*table
, const char *column_name
,
2368 const struct ovsdb_idl_column
**columnp
)
2370 die_if_error(get_column(table
, column_name
, columnp
));
2371 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
2375 missing_operator_error(const char *arg
, const char **allowed_operators
,
2381 ds_put_format(&s
, "%s: argument does not end in ", arg
);
2382 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
2383 if (n_allowed
== 2) {
2384 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
2385 } else if (n_allowed
> 2) {
2388 for (i
= 1; i
< n_allowed
- 1; i
++) {
2389 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
2391 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
2393 ds_put_format(&s
, " followed by a value.");
2395 return ds_steal_cstr(&s
);
2398 /* Breaks 'arg' apart into a number of fields in the following order:
2400 * - The name of a column in 'table', stored into '*columnp'. The column
2401 * name may be abbreviated.
2403 * - Optionally ':' followed by a key string. The key is stored as a
2404 * malloc()'d string into '*keyp', or NULL if no key is present in
2407 * - If 'valuep' is nonnull, an operator followed by a value string. The
2408 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2409 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2410 * operator is stored into '*operatorp' (one of the pointers from
2411 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2412 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2415 * On success, returns NULL. On failure, returned a malloc()'d string error
2416 * message and stores NULL into all of the nonnull output arguments. */
2417 static char * WARN_UNUSED_RESULT
2418 parse_column_key_value(const char *arg
,
2419 const struct vsctl_table_class
*table
,
2420 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2421 const char **operatorp
,
2422 const char **allowed_operators
, size_t n_allowed
,
2425 const char *p
= arg
;
2429 assert(!(operatorp
&& !valuep
));
2435 /* Parse column name. */
2436 error
= ovsdb_token_parse(&p
, &column_name
);
2440 if (column_name
[0] == '\0') {
2442 error
= xasprintf("%s: missing column name", arg
);
2445 error
= get_column(table
, column_name
, columnp
);
2451 /* Parse key string. */
2454 error
= ovsdb_token_parse(&p
, keyp
);
2460 /* Parse value string. */
2466 if (!allowed_operators
) {
2467 static const char *equals
= "=";
2468 allowed_operators
= &equals
;
2474 for (i
= 0; i
< n_allowed
; i
++) {
2475 const char *op
= allowed_operators
[i
];
2476 size_t op_len
= strlen(op
);
2478 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2484 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2491 *valuep
= xstrdup(p
+ best_len
);
2494 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2516 pre_parse_column_key_value(struct vsctl_context
*ctx
,
2518 const struct vsctl_table_class
*table
)
2520 const struct ovsdb_idl_column
*column
;
2525 die_if_error(ovsdb_token_parse(&p
, &column_name
));
2526 if (column_name
[0] == '\0') {
2527 vsctl_fatal("%s: missing column name", arg
);
2530 pre_get_column(ctx
, table
, column_name
, &column
);
2535 pre_cmd_get(struct vsctl_context
*ctx
)
2537 const char *id
= shash_find_data(&ctx
->options
, "--id");
2538 const char *table_name
= ctx
->argv
[1];
2539 const struct vsctl_table_class
*table
;
2542 /* Using "get" without --id or a column name could possibly make sense.
2543 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2544 * But it is unlikely that an interactive user would want to do that, so
2545 * issue a warning if we're running on a terminal. */
2546 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
2547 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2548 "possibly erroneous");
2551 table
= pre_get_table(ctx
, table_name
);
2552 for (i
= 3; i
< ctx
->argc
; i
++) {
2553 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2554 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2558 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2563 cmd_get(struct vsctl_context
*ctx
)
2565 const char *id
= shash_find_data(&ctx
->options
, "--id");
2566 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2567 const char *table_name
= ctx
->argv
[1];
2568 const char *record_id
= ctx
->argv
[2];
2569 const struct vsctl_table_class
*table
;
2570 const struct ovsdb_idl_row
*row
;
2571 struct ds
*out
= &ctx
->output
;
2574 table
= get_table(table_name
);
2575 row
= must_get_row(ctx
, table
, record_id
);
2577 struct ovsdb_symbol
*symbol
;
2580 symbol
= create_symbol(ctx
->symtab
, id
, &new);
2582 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2583 "before it was defined", id
);
2585 symbol
->uuid
= row
->uuid
;
2587 /* This symbol refers to a row that already exists, so disable warnings
2588 * about it being unreferenced. */
2589 symbol
->strong_ref
= true;
2591 for (i
= 3; i
< ctx
->argc
; i
++) {
2592 const struct ovsdb_idl_column
*column
;
2593 const struct ovsdb_datum
*datum
;
2596 /* Special case for obtaining the UUID of a row. We can't just do this
2597 * through parse_column_key_value() below since it returns a "struct
2598 * ovsdb_idl_column" and the UUID column doesn't have one. */
2599 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2600 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2601 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2605 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2606 &column
, &key_string
,
2607 NULL
, NULL
, 0, NULL
));
2609 ovsdb_idl_txn_verify(row
, column
);
2610 datum
= ovsdb_idl_read(row
, column
);
2612 union ovsdb_atom key
;
2615 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2616 vsctl_fatal("cannot specify key to get for non-map column %s",
2620 die_if_error(ovsdb_atom_from_string(&key
,
2622 key_string
, ctx
->symtab
));
2624 idx
= ovsdb_datum_find_key(datum
, &key
,
2625 column
->type
.key
.type
);
2626 if (idx
== UINT_MAX
) {
2628 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2629 key_string
, table
->class->name
, record_id
,
2633 ovsdb_atom_to_string(&datum
->values
[idx
],
2634 column
->type
.value
.type
, out
);
2636 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2638 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2640 ds_put_char(out
, '\n');
2647 parse_column_names(const char *column_names
,
2648 const struct vsctl_table_class
*table
,
2649 const struct ovsdb_idl_column
***columnsp
,
2652 const struct ovsdb_idl_column
**columns
;
2655 if (!column_names
) {
2658 n_columns
= table
->class->n_columns
+ 1;
2659 columns
= xmalloc(n_columns
* sizeof *columns
);
2661 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2662 columns
[i
+ 1] = &table
->class->columns
[i
];
2665 char *s
= xstrdup(column_names
);
2666 size_t allocated_columns
;
2667 char *save_ptr
= NULL
;
2671 allocated_columns
= n_columns
= 0;
2672 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
2673 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
2674 const struct ovsdb_idl_column
*column
;
2676 if (!strcasecmp(column_name
, "_uuid")) {
2679 die_if_error(get_column(table
, column_name
, &column
));
2681 if (n_columns
>= allocated_columns
) {
2682 columns
= x2nrealloc(columns
, &allocated_columns
,
2685 columns
[n_columns
++] = column
;
2690 vsctl_fatal("must specify at least one column name");
2693 *columnsp
= columns
;
2694 *n_columnsp
= n_columns
;
2699 pre_list_columns(struct vsctl_context
*ctx
,
2700 const struct vsctl_table_class
*table
,
2701 const char *column_names
)
2703 const struct ovsdb_idl_column
**columns
;
2707 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2708 for (i
= 0; i
< n_columns
; i
++) {
2710 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
2717 pre_cmd_list(struct vsctl_context
*ctx
)
2719 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2720 const char *table_name
= ctx
->argv
[1];
2721 const struct vsctl_table_class
*table
;
2723 table
= pre_get_table(ctx
, table_name
);
2724 pre_list_columns(ctx
, table
, column_names
);
2727 static struct table
*
2728 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
2733 out
= xmalloc(sizeof *out
);
2736 for (i
= 0; i
< n_columns
; i
++) {
2737 const struct ovsdb_idl_column
*column
= columns
[i
];
2738 const char *column_name
= column
? column
->name
: "_uuid";
2740 table_add_column(out
, "%s", column_name
);
2747 list_record(const struct ovsdb_idl_row
*row
,
2748 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
2754 for (i
= 0; i
< n_columns
; i
++) {
2755 const struct ovsdb_idl_column
*column
= columns
[i
];
2756 struct cell
*cell
= table_add_cell(out
);
2759 struct ovsdb_datum datum
;
2760 union ovsdb_atom atom
;
2762 atom
.uuid
= row
->uuid
;
2765 datum
.values
= NULL
;
2768 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
2769 cell
->type
= &ovsdb_type_uuid
;
2771 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
2773 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
2774 cell
->type
= &column
->type
;
2780 cmd_list(struct vsctl_context
*ctx
)
2782 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2783 const struct ovsdb_idl_column
**columns
;
2784 const char *table_name
= ctx
->argv
[1];
2785 const struct vsctl_table_class
*table
;
2790 table
= get_table(table_name
);
2791 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2792 out
= ctx
->table
= list_make_table(columns
, n_columns
);
2793 if (ctx
->argc
> 2) {
2794 for (i
= 2; i
< ctx
->argc
; i
++) {
2795 list_record(must_get_row(ctx
, table
, ctx
->argv
[i
]),
2796 columns
, n_columns
, out
);
2799 const struct ovsdb_idl_row
*row
;
2801 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
!= NULL
;
2802 row
= ovsdb_idl_next_row(row
)) {
2803 list_record(row
, columns
, n_columns
, out
);
2810 pre_cmd_find(struct vsctl_context
*ctx
)
2812 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2813 const char *table_name
= ctx
->argv
[1];
2814 const struct vsctl_table_class
*table
;
2817 table
= pre_get_table(ctx
, table_name
);
2818 pre_list_columns(ctx
, table
, column_names
);
2819 for (i
= 2; i
< ctx
->argc
; i
++) {
2820 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2825 cmd_find(struct vsctl_context
*ctx
)
2827 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2828 const struct ovsdb_idl_column
**columns
;
2829 const char *table_name
= ctx
->argv
[1];
2830 const struct vsctl_table_class
*table
;
2831 const struct ovsdb_idl_row
*row
;
2835 table
= get_table(table_name
);
2836 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2837 out
= ctx
->table
= list_make_table(columns
, n_columns
);
2838 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
;
2839 row
= ovsdb_idl_next_row(row
)) {
2842 for (i
= 2; i
< ctx
->argc
; i
++) {
2843 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
],
2848 list_record(row
, columns
, n_columns
, out
);
2856 pre_cmd_set(struct vsctl_context
*ctx
)
2858 const char *table_name
= ctx
->argv
[1];
2859 const struct vsctl_table_class
*table
;
2862 table
= pre_get_table(ctx
, table_name
);
2863 for (i
= 3; i
< ctx
->argc
; i
++) {
2864 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2869 set_column(const struct vsctl_table_class
*table
,
2870 const struct ovsdb_idl_row
*row
, const char *arg
,
2871 struct ovsdb_symbol_table
*symtab
)
2873 const struct ovsdb_idl_column
*column
;
2874 char *key_string
, *value_string
;
2877 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
2878 NULL
, NULL
, 0, &value_string
);
2879 die_if_error(error
);
2880 if (!value_string
) {
2881 vsctl_fatal("%s: missing value", arg
);
2885 union ovsdb_atom key
, value
;
2886 struct ovsdb_datum datum
;
2888 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2889 vsctl_fatal("cannot specify key to set for non-map column %s",
2893 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
2894 key_string
, symtab
));
2895 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
2896 value_string
, symtab
));
2898 ovsdb_datum_init_empty(&datum
);
2899 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
2901 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2902 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
2904 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
2905 &column
->type
, false);
2906 ovsdb_idl_txn_write(row
, column
, &datum
);
2908 struct ovsdb_datum datum
;
2910 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
2911 value_string
, symtab
));
2912 ovsdb_idl_txn_write(row
, column
, &datum
);
2920 cmd_set(struct vsctl_context
*ctx
)
2922 const char *table_name
= ctx
->argv
[1];
2923 const char *record_id
= ctx
->argv
[2];
2924 const struct vsctl_table_class
*table
;
2925 const struct ovsdb_idl_row
*row
;
2928 table
= get_table(table_name
);
2929 row
= must_get_row(ctx
, table
, record_id
);
2930 for (i
= 3; i
< ctx
->argc
; i
++) {
2931 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
2936 pre_cmd_add(struct vsctl_context
*ctx
)
2938 const char *table_name
= ctx
->argv
[1];
2939 const char *column_name
= ctx
->argv
[3];
2940 const struct vsctl_table_class
*table
;
2941 const struct ovsdb_idl_column
*column
;
2943 table
= pre_get_table(ctx
, table_name
);
2944 pre_get_column(ctx
, table
, column_name
, &column
);
2948 cmd_add(struct vsctl_context
*ctx
)
2950 const char *table_name
= ctx
->argv
[1];
2951 const char *record_id
= ctx
->argv
[2];
2952 const char *column_name
= ctx
->argv
[3];
2953 const struct vsctl_table_class
*table
;
2954 const struct ovsdb_idl_column
*column
;
2955 const struct ovsdb_idl_row
*row
;
2956 const struct ovsdb_type
*type
;
2957 struct ovsdb_datum old
;
2960 table
= get_table(table_name
);
2961 row
= must_get_row(ctx
, table
, record_id
);
2962 die_if_error(get_column(table
, column_name
, &column
));
2964 type
= &column
->type
;
2965 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
2966 for (i
= 4; i
< ctx
->argc
; i
++) {
2967 struct ovsdb_type add_type
;
2968 struct ovsdb_datum add
;
2972 add_type
.n_max
= UINT_MAX
;
2973 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
2975 ovsdb_datum_union(&old
, &add
, type
, false);
2976 ovsdb_datum_destroy(&add
, type
);
2978 if (old
.n
> type
->n_max
) {
2979 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2980 "table %s but the maximum number is %u",
2982 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
2983 column
->name
, table
->class->name
, type
->n_max
);
2985 ovsdb_idl_txn_verify(row
, column
);
2986 ovsdb_idl_txn_write(row
, column
, &old
);
2990 pre_cmd_remove(struct vsctl_context
*ctx
)
2992 const char *table_name
= ctx
->argv
[1];
2993 const char *column_name
= ctx
->argv
[3];
2994 const struct vsctl_table_class
*table
;
2995 const struct ovsdb_idl_column
*column
;
2997 table
= pre_get_table(ctx
, table_name
);
2998 pre_get_column(ctx
, table
, column_name
, &column
);
3002 cmd_remove(struct vsctl_context
*ctx
)
3004 const char *table_name
= ctx
->argv
[1];
3005 const char *record_id
= ctx
->argv
[2];
3006 const char *column_name
= ctx
->argv
[3];
3007 const struct vsctl_table_class
*table
;
3008 const struct ovsdb_idl_column
*column
;
3009 const struct ovsdb_idl_row
*row
;
3010 const struct ovsdb_type
*type
;
3011 struct ovsdb_datum old
;
3014 table
= get_table(table_name
);
3015 row
= must_get_row(ctx
, table
, record_id
);
3016 die_if_error(get_column(table
, column_name
, &column
));
3018 type
= &column
->type
;
3019 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
3020 for (i
= 4; i
< ctx
->argc
; i
++) {
3021 struct ovsdb_type rm_type
;
3022 struct ovsdb_datum rm
;
3027 rm_type
.n_max
= UINT_MAX
;
3028 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
3029 ctx
->argv
[i
], ctx
->symtab
);
3030 if (error
&& ovsdb_type_is_map(&rm_type
)) {
3032 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
3033 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
3034 ctx
->argv
[i
], ctx
->symtab
));
3036 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
3037 ovsdb_datum_destroy(&rm
, &rm_type
);
3039 if (old
.n
< type
->n_min
) {
3040 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3041 "table %s but the minimum number is %u",
3043 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
3044 column
->name
, table
->class->name
, type
->n_min
);
3046 ovsdb_idl_txn_verify(row
, column
);
3047 ovsdb_idl_txn_write(row
, column
, &old
);
3051 pre_cmd_clear(struct vsctl_context
*ctx
)
3053 const char *table_name
= ctx
->argv
[1];
3054 const struct vsctl_table_class
*table
;
3057 table
= pre_get_table(ctx
, table_name
);
3058 for (i
= 3; i
< ctx
->argc
; i
++) {
3059 const struct ovsdb_idl_column
*column
;
3061 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
3066 cmd_clear(struct vsctl_context
*ctx
)
3068 const char *table_name
= ctx
->argv
[1];
3069 const char *record_id
= ctx
->argv
[2];
3070 const struct vsctl_table_class
*table
;
3071 const struct ovsdb_idl_row
*row
;
3074 table
= get_table(table_name
);
3075 row
= must_get_row(ctx
, table
, record_id
);
3076 for (i
= 3; i
< ctx
->argc
; i
++) {
3077 const struct ovsdb_idl_column
*column
;
3078 const struct ovsdb_type
*type
;
3079 struct ovsdb_datum datum
;
3081 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
3083 type
= &column
->type
;
3084 if (type
->n_min
> 0) {
3085 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3086 "of table %s, which is not allowed to be empty",
3087 column
->name
, table
->class->name
);
3090 ovsdb_datum_init_empty(&datum
);
3091 ovsdb_idl_txn_write(row
, column
, &datum
);
3096 pre_create(struct vsctl_context
*ctx
)
3098 const char *id
= shash_find_data(&ctx
->options
, "--id");
3099 const char *table_name
= ctx
->argv
[1];
3100 const struct vsctl_table_class
*table
;
3102 table
= get_table(table_name
);
3103 if (!id
&& !table
->class->is_root
) {
3104 VLOG_WARN("applying \"create\" command to table %s without --id "
3105 "option will have no effect", table
->class->name
);
3110 cmd_create(struct vsctl_context
*ctx
)
3112 const char *id
= shash_find_data(&ctx
->options
, "--id");
3113 const char *table_name
= ctx
->argv
[1];
3114 const struct vsctl_table_class
*table
= get_table(table_name
);
3115 const struct ovsdb_idl_row
*row
;
3116 const struct uuid
*uuid
;
3120 struct ovsdb_symbol
*symbol
= create_symbol(ctx
->symtab
, id
, NULL
);
3121 if (table
->class->is_root
) {
3122 /* This table is in the root set, meaning that rows created in it
3123 * won't disappear even if they are unreferenced, so disable
3124 * warnings about that by pretending that there is a reference. */
3125 symbol
->strong_ref
= true;
3127 uuid
= &symbol
->uuid
;
3132 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
3133 for (i
= 2; i
< ctx
->argc
; i
++) {
3134 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
3136 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
3139 /* This function may be used as the 'postprocess' function for commands that
3140 * insert new rows into the database. It expects that the command's 'run'
3141 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3142 * sole output. It replaces that output by the row's permanent UUID assigned
3143 * by the database server and appends a new-line.
3145 * Currently we use this only for "create", because the higher-level commands
3146 * are supposed to be independent of the actual structure of the vswitch
3149 post_create(struct vsctl_context
*ctx
)
3151 const struct uuid
*real
;
3154 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
3157 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
3159 ds_clear(&ctx
->output
);
3160 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
3162 ds_put_char(&ctx
->output
, '\n');
3166 pre_cmd_destroy(struct vsctl_context
*ctx
)
3168 const char *table_name
= ctx
->argv
[1];
3170 pre_get_table(ctx
, table_name
);
3174 cmd_destroy(struct vsctl_context
*ctx
)
3176 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
3177 const char *table_name
= ctx
->argv
[1];
3178 const struct vsctl_table_class
*table
;
3181 table
= get_table(table_name
);
3182 for (i
= 2; i
< ctx
->argc
; i
++) {
3183 const struct ovsdb_idl_row
*row
;
3185 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
3187 ovsdb_idl_txn_delete(row
);
3193 is_condition_satisfied(const struct vsctl_table_class
*table
,
3194 const struct ovsdb_idl_row
*row
, const char *arg
,
3195 struct ovsdb_symbol_table
*symtab
)
3197 static const char *operators
[] = {
3198 "=", "!=", "<", ">", "<=", ">="
3201 const struct ovsdb_idl_column
*column
;
3202 const struct ovsdb_datum
*have_datum
;
3203 char *key_string
, *value_string
;
3204 const char *operator;
3209 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
3210 &operator, operators
, ARRAY_SIZE(operators
),
3212 die_if_error(error
);
3213 if (!value_string
) {
3214 vsctl_fatal("%s: missing value", arg
);
3217 have_datum
= ovsdb_idl_read(row
, column
);
3219 union ovsdb_atom want_key
, want_value
;
3221 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
3222 vsctl_fatal("cannot specify key to check for non-map column %s",
3226 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
3227 key_string
, symtab
));
3228 die_if_error(ovsdb_atom_from_string(&want_value
, &column
->type
.value
,
3229 value_string
, symtab
));
3231 idx
= ovsdb_datum_find_key(have_datum
,
3232 &want_key
, column
->type
.key
.type
);
3233 if (idx
!= UINT_MAX
) {
3234 cmp
= ovsdb_atom_compare_3way(&have_datum
->values
[idx
],
3236 column
->type
.value
.type
);
3239 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
3240 ovsdb_atom_destroy(&want_value
, column
->type
.value
.type
);
3242 struct ovsdb_datum want_datum
;
3244 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
3245 value_string
, symtab
));
3247 cmp
= ovsdb_datum_compare_3way(have_datum
, &want_datum
,
3249 ovsdb_datum_destroy(&want_datum
, &column
->type
);
3255 return (idx
== UINT_MAX
? false
3256 : !strcmp(operator, "=") ? cmp
== 0
3257 : !strcmp(operator, "!=") ? cmp
!= 0
3258 : !strcmp(operator, "<") ? cmp
< 0
3259 : !strcmp(operator, ">") ? cmp
> 0
3260 : !strcmp(operator, "<=") ? cmp
<= 0
3261 : !strcmp(operator, ">=") ? cmp
>= 0
3266 pre_cmd_wait_until(struct vsctl_context
*ctx
)
3268 const char *table_name
= ctx
->argv
[1];
3269 const struct vsctl_table_class
*table
;
3272 table
= pre_get_table(ctx
, table_name
);
3274 for (i
= 3; i
< ctx
->argc
; i
++) {
3275 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
3280 cmd_wait_until(struct vsctl_context
*ctx
)
3282 const char *table_name
= ctx
->argv
[1];
3283 const char *record_id
= ctx
->argv
[2];
3284 const struct vsctl_table_class
*table
;
3285 const struct ovsdb_idl_row
*row
;
3288 table
= get_table(table_name
);
3290 row
= get_row(ctx
, table
, record_id
);
3292 ctx
->try_again
= true;
3296 for (i
= 3; i
< ctx
->argc
; i
++) {
3297 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
3298 ctx
->try_again
= true;
3304 static struct json
*
3305 where_uuid_equals(const struct uuid
*uuid
)
3308 json_array_create_1(
3309 json_array_create_3(
3310 json_string_create("_uuid"),
3311 json_string_create("=="),
3312 json_array_create_2(
3313 json_string_create("uuid"),
3314 json_string_create_nocopy(
3315 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
3319 vsctl_context_init(struct vsctl_context
*ctx
, struct vsctl_command
*command
,
3320 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
3321 const struct ovsrec_open_vswitch
*ovs
,
3322 struct ovsdb_symbol_table
*symtab
)
3324 ctx
->argc
= command
->argc
;
3325 ctx
->argv
= command
->argv
;
3326 ctx
->options
= command
->options
;
3328 ds_swap(&ctx
->output
, &command
->output
);
3329 ctx
->table
= command
->table
;
3333 ctx
->symtab
= symtab
;
3334 ctx
->verified_ports
= false;
3336 ctx
->try_again
= false;
3340 vsctl_context_done(struct vsctl_context
*ctx
, struct vsctl_command
*command
)
3342 ds_swap(&ctx
->output
, &command
->output
);
3343 command
->table
= ctx
->table
;
3347 run_prerequisites(struct vsctl_command
*commands
, size_t n_commands
,
3348 struct ovsdb_idl
*idl
)
3350 struct vsctl_command
*c
;
3352 ovsdb_idl_add_table(idl
, &ovsrec_table_open_vswitch
);
3353 if (wait_for_reload
) {
3354 ovsdb_idl_add_column(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
3356 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3357 if (c
->syntax
->prerequisites
) {
3358 struct vsctl_context ctx
;
3360 ds_init(&c
->output
);
3363 vsctl_context_init(&ctx
, c
, idl
, NULL
, NULL
, NULL
);
3364 (c
->syntax
->prerequisites
)(&ctx
);
3365 vsctl_context_done(&ctx
, c
);
3367 assert(!c
->output
.string
);
3374 do_vsctl(const char *args
, struct vsctl_command
*commands
, size_t n_commands
,
3375 struct ovsdb_idl
*idl
)
3377 struct ovsdb_idl_txn
*txn
;
3378 const struct ovsrec_open_vswitch
*ovs
;
3379 enum ovsdb_idl_txn_status status
;
3380 struct ovsdb_symbol_table
*symtab
;
3381 struct vsctl_command
*c
;
3382 struct shash_node
*node
;
3383 int64_t next_cfg
= 0;
3386 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
3388 ovsdb_idl_txn_set_dry_run(txn
);
3391 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
3393 ovs
= ovsrec_open_vswitch_first(idl
);
3395 /* XXX add verification that table is empty */
3396 ovs
= ovsrec_open_vswitch_insert(txn
);
3399 if (wait_for_reload
) {
3400 struct json
*where
= where_uuid_equals(&ovs
->header_
.uuid
);
3401 ovsdb_idl_txn_increment(txn
, "Open_vSwitch", "next_cfg", where
);
3402 json_destroy(where
);
3405 symtab
= ovsdb_symbol_table_create();
3406 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3407 ds_init(&c
->output
);
3410 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3411 struct vsctl_context ctx
;
3413 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
3414 (c
->syntax
->run
)(&ctx
);
3415 vsctl_context_done(&ctx
, c
);
3417 if (ctx
.try_again
) {
3422 SHASH_FOR_EACH (node
, &symtab
->sh
) {
3423 struct ovsdb_symbol
*symbol
= node
->data
;
3424 if (!symbol
->created
) {
3425 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3426 "with \"-- --id=%s create ...\")",
3427 node
->name
, node
->name
);
3429 if (!symbol
->strong_ref
) {
3430 if (!symbol
->weak_ref
) {
3431 VLOG_WARN("row id \"%s\" was created but no reference to it "
3432 "was inserted, so it will not actually appear in "
3433 "the database", node
->name
);
3435 VLOG_WARN("row id \"%s\" was created but only a weak "
3436 "reference to it was inserted, so it will not "
3437 "actually appear in the database", node
->name
);
3442 status
= ovsdb_idl_txn_commit_block(txn
);
3443 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
3444 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
3446 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
3447 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3448 if (c
->syntax
->postprocess
) {
3449 struct vsctl_context ctx
;
3451 vsctl_context_init(&ctx
, c
, idl
, txn
, ovs
, symtab
);
3452 (c
->syntax
->postprocess
)(&ctx
);
3453 vsctl_context_done(&ctx
, c
);
3457 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
3458 ovsdb_idl_txn_destroy(txn
);
3459 txn
= the_idl_txn
= NULL
;
3462 case TXN_INCOMPLETE
:
3466 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3467 vsctl_fatal("transaction aborted");
3477 vsctl_fatal("transaction error: %s", error
);
3484 ovsdb_symbol_table_destroy(symtab
);
3486 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3487 struct ds
*ds
= &c
->output
;
3490 table_print(c
->table
, &table_style
);
3491 } else if (oneline
) {
3495 for (j
= 0; j
< ds
->length
; j
++) {
3496 int ch
= ds
->string
[j
];
3499 fputs("\\n", stdout
);
3503 fputs("\\\\", stdout
);
3512 fputs(ds_cstr(ds
), stdout
);
3514 ds_destroy(&c
->output
);
3515 table_destroy(c
->table
);
3518 smap_destroy(&c
->options
);
3522 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
3525 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
3526 if (ovs
->cur_cfg
>= next_cfg
) {
3530 ovsdb_idl_wait(idl
);
3535 ovsdb_idl_destroy(idl
);
3540 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3541 * resources and return so that the caller can try again. */
3543 ovsdb_idl_txn_abort(txn
);
3544 ovsdb_idl_txn_destroy(txn
);
3546 ovsdb_symbol_table_destroy(symtab
);
3547 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3548 ds_destroy(&c
->output
);
3549 table_destroy(c
->table
);
3555 static const struct vsctl_command_syntax all_commands
[] = {
3556 /* Open vSwitch commands. */
3557 {"init", 0, 0, NULL
, cmd_init
, NULL
, "", RW
},
3559 /* Bridge commands. */
3560 {"add-br", 1, 3, pre_get_info
, cmd_add_br
, NULL
, "--may-exist", RW
},
3561 {"del-br", 1, 1, pre_get_info
, cmd_del_br
, NULL
, "--if-exists", RW
},
3562 {"list-br", 0, 0, pre_get_info
, cmd_list_br
, NULL
, "", RO
},
3563 {"br-exists", 1, 1, pre_get_info
, cmd_br_exists
, NULL
, "", RO
},
3564 {"br-to-vlan", 1, 1, pre_get_info
, cmd_br_to_vlan
, NULL
, "", RO
},
3565 {"br-to-parent", 1, 1, pre_get_info
, cmd_br_to_parent
, NULL
, "", RO
},
3566 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id
,
3567 cmd_br_set_external_id
, NULL
, "", RW
},
3568 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id
,
3569 cmd_br_get_external_id
, NULL
, "", RO
},
3571 /* Port commands. */
3572 {"list-ports", 1, 1, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
3573 {"add-port", 2, INT_MAX
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
3575 {"add-bond", 4, INT_MAX
, pre_get_info
, cmd_add_bond
, NULL
,
3576 "--may-exist,--fake-iface", RW
},
3577 {"del-port", 1, 2, pre_get_info
, cmd_del_port
, NULL
,
3578 "--if-exists,--with-iface", RW
},
3579 {"port-to-br", 1, 1, pre_get_info
, cmd_port_to_br
, NULL
, "", RO
},
3581 /* Interface commands. */
3582 {"list-ifaces", 1, 1, pre_get_info
, cmd_list_ifaces
, NULL
, "", RO
},
3583 {"iface-to-br", 1, 1, pre_get_info
, cmd_iface_to_br
, NULL
, "", RO
},
3585 /* Controller commands. */
3586 {"get-controller", 1, 1, pre_controller
, cmd_get_controller
, NULL
, "", RO
},
3587 {"del-controller", 1, 1, pre_controller
, cmd_del_controller
, NULL
, "", RW
},
3588 {"set-controller", 1, INT_MAX
, pre_controller
, cmd_set_controller
, NULL
,
3590 {"get-fail-mode", 1, 1, pre_get_info
, cmd_get_fail_mode
, NULL
, "", RO
},
3591 {"del-fail-mode", 1, 1, pre_get_info
, cmd_del_fail_mode
, NULL
, "", RW
},
3592 {"set-fail-mode", 2, 2, pre_get_info
, cmd_set_fail_mode
, NULL
, "", RW
},
3594 /* Manager commands. */
3595 {"get-manager", 0, 0, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
3596 {"del-manager", 0, INT_MAX
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
3597 {"set-manager", 1, INT_MAX
, pre_manager
, cmd_set_manager
, NULL
, "", RW
},
3600 {"get-ssl", 0, 0, pre_cmd_get_ssl
, cmd_get_ssl
, NULL
, "", RO
},
3601 {"del-ssl", 0, 0, pre_cmd_del_ssl
, cmd_del_ssl
, NULL
, "", RW
},
3602 {"set-ssl", 3, 3, pre_cmd_set_ssl
, cmd_set_ssl
, NULL
, "--bootstrap", RW
},
3604 /* Switch commands. */
3605 {"emer-reset", 0, 0, pre_cmd_emer_reset
, cmd_emer_reset
, NULL
, "", RW
},
3607 /* Parameter commands. */
3608 {"get", 2, INT_MAX
, pre_cmd_get
, cmd_get
, NULL
, "--if-exists,--id=", RO
},
3609 {"list", 1, INT_MAX
, pre_cmd_list
, cmd_list
, NULL
, "--columns=", RO
},
3610 {"find", 1, INT_MAX
, pre_cmd_find
, cmd_find
, NULL
, "--columns=", RO
},
3611 {"set", 3, INT_MAX
, pre_cmd_set
, cmd_set
, NULL
, "", RW
},
3612 {"add", 4, INT_MAX
, pre_cmd_add
, cmd_add
, NULL
, "", RW
},
3613 {"remove", 4, INT_MAX
, pre_cmd_remove
, cmd_remove
, NULL
, "", RW
},
3614 {"clear", 3, INT_MAX
, pre_cmd_clear
, cmd_clear
, NULL
, "", RW
},
3615 {"create", 2, INT_MAX
, pre_create
, cmd_create
, post_create
, "--id=", RW
},
3616 {"destroy", 1, INT_MAX
, pre_cmd_destroy
, cmd_destroy
, NULL
, "--if-exists",
3618 {"wait-until", 2, INT_MAX
, pre_cmd_wait_until
, cmd_wait_until
, NULL
, "",
3621 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, RO
},