]> git.proxmox.com Git - ovs.git/blame - utilities/ovs-vsctl.c
lib: Move compiler.h to <openvswitch/compiler.h>
[ovs.git] / utilities / ovs-vsctl.c
CommitLineData
c75d1511 1/*
c3ccfe98 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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
BP
29
30#include "command-line.h"
31#include "compiler.h"
32#include "dirs.h"
33#include "dynamic-string.h"
8a777cf6 34#include "fatal-signal.h"
a341ee57 35#include "hash.h"
b54e22e9 36#include "json.h"
ad83bfa6 37#include "ovsdb-data.h"
c75d1511
BP
38#include "ovsdb-idl.h"
39#include "poll-loop.h"
f8ff4bc4 40#include "process.h"
ae9a3235 41#include "stream.h"
218a6f59 42#include "stream-ssl.h"
a699f614 43#include "smap.h"
b3c01ed3 44#include "sset.h"
dfbe07ba 45#include "svec.h"
eaa67ba8 46#include "lib/vswitch-idl.h"
e051b42c 47#include "table.h"
c75d1511
BP
48#include "timeval.h"
49#include "util.h"
070723f9 50#include "vconn.h"
c75d1511 51#include "vlog.h"
5136ce49 52
d98e6007 53VLOG_DEFINE_THIS_MODULE(vsctl);
c75d1511 54
def90f62
BP
55/* vsctl_fatal() also logs the error, so it is preferred in this file. */
56#define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
57
f8ff4bc4
BP
58struct vsctl_context;
59
e5e12280 60/* A command supported by ovs-vsctl. */
f8ff4bc4 61struct vsctl_command_syntax {
e5e12280
BP
62 const char *name; /* e.g. "add-br" */
63 int min_args; /* Min number of arguments following name. */
64 int max_args; /* Max number of arguments following name. */
65
66 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
67 * each column or table in ctx->idl that it uses. */
68 void (*prerequisites)(struct vsctl_context *ctx);
69
70 /* Does the actual work of the command and puts the command's output, if
e051b42c 71 * any, in ctx->output or ctx->table.
e5e12280
BP
72 *
73 * Alternatively, if some prerequisite of the command is not met and the
74 * caller should wait for something to change and then retry, it may set
75 * ctx->try_again to true. (Only the "wait-until" command currently does
76 * this.) */
77 void (*run)(struct vsctl_context *ctx);
78
79 /* If nonnull, called after the transaction has been successfully
80 * committed. ctx->output is the output from the "run" function, which
81 * this function may modify and otherwise postprocess as needed. (Only the
82 * "create" command currently does any postprocessing.) */
83 void (*postprocess)(struct vsctl_context *ctx);
84
85 /* A comma-separated list of supported options, e.g. "--a,--b", or the
86 * empty string if the command does not support any options. */
f8ff4bc4 87 const char *options;
0c18b5a0 88 enum { RO, RW } mode; /* Does this command modify the database? */
f8ff4bc4
BP
89};
90
91struct vsctl_command {
92 /* Data that remains constant after initialization. */
93 const struct vsctl_command_syntax *syntax;
94 int argc;
95 char **argv;
96 struct shash options;
97
98 /* Data modified by commands. */
99 struct ds output;
e051b42c 100 struct table *table;
f8ff4bc4
BP
101};
102
c75d1511
BP
103/* --db: The database server to contact. */
104static const char *db;
105
106/* --oneline: Write each command's output as a single line? */
107static bool oneline;
108
577aebdf
BP
109/* --dry-run: Do not commit any changes. */
110static bool dry_run;
111
b54e22e9
BP
112/* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
113static bool wait_for_reload = true;
114
a39a859a 115/* --timeout: Time to wait for a connection to 'db'. */
6b7b9d34 116static int timeout;
a39a859a 117
fba6bd1d
BP
118/* --retry: If true, ovs-vsctl will retry connecting to the database forever.
119 * If false and --db says to use an active connection method (e.g. "unix:",
120 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
121 * error if the database server cannot be contacted (e.g. ovsdb-server is not
122 * running).
123 *
124 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
125 * wait. */
126static bool retry;
127
e051b42c
BP
128/* Format for table output. */
129static struct table_style table_style = TABLE_STYLE_DEFAULT;
130
f8ff4bc4 131/* All supported commands. */
3815d6c2 132static const struct vsctl_command_syntax *get_all_commands(void);
f8ff4bc4 133
1d48b4be
BP
134/* The IDL we're using and the current transaction, if any.
135 * This is for use by vsctl_exit() only, to allow it to clean up.
136 * Other code should use its context arguments. */
137static struct ovsdb_idl *the_idl;
138static struct ovsdb_idl_txn *the_idl_txn;
139
cab50449
TG
140OVS_NO_RETURN static void vsctl_exit(int status);
141OVS_NO_RETURN static void vsctl_fatal(const char *, ...) OVS_PRINTF_FORMAT(1, 2);
c75d1511 142static char *default_db(void);
cab50449 143OVS_NO_RETURN static void usage(void);
401d5a6d 144static void parse_options(int argc, char *argv[], struct shash *local_options);
0c18b5a0 145static bool might_write_to_db(char **argv);
c75d1511 146
f8ff4bc4 147static struct vsctl_command *parse_commands(int argc, char *argv[],
401d5a6d 148 struct shash *local_options,
f8ff4bc4 149 size_t *n_commandsp);
401d5a6d
BP
150static void parse_command(int argc, char *argv[], struct shash *local_options,
151 struct vsctl_command *);
1998cd4d 152static const struct vsctl_command_syntax *find_command(const char *name);
e5e12280
BP
153static void run_prerequisites(struct vsctl_command[], size_t n_commands,
154 struct ovsdb_idl *);
854a94d9
BP
155static void do_vsctl(const char *args, struct vsctl_command *, size_t n,
156 struct ovsdb_idl *);
c75d1511 157
18b239f5
BP
158static const struct vsctl_table_class *get_table(const char *table_name);
159static void set_column(const struct vsctl_table_class *,
ce5a3e38
BP
160 const struct ovsdb_idl_row *, const char *arg,
161 struct ovsdb_symbol_table *);
18b239f5 162
0a140468
BP
163static bool is_condition_satisfied(const struct vsctl_table_class *,
164 const struct ovsdb_idl_row *,
165 const char *arg,
166 struct ovsdb_symbol_table *);
167
c3ccfe98
AZ
168/* Post_db_reload_check frame work is to allow ovs-vsctl to do additional
169 * checks after OVSDB transactions are successfully recorded and reload by
170 * ovs-vswitchd.
171 *
172 * For example, When a new interface is added to OVSDB, ovs-vswitchd will
173 * either store a positive values on successful implementing the new
174 * interface, or -1 on failure.
175 *
176 * Unless -no-wait command line option is specified,
177 * post_db_reload_do_checks() is called right after any configuration
178 * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
179 * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
180 * more detailed messages about those errors.
181 *
182 * Current implementation only check for Post OVSDB reload failures on new
183 * interface additions with 'add-br' and 'add-port' commands.
184 *
185 * post_db_reload_expect_iface()
186 *
187 * keep track of interfaces to be checked post OVSDB reload. */
188static void post_db_reload_check_init(void);
189static void post_db_reload_do_checks(const struct vsctl_context *);
190static void post_db_reload_expect_iface(const struct ovsrec_interface *);
191
192static struct uuid *neoteric_ifaces;
193static size_t n_neoteric_ifaces;
194static size_t allocated_neoteric_ifaces;
195
c75d1511
BP
196int
197main(int argc, char *argv[])
198{
480ce8ab 199 extern struct vlog_module VLM_reconnect;
c75d1511 200 struct ovsdb_idl *idl;
f8ff4bc4 201 struct vsctl_command *commands;
401d5a6d 202 struct shash local_options;
854a94d9 203 unsigned int seqno;
f8ff4bc4
BP
204 size_t n_commands;
205 char *args;
c75d1511
BP
206
207 set_program_name(argv[0]);
8a777cf6 208 fatal_ignore_sigpipe();
480ce8ab
BP
209 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
210 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
bd76d25d 211 ovsrec_init();
f8ff4bc4
BP
212
213 /* Log our arguments. This is often valuable for debugging systems. */
214 args = process_escape_args(argv);
0c18b5a0 215 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
f8ff4bc4
BP
216
217 /* Parse command line. */
401d5a6d
BP
218 shash_init(&local_options);
219 parse_options(argc, argv, &local_options);
220 commands = parse_commands(argc - optind, argv + optind, &local_options,
221 &n_commands);
c75d1511 222
a39a859a
JP
223 if (timeout) {
224 time_alarm(timeout);
225 }
226
e5e12280 227 /* Initialize IDL. */
fba6bd1d 228 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
e5e12280
BP
229 run_prerequisites(commands, n_commands, idl);
230
854a94d9
BP
231 /* Execute the commands.
232 *
233 * 'seqno' is the database sequence number for which we last tried to
234 * execute our transaction. There's no point in trying to commit more than
235 * once for any given sequence number, because if the transaction fails
236 * it's because the database changed and we need to obtain an up-to-date
237 * view of the database before we try the transaction again. */
238 seqno = ovsdb_idl_get_seqno(idl);
c75d1511 239 for (;;) {
854a94d9 240 ovsdb_idl_run(idl);
fba6bd1d
BP
241 if (!ovsdb_idl_is_alive(idl)) {
242 int retval = ovsdb_idl_get_last_error(idl);
243 vsctl_fatal("%s: database connection failed (%s)",
244 db, ovs_retval_to_string(retval));
245 }
854a94d9
BP
246
247 if (seqno != ovsdb_idl_get_seqno(idl)) {
248 seqno = ovsdb_idl_get_seqno(idl);
249 do_vsctl(args, commands, n_commands, idl);
c75d1511
BP
250 }
251
854a94d9 252 if (seqno == ovsdb_idl_get_seqno(idl)) {
4fdfe5cc
BP
253 ovsdb_idl_wait(idl);
254 poll_block();
255 }
c75d1511
BP
256 }
257}
258
401d5a6d
BP
259static struct option *
260find_option(const char *name, struct option *options, size_t n_options)
261{
262 size_t i;
263
264 for (i = 0; i < n_options; i++) {
265 if (!strcmp(options[i].name, name)) {
266 return &options[i];
267 }
268 }
269 return NULL;
270}
271
272static struct option *
273add_option(struct option **optionsp, size_t *n_optionsp,
274 size_t *allocated_optionsp)
275{
276 if (*n_optionsp >= *allocated_optionsp) {
277 *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
278 sizeof **optionsp);
279 }
280 return &(*optionsp)[(*n_optionsp)++];
281}
282
c75d1511 283static void
401d5a6d 284parse_options(int argc, char *argv[], struct shash *local_options)
c75d1511
BP
285{
286 enum {
287 OPT_DB = UCHAR_MAX + 1,
288 OPT_ONELINE,
0c3dd1e1 289 OPT_NO_SYSLOG,
577aebdf 290 OPT_NO_WAIT,
e26b5a06 291 OPT_DRY_RUN,
218a6f59 292 OPT_PEER_CA_CERT,
401d5a6d 293 OPT_LOCAL,
fba6bd1d 294 OPT_RETRY,
e051b42c
BP
295 VLOG_OPTION_ENUMS,
296 TABLE_OPTION_ENUMS
c75d1511 297 };
401d5a6d 298 static const struct option global_long_options[] = {
e3c17733
BP
299 {"db", required_argument, NULL, OPT_DB},
300 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
301 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
302 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
303 {"oneline", no_argument, NULL, OPT_ONELINE},
304 {"timeout", required_argument, NULL, 't'},
fba6bd1d 305 {"retry", no_argument, NULL, OPT_RETRY},
e3c17733
BP
306 {"help", no_argument, NULL, 'h'},
307 {"version", no_argument, NULL, 'V'},
e26b5a06 308 VLOG_LONG_OPTIONS,
e051b42c 309 TABLE_LONG_OPTIONS,
bf8f2167 310 STREAM_SSL_LONG_OPTIONS,
e3c17733
BP
311 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
312 {NULL, 0, NULL, 0},
c75d1511 313 };
401d5a6d 314 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
a2a9d2d9 315 char *tmp, *short_options;
c75d1511 316
401d5a6d
BP
317 const struct vsctl_command_syntax *p;
318 struct option *options, *o;
319 size_t allocated_options;
320 size_t n_options;
321 size_t i;
322
323 tmp = long_options_to_short_options(global_long_options);
a2a9d2d9
BP
324 short_options = xasprintf("+%s", tmp);
325 free(tmp);
342045e1 326
401d5a6d
BP
327 /* We want to parse both global and command-specific options here, but
328 * getopt_long() isn't too convenient for the job. We copy our global
329 * options into a dynamic array, then append all of the command-specific
330 * options. */
331 options = xmemdup(global_long_options, sizeof global_long_options);
332 allocated_options = ARRAY_SIZE(global_long_options);
333 n_options = n_global_long_options;
3815d6c2 334 for (p = get_all_commands(); p->name; p++) {
401d5a6d
BP
335 if (p->options[0]) {
336 char *save_ptr = NULL;
337 char *name;
338 char *s;
339
340 s = xstrdup(p->options);
341 for (name = strtok_r(s, ",", &save_ptr); name != NULL;
342 name = strtok_r(NULL, ",", &save_ptr)) {
343 char *equals;
344 int has_arg;
345
cb22974d 346 ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
401d5a6d
BP
347 name += 2;
348
349 equals = strchr(name, '=');
350 if (equals) {
351 has_arg = required_argument;
352 *equals = '\0';
353 } else {
354 has_arg = no_argument;
355 }
356
357 o = find_option(name, options, n_options);
358 if (o) {
cb22974d
BP
359 ovs_assert(o - options >= n_global_long_options);
360 ovs_assert(o->has_arg == has_arg);
401d5a6d
BP
361 } else {
362 o = add_option(&options, &n_options, &allocated_options);
363 o->name = xstrdup(name);
364 o->has_arg = has_arg;
365 o->flag = NULL;
366 o->val = OPT_LOCAL;
367 }
368 }
369
370 free(s);
371 }
372 }
373 o = add_option(&options, &n_options, &allocated_options);
374 memset(o, 0, sizeof *o);
375
e051b42c
BP
376 table_style.format = TF_LIST;
377
c75d1511 378 for (;;) {
401d5a6d 379 int idx;
c75d1511
BP
380 int c;
381
401d5a6d 382 c = getopt_long(argc, argv, short_options, options, &idx);
c75d1511
BP
383 if (c == -1) {
384 break;
385 }
386
387 switch (c) {
388 case OPT_DB:
389 db = optarg;
390 break;
391
392 case OPT_ONELINE:
393 oneline = true;
394 break;
395
dfbe07ba 396 case OPT_NO_SYSLOG:
480ce8ab 397 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
dfbe07ba
BP
398 break;
399
0c3dd1e1 400 case OPT_NO_WAIT:
b54e22e9 401 wait_for_reload = false;
0c3dd1e1
BP
402 break;
403
577aebdf
BP
404 case OPT_DRY_RUN:
405 dry_run = true;
406 break;
407
401d5a6d
BP
408 case OPT_LOCAL:
409 if (shash_find(local_options, options[idx].name)) {
410 vsctl_fatal("'%s' option specified multiple times",
411 options[idx].name);
412 }
413 shash_add_nocopy(local_options,
414 xasprintf("--%s", options[idx].name),
415 optarg ? xstrdup(optarg) : NULL);
416 break;
417
c75d1511
BP
418 case 'h':
419 usage();
420
421 case 'V':
55d5bb44 422 ovs_print_version(0, 0);
c51513a2 423 printf("DB Schema %s\n", ovsrec_get_db_version());
c75d1511
BP
424 exit(EXIT_SUCCESS);
425
342045e1
BP
426 case 't':
427 timeout = strtoul(optarg, NULL, 10);
a39a859a 428 if (timeout < 0) {
def90f62
BP
429 vsctl_fatal("value %s on -t or --timeout is invalid",
430 optarg);
342045e1
BP
431 }
432 break;
433
fba6bd1d
BP
434 case OPT_RETRY:
435 retry = true;
436 break;
437
e26b5a06 438 VLOG_OPTION_HANDLERS
e051b42c 439 TABLE_OPTION_HANDLERS(&table_style)
c75d1511 440
218a6f59
BP
441 STREAM_SSL_OPTION_HANDLERS
442
443 case OPT_PEER_CA_CERT:
444 stream_ssl_set_peer_ca_cert_file(optarg);
445 break;
218a6f59 446
c75d1511
BP
447 case '?':
448 exit(EXIT_FAILURE);
449
450 default:
451 abort();
452 }
453 }
a2a9d2d9 454 free(short_options);
c75d1511
BP
455
456 if (!db) {
457 db = default_db();
458 }
401d5a6d
BP
459
460 for (i = n_global_long_options; options[i].name; i++) {
461 free(CONST_CAST(char *, options[i].name));
462 }
463 free(options);
c75d1511
BP
464}
465
f8ff4bc4 466static struct vsctl_command *
401d5a6d
BP
467parse_commands(int argc, char *argv[], struct shash *local_options,
468 size_t *n_commandsp)
f8ff4bc4
BP
469{
470 struct vsctl_command *commands;
471 size_t n_commands, allocated_commands;
472 int i, start;
473
474 commands = NULL;
475 n_commands = allocated_commands = 0;
476
477 for (start = i = 0; i <= argc; i++) {
478 if (i == argc || !strcmp(argv[i], "--")) {
479 if (i > start) {
480 if (n_commands >= allocated_commands) {
481 struct vsctl_command *c;
482
483 commands = x2nrealloc(commands, &allocated_commands,
484 sizeof *commands);
485 for (c = commands; c < &commands[n_commands]; c++) {
486 shash_moved(&c->options);
487 }
488 }
401d5a6d 489 parse_command(i - start, &argv[start], local_options,
f8ff4bc4 490 &commands[n_commands++]);
401d5a6d
BP
491 } else if (!shash_is_empty(local_options)) {
492 vsctl_fatal("missing command name (use --help for help)");
f8ff4bc4
BP
493 }
494 start = i + 1;
495 }
496 }
497 if (!n_commands) {
498 vsctl_fatal("missing command name (use --help for help)");
499 }
500 *n_commandsp = n_commands;
501 return commands;
502}
503
504static void
401d5a6d
BP
505parse_command(int argc, char *argv[], struct shash *local_options,
506 struct vsctl_command *command)
f8ff4bc4
BP
507{
508 const struct vsctl_command_syntax *p;
1998cd4d
BP
509 struct shash_node *node;
510 int n_arg;
f8ff4bc4
BP
511 int i;
512
513 shash_init(&command->options);
401d5a6d 514 shash_swap(local_options, &command->options);
f8ff4bc4 515 for (i = 0; i < argc; i++) {
4a033593
BP
516 const char *option = argv[i];
517 const char *equals;
518 char *key, *value;
519
520 if (option[0] != '-') {
f8ff4bc4
BP
521 break;
522 }
4a033593
BP
523
524 equals = strchr(option, '=');
525 if (equals) {
526 key = xmemdup0(option, equals - option);
527 value = xstrdup(equals + 1);
528 } else {
529 key = xstrdup(option);
530 value = NULL;
531 }
532
533 if (shash_find(&command->options, key)) {
f8ff4bc4
BP
534 vsctl_fatal("'%s' option specified multiple times", argv[i]);
535 }
4a033593 536 shash_add_nocopy(&command->options, key, value);
f8ff4bc4
BP
537 }
538 if (i == argc) {
401d5a6d 539 vsctl_fatal("missing command name (use --help for help)");
f8ff4bc4
BP
540 }
541
1998cd4d
BP
542 p = find_command(argv[i]);
543 if (!p) {
544 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
545 }
f8ff4bc4 546
1998cd4d
BP
547 SHASH_FOR_EACH (node, &command->options) {
548 const char *s = strstr(p->options, node->name);
549 int end = s ? s[strlen(node->name)] : EOF;
4a033593 550
1998cd4d
BP
551 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
552 vsctl_fatal("'%s' command has no '%s' option",
553 argv[i], node->name);
554 }
555 if ((end == '=') != (node->data != NULL)) {
556 if (end == '=') {
557 vsctl_fatal("missing argument to '%s' option on '%s' "
558 "command", node->name, argv[i]);
559 } else {
560 vsctl_fatal("'%s' option on '%s' does not accept an "
561 "argument", node->name, argv[i]);
f8ff4bc4 562 }
1998cd4d
BP
563 }
564 }
f8ff4bc4 565
1998cd4d
BP
566 n_arg = argc - i - 1;
567 if (n_arg < p->min_args) {
568 vsctl_fatal("'%s' command requires at least %d arguments",
569 p->name, p->min_args);
570 } else if (n_arg > p->max_args) {
571 int j;
572
573 for (j = i + 1; j < argc; j++) {
574 if (argv[j][0] == '-') {
575 vsctl_fatal("'%s' command takes at most %d arguments "
576 "(note that options must precede command "
577 "names and follow a \"--\" argument)",
f8ff4bc4 578 p->name, p->max_args);
f8ff4bc4
BP
579 }
580 }
1998cd4d
BP
581
582 vsctl_fatal("'%s' command takes at most %d arguments",
583 p->name, p->max_args);
584 }
585
586 command->syntax = p;
587 command->argc = n_arg + 1;
588 command->argv = &argv[i];
589}
590
591/* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
592 * null pointer if there is none. */
593static const struct vsctl_command_syntax *
594find_command(const char *name)
595{
596 static struct shash commands = SHASH_INITIALIZER(&commands);
597
598 if (shash_is_empty(&commands)) {
599 const struct vsctl_command_syntax *p;
600
3815d6c2 601 for (p = get_all_commands(); p->name; p++) {
1998cd4d
BP
602 shash_add_assert(&commands, p->name, p);
603 }
f8ff4bc4
BP
604 }
605
1998cd4d 606 return shash_find_data(&commands, name);
f8ff4bc4
BP
607}
608
609static void
610vsctl_fatal(const char *format, ...)
611{
612 char *message;
613 va_list args;
614
615 va_start(args, format);
616 message = xvasprintf(format, args);
617 va_end(args);
618
c1a543a8 619 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_OFF);
f8ff4bc4 620 VLOG_ERR("%s", message);
def90f62 621 ovs_error(0, "%s", message);
1d48b4be
BP
622 vsctl_exit(EXIT_FAILURE);
623}
624
625/* Frees the current transaction and the underlying IDL and then calls
626 * exit(status).
627 *
628 * Freeing the transaction and the IDL is not strictly necessary, but it makes
629 * for a clean memory leak report from valgrind in the normal case. That makes
630 * it easier to notice real memory leaks. */
631static void
632vsctl_exit(int status)
633{
634 if (the_idl_txn) {
635 ovsdb_idl_txn_abort(the_idl_txn);
636 ovsdb_idl_txn_destroy(the_idl_txn);
637 }
638 ovsdb_idl_destroy(the_idl);
639 exit(status);
f8ff4bc4
BP
640}
641
c75d1511
BP
642static void
643usage(void)
644{
8f7501e8
BP
645 printf("\
646%s: ovs-vswitchd management utility\n\
647usage: %s [OPTIONS] COMMAND [ARG...]\n\
648\n\
ae9a3235
BP
649Open vSwitch commands:\n\
650 init initialize database, if not yet initialized\n\
9b1735a7 651 show print overview of database contents\n\
ae9a3235
BP
652 emer-reset reset configuration to clean state\n\
653\n\
8f7501e8
BP
654Bridge commands:\n\
655 add-br BRIDGE create a new bridge named BRIDGE\n\
656 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
657 del-br BRIDGE delete BRIDGE and all of its ports\n\
658 list-br print the names of all the bridges\n\
b5fcae50 659 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
8f7501e8
BP
660 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
661 br-to-parent BRIDGE print the parent of BRIDGE\n\
662 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
663 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
664 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
665 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
666\n\
ae9a3235 667Port commands (a bond is considered to be a single port):\n\
8f7501e8
BP
668 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
669 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
670 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
671 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
672 port-to-br PORT print name of bridge that contains PORT\n\
8f7501e8
BP
673\n\
674Interface commands (a bond consists of multiple interfaces):\n\
675 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
676 iface-to-br IFACE print name of bridge that contains IFACE\n\
8f7501e8
BP
677\n\
678Controller commands:\n\
a892775d
BP
679 get-controller BRIDGE print the controllers for BRIDGE\n\
680 del-controller BRIDGE delete the controllers for BRIDGE\n\
681 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
1a048029
JP
682 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
683 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
684 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
8f7501e8 685\n\
24b8b259 686Manager commands:\n\
a892775d
BP
687 get-manager print the managers\n\
688 del-manager delete the managers\n\
689 set-manager TARGET... set the list of managers to TARGET...\n\
24b8b259 690\n\
8f7501e8
BP
691SSL commands:\n\
692 get-ssl print the SSL configuration\n\
693 del-ssl delete the SSL configuration\n\
694 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
695\n\
18ee958b
JP
696Switch commands:\n\
697 emer-reset reset switch to known good state\n\
698\n\
8f7501e8
BP
699Database commands:\n\
700 list TBL [REC] list RECord (or all records) in TBL\n\
0a140468 701 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
4f1361e8 702 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
8f7501e8
BP
703 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
704 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
705 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
706 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
707 create TBL COL[:KEY]=VALUE create and initialize new record\n\
4f1361e8 708 destroy TBL REC delete RECord from TBL\n\
7db03f7c 709 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
8f7501e8
BP
710Potentially unsafe database commands require --force option.\n\
711\n\
712Options:\n\
713 --db=DATABASE connect to DATABASE\n\
714 (default: %s)\n\
ae9a3235 715 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
fba6bd1d 716 --retry keep trying to connect to server forever\n\
ae9a3235
BP
717 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
718 --dry-run do not commit changes to database\n\
8f7501e8
BP
719 --oneline print exactly one line of output per command\n",
720 program_name, program_name, default_db());
c75d1511 721 vlog_usage();
ae9a3235
BP
722 printf("\
723 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
724 stream_usage("database", true, true, false);
8f7501e8
BP
725 printf("\n\
726Other options:\n\
727 -h, --help display this help message\n\
728 -V, --version display version information\n");
c75d1511
BP
729 exit(EXIT_SUCCESS);
730}
731
732static char *
733default_db(void)
734{
735 static char *def;
736 if (!def) {
b43c6fe2 737 def = xasprintf("unix:%s/db.sock", ovs_rundir());
c75d1511
BP
738 }
739 return def;
740}
0c18b5a0
BP
741
742/* Returns true if it looks like this set of arguments might modify the
743 * database, otherwise false. (Not very smart, so it's prone to false
744 * positives.) */
745static bool
746might_write_to_db(char **argv)
747{
748 for (; *argv; argv++) {
749 const struct vsctl_command_syntax *p = find_command(*argv);
750 if (p && p->mode == RW) {
751 return true;
752 }
753 }
754 return false;
755}
c75d1511 756\f
5d9cb63c 757struct vsctl_context {
f8ff4bc4 758 /* Read-only. */
5d9cb63c
BP
759 int argc;
760 char **argv;
f8ff4bc4
BP
761 struct shash options;
762
763 /* Modifiable state. */
764 struct ds output;
e051b42c 765 struct table *table;
ad83bfa6 766 struct ovsdb_idl *idl;
f8ff4bc4 767 struct ovsdb_idl_txn *txn;
ce5a3e38 768 struct ovsdb_symbol_table *symtab;
5d9cb63c 769 const struct ovsrec_open_vswitch *ovs;
f74055e7 770 bool verified_ports;
87b23a01 771
5ce5a6b5
BP
772 /* A cache of the contents of the database.
773 *
774 * A command that needs to use any of this information must first call
775 * vsctl_context_populate_cache(). A command that changes anything that
776 * could invalidate the cache must either call
777 * vsctl_context_invalidate_cache() or manually update the cache to
778 * maintain its correctness. */
779 bool cache_valid;
780 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
781 struct shash ports; /* Maps from port name to struct vsctl_port. */
782 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
783
87b23a01
BP
784 /* A command may set this member to true if some prerequisite is not met
785 * and the caller should wait for something to change and then retry. */
786 bool try_again;
5d9cb63c
BP
787};
788
c75d1511
BP
789struct vsctl_bridge {
790 struct ovsrec_bridge *br_cfg;
791 char *name;
a341ee57 792 struct list ports; /* Contains "struct vsctl_port"s. */
5341d046
BP
793
794 /* VLAN ("fake") bridge support.
795 *
796 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
797 * in either case. */
a341ee57
BP
798 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
799 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
5341d046
BP
800 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
801 int vlan; /* VLAN VID (0...4095), or 0. */
c75d1511
BP
802};
803
804struct vsctl_port {
a341ee57
BP
805 struct list ports_node; /* In struct vsctl_bridge's 'ports' list. */
806 struct list ifaces; /* Contains "struct vsctl_iface"s. */
c75d1511
BP
807 struct ovsrec_port *port_cfg;
808 struct vsctl_bridge *bridge;
809};
810
811struct vsctl_iface {
a341ee57 812 struct list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
c75d1511
BP
813 struct ovsrec_interface *iface_cfg;
814 struct vsctl_port *port;
815};
816
5dd9826c
BP
817static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
818 int vlan);
819
bb1c67c8
BP
820static char *
821vsctl_context_to_string(const struct vsctl_context *ctx)
822{
823 const struct shash_node *node;
824 struct svec words;
825 char *s;
826 int i;
827
828 svec_init(&words);
829 SHASH_FOR_EACH (node, &ctx->options) {
830 svec_add(&words, node->name);
831 }
832 for (i = 0; i < ctx->argc; i++) {
833 svec_add(&words, ctx->argv[i]);
834 }
835 svec_terminate(&words);
836
837 s = process_escape_args(words.names);
838
839 svec_destroy(&words);
840
841 return s;
842}
843
f74055e7
BP
844static void
845verify_ports(struct vsctl_context *ctx)
846{
847 if (!ctx->verified_ports) {
848 const struct ovsrec_bridge *bridge;
849 const struct ovsrec_port *port;
850
851 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
852 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
853 ovsrec_bridge_verify_ports(bridge);
854 }
855 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
856 ovsrec_port_verify_interfaces(port);
857 }
858
859 ctx->verified_ports = true;
860 }
861}
862
c75d1511 863static struct vsctl_bridge *
a341ee57
BP
864add_bridge_to_cache(struct vsctl_context *ctx,
865 struct ovsrec_bridge *br_cfg, const char *name,
866 struct vsctl_bridge *parent, int vlan)
c75d1511
BP
867{
868 struct vsctl_bridge *br = xmalloc(sizeof *br);
869 br->br_cfg = br_cfg;
870 br->name = xstrdup(name);
a341ee57 871 list_init(&br->ports);
c75d1511
BP
872 br->parent = parent;
873 br->vlan = vlan;
a341ee57
BP
874 hmap_init(&br->children);
875 if (parent) {
5dd9826c
BP
876 struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
877 if (conflict) {
878 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
879 "for VLAN %d, but only one is allowed",
880 parent->name, name, conflict->name, vlan);
881 } else {
882 hmap_insert(&parent->children, &br->children_node,
883 hash_int(vlan, 0));
884 }
a341ee57 885 }
5ce5a6b5 886 shash_add(&ctx->bridges, br->name, br);
c75d1511
BP
887 return br;
888}
889
a341ee57
BP
890static void
891ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
892 struct ovsrec_bridge *bridge)
893{
894 struct ovsrec_bridge **bridges;
895 size_t i, n;
896
897 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
898 for (i = n = 0; i < ovs->n_bridges; i++) {
899 if (ovs->bridges[i] != bridge) {
900 bridges[n++] = ovs->bridges[i];
901 }
902 }
903 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
904 free(bridges);
905}
906
907static void
908del_cached_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
909{
cb22974d
BP
910 ovs_assert(list_is_empty(&br->ports));
911 ovs_assert(hmap_is_empty(&br->children));
a341ee57
BP
912 if (br->parent) {
913 hmap_remove(&br->parent->children, &br->children_node);
914 }
915 if (br->br_cfg) {
916 ovsrec_bridge_delete(br->br_cfg);
917 ovs_delete_bridge(ctx->ovs, br->br_cfg);
918 }
919 shash_find_and_delete(&ctx->bridges, br->name);
920 hmap_destroy(&br->children);
921 free(br->name);
922 free(br);
923}
924
c75d1511
BP
925static bool
926port_is_fake_bridge(const struct ovsrec_port *port_cfg)
927{
928 return (port_cfg->fake_bridge
929 && port_cfg->tag
5341d046 930 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
c75d1511
BP
931}
932
933static struct vsctl_bridge *
a341ee57 934find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
c75d1511 935{
a341ee57 936 struct vsctl_bridge *child;
c75d1511 937
a341ee57
BP
938 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
939 &parent->children) {
940 if (child->vlan == vlan) {
941 return child;
c75d1511
BP
942 }
943 }
944
945 return NULL;
946}
947
a341ee57
BP
948static struct vsctl_port *
949add_port_to_cache(struct vsctl_context *ctx, struct vsctl_bridge *parent,
950 struct ovsrec_port *port_cfg)
951{
952 struct vsctl_port *port;
953
954 if (port_cfg->tag
955 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
956 struct vsctl_bridge *vlan_bridge;
957
958 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
959 if (vlan_bridge) {
960 parent = vlan_bridge;
961 }
962 }
963
964 port = xmalloc(sizeof *port);
965 list_push_back(&parent->ports, &port->ports_node);
966 list_init(&port->ifaces);
967 port->port_cfg = port_cfg;
968 port->bridge = parent;
969 shash_add(&ctx->ports, port_cfg->name, port);
970
971 return port;
972}
973
974static void
975del_cached_port(struct vsctl_context *ctx, struct vsctl_port *port)
976{
cb22974d 977 ovs_assert(list_is_empty(&port->ifaces));
a341ee57
BP
978 list_remove(&port->ports_node);
979 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
980 ovsrec_port_delete(port->port_cfg);
981 free(port);
982}
983
984static struct vsctl_iface *
985add_iface_to_cache(struct vsctl_context *ctx, struct vsctl_port *parent,
986 struct ovsrec_interface *iface_cfg)
987{
988 struct vsctl_iface *iface;
989
990 iface = xmalloc(sizeof *iface);
991 list_push_back(&parent->ifaces, &iface->ifaces_node);
992 iface->iface_cfg = iface_cfg;
993 iface->port = parent;
994 shash_add(&ctx->ifaces, iface_cfg->name, iface);
995
996 return iface;
997}
998
999static void
1000del_cached_iface(struct vsctl_context *ctx, struct vsctl_iface *iface)
1001{
1002 list_remove(&iface->ifaces_node);
1003 shash_find_and_delete(&ctx->ifaces, iface->iface_cfg->name);
1004 ovsrec_interface_delete(iface->iface_cfg);
1005 free(iface);
1006}
1007
c75d1511 1008static void
5ce5a6b5 1009vsctl_context_invalidate_cache(struct vsctl_context *ctx)
c75d1511
BP
1010{
1011 struct shash_node *node;
1012
5ce5a6b5
BP
1013 if (!ctx->cache_valid) {
1014 return;
1015 }
1016 ctx->cache_valid = false;
1017
1018 SHASH_FOR_EACH (node, &ctx->bridges) {
c75d1511 1019 struct vsctl_bridge *bridge = node->data;
a341ee57 1020 hmap_destroy(&bridge->children);
c75d1511
BP
1021 free(bridge->name);
1022 free(bridge);
1023 }
5ce5a6b5 1024 shash_destroy(&ctx->bridges);
c75d1511 1025
5ce5a6b5
BP
1026 shash_destroy_free_data(&ctx->ports);
1027 shash_destroy_free_data(&ctx->ifaces);
c75d1511
BP
1028}
1029
e5e12280
BP
1030static void
1031pre_get_info(struct vsctl_context *ctx)
1032{
1033 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
1034
1035 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
1036 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1037 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1038 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
1039
1040 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
1041 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
1042 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
1043 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
1044
1045 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
c3ccfe98 1046 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
e5e12280
BP
1047}
1048
c75d1511 1049static void
5ce5a6b5 1050vsctl_context_populate_cache(struct vsctl_context *ctx)
c75d1511 1051{
1588bb8d 1052 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
b3c01ed3 1053 struct sset bridges, ports;
c75d1511
BP
1054 size_t i;
1055
5ce5a6b5
BP
1056 if (ctx->cache_valid) {
1057 /* Cache is already populated. */
1058 return;
1059 }
1060 ctx->cache_valid = true;
1061 shash_init(&ctx->bridges);
1062 shash_init(&ctx->ports);
1063 shash_init(&ctx->ifaces);
c75d1511 1064
b3c01ed3
BP
1065 sset_init(&bridges);
1066 sset_init(&ports);
c75d1511
BP
1067 for (i = 0; i < ovs->n_bridges; i++) {
1068 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
1069 struct vsctl_bridge *br;
1070 size_t j;
1071
b3c01ed3 1072 if (!sset_add(&bridges, br_cfg->name)) {
c75d1511
BP
1073 VLOG_WARN("%s: database contains duplicate bridge name",
1074 br_cfg->name);
1075 continue;
1076 }
a341ee57 1077 br = add_bridge_to_cache(ctx, br_cfg, br_cfg->name, NULL, 0);
c75d1511
BP
1078 if (!br) {
1079 continue;
1080 }
1081
1082 for (j = 0; j < br_cfg->n_ports; j++) {
1083 struct ovsrec_port *port_cfg = br_cfg->ports[j];
1084
b3c01ed3 1085 if (!sset_add(&ports, port_cfg->name)) {
48a69501 1086 /* Duplicate port name. (We will warn about that later.) */
c75d1511
BP
1087 continue;
1088 }
1089
1090 if (port_is_fake_bridge(port_cfg)
b3c01ed3 1091 && sset_add(&bridges, port_cfg->name)) {
a341ee57
BP
1092 add_bridge_to_cache(ctx, NULL, port_cfg->name, br,
1093 *port_cfg->tag);
c75d1511
BP
1094 }
1095 }
1096 }
b3c01ed3
BP
1097 sset_destroy(&bridges);
1098 sset_destroy(&ports);
c75d1511 1099
b3c01ed3 1100 sset_init(&bridges);
c75d1511
BP
1101 for (i = 0; i < ovs->n_bridges; i++) {
1102 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
1103 struct vsctl_bridge *br;
1104 size_t j;
1105
b3c01ed3 1106 if (!sset_add(&bridges, br_cfg->name)) {
c75d1511
BP
1107 continue;
1108 }
5ce5a6b5 1109 br = shash_find_data(&ctx->bridges, br_cfg->name);
c75d1511
BP
1110 for (j = 0; j < br_cfg->n_ports; j++) {
1111 struct ovsrec_port *port_cfg = br_cfg->ports[j];
1112 struct vsctl_port *port;
1113 size_t k;
1114
5ce5a6b5 1115 port = shash_find_data(&ctx->ports, port_cfg->name);
48a69501
BP
1116 if (port) {
1117 if (port_cfg == port->port_cfg) {
1118 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
1119 port_cfg->name, br->name, port->bridge->name);
1120 } else {
1121 /* Log as an error because this violates the database's
1122 * uniqueness constraints, so the database server shouldn't
1123 * have allowed it. */
1124 VLOG_ERR("%s: database contains duplicate port name",
1125 port_cfg->name);
1126 }
c75d1511
BP
1127 continue;
1128 }
1129
1130 if (port_is_fake_bridge(port_cfg)
b3c01ed3 1131 && !sset_add(&bridges, port_cfg->name)) {
c75d1511
BP
1132 continue;
1133 }
1134
a341ee57 1135 port = add_port_to_cache(ctx, br, port_cfg);
c75d1511
BP
1136 for (k = 0; k < port_cfg->n_interfaces; k++) {
1137 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
1138 struct vsctl_iface *iface;
1139
5ce5a6b5 1140 iface = shash_find_data(&ctx->ifaces, iface_cfg->name);
48a69501
BP
1141 if (iface) {
1142 if (iface_cfg == iface->iface_cfg) {
1143 VLOG_WARN("%s: interface is in multiple ports "
1144 "(%s and %s)",
1145 iface_cfg->name,
1146 iface->port->port_cfg->name,
1147 port->port_cfg->name);
1148 } else {
1149 /* Log as an error because this violates the database's
1150 * uniqueness constraints, so the database server
1151 * shouldn't have allowed it. */
1152 VLOG_ERR("%s: database contains duplicate interface "
1153 "name", iface_cfg->name);
1154 }
c75d1511
BP
1155 continue;
1156 }
1157
a341ee57 1158 add_iface_to_cache(ctx, port, iface_cfg);
c75d1511
BP
1159 }
1160 }
1161 }
b3c01ed3 1162 sset_destroy(&bridges);
c75d1511
BP
1163}
1164
1165static void
5ce5a6b5 1166check_conflicts(struct vsctl_context *ctx, const char *name,
c75d1511
BP
1167 char *msg)
1168{
1169 struct vsctl_iface *iface;
1170 struct vsctl_port *port;
1171
5ce5a6b5 1172 verify_ports(ctx);
f74055e7 1173
5ce5a6b5 1174 if (shash_find(&ctx->bridges, name)) {
c88b6a27
BP
1175 vsctl_fatal("%s because a bridge named %s already exists",
1176 msg, name);
c75d1511
BP
1177 }
1178
5ce5a6b5 1179 port = shash_find_data(&ctx->ports, name);
c75d1511 1180 if (port) {
c88b6a27
BP
1181 vsctl_fatal("%s because a port named %s already exists on "
1182 "bridge %s", msg, name, port->bridge->name);
c75d1511
BP
1183 }
1184
5ce5a6b5 1185 iface = shash_find_data(&ctx->ifaces, name);
c75d1511 1186 if (iface) {
c88b6a27
BP
1187 vsctl_fatal("%s because an interface named %s already exists "
1188 "on bridge %s", msg, name, iface->port->bridge->name);
c75d1511
BP
1189 }
1190
1191 free(msg);
1192}
1193
1194static struct vsctl_bridge *
5ce5a6b5 1195find_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
c75d1511 1196{
5ce5a6b5
BP
1197 struct vsctl_bridge *br;
1198
cb22974d 1199 ovs_assert(ctx->cache_valid);
5ce5a6b5
BP
1200
1201 br = shash_find_data(&ctx->bridges, name);
01845ce8 1202 if (must_exist && !br) {
c88b6a27 1203 vsctl_fatal("no bridge named %s", name);
c75d1511 1204 }
5ce5a6b5 1205 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
c75d1511
BP
1206 return br;
1207}
1208
975ac531 1209static struct vsctl_bridge *
5ce5a6b5 1210find_real_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
975ac531 1211{
5ce5a6b5 1212 struct vsctl_bridge *br = find_bridge(ctx, name, must_exist);
975ac531
JP
1213 if (br && br->parent) {
1214 vsctl_fatal("%s is a fake bridge", name);
1215 }
1216 return br;
1217}
1218
c75d1511 1219static struct vsctl_port *
5ce5a6b5 1220find_port(struct vsctl_context *ctx, const char *name, bool must_exist)
c75d1511 1221{
5ce5a6b5
BP
1222 struct vsctl_port *port;
1223
cb22974d 1224 ovs_assert(ctx->cache_valid);
5ce5a6b5
BP
1225
1226 port = shash_find_data(&ctx->ports, name);
460aad80 1227 if (port && !strcmp(name, port->bridge->name)) {
01845ce8
BP
1228 port = NULL;
1229 }
1230 if (must_exist && !port) {
c88b6a27 1231 vsctl_fatal("no port named %s", name);
c75d1511 1232 }
5ce5a6b5 1233 verify_ports(ctx);
c75d1511
BP
1234 return port;
1235}
1236
1237static struct vsctl_iface *
5ce5a6b5 1238find_iface(struct vsctl_context *ctx, const char *name, bool must_exist)
c75d1511 1239{
5ce5a6b5
BP
1240 struct vsctl_iface *iface;
1241
cb22974d 1242 ovs_assert(ctx->cache_valid);
5ce5a6b5
BP
1243
1244 iface = shash_find_data(&ctx->ifaces, name);
460aad80 1245 if (iface && !strcmp(name, iface->port->bridge->name)) {
01845ce8
BP
1246 iface = NULL;
1247 }
1248 if (must_exist && !iface) {
c88b6a27 1249 vsctl_fatal("no interface named %s", name);
c75d1511 1250 }
5ce5a6b5 1251 verify_ports(ctx);
c75d1511
BP
1252 return iface;
1253}
1254
1255static void
1256bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1257{
1258 struct ovsrec_port **ports;
1259 size_t i;
1260
1261 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
1262 for (i = 0; i < br->n_ports; i++) {
1263 ports[i] = br->ports[i];
1264 }
c75d1511
BP
1265 ports[br->n_ports] = port;
1266 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
1267 free(ports);
1268}
1269
1270static void
1271bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1272{
1273 struct ovsrec_port **ports;
1274 size_t i, n;
1275
1276 ports = xmalloc(sizeof *br->ports * br->n_ports);
1277 for (i = n = 0; i < br->n_ports; i++) {
1278 if (br->ports[i] != port) {
1279 ports[n++] = br->ports[i];
1280 }
1281 }
1282 ovsrec_bridge_set_ports(br, ports, n);
1283 free(ports);
1284}
1285
1286static void
1287ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
1288 struct ovsrec_bridge *bridge)
1289{
1290 struct ovsrec_bridge **bridges;
1291 size_t i;
1292
1293 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
1294 for (i = 0; i < ovs->n_bridges; i++) {
1295 bridges[i] = ovs->bridges[i];
1296 }
1297 bridges[ovs->n_bridges] = bridge;
1298 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1299 free(bridges);
1300}
1301
524555d1 1302static void
c69ee87c 1303cmd_init(struct vsctl_context *ctx OVS_UNUSED)
524555d1
BP
1304{
1305}
1306
9b1735a7
BP
1307struct cmd_show_table {
1308 const struct ovsdb_idl_table_class *table;
1309 const struct ovsdb_idl_column *name_column;
1310 const struct ovsdb_idl_column *columns[3];
1311 bool recurse;
1312};
1313
1314static struct cmd_show_table cmd_show_tables[] = {
1315 {&ovsrec_table_open_vswitch,
1316 NULL,
1317 {&ovsrec_open_vswitch_col_manager_options,
1318 &ovsrec_open_vswitch_col_bridges,
1319 &ovsrec_open_vswitch_col_ovs_version},
1320 false},
1321
1322 {&ovsrec_table_bridge,
1323 &ovsrec_bridge_col_name,
1324 {&ovsrec_bridge_col_controller,
1325 &ovsrec_bridge_col_fail_mode,
1326 &ovsrec_bridge_col_ports},
1327 false},
1328
1329 {&ovsrec_table_port,
1330 &ovsrec_port_col_name,
1331 {&ovsrec_port_col_tag,
1332 &ovsrec_port_col_trunks,
1333 &ovsrec_port_col_interfaces},
1334 false},
1335
1336 {&ovsrec_table_interface,
1337 &ovsrec_interface_col_name,
1338 {&ovsrec_interface_col_type,
1339 &ovsrec_interface_col_options,
9500f505 1340 &ovsrec_interface_col_error},
9b1735a7
BP
1341 false},
1342
1343 {&ovsrec_table_controller,
1344 &ovsrec_controller_col_target,
1345 {&ovsrec_controller_col_is_connected,
1346 NULL,
1347 NULL},
1348 false},
1349
1350 {&ovsrec_table_manager,
1351 &ovsrec_manager_col_target,
1352 {&ovsrec_manager_col_is_connected,
1353 NULL,
1354 NULL},
1355 false},
1356};
1357
1358static void
1359pre_cmd_show(struct vsctl_context *ctx)
1360{
1361 struct cmd_show_table *show;
1362
1363 for (show = cmd_show_tables;
1364 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1365 show++) {
1366 size_t i;
1367
1368 ovsdb_idl_add_table(ctx->idl, show->table);
1369 if (show->name_column) {
1370 ovsdb_idl_add_column(ctx->idl, show->name_column);
1371 }
1372 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1373 const struct ovsdb_idl_column *column = show->columns[i];
1374 if (column) {
1375 ovsdb_idl_add_column(ctx->idl, column);
1376 }
1377 }
1378 }
1379}
1380
1381static struct cmd_show_table *
1382cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1383{
1384 struct cmd_show_table *show;
1385
1386 for (show = cmd_show_tables;
1387 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1388 show++) {
1389 if (show->table == row->table->class) {
1390 return show;
1391 }
1392 }
1393 return NULL;
1394}
1395
1396static struct cmd_show_table *
1397cmd_show_find_table_by_name(const char *name)
1398{
1399 struct cmd_show_table *show;
1400
1401 for (show = cmd_show_tables;
1402 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1403 show++) {
1404 if (!strcmp(show->table->name, name)) {
1405 return show;
1406 }
1407 }
1408 return NULL;
1409}
1410
1411static void
1412cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1413 int level)
1414{
1415 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1416 size_t i;
1417
1418 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1419 if (show && show->name_column) {
1420 const struct ovsdb_datum *datum;
1421
1422 ds_put_format(&ctx->output, "%s ", show->table->name);
1423 datum = ovsdb_idl_read(row, show->name_column);
1424 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1425 } else {
1426 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1427 }
1428 ds_put_char(&ctx->output, '\n');
1429
1430 if (!show || show->recurse) {
1431 return;
1432 }
1433
1434 show->recurse = true;
1435 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1436 const struct ovsdb_idl_column *column = show->columns[i];
1437 const struct ovsdb_datum *datum;
1438
1439 if (!column) {
1440 break;
1441 }
1442
1443 datum = ovsdb_idl_read(row, column);
1444 if (column->type.key.type == OVSDB_TYPE_UUID &&
1445 column->type.key.u.uuid.refTableName) {
1446 struct cmd_show_table *ref_show;
1447 size_t j;
1448
1449 ref_show = cmd_show_find_table_by_name(
1450 column->type.key.u.uuid.refTableName);
1451 if (ref_show) {
1452 for (j = 0; j < datum->n; j++) {
1453 const struct ovsdb_idl_row *ref_row;
1454
1455 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1456 ref_show->table,
1457 &datum->keys[j].uuid);
1458 if (ref_row) {
1459 cmd_show_row(ctx, ref_row, level + 1);
1460 }
1461 }
1462 continue;
1463 }
1464 }
1465
1466 if (!ovsdb_datum_is_default(datum, &column->type)) {
1467 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1468 ds_put_format(&ctx->output, "%s: ", column->name);
1469 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1470 ds_put_char(&ctx->output, '\n');
1471 }
1472 }
1473 show->recurse = false;
1474}
1475
1476static void
1477cmd_show(struct vsctl_context *ctx)
1478{
1479 const struct ovsdb_idl_row *row;
1480
1481 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1482 row; row = ovsdb_idl_next_row(row)) {
1483 cmd_show_row(ctx, row, 0);
1484 }
1485}
1486
e5e12280
BP
1487static void
1488pre_cmd_emer_reset(struct vsctl_context *ctx)
1489{
87824b0b 1490 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
e5e12280
BP
1491 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1492
1493 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
f67e3b66 1494 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
e5e12280
BP
1495 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1496 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1497 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
29089a54 1498 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ipfix);
e5e12280
BP
1499 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1500 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1501
1502 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1503
1504 ovsdb_idl_add_column(ctx->idl,
1505 &ovsrec_interface_col_ingress_policing_rate);
1506 ovsdb_idl_add_column(ctx->idl,
1507 &ovsrec_interface_col_ingress_policing_burst);
1508}
1509
18ee958b
JP
1510static void
1511cmd_emer_reset(struct vsctl_context *ctx)
1512{
1513 const struct ovsdb_idl *idl = ctx->idl;
1514 const struct ovsrec_bridge *br;
1515 const struct ovsrec_port *port;
1516 const struct ovsrec_interface *iface;
28a14bf3
EJ
1517 const struct ovsrec_mirror *mirror, *next_mirror;
1518 const struct ovsrec_controller *ctrl, *next_ctrl;
1519 const struct ovsrec_manager *mgr, *next_mgr;
1520 const struct ovsrec_netflow *nf, *next_nf;
1521 const struct ovsrec_ssl *ssl, *next_ssl;
1522 const struct ovsrec_sflow *sflow, *next_sflow;
29089a54
RL
1523 const struct ovsrec_ipfix *ipfix, *next_ipfix;
1524 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
18ee958b 1525
18ee958b 1526 /* Reset the Open_vSwitch table. */
87824b0b 1527 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
18ee958b
JP
1528 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1529
1530 OVSREC_BRIDGE_FOR_EACH (br, idl) {
a699f614 1531 const char *hwaddr;
18ee958b
JP
1532
1533 ovsrec_bridge_set_controller(br, NULL, 0);
f67e3b66 1534 ovsrec_bridge_set_fail_mode(br, NULL);
18ee958b
JP
1535 ovsrec_bridge_set_mirrors(br, NULL, 0);
1536 ovsrec_bridge_set_netflow(br, NULL);
1537 ovsrec_bridge_set_sflow(br, NULL);
29089a54 1538 ovsrec_bridge_set_ipfix(br, NULL);
18ee958b
JP
1539 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1540
1541 /* We only want to save the "hwaddr" key from other_config. */
a699f614
EJ
1542 hwaddr = smap_get(&br->other_config, "hwaddr");
1543 if (hwaddr) {
1544 struct smap smap = SMAP_INITIALIZER(&smap);
1545 smap_add(&smap, "hwaddr", hwaddr);
1546 ovsrec_bridge_set_other_config(br, &smap);
1547 smap_destroy(&smap);
18ee958b 1548 } else {
a699f614 1549 ovsrec_bridge_set_other_config(br, NULL);
18ee958b
JP
1550 }
1551 }
1552
1553 OVSREC_PORT_FOR_EACH (port, idl) {
a699f614 1554 ovsrec_port_set_other_config(port, NULL);
18ee958b
JP
1555 }
1556
1557 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1558 /* xxx What do we do about gre/patch devices created by mgr? */
1559
1560 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1561 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1562 }
28a14bf3
EJ
1563
1564 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1565 ovsrec_mirror_delete(mirror);
1566 }
1567
1568 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1569 ovsrec_controller_delete(ctrl);
1570 }
1571
1572 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1573 ovsrec_manager_delete(mgr);
1574 }
1575
1576 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1577 ovsrec_netflow_delete(nf);
1578 }
1579
1580 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1581 ovsrec_ssl_delete(ssl);
1582 }
1583
1584 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1585 ovsrec_sflow_delete(sflow);
1586 }
5ce5a6b5 1587
29089a54
RL
1588 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix, next_ipfix, idl) {
1589 ovsrec_ipfix_delete(ipfix);
1590 }
1591
1592 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset, idl) {
1593 ovsrec_flow_sample_collector_set_delete(fscset);
1594 }
1595
5ce5a6b5 1596 vsctl_context_invalidate_cache(ctx);
18ee958b
JP
1597}
1598
c75d1511 1599static void
5d9cb63c 1600cmd_add_br(struct vsctl_context *ctx)
c75d1511 1601{
e3c17733 1602 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
b89d8339 1603 const char *br_name, *parent_name;
c3ccfe98 1604 struct ovsrec_interface *iface;
b89d8339 1605 int vlan;
c75d1511 1606
aeee85aa
BP
1607 br_name = ctx->argv[1];
1608 if (ctx->argc == 2) {
1609 parent_name = NULL;
1610 vlan = 0;
1611 } else if (ctx->argc == 4) {
1612 parent_name = ctx->argv[2];
1613 vlan = atoi(ctx->argv[3]);
5341d046
BP
1614 if (vlan < 0 || vlan > 4095) {
1615 vsctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
aeee85aa
BP
1616 }
1617 } else {
1618 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1619 ctx->argv[0]);
1620 }
1621
5ce5a6b5 1622 vsctl_context_populate_cache(ctx);
aeee85aa
BP
1623 if (may_exist) {
1624 struct vsctl_bridge *br;
1625
5ce5a6b5 1626 br = find_bridge(ctx, br_name, false);
aeee85aa
BP
1627 if (br) {
1628 if (!parent_name) {
1629 if (br->parent) {
1630 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1631 "a VLAN bridge for VLAN %d",
1632 br_name, br_name, br->vlan);
1633 }
1634 } else {
1635 if (!br->parent) {
1636 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1637 "is not a VLAN bridge",
1638 br_name, parent_name, vlan, br_name);
1639 } else if (strcmp(br->parent->name, parent_name)) {
1640 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1641 "has the wrong parent %s",
1642 br_name, parent_name, vlan,
1643 br_name, br->parent->name);
1644 } else if (br->vlan != vlan) {
1645 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1646 "is a VLAN bridge for the wrong VLAN %d",
1647 br_name, parent_name, vlan, br_name, br->vlan);
1648 }
1649 }
1650 return;
1651 }
1652 }
5ce5a6b5 1653 check_conflicts(ctx, br_name,
c75d1511
BP
1654 xasprintf("cannot create a bridge named %s", br_name));
1655
aeee85aa 1656 if (!parent_name) {
c75d1511 1657 struct ovsrec_port *port;
aeee85aa 1658 struct ovsrec_bridge *br;
c75d1511 1659
f8ff4bc4 1660 iface = ovsrec_interface_insert(ctx->txn);
c75d1511 1661 ovsrec_interface_set_name(iface, br_name);
9106d88c 1662 ovsrec_interface_set_type(iface, "internal");
c75d1511 1663
f8ff4bc4 1664 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1665 ovsrec_port_set_name(port, br_name);
1666 ovsrec_port_set_interfaces(port, &iface, 1);
1667
f8ff4bc4 1668 br = ovsrec_bridge_insert(ctx->txn);
c75d1511
BP
1669 ovsrec_bridge_set_name(br, br_name);
1670 ovsrec_bridge_set_ports(br, &port, 1);
1671
5d9cb63c 1672 ovs_insert_bridge(ctx->ovs, br);
aeee85aa 1673 } else {
5dd9826c 1674 struct vsctl_bridge *conflict;
c75d1511
BP
1675 struct vsctl_bridge *parent;
1676 struct ovsrec_port *port;
aeee85aa 1677 struct ovsrec_bridge *br;
c75d1511
BP
1678 int64_t tag = vlan;
1679
5ce5a6b5 1680 parent = find_bridge(ctx, parent_name, false);
5341d046 1681 if (parent && parent->parent) {
11aa5627 1682 vsctl_fatal("cannot create bridge with fake bridge as parent");
c75d1511
BP
1683 }
1684 if (!parent) {
c88b6a27 1685 vsctl_fatal("parent bridge %s does not exist", parent_name);
c75d1511 1686 }
5dd9826c
BP
1687 conflict = find_vlan_bridge(parent, vlan);
1688 if (conflict) {
1689 vsctl_fatal("bridge %s already has a child VLAN bridge %s "
1690 "on VLAN %d", parent_name, conflict->name, vlan);
1691 }
c75d1511
BP
1692 br = parent->br_cfg;
1693
f8ff4bc4 1694 iface = ovsrec_interface_insert(ctx->txn);
c75d1511
BP
1695 ovsrec_interface_set_name(iface, br_name);
1696 ovsrec_interface_set_type(iface, "internal");
1697
f8ff4bc4 1698 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1699 ovsrec_port_set_name(port, br_name);
1700 ovsrec_port_set_interfaces(port, &iface, 1);
1701 ovsrec_port_set_fake_bridge(port, true);
1702 ovsrec_port_set_tag(port, &tag, 1);
dfbe07ba
BP
1703
1704 bridge_insert_port(br, port);
c75d1511
BP
1705 }
1706
c3ccfe98 1707 post_db_reload_expect_iface(iface);
5ce5a6b5 1708 vsctl_context_invalidate_cache(ctx);
c75d1511
BP
1709}
1710
1711static void
5ce5a6b5 1712del_port(struct vsctl_context *ctx, struct vsctl_port *port)
c75d1511 1713{
a341ee57 1714 struct vsctl_iface *iface, *next_iface;
28a14bf3 1715
c75d1511
BP
1716 bridge_delete_port((port->bridge->parent
1717 ? port->bridge->parent->br_cfg
1718 : port->bridge->br_cfg), port->port_cfg);
a341ee57
BP
1719
1720 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1721 del_cached_iface(ctx, iface);
1722 }
1723 del_cached_port(ctx, port);
1724}
1725
1726static void
1727del_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
1728{
1729 struct vsctl_bridge *child, *next_child;
1730 struct vsctl_port *port, *next_port;
29089a54 1731 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
a341ee57
BP
1732
1733 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1734 del_bridge(ctx, child);
1735 }
1736
1737 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1738 del_port(ctx, port);
1739 }
1740
29089a54
RL
1741 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset,
1742 ctx->idl) {
1743 if (fscset->bridge == br->br_cfg) {
1744 ovsrec_flow_sample_collector_set_delete(fscset);
1745 }
1746 }
1747
a341ee57 1748 del_cached_bridge(ctx, br);
c75d1511
BP
1749}
1750
1751static void
5d9cb63c 1752cmd_del_br(struct vsctl_context *ctx)
c75d1511 1753{
460aad80 1754 bool must_exist = !shash_find(&ctx->options, "--if-exists");
c75d1511
BP
1755 struct vsctl_bridge *bridge;
1756
5ce5a6b5
BP
1757 vsctl_context_populate_cache(ctx);
1758 bridge = find_bridge(ctx, ctx->argv[1], must_exist);
460aad80 1759 if (bridge) {
a341ee57 1760 del_bridge(ctx, bridge);
c75d1511 1761 }
c75d1511
BP
1762}
1763
dfbe07ba
BP
1764static void
1765output_sorted(struct svec *svec, struct ds *output)
1766{
1767 const char *name;
1768 size_t i;
1769
1770 svec_sort(svec);
1771 SVEC_FOR_EACH (i, name, svec) {
1772 ds_put_format(output, "%s\n", name);
1773 }
1774}
1775
c75d1511 1776static void
5d9cb63c 1777cmd_list_br(struct vsctl_context *ctx)
c75d1511
BP
1778{
1779 struct shash_node *node;
dfbe07ba 1780 struct svec bridges;
515d830a
JP
1781 bool real = shash_find(&ctx->options, "--real");
1782 bool fake = shash_find(&ctx->options, "--fake");
1783
1784 /* If neither fake nor real were requested, return both. */
1785 if (!real && !fake) {
1786 real = fake = true;
1787 }
c75d1511 1788
5ce5a6b5 1789 vsctl_context_populate_cache(ctx);
dfbe07ba
BP
1790
1791 svec_init(&bridges);
5ce5a6b5 1792 SHASH_FOR_EACH (node, &ctx->bridges) {
c75d1511 1793 struct vsctl_bridge *br = node->data;
515d830a
JP
1794
1795 if (br->parent ? fake : real) {
1796 svec_add(&bridges, br->name);
1797 }
c75d1511 1798 }
5d9cb63c 1799 output_sorted(&bridges, &ctx->output);
dfbe07ba 1800 svec_destroy(&bridges);
c75d1511
BP
1801}
1802
1803static void
5d9cb63c 1804cmd_br_exists(struct vsctl_context *ctx)
c75d1511 1805{
5ce5a6b5
BP
1806 vsctl_context_populate_cache(ctx);
1807 if (!find_bridge(ctx, ctx->argv[1], false)) {
1d48b4be 1808 vsctl_exit(2);
c75d1511 1809 }
c75d1511
BP
1810}
1811
457e1eb0 1812static void
a699f614
EJ
1813set_external_id(struct smap *old, struct smap *new,
1814 char *key, char *value)
457e1eb0 1815{
a699f614 1816 smap_clone(new, old);
457e1eb0 1817
457e1eb0 1818 if (value) {
a699f614
EJ
1819 smap_replace(new, key, value);
1820 } else {
1821 smap_remove(new, key);
457e1eb0 1822 }
457e1eb0
BP
1823}
1824
e5e12280
BP
1825static void
1826pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1827{
1828 pre_get_info(ctx);
1829 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1830 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1831}
1832
457e1eb0 1833static void
5d9cb63c 1834cmd_br_set_external_id(struct vsctl_context *ctx)
457e1eb0 1835{
457e1eb0 1836 struct vsctl_bridge *bridge;
a699f614 1837 struct smap new;
457e1eb0 1838
5ce5a6b5
BP
1839 vsctl_context_populate_cache(ctx);
1840 bridge = find_bridge(ctx, ctx->argv[1], true);
457e1eb0 1841 if (bridge->br_cfg) {
a699f614
EJ
1842
1843 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1844 ctx->argc >= 4 ? ctx->argv[3] : NULL);
f74055e7 1845 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
a699f614 1846 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
457e1eb0 1847 } else {
5d9cb63c 1848 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
5ce5a6b5 1849 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
a699f614
EJ
1850 set_external_id(&port->port_cfg->external_ids, &new,
1851 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
f74055e7 1852 ovsrec_port_verify_external_ids(port->port_cfg);
a699f614 1853 ovsrec_port_set_external_ids(port->port_cfg, &new);
457e1eb0
BP
1854 free(key);
1855 }
a699f614 1856 smap_destroy(&new);
457e1eb0
BP
1857}
1858
1859static void
a699f614 1860get_external_id(struct smap *smap, const char *prefix, const char *key,
457e1eb0
BP
1861 struct ds *output)
1862{
a699f614
EJ
1863 if (key) {
1864 char *prefix_key = xasprintf("%s%s", prefix, key);
1865 const char *value = smap_get(smap, prefix_key);
457e1eb0 1866
a699f614
EJ
1867 if (value) {
1868 ds_put_format(output, "%s\n", value);
1869 }
1870 free(prefix_key);
1871 } else {
1872 const struct smap_node **sorted = smap_sort(smap);
1873 size_t prefix_len = strlen(prefix);
1874 size_t i;
1875
1876 for (i = 0; i < smap_count(smap); i++) {
1877 const struct smap_node *node = sorted[i];
1878 if (!strncmp(node->key, prefix, prefix_len)) {
1879 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1880 node->value);
1881 }
457e1eb0 1882 }
a699f614 1883 free(sorted);
457e1eb0 1884 }
457e1eb0
BP
1885}
1886
e5e12280
BP
1887static void
1888pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1889{
1890 pre_cmd_br_set_external_id(ctx);
1891}
1892
457e1eb0 1893static void
5d9cb63c 1894cmd_br_get_external_id(struct vsctl_context *ctx)
457e1eb0 1895{
457e1eb0
BP
1896 struct vsctl_bridge *bridge;
1897
5ce5a6b5
BP
1898 vsctl_context_populate_cache(ctx);
1899
1900 bridge = find_bridge(ctx, ctx->argv[1], true);
457e1eb0 1901 if (bridge->br_cfg) {
f74055e7 1902 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
a699f614
EJ
1903 get_external_id(&bridge->br_cfg->external_ids, "",
1904 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
457e1eb0 1905 } else {
5ce5a6b5 1906 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
f74055e7 1907 ovsrec_port_verify_external_ids(port->port_cfg);
a699f614
EJ
1908 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1909 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
457e1eb0 1910 }
457e1eb0
BP
1911}
1912
c75d1511 1913static void
5d9cb63c 1914cmd_list_ports(struct vsctl_context *ctx)
c75d1511
BP
1915{
1916 struct vsctl_bridge *br;
a341ee57 1917 struct vsctl_port *port;
dfbe07ba 1918 struct svec ports;
c75d1511 1919
5ce5a6b5
BP
1920 vsctl_context_populate_cache(ctx);
1921 br = find_bridge(ctx, ctx->argv[1], true);
f74055e7 1922 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
dfbe07ba
BP
1923
1924 svec_init(&ports);
a341ee57
BP
1925 LIST_FOR_EACH (port, ports_node, &br->ports) {
1926 if (strcmp(port->port_cfg->name, br->name)) {
dfbe07ba 1927 svec_add(&ports, port->port_cfg->name);
c75d1511
BP
1928 }
1929 }
5d9cb63c 1930 output_sorted(&ports, &ctx->output);
dfbe07ba 1931 svec_destroy(&ports);
c75d1511
BP
1932}
1933
1934static void
f8ff4bc4 1935add_port(struct vsctl_context *ctx,
bb1c67c8
BP
1936 const char *br_name, const char *port_name,
1937 bool may_exist, bool fake_iface,
18b239f5
BP
1938 char *iface_names[], int n_ifaces,
1939 char *settings[], int n_settings)
c75d1511 1940{
a341ee57 1941 struct vsctl_port *vsctl_port;
c75d1511
BP
1942 struct vsctl_bridge *bridge;
1943 struct ovsrec_interface **ifaces;
1944 struct ovsrec_port *port;
1945 size_t i;
1946
5ce5a6b5 1947 vsctl_context_populate_cache(ctx);
bb1c67c8 1948 if (may_exist) {
2a022368 1949 struct vsctl_port *vsctl_port;
bb1c67c8 1950
5ce5a6b5 1951 vsctl_port = find_port(ctx, port_name, false);
2a022368 1952 if (vsctl_port) {
bb1c67c8 1953 struct svec want_names, have_names;
bb1c67c8
BP
1954
1955 svec_init(&want_names);
1956 for (i = 0; i < n_ifaces; i++) {
1957 svec_add(&want_names, iface_names[i]);
1958 }
1959 svec_sort(&want_names);
1960
1961 svec_init(&have_names);
2a022368
BP
1962 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1963 svec_add(&have_names,
1964 vsctl_port->port_cfg->interfaces[i]->name);
bb1c67c8
BP
1965 }
1966 svec_sort(&have_names);
1967
2a022368 1968 if (strcmp(vsctl_port->bridge->name, br_name)) {
bb1c67c8
BP
1969 char *command = vsctl_context_to_string(ctx);
1970 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
2a022368 1971 command, port_name, vsctl_port->bridge->name);
bb1c67c8
BP
1972 }
1973
1974 if (!svec_equal(&want_names, &have_names)) {
1975 char *have_names_string = svec_join(&have_names, ", ", "");
1976 char *command = vsctl_context_to_string(ctx);
1977
1978 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1979 command, port_name, have_names_string);
1980 }
1981
1982 svec_destroy(&want_names);
1983 svec_destroy(&have_names);
1984
1985 return;
1986 }
1987 }
5ce5a6b5 1988 check_conflicts(ctx, port_name,
c75d1511 1989 xasprintf("cannot create a port named %s", port_name));
bb1c67c8 1990 for (i = 0; i < n_ifaces; i++) {
5ce5a6b5 1991 check_conflicts(ctx, iface_names[i],
bb1c67c8
BP
1992 xasprintf("cannot create an interface named %s",
1993 iface_names[i]));
1994 }
5ce5a6b5 1995 bridge = find_bridge(ctx, br_name, true);
c75d1511
BP
1996
1997 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1998 for (i = 0; i < n_ifaces; i++) {
f8ff4bc4 1999 ifaces[i] = ovsrec_interface_insert(ctx->txn);
c75d1511 2000 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
c3ccfe98 2001 post_db_reload_expect_iface(ifaces[i]);
c75d1511
BP
2002 }
2003
f8ff4bc4 2004 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
2005 ovsrec_port_set_name(port, port_name);
2006 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
b4182c7f 2007 ovsrec_port_set_bond_fake_iface(port, fake_iface);
a0a9f31d 2008
5341d046 2009 if (bridge->parent) {
c75d1511
BP
2010 int64_t tag = bridge->vlan;
2011 ovsrec_port_set_tag(port, &tag, 1);
2012 }
2013
18b239f5 2014 for (i = 0; i < n_settings; i++) {
ce5a3e38
BP
2015 set_column(get_table("Port"), &port->header_, settings[i],
2016 ctx->symtab);
18b239f5
BP
2017 }
2018
c75d1511
BP
2019 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
2020 : bridge->br_cfg), port);
2021
a341ee57
BP
2022 vsctl_port = add_port_to_cache(ctx, bridge, port);
2023 for (i = 0; i < n_ifaces; i++) {
2024 add_iface_to_cache(ctx, vsctl_port, ifaces[i]);
2025 }
2026 free(ifaces);
c75d1511
BP
2027}
2028
2029static void
5d9cb63c 2030cmd_add_port(struct vsctl_context *ctx)
c75d1511 2031{
e3c17733 2032 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
bb1c67c8
BP
2033
2034 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
18b239f5 2035 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
c75d1511
BP
2036}
2037
2038static void
5d9cb63c 2039cmd_add_bond(struct vsctl_context *ctx)
c75d1511 2040{
e3c17733 2041 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
b4182c7f 2042 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
18b239f5
BP
2043 int n_ifaces;
2044 int i;
2045
2046 n_ifaces = ctx->argc - 3;
2047 for (i = 3; i < ctx->argc; i++) {
2048 if (strchr(ctx->argv[i], '=')) {
2049 n_ifaces = i - 3;
2050 break;
2051 }
2052 }
2053 if (n_ifaces < 2) {
2054 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
2055 "%d were specified", n_ifaces);
2056 }
b4182c7f 2057
bb1c67c8 2058 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
18b239f5
BP
2059 &ctx->argv[3], n_ifaces,
2060 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
c75d1511
BP
2061}
2062
2063static void
5d9cb63c 2064cmd_del_port(struct vsctl_context *ctx)
c75d1511 2065{
460aad80 2066 bool must_exist = !shash_find(&ctx->options, "--if-exists");
7c79588e 2067 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
89f3c258 2068 const char *target = ctx->argv[ctx->argc - 1];
7c79588e 2069 struct vsctl_port *port;
c75d1511 2070
5ce5a6b5 2071 vsctl_context_populate_cache(ctx);
89f3c258 2072 if (find_bridge(ctx, target, false)) {
25a27ba0
BP
2073 if (must_exist) {
2074 vsctl_fatal("cannot delete port %s because it is the local port "
2075 "for bridge %s (deleting this port requires deleting "
2076 "the entire bridge)", target, target);
2077 }
2078 port = NULL;
89f3c258
BP
2079 } else if (!with_iface) {
2080 port = find_port(ctx, target, must_exist);
7c79588e 2081 } else {
7c79588e
BP
2082 struct vsctl_iface *iface;
2083
5ce5a6b5 2084 port = find_port(ctx, target, false);
7c79588e 2085 if (!port) {
5ce5a6b5 2086 iface = find_iface(ctx, target, false);
7c79588e
BP
2087 if (iface) {
2088 port = iface->port;
2089 }
2090 }
2091 if (must_exist && !port) {
2092 vsctl_fatal("no port or interface named %s", target);
460aad80 2093 }
7c79588e 2094 }
460aad80 2095
7c79588e
BP
2096 if (port) {
2097 if (ctx->argc == 3) {
2098 struct vsctl_bridge *bridge;
2099
5ce5a6b5 2100 bridge = find_bridge(ctx, ctx->argv[1], true);
7c79588e
BP
2101 if (port->bridge != bridge) {
2102 if (port->bridge->parent == bridge) {
2103 vsctl_fatal("bridge %s does not have a port %s (although "
2104 "its parent bridge %s does)",
2105 ctx->argv[1], ctx->argv[2],
2106 bridge->parent->name);
2107 } else {
2108 vsctl_fatal("bridge %s does not have a port %s",
2109 ctx->argv[1], ctx->argv[2]);
2110 }
460aad80 2111 }
c75d1511 2112 }
7c79588e 2113
5ce5a6b5 2114 del_port(ctx, port);
c75d1511 2115 }
c75d1511
BP
2116}
2117
2118static void
5d9cb63c 2119cmd_port_to_br(struct vsctl_context *ctx)
c75d1511
BP
2120{
2121 struct vsctl_port *port;
c75d1511 2122
5ce5a6b5
BP
2123 vsctl_context_populate_cache(ctx);
2124
2125 port = find_port(ctx, ctx->argv[1], true);
5d9cb63c 2126 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
c75d1511
BP
2127}
2128
2129static void
5d9cb63c 2130cmd_br_to_vlan(struct vsctl_context *ctx)
c75d1511
BP
2131{
2132 struct vsctl_bridge *bridge;
c75d1511 2133
5ce5a6b5
BP
2134 vsctl_context_populate_cache(ctx);
2135
2136 bridge = find_bridge(ctx, ctx->argv[1], true);
5d9cb63c 2137 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
c75d1511
BP
2138}
2139
2140static void
5d9cb63c 2141cmd_br_to_parent(struct vsctl_context *ctx)
c75d1511
BP
2142{
2143 struct vsctl_bridge *bridge;
c75d1511 2144
5ce5a6b5
BP
2145 vsctl_context_populate_cache(ctx);
2146
2147 bridge = find_bridge(ctx, ctx->argv[1], true);
c75d1511
BP
2148 if (bridge->parent) {
2149 bridge = bridge->parent;
2150 }
5d9cb63c 2151 ds_put_format(&ctx->output, "%s\n", bridge->name);
c75d1511
BP
2152}
2153
2154static void
5d9cb63c 2155cmd_list_ifaces(struct vsctl_context *ctx)
c75d1511
BP
2156{
2157 struct vsctl_bridge *br;
a341ee57 2158 struct vsctl_port *port;
dfbe07ba 2159 struct svec ifaces;
c75d1511 2160
5ce5a6b5
BP
2161 vsctl_context_populate_cache(ctx);
2162
2163 br = find_bridge(ctx, ctx->argv[1], true);
f74055e7 2164 verify_ports(ctx);
dfbe07ba
BP
2165
2166 svec_init(&ifaces);
a341ee57
BP
2167 LIST_FOR_EACH (port, ports_node, &br->ports) {
2168 struct vsctl_iface *iface;
c75d1511 2169
a341ee57
BP
2170 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
2171 if (strcmp(iface->iface_cfg->name, br->name)) {
2172 svec_add(&ifaces, iface->iface_cfg->name);
2173 }
c75d1511
BP
2174 }
2175 }
5d9cb63c 2176 output_sorted(&ifaces, &ctx->output);
dfbe07ba 2177 svec_destroy(&ifaces);
c75d1511
BP
2178}
2179
2180static void
5d9cb63c 2181cmd_iface_to_br(struct vsctl_context *ctx)
c75d1511
BP
2182{
2183 struct vsctl_iface *iface;
c75d1511 2184
5ce5a6b5
BP
2185 vsctl_context_populate_cache(ctx);
2186
2187 iface = find_iface(ctx, ctx->argv[1], true);
5d9cb63c 2188 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
c75d1511 2189}
457e1eb0 2190
f74055e7
BP
2191static void
2192verify_controllers(struct ovsrec_bridge *bridge)
2193{
7da6c3a6 2194 size_t i;
f74055e7 2195
7da6c3a6
BP
2196 ovsrec_bridge_verify_controller(bridge);
2197 for (i = 0; i < bridge->n_controller; i++) {
2198 ovsrec_controller_verify_target(bridge->controller[i]);
f74055e7
BP
2199 }
2200}
2201
4e3e7ff9
BP
2202static void
2203pre_controller(struct vsctl_context *ctx)
2204{
2205 pre_get_info(ctx);
2206
2207 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
2208}
2209
76ce9432 2210static void
1a048029 2211cmd_get_controller(struct vsctl_context *ctx)
76ce9432 2212{
1a048029 2213 struct vsctl_bridge *br;
76ce9432
BP
2214 struct svec targets;
2215 size_t i;
2216
5ce5a6b5
BP
2217 vsctl_context_populate_cache(ctx);
2218
2219 br = find_bridge(ctx, ctx->argv[1], true);
7da6c3a6
BP
2220 if (br->parent) {
2221 br = br->parent;
2222 }
f74055e7 2223 verify_controllers(br->br_cfg);
1a048029
JP
2224
2225 /* Print the targets in sorted order for reproducibility. */
76ce9432 2226 svec_init(&targets);
286a2e82
BP
2227 for (i = 0; i < br->br_cfg->n_controller; i++) {
2228 svec_add(&targets, br->br_cfg->controller[i]->target);
76ce9432
BP
2229 }
2230
2231 svec_sort(&targets);
2232 for (i = 0; i < targets.n; i++) {
2233 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2234 }
2235 svec_destroy(&targets);
5aa00635
JP
2236}
2237
28a14bf3
EJ
2238static void
2239delete_controllers(struct ovsrec_controller **controllers,
2240 size_t n_controllers)
2241{
2242 size_t i;
2243
2244 for (i = 0; i < n_controllers; i++) {
2245 ovsrec_controller_delete(controllers[i]);
2246 }
2247}
2248
5aa00635
JP
2249static void
2250cmd_del_controller(struct vsctl_context *ctx)
2251{
286a2e82 2252 struct ovsrec_bridge *br;
5aa00635 2253
5ce5a6b5 2254 vsctl_context_populate_cache(ctx);
5aa00635 2255
286a2e82
BP
2256 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2257 verify_controllers(br);
5aa00635 2258
286a2e82
BP
2259 if (br->controller) {
2260 delete_controllers(br->controller, br->n_controller);
2261 ovsrec_bridge_set_controller(br, NULL, 0);
5ce5a6b5 2262 }
5aa00635
JP
2263}
2264
76ce9432
BP
2265static struct ovsrec_controller **
2266insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2267{
2268 struct ovsrec_controller **controllers;
2269 size_t i;
2270
2271 controllers = xmalloc(n * sizeof *controllers);
2272 for (i = 0; i < n; i++) {
070723f9
JP
2273 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
2274 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2275 }
76ce9432
BP
2276 controllers[i] = ovsrec_controller_insert(txn);
2277 ovsrec_controller_set_target(controllers[i], targets[i]);
2278 }
2279
2280 return controllers;
2281}
2282
5aa00635
JP
2283static void
2284cmd_set_controller(struct vsctl_context *ctx)
2285{
1a048029 2286 struct ovsrec_controller **controllers;
286a2e82 2287 struct ovsrec_bridge *br;
1a048029 2288 size_t n;
5aa00635 2289
5ce5a6b5
BP
2290 vsctl_context_populate_cache(ctx);
2291
286a2e82
BP
2292 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2293 verify_controllers(br);
28a14bf3 2294
286a2e82 2295 delete_controllers(br->controller, br->n_controller);
76ce9432 2296
1a048029
JP
2297 n = ctx->argc - 2;
2298 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
286a2e82 2299 ovsrec_bridge_set_controller(br, controllers, n);
1a048029 2300 free(controllers);
5aa00635
JP
2301}
2302
2303static void
2304cmd_get_fail_mode(struct vsctl_context *ctx)
2305{
1a048029 2306 struct vsctl_bridge *br;
753cb20f 2307 const char *fail_mode;
5aa00635 2308
5ce5a6b5
BP
2309 vsctl_context_populate_cache(ctx);
2310 br = find_bridge(ctx, ctx->argv[1], true);
5aa00635 2311
753cb20f
BP
2312 if (br->parent) {
2313 br = br->parent;
2314 }
2315 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2316
2317 fail_mode = br->br_cfg->fail_mode;
2318 if (fail_mode && strlen(fail_mode)) {
2319 ds_put_format(&ctx->output, "%s\n", fail_mode);
5aa00635 2320 }
5aa00635
JP
2321}
2322
2323static void
2324cmd_del_fail_mode(struct vsctl_context *ctx)
2325{
1a048029 2326 struct vsctl_bridge *br;
5aa00635 2327
5ce5a6b5 2328 vsctl_context_populate_cache(ctx);
5aa00635 2329
5ce5a6b5 2330 br = find_real_bridge(ctx, ctx->argv[1], true);
5aa00635 2331
5ce5a6b5 2332 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
5aa00635
JP
2333}
2334
2335static void
2336cmd_set_fail_mode(struct vsctl_context *ctx)
2337{
1a048029
JP
2338 struct vsctl_bridge *br;
2339 const char *fail_mode = ctx->argv[2];
5aa00635 2340
5ce5a6b5
BP
2341 vsctl_context_populate_cache(ctx);
2342
2343 br = find_real_bridge(ctx, ctx->argv[1], true);
5aa00635
JP
2344
2345 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2346 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2347 }
2348
31681a5d 2349 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
5aa00635 2350}
dd8ac6fe 2351
24b8b259
AE
2352static void
2353verify_managers(const struct ovsrec_open_vswitch *ovs)
2354{
2355 size_t i;
2356
24b8b259
AE
2357 ovsrec_open_vswitch_verify_manager_options(ovs);
2358
2359 for (i = 0; i < ovs->n_manager_options; ++i) {
2360 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2361
2362 ovsrec_manager_verify_target(mgr);
2363 }
2364}
2365
2366static void
2367pre_manager(struct vsctl_context *ctx)
2368{
24b8b259
AE
2369 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2370 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2371}
2372
2373static void
2374cmd_get_manager(struct vsctl_context *ctx)
2375{
2376 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2377 struct svec targets;
2378 size_t i;
2379
2380 verify_managers(ovs);
2381
2382 /* Print the targets in sorted order for reproducibility. */
2383 svec_init(&targets);
2384
24b8b259
AE
2385 for (i = 0; i < ovs->n_manager_options; i++) {
2386 svec_add(&targets, ovs->manager_options[i]->target);
2387 }
2388
2389 svec_sort_unique(&targets);
2390 for (i = 0; i < targets.n; i++) {
2391 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2392 }
2393 svec_destroy(&targets);
2394}
2395
24b8b259 2396static void
28a14bf3 2397delete_managers(const struct vsctl_context *ctx)
24b8b259
AE
2398{
2399 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
28a14bf3
EJ
2400 size_t i;
2401
2402 /* Delete Manager rows pointed to by 'manager_options' column. */
2403 for (i = 0; i < ovs->n_manager_options; i++) {
2404 ovsrec_manager_delete(ovs->manager_options[i]);
2405 }
24b8b259 2406
28a14bf3 2407 /* Delete 'Manager' row refs in 'manager_options' column. */
c5f341ab 2408 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
24b8b259
AE
2409}
2410
28a14bf3
EJ
2411static void
2412cmd_del_manager(struct vsctl_context *ctx)
2413{
2414 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2415
2416 verify_managers(ovs);
2417 delete_managers(ctx);
2418}
2419
24b8b259
AE
2420static void
2421insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2422{
2423 struct ovsrec_manager **managers;
2424 size_t i;
2425
24b8b259
AE
2426 /* Insert each manager in a new row in Manager table. */
2427 managers = xmalloc(n * sizeof *managers);
2428 for (i = 0; i < n; i++) {
070723f9
JP
2429 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2430 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2431 }
24b8b259
AE
2432 managers[i] = ovsrec_manager_insert(ctx->txn);
2433 ovsrec_manager_set_target(managers[i], targets[i]);
2434 }
2435
2436 /* Store uuids of new Manager rows in 'manager_options' column. */
2437 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2438 free(managers);
2439}
2440
2441static void
2442cmd_set_manager(struct vsctl_context *ctx)
2443{
2444 const size_t n = ctx->argc - 1;
2445
28a14bf3
EJ
2446 verify_managers(ctx->ovs);
2447 delete_managers(ctx);
24b8b259
AE
2448 insert_managers(ctx, &ctx->argv[1], n);
2449}
2450
e5e12280
BP
2451static void
2452pre_cmd_get_ssl(struct vsctl_context *ctx)
2453{
2454 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2455
2456 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2457 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2458 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2459 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2460}
2461
dd8ac6fe
JP
2462static void
2463cmd_get_ssl(struct vsctl_context *ctx)
2464{
2465 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2466
f74055e7 2467 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
dd8ac6fe 2468 if (ssl) {
f74055e7
BP
2469 ovsrec_ssl_verify_private_key(ssl);
2470 ovsrec_ssl_verify_certificate(ssl);
2471 ovsrec_ssl_verify_ca_cert(ssl);
2472 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2473
dd8ac6fe
JP
2474 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2475 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2476 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2477 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2478 ssl->bootstrap_ca_cert ? "true" : "false");
2479 }
2480}
2481
e5e12280
BP
2482static void
2483pre_cmd_del_ssl(struct vsctl_context *ctx)
2484{
2485 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2486}
2487
dd8ac6fe
JP
2488static void
2489cmd_del_ssl(struct vsctl_context *ctx)
2490{
28a14bf3
EJ
2491 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2492
2493 if (ssl) {
2494 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2495 ovsrec_ssl_delete(ssl);
2496 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2497 }
dd8ac6fe
JP
2498}
2499
e5e12280
BP
2500static void
2501pre_cmd_set_ssl(struct vsctl_context *ctx)
2502{
2503 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2504}
2505
dd8ac6fe
JP
2506static void
2507cmd_set_ssl(struct vsctl_context *ctx)
2508{
2509 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
28a14bf3 2510 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
dd8ac6fe 2511
28a14bf3
EJ
2512 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2513 if (ssl) {
2514 ovsrec_ssl_delete(ssl);
2515 }
f8ff4bc4 2516 ssl = ovsrec_ssl_insert(ctx->txn);
dd8ac6fe
JP
2517
2518 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2519 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2520 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2521
2522 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2523
2524 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2525}
c75d1511 2526\f
ad83bfa6
BP
2527/* Parameter commands. */
2528
ad83bfa6
BP
2529struct vsctl_row_id {
2530 const struct ovsdb_idl_table_class *table;
2531 const struct ovsdb_idl_column *name_column;
2532 const struct ovsdb_idl_column *uuid_column;
2533};
2534
2535struct vsctl_table_class {
2536 struct ovsdb_idl_table_class *class;
ad83bfa6
BP
2537 struct vsctl_row_id row_ids[2];
2538};
2539
2540static const struct vsctl_table_class tables[] = {
bd76d25d 2541 {&ovsrec_table_bridge,
ad83bfa6 2542 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
29089a54
RL
2543 {&ovsrec_table_flow_sample_collector_set, NULL,
2544 &ovsrec_flow_sample_collector_set_col_bridge}}},
ad83bfa6 2545
bd76d25d 2546 {&ovsrec_table_controller,
ad83bfa6
BP
2547 {{&ovsrec_table_bridge,
2548 &ovsrec_bridge_col_name,
1a048029 2549 &ovsrec_bridge_col_controller}}},
ad83bfa6 2550
bd76d25d 2551 {&ovsrec_table_interface,
ad83bfa6
BP
2552 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2553 {NULL, NULL, NULL}}},
2554
bd76d25d 2555 {&ovsrec_table_mirror,
ad83bfa6
BP
2556 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2557 {NULL, NULL, NULL}}},
2558
94db5407
BP
2559 {&ovsrec_table_manager,
2560 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2561 {NULL, NULL, NULL}}},
2562
bd76d25d 2563 {&ovsrec_table_netflow,
ad83bfa6
BP
2564 {{&ovsrec_table_bridge,
2565 &ovsrec_bridge_col_name,
2566 &ovsrec_bridge_col_netflow},
2567 {NULL, NULL, NULL}}},
2568
bd76d25d 2569 {&ovsrec_table_open_vswitch,
ad83bfa6
BP
2570 {{&ovsrec_table_open_vswitch, NULL, NULL},
2571 {NULL, NULL, NULL}}},
2572
bd76d25d 2573 {&ovsrec_table_port,
ad83bfa6
BP
2574 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2575 {NULL, NULL, NULL}}},
2576
c1c9c9c4
BP
2577 {&ovsrec_table_qos,
2578 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2579 {NULL, NULL, NULL}}},
2580
2581 {&ovsrec_table_queue,
2582 {{NULL, NULL, NULL},
2583 {NULL, NULL, NULL}}},
2584
bd76d25d 2585 {&ovsrec_table_ssl,
ad83bfa6
BP
2586 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2587
d01600a2
BP
2588 {&ovsrec_table_sflow,
2589 {{&ovsrec_table_bridge,
2590 &ovsrec_bridge_col_name,
2591 &ovsrec_bridge_col_sflow},
2592 {NULL, NULL, NULL}}},
2593
254750ce
BP
2594 {&ovsrec_table_flow_table,
2595 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2596 {NULL, NULL, NULL}}},
2597
29089a54
RL
2598 {&ovsrec_table_ipfix,
2599 {{&ovsrec_table_bridge,
2600 &ovsrec_bridge_col_name,
2601 &ovsrec_bridge_col_ipfix},
2602 {&ovsrec_table_flow_sample_collector_set, NULL,
2603 &ovsrec_flow_sample_collector_set_col_ipfix}}},
2604
2605 {&ovsrec_table_flow_sample_collector_set,
2606 {{NULL, NULL, NULL},
2607 {NULL, NULL, NULL}}},
2608
bd76d25d 2609 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
ad83bfa6
BP
2610};
2611
1bc6ff29
BP
2612static void
2613die_if_error(char *error)
2614{
2615 if (error) {
def90f62 2616 vsctl_fatal("%s", error);
1bc6ff29
BP
2617 }
2618}
2619
ad83bfa6
BP
2620static int
2621to_lower_and_underscores(unsigned c)
2622{
2623 return c == '-' ? '_' : tolower(c);
2624}
2625
2626static unsigned int
2627score_partial_match(const char *name, const char *s)
2628{
2629 int score;
2630
5128bd9c
BP
2631 if (!strcmp(name, s)) {
2632 return UINT_MAX;
2633 }
ad83bfa6
BP
2634 for (score = 0; ; score++, name++, s++) {
2635 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2636 break;
2637 } else if (*name == '\0') {
5128bd9c 2638 return UINT_MAX - 1;
ad83bfa6
BP
2639 }
2640 }
2641 return *s == '\0' ? score : 0;
2642}
2643
2644static const struct vsctl_table_class *
2645get_table(const char *table_name)
2646{
2647 const struct vsctl_table_class *table;
2648 const struct vsctl_table_class *best_match = NULL;
2649 unsigned int best_score = 0;
2650
2651 for (table = tables; table->class; table++) {
2652 unsigned int score = score_partial_match(table->class->name,
2653 table_name);
2654 if (score > best_score) {
2655 best_match = table;
2656 best_score = score;
2657 } else if (score == best_score) {
2658 best_match = NULL;
2659 }
2660 }
2661 if (best_match) {
2662 return best_match;
2663 } else if (best_score) {
def90f62 2664 vsctl_fatal("multiple table names match \"%s\"", table_name);
ad83bfa6 2665 } else {
def90f62 2666 vsctl_fatal("unknown table \"%s\"", table_name);
ad83bfa6
BP
2667 }
2668}
2669
e5e12280
BP
2670static const struct vsctl_table_class *
2671pre_get_table(struct vsctl_context *ctx, const char *table_name)
2672{
2673 const struct vsctl_table_class *table_class;
2674 int i;
2675
2676 table_class = get_table(table_name);
2677 ovsdb_idl_add_table(ctx->idl, table_class->class);
2678
2679 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2680 const struct vsctl_row_id *id = &table_class->row_ids[i];
2681 if (id->table) {
2682 ovsdb_idl_add_table(ctx->idl, id->table);
2683 }
2684 if (id->name_column) {
2685 ovsdb_idl_add_column(ctx->idl, id->name_column);
2686 }
2687 if (id->uuid_column) {
2688 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2689 }
2690 }
2691
2692 return table_class;
2693}
2694
ad83bfa6
BP
2695static const struct ovsdb_idl_row *
2696get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
e111e681 2697 const struct vsctl_row_id *id, const char *record_id)
ad83bfa6
BP
2698{
2699 const struct ovsdb_idl_row *referrer, *final;
2700
2701 if (!id->table) {
2702 return NULL;
2703 }
2704
2705 if (!id->name_column) {
2706 if (strcmp(record_id, ".")) {
2707 return NULL;
2708 }
2709 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2710 if (!referrer || ovsdb_idl_next_row(referrer)) {
2711 return NULL;
2712 }
2713 } else {
2714 const struct ovsdb_idl_row *row;
ad83bfa6 2715
ad83bfa6
BP
2716 referrer = NULL;
2717 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
e111e681 2718 row != NULL;
ad83bfa6
BP
2719 row = ovsdb_idl_next_row(row))
2720 {
8c3c2f30 2721 const struct ovsdb_datum *name;
ad83bfa6 2722
8c3c2f30
BP
2723 name = ovsdb_idl_get(row, id->name_column,
2724 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
e111e681
BP
2725 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2726 if (referrer) {
2727 vsctl_fatal("multiple rows in %s match \"%s\"",
2728 table->class->name, record_id);
ad83bfa6 2729 }
e111e681 2730 referrer = row;
ad83bfa6 2731 }
ad83bfa6
BP
2732 }
2733 }
2734 if (!referrer) {
2735 return NULL;
2736 }
2737
93255bc5 2738 final = NULL;
ad83bfa6 2739 if (id->uuid_column) {
8c3c2f30 2740 const struct ovsdb_datum *uuid;
ad83bfa6 2741
f74055e7 2742 ovsdb_idl_txn_verify(referrer, id->uuid_column);
8c3c2f30
BP
2743 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2744 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2745 if (uuid->n == 1) {
ad83bfa6 2746 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
8c3c2f30 2747 &uuid->keys[0].uuid);
ad83bfa6 2748 }
ad83bfa6
BP
2749 } else {
2750 final = referrer;
2751 }
2752
2753 return final;
2754}
2755
2756static const struct ovsdb_idl_row *
e111e681 2757get_row (struct vsctl_context *ctx,
e99d41f7
BP
2758 const struct vsctl_table_class *table, const char *record_id,
2759 bool must_exist)
ad83bfa6
BP
2760{
2761 const struct ovsdb_idl_row *row;
2762 struct uuid uuid;
2763
17fb85a5 2764 row = NULL;
ad83bfa6
BP
2765 if (uuid_from_string(&uuid, record_id)) {
2766 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
17fb85a5
BP
2767 }
2768 if (!row) {
ad83bfa6
BP
2769 int i;
2770
2771 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
e111e681 2772 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
ad83bfa6
BP
2773 if (row) {
2774 break;
2775 }
2776 }
2777 }
e99d41f7 2778 if (must_exist && !row) {
def90f62
BP
2779 vsctl_fatal("no row \"%s\" in table %s",
2780 record_id, table->class->name);
ad83bfa6
BP
2781 }
2782 return row;
2783}
2784
1bc6ff29
BP
2785static char *
2786get_column(const struct vsctl_table_class *table, const char *column_name,
bd76d25d 2787 const struct ovsdb_idl_column **columnp)
ad83bfa6 2788{
bd76d25d 2789 const struct ovsdb_idl_column *best_match = NULL;
ad83bfa6 2790 unsigned int best_score = 0;
bd76d25d 2791 size_t i;
ad83bfa6 2792
bd76d25d
BP
2793 for (i = 0; i < table->class->n_columns; i++) {
2794 const struct ovsdb_idl_column *column = &table->class->columns[i];
2795 unsigned int score = score_partial_match(column->name, column_name);
2796 if (score > best_score) {
2797 best_match = column;
2798 best_score = score;
2799 } else if (score == best_score) {
2800 best_match = NULL;
ad83bfa6
BP
2801 }
2802 }
1bc6ff29
BP
2803
2804 *columnp = best_match;
ad83bfa6 2805 if (best_match) {
1bc6ff29 2806 return NULL;
ad83bfa6 2807 } else if (best_score) {
1bc6ff29
BP
2808 return xasprintf("%s contains more than one column whose name "
2809 "matches \"%s\"", table->class->name, column_name);
ad83bfa6 2810 } else {
1bc6ff29
BP
2811 return xasprintf("%s does not contain a column whose name matches "
2812 "\"%s\"", table->class->name, column_name);
ad83bfa6
BP
2813 }
2814}
2815
c5f341ab 2816static struct ovsdb_symbol *
aed133bf
BP
2817create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2818{
2819 struct ovsdb_symbol *symbol;
2820
2821 if (id[0] != '@') {
2822 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2823 }
2824
2825 if (newp) {
2826 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2827 }
2828
2829 symbol = ovsdb_symbol_table_insert(symtab, id);
e9387de4 2830 if (symbol->created) {
aed133bf
BP
2831 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2832 id);
2833 }
e9387de4 2834 symbol->created = true;
c5f341ab 2835 return symbol;
aed133bf
BP
2836}
2837
e5e12280
BP
2838static void
2839pre_get_column(struct vsctl_context *ctx,
2840 const struct vsctl_table_class *table, const char *column_name,
2841 const struct ovsdb_idl_column **columnp)
2842{
2843 die_if_error(get_column(table, column_name, columnp));
2844 ovsdb_idl_add_column(ctx->idl, *columnp);
2845}
2846
e89e5374
BP
2847static char *
2848missing_operator_error(const char *arg, const char **allowed_operators,
2849 size_t n_allowed)
2850{
2851 struct ds s;
2852
2853 ds_init(&s);
2854 ds_put_format(&s, "%s: argument does not end in ", arg);
2855 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2856 if (n_allowed == 2) {
2857 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2858 } else if (n_allowed > 2) {
2859 size_t i;
2860
2861 for (i = 1; i < n_allowed - 1; i++) {
2862 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2863 }
2864 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2865 }
2866 ds_put_format(&s, " followed by a value.");
2867
2868 return ds_steal_cstr(&s);
2869}
2870
2871/* Breaks 'arg' apart into a number of fields in the following order:
2872 *
4a433a0f
BP
2873 * - The name of a column in 'table', stored into '*columnp'. The column
2874 * name may be abbreviated.
e89e5374 2875 *
4a433a0f
BP
2876 * - Optionally ':' followed by a key string. The key is stored as a
2877 * malloc()'d string into '*keyp', or NULL if no key is present in
2878 * 'arg'.
e89e5374
BP
2879 *
2880 * - If 'valuep' is nonnull, an operator followed by a value string. The
2881 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2882 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2a9537e2
BP
2883 * index of the operator within 'allowed_operators' is stored into
2884 * '*operatorp'. The value is stored as a malloc()'d string into
2885 * '*valuep', or NULL if no value is present in 'arg'.
e89e5374 2886 *
e89e5374
BP
2887 * On success, returns NULL. On failure, returned a malloc()'d string error
2888 * message and stores NULL into all of the nonnull output arguments. */
cab50449 2889static char * OVS_WARN_UNUSED_RESULT
e89e5374
BP
2890parse_column_key_value(const char *arg,
2891 const struct vsctl_table_class *table,
2892 const struct ovsdb_idl_column **columnp, char **keyp,
2a9537e2 2893 int *operatorp,
e89e5374
BP
2894 const char **allowed_operators, size_t n_allowed,
2895 char **valuep)
ad83bfa6
BP
2896{
2897 const char *p = arg;
4a433a0f 2898 char *column_name;
1bc6ff29 2899 char *error;
ad83bfa6 2900
cb22974d 2901 ovs_assert(!(operatorp && !valuep));
4a433a0f 2902 *keyp = NULL;
1bc6ff29
BP
2903 if (valuep) {
2904 *valuep = NULL;
2905 }
ad83bfa6
BP
2906
2907 /* Parse column name. */
4a433a0f
BP
2908 error = ovsdb_token_parse(&p, &column_name);
2909 if (error) {
2910 goto error;
2911 }
2912 if (column_name[0] == '\0') {
a3326252 2913 free(column_name);
4a433a0f
BP
2914 error = xasprintf("%s: missing column name", arg);
2915 goto error;
2916 }
2917 error = get_column(table, column_name, columnp);
2918 free(column_name);
2919 if (error) {
2920 goto error;
ad83bfa6
BP
2921 }
2922
2923 /* Parse key string. */
4a433a0f
BP
2924 if (*p == ':') {
2925 p++;
1bc6ff29
BP
2926 error = ovsdb_token_parse(&p, keyp);
2927 if (error) {
2928 goto error;
ad83bfa6 2929 }
ad83bfa6
BP
2930 }
2931
2932 /* Parse value string. */
e89e5374 2933 if (valuep) {
e89e5374
BP
2934 size_t best_len;
2935 size_t i;
2a9537e2 2936 int best;
e89e5374
BP
2937
2938 if (!allowed_operators) {
2939 static const char *equals = "=";
2940 allowed_operators = &equals;
2941 n_allowed = 1;
2942 }
2943
2a9537e2 2944 best = -1;
e89e5374
BP
2945 best_len = 0;
2946 for (i = 0; i < n_allowed; i++) {
2947 const char *op = allowed_operators[i];
2948 size_t op_len = strlen(op);
2949
7db03f7c 2950 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
e89e5374 2951 best_len = op_len;
2a9537e2 2952 best = i;
e89e5374
BP
2953 }
2954 }
2a9537e2 2955 if (best < 0) {
e89e5374 2956 error = missing_operator_error(arg, allowed_operators, n_allowed);
1bc6ff29 2957 goto error;
ad83bfa6 2958 }
e89e5374
BP
2959
2960 if (operatorp) {
2961 *operatorp = best;
2962 }
7db03f7c 2963 *valuep = xstrdup(p + best_len);
ad83bfa6 2964 } else {
1bc6ff29 2965 if (*p != '\0') {
c29a8ba8
BP
2966 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2967 arg, p);
1bc6ff29
BP
2968 goto error;
2969 }
2970 }
2971 return NULL;
2972
2973error:
4a433a0f
BP
2974 *columnp = NULL;
2975 free(*keyp);
2976 *keyp = NULL;
1bc6ff29
BP
2977 if (valuep) {
2978 free(*valuep);
2979 *valuep = NULL;
e89e5374 2980 if (operatorp) {
2a9537e2 2981 *operatorp = -1;
e89e5374 2982 }
ad83bfa6 2983 }
1bc6ff29 2984 return error;
ad83bfa6
BP
2985}
2986
341c4e59 2987static const struct ovsdb_idl_column *
e5e12280
BP
2988pre_parse_column_key_value(struct vsctl_context *ctx,
2989 const char *arg,
2990 const struct vsctl_table_class *table)
2991{
2992 const struct ovsdb_idl_column *column;
2993 const char *p;
2994 char *column_name;
2995
2996 p = arg;
2997 die_if_error(ovsdb_token_parse(&p, &column_name));
2998 if (column_name[0] == '\0') {
2999 vsctl_fatal("%s: missing column name", arg);
3000 }
3001
3002 pre_get_column(ctx, table, column_name, &column);
3003 free(column_name);
341c4e59
BP
3004
3005 return column;
3006}
3007
3008static void
ff495b63 3009check_mutable(const struct ovsdb_idl_row *row,
341c4e59
BP
3010 const struct ovsdb_idl_column *column)
3011{
ff495b63 3012 if (!ovsdb_idl_is_mutable(row, column)) {
341c4e59 3013 vsctl_fatal("cannot modify read-only column %s in table %s",
ff495b63 3014 column->name, row->table->class->name);
341c4e59 3015 }
e5e12280
BP
3016}
3017
3018static void
3019pre_cmd_get(struct vsctl_context *ctx)
3020{
6d5abe94 3021 const char *id = shash_find_data(&ctx->options, "--id");
e5e12280
BP
3022 const char *table_name = ctx->argv[1];
3023 const struct vsctl_table_class *table;
3024 int i;
3025
6d5abe94
BP
3026 /* Using "get" without --id or a column name could possibly make sense.
3027 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
3028 * But it is unlikely that an interactive user would want to do that, so
3029 * issue a warning if we're running on a terminal. */
3030 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
3031 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
3032 "possibly erroneous");
3033 }
3034
e5e12280
BP
3035 table = pre_get_table(ctx, table_name);
3036 for (i = 3; i < ctx->argc; i++) {
3037 if (!strcasecmp(ctx->argv[i], "_uuid")
3038 || !strcasecmp(ctx->argv[i], "-uuid")) {
3039 continue;
3040 }
3041
3042 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3043 }
3044}
3045
ad83bfa6
BP
3046static void
3047cmd_get(struct vsctl_context *ctx)
3048{
aed133bf 3049 const char *id = shash_find_data(&ctx->options, "--id");
e99d41f7 3050 bool must_exist = !shash_find(&ctx->options, "--if-exists");
ad83bfa6
BP
3051 const char *table_name = ctx->argv[1];
3052 const char *record_id = ctx->argv[2];
3053 const struct vsctl_table_class *table;
3054 const struct ovsdb_idl_row *row;
3055 struct ds *out = &ctx->output;
3056 int i;
3057
e99d41f7
BP
3058 if (id && !must_exist) {
3059 vsctl_fatal("--if-exists and --id may not be specified together");
3060 }
3061
ad83bfa6 3062 table = get_table(table_name);
e99d41f7
BP
3063 row = get_row(ctx, table, record_id, must_exist);
3064 if (!row) {
3065 return;
3066 }
3067
aed133bf 3068 if (id) {
c5f341ab 3069 struct ovsdb_symbol *symbol;
aed133bf
BP
3070 bool new;
3071
c5f341ab 3072 symbol = create_symbol(ctx->symtab, id, &new);
aed133bf
BP
3073 if (!new) {
3074 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
3075 "before it was defined", id);
3076 }
c5f341ab
BP
3077 symbol->uuid = row->uuid;
3078
3079 /* This symbol refers to a row that already exists, so disable warnings
3080 * about it being unreferenced. */
3081 symbol->strong_ref = true;
aed133bf 3082 }
ad83bfa6 3083 for (i = 3; i < ctx->argc; i++) {
bd76d25d 3084 const struct ovsdb_idl_column *column;
8c3c2f30 3085 const struct ovsdb_datum *datum;
ad83bfa6
BP
3086 char *key_string;
3087
f40a9b61
BP
3088 /* Special case for obtaining the UUID of a row. We can't just do this
3089 * through parse_column_key_value() below since it returns a "struct
3090 * ovsdb_idl_column" and the UUID column doesn't have one. */
3091 if (!strcasecmp(ctx->argv[i], "_uuid")
3092 || !strcasecmp(ctx->argv[i], "-uuid")) {
3093 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
3094 continue;
3095 }
3096
1bc6ff29 3097 die_if_error(parse_column_key_value(ctx->argv[i], table,
e89e5374
BP
3098 &column, &key_string,
3099 NULL, NULL, 0, NULL));
ad83bfa6 3100
f74055e7 3101 ovsdb_idl_txn_verify(row, column);
8c3c2f30 3102 datum = ovsdb_idl_read(row, column);
ad83bfa6
BP
3103 if (key_string) {
3104 union ovsdb_atom key;
3105 unsigned int idx;
3106
bd76d25d 3107 if (column->type.value.type == OVSDB_TYPE_VOID) {
def90f62 3108 vsctl_fatal("cannot specify key to get for non-map column %s",
bd76d25d 3109 column->name);
ad83bfa6
BP
3110 }
3111
1bc6ff29 3112 die_if_error(ovsdb_atom_from_string(&key,
bd76d25d 3113 &column->type.key,
ce5a3e38 3114 key_string, ctx->symtab));
ad83bfa6 3115
8c3c2f30 3116 idx = ovsdb_datum_find_key(datum, &key,
bd76d25d 3117 column->type.key.type);
ad83bfa6 3118 if (idx == UINT_MAX) {
e99d41f7 3119 if (must_exist) {
def90f62
BP
3120 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
3121 key_string, table->class->name, record_id,
bd76d25d 3122 column->name);
870aeb4a
BP
3123 }
3124 } else {
8c3c2f30 3125 ovsdb_atom_to_string(&datum->values[idx],
bd76d25d 3126 column->type.value.type, out);
ad83bfa6 3127 }
bd76d25d 3128 ovsdb_atom_destroy(&key, column->type.key.type);
ad83bfa6 3129 } else {
8c3c2f30 3130 ovsdb_datum_to_string(datum, &column->type, out);
ad83bfa6
BP
3131 }
3132 ds_put_char(out, '\n');
ad83bfa6
BP
3133
3134 free(key_string);
3135 }
3136}
3137
9591fefe
BP
3138static void
3139parse_column_names(const char *column_names,
3140 const struct vsctl_table_class *table,
3141 const struct ovsdb_idl_column ***columnsp,
3142 size_t *n_columnsp)
3143{
3144 const struct ovsdb_idl_column **columns;
3145 size_t n_columns;
3146
3147 if (!column_names) {
3148 size_t i;
3149
3150 n_columns = table->class->n_columns + 1;
3151 columns = xmalloc(n_columns * sizeof *columns);
3152 columns[0] = NULL;
3153 for (i = 0; i < table->class->n_columns; i++) {
3154 columns[i + 1] = &table->class->columns[i];
3155 }
3156 } else {
3157 char *s = xstrdup(column_names);
3158 size_t allocated_columns;
3159 char *save_ptr = NULL;
3160 char *column_name;
3161
3162 columns = NULL;
3163 allocated_columns = n_columns = 0;
3164 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
3165 column_name = strtok_r(NULL, ", ", &save_ptr)) {
3166 const struct ovsdb_idl_column *column;
3167
3168 if (!strcasecmp(column_name, "_uuid")) {
3169 column = NULL;
3170 } else {
3171 die_if_error(get_column(table, column_name, &column));
3172 }
3173 if (n_columns >= allocated_columns) {
3174 columns = x2nrealloc(columns, &allocated_columns,
3175 sizeof *columns);
3176 }
3177 columns[n_columns++] = column;
3178 }
3179 free(s);
3180
3181 if (!n_columns) {
3182 vsctl_fatal("must specify at least one column name");
3183 }
3184 }
3185 *columnsp = columns;
3186 *n_columnsp = n_columns;
3187}
3188
3189
3190static void
3191pre_list_columns(struct vsctl_context *ctx,
3192 const struct vsctl_table_class *table,
3193 const char *column_names)
3194{
3195 const struct ovsdb_idl_column **columns;
3196 size_t n_columns;
3197 size_t i;
3198
3199 parse_column_names(column_names, table, &columns, &n_columns);
3200 for (i = 0; i < n_columns; i++) {
3201 if (columns[i]) {
3202 ovsdb_idl_add_column(ctx->idl, columns[i]);
3203 }
3204 }
3205 free(columns);
3206}
3207
e5e12280
BP
3208static void
3209pre_cmd_list(struct vsctl_context *ctx)
3210{
9591fefe 3211 const char *column_names = shash_find_data(&ctx->options, "--columns");
e5e12280
BP
3212 const char *table_name = ctx->argv[1];
3213 const struct vsctl_table_class *table;
e5e12280
BP
3214
3215 table = pre_get_table(ctx, table_name);
9591fefe 3216 pre_list_columns(ctx, table, column_names);
e5e12280
BP
3217}
3218
e051b42c
BP
3219static struct table *
3220list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
3221{
3222 struct table *out;
3223 size_t i;
3224
3225 out = xmalloc(sizeof *out);
3226 table_init(out);
3227
3228 for (i = 0; i < n_columns; i++) {
3229 const struct ovsdb_idl_column *column = columns[i];
3230 const char *column_name = column ? column->name : "_uuid";
3231
3232 table_add_column(out, "%s", column_name);
3233 }
3234
3235 return out;
3236}
3237
ad83bfa6 3238static void
9591fefe
BP
3239list_record(const struct ovsdb_idl_row *row,
3240 const struct ovsdb_idl_column **columns, size_t n_columns,
e051b42c 3241 struct table *out)
ad83bfa6 3242{
bd76d25d 3243 size_t i;
ad83bfa6 3244
e99d41f7
BP
3245 if (!row) {
3246 return;
3247 }
3248
e051b42c 3249 table_add_row(out);
9591fefe
BP
3250 for (i = 0; i < n_columns; i++) {
3251 const struct ovsdb_idl_column *column = columns[i];
e051b42c 3252 struct cell *cell = table_add_cell(out);
ad83bfa6 3253
9591fefe 3254 if (!column) {
e051b42c
BP
3255 struct ovsdb_datum datum;
3256 union ovsdb_atom atom;
3257
3258 atom.uuid = row->uuid;
3259
3260 datum.keys = &atom;
3261 datum.values = NULL;
3262 datum.n = 1;
3263
3264 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
3265 cell->type = &ovsdb_type_uuid;
9591fefe
BP
3266 } else {
3267 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
e051b42c
BP
3268
3269 cell->json = ovsdb_datum_to_json(datum, &column->type);
3270 cell->type = &column->type;
9591fefe 3271 }
ad83bfa6
BP
3272 }
3273}
3274
3275static void
3276cmd_list(struct vsctl_context *ctx)
3277{
9591fefe 3278 const char *column_names = shash_find_data(&ctx->options, "--columns");
e99d41f7 3279 bool must_exist = !shash_find(&ctx->options, "--if-exists");
9591fefe 3280 const struct ovsdb_idl_column **columns;
ad83bfa6
BP
3281 const char *table_name = ctx->argv[1];
3282 const struct vsctl_table_class *table;
e051b42c 3283 struct table *out;
9591fefe 3284 size_t n_columns;
ad83bfa6
BP
3285 int i;
3286
3287 table = get_table(table_name);
9591fefe 3288 parse_column_names(column_names, table, &columns, &n_columns);
e051b42c 3289 out = ctx->table = list_make_table(columns, n_columns);
ad83bfa6
BP
3290 if (ctx->argc > 2) {
3291 for (i = 2; i < ctx->argc; i++) {
e99d41f7 3292 list_record(get_row(ctx, table, ctx->argv[i], must_exist),
9591fefe 3293 columns, n_columns, out);
ad83bfa6
BP
3294 }
3295 } else {
3296 const struct ovsdb_idl_row *row;
ad83bfa6 3297
16f4f61d
EJ
3298 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3299 row = ovsdb_idl_next_row(row)) {
9591fefe 3300 list_record(row, columns, n_columns, out);
ad83bfa6
BP
3301 }
3302 }
9591fefe 3303 free(columns);
ad83bfa6
BP
3304}
3305
0a140468
BP
3306static void
3307pre_cmd_find(struct vsctl_context *ctx)
3308{
3309 const char *column_names = shash_find_data(&ctx->options, "--columns");
3310 const char *table_name = ctx->argv[1];
3311 const struct vsctl_table_class *table;
3312 int i;
3313
3314 table = pre_get_table(ctx, table_name);
3315 pre_list_columns(ctx, table, column_names);
3316 for (i = 2; i < ctx->argc; i++) {
3317 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3318 }
3319}
3320
3321static void
3322cmd_find(struct vsctl_context *ctx)
3323{
3324 const char *column_names = shash_find_data(&ctx->options, "--columns");
3325 const struct ovsdb_idl_column **columns;
3326 const char *table_name = ctx->argv[1];
3327 const struct vsctl_table_class *table;
3328 const struct ovsdb_idl_row *row;
e051b42c 3329 struct table *out;
0a140468
BP
3330 size_t n_columns;
3331
3332 table = get_table(table_name);
3333 parse_column_names(column_names, table, &columns, &n_columns);
e051b42c 3334 out = ctx->table = list_make_table(columns, n_columns);
0a140468
BP
3335 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3336 row = ovsdb_idl_next_row(row)) {
3337 int i;
3338
3339 for (i = 2; i < ctx->argc; i++) {
3340 if (!is_condition_satisfied(table, row, ctx->argv[i],
3341 ctx->symtab)) {
3342 goto next_row;
3343 }
3344 }
0a140468
BP
3345 list_record(row, columns, n_columns, out);
3346
3347 next_row: ;
3348 }
cea0c393 3349 free(columns);
0a140468
BP
3350}
3351
e5e12280
BP
3352static void
3353pre_cmd_set(struct vsctl_context *ctx)
3354{
3355 const char *table_name = ctx->argv[1];
3356 const struct vsctl_table_class *table;
3357 int i;
3358
3359 table = pre_get_table(ctx, table_name);
3360 for (i = 3; i < ctx->argc; i++) {
ff495b63 3361 pre_parse_column_key_value(ctx, ctx->argv[i], table);
e5e12280
BP
3362 }
3363}
3364
ad83bfa6 3365static void
557e3718 3366set_column(const struct vsctl_table_class *table,
ce5a3e38
BP
3367 const struct ovsdb_idl_row *row, const char *arg,
3368 struct ovsdb_symbol_table *symtab)
ad83bfa6 3369{
bd76d25d 3370 const struct ovsdb_idl_column *column;
557e3718
BP
3371 char *key_string, *value_string;
3372 char *error;
ad83bfa6 3373
557e3718 3374 error = parse_column_key_value(arg, table, &column, &key_string,
e89e5374 3375 NULL, NULL, 0, &value_string);
557e3718 3376 die_if_error(error);
557e3718 3377 if (!value_string) {
def90f62 3378 vsctl_fatal("%s: missing value", arg);
557e3718 3379 }
ff495b63 3380 check_mutable(row, column);
ad83bfa6 3381
557e3718
BP
3382 if (key_string) {
3383 union ovsdb_atom key, value;
8c3c2f30 3384 struct ovsdb_datum datum;
557e3718 3385
bd76d25d 3386 if (column->type.value.type == OVSDB_TYPE_VOID) {
def90f62 3387 vsctl_fatal("cannot specify key to set for non-map column %s",
bd76d25d 3388 column->name);
ad83bfa6
BP
3389 }
3390
bd76d25d 3391 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
ce5a3e38 3392 key_string, symtab));
bd76d25d 3393 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
ce5a3e38 3394 value_string, symtab));
ad83bfa6 3395
8c3c2f30
BP
3396 ovsdb_datum_init_empty(&datum);
3397 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
ad83bfa6 3398
bd76d25d
BP
3399 ovsdb_atom_destroy(&key, column->type.key.type);
3400 ovsdb_atom_destroy(&value, column->type.value.type);
a3326252 3401
8c3c2f30
BP
3402 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3403 &column->type, false);
66dbf925 3404 ovsdb_idl_txn_verify(row, column);
8c3c2f30 3405 ovsdb_idl_txn_write(row, column, &datum);
557e3718
BP
3406 } else {
3407 struct ovsdb_datum datum;
ad83bfa6 3408
bd76d25d 3409 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
ce5a3e38 3410 value_string, symtab));
bd76d25d 3411 ovsdb_idl_txn_write(row, column, &datum);
557e3718 3412 }
ad83bfa6 3413
557e3718 3414 free(key_string);
a3326252 3415 free(value_string);
557e3718 3416}
ad83bfa6 3417
557e3718
BP
3418static void
3419cmd_set(struct vsctl_context *ctx)
3420{
e99d41f7 3421 bool must_exist = !shash_find(&ctx->options, "--if-exists");
557e3718
BP
3422 const char *table_name = ctx->argv[1];
3423 const char *record_id = ctx->argv[2];
3424 const struct vsctl_table_class *table;
3425 const struct ovsdb_idl_row *row;
3426 int i;
ad83bfa6 3427
557e3718 3428 table = get_table(table_name);
e99d41f7
BP
3429 row = get_row(ctx, table, record_id, must_exist);
3430 if (!row) {
3431 return;
3432 }
3433
557e3718 3434 for (i = 3; i < ctx->argc; i++) {
ce5a3e38 3435 set_column(table, row, ctx->argv[i], ctx->symtab);
ad83bfa6 3436 }
5ce5a6b5
BP
3437
3438 vsctl_context_invalidate_cache(ctx);
ad83bfa6
BP
3439}
3440
e5e12280
BP
3441static void
3442pre_cmd_add(struct vsctl_context *ctx)
3443{
3444 const char *table_name = ctx->argv[1];
3445 const char *column_name = ctx->argv[3];
3446 const struct vsctl_table_class *table;
3447 const struct ovsdb_idl_column *column;
3448
3449 table = pre_get_table(ctx, table_name);
3450 pre_get_column(ctx, table, column_name, &column);
3451}
3452
ad83bfa6
BP
3453static void
3454cmd_add(struct vsctl_context *ctx)
3455{
e99d41f7 3456 bool must_exist = !shash_find(&ctx->options, "--if-exists");
ad83bfa6
BP
3457 const char *table_name = ctx->argv[1];
3458 const char *record_id = ctx->argv[2];
3459 const char *column_name = ctx->argv[3];
3460 const struct vsctl_table_class *table;
bd76d25d 3461 const struct ovsdb_idl_column *column;
ad83bfa6
BP
3462 const struct ovsdb_idl_row *row;
3463 const struct ovsdb_type *type;
3464 struct ovsdb_datum old;
3465 int i;
3466
3467 table = get_table(table_name);
1bc6ff29 3468 die_if_error(get_column(table, column_name, &column));
e99d41f7
BP
3469 row = get_row(ctx, table, record_id, must_exist);
3470 if (!row) {
3471 return;
3472 }
ff495b63 3473 check_mutable(row, column);
c29a8ba8 3474
bd76d25d 3475 type = &column->type;
8c3c2f30 3476 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
ad83bfa6
BP
3477 for (i = 4; i < ctx->argc; i++) {
3478 struct ovsdb_type add_type;
3479 struct ovsdb_datum add;
3480
ad83bfa6
BP
3481 add_type = *type;
3482 add_type.n_min = 1;
3483 add_type.n_max = UINT_MAX;
ce5a3e38
BP
3484 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3485 ctx->symtab));
ad83bfa6
BP
3486 ovsdb_datum_union(&old, &add, type, false);
3487 ovsdb_datum_destroy(&add, type);
3488 }
3489 if (old.n > type->n_max) {
def90f62
BP
3490 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3491 "table %s but the maximum number is %u",
3492 old.n,
bd76d25d
BP
3493 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3494 column->name, table->class->name, type->n_max);
ad83bfa6 3495 }
f74055e7 3496 ovsdb_idl_txn_verify(row, column);
bd76d25d 3497 ovsdb_idl_txn_write(row, column, &old);
5ce5a6b5
BP
3498
3499 vsctl_context_invalidate_cache(ctx);
ad83bfa6 3500}
90c4bd00 3501
e5e12280
BP
3502static void
3503pre_cmd_remove(struct vsctl_context *ctx)
3504{
3505 const char *table_name = ctx->argv[1];
3506 const char *column_name = ctx->argv[3];
3507 const struct vsctl_table_class *table;
3508 const struct ovsdb_idl_column *column;
3509
3510 table = pre_get_table(ctx, table_name);
3511 pre_get_column(ctx, table, column_name, &column);
3512}
3513
90c4bd00
BP
3514static void
3515cmd_remove(struct vsctl_context *ctx)
3516{
e99d41f7 3517 bool must_exist = !shash_find(&ctx->options, "--if-exists");
90c4bd00
BP
3518 const char *table_name = ctx->argv[1];
3519 const char *record_id = ctx->argv[2];
3520 const char *column_name = ctx->argv[3];
3521 const struct vsctl_table_class *table;
bd76d25d 3522 const struct ovsdb_idl_column *column;
90c4bd00
BP
3523 const struct ovsdb_idl_row *row;
3524 const struct ovsdb_type *type;
3525 struct ovsdb_datum old;
3526 int i;
3527
3528 table = get_table(table_name);
90c4bd00 3529 die_if_error(get_column(table, column_name, &column));
e99d41f7
BP
3530 row = get_row(ctx, table, record_id, must_exist);
3531 if (!row) {
3532 return;
3533 }
ff495b63 3534 check_mutable(row, column);
c29a8ba8 3535
bd76d25d 3536 type = &column->type;
8c3c2f30 3537 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
90c4bd00
BP
3538 for (i = 4; i < ctx->argc; i++) {
3539 struct ovsdb_type rm_type;
3540 struct ovsdb_datum rm;
3541 char *error;
3542
90c4bd00
BP
3543 rm_type = *type;
3544 rm_type.n_min = 1;
3545 rm_type.n_max = UINT_MAX;
ce5a3e38
BP
3546 error = ovsdb_datum_from_string(&rm, &rm_type,
3547 ctx->argv[i], ctx->symtab);
d6d2a5b3
BP
3548 if (error) {
3549 if (ovsdb_type_is_map(&rm_type)) {
3550 rm_type.value.type = OVSDB_TYPE_VOID;
3551 free(error);
3552 die_if_error(ovsdb_datum_from_string(
3553 &rm, &rm_type, ctx->argv[i], ctx->symtab));
3554 } else {
3555 vsctl_fatal("%s", error);
3556 }
90c4bd00
BP
3557 }
3558 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3559 ovsdb_datum_destroy(&rm, &rm_type);
3560 }
3561 if (old.n < type->n_min) {
def90f62 3562 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
f19f25a4 3563 "table %s but the minimum number is %u",
def90f62 3564 old.n,
bd76d25d
BP
3565 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3566 column->name, table->class->name, type->n_min);
90c4bd00 3567 }
f74055e7 3568 ovsdb_idl_txn_verify(row, column);
bd76d25d 3569 ovsdb_idl_txn_write(row, column, &old);
5ce5a6b5
BP
3570
3571 vsctl_context_invalidate_cache(ctx);
90c4bd00
BP
3572}
3573
e5e12280
BP
3574static void
3575pre_cmd_clear(struct vsctl_context *ctx)
3576{
3577 const char *table_name = ctx->argv[1];
3578 const struct vsctl_table_class *table;
3579 int i;
3580
3581 table = pre_get_table(ctx, table_name);
3582 for (i = 3; i < ctx->argc; i++) {
3583 const struct ovsdb_idl_column *column;
3584
3585 pre_get_column(ctx, table, ctx->argv[i], &column);
3586 }
3587}
3588
90c4bd00
BP
3589static void
3590cmd_clear(struct vsctl_context *ctx)
3591{
e99d41f7 3592 bool must_exist = !shash_find(&ctx->options, "--if-exists");
90c4bd00
BP
3593 const char *table_name = ctx->argv[1];
3594 const char *record_id = ctx->argv[2];
3595 const struct vsctl_table_class *table;
3596 const struct ovsdb_idl_row *row;
3597 int i;
3598
3599 table = get_table(table_name);
e99d41f7
BP
3600 row = get_row(ctx, table, record_id, must_exist);
3601 if (!row) {
3602 return;
3603 }
3604
90c4bd00 3605 for (i = 3; i < ctx->argc; i++) {
bd76d25d 3606 const struct ovsdb_idl_column *column;
90c4bd00
BP
3607 const struct ovsdb_type *type;
3608 struct ovsdb_datum datum;
3609
3610 die_if_error(get_column(table, ctx->argv[i], &column));
ff495b63 3611 check_mutable(row, column);
90c4bd00 3612
bd76d25d
BP
3613 type = &column->type;
3614 if (type->n_min > 0) {
def90f62
BP
3615 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3616 "of table %s, which is not allowed to be empty",
bd76d25d 3617 column->name, table->class->name);
90c4bd00
BP
3618 }
3619
3620 ovsdb_datum_init_empty(&datum);
bd76d25d 3621 ovsdb_idl_txn_write(row, column, &datum);
90c4bd00 3622 }
5ce5a6b5
BP
3623
3624 vsctl_context_invalidate_cache(ctx);
90c4bd00 3625}
557e3718
BP
3626
3627static void
c5f341ab 3628pre_create(struct vsctl_context *ctx)
557e3718 3629{
ce5a3e38 3630 const char *id = shash_find_data(&ctx->options, "--id");
557e3718
BP
3631 const char *table_name = ctx->argv[1];
3632 const struct vsctl_table_class *table;
c5f341ab
BP
3633
3634 table = get_table(table_name);
3635 if (!id && !table->class->is_root) {
3636 VLOG_WARN("applying \"create\" command to table %s without --id "
3637 "option will have no effect", table->class->name);
3638 }
3639}
3640
3641static void
3642cmd_create(struct vsctl_context *ctx)
3643{
3644 const char *id = shash_find_data(&ctx->options, "--id");
3645 const char *table_name = ctx->argv[1];
3646 const struct vsctl_table_class *table = get_table(table_name);
557e3718 3647 const struct ovsdb_idl_row *row;
ce5a3e38 3648 const struct uuid *uuid;
557e3718
BP
3649 int i;
3650
c5f341ab
BP
3651 if (id) {
3652 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3653 if (table->class->is_root) {
3654 /* This table is in the root set, meaning that rows created in it
3655 * won't disappear even if they are unreferenced, so disable
3656 * warnings about that by pretending that there is a reference. */
3657 symbol->strong_ref = true;
3658 }
3659 uuid = &symbol->uuid;
3660 } else {
3661 uuid = NULL;
3662 }
ce5a3e38 3663
ce5a3e38 3664 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
557e3718 3665 for (i = 2; i < ctx->argc; i++) {
ce5a3e38 3666 set_column(table, row, ctx->argv[i], ctx->symtab);
557e3718 3667 }
f8ff4bc4 3668 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
557e3718 3669}
b7f74b6f 3670
3ef917b5
BP
3671/* This function may be used as the 'postprocess' function for commands that
3672 * insert new rows into the database. It expects that the command's 'run'
3673 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3674 * sole output. It replaces that output by the row's permanent UUID assigned
3675 * by the database server and appends a new-line.
3676 *
3677 * Currently we use this only for "create", because the higher-level commands
3678 * are supposed to be independent of the actual structure of the vswitch
3679 * configuration. */
3680static void
3681post_create(struct vsctl_context *ctx)
3682{
3683 const struct uuid *real;
3684 struct uuid dummy;
3685
1611cf3f 3686 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
428b2edd 3687 OVS_NOT_REACHED();
1611cf3f 3688 }
3ef917b5
BP
3689 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3690 if (real) {
3691 ds_clear(&ctx->output);
3692 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3693 }
3694 ds_put_char(&ctx->output, '\n');
3695}
3696
c3ccfe98
AZ
3697static void
3698post_db_reload_check_init(void)
3699{
3700 n_neoteric_ifaces = 0;
3701}
3702
3703static void
3704post_db_reload_expect_iface(const struct ovsrec_interface *iface)
3705{
3706 if (n_neoteric_ifaces >= allocated_neoteric_ifaces) {
3707 neoteric_ifaces = x2nrealloc(neoteric_ifaces,
3708 &allocated_neoteric_ifaces,
3709 sizeof *neoteric_ifaces);
3710 }
3711 neoteric_ifaces[n_neoteric_ifaces++] = iface->header_.uuid;
3712}
3713
3714static void
3715post_db_reload_do_checks(const struct vsctl_context *ctx)
3716{
3717 struct ds dead_ifaces = DS_EMPTY_INITIALIZER;
3718 size_t i;
3719
3720 for (i = 0; i < n_neoteric_ifaces; i++) {
3721 const struct uuid *uuid;
3722
3723 uuid = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &neoteric_ifaces[i]);
3724 if (uuid) {
3725 const struct ovsrec_interface *iface;
3726
3727 iface = ovsrec_interface_get_for_uuid(ctx->idl, uuid);
3728 if (iface && (!iface->ofport || *iface->ofport == -1)) {
3729 ds_put_format(&dead_ifaces, "'%s', ", iface->name);
3730 }
3731 }
3732 }
3733
3734 if (dead_ifaces.length) {
3735 dead_ifaces.length -= 2; /* Strip off trailing comma and space. */
3736 ovs_error(0, "Error detected while setting up %s. See ovs-vswitchd "
3737 "log for details.", ds_cstr(&dead_ifaces));
3738 }
3739
3740 ds_destroy(&dead_ifaces);
3741}
3742
e5e12280
BP
3743static void
3744pre_cmd_destroy(struct vsctl_context *ctx)
3745{
3746 const char *table_name = ctx->argv[1];
3747
3748 pre_get_table(ctx, table_name);
3749}
3750
b7f74b6f
BP
3751static void
3752cmd_destroy(struct vsctl_context *ctx)
3753{
b7f74b6f 3754 bool must_exist = !shash_find(&ctx->options, "--if-exists");
eeb8467e 3755 bool delete_all = shash_find(&ctx->options, "--all");
b7f74b6f
BP
3756 const char *table_name = ctx->argv[1];
3757 const struct vsctl_table_class *table;
3758 int i;
3759
b7f74b6f 3760 table = get_table(table_name);
eeb8467e
AS
3761
3762 if (delete_all && ctx->argc > 2) {
3763 vsctl_fatal("--all and records argument should not be specified together");
3764 }
3765
3766 if (delete_all && !must_exist) {
3767 vsctl_fatal("--all and --if-exists should not be specified together");
3768 }
3769
3770 if (delete_all) {
b7f74b6f 3771 const struct ovsdb_idl_row *row;
eeb8467e 3772 const struct ovsdb_idl_row *next_row;
b7f74b6f 3773
eeb8467e
AS
3774 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3775 row;) {
3776 next_row = ovsdb_idl_next_row(row);
3777 ovsdb_idl_txn_delete(row);
3778 row = next_row;
b7f74b6f 3779 }
eeb8467e
AS
3780 } else {
3781 for (i = 2; i < ctx->argc; i++) {
3782 const struct ovsdb_idl_row *row;
5ce5a6b5 3783
e99d41f7 3784 row = get_row(ctx, table, ctx->argv[i], must_exist);
eeb8467e
AS
3785 if (row) {
3786 ovsdb_idl_txn_delete(row);
3787 }
3788 }
3789 }
5ce5a6b5 3790 vsctl_context_invalidate_cache(ctx);
b7f74b6f 3791}
7db03f7c 3792
2a9537e2
BP
3793#define RELOPS \
3794 RELOP(RELOP_EQ, "=") \
3795 RELOP(RELOP_NE, "!=") \
3796 RELOP(RELOP_LT, "<") \
3797 RELOP(RELOP_GT, ">") \
3798 RELOP(RELOP_LE, "<=") \
3799 RELOP(RELOP_GE, ">=") \
3800 RELOP(RELOP_SET_EQ, "{=}") \
3801 RELOP(RELOP_SET_NE, "{!=}") \
3802 RELOP(RELOP_SET_LT, "{<}") \
3803 RELOP(RELOP_SET_GT, "{>}") \
3804 RELOP(RELOP_SET_LE, "{<=}") \
3805 RELOP(RELOP_SET_GE, "{>=}")
3806
3807enum relop {
3808#define RELOP(ENUM, STRING) ENUM,
3809 RELOPS
3810#undef RELOP
3811};
3812
3813static bool
3814is_set_operator(enum relop op)
3815{
3816 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3817 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3818 op == RELOP_SET_LE || op == RELOP_SET_GE);
3819}
3820
3821static bool
3822evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3823 const struct ovsdb_type *type, enum relop op)
3824{
3825 switch (op) {
3826 case RELOP_EQ:
3827 case RELOP_SET_EQ:
3828 return ovsdb_datum_compare_3way(a, b, type) == 0;
3829 case RELOP_NE:
3830 case RELOP_SET_NE:
3831 return ovsdb_datum_compare_3way(a, b, type) != 0;
3832 case RELOP_LT:
3833 return ovsdb_datum_compare_3way(a, b, type) < 0;
3834 case RELOP_GT:
3835 return ovsdb_datum_compare_3way(a, b, type) > 0;
3836 case RELOP_LE:
3837 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3838 case RELOP_GE:
3839 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3840
3841 case RELOP_SET_LT:
3842 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3843 case RELOP_SET_GT:
3844 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3845 case RELOP_SET_LE:
3846 return ovsdb_datum_includes_all(a, b, type);
3847 case RELOP_SET_GE:
3848 return ovsdb_datum_includes_all(b, a, type);
3849
3850 default:
428b2edd 3851 OVS_NOT_REACHED();
2a9537e2
BP
3852 }
3853}
3854
7db03f7c 3855static bool
f158c54a
BP
3856is_condition_satisfied(const struct vsctl_table_class *table,
3857 const struct ovsdb_idl_row *row, const char *arg,
3858 struct ovsdb_symbol_table *symtab)
7db03f7c
BP
3859{
3860 static const char *operators[] = {
2a9537e2
BP
3861#define RELOP(ENUM, STRING) STRING,
3862 RELOPS
3863#undef RELOP
7db03f7c
BP
3864 };
3865
3866 const struct ovsdb_idl_column *column;
8c3c2f30 3867 const struct ovsdb_datum *have_datum;
7db03f7c 3868 char *key_string, *value_string;
2a9537e2
BP
3869 struct ovsdb_type type;
3870 int operator;
3871 bool retval;
7db03f7c 3872 char *error;
7db03f7c
BP
3873
3874 error = parse_column_key_value(arg, table, &column, &key_string,
3875 &operator, operators, ARRAY_SIZE(operators),
3876 &value_string);
3877 die_if_error(error);
3878 if (!value_string) {
3879 vsctl_fatal("%s: missing value", arg);
3880 }
3881
2a9537e2
BP
3882 type = column->type;
3883 type.n_max = UINT_MAX;
3884
8c3c2f30 3885 have_datum = ovsdb_idl_read(row, column);
7db03f7c 3886 if (key_string) {
2a9537e2
BP
3887 union ovsdb_atom want_key;
3888 struct ovsdb_datum b;
3889 unsigned int idx;
7db03f7c
BP
3890
3891 if (column->type.value.type == OVSDB_TYPE_VOID) {
3892 vsctl_fatal("cannot specify key to check for non-map column %s",
3893 column->name);
3894 }
3895
3896 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3897 key_string, symtab));
2a9537e2
BP
3898
3899 type.key = type.value;
3900 type.value.type = OVSDB_TYPE_VOID;
3901 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
7db03f7c 3902
8c3c2f30 3903 idx = ovsdb_datum_find_key(have_datum,
7db03f7c 3904 &want_key, column->type.key.type);
2a9537e2
BP
3905 if (idx == UINT_MAX && !is_set_operator(operator)) {
3906 retval = false;
3907 } else {
3908 struct ovsdb_datum a;
3909
3910 if (idx != UINT_MAX) {
3911 a.n = 1;
3912 a.keys = &have_datum->values[idx];
3913 a.values = NULL;
3914 } else {
3915 a.n = 0;
3916 a.keys = NULL;
3917 a.values = NULL;
3918 }
3919
3920 retval = evaluate_relop(&a, &b, &type, operator);
7db03f7c
BP
3921 }
3922
3923 ovsdb_atom_destroy(&want_key, column->type.key.type);
e49190c4 3924 ovsdb_datum_destroy(&b, &type);
7db03f7c
BP
3925 } else {
3926 struct ovsdb_datum want_datum;
3927
3928 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3929 value_string, symtab));
2a9537e2 3930 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
7db03f7c
BP
3931 ovsdb_datum_destroy(&want_datum, &column->type);
3932 }
7db03f7c
BP
3933
3934 free(key_string);
3935 free(value_string);
3936
2a9537e2 3937 return retval;
7db03f7c
BP
3938}
3939
e5e12280
BP
3940static void
3941pre_cmd_wait_until(struct vsctl_context *ctx)
3942{
3943 const char *table_name = ctx->argv[1];
3944 const struct vsctl_table_class *table;
3945 int i;
3946
3947 table = pre_get_table(ctx, table_name);
3948
3949 for (i = 3; i < ctx->argc; i++) {
3950 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3951 }
3952}
3953
7db03f7c
BP
3954static void
3955cmd_wait_until(struct vsctl_context *ctx)
3956{
3957 const char *table_name = ctx->argv[1];
3958 const char *record_id = ctx->argv[2];
3959 const struct vsctl_table_class *table;
3960 const struct ovsdb_idl_row *row;
3961 int i;
3962
3963 table = get_table(table_name);
3964
e99d41f7 3965 row = get_row(ctx, table, record_id, false);
7db03f7c
BP
3966 if (!row) {
3967 ctx->try_again = true;
3968 return;
3969 }
3970
3971 for (i = 3; i < ctx->argc; i++) {
f158c54a 3972 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
7db03f7c
BP
3973 ctx->try_again = true;
3974 return;
3975 }
3976 }
3977}
ad83bfa6 3978\f
5ce5a6b5
BP
3979/* Prepares 'ctx', which has already been initialized with
3980 * vsctl_context_init(), for processing 'command'. */
c75d1511 3981static void
5ce5a6b5
BP
3982vsctl_context_init_command(struct vsctl_context *ctx,
3983 struct vsctl_command *command)
f8ff4bc4
BP
3984{
3985 ctx->argc = command->argc;
3986 ctx->argv = command->argv;
3987 ctx->options = command->options;
3988
3989 ds_swap(&ctx->output, &command->output);
e051b42c 3990 ctx->table = command->table;
5ce5a6b5
BP
3991
3992 ctx->verified_ports = false;
3993
3994 ctx->try_again = false;
3995}
3996
3997/* Prepares 'ctx' for processing commands, initializing its members with the
3998 * values passed in as arguments.
3999 *
4000 * If 'command' is nonnull, calls vsctl_context_init_command() to prepare for
4001 * that particular command. */
4002static void
4003vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
4004 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
4005 const struct ovsrec_open_vswitch *ovs,
4006 struct ovsdb_symbol_table *symtab)
4007{
4008 if (command) {
4009 vsctl_context_init_command(ctx, command);
4010 }
f8ff4bc4
BP
4011 ctx->idl = idl;
4012 ctx->txn = txn;
4013 ctx->ovs = ovs;
ce5a3e38 4014 ctx->symtab = symtab;
5ce5a6b5 4015 ctx->cache_valid = false;
f8ff4bc4
BP
4016}
4017
5ce5a6b5 4018/* Completes processing of 'command' within 'ctx'. */
f8ff4bc4 4019static void
5ce5a6b5
BP
4020vsctl_context_done_command(struct vsctl_context *ctx,
4021 struct vsctl_command *command)
f8ff4bc4
BP
4022{
4023 ds_swap(&ctx->output, &command->output);
e051b42c 4024 command->table = ctx->table;
f8ff4bc4
BP
4025}
4026
5ce5a6b5
BP
4027/* Finishes up with 'ctx'.
4028 *
4029 * If command is nonnull, first calls vsctl_context_done_command() to complete
4030 * processing that command within 'ctx'. */
4031static void
4032vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
4033{
4034 if (command) {
4035 vsctl_context_done_command(ctx, command);
4036 }
4037 vsctl_context_invalidate_cache(ctx);
4038}
4039
e5e12280
BP
4040static void
4041run_prerequisites(struct vsctl_command *commands, size_t n_commands,
4042 struct ovsdb_idl *idl)
4043{
4044 struct vsctl_command *c;
4045
4046 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
b8fa7102
BP
4047 if (wait_for_reload) {
4048 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
4049 }
e5e12280
BP
4050 for (c = commands; c < &commands[n_commands]; c++) {
4051 if (c->syntax->prerequisites) {
4052 struct vsctl_context ctx;
4053
4054 ds_init(&c->output);
e051b42c 4055 c->table = NULL;
e5e12280
BP
4056
4057 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
4058 (c->syntax->prerequisites)(&ctx);
4059 vsctl_context_done(&ctx, c);
4060
cb22974d
BP
4061 ovs_assert(!c->output.string);
4062 ovs_assert(!c->table);
e5e12280
BP
4063 }
4064 }
4065}
4066
854a94d9 4067static void
f8ff4bc4
BP
4068do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
4069 struct ovsdb_idl *idl)
c75d1511
BP
4070{
4071 struct ovsdb_idl_txn *txn;
4072 const struct ovsrec_open_vswitch *ovs;
4073 enum ovsdb_idl_txn_status status;
ce5a3e38 4074 struct ovsdb_symbol_table *symtab;
5ce5a6b5 4075 struct vsctl_context ctx;
f8ff4bc4 4076 struct vsctl_command *c;
0dc66db9 4077 struct shash_node *node;
84a0ee89 4078 int64_t next_cfg = 0;
af9af3e2 4079 char *error = NULL;
c75d1511 4080
1d48b4be 4081 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
577aebdf
BP
4082 if (dry_run) {
4083 ovsdb_idl_txn_set_dry_run(txn);
4084 }
524555d1 4085
e1c0e2d1 4086 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
d171b584 4087
c75d1511
BP
4088 ovs = ovsrec_open_vswitch_first(idl);
4089 if (!ovs) {
524555d1
BP
4090 /* XXX add verification that table is empty */
4091 ovs = ovsrec_open_vswitch_insert(txn);
c75d1511
BP
4092 }
4093
b54e22e9 4094 if (wait_for_reload) {
94fbe1aa
BP
4095 ovsdb_idl_txn_increment(txn, &ovs->header_,
4096 &ovsrec_open_vswitch_col_next_cfg);
b54e22e9
BP
4097 }
4098
c3ccfe98 4099 post_db_reload_check_init();
ce5a3e38 4100 symtab = ovsdb_symbol_table_create();
87b23a01
BP
4101 for (c = commands; c < &commands[n_commands]; c++) {
4102 ds_init(&c->output);
e051b42c 4103 c->table = NULL;
87b23a01 4104 }
5ce5a6b5 4105 vsctl_context_init(&ctx, NULL, idl, txn, ovs, symtab);
f8ff4bc4 4106 for (c = commands; c < &commands[n_commands]; c++) {
5ce5a6b5 4107 vsctl_context_init_command(&ctx, c);
ffd66ea9
BP
4108 if (c->syntax->run) {
4109 (c->syntax->run)(&ctx);
4110 }
5ce5a6b5 4111 vsctl_context_done_command(&ctx, c);
87b23a01
BP
4112
4113 if (ctx.try_again) {
5ce5a6b5 4114 vsctl_context_done(&ctx, NULL);
87b23a01
BP
4115 goto try_again;
4116 }
c75d1511 4117 }
5ce5a6b5 4118 vsctl_context_done(&ctx, NULL);
c75d1511 4119
0dc66db9
BP
4120 SHASH_FOR_EACH (node, &symtab->sh) {
4121 struct ovsdb_symbol *symbol = node->data;
4122 if (!symbol->created) {
4123 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
4124 "with \"-- --id=%s create ...\")",
4125 node->name, node->name);
4126 }
c5f341ab
BP
4127 if (!symbol->strong_ref) {
4128 if (!symbol->weak_ref) {
4129 VLOG_WARN("row id \"%s\" was created but no reference to it "
4130 "was inserted, so it will not actually appear in "
4131 "the database", node->name);
4132 } else {
4133 VLOG_WARN("row id \"%s\" was created but only a weak "
4134 "reference to it was inserted, so it will not "
4135 "actually appear in the database", node->name);
4136 }
4137 }
28a3b753
BP
4138 }
4139
af96ccd2 4140 status = ovsdb_idl_txn_commit_block(txn);
b54e22e9
BP
4141 if (wait_for_reload && status == TXN_SUCCESS) {
4142 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
4143 }
8d49c47a
BP
4144 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
4145 for (c = commands; c < &commands[n_commands]; c++) {
4146 if (c->syntax->postprocess) {
4147 struct vsctl_context ctx;
4148
ce5a3e38 4149 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
8d49c47a
BP
4150 (c->syntax->postprocess)(&ctx);
4151 vsctl_context_done(&ctx, c);
4152 }
3da1c516
BP
4153 }
4154 }
91e310a5 4155 error = xstrdup(ovsdb_idl_txn_get_error(txn));
c75d1511
BP
4156
4157 switch (status) {
2096903b 4158 case TXN_UNCOMMITTED:
c75d1511 4159 case TXN_INCOMPLETE:
428b2edd 4160 OVS_NOT_REACHED();
c75d1511
BP
4161
4162 case TXN_ABORTED:
4163 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
c88b6a27 4164 vsctl_fatal("transaction aborted");
c75d1511 4165
b54e22e9 4166 case TXN_UNCHANGED:
c75d1511
BP
4167 case TXN_SUCCESS:
4168 break;
4169
854a94d9 4170 case TXN_TRY_AGAIN:
87b23a01 4171 goto try_again;
c75d1511
BP
4172
4173 case TXN_ERROR:
91e310a5 4174 vsctl_fatal("transaction error: %s", error);
c75d1511 4175
06b6d651
BP
4176 case TXN_NOT_LOCKED:
4177 /* Should not happen--we never call ovsdb_idl_set_lock(). */
4178 vsctl_fatal("database not locked");
4179
c75d1511 4180 default:
428b2edd 4181 OVS_NOT_REACHED();
c75d1511 4182 }
91e310a5 4183 free(error);
c75d1511 4184
87b23a01
BP
4185 ovsdb_symbol_table_destroy(symtab);
4186
f8ff4bc4
BP
4187 for (c = commands; c < &commands[n_commands]; c++) {
4188 struct ds *ds = &c->output;
ce5a3e38 4189
e051b42c
BP
4190 if (c->table) {
4191 table_print(c->table, &table_style);
4192 } else if (oneline) {
c75d1511
BP
4193 size_t j;
4194
4195 ds_chomp(ds, '\n');
4196 for (j = 0; j < ds->length; j++) {
2a022368
BP
4197 int ch = ds->string[j];
4198 switch (ch) {
c75d1511
BP
4199 case '\n':
4200 fputs("\\n", stdout);
4201 break;
4202
4203 case '\\':
4204 fputs("\\\\", stdout);
4205 break;
4206
4207 default:
2a022368 4208 putchar(ch);
c75d1511
BP
4209 }
4210 }
4211 putchar('\n');
4212 } else {
4213 fputs(ds_cstr(ds), stdout);
4214 }
b86b43aa 4215 ds_destroy(&c->output);
e051b42c
BP
4216 table_destroy(c->table);
4217 free(c->table);
ce5a3e38 4218
79f1cbe9 4219 shash_destroy_free_data(&c->options);
c75d1511 4220 }
b86b43aa 4221 free(commands);
b54e22e9
BP
4222
4223 if (wait_for_reload && status != TXN_UNCHANGED) {
705d7a39
AA
4224 /* Even, if --retry flag was not specified, ovs-vsctl still
4225 * has to retry to establish OVSDB connection, if wait_for_reload
4226 * was set. Otherwise, ovs-vsctl would end up waiting forever
4227 * until cur_cfg would be updated. */
4228 ovsdb_idl_enable_reconnect(idl);
b54e22e9 4229 for (;;) {
b54e22e9
BP
4230 ovsdb_idl_run(idl);
4231 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
4232 if (ovs->cur_cfg >= next_cfg) {
c3ccfe98 4233 post_db_reload_do_checks(&ctx);
b54e22e9
BP
4234 goto done;
4235 }
4236 }
4237 ovsdb_idl_wait(idl);
4238 poll_block();
4239 }
4240 done: ;
4241 }
c3ccfe98 4242 ovsdb_idl_txn_destroy(txn);
b86b43aa 4243 ovsdb_idl_destroy(idl);
b54e22e9 4244
c75d1511 4245 exit(EXIT_SUCCESS);
87b23a01
BP
4246
4247try_again:
4248 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
4249 * resources and return so that the caller can try again. */
b7b6e2c4
JP
4250 if (txn) {
4251 ovsdb_idl_txn_abort(txn);
4252 ovsdb_idl_txn_destroy(txn);
85f60635 4253 the_idl_txn = NULL;
b7b6e2c4 4254 }
87b23a01
BP
4255 ovsdb_symbol_table_destroy(symtab);
4256 for (c = commands; c < &commands[n_commands]; c++) {
4257 ds_destroy(&c->output);
e051b42c
BP
4258 table_destroy(c->table);
4259 free(c->table);
87b23a01
BP
4260 }
4261 free(error);
c75d1511
BP
4262}
4263
f8ff4bc4
BP
4264static const struct vsctl_command_syntax all_commands[] = {
4265 /* Open vSwitch commands. */
e5e12280 4266 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
9b1735a7 4267 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
f8ff4bc4
BP
4268
4269 /* Bridge commands. */
e5e12280
BP
4270 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
4271 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
515d830a 4272 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "--real,--fake", RO},
e5e12280
BP
4273 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
4274 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
4275 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
fbd8715e
BP
4276 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
4277 cmd_br_set_external_id, NULL, "", RW},
e5e12280
BP
4278 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
4279 cmd_br_get_external_id, NULL, "", RO},
f8ff4bc4
BP
4280
4281 /* Port commands. */
e5e12280
BP
4282 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
4283 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
4284 RW},
4285 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
4286 "--may-exist,--fake-iface", RW},
4287 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
4288 "--if-exists,--with-iface", RW},
4289 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
f8ff4bc4
BP
4290
4291 /* Interface commands. */
e5e12280
BP
4292 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
4293 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
f8ff4bc4
BP
4294
4295 /* Controller commands. */
4e3e7ff9
BP
4296 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
4297 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
4298 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
4299 "", RW},
e5e12280
BP
4300 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
4301 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
4302 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
f8ff4bc4 4303
24b8b259
AE
4304 /* Manager commands. */
4305 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
0d2f19a9 4306 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
24b8b259
AE
4307 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
4308
f8ff4bc4 4309 /* SSL commands. */
e5e12280
BP
4310 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
4311 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
4312 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
f8ff4bc4 4313
18ee958b 4314 /* Switch commands. */
e5e12280 4315 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
18ee958b 4316
ffd66ea9
BP
4317 /* Database commands. */
4318 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
e5e12280 4319 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
e99d41f7
BP
4320 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL,
4321 "--if-exists,--columns=", RO},
0a140468 4322 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
e99d41f7
BP
4323 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "--if-exists", RW},
4324 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "--if-exists", RW},
4325 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "--if-exists",
4326 RW},
4327 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "--if-exists", RW},
c5f341ab 4328 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
eeb8467e
AS
4329 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
4330 "--if-exists,--all", RW},
e5e12280
BP
4331 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
4332 RO},
4333
4334 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},
f8ff4bc4 4335};
5d9cb63c 4336
3815d6c2
LS
4337static const struct vsctl_command_syntax *get_all_commands(void)
4338{
4339 return all_commands;
4340}