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();
301 ctl_print_options(global_long_options
);
305 ovs_print_version(0, 0);
306 printf("DB Schema %s\n", ovsrec_get_db_version());
310 timeout
= strtoul(optarg
, NULL
, 10);
312 ctl_fatal("value %s on -t or --timeout is invalid",
322 TABLE_OPTION_HANDLERS(&table_style
)
324 STREAM_SSL_OPTION_HANDLERS
326 case OPT_PEER_CA_CERT
:
327 stream_ssl_set_peer_ca_cert_file(optarg
);
330 case OPT_BOOTSTRAP_CA_CERT
:
331 stream_ssl_set_ca_cert_file(optarg
, true);
344 db
= ctl_default_db();
347 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
348 free(CONST_CAST(char *, options
[i
].name
));
357 %s: ovs-vswitchd management utility\n\
358 usage: %s [OPTIONS] COMMAND [ARG...]\n\
360 Open vSwitch commands:\n\
361 init initialize database, if not yet initialized\n\
362 show print overview of database contents\n\
363 emer-reset reset configuration to clean state\n\
366 add-br BRIDGE create a new bridge named BRIDGE\n\
367 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
368 del-br BRIDGE delete BRIDGE and all of its ports\n\
369 list-br print the names of all the bridges\n\
370 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
371 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
372 br-to-parent BRIDGE print the parent of BRIDGE\n\
373 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
374 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
375 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
376 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
378 Port commands (a bond is considered to be a single port):\n\
379 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
380 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
381 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
382 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
383 port-to-br PORT print name of bridge that contains PORT\n\
385 Interface commands (a bond consists of multiple interfaces):\n\
386 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
387 iface-to-br IFACE print name of bridge that contains IFACE\n\
389 Controller commands:\n\
390 get-controller BRIDGE print the controllers for BRIDGE\n\
391 del-controller BRIDGE delete the controllers for BRIDGE\n\
392 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
393 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
394 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
395 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
398 get-manager print the managers\n\
399 del-manager delete the managers\n\
400 set-manager TARGET... set the list of managers to TARGET...\n\
403 get-ssl print the SSL configuration\n\
404 del-ssl delete the SSL configuration\n\
405 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
407 Auto Attach commands:\n\
408 add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
409 del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
410 get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
413 emer-reset reset switch to known good state\n\
418 --db=DATABASE connect to DATABASE\n\
420 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
421 --retry keep trying to connect to server forever\n\
422 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
423 --dry-run do not commit changes to database\n\
424 --oneline print exactly one line of output per command\n",
425 program_name
, program_name
, ctl_get_db_cmd_usage(), ctl_default_db());
429 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
430 stream_usage("database", true, true, false);
433 -h, --help display this help message\n\
434 -V, --version display version information\n");
439 /* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
440 struct vsctl_context
{
441 struct ctl_context base
;
443 /* Modifiable state. */
444 const struct ovsrec_open_vswitch
*ovs
;
447 /* A cache of the contents of the database.
449 * A command that needs to use any of this information must first call
450 * vsctl_context_populate_cache(). A command that changes anything that
451 * could invalidate the cache must either call
452 * vsctl_context_invalidate_cache() or manually update the cache to
453 * maintain its correctness. */
455 struct shash bridges
; /* Maps from bridge name to struct vsctl_bridge. */
456 struct shash ports
; /* Maps from port name to struct vsctl_port. */
457 struct shash ifaces
; /* Maps from port name to struct vsctl_iface. */
460 struct vsctl_bridge
{
461 struct ovsrec_bridge
*br_cfg
;
463 struct ovs_list ports
; /* Contains "struct vsctl_port"s. */
465 /* VLAN ("fake") bridge support.
467 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
469 struct hmap children
; /* VLAN bridges indexed by 'vlan'. */
470 struct hmap_node children_node
; /* Node in parent's 'children' hmap. */
471 struct vsctl_bridge
*parent
; /* Real bridge, or NULL. */
472 int vlan
; /* VLAN VID (0...4095), or 0. */
476 struct ovs_list ports_node
; /* In struct vsctl_bridge's 'ports' list. */
477 struct ovs_list ifaces
; /* Contains "struct vsctl_iface"s. */
478 struct ovsrec_port
*port_cfg
;
479 struct vsctl_bridge
*bridge
;
483 struct ovs_list ifaces_node
; /* In struct vsctl_port's 'ifaces' list. */
484 struct ovsrec_interface
*iface_cfg
;
485 struct vsctl_port
*port
;
488 /* Casts 'base' into 'struct vsctl_context'. */
489 static struct vsctl_context
*
490 vsctl_context_cast(struct ctl_context
*base
)
492 return CONTAINER_OF(base
, struct vsctl_context
, base
);
495 static struct vsctl_bridge
*find_vlan_bridge(struct vsctl_bridge
*parent
,
499 vsctl_context_to_string(const struct ctl_context
*ctx
)
501 const struct shash_node
*node
;
507 SHASH_FOR_EACH (node
, &ctx
->options
) {
508 svec_add(&words
, node
->name
);
510 for (i
= 0; i
< ctx
->argc
; i
++) {
511 svec_add(&words
, ctx
->argv
[i
]);
513 svec_terminate(&words
);
515 s
= process_escape_args(words
.names
);
517 svec_destroy(&words
);
523 verify_ports(struct vsctl_context
*vsctl_ctx
)
525 if (!vsctl_ctx
->verified_ports
) {
526 const struct ovsrec_bridge
*bridge
;
527 const struct ovsrec_port
*port
;
529 ovsrec_open_vswitch_verify_bridges(vsctl_ctx
->ovs
);
530 OVSREC_BRIDGE_FOR_EACH (bridge
, vsctl_ctx
->base
.idl
) {
531 ovsrec_bridge_verify_ports(bridge
);
533 OVSREC_PORT_FOR_EACH (port
, vsctl_ctx
->base
.idl
) {
534 ovsrec_port_verify_interfaces(port
);
537 vsctl_ctx
->verified_ports
= true;
541 static struct vsctl_bridge
*
542 add_bridge_to_cache(struct vsctl_context
*vsctl_ctx
,
543 struct ovsrec_bridge
*br_cfg
, const char *name
,
544 struct vsctl_bridge
*parent
, int vlan
)
546 struct vsctl_bridge
*br
= xmalloc(sizeof *br
);
548 br
->name
= xstrdup(name
);
549 ovs_list_init(&br
->ports
);
552 hmap_init(&br
->children
);
554 struct vsctl_bridge
*conflict
= find_vlan_bridge(parent
, vlan
);
556 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
557 "for VLAN %d, but only one is allowed",
558 parent
->name
, name
, conflict
->name
, vlan
);
560 hmap_insert(&parent
->children
, &br
->children_node
,
564 shash_add(&vsctl_ctx
->bridges
, br
->name
, br
);
569 ovs_delete_bridge(const struct ovsrec_open_vswitch
*ovs
,
570 struct ovsrec_bridge
*bridge
)
572 struct ovsrec_bridge
**bridges
;
575 bridges
= xmalloc(sizeof *ovs
->bridges
* ovs
->n_bridges
);
576 for (i
= n
= 0; i
< ovs
->n_bridges
; i
++) {
577 if (ovs
->bridges
[i
] != bridge
) {
578 bridges
[n
++] = ovs
->bridges
[i
];
581 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, n
);
586 del_cached_bridge(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*br
)
588 ovs_assert(ovs_list_is_empty(&br
->ports
));
589 ovs_assert(hmap_is_empty(&br
->children
));
591 hmap_remove(&br
->parent
->children
, &br
->children_node
);
594 ovsrec_bridge_delete(br
->br_cfg
);
595 ovs_delete_bridge(vsctl_ctx
->ovs
, br
->br_cfg
);
597 shash_find_and_delete(&vsctl_ctx
->bridges
, br
->name
);
598 hmap_destroy(&br
->children
);
604 port_is_fake_bridge(const struct ovsrec_port
*port_cfg
)
606 return (port_cfg
->fake_bridge
608 && *port_cfg
->tag
>= 0 && *port_cfg
->tag
<= 4095);
611 static struct vsctl_bridge
*
612 find_vlan_bridge(struct vsctl_bridge
*parent
, int vlan
)
614 struct vsctl_bridge
*child
;
616 HMAP_FOR_EACH_IN_BUCKET (child
, children_node
, hash_int(vlan
, 0),
618 if (child
->vlan
== vlan
) {
626 static struct vsctl_port
*
627 add_port_to_cache(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*parent
,
628 struct ovsrec_port
*port_cfg
)
630 struct vsctl_port
*port
;
633 && *port_cfg
->tag
>= 0 && *port_cfg
->tag
<= 4095) {
634 struct vsctl_bridge
*vlan_bridge
;
636 vlan_bridge
= find_vlan_bridge(parent
, *port_cfg
->tag
);
638 parent
= vlan_bridge
;
642 port
= xmalloc(sizeof *port
);
643 ovs_list_push_back(&parent
->ports
, &port
->ports_node
);
644 ovs_list_init(&port
->ifaces
);
645 port
->port_cfg
= port_cfg
;
646 port
->bridge
= parent
;
647 shash_add(&vsctl_ctx
->ports
, port_cfg
->name
, port
);
653 del_cached_port(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*port
)
655 ovs_assert(ovs_list_is_empty(&port
->ifaces
));
656 ovs_list_remove(&port
->ports_node
);
657 shash_find_and_delete(&vsctl_ctx
->ports
, port
->port_cfg
->name
);
658 ovsrec_port_delete(port
->port_cfg
);
662 static struct vsctl_iface
*
663 add_iface_to_cache(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*parent
,
664 struct ovsrec_interface
*iface_cfg
)
666 struct vsctl_iface
*iface
;
668 iface
= xmalloc(sizeof *iface
);
669 ovs_list_push_back(&parent
->ifaces
, &iface
->ifaces_node
);
670 iface
->iface_cfg
= iface_cfg
;
671 iface
->port
= parent
;
672 shash_add(&vsctl_ctx
->ifaces
, iface_cfg
->name
, iface
);
678 del_cached_iface(struct vsctl_context
*vsctl_ctx
, struct vsctl_iface
*iface
)
680 ovs_list_remove(&iface
->ifaces_node
);
681 shash_find_and_delete(&vsctl_ctx
->ifaces
, iface
->iface_cfg
->name
);
682 ovsrec_interface_delete(iface
->iface_cfg
);
687 vsctl_context_invalidate_cache(struct ctl_context
*ctx
)
689 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
690 struct shash_node
*node
;
692 if (!vsctl_ctx
->cache_valid
) {
695 vsctl_ctx
->cache_valid
= false;
697 SHASH_FOR_EACH (node
, &vsctl_ctx
->bridges
) {
698 struct vsctl_bridge
*bridge
= node
->data
;
699 hmap_destroy(&bridge
->children
);
703 shash_destroy(&vsctl_ctx
->bridges
);
705 shash_destroy_free_data(&vsctl_ctx
->ports
);
706 shash_destroy_free_data(&vsctl_ctx
->ifaces
);
710 pre_get_info(struct ctl_context
*ctx
)
712 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_bridges
);
714 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_name
);
715 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
716 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
717 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ports
);
719 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_name
);
720 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_fake_bridge
);
721 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_tag
);
722 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_interfaces
);
724 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_name
);
726 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_ofport
);
727 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_interface_col_error
);
731 vsctl_context_populate_cache(struct ctl_context
*ctx
)
733 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
734 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
735 struct sset bridges
, ports
;
738 if (vsctl_ctx
->cache_valid
) {
739 /* Cache is already populated. */
742 vsctl_ctx
->cache_valid
= true;
743 shash_init(&vsctl_ctx
->bridges
);
744 shash_init(&vsctl_ctx
->ports
);
745 shash_init(&vsctl_ctx
->ifaces
);
749 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
750 struct ovsrec_bridge
*br_cfg
= ovs
->bridges
[i
];
751 struct vsctl_bridge
*br
;
754 if (!sset_add(&bridges
, br_cfg
->name
)) {
755 VLOG_WARN("%s: database contains duplicate bridge name",
759 br
= add_bridge_to_cache(vsctl_ctx
, br_cfg
, br_cfg
->name
, NULL
, 0);
761 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
762 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
764 if (!sset_add(&ports
, port_cfg
->name
)) {
765 /* Duplicate port name. (We will warn about that later.) */
769 if (port_is_fake_bridge(port_cfg
)
770 && sset_add(&bridges
, port_cfg
->name
)) {
771 add_bridge_to_cache(vsctl_ctx
, NULL
, port_cfg
->name
, br
,
776 sset_destroy(&bridges
);
777 sset_destroy(&ports
);
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
)) {
788 br
= shash_find_data(&vsctl_ctx
->bridges
, br_cfg
->name
);
789 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
790 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
791 struct vsctl_port
*port
;
794 port
= shash_find_data(&vsctl_ctx
->ports
, port_cfg
->name
);
796 if (port_cfg
== port
->port_cfg
) {
797 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
798 port_cfg
->name
, br
->name
, port
->bridge
->name
);
800 /* Log as an error because this violates the database's
801 * uniqueness constraints, so the database server shouldn't
802 * have allowed it. */
803 VLOG_ERR("%s: database contains duplicate port name",
809 if (port_is_fake_bridge(port_cfg
)
810 && !sset_add(&bridges
, port_cfg
->name
)) {
814 port
= add_port_to_cache(vsctl_ctx
, br
, port_cfg
);
815 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
816 struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[k
];
817 struct vsctl_iface
*iface
;
819 iface
= shash_find_data(&vsctl_ctx
->ifaces
, iface_cfg
->name
);
821 if (iface_cfg
== iface
->iface_cfg
) {
822 VLOG_WARN("%s: interface is in multiple ports "
825 iface
->port
->port_cfg
->name
,
826 port
->port_cfg
->name
);
828 /* Log as an error because this violates the database's
829 * uniqueness constraints, so the database server
830 * shouldn't have allowed it. */
831 VLOG_ERR("%s: database contains duplicate interface "
832 "name", iface_cfg
->name
);
837 add_iface_to_cache(vsctl_ctx
, port
, iface_cfg
);
841 sset_destroy(&bridges
);
845 check_conflicts(struct vsctl_context
*vsctl_ctx
, const char *name
,
848 struct vsctl_iface
*iface
;
849 struct vsctl_port
*port
;
851 verify_ports(vsctl_ctx
);
853 if (shash_find(&vsctl_ctx
->bridges
, name
)) {
854 ctl_fatal("%s because a bridge named %s already exists",
858 port
= shash_find_data(&vsctl_ctx
->ports
, name
);
860 ctl_fatal("%s because a port named %s already exists on "
861 "bridge %s", msg
, name
, port
->bridge
->name
);
864 iface
= shash_find_data(&vsctl_ctx
->ifaces
, name
);
866 ctl_fatal("%s because an interface named %s already exists "
867 "on bridge %s", msg
, name
, iface
->port
->bridge
->name
);
873 static struct vsctl_bridge
*
874 find_bridge(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
876 struct vsctl_bridge
*br
;
878 ovs_assert(vsctl_ctx
->cache_valid
);
880 br
= shash_find_data(&vsctl_ctx
->bridges
, name
);
881 if (must_exist
&& !br
) {
882 ctl_fatal("no bridge named %s", name
);
884 ovsrec_open_vswitch_verify_bridges(vsctl_ctx
->ovs
);
888 static struct vsctl_bridge
*
889 find_real_bridge(struct vsctl_context
*vsctl_ctx
,
890 const char *name
, bool must_exist
)
892 struct vsctl_bridge
*br
= find_bridge(vsctl_ctx
, name
, must_exist
);
893 if (br
&& br
->parent
) {
894 ctl_fatal("%s is a fake bridge", name
);
899 static struct vsctl_port
*
900 find_port(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
902 struct vsctl_port
*port
;
904 ovs_assert(vsctl_ctx
->cache_valid
);
906 port
= shash_find_data(&vsctl_ctx
->ports
, name
);
907 if (port
&& !strcmp(name
, port
->bridge
->name
)) {
910 if (must_exist
&& !port
) {
911 ctl_fatal("no port named %s", name
);
913 verify_ports(vsctl_ctx
);
917 static struct vsctl_iface
*
918 find_iface(struct vsctl_context
*vsctl_ctx
, const char *name
, bool must_exist
)
920 struct vsctl_iface
*iface
;
922 ovs_assert(vsctl_ctx
->cache_valid
);
924 iface
= shash_find_data(&vsctl_ctx
->ifaces
, name
);
925 if (iface
&& !strcmp(name
, iface
->port
->bridge
->name
)) {
928 if (must_exist
&& !iface
) {
929 ctl_fatal("no interface named %s", name
);
931 verify_ports(vsctl_ctx
);
936 bridge_insert_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
938 struct ovsrec_port
**ports
;
941 ports
= xmalloc(sizeof *br
->ports
* (br
->n_ports
+ 1));
942 for (i
= 0; i
< br
->n_ports
; i
++) {
943 ports
[i
] = br
->ports
[i
];
945 ports
[br
->n_ports
] = port
;
946 ovsrec_bridge_set_ports(br
, ports
, br
->n_ports
+ 1);
951 bridge_delete_port(struct ovsrec_bridge
*br
, struct ovsrec_port
*port
)
953 struct ovsrec_port
**ports
;
956 ports
= xmalloc(sizeof *br
->ports
* br
->n_ports
);
957 for (i
= n
= 0; i
< br
->n_ports
; i
++) {
958 if (br
->ports
[i
] != port
) {
959 ports
[n
++] = br
->ports
[i
];
962 ovsrec_bridge_set_ports(br
, ports
, n
);
967 ovs_insert_bridge(const struct ovsrec_open_vswitch
*ovs
,
968 struct ovsrec_bridge
*bridge
)
970 struct ovsrec_bridge
**bridges
;
973 bridges
= xmalloc(sizeof *ovs
->bridges
* (ovs
->n_bridges
+ 1));
974 for (i
= 0; i
< ovs
->n_bridges
; i
++) {
975 bridges
[i
] = ovs
->bridges
[i
];
977 bridges
[ovs
->n_bridges
] = bridge
;
978 ovsrec_open_vswitch_set_bridges(ovs
, bridges
, ovs
->n_bridges
+ 1);
983 cmd_init(struct ctl_context
*ctx OVS_UNUSED
)
987 static struct cmd_show_table cmd_show_tables
[] = {
988 {&ovsrec_table_open_vswitch
,
990 {&ovsrec_open_vswitch_col_manager_options
,
991 &ovsrec_open_vswitch_col_bridges
,
992 &ovsrec_open_vswitch_col_ovs_version
},
996 {&ovsrec_table_bridge
,
997 &ovsrec_bridge_col_name
,
998 {&ovsrec_bridge_col_controller
,
999 &ovsrec_bridge_col_fail_mode
,
1000 &ovsrec_bridge_col_ports
},
1004 {&ovsrec_table_port
,
1005 &ovsrec_port_col_name
,
1006 {&ovsrec_port_col_tag
,
1007 &ovsrec_port_col_trunks
,
1008 &ovsrec_port_col_interfaces
},
1012 {&ovsrec_table_interface
,
1013 &ovsrec_interface_col_name
,
1014 {&ovsrec_interface_col_type
,
1015 &ovsrec_interface_col_options
,
1016 &ovsrec_interface_col_error
},
1020 {&ovsrec_table_controller
,
1021 &ovsrec_controller_col_target
,
1022 {&ovsrec_controller_col_is_connected
,
1028 {&ovsrec_table_manager
,
1029 &ovsrec_manager_col_target
,
1030 {&ovsrec_manager_col_is_connected
,
1036 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
1040 pre_cmd_emer_reset(struct ctl_context
*ctx
)
1042 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1043 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
1045 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_controller
);
1046 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_fail_mode
);
1047 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_mirrors
);
1048 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_netflow
);
1049 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_sflow
);
1050 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_ipfix
);
1051 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_flood_vlans
);
1052 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_other_config
);
1054 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_other_config
);
1056 ovsdb_idl_add_column(ctx
->idl
,
1057 &ovsrec_interface_col_ingress_policing_rate
);
1058 ovsdb_idl_add_column(ctx
->idl
,
1059 &ovsrec_interface_col_ingress_policing_burst
);
1063 cmd_emer_reset(struct ctl_context
*ctx
)
1065 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1066 const struct ovsdb_idl
*idl
= ctx
->idl
;
1067 const struct ovsrec_bridge
*br
;
1068 const struct ovsrec_port
*port
;
1069 const struct ovsrec_interface
*iface
;
1070 const struct ovsrec_mirror
*mirror
, *next_mirror
;
1071 const struct ovsrec_controller
*ctrl
, *next_ctrl
;
1072 const struct ovsrec_manager
*mgr
, *next_mgr
;
1073 const struct ovsrec_netflow
*nf
, *next_nf
;
1074 const struct ovsrec_ssl
*ssl
, *next_ssl
;
1075 const struct ovsrec_sflow
*sflow
, *next_sflow
;
1076 const struct ovsrec_ipfix
*ipfix
, *next_ipfix
;
1077 const struct ovsrec_flow_sample_collector_set
*fscset
, *next_fscset
;
1079 /* Reset the Open_vSwitch table. */
1080 ovsrec_open_vswitch_set_manager_options(vsctl_ctx
->ovs
, NULL
, 0);
1081 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, NULL
);
1083 OVSREC_BRIDGE_FOR_EACH (br
, idl
) {
1086 ovsrec_bridge_set_controller(br
, NULL
, 0);
1087 ovsrec_bridge_set_fail_mode(br
, NULL
);
1088 ovsrec_bridge_set_mirrors(br
, NULL
, 0);
1089 ovsrec_bridge_set_netflow(br
, NULL
);
1090 ovsrec_bridge_set_sflow(br
, NULL
);
1091 ovsrec_bridge_set_ipfix(br
, NULL
);
1092 ovsrec_bridge_set_flood_vlans(br
, NULL
, 0);
1094 /* We only want to save the "hwaddr" key from other_config. */
1095 hwaddr
= smap_get(&br
->other_config
, "hwaddr");
1097 const struct smap smap
= SMAP_CONST1(&smap
, "hwaddr", hwaddr
);
1098 ovsrec_bridge_set_other_config(br
, &smap
);
1100 ovsrec_bridge_set_other_config(br
, NULL
);
1104 OVSREC_PORT_FOR_EACH (port
, idl
) {
1105 ovsrec_port_set_other_config(port
, NULL
);
1108 OVSREC_INTERFACE_FOR_EACH (iface
, idl
) {
1109 /* xxx What do we do about gre/patch devices created by mgr? */
1111 ovsrec_interface_set_ingress_policing_rate(iface
, 0);
1112 ovsrec_interface_set_ingress_policing_burst(iface
, 0);
1115 OVSREC_MIRROR_FOR_EACH_SAFE (mirror
, next_mirror
, idl
) {
1116 ovsrec_mirror_delete(mirror
);
1119 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl
, next_ctrl
, idl
) {
1120 ovsrec_controller_delete(ctrl
);
1123 OVSREC_MANAGER_FOR_EACH_SAFE (mgr
, next_mgr
, idl
) {
1124 ovsrec_manager_delete(mgr
);
1127 OVSREC_NETFLOW_FOR_EACH_SAFE (nf
, next_nf
, idl
) {
1128 ovsrec_netflow_delete(nf
);
1131 OVSREC_SSL_FOR_EACH_SAFE (ssl
, next_ssl
, idl
) {
1132 ovsrec_ssl_delete(ssl
);
1135 OVSREC_SFLOW_FOR_EACH_SAFE (sflow
, next_sflow
, idl
) {
1136 ovsrec_sflow_delete(sflow
);
1139 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix
, next_ipfix
, idl
) {
1140 ovsrec_ipfix_delete(ipfix
);
1143 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset
, next_fscset
, idl
) {
1144 ovsrec_flow_sample_collector_set_delete(fscset
);
1147 vsctl_context_invalidate_cache(ctx
);
1151 cmd_add_br(struct ctl_context
*ctx
)
1153 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1154 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1155 const char *br_name
, *parent_name
;
1156 struct ovsrec_interface
*iface
;
1159 br_name
= ctx
->argv
[1];
1161 ctl_fatal("bridge name must not be empty string");
1163 if (ctx
->argc
== 2) {
1166 } else if (ctx
->argc
== 4) {
1167 parent_name
= ctx
->argv
[2];
1168 vlan
= atoi(ctx
->argv
[3]);
1169 if (vlan
< 0 || vlan
> 4095) {
1170 ctl_fatal("%s: vlan must be between 0 and 4095", ctx
->argv
[0]);
1173 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1177 vsctl_context_populate_cache(ctx
);
1179 struct vsctl_bridge
*br
;
1181 br
= find_bridge(vsctl_ctx
, br_name
, false);
1185 ctl_fatal("\"--may-exist add-br %s\" but %s is "
1186 "a VLAN bridge for VLAN %d",
1187 br_name
, br_name
, br
->vlan
);
1191 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1192 "is not a VLAN bridge",
1193 br_name
, parent_name
, vlan
, br_name
);
1194 } else if (strcmp(br
->parent
->name
, parent_name
)) {
1195 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1196 "has the wrong parent %s",
1197 br_name
, parent_name
, vlan
,
1198 br_name
, br
->parent
->name
);
1199 } else if (br
->vlan
!= vlan
) {
1200 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1201 "is a VLAN bridge for the wrong VLAN %d",
1202 br_name
, parent_name
, vlan
, br_name
, br
->vlan
);
1208 check_conflicts(vsctl_ctx
, br_name
,
1209 xasprintf("cannot create a bridge named %s", br_name
));
1212 struct ovsrec_port
*port
;
1213 struct ovsrec_bridge
*br
;
1215 iface
= ovsrec_interface_insert(ctx
->txn
);
1216 ovsrec_interface_set_name(iface
, br_name
);
1217 ovsrec_interface_set_type(iface
, "internal");
1219 port
= ovsrec_port_insert(ctx
->txn
);
1220 ovsrec_port_set_name(port
, br_name
);
1221 ovsrec_port_set_interfaces(port
, &iface
, 1);
1223 br
= ovsrec_bridge_insert(ctx
->txn
);
1224 ovsrec_bridge_set_name(br
, br_name
);
1225 ovsrec_bridge_set_ports(br
, &port
, 1);
1227 ovs_insert_bridge(vsctl_ctx
->ovs
, br
);
1229 struct vsctl_bridge
*conflict
;
1230 struct vsctl_bridge
*parent
;
1231 struct ovsrec_port
*port
;
1232 struct ovsrec_bridge
*br
;
1235 parent
= find_bridge(vsctl_ctx
, parent_name
, false);
1236 if (parent
&& parent
->parent
) {
1237 ctl_fatal("cannot create bridge with fake bridge as parent");
1240 ctl_fatal("parent bridge %s does not exist", parent_name
);
1242 conflict
= find_vlan_bridge(parent
, vlan
);
1244 ctl_fatal("bridge %s already has a child VLAN bridge %s "
1245 "on VLAN %d", parent_name
, conflict
->name
, vlan
);
1247 br
= parent
->br_cfg
;
1249 iface
= ovsrec_interface_insert(ctx
->txn
);
1250 ovsrec_interface_set_name(iface
, br_name
);
1251 ovsrec_interface_set_type(iface
, "internal");
1253 port
= ovsrec_port_insert(ctx
->txn
);
1254 ovsrec_port_set_name(port
, br_name
);
1255 ovsrec_port_set_interfaces(port
, &iface
, 1);
1256 ovsrec_port_set_fake_bridge(port
, true);
1257 ovsrec_port_set_tag(port
, &tag
, 1);
1259 bridge_insert_port(br
, port
);
1262 post_db_reload_expect_iface(iface
);
1263 vsctl_context_invalidate_cache(ctx
);
1267 del_port(struct vsctl_context
*vsctl_ctx
, struct vsctl_port
*port
)
1269 struct vsctl_iface
*iface
, *next_iface
;
1271 bridge_delete_port((port
->bridge
->parent
1272 ? port
->bridge
->parent
->br_cfg
1273 : port
->bridge
->br_cfg
), port
->port_cfg
);
1275 LIST_FOR_EACH_SAFE (iface
, next_iface
, ifaces_node
, &port
->ifaces
) {
1276 del_cached_iface(vsctl_ctx
, iface
);
1278 del_cached_port(vsctl_ctx
, port
);
1282 del_bridge(struct vsctl_context
*vsctl_ctx
, struct vsctl_bridge
*br
)
1284 struct vsctl_bridge
*child
, *next_child
;
1285 struct vsctl_port
*port
, *next_port
;
1286 const struct ovsrec_flow_sample_collector_set
*fscset
, *next_fscset
;
1288 HMAP_FOR_EACH_SAFE (child
, next_child
, children_node
, &br
->children
) {
1289 del_bridge(vsctl_ctx
, child
);
1292 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &br
->ports
) {
1293 del_port(vsctl_ctx
, port
);
1296 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset
, next_fscset
,
1297 vsctl_ctx
->base
.idl
) {
1298 if (fscset
->bridge
== br
->br_cfg
) {
1299 ovsrec_flow_sample_collector_set_delete(fscset
);
1303 del_cached_bridge(vsctl_ctx
, br
);
1307 cmd_del_br(struct ctl_context
*ctx
)
1309 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1310 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1311 struct vsctl_bridge
*bridge
;
1313 vsctl_context_populate_cache(ctx
);
1314 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], must_exist
);
1316 del_bridge(vsctl_ctx
, bridge
);
1321 output_sorted(struct svec
*svec
, struct ds
*output
)
1327 SVEC_FOR_EACH (i
, name
, svec
) {
1328 ds_put_format(output
, "%s\n", name
);
1333 cmd_list_br(struct ctl_context
*ctx
)
1335 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1336 struct shash_node
*node
;
1337 struct svec bridges
;
1338 bool real
= shash_find(&ctx
->options
, "--real");
1339 bool fake
= shash_find(&ctx
->options
, "--fake");
1341 /* If neither fake nor real were requested, return both. */
1342 if (!real
&& !fake
) {
1346 vsctl_context_populate_cache(ctx
);
1348 svec_init(&bridges
);
1349 SHASH_FOR_EACH (node
, &vsctl_ctx
->bridges
) {
1350 struct vsctl_bridge
*br
= node
->data
;
1352 if (br
->parent
? fake
: real
) {
1353 svec_add(&bridges
, br
->name
);
1356 output_sorted(&bridges
, &ctx
->output
);
1357 svec_destroy(&bridges
);
1361 cmd_br_exists(struct ctl_context
*ctx
)
1363 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1365 vsctl_context_populate_cache(ctx
);
1366 if (!find_bridge(vsctl_ctx
, ctx
->argv
[1], false)) {
1372 set_external_id(struct smap
*old
, struct smap
*new,
1373 char *key
, char *value
)
1375 smap_clone(new, old
);
1378 smap_replace(new, key
, value
);
1380 smap_remove(new, key
);
1385 pre_cmd_br_set_external_id(struct ctl_context
*ctx
)
1388 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_external_ids
);
1389 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_port_col_external_ids
);
1393 cmd_br_set_external_id(struct ctl_context
*ctx
)
1395 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1396 struct vsctl_bridge
*bridge
;
1399 vsctl_context_populate_cache(ctx
);
1400 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1401 if (bridge
->br_cfg
) {
1403 set_external_id(&bridge
->br_cfg
->external_ids
, &new, ctx
->argv
[2],
1404 ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
);
1405 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1406 ovsrec_bridge_set_external_ids(bridge
->br_cfg
, &new);
1408 char *key
= xasprintf("fake-bridge-%s", ctx
->argv
[2]);
1409 struct vsctl_port
*port
= shash_find_data(&vsctl_ctx
->ports
,
1411 set_external_id(&port
->port_cfg
->external_ids
, &new,
1412 key
, ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
);
1413 ovsrec_port_verify_external_ids(port
->port_cfg
);
1414 ovsrec_port_set_external_ids(port
->port_cfg
, &new);
1421 get_external_id(struct smap
*smap
, const char *prefix
, const char *key
,
1425 char *prefix_key
= xasprintf("%s%s", prefix
, key
);
1426 const char *value
= smap_get(smap
, prefix_key
);
1429 ds_put_format(output
, "%s\n", value
);
1433 const struct smap_node
**sorted
= smap_sort(smap
);
1434 size_t prefix_len
= strlen(prefix
);
1437 for (i
= 0; i
< smap_count(smap
); i
++) {
1438 const struct smap_node
*node
= sorted
[i
];
1439 if (!strncmp(node
->key
, prefix
, prefix_len
)) {
1440 ds_put_format(output
, "%s=%s\n", node
->key
+ prefix_len
,
1449 pre_cmd_br_get_external_id(struct ctl_context
*ctx
)
1451 pre_cmd_br_set_external_id(ctx
);
1455 cmd_br_get_external_id(struct ctl_context
*ctx
)
1457 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1458 struct vsctl_bridge
*bridge
;
1460 vsctl_context_populate_cache(ctx
);
1462 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1463 if (bridge
->br_cfg
) {
1464 ovsrec_bridge_verify_external_ids(bridge
->br_cfg
);
1465 get_external_id(&bridge
->br_cfg
->external_ids
, "",
1466 ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1468 struct vsctl_port
*port
= shash_find_data(&vsctl_ctx
->ports
,
1470 ovsrec_port_verify_external_ids(port
->port_cfg
);
1471 get_external_id(&port
->port_cfg
->external_ids
, "fake-bridge-",
1472 ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
, &ctx
->output
);
1477 cmd_list_ports(struct ctl_context
*ctx
)
1479 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1480 struct vsctl_bridge
*br
;
1481 struct vsctl_port
*port
;
1484 vsctl_context_populate_cache(ctx
);
1485 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1486 ovsrec_bridge_verify_ports(br
->br_cfg
? br
->br_cfg
: br
->parent
->br_cfg
);
1489 LIST_FOR_EACH (port
, ports_node
, &br
->ports
) {
1490 if (strcmp(port
->port_cfg
->name
, br
->name
)) {
1491 svec_add(&ports
, port
->port_cfg
->name
);
1494 output_sorted(&ports
, &ctx
->output
);
1495 svec_destroy(&ports
);
1499 add_port(struct ctl_context
*ctx
,
1500 const char *br_name
, const char *port_name
,
1501 bool may_exist
, bool fake_iface
,
1502 char *iface_names
[], int n_ifaces
,
1503 char *settings
[], int n_settings
)
1505 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1506 struct vsctl_bridge
*bridge
;
1507 struct ovsrec_interface
**ifaces
;
1508 struct ovsrec_port
*port
;
1511 if (!port_name
[0]) {
1512 ctl_fatal("port name must not be empty string");
1514 for (i
= 0; i
< n_ifaces
; i
++) {
1515 if (!iface_names
[i
][0]) {
1516 ctl_fatal("interface name must not be empty string");
1520 vsctl_context_populate_cache(ctx
);
1522 struct vsctl_port
*vsctl_port
;
1524 vsctl_port
= find_port(vsctl_ctx
, port_name
, false);
1526 struct svec want_names
, have_names
;
1528 svec_init(&want_names
);
1529 for (i
= 0; i
< n_ifaces
; i
++) {
1530 svec_add(&want_names
, iface_names
[i
]);
1532 svec_sort(&want_names
);
1534 svec_init(&have_names
);
1535 for (i
= 0; i
< vsctl_port
->port_cfg
->n_interfaces
; i
++) {
1536 svec_add(&have_names
,
1537 vsctl_port
->port_cfg
->interfaces
[i
]->name
);
1539 svec_sort(&have_names
);
1541 if (strcmp(vsctl_port
->bridge
->name
, br_name
)) {
1542 char *command
= vsctl_context_to_string(ctx
);
1543 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1544 command
, port_name
, vsctl_port
->bridge
->name
);
1547 if (!svec_equal(&want_names
, &have_names
)) {
1548 char *have_names_string
= svec_join(&have_names
, ", ", "");
1549 char *command
= vsctl_context_to_string(ctx
);
1551 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1552 command
, port_name
, have_names_string
);
1555 svec_destroy(&want_names
);
1556 svec_destroy(&have_names
);
1561 check_conflicts(vsctl_ctx
, port_name
,
1562 xasprintf("cannot create a port named %s", port_name
));
1563 for (i
= 0; i
< n_ifaces
; i
++) {
1564 check_conflicts(vsctl_ctx
, iface_names
[i
],
1565 xasprintf("cannot create an interface named %s",
1568 bridge
= find_bridge(vsctl_ctx
, br_name
, true);
1570 ifaces
= xmalloc(n_ifaces
* sizeof *ifaces
);
1571 for (i
= 0; i
< n_ifaces
; i
++) {
1572 ifaces
[i
] = ovsrec_interface_insert(ctx
->txn
);
1573 ovsrec_interface_set_name(ifaces
[i
], iface_names
[i
]);
1574 post_db_reload_expect_iface(ifaces
[i
]);
1577 port
= ovsrec_port_insert(ctx
->txn
);
1578 ovsrec_port_set_name(port
, port_name
);
1579 ovsrec_port_set_interfaces(port
, ifaces
, n_ifaces
);
1580 ovsrec_port_set_bond_fake_iface(port
, fake_iface
);
1582 if (bridge
->parent
) {
1583 int64_t tag
= bridge
->vlan
;
1584 ovsrec_port_set_tag(port
, &tag
, 1);
1587 for (i
= 0; i
< n_settings
; i
++) {
1588 ctl_set_column("Port", &port
->header_
, settings
[i
],
1592 bridge_insert_port((bridge
->parent
? bridge
->parent
->br_cfg
1593 : bridge
->br_cfg
), port
);
1595 struct vsctl_port
*vsctl_port
= add_port_to_cache(vsctl_ctx
, bridge
, port
);
1596 for (i
= 0; i
< n_ifaces
; i
++) {
1597 add_iface_to_cache(vsctl_ctx
, vsctl_port
, ifaces
[i
]);
1603 cmd_add_port(struct ctl_context
*ctx
)
1605 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1607 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, false,
1608 &ctx
->argv
[2], 1, &ctx
->argv
[3], ctx
->argc
- 3);
1612 cmd_add_bond(struct ctl_context
*ctx
)
1614 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1615 bool fake_iface
= shash_find(&ctx
->options
, "--fake-iface");
1619 n_ifaces
= ctx
->argc
- 3;
1620 for (i
= 3; i
< ctx
->argc
; i
++) {
1621 if (strchr(ctx
->argv
[i
], '=')) {
1627 ctl_fatal("add-bond requires at least 2 interfaces, but only "
1628 "%d were specified", n_ifaces
);
1631 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
, fake_iface
,
1632 &ctx
->argv
[3], n_ifaces
,
1633 &ctx
->argv
[n_ifaces
+ 3], ctx
->argc
- 3 - n_ifaces
);
1637 cmd_del_port(struct ctl_context
*ctx
)
1639 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1640 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1641 bool with_iface
= shash_find(&ctx
->options
, "--with-iface") != NULL
;
1642 const char *target
= ctx
->argv
[ctx
->argc
- 1];
1643 struct vsctl_port
*port
;
1645 vsctl_context_populate_cache(ctx
);
1646 if (find_bridge(vsctl_ctx
, target
, false)) {
1648 ctl_fatal("cannot delete port %s because it is the local port "
1649 "for bridge %s (deleting this port requires deleting "
1650 "the entire bridge)", target
, target
);
1653 } else if (!with_iface
) {
1654 port
= find_port(vsctl_ctx
, target
, must_exist
);
1656 struct vsctl_iface
*iface
;
1658 port
= find_port(vsctl_ctx
, target
, false);
1660 iface
= find_iface(vsctl_ctx
, target
, false);
1665 if (must_exist
&& !port
) {
1666 ctl_fatal("no port or interface named %s", target
);
1671 if (ctx
->argc
== 3) {
1672 struct vsctl_bridge
*bridge
;
1674 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1675 if (port
->bridge
!= bridge
) {
1676 if (port
->bridge
->parent
== bridge
) {
1677 ctl_fatal("bridge %s does not have a port %s (although "
1678 "its parent bridge %s does)",
1679 ctx
->argv
[1], ctx
->argv
[2],
1680 bridge
->parent
->name
);
1682 ctl_fatal("bridge %s does not have a port %s",
1683 ctx
->argv
[1], ctx
->argv
[2]);
1688 del_port(vsctl_ctx
, port
);
1693 cmd_port_to_br(struct ctl_context
*ctx
)
1695 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1696 struct vsctl_port
*port
;
1698 vsctl_context_populate_cache(ctx
);
1700 port
= find_port(vsctl_ctx
, ctx
->argv
[1], true);
1701 ds_put_format(&ctx
->output
, "%s\n", port
->bridge
->name
);
1705 cmd_br_to_vlan(struct ctl_context
*ctx
)
1707 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1708 struct vsctl_bridge
*bridge
;
1710 vsctl_context_populate_cache(ctx
);
1712 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1713 ds_put_format(&ctx
->output
, "%d\n", bridge
->vlan
);
1717 cmd_br_to_parent(struct ctl_context
*ctx
)
1719 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1720 struct vsctl_bridge
*bridge
;
1722 vsctl_context_populate_cache(ctx
);
1724 bridge
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1725 if (bridge
->parent
) {
1726 bridge
= bridge
->parent
;
1728 ds_put_format(&ctx
->output
, "%s\n", bridge
->name
);
1732 cmd_list_ifaces(struct ctl_context
*ctx
)
1734 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1735 struct vsctl_bridge
*br
;
1736 struct vsctl_port
*port
;
1739 vsctl_context_populate_cache(ctx
);
1741 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1742 verify_ports(vsctl_ctx
);
1745 LIST_FOR_EACH (port
, ports_node
, &br
->ports
) {
1746 struct vsctl_iface
*iface
;
1748 LIST_FOR_EACH (iface
, ifaces_node
, &port
->ifaces
) {
1749 if (strcmp(iface
->iface_cfg
->name
, br
->name
)) {
1750 svec_add(&ifaces
, iface
->iface_cfg
->name
);
1754 output_sorted(&ifaces
, &ctx
->output
);
1755 svec_destroy(&ifaces
);
1759 cmd_iface_to_br(struct ctl_context
*ctx
)
1761 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1762 struct vsctl_iface
*iface
;
1764 vsctl_context_populate_cache(ctx
);
1766 iface
= find_iface(vsctl_ctx
, ctx
->argv
[1], true);
1767 ds_put_format(&ctx
->output
, "%s\n", iface
->port
->bridge
->name
);
1771 verify_controllers(struct ovsrec_bridge
*bridge
)
1775 ovsrec_bridge_verify_controller(bridge
);
1776 for (i
= 0; i
< bridge
->n_controller
; i
++) {
1777 ovsrec_controller_verify_target(bridge
->controller
[i
]);
1782 pre_controller(struct ctl_context
*ctx
)
1786 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_controller_col_target
);
1790 cmd_get_controller(struct ctl_context
*ctx
)
1792 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1793 struct vsctl_bridge
*br
;
1794 struct svec targets
;
1797 vsctl_context_populate_cache(ctx
);
1799 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1803 verify_controllers(br
->br_cfg
);
1805 /* Print the targets in sorted order for reproducibility. */
1806 svec_init(&targets
);
1807 for (i
= 0; i
< br
->br_cfg
->n_controller
; i
++) {
1808 svec_add(&targets
, br
->br_cfg
->controller
[i
]->target
);
1811 svec_sort(&targets
);
1812 for (i
= 0; i
< targets
.n
; i
++) {
1813 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1815 svec_destroy(&targets
);
1819 delete_controllers(struct ovsrec_controller
**controllers
,
1820 size_t n_controllers
)
1824 for (i
= 0; i
< n_controllers
; i
++) {
1825 ovsrec_controller_delete(controllers
[i
]);
1830 cmd_del_controller(struct ctl_context
*ctx
)
1832 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1833 struct ovsrec_bridge
*br
;
1835 vsctl_context_populate_cache(ctx
);
1837 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true)->br_cfg
;
1838 verify_controllers(br
);
1840 if (br
->controller
) {
1841 delete_controllers(br
->controller
, br
->n_controller
);
1842 ovsrec_bridge_set_controller(br
, NULL
, 0);
1846 static struct ovsrec_controller
**
1847 insert_controllers(struct ovsdb_idl_txn
*txn
, char *targets
[], size_t n
)
1849 struct ovsrec_controller
**controllers
;
1852 controllers
= xmalloc(n
* sizeof *controllers
);
1853 for (i
= 0; i
< n
; i
++) {
1854 if (vconn_verify_name(targets
[i
]) && pvconn_verify_name(targets
[i
])) {
1855 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
1857 controllers
[i
] = ovsrec_controller_insert(txn
);
1858 ovsrec_controller_set_target(controllers
[i
], targets
[i
]);
1865 cmd_set_controller(struct ctl_context
*ctx
)
1867 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1868 struct ovsrec_controller
**controllers
;
1869 struct ovsrec_bridge
*br
;
1872 vsctl_context_populate_cache(ctx
);
1874 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true)->br_cfg
;
1875 verify_controllers(br
);
1877 delete_controllers(br
->controller
, br
->n_controller
);
1880 controllers
= insert_controllers(ctx
->txn
, &ctx
->argv
[2], n
);
1881 ovsrec_bridge_set_controller(br
, controllers
, n
);
1886 cmd_get_fail_mode(struct ctl_context
*ctx
)
1888 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1889 struct vsctl_bridge
*br
;
1890 const char *fail_mode
;
1892 vsctl_context_populate_cache(ctx
);
1893 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1898 ovsrec_bridge_verify_fail_mode(br
->br_cfg
);
1900 fail_mode
= br
->br_cfg
->fail_mode
;
1901 if (fail_mode
&& strlen(fail_mode
)) {
1902 ds_put_format(&ctx
->output
, "%s\n", fail_mode
);
1907 cmd_del_fail_mode(struct ctl_context
*ctx
)
1909 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1910 struct vsctl_bridge
*br
;
1912 vsctl_context_populate_cache(ctx
);
1914 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1916 ovsrec_bridge_set_fail_mode(br
->br_cfg
, NULL
);
1920 cmd_set_fail_mode(struct ctl_context
*ctx
)
1922 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1923 struct vsctl_bridge
*br
;
1924 const char *fail_mode
= ctx
->argv
[2];
1926 vsctl_context_populate_cache(ctx
);
1928 br
= find_real_bridge(vsctl_ctx
, ctx
->argv
[1], true);
1930 if (strcmp(fail_mode
, "standalone") && strcmp(fail_mode
, "secure")) {
1931 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1934 ovsrec_bridge_set_fail_mode(br
->br_cfg
, fail_mode
);
1938 verify_managers(const struct ovsrec_open_vswitch
*ovs
)
1942 ovsrec_open_vswitch_verify_manager_options(ovs
);
1944 for (i
= 0; i
< ovs
->n_manager_options
; ++i
) {
1945 const struct ovsrec_manager
*mgr
= ovs
->manager_options
[i
];
1947 ovsrec_manager_verify_target(mgr
);
1952 pre_manager(struct ctl_context
*ctx
)
1954 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_manager_options
);
1955 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_manager_col_target
);
1959 cmd_get_manager(struct ctl_context
*ctx
)
1961 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
1962 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
1963 struct svec targets
;
1966 verify_managers(ovs
);
1968 /* Print the targets in sorted order for reproducibility. */
1969 svec_init(&targets
);
1971 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1972 svec_add(&targets
, ovs
->manager_options
[i
]->target
);
1975 svec_sort_unique(&targets
);
1976 for (i
= 0; i
< targets
.n
; i
++) {
1977 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1979 svec_destroy(&targets
);
1983 delete_managers(const struct ovsrec_open_vswitch
*ovs
)
1987 /* Delete Manager rows pointed to by 'manager_options' column. */
1988 for (i
= 0; i
< ovs
->n_manager_options
; i
++) {
1989 ovsrec_manager_delete(ovs
->manager_options
[i
]);
1992 /* Delete 'Manager' row refs in 'manager_options' column. */
1993 ovsrec_open_vswitch_set_manager_options(ovs
, NULL
, 0);
1997 cmd_del_manager(struct ctl_context
*ctx
)
1999 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2000 const struct ovsrec_open_vswitch
*ovs
= vsctl_ctx
->ovs
;
2002 verify_managers(ovs
);
2003 delete_managers(ovs
);
2007 insert_managers(struct vsctl_context
*vsctl_ctx
, char *targets
[], size_t n
)
2009 struct ovsrec_manager
**managers
;
2012 /* Insert each manager in a new row in Manager table. */
2013 managers
= xmalloc(n
* sizeof *managers
);
2014 for (i
= 0; i
< n
; i
++) {
2015 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2016 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2018 managers
[i
] = ovsrec_manager_insert(vsctl_ctx
->base
.txn
);
2019 ovsrec_manager_set_target(managers
[i
], targets
[i
]);
2022 /* Store uuids of new Manager rows in 'manager_options' column. */
2023 ovsrec_open_vswitch_set_manager_options(vsctl_ctx
->ovs
, managers
, n
);
2028 cmd_set_manager(struct ctl_context
*ctx
)
2030 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2031 const size_t n
= ctx
->argc
- 1;
2033 verify_managers(vsctl_ctx
->ovs
);
2034 delete_managers(vsctl_ctx
->ovs
);
2035 insert_managers(vsctl_ctx
, &ctx
->argv
[1], n
);
2039 pre_cmd_get_ssl(struct ctl_context
*ctx
)
2041 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2043 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_private_key
);
2044 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_certificate
);
2045 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_ca_cert
);
2046 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_ssl_col_bootstrap_ca_cert
);
2050 cmd_get_ssl(struct ctl_context
*ctx
)
2052 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2053 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2055 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2057 ovsrec_ssl_verify_private_key(ssl
);
2058 ovsrec_ssl_verify_certificate(ssl
);
2059 ovsrec_ssl_verify_ca_cert(ssl
);
2060 ovsrec_ssl_verify_bootstrap_ca_cert(ssl
);
2062 ds_put_format(&ctx
->output
, "Private key: %s\n", ssl
->private_key
);
2063 ds_put_format(&ctx
->output
, "Certificate: %s\n", ssl
->certificate
);
2064 ds_put_format(&ctx
->output
, "CA Certificate: %s\n", ssl
->ca_cert
);
2065 ds_put_format(&ctx
->output
, "Bootstrap: %s\n",
2066 ssl
->bootstrap_ca_cert
? "true" : "false");
2071 pre_cmd_del_ssl(struct ctl_context
*ctx
)
2073 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2077 cmd_del_ssl(struct ctl_context
*ctx
)
2079 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2080 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2083 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2084 ovsrec_ssl_delete(ssl
);
2085 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, NULL
);
2090 pre_cmd_set_ssl(struct ctl_context
*ctx
)
2092 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_open_vswitch_col_ssl
);
2096 cmd_set_ssl(struct ctl_context
*ctx
)
2098 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2099 bool bootstrap
= shash_find(&ctx
->options
, "--bootstrap");
2100 struct ovsrec_ssl
*ssl
= vsctl_ctx
->ovs
->ssl
;
2102 ovsrec_open_vswitch_verify_ssl(vsctl_ctx
->ovs
);
2104 ovsrec_ssl_delete(ssl
);
2106 ssl
= ovsrec_ssl_insert(ctx
->txn
);
2108 ovsrec_ssl_set_private_key(ssl
, ctx
->argv
[1]);
2109 ovsrec_ssl_set_certificate(ssl
, ctx
->argv
[2]);
2110 ovsrec_ssl_set_ca_cert(ssl
, ctx
->argv
[3]);
2112 ovsrec_ssl_set_bootstrap_ca_cert(ssl
, bootstrap
);
2114 ovsrec_open_vswitch_set_ssl(vsctl_ctx
->ovs
, ssl
);
2118 autoattach_insert_mapping(struct ovsrec_autoattach
*aa
,
2122 int64_t *key_mappings
, *value_mappings
;
2125 key_mappings
= xmalloc(sizeof *aa
->key_mappings
* (aa
->n_mappings
+ 1));
2126 value_mappings
= xmalloc(sizeof *aa
->value_mappings
* (aa
->n_mappings
+ 1));
2128 for (i
= 0; i
< aa
->n_mappings
; i
++) {
2129 key_mappings
[i
] = aa
->key_mappings
[i
];
2130 value_mappings
[i
] = aa
->value_mappings
[i
];
2132 key_mappings
[aa
->n_mappings
] = isid
;
2133 value_mappings
[aa
->n_mappings
] = vlan
;
2135 ovsrec_autoattach_set_mappings(aa
, key_mappings
, value_mappings
,
2136 aa
->n_mappings
+ 1);
2139 free(value_mappings
);
2143 cmd_add_aa_mapping(struct ctl_context
*ctx
)
2145 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2146 struct vsctl_bridge
*br
;
2150 isid
= strtoull(ctx
->argv
[2], &nptr
, 10);
2151 if (nptr
== ctx
->argv
[2] || nptr
== NULL
) {
2152 ctl_fatal("Invalid argument %s", ctx
->argv
[2]);
2156 vlan
= strtoull(ctx
->argv
[3], &nptr
, 10);
2157 if (nptr
== ctx
->argv
[3] || nptr
== NULL
) {
2158 ctl_fatal("Invalid argument %s", ctx
->argv
[3]);
2162 vsctl_context_populate_cache(ctx
);
2164 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2170 if (!br
->br_cfg
->auto_attach
) {
2171 struct ovsrec_autoattach
*aa
= ovsrec_autoattach_insert(ctx
->txn
);
2172 ovsrec_bridge_set_auto_attach(br
->br_cfg
, aa
);
2174 autoattach_insert_mapping(br
->br_cfg
->auto_attach
, isid
, vlan
);
2179 del_aa_mapping(struct ovsrec_autoattach
*aa
,
2183 int64_t *key_mappings
, *value_mappings
;
2186 key_mappings
= xmalloc(sizeof *aa
->key_mappings
* (aa
->n_mappings
));
2187 value_mappings
= xmalloc(sizeof *value_mappings
* (aa
->n_mappings
));
2189 for (i
= n
= 0; i
< aa
->n_mappings
; i
++) {
2190 if (aa
->key_mappings
[i
] != isid
&& aa
->value_mappings
[i
] != vlan
) {
2191 key_mappings
[n
] = aa
->key_mappings
[i
];
2192 value_mappings
[n
++] = aa
->value_mappings
[i
];
2196 ovsrec_autoattach_set_mappings(aa
, key_mappings
, value_mappings
, n
);
2199 free(value_mappings
);
2203 cmd_del_aa_mapping(struct ctl_context
*ctx
)
2205 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2206 struct vsctl_bridge
*br
;
2210 isid
= strtoull(ctx
->argv
[2], &nptr
, 10);
2211 if (nptr
== ctx
->argv
[2] || nptr
== NULL
) {
2212 ctl_fatal("Invalid argument %s", ctx
->argv
[2]);
2216 vlan
= strtoull(ctx
->argv
[3], &nptr
, 10);
2217 if (nptr
== ctx
->argv
[3] || nptr
== NULL
) {
2218 ctl_fatal("Invalid argument %s", ctx
->argv
[3]);
2222 vsctl_context_populate_cache(ctx
);
2224 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2229 if (br
->br_cfg
&& br
->br_cfg
->auto_attach
&&
2230 br
->br_cfg
->auto_attach
->key_mappings
&&
2231 br
->br_cfg
->auto_attach
->value_mappings
) {
2234 for (i
= 0; i
< br
->br_cfg
->auto_attach
->n_mappings
; i
++) {
2235 if (br
->br_cfg
->auto_attach
->key_mappings
[i
] == isid
&&
2236 br
->br_cfg
->auto_attach
->value_mappings
[i
] == vlan
) {
2237 del_aa_mapping(br
->br_cfg
->auto_attach
, isid
, vlan
);
2245 pre_aa_mapping(struct ctl_context
*ctx
)
2249 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_bridge_col_auto_attach
);
2250 ovsdb_idl_add_column(ctx
->idl
, &ovsrec_autoattach_col_mappings
);
2254 verify_auto_attach(struct ovsrec_bridge
*bridge
)
2257 ovsrec_bridge_verify_auto_attach(bridge
);
2259 if (bridge
->auto_attach
) {
2260 ovsrec_autoattach_verify_mappings(bridge
->auto_attach
);
2266 cmd_get_aa_mapping(struct ctl_context
*ctx
)
2268 struct vsctl_context
*vsctl_ctx
= vsctl_context_cast(ctx
);
2269 struct vsctl_bridge
*br
;
2271 vsctl_context_populate_cache(ctx
);
2273 br
= find_bridge(vsctl_ctx
, ctx
->argv
[1], true);
2278 verify_auto_attach(br
->br_cfg
);
2280 if (br
->br_cfg
&& br
->br_cfg
->auto_attach
&&
2281 br
->br_cfg
->auto_attach
->key_mappings
&&
2282 br
->br_cfg
->auto_attach
->value_mappings
) {
2285 for (i
= 0; i
< br
->br_cfg
->auto_attach
->n_mappings
; i
++) {
2286 ds_put_format(&ctx
->output
, "%"PRId64
" %"PRId64
"\n",
2287 br
->br_cfg
->auto_attach
->key_mappings
[i
],
2288 br
->br_cfg
->auto_attach
->value_mappings
[i
]);
2294 static const struct ctl_table_class tables
[OVSREC_N_TABLES
] = {
2295 [OVSREC_TABLE_BRIDGE
].row_ids
[0] = {&ovsrec_bridge_col_name
, NULL
, NULL
},
2297 [OVSREC_TABLE_CONTROLLER
].row_ids
[0]
2298 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_controller
},
2300 [OVSREC_TABLE_INTERFACE
].row_ids
[0]
2301 = {&ovsrec_interface_col_name
, NULL
, NULL
},
2303 [OVSREC_TABLE_MIRROR
].row_ids
[0] = {&ovsrec_mirror_col_name
, NULL
, NULL
},
2305 [OVSREC_TABLE_MANAGER
].row_ids
[0]
2306 = {&ovsrec_manager_col_target
, NULL
, NULL
},
2308 [OVSREC_TABLE_NETFLOW
].row_ids
[0]
2309 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_netflow
},
2311 [OVSREC_TABLE_PORT
].row_ids
[0] = {&ovsrec_port_col_name
, NULL
, NULL
},
2313 [OVSREC_TABLE_QOS
].row_ids
[0]
2314 = {&ovsrec_port_col_name
, NULL
, &ovsrec_port_col_qos
},
2316 [OVSREC_TABLE_SFLOW
].row_ids
[0]
2317 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_sflow
},
2319 [OVSREC_TABLE_FLOW_TABLE
].row_ids
[0]
2320 = {&ovsrec_flow_table_col_name
, NULL
, NULL
},
2322 [OVSREC_TABLE_IPFIX
].row_ids
[0]
2323 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_ipfix
},
2325 [OVSREC_TABLE_AUTOATTACH
].row_ids
[0]
2326 = {&ovsrec_bridge_col_name
, NULL
, &ovsrec_bridge_col_auto_attach
},
2328 [OVSREC_TABLE_FLOW_SAMPLE_COLLECTOR_SET
].row_ids
[0]
2329 = {&ovsrec_flow_sample_collector_set_col_id
, NULL
, NULL
},
2333 post_db_reload_check_init(void)
2335 n_neoteric_ifaces
= 0;
2339 post_db_reload_expect_iface(const struct ovsrec_interface
*iface
)
2341 if (n_neoteric_ifaces
>= allocated_neoteric_ifaces
) {
2342 neoteric_ifaces
= x2nrealloc(neoteric_ifaces
,
2343 &allocated_neoteric_ifaces
,
2344 sizeof *neoteric_ifaces
);
2346 neoteric_ifaces
[n_neoteric_ifaces
++] = iface
->header_
.uuid
;
2350 post_db_reload_do_checks(const struct vsctl_context
*vsctl_ctx
)
2352 bool print_error
= false;
2355 for (i
= 0; i
< n_neoteric_ifaces
; i
++) {
2356 const struct uuid
*uuid
;
2358 uuid
= ovsdb_idl_txn_get_insert_uuid(vsctl_ctx
->base
.txn
,
2359 &neoteric_ifaces
[i
]);
2361 const struct ovsrec_interface
*iface
;
2363 iface
= ovsrec_interface_get_for_uuid(vsctl_ctx
->base
.idl
, uuid
);
2364 if (iface
&& (!iface
->ofport
|| *iface
->ofport
== -1)) {
2365 if (iface
->error
&& *iface
->error
) {
2366 ovs_error(0, "Error detected while setting up '%s': %s. "
2367 "See ovs-vswitchd log for details.",
2368 iface
->name
, iface
->error
);
2370 ovs_error(0, "Error detected while setting up '%s'. "
2371 "See ovs-vswitchd log for details.",
2380 ovs_error(0, "The default log directory is \"%s\".", ovs_logdir());
2386 vsctl_context_init_command(struct vsctl_context
*vsctl_ctx
,
2387 struct ctl_command
*command
)
2389 ctl_context_init_command(&vsctl_ctx
->base
, command
);
2390 vsctl_ctx
->verified_ports
= false;
2394 vsctl_context_init(struct vsctl_context
*vsctl_ctx
,
2395 struct ctl_command
*command
, struct ovsdb_idl
*idl
,
2396 struct ovsdb_idl_txn
*txn
,
2397 const struct ovsrec_open_vswitch
*ovs
,
2398 struct ovsdb_symbol_table
*symtab
)
2400 ctl_context_init(&vsctl_ctx
->base
, command
, idl
, txn
, symtab
,
2401 vsctl_context_invalidate_cache
);
2403 vsctl_ctx
->verified_ports
= false;
2405 vsctl_ctx
->ovs
= ovs
;
2406 vsctl_ctx
->cache_valid
= false;
2410 vsctl_context_done_command(struct vsctl_context
*vsctl_ctx
,
2411 struct ctl_command
*command
)
2413 ctl_context_done_command(&vsctl_ctx
->base
, command
);
2417 vsctl_context_done(struct vsctl_context
*vsctl_ctx
,
2418 struct ctl_command
*command
)
2420 ctl_context_done(&vsctl_ctx
->base
, command
);
2424 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2425 struct ovsdb_idl
*idl
)
2427 struct ctl_command
*c
;
2429 ovsdb_idl_add_table(idl
, &ovsrec_table_open_vswitch
);
2430 if (wait_for_reload
) {
2431 ovsdb_idl_add_column(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
2433 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2434 if (c
->syntax
->prerequisites
) {
2435 struct vsctl_context vsctl_ctx
;
2437 ds_init(&c
->output
);
2440 vsctl_context_init(&vsctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2441 (c
->syntax
->prerequisites
)(&vsctl_ctx
.base
);
2442 vsctl_context_done(&vsctl_ctx
, c
);
2444 ovs_assert(!c
->output
.string
);
2445 ovs_assert(!c
->table
);
2451 vsctl_parent_process_info(void)
2457 parent_pid
= getppid();
2460 /* Retrive the command line of the parent process, except the init
2461 * process since /proc/0 does not exist. */
2466 procfile
= xasprintf("/proc/%d/cmdline", parent_pid
);
2468 f
= fopen(procfile
, "r");
2475 ds_put_cstr(&s
, "init");
2478 ds_put_format(&s
, " (pid %d)", parent_pid
);
2479 return ds_steal_cstr(&s
);
2486 do_vsctl(const char *args
, struct ctl_command
*commands
, size_t n_commands
,
2487 struct ovsdb_idl
*idl
)
2489 struct ovsdb_idl_txn
*txn
;
2490 const struct ovsrec_open_vswitch
*ovs
;
2491 enum ovsdb_idl_txn_status status
;
2492 struct ovsdb_symbol_table
*symtab
;
2493 struct vsctl_context vsctl_ctx
;
2494 struct ctl_command
*c
;
2495 struct shash_node
*node
;
2496 int64_t next_cfg
= 0;
2498 char *ppid_info
= NULL
;
2500 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2502 ovsdb_idl_txn_set_dry_run(txn
);
2505 ppid_info
= vsctl_parent_process_info();
2507 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl (invoked by %s): %s",
2511 ovsdb_idl_txn_add_comment(txn
, "ovs-vsctl: %s", args
);
2514 ovs
= ovsrec_open_vswitch_first(idl
);
2516 /* XXX add verification that table is empty */
2517 ovs
= ovsrec_open_vswitch_insert(txn
);
2520 if (wait_for_reload
) {
2521 ovsdb_idl_txn_increment(txn
, &ovs
->header_
,
2522 &ovsrec_open_vswitch_col_next_cfg
, false);
2525 post_db_reload_check_init();
2526 symtab
= ovsdb_symbol_table_create();
2527 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2528 ds_init(&c
->output
);
2531 vsctl_context_init(&vsctl_ctx
, NULL
, idl
, txn
, ovs
, symtab
);
2532 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2533 vsctl_context_init_command(&vsctl_ctx
, c
);
2534 if (c
->syntax
->run
) {
2535 (c
->syntax
->run
)(&vsctl_ctx
.base
);
2537 vsctl_context_done_command(&vsctl_ctx
, c
);
2539 if (vsctl_ctx
.base
.try_again
) {
2540 vsctl_context_done(&vsctl_ctx
, NULL
);
2544 vsctl_context_done(&vsctl_ctx
, NULL
);
2546 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2547 struct ovsdb_symbol
*symbol
= node
->data
;
2548 if (!symbol
->created
) {
2549 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2550 "with \"-- --id=%s create ...\")",
2551 node
->name
, node
->name
);
2553 if (!symbol
->strong_ref
) {
2554 if (!symbol
->weak_ref
) {
2555 VLOG_WARN("row id \"%s\" was created but no reference to it "
2556 "was inserted, so it will not actually appear in "
2557 "the database", node
->name
);
2559 VLOG_WARN("row id \"%s\" was created but only a weak "
2560 "reference to it was inserted, so it will not "
2561 "actually appear in the database", node
->name
);
2566 status
= ovsdb_idl_txn_commit_block(txn
);
2567 if (wait_for_reload
&& status
== TXN_SUCCESS
) {
2568 next_cfg
= ovsdb_idl_txn_get_increment_new_value(txn
);
2570 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2571 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2572 if (c
->syntax
->postprocess
) {
2573 vsctl_context_init(&vsctl_ctx
, c
, idl
, txn
, ovs
, symtab
);
2574 (c
->syntax
->postprocess
)(&vsctl_ctx
.base
);
2575 vsctl_context_done(&vsctl_ctx
, c
);
2579 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2582 case TXN_UNCOMMITTED
:
2583 case TXN_INCOMPLETE
:
2587 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2588 ctl_fatal("transaction aborted");
2598 ctl_fatal("transaction error: %s", error
);
2600 case TXN_NOT_LOCKED
:
2601 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2602 ctl_fatal("database not locked");
2609 ovsdb_symbol_table_destroy(symtab
);
2611 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2612 struct ds
*ds
= &c
->output
;
2615 table_print(c
->table
, &table_style
);
2616 } else if (oneline
) {
2620 for (j
= 0; j
< ds
->length
; j
++) {
2621 int ch
= ds
->string
[j
];
2624 fputs("\\n", stdout
);
2628 fputs("\\\\", stdout
);
2637 fputs(ds_cstr(ds
), stdout
);
2639 ds_destroy(&c
->output
);
2640 table_destroy(c
->table
);
2643 shash_destroy_free_data(&c
->options
);
2647 if (wait_for_reload
&& status
!= TXN_UNCHANGED
) {
2648 /* Even, if --retry flag was not specified, ovs-vsctl still
2649 * has to retry to establish OVSDB connection, if wait_for_reload
2650 * was set. Otherwise, ovs-vsctl would end up waiting forever
2651 * until cur_cfg would be updated. */
2652 ovsdb_idl_enable_reconnect(idl
);
2655 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs
, idl
) {
2656 if (ovs
->cur_cfg
>= next_cfg
) {
2657 post_db_reload_do_checks(&vsctl_ctx
);
2661 ovsdb_idl_wait(idl
);
2666 ovsdb_idl_txn_destroy(txn
);
2667 ovsdb_idl_destroy(idl
);
2672 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2673 * resources and return so that the caller can try again. */
2674 ovsdb_idl_txn_abort(txn
);
2675 ovsdb_idl_txn_destroy(txn
);
2678 ovsdb_symbol_table_destroy(symtab
);
2679 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2680 ds_destroy(&c
->output
);
2681 table_destroy(c
->table
);
2687 /* Frees the current transaction and the underlying IDL and then calls
2690 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2691 * for a clean memory leak report from valgrind in the normal case. That makes
2692 * it easier to notice real memory leaks. */
2694 vsctl_exit(int status
)
2697 ovsdb_idl_txn_abort(the_idl_txn
);
2698 ovsdb_idl_txn_destroy(the_idl_txn
);
2700 ovsdb_idl_destroy(the_idl
);
2705 * Developers who add new commands to the 'struct ctl_command_syntax' must
2706 * define the 'arguments' member of the struct. The following keywords are
2707 * available for composing the argument format:
2709 * TABLE RECORD BRIDGE PARENT PORT
2710 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
2711 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
2712 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
2713 * TARGET NEW-* (e.g. NEW-PORT)
2715 * For argument types not listed above, just uses 'ARG' as place holder.
2717 * Encloses the keyword with '[]' if it is optional. Appends '...' to
2718 * keyword or enclosed keyword to indicate that the argument can be specified
2722 static const struct ctl_command_syntax vsctl_commands
[] = {
2723 /* Open vSwitch commands. */
2724 {"init", 0, 0, "", NULL
, cmd_init
, NULL
, "", RW
},
2726 /* Bridge commands. */
2727 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info
,
2728 cmd_add_br
, NULL
, "--may-exist", RW
},
2729 {"del-br", 1, 1, "BRIDGE", pre_get_info
, cmd_del_br
,
2730 NULL
, "--if-exists", RW
},
2731 {"list-br", 0, 0, "", pre_get_info
, cmd_list_br
, NULL
, "--real,--fake",
2733 {"br-exists", 1, 1, "BRIDGE", pre_get_info
, cmd_br_exists
, NULL
, "", RO
},
2734 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info
, cmd_br_to_vlan
, NULL
, "",
2736 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info
, cmd_br_to_parent
, NULL
,
2738 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2739 pre_cmd_br_set_external_id
, cmd_br_set_external_id
, NULL
, "", RW
},
2740 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id
,
2741 cmd_br_get_external_id
, NULL
, "", RO
},
2743 /* Port commands. */
2744 {"list-ports", 1, 1, "BRIDGE", pre_get_info
, cmd_list_ports
, NULL
, "",
2746 {"add-port", 2, INT_MAX
, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2747 pre_get_info
, cmd_add_port
, NULL
, "--may-exist", RW
},
2748 {"add-bond", 4, INT_MAX
,
2749 "BRIDGE NEW-BOND-PORT SYSIFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info
,
2750 cmd_add_bond
, NULL
, "--may-exist,--fake-iface", RW
},
2751 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info
, cmd_del_port
, NULL
,
2752 "--if-exists,--with-iface", RW
},
2753 {"port-to-br", 1, 1, "PORT", pre_get_info
, cmd_port_to_br
, NULL
, "", RO
},
2755 /* Interface commands. */
2756 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info
, cmd_list_ifaces
, NULL
, "",
2758 {"iface-to-br", 1, 1, "IFACE", pre_get_info
, cmd_iface_to_br
, NULL
, "",
2761 /* Controller commands. */
2762 {"get-controller", 1, 1, "BRIDGE", pre_controller
, cmd_get_controller
,
2764 {"del-controller", 1, 1, "BRIDGE", pre_controller
, cmd_del_controller
,
2766 {"set-controller", 1, INT_MAX
, "BRIDGE TARGET...", pre_controller
,
2767 cmd_set_controller
, NULL
, "", RW
},
2768 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info
, cmd_get_fail_mode
, NULL
,
2770 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info
, cmd_del_fail_mode
, NULL
,
2772 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info
, cmd_set_fail_mode
,
2775 /* Manager commands. */
2776 {"get-manager", 0, 0, "", pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2777 {"del-manager", 0, 0, "", pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2778 {"set-manager", 1, INT_MAX
, "TARGET...", pre_manager
, cmd_set_manager
,
2782 {"get-ssl", 0, 0, "", pre_cmd_get_ssl
, cmd_get_ssl
, NULL
, "", RO
},
2783 {"del-ssl", 0, 0, "", pre_cmd_del_ssl
, cmd_del_ssl
, NULL
, "", RW
},
2784 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl
,
2785 cmd_set_ssl
, NULL
, "--bootstrap", RW
},
2787 /* Auto Attach commands. */
2788 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping
, cmd_add_aa_mapping
,
2790 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping
, cmd_del_aa_mapping
,
2792 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping
, cmd_get_aa_mapping
,
2795 /* Switch commands. */
2796 {"emer-reset", 0, 0, "", pre_cmd_emer_reset
, cmd_emer_reset
, NULL
, "", RW
},
2798 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2801 /* Registers vsctl and common db commands. */
2803 vsctl_cmd_init(void)
2805 ctl_init(ovsrec_table_classes
, tables
, cmd_show_tables
, vsctl_exit
);
2806 ctl_register_commands(vsctl_commands
);