]> git.proxmox.com Git - ovs.git/blame - utilities/ovs-vsctl.c
db-ctl-base: Drop redundant 'table' field from struct ctl_row_id.
[ovs.git] / utilities / ovs-vsctl.c
CommitLineData
c75d1511 1/*
3f5b5f7b 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
c75d1511
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
ad83bfa6 19#include <ctype.h>
c75d1511 20#include <errno.h>
ad83bfa6 21#include <float.h>
c75d1511
BP
22#include <getopt.h>
23#include <inttypes.h>
24#include <signal.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <string.h>
6d5abe94 28#include <unistd.h>
c75d1511 29
07ff77cc
AW
30#include "db-ctl-base.h"
31
c75d1511
BP
32#include "command-line.h"
33#include "compiler.h"
feb38b6d 34#include "dirs.h"
3e8a2ad1 35#include "openvswitch/dynamic-string.h"
8a777cf6 36#include "fatal-signal.h"
a341ee57 37#include "hash.h"
ee89ea7b 38#include "openvswitch/json.h"
ad83bfa6 39#include "ovsdb-data.h"
c75d1511
BP
40#include "ovsdb-idl.h"
41#include "poll-loop.h"
f8ff4bc4 42#include "process.h"
ae9a3235 43#include "stream.h"
218a6f59 44#include "stream-ssl.h"
a699f614 45#include "smap.h"
b3c01ed3 46#include "sset.h"
dfbe07ba 47#include "svec.h"
eaa67ba8 48#include "lib/vswitch-idl.h"
e051b42c 49#include "table.h"
c75d1511
BP
50#include "timeval.h"
51#include "util.h"
4a1f523f 52#include "openvswitch/vconn.h"
e6211adc 53#include "openvswitch/vlog.h"
5136ce49 54
d98e6007 55VLOG_DEFINE_THIS_MODULE(vsctl);
c75d1511 56
f8ff4bc4
BP
57struct vsctl_context;
58
c75d1511
BP
59/* --db: The database server to contact. */
60static const char *db;
61
62/* --oneline: Write each command's output as a single line? */
63static bool oneline;
64
577aebdf
BP
65/* --dry-run: Do not commit any changes. */
66static bool dry_run;
67
b54e22e9
BP
68/* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
69static bool wait_for_reload = true;
70
a39a859a 71/* --timeout: Time to wait for a connection to 'db'. */
6b7b9d34 72static int timeout;
a39a859a 73
fba6bd1d
BP
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
78 * running).
79 *
80 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
81 * wait. */
82static bool retry;
83
e051b42c
BP
84/* Format for table output. */
85static struct table_style table_style = TABLE_STYLE_DEFAULT;
86
07ff77cc 87static void vsctl_cmd_init(void);
ce6f1d1f
AZ
88
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. */
92static struct ovsdb_idl *the_idl;
93static struct ovsdb_idl_txn *the_idl_txn;
94OVS_NO_RETURN static void vsctl_exit(int status);
95
cab50449 96OVS_NO_RETURN static void usage(void);
401d5a6d 97static void parse_options(int argc, char *argv[], struct shash *local_options);
07ff77cc 98static void run_prerequisites(struct ctl_command[], size_t n_commands,
e5e12280 99 struct ovsdb_idl *);
07ff77cc 100static void do_vsctl(const char *args, struct ctl_command *, size_t n,
854a94d9 101 struct ovsdb_idl *);
c75d1511 102
07ff77cc 103/* post_db_reload_check frame work is to allow ovs-vsctl to do additional
c3ccfe98
AZ
104 * checks after OVSDB transactions are successfully recorded and reload by
105 * ovs-vswitchd.
106 *
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.
110 *
b2ffb17c 111 * Unless --no-wait command line option is specified,
c3ccfe98
AZ
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.
116 *
117 * Current implementation only check for Post OVSDB reload failures on new
118 * interface additions with 'add-br' and 'add-port' commands.
119 *
120 * post_db_reload_expect_iface()
121 *
122 * keep track of interfaces to be checked post OVSDB reload. */
123static void post_db_reload_check_init(void);
124static void post_db_reload_do_checks(const struct vsctl_context *);
125static void post_db_reload_expect_iface(const struct ovsrec_interface *);
126
127static struct uuid *neoteric_ifaces;
128static size_t n_neoteric_ifaces;
129static size_t allocated_neoteric_ifaces;
130
c75d1511
BP
131int
132main(int argc, char *argv[])
133{
134 struct ovsdb_idl *idl;
07ff77cc 135 struct ctl_command *commands;
401d5a6d 136 struct shash local_options;
854a94d9 137 unsigned int seqno;
f8ff4bc4
BP
138 size_t n_commands;
139 char *args;
c75d1511
BP
140
141 set_program_name(argv[0]);
8a777cf6 142 fatal_ignore_sigpipe();
480ce8ab 143 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
45863ce5 144 vlog_set_levels_from_string_assert("reconnect:warn");
f8ff4bc4 145
07ff77cc
AW
146 vsctl_cmd_init();
147
f8ff4bc4
BP
148 /* Log our arguments. This is often valuable for debugging systems. */
149 args = process_escape_args(argv);
07ff77cc 150 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
f8ff4bc4
BP
151
152 /* Parse command line. */
401d5a6d
BP
153 shash_init(&local_options);
154 parse_options(argc, argv, &local_options);
07ff77cc
AW
155 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
156 &n_commands);
c75d1511 157
a39a859a
JP
158 if (timeout) {
159 time_alarm(timeout);
160 }
161
e5e12280 162 /* Initialize IDL. */
fba6bd1d 163 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
e5e12280
BP
164 run_prerequisites(commands, n_commands, idl);
165
854a94d9
BP
166 /* Execute the commands.
167 *
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);
c75d1511 174 for (;;) {
854a94d9 175 ovsdb_idl_run(idl);
fba6bd1d
BP
176 if (!ovsdb_idl_is_alive(idl)) {
177 int retval = ovsdb_idl_get_last_error(idl);
07ff77cc 178 ctl_fatal("%s: database connection failed (%s)",
fba6bd1d
BP
179 db, ovs_retval_to_string(retval));
180 }
854a94d9
BP
181
182 if (seqno != ovsdb_idl_get_seqno(idl)) {
183 seqno = ovsdb_idl_get_seqno(idl);
184 do_vsctl(args, commands, n_commands, idl);
c75d1511
BP
185 }
186
854a94d9 187 if (seqno == ovsdb_idl_get_seqno(idl)) {
4fdfe5cc
BP
188 ovsdb_idl_wait(idl);
189 poll_block();
190 }
c75d1511
BP
191 }
192}
193
194static void
401d5a6d 195parse_options(int argc, char *argv[], struct shash *local_options)
c75d1511
BP
196{
197 enum {
198 OPT_DB = UCHAR_MAX + 1,
199 OPT_ONELINE,
0c3dd1e1 200 OPT_NO_SYSLOG,
577aebdf 201 OPT_NO_WAIT,
e26b5a06 202 OPT_DRY_RUN,
6b777e47 203 OPT_BOOTSTRAP_CA_CERT,
218a6f59 204 OPT_PEER_CA_CERT,
401d5a6d 205 OPT_LOCAL,
fba6bd1d 206 OPT_RETRY,
95e4a97a
PA
207 OPT_COMMANDS,
208 OPT_OPTIONS,
e051b42c 209 VLOG_OPTION_ENUMS,
e18a1d08
ER
210 TABLE_OPTION_ENUMS,
211 SSL_OPTION_ENUMS,
c75d1511 212 };
401d5a6d 213 static const struct option global_long_options[] = {
e3c17733
BP
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'},
fba6bd1d 220 {"retry", no_argument, NULL, OPT_RETRY},
e3c17733 221 {"help", no_argument, NULL, 'h'},
95e4a97a
PA
222 {"commands", no_argument, NULL, OPT_COMMANDS},
223 {"options", no_argument, NULL, OPT_OPTIONS},
e3c17733 224 {"version", no_argument, NULL, 'V'},
e26b5a06 225 VLOG_LONG_OPTIONS,
e051b42c 226 TABLE_LONG_OPTIONS,
bf8f2167 227 STREAM_SSL_LONG_OPTIONS,
6b777e47 228 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
e3c17733
BP
229 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
230 {NULL, 0, NULL, 0},
c75d1511 231 };
401d5a6d 232 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
a2a9d2d9 233 char *tmp, *short_options;
c75d1511 234
51a73ff0 235 struct option *options;
401d5a6d
BP
236 size_t allocated_options;
237 size_t n_options;
238 size_t i;
239
5f383751 240 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
a2a9d2d9
BP
241 short_options = xasprintf("+%s", tmp);
242 free(tmp);
342045e1 243
401d5a6d
BP
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
247 * options. */
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;
51a73ff0 251 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
e051b42c 252
c75d1511 253 for (;;) {
401d5a6d 254 int idx;
c75d1511
BP
255 int c;
256
401d5a6d 257 c = getopt_long(argc, argv, short_options, options, &idx);
c75d1511
BP
258 if (c == -1) {
259 break;
260 }
261
262 switch (c) {
263 case OPT_DB:
264 db = optarg;
265 break;
266
267 case OPT_ONELINE:
268 oneline = true;
269 break;
270
dfbe07ba 271 case OPT_NO_SYSLOG:
922fed06 272 vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
dfbe07ba
BP
273 break;
274
0c3dd1e1 275 case OPT_NO_WAIT:
b54e22e9 276 wait_for_reload = false;
0c3dd1e1
BP
277 break;
278
577aebdf
BP
279 case OPT_DRY_RUN:
280 dry_run = true;
281 break;
282
401d5a6d
BP
283 case OPT_LOCAL:
284 if (shash_find(local_options, options[idx].name)) {
07ff77cc 285 ctl_fatal("'%s' option specified multiple times",
401d5a6d
BP
286 options[idx].name);
287 }
288 shash_add_nocopy(local_options,
289 xasprintf("--%s", options[idx].name),
2225c0b9 290 nullable_xstrdup(optarg));
401d5a6d
BP
291 break;
292
c75d1511
BP
293 case 'h':
294 usage();
295
95e4a97a 296 case OPT_COMMANDS:
51a73ff0 297 ctl_print_commands();
95e4a97a
PA
298
299 case OPT_OPTIONS:
51a73ff0 300 ctl_print_options(global_long_options);
95e4a97a 301
c75d1511 302 case 'V':
55d5bb44 303 ovs_print_version(0, 0);
c51513a2 304 printf("DB Schema %s\n", ovsrec_get_db_version());
c75d1511
BP
305 exit(EXIT_SUCCESS);
306
342045e1
BP
307 case 't':
308 timeout = strtoul(optarg, NULL, 10);
a39a859a 309 if (timeout < 0) {
07ff77cc 310 ctl_fatal("value %s on -t or --timeout is invalid",
def90f62 311 optarg);
342045e1
BP
312 }
313 break;
314
fba6bd1d
BP
315 case OPT_RETRY:
316 retry = true;
317 break;
318
e26b5a06 319 VLOG_OPTION_HANDLERS
e051b42c 320 TABLE_OPTION_HANDLERS(&table_style)
c75d1511 321
218a6f59
BP
322 STREAM_SSL_OPTION_HANDLERS
323
324 case OPT_PEER_CA_CERT:
325 stream_ssl_set_peer_ca_cert_file(optarg);
326 break;
218a6f59 327
6b777e47
GS
328 case OPT_BOOTSTRAP_CA_CERT:
329 stream_ssl_set_ca_cert_file(optarg, true);
330 break;
331
c75d1511
BP
332 case '?':
333 exit(EXIT_FAILURE);
334
335 default:
336 abort();
337 }
338 }
a2a9d2d9 339 free(short_options);
c75d1511
BP
340
341 if (!db) {
51a73ff0 342 db = ctl_default_db();
c75d1511 343 }
401d5a6d
BP
344
345 for (i = n_global_long_options; options[i].name; i++) {
346 free(CONST_CAST(char *, options[i].name));
347 }
348 free(options);
c75d1511
BP
349}
350
351static void
352usage(void)
353{
8f7501e8
BP
354 printf("\
355%s: ovs-vswitchd management utility\n\
356usage: %s [OPTIONS] COMMAND [ARG...]\n\
357\n\
ae9a3235
BP
358Open vSwitch commands:\n\
359 init initialize database, if not yet initialized\n\
9b1735a7 360 show print overview of database contents\n\
ae9a3235
BP
361 emer-reset reset configuration to clean state\n\
362\n\
8f7501e8
BP
363Bridge commands:\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\
b5fcae50 368 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
8f7501e8
BP
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\
375\n\
ae9a3235 376Port commands (a bond is considered to be a single port):\n\
8f7501e8
BP
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\
8f7501e8
BP
382\n\
383Interface 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\
8f7501e8
BP
386\n\
387Controller commands:\n\
a892775d
BP
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\
1a048029
JP
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\
8f7501e8 394\n\
24b8b259 395Manager commands:\n\
a892775d
BP
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\
24b8b259 399\n\
8f7501e8
BP
400SSL commands:\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\
404\n\
99eef98b
DF
405Auto 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\
409\n\
18ee958b
JP
410Switch commands:\n\
411 emer-reset reset switch to known good state\n\
412\n\
07ff77cc 413%s\
8f7501e8
BP
414\n\
415Options:\n\
416 --db=DATABASE connect to DATABASE\n\
417 (default: %s)\n\
ae9a3235 418 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
fba6bd1d 419 --retry keep trying to connect to server forever\n\
ae9a3235
BP
420 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
421 --dry-run do not commit changes to database\n\
8f7501e8 422 --oneline print exactly one line of output per command\n",
51a73ff0 423 program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
bcb58ce0 424 table_usage();
c75d1511 425 vlog_usage();
ae9a3235
BP
426 printf("\
427 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
428 stream_usage("database", true, true, false);
8f7501e8
BP
429 printf("\n\
430Other options:\n\
431 -h, --help display this help message\n\
432 -V, --version display version information\n");
c75d1511
BP
433 exit(EXIT_SUCCESS);
434}
435
c75d1511 436\f
07ff77cc 437/* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
5d9cb63c 438struct vsctl_context {
07ff77cc 439 struct ctl_context base;
f8ff4bc4
BP
440
441 /* Modifiable state. */
5d9cb63c 442 const struct ovsrec_open_vswitch *ovs;
f74055e7 443 bool verified_ports;
87b23a01 444
5ce5a6b5
BP
445 /* A cache of the contents of the database.
446 *
447 * A command that needs to use any of this information must first call
448 * vsctl_context_populate_cache(). A command that changes anything that
449 * could invalidate the cache must either call
450 * vsctl_context_invalidate_cache() or manually update the cache to
451 * maintain its correctness. */
452 bool cache_valid;
453 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
454 struct shash ports; /* Maps from port name to struct vsctl_port. */
455 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
5d9cb63c
BP
456};
457
c75d1511
BP
458struct vsctl_bridge {
459 struct ovsrec_bridge *br_cfg;
460 char *name;
ca6ba700 461 struct ovs_list ports; /* Contains "struct vsctl_port"s. */
5341d046
BP
462
463 /* VLAN ("fake") bridge support.
464 *
465 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
466 * in either case. */
a341ee57
BP
467 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
468 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
5341d046
BP
469 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
470 int vlan; /* VLAN VID (0...4095), or 0. */
c75d1511
BP
471};
472
473struct vsctl_port {
ca6ba700
TG
474 struct ovs_list ports_node; /* In struct vsctl_bridge's 'ports' list. */
475 struct ovs_list ifaces; /* Contains "struct vsctl_iface"s. */
c75d1511
BP
476 struct ovsrec_port *port_cfg;
477 struct vsctl_bridge *bridge;
478};
479
480struct vsctl_iface {
ca6ba700 481 struct ovs_list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
c75d1511
BP
482 struct ovsrec_interface *iface_cfg;
483 struct vsctl_port *port;
484};
485
ec4eed45 486/* Casts 'base' into 'struct vsctl_context'. */
07ff77cc
AW
487static struct vsctl_context *
488vsctl_context_cast(struct ctl_context *base)
489{
490 return CONTAINER_OF(base, struct vsctl_context, base);
491}
492
5dd9826c
BP
493static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
494 int vlan);
495
bb1c67c8 496static char *
07ff77cc 497vsctl_context_to_string(const struct ctl_context *ctx)
bb1c67c8
BP
498{
499 const struct shash_node *node;
500 struct svec words;
501 char *s;
502 int i;
503
504 svec_init(&words);
505 SHASH_FOR_EACH (node, &ctx->options) {
506 svec_add(&words, node->name);
507 }
508 for (i = 0; i < ctx->argc; i++) {
509 svec_add(&words, ctx->argv[i]);
510 }
511 svec_terminate(&words);
512
513 s = process_escape_args(words.names);
514
515 svec_destroy(&words);
516
517 return s;
518}
519
f74055e7 520static void
07ff77cc 521verify_ports(struct vsctl_context *vsctl_ctx)
f74055e7 522{
07ff77cc 523 if (!vsctl_ctx->verified_ports) {
f74055e7
BP
524 const struct ovsrec_bridge *bridge;
525 const struct ovsrec_port *port;
526
07ff77cc
AW
527 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
528 OVSREC_BRIDGE_FOR_EACH (bridge, vsctl_ctx->base.idl) {
f74055e7
BP
529 ovsrec_bridge_verify_ports(bridge);
530 }
07ff77cc 531 OVSREC_PORT_FOR_EACH (port, vsctl_ctx->base.idl) {
f74055e7
BP
532 ovsrec_port_verify_interfaces(port);
533 }
534
07ff77cc 535 vsctl_ctx->verified_ports = true;
f74055e7
BP
536 }
537}
538
c75d1511 539static struct vsctl_bridge *
07ff77cc 540add_bridge_to_cache(struct vsctl_context *vsctl_ctx,
a341ee57
BP
541 struct ovsrec_bridge *br_cfg, const char *name,
542 struct vsctl_bridge *parent, int vlan)
c75d1511
BP
543{
544 struct vsctl_bridge *br = xmalloc(sizeof *br);
545 br->br_cfg = br_cfg;
546 br->name = xstrdup(name);
417e7e66 547 ovs_list_init(&br->ports);
c75d1511
BP
548 br->parent = parent;
549 br->vlan = vlan;
a341ee57
BP
550 hmap_init(&br->children);
551 if (parent) {
5dd9826c
BP
552 struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
553 if (conflict) {
554 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
555 "for VLAN %d, but only one is allowed",
556 parent->name, name, conflict->name, vlan);
557 } else {
558 hmap_insert(&parent->children, &br->children_node,
559 hash_int(vlan, 0));
560 }
a341ee57 561 }
07ff77cc 562 shash_add(&vsctl_ctx->bridges, br->name, br);
c75d1511
BP
563 return br;
564}
565
a341ee57
BP
566static void
567ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
568 struct ovsrec_bridge *bridge)
569{
570 struct ovsrec_bridge **bridges;
571 size_t i, n;
572
573 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
574 for (i = n = 0; i < ovs->n_bridges; i++) {
575 if (ovs->bridges[i] != bridge) {
576 bridges[n++] = ovs->bridges[i];
577 }
578 }
579 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
580 free(bridges);
581}
582
583static void
07ff77cc 584del_cached_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
a341ee57 585{
417e7e66 586 ovs_assert(ovs_list_is_empty(&br->ports));
cb22974d 587 ovs_assert(hmap_is_empty(&br->children));
a341ee57
BP
588 if (br->parent) {
589 hmap_remove(&br->parent->children, &br->children_node);
590 }
591 if (br->br_cfg) {
592 ovsrec_bridge_delete(br->br_cfg);
07ff77cc 593 ovs_delete_bridge(vsctl_ctx->ovs, br->br_cfg);
a341ee57 594 }
07ff77cc 595 shash_find_and_delete(&vsctl_ctx->bridges, br->name);
a341ee57
BP
596 hmap_destroy(&br->children);
597 free(br->name);
598 free(br);
599}
600
c75d1511
BP
601static bool
602port_is_fake_bridge(const struct ovsrec_port *port_cfg)
603{
604 return (port_cfg->fake_bridge
605 && port_cfg->tag
5341d046 606 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
c75d1511
BP
607}
608
609static struct vsctl_bridge *
a341ee57 610find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
c75d1511 611{
a341ee57 612 struct vsctl_bridge *child;
c75d1511 613
a341ee57
BP
614 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
615 &parent->children) {
616 if (child->vlan == vlan) {
617 return child;
c75d1511
BP
618 }
619 }
620
621 return NULL;
622}
623
a341ee57 624static struct vsctl_port *
07ff77cc 625add_port_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *parent,
a341ee57
BP
626 struct ovsrec_port *port_cfg)
627{
628 struct vsctl_port *port;
629
630 if (port_cfg->tag
631 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
632 struct vsctl_bridge *vlan_bridge;
633
634 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
635 if (vlan_bridge) {
636 parent = vlan_bridge;
637 }
638 }
639
640 port = xmalloc(sizeof *port);
417e7e66
BW
641 ovs_list_push_back(&parent->ports, &port->ports_node);
642 ovs_list_init(&port->ifaces);
a341ee57
BP
643 port->port_cfg = port_cfg;
644 port->bridge = parent;
07ff77cc 645 shash_add(&vsctl_ctx->ports, port_cfg->name, port);
a341ee57
BP
646
647 return port;
648}
649
650static void
07ff77cc 651del_cached_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
a341ee57 652{
417e7e66
BW
653 ovs_assert(ovs_list_is_empty(&port->ifaces));
654 ovs_list_remove(&port->ports_node);
07ff77cc 655 shash_find_and_delete(&vsctl_ctx->ports, port->port_cfg->name);
a341ee57
BP
656 ovsrec_port_delete(port->port_cfg);
657 free(port);
658}
659
660static struct vsctl_iface *
07ff77cc 661add_iface_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_port *parent,
a341ee57
BP
662 struct ovsrec_interface *iface_cfg)
663{
664 struct vsctl_iface *iface;
665
666 iface = xmalloc(sizeof *iface);
417e7e66 667 ovs_list_push_back(&parent->ifaces, &iface->ifaces_node);
a341ee57
BP
668 iface->iface_cfg = iface_cfg;
669 iface->port = parent;
07ff77cc 670 shash_add(&vsctl_ctx->ifaces, iface_cfg->name, iface);
a341ee57
BP
671
672 return iface;
673}
674
675static void
07ff77cc 676del_cached_iface(struct vsctl_context *vsctl_ctx, struct vsctl_iface *iface)
a341ee57 677{
417e7e66 678 ovs_list_remove(&iface->ifaces_node);
07ff77cc 679 shash_find_and_delete(&vsctl_ctx->ifaces, iface->iface_cfg->name);
a341ee57
BP
680 ovsrec_interface_delete(iface->iface_cfg);
681 free(iface);
682}
683
c75d1511 684static void
07ff77cc 685vsctl_context_invalidate_cache(struct ctl_context *ctx)
c75d1511 686{
07ff77cc 687 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511
BP
688 struct shash_node *node;
689
07ff77cc 690 if (!vsctl_ctx->cache_valid) {
5ce5a6b5
BP
691 return;
692 }
07ff77cc 693 vsctl_ctx->cache_valid = false;
5ce5a6b5 694
07ff77cc 695 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
c75d1511 696 struct vsctl_bridge *bridge = node->data;
a341ee57 697 hmap_destroy(&bridge->children);
c75d1511
BP
698 free(bridge->name);
699 free(bridge);
700 }
07ff77cc 701 shash_destroy(&vsctl_ctx->bridges);
c75d1511 702
07ff77cc
AW
703 shash_destroy_free_data(&vsctl_ctx->ports);
704 shash_destroy_free_data(&vsctl_ctx->ifaces);
c75d1511
BP
705}
706
e5e12280 707static void
07ff77cc 708pre_get_info(struct ctl_context *ctx)
e5e12280
BP
709{
710 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
711
712 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
713 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
714 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
715 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
716
717 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
718 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
719 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
720 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
721
722 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
99eef98b 723
c3ccfe98 724 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
feb38b6d 725 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_error);
e5e12280
BP
726}
727
c75d1511 728static void
07ff77cc 729vsctl_context_populate_cache(struct ctl_context *ctx)
c75d1511 730{
07ff77cc
AW
731 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
732 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
b3c01ed3 733 struct sset bridges, ports;
c75d1511
BP
734 size_t i;
735
07ff77cc 736 if (vsctl_ctx->cache_valid) {
5ce5a6b5
BP
737 /* Cache is already populated. */
738 return;
739 }
07ff77cc
AW
740 vsctl_ctx->cache_valid = true;
741 shash_init(&vsctl_ctx->bridges);
742 shash_init(&vsctl_ctx->ports);
743 shash_init(&vsctl_ctx->ifaces);
c75d1511 744
b3c01ed3
BP
745 sset_init(&bridges);
746 sset_init(&ports);
c75d1511
BP
747 for (i = 0; i < ovs->n_bridges; i++) {
748 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
749 struct vsctl_bridge *br;
750 size_t j;
751
b3c01ed3 752 if (!sset_add(&bridges, br_cfg->name)) {
c75d1511
BP
753 VLOG_WARN("%s: database contains duplicate bridge name",
754 br_cfg->name);
755 continue;
756 }
07ff77cc 757 br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
c75d1511
BP
758
759 for (j = 0; j < br_cfg->n_ports; j++) {
760 struct ovsrec_port *port_cfg = br_cfg->ports[j];
761
b3c01ed3 762 if (!sset_add(&ports, port_cfg->name)) {
48a69501 763 /* Duplicate port name. (We will warn about that later.) */
c75d1511
BP
764 continue;
765 }
766
767 if (port_is_fake_bridge(port_cfg)
b3c01ed3 768 && sset_add(&bridges, port_cfg->name)) {
07ff77cc 769 add_bridge_to_cache(vsctl_ctx, NULL, port_cfg->name, br,
a341ee57 770 *port_cfg->tag);
c75d1511
BP
771 }
772 }
773 }
b3c01ed3
BP
774 sset_destroy(&bridges);
775 sset_destroy(&ports);
c75d1511 776
b3c01ed3 777 sset_init(&bridges);
c75d1511
BP
778 for (i = 0; i < ovs->n_bridges; i++) {
779 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
780 struct vsctl_bridge *br;
781 size_t j;
782
b3c01ed3 783 if (!sset_add(&bridges, br_cfg->name)) {
c75d1511
BP
784 continue;
785 }
07ff77cc 786 br = shash_find_data(&vsctl_ctx->bridges, br_cfg->name);
c75d1511
BP
787 for (j = 0; j < br_cfg->n_ports; j++) {
788 struct ovsrec_port *port_cfg = br_cfg->ports[j];
789 struct vsctl_port *port;
790 size_t k;
791
07ff77cc 792 port = shash_find_data(&vsctl_ctx->ports, port_cfg->name);
48a69501
BP
793 if (port) {
794 if (port_cfg == port->port_cfg) {
795 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
796 port_cfg->name, br->name, port->bridge->name);
797 } else {
798 /* Log as an error because this violates the database's
799 * uniqueness constraints, so the database server shouldn't
800 * have allowed it. */
801 VLOG_ERR("%s: database contains duplicate port name",
802 port_cfg->name);
803 }
c75d1511
BP
804 continue;
805 }
806
807 if (port_is_fake_bridge(port_cfg)
b3c01ed3 808 && !sset_add(&bridges, port_cfg->name)) {
c75d1511
BP
809 continue;
810 }
811
07ff77cc 812 port = add_port_to_cache(vsctl_ctx, br, port_cfg);
c75d1511
BP
813 for (k = 0; k < port_cfg->n_interfaces; k++) {
814 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
815 struct vsctl_iface *iface;
816
07ff77cc 817 iface = shash_find_data(&vsctl_ctx->ifaces, iface_cfg->name);
48a69501
BP
818 if (iface) {
819 if (iface_cfg == iface->iface_cfg) {
820 VLOG_WARN("%s: interface is in multiple ports "
821 "(%s and %s)",
822 iface_cfg->name,
823 iface->port->port_cfg->name,
824 port->port_cfg->name);
825 } else {
826 /* Log as an error because this violates the database's
827 * uniqueness constraints, so the database server
828 * shouldn't have allowed it. */
829 VLOG_ERR("%s: database contains duplicate interface "
830 "name", iface_cfg->name);
831 }
c75d1511
BP
832 continue;
833 }
834
07ff77cc 835 add_iface_to_cache(vsctl_ctx, port, iface_cfg);
c75d1511
BP
836 }
837 }
838 }
b3c01ed3 839 sset_destroy(&bridges);
c75d1511
BP
840}
841
842static void
07ff77cc 843check_conflicts(struct vsctl_context *vsctl_ctx, const char *name,
c75d1511
BP
844 char *msg)
845{
846 struct vsctl_iface *iface;
847 struct vsctl_port *port;
848
07ff77cc 849 verify_ports(vsctl_ctx);
f74055e7 850
07ff77cc
AW
851 if (shash_find(&vsctl_ctx->bridges, name)) {
852 ctl_fatal("%s because a bridge named %s already exists",
c88b6a27 853 msg, name);
c75d1511
BP
854 }
855
07ff77cc 856 port = shash_find_data(&vsctl_ctx->ports, name);
c75d1511 857 if (port) {
07ff77cc 858 ctl_fatal("%s because a port named %s already exists on "
c88b6a27 859 "bridge %s", msg, name, port->bridge->name);
c75d1511
BP
860 }
861
07ff77cc 862 iface = shash_find_data(&vsctl_ctx->ifaces, name);
c75d1511 863 if (iface) {
07ff77cc 864 ctl_fatal("%s because an interface named %s already exists "
c88b6a27 865 "on bridge %s", msg, name, iface->port->bridge->name);
c75d1511
BP
866 }
867
868 free(msg);
869}
870
871static struct vsctl_bridge *
07ff77cc 872find_bridge(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
c75d1511 873{
5ce5a6b5
BP
874 struct vsctl_bridge *br;
875
07ff77cc 876 ovs_assert(vsctl_ctx->cache_valid);
5ce5a6b5 877
07ff77cc 878 br = shash_find_data(&vsctl_ctx->bridges, name);
01845ce8 879 if (must_exist && !br) {
07ff77cc 880 ctl_fatal("no bridge named %s", name);
c75d1511 881 }
07ff77cc 882 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
c75d1511
BP
883 return br;
884}
885
975ac531 886static struct vsctl_bridge *
07ff77cc
AW
887find_real_bridge(struct vsctl_context *vsctl_ctx,
888 const char *name, bool must_exist)
975ac531 889{
07ff77cc 890 struct vsctl_bridge *br = find_bridge(vsctl_ctx, name, must_exist);
975ac531 891 if (br && br->parent) {
07ff77cc 892 ctl_fatal("%s is a fake bridge", name);
975ac531
JP
893 }
894 return br;
895}
896
c75d1511 897static struct vsctl_port *
07ff77cc 898find_port(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
c75d1511 899{
5ce5a6b5
BP
900 struct vsctl_port *port;
901
07ff77cc 902 ovs_assert(vsctl_ctx->cache_valid);
5ce5a6b5 903
07ff77cc 904 port = shash_find_data(&vsctl_ctx->ports, name);
460aad80 905 if (port && !strcmp(name, port->bridge->name)) {
01845ce8
BP
906 port = NULL;
907 }
908 if (must_exist && !port) {
07ff77cc 909 ctl_fatal("no port named %s", name);
c75d1511 910 }
07ff77cc 911 verify_ports(vsctl_ctx);
c75d1511
BP
912 return port;
913}
914
915static struct vsctl_iface *
07ff77cc 916find_iface(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
c75d1511 917{
5ce5a6b5
BP
918 struct vsctl_iface *iface;
919
07ff77cc 920 ovs_assert(vsctl_ctx->cache_valid);
5ce5a6b5 921
07ff77cc 922 iface = shash_find_data(&vsctl_ctx->ifaces, name);
460aad80 923 if (iface && !strcmp(name, iface->port->bridge->name)) {
01845ce8
BP
924 iface = NULL;
925 }
926 if (must_exist && !iface) {
07ff77cc 927 ctl_fatal("no interface named %s", name);
c75d1511 928 }
07ff77cc 929 verify_ports(vsctl_ctx);
c75d1511
BP
930 return iface;
931}
932
933static void
934bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
935{
936 struct ovsrec_port **ports;
937 size_t i;
938
939 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
940 for (i = 0; i < br->n_ports; i++) {
941 ports[i] = br->ports[i];
942 }
c75d1511
BP
943 ports[br->n_ports] = port;
944 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
945 free(ports);
946}
947
948static void
949bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
950{
951 struct ovsrec_port **ports;
952 size_t i, n;
953
954 ports = xmalloc(sizeof *br->ports * br->n_ports);
955 for (i = n = 0; i < br->n_ports; i++) {
956 if (br->ports[i] != port) {
957 ports[n++] = br->ports[i];
958 }
959 }
960 ovsrec_bridge_set_ports(br, ports, n);
961 free(ports);
962}
963
964static void
965ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
966 struct ovsrec_bridge *bridge)
967{
968 struct ovsrec_bridge **bridges;
969 size_t i;
970
971 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
972 for (i = 0; i < ovs->n_bridges; i++) {
973 bridges[i] = ovs->bridges[i];
974 }
975 bridges[ovs->n_bridges] = bridge;
976 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
977 free(bridges);
978}
979
524555d1 980static void
07ff77cc 981cmd_init(struct ctl_context *ctx OVS_UNUSED)
524555d1
BP
982{
983}
984
d33340a5 985static struct cmd_show_table cmd_show_tables[] = {
9b1735a7
BP
986 {&ovsrec_table_open_vswitch,
987 NULL,
988 {&ovsrec_open_vswitch_col_manager_options,
989 &ovsrec_open_vswitch_col_bridges,
016e4684
AW
990 &ovsrec_open_vswitch_col_ovs_version},
991 {NULL, NULL, NULL}
6530be3b 992 },
9b1735a7
BP
993
994 {&ovsrec_table_bridge,
995 &ovsrec_bridge_col_name,
996 {&ovsrec_bridge_col_controller,
997 &ovsrec_bridge_col_fail_mode,
016e4684
AW
998 &ovsrec_bridge_col_ports},
999 {NULL, NULL, NULL}
6530be3b 1000 },
9b1735a7
BP
1001
1002 {&ovsrec_table_port,
1003 &ovsrec_port_col_name,
1004 {&ovsrec_port_col_tag,
1005 &ovsrec_port_col_trunks,
016e4684
AW
1006 &ovsrec_port_col_interfaces},
1007 {NULL, NULL, NULL}
6530be3b 1008 },
9b1735a7
BP
1009
1010 {&ovsrec_table_interface,
1011 &ovsrec_interface_col_name,
1012 {&ovsrec_interface_col_type,
1013 &ovsrec_interface_col_options,
016e4684
AW
1014 &ovsrec_interface_col_error},
1015 {NULL, NULL, NULL}
6530be3b 1016 },
9b1735a7
BP
1017
1018 {&ovsrec_table_controller,
1019 &ovsrec_controller_col_target,
1020 {&ovsrec_controller_col_is_connected,
1021 NULL,
016e4684
AW
1022 NULL},
1023 {NULL, NULL, NULL}
6530be3b 1024 },
9b1735a7
BP
1025
1026 {&ovsrec_table_manager,
1027 &ovsrec_manager_col_target,
1028 {&ovsrec_manager_col_is_connected,
1029 NULL,
016e4684
AW
1030 NULL},
1031 {NULL, NULL, NULL}
6530be3b 1032 },
9b1735a7 1033
016e4684 1034 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
af046a16 1035};
9b1735a7 1036
e5e12280 1037static void
07ff77cc 1038pre_cmd_emer_reset(struct ctl_context *ctx)
e5e12280 1039{
87824b0b 1040 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
e5e12280
BP
1041 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1042
1043 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
f67e3b66 1044 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
e5e12280
BP
1045 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1046 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1047 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
29089a54 1048 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ipfix);
e5e12280
BP
1049 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1050 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1051
1052 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1053
1054 ovsdb_idl_add_column(ctx->idl,
1055 &ovsrec_interface_col_ingress_policing_rate);
1056 ovsdb_idl_add_column(ctx->idl,
1057 &ovsrec_interface_col_ingress_policing_burst);
1058}
1059
18ee958b 1060static void
07ff77cc 1061cmd_emer_reset(struct ctl_context *ctx)
18ee958b 1062{
07ff77cc 1063 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
18ee958b
JP
1064 const struct ovsdb_idl *idl = ctx->idl;
1065 const struct ovsrec_bridge *br;
1066 const struct ovsrec_port *port;
1067 const struct ovsrec_interface *iface;
28a14bf3
EJ
1068 const struct ovsrec_mirror *mirror, *next_mirror;
1069 const struct ovsrec_controller *ctrl, *next_ctrl;
1070 const struct ovsrec_manager *mgr, *next_mgr;
1071 const struct ovsrec_netflow *nf, *next_nf;
1072 const struct ovsrec_ssl *ssl, *next_ssl;
1073 const struct ovsrec_sflow *sflow, *next_sflow;
29089a54
RL
1074 const struct ovsrec_ipfix *ipfix, *next_ipfix;
1075 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
18ee958b 1076
18ee958b 1077 /* Reset the Open_vSwitch table. */
07ff77cc
AW
1078 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, NULL, 0);
1079 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
18ee958b
JP
1080
1081 OVSREC_BRIDGE_FOR_EACH (br, idl) {
a699f614 1082 const char *hwaddr;
18ee958b
JP
1083
1084 ovsrec_bridge_set_controller(br, NULL, 0);
f67e3b66 1085 ovsrec_bridge_set_fail_mode(br, NULL);
18ee958b
JP
1086 ovsrec_bridge_set_mirrors(br, NULL, 0);
1087 ovsrec_bridge_set_netflow(br, NULL);
1088 ovsrec_bridge_set_sflow(br, NULL);
29089a54 1089 ovsrec_bridge_set_ipfix(br, NULL);
18ee958b
JP
1090 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1091
1092 /* We only want to save the "hwaddr" key from other_config. */
a699f614
EJ
1093 hwaddr = smap_get(&br->other_config, "hwaddr");
1094 if (hwaddr) {
aaf881c6 1095 const struct smap smap = SMAP_CONST1(&smap, "hwaddr", hwaddr);
a699f614 1096 ovsrec_bridge_set_other_config(br, &smap);
18ee958b 1097 } else {
a699f614 1098 ovsrec_bridge_set_other_config(br, NULL);
18ee958b
JP
1099 }
1100 }
1101
1102 OVSREC_PORT_FOR_EACH (port, idl) {
a699f614 1103 ovsrec_port_set_other_config(port, NULL);
18ee958b
JP
1104 }
1105
1106 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1107 /* xxx What do we do about gre/patch devices created by mgr? */
1108
1109 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1110 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1111 }
28a14bf3
EJ
1112
1113 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1114 ovsrec_mirror_delete(mirror);
1115 }
1116
1117 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1118 ovsrec_controller_delete(ctrl);
1119 }
1120
1121 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1122 ovsrec_manager_delete(mgr);
1123 }
1124
1125 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1126 ovsrec_netflow_delete(nf);
1127 }
1128
1129 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1130 ovsrec_ssl_delete(ssl);
1131 }
1132
1133 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1134 ovsrec_sflow_delete(sflow);
1135 }
5ce5a6b5 1136
29089a54
RL
1137 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix, next_ipfix, idl) {
1138 ovsrec_ipfix_delete(ipfix);
1139 }
1140
1141 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset, idl) {
1142 ovsrec_flow_sample_collector_set_delete(fscset);
1143 }
1144
5ce5a6b5 1145 vsctl_context_invalidate_cache(ctx);
18ee958b
JP
1146}
1147
c75d1511 1148static void
07ff77cc 1149cmd_add_br(struct ctl_context *ctx)
c75d1511 1150{
07ff77cc 1151 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
e3c17733 1152 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
b89d8339 1153 const char *br_name, *parent_name;
c3ccfe98 1154 struct ovsrec_interface *iface;
b89d8339 1155 int vlan;
c75d1511 1156
aeee85aa 1157 br_name = ctx->argv[1];
f75612a1
BP
1158 if (!br_name[0]) {
1159 ctl_fatal("bridge name must not be empty string");
1160 }
aeee85aa
BP
1161 if (ctx->argc == 2) {
1162 parent_name = NULL;
1163 vlan = 0;
1164 } else if (ctx->argc == 4) {
1165 parent_name = ctx->argv[2];
1166 vlan = atoi(ctx->argv[3]);
5341d046 1167 if (vlan < 0 || vlan > 4095) {
07ff77cc 1168 ctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
aeee85aa
BP
1169 }
1170 } else {
07ff77cc 1171 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
aeee85aa
BP
1172 ctx->argv[0]);
1173 }
1174
5ce5a6b5 1175 vsctl_context_populate_cache(ctx);
aeee85aa
BP
1176 if (may_exist) {
1177 struct vsctl_bridge *br;
1178
07ff77cc 1179 br = find_bridge(vsctl_ctx, br_name, false);
aeee85aa
BP
1180 if (br) {
1181 if (!parent_name) {
1182 if (br->parent) {
07ff77cc 1183 ctl_fatal("\"--may-exist add-br %s\" but %s is "
aeee85aa
BP
1184 "a VLAN bridge for VLAN %d",
1185 br_name, br_name, br->vlan);
1186 }
1187 } else {
1188 if (!br->parent) {
07ff77cc 1189 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
aeee85aa
BP
1190 "is not a VLAN bridge",
1191 br_name, parent_name, vlan, br_name);
1192 } else if (strcmp(br->parent->name, parent_name)) {
07ff77cc 1193 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
aeee85aa
BP
1194 "has the wrong parent %s",
1195 br_name, parent_name, vlan,
1196 br_name, br->parent->name);
1197 } else if (br->vlan != vlan) {
07ff77cc 1198 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
aeee85aa
BP
1199 "is a VLAN bridge for the wrong VLAN %d",
1200 br_name, parent_name, vlan, br_name, br->vlan);
1201 }
1202 }
1203 return;
1204 }
1205 }
07ff77cc 1206 check_conflicts(vsctl_ctx, br_name,
c75d1511
BP
1207 xasprintf("cannot create a bridge named %s", br_name));
1208
aeee85aa 1209 if (!parent_name) {
c75d1511 1210 struct ovsrec_port *port;
aeee85aa 1211 struct ovsrec_bridge *br;
c75d1511 1212
f8ff4bc4 1213 iface = ovsrec_interface_insert(ctx->txn);
c75d1511 1214 ovsrec_interface_set_name(iface, br_name);
9106d88c 1215 ovsrec_interface_set_type(iface, "internal");
c75d1511 1216
f8ff4bc4 1217 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1218 ovsrec_port_set_name(port, br_name);
1219 ovsrec_port_set_interfaces(port, &iface, 1);
1220
f8ff4bc4 1221 br = ovsrec_bridge_insert(ctx->txn);
c75d1511
BP
1222 ovsrec_bridge_set_name(br, br_name);
1223 ovsrec_bridge_set_ports(br, &port, 1);
1224
07ff77cc 1225 ovs_insert_bridge(vsctl_ctx->ovs, br);
aeee85aa 1226 } else {
5dd9826c 1227 struct vsctl_bridge *conflict;
c75d1511
BP
1228 struct vsctl_bridge *parent;
1229 struct ovsrec_port *port;
aeee85aa 1230 struct ovsrec_bridge *br;
c75d1511
BP
1231 int64_t tag = vlan;
1232
07ff77cc 1233 parent = find_bridge(vsctl_ctx, parent_name, false);
5341d046 1234 if (parent && parent->parent) {
07ff77cc 1235 ctl_fatal("cannot create bridge with fake bridge as parent");
c75d1511
BP
1236 }
1237 if (!parent) {
07ff77cc 1238 ctl_fatal("parent bridge %s does not exist", parent_name);
c75d1511 1239 }
5dd9826c
BP
1240 conflict = find_vlan_bridge(parent, vlan);
1241 if (conflict) {
07ff77cc 1242 ctl_fatal("bridge %s already has a child VLAN bridge %s "
5dd9826c
BP
1243 "on VLAN %d", parent_name, conflict->name, vlan);
1244 }
c75d1511
BP
1245 br = parent->br_cfg;
1246
f8ff4bc4 1247 iface = ovsrec_interface_insert(ctx->txn);
c75d1511
BP
1248 ovsrec_interface_set_name(iface, br_name);
1249 ovsrec_interface_set_type(iface, "internal");
1250
f8ff4bc4 1251 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1252 ovsrec_port_set_name(port, br_name);
1253 ovsrec_port_set_interfaces(port, &iface, 1);
1254 ovsrec_port_set_fake_bridge(port, true);
1255 ovsrec_port_set_tag(port, &tag, 1);
dfbe07ba
BP
1256
1257 bridge_insert_port(br, port);
c75d1511
BP
1258 }
1259
c3ccfe98 1260 post_db_reload_expect_iface(iface);
5ce5a6b5 1261 vsctl_context_invalidate_cache(ctx);
c75d1511
BP
1262}
1263
1264static void
07ff77cc 1265del_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
c75d1511 1266{
a341ee57 1267 struct vsctl_iface *iface, *next_iface;
28a14bf3 1268
c75d1511
BP
1269 bridge_delete_port((port->bridge->parent
1270 ? port->bridge->parent->br_cfg
1271 : port->bridge->br_cfg), port->port_cfg);
a341ee57
BP
1272
1273 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
07ff77cc 1274 del_cached_iface(vsctl_ctx, iface);
a341ee57 1275 }
07ff77cc 1276 del_cached_port(vsctl_ctx, port);
a341ee57
BP
1277}
1278
1279static void
07ff77cc 1280del_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
a341ee57
BP
1281{
1282 struct vsctl_bridge *child, *next_child;
1283 struct vsctl_port *port, *next_port;
29089a54 1284 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
a341ee57
BP
1285
1286 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
07ff77cc 1287 del_bridge(vsctl_ctx, child);
a341ee57
BP
1288 }
1289
1290 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
07ff77cc 1291 del_port(vsctl_ctx, port);
a341ee57
BP
1292 }
1293
29089a54 1294 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset,
07ff77cc 1295 vsctl_ctx->base.idl) {
29089a54
RL
1296 if (fscset->bridge == br->br_cfg) {
1297 ovsrec_flow_sample_collector_set_delete(fscset);
1298 }
1299 }
1300
07ff77cc 1301 del_cached_bridge(vsctl_ctx, br);
c75d1511
BP
1302}
1303
1304static void
07ff77cc 1305cmd_del_br(struct ctl_context *ctx)
c75d1511 1306{
07ff77cc 1307 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
460aad80 1308 bool must_exist = !shash_find(&ctx->options, "--if-exists");
c75d1511
BP
1309 struct vsctl_bridge *bridge;
1310
5ce5a6b5 1311 vsctl_context_populate_cache(ctx);
07ff77cc 1312 bridge = find_bridge(vsctl_ctx, ctx->argv[1], must_exist);
460aad80 1313 if (bridge) {
07ff77cc 1314 del_bridge(vsctl_ctx, bridge);
c75d1511 1315 }
c75d1511
BP
1316}
1317
dfbe07ba
BP
1318static void
1319output_sorted(struct svec *svec, struct ds *output)
1320{
1321 const char *name;
1322 size_t i;
1323
1324 svec_sort(svec);
1325 SVEC_FOR_EACH (i, name, svec) {
1326 ds_put_format(output, "%s\n", name);
1327 }
1328}
1329
c75d1511 1330static void
07ff77cc 1331cmd_list_br(struct ctl_context *ctx)
c75d1511 1332{
07ff77cc 1333 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1334 struct shash_node *node;
dfbe07ba 1335 struct svec bridges;
515d830a
JP
1336 bool real = shash_find(&ctx->options, "--real");
1337 bool fake = shash_find(&ctx->options, "--fake");
1338
1339 /* If neither fake nor real were requested, return both. */
1340 if (!real && !fake) {
1341 real = fake = true;
1342 }
c75d1511 1343
5ce5a6b5 1344 vsctl_context_populate_cache(ctx);
dfbe07ba
BP
1345
1346 svec_init(&bridges);
07ff77cc 1347 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
c75d1511 1348 struct vsctl_bridge *br = node->data;
515d830a
JP
1349
1350 if (br->parent ? fake : real) {
1351 svec_add(&bridges, br->name);
1352 }
c75d1511 1353 }
5d9cb63c 1354 output_sorted(&bridges, &ctx->output);
dfbe07ba 1355 svec_destroy(&bridges);
c75d1511
BP
1356}
1357
1358static void
07ff77cc 1359cmd_br_exists(struct ctl_context *ctx)
c75d1511 1360{
07ff77cc
AW
1361 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1362
5ce5a6b5 1363 vsctl_context_populate_cache(ctx);
07ff77cc 1364 if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
ce6f1d1f 1365 vsctl_exit(2);
c75d1511 1366 }
c75d1511
BP
1367}
1368
457e1eb0 1369static void
a699f614
EJ
1370set_external_id(struct smap *old, struct smap *new,
1371 char *key, char *value)
457e1eb0 1372{
a699f614 1373 smap_clone(new, old);
457e1eb0 1374
457e1eb0 1375 if (value) {
a699f614
EJ
1376 smap_replace(new, key, value);
1377 } else {
1378 smap_remove(new, key);
457e1eb0 1379 }
457e1eb0
BP
1380}
1381
e5e12280 1382static void
07ff77cc 1383pre_cmd_br_set_external_id(struct ctl_context *ctx)
e5e12280
BP
1384{
1385 pre_get_info(ctx);
1386 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1387 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1388}
1389
457e1eb0 1390static void
07ff77cc 1391cmd_br_set_external_id(struct ctl_context *ctx)
457e1eb0 1392{
07ff77cc 1393 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
457e1eb0 1394 struct vsctl_bridge *bridge;
a699f614 1395 struct smap new;
457e1eb0 1396
5ce5a6b5 1397 vsctl_context_populate_cache(ctx);
07ff77cc 1398 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
457e1eb0 1399 if (bridge->br_cfg) {
a699f614
EJ
1400
1401 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1402 ctx->argc >= 4 ? ctx->argv[3] : NULL);
f74055e7 1403 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
a699f614 1404 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
457e1eb0 1405 } else {
5d9cb63c 1406 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
07ff77cc
AW
1407 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1408 ctx->argv[1]);
a699f614
EJ
1409 set_external_id(&port->port_cfg->external_ids, &new,
1410 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
f74055e7 1411 ovsrec_port_verify_external_ids(port->port_cfg);
a699f614 1412 ovsrec_port_set_external_ids(port->port_cfg, &new);
457e1eb0
BP
1413 free(key);
1414 }
a699f614 1415 smap_destroy(&new);
457e1eb0
BP
1416}
1417
1418static void
a699f614 1419get_external_id(struct smap *smap, const char *prefix, const char *key,
457e1eb0
BP
1420 struct ds *output)
1421{
a699f614
EJ
1422 if (key) {
1423 char *prefix_key = xasprintf("%s%s", prefix, key);
1424 const char *value = smap_get(smap, prefix_key);
457e1eb0 1425
a699f614
EJ
1426 if (value) {
1427 ds_put_format(output, "%s\n", value);
1428 }
1429 free(prefix_key);
1430 } else {
1431 const struct smap_node **sorted = smap_sort(smap);
1432 size_t prefix_len = strlen(prefix);
1433 size_t i;
1434
1435 for (i = 0; i < smap_count(smap); i++) {
1436 const struct smap_node *node = sorted[i];
1437 if (!strncmp(node->key, prefix, prefix_len)) {
1438 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1439 node->value);
1440 }
457e1eb0 1441 }
a699f614 1442 free(sorted);
457e1eb0 1443 }
457e1eb0
BP
1444}
1445
e5e12280 1446static void
07ff77cc 1447pre_cmd_br_get_external_id(struct ctl_context *ctx)
e5e12280
BP
1448{
1449 pre_cmd_br_set_external_id(ctx);
1450}
1451
457e1eb0 1452static void
07ff77cc 1453cmd_br_get_external_id(struct ctl_context *ctx)
457e1eb0 1454{
07ff77cc 1455 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
457e1eb0
BP
1456 struct vsctl_bridge *bridge;
1457
5ce5a6b5
BP
1458 vsctl_context_populate_cache(ctx);
1459
07ff77cc 1460 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
457e1eb0 1461 if (bridge->br_cfg) {
f74055e7 1462 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
a699f614
EJ
1463 get_external_id(&bridge->br_cfg->external_ids, "",
1464 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
457e1eb0 1465 } else {
07ff77cc
AW
1466 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1467 ctx->argv[1]);
f74055e7 1468 ovsrec_port_verify_external_ids(port->port_cfg);
a699f614
EJ
1469 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1470 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
457e1eb0 1471 }
457e1eb0
BP
1472}
1473
c75d1511 1474static void
07ff77cc 1475cmd_list_ports(struct ctl_context *ctx)
c75d1511 1476{
07ff77cc 1477 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1478 struct vsctl_bridge *br;
a341ee57 1479 struct vsctl_port *port;
dfbe07ba 1480 struct svec ports;
c75d1511 1481
5ce5a6b5 1482 vsctl_context_populate_cache(ctx);
07ff77cc 1483 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
f74055e7 1484 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
dfbe07ba
BP
1485
1486 svec_init(&ports);
a341ee57
BP
1487 LIST_FOR_EACH (port, ports_node, &br->ports) {
1488 if (strcmp(port->port_cfg->name, br->name)) {
dfbe07ba 1489 svec_add(&ports, port->port_cfg->name);
c75d1511
BP
1490 }
1491 }
5d9cb63c 1492 output_sorted(&ports, &ctx->output);
dfbe07ba 1493 svec_destroy(&ports);
c75d1511
BP
1494}
1495
1496static void
07ff77cc 1497add_port(struct ctl_context *ctx,
bb1c67c8
BP
1498 const char *br_name, const char *port_name,
1499 bool may_exist, bool fake_iface,
18b239f5
BP
1500 char *iface_names[], int n_ifaces,
1501 char *settings[], int n_settings)
c75d1511 1502{
07ff77cc 1503 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
a341ee57 1504 struct vsctl_port *vsctl_port;
c75d1511
BP
1505 struct vsctl_bridge *bridge;
1506 struct ovsrec_interface **ifaces;
1507 struct ovsrec_port *port;
1508 size_t i;
1509
f75612a1
BP
1510 if (!port_name[0]) {
1511 ctl_fatal("port name must not be empty string");
1512 }
1513 for (i = 0; i < n_ifaces; i++) {
1514 if (!iface_names[i][0]) {
1515 ctl_fatal("interface name must not be empty string");
1516 }
1517 }
1518
5ce5a6b5 1519 vsctl_context_populate_cache(ctx);
bb1c67c8 1520 if (may_exist) {
2a022368 1521 struct vsctl_port *vsctl_port;
bb1c67c8 1522
07ff77cc 1523 vsctl_port = find_port(vsctl_ctx, port_name, false);
2a022368 1524 if (vsctl_port) {
bb1c67c8 1525 struct svec want_names, have_names;
bb1c67c8
BP
1526
1527 svec_init(&want_names);
1528 for (i = 0; i < n_ifaces; i++) {
1529 svec_add(&want_names, iface_names[i]);
1530 }
1531 svec_sort(&want_names);
1532
1533 svec_init(&have_names);
2a022368
BP
1534 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1535 svec_add(&have_names,
1536 vsctl_port->port_cfg->interfaces[i]->name);
bb1c67c8
BP
1537 }
1538 svec_sort(&have_names);
1539
2a022368 1540 if (strcmp(vsctl_port->bridge->name, br_name)) {
bb1c67c8 1541 char *command = vsctl_context_to_string(ctx);
07ff77cc 1542 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
2a022368 1543 command, port_name, vsctl_port->bridge->name);
bb1c67c8
BP
1544 }
1545
1546 if (!svec_equal(&want_names, &have_names)) {
1547 char *have_names_string = svec_join(&have_names, ", ", "");
1548 char *command = vsctl_context_to_string(ctx);
1549
07ff77cc 1550 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
bb1c67c8
BP
1551 command, port_name, have_names_string);
1552 }
1553
1554 svec_destroy(&want_names);
1555 svec_destroy(&have_names);
1556
1557 return;
1558 }
1559 }
07ff77cc 1560 check_conflicts(vsctl_ctx, port_name,
c75d1511 1561 xasprintf("cannot create a port named %s", port_name));
bb1c67c8 1562 for (i = 0; i < n_ifaces; i++) {
07ff77cc 1563 check_conflicts(vsctl_ctx, iface_names[i],
bb1c67c8
BP
1564 xasprintf("cannot create an interface named %s",
1565 iface_names[i]));
1566 }
07ff77cc 1567 bridge = find_bridge(vsctl_ctx, br_name, true);
c75d1511
BP
1568
1569 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1570 for (i = 0; i < n_ifaces; i++) {
f8ff4bc4 1571 ifaces[i] = ovsrec_interface_insert(ctx->txn);
c75d1511 1572 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
c3ccfe98 1573 post_db_reload_expect_iface(ifaces[i]);
c75d1511
BP
1574 }
1575
f8ff4bc4 1576 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1577 ovsrec_port_set_name(port, port_name);
1578 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
b4182c7f 1579 ovsrec_port_set_bond_fake_iface(port, fake_iface);
a0a9f31d 1580
5341d046 1581 if (bridge->parent) {
c75d1511
BP
1582 int64_t tag = bridge->vlan;
1583 ovsrec_port_set_tag(port, &tag, 1);
1584 }
1585
18b239f5 1586 for (i = 0; i < n_settings; i++) {
15ffc202
AZ
1587 ctl_set_column("Port", &port->header_, settings[i],
1588 ctx->symtab);
18b239f5
BP
1589 }
1590
c75d1511
BP
1591 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1592 : bridge->br_cfg), port);
1593
07ff77cc 1594 vsctl_port = add_port_to_cache(vsctl_ctx, bridge, port);
a341ee57 1595 for (i = 0; i < n_ifaces; i++) {
07ff77cc 1596 add_iface_to_cache(vsctl_ctx, vsctl_port, ifaces[i]);
a341ee57
BP
1597 }
1598 free(ifaces);
c75d1511
BP
1599}
1600
1601static void
07ff77cc 1602cmd_add_port(struct ctl_context *ctx)
c75d1511 1603{
e3c17733 1604 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
bb1c67c8
BP
1605
1606 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
18b239f5 1607 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
c75d1511
BP
1608}
1609
1610static void
07ff77cc 1611cmd_add_bond(struct ctl_context *ctx)
c75d1511 1612{
e3c17733 1613 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
b4182c7f 1614 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
18b239f5
BP
1615 int n_ifaces;
1616 int i;
1617
1618 n_ifaces = ctx->argc - 3;
1619 for (i = 3; i < ctx->argc; i++) {
1620 if (strchr(ctx->argv[i], '=')) {
1621 n_ifaces = i - 3;
1622 break;
1623 }
1624 }
1625 if (n_ifaces < 2) {
07ff77cc 1626 ctl_fatal("add-bond requires at least 2 interfaces, but only "
18b239f5
BP
1627 "%d were specified", n_ifaces);
1628 }
b4182c7f 1629
bb1c67c8 1630 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
18b239f5
BP
1631 &ctx->argv[3], n_ifaces,
1632 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
c75d1511
BP
1633}
1634
1635static void
07ff77cc 1636cmd_del_port(struct ctl_context *ctx)
c75d1511 1637{
07ff77cc 1638 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
460aad80 1639 bool must_exist = !shash_find(&ctx->options, "--if-exists");
7c79588e 1640 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
89f3c258 1641 const char *target = ctx->argv[ctx->argc - 1];
7c79588e 1642 struct vsctl_port *port;
c75d1511 1643
5ce5a6b5 1644 vsctl_context_populate_cache(ctx);
07ff77cc 1645 if (find_bridge(vsctl_ctx, target, false)) {
25a27ba0 1646 if (must_exist) {
07ff77cc 1647 ctl_fatal("cannot delete port %s because it is the local port "
25a27ba0
BP
1648 "for bridge %s (deleting this port requires deleting "
1649 "the entire bridge)", target, target);
1650 }
1651 port = NULL;
89f3c258 1652 } else if (!with_iface) {
07ff77cc 1653 port = find_port(vsctl_ctx, target, must_exist);
7c79588e 1654 } else {
7c79588e
BP
1655 struct vsctl_iface *iface;
1656
07ff77cc 1657 port = find_port(vsctl_ctx, target, false);
7c79588e 1658 if (!port) {
07ff77cc 1659 iface = find_iface(vsctl_ctx, target, false);
7c79588e
BP
1660 if (iface) {
1661 port = iface->port;
1662 }
1663 }
1664 if (must_exist && !port) {
07ff77cc 1665 ctl_fatal("no port or interface named %s", target);
460aad80 1666 }
7c79588e 1667 }
460aad80 1668
7c79588e
BP
1669 if (port) {
1670 if (ctx->argc == 3) {
1671 struct vsctl_bridge *bridge;
1672
07ff77cc 1673 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
7c79588e
BP
1674 if (port->bridge != bridge) {
1675 if (port->bridge->parent == bridge) {
07ff77cc 1676 ctl_fatal("bridge %s does not have a port %s (although "
7c79588e
BP
1677 "its parent bridge %s does)",
1678 ctx->argv[1], ctx->argv[2],
1679 bridge->parent->name);
1680 } else {
07ff77cc 1681 ctl_fatal("bridge %s does not have a port %s",
7c79588e
BP
1682 ctx->argv[1], ctx->argv[2]);
1683 }
460aad80 1684 }
c75d1511 1685 }
7c79588e 1686
07ff77cc 1687 del_port(vsctl_ctx, port);
c75d1511 1688 }
c75d1511
BP
1689}
1690
1691static void
07ff77cc 1692cmd_port_to_br(struct ctl_context *ctx)
c75d1511 1693{
07ff77cc 1694 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1695 struct vsctl_port *port;
c75d1511 1696
5ce5a6b5
BP
1697 vsctl_context_populate_cache(ctx);
1698
07ff77cc 1699 port = find_port(vsctl_ctx, ctx->argv[1], true);
5d9cb63c 1700 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
c75d1511
BP
1701}
1702
1703static void
07ff77cc 1704cmd_br_to_vlan(struct ctl_context *ctx)
c75d1511 1705{
07ff77cc 1706 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1707 struct vsctl_bridge *bridge;
c75d1511 1708
5ce5a6b5
BP
1709 vsctl_context_populate_cache(ctx);
1710
07ff77cc 1711 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
5d9cb63c 1712 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
c75d1511
BP
1713}
1714
1715static void
07ff77cc 1716cmd_br_to_parent(struct ctl_context *ctx)
c75d1511 1717{
07ff77cc 1718 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1719 struct vsctl_bridge *bridge;
c75d1511 1720
5ce5a6b5
BP
1721 vsctl_context_populate_cache(ctx);
1722
07ff77cc 1723 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
c75d1511
BP
1724 if (bridge->parent) {
1725 bridge = bridge->parent;
1726 }
5d9cb63c 1727 ds_put_format(&ctx->output, "%s\n", bridge->name);
c75d1511
BP
1728}
1729
1730static void
07ff77cc 1731cmd_list_ifaces(struct ctl_context *ctx)
c75d1511 1732{
07ff77cc 1733 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1734 struct vsctl_bridge *br;
a341ee57 1735 struct vsctl_port *port;
dfbe07ba 1736 struct svec ifaces;
c75d1511 1737
5ce5a6b5
BP
1738 vsctl_context_populate_cache(ctx);
1739
07ff77cc
AW
1740 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1741 verify_ports(vsctl_ctx);
dfbe07ba
BP
1742
1743 svec_init(&ifaces);
a341ee57
BP
1744 LIST_FOR_EACH (port, ports_node, &br->ports) {
1745 struct vsctl_iface *iface;
c75d1511 1746
a341ee57
BP
1747 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1748 if (strcmp(iface->iface_cfg->name, br->name)) {
1749 svec_add(&ifaces, iface->iface_cfg->name);
1750 }
c75d1511
BP
1751 }
1752 }
5d9cb63c 1753 output_sorted(&ifaces, &ctx->output);
dfbe07ba 1754 svec_destroy(&ifaces);
c75d1511
BP
1755}
1756
1757static void
07ff77cc 1758cmd_iface_to_br(struct ctl_context *ctx)
c75d1511 1759{
07ff77cc 1760 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
c75d1511 1761 struct vsctl_iface *iface;
c75d1511 1762
5ce5a6b5
BP
1763 vsctl_context_populate_cache(ctx);
1764
07ff77cc 1765 iface = find_iface(vsctl_ctx, ctx->argv[1], true);
5d9cb63c 1766 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
c75d1511 1767}
457e1eb0 1768
f74055e7
BP
1769static void
1770verify_controllers(struct ovsrec_bridge *bridge)
1771{
7da6c3a6 1772 size_t i;
f74055e7 1773
7da6c3a6
BP
1774 ovsrec_bridge_verify_controller(bridge);
1775 for (i = 0; i < bridge->n_controller; i++) {
1776 ovsrec_controller_verify_target(bridge->controller[i]);
f74055e7
BP
1777 }
1778}
1779
4e3e7ff9 1780static void
07ff77cc 1781pre_controller(struct ctl_context *ctx)
4e3e7ff9
BP
1782{
1783 pre_get_info(ctx);
1784
1785 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1786}
1787
76ce9432 1788static void
07ff77cc 1789cmd_get_controller(struct ctl_context *ctx)
76ce9432 1790{
07ff77cc 1791 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1a048029 1792 struct vsctl_bridge *br;
76ce9432
BP
1793 struct svec targets;
1794 size_t i;
1795
5ce5a6b5
BP
1796 vsctl_context_populate_cache(ctx);
1797
07ff77cc 1798 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
7da6c3a6
BP
1799 if (br->parent) {
1800 br = br->parent;
1801 }
f74055e7 1802 verify_controllers(br->br_cfg);
1a048029
JP
1803
1804 /* Print the targets in sorted order for reproducibility. */
76ce9432 1805 svec_init(&targets);
286a2e82
BP
1806 for (i = 0; i < br->br_cfg->n_controller; i++) {
1807 svec_add(&targets, br->br_cfg->controller[i]->target);
76ce9432
BP
1808 }
1809
1810 svec_sort(&targets);
1811 for (i = 0; i < targets.n; i++) {
1812 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1813 }
1814 svec_destroy(&targets);
5aa00635
JP
1815}
1816
28a14bf3
EJ
1817static void
1818delete_controllers(struct ovsrec_controller **controllers,
1819 size_t n_controllers)
1820{
1821 size_t i;
1822
1823 for (i = 0; i < n_controllers; i++) {
1824 ovsrec_controller_delete(controllers[i]);
1825 }
1826}
1827
5aa00635 1828static void
07ff77cc 1829cmd_del_controller(struct ctl_context *ctx)
5aa00635 1830{
07ff77cc 1831 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
286a2e82 1832 struct ovsrec_bridge *br;
5aa00635 1833
5ce5a6b5 1834 vsctl_context_populate_cache(ctx);
5aa00635 1835
07ff77cc 1836 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
286a2e82 1837 verify_controllers(br);
5aa00635 1838
286a2e82
BP
1839 if (br->controller) {
1840 delete_controllers(br->controller, br->n_controller);
1841 ovsrec_bridge_set_controller(br, NULL, 0);
5ce5a6b5 1842 }
5aa00635
JP
1843}
1844
76ce9432
BP
1845static struct ovsrec_controller **
1846insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1847{
1848 struct ovsrec_controller **controllers;
1849 size_t i;
1850
1851 controllers = xmalloc(n * sizeof *controllers);
1852 for (i = 0; i < n; i++) {
070723f9
JP
1853 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
1854 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1855 }
76ce9432
BP
1856 controllers[i] = ovsrec_controller_insert(txn);
1857 ovsrec_controller_set_target(controllers[i], targets[i]);
1858 }
1859
1860 return controllers;
1861}
1862
5aa00635 1863static void
07ff77cc 1864cmd_set_controller(struct ctl_context *ctx)
5aa00635 1865{
07ff77cc 1866 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1a048029 1867 struct ovsrec_controller **controllers;
286a2e82 1868 struct ovsrec_bridge *br;
1a048029 1869 size_t n;
5aa00635 1870
5ce5a6b5
BP
1871 vsctl_context_populate_cache(ctx);
1872
07ff77cc 1873 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
286a2e82 1874 verify_controllers(br);
28a14bf3 1875
286a2e82 1876 delete_controllers(br->controller, br->n_controller);
76ce9432 1877
1a048029
JP
1878 n = ctx->argc - 2;
1879 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
286a2e82 1880 ovsrec_bridge_set_controller(br, controllers, n);
1a048029 1881 free(controllers);
5aa00635
JP
1882}
1883
1884static void
07ff77cc 1885cmd_get_fail_mode(struct ctl_context *ctx)
5aa00635 1886{
07ff77cc 1887 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1a048029 1888 struct vsctl_bridge *br;
753cb20f 1889 const char *fail_mode;
5aa00635 1890
5ce5a6b5 1891 vsctl_context_populate_cache(ctx);
07ff77cc 1892 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
5aa00635 1893
753cb20f
BP
1894 if (br->parent) {
1895 br = br->parent;
1896 }
1897 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1898
1899 fail_mode = br->br_cfg->fail_mode;
1900 if (fail_mode && strlen(fail_mode)) {
1901 ds_put_format(&ctx->output, "%s\n", fail_mode);
5aa00635 1902 }
5aa00635
JP
1903}
1904
1905static void
07ff77cc 1906cmd_del_fail_mode(struct ctl_context *ctx)
5aa00635 1907{
07ff77cc 1908 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1a048029 1909 struct vsctl_bridge *br;
5aa00635 1910
5ce5a6b5 1911 vsctl_context_populate_cache(ctx);
5aa00635 1912
07ff77cc 1913 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
5aa00635 1914
5ce5a6b5 1915 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
5aa00635
JP
1916}
1917
1918static void
07ff77cc 1919cmd_set_fail_mode(struct ctl_context *ctx)
5aa00635 1920{
07ff77cc 1921 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1a048029
JP
1922 struct vsctl_bridge *br;
1923 const char *fail_mode = ctx->argv[2];
5aa00635 1924
5ce5a6b5
BP
1925 vsctl_context_populate_cache(ctx);
1926
07ff77cc 1927 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
5aa00635
JP
1928
1929 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
07ff77cc 1930 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
5aa00635
JP
1931 }
1932
31681a5d 1933 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
5aa00635 1934}
dd8ac6fe 1935
24b8b259
AE
1936static void
1937verify_managers(const struct ovsrec_open_vswitch *ovs)
1938{
1939 size_t i;
1940
24b8b259
AE
1941 ovsrec_open_vswitch_verify_manager_options(ovs);
1942
1943 for (i = 0; i < ovs->n_manager_options; ++i) {
1944 const struct ovsrec_manager *mgr = ovs->manager_options[i];
1945
1946 ovsrec_manager_verify_target(mgr);
1947 }
1948}
1949
1950static void
07ff77cc 1951pre_manager(struct ctl_context *ctx)
24b8b259 1952{
24b8b259
AE
1953 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1954 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1955}
1956
1957static void
07ff77cc 1958cmd_get_manager(struct ctl_context *ctx)
24b8b259 1959{
07ff77cc
AW
1960 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1961 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
24b8b259
AE
1962 struct svec targets;
1963 size_t i;
1964
1965 verify_managers(ovs);
1966
1967 /* Print the targets in sorted order for reproducibility. */
1968 svec_init(&targets);
1969
24b8b259
AE
1970 for (i = 0; i < ovs->n_manager_options; i++) {
1971 svec_add(&targets, ovs->manager_options[i]->target);
1972 }
1973
1974 svec_sort_unique(&targets);
1975 for (i = 0; i < targets.n; i++) {
1976 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1977 }
1978 svec_destroy(&targets);
1979}
1980
24b8b259 1981static void
29c3a9f5 1982delete_managers(const struct ovsrec_open_vswitch *ovs)
24b8b259 1983{
28a14bf3
EJ
1984 size_t i;
1985
1986 /* Delete Manager rows pointed to by 'manager_options' column. */
1987 for (i = 0; i < ovs->n_manager_options; i++) {
1988 ovsrec_manager_delete(ovs->manager_options[i]);
1989 }
24b8b259 1990
28a14bf3 1991 /* Delete 'Manager' row refs in 'manager_options' column. */
c5f341ab 1992 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
24b8b259
AE
1993}
1994
28a14bf3 1995static void
07ff77cc 1996cmd_del_manager(struct ctl_context *ctx)
28a14bf3 1997{
07ff77cc
AW
1998 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1999 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
28a14bf3
EJ
2000
2001 verify_managers(ovs);
29c3a9f5 2002 delete_managers(ovs);
28a14bf3
EJ
2003}
2004
24b8b259 2005static void
07ff77cc 2006insert_managers(struct vsctl_context *vsctl_ctx, char *targets[], size_t n)
24b8b259
AE
2007{
2008 struct ovsrec_manager **managers;
2009 size_t i;
2010
24b8b259
AE
2011 /* Insert each manager in a new row in Manager table. */
2012 managers = xmalloc(n * sizeof *managers);
2013 for (i = 0; i < n; i++) {
070723f9
JP
2014 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2015 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2016 }
07ff77cc 2017 managers[i] = ovsrec_manager_insert(vsctl_ctx->base.txn);
24b8b259
AE
2018 ovsrec_manager_set_target(managers[i], targets[i]);
2019 }
2020
2021 /* Store uuids of new Manager rows in 'manager_options' column. */
07ff77cc 2022 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, managers, n);
24b8b259
AE
2023 free(managers);
2024}
2025
2026static void
07ff77cc 2027cmd_set_manager(struct ctl_context *ctx)
24b8b259 2028{
07ff77cc 2029 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
24b8b259
AE
2030 const size_t n = ctx->argc - 1;
2031
07ff77cc
AW
2032 verify_managers(vsctl_ctx->ovs);
2033 delete_managers(vsctl_ctx->ovs);
2034 insert_managers(vsctl_ctx, &ctx->argv[1], n);
24b8b259
AE
2035}
2036
e5e12280 2037static void
07ff77cc 2038pre_cmd_get_ssl(struct ctl_context *ctx)
e5e12280
BP
2039{
2040 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2041
2042 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2043 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2044 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2045 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2046}
2047
dd8ac6fe 2048static void
07ff77cc 2049cmd_get_ssl(struct ctl_context *ctx)
dd8ac6fe 2050{
07ff77cc
AW
2051 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2052 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
dd8ac6fe 2053
07ff77cc 2054 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
dd8ac6fe 2055 if (ssl) {
f74055e7
BP
2056 ovsrec_ssl_verify_private_key(ssl);
2057 ovsrec_ssl_verify_certificate(ssl);
2058 ovsrec_ssl_verify_ca_cert(ssl);
2059 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2060
dd8ac6fe
JP
2061 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2062 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2063 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2064 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2065 ssl->bootstrap_ca_cert ? "true" : "false");
2066 }
2067}
2068
e5e12280 2069static void
07ff77cc 2070pre_cmd_del_ssl(struct ctl_context *ctx)
e5e12280
BP
2071{
2072 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2073}
2074
dd8ac6fe 2075static void
07ff77cc 2076cmd_del_ssl(struct ctl_context *ctx)
dd8ac6fe 2077{
07ff77cc
AW
2078 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2079 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
28a14bf3
EJ
2080
2081 if (ssl) {
07ff77cc 2082 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
28a14bf3 2083 ovsrec_ssl_delete(ssl);
07ff77cc 2084 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
28a14bf3 2085 }
dd8ac6fe
JP
2086}
2087
e5e12280 2088static void
07ff77cc 2089pre_cmd_set_ssl(struct ctl_context *ctx)
e5e12280
BP
2090{
2091 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2092}
2093
dd8ac6fe 2094static void
07ff77cc 2095cmd_set_ssl(struct ctl_context *ctx)
dd8ac6fe 2096{
07ff77cc 2097 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
dd8ac6fe 2098 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
07ff77cc 2099 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
dd8ac6fe 2100
07ff77cc 2101 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
28a14bf3
EJ
2102 if (ssl) {
2103 ovsrec_ssl_delete(ssl);
2104 }
f8ff4bc4 2105 ssl = ovsrec_ssl_insert(ctx->txn);
dd8ac6fe
JP
2106
2107 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2108 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2109 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2110
2111 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2112
07ff77cc 2113 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, ssl);
dd8ac6fe 2114}
99eef98b
DF
2115
2116static void
2117autoattach_insert_mapping(struct ovsrec_autoattach *aa,
2118 int64_t isid,
2119 int64_t vlan)
2120{
2121 int64_t *key_mappings, *value_mappings;
2122 size_t i;
2123
2124 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings + 1));
2125 value_mappings = xmalloc(sizeof *aa->value_mappings * (aa->n_mappings + 1));
2126
2127 for (i = 0; i < aa->n_mappings; i++) {
2128 key_mappings[i] = aa->key_mappings[i];
2129 value_mappings[i] = aa->value_mappings[i];
2130 }
2131 key_mappings[aa->n_mappings] = isid;
2132 value_mappings[aa->n_mappings] = vlan;
2133
2134 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings,
2135 aa->n_mappings + 1);
2136
2137 free(key_mappings);
2138 free(value_mappings);
2139}
2140
2141static void
07ff77cc 2142cmd_add_aa_mapping(struct ctl_context *ctx)
99eef98b 2143{
07ff77cc 2144 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
99eef98b
DF
2145 struct vsctl_bridge *br;
2146 int64_t isid, vlan;
2147 char *nptr = NULL;
2148
2149 isid = strtoull(ctx->argv[2], &nptr, 10);
2150 if (nptr == ctx->argv[2] || nptr == NULL) {
07ff77cc 2151 ctl_fatal("Invalid argument %s", ctx->argv[2]);
99eef98b
DF
2152 return;
2153 }
2154
2155 vlan = strtoull(ctx->argv[3], &nptr, 10);
2156 if (nptr == ctx->argv[3] || nptr == NULL) {
07ff77cc 2157 ctl_fatal("Invalid argument %s", ctx->argv[3]);
99eef98b
DF
2158 return;
2159 }
2160
2161 vsctl_context_populate_cache(ctx);
2162
07ff77cc 2163 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
99eef98b
DF
2164 if (br->parent) {
2165 br = br->parent;
2166 }
2167
2bb0bea8 2168 if (br->br_cfg) {
c557ca04
BP
2169 if (!br->br_cfg->auto_attach) {
2170 struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
2171 ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
2172 }
99eef98b
DF
2173 autoattach_insert_mapping(br->br_cfg->auto_attach, isid, vlan);
2174 }
2175}
2176
2177static void
2178del_aa_mapping(struct ovsrec_autoattach *aa,
2179 int64_t isid,
2180 int64_t vlan)
2181{
2182 int64_t *key_mappings, *value_mappings;
2183 size_t i, n;
2184
2185 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings));
2186 value_mappings = xmalloc(sizeof *value_mappings * (aa->n_mappings));
2187
2188 for (i = n = 0; i < aa->n_mappings; i++) {
2189 if (aa->key_mappings[i] != isid && aa->value_mappings[i] != vlan) {
2190 key_mappings[n] = aa->key_mappings[i];
2191 value_mappings[n++] = aa->value_mappings[i];
2192 }
2193 }
2194
2195 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings, n);
2196
2197 free(key_mappings);
2198 free(value_mappings);
2199}
2200
2201static void
07ff77cc 2202cmd_del_aa_mapping(struct ctl_context *ctx)
99eef98b 2203{
07ff77cc 2204 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
99eef98b
DF
2205 struct vsctl_bridge *br;
2206 int64_t isid, vlan;
2207 char *nptr = NULL;
2208
2209 isid = strtoull(ctx->argv[2], &nptr, 10);
2210 if (nptr == ctx->argv[2] || nptr == NULL) {
07ff77cc 2211 ctl_fatal("Invalid argument %s", ctx->argv[2]);
99eef98b
DF
2212 return;
2213 }
2214
2215 vlan = strtoull(ctx->argv[3], &nptr, 10);
2216 if (nptr == ctx->argv[3] || nptr == NULL) {
07ff77cc 2217 ctl_fatal("Invalid argument %s", ctx->argv[3]);
99eef98b
DF
2218 return;
2219 }
2220
2221 vsctl_context_populate_cache(ctx);
2222
07ff77cc 2223 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
99eef98b
DF
2224 if (br->parent) {
2225 br = br->parent;
2226 }
2227
2bb0bea8 2228 if (br->br_cfg && br->br_cfg->auto_attach &&
99eef98b
DF
2229 br->br_cfg->auto_attach->key_mappings &&
2230 br->br_cfg->auto_attach->value_mappings) {
2231 size_t i;
2232
2233 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2234 if (br->br_cfg->auto_attach->key_mappings[i] == isid &&
2235 br->br_cfg->auto_attach->value_mappings[i] == vlan) {
2236 del_aa_mapping(br->br_cfg->auto_attach, isid, vlan);
2237 break;
2238 }
2239 }
2240 }
2241}
2242
2243static void
07ff77cc 2244pre_aa_mapping(struct ctl_context *ctx)
99eef98b
DF
2245{
2246 pre_get_info(ctx);
2247
d6f115f5 2248 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_auto_attach);
99eef98b
DF
2249 ovsdb_idl_add_column(ctx->idl, &ovsrec_autoattach_col_mappings);
2250}
2251
2252static void
2253verify_auto_attach(struct ovsrec_bridge *bridge)
2254{
2255 if (bridge) {
2256 ovsrec_bridge_verify_auto_attach(bridge);
2257
2258 if (bridge->auto_attach) {
2259 ovsrec_autoattach_verify_mappings(bridge->auto_attach);
2260 }
2261 }
2262}
2263
2264static void
07ff77cc 2265cmd_get_aa_mapping(struct ctl_context *ctx)
99eef98b 2266{
07ff77cc 2267 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
99eef98b
DF
2268 struct vsctl_bridge *br;
2269
2270 vsctl_context_populate_cache(ctx);
2271
07ff77cc 2272 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
99eef98b
DF
2273 if (br->parent) {
2274 br = br->parent;
2275 }
2276
2277 verify_auto_attach(br->br_cfg);
2278
2bb0bea8 2279 if (br->br_cfg && br->br_cfg->auto_attach &&
99eef98b
DF
2280 br->br_cfg->auto_attach->key_mappings &&
2281 br->br_cfg->auto_attach->value_mappings) {
2282 size_t i;
2283
2284 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2285 ds_put_format(&ctx->output, "%"PRId64" %"PRId64"\n",
501b8053
BP
2286 br->br_cfg->auto_attach->key_mappings[i],
2287 br->br_cfg->auto_attach->value_mappings[i]);
99eef98b
DF
2288 }
2289 }
2290}
2291
c75d1511 2292\f
3f5b5f7b 2293static const struct ctl_table_class tables[OVSREC_N_TABLES] = {
a0b02897 2294 [OVSREC_TABLE_BRIDGE].row_ids[0] = {&ovsrec_bridge_col_name, NULL},
ad83bfa6 2295
3f5b5f7b 2296 [OVSREC_TABLE_CONTROLLER].row_ids[0]
a0b02897 2297 = {&ovsrec_bridge_col_name, &ovsrec_bridge_col_controller},
ad83bfa6 2298
a0b02897 2299 [OVSREC_TABLE_INTERFACE].row_ids[0] = {&ovsrec_interface_col_name, NULL},
ad83bfa6 2300
a0b02897 2301 [OVSREC_TABLE_MIRROR].row_ids[0] = {&ovsrec_mirror_col_name, NULL},
ad83bfa6 2302
a0b02897 2303 [OVSREC_TABLE_MANAGER].row_ids[0] = {&ovsrec_manager_col_target, NULL},
94db5407 2304
3f5b5f7b 2305 [OVSREC_TABLE_NETFLOW].row_ids[0]
a0b02897 2306 = {&ovsrec_bridge_col_name, &ovsrec_bridge_col_netflow},
ad83bfa6 2307
a0b02897 2308 [OVSREC_TABLE_PORT].row_ids[0] = {&ovsrec_port_col_name, NULL},
ad83bfa6 2309
3f5b5f7b 2310 [OVSREC_TABLE_QOS].row_ids[0]
a0b02897 2311 = {&ovsrec_port_col_name, &ovsrec_port_col_qos},
ad83bfa6 2312
3f5b5f7b 2313 [OVSREC_TABLE_SFLOW].row_ids[0]
a0b02897 2314 = {&ovsrec_bridge_col_name, &ovsrec_bridge_col_sflow},
c1c9c9c4 2315
3f5b5f7b 2316 [OVSREC_TABLE_FLOW_TABLE].row_ids[0]
a0b02897 2317 = {&ovsrec_flow_table_col_name, NULL},
ad83bfa6 2318
a0b02897
BP
2319 [OVSREC_TABLE_IPFIX].row_ids[0]
2320 = {&ovsrec_bridge_col_name, &ovsrec_bridge_col_ipfix},
3f5b5f7b
BP
2321
2322 [OVSREC_TABLE_AUTOATTACH].row_ids[0]
a0b02897 2323 = {&ovsrec_bridge_col_name, &ovsrec_bridge_col_auto_attach},
99eef98b 2324
3f5b5f7b 2325 [OVSREC_TABLE_FLOW_SAMPLE_COLLECTOR_SET].row_ids[0]
a0b02897 2326 = {&ovsrec_flow_sample_collector_set_col_id, NULL},
ad83bfa6
BP
2327};
2328
1bc6ff29 2329static void
07ff77cc 2330post_db_reload_check_init(void)
ad83bfa6 2331{
07ff77cc 2332 n_neoteric_ifaces = 0;
ad83bfa6
BP
2333}
2334
07ff77cc
AW
2335static void
2336post_db_reload_expect_iface(const struct ovsrec_interface *iface)
ad83bfa6 2337{
07ff77cc
AW
2338 if (n_neoteric_ifaces >= allocated_neoteric_ifaces) {
2339 neoteric_ifaces = x2nrealloc(neoteric_ifaces,
2340 &allocated_neoteric_ifaces,
2341 sizeof *neoteric_ifaces);
ad83bfa6 2342 }
07ff77cc 2343 neoteric_ifaces[n_neoteric_ifaces++] = iface->header_.uuid;
ad83bfa6
BP
2344}
2345
07ff77cc
AW
2346static void
2347post_db_reload_do_checks(const struct vsctl_context *vsctl_ctx)
e5e12280 2348{
feb38b6d 2349 bool print_error = false;
07ff77cc 2350 size_t i;
e5e12280 2351
07ff77cc
AW
2352 for (i = 0; i < n_neoteric_ifaces; i++) {
2353 const struct uuid *uuid;
ad83bfa6 2354
07ff77cc
AW
2355 uuid = ovsdb_idl_txn_get_insert_uuid(vsctl_ctx->base.txn,
2356 &neoteric_ifaces[i]);
2357 if (uuid) {
2358 const struct ovsrec_interface *iface;
ad83bfa6 2359
07ff77cc
AW
2360 iface = ovsrec_interface_get_for_uuid(vsctl_ctx->base.idl, uuid);
2361 if (iface && (!iface->ofport || *iface->ofport == -1)) {
feb38b6d
DDP
2362 if (iface->error && *iface->error) {
2363 ovs_error(0, "Error detected while setting up '%s': %s. "
2364 "See ovs-vswitchd log for details.",
2365 iface->name, iface->error);
2366 } else {
2367 ovs_error(0, "Error detected while setting up '%s'. "
2368 "See ovs-vswitchd log for details.",
2369 iface->name);
2370 }
2371 print_error = true;
ad83bfa6 2372 }
ad83bfa6
BP
2373 }
2374 }
ad83bfa6 2375
feb38b6d
DDP
2376 if (print_error) {
2377 ovs_error(0, "The default log directory is \"%s\".", ovs_logdir());
ad83bfa6 2378 }
ad83bfa6
BP
2379}
2380
07ff77cc
AW
2381\f
2382static void
2383vsctl_context_init_command(struct vsctl_context *vsctl_ctx,
2384 struct ctl_command *command)
ad83bfa6 2385{
07ff77cc
AW
2386 ctl_context_init_command(&vsctl_ctx->base, command);
2387 vsctl_ctx->verified_ports = false;
ad83bfa6
BP
2388}
2389
07ff77cc
AW
2390static void
2391vsctl_context_init(struct vsctl_context *vsctl_ctx,
2392 struct ctl_command *command, struct ovsdb_idl *idl,
2393 struct ovsdb_idl_txn *txn,
2394 const struct ovsrec_open_vswitch *ovs,
2395 struct ovsdb_symbol_table *symtab)
ad83bfa6 2396{
07ff77cc
AW
2397 ctl_context_init(&vsctl_ctx->base, command, idl, txn, symtab,
2398 vsctl_context_invalidate_cache);
2399 if (command) {
2400 vsctl_ctx->verified_ports = false;
ad83bfa6 2401 }
07ff77cc
AW
2402 vsctl_ctx->ovs = ovs;
2403 vsctl_ctx->cache_valid = false;
ad83bfa6
BP
2404}
2405
07ff77cc
AW
2406static void
2407vsctl_context_done_command(struct vsctl_context *vsctl_ctx,
2408 struct ctl_command *command)
aed133bf 2409{
07ff77cc 2410 ctl_context_done_command(&vsctl_ctx->base, command);
aed133bf
BP
2411}
2412
e5e12280 2413static void
07ff77cc
AW
2414vsctl_context_done(struct vsctl_context *vsctl_ctx,
2415 struct ctl_command *command)
e5e12280 2416{
07ff77cc 2417 ctl_context_done(&vsctl_ctx->base, command);
e5e12280
BP
2418}
2419
07ff77cc
AW
2420static void
2421run_prerequisites(struct ctl_command *commands, size_t n_commands,
2422 struct ovsdb_idl *idl)
e89e5374 2423{
07ff77cc 2424 struct ctl_command *c;
e89e5374 2425
07ff77cc
AW
2426 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
2427 if (wait_for_reload) {
2428 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
ad83bfa6 2429 }
07ff77cc
AW
2430 for (c = commands; c < &commands[n_commands]; c++) {
2431 if (c->syntax->prerequisites) {
2432 struct vsctl_context vsctl_ctx;
ad83bfa6 2433
07ff77cc
AW
2434 ds_init(&c->output);
2435 c->table = NULL;
e89e5374 2436
07ff77cc
AW
2437 vsctl_context_init(&vsctl_ctx, c, idl, NULL, NULL, NULL);
2438 (c->syntax->prerequisites)(&vsctl_ctx.base);
2439 vsctl_context_done(&vsctl_ctx, c);
1bc6ff29 2440
07ff77cc
AW
2441 ovs_assert(!c->output.string);
2442 ovs_assert(!c->table);
e89e5374 2443 }
ad83bfa6
BP
2444 }
2445}
2446
48437177
WT
2447static char *
2448vsctl_parent_process_info(void)
2449{
2450#ifdef __linux__
2451 pid_t parent_pid;
48437177 2452 struct ds s;
48437177
WT
2453
2454 parent_pid = getppid();
9f4ecd65
KM
2455 ds_init(&s);
2456
2457 /* Retrive the command line of the parent process, except the init
2458 * process since /proc/0 does not exist. */
2459 if (parent_pid) {
2460 char *procfile;
2461 FILE *f;
2462
2463 procfile = xasprintf("/proc/%d/cmdline", parent_pid);
48437177 2464
9f4ecd65 2465 f = fopen(procfile, "r");
48437177 2466 free(procfile);
ed29f22b
AZ
2467 if (f) {
2468 ds_get_line(&s, f);
2469 fclose(f);
48437177 2470 }
9f4ecd65
KM
2471 } else {
2472 ds_put_cstr(&s, "init");
48437177 2473 }
48437177
WT
2474
2475 ds_put_format(&s, " (pid %d)", parent_pid);
48437177
WT
2476 return ds_steal_cstr(&s);
2477#else
2478 return NULL;
2479#endif
2480}
2481
07ff77cc
AW
2482static void
2483do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
2484 struct ovsdb_idl *idl)
e5e12280 2485{
07ff77cc
AW
2486 struct ovsdb_idl_txn *txn;
2487 const struct ovsrec_open_vswitch *ovs;
2488 enum ovsdb_idl_txn_status status;
2489 struct ovsdb_symbol_table *symtab;
2490 struct vsctl_context vsctl_ctx;
2491 struct ctl_command *c;
2492 struct shash_node *node;
2493 int64_t next_cfg = 0;
2494 char *error = NULL;
48437177 2495 char *ppid_info = NULL;
e5e12280 2496
07ff77cc
AW
2497 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2498 if (dry_run) {
2499 ovsdb_idl_txn_set_dry_run(txn);
e5e12280
BP
2500 }
2501
48437177
WT
2502 ppid_info = vsctl_parent_process_info();
2503 if (ppid_info) {
2504 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl (invoked by %s): %s",
2505 ppid_info, args);
2506 free(ppid_info);
2507 } else {
2508 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2509 }
341c4e59 2510
07ff77cc
AW
2511 ovs = ovsrec_open_vswitch_first(idl);
2512 if (!ovs) {
2513 /* XXX add verification that table is empty */
2514 ovs = ovsrec_open_vswitch_insert(txn);
341c4e59 2515 }
e5e12280 2516
07ff77cc
AW
2517 if (wait_for_reload) {
2518 ovsdb_idl_txn_increment(txn, &ovs->header_,
de32cec7 2519 &ovsrec_open_vswitch_col_next_cfg, false);
b54e22e9
BP
2520 }
2521
c3ccfe98 2522 post_db_reload_check_init();
ce5a3e38 2523 symtab = ovsdb_symbol_table_create();
87b23a01
BP
2524 for (c = commands; c < &commands[n_commands]; c++) {
2525 ds_init(&c->output);
e051b42c 2526 c->table = NULL;
87b23a01 2527 }
07ff77cc 2528 vsctl_context_init(&vsctl_ctx, NULL, idl, txn, ovs, symtab);
f8ff4bc4 2529 for (c = commands; c < &commands[n_commands]; c++) {
07ff77cc 2530 vsctl_context_init_command(&vsctl_ctx, c);
ffd66ea9 2531 if (c->syntax->run) {
07ff77cc 2532 (c->syntax->run)(&vsctl_ctx.base);
ffd66ea9 2533 }
07ff77cc 2534 vsctl_context_done_command(&vsctl_ctx, c);
87b23a01 2535
07ff77cc
AW
2536 if (vsctl_ctx.base.try_again) {
2537 vsctl_context_done(&vsctl_ctx, NULL);
87b23a01
BP
2538 goto try_again;
2539 }
c75d1511 2540 }
07ff77cc 2541 vsctl_context_done(&vsctl_ctx, NULL);
c75d1511 2542
0dc66db9
BP
2543 SHASH_FOR_EACH (node, &symtab->sh) {
2544 struct ovsdb_symbol *symbol = node->data;
2545 if (!symbol->created) {
07ff77cc 2546 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
0dc66db9
BP
2547 "with \"-- --id=%s create ...\")",
2548 node->name, node->name);
2549 }
c5f341ab
BP
2550 if (!symbol->strong_ref) {
2551 if (!symbol->weak_ref) {
2552 VLOG_WARN("row id \"%s\" was created but no reference to it "
2553 "was inserted, so it will not actually appear in "
2554 "the database", node->name);
2555 } else {
2556 VLOG_WARN("row id \"%s\" was created but only a weak "
2557 "reference to it was inserted, so it will not "
2558 "actually appear in the database", node->name);
2559 }
2560 }
28a3b753
BP
2561 }
2562
af96ccd2 2563 status = ovsdb_idl_txn_commit_block(txn);
b54e22e9
BP
2564 if (wait_for_reload && status == TXN_SUCCESS) {
2565 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2566 }
8d49c47a
BP
2567 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2568 for (c = commands; c < &commands[n_commands]; c++) {
2569 if (c->syntax->postprocess) {
07ff77cc
AW
2570 vsctl_context_init(&vsctl_ctx, c, idl, txn, ovs, symtab);
2571 (c->syntax->postprocess)(&vsctl_ctx.base);
2572 vsctl_context_done(&vsctl_ctx, c);
8d49c47a 2573 }
3da1c516
BP
2574 }
2575 }
91e310a5 2576 error = xstrdup(ovsdb_idl_txn_get_error(txn));
c75d1511
BP
2577
2578 switch (status) {
2096903b 2579 case TXN_UNCOMMITTED:
c75d1511 2580 case TXN_INCOMPLETE:
428b2edd 2581 OVS_NOT_REACHED();
c75d1511
BP
2582
2583 case TXN_ABORTED:
2584 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
07ff77cc 2585 ctl_fatal("transaction aborted");
c75d1511 2586
b54e22e9 2587 case TXN_UNCHANGED:
c75d1511
BP
2588 case TXN_SUCCESS:
2589 break;
2590
854a94d9 2591 case TXN_TRY_AGAIN:
87b23a01 2592 goto try_again;
c75d1511
BP
2593
2594 case TXN_ERROR:
07ff77cc 2595 ctl_fatal("transaction error: %s", error);
c75d1511 2596
06b6d651
BP
2597 case TXN_NOT_LOCKED:
2598 /* Should not happen--we never call ovsdb_idl_set_lock(). */
07ff77cc 2599 ctl_fatal("database not locked");
06b6d651 2600
c75d1511 2601 default:
428b2edd 2602 OVS_NOT_REACHED();
c75d1511 2603 }
91e310a5 2604 free(error);
c75d1511 2605
87b23a01
BP
2606 ovsdb_symbol_table_destroy(symtab);
2607
f8ff4bc4
BP
2608 for (c = commands; c < &commands[n_commands]; c++) {
2609 struct ds *ds = &c->output;
ce5a3e38 2610
e051b42c
BP
2611 if (c->table) {
2612 table_print(c->table, &table_style);
2613 } else if (oneline) {
c75d1511
BP
2614 size_t j;
2615
2616 ds_chomp(ds, '\n');
2617 for (j = 0; j < ds->length; j++) {
2a022368
BP
2618 int ch = ds->string[j];
2619 switch (ch) {
c75d1511
BP
2620 case '\n':
2621 fputs("\\n", stdout);
2622 break;
2623
2624 case '\\':
2625 fputs("\\\\", stdout);
2626 break;
2627
2628 default:
2a022368 2629 putchar(ch);
c75d1511
BP
2630 }
2631 }
2632 putchar('\n');
2633 } else {
2634 fputs(ds_cstr(ds), stdout);
2635 }
b86b43aa 2636 ds_destroy(&c->output);
e051b42c
BP
2637 table_destroy(c->table);
2638 free(c->table);
ce5a3e38 2639
79f1cbe9 2640 shash_destroy_free_data(&c->options);
c75d1511 2641 }
b86b43aa 2642 free(commands);
b54e22e9
BP
2643
2644 if (wait_for_reload && status != TXN_UNCHANGED) {
705d7a39
AA
2645 /* Even, if --retry flag was not specified, ovs-vsctl still
2646 * has to retry to establish OVSDB connection, if wait_for_reload
2647 * was set. Otherwise, ovs-vsctl would end up waiting forever
2648 * until cur_cfg would be updated. */
2649 ovsdb_idl_enable_reconnect(idl);
b54e22e9 2650 for (;;) {
b54e22e9
BP
2651 ovsdb_idl_run(idl);
2652 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2653 if (ovs->cur_cfg >= next_cfg) {
07ff77cc 2654 post_db_reload_do_checks(&vsctl_ctx);
b54e22e9
BP
2655 goto done;
2656 }
2657 }
2658 ovsdb_idl_wait(idl);
2659 poll_block();
2660 }
2661 done: ;
2662 }
c3ccfe98 2663 ovsdb_idl_txn_destroy(txn);
b86b43aa 2664 ovsdb_idl_destroy(idl);
b54e22e9 2665
c75d1511 2666 exit(EXIT_SUCCESS);
87b23a01
BP
2667
2668try_again:
2669 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2670 * resources and return so that the caller can try again. */
b7b6e2c4
JP
2671 if (txn) {
2672 ovsdb_idl_txn_abort(txn);
2673 ovsdb_idl_txn_destroy(txn);
85f60635 2674 the_idl_txn = NULL;
b7b6e2c4 2675 }
87b23a01
BP
2676 ovsdb_symbol_table_destroy(symtab);
2677 for (c = commands; c < &commands[n_commands]; c++) {
2678 ds_destroy(&c->output);
e051b42c
BP
2679 table_destroy(c->table);
2680 free(c->table);
87b23a01
BP
2681 }
2682 free(error);
c75d1511
BP
2683}
2684
ce6f1d1f
AZ
2685/* Frees the current transaction and the underlying IDL and then calls
2686 * exit(status).
2687 *
2688 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2689 * for a clean memory leak report from valgrind in the normal case. That makes
2690 * it easier to notice real memory leaks. */
2691static void
2692vsctl_exit(int status)
2693{
2694 if (the_idl_txn) {
2695 ovsdb_idl_txn_abort(the_idl_txn);
2696 ovsdb_idl_txn_destroy(the_idl_txn);
2697 }
2698 ovsdb_idl_destroy(the_idl);
2699 exit(status);
2700}
2701
95e4a97a 2702/*
07ff77cc 2703 * Developers who add new commands to the 'struct ctl_command_syntax' must
95e4a97a
PA
2704 * define the 'arguments' member of the struct. The following keywords are
2705 * available for composing the argument format:
2706 *
2707 * TABLE RECORD BRIDGE PARENT PORT
2708 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
2709 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
2710 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
2711 * TARGET NEW-* (e.g. NEW-PORT)
2712 *
2713 * For argument types not listed above, just uses 'ARG' as place holder.
2714 *
2715 * Encloses the keyword with '[]' if it is optional. Appends '...' to
2716 * keyword or enclosed keyword to indicate that the argument can be specified
2717 * multiple times.
2718 *
2719 * */
07ff77cc 2720static const struct ctl_command_syntax vsctl_commands[] = {
f8ff4bc4 2721 /* Open vSwitch commands. */
95e4a97a 2722 {"init", 0, 0, "", NULL, cmd_init, NULL, "", RW},
f8ff4bc4
BP
2723
2724 /* Bridge commands. */
95e4a97a
PA
2725 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info,
2726 cmd_add_br, NULL, "--may-exist", RW},
2727 {"del-br", 1, 1, "BRIDGE", pre_get_info, cmd_del_br,
2728 NULL, "--if-exists", RW},
2729 {"list-br", 0, 0, "", pre_get_info, cmd_list_br, NULL, "--real,--fake",
2730 RO},
2731 {"br-exists", 1, 1, "BRIDGE", pre_get_info, cmd_br_exists, NULL, "", RO},
2732 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_vlan, NULL, "",
2733 RO},
2734 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_parent, NULL,
2735 "", RO},
2736 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2737 pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
2738 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id,
e5e12280 2739 cmd_br_get_external_id, NULL, "", RO},
f8ff4bc4
BP
2740
2741 /* Port commands. */
95e4a97a
PA
2742 {"list-ports", 1, 1, "BRIDGE", pre_get_info, cmd_list_ports, NULL, "",
2743 RO},
2744 {"add-port", 2, INT_MAX, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2745 pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
2746 {"add-bond", 4, INT_MAX,
2747 "BRIDGE NEW-BOND-PORT SYSIFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info,
2748 cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
2749 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info, cmd_del_port, NULL,
e5e12280 2750 "--if-exists,--with-iface", RW},
95e4a97a 2751 {"port-to-br", 1, 1, "PORT", pre_get_info, cmd_port_to_br, NULL, "", RO},
f8ff4bc4
BP
2752
2753 /* Interface commands. */
95e4a97a
PA
2754 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info, cmd_list_ifaces, NULL, "",
2755 RO},
2756 {"iface-to-br", 1, 1, "IFACE", pre_get_info, cmd_iface_to_br, NULL, "",
2757 RO},
f8ff4bc4
BP
2758
2759 /* Controller commands. */
95e4a97a
PA
2760 {"get-controller", 1, 1, "BRIDGE", pre_controller, cmd_get_controller,
2761 NULL, "", RO},
2762 {"del-controller", 1, 1, "BRIDGE", pre_controller, cmd_del_controller,
2763 NULL, "", RW},
2764 {"set-controller", 1, INT_MAX, "BRIDGE TARGET...", pre_controller,
2765 cmd_set_controller, NULL, "", RW},
2766 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_get_fail_mode, NULL,
2767 "", RO},
2768 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_del_fail_mode, NULL,
4e3e7ff9 2769 "", RW},
95e4a97a
PA
2770 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info, cmd_set_fail_mode,
2771 NULL, "", RW},
f8ff4bc4 2772
24b8b259 2773 /* Manager commands. */
95e4a97a
PA
2774 {"get-manager", 0, 0, "", pre_manager, cmd_get_manager, NULL, "", RO},
2775 {"del-manager", 0, 0, "", pre_manager, cmd_del_manager, NULL, "", RW},
2776 {"set-manager", 1, INT_MAX, "TARGET...", pre_manager, cmd_set_manager,
2777 NULL, "", RW},
24b8b259 2778
f8ff4bc4 2779 /* SSL commands. */
95e4a97a
PA
2780 {"get-ssl", 0, 0, "", pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
2781 {"del-ssl", 0, 0, "", pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
2782 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl,
2783 cmd_set_ssl, NULL, "--bootstrap", RW},
f8ff4bc4 2784
99eef98b 2785 /* Auto Attach commands. */
d6f115f5 2786 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_add_aa_mapping,
95e4a97a
PA
2787 NULL, "", RW},
2788 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_del_aa_mapping,
2789 NULL, "", RW},
2790 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping, cmd_get_aa_mapping,
2791 NULL, "", RO},
99eef98b 2792
18ee958b 2793 /* Switch commands. */
95e4a97a 2794 {"emer-reset", 0, 0, "", pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
18ee958b 2795
95e4a97a 2796 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
f8ff4bc4 2797};
5d9cb63c 2798
07ff77cc
AW
2799/* Registers vsctl and common db commands. */
2800static void
2801vsctl_cmd_init(void)
3815d6c2 2802{
3f5b5f7b 2803 ctl_init(ovsrec_table_classes, tables, cmd_show_tables, vsctl_exit);
07ff77cc 2804 ctl_register_commands(vsctl_commands);
3815d6c2 2805}