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