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