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