2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
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 "db-ctl-base.h"
32 #include "command-line.h"
35 #include "fatal-signal.h"
37 #include "openvswitch/dynamic-string.h"
38 #include "openvswitch/json.h"
39 #include "openvswitch/ofp-parse.h"
40 #include "openvswitch/poll-loop.h"
41 #include "openvswitch/vconn.h"
42 #include "openvswitch/vlog.h"
43 #include "ovsdb-data.h"
44 #include "ovsdb-idl.h"
48 #include "stream-ssl.h"
52 #include "lib/vswitch-idl.h"
57 VLOG_DEFINE_THIS_MODULE(vsctl
);
61 /* --db: The database server to contact. */
62 static const char *db
;
64 /* --oneline: Write each command's output as a single line? */
67 /* --dry-run: Do not commit any changes. */
70 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
71 static bool wait_for_reload
= true;
73 /* --timeout: Time to wait for a connection to 'db'. */
74 static unsigned int timeout
;
76 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
77 * If false and --db says to use an active connection method (e.g. "unix:",
78 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
79 * error if the database server cannot be contacted (e.g. ovsdb-server is not
82 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
86 /* --leader-only, --no-leader-only: Only accept the leader in a cluster.
88 * In a real Open vSwitch environment, it doesn't make much sense to cluster
89 * the Open vSwitch database. This option exists to enable using ovs-vsctl to
90 * test OVSDB's clustering feature. */
91 static int leader_only
= true;
93 /* --shuffle-remotes, --no-shuffle-remotes: Shuffle the order of remotes that
94 * are specified in the connetion method string.
96 * In a real Open vSwitch environment, it doesn't make much sense to cluster
97 * the Open vSwitch database. This option exists to enable using ovs-vsctl to
98 * test OVSDB's clustering feature. */
99 static int shuffle_remotes
= true;
101 /* Format for table output. */
102 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
104 static void vsctl_cmd_init(void);
106 /* The IDL we're using and the current transaction, if any.
107 * This is for use by vsctl_exit() only, to allow it to clean up.
108 * Other code should use its context arguments. */
109 static struct ovsdb_idl
*the_idl
;
110 static struct ovsdb_idl_txn
*the_idl_txn
;
111 OVS_NO_RETURN
static void vsctl_exit(int status
);
113 OVS_NO_RETURN
static void usage(void);
114 static void parse_options(int argc
, char *argv
[], struct shash
*local_options
);
115 static void run_prerequisites(struct ctl_command
[], size_t n_commands
,
117 static bool do_vsctl(const char *args
, struct ctl_command
*, size_t n
,
120 /* post_db_reload_check frame work is to allow ovs-vsctl to do additional
121 * checks after OVSDB transactions are successfully recorded and reload by
124 * For example, When a new interface is added to OVSDB, ovs-vswitchd will
125 * either store a positive values on successful implementing the new
126 * interface, or -1 on failure.
128 * Unless --no-wait command line option is specified,
129 * post_db_reload_do_checks() is called right after any configuration
130 * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
131 * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
132 * more detailed messages about those errors.
134 * Current implementation only check for Post OVSDB reload failures on new
135 * interface additions with 'add-br' and 'add-port' commands.
137 * post_db_reload_expect_iface()
139 * keep track of interfaces to be checked post OVSDB reload. */
140 static void post_db_reload_check_init(void);
141 static void post_db_reload_do_checks(const struct vsctl_context
*);
142 static void post_db_reload_expect_iface(const struct ovsrec_interface
*);
144 static struct uuid
*neoteric_ifaces
;
145 static size_t n_neoteric_ifaces
;
146 static size_t allocated_neoteric_ifaces
;
149 main(int argc
, char *argv
[])
151 struct ovsdb_idl
*idl
;
152 struct ctl_command
*commands
;
153 struct shash local_options
;
157 set_program_name(argv
[0]);
158 fatal_ignore_sigpipe();
159 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
160 vlog_set_levels_from_string_assert("reconnect:warn");
164 /* Parse command line. */
165 char *args
= process_escape_args(argv
);
166 shash_init(&local_options
);
167 parse_options(argc
, argv
, &local_options
);
168 char *error
= ctl_parse_commands(argc
- optind
, argv
+ optind
,
169 &local_options
, &commands
, &n_commands
);
171 ctl_fatal("%s", error
);
173 VLOG(ctl_might_write_to_db(commands
, n_commands
) ? VLL_INFO
: VLL_DBG
,
174 "Called as %s", args
);
176 ctl_timeout_setup(timeout
);
178 /* Initialize IDL. */
179 idl
= the_idl
= ovsdb_idl_create_unconnected(&ovsrec_idl_class
, false);
180 ovsdb_idl_set_shuffle_remotes(idl
, shuffle_remotes
);
181 ovsdb_idl_set_remote(idl
, db
, retry
);
182 ovsdb_idl_set_leader_only(idl
, leader_only
);
183 run_prerequisites(commands
, n_commands
, idl
);
185 /* Execute the commands.
187 * 'seqno' is the database sequence number for which we last tried to
188 * execute our transaction. There's no point in trying to commit more than
189 * once for any given sequence number, because if the transaction fails
190 * it's because the database changed and we need to obtain an up-to-date
191 * view of the database before we try the transaction again. */
192 seqno
= ovsdb_idl_get_seqno(idl
);
195 if (!ovsdb_idl_is_alive(idl
)) {
196 int retval
= ovsdb_idl_get_last_error(idl
);
197 ctl_fatal("%s: database connection failed (%s)",
198 db
, ovs_retval_to_string(retval
));
201 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
202 seqno
= ovsdb_idl_get_seqno(idl
);
203 if (do_vsctl(args
, commands
, n_commands
, idl
)) {
209 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
217 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
220 OPT_DB
= UCHAR_MAX
+ 1,
225 OPT_BOOTSTRAP_CA_CERT
,
235 static const struct option global_long_options
[] = {
236 {"db", required_argument
, NULL
, OPT_DB
},
237 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
238 {"no-wait", no_argument
, NULL
, OPT_NO_WAIT
},
239 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
240 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
241 {"timeout", required_argument
, NULL
, 't'},
242 {"retry", no_argument
, NULL
, OPT_RETRY
},
243 {"help", no_argument
, NULL
, 'h'},
244 {"commands", no_argument
, NULL
, OPT_COMMANDS
},
245 {"options", no_argument
, NULL
, OPT_OPTIONS
},
246 {"leader-only", no_argument
, &leader_only
, true},
247 {"no-leader-only", no_argument
, &leader_only
, false},
248 {"shuffle-remotes", no_argument
, &shuffle_remotes
, true},
249 {"no-shuffle-remotes", no_argument
, &shuffle_remotes
, false},
250 {"version", no_argument
, NULL
, 'V'},
253 STREAM_SSL_LONG_OPTIONS
,
254 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
255 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
258 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
259 char *tmp
, *short_options
;
261 struct option
*options
;
262 size_t allocated_options
;
266 tmp
= ovs_cmdl_long_options_to_short_options(global_long_options
);
267 short_options
= xasprintf("+%s", tmp
);
270 /* We want to parse both global and command-specific options here, but
271 * getopt_long() isn't too convenient for the job. We copy our global
272 * options into a dynamic array, then append all of the command-specific
274 options
= xmemdup(global_long_options
, sizeof global_long_options
);
275 allocated_options
= ARRAY_SIZE(global_long_options
);
276 n_options
= n_global_long_options
;
277 ctl_add_cmd_options(&options
, &n_options
, &allocated_options
, OPT_LOCAL
);
283 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
298 vlog_set_levels(&this_module
, VLF_SYSLOG
, VLL_WARN
);
302 wait_for_reload
= false;
310 if (shash_find(local_options
, options
[idx
].name
)) {
311 ctl_fatal("'%s' option specified multiple times",
314 shash_add_nocopy(local_options
,
315 xasprintf("--%s", options
[idx
].name
),
316 nullable_xstrdup(optarg
));
323 ctl_print_commands();
327 ctl_print_options(global_long_options
);
331 ovs_print_version(0, 0);
332 printf("DB Schema %s\n", ovsrec_get_db_version());
336 if (!str_to_uint(optarg
, 10, &timeout
) || !timeout
) {
337 ctl_fatal("value %s on -t or --timeout is invalid", optarg
);
346 TABLE_OPTION_HANDLERS(&table_style
)
348 STREAM_SSL_OPTION_HANDLERS
350 case OPT_PEER_CA_CERT
:
351 stream_ssl_set_peer_ca_cert_file(optarg
);
354 case OPT_BOOTSTRAP_CA_CERT
:
355 stream_ssl_set_ca_cert_file(optarg
, true);
371 db
= ctl_default_db();
374 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
375 free(CONST_CAST(char *, options
[i
].name
));
384 %s: ovs-vswitchd management utility\n\
385 usage: %s [OPTIONS] COMMAND [ARG...]\n\
387 Open vSwitch commands:\n\
388 init initialize database, if not yet initialized\n\
389 show print overview of database contents\n\
390 emer-reset reset configuration to clean state\n\
393 add-br BRIDGE create a new bridge named BRIDGE\n\
394 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
395 del-br BRIDGE delete BRIDGE and all of its ports\n\
396 list-br print the names of all the bridges\n\
397 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
398 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
399 br-to-parent BRIDGE print the parent of BRIDGE\n\
400 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
401 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
402 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
403 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
405 Port commands (a bond is considered to be a single port):\n\
406 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
407 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
408 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
409 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
410 port-to-br PORT print name of bridge that contains PORT\n\
412 Interface commands (a bond consists of multiple interfaces):\n\
413 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
414 iface-to-br IFACE print name of bridge that contains IFACE\n\
416 Controller commands:\n\
417 get-controller BRIDGE print the controllers for BRIDGE\n\
418 del-controller BRIDGE delete the controllers for BRIDGE\n\
419 [--inactivity-probe=MSECS]\n\
420 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
421 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
422 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
423 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
426 get-manager print the managers\n\
427 del-manager delete the managers\n\
428 [--inactivity-probe=MSECS]\n\
429 set-manager TARGET... set the list of managers to TARGET...\n\
432 get-ssl print the SSL configuration\n\
433 del-ssl delete the SSL configuration\n\
434 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
436 Auto Attach commands:\n\
437 add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
438 del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
439 get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
442 emer-reset reset switch to known good state\n\
448 --db=DATABASE connect to DATABASE\n\
450 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
451 --retry keep trying to connect to server forever\n\
452 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
453 --dry-run do not commit changes to database\n\
454 --oneline print exactly one line of output per command\n",
455 program_name
, program_name
, ctl_get_db_cmd_usage(),
456 ctl_list_db_tables_usage(), ctl_default_db());
460 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
461 stream_usage("database", true, true, true);
464 -h, --help display this help message\n\
465 -V, --version display version information\n");
470 /* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
471 struct vsctl_context
{
472 struct ctl_context base
;
474 /* Modifiable state. */
475 const struct ovsrec_open_vswitch
*ovs
;
478 /* A cache of the contents of the database.
480 * A command that needs to use any of this information must first call
481 * vsctl_context_populate_cache(). A command that changes anything that
482 * could invalidate the cache must either call
483 * vsctl_context_invalidate_cache() or manually update the cache to
484 * maintain its correctness. */
486 struct shash bridges
; /* Maps from bridge name to struct vsctl_bridge. */
487 struct shash ports
; /* Maps from port name to struct vsctl_port. */
488 struct shash ifaces
; /* Maps from port name to struct vsctl_iface. */
491 struct vsctl_bridge
{
492 struct ovsrec_bridge
*br_cfg
;
494 struct ovs_list ports
; /* Contains "struct vsctl_port"s. */
496 /* VLAN ("fake") bridge support.
498 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
500 struct hmap children
; /* VLAN bridges indexed by 'vlan'. */
501 struct hmap_node children_node
; /* Node in parent's 'children' hmap. */
502 struct vsctl_bridge
*parent
; /* Real bridge, or NULL. */
503 int vlan
; /* VLAN VID (0...4095), or 0. */
507 struct ovs_list ports_node
; /* In struct vsctl_bridge's 'ports' list. */
508 struct ovs_list ifaces
; /* Contains "struct vsctl_iface"s. */
509 struct ovsrec_port
*port_cfg
;
510 struct vsctl_bridge
*bridge
;
514 struct ovs_list ifaces_node
; /* In struct vsctl_port's 'ifaces' list. */
515 struct ovsrec_interface
*iface_cfg
;
516 struct vsctl_port
*port
;
519 /* Casts 'base' into 'struct vsctl_context'. */
520 static struct vsctl_context
*
521 vsctl_context_cast(struct ctl_context
*base
)
523 return CONTAINER_OF(base
, struct vsctl_context
, base
);
526 static struct vsctl_bridge
*find_vlan_bridge(struct vsctl_bridge
*parent
,
530 vsctl_context_to_string(const struct ctl_context
*ctx
)
532 const struct shash_node
*node
;
538 SHASH_FOR_EACH (node
, &ctx
->options
) {
539 svec_add(&words
, node
->name
);
541 for (i
= 0; i
< ctx
->argc
; i
++) {
542 svec_add(&words
, ctx
->argv
[i
]);
544 svec_terminate(&words
);
546 s
= process_escape_args(words
.names
);
548 svec_destroy(&words
);
554 verify_ports(struct vsctl_context
*vsctl_ctx
)
556 if (!vsctl_ctx
->verified_ports
) {
557 const struct ovsrec_bridge
*bridge
;
558 const struct ovsrec_port
*port
;
560 ovsrec_open_vswitch_verify_bridges(vsctl_ctx
->ovs
);
561 OVSREC_BRIDGE_FOR_EACH (bridge
, vsctl_ctx
->base
.idl
) {
562 ovsrec_bridge_verify_ports(bridge
);
564 OVSREC_PORT_FOR_EACH (port
, vsctl_ctx
->base
.idl
) {
565 ovsrec_port_verify_interfaces(port
);
568 vsctl_ctx
->verified_ports
= true;
572 static struct vsctl_bridge
*
573 add_bridge_to_cache(struct vsctl_context
*vsctl_ctx
,
574 struct ovsrec_bridge
*br_cfg
, const char *name
,
575 struct vsctl_bridge
*parent
, int vlan
)
577 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
579 br
->name
= xstrdup(name
);
580 ovs_list_init(&br
->ports
);
583 hmap_init(&br
->children
);
585 struct vsctl_bridge
*conflict
= find_vlan_bridge(parent
, vlan
);
587 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
588 "for VLAN %d, but only one is allowed",
589 parent
->name
, name
, conflict
->name
, vlan
);
591 hmap_insert(&parent
->children
, &br
->children_node
,
595 shash_add(&vsctl_ctx
->bridges
, br
->name
, br
);
600 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
601 struct ovsrec_bridge
*bridge
)
603 struct ovsrec_bridge
**bridges
;
606 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
607 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
608 if (ovs
->bridges
[i
] != bridge
) {
609 bridges
[n
++] = ovs
->bridges
[i
];
612 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
617 del_cached_bridge(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*br
)
619 ovs_assert(ovs_list_is_empty(&br
->ports
));
620 ovs_assert(hmap_is_empty(&br
->children
));
622 hmap_remove(&br
->parent
->children
, &br
->children_node
);
625 ovsrec_bridge_delete(br
->br_cfg
);
626 ovs_delete_bridge(vsctl_ctx
->ovs
, br
->br_cfg
);
628 shash_find_and_delete(&vsctl_ctx
->bridges
, br
->name
);
629 hmap_destroy(&br
->children
);
635 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
637 return (port_cfg
->fake_bridge
639 && *port_cfg
->tag
>= 0 && *port_cfg
->tag
<= 4095);
642 static struct vsctl_bridge
*
643 find_vlan_bridge(struct vsctl_bridge
*parent
, int vlan
)
645 struct vsctl_bridge
*child
;
647 HMAP_FOR_EACH_IN_BUCKET (child
, children_node
, hash_int(vlan
, 0),
649 if (child
->vlan
== vlan
) {
657 static struct vsctl_port
*
658 add_port_to_cache(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*parent
,
659 struct ovsrec_port
*port_cfg
)
661 struct vsctl_port
*port
;
664 && *port_cfg
->tag
>= 0 && *port_cfg
->tag
<= 4095) {
665 struct vsctl_bridge
*vlan_bridge
;
667 vlan_bridge
= find_vlan_bridge(parent
, *port_cfg
->tag
);
669 parent
= vlan_bridge
;
673 port
= xmalloc(sizeof *port
);
674 ovs_list_push_back(&parent
->ports
, &port
->ports_node
);
675 ovs_list_init(&port
->ifaces
);
676 port
->port_cfg
= port_cfg
;
677 port
->bridge
= parent
;
678 shash_add(&vsctl_ctx
->ports
, port_cfg
->name
, port
);
684 del_cached_port(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*port
)
686 ovs_assert(ovs_list_is_empty(&port
->ifaces
));
687 ovs_list_remove(&port
->ports_node
);
688 shash_find_and_delete(&vsctl_ctx
->ports
, port
->port_cfg
->name
);
689 ovsrec_port_delete(port
->port_cfg
);
693 static struct vsctl_iface
*
694 add_iface_to_cache(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*parent
,
695 struct ovsrec_interface
*iface_cfg
)
697 struct vsctl_iface
*iface
;
699 iface
= xmalloc(sizeof *iface
);
700 ovs_list_push_back(&parent
->ifaces
, &iface
->ifaces_node
);
701 iface
->iface_cfg
= iface_cfg
;
702 iface
->port
= parent
;
703 shash_add(&vsctl_ctx
->ifaces
, iface_cfg
->name
, iface
);
709 del_cached_iface(struct vsctl_context
*vsctl_ctx
, struct vsctl_iface
*iface
)
711 ovs_list_remove(&iface
->ifaces_node
);
712 shash_find_and_delete(&vsctl_ctx
->ifaces
, iface
->iface_cfg
->name
);
713 ovsrec_interface_delete(iface
->iface_cfg
);
718 vsctl_context_invalidate_cache(struct ctl_context
*ctx
)
720 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
721 struct shash_node
*node
;
723 if (!vsctl_ctx
->cache_valid
) {
726 vsctl_ctx
->cache_valid
= false;
728 SHASH_FOR_EACH (node
, &vsctl_ctx
->bridges
) {
729 struct vsctl_bridge
*bridge
= node
->data
;
730 hmap_destroy(&bridge
->children
);
734 shash_destroy(&vsctl_ctx
->bridges
);
736 shash_destroy_free_data(&vsctl_ctx
->ports
);
737 shash_destroy_free_data(&vsctl_ctx
->ifaces
);
741 pre_get_info(struct ctl_context
*ctx
)
743 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_bridges
);
745 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_name
);
746 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
747 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
748 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ports
);
750 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_name
);
751 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_fake_bridge
);
752 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_tag
);
753 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_interfaces
);
755 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_name
);
757 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_ofport
);
758 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_error
);
762 vsctl_context_populate_cache(struct ctl_context
*ctx
)
764 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
765 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
766 struct sset bridges
, ports
;
769 if (vsctl_ctx
->cache_valid
) {
770 /* Cache is already populated. */
773 vsctl_ctx
->cache_valid
= true;
774 shash_init(&vsctl_ctx
->bridges
);
775 shash_init(&vsctl_ctx
->ports
);
776 shash_init(&vsctl_ctx
->ifaces
);
780 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
781 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
782 struct vsctl_bridge
*br
;
785 if (!sset_add(&bridges
, br_cfg
->name
)) {
786 VLOG_WARN("%s: database contains duplicate bridge name",
790 br
= add_bridge_to_cache(vsctl_ctx
, br_cfg
, br_cfg
->name
, NULL
, 0);
792 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
793 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
795 if (!sset_add(&ports
, port_cfg
->name
)) {
796 /* Duplicate port name. (We will warn about that later.) */
800 if (port_is_fake_bridge(port_cfg
)
801 && sset_add(&bridges
, port_cfg
->name
)) {
802 add_bridge_to_cache(vsctl_ctx
, NULL
, port_cfg
->name
, br
,
807 sset_destroy(&bridges
);
808 sset_destroy(&ports
);
811 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
812 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
813 struct vsctl_bridge
*br
;
816 if (!sset_add(&bridges
, br_cfg
->name
)) {
819 br
= shash_find_data(&vsctl_ctx
->bridges
, br_cfg
->name
);
820 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
821 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
822 struct vsctl_port
*port
;
825 port
= shash_find_data(&vsctl_ctx
->ports
, port_cfg
->name
);
827 if (port_cfg
== port
->port_cfg
) {
828 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
829 port_cfg
->name
, br
->name
, port
->bridge
->name
);
831 /* Log as an error because this violates the database's
832 * uniqueness constraints, so the database server shouldn't
833 * have allowed it. */
834 VLOG_ERR("%s: database contains duplicate port name",
840 if (port_is_fake_bridge(port_cfg
)
841 && !sset_add(&bridges
, port_cfg
->name
)) {
845 port
= add_port_to_cache(vsctl_ctx
, br
, port_cfg
);
846 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
847 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
848 struct vsctl_iface
*iface
;
850 iface
= shash_find_data(&vsctl_ctx
->ifaces
, iface_cfg
->name
);
852 if (iface_cfg
== iface
->iface_cfg
) {
853 VLOG_WARN("%s: interface is in multiple ports "
856 iface
->port
->port_cfg
->name
,
857 port
->port_cfg
->name
);
859 /* Log as an error because this violates the database's
860 * uniqueness constraints, so the database server
861 * shouldn't have allowed it. */
862 VLOG_ERR("%s: database contains duplicate interface "
863 "name", iface_cfg
->name
);
868 add_iface_to_cache(vsctl_ctx
, port
, iface_cfg
);
872 sset_destroy(&bridges
);
876 check_conflicts(struct vsctl_context
*vsctl_ctx
, const char *name
,
879 struct vsctl_iface
*iface
;
880 struct vsctl_port
*port
;
882 verify_ports(vsctl_ctx
);
884 if (shash_find(&vsctl_ctx
->bridges
, name
)) {
885 ctl_fatal("%s because a bridge named %s already exists",
889 port
= shash_find_data(&vsctl_ctx
->ports
, name
);
891 ctl_fatal("%s because a port named %s already exists on "
892 "bridge %s", msg
, name
, port
->bridge
->name
);
895 iface
= shash_find_data(&vsctl_ctx
->ifaces
, name
);
897 ctl_fatal("%s because an interface named %s already exists "
898 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
904 static struct vsctl_bridge
*
905 find_bridge(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
907 struct vsctl_bridge
*br
;
909 ovs_assert(vsctl_ctx
->cache_valid
);
911 br
= shash_find_data(&vsctl_ctx
->bridges
, name
);
912 if (must_exist
&& !br
) {
913 ctl_fatal("no bridge named %s", name
);
915 ovsrec_open_vswitch_verify_bridges(vsctl_ctx
->ovs
);
919 static struct vsctl_bridge
*
920 find_real_bridge(struct vsctl_context
*vsctl_ctx
,
921 const char *name
, bool must_exist
)
923 struct vsctl_bridge
*br
= find_bridge(vsctl_ctx
, name
, must_exist
);
924 if (br
&& br
->parent
) {
925 ctl_fatal("%s is a fake bridge", name
);
930 static struct vsctl_port
*
931 find_port(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
933 struct vsctl_port
*port
;
935 ovs_assert(vsctl_ctx
->cache_valid
);
937 port
= shash_find_data(&vsctl_ctx
->ports
, name
);
938 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
941 if (must_exist
&& !port
) {
942 ctl_fatal("no port named %s", name
);
944 verify_ports(vsctl_ctx
);
948 static struct vsctl_iface
*
949 find_iface(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
951 struct vsctl_iface
*iface
;
953 ovs_assert(vsctl_ctx
->cache_valid
);
955 iface
= shash_find_data(&vsctl_ctx
->ifaces
, name
);
956 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
959 if (must_exist
&& !iface
) {
960 ctl_fatal("no interface named %s", name
);
962 verify_ports(vsctl_ctx
);
967 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
969 struct ovsrec_port
**ports
;
972 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
973 for (i
= 0; i
< br
->n_ports
; i
++) {
974 ports
[i
] = br
->ports
[i
];
976 ports
[br
->n_ports
] = port
;
977 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
982 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
984 struct ovsrec_port
**ports
;
987 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
988 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
989 if (br
->ports
[i
] != port
) {
990 ports
[n
++] = br
->ports
[i
];
993 ovsrec_bridge_set_ports(br
, ports
, n
);
998 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
999 struct ovsrec_bridge
*bridge
)
1001 struct ovsrec_bridge
**bridges
;
1004 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
1005 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
1006 bridges
[i
] = ovs
->bridges
[i
];
1008 bridges
[ovs
->n_bridges
] = bridge
;
1009 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
1014 cmd_init(struct ctl_context
*ctx OVS_UNUSED
)
1018 static struct cmd_show_table cmd_show_tables
[] = {
1019 {&ovsrec_table_open_vswitch
,
1021 {&ovsrec_open_vswitch_col_manager_options
,
1022 &ovsrec_open_vswitch_col_bridges
,
1023 &ovsrec_open_vswitch_col_ovs_version
},
1027 {&ovsrec_table_bridge
,
1028 &ovsrec_bridge_col_name
,
1029 {&ovsrec_bridge_col_controller
,
1030 &ovsrec_bridge_col_fail_mode
,
1031 &ovsrec_bridge_col_datapath_type
,
1032 &ovsrec_bridge_col_ports
},
1036 {&ovsrec_table_port
,
1037 &ovsrec_port_col_name
,
1038 {&ovsrec_port_col_tag
,
1039 &ovsrec_port_col_trunks
,
1040 &ovsrec_port_col_interfaces
},
1044 {&ovsrec_table_interface
,
1045 &ovsrec_interface_col_name
,
1046 {&ovsrec_interface_col_type
,
1047 &ovsrec_interface_col_options
,
1048 &ovsrec_interface_col_error
,
1049 &ovsrec_interface_col_bfd_status
},
1053 {&ovsrec_table_controller
,
1054 &ovsrec_controller_col_target
,
1055 {&ovsrec_controller_col_is_connected
,
1061 {&ovsrec_table_manager
,
1062 &ovsrec_manager_col_target
,
1063 {&ovsrec_manager_col_is_connected
,
1069 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
1073 pre_cmd_emer_reset(struct ctl_context
*ctx
)
1075 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1076 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1078 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
1079 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
1080 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_mirrors
);
1081 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_netflow
);
1082 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_sflow
);
1083 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ipfix
);
1084 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_flood_vlans
);
1085 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_other_config
);
1087 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_other_config
);
1089 ovsdb_idl_add_column(ctx
->idl
,
1090 &ovsrec_interface_col_ingress_policing_rate
);
1091 ovsdb_idl_add_column(ctx
->idl
,
1092 &ovsrec_interface_col_ingress_policing_burst
);
1096 cmd_emer_reset(struct ctl_context
*ctx
)
1098 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1099 const struct ovsdb_idl
*idl
= ctx
->idl
;
1100 const struct ovsrec_bridge
*br
;
1101 const struct ovsrec_port
*port
;
1102 const struct ovsrec_interface
*iface
;
1103 const struct ovsrec_mirror
*mirror
, *next_mirror
;
1104 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
1105 const struct ovsrec_manager
*mgr
, *next_mgr
;
1106 const struct ovsrec_netflow
*nf
, *next_nf
;
1107 const struct ovsrec_ssl
*ssl
, *next_ssl
;
1108 const struct ovsrec_sflow
*sflow
, *next_sflow
;
1109 const struct ovsrec_ipfix
*ipfix
, *next_ipfix
;
1110 const struct ovsrec_flow_sample_collector_set
*fscset
, *next_fscset
;
1112 /* Reset the Open_vSwitch table. */
1113 ovsrec_open_vswitch_set_manager_options(vsctl_ctx
->ovs
, NULL
, 0);
1114 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, NULL
);
1116 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
1119 ovsrec_bridge_set_controller(br
, NULL
, 0);
1120 ovsrec_bridge_set_fail_mode(br
, NULL
);
1121 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
1122 ovsrec_bridge_set_netflow(br
, NULL
);
1123 ovsrec_bridge_set_sflow(br
, NULL
);
1124 ovsrec_bridge_set_ipfix(br
, NULL
);
1125 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
1127 /* We only want to save the "hwaddr" key from other_config. */
1128 hwaddr
= smap_get(&br
->other_config
, "hwaddr");
1130 const struct smap smap
= SMAP_CONST1(&smap
, "hwaddr", hwaddr
);
1131 ovsrec_bridge_set_other_config(br
, &smap
);
1133 ovsrec_bridge_set_other_config(br
, NULL
);
1137 OVSREC_PORT_FOR_EACH (port
, idl
) {
1138 ovsrec_port_set_other_config(port
, NULL
);
1141 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
1142 /* xxx What do we do about gre/patch devices created by mgr? */
1144 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
1145 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
1148 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
1149 ovsrec_mirror_delete(mirror
);
1152 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
1153 ovsrec_controller_delete(ctrl
);
1156 OVSREC_MANAGER_FOR_EACH_SAFE (mgr
, next_mgr
, idl
) {
1157 ovsrec_manager_delete(mgr
);
1160 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
1161 ovsrec_netflow_delete(nf
);
1164 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
1165 ovsrec_ssl_delete(ssl
);
1168 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
1169 ovsrec_sflow_delete(sflow
);
1172 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix
, next_ipfix
, idl
) {
1173 ovsrec_ipfix_delete(ipfix
);
1176 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset
, next_fscset
, idl
) {
1177 ovsrec_flow_sample_collector_set_delete(fscset
);
1180 vsctl_context_invalidate_cache(ctx
);
1183 static struct ovsrec_datapath
*
1184 find_datapath(struct vsctl_context
*vsctl_ctx
, const char *dp_name
)
1186 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
1188 for (int i
= 0; i
< ovs
->n_datapaths
; i
++) {
1189 if (!strcmp(ovs
->key_datapaths
[i
], dp_name
)) {
1190 return ovs
->value_datapaths
[i
];
1196 static struct ovsrec_ct_zone
*
1197 find_ct_zone(struct ovsrec_datapath
*dp
, const int64_t zone_id
)
1199 for (int i
= 0; i
< dp
->n_ct_zones
; i
++) {
1200 if (dp
->key_ct_zones
[i
] == zone_id
) {
1201 return dp
->value_ct_zones
[i
];
1207 static struct ovsrec_ct_timeout_policy
*
1208 create_timeout_policy(struct ctl_context
*ctx
, char **tps
, int n_tps
)
1210 const struct ovsrec_ct_timeout_policy_table
*tp_table
;
1211 const struct ovsrec_ct_timeout_policy
*row
;
1212 struct ovsrec_ct_timeout_policy
*tp
= NULL
;
1213 struct simap new_tp
= SIMAP_INITIALIZER(&new_tp
);
1215 char **policies
= xzalloc(sizeof *policies
* n_tps
);
1216 const char **key_timeouts
= xmalloc(sizeof *key_timeouts
* n_tps
);
1217 int64_t *value_timeouts
= xmalloc(sizeof *value_timeouts
* n_tps
);
1219 /* Parse timeout arguments. */
1220 for (int i
= 0; i
< n_tps
; i
++) {
1221 policies
[i
] = xstrdup(tps
[i
]);
1224 char *policy
= policies
[i
];
1225 if (!ofputil_parse_key_value(&policy
, &key
, &value
)) {
1228 key_timeouts
[i
] = key
;
1229 value_timeouts
[i
] = atoi(value
);
1230 simap_put(&new_tp
, key
, (unsigned int)value_timeouts
[i
]);
1234 tp_table
= ovsrec_ct_timeout_policy_table_get(ctx
->idl
);
1235 OVSREC_CT_TIMEOUT_POLICY_TABLE_FOR_EACH (row
, tp_table
) {
1236 struct simap s
= SIMAP_INITIALIZER(&s
);
1238 /* Convert to simap. */
1239 for (int i
= 0; i
< row
->n_timeouts
; i
++) {
1240 simap_put(&s
, row
->key_timeouts
[i
], row
->value_timeouts
[i
]);
1243 if (simap_equal(&s
, &new_tp
)) {
1244 tp
= CONST_CAST(struct ovsrec_ct_timeout_policy
*, row
);
1252 tp
= ovsrec_ct_timeout_policy_insert(ctx
->txn
);
1253 ovsrec_ct_timeout_policy_set_timeouts(tp
, key_timeouts
,
1254 (const int64_t *)value_timeouts
,
1258 for (int i
= 0; i
< n_tps
; i
++) {
1262 simap_destroy(&new_tp
);
1264 free(value_timeouts
);
1269 cmd_add_zone_tp(struct ctl_context
*ctx
)
1271 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1272 struct ovsrec_ct_timeout_policy
*tp
;
1275 const char *dp_name
= ctx
->argv
[1];
1276 ovs_scan(ctx
->argv
[2], "zone=%"SCNi64
, &zone_id
);
1277 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1279 struct ovsrec_datapath
*dp
= find_datapath(vsctl_ctx
, dp_name
);
1281 ctl_fatal("datapath %s does not exist", dp_name
);
1284 int n_tps
= ctx
->argc
- 3;
1285 struct ovsrec_ct_zone
*zone
= find_ct_zone(dp
, zone_id
);
1288 ctl_fatal("No timeout policy");
1291 if (zone
&& !may_exist
) {
1292 ctl_fatal("zone id %"PRIu64
" already exists", zone_id
);
1295 tp
= create_timeout_policy(ctx
, &ctx
->argv
[3], n_tps
);
1297 ovsrec_ct_zone_set_timeout_policy(zone
, tp
);
1299 zone
= ovsrec_ct_zone_insert(ctx
->txn
);
1300 ovsrec_ct_zone_set_timeout_policy(zone
, tp
);
1301 ovsrec_datapath_update_ct_zones_setkey(dp
, zone_id
, zone
);
1306 cmd_del_zone_tp(struct ctl_context
*ctx
)
1308 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1311 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1312 const char *dp_name
= ctx
->argv
[1];
1313 ovs_scan(ctx
->argv
[2], "zone=%"SCNi64
, &zone_id
);
1315 struct ovsrec_datapath
*dp
= find_datapath(vsctl_ctx
, dp_name
);
1317 ctl_fatal("datapath %s does not exist", dp_name
);
1320 struct ovsrec_ct_zone
*zone
= find_ct_zone(dp
, zone_id
);
1321 if (must_exist
&& !zone
) {
1322 ctl_fatal("zone id %"PRIu64
" does not exist", zone_id
);
1326 ovsrec_datapath_update_ct_zones_delkey(dp
, zone_id
);
1331 cmd_list_zone_tp(struct ctl_context
*ctx
)
1333 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1335 struct ovsrec_datapath
*dp
= find_datapath(vsctl_ctx
, ctx
->argv
[1]);
1337 ctl_fatal("datapath: %s record not found", ctx
->argv
[1]);
1340 for (int i
= 0; i
< dp
->n_ct_zones
; i
++) {
1341 struct ovsrec_ct_zone
*zone
= dp
->value_ct_zones
[i
];
1342 ds_put_format(&ctx
->output
, "Zone:%"PRIu64
", Timeout Policies: ",
1343 dp
->key_ct_zones
[i
]);
1345 struct ovsrec_ct_timeout_policy
*tp
= zone
->timeout_policy
;
1347 for (int j
= 0; j
< tp
->n_timeouts
; j
++) {
1348 ds_put_format(&ctx
->output
, "%s=%"PRIu64
" ",
1349 tp
->key_timeouts
[j
], tp
->value_timeouts
[j
]);
1351 ds_chomp(&ctx
->output
, ' ');
1352 ds_put_char(&ctx
->output
, '\n');
1357 pre_get_zone(struct ctl_context
*ctx
)
1359 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_datapaths
);
1360 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_datapath_col_ct_zones
);
1361 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ct_zone_col_timeout_policy
);
1362 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ct_timeout_policy_col_timeouts
);
1366 cmd_add_br(struct ctl_context
*ctx
)
1368 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1369 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1370 const char *br_name
, *parent_name
;
1371 struct ovsrec_interface
*iface
;
1374 br_name
= ctx
->argv
[1];
1376 ctl_fatal("bridge name must not be empty string");
1378 if (ctx
->argc
== 2) {
1381 } else if (ctx
->argc
== 4) {
1382 parent_name
= ctx
->argv
[2];
1383 vlan
= atoi(ctx
->argv
[3]);
1384 if (vlan
< 0 || vlan
> 4095) {
1385 ctl_fatal("%s: vlan must be between 0 and 4095", ctx
->argv
[0]);
1388 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1392 vsctl_context_populate_cache(ctx
);
1394 struct vsctl_bridge
*br
;
1396 br
= find_bridge(vsctl_ctx
, br_name
, false);
1400 ctl_fatal("\"--may-exist add-br %s\" but %s is "
1401 "a VLAN bridge for VLAN %d",
1402 br_name
, br_name
, br
->vlan
);
1406 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1407 "is not a VLAN bridge",
1408 br_name
, parent_name
, vlan
, br_name
);
1409 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1410 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1411 "has the wrong parent %s",
1412 br_name
, parent_name
, vlan
,
1413 br_name
, br
->parent
->name
);
1414 } else if (br
->vlan
!= vlan
) {
1415 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1416 "is a VLAN bridge for the wrong VLAN %d",
1417 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1423 check_conflicts(vsctl_ctx
, br_name
,
1424 xasprintf("cannot create a bridge named %s", br_name
));
1427 struct ovsrec_port
*port
;
1428 struct ovsrec_bridge
*br
;
1430 iface
= ovsrec_interface_insert(ctx
->txn
);
1431 ovsrec_interface_set_name(iface
, br_name
);
1432 ovsrec_interface_set_type(iface
, "internal");
1434 port
= ovsrec_port_insert(ctx
->txn
);
1435 ovsrec_port_set_name(port
, br_name
);
1436 ovsrec_port_set_interfaces(port
, &iface
, 1);
1438 br
= ovsrec_bridge_insert(ctx
->txn
);
1439 ovsrec_bridge_set_name(br
, br_name
);
1440 ovsrec_bridge_set_ports(br
, &port
, 1);
1442 ovs_insert_bridge(vsctl_ctx
->ovs
, br
);
1444 struct vsctl_bridge
*conflict
;
1445 struct vsctl_bridge
*parent
;
1446 struct ovsrec_port
*port
;
1447 struct ovsrec_bridge
*br
;
1450 parent
= find_bridge(vsctl_ctx
, parent_name
, false);
1451 if (parent
&& parent
->parent
) {
1452 ctl_fatal("cannot create bridge with fake bridge as parent");
1455 ctl_fatal("parent bridge %s does not exist", parent_name
);
1457 conflict
= find_vlan_bridge(parent
, vlan
);
1459 ctl_fatal("bridge %s already has a child VLAN bridge %s "
1460 "on VLAN %d", parent_name
, conflict
->name
, vlan
);
1462 br
= parent
->br_cfg
;
1464 iface
= ovsrec_interface_insert(ctx
->txn
);
1465 ovsrec_interface_set_name(iface
, br_name
);
1466 ovsrec_interface_set_type(iface
, "internal");
1468 port
= ovsrec_port_insert(ctx
->txn
);
1469 ovsrec_port_set_name(port
, br_name
);
1470 ovsrec_port_set_interfaces(port
, &iface
, 1);
1471 ovsrec_port_set_fake_bridge(port
, true);
1472 ovsrec_port_set_tag(port
, &tag
, 1);
1474 bridge_insert_port(br
, port
);
1477 post_db_reload_expect_iface(iface
);
1478 vsctl_context_invalidate_cache(ctx
);
1482 del_port(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*port
)
1484 struct vsctl_iface
*iface
, *next_iface
;
1486 bridge_delete_port((port
->bridge
->parent
1487 ? port
->bridge
->parent
->br_cfg
1488 : port
->bridge
->br_cfg
), port
->port_cfg
);
1490 LIST_FOR_EACH_SAFE (iface
, next_iface
, ifaces_node
, &port
->ifaces
) {
1491 del_cached_iface(vsctl_ctx
, iface
);
1493 del_cached_port(vsctl_ctx
, port
);
1497 del_bridge(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*br
)
1499 struct vsctl_bridge
*child
, *next_child
;
1500 struct vsctl_port
*port
, *next_port
;
1501 const struct ovsrec_flow_sample_collector_set
*fscset
, *next_fscset
;
1503 HMAP_FOR_EACH_SAFE (child
, next_child
, children_node
, &br
->children
) {
1504 del_bridge(vsctl_ctx
, child
);
1507 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &br
->ports
) {
1508 del_port(vsctl_ctx
, port
);
1511 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset
, next_fscset
,
1512 vsctl_ctx
->base
.idl
) {
1513 if (fscset
->bridge
== br
->br_cfg
) {
1514 ovsrec_flow_sample_collector_set_delete(fscset
);
1518 del_cached_bridge(vsctl_ctx
, br
);
1522 cmd_del_br(struct ctl_context
*ctx
)
1524 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1525 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1526 struct vsctl_bridge
*bridge
;
1528 vsctl_context_populate_cache(ctx
);
1529 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], must_exist
);
1531 del_bridge(vsctl_ctx
, bridge
);
1536 output_sorted(struct svec
*svec
, struct ds
*output
)
1542 SVEC_FOR_EACH (i
, name
, svec
) {
1543 ds_put_format(output
, "%s\n", name
);
1548 cmd_list_br(struct ctl_context
*ctx
)
1550 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1551 struct shash_node
*node
;
1552 struct svec bridges
;
1553 bool real
= shash_find(&ctx
->options
, "--real");
1554 bool fake
= shash_find(&ctx
->options
, "--fake");
1556 /* If neither fake nor real were requested, return both. */
1557 if (!real
&& !fake
) {
1561 vsctl_context_populate_cache(ctx
);
1563 svec_init(&bridges
);
1564 SHASH_FOR_EACH (node
, &vsctl_ctx
->bridges
) {
1565 struct vsctl_bridge
*br
= node
->data
;
1567 if (br
->parent
? fake
: real
) {
1568 svec_add(&bridges
, br
->name
);
1571 output_sorted(&bridges
, &ctx
->output
);
1572 svec_destroy(&bridges
);
1576 cmd_br_exists(struct ctl_context
*ctx
)
1578 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1580 vsctl_context_populate_cache(ctx
);
1581 if (!find_bridge(vsctl_ctx
, ctx
->argv
[1], false)) {
1587 set_external_id(struct smap
*old
, struct smap
*new,
1588 char *key
, char *value
)
1590 smap_clone(new, old
);
1593 smap_replace(new, key
, value
);
1595 smap_remove(new, key
);
1600 pre_cmd_br_set_external_id(struct ctl_context
*ctx
)
1603 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_external_ids
);
1604 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_external_ids
);
1608 cmd_br_set_external_id(struct ctl_context
*ctx
)
1610 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1611 struct vsctl_bridge
*bridge
;
1614 vsctl_context_populate_cache(ctx
);
1615 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1616 if (bridge
->br_cfg
) {
1618 set_external_id(&bridge
->br_cfg
->external_ids
, &new, ctx
->argv
[2],
1619 ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
);
1620 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1621 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, &new);
1623 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1624 struct vsctl_port
*port
= shash_find_data(&vsctl_ctx
->ports
,
1626 set_external_id(&port
->port_cfg
->external_ids
, &new,
1627 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
);
1628 ovsrec_port_verify_external_ids(port
->port_cfg
);
1629 ovsrec_port_set_external_ids(port
->port_cfg
, &new);
1636 get_external_id(struct smap
*smap
, const char *prefix
, const char *key
,
1640 char *prefix_key
= xasprintf("%s%s", prefix
, key
);
1641 const char *value
= smap_get(smap
, prefix_key
);
1644 ds_put_format(output
, "%s\n", value
);
1648 const struct smap_node
**sorted
= smap_sort(smap
);
1649 size_t prefix_len
= strlen(prefix
);
1652 for (i
= 0; i
< smap_count(smap
); i
++) {
1653 const struct smap_node
*node
= sorted
[i
];
1654 if (!strncmp(node
->key
, prefix
, prefix_len
)) {
1655 ds_put_format(output
, "%s=%s\n", node
->key
+ prefix_len
,
1664 pre_cmd_br_get_external_id(struct ctl_context
*ctx
)
1666 pre_cmd_br_set_external_id(ctx
);
1670 cmd_br_get_external_id(struct ctl_context
*ctx
)
1672 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1673 struct vsctl_bridge
*bridge
;
1675 vsctl_context_populate_cache(ctx
);
1677 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1678 if (bridge
->br_cfg
) {
1679 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1680 get_external_id(&bridge
->br_cfg
->external_ids
, "",
1681 ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1683 struct vsctl_port
*port
= shash_find_data(&vsctl_ctx
->ports
,
1685 ovsrec_port_verify_external_ids(port
->port_cfg
);
1686 get_external_id(&port
->port_cfg
->external_ids
, "fake-bridge-",
1687 ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1692 cmd_list_ports(struct ctl_context
*ctx
)
1694 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1695 struct vsctl_bridge
*br
;
1696 struct vsctl_port
*port
;
1699 vsctl_context_populate_cache(ctx
);
1700 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1701 ovsrec_bridge_verify_ports(br
->br_cfg
? br
->br_cfg
: br
->parent
->br_cfg
);
1704 LIST_FOR_EACH (port
, ports_node
, &br
->ports
) {
1705 if (strcmp(port
->port_cfg
->name
, br
->name
)) {
1706 svec_add(&ports
, port
->port_cfg
->name
);
1709 output_sorted(&ports
, &ctx
->output
);
1710 svec_destroy(&ports
);
1714 add_port(struct ctl_context
*ctx
,
1715 const char *br_name
, const char *port_name
,
1716 bool may_exist
, bool fake_iface
,
1717 char *iface_names
[], int n_ifaces
,
1718 char *settings
[], int n_settings
)
1720 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1721 struct vsctl_bridge
*bridge
;
1722 struct ovsrec_interface
**ifaces
;
1723 struct ovsrec_port
*port
;
1726 if (!port_name
[0]) {
1727 ctl_fatal("port name must not be empty string");
1729 for (i
= 0; i
< n_ifaces
; i
++) {
1730 if (!iface_names
[i
][0]) {
1731 ctl_fatal("interface name must not be empty string");
1735 vsctl_context_populate_cache(ctx
);
1737 struct vsctl_port
*vsctl_port
;
1739 vsctl_port
= find_port(vsctl_ctx
, port_name
, false);
1741 struct svec want_names
, have_names
;
1743 svec_init(&want_names
);
1744 for (i
= 0; i
< n_ifaces
; i
++) {
1745 svec_add(&want_names
, iface_names
[i
]);
1747 svec_sort(&want_names
);
1749 svec_init(&have_names
);
1750 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1751 svec_add(&have_names
,
1752 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1754 svec_sort(&have_names
);
1756 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1757 char *command
= vsctl_context_to_string(ctx
);
1758 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1759 command
, port_name
, vsctl_port
->bridge
->name
);
1762 if (!svec_equal(&want_names
, &have_names
)) {
1763 char *have_names_string
= svec_join(&have_names
, ", ", "");
1764 char *command
= vsctl_context_to_string(ctx
);
1766 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1767 command
, port_name
, have_names_string
);
1770 svec_destroy(&want_names
);
1771 svec_destroy(&have_names
);
1776 check_conflicts(vsctl_ctx
, port_name
,
1777 xasprintf("cannot create a port named %s", port_name
));
1778 for (i
= 0; i
< n_ifaces
; i
++) {
1779 check_conflicts(vsctl_ctx
, iface_names
[i
],
1780 xasprintf("cannot create an interface named %s",
1783 bridge
= find_bridge(vsctl_ctx
, br_name
, true);
1785 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1786 for (i
= 0; i
< n_ifaces
; i
++) {
1787 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1788 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1789 post_db_reload_expect_iface(ifaces
[i
]);
1792 port
= ovsrec_port_insert(ctx
->txn
);
1793 ovsrec_port_set_name(port
, port_name
);
1794 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1795 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1797 if (bridge
->parent
) {
1798 int64_t tag
= bridge
->vlan
;
1799 ovsrec_port_set_tag(port
, &tag
, 1);
1802 for (i
= 0; i
< n_settings
; i
++) {
1803 char *error
= ctl_set_column("Port", &port
->header_
, settings
[i
],
1806 ctl_fatal("%s", error
);
1810 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1811 : bridge
->br_cfg
), port
);
1813 struct vsctl_port
*vsctl_port
= add_port_to_cache(vsctl_ctx
, bridge
, port
);
1814 for (i
= 0; i
< n_ifaces
; i
++) {
1815 add_iface_to_cache(vsctl_ctx
, vsctl_port
, ifaces
[i
]);
1821 cmd_add_port(struct ctl_context
*ctx
)
1823 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1825 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1826 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1830 cmd_add_bond(struct ctl_context
*ctx
)
1832 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1833 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1837 n_ifaces
= ctx
->argc
- 3;
1838 for (i
= 3; i
< ctx
->argc
; i
++) {
1839 if (strchr(ctx
->argv
[i
], '=')) {
1845 ctl_fatal("add-bond requires at least 2 interfaces, but only "
1846 "%d were specified", n_ifaces
);
1849 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1850 &ctx
->argv
[3], n_ifaces
,
1851 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1855 cmd_add_bond_iface(struct ctl_context
*ctx
)
1857 vsctl_context_populate_cache(ctx
);
1859 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1860 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1861 struct vsctl_port
*port
= find_port(vsctl_ctx
, ctx
->argv
[1], true);
1863 const char *iface_name
= ctx
->argv
[2];
1865 struct vsctl_iface
*iface
= find_iface(vsctl_ctx
, iface_name
, false);
1867 if (iface
->port
== port
) {
1870 char *command
= vsctl_context_to_string(ctx
);
1871 ctl_fatal("\"%s\" but %s is actually attached to port %s",
1872 command
, iface_name
, iface
->port
->port_cfg
->name
);
1875 check_conflicts(vsctl_ctx
, iface_name
,
1876 xasprintf("cannot create an interface named %s",
1879 struct ovsrec_interface
*iface
= ovsrec_interface_insert(ctx
->txn
);
1880 ovsrec_interface_set_name(iface
, iface_name
);
1881 ovsrec_port_update_interfaces_addvalue(port
->port_cfg
, iface
);
1882 post_db_reload_expect_iface(iface
);
1883 add_iface_to_cache(vsctl_ctx
, port
, iface
);
1887 cmd_del_bond_iface(struct ctl_context
*ctx
)
1889 vsctl_context_populate_cache(ctx
);
1891 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1892 const char *iface_name
= ctx
->argv
[ctx
->argc
- 1];
1893 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1894 struct vsctl_iface
*iface
= find_iface(vsctl_ctx
, iface_name
, must_exist
);
1896 ovs_assert(!must_exist
);
1900 const char *port_name
= ctx
->argc
> 2 ? ctx
->argv
[1] : NULL
;
1902 struct vsctl_port
*port
= find_port(vsctl_ctx
, port_name
, true);
1903 if (iface
->port
!= port
) {
1904 ctl_fatal("port %s does not have an interface %s",
1905 port_name
, iface_name
);
1909 if (ovs_list_is_short(&iface
->port
->ifaces
)) {
1910 ctl_fatal("cannot delete last interface from port %s",
1911 iface
->port
->port_cfg
->name
);
1914 ovsrec_port_update_interfaces_delvalue(iface
->port
->port_cfg
,
1916 del_cached_iface(vsctl_ctx
, iface
);
1920 cmd_del_port(struct ctl_context
*ctx
)
1922 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1923 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1924 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1925 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1926 struct vsctl_port
*port
;
1928 vsctl_context_populate_cache(ctx
);
1929 if (find_bridge(vsctl_ctx
, target
, false)) {
1931 ctl_fatal("cannot delete port %s because it is the local port "
1932 "for bridge %s (deleting this port requires deleting "
1933 "the entire bridge)", target
, target
);
1936 } else if (!with_iface
) {
1937 port
= find_port(vsctl_ctx
, target
, must_exist
);
1939 struct vsctl_iface
*iface
;
1941 port
= find_port(vsctl_ctx
, target
, false);
1943 iface
= find_iface(vsctl_ctx
, target
, false);
1948 if (must_exist
&& !port
) {
1949 ctl_fatal("no port or interface named %s", target
);
1954 if (ctx
->argc
== 3) {
1955 struct vsctl_bridge
*bridge
;
1957 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1958 if (port
->bridge
!= bridge
) {
1959 if (port
->bridge
->parent
== bridge
) {
1960 ctl_fatal("bridge %s does not have a port %s (although "
1961 "its child bridge %s does)",
1962 ctx
->argv
[1], ctx
->argv
[2],
1963 port
->bridge
->name
);
1965 ctl_fatal("bridge %s does not have a port %s",
1966 ctx
->argv
[1], ctx
->argv
[2]);
1971 del_port(vsctl_ctx
, port
);
1976 cmd_port_to_br(struct ctl_context
*ctx
)
1978 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1979 struct vsctl_port
*port
;
1981 vsctl_context_populate_cache(ctx
);
1983 port
= find_port(vsctl_ctx
, ctx
->argv
[1], true);
1984 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1988 cmd_br_to_vlan(struct ctl_context
*ctx
)
1990 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1991 struct vsctl_bridge
*bridge
;
1993 vsctl_context_populate_cache(ctx
);
1995 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1996 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
2000 cmd_br_to_parent(struct ctl_context
*ctx
)
2002 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2003 struct vsctl_bridge
*bridge
;
2005 vsctl_context_populate_cache(ctx
);
2007 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2008 if (bridge
->parent
) {
2009 bridge
= bridge
->parent
;
2011 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
2015 cmd_list_ifaces(struct ctl_context
*ctx
)
2017 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2018 struct vsctl_bridge
*br
;
2019 struct vsctl_port
*port
;
2022 vsctl_context_populate_cache(ctx
);
2024 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2025 verify_ports(vsctl_ctx
);
2028 LIST_FOR_EACH (port
, ports_node
, &br
->ports
) {
2029 struct vsctl_iface
*iface
;
2031 LIST_FOR_EACH (iface
, ifaces_node
, &port
->ifaces
) {
2032 if (strcmp(iface
->iface_cfg
->name
, br
->name
)) {
2033 svec_add(&ifaces
, iface
->iface_cfg
->name
);
2037 output_sorted(&ifaces
, &ctx
->output
);
2038 svec_destroy(&ifaces
);
2042 cmd_iface_to_br(struct ctl_context
*ctx
)
2044 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2045 struct vsctl_iface
*iface
;
2047 vsctl_context_populate_cache(ctx
);
2049 iface
= find_iface(vsctl_ctx
, ctx
->argv
[1], true);
2050 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
2054 verify_controllers(struct ovsrec_bridge
*bridge
)
2058 ovsrec_bridge_verify_controller(bridge
);
2059 for (i
= 0; i
< bridge
->n_controller
; i
++) {
2060 ovsrec_controller_verify_target(bridge
->controller
[i
]);
2065 pre_controller(struct ctl_context
*ctx
)
2069 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_controller_col_target
);
2070 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_controller_col_inactivity_probe
);
2074 cmd_get_controller(struct ctl_context
*ctx
)
2076 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2077 struct vsctl_bridge
*br
;
2078 struct svec targets
;
2081 vsctl_context_populate_cache(ctx
);
2083 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2087 verify_controllers(br
->br_cfg
);
2089 /* Print the targets in sorted order for reproducibility. */
2090 svec_init(&targets
);
2091 for (i
= 0; i
< br
->br_cfg
->n_controller
; i
++) {
2092 svec_add(&targets
, br
->br_cfg
->controller
[i
]->target
);
2095 svec_sort(&targets
);
2096 for (i
= 0; i
< targets
.n
; i
++) {
2097 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2099 svec_destroy(&targets
);
2103 delete_controllers(struct ovsrec_controller
**controllers
,
2104 size_t n_controllers
)
2108 for (i
= 0; i
< n_controllers
; i
++) {
2109 ovsrec_controller_delete(controllers
[i
]);
2114 cmd_del_controller(struct ctl_context
*ctx
)
2116 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2117 struct ovsrec_bridge
*br
;
2119 vsctl_context_populate_cache(ctx
);
2121 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true)->br_cfg
;
2122 verify_controllers(br
);
2124 if (br
->controller
) {
2125 delete_controllers(br
->controller
, br
->n_controller
);
2126 ovsrec_bridge_set_controller(br
, NULL
, 0);
2130 static struct ovsrec_controller
**
2131 insert_controllers(struct ctl_context
*ctx
, char *targets
[], size_t n
)
2133 struct ovsrec_controller
**controllers
;
2135 const char *inactivity_probe
= shash_find_data(&ctx
->options
,
2136 "--inactivity-probe");
2138 controllers
= xmalloc(n
* sizeof *controllers
);
2139 for (i
= 0; i
< n
; i
++) {
2140 if (vconn_verify_name(targets
[i
]) && pvconn_verify_name(targets
[i
])) {
2141 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2143 controllers
[i
] = ovsrec_controller_insert(ctx
->txn
);
2144 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
2145 if (inactivity_probe
) {
2146 int64_t msecs
= atoll(inactivity_probe
);
2147 ovsrec_controller_set_inactivity_probe(controllers
[i
], &msecs
, 1);
2155 cmd_set_controller(struct ctl_context
*ctx
)
2157 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2158 struct ovsrec_controller
**controllers
;
2159 struct ovsrec_bridge
*br
;
2162 vsctl_context_populate_cache(ctx
);
2164 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true)->br_cfg
;
2165 verify_controllers(br
);
2167 delete_controllers(br
->controller
, br
->n_controller
);
2170 controllers
= insert_controllers(ctx
, &ctx
->argv
[2], n
);
2171 ovsrec_bridge_set_controller(br
, controllers
, n
);
2176 cmd_get_fail_mode(struct ctl_context
*ctx
)
2178 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2179 struct vsctl_bridge
*br
;
2180 const char *fail_mode
;
2182 vsctl_context_populate_cache(ctx
);
2183 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2188 ovsrec_bridge_verify_fail_mode(br
->br_cfg
);
2190 fail_mode
= br
->br_cfg
->fail_mode
;
2191 if (fail_mode
&& strlen(fail_mode
)) {
2192 ds_put_format(&ctx
->output
, "%s\n", fail_mode
);
2197 cmd_del_fail_mode(struct ctl_context
*ctx
)
2199 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2200 struct vsctl_bridge
*br
;
2202 vsctl_context_populate_cache(ctx
);
2204 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2206 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
2210 cmd_set_fail_mode(struct ctl_context
*ctx
)
2212 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2213 struct vsctl_bridge
*br
;
2214 const char *fail_mode
= ctx
->argv
[2];
2216 vsctl_context_populate_cache(ctx
);
2218 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2220 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
2221 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2224 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
2228 verify_managers(const struct ovsrec_open_vswitch
*ovs
)
2232 ovsrec_open_vswitch_verify_manager_options(ovs
);
2234 for (i
= 0; i
< ovs
->n_manager_options
; ++i
) {
2235 const struct ovsrec_manager
*mgr
= ovs
->manager_options
[i
];
2237 ovsrec_manager_verify_target(mgr
);
2242 pre_manager(struct ctl_context
*ctx
)
2244 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
2245 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_manager_col_target
);
2246 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_manager_col_inactivity_probe
);
2250 cmd_get_manager(struct ctl_context
*ctx
)
2252 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2253 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
2254 struct svec targets
;
2257 verify_managers(ovs
);
2259 /* Print the targets in sorted order for reproducibility. */
2260 svec_init(&targets
);
2262 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
2263 svec_add(&targets
, ovs
->manager_options
[i
]->target
);
2266 svec_sort_unique(&targets
);
2267 for (i
= 0; i
< targets
.n
; i
++) {
2268 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2270 svec_destroy(&targets
);
2274 delete_managers(const struct ovsrec_open_vswitch
*ovs
)
2278 /* Delete Manager rows pointed to by 'manager_options' column. */
2279 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
2280 ovsrec_manager_delete(ovs
->manager_options
[i
]);
2283 /* Delete 'Manager' row refs in 'manager_options' column. */
2284 ovsrec_open_vswitch_set_manager_options(ovs
, NULL
, 0);
2288 cmd_del_manager(struct ctl_context
*ctx
)
2290 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2291 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
2293 verify_managers(ovs
);
2294 delete_managers(ovs
);
2298 insert_managers(struct vsctl_context
*vsctl_ctx
, char *targets
[], size_t n
,
2299 struct shash
*options
)
2301 struct ovsrec_manager
**managers
;
2303 const char *inactivity_probe
= shash_find_data(options
,
2304 "--inactivity-probe");
2306 /* Insert each manager in a new row in Manager table. */
2307 managers
= xmalloc(n
* sizeof *managers
);
2308 for (i
= 0; i
< n
; i
++) {
2309 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2310 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2312 managers
[i
] = ovsrec_manager_insert(vsctl_ctx
->base
.txn
);
2313 ovsrec_manager_set_target(managers
[i
], targets
[i
]);
2314 if (inactivity_probe
) {
2315 int64_t msecs
= atoll(inactivity_probe
);
2316 ovsrec_manager_set_inactivity_probe(managers
[i
], &msecs
, 1);
2320 /* Store uuids of new Manager rows in 'manager_options' column. */
2321 ovsrec_open_vswitch_set_manager_options(vsctl_ctx
->ovs
, managers
, n
);
2326 cmd_set_manager(struct ctl_context
*ctx
)
2328 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2329 const size_t n
= ctx
->argc
- 1;
2331 verify_managers(vsctl_ctx
->ovs
);
2332 delete_managers(vsctl_ctx
->ovs
);
2333 insert_managers(vsctl_ctx
, &ctx
->argv
[1], n
, &ctx
->options
);
2337 pre_cmd_get_ssl(struct ctl_context
*ctx
)
2339 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2341 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_private_key
);
2342 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_certificate
);
2343 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_ca_cert
);
2344 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_bootstrap_ca_cert
);
2348 cmd_get_ssl(struct ctl_context
*ctx
)
2350 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2351 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2353 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2355 ovsrec_ssl_verify_private_key(ssl
);
2356 ovsrec_ssl_verify_certificate(ssl
);
2357 ovsrec_ssl_verify_ca_cert(ssl
);
2358 ovsrec_ssl_verify_bootstrap_ca_cert(ssl
);
2360 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
2361 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
2362 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
2363 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
2364 ssl
->bootstrap_ca_cert
? "true" : "false");
2369 pre_cmd_del_ssl(struct ctl_context
*ctx
)
2371 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2375 cmd_del_ssl(struct ctl_context
*ctx
)
2377 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2378 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2381 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2382 ovsrec_ssl_delete(ssl
);
2383 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, NULL
);
2388 pre_cmd_set_ssl(struct ctl_context
*ctx
)
2390 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2394 cmd_set_ssl(struct ctl_context
*ctx
)
2396 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2397 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
2398 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2400 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2402 ovsrec_ssl_delete(ssl
);
2404 ssl
= ovsrec_ssl_insert(ctx
->txn
);
2406 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
2407 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
2408 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
2410 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
2412 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, ssl
);
2416 autoattach_insert_mapping(struct ovsrec_autoattach
*aa
,
2420 int64_t *key_mappings
, *value_mappings
;
2423 key_mappings
= xmalloc(sizeof *aa
->key_mappings
* (aa
->n_mappings
+ 1));
2424 value_mappings
= xmalloc(sizeof *aa
->value_mappings
* (aa
->n_mappings
+ 1));
2426 for (i
= 0; i
< aa
->n_mappings
; i
++) {
2427 key_mappings
[i
] = aa
->key_mappings
[i
];
2428 value_mappings
[i
] = aa
->value_mappings
[i
];
2430 key_mappings
[aa
->n_mappings
] = isid
;
2431 value_mappings
[aa
->n_mappings
] = vlan
;
2433 ovsrec_autoattach_set_mappings(aa
, key_mappings
, value_mappings
,
2434 aa
->n_mappings
+ 1);
2437 free(value_mappings
);
2441 cmd_add_aa_mapping(struct ctl_context
*ctx
)
2443 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2444 struct vsctl_bridge
*br
;
2448 isid
= strtoull(ctx
->argv
[2], &nptr
, 10);
2449 if (nptr
== ctx
->argv
[2] || nptr
== NULL
) {
2450 ctl_fatal("Invalid argument %s", ctx
->argv
[2]);
2454 vlan
= strtoull(ctx
->argv
[3], &nptr
, 10);
2455 if (nptr
== ctx
->argv
[3] || nptr
== NULL
) {
2456 ctl_fatal("Invalid argument %s", ctx
->argv
[3]);
2460 vsctl_context_populate_cache(ctx
);
2462 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2468 if (!br
->br_cfg
->auto_attach
) {
2469 struct ovsrec_autoattach
*aa
= ovsrec_autoattach_insert(ctx
->txn
);
2470 ovsrec_bridge_set_auto_attach(br
->br_cfg
, aa
);
2472 autoattach_insert_mapping(br
->br_cfg
->auto_attach
, isid
, vlan
);
2477 del_aa_mapping(struct ovsrec_autoattach
*aa
,
2481 int64_t *key_mappings
, *value_mappings
;
2484 key_mappings
= xmalloc(sizeof *aa
->key_mappings
* (aa
->n_mappings
));
2485 value_mappings
= xmalloc(sizeof *value_mappings
* (aa
->n_mappings
));
2487 for (i
= n
= 0; i
< aa
->n_mappings
; i
++) {
2488 if (aa
->key_mappings
[i
] != isid
&& aa
->value_mappings
[i
] != vlan
) {
2489 key_mappings
[n
] = aa
->key_mappings
[i
];
2490 value_mappings
[n
++] = aa
->value_mappings
[i
];
2494 ovsrec_autoattach_set_mappings(aa
, key_mappings
, value_mappings
, n
);
2497 free(value_mappings
);
2501 cmd_del_aa_mapping(struct ctl_context
*ctx
)
2503 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2504 struct vsctl_bridge
*br
;
2508 isid
= strtoull(ctx
->argv
[2], &nptr
, 10);
2509 if (nptr
== ctx
->argv
[2] || nptr
== NULL
) {
2510 ctl_fatal("Invalid argument %s", ctx
->argv
[2]);
2514 vlan
= strtoull(ctx
->argv
[3], &nptr
, 10);
2515 if (nptr
== ctx
->argv
[3] || nptr
== NULL
) {
2516 ctl_fatal("Invalid argument %s", ctx
->argv
[3]);
2520 vsctl_context_populate_cache(ctx
);
2522 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2527 if (br
->br_cfg
&& br
->br_cfg
->auto_attach
&&
2528 br
->br_cfg
->auto_attach
->key_mappings
&&
2529 br
->br_cfg
->auto_attach
->value_mappings
) {
2532 for (i
= 0; i
< br
->br_cfg
->auto_attach
->n_mappings
; i
++) {
2533 if (br
->br_cfg
->auto_attach
->key_mappings
[i
] == isid
&&
2534 br
->br_cfg
->auto_attach
->value_mappings
[i
] == vlan
) {
2535 del_aa_mapping(br
->br_cfg
->auto_attach
, isid
, vlan
);
2543 pre_aa_mapping(struct ctl_context
*ctx
)
2547 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_auto_attach
);
2548 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_autoattach_col_mappings
);
2552 verify_auto_attach(struct ovsrec_bridge
*bridge
)
2555 ovsrec_bridge_verify_auto_attach(bridge
);
2557 if (bridge
->auto_attach
) {
2558 ovsrec_autoattach_verify_mappings(bridge
->auto_attach
);
2564 cmd_get_aa_mapping(struct ctl_context
*ctx
)
2566 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2567 struct vsctl_bridge
*br
;
2569 vsctl_context_populate_cache(ctx
);
2571 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2576 verify_auto_attach(br
->br_cfg
);
2578 if (br
->br_cfg
&& br
->br_cfg
->auto_attach
&&
2579 br
->br_cfg
->auto_attach
->key_mappings
&&
2580 br
->br_cfg
->auto_attach
->value_mappings
) {
2583 for (i
= 0; i
< br
->br_cfg
->auto_attach
->n_mappings
; i
++) {
2584 ds_put_format(&ctx
->output
, "%"PRId64
" %"PRId64
"\n",
2585 br
->br_cfg
->auto_attach
->key_mappings
[i
],
2586 br
->br_cfg
->auto_attach
->value_mappings
[i
]);
2592 static const struct ctl_table_class tables
[OVSREC_N_TABLES
] = {
2593 [OVSREC_TABLE_BRIDGE
].row_ids
[0] = {&ovsrec_bridge_col_name
, NULL
, NULL
},
2595 [OVSREC_TABLE_CONTROLLER
].row_ids
[0]
2596 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_controller
},
2598 [OVSREC_TABLE_INTERFACE
].row_ids
[0]
2599 = {&ovsrec_interface_col_name
, NULL
, NULL
},
2601 [OVSREC_TABLE_MIRROR
].row_ids
[0] = {&ovsrec_mirror_col_name
, NULL
, NULL
},
2603 [OVSREC_TABLE_MANAGER
].row_ids
[0]
2604 = {&ovsrec_manager_col_target
, NULL
, NULL
},
2606 [OVSREC_TABLE_NETFLOW
].row_ids
[0]
2607 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_netflow
},
2609 [OVSREC_TABLE_PORT
].row_ids
[0] = {&ovsrec_port_col_name
, NULL
, NULL
},
2611 [OVSREC_TABLE_QOS
].row_ids
[0]
2612 = {&ovsrec_port_col_name
, NULL
, &ovsrec_port_col_qos
},
2614 [OVSREC_TABLE_SFLOW
].row_ids
[0]
2615 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_sflow
},
2617 [OVSREC_TABLE_FLOW_TABLE
].row_ids
[0]
2618 = {&ovsrec_flow_table_col_name
, NULL
, NULL
},
2620 [OVSREC_TABLE_IPFIX
].row_ids
[0]
2621 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_ipfix
},
2623 [OVSREC_TABLE_AUTOATTACH
].row_ids
[0]
2624 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_auto_attach
},
2626 [OVSREC_TABLE_FLOW_SAMPLE_COLLECTOR_SET
].row_ids
[0]
2627 = {&ovsrec_flow_sample_collector_set_col_id
, NULL
, NULL
},
2631 post_db_reload_check_init(void)
2633 n_neoteric_ifaces
= 0;
2637 post_db_reload_expect_iface(const struct ovsrec_interface
*iface
)
2639 if (n_neoteric_ifaces
>= allocated_neoteric_ifaces
) {
2640 neoteric_ifaces
= x2nrealloc(neoteric_ifaces
,
2641 &allocated_neoteric_ifaces
,
2642 sizeof *neoteric_ifaces
);
2644 neoteric_ifaces
[n_neoteric_ifaces
++] = iface
->header_
.uuid
;
2648 post_db_reload_do_checks(const struct vsctl_context
*vsctl_ctx
)
2650 bool print_error
= false;
2653 for (i
= 0; i
< n_neoteric_ifaces
; i
++) {
2654 const struct uuid
*uuid
;
2656 uuid
= ovsdb_idl_txn_get_insert_uuid(vsctl_ctx
->base
.txn
,
2657 &neoteric_ifaces
[i
]);
2659 const struct ovsrec_interface
*iface
;
2661 iface
= ovsrec_interface_get_for_uuid(vsctl_ctx
->base
.idl
, uuid
);
2662 if (iface
&& (!iface
->ofport
|| *iface
->ofport
== -1)) {
2663 if (iface
->error
&& *iface
->error
) {
2664 ovs_error(0, "Error detected while setting up '%s': %s. "
2665 "See ovs-vswitchd log for details.",
2666 iface
->name
, iface
->error
);
2668 ovs_error(0, "Error detected while setting up '%s'. "
2669 "See ovs-vswitchd log for details.",
2678 ovs_error(0, "The default log directory is \"%s\".", ovs_logdir());
2684 vsctl_context_init_command(struct vsctl_context
*vsctl_ctx
,
2685 struct ctl_command
*command
)
2687 ctl_context_init_command(&vsctl_ctx
->base
, command
);
2688 vsctl_ctx
->verified_ports
= false;
2692 vsctl_context_init(struct vsctl_context
*vsctl_ctx
,
2693 struct ctl_command
*command
, struct ovsdb_idl
*idl
,
2694 struct ovsdb_idl_txn
*txn
,
2695 const struct ovsrec_open_vswitch
*ovs
,
2696 struct ovsdb_symbol_table
*symtab
)
2698 ctl_context_init(&vsctl_ctx
->base
, command
, idl
, txn
, symtab
,
2699 vsctl_context_invalidate_cache
);
2701 vsctl_ctx
->verified_ports
= false;
2703 vsctl_ctx
->ovs
= ovs
;
2704 vsctl_ctx
->cache_valid
= false;
2708 vsctl_context_done_command(struct vsctl_context
*vsctl_ctx
,
2709 struct ctl_command
*command
)
2711 ctl_context_done_command(&vsctl_ctx
->base
, command
);
2715 vsctl_context_done(struct vsctl_context
*vsctl_ctx
,
2716 struct ctl_command
*command
)
2718 ctl_context_done(&vsctl_ctx
->base
, command
);
2722 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2723 struct ovsdb_idl
*idl
)
2725 struct ctl_command
*c
;
2727 ovsdb_idl_add_table(idl
, &ovsrec_table_open_vswitch
);
2728 if (wait_for_reload
) {
2729 ovsdb_idl_add_column(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
2731 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2732 if (c
->syntax
->prerequisites
) {
2733 struct vsctl_context vsctl_ctx
;
2735 ds_init(&c
->output
);
2738 vsctl_context_init(&vsctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2739 (c
->syntax
->prerequisites
)(&vsctl_ctx
.base
);
2740 if (vsctl_ctx
.base
.error
) {
2741 ctl_fatal("%s", vsctl_ctx
.base
.error
);
2743 vsctl_context_done(&vsctl_ctx
, c
);
2745 ovs_assert(!c
->output
.string
);
2746 ovs_assert(!c
->table
);
2752 vsctl_parent_process_info(void)
2758 parent_pid
= getppid();
2761 /* Retrive the command line of the parent process, except the init
2762 * process since /proc/0 does not exist. */
2767 procfile
= xasprintf("/proc/%d/cmdline", parent_pid
);
2769 f
= fopen(procfile
, "r");
2776 ds_put_cstr(&s
, "init");
2779 ds_put_format(&s
, " (pid %d)", parent_pid
);
2780 return ds_steal_cstr(&s
);
2787 do_vsctl(const char *args
, struct ctl_command
*commands
, size_t n_commands
,
2788 struct ovsdb_idl
*idl
)
2790 struct ovsdb_idl_txn
*txn
;
2791 const struct ovsrec_open_vswitch
*ovs
;
2792 enum ovsdb_idl_txn_status status
;
2793 struct ovsdb_symbol_table
*symtab
;
2794 struct vsctl_context vsctl_ctx
;
2795 struct ctl_command
*c
;
2796 struct shash_node
*node
;
2797 int64_t next_cfg
= 0;
2798 char *ppid_info
= NULL
;
2800 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2802 ovsdb_idl_txn_set_dry_run(txn
);
2805 ppid_info
= vsctl_parent_process_info();
2807 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl (invoked by %s): %s",
2811 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2814 ovs
= ovsrec_open_vswitch_first(idl
);
2816 /* XXX add verification that table is empty */
2817 ovs
= ovsrec_open_vswitch_insert(txn
);
2820 if (wait_for_reload
) {
2821 ovsdb_idl_txn_increment(txn
, &ovs
->header_
,
2822 &ovsrec_open_vswitch_col_next_cfg
, false);
2825 post_db_reload_check_init();
2826 symtab
= ovsdb_symbol_table_create();
2827 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2828 ds_init(&c
->output
);
2831 vsctl_context_init(&vsctl_ctx
, NULL
, idl
, txn
, ovs
, symtab
);
2832 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2833 vsctl_context_init_command(&vsctl_ctx
, c
);
2834 if (c
->syntax
->run
) {
2835 (c
->syntax
->run
)(&vsctl_ctx
.base
);
2837 if (vsctl_ctx
.base
.error
) {
2838 ctl_fatal("%s", vsctl_ctx
.base
.error
);
2840 vsctl_context_done_command(&vsctl_ctx
, c
);
2842 if (vsctl_ctx
.base
.try_again
) {
2843 vsctl_context_done(&vsctl_ctx
, NULL
);
2847 vsctl_context_done(&vsctl_ctx
, NULL
);
2849 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2850 struct ovsdb_symbol
*symbol
= node
->data
;
2851 if (!symbol
->created
) {
2852 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2853 "with \"-- --id=%s create ...\")",
2854 node
->name
, node
->name
);
2856 if (!symbol
->strong_ref
) {
2857 if (!symbol
->weak_ref
) {
2858 VLOG_WARN("row id \"%s\" was created but no reference to it "
2859 "was inserted, so it will not actually appear in "
2860 "the database", node
->name
);
2862 VLOG_WARN("row id \"%s\" was created but only a weak "
2863 "reference to it was inserted, so it will not "
2864 "actually appear in the database", node
->name
);
2869 status
= ovsdb_idl_txn_commit_block(txn
);
2870 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2871 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2873 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2874 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2875 if (c
->syntax
->postprocess
) {
2876 vsctl_context_init(&vsctl_ctx
, c
, idl
, txn
, ovs
, symtab
);
2877 (c
->syntax
->postprocess
)(&vsctl_ctx
.base
);
2878 if (vsctl_ctx
.base
.error
) {
2879 ctl_fatal("%s", vsctl_ctx
.base
.error
);
2881 vsctl_context_done(&vsctl_ctx
, c
);
2887 case TXN_UNCOMMITTED
:
2888 case TXN_INCOMPLETE
:
2892 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2893 ctl_fatal("transaction aborted");
2903 ctl_fatal("transaction error: %s", ovsdb_idl_txn_get_error(txn
));
2905 case TXN_NOT_LOCKED
:
2906 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2907 ctl_fatal("database not locked");
2913 ovsdb_symbol_table_destroy(symtab
);
2915 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2916 struct ds
*ds
= &c
->output
;
2919 table_print(c
->table
, &table_style
);
2920 } else if (oneline
) {
2924 for (j
= 0; j
< ds
->length
; j
++) {
2925 int ch
= ds
->string
[j
];
2928 fputs("\\n", stdout
);
2932 fputs("\\\\", stdout
);
2941 fputs(ds_cstr(ds
), stdout
);
2943 ds_destroy(&c
->output
);
2944 table_destroy(c
->table
);
2947 shash_destroy_free_data(&c
->options
);
2951 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2952 /* Even, if --retry flag was not specified, ovs-vsctl still
2953 * has to retry to establish OVSDB connection, if wait_for_reload
2954 * was set. Otherwise, ovs-vsctl would end up waiting forever
2955 * until cur_cfg would be updated. */
2956 ovsdb_idl_enable_reconnect(idl
);
2959 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2960 if (ovs
->cur_cfg
>= next_cfg
) {
2961 post_db_reload_do_checks(&vsctl_ctx
);
2965 ovsdb_idl_wait(idl
);
2970 ovsdb_idl_txn_destroy(txn
);
2971 ovsdb_idl_destroy(idl
);
2976 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2977 * resources and return so that the caller can try again. */
2978 ovsdb_idl_txn_abort(txn
);
2979 ovsdb_idl_txn_destroy(txn
);
2982 ovsdb_symbol_table_destroy(symtab
);
2983 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2984 ds_destroy(&c
->output
);
2985 table_destroy(c
->table
);
2991 /* Frees the current transaction and the underlying IDL and then calls
2994 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2995 * for a clean memory leak report from valgrind in the normal case. That makes
2996 * it easier to notice real memory leaks. */
2998 vsctl_exit(int status
)
3001 ovsdb_idl_txn_abort(the_idl_txn
);
3002 ovsdb_idl_txn_destroy(the_idl_txn
);
3004 ovsdb_idl_destroy(the_idl
);
3009 * Developers who add new commands to the 'struct ctl_command_syntax' must
3010 * define the 'arguments' member of the struct. The following keywords are
3011 * available for composing the argument format:
3013 * TABLE RECORD BRIDGE PARENT PORT
3014 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
3015 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
3016 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
3017 * TARGET NEW-* (e.g. NEW-PORT)
3019 * For argument types not listed above, just uses 'ARG' as place holder.
3021 * Encloses the keyword with '[]' if it is optional. Appends '...' to
3022 * keyword or enclosed keyword to indicate that the argument can be specified
3026 static const struct ctl_command_syntax vsctl_commands
[] = {
3027 /* Open vSwitch commands. */
3028 {"init", 0, 0, "", NULL
, cmd_init
, NULL
, "", RW
},
3030 /* Bridge commands. */
3031 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info
,
3032 cmd_add_br
, NULL
, "--may-exist", RW
},
3033 {"del-br", 1, 1, "BRIDGE", pre_get_info
, cmd_del_br
,
3034 NULL
, "--if-exists", RW
},
3035 {"list-br", 0, 0, "", pre_get_info
, cmd_list_br
, NULL
, "--real,--fake",
3037 {"br-exists", 1, 1, "BRIDGE", pre_get_info
, cmd_br_exists
, NULL
, "", RO
},
3038 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info
, cmd_br_to_vlan
, NULL
, "",
3040 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info
, cmd_br_to_parent
, NULL
,
3042 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
3043 pre_cmd_br_set_external_id
, cmd_br_set_external_id
, NULL
, "", RW
},
3044 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id
,
3045 cmd_br_get_external_id
, NULL
, "", RO
},
3047 /* Port commands. */
3048 {"list-ports", 1, 1, "BRIDGE", pre_get_info
, cmd_list_ports
, NULL
, "",
3050 {"add-port", 2, INT_MAX
, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
3051 pre_get_info
, cmd_add_port
, NULL
, "--may-exist", RW
},
3052 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info
, cmd_del_port
, NULL
,
3053 "--if-exists,--with-iface", RW
},
3054 {"port-to-br", 1, 1, "PORT", pre_get_info
, cmd_port_to_br
, NULL
, "", RO
},
3056 /* Bond commands. */
3057 {"add-bond", 4, INT_MAX
,
3058 "BRIDGE BOND IFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info
,
3059 cmd_add_bond
, NULL
, "--may-exist,--fake-iface", RW
},
3060 {"add-bond-iface", 2, 2, "BOND IFACE", pre_get_info
, cmd_add_bond_iface
,
3061 NULL
, "--may-exist", RW
},
3062 {"del-bond-iface", 1, 2, "[BOND] IFACE", pre_get_info
, cmd_del_bond_iface
,
3063 NULL
, "--if-exists", RW
},
3065 /* Interface commands. */
3066 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info
, cmd_list_ifaces
, NULL
, "",
3068 {"iface-to-br", 1, 1, "IFACE", pre_get_info
, cmd_iface_to_br
, NULL
, "",
3071 /* Controller commands. */
3072 {"get-controller", 1, 1, "BRIDGE", pre_controller
, cmd_get_controller
,
3074 {"del-controller", 1, 1, "BRIDGE", pre_controller
, cmd_del_controller
,
3076 {"set-controller", 1, INT_MAX
, "BRIDGE TARGET...", pre_controller
,
3077 cmd_set_controller
, NULL
, "--inactivity-probe=", RW
},
3078 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info
, cmd_get_fail_mode
, NULL
,
3080 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info
, cmd_del_fail_mode
, NULL
,
3082 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info
, cmd_set_fail_mode
,
3085 /* Manager commands. */
3086 {"get-manager", 0, 0, "", pre_manager
, cmd_get_manager
, NULL
, "", RO
},
3087 {"del-manager", 0, 0, "", pre_manager
, cmd_del_manager
, NULL
, "", RW
},
3088 {"set-manager", 1, INT_MAX
, "TARGET...", pre_manager
, cmd_set_manager
,
3089 NULL
, "--inactivity-probe=", RW
},
3092 {"get-ssl", 0, 0, "", pre_cmd_get_ssl
, cmd_get_ssl
, NULL
, "", RO
},
3093 {"del-ssl", 0, 0, "", pre_cmd_del_ssl
, cmd_del_ssl
, NULL
, "", RW
},
3094 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl
,
3095 cmd_set_ssl
, NULL
, "--bootstrap", RW
},
3097 /* Auto Attach commands. */
3098 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping
, cmd_add_aa_mapping
,
3100 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping
, cmd_del_aa_mapping
,
3102 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping
, cmd_get_aa_mapping
,
3105 /* Switch commands. */
3106 {"emer-reset", 0, 0, "", pre_cmd_emer_reset
, cmd_emer_reset
, NULL
, "", RW
},
3108 /* Zone and CT Timeout Policy commands. */
3109 {"add-zone-tp", 3, INT_MAX
, "", pre_get_zone
, cmd_add_zone_tp
, NULL
,
3111 {"del-zone-tp", 2, 2, "", pre_get_zone
, cmd_del_zone_tp
, NULL
,
3113 {"list-zone-tp", 1, 1, "", pre_get_zone
, cmd_list_zone_tp
, NULL
, "", RO
},
3115 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
3118 /* Registers vsctl and common db commands. */
3120 vsctl_cmd_init(void)
3122 ctl_init(&ovsrec_idl_class
, ovsrec_table_classes
, tables
, cmd_show_tables
,
3124 ctl_register_commands(vsctl_commands
);