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