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 "openvswitch/dynamic-string.h"
36 #include "fatal-signal.h"
38 #include "openvswitch/json.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-idl.h"
41 #include "poll-loop.h"
44 #include "stream-ssl.h"
48 #include "lib/vswitch-idl.h"
52 #include "openvswitch/vconn.h"
53 #include "openvswitch/vlog.h"
55 VLOG_DEFINE_THIS_MODULE(vsctl
);
59 /* --db: The database server to contact. */
60 static const char *db
;
62 /* --oneline: Write each command's output as a single line? */
65 /* --dry-run: Do not commit any changes. */
68 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
69 static bool wait_for_reload
= true;
71 /* --timeout: Time to wait for a connection to 'db'. */
74 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
75 * If false and --db says to use an active connection method (e.g. "unix:",
76 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
77 * error if the database server cannot be contacted (e.g. ovsdb-server is not
80 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
84 /* Format for table output. */
85 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
87 static void vsctl_cmd_init(void);
89 /* The IDL we're using and the current transaction, if any.
90 * This is for use by vsctl_exit() only, to allow it to clean up.
91 * Other code should use its context arguments. */
92 static struct ovsdb_idl
*the_idl
;
93 static struct ovsdb_idl_txn
*the_idl_txn
;
94 OVS_NO_RETURN
static void vsctl_exit(int status
);
96 OVS_NO_RETURN
static void usage(void);
97 static void parse_options(int argc
, char *argv
[], struct shash
*local_options
);
98 static void run_prerequisites(struct ctl_command
[], size_t n_commands
,
100 static void do_vsctl(const char *args
, struct ctl_command
*, size_t n
,
103 /* post_db_reload_check frame work is to allow ovs-vsctl to do additional
104 * checks after OVSDB transactions are successfully recorded and reload by
107 * For example, When a new interface is added to OVSDB, ovs-vswitchd will
108 * either store a positive values on successful implementing the new
109 * interface, or -1 on failure.
111 * Unless --no-wait command line option is specified,
112 * post_db_reload_do_checks() is called right after any configuration
113 * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
114 * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
115 * more detailed messages about those errors.
117 * Current implementation only check for Post OVSDB reload failures on new
118 * interface additions with 'add-br' and 'add-port' commands.
120 * post_db_reload_expect_iface()
122 * keep track of interfaces to be checked post OVSDB reload. */
123 static void post_db_reload_check_init(void);
124 static void post_db_reload_do_checks(const struct vsctl_context
*);
125 static void post_db_reload_expect_iface(const struct ovsrec_interface
*);
127 static struct uuid
*neoteric_ifaces
;
128 static size_t n_neoteric_ifaces
;
129 static size_t allocated_neoteric_ifaces
;
132 main(int argc
, char *argv
[])
134 struct ovsdb_idl
*idl
;
135 struct ctl_command
*commands
;
136 struct shash local_options
;
141 set_program_name(argv
[0]);
142 fatal_ignore_sigpipe();
143 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
144 vlog_set_levels_from_string_assert("reconnect:warn");
148 /* Log our arguments. This is often valuable for debugging systems. */
149 args
= process_escape_args(argv
);
150 VLOG(ctl_might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
152 /* Parse command line. */
153 shash_init(&local_options
);
154 parse_options(argc
, argv
, &local_options
);
155 commands
= ctl_parse_commands(argc
- optind
, argv
+ optind
, &local_options
,
162 /* Initialize IDL. */
163 idl
= the_idl
= ovsdb_idl_create(db
, &ovsrec_idl_class
, false, retry
);
164 run_prerequisites(commands
, n_commands
, idl
);
166 /* Execute the commands.
168 * 'seqno' is the database sequence number for which we last tried to
169 * execute our transaction. There's no point in trying to commit more than
170 * once for any given sequence number, because if the transaction fails
171 * it's because the database changed and we need to obtain an up-to-date
172 * view of the database before we try the transaction again. */
173 seqno
= ovsdb_idl_get_seqno(idl
);
176 if (!ovsdb_idl_is_alive(idl
)) {
177 int retval
= ovsdb_idl_get_last_error(idl
);
178 ctl_fatal("%s: database connection failed (%s)",
179 db
, ovs_retval_to_string(retval
));
182 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
183 seqno
= ovsdb_idl_get_seqno(idl
);
184 do_vsctl(args
, commands
, n_commands
, idl
);
187 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
195 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
198 OPT_DB
= UCHAR_MAX
+ 1,
203 OPT_BOOTSTRAP_CA_CERT
,
213 static const struct option global_long_options
[] = {
214 {"db", required_argument
, NULL
, OPT_DB
},
215 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
216 {"no-wait", no_argument
, NULL
, OPT_NO_WAIT
},
217 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
218 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
219 {"timeout", required_argument
, NULL
, 't'},
220 {"retry", no_argument
, NULL
, OPT_RETRY
},
221 {"help", no_argument
, NULL
, 'h'},
222 {"commands", no_argument
, NULL
, OPT_COMMANDS
},
223 {"options", no_argument
, NULL
, OPT_OPTIONS
},
224 {"version", no_argument
, NULL
, 'V'},
227 STREAM_SSL_LONG_OPTIONS
,
228 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
229 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
232 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
233 char *tmp
, *short_options
;
235 struct option
*options
;
236 size_t allocated_options
;
240 tmp
= ovs_cmdl_long_options_to_short_options(global_long_options
);
241 short_options
= xasprintf("+%s", tmp
);
244 /* We want to parse both global and command-specific options here, but
245 * getopt_long() isn't too convenient for the job. We copy our global
246 * options into a dynamic array, then append all of the command-specific
248 options
= xmemdup(global_long_options
, sizeof global_long_options
);
249 allocated_options
= ARRAY_SIZE(global_long_options
);
250 n_options
= n_global_long_options
;
251 ctl_add_cmd_options(&options
, &n_options
, &allocated_options
, OPT_LOCAL
);
257 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
272 vlog_set_levels(&this_module
, VLF_SYSLOG
, VLL_WARN
);
276 wait_for_reload
= false;
284 if (shash_find(local_options
, options
[idx
].name
)) {
285 ctl_fatal("'%s' option specified multiple times",
288 shash_add_nocopy(local_options
,
289 xasprintf("--%s", options
[idx
].name
),
290 nullable_xstrdup(optarg
));
297 ctl_print_commands();
300 ctl_print_options(global_long_options
);
303 ovs_print_version(0, 0);
304 printf("DB Schema %s\n", ovsrec_get_db_version());
308 timeout
= strtoul(optarg
, NULL
, 10);
310 ctl_fatal("value %s on -t or --timeout is invalid",
320 TABLE_OPTION_HANDLERS(&table_style
)
322 STREAM_SSL_OPTION_HANDLERS
324 case OPT_PEER_CA_CERT
:
325 stream_ssl_set_peer_ca_cert_file(optarg
);
328 case OPT_BOOTSTRAP_CA_CERT
:
329 stream_ssl_set_ca_cert_file(optarg
, true);
342 db
= ctl_default_db();
345 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
346 free(CONST_CAST(char *, options
[i
].name
));
355 %s: ovs-vswitchd management utility\n\
356 usage: %s [OPTIONS] COMMAND [ARG...]\n\
358 Open vSwitch commands:\n\
359 init initialize database, if not yet initialized\n\
360 show print overview of database contents\n\
361 emer-reset reset configuration to clean state\n\
364 add-br BRIDGE create a new bridge named BRIDGE\n\
365 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
366 del-br BRIDGE delete BRIDGE and all of its ports\n\
367 list-br print the names of all the bridges\n\
368 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
369 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
370 br-to-parent BRIDGE print the parent of BRIDGE\n\
371 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
372 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
373 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
374 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
376 Port commands (a bond is considered to be a single port):\n\
377 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
378 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
379 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
380 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
381 port-to-br PORT print name of bridge that contains PORT\n\
383 Interface commands (a bond consists of multiple interfaces):\n\
384 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
385 iface-to-br IFACE print name of bridge that contains IFACE\n\
387 Controller commands:\n\
388 get-controller BRIDGE print the controllers for BRIDGE\n\
389 del-controller BRIDGE delete the controllers for BRIDGE\n\
390 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
391 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
392 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
393 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
396 get-manager print the managers\n\
397 del-manager delete the managers\n\
398 set-manager TARGET... set the list of managers to TARGET...\n\
401 get-ssl print the SSL configuration\n\
402 del-ssl delete the SSL configuration\n\
403 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
405 Auto Attach commands:\n\
406 add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
407 del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
408 get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
411 emer-reset reset switch to known good state\n\
416 --db=DATABASE connect to DATABASE\n\
418 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
419 --retry keep trying to connect to server forever\n\
420 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
421 --dry-run do not commit changes to database\n\
422 --oneline print exactly one line of output per command\n",
423 program_name
, program_name
, ctl_get_db_cmd_usage(), ctl_default_db());
426 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
427 stream_usage("database", true, true, false);
430 -h, --help display this help message\n\
431 -V, --version display version information\n");
436 /* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
437 struct vsctl_context
{
438 struct ctl_context base
;
440 /* Modifiable state. */
441 const struct ovsrec_open_vswitch
*ovs
;
444 /* A cache of the contents of the database.
446 * A command that needs to use any of this information must first call
447 * vsctl_context_populate_cache(). A command that changes anything that
448 * could invalidate the cache must either call
449 * vsctl_context_invalidate_cache() or manually update the cache to
450 * maintain its correctness. */
452 struct shash bridges
; /* Maps from bridge name to struct vsctl_bridge. */
453 struct shash ports
; /* Maps from port name to struct vsctl_port. */
454 struct shash ifaces
; /* Maps from port name to struct vsctl_iface. */
457 struct vsctl_bridge
{
458 struct ovsrec_bridge
*br_cfg
;
460 struct ovs_list ports
; /* Contains "struct vsctl_port"s. */
462 /* VLAN ("fake") bridge support.
464 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
466 struct hmap children
; /* VLAN bridges indexed by 'vlan'. */
467 struct hmap_node children_node
; /* Node in parent's 'children' hmap. */
468 struct vsctl_bridge
*parent
; /* Real bridge, or NULL. */
469 int vlan
; /* VLAN VID (0...4095), or 0. */
473 struct ovs_list ports_node
; /* In struct vsctl_bridge's 'ports' list. */
474 struct ovs_list ifaces
; /* Contains "struct vsctl_iface"s. */
475 struct ovsrec_port
*port_cfg
;
476 struct vsctl_bridge
*bridge
;
480 struct ovs_list ifaces_node
; /* In struct vsctl_port's 'ifaces' list. */
481 struct ovsrec_interface
*iface_cfg
;
482 struct vsctl_port
*port
;
485 /* Casts 'base' into 'struct vsctl_context'. */
486 static struct vsctl_context
*
487 vsctl_context_cast(struct ctl_context
*base
)
489 return CONTAINER_OF(base
, struct vsctl_context
, base
);
492 static struct vsctl_bridge
*find_vlan_bridge(struct vsctl_bridge
*parent
,
496 vsctl_context_to_string(const struct ctl_context
*ctx
)
498 const struct shash_node
*node
;
504 SHASH_FOR_EACH (node
, &ctx
->options
) {
505 svec_add(&words
, node
->name
);
507 for (i
= 0; i
< ctx
->argc
; i
++) {
508 svec_add(&words
, ctx
->argv
[i
]);
510 svec_terminate(&words
);
512 s
= process_escape_args(words
.names
);
514 svec_destroy(&words
);
520 verify_ports(struct vsctl_context
*vsctl_ctx
)
522 if (!vsctl_ctx
->verified_ports
) {
523 const struct ovsrec_bridge
*bridge
;
524 const struct ovsrec_port
*port
;
526 ovsrec_open_vswitch_verify_bridges(vsctl_ctx
->ovs
);
527 OVSREC_BRIDGE_FOR_EACH (bridge
, vsctl_ctx
->base
.idl
) {
528 ovsrec_bridge_verify_ports(bridge
);
530 OVSREC_PORT_FOR_EACH (port
, vsctl_ctx
->base
.idl
) {
531 ovsrec_port_verify_interfaces(port
);
534 vsctl_ctx
->verified_ports
= true;
538 static struct vsctl_bridge
*
539 add_bridge_to_cache(struct vsctl_context
*vsctl_ctx
,
540 struct ovsrec_bridge
*br_cfg
, const char *name
,
541 struct vsctl_bridge
*parent
, int vlan
)
543 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
545 br
->name
= xstrdup(name
);
546 ovs_list_init(&br
->ports
);
549 hmap_init(&br
->children
);
551 struct vsctl_bridge
*conflict
= find_vlan_bridge(parent
, vlan
);
553 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
554 "for VLAN %d, but only one is allowed",
555 parent
->name
, name
, conflict
->name
, vlan
);
557 hmap_insert(&parent
->children
, &br
->children_node
,
561 shash_add(&vsctl_ctx
->bridges
, br
->name
, br
);
566 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
567 struct ovsrec_bridge
*bridge
)
569 struct ovsrec_bridge
**bridges
;
572 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
573 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
574 if (ovs
->bridges
[i
] != bridge
) {
575 bridges
[n
++] = ovs
->bridges
[i
];
578 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
583 del_cached_bridge(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*br
)
585 ovs_assert(ovs_list_is_empty(&br
->ports
));
586 ovs_assert(hmap_is_empty(&br
->children
));
588 hmap_remove(&br
->parent
->children
, &br
->children_node
);
591 ovsrec_bridge_delete(br
->br_cfg
);
592 ovs_delete_bridge(vsctl_ctx
->ovs
, br
->br_cfg
);
594 shash_find_and_delete(&vsctl_ctx
->bridges
, br
->name
);
595 hmap_destroy(&br
->children
);
601 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
603 return (port_cfg
->fake_bridge
605 && *port_cfg
->tag
>= 0 && *port_cfg
->tag
<= 4095);
608 static struct vsctl_bridge
*
609 find_vlan_bridge(struct vsctl_bridge
*parent
, int vlan
)
611 struct vsctl_bridge
*child
;
613 HMAP_FOR_EACH_IN_BUCKET (child
, children_node
, hash_int(vlan
, 0),
615 if (child
->vlan
== vlan
) {
623 static struct vsctl_port
*
624 add_port_to_cache(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*parent
,
625 struct ovsrec_port
*port_cfg
)
627 struct vsctl_port
*port
;
630 && *port_cfg
->tag
>= 0 && *port_cfg
->tag
<= 4095) {
631 struct vsctl_bridge
*vlan_bridge
;
633 vlan_bridge
= find_vlan_bridge(parent
, *port_cfg
->tag
);
635 parent
= vlan_bridge
;
639 port
= xmalloc(sizeof *port
);
640 ovs_list_push_back(&parent
->ports
, &port
->ports_node
);
641 ovs_list_init(&port
->ifaces
);
642 port
->port_cfg
= port_cfg
;
643 port
->bridge
= parent
;
644 shash_add(&vsctl_ctx
->ports
, port_cfg
->name
, port
);
650 del_cached_port(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*port
)
652 ovs_assert(ovs_list_is_empty(&port
->ifaces
));
653 ovs_list_remove(&port
->ports_node
);
654 shash_find_and_delete(&vsctl_ctx
->ports
, port
->port_cfg
->name
);
655 ovsrec_port_delete(port
->port_cfg
);
659 static struct vsctl_iface
*
660 add_iface_to_cache(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*parent
,
661 struct ovsrec_interface
*iface_cfg
)
663 struct vsctl_iface
*iface
;
665 iface
= xmalloc(sizeof *iface
);
666 ovs_list_push_back(&parent
->ifaces
, &iface
->ifaces_node
);
667 iface
->iface_cfg
= iface_cfg
;
668 iface
->port
= parent
;
669 shash_add(&vsctl_ctx
->ifaces
, iface_cfg
->name
, iface
);
675 del_cached_iface(struct vsctl_context
*vsctl_ctx
, struct vsctl_iface
*iface
)
677 ovs_list_remove(&iface
->ifaces_node
);
678 shash_find_and_delete(&vsctl_ctx
->ifaces
, iface
->iface_cfg
->name
);
679 ovsrec_interface_delete(iface
->iface_cfg
);
684 vsctl_context_invalidate_cache(struct ctl_context
*ctx
)
686 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
687 struct shash_node
*node
;
689 if (!vsctl_ctx
->cache_valid
) {
692 vsctl_ctx
->cache_valid
= false;
694 SHASH_FOR_EACH (node
, &vsctl_ctx
->bridges
) {
695 struct vsctl_bridge
*bridge
= node
->data
;
696 hmap_destroy(&bridge
->children
);
700 shash_destroy(&vsctl_ctx
->bridges
);
702 shash_destroy_free_data(&vsctl_ctx
->ports
);
703 shash_destroy_free_data(&vsctl_ctx
->ifaces
);
707 pre_get_info(struct ctl_context
*ctx
)
709 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_bridges
);
711 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_name
);
712 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
713 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
714 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ports
);
716 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_name
);
717 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_fake_bridge
);
718 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_tag
);
719 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_interfaces
);
721 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_name
);
723 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_ofport
);
724 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_error
);
728 vsctl_context_populate_cache(struct ctl_context
*ctx
)
730 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
731 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
732 struct sset bridges
, ports
;
735 if (vsctl_ctx
->cache_valid
) {
736 /* Cache is already populated. */
739 vsctl_ctx
->cache_valid
= true;
740 shash_init(&vsctl_ctx
->bridges
);
741 shash_init(&vsctl_ctx
->ports
);
742 shash_init(&vsctl_ctx
->ifaces
);
746 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
747 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
748 struct vsctl_bridge
*br
;
751 if (!sset_add(&bridges
, br_cfg
->name
)) {
752 VLOG_WARN("%s: database contains duplicate bridge name",
756 br
= add_bridge_to_cache(vsctl_ctx
, br_cfg
, br_cfg
->name
, NULL
, 0);
758 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
759 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
761 if (!sset_add(&ports
, port_cfg
->name
)) {
762 /* Duplicate port name. (We will warn about that later.) */
766 if (port_is_fake_bridge(port_cfg
)
767 && sset_add(&bridges
, port_cfg
->name
)) {
768 add_bridge_to_cache(vsctl_ctx
, NULL
, port_cfg
->name
, br
,
773 sset_destroy(&bridges
);
774 sset_destroy(&ports
);
777 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
778 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
779 struct vsctl_bridge
*br
;
782 if (!sset_add(&bridges
, br_cfg
->name
)) {
785 br
= shash_find_data(&vsctl_ctx
->bridges
, br_cfg
->name
);
786 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
787 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
788 struct vsctl_port
*port
;
791 port
= shash_find_data(&vsctl_ctx
->ports
, port_cfg
->name
);
793 if (port_cfg
== port
->port_cfg
) {
794 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
795 port_cfg
->name
, br
->name
, port
->bridge
->name
);
797 /* Log as an error because this violates the database's
798 * uniqueness constraints, so the database server shouldn't
799 * have allowed it. */
800 VLOG_ERR("%s: database contains duplicate port name",
806 if (port_is_fake_bridge(port_cfg
)
807 && !sset_add(&bridges
, port_cfg
->name
)) {
811 port
= add_port_to_cache(vsctl_ctx
, br
, port_cfg
);
812 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
813 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
814 struct vsctl_iface
*iface
;
816 iface
= shash_find_data(&vsctl_ctx
->ifaces
, iface_cfg
->name
);
818 if (iface_cfg
== iface
->iface_cfg
) {
819 VLOG_WARN("%s: interface is in multiple ports "
822 iface
->port
->port_cfg
->name
,
823 port
->port_cfg
->name
);
825 /* Log as an error because this violates the database's
826 * uniqueness constraints, so the database server
827 * shouldn't have allowed it. */
828 VLOG_ERR("%s: database contains duplicate interface "
829 "name", iface_cfg
->name
);
834 add_iface_to_cache(vsctl_ctx
, port
, iface_cfg
);
838 sset_destroy(&bridges
);
842 check_conflicts(struct vsctl_context
*vsctl_ctx
, const char *name
,
845 struct vsctl_iface
*iface
;
846 struct vsctl_port
*port
;
848 verify_ports(vsctl_ctx
);
850 if (shash_find(&vsctl_ctx
->bridges
, name
)) {
851 ctl_fatal("%s because a bridge named %s already exists",
855 port
= shash_find_data(&vsctl_ctx
->ports
, name
);
857 ctl_fatal("%s because a port named %s already exists on "
858 "bridge %s", msg
, name
, port
->bridge
->name
);
861 iface
= shash_find_data(&vsctl_ctx
->ifaces
, name
);
863 ctl_fatal("%s because an interface named %s already exists "
864 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
870 static struct vsctl_bridge
*
871 find_bridge(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
873 struct vsctl_bridge
*br
;
875 ovs_assert(vsctl_ctx
->cache_valid
);
877 br
= shash_find_data(&vsctl_ctx
->bridges
, name
);
878 if (must_exist
&& !br
) {
879 ctl_fatal("no bridge named %s", name
);
881 ovsrec_open_vswitch_verify_bridges(vsctl_ctx
->ovs
);
885 static struct vsctl_bridge
*
886 find_real_bridge(struct vsctl_context
*vsctl_ctx
,
887 const char *name
, bool must_exist
)
889 struct vsctl_bridge
*br
= find_bridge(vsctl_ctx
, name
, must_exist
);
890 if (br
&& br
->parent
) {
891 ctl_fatal("%s is a fake bridge", name
);
896 static struct vsctl_port
*
897 find_port(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
899 struct vsctl_port
*port
;
901 ovs_assert(vsctl_ctx
->cache_valid
);
903 port
= shash_find_data(&vsctl_ctx
->ports
, name
);
904 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
907 if (must_exist
&& !port
) {
908 ctl_fatal("no port named %s", name
);
910 verify_ports(vsctl_ctx
);
914 static struct vsctl_iface
*
915 find_iface(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
917 struct vsctl_iface
*iface
;
919 ovs_assert(vsctl_ctx
->cache_valid
);
921 iface
= shash_find_data(&vsctl_ctx
->ifaces
, name
);
922 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
925 if (must_exist
&& !iface
) {
926 ctl_fatal("no interface named %s", name
);
928 verify_ports(vsctl_ctx
);
933 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
935 struct ovsrec_port
**ports
;
938 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
939 for (i
= 0; i
< br
->n_ports
; i
++) {
940 ports
[i
] = br
->ports
[i
];
942 ports
[br
->n_ports
] = port
;
943 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
948 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
950 struct ovsrec_port
**ports
;
953 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
954 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
955 if (br
->ports
[i
] != port
) {
956 ports
[n
++] = br
->ports
[i
];
959 ovsrec_bridge_set_ports(br
, ports
, n
);
964 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
965 struct ovsrec_bridge
*bridge
)
967 struct ovsrec_bridge
**bridges
;
970 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
971 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
972 bridges
[i
] = ovs
->bridges
[i
];
974 bridges
[ovs
->n_bridges
] = bridge
;
975 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
980 cmd_init(struct ctl_context
*ctx OVS_UNUSED
)
984 static struct cmd_show_table cmd_show_tables
[] = {
985 {&ovsrec_table_open_vswitch
,
987 {&ovsrec_open_vswitch_col_manager_options
,
988 &ovsrec_open_vswitch_col_bridges
,
989 &ovsrec_open_vswitch_col_ovs_version
},
993 {&ovsrec_table_bridge
,
994 &ovsrec_bridge_col_name
,
995 {&ovsrec_bridge_col_controller
,
996 &ovsrec_bridge_col_fail_mode
,
997 &ovsrec_bridge_col_ports
},
1001 {&ovsrec_table_port
,
1002 &ovsrec_port_col_name
,
1003 {&ovsrec_port_col_tag
,
1004 &ovsrec_port_col_trunks
,
1005 &ovsrec_port_col_interfaces
},
1009 {&ovsrec_table_interface
,
1010 &ovsrec_interface_col_name
,
1011 {&ovsrec_interface_col_type
,
1012 &ovsrec_interface_col_options
,
1013 &ovsrec_interface_col_error
},
1017 {&ovsrec_table_controller
,
1018 &ovsrec_controller_col_target
,
1019 {&ovsrec_controller_col_is_connected
,
1025 {&ovsrec_table_manager
,
1026 &ovsrec_manager_col_target
,
1027 {&ovsrec_manager_col_is_connected
,
1033 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
1037 pre_cmd_emer_reset(struct ctl_context
*ctx
)
1039 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1040 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1042 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
1043 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
1044 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_mirrors
);
1045 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_netflow
);
1046 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_sflow
);
1047 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ipfix
);
1048 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_flood_vlans
);
1049 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_other_config
);
1051 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_other_config
);
1053 ovsdb_idl_add_column(ctx
->idl
,
1054 &ovsrec_interface_col_ingress_policing_rate
);
1055 ovsdb_idl_add_column(ctx
->idl
,
1056 &ovsrec_interface_col_ingress_policing_burst
);
1060 cmd_emer_reset(struct ctl_context
*ctx
)
1062 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1063 const struct ovsdb_idl
*idl
= ctx
->idl
;
1064 const struct ovsrec_bridge
*br
;
1065 const struct ovsrec_port
*port
;
1066 const struct ovsrec_interface
*iface
;
1067 const struct ovsrec_mirror
*mirror
, *next_mirror
;
1068 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
1069 const struct ovsrec_manager
*mgr
, *next_mgr
;
1070 const struct ovsrec_netflow
*nf
, *next_nf
;
1071 const struct ovsrec_ssl
*ssl
, *next_ssl
;
1072 const struct ovsrec_sflow
*sflow
, *next_sflow
;
1073 const struct ovsrec_ipfix
*ipfix
, *next_ipfix
;
1074 const struct ovsrec_flow_sample_collector_set
*fscset
, *next_fscset
;
1076 /* Reset the Open_vSwitch table. */
1077 ovsrec_open_vswitch_set_manager_options(vsctl_ctx
->ovs
, NULL
, 0);
1078 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, NULL
);
1080 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
1083 ovsrec_bridge_set_controller(br
, NULL
, 0);
1084 ovsrec_bridge_set_fail_mode(br
, NULL
);
1085 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
1086 ovsrec_bridge_set_netflow(br
, NULL
);
1087 ovsrec_bridge_set_sflow(br
, NULL
);
1088 ovsrec_bridge_set_ipfix(br
, NULL
);
1089 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
1091 /* We only want to save the "hwaddr" key from other_config. */
1092 hwaddr
= smap_get(&br
->other_config
, "hwaddr");
1094 const struct smap smap
= SMAP_CONST1(&smap
, "hwaddr", hwaddr
);
1095 ovsrec_bridge_set_other_config(br
, &smap
);
1097 ovsrec_bridge_set_other_config(br
, NULL
);
1101 OVSREC_PORT_FOR_EACH (port
, idl
) {
1102 ovsrec_port_set_other_config(port
, NULL
);
1105 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
1106 /* xxx What do we do about gre/patch devices created by mgr? */
1108 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
1109 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
1112 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
1113 ovsrec_mirror_delete(mirror
);
1116 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
1117 ovsrec_controller_delete(ctrl
);
1120 OVSREC_MANAGER_FOR_EACH_SAFE (mgr
, next_mgr
, idl
) {
1121 ovsrec_manager_delete(mgr
);
1124 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
1125 ovsrec_netflow_delete(nf
);
1128 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
1129 ovsrec_ssl_delete(ssl
);
1132 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
1133 ovsrec_sflow_delete(sflow
);
1136 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix
, next_ipfix
, idl
) {
1137 ovsrec_ipfix_delete(ipfix
);
1140 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset
, next_fscset
, idl
) {
1141 ovsrec_flow_sample_collector_set_delete(fscset
);
1144 vsctl_context_invalidate_cache(ctx
);
1148 cmd_add_br(struct ctl_context
*ctx
)
1150 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1151 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1152 const char *br_name
, *parent_name
;
1153 struct ovsrec_interface
*iface
;
1156 br_name
= ctx
->argv
[1];
1157 if (ctx
->argc
== 2) {
1160 } else if (ctx
->argc
== 4) {
1161 parent_name
= ctx
->argv
[2];
1162 vlan
= atoi(ctx
->argv
[3]);
1163 if (vlan
< 0 || vlan
> 4095) {
1164 ctl_fatal("%s: vlan must be between 0 and 4095", ctx
->argv
[0]);
1167 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1171 vsctl_context_populate_cache(ctx
);
1173 struct vsctl_bridge
*br
;
1175 br
= find_bridge(vsctl_ctx
, br_name
, false);
1179 ctl_fatal("\"--may-exist add-br %s\" but %s is "
1180 "a VLAN bridge for VLAN %d",
1181 br_name
, br_name
, br
->vlan
);
1185 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1186 "is not a VLAN bridge",
1187 br_name
, parent_name
, vlan
, br_name
);
1188 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1189 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1190 "has the wrong parent %s",
1191 br_name
, parent_name
, vlan
,
1192 br_name
, br
->parent
->name
);
1193 } else if (br
->vlan
!= vlan
) {
1194 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1195 "is a VLAN bridge for the wrong VLAN %d",
1196 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1202 check_conflicts(vsctl_ctx
, br_name
,
1203 xasprintf("cannot create a bridge named %s", br_name
));
1206 struct ovsrec_port
*port
;
1207 struct ovsrec_bridge
*br
;
1209 iface
= ovsrec_interface_insert(ctx
->txn
);
1210 ovsrec_interface_set_name(iface
, br_name
);
1211 ovsrec_interface_set_type(iface
, "internal");
1213 port
= ovsrec_port_insert(ctx
->txn
);
1214 ovsrec_port_set_name(port
, br_name
);
1215 ovsrec_port_set_interfaces(port
, &iface
, 1);
1217 br
= ovsrec_bridge_insert(ctx
->txn
);
1218 ovsrec_bridge_set_name(br
, br_name
);
1219 ovsrec_bridge_set_ports(br
, &port
, 1);
1221 ovs_insert_bridge(vsctl_ctx
->ovs
, br
);
1223 struct vsctl_bridge
*conflict
;
1224 struct vsctl_bridge
*parent
;
1225 struct ovsrec_port
*port
;
1226 struct ovsrec_bridge
*br
;
1229 parent
= find_bridge(vsctl_ctx
, parent_name
, false);
1230 if (parent
&& parent
->parent
) {
1231 ctl_fatal("cannot create bridge with fake bridge as parent");
1234 ctl_fatal("parent bridge %s does not exist", parent_name
);
1236 conflict
= find_vlan_bridge(parent
, vlan
);
1238 ctl_fatal("bridge %s already has a child VLAN bridge %s "
1239 "on VLAN %d", parent_name
, conflict
->name
, vlan
);
1241 br
= parent
->br_cfg
;
1243 iface
= ovsrec_interface_insert(ctx
->txn
);
1244 ovsrec_interface_set_name(iface
, br_name
);
1245 ovsrec_interface_set_type(iface
, "internal");
1247 port
= ovsrec_port_insert(ctx
->txn
);
1248 ovsrec_port_set_name(port
, br_name
);
1249 ovsrec_port_set_interfaces(port
, &iface
, 1);
1250 ovsrec_port_set_fake_bridge(port
, true);
1251 ovsrec_port_set_tag(port
, &tag
, 1);
1253 bridge_insert_port(br
, port
);
1256 post_db_reload_expect_iface(iface
);
1257 vsctl_context_invalidate_cache(ctx
);
1261 del_port(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*port
)
1263 struct vsctl_iface
*iface
, *next_iface
;
1265 bridge_delete_port((port
->bridge
->parent
1266 ? port
->bridge
->parent
->br_cfg
1267 : port
->bridge
->br_cfg
), port
->port_cfg
);
1269 LIST_FOR_EACH_SAFE (iface
, next_iface
, ifaces_node
, &port
->ifaces
) {
1270 del_cached_iface(vsctl_ctx
, iface
);
1272 del_cached_port(vsctl_ctx
, port
);
1276 del_bridge(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*br
)
1278 struct vsctl_bridge
*child
, *next_child
;
1279 struct vsctl_port
*port
, *next_port
;
1280 const struct ovsrec_flow_sample_collector_set
*fscset
, *next_fscset
;
1282 HMAP_FOR_EACH_SAFE (child
, next_child
, children_node
, &br
->children
) {
1283 del_bridge(vsctl_ctx
, child
);
1286 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &br
->ports
) {
1287 del_port(vsctl_ctx
, port
);
1290 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset
, next_fscset
,
1291 vsctl_ctx
->base
.idl
) {
1292 if (fscset
->bridge
== br
->br_cfg
) {
1293 ovsrec_flow_sample_collector_set_delete(fscset
);
1297 del_cached_bridge(vsctl_ctx
, br
);
1301 cmd_del_br(struct ctl_context
*ctx
)
1303 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1304 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1305 struct vsctl_bridge
*bridge
;
1307 vsctl_context_populate_cache(ctx
);
1308 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], must_exist
);
1310 del_bridge(vsctl_ctx
, bridge
);
1315 output_sorted(struct svec
*svec
, struct ds
*output
)
1321 SVEC_FOR_EACH (i
, name
, svec
) {
1322 ds_put_format(output
, "%s\n", name
);
1327 cmd_list_br(struct ctl_context
*ctx
)
1329 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1330 struct shash_node
*node
;
1331 struct svec bridges
;
1332 bool real
= shash_find(&ctx
->options
, "--real");
1333 bool fake
= shash_find(&ctx
->options
, "--fake");
1335 /* If neither fake nor real were requested, return both. */
1336 if (!real
&& !fake
) {
1340 vsctl_context_populate_cache(ctx
);
1342 svec_init(&bridges
);
1343 SHASH_FOR_EACH (node
, &vsctl_ctx
->bridges
) {
1344 struct vsctl_bridge
*br
= node
->data
;
1346 if (br
->parent
? fake
: real
) {
1347 svec_add(&bridges
, br
->name
);
1350 output_sorted(&bridges
, &ctx
->output
);
1351 svec_destroy(&bridges
);
1355 cmd_br_exists(struct ctl_context
*ctx
)
1357 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1359 vsctl_context_populate_cache(ctx
);
1360 if (!find_bridge(vsctl_ctx
, ctx
->argv
[1], false)) {
1366 set_external_id(struct smap
*old
, struct smap
*new,
1367 char *key
, char *value
)
1369 smap_clone(new, old
);
1372 smap_replace(new, key
, value
);
1374 smap_remove(new, key
);
1379 pre_cmd_br_set_external_id(struct ctl_context
*ctx
)
1382 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_external_ids
);
1383 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_external_ids
);
1387 cmd_br_set_external_id(struct ctl_context
*ctx
)
1389 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1390 struct vsctl_bridge
*bridge
;
1393 vsctl_context_populate_cache(ctx
);
1394 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1395 if (bridge
->br_cfg
) {
1397 set_external_id(&bridge
->br_cfg
->external_ids
, &new, ctx
->argv
[2],
1398 ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
);
1399 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1400 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, &new);
1402 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1403 struct vsctl_port
*port
= shash_find_data(&vsctl_ctx
->ports
,
1405 set_external_id(&port
->port_cfg
->external_ids
, &new,
1406 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
);
1407 ovsrec_port_verify_external_ids(port
->port_cfg
);
1408 ovsrec_port_set_external_ids(port
->port_cfg
, &new);
1415 get_external_id(struct smap
*smap
, const char *prefix
, const char *key
,
1419 char *prefix_key
= xasprintf("%s%s", prefix
, key
);
1420 const char *value
= smap_get(smap
, prefix_key
);
1423 ds_put_format(output
, "%s\n", value
);
1427 const struct smap_node
**sorted
= smap_sort(smap
);
1428 size_t prefix_len
= strlen(prefix
);
1431 for (i
= 0; i
< smap_count(smap
); i
++) {
1432 const struct smap_node
*node
= sorted
[i
];
1433 if (!strncmp(node
->key
, prefix
, prefix_len
)) {
1434 ds_put_format(output
, "%s=%s\n", node
->key
+ prefix_len
,
1443 pre_cmd_br_get_external_id(struct ctl_context
*ctx
)
1445 pre_cmd_br_set_external_id(ctx
);
1449 cmd_br_get_external_id(struct ctl_context
*ctx
)
1451 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1452 struct vsctl_bridge
*bridge
;
1454 vsctl_context_populate_cache(ctx
);
1456 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1457 if (bridge
->br_cfg
) {
1458 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1459 get_external_id(&bridge
->br_cfg
->external_ids
, "",
1460 ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1462 struct vsctl_port
*port
= shash_find_data(&vsctl_ctx
->ports
,
1464 ovsrec_port_verify_external_ids(port
->port_cfg
);
1465 get_external_id(&port
->port_cfg
->external_ids
, "fake-bridge-",
1466 ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1471 cmd_list_ports(struct ctl_context
*ctx
)
1473 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1474 struct vsctl_bridge
*br
;
1475 struct vsctl_port
*port
;
1478 vsctl_context_populate_cache(ctx
);
1479 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1480 ovsrec_bridge_verify_ports(br
->br_cfg
? br
->br_cfg
: br
->parent
->br_cfg
);
1483 LIST_FOR_EACH (port
, ports_node
, &br
->ports
) {
1484 if (strcmp(port
->port_cfg
->name
, br
->name
)) {
1485 svec_add(&ports
, port
->port_cfg
->name
);
1488 output_sorted(&ports
, &ctx
->output
);
1489 svec_destroy(&ports
);
1493 add_port(struct ctl_context
*ctx
,
1494 const char *br_name
, const char *port_name
,
1495 bool may_exist
, bool fake_iface
,
1496 char *iface_names
[], int n_ifaces
,
1497 char *settings
[], int n_settings
)
1499 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1500 struct vsctl_port
*vsctl_port
;
1501 struct vsctl_bridge
*bridge
;
1502 struct ovsrec_interface
**ifaces
;
1503 struct ovsrec_port
*port
;
1506 vsctl_context_populate_cache(ctx
);
1508 struct vsctl_port
*vsctl_port
;
1510 vsctl_port
= find_port(vsctl_ctx
, port_name
, false);
1512 struct svec want_names
, have_names
;
1514 svec_init(&want_names
);
1515 for (i
= 0; i
< n_ifaces
; i
++) {
1516 svec_add(&want_names
, iface_names
[i
]);
1518 svec_sort(&want_names
);
1520 svec_init(&have_names
);
1521 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1522 svec_add(&have_names
,
1523 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1525 svec_sort(&have_names
);
1527 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1528 char *command
= vsctl_context_to_string(ctx
);
1529 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1530 command
, port_name
, vsctl_port
->bridge
->name
);
1533 if (!svec_equal(&want_names
, &have_names
)) {
1534 char *have_names_string
= svec_join(&have_names
, ", ", "");
1535 char *command
= vsctl_context_to_string(ctx
);
1537 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1538 command
, port_name
, have_names_string
);
1541 svec_destroy(&want_names
);
1542 svec_destroy(&have_names
);
1547 check_conflicts(vsctl_ctx
, port_name
,
1548 xasprintf("cannot create a port named %s", port_name
));
1549 for (i
= 0; i
< n_ifaces
; i
++) {
1550 check_conflicts(vsctl_ctx
, iface_names
[i
],
1551 xasprintf("cannot create an interface named %s",
1554 bridge
= find_bridge(vsctl_ctx
, br_name
, true);
1556 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1557 for (i
= 0; i
< n_ifaces
; i
++) {
1558 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1559 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1560 post_db_reload_expect_iface(ifaces
[i
]);
1563 port
= ovsrec_port_insert(ctx
->txn
);
1564 ovsrec_port_set_name(port
, port_name
);
1565 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1566 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1568 if (bridge
->parent
) {
1569 int64_t tag
= bridge
->vlan
;
1570 ovsrec_port_set_tag(port
, &tag
, 1);
1573 for (i
= 0; i
< n_settings
; i
++) {
1574 ctl_set_column("Port", &port
->header_
, settings
[i
],
1578 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1579 : bridge
->br_cfg
), port
);
1581 vsctl_port
= add_port_to_cache(vsctl_ctx
, bridge
, port
);
1582 for (i
= 0; i
< n_ifaces
; i
++) {
1583 add_iface_to_cache(vsctl_ctx
, vsctl_port
, ifaces
[i
]);
1589 cmd_add_port(struct ctl_context
*ctx
)
1591 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1593 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1594 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1598 cmd_add_bond(struct ctl_context
*ctx
)
1600 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1601 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1605 n_ifaces
= ctx
->argc
- 3;
1606 for (i
= 3; i
< ctx
->argc
; i
++) {
1607 if (strchr(ctx
->argv
[i
], '=')) {
1613 ctl_fatal("add-bond requires at least 2 interfaces, but only "
1614 "%d were specified", n_ifaces
);
1617 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1618 &ctx
->argv
[3], n_ifaces
,
1619 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1623 cmd_del_port(struct ctl_context
*ctx
)
1625 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1626 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1627 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1628 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1629 struct vsctl_port
*port
;
1631 vsctl_context_populate_cache(ctx
);
1632 if (find_bridge(vsctl_ctx
, target
, false)) {
1634 ctl_fatal("cannot delete port %s because it is the local port "
1635 "for bridge %s (deleting this port requires deleting "
1636 "the entire bridge)", target
, target
);
1639 } else if (!with_iface
) {
1640 port
= find_port(vsctl_ctx
, target
, must_exist
);
1642 struct vsctl_iface
*iface
;
1644 port
= find_port(vsctl_ctx
, target
, false);
1646 iface
= find_iface(vsctl_ctx
, target
, false);
1651 if (must_exist
&& !port
) {
1652 ctl_fatal("no port or interface named %s", target
);
1657 if (ctx
->argc
== 3) {
1658 struct vsctl_bridge
*bridge
;
1660 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1661 if (port
->bridge
!= bridge
) {
1662 if (port
->bridge
->parent
== bridge
) {
1663 ctl_fatal("bridge %s does not have a port %s (although "
1664 "its parent bridge %s does)",
1665 ctx
->argv
[1], ctx
->argv
[2],
1666 bridge
->parent
->name
);
1668 ctl_fatal("bridge %s does not have a port %s",
1669 ctx
->argv
[1], ctx
->argv
[2]);
1674 del_port(vsctl_ctx
, port
);
1679 cmd_port_to_br(struct ctl_context
*ctx
)
1681 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1682 struct vsctl_port
*port
;
1684 vsctl_context_populate_cache(ctx
);
1686 port
= find_port(vsctl_ctx
, ctx
->argv
[1], true);
1687 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1691 cmd_br_to_vlan(struct ctl_context
*ctx
)
1693 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1694 struct vsctl_bridge
*bridge
;
1696 vsctl_context_populate_cache(ctx
);
1698 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1699 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1703 cmd_br_to_parent(struct ctl_context
*ctx
)
1705 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1706 struct vsctl_bridge
*bridge
;
1708 vsctl_context_populate_cache(ctx
);
1710 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1711 if (bridge
->parent
) {
1712 bridge
= bridge
->parent
;
1714 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1718 cmd_list_ifaces(struct ctl_context
*ctx
)
1720 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1721 struct vsctl_bridge
*br
;
1722 struct vsctl_port
*port
;
1725 vsctl_context_populate_cache(ctx
);
1727 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1728 verify_ports(vsctl_ctx
);
1731 LIST_FOR_EACH (port
, ports_node
, &br
->ports
) {
1732 struct vsctl_iface
*iface
;
1734 LIST_FOR_EACH (iface
, ifaces_node
, &port
->ifaces
) {
1735 if (strcmp(iface
->iface_cfg
->name
, br
->name
)) {
1736 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1740 output_sorted(&ifaces
, &ctx
->output
);
1741 svec_destroy(&ifaces
);
1745 cmd_iface_to_br(struct ctl_context
*ctx
)
1747 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1748 struct vsctl_iface
*iface
;
1750 vsctl_context_populate_cache(ctx
);
1752 iface
= find_iface(vsctl_ctx
, ctx
->argv
[1], true);
1753 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1757 verify_controllers(struct ovsrec_bridge
*bridge
)
1761 ovsrec_bridge_verify_controller(bridge
);
1762 for (i
= 0; i
< bridge
->n_controller
; i
++) {
1763 ovsrec_controller_verify_target(bridge
->controller
[i
]);
1768 pre_controller(struct ctl_context
*ctx
)
1772 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_controller_col_target
);
1776 cmd_get_controller(struct ctl_context
*ctx
)
1778 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1779 struct vsctl_bridge
*br
;
1780 struct svec targets
;
1783 vsctl_context_populate_cache(ctx
);
1785 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1789 verify_controllers(br
->br_cfg
);
1791 /* Print the targets in sorted order for reproducibility. */
1792 svec_init(&targets
);
1793 for (i
= 0; i
< br
->br_cfg
->n_controller
; i
++) {
1794 svec_add(&targets
, br
->br_cfg
->controller
[i
]->target
);
1797 svec_sort(&targets
);
1798 for (i
= 0; i
< targets
.n
; i
++) {
1799 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1801 svec_destroy(&targets
);
1805 delete_controllers(struct ovsrec_controller
**controllers
,
1806 size_t n_controllers
)
1810 for (i
= 0; i
< n_controllers
; i
++) {
1811 ovsrec_controller_delete(controllers
[i
]);
1816 cmd_del_controller(struct ctl_context
*ctx
)
1818 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1819 struct ovsrec_bridge
*br
;
1821 vsctl_context_populate_cache(ctx
);
1823 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true)->br_cfg
;
1824 verify_controllers(br
);
1826 if (br
->controller
) {
1827 delete_controllers(br
->controller
, br
->n_controller
);
1828 ovsrec_bridge_set_controller(br
, NULL
, 0);
1832 static struct ovsrec_controller
**
1833 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1835 struct ovsrec_controller
**controllers
;
1838 controllers
= xmalloc(n
* sizeof *controllers
);
1839 for (i
= 0; i
< n
; i
++) {
1840 if (vconn_verify_name(targets
[i
]) && pvconn_verify_name(targets
[i
])) {
1841 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
1843 controllers
[i
] = ovsrec_controller_insert(txn
);
1844 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1851 cmd_set_controller(struct ctl_context
*ctx
)
1853 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1854 struct ovsrec_controller
**controllers
;
1855 struct ovsrec_bridge
*br
;
1858 vsctl_context_populate_cache(ctx
);
1860 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true)->br_cfg
;
1861 verify_controllers(br
);
1863 delete_controllers(br
->controller
, br
->n_controller
);
1866 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1867 ovsrec_bridge_set_controller(br
, controllers
, n
);
1872 cmd_get_fail_mode(struct ctl_context
*ctx
)
1874 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1875 struct vsctl_bridge
*br
;
1876 const char *fail_mode
;
1878 vsctl_context_populate_cache(ctx
);
1879 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1884 ovsrec_bridge_verify_fail_mode(br
->br_cfg
);
1886 fail_mode
= br
->br_cfg
->fail_mode
;
1887 if (fail_mode
&& strlen(fail_mode
)) {
1888 ds_put_format(&ctx
->output
, "%s\n", fail_mode
);
1893 cmd_del_fail_mode(struct ctl_context
*ctx
)
1895 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1896 struct vsctl_bridge
*br
;
1898 vsctl_context_populate_cache(ctx
);
1900 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1902 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1906 cmd_set_fail_mode(struct ctl_context
*ctx
)
1908 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1909 struct vsctl_bridge
*br
;
1910 const char *fail_mode
= ctx
->argv
[2];
1912 vsctl_context_populate_cache(ctx
);
1914 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1916 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1917 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1920 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1924 verify_managers(const struct ovsrec_open_vswitch
*ovs
)
1928 ovsrec_open_vswitch_verify_manager_options(ovs
);
1930 for (i
= 0; i
< ovs
->n_manager_options
; ++i
) {
1931 const struct ovsrec_manager
*mgr
= ovs
->manager_options
[i
];
1933 ovsrec_manager_verify_target(mgr
);
1938 pre_manager(struct ctl_context
*ctx
)
1940 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1941 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_manager_col_target
);
1945 cmd_get_manager(struct ctl_context
*ctx
)
1947 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1948 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
1949 struct svec targets
;
1952 verify_managers(ovs
);
1954 /* Print the targets in sorted order for reproducibility. */
1955 svec_init(&targets
);
1957 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1958 svec_add(&targets
, ovs
->manager_options
[i
]->target
);
1961 svec_sort_unique(&targets
);
1962 for (i
= 0; i
< targets
.n
; i
++) {
1963 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1965 svec_destroy(&targets
);
1969 delete_managers(const struct ovsrec_open_vswitch
*ovs
)
1973 /* Delete Manager rows pointed to by 'manager_options' column. */
1974 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1975 ovsrec_manager_delete(ovs
->manager_options
[i
]);
1978 /* Delete 'Manager' row refs in 'manager_options' column. */
1979 ovsrec_open_vswitch_set_manager_options(ovs
, NULL
, 0);
1983 cmd_del_manager(struct ctl_context
*ctx
)
1985 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1986 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
1988 verify_managers(ovs
);
1989 delete_managers(ovs
);
1993 insert_managers(struct vsctl_context
*vsctl_ctx
, char *targets
[], size_t n
)
1995 struct ovsrec_manager
**managers
;
1998 /* Insert each manager in a new row in Manager table. */
1999 managers
= xmalloc(n
* sizeof *managers
);
2000 for (i
= 0; i
< n
; i
++) {
2001 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2002 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2004 managers
[i
] = ovsrec_manager_insert(vsctl_ctx
->base
.txn
);
2005 ovsrec_manager_set_target(managers
[i
], targets
[i
]);
2008 /* Store uuids of new Manager rows in 'manager_options' column. */
2009 ovsrec_open_vswitch_set_manager_options(vsctl_ctx
->ovs
, managers
, n
);
2014 cmd_set_manager(struct ctl_context
*ctx
)
2016 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2017 const size_t n
= ctx
->argc
- 1;
2019 verify_managers(vsctl_ctx
->ovs
);
2020 delete_managers(vsctl_ctx
->ovs
);
2021 insert_managers(vsctl_ctx
, &ctx
->argv
[1], n
);
2025 pre_cmd_get_ssl(struct ctl_context
*ctx
)
2027 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2029 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_private_key
);
2030 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_certificate
);
2031 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_ca_cert
);
2032 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_bootstrap_ca_cert
);
2036 cmd_get_ssl(struct ctl_context
*ctx
)
2038 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2039 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2041 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2043 ovsrec_ssl_verify_private_key(ssl
);
2044 ovsrec_ssl_verify_certificate(ssl
);
2045 ovsrec_ssl_verify_ca_cert(ssl
);
2046 ovsrec_ssl_verify_bootstrap_ca_cert(ssl
);
2048 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
2049 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
2050 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
2051 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
2052 ssl
->bootstrap_ca_cert
? "true" : "false");
2057 pre_cmd_del_ssl(struct ctl_context
*ctx
)
2059 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2063 cmd_del_ssl(struct ctl_context
*ctx
)
2065 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2066 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2069 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2070 ovsrec_ssl_delete(ssl
);
2071 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, NULL
);
2076 pre_cmd_set_ssl(struct ctl_context
*ctx
)
2078 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2082 cmd_set_ssl(struct ctl_context
*ctx
)
2084 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2085 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
2086 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2088 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2090 ovsrec_ssl_delete(ssl
);
2092 ssl
= ovsrec_ssl_insert(ctx
->txn
);
2094 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
2095 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
2096 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
2098 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
2100 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, ssl
);
2104 autoattach_insert_mapping(struct ovsrec_autoattach
*aa
,
2108 int64_t *key_mappings
, *value_mappings
;
2111 key_mappings
= xmalloc(sizeof *aa
->key_mappings
* (aa
->n_mappings
+ 1));
2112 value_mappings
= xmalloc(sizeof *aa
->value_mappings
* (aa
->n_mappings
+ 1));
2114 for (i
= 0; i
< aa
->n_mappings
; i
++) {
2115 key_mappings
[i
] = aa
->key_mappings
[i
];
2116 value_mappings
[i
] = aa
->value_mappings
[i
];
2118 key_mappings
[aa
->n_mappings
] = isid
;
2119 value_mappings
[aa
->n_mappings
] = vlan
;
2121 ovsrec_autoattach_set_mappings(aa
, key_mappings
, value_mappings
,
2122 aa
->n_mappings
+ 1);
2125 free(value_mappings
);
2129 cmd_add_aa_mapping(struct ctl_context
*ctx
)
2131 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2132 struct vsctl_bridge
*br
;
2136 isid
= strtoull(ctx
->argv
[2], &nptr
, 10);
2137 if (nptr
== ctx
->argv
[2] || nptr
== NULL
) {
2138 ctl_fatal("Invalid argument %s", ctx
->argv
[2]);
2142 vlan
= strtoull(ctx
->argv
[3], &nptr
, 10);
2143 if (nptr
== ctx
->argv
[3] || nptr
== NULL
) {
2144 ctl_fatal("Invalid argument %s", ctx
->argv
[3]);
2148 vsctl_context_populate_cache(ctx
);
2150 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2156 if (!br
->br_cfg
->auto_attach
) {
2157 struct ovsrec_autoattach
*aa
= ovsrec_autoattach_insert(ctx
->txn
);
2158 ovsrec_bridge_set_auto_attach(br
->br_cfg
, aa
);
2160 autoattach_insert_mapping(br
->br_cfg
->auto_attach
, isid
, vlan
);
2165 del_aa_mapping(struct ovsrec_autoattach
*aa
,
2169 int64_t *key_mappings
, *value_mappings
;
2172 key_mappings
= xmalloc(sizeof *aa
->key_mappings
* (aa
->n_mappings
));
2173 value_mappings
= xmalloc(sizeof *value_mappings
* (aa
->n_mappings
));
2175 for (i
= n
= 0; i
< aa
->n_mappings
; i
++) {
2176 if (aa
->key_mappings
[i
] != isid
&& aa
->value_mappings
[i
] != vlan
) {
2177 key_mappings
[n
] = aa
->key_mappings
[i
];
2178 value_mappings
[n
++] = aa
->value_mappings
[i
];
2182 ovsrec_autoattach_set_mappings(aa
, key_mappings
, value_mappings
, n
);
2185 free(value_mappings
);
2189 cmd_del_aa_mapping(struct ctl_context
*ctx
)
2191 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2192 struct vsctl_bridge
*br
;
2196 isid
= strtoull(ctx
->argv
[2], &nptr
, 10);
2197 if (nptr
== ctx
->argv
[2] || nptr
== NULL
) {
2198 ctl_fatal("Invalid argument %s", ctx
->argv
[2]);
2202 vlan
= strtoull(ctx
->argv
[3], &nptr
, 10);
2203 if (nptr
== ctx
->argv
[3] || nptr
== NULL
) {
2204 ctl_fatal("Invalid argument %s", ctx
->argv
[3]);
2208 vsctl_context_populate_cache(ctx
);
2210 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2215 if (br
->br_cfg
&& br
->br_cfg
->auto_attach
&&
2216 br
->br_cfg
->auto_attach
->key_mappings
&&
2217 br
->br_cfg
->auto_attach
->value_mappings
) {
2220 for (i
= 0; i
< br
->br_cfg
->auto_attach
->n_mappings
; i
++) {
2221 if (br
->br_cfg
->auto_attach
->key_mappings
[i
] == isid
&&
2222 br
->br_cfg
->auto_attach
->value_mappings
[i
] == vlan
) {
2223 del_aa_mapping(br
->br_cfg
->auto_attach
, isid
, vlan
);
2231 pre_aa_mapping(struct ctl_context
*ctx
)
2235 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_auto_attach
);
2236 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_autoattach_col_mappings
);
2240 verify_auto_attach(struct ovsrec_bridge
*bridge
)
2243 ovsrec_bridge_verify_auto_attach(bridge
);
2245 if (bridge
->auto_attach
) {
2246 ovsrec_autoattach_verify_mappings(bridge
->auto_attach
);
2252 cmd_get_aa_mapping(struct ctl_context
*ctx
)
2254 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2255 struct vsctl_bridge
*br
;
2257 vsctl_context_populate_cache(ctx
);
2259 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2264 verify_auto_attach(br
->br_cfg
);
2266 if (br
->br_cfg
&& br
->br_cfg
->auto_attach
&&
2267 br
->br_cfg
->auto_attach
->key_mappings
&&
2268 br
->br_cfg
->auto_attach
->value_mappings
) {
2271 for (i
= 0; i
< br
->br_cfg
->auto_attach
->n_mappings
; i
++) {
2272 ds_put_format(&ctx
->output
, "%"PRId64
" %"PRId64
"\n",
2273 br
->br_cfg
->auto_attach
->key_mappings
[i
],
2274 br
->br_cfg
->auto_attach
->value_mappings
[i
]);
2280 static const struct ctl_table_class tables
[OVSREC_N_TABLES
] = {
2281 [OVSREC_TABLE_BRIDGE
].row_ids
= {
2282 {&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, NULL
},
2283 {&ovsrec_table_flow_sample_collector_set
, NULL
,
2284 &ovsrec_flow_sample_collector_set_col_bridge
}},
2286 [OVSREC_TABLE_CONTROLLER
].row_ids
[0]
2287 = {&ovsrec_table_bridge
, &ovsrec_bridge_col_name
,
2288 &ovsrec_bridge_col_controller
},
2290 [OVSREC_TABLE_INTERFACE
].row_ids
[0]
2291 = {&ovsrec_table_interface
, &ovsrec_interface_col_name
, NULL
},
2293 [OVSREC_TABLE_MIRROR
].row_ids
[0]
2294 = {&ovsrec_table_mirror
, &ovsrec_mirror_col_name
, NULL
},
2296 [OVSREC_TABLE_MANAGER
].row_ids
[0]
2297 = {&ovsrec_table_manager
, &ovsrec_manager_col_target
, NULL
},
2299 [OVSREC_TABLE_NETFLOW
].row_ids
[0]
2300 = {&ovsrec_table_bridge
, &ovsrec_bridge_col_name
,
2301 &ovsrec_bridge_col_netflow
},
2303 [OVSREC_TABLE_PORT
].row_ids
[0]
2304 = {&ovsrec_table_port
, &ovsrec_port_col_name
, NULL
},
2306 [OVSREC_TABLE_QOS
].row_ids
[0]
2307 = {&ovsrec_table_port
, &ovsrec_port_col_name
, &ovsrec_port_col_qos
},
2309 [OVSREC_TABLE_SSL
].row_ids
[0]
2310 = {&ovsrec_table_open_vswitch
, NULL
, &ovsrec_open_vswitch_col_ssl
},
2312 [OVSREC_TABLE_SFLOW
].row_ids
[0]
2313 = {&ovsrec_table_bridge
, &ovsrec_bridge_col_name
,
2314 &ovsrec_bridge_col_sflow
},
2316 [OVSREC_TABLE_FLOW_TABLE
].row_ids
[0]
2317 = {&ovsrec_table_flow_table
, &ovsrec_flow_table_col_name
, NULL
},
2319 [OVSREC_TABLE_IPFIX
].row_ids
= {
2320 {&ovsrec_table_bridge
, &ovsrec_bridge_col_name
, &ovsrec_bridge_col_ipfix
},
2321 {&ovsrec_table_flow_sample_collector_set
, NULL
,
2322 &ovsrec_flow_sample_collector_set_col_ipfix
}},
2324 [OVSREC_TABLE_AUTOATTACH
].row_ids
[0]
2325 = {&ovsrec_table_bridge
, &ovsrec_bridge_col_name
,
2326 &ovsrec_bridge_col_auto_attach
},
2328 [OVSREC_TABLE_FLOW_SAMPLE_COLLECTOR_SET
].row_ids
[0]
2329 = {&ovsrec_table_flow_sample_collector_set
,
2330 &ovsrec_flow_sample_collector_set_col_id
,
2335 post_db_reload_check_init(void)
2337 n_neoteric_ifaces
= 0;
2341 post_db_reload_expect_iface(const struct ovsrec_interface
*iface
)
2343 if (n_neoteric_ifaces
>= allocated_neoteric_ifaces
) {
2344 neoteric_ifaces
= x2nrealloc(neoteric_ifaces
,
2345 &allocated_neoteric_ifaces
,
2346 sizeof *neoteric_ifaces
);
2348 neoteric_ifaces
[n_neoteric_ifaces
++] = iface
->header_
.uuid
;
2352 post_db_reload_do_checks(const struct vsctl_context
*vsctl_ctx
)
2354 bool print_error
= false;
2357 for (i
= 0; i
< n_neoteric_ifaces
; i
++) {
2358 const struct uuid
*uuid
;
2360 uuid
= ovsdb_idl_txn_get_insert_uuid(vsctl_ctx
->base
.txn
,
2361 &neoteric_ifaces
[i
]);
2363 const struct ovsrec_interface
*iface
;
2365 iface
= ovsrec_interface_get_for_uuid(vsctl_ctx
->base
.idl
, uuid
);
2366 if (iface
&& (!iface
->ofport
|| *iface
->ofport
== -1)) {
2367 if (iface
->error
&& *iface
->error
) {
2368 ovs_error(0, "Error detected while setting up '%s': %s. "
2369 "See ovs-vswitchd log for details.",
2370 iface
->name
, iface
->error
);
2372 ovs_error(0, "Error detected while setting up '%s'. "
2373 "See ovs-vswitchd log for details.",
2382 ovs_error(0, "The default log directory is \"%s\".", ovs_logdir());
2388 vsctl_context_init_command(struct vsctl_context
*vsctl_ctx
,
2389 struct ctl_command
*command
)
2391 ctl_context_init_command(&vsctl_ctx
->base
, command
);
2392 vsctl_ctx
->verified_ports
= false;
2396 vsctl_context_init(struct vsctl_context
*vsctl_ctx
,
2397 struct ctl_command
*command
, struct ovsdb_idl
*idl
,
2398 struct ovsdb_idl_txn
*txn
,
2399 const struct ovsrec_open_vswitch
*ovs
,
2400 struct ovsdb_symbol_table
*symtab
)
2402 ctl_context_init(&vsctl_ctx
->base
, command
, idl
, txn
, symtab
,
2403 vsctl_context_invalidate_cache
);
2405 vsctl_ctx
->verified_ports
= false;
2407 vsctl_ctx
->ovs
= ovs
;
2408 vsctl_ctx
->cache_valid
= false;
2412 vsctl_context_done_command(struct vsctl_context
*vsctl_ctx
,
2413 struct ctl_command
*command
)
2415 ctl_context_done_command(&vsctl_ctx
->base
, command
);
2419 vsctl_context_done(struct vsctl_context
*vsctl_ctx
,
2420 struct ctl_command
*command
)
2422 ctl_context_done(&vsctl_ctx
->base
, command
);
2426 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2427 struct ovsdb_idl
*idl
)
2429 struct ctl_command
*c
;
2431 ovsdb_idl_add_table(idl
, &ovsrec_table_open_vswitch
);
2432 if (wait_for_reload
) {
2433 ovsdb_idl_add_column(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
2435 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2436 if (c
->syntax
->prerequisites
) {
2437 struct vsctl_context vsctl_ctx
;
2439 ds_init(&c
->output
);
2442 vsctl_context_init(&vsctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2443 (c
->syntax
->prerequisites
)(&vsctl_ctx
.base
);
2444 vsctl_context_done(&vsctl_ctx
, c
);
2446 ovs_assert(!c
->output
.string
);
2447 ovs_assert(!c
->table
);
2453 vsctl_parent_process_info(void)
2459 parent_pid
= getppid();
2462 /* Retrive the command line of the parent process, except the init
2463 * process since /proc/0 does not exist. */
2468 procfile
= xasprintf("/proc/%d/cmdline", parent_pid
);
2470 f
= fopen(procfile
, "r");
2477 ds_put_cstr(&s
, "init");
2480 ds_put_format(&s
, " (pid %d)", parent_pid
);
2481 return ds_steal_cstr(&s
);
2488 do_vsctl(const char *args
, struct ctl_command
*commands
, size_t n_commands
,
2489 struct ovsdb_idl
*idl
)
2491 struct ovsdb_idl_txn
*txn
;
2492 const struct ovsrec_open_vswitch
*ovs
;
2493 enum ovsdb_idl_txn_status status
;
2494 struct ovsdb_symbol_table
*symtab
;
2495 struct vsctl_context vsctl_ctx
;
2496 struct ctl_command
*c
;
2497 struct shash_node
*node
;
2498 int64_t next_cfg
= 0;
2500 char *ppid_info
= NULL
;
2502 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2504 ovsdb_idl_txn_set_dry_run(txn
);
2507 ppid_info
= vsctl_parent_process_info();
2509 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl (invoked by %s): %s",
2513 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2516 ovs
= ovsrec_open_vswitch_first(idl
);
2518 /* XXX add verification that table is empty */
2519 ovs
= ovsrec_open_vswitch_insert(txn
);
2522 if (wait_for_reload
) {
2523 ovsdb_idl_txn_increment(txn
, &ovs
->header_
,
2524 &ovsrec_open_vswitch_col_next_cfg
, false);
2527 post_db_reload_check_init();
2528 symtab
= ovsdb_symbol_table_create();
2529 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2530 ds_init(&c
->output
);
2533 vsctl_context_init(&vsctl_ctx
, NULL
, idl
, txn
, ovs
, symtab
);
2534 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2535 vsctl_context_init_command(&vsctl_ctx
, c
);
2536 if (c
->syntax
->run
) {
2537 (c
->syntax
->run
)(&vsctl_ctx
.base
);
2539 vsctl_context_done_command(&vsctl_ctx
, c
);
2541 if (vsctl_ctx
.base
.try_again
) {
2542 vsctl_context_done(&vsctl_ctx
, NULL
);
2546 vsctl_context_done(&vsctl_ctx
, NULL
);
2548 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2549 struct ovsdb_symbol
*symbol
= node
->data
;
2550 if (!symbol
->created
) {
2551 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2552 "with \"-- --id=%s create ...\")",
2553 node
->name
, node
->name
);
2555 if (!symbol
->strong_ref
) {
2556 if (!symbol
->weak_ref
) {
2557 VLOG_WARN("row id \"%s\" was created but no reference to it "
2558 "was inserted, so it will not actually appear in "
2559 "the database", node
->name
);
2561 VLOG_WARN("row id \"%s\" was created but only a weak "
2562 "reference to it was inserted, so it will not "
2563 "actually appear in the database", node
->name
);
2568 status
= ovsdb_idl_txn_commit_block(txn
);
2569 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2570 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2572 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2573 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2574 if (c
->syntax
->postprocess
) {
2575 vsctl_context_init(&vsctl_ctx
, c
, idl
, txn
, ovs
, symtab
);
2576 (c
->syntax
->postprocess
)(&vsctl_ctx
.base
);
2577 vsctl_context_done(&vsctl_ctx
, c
);
2581 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2584 case TXN_UNCOMMITTED
:
2585 case TXN_INCOMPLETE
:
2589 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2590 ctl_fatal("transaction aborted");
2600 ctl_fatal("transaction error: %s", error
);
2602 case TXN_NOT_LOCKED
:
2603 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2604 ctl_fatal("database not locked");
2611 ovsdb_symbol_table_destroy(symtab
);
2613 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2614 struct ds
*ds
= &c
->output
;
2617 table_print(c
->table
, &table_style
);
2618 } else if (oneline
) {
2622 for (j
= 0; j
< ds
->length
; j
++) {
2623 int ch
= ds
->string
[j
];
2626 fputs("\\n", stdout
);
2630 fputs("\\\\", stdout
);
2639 fputs(ds_cstr(ds
), stdout
);
2641 ds_destroy(&c
->output
);
2642 table_destroy(c
->table
);
2645 shash_destroy_free_data(&c
->options
);
2649 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2650 /* Even, if --retry flag was not specified, ovs-vsctl still
2651 * has to retry to establish OVSDB connection, if wait_for_reload
2652 * was set. Otherwise, ovs-vsctl would end up waiting forever
2653 * until cur_cfg would be updated. */
2654 ovsdb_idl_enable_reconnect(idl
);
2657 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2658 if (ovs
->cur_cfg
>= next_cfg
) {
2659 post_db_reload_do_checks(&vsctl_ctx
);
2663 ovsdb_idl_wait(idl
);
2668 ovsdb_idl_txn_destroy(txn
);
2669 ovsdb_idl_destroy(idl
);
2674 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2675 * resources and return so that the caller can try again. */
2677 ovsdb_idl_txn_abort(txn
);
2678 ovsdb_idl_txn_destroy(txn
);
2681 ovsdb_symbol_table_destroy(symtab
);
2682 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2683 ds_destroy(&c
->output
);
2684 table_destroy(c
->table
);
2690 /* Frees the current transaction and the underlying IDL and then calls
2693 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2694 * for a clean memory leak report from valgrind in the normal case. That makes
2695 * it easier to notice real memory leaks. */
2697 vsctl_exit(int status
)
2700 ovsdb_idl_txn_abort(the_idl_txn
);
2701 ovsdb_idl_txn_destroy(the_idl_txn
);
2703 ovsdb_idl_destroy(the_idl
);
2708 * Developers who add new commands to the 'struct ctl_command_syntax' must
2709 * define the 'arguments' member of the struct. The following keywords are
2710 * available for composing the argument format:
2712 * TABLE RECORD BRIDGE PARENT PORT
2713 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
2714 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
2715 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
2716 * TARGET NEW-* (e.g. NEW-PORT)
2718 * For argument types not listed above, just uses 'ARG' as place holder.
2720 * Encloses the keyword with '[]' if it is optional. Appends '...' to
2721 * keyword or enclosed keyword to indicate that the argument can be specified
2725 static const struct ctl_command_syntax vsctl_commands
[] = {
2726 /* Open vSwitch commands. */
2727 {"init", 0, 0, "", NULL
, cmd_init
, NULL
, "", RW
},
2729 /* Bridge commands. */
2730 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info
,
2731 cmd_add_br
, NULL
, "--may-exist", RW
},
2732 {"del-br", 1, 1, "BRIDGE", pre_get_info
, cmd_del_br
,
2733 NULL
, "--if-exists", RW
},
2734 {"list-br", 0, 0, "", pre_get_info
, cmd_list_br
, NULL
, "--real,--fake",
2736 {"br-exists", 1, 1, "BRIDGE", pre_get_info
, cmd_br_exists
, NULL
, "", RO
},
2737 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info
, cmd_br_to_vlan
, NULL
, "",
2739 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info
, cmd_br_to_parent
, NULL
,
2741 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2742 pre_cmd_br_set_external_id
, cmd_br_set_external_id
, NULL
, "", RW
},
2743 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id
,
2744 cmd_br_get_external_id
, NULL
, "", RO
},
2746 /* Port commands. */
2747 {"list-ports", 1, 1, "BRIDGE", pre_get_info
, cmd_list_ports
, NULL
, "",
2749 {"add-port", 2, INT_MAX
, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2750 pre_get_info
, cmd_add_port
, NULL
, "--may-exist", RW
},
2751 {"add-bond", 4, INT_MAX
,
2752 "BRIDGE NEW-BOND-PORT SYSIFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info
,
2753 cmd_add_bond
, NULL
, "--may-exist,--fake-iface", RW
},
2754 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info
, cmd_del_port
, NULL
,
2755 "--if-exists,--with-iface", RW
},
2756 {"port-to-br", 1, 1, "PORT", pre_get_info
, cmd_port_to_br
, NULL
, "", RO
},
2758 /* Interface commands. */
2759 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info
, cmd_list_ifaces
, NULL
, "",
2761 {"iface-to-br", 1, 1, "IFACE", pre_get_info
, cmd_iface_to_br
, NULL
, "",
2764 /* Controller commands. */
2765 {"get-controller", 1, 1, "BRIDGE", pre_controller
, cmd_get_controller
,
2767 {"del-controller", 1, 1, "BRIDGE", pre_controller
, cmd_del_controller
,
2769 {"set-controller", 1, INT_MAX
, "BRIDGE TARGET...", pre_controller
,
2770 cmd_set_controller
, NULL
, "", RW
},
2771 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info
, cmd_get_fail_mode
, NULL
,
2773 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info
, cmd_del_fail_mode
, NULL
,
2775 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info
, cmd_set_fail_mode
,
2778 /* Manager commands. */
2779 {"get-manager", 0, 0, "", pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2780 {"del-manager", 0, 0, "", pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2781 {"set-manager", 1, INT_MAX
, "TARGET...", pre_manager
, cmd_set_manager
,
2785 {"get-ssl", 0, 0, "", pre_cmd_get_ssl
, cmd_get_ssl
, NULL
, "", RO
},
2786 {"del-ssl", 0, 0, "", pre_cmd_del_ssl
, cmd_del_ssl
, NULL
, "", RW
},
2787 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl
,
2788 cmd_set_ssl
, NULL
, "--bootstrap", RW
},
2790 /* Auto Attach commands. */
2791 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping
, cmd_add_aa_mapping
,
2793 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping
, cmd_del_aa_mapping
,
2795 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping
, cmd_get_aa_mapping
,
2798 /* Switch commands. */
2799 {"emer-reset", 0, 0, "", pre_cmd_emer_reset
, cmd_emer_reset
, NULL
, "", RW
},
2801 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2804 /* Registers vsctl and common db commands. */
2806 vsctl_cmd_init(void)
2808 ctl_init(ovsrec_table_classes
, tables
, cmd_show_tables
, vsctl_exit
);
2809 ctl_register_commands(vsctl_commands
);