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