]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-vsctl.c
ovs-dpctl: Use datapath enumeration functions instead of guessing names.
[mirror_ovs.git] / utilities / ovs-vsctl.c
CommitLineData
c75d1511 1/*
ad83bfa6 2 * Copyright (c) 2009, 2010 Nicira Networks.
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
19#include <assert.h>
ad83bfa6 20#include <ctype.h>
c75d1511 21#include <errno.h>
ad83bfa6 22#include <float.h>
c75d1511
BP
23#include <getopt.h>
24#include <inttypes.h>
25#include <signal.h>
26#include <stdarg.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "command-line.h"
31#include "compiler.h"
32#include "dirs.h"
33#include "dynamic-string.h"
b54e22e9 34#include "json.h"
ad83bfa6 35#include "ovsdb-data.h"
c75d1511
BP
36#include "ovsdb-idl.h"
37#include "poll-loop.h"
f8ff4bc4 38#include "process.h"
218a6f59 39#include "stream-ssl.h"
dfbe07ba 40#include "svec.h"
c75d1511
BP
41#include "vswitchd/vswitch-idl.h"
42#include "timeval.h"
43#include "util.h"
c75d1511 44#include "vlog.h"
5136ce49 45
d98e6007 46VLOG_DEFINE_THIS_MODULE(vsctl);
c75d1511 47
def90f62
BP
48/* vsctl_fatal() also logs the error, so it is preferred in this file. */
49#define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
50
f8ff4bc4
BP
51struct vsctl_context;
52
e5e12280 53/* A command supported by ovs-vsctl. */
f8ff4bc4 54struct vsctl_command_syntax {
e5e12280
BP
55 const char *name; /* e.g. "add-br" */
56 int min_args; /* Min number of arguments following name. */
57 int max_args; /* Max number of arguments following name. */
58
59 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
60 * each column or table in ctx->idl that it uses. */
61 void (*prerequisites)(struct vsctl_context *ctx);
62
63 /* Does the actual work of the command and puts the command's output, if
64 * any, in ctx->output.
65 *
66 * Alternatively, if some prerequisite of the command is not met and the
67 * caller should wait for something to change and then retry, it may set
68 * ctx->try_again to true. (Only the "wait-until" command currently does
69 * this.) */
70 void (*run)(struct vsctl_context *ctx);
71
72 /* If nonnull, called after the transaction has been successfully
73 * committed. ctx->output is the output from the "run" function, which
74 * this function may modify and otherwise postprocess as needed. (Only the
75 * "create" command currently does any postprocessing.) */
76 void (*postprocess)(struct vsctl_context *ctx);
77
78 /* A comma-separated list of supported options, e.g. "--a,--b", or the
79 * empty string if the command does not support any options. */
f8ff4bc4 80 const char *options;
0c18b5a0 81 enum { RO, RW } mode; /* Does this command modify the database? */
f8ff4bc4
BP
82};
83
84struct vsctl_command {
85 /* Data that remains constant after initialization. */
86 const struct vsctl_command_syntax *syntax;
87 int argc;
88 char **argv;
89 struct shash options;
90
91 /* Data modified by commands. */
92 struct ds output;
93};
94
c75d1511
BP
95/* --db: The database server to contact. */
96static const char *db;
97
98/* --oneline: Write each command's output as a single line? */
99static bool oneline;
100
577aebdf
BP
101/* --dry-run: Do not commit any changes. */
102static bool dry_run;
103
b54e22e9
BP
104/* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
105static bool wait_for_reload = true;
106
a39a859a 107/* --timeout: Time to wait for a connection to 'db'. */
6b7b9d34 108static int timeout;
a39a859a 109
f8ff4bc4
BP
110/* All supported commands. */
111static const struct vsctl_command_syntax all_commands[];
112
1d48b4be
BP
113/* The IDL we're using and the current transaction, if any.
114 * This is for use by vsctl_exit() only, to allow it to clean up.
115 * Other code should use its context arguments. */
116static struct ovsdb_idl *the_idl;
117static struct ovsdb_idl_txn *the_idl_txn;
118
119static void vsctl_exit(int status) NO_RETURN;
c88b6a27 120static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
c75d1511
BP
121static char *default_db(void);
122static void usage(void) NO_RETURN;
123static void parse_options(int argc, char *argv[]);
0c18b5a0 124static bool might_write_to_db(char **argv);
c75d1511 125
f8ff4bc4
BP
126static struct vsctl_command *parse_commands(int argc, char *argv[],
127 size_t *n_commandsp);
128static void parse_command(int argc, char *argv[], struct vsctl_command *);
1998cd4d 129static const struct vsctl_command_syntax *find_command(const char *name);
e5e12280
BP
130static void run_prerequisites(struct vsctl_command[], size_t n_commands,
131 struct ovsdb_idl *);
f8ff4bc4
BP
132static void do_vsctl(const char *args,
133 struct vsctl_command *, size_t n_commands,
134 struct ovsdb_idl *);
c75d1511 135
18b239f5
BP
136static const struct vsctl_table_class *get_table(const char *table_name);
137static void set_column(const struct vsctl_table_class *,
ce5a3e38
BP
138 const struct ovsdb_idl_row *, const char *arg,
139 struct ovsdb_symbol_table *);
18b239f5 140
c75d1511
BP
141int
142main(int argc, char *argv[])
143{
480ce8ab 144 extern struct vlog_module VLM_reconnect;
c75d1511 145 struct ovsdb_idl *idl;
f8ff4bc4
BP
146 struct vsctl_command *commands;
147 size_t n_commands;
148 char *args;
c75d1511
BP
149
150 set_program_name(argv[0]);
151 signal(SIGPIPE, SIG_IGN);
480ce8ab
BP
152 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
153 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
bd76d25d 154 ovsrec_init();
f8ff4bc4
BP
155
156 /* Log our arguments. This is often valuable for debugging systems. */
157 args = process_escape_args(argv);
0c18b5a0 158 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
f8ff4bc4
BP
159
160 /* Parse command line. */
c75d1511 161 parse_options(argc, argv);
f8ff4bc4 162 commands = parse_commands(argc - optind, argv + optind, &n_commands);
c75d1511 163
a39a859a
JP
164 if (timeout) {
165 time_alarm(timeout);
166 }
167
e5e12280
BP
168 /* Initialize IDL. */
169 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
170 run_prerequisites(commands, n_commands, idl);
171
524555d1 172 /* Now execute the commands. */
c75d1511 173 for (;;) {
4ea21243 174 if (ovsdb_idl_run(idl)) {
f8ff4bc4 175 do_vsctl(args, commands, n_commands, idl);
c75d1511
BP
176 }
177
178 ovsdb_idl_wait(idl);
179 poll_block();
180 }
181}
182
183static void
184parse_options(int argc, char *argv[])
185{
186 enum {
187 OPT_DB = UCHAR_MAX + 1,
188 OPT_ONELINE,
0c3dd1e1 189 OPT_NO_SYSLOG,
577aebdf 190 OPT_NO_WAIT,
e26b5a06 191 OPT_DRY_RUN,
218a6f59 192 OPT_PEER_CA_CERT,
e26b5a06 193 VLOG_OPTION_ENUMS
c75d1511
BP
194 };
195 static struct option long_options[] = {
196 {"db", required_argument, 0, OPT_DB},
dfbe07ba 197 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
0c3dd1e1 198 {"no-wait", no_argument, 0, OPT_NO_WAIT},
577aebdf 199 {"dry-run", no_argument, 0, OPT_DRY_RUN},
c75d1511 200 {"oneline", no_argument, 0, OPT_ONELINE},
342045e1 201 {"timeout", required_argument, 0, 't'},
c75d1511
BP
202 {"help", no_argument, 0, 'h'},
203 {"version", no_argument, 0, 'V'},
e26b5a06 204 VLOG_LONG_OPTIONS,
218a6f59
BP
205#ifdef HAVE_OPENSSL
206 STREAM_SSL_LONG_OPTIONS
207 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
208#endif
c75d1511
BP
209 {0, 0, 0, 0},
210 };
a2a9d2d9 211 char *tmp, *short_options;
c75d1511 212
a2a9d2d9
BP
213 tmp = long_options_to_short_options(long_options);
214 short_options = xasprintf("+%s", tmp);
215 free(tmp);
342045e1 216
c75d1511
BP
217 for (;;) {
218 int c;
219
a2a9d2d9 220 c = getopt_long(argc, argv, short_options, long_options, NULL);
c75d1511
BP
221 if (c == -1) {
222 break;
223 }
224
225 switch (c) {
226 case OPT_DB:
227 db = optarg;
228 break;
229
230 case OPT_ONELINE:
231 oneline = true;
232 break;
233
dfbe07ba 234 case OPT_NO_SYSLOG:
480ce8ab 235 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
dfbe07ba
BP
236 break;
237
0c3dd1e1 238 case OPT_NO_WAIT:
b54e22e9 239 wait_for_reload = false;
0c3dd1e1
BP
240 break;
241
577aebdf
BP
242 case OPT_DRY_RUN:
243 dry_run = true;
244 break;
245
c75d1511
BP
246 case 'h':
247 usage();
248
249 case 'V':
250 OVS_PRINT_VERSION(0, 0);
251 exit(EXIT_SUCCESS);
252
342045e1
BP
253 case 't':
254 timeout = strtoul(optarg, NULL, 10);
a39a859a 255 if (timeout < 0) {
def90f62
BP
256 vsctl_fatal("value %s on -t or --timeout is invalid",
257 optarg);
342045e1
BP
258 }
259 break;
260
e26b5a06 261 VLOG_OPTION_HANDLERS
c75d1511 262
218a6f59
BP
263#ifdef HAVE_OPENSSL
264 STREAM_SSL_OPTION_HANDLERS
265
266 case OPT_PEER_CA_CERT:
267 stream_ssl_set_peer_ca_cert_file(optarg);
268 break;
269#endif
270
c75d1511
BP
271 case '?':
272 exit(EXIT_FAILURE);
273
274 default:
275 abort();
276 }
277 }
a2a9d2d9 278 free(short_options);
c75d1511
BP
279
280 if (!db) {
281 db = default_db();
282 }
283}
284
f8ff4bc4
BP
285static struct vsctl_command *
286parse_commands(int argc, char *argv[], size_t *n_commandsp)
287{
288 struct vsctl_command *commands;
289 size_t n_commands, allocated_commands;
290 int i, start;
291
292 commands = NULL;
293 n_commands = allocated_commands = 0;
294
295 for (start = i = 0; i <= argc; i++) {
296 if (i == argc || !strcmp(argv[i], "--")) {
297 if (i > start) {
298 if (n_commands >= allocated_commands) {
299 struct vsctl_command *c;
300
301 commands = x2nrealloc(commands, &allocated_commands,
302 sizeof *commands);
303 for (c = commands; c < &commands[n_commands]; c++) {
304 shash_moved(&c->options);
305 }
306 }
307 parse_command(i - start, &argv[start],
308 &commands[n_commands++]);
309 }
310 start = i + 1;
311 }
312 }
313 if (!n_commands) {
314 vsctl_fatal("missing command name (use --help for help)");
315 }
316 *n_commandsp = n_commands;
317 return commands;
318}
319
320static void
321parse_command(int argc, char *argv[], struct vsctl_command *command)
322{
323 const struct vsctl_command_syntax *p;
1998cd4d
BP
324 struct shash_node *node;
325 int n_arg;
f8ff4bc4
BP
326 int i;
327
328 shash_init(&command->options);
329 for (i = 0; i < argc; i++) {
4a033593
BP
330 const char *option = argv[i];
331 const char *equals;
332 char *key, *value;
333
334 if (option[0] != '-') {
f8ff4bc4
BP
335 break;
336 }
4a033593
BP
337
338 equals = strchr(option, '=');
339 if (equals) {
340 key = xmemdup0(option, equals - option);
341 value = xstrdup(equals + 1);
342 } else {
343 key = xstrdup(option);
344 value = NULL;
345 }
346
347 if (shash_find(&command->options, key)) {
f8ff4bc4
BP
348 vsctl_fatal("'%s' option specified multiple times", argv[i]);
349 }
4a033593 350 shash_add_nocopy(&command->options, key, value);
f8ff4bc4
BP
351 }
352 if (i == argc) {
353 vsctl_fatal("missing command name");
354 }
355
1998cd4d
BP
356 p = find_command(argv[i]);
357 if (!p) {
358 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
359 }
f8ff4bc4 360
1998cd4d
BP
361 SHASH_FOR_EACH (node, &command->options) {
362 const char *s = strstr(p->options, node->name);
363 int end = s ? s[strlen(node->name)] : EOF;
4a033593 364
1998cd4d
BP
365 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
366 vsctl_fatal("'%s' command has no '%s' option",
367 argv[i], node->name);
368 }
369 if ((end == '=') != (node->data != NULL)) {
370 if (end == '=') {
371 vsctl_fatal("missing argument to '%s' option on '%s' "
372 "command", node->name, argv[i]);
373 } else {
374 vsctl_fatal("'%s' option on '%s' does not accept an "
375 "argument", node->name, argv[i]);
f8ff4bc4 376 }
1998cd4d
BP
377 }
378 }
f8ff4bc4 379
1998cd4d
BP
380 n_arg = argc - i - 1;
381 if (n_arg < p->min_args) {
382 vsctl_fatal("'%s' command requires at least %d arguments",
383 p->name, p->min_args);
384 } else if (n_arg > p->max_args) {
385 int j;
386
387 for (j = i + 1; j < argc; j++) {
388 if (argv[j][0] == '-') {
389 vsctl_fatal("'%s' command takes at most %d arguments "
390 "(note that options must precede command "
391 "names and follow a \"--\" argument)",
f8ff4bc4 392 p->name, p->max_args);
f8ff4bc4
BP
393 }
394 }
1998cd4d
BP
395
396 vsctl_fatal("'%s' command takes at most %d arguments",
397 p->name, p->max_args);
398 }
399
400 command->syntax = p;
401 command->argc = n_arg + 1;
402 command->argv = &argv[i];
403}
404
405/* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
406 * null pointer if there is none. */
407static const struct vsctl_command_syntax *
408find_command(const char *name)
409{
410 static struct shash commands = SHASH_INITIALIZER(&commands);
411
412 if (shash_is_empty(&commands)) {
413 const struct vsctl_command_syntax *p;
414
415 for (p = all_commands; p->name; p++) {
416 shash_add_assert(&commands, p->name, p);
417 }
f8ff4bc4
BP
418 }
419
1998cd4d 420 return shash_find_data(&commands, name);
f8ff4bc4
BP
421}
422
423static void
424vsctl_fatal(const char *format, ...)
425{
426 char *message;
427 va_list args;
428
429 va_start(args, format);
430 message = xvasprintf(format, args);
431 va_end(args);
432
480ce8ab 433 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
f8ff4bc4 434 VLOG_ERR("%s", message);
def90f62 435 ovs_error(0, "%s", message);
1d48b4be
BP
436 vsctl_exit(EXIT_FAILURE);
437}
438
439/* Frees the current transaction and the underlying IDL and then calls
440 * exit(status).
441 *
442 * Freeing the transaction and the IDL is not strictly necessary, but it makes
443 * for a clean memory leak report from valgrind in the normal case. That makes
444 * it easier to notice real memory leaks. */
445static void
446vsctl_exit(int status)
447{
448 if (the_idl_txn) {
449 ovsdb_idl_txn_abort(the_idl_txn);
450 ovsdb_idl_txn_destroy(the_idl_txn);
451 }
452 ovsdb_idl_destroy(the_idl);
453 exit(status);
f8ff4bc4
BP
454}
455
c75d1511
BP
456static void
457usage(void)
458{
8f7501e8
BP
459 printf("\
460%s: ovs-vswitchd management utility\n\
461usage: %s [OPTIONS] COMMAND [ARG...]\n\
462\n\
463Bridge commands:\n\
464 add-br BRIDGE create a new bridge named BRIDGE\n\
465 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
466 del-br BRIDGE delete BRIDGE and all of its ports\n\
467 list-br print the names of all the bridges\n\
468 br-exists BRIDGE test whether BRIDGE exists\n\
469 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
470 br-to-parent BRIDGE print the parent of BRIDGE\n\
471 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
472 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
473 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
474 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
475\n\
476Port commands:\n\
477 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
478 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
479 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
480 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
481 port-to-br PORT print name of bridge that contains PORT\n\
8f7501e8
BP
482A bond is considered to be a single port.\n\
483\n\
484Interface commands (a bond consists of multiple interfaces):\n\
485 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
486 iface-to-br IFACE print name of bridge that contains IFACE\n\
8f7501e8
BP
487\n\
488Controller commands:\n\
1a048029
JP
489 get-controller BRIDGE print the controller for BRIDGE\n\
490 del-controller BRIDGE delete the controller for BRIDGE\n\
491 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
492 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
493 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
494 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
8f7501e8
BP
495\n\
496SSL commands:\n\
497 get-ssl print the SSL configuration\n\
498 del-ssl delete the SSL configuration\n\
499 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
500\n\
18ee958b
JP
501Switch commands:\n\
502 emer-reset reset switch to known good state\n\
503\n\
8f7501e8
BP
504Database commands:\n\
505 list TBL [REC] list RECord (or all records) in TBL\n\
4f1361e8 506 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
8f7501e8
BP
507 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
508 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
509 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
510 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
511 create TBL COL[:KEY]=VALUE create and initialize new record\n\
4f1361e8 512 destroy TBL REC delete RECord from TBL\n\
7db03f7c 513 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
8f7501e8
BP
514Potentially unsafe database commands require --force option.\n\
515\n\
516Options:\n\
517 --db=DATABASE connect to DATABASE\n\
518 (default: %s)\n\
519 --oneline print exactly one line of output per command\n",
520 program_name, program_name, default_db());
c75d1511 521 vlog_usage();
8f7501e8
BP
522 printf("\n\
523Other options:\n\
524 -h, --help display this help message\n\
525 -V, --version display version information\n");
c75d1511
BP
526 exit(EXIT_SUCCESS);
527}
528
529static char *
530default_db(void)
531{
532 static char *def;
533 if (!def) {
b43c6fe2 534 def = xasprintf("unix:%s/db.sock", ovs_rundir());
c75d1511
BP
535 }
536 return def;
537}
0c18b5a0
BP
538
539/* Returns true if it looks like this set of arguments might modify the
540 * database, otherwise false. (Not very smart, so it's prone to false
541 * positives.) */
542static bool
543might_write_to_db(char **argv)
544{
545 for (; *argv; argv++) {
546 const struct vsctl_command_syntax *p = find_command(*argv);
547 if (p && p->mode == RW) {
548 return true;
549 }
550 }
551 return false;
552}
c75d1511 553\f
5d9cb63c 554struct vsctl_context {
f8ff4bc4 555 /* Read-only. */
5d9cb63c
BP
556 int argc;
557 char **argv;
f8ff4bc4
BP
558 struct shash options;
559
560 /* Modifiable state. */
561 struct ds output;
ad83bfa6 562 struct ovsdb_idl *idl;
f8ff4bc4 563 struct ovsdb_idl_txn *txn;
ce5a3e38 564 struct ovsdb_symbol_table *symtab;
5d9cb63c 565 const struct ovsrec_open_vswitch *ovs;
f74055e7 566 bool verified_ports;
87b23a01
BP
567
568 /* A command may set this member to true if some prerequisite is not met
569 * and the caller should wait for something to change and then retry. */
570 bool try_again;
5d9cb63c
BP
571};
572
c75d1511
BP
573struct vsctl_bridge {
574 struct ovsrec_bridge *br_cfg;
575 char *name;
76ce9432 576 struct ovsrec_controller **ctrl;
31681a5d 577 char *fail_mode;
76ce9432 578 size_t n_ctrl;
c75d1511
BP
579 struct vsctl_bridge *parent;
580 int vlan;
581};
582
583struct vsctl_port {
584 struct ovsrec_port *port_cfg;
585 struct vsctl_bridge *bridge;
586};
587
588struct vsctl_iface {
589 struct ovsrec_interface *iface_cfg;
590 struct vsctl_port *port;
591};
592
593struct vsctl_info {
1588bb8d 594 struct vsctl_context *ctx;
e5e12280
BP
595 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
596 struct shash ports; /* Maps from port name to struct vsctl_port. */
597 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
c75d1511
BP
598};
599
bb1c67c8
BP
600static char *
601vsctl_context_to_string(const struct vsctl_context *ctx)
602{
603 const struct shash_node *node;
604 struct svec words;
605 char *s;
606 int i;
607
608 svec_init(&words);
609 SHASH_FOR_EACH (node, &ctx->options) {
610 svec_add(&words, node->name);
611 }
612 for (i = 0; i < ctx->argc; i++) {
613 svec_add(&words, ctx->argv[i]);
614 }
615 svec_terminate(&words);
616
617 s = process_escape_args(words.names);
618
619 svec_destroy(&words);
620
621 return s;
622}
623
f74055e7
BP
624static void
625verify_ports(struct vsctl_context *ctx)
626{
627 if (!ctx->verified_ports) {
628 const struct ovsrec_bridge *bridge;
629 const struct ovsrec_port *port;
630
631 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
632 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
633 ovsrec_bridge_verify_ports(bridge);
634 }
635 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
636 ovsrec_port_verify_interfaces(port);
637 }
638
639 ctx->verified_ports = true;
640 }
641}
642
c75d1511
BP
643static struct vsctl_bridge *
644add_bridge(struct vsctl_info *b,
645 struct ovsrec_bridge *br_cfg, const char *name,
646 struct vsctl_bridge *parent, int vlan)
647{
648 struct vsctl_bridge *br = xmalloc(sizeof *br);
649 br->br_cfg = br_cfg;
650 br->name = xstrdup(name);
651 br->parent = parent;
652 br->vlan = vlan;
76ce9432
BP
653 if (parent) {
654 br->ctrl = parent->br_cfg->controller;
655 br->n_ctrl = parent->br_cfg->n_controller;
31681a5d 656 br->fail_mode = parent->br_cfg->fail_mode;
76ce9432
BP
657 } else {
658 br->ctrl = br_cfg->controller;
659 br->n_ctrl = br_cfg->n_controller;
31681a5d 660 br->fail_mode = br_cfg->fail_mode;
76ce9432 661 }
c75d1511
BP
662 shash_add(&b->bridges, br->name, br);
663 return br;
664}
665
666static bool
667port_is_fake_bridge(const struct ovsrec_port *port_cfg)
668{
669 return (port_cfg->fake_bridge
670 && port_cfg->tag
671 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
672}
673
674static struct vsctl_bridge *
675find_vlan_bridge(struct vsctl_info *info,
676 struct vsctl_bridge *parent, int vlan)
677{
678 struct shash_node *node;
679
680 SHASH_FOR_EACH (node, &info->bridges) {
681 struct vsctl_bridge *br = node->data;
682 if (br->parent == parent && br->vlan == vlan) {
683 return br;
684 }
685 }
686
687 return NULL;
688}
689
690static void
691free_info(struct vsctl_info *info)
692{
693 struct shash_node *node;
694
695 SHASH_FOR_EACH (node, &info->bridges) {
696 struct vsctl_bridge *bridge = node->data;
697 free(bridge->name);
698 free(bridge);
699 }
700 shash_destroy(&info->bridges);
701
506051fc
BP
702 shash_destroy_free_data(&info->ports);
703 shash_destroy_free_data(&info->ifaces);
c75d1511
BP
704}
705
e5e12280
BP
706static void
707pre_get_info(struct vsctl_context *ctx)
708{
709 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
710
711 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
712 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
713 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
714 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
715
716 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
717 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
718 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
719 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
720
721 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
722}
723
c75d1511 724static void
1588bb8d 725get_info(struct vsctl_context *ctx, struct vsctl_info *info)
c75d1511 726{
1588bb8d 727 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
c75d1511
BP
728 struct shash bridges, ports;
729 size_t i;
730
1588bb8d 731 info->ctx = ctx;
c75d1511
BP
732 shash_init(&info->bridges);
733 shash_init(&info->ports);
734 shash_init(&info->ifaces);
735
736 shash_init(&bridges);
737 shash_init(&ports);
738 for (i = 0; i < ovs->n_bridges; i++) {
739 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
740 struct vsctl_bridge *br;
741 size_t j;
742
743 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
744 VLOG_WARN("%s: database contains duplicate bridge name",
745 br_cfg->name);
746 continue;
747 }
748 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
749 if (!br) {
750 continue;
751 }
752
753 for (j = 0; j < br_cfg->n_ports; j++) {
754 struct ovsrec_port *port_cfg = br_cfg->ports[j];
755
756 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
757 VLOG_WARN("%s: database contains duplicate port name",
758 port_cfg->name);
759 continue;
760 }
761
762 if (port_is_fake_bridge(port_cfg)
dfbe07ba 763 && shash_add_once(&bridges, port_cfg->name, NULL)) {
c75d1511
BP
764 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
765 }
766 }
767 }
768 shash_destroy(&bridges);
769 shash_destroy(&ports);
770
771 shash_init(&bridges);
772 shash_init(&ports);
773 for (i = 0; i < ovs->n_bridges; i++) {
774 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
775 struct vsctl_bridge *br;
776 size_t j;
777
778 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
779 continue;
780 }
781 br = shash_find_data(&info->bridges, br_cfg->name);
782 for (j = 0; j < br_cfg->n_ports; j++) {
783 struct ovsrec_port *port_cfg = br_cfg->ports[j];
784 struct vsctl_port *port;
785 size_t k;
786
787 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
788 continue;
789 }
790
791 if (port_is_fake_bridge(port_cfg)
dfbe07ba 792 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
c75d1511
BP
793 continue;
794 }
795
796 port = xmalloc(sizeof *port);
797 port->port_cfg = port_cfg;
798 if (port_cfg->tag
799 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
800 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
801 if (!port->bridge) {
802 port->bridge = br;
803 }
804 } else {
805 port->bridge = br;
806 }
807 shash_add(&info->ports, port_cfg->name, port);
808
809 for (k = 0; k < port_cfg->n_interfaces; k++) {
810 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
811 struct vsctl_iface *iface;
812
813 if (shash_find(&info->ifaces, iface_cfg->name)) {
814 VLOG_WARN("%s: database contains duplicate interface name",
815 iface_cfg->name);
816 continue;
817 }
818
819 iface = xmalloc(sizeof *iface);
820 iface->iface_cfg = iface_cfg;
821 iface->port = port;
dfbe07ba 822 shash_add(&info->ifaces, iface_cfg->name, iface);
c75d1511
BP
823 }
824 }
825 }
826 shash_destroy(&bridges);
827 shash_destroy(&ports);
828}
829
830static void
831check_conflicts(struct vsctl_info *info, const char *name,
832 char *msg)
833{
834 struct vsctl_iface *iface;
835 struct vsctl_port *port;
836
f74055e7
BP
837 verify_ports(info->ctx);
838
c75d1511 839 if (shash_find(&info->bridges, name)) {
c88b6a27
BP
840 vsctl_fatal("%s because a bridge named %s already exists",
841 msg, name);
c75d1511
BP
842 }
843
844 port = shash_find_data(&info->ports, name);
845 if (port) {
c88b6a27
BP
846 vsctl_fatal("%s because a port named %s already exists on "
847 "bridge %s", msg, name, port->bridge->name);
c75d1511
BP
848 }
849
850 iface = shash_find_data(&info->ifaces, name);
851 if (iface) {
c88b6a27
BP
852 vsctl_fatal("%s because an interface named %s already exists "
853 "on bridge %s", msg, name, iface->port->bridge->name);
c75d1511
BP
854 }
855
856 free(msg);
857}
858
859static struct vsctl_bridge *
01845ce8 860find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
c75d1511
BP
861{
862 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
01845ce8 863 if (must_exist && !br) {
c88b6a27 864 vsctl_fatal("no bridge named %s", name);
c75d1511 865 }
f74055e7 866 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
c75d1511
BP
867 return br;
868}
869
975ac531
JP
870static struct vsctl_bridge *
871find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
872{
873 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
874 if (br && br->parent) {
875 vsctl_fatal("%s is a fake bridge", name);
876 }
877 return br;
878}
879
c75d1511 880static struct vsctl_port *
01845ce8 881find_port(struct vsctl_info *info, const char *name, bool must_exist)
c75d1511
BP
882{
883 struct vsctl_port *port = shash_find_data(&info->ports, name);
460aad80 884 if (port && !strcmp(name, port->bridge->name)) {
01845ce8
BP
885 port = NULL;
886 }
887 if (must_exist && !port) {
c88b6a27 888 vsctl_fatal("no port named %s", name);
c75d1511 889 }
f74055e7 890 verify_ports(info->ctx);
c75d1511
BP
891 return port;
892}
893
894static struct vsctl_iface *
01845ce8 895find_iface(struct vsctl_info *info, const char *name, bool must_exist)
c75d1511
BP
896{
897 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
460aad80 898 if (iface && !strcmp(name, iface->port->bridge->name)) {
01845ce8
BP
899 iface = NULL;
900 }
901 if (must_exist && !iface) {
c88b6a27 902 vsctl_fatal("no interface named %s", name);
c75d1511 903 }
f74055e7 904 verify_ports(info->ctx);
c75d1511
BP
905 return iface;
906}
907
908static void
909bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
910{
911 struct ovsrec_port **ports;
912 size_t i;
913
914 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
915 for (i = 0; i < br->n_ports; i++) {
916 ports[i] = br->ports[i];
917 }
c75d1511
BP
918 ports[br->n_ports] = port;
919 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
920 free(ports);
921}
922
923static void
924bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
925{
926 struct ovsrec_port **ports;
927 size_t i, n;
928
929 ports = xmalloc(sizeof *br->ports * br->n_ports);
930 for (i = n = 0; i < br->n_ports; i++) {
931 if (br->ports[i] != port) {
932 ports[n++] = br->ports[i];
933 }
934 }
935 ovsrec_bridge_set_ports(br, ports, n);
936 free(ports);
937}
938
939static void
940ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
941 struct ovsrec_bridge *bridge)
942{
943 struct ovsrec_bridge **bridges;
944 size_t i;
945
946 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
947 for (i = 0; i < ovs->n_bridges; i++) {
948 bridges[i] = ovs->bridges[i];
949 }
950 bridges[ovs->n_bridges] = bridge;
951 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
952 free(bridges);
953}
954
955static void
956ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
957 struct ovsrec_bridge *bridge)
958{
959 struct ovsrec_bridge **bridges;
960 size_t i, n;
961
962 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
963 for (i = n = 0; i < ovs->n_bridges; i++) {
964 if (ovs->bridges[i] != bridge) {
965 bridges[n++] = ovs->bridges[i];
966 }
967 }
968 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
969 free(bridges);
970}
971
524555d1 972static void
c69ee87c 973cmd_init(struct vsctl_context *ctx OVS_UNUSED)
524555d1
BP
974{
975}
976
e5e12280
BP
977static void
978pre_cmd_emer_reset(struct vsctl_context *ctx)
979{
980 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers);
981 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
982
983 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
f67e3b66 984 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
e5e12280
BP
985 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
986 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
987 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
988 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
989 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
990
991 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
992
993 ovsdb_idl_add_column(ctx->idl,
994 &ovsrec_interface_col_ingress_policing_rate);
995 ovsdb_idl_add_column(ctx->idl,
996 &ovsrec_interface_col_ingress_policing_burst);
997}
998
18ee958b
JP
999static void
1000cmd_emer_reset(struct vsctl_context *ctx)
1001{
1002 const struct ovsdb_idl *idl = ctx->idl;
1003 const struct ovsrec_bridge *br;
1004 const struct ovsrec_port *port;
1005 const struct ovsrec_interface *iface;
1006 const struct ovsrec_mirror *mirror, *next_mirror;
1007 const struct ovsrec_controller *ctrl, *next_ctrl;
1008 const struct ovsrec_netflow *nf, *next_nf;
1009 const struct ovsrec_ssl *ssl, *next_ssl;
1010 const struct ovsrec_sflow *sflow, *next_sflow;
1011
18ee958b
JP
1012 /* Reset the Open_vSwitch table. */
1013 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
18ee958b
JP
1014 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1015
1016 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1017 int i;
1018 char *hw_key = "hwaddr";
1019 char *hw_val = NULL;
1020
1021 ovsrec_bridge_set_controller(br, NULL, 0);
f67e3b66 1022 ovsrec_bridge_set_fail_mode(br, NULL);
18ee958b
JP
1023 ovsrec_bridge_set_mirrors(br, NULL, 0);
1024 ovsrec_bridge_set_netflow(br, NULL);
1025 ovsrec_bridge_set_sflow(br, NULL);
1026 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1027
1028 /* We only want to save the "hwaddr" key from other_config. */
1029 for (i=0; i < br->n_other_config; i++) {
1030 if (!strcmp(br->key_other_config[i], hw_key)) {
1031 hw_val = br->value_other_config[i];
1032 break;
1033 }
1034 }
1035 if (hw_val) {
1036 char *val = xstrdup(hw_val);
1037 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1038 free(val);
1039 } else {
1040 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1041 }
1042 }
1043
1044 OVSREC_PORT_FOR_EACH (port, idl) {
1045 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1046 }
1047
1048 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1049 /* xxx What do we do about gre/patch devices created by mgr? */
1050
1051 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1052 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1053 }
1054
1055 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1056 ovsrec_mirror_delete(mirror);
1057 }
1058
1059 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1060 ovsrec_controller_delete(ctrl);
1061 }
1062
1063 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1064 ovsrec_netflow_delete(nf);
1065 }
1066
1067 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1068 ovsrec_ssl_delete(ssl);
1069 }
1070
1071 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1072 ovsrec_sflow_delete(sflow);
1073 }
1074}
1075
c75d1511 1076static void
5d9cb63c 1077cmd_add_br(struct vsctl_context *ctx)
c75d1511 1078{
aeee85aa 1079 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
b89d8339 1080 const char *br_name, *parent_name;
c75d1511 1081 struct vsctl_info info;
b89d8339 1082 int vlan;
c75d1511 1083
aeee85aa
BP
1084 br_name = ctx->argv[1];
1085 if (ctx->argc == 2) {
1086 parent_name = NULL;
1087 vlan = 0;
1088 } else if (ctx->argc == 4) {
1089 parent_name = ctx->argv[2];
1090 vlan = atoi(ctx->argv[3]);
1091 if (vlan < 1 || vlan > 4095) {
1092 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1093 }
1094 } else {
1095 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1096 ctx->argv[0]);
1097 }
1098
1588bb8d 1099 get_info(ctx, &info);
aeee85aa
BP
1100 if (may_exist) {
1101 struct vsctl_bridge *br;
1102
1103 br = find_bridge(&info, br_name, false);
1104 if (br) {
1105 if (!parent_name) {
1106 if (br->parent) {
1107 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1108 "a VLAN bridge for VLAN %d",
1109 br_name, br_name, br->vlan);
1110 }
1111 } else {
1112 if (!br->parent) {
1113 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1114 "is not a VLAN bridge",
1115 br_name, parent_name, vlan, br_name);
1116 } else if (strcmp(br->parent->name, parent_name)) {
1117 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1118 "has the wrong parent %s",
1119 br_name, parent_name, vlan,
1120 br_name, br->parent->name);
1121 } else if (br->vlan != vlan) {
1122 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1123 "is a VLAN bridge for the wrong VLAN %d",
1124 br_name, parent_name, vlan, br_name, br->vlan);
1125 }
1126 }
1127 return;
1128 }
1129 }
c75d1511
BP
1130 check_conflicts(&info, br_name,
1131 xasprintf("cannot create a bridge named %s", br_name));
1132
aeee85aa 1133 if (!parent_name) {
c75d1511
BP
1134 struct ovsrec_port *port;
1135 struct ovsrec_interface *iface;
aeee85aa 1136 struct ovsrec_bridge *br;
c75d1511 1137
f8ff4bc4 1138 iface = ovsrec_interface_insert(ctx->txn);
c75d1511 1139 ovsrec_interface_set_name(iface, br_name);
9106d88c 1140 ovsrec_interface_set_type(iface, "internal");
c75d1511 1141
f8ff4bc4 1142 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1143 ovsrec_port_set_name(port, br_name);
1144 ovsrec_port_set_interfaces(port, &iface, 1);
1145
f8ff4bc4 1146 br = ovsrec_bridge_insert(ctx->txn);
c75d1511
BP
1147 ovsrec_bridge_set_name(br, br_name);
1148 ovsrec_bridge_set_ports(br, &port, 1);
1149
5d9cb63c 1150 ovs_insert_bridge(ctx->ovs, br);
aeee85aa 1151 } else {
c75d1511
BP
1152 struct vsctl_bridge *parent;
1153 struct ovsrec_port *port;
1154 struct ovsrec_interface *iface;
aeee85aa 1155 struct ovsrec_bridge *br;
c75d1511
BP
1156 int64_t tag = vlan;
1157
01845ce8 1158 parent = find_bridge(&info, parent_name, false);
c75d1511 1159 if (parent && parent->vlan) {
11aa5627 1160 vsctl_fatal("cannot create bridge with fake bridge as parent");
c75d1511
BP
1161 }
1162 if (!parent) {
c88b6a27 1163 vsctl_fatal("parent bridge %s does not exist", parent_name);
c75d1511
BP
1164 }
1165 br = parent->br_cfg;
1166
f8ff4bc4 1167 iface = ovsrec_interface_insert(ctx->txn);
c75d1511
BP
1168 ovsrec_interface_set_name(iface, br_name);
1169 ovsrec_interface_set_type(iface, "internal");
1170
f8ff4bc4 1171 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1172 ovsrec_port_set_name(port, br_name);
1173 ovsrec_port_set_interfaces(port, &iface, 1);
1174 ovsrec_port_set_fake_bridge(port, true);
1175 ovsrec_port_set_tag(port, &tag, 1);
dfbe07ba
BP
1176
1177 bridge_insert_port(br, port);
c75d1511
BP
1178 }
1179
1180 free_info(&info);
1181}
1182
1183static void
1184del_port(struct vsctl_info *info, struct vsctl_port *port)
1185{
1186 struct shash_node *node;
1187
1188 SHASH_FOR_EACH (node, &info->ifaces) {
1189 struct vsctl_iface *iface = node->data;
1190 if (iface->port == port) {
1191 ovsrec_interface_delete(iface->iface_cfg);
1192 }
1193 }
1194 ovsrec_port_delete(port->port_cfg);
1195
1196 bridge_delete_port((port->bridge->parent
1197 ? port->bridge->parent->br_cfg
1198 : port->bridge->br_cfg), port->port_cfg);
1199}
1200
1201static void
5d9cb63c 1202cmd_del_br(struct vsctl_context *ctx)
c75d1511 1203{
460aad80 1204 bool must_exist = !shash_find(&ctx->options, "--if-exists");
c75d1511 1205 struct vsctl_bridge *bridge;
460aad80 1206 struct vsctl_info info;
c75d1511 1207
1588bb8d 1208 get_info(ctx, &info);
460aad80
BP
1209 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1210 if (bridge) {
1211 struct shash_node *node;
1212
1213 SHASH_FOR_EACH (node, &info.ports) {
1214 struct vsctl_port *port = node->data;
8cf8cc66 1215 if (port->bridge == bridge || port->bridge->parent == bridge
460aad80
BP
1216 || !strcmp(port->port_cfg->name, bridge->name)) {
1217 del_port(&info, port);
1218 }
1219 }
1220 if (bridge->br_cfg) {
1221 ovsrec_bridge_delete(bridge->br_cfg);
1222 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
c75d1511 1223 }
c75d1511
BP
1224 }
1225 free_info(&info);
1226}
1227
dfbe07ba
BP
1228static void
1229output_sorted(struct svec *svec, struct ds *output)
1230{
1231 const char *name;
1232 size_t i;
1233
1234 svec_sort(svec);
1235 SVEC_FOR_EACH (i, name, svec) {
1236 ds_put_format(output, "%s\n", name);
1237 }
1238}
1239
c75d1511 1240static void
5d9cb63c 1241cmd_list_br(struct vsctl_context *ctx)
c75d1511
BP
1242{
1243 struct shash_node *node;
1244 struct vsctl_info info;
dfbe07ba 1245 struct svec bridges;
c75d1511 1246
1588bb8d 1247 get_info(ctx, &info);
dfbe07ba
BP
1248
1249 svec_init(&bridges);
c75d1511
BP
1250 SHASH_FOR_EACH (node, &info.bridges) {
1251 struct vsctl_bridge *br = node->data;
dfbe07ba 1252 svec_add(&bridges, br->name);
c75d1511 1253 }
5d9cb63c 1254 output_sorted(&bridges, &ctx->output);
dfbe07ba
BP
1255 svec_destroy(&bridges);
1256
c75d1511
BP
1257 free_info(&info);
1258}
1259
1260static void
5d9cb63c 1261cmd_br_exists(struct vsctl_context *ctx)
c75d1511
BP
1262{
1263 struct vsctl_info info;
1264
1588bb8d 1265 get_info(ctx, &info);
01845ce8 1266 if (!find_bridge(&info, ctx->argv[1], false)) {
1d48b4be 1267 vsctl_exit(2);
c75d1511
BP
1268 }
1269 free_info(&info);
1270}
1271
457e1eb0
BP
1272/* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1273 * equals 'a', false otherwise. */
1274static bool
1275key_matches(const char *a,
1276 const char *b_prefix, size_t b_prefix_len, const char *b)
1277{
1278 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1279}
1280
1281static void
1282set_external_id(char **old_keys, char **old_values, size_t old_n,
1283 char *key, char *value,
1284 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1285{
1286 char **new_keys;
1287 char **new_values;
1288 size_t new_n;
1289 size_t i;
1290
1291 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1292 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1293 new_n = 0;
1294 for (i = 0; i < old_n; i++) {
1295 if (strcmp(key, old_keys[i])) {
1296 new_keys[new_n] = old_keys[i];
1297 new_values[new_n] = old_values[i];
1298 new_n++;
1299 }
1300 }
1301 if (value) {
1302 new_keys[new_n] = key;
1303 new_values[new_n] = value;
1304 new_n++;
1305 }
1306 *new_keysp = new_keys;
1307 *new_valuesp = new_values;
1308 *new_np = new_n;
1309}
1310
e5e12280
BP
1311static void
1312pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1313{
1314 pre_get_info(ctx);
1315 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1316 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1317}
1318
457e1eb0 1319static void
5d9cb63c 1320cmd_br_set_external_id(struct vsctl_context *ctx)
457e1eb0
BP
1321{
1322 struct vsctl_info info;
1323 struct vsctl_bridge *bridge;
1324 char **keys, **values;
1325 size_t n;
1326
1588bb8d 1327 get_info(ctx, &info);
01845ce8 1328 bridge = find_bridge(&info, ctx->argv[1], true);
457e1eb0
BP
1329 if (bridge->br_cfg) {
1330 set_external_id(bridge->br_cfg->key_external_ids,
1331 bridge->br_cfg->value_external_ids,
1332 bridge->br_cfg->n_external_ids,
5d9cb63c 1333 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
457e1eb0 1334 &keys, &values, &n);
f74055e7 1335 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
457e1eb0
BP
1336 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1337 } else {
5d9cb63c
BP
1338 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1339 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
457e1eb0
BP
1340 set_external_id(port->port_cfg->key_external_ids,
1341 port->port_cfg->value_external_ids,
1342 port->port_cfg->n_external_ids,
5d9cb63c 1343 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
457e1eb0 1344 &keys, &values, &n);
f74055e7 1345 ovsrec_port_verify_external_ids(port->port_cfg);
457e1eb0
BP
1346 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1347 free(key);
1348 }
1349 free(keys);
1350 free(values);
1351
1352 free_info(&info);
1353}
1354
1355static void
1356get_external_id(char **keys, char **values, size_t n,
1357 const char *prefix, const char *key,
1358 struct ds *output)
1359{
1360 size_t prefix_len = strlen(prefix);
1361 struct svec svec;
1362 size_t i;
1363
1364 svec_init(&svec);
1365 for (i = 0; i < n; i++) {
1366 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1367 svec_add_nocopy(&svec, xasprintf("%s=%s",
1368 keys[i] + prefix_len, values[i]));
1369 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1370 svec_add(&svec, values[i]);
1371 break;
1372 }
1373 }
1374 output_sorted(&svec, output);
1375 svec_destroy(&svec);
1376}
1377
e5e12280
BP
1378static void
1379pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1380{
1381 pre_cmd_br_set_external_id(ctx);
1382}
1383
457e1eb0 1384static void
5d9cb63c 1385cmd_br_get_external_id(struct vsctl_context *ctx)
457e1eb0
BP
1386{
1387 struct vsctl_info info;
1388 struct vsctl_bridge *bridge;
1389
1588bb8d 1390 get_info(ctx, &info);
01845ce8 1391 bridge = find_bridge(&info, ctx->argv[1], true);
457e1eb0 1392 if (bridge->br_cfg) {
f74055e7 1393 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
457e1eb0
BP
1394 get_external_id(bridge->br_cfg->key_external_ids,
1395 bridge->br_cfg->value_external_ids,
1396 bridge->br_cfg->n_external_ids,
5d9cb63c
BP
1397 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1398 &ctx->output);
457e1eb0 1399 } else {
5d9cb63c 1400 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
f74055e7 1401 ovsrec_port_verify_external_ids(port->port_cfg);
457e1eb0
BP
1402 get_external_id(port->port_cfg->key_external_ids,
1403 port->port_cfg->value_external_ids,
1404 port->port_cfg->n_external_ids,
5d9cb63c 1405 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
457e1eb0
BP
1406 }
1407 free_info(&info);
1408}
1409
1410
c75d1511 1411static void
5d9cb63c 1412cmd_list_ports(struct vsctl_context *ctx)
c75d1511
BP
1413{
1414 struct vsctl_bridge *br;
1415 struct shash_node *node;
1416 struct vsctl_info info;
dfbe07ba 1417 struct svec ports;
c75d1511 1418
1588bb8d 1419 get_info(ctx, &info);
01845ce8 1420 br = find_bridge(&info, ctx->argv[1], true);
f74055e7 1421 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
dfbe07ba
BP
1422
1423 svec_init(&ports);
c75d1511
BP
1424 SHASH_FOR_EACH (node, &info.ports) {
1425 struct vsctl_port *port = node->data;
1426
1427 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
dfbe07ba 1428 svec_add(&ports, port->port_cfg->name);
c75d1511
BP
1429 }
1430 }
5d9cb63c 1431 output_sorted(&ports, &ctx->output);
dfbe07ba
BP
1432 svec_destroy(&ports);
1433
c75d1511
BP
1434 free_info(&info);
1435}
1436
1437static void
f8ff4bc4 1438add_port(struct vsctl_context *ctx,
bb1c67c8
BP
1439 const char *br_name, const char *port_name,
1440 bool may_exist, bool fake_iface,
18b239f5
BP
1441 char *iface_names[], int n_ifaces,
1442 char *settings[], int n_settings)
c75d1511
BP
1443{
1444 struct vsctl_info info;
1445 struct vsctl_bridge *bridge;
1446 struct ovsrec_interface **ifaces;
1447 struct ovsrec_port *port;
1448 size_t i;
1449
1588bb8d 1450 get_info(ctx, &info);
bb1c67c8 1451 if (may_exist) {
2a022368 1452 struct vsctl_port *vsctl_port;
bb1c67c8 1453
2a022368
BP
1454 vsctl_port = find_port(&info, port_name, false);
1455 if (vsctl_port) {
bb1c67c8 1456 struct svec want_names, have_names;
bb1c67c8
BP
1457
1458 svec_init(&want_names);
1459 for (i = 0; i < n_ifaces; i++) {
1460 svec_add(&want_names, iface_names[i]);
1461 }
1462 svec_sort(&want_names);
1463
1464 svec_init(&have_names);
2a022368
BP
1465 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1466 svec_add(&have_names,
1467 vsctl_port->port_cfg->interfaces[i]->name);
bb1c67c8
BP
1468 }
1469 svec_sort(&have_names);
1470
2a022368 1471 if (strcmp(vsctl_port->bridge->name, br_name)) {
bb1c67c8
BP
1472 char *command = vsctl_context_to_string(ctx);
1473 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
2a022368 1474 command, port_name, vsctl_port->bridge->name);
bb1c67c8
BP
1475 }
1476
1477 if (!svec_equal(&want_names, &have_names)) {
1478 char *have_names_string = svec_join(&have_names, ", ", "");
1479 char *command = vsctl_context_to_string(ctx);
1480
1481 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1482 command, port_name, have_names_string);
1483 }
1484
1485 svec_destroy(&want_names);
1486 svec_destroy(&have_names);
1487
1488 return;
1489 }
1490 }
c75d1511
BP
1491 check_conflicts(&info, port_name,
1492 xasprintf("cannot create a port named %s", port_name));
bb1c67c8
BP
1493 for (i = 0; i < n_ifaces; i++) {
1494 check_conflicts(&info, iface_names[i],
1495 xasprintf("cannot create an interface named %s",
1496 iface_names[i]));
1497 }
01845ce8 1498 bridge = find_bridge(&info, br_name, true);
c75d1511
BP
1499
1500 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1501 for (i = 0; i < n_ifaces; i++) {
f8ff4bc4 1502 ifaces[i] = ovsrec_interface_insert(ctx->txn);
c75d1511
BP
1503 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1504 }
1505
f8ff4bc4 1506 port = ovsrec_port_insert(ctx->txn);
c75d1511
BP
1507 ovsrec_port_set_name(port, port_name);
1508 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
b4182c7f 1509 ovsrec_port_set_bond_fake_iface(port, fake_iface);
a0a9f31d
JP
1510 free(ifaces);
1511
c75d1511
BP
1512 if (bridge->vlan) {
1513 int64_t tag = bridge->vlan;
1514 ovsrec_port_set_tag(port, &tag, 1);
1515 }
1516
18b239f5 1517 for (i = 0; i < n_settings; i++) {
ce5a3e38
BP
1518 set_column(get_table("Port"), &port->header_, settings[i],
1519 ctx->symtab);
18b239f5
BP
1520 }
1521
c75d1511
BP
1522 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1523 : bridge->br_cfg), port);
1524
1525 free_info(&info);
1526}
1527
1528static void
5d9cb63c 1529cmd_add_port(struct vsctl_context *ctx)
c75d1511 1530{
bb1c67c8
BP
1531 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1532
1533 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
18b239f5 1534 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
c75d1511
BP
1535}
1536
1537static void
5d9cb63c 1538cmd_add_bond(struct vsctl_context *ctx)
c75d1511 1539{
bb1c67c8 1540 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
b4182c7f 1541 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
18b239f5
BP
1542 int n_ifaces;
1543 int i;
1544
1545 n_ifaces = ctx->argc - 3;
1546 for (i = 3; i < ctx->argc; i++) {
1547 if (strchr(ctx->argv[i], '=')) {
1548 n_ifaces = i - 3;
1549 break;
1550 }
1551 }
1552 if (n_ifaces < 2) {
1553 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1554 "%d were specified", n_ifaces);
1555 }
b4182c7f 1556
bb1c67c8 1557 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
18b239f5
BP
1558 &ctx->argv[3], n_ifaces,
1559 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
c75d1511
BP
1560}
1561
1562static void
5d9cb63c 1563cmd_del_port(struct vsctl_context *ctx)
c75d1511 1564{
460aad80 1565 bool must_exist = !shash_find(&ctx->options, "--if-exists");
7c79588e
BP
1566 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1567 struct vsctl_port *port;
c75d1511
BP
1568 struct vsctl_info info;
1569
1588bb8d 1570 get_info(ctx, &info);
7c79588e
BP
1571 if (!with_iface) {
1572 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1573 } else {
1574 const char *target = ctx->argv[ctx->argc - 1];
1575 struct vsctl_iface *iface;
1576
1577 port = find_port(&info, target, false);
1578 if (!port) {
1579 iface = find_iface(&info, target, false);
1580 if (iface) {
1581 port = iface->port;
1582 }
1583 }
1584 if (must_exist && !port) {
1585 vsctl_fatal("no port or interface named %s", target);
460aad80 1586 }
7c79588e 1587 }
460aad80 1588
7c79588e
BP
1589 if (port) {
1590 if (ctx->argc == 3) {
1591 struct vsctl_bridge *bridge;
1592
1593 bridge = find_bridge(&info, ctx->argv[1], true);
1594 if (port->bridge != bridge) {
1595 if (port->bridge->parent == bridge) {
1596 vsctl_fatal("bridge %s does not have a port %s (although "
1597 "its parent bridge %s does)",
1598 ctx->argv[1], ctx->argv[2],
1599 bridge->parent->name);
1600 } else {
1601 vsctl_fatal("bridge %s does not have a port %s",
1602 ctx->argv[1], ctx->argv[2]);
1603 }
460aad80 1604 }
c75d1511 1605 }
7c79588e
BP
1606
1607 del_port(&info, port);
c75d1511 1608 }
7c79588e 1609
c75d1511
BP
1610 free_info(&info);
1611}
1612
1613static void
5d9cb63c 1614cmd_port_to_br(struct vsctl_context *ctx)
c75d1511
BP
1615{
1616 struct vsctl_port *port;
1617 struct vsctl_info info;
1618
1588bb8d 1619 get_info(ctx, &info);
01845ce8 1620 port = find_port(&info, ctx->argv[1], true);
5d9cb63c 1621 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
c75d1511
BP
1622 free_info(&info);
1623}
1624
1625static void
5d9cb63c 1626cmd_br_to_vlan(struct vsctl_context *ctx)
c75d1511
BP
1627{
1628 struct vsctl_bridge *bridge;
1629 struct vsctl_info info;
1630
1588bb8d 1631 get_info(ctx, &info);
01845ce8 1632 bridge = find_bridge(&info, ctx->argv[1], true);
5d9cb63c 1633 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
c75d1511
BP
1634 free_info(&info);
1635}
1636
1637static void
5d9cb63c 1638cmd_br_to_parent(struct vsctl_context *ctx)
c75d1511
BP
1639{
1640 struct vsctl_bridge *bridge;
1641 struct vsctl_info info;
1642
1588bb8d 1643 get_info(ctx, &info);
01845ce8 1644 bridge = find_bridge(&info, ctx->argv[1], true);
c75d1511
BP
1645 if (bridge->parent) {
1646 bridge = bridge->parent;
1647 }
5d9cb63c 1648 ds_put_format(&ctx->output, "%s\n", bridge->name);
c75d1511
BP
1649 free_info(&info);
1650}
1651
1652static void
5d9cb63c 1653cmd_list_ifaces(struct vsctl_context *ctx)
c75d1511
BP
1654{
1655 struct vsctl_bridge *br;
1656 struct shash_node *node;
1657 struct vsctl_info info;
dfbe07ba 1658 struct svec ifaces;
c75d1511 1659
1588bb8d 1660 get_info(ctx, &info);
01845ce8 1661 br = find_bridge(&info, ctx->argv[1], true);
f74055e7 1662 verify_ports(ctx);
dfbe07ba
BP
1663
1664 svec_init(&ifaces);
c75d1511
BP
1665 SHASH_FOR_EACH (node, &info.ifaces) {
1666 struct vsctl_iface *iface = node->data;
1667
dfbe07ba
BP
1668 if (strcmp(iface->iface_cfg->name, br->name)
1669 && br == iface->port->bridge) {
1670 svec_add(&ifaces, iface->iface_cfg->name);
c75d1511
BP
1671 }
1672 }
5d9cb63c 1673 output_sorted(&ifaces, &ctx->output);
dfbe07ba
BP
1674 svec_destroy(&ifaces);
1675
c75d1511
BP
1676 free_info(&info);
1677}
1678
1679static void
5d9cb63c 1680cmd_iface_to_br(struct vsctl_context *ctx)
c75d1511
BP
1681{
1682 struct vsctl_iface *iface;
1683 struct vsctl_info info;
1684
1588bb8d 1685 get_info(ctx, &info);
01845ce8 1686 iface = find_iface(&info, ctx->argv[1], true);
5d9cb63c 1687 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
c75d1511
BP
1688 free_info(&info);
1689}
457e1eb0 1690
f74055e7
BP
1691static void
1692verify_controllers(struct ovsrec_bridge *bridge)
1693{
1694 if (bridge) {
1695 size_t i;
1696
1697 ovsrec_bridge_verify_controller(bridge);
1698 for (i = 0; i < bridge->n_controller; i++) {
1699 ovsrec_controller_verify_target(bridge->controller[i]);
1700 }
1701 }
1702}
1703
4e3e7ff9
BP
1704static void
1705pre_controller(struct vsctl_context *ctx)
1706{
1707 pre_get_info(ctx);
1708
1709 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1710}
1711
76ce9432 1712static void
1a048029 1713cmd_get_controller(struct vsctl_context *ctx)
76ce9432 1714{
1a048029
JP
1715 struct vsctl_info info;
1716 struct vsctl_bridge *br;
76ce9432
BP
1717 struct svec targets;
1718 size_t i;
1719
1588bb8d 1720 get_info(ctx, &info);
1a048029 1721 br = find_bridge(&info, ctx->argv[1], true);
f74055e7 1722 verify_controllers(br->br_cfg);
1a048029
JP
1723
1724 /* Print the targets in sorted order for reproducibility. */
76ce9432 1725 svec_init(&targets);
1a048029
JP
1726 for (i = 0; i < br->n_ctrl; i++) {
1727 svec_add(&targets, br->ctrl[i]->target);
76ce9432
BP
1728 }
1729
1730 svec_sort(&targets);
1731 for (i = 0; i < targets.n; i++) {
1732 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1733 }
1734 svec_destroy(&targets);
5aa00635
JP
1735
1736 free_info(&info);
1737}
1738
76ce9432
BP
1739static void
1740delete_controllers(struct ovsrec_controller **controllers,
1741 size_t n_controllers)
1742{
1743 size_t i;
1744
1745 for (i = 0; i < n_controllers; i++) {
1746 ovsrec_controller_delete(controllers[i]);
1747 }
1748}
1749
5aa00635
JP
1750static void
1751cmd_del_controller(struct vsctl_context *ctx)
1752{
1753 struct vsctl_info info;
1a048029 1754 struct vsctl_bridge *br;
5aa00635 1755
1588bb8d 1756 get_info(ctx, &info);
1a048029 1757 br = find_real_bridge(&info, ctx->argv[1], true);
f74055e7 1758 verify_controllers(br->br_cfg);
5aa00635 1759
1a048029
JP
1760 if (br->ctrl) {
1761 delete_controllers(br->ctrl, br->n_ctrl);
1762 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
5aa00635
JP
1763 }
1764
1765 free_info(&info);
1766}
1767
76ce9432
BP
1768static struct ovsrec_controller **
1769insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1770{
1771 struct ovsrec_controller **controllers;
1772 size_t i;
1773
1774 controllers = xmalloc(n * sizeof *controllers);
1775 for (i = 0; i < n; i++) {
1776 controllers[i] = ovsrec_controller_insert(txn);
1777 ovsrec_controller_set_target(controllers[i], targets[i]);
1778 }
1779
1780 return controllers;
1781}
1782
5aa00635
JP
1783static void
1784cmd_set_controller(struct vsctl_context *ctx)
1785{
1786 struct vsctl_info info;
1a048029
JP
1787 struct vsctl_bridge *br;
1788 struct ovsrec_controller **controllers;
1789 size_t n;
5aa00635 1790
1588bb8d 1791 get_info(ctx, &info);
1a048029 1792 br = find_real_bridge(&info, ctx->argv[1], true);
f74055e7 1793 verify_controllers(br->br_cfg);
5aa00635 1794
1a048029 1795 delete_controllers(br->ctrl, br->n_ctrl);
76ce9432 1796
1a048029
JP
1797 n = ctx->argc - 2;
1798 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1799 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1800 free(controllers);
5aa00635
JP
1801
1802 free_info(&info);
1803}
1804
1805static void
1806cmd_get_fail_mode(struct vsctl_context *ctx)
1807{
1808 struct vsctl_info info;
1a048029 1809 struct vsctl_bridge *br;
5aa00635 1810
1588bb8d 1811 get_info(ctx, &info);
1a048029 1812 br = find_bridge(&info, ctx->argv[1], true);
5aa00635 1813
f74055e7
BP
1814 if (br->br_cfg) {
1815 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1816 }
31681a5d
JP
1817 if (br->fail_mode && strlen(br->fail_mode)) {
1818 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
5aa00635
JP
1819 }
1820
1821 free_info(&info);
1822}
1823
1824static void
1825cmd_del_fail_mode(struct vsctl_context *ctx)
1826{
1827 struct vsctl_info info;
1a048029 1828 struct vsctl_bridge *br;
5aa00635 1829
1588bb8d 1830 get_info(ctx, &info);
1a048029 1831 br = find_real_bridge(&info, ctx->argv[1], true);
5aa00635 1832
31681a5d 1833 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
5aa00635
JP
1834
1835 free_info(&info);
1836}
1837
1838static void
1839cmd_set_fail_mode(struct vsctl_context *ctx)
1840{
1841 struct vsctl_info info;
1a048029
JP
1842 struct vsctl_bridge *br;
1843 const char *fail_mode = ctx->argv[2];
5aa00635 1844
1588bb8d 1845 get_info(ctx, &info);
1a048029 1846 br = find_real_bridge(&info, ctx->argv[1], true);
5aa00635
JP
1847
1848 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1849 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1850 }
1851
31681a5d 1852 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
5aa00635
JP
1853
1854 free_info(&info);
1855}
dd8ac6fe 1856
e5e12280
BP
1857static void
1858pre_cmd_get_ssl(struct vsctl_context *ctx)
1859{
1860 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1861
1862 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
1863 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
1864 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
1865 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
1866}
1867
dd8ac6fe
JP
1868static void
1869cmd_get_ssl(struct vsctl_context *ctx)
1870{
1871 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1872
f74055e7 1873 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
dd8ac6fe 1874 if (ssl) {
f74055e7
BP
1875 ovsrec_ssl_verify_private_key(ssl);
1876 ovsrec_ssl_verify_certificate(ssl);
1877 ovsrec_ssl_verify_ca_cert(ssl);
1878 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
1879
dd8ac6fe
JP
1880 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1881 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1882 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1883 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1884 ssl->bootstrap_ca_cert ? "true" : "false");
1885 }
1886}
1887
e5e12280
BP
1888static void
1889pre_cmd_del_ssl(struct vsctl_context *ctx)
1890{
1891 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1892}
1893
dd8ac6fe
JP
1894static void
1895cmd_del_ssl(struct vsctl_context *ctx)
1896{
1897 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1898
1899 if (ssl) {
f74055e7 1900 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
dd8ac6fe
JP
1901 ovsrec_ssl_delete(ssl);
1902 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1903 }
1904}
1905
e5e12280
BP
1906static void
1907pre_cmd_set_ssl(struct vsctl_context *ctx)
1908{
1909 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1910}
1911
dd8ac6fe
JP
1912static void
1913cmd_set_ssl(struct vsctl_context *ctx)
1914{
1915 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1916 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1917
f74055e7 1918 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
dd8ac6fe
JP
1919 if (ssl) {
1920 ovsrec_ssl_delete(ssl);
1921 }
f8ff4bc4 1922 ssl = ovsrec_ssl_insert(ctx->txn);
dd8ac6fe
JP
1923
1924 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1925 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1926 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1927
1928 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1929
1930 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1931}
c75d1511 1932\f
ad83bfa6
BP
1933/* Parameter commands. */
1934
ad83bfa6
BP
1935struct vsctl_row_id {
1936 const struct ovsdb_idl_table_class *table;
1937 const struct ovsdb_idl_column *name_column;
1938 const struct ovsdb_idl_column *uuid_column;
1939};
1940
1941struct vsctl_table_class {
1942 struct ovsdb_idl_table_class *class;
ad83bfa6
BP
1943 struct vsctl_row_id row_ids[2];
1944};
1945
1946static const struct vsctl_table_class tables[] = {
bd76d25d 1947 {&ovsrec_table_bridge,
ad83bfa6
BP
1948 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1949 {NULL, NULL, NULL}}},
1950
bd76d25d 1951 {&ovsrec_table_controller,
ad83bfa6
BP
1952 {{&ovsrec_table_bridge,
1953 &ovsrec_bridge_col_name,
1a048029 1954 &ovsrec_bridge_col_controller}}},
ad83bfa6 1955
bd76d25d 1956 {&ovsrec_table_interface,
ad83bfa6
BP
1957 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1958 {NULL, NULL, NULL}}},
1959
bd76d25d 1960 {&ovsrec_table_mirror,
ad83bfa6
BP
1961 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1962 {NULL, NULL, NULL}}},
1963
94db5407
BP
1964 {&ovsrec_table_manager,
1965 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
1966 {NULL, NULL, NULL}}},
1967
bd76d25d 1968 {&ovsrec_table_netflow,
ad83bfa6
BP
1969 {{&ovsrec_table_bridge,
1970 &ovsrec_bridge_col_name,
1971 &ovsrec_bridge_col_netflow},
1972 {NULL, NULL, NULL}}},
1973
bd76d25d 1974 {&ovsrec_table_open_vswitch,
ad83bfa6
BP
1975 {{&ovsrec_table_open_vswitch, NULL, NULL},
1976 {NULL, NULL, NULL}}},
1977
bd76d25d 1978 {&ovsrec_table_port,
ad83bfa6
BP
1979 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1980 {NULL, NULL, NULL}}},
1981
c1c9c9c4
BP
1982 {&ovsrec_table_qos,
1983 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1984 {NULL, NULL, NULL}}},
1985
b31bcf60
EJ
1986 {&ovsrec_table_monitor,
1987 {{&ovsrec_table_interface,
1988 &ovsrec_interface_col_name,
1989 &ovsrec_interface_col_monitor},
1990 {NULL, NULL, NULL}}},
1991
1992 {&ovsrec_table_maintenance_point,
1993 {{NULL, NULL, NULL},
1994 {NULL, NULL, NULL}}},
1995
c1c9c9c4
BP
1996 {&ovsrec_table_queue,
1997 {{NULL, NULL, NULL},
1998 {NULL, NULL, NULL}}},
1999
bd76d25d 2000 {&ovsrec_table_ssl,
ad83bfa6
BP
2001 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2002
d01600a2
BP
2003 {&ovsrec_table_sflow,
2004 {{&ovsrec_table_bridge,
2005 &ovsrec_bridge_col_name,
2006 &ovsrec_bridge_col_sflow},
2007 {NULL, NULL, NULL}}},
2008
bd76d25d 2009 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
ad83bfa6
BP
2010};
2011
1bc6ff29
BP
2012static void
2013die_if_error(char *error)
2014{
2015 if (error) {
def90f62 2016 vsctl_fatal("%s", error);
1bc6ff29
BP
2017 }
2018}
2019
ad83bfa6
BP
2020static int
2021to_lower_and_underscores(unsigned c)
2022{
2023 return c == '-' ? '_' : tolower(c);
2024}
2025
2026static unsigned int
2027score_partial_match(const char *name, const char *s)
2028{
2029 int score;
2030
5128bd9c
BP
2031 if (!strcmp(name, s)) {
2032 return UINT_MAX;
2033 }
ad83bfa6
BP
2034 for (score = 0; ; score++, name++, s++) {
2035 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2036 break;
2037 } else if (*name == '\0') {
5128bd9c 2038 return UINT_MAX - 1;
ad83bfa6
BP
2039 }
2040 }
2041 return *s == '\0' ? score : 0;
2042}
2043
2044static const struct vsctl_table_class *
2045get_table(const char *table_name)
2046{
2047 const struct vsctl_table_class *table;
2048 const struct vsctl_table_class *best_match = NULL;
2049 unsigned int best_score = 0;
2050
2051 for (table = tables; table->class; table++) {
2052 unsigned int score = score_partial_match(table->class->name,
2053 table_name);
2054 if (score > best_score) {
2055 best_match = table;
2056 best_score = score;
2057 } else if (score == best_score) {
2058 best_match = NULL;
2059 }
2060 }
2061 if (best_match) {
2062 return best_match;
2063 } else if (best_score) {
def90f62 2064 vsctl_fatal("multiple table names match \"%s\"", table_name);
ad83bfa6 2065 } else {
def90f62 2066 vsctl_fatal("unknown table \"%s\"", table_name);
ad83bfa6
BP
2067 }
2068}
2069
e5e12280
BP
2070static const struct vsctl_table_class *
2071pre_get_table(struct vsctl_context *ctx, const char *table_name)
2072{
2073 const struct vsctl_table_class *table_class;
2074 int i;
2075
2076 table_class = get_table(table_name);
2077 ovsdb_idl_add_table(ctx->idl, table_class->class);
2078
2079 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2080 const struct vsctl_row_id *id = &table_class->row_ids[i];
2081 if (id->table) {
2082 ovsdb_idl_add_table(ctx->idl, id->table);
2083 }
2084 if (id->name_column) {
2085 ovsdb_idl_add_column(ctx->idl, id->name_column);
2086 }
2087 if (id->uuid_column) {
2088 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2089 }
2090 }
2091
2092 return table_class;
2093}
2094
ad83bfa6
BP
2095static const struct ovsdb_idl_row *
2096get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
e111e681 2097 const struct vsctl_row_id *id, const char *record_id)
ad83bfa6
BP
2098{
2099 const struct ovsdb_idl_row *referrer, *final;
2100
2101 if (!id->table) {
2102 return NULL;
2103 }
2104
2105 if (!id->name_column) {
2106 if (strcmp(record_id, ".")) {
2107 return NULL;
2108 }
2109 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2110 if (!referrer || ovsdb_idl_next_row(referrer)) {
2111 return NULL;
2112 }
2113 } else {
2114 const struct ovsdb_idl_row *row;
ad83bfa6 2115
ad83bfa6
BP
2116 referrer = NULL;
2117 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
e111e681 2118 row != NULL;
ad83bfa6
BP
2119 row = ovsdb_idl_next_row(row))
2120 {
8c3c2f30 2121 const struct ovsdb_datum *name;
ad83bfa6 2122
8c3c2f30
BP
2123 name = ovsdb_idl_get(row, id->name_column,
2124 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
e111e681
BP
2125 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2126 if (referrer) {
2127 vsctl_fatal("multiple rows in %s match \"%s\"",
2128 table->class->name, record_id);
ad83bfa6 2129 }
e111e681 2130 referrer = row;
ad83bfa6 2131 }
ad83bfa6
BP
2132 }
2133 }
2134 if (!referrer) {
2135 return NULL;
2136 }
2137
93255bc5 2138 final = NULL;
ad83bfa6 2139 if (id->uuid_column) {
8c3c2f30 2140 const struct ovsdb_datum *uuid;
ad83bfa6 2141
f74055e7 2142 ovsdb_idl_txn_verify(referrer, id->uuid_column);
8c3c2f30
BP
2143 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2144 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2145 if (uuid->n == 1) {
ad83bfa6 2146 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
8c3c2f30 2147 &uuid->keys[0].uuid);
ad83bfa6 2148 }
ad83bfa6
BP
2149 } else {
2150 final = referrer;
2151 }
2152
2153 return final;
2154}
2155
2156static const struct ovsdb_idl_row *
e111e681
BP
2157get_row (struct vsctl_context *ctx,
2158 const struct vsctl_table_class *table, const char *record_id)
ad83bfa6
BP
2159{
2160 const struct ovsdb_idl_row *row;
2161 struct uuid uuid;
2162
2163 if (uuid_from_string(&uuid, record_id)) {
2164 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2165 } else {
2166 int i;
2167
2168 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
e111e681 2169 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
ad83bfa6
BP
2170 if (row) {
2171 break;
2172 }
2173 }
2174 }
b7f74b6f
BP
2175 return row;
2176}
2177
2178static const struct ovsdb_idl_row *
2179must_get_row(struct vsctl_context *ctx,
2180 const struct vsctl_table_class *table, const char *record_id)
2181{
2182 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
ad83bfa6 2183 if (!row) {
def90f62
BP
2184 vsctl_fatal("no row \"%s\" in table %s",
2185 record_id, table->class->name);
ad83bfa6
BP
2186 }
2187 return row;
2188}
2189
1bc6ff29
BP
2190static char *
2191get_column(const struct vsctl_table_class *table, const char *column_name,
bd76d25d 2192 const struct ovsdb_idl_column **columnp)
ad83bfa6 2193{
bd76d25d 2194 const struct ovsdb_idl_column *best_match = NULL;
ad83bfa6 2195 unsigned int best_score = 0;
bd76d25d 2196 size_t i;
ad83bfa6 2197
bd76d25d
BP
2198 for (i = 0; i < table->class->n_columns; i++) {
2199 const struct ovsdb_idl_column *column = &table->class->columns[i];
2200 unsigned int score = score_partial_match(column->name, column_name);
2201 if (score > best_score) {
2202 best_match = column;
2203 best_score = score;
2204 } else if (score == best_score) {
2205 best_match = NULL;
ad83bfa6
BP
2206 }
2207 }
1bc6ff29
BP
2208
2209 *columnp = best_match;
ad83bfa6 2210 if (best_match) {
1bc6ff29 2211 return NULL;
ad83bfa6 2212 } else if (best_score) {
1bc6ff29
BP
2213 return xasprintf("%s contains more than one column whose name "
2214 "matches \"%s\"", table->class->name, column_name);
ad83bfa6 2215 } else {
1bc6ff29
BP
2216 return xasprintf("%s does not contain a column whose name matches "
2217 "\"%s\"", table->class->name, column_name);
ad83bfa6
BP
2218 }
2219}
2220
aed133bf
BP
2221static struct uuid *
2222create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2223{
2224 struct ovsdb_symbol *symbol;
2225
2226 if (id[0] != '@') {
2227 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2228 }
2229
2230 if (newp) {
2231 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2232 }
2233
2234 symbol = ovsdb_symbol_table_insert(symtab, id);
2235 if (symbol->used) {
2236 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2237 id);
2238 }
2239 symbol->used = true;
2240 return &symbol->uuid;
2241}
2242
e5e12280
BP
2243static void
2244pre_get_column(struct vsctl_context *ctx,
2245 const struct vsctl_table_class *table, const char *column_name,
2246 const struct ovsdb_idl_column **columnp)
2247{
2248 die_if_error(get_column(table, column_name, columnp));
2249 ovsdb_idl_add_column(ctx->idl, *columnp);
2250}
2251
e89e5374
BP
2252static char *
2253missing_operator_error(const char *arg, const char **allowed_operators,
2254 size_t n_allowed)
2255{
2256 struct ds s;
2257
2258 ds_init(&s);
2259 ds_put_format(&s, "%s: argument does not end in ", arg);
2260 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2261 if (n_allowed == 2) {
2262 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2263 } else if (n_allowed > 2) {
2264 size_t i;
2265
2266 for (i = 1; i < n_allowed - 1; i++) {
2267 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2268 }
2269 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2270 }
2271 ds_put_format(&s, " followed by a value.");
2272
2273 return ds_steal_cstr(&s);
2274}
2275
2276/* Breaks 'arg' apart into a number of fields in the following order:
2277 *
2278 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2279 * is stored into '*columnp'. The column name may be abbreviated.
2280 *
2281 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2282 * and 'keyp' are nonnull, then the column and key names are expected to
2283 * be separated by ':'). The key is stored as a malloc()'d string into
2284 * '*keyp', or NULL if no key is present in 'arg'.
2285 *
2286 * - If 'valuep' is nonnull, an operator followed by a value string. The
2287 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2288 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2289 * operator is stored into '*operatorp' (one of the pointers from
2290 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2291 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2292 * present in 'arg'.
2293 *
2294 * At least 'columnp' or 'keyp' must be nonnull.
2295 *
2296 * On success, returns NULL. On failure, returned a malloc()'d string error
2297 * message and stores NULL into all of the nonnull output arguments. */
1bc6ff29 2298static char * WARN_UNUSED_RESULT
e89e5374
BP
2299parse_column_key_value(const char *arg,
2300 const struct vsctl_table_class *table,
2301 const struct ovsdb_idl_column **columnp, char **keyp,
2302 const char **operatorp,
2303 const char **allowed_operators, size_t n_allowed,
2304 char **valuep)
ad83bfa6
BP
2305{
2306 const char *p = arg;
1bc6ff29 2307 char *error;
ad83bfa6
BP
2308
2309 assert(columnp || keyp);
e89e5374 2310 assert(!(operatorp && !valuep));
1bc6ff29
BP
2311 if (keyp) {
2312 *keyp = NULL;
2313 }
2314 if (valuep) {
2315 *valuep = NULL;
2316 }
ad83bfa6
BP
2317
2318 /* Parse column name. */
2319 if (columnp) {
2320 char *column_name;
2321
557e3718 2322 error = ovsdb_token_parse(&p, &column_name);
1bc6ff29
BP
2323 if (error) {
2324 goto error;
2325 }
ad83bfa6 2326 if (column_name[0] == '\0') {
1bc6ff29
BP
2327 free(column_name);
2328 error = xasprintf("%s: missing column name", arg);
2329 goto error;
2330 }
2331 error = get_column(table, column_name, columnp);
a3326252 2332 free(column_name);
1bc6ff29
BP
2333 if (error) {
2334 goto error;
ad83bfa6 2335 }
ad83bfa6
BP
2336 }
2337
2338 /* Parse key string. */
2339 if (*p == ':' || !columnp) {
2340 if (columnp) {
2341 p++;
2342 } else if (!keyp) {
1bc6ff29
BP
2343 error = xasprintf("%s: key not accepted here", arg);
2344 goto error;
2345 }
2346 error = ovsdb_token_parse(&p, keyp);
2347 if (error) {
2348 goto error;
ad83bfa6 2349 }
ad83bfa6
BP
2350 } else if (keyp) {
2351 *keyp = NULL;
2352 }
2353
2354 /* Parse value string. */
e89e5374
BP
2355 if (valuep) {
2356 const char *best;
2357 size_t best_len;
2358 size_t i;
2359
2360 if (!allowed_operators) {
2361 static const char *equals = "=";
2362 allowed_operators = &equals;
2363 n_allowed = 1;
2364 }
2365
2366 best = NULL;
2367 best_len = 0;
2368 for (i = 0; i < n_allowed; i++) {
2369 const char *op = allowed_operators[i];
2370 size_t op_len = strlen(op);
2371
7db03f7c 2372 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
e89e5374
BP
2373 best_len = op_len;
2374 best = op;
2375 }
2376 }
2377 if (!best) {
2378 error = missing_operator_error(arg, allowed_operators, n_allowed);
1bc6ff29 2379 goto error;
ad83bfa6 2380 }
e89e5374
BP
2381
2382 if (operatorp) {
2383 *operatorp = best;
2384 }
7db03f7c 2385 *valuep = xstrdup(p + best_len);
ad83bfa6
BP
2386 } else {
2387 if (valuep) {
2388 *valuep = NULL;
2389 }
1bc6ff29 2390 if (*p != '\0') {
c29a8ba8
BP
2391 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2392 arg, p);
1bc6ff29
BP
2393 goto error;
2394 }
2395 }
2396 return NULL;
2397
2398error:
2399 if (columnp) {
2400 *columnp = NULL;
2401 }
2402 if (keyp) {
2403 free(*keyp);
2404 *keyp = NULL;
2405 }
2406 if (valuep) {
2407 free(*valuep);
2408 *valuep = NULL;
e89e5374
BP
2409 if (operatorp) {
2410 *operatorp = NULL;
2411 }
ad83bfa6 2412 }
1bc6ff29 2413 return error;
ad83bfa6
BP
2414}
2415
e5e12280
BP
2416static void
2417pre_parse_column_key_value(struct vsctl_context *ctx,
2418 const char *arg,
2419 const struct vsctl_table_class *table)
2420{
2421 const struct ovsdb_idl_column *column;
2422 const char *p;
2423 char *column_name;
2424
2425 p = arg;
2426 die_if_error(ovsdb_token_parse(&p, &column_name));
2427 if (column_name[0] == '\0') {
2428 vsctl_fatal("%s: missing column name", arg);
2429 }
2430
2431 pre_get_column(ctx, table, column_name, &column);
2432 free(column_name);
2433}
2434
2435static void
2436pre_cmd_get(struct vsctl_context *ctx)
2437{
2438 const char *table_name = ctx->argv[1];
2439 const struct vsctl_table_class *table;
2440 int i;
2441
2442 table = pre_get_table(ctx, table_name);
2443 for (i = 3; i < ctx->argc; i++) {
2444 if (!strcasecmp(ctx->argv[i], "_uuid")
2445 || !strcasecmp(ctx->argv[i], "-uuid")) {
2446 continue;
2447 }
2448
2449 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2450 }
2451}
2452
ad83bfa6
BP
2453static void
2454cmd_get(struct vsctl_context *ctx)
2455{
aed133bf 2456 const char *id = shash_find_data(&ctx->options, "--id");
870aeb4a 2457 bool if_exists = shash_find(&ctx->options, "--if-exists");
ad83bfa6
BP
2458 const char *table_name = ctx->argv[1];
2459 const char *record_id = ctx->argv[2];
2460 const struct vsctl_table_class *table;
2461 const struct ovsdb_idl_row *row;
2462 struct ds *out = &ctx->output;
2463 int i;
2464
2465 table = get_table(table_name);
b7f74b6f 2466 row = must_get_row(ctx, table, record_id);
aed133bf
BP
2467 if (id) {
2468 bool new;
2469
2470 *create_symbol(ctx->symtab, id, &new) = row->uuid;
2471 if (!new) {
2472 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2473 "before it was defined", id);
2474 }
2475 }
ad83bfa6 2476 for (i = 3; i < ctx->argc; i++) {
bd76d25d 2477 const struct ovsdb_idl_column *column;
8c3c2f30 2478 const struct ovsdb_datum *datum;
ad83bfa6
BP
2479 char *key_string;
2480
f40a9b61
BP
2481 /* Special case for obtaining the UUID of a row. We can't just do this
2482 * through parse_column_key_value() below since it returns a "struct
2483 * ovsdb_idl_column" and the UUID column doesn't have one. */
2484 if (!strcasecmp(ctx->argv[i], "_uuid")
2485 || !strcasecmp(ctx->argv[i], "-uuid")) {
2486 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2487 continue;
2488 }
2489
1bc6ff29 2490 die_if_error(parse_column_key_value(ctx->argv[i], table,
e89e5374
BP
2491 &column, &key_string,
2492 NULL, NULL, 0, NULL));
ad83bfa6 2493
f74055e7 2494 ovsdb_idl_txn_verify(row, column);
8c3c2f30 2495 datum = ovsdb_idl_read(row, column);
ad83bfa6
BP
2496 if (key_string) {
2497 union ovsdb_atom key;
2498 unsigned int idx;
2499
bd76d25d 2500 if (column->type.value.type == OVSDB_TYPE_VOID) {
def90f62 2501 vsctl_fatal("cannot specify key to get for non-map column %s",
bd76d25d 2502 column->name);
ad83bfa6
BP
2503 }
2504
1bc6ff29 2505 die_if_error(ovsdb_atom_from_string(&key,
bd76d25d 2506 &column->type.key,
ce5a3e38 2507 key_string, ctx->symtab));
ad83bfa6 2508
8c3c2f30 2509 idx = ovsdb_datum_find_key(datum, &key,
bd76d25d 2510 column->type.key.type);
ad83bfa6 2511 if (idx == UINT_MAX) {
870aeb4a 2512 if (!if_exists) {
def90f62
BP
2513 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2514 key_string, table->class->name, record_id,
bd76d25d 2515 column->name);
870aeb4a
BP
2516 }
2517 } else {
8c3c2f30 2518 ovsdb_atom_to_string(&datum->values[idx],
bd76d25d 2519 column->type.value.type, out);
ad83bfa6 2520 }
bd76d25d 2521 ovsdb_atom_destroy(&key, column->type.key.type);
ad83bfa6 2522 } else {
8c3c2f30 2523 ovsdb_datum_to_string(datum, &column->type, out);
ad83bfa6
BP
2524 }
2525 ds_put_char(out, '\n');
ad83bfa6
BP
2526
2527 free(key_string);
2528 }
2529}
2530
e5e12280
BP
2531static void
2532pre_cmd_list(struct vsctl_context *ctx)
2533{
2534 const char *table_name = ctx->argv[1];
2535 const struct vsctl_table_class *table;
2536 size_t i;
2537
2538 table = pre_get_table(ctx, table_name);
2539 for (i = 0; i < table->class->n_columns; i++) {
2540 ovsdb_idl_add_column(ctx->idl, &table->class->columns[i]);
2541 }
2542}
2543
ad83bfa6 2544static void
1bc6ff29
BP
2545list_record(const struct vsctl_table_class *table,
2546 const struct ovsdb_idl_row *row, struct ds *out)
ad83bfa6 2547{
bd76d25d 2548 size_t i;
ad83bfa6 2549
bd76d25d 2550 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
c611c9d0 2551 UUID_ARGS(&row->uuid));
bd76d25d
BP
2552 for (i = 0; i < table->class->n_columns; i++) {
2553 const struct ovsdb_idl_column *column = &table->class->columns[i];
8c3c2f30 2554 const struct ovsdb_datum *datum;
ad83bfa6 2555
8c3c2f30 2556 datum = ovsdb_idl_read(row, column);
ad83bfa6 2557
bd76d25d 2558 ds_put_format(out, "%-20s: ", column->name);
8c3c2f30 2559 ovsdb_datum_to_string(datum, &column->type, out);
ad83bfa6 2560 ds_put_char(out, '\n');
ad83bfa6
BP
2561 }
2562}
2563
2564static void
2565cmd_list(struct vsctl_context *ctx)
2566{
2567 const char *table_name = ctx->argv[1];
2568 const struct vsctl_table_class *table;
2569 struct ds *out = &ctx->output;
2570 int i;
2571
2572 table = get_table(table_name);
2573 if (ctx->argc > 2) {
2574 for (i = 2; i < ctx->argc; i++) {
2575 if (i > 2) {
2576 ds_put_char(out, '\n');
2577 }
b7f74b6f 2578 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
ad83bfa6
BP
2579 }
2580 } else {
2581 const struct ovsdb_idl_row *row;
2582 bool first;
2583
2584 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2585 row != NULL;
2586 row = ovsdb_idl_next_row(row), first = false) {
2587 if (!first) {
2588 ds_put_char(out, '\n');
2589 }
2590 list_record(table, row, out);
2591 }
2592 }
2593}
2594
e5e12280
BP
2595static void
2596pre_cmd_set(struct vsctl_context *ctx)
2597{
2598 const char *table_name = ctx->argv[1];
2599 const struct vsctl_table_class *table;
2600 int i;
2601
2602 table = pre_get_table(ctx, table_name);
2603 for (i = 3; i < ctx->argc; i++) {
2604 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2605 }
2606}
2607
ad83bfa6 2608static void
557e3718 2609set_column(const struct vsctl_table_class *table,
ce5a3e38
BP
2610 const struct ovsdb_idl_row *row, const char *arg,
2611 struct ovsdb_symbol_table *symtab)
ad83bfa6 2612{
bd76d25d 2613 const struct ovsdb_idl_column *column;
557e3718
BP
2614 char *key_string, *value_string;
2615 char *error;
ad83bfa6 2616
557e3718 2617 error = parse_column_key_value(arg, table, &column, &key_string,
e89e5374 2618 NULL, NULL, 0, &value_string);
557e3718 2619 die_if_error(error);
557e3718 2620 if (!value_string) {
def90f62 2621 vsctl_fatal("%s: missing value", arg);
557e3718 2622 }
ad83bfa6 2623
557e3718
BP
2624 if (key_string) {
2625 union ovsdb_atom key, value;
8c3c2f30 2626 struct ovsdb_datum datum;
557e3718 2627
bd76d25d 2628 if (column->type.value.type == OVSDB_TYPE_VOID) {
def90f62 2629 vsctl_fatal("cannot specify key to set for non-map column %s",
bd76d25d 2630 column->name);
ad83bfa6
BP
2631 }
2632
bd76d25d 2633 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
ce5a3e38 2634 key_string, symtab));
bd76d25d 2635 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
ce5a3e38 2636 value_string, symtab));
ad83bfa6 2637
8c3c2f30
BP
2638 ovsdb_datum_init_empty(&datum);
2639 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
ad83bfa6 2640
bd76d25d
BP
2641 ovsdb_atom_destroy(&key, column->type.key.type);
2642 ovsdb_atom_destroy(&value, column->type.value.type);
a3326252 2643
8c3c2f30
BP
2644 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2645 &column->type, false);
2646 ovsdb_idl_txn_write(row, column, &datum);
557e3718
BP
2647 } else {
2648 struct ovsdb_datum datum;
ad83bfa6 2649
bd76d25d 2650 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
ce5a3e38 2651 value_string, symtab));
bd76d25d 2652 ovsdb_idl_txn_write(row, column, &datum);
557e3718 2653 }
ad83bfa6 2654
557e3718 2655 free(key_string);
a3326252 2656 free(value_string);
557e3718 2657}
ad83bfa6 2658
557e3718
BP
2659static void
2660cmd_set(struct vsctl_context *ctx)
2661{
557e3718
BP
2662 const char *table_name = ctx->argv[1];
2663 const char *record_id = ctx->argv[2];
2664 const struct vsctl_table_class *table;
2665 const struct ovsdb_idl_row *row;
2666 int i;
ad83bfa6 2667
557e3718 2668 table = get_table(table_name);
b7f74b6f 2669 row = must_get_row(ctx, table, record_id);
557e3718 2670 for (i = 3; i < ctx->argc; i++) {
ce5a3e38 2671 set_column(table, row, ctx->argv[i], ctx->symtab);
ad83bfa6
BP
2672 }
2673}
2674
e5e12280
BP
2675static void
2676pre_cmd_add(struct vsctl_context *ctx)
2677{
2678 const char *table_name = ctx->argv[1];
2679 const char *column_name = ctx->argv[3];
2680 const struct vsctl_table_class *table;
2681 const struct ovsdb_idl_column *column;
2682
2683 table = pre_get_table(ctx, table_name);
2684 pre_get_column(ctx, table, column_name, &column);
2685}
2686
ad83bfa6
BP
2687static void
2688cmd_add(struct vsctl_context *ctx)
2689{
2690 const char *table_name = ctx->argv[1];
2691 const char *record_id = ctx->argv[2];
2692 const char *column_name = ctx->argv[3];
2693 const struct vsctl_table_class *table;
bd76d25d 2694 const struct ovsdb_idl_column *column;
ad83bfa6
BP
2695 const struct ovsdb_idl_row *row;
2696 const struct ovsdb_type *type;
2697 struct ovsdb_datum old;
2698 int i;
2699
2700 table = get_table(table_name);
b7f74b6f 2701 row = must_get_row(ctx, table, record_id);
1bc6ff29 2702 die_if_error(get_column(table, column_name, &column));
c29a8ba8 2703
bd76d25d 2704 type = &column->type;
8c3c2f30 2705 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
ad83bfa6
BP
2706 for (i = 4; i < ctx->argc; i++) {
2707 struct ovsdb_type add_type;
2708 struct ovsdb_datum add;
2709
ad83bfa6
BP
2710 add_type = *type;
2711 add_type.n_min = 1;
2712 add_type.n_max = UINT_MAX;
ce5a3e38
BP
2713 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2714 ctx->symtab));
ad83bfa6
BP
2715 ovsdb_datum_union(&old, &add, type, false);
2716 ovsdb_datum_destroy(&add, type);
2717 }
2718 if (old.n > type->n_max) {
def90f62
BP
2719 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2720 "table %s but the maximum number is %u",
2721 old.n,
bd76d25d
BP
2722 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2723 column->name, table->class->name, type->n_max);
ad83bfa6 2724 }
f74055e7 2725 ovsdb_idl_txn_verify(row, column);
bd76d25d 2726 ovsdb_idl_txn_write(row, column, &old);
ad83bfa6 2727}
90c4bd00 2728
e5e12280
BP
2729static void
2730pre_cmd_remove(struct vsctl_context *ctx)
2731{
2732 const char *table_name = ctx->argv[1];
2733 const char *column_name = ctx->argv[3];
2734 const struct vsctl_table_class *table;
2735 const struct ovsdb_idl_column *column;
2736
2737 table = pre_get_table(ctx, table_name);
2738 pre_get_column(ctx, table, column_name, &column);
2739}
2740
90c4bd00
BP
2741static void
2742cmd_remove(struct vsctl_context *ctx)
2743{
2744 const char *table_name = ctx->argv[1];
2745 const char *record_id = ctx->argv[2];
2746 const char *column_name = ctx->argv[3];
2747 const struct vsctl_table_class *table;
bd76d25d 2748 const struct ovsdb_idl_column *column;
90c4bd00
BP
2749 const struct ovsdb_idl_row *row;
2750 const struct ovsdb_type *type;
2751 struct ovsdb_datum old;
2752 int i;
2753
2754 table = get_table(table_name);
b7f74b6f 2755 row = must_get_row(ctx, table, record_id);
90c4bd00 2756 die_if_error(get_column(table, column_name, &column));
c29a8ba8 2757
bd76d25d 2758 type = &column->type;
8c3c2f30 2759 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
90c4bd00
BP
2760 for (i = 4; i < ctx->argc; i++) {
2761 struct ovsdb_type rm_type;
2762 struct ovsdb_datum rm;
2763 char *error;
2764
90c4bd00
BP
2765 rm_type = *type;
2766 rm_type.n_min = 1;
2767 rm_type.n_max = UINT_MAX;
ce5a3e38
BP
2768 error = ovsdb_datum_from_string(&rm, &rm_type,
2769 ctx->argv[i], ctx->symtab);
90c4bd00
BP
2770 if (error && ovsdb_type_is_map(&rm_type)) {
2771 free(error);
bd76d25d 2772 rm_type.value.type = OVSDB_TYPE_VOID;
ce5a3e38
BP
2773 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2774 ctx->argv[i], ctx->symtab));
90c4bd00
BP
2775 }
2776 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2777 ovsdb_datum_destroy(&rm, &rm_type);
2778 }
2779 if (old.n < type->n_min) {
def90f62 2780 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
f19f25a4 2781 "table %s but the minimum number is %u",
def90f62 2782 old.n,
bd76d25d
BP
2783 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2784 column->name, table->class->name, type->n_min);
90c4bd00 2785 }
f74055e7 2786 ovsdb_idl_txn_verify(row, column);
bd76d25d 2787 ovsdb_idl_txn_write(row, column, &old);
90c4bd00
BP
2788}
2789
e5e12280
BP
2790static void
2791pre_cmd_clear(struct vsctl_context *ctx)
2792{
2793 const char *table_name = ctx->argv[1];
2794 const struct vsctl_table_class *table;
2795 int i;
2796
2797 table = pre_get_table(ctx, table_name);
2798 for (i = 3; i < ctx->argc; i++) {
2799 const struct ovsdb_idl_column *column;
2800
2801 pre_get_column(ctx, table, ctx->argv[i], &column);
2802 }
2803}
2804
90c4bd00
BP
2805static void
2806cmd_clear(struct vsctl_context *ctx)
2807{
2808 const char *table_name = ctx->argv[1];
2809 const char *record_id = ctx->argv[2];
2810 const struct vsctl_table_class *table;
2811 const struct ovsdb_idl_row *row;
2812 int i;
2813
2814 table = get_table(table_name);
b7f74b6f 2815 row = must_get_row(ctx, table, record_id);
90c4bd00 2816 for (i = 3; i < ctx->argc; i++) {
bd76d25d 2817 const struct ovsdb_idl_column *column;
90c4bd00
BP
2818 const struct ovsdb_type *type;
2819 struct ovsdb_datum datum;
2820
2821 die_if_error(get_column(table, ctx->argv[i], &column));
2822
bd76d25d
BP
2823 type = &column->type;
2824 if (type->n_min > 0) {
def90f62
BP
2825 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2826 "of table %s, which is not allowed to be empty",
bd76d25d 2827 column->name, table->class->name);
90c4bd00
BP
2828 }
2829
2830 ovsdb_datum_init_empty(&datum);
bd76d25d 2831 ovsdb_idl_txn_write(row, column, &datum);
90c4bd00
BP
2832 }
2833}
557e3718
BP
2834
2835static void
2836cmd_create(struct vsctl_context *ctx)
2837{
ce5a3e38 2838 const char *id = shash_find_data(&ctx->options, "--id");
557e3718
BP
2839 const char *table_name = ctx->argv[1];
2840 const struct vsctl_table_class *table;
2841 const struct ovsdb_idl_row *row;
ce5a3e38 2842 const struct uuid *uuid;
557e3718
BP
2843 int i;
2844
aed133bf 2845 uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL;
ce5a3e38 2846
557e3718 2847 table = get_table(table_name);
ce5a3e38 2848 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
557e3718 2849 for (i = 2; i < ctx->argc; i++) {
ce5a3e38 2850 set_column(table, row, ctx->argv[i], ctx->symtab);
557e3718 2851 }
f8ff4bc4 2852 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
557e3718 2853}
b7f74b6f 2854
3ef917b5
BP
2855/* This function may be used as the 'postprocess' function for commands that
2856 * insert new rows into the database. It expects that the command's 'run'
2857 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2858 * sole output. It replaces that output by the row's permanent UUID assigned
2859 * by the database server and appends a new-line.
2860 *
2861 * Currently we use this only for "create", because the higher-level commands
2862 * are supposed to be independent of the actual structure of the vswitch
2863 * configuration. */
2864static void
2865post_create(struct vsctl_context *ctx)
2866{
2867 const struct uuid *real;
2868 struct uuid dummy;
2869
2870 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2871 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2872 if (real) {
2873 ds_clear(&ctx->output);
2874 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2875 }
2876 ds_put_char(&ctx->output, '\n');
2877}
2878
e5e12280
BP
2879static void
2880pre_cmd_destroy(struct vsctl_context *ctx)
2881{
2882 const char *table_name = ctx->argv[1];
2883
2884 pre_get_table(ctx, table_name);
2885}
2886
b7f74b6f
BP
2887static void
2888cmd_destroy(struct vsctl_context *ctx)
2889{
b7f74b6f
BP
2890 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2891 const char *table_name = ctx->argv[1];
2892 const struct vsctl_table_class *table;
2893 int i;
2894
b7f74b6f 2895 table = get_table(table_name);
f8ff4bc4 2896 for (i = 2; i < ctx->argc; i++) {
b7f74b6f
BP
2897 const struct ovsdb_idl_row *row;
2898
2899 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2900 if (row) {
2901 ovsdb_idl_txn_delete(row);
2902 }
2903 }
2904}
7db03f7c
BP
2905
2906static bool
2907is_condition_satified(const struct vsctl_table_class *table,
2908 const struct ovsdb_idl_row *row, const char *arg,
2909 struct ovsdb_symbol_table *symtab)
2910{
2911 static const char *operators[] = {
2912 "=", "!=", "<", ">", "<=", ">="
2913 };
2914
2915 const struct ovsdb_idl_column *column;
8c3c2f30 2916 const struct ovsdb_datum *have_datum;
7db03f7c 2917 char *key_string, *value_string;
7db03f7c
BP
2918 const char *operator;
2919 unsigned int idx;
2920 char *error;
af9af3e2 2921 int cmp = 0;
7db03f7c
BP
2922
2923 error = parse_column_key_value(arg, table, &column, &key_string,
2924 &operator, operators, ARRAY_SIZE(operators),
2925 &value_string);
2926 die_if_error(error);
2927 if (!value_string) {
2928 vsctl_fatal("%s: missing value", arg);
2929 }
2930
8c3c2f30 2931 have_datum = ovsdb_idl_read(row, column);
7db03f7c
BP
2932 if (key_string) {
2933 union ovsdb_atom want_key, want_value;
2934
2935 if (column->type.value.type == OVSDB_TYPE_VOID) {
2936 vsctl_fatal("cannot specify key to check for non-map column %s",
2937 column->name);
2938 }
2939
2940 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2941 key_string, symtab));
2942 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2943 value_string, symtab));
2944
8c3c2f30 2945 idx = ovsdb_datum_find_key(have_datum,
7db03f7c
BP
2946 &want_key, column->type.key.type);
2947 if (idx != UINT_MAX) {
8c3c2f30 2948 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
7db03f7c
BP
2949 &want_value,
2950 column->type.value.type);
2951 }
2952
2953 ovsdb_atom_destroy(&want_key, column->type.key.type);
2954 ovsdb_atom_destroy(&want_value, column->type.value.type);
2955 } else {
2956 struct ovsdb_datum want_datum;
2957
2958 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2959 value_string, symtab));
2960 idx = 0;
8c3c2f30 2961 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
7db03f7c
BP
2962 &column->type);
2963 ovsdb_datum_destroy(&want_datum, &column->type);
2964 }
7db03f7c
BP
2965
2966 free(key_string);
2967 free(value_string);
2968
2969 return (idx == UINT_MAX ? false
2970 : !strcmp(operator, "=") ? cmp == 0
2971 : !strcmp(operator, "!=") ? cmp != 0
2972 : !strcmp(operator, "<") ? cmp < 0
2973 : !strcmp(operator, ">") ? cmp > 0
2974 : !strcmp(operator, "<=") ? cmp <= 0
2975 : !strcmp(operator, ">=") ? cmp >= 0
2976 : (abort(), 0));
2977}
2978
e5e12280
BP
2979static void
2980pre_cmd_wait_until(struct vsctl_context *ctx)
2981{
2982 const char *table_name = ctx->argv[1];
2983 const struct vsctl_table_class *table;
2984 int i;
2985
2986 table = pre_get_table(ctx, table_name);
2987
2988 for (i = 3; i < ctx->argc; i++) {
2989 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2990 }
2991}
2992
7db03f7c
BP
2993static void
2994cmd_wait_until(struct vsctl_context *ctx)
2995{
2996 const char *table_name = ctx->argv[1];
2997 const char *record_id = ctx->argv[2];
2998 const struct vsctl_table_class *table;
2999 const struct ovsdb_idl_row *row;
3000 int i;
3001
3002 table = get_table(table_name);
3003
e111e681 3004 row = get_row(ctx, table, record_id);
7db03f7c
BP
3005 if (!row) {
3006 ctx->try_again = true;
3007 return;
3008 }
3009
3010 for (i = 3; i < ctx->argc; i++) {
3011 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
3012 ctx->try_again = true;
3013 return;
3014 }
3015 }
3016}
ad83bfa6 3017\f
b54e22e9
BP
3018static struct json *
3019where_uuid_equals(const struct uuid *uuid)
3020{
3021 return
3022 json_array_create_1(
3023 json_array_create_3(
3024 json_string_create("_uuid"),
3025 json_string_create("=="),
3026 json_array_create_2(
3027 json_string_create("uuid"),
3028 json_string_create_nocopy(
3029 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3030}
3031
c75d1511 3032static void
f8ff4bc4
BP
3033vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3034 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
ce5a3e38 3035 const struct ovsrec_open_vswitch *ovs,
e5e12280 3036 struct ovsdb_symbol_table *symtab)
f8ff4bc4
BP
3037{
3038 ctx->argc = command->argc;
3039 ctx->argv = command->argv;
3040 ctx->options = command->options;
3041
3042 ds_swap(&ctx->output, &command->output);
3043 ctx->idl = idl;
3044 ctx->txn = txn;
3045 ctx->ovs = ovs;
ce5a3e38 3046 ctx->symtab = symtab;
f74055e7 3047 ctx->verified_ports = false;
87b23a01
BP
3048
3049 ctx->try_again = false;
f8ff4bc4
BP
3050}
3051
3052static void
3053vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3054{
3055 ds_swap(&ctx->output, &command->output);
3056}
3057
e5e12280
BP
3058static void
3059run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3060 struct ovsdb_idl *idl)
3061{
3062 struct vsctl_command *c;
3063
3064 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
b8fa7102
BP
3065 if (wait_for_reload) {
3066 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3067 }
e5e12280
BP
3068 for (c = commands; c < &commands[n_commands]; c++) {
3069 if (c->syntax->prerequisites) {
3070 struct vsctl_context ctx;
3071
3072 ds_init(&c->output);
3073
3074 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3075 (c->syntax->prerequisites)(&ctx);
3076 vsctl_context_done(&ctx, c);
3077
3078 assert(!c->output.string);
3079 }
3080 }
3081}
3082
f8ff4bc4
BP
3083static void
3084do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3085 struct ovsdb_idl *idl)
c75d1511
BP
3086{
3087 struct ovsdb_idl_txn *txn;
3088 const struct ovsrec_open_vswitch *ovs;
3089 enum ovsdb_idl_txn_status status;
ce5a3e38
BP
3090 struct ovsdb_symbol_table *symtab;
3091 const char *unused;
f8ff4bc4 3092 struct vsctl_command *c;
84a0ee89 3093 int64_t next_cfg = 0;
af9af3e2 3094 char *error = NULL;
c75d1511 3095
1d48b4be 3096 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
577aebdf
BP
3097 if (dry_run) {
3098 ovsdb_idl_txn_set_dry_run(txn);
3099 }
524555d1 3100
e1c0e2d1 3101 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
d171b584 3102
c75d1511
BP
3103 ovs = ovsrec_open_vswitch_first(idl);
3104 if (!ovs) {
524555d1
BP
3105 /* XXX add verification that table is empty */
3106 ovs = ovsrec_open_vswitch_insert(txn);
c75d1511
BP
3107 }
3108
b54e22e9
BP
3109 if (wait_for_reload) {
3110 struct json *where = where_uuid_equals(&ovs->header_.uuid);
ad83bfa6 3111 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
b54e22e9
BP
3112 json_destroy(where);
3113 }
3114
ce5a3e38 3115 symtab = ovsdb_symbol_table_create();
87b23a01
BP
3116 for (c = commands; c < &commands[n_commands]; c++) {
3117 ds_init(&c->output);
3118 }
f8ff4bc4
BP
3119 for (c = commands; c < &commands[n_commands]; c++) {
3120 struct vsctl_context ctx;
3121
ce5a3e38 3122 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
f8ff4bc4
BP
3123 (c->syntax->run)(&ctx);
3124 vsctl_context_done(&ctx, c);
87b23a01
BP
3125
3126 if (ctx.try_again) {
3127 goto try_again;
3128 }
c75d1511 3129 }
c75d1511 3130
af96ccd2 3131 status = ovsdb_idl_txn_commit_block(txn);
b54e22e9
BP
3132 if (wait_for_reload && status == TXN_SUCCESS) {
3133 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3134 }
8d49c47a
BP
3135 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3136 for (c = commands; c < &commands[n_commands]; c++) {
3137 if (c->syntax->postprocess) {
3138 struct vsctl_context ctx;
3139
ce5a3e38 3140 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
8d49c47a
BP
3141 (c->syntax->postprocess)(&ctx);
3142 vsctl_context_done(&ctx, c);
3143 }
3da1c516
BP
3144 }
3145 }
91e310a5 3146 error = xstrdup(ovsdb_idl_txn_get_error(txn));
c75d1511 3147 ovsdb_idl_txn_destroy(txn);
b7b6e2c4 3148 txn = the_idl_txn = NULL;
c75d1511 3149
ce5a3e38
BP
3150 unused = ovsdb_symbol_table_find_unused(symtab);
3151 if (unused) {
3152 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3153 "with \"-- --id=%s create ...\")", unused, unused);
3154 }
ce5a3e38 3155
c75d1511
BP
3156 switch (status) {
3157 case TXN_INCOMPLETE:
3158 NOT_REACHED();
3159
3160 case TXN_ABORTED:
3161 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
c88b6a27 3162 vsctl_fatal("transaction aborted");
c75d1511 3163
b54e22e9 3164 case TXN_UNCHANGED:
c75d1511
BP
3165 case TXN_SUCCESS:
3166 break;
3167
3168 case TXN_TRY_AGAIN:
87b23a01 3169 goto try_again;
c75d1511
BP
3170
3171 case TXN_ERROR:
91e310a5 3172 vsctl_fatal("transaction error: %s", error);
c75d1511
BP
3173
3174 default:
3175 NOT_REACHED();
3176 }
91e310a5 3177 free(error);
c75d1511 3178
87b23a01
BP
3179 ovsdb_symbol_table_destroy(symtab);
3180
f8ff4bc4
BP
3181 for (c = commands; c < &commands[n_commands]; c++) {
3182 struct ds *ds = &c->output;
ce5a3e38 3183
c75d1511
BP
3184 if (oneline) {
3185 size_t j;
3186
3187 ds_chomp(ds, '\n');
3188 for (j = 0; j < ds->length; j++) {
2a022368
BP
3189 int ch = ds->string[j];
3190 switch (ch) {
c75d1511
BP
3191 case '\n':
3192 fputs("\\n", stdout);
3193 break;
3194
3195 case '\\':
3196 fputs("\\\\", stdout);
3197 break;
3198
3199 default:
2a022368 3200 putchar(ch);
c75d1511
BP
3201 }
3202 }
3203 putchar('\n');
3204 } else {
3205 fputs(ds_cstr(ds), stdout);
3206 }
b86b43aa 3207 ds_destroy(&c->output);
ce5a3e38 3208
c56d226f 3209 smap_destroy(&c->options);
c75d1511 3210 }
b86b43aa 3211 free(commands);
b54e22e9
BP
3212
3213 if (wait_for_reload && status != TXN_UNCHANGED) {
3214 for (;;) {
b54e22e9
BP
3215 ovsdb_idl_run(idl);
3216 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3217 if (ovs->cur_cfg >= next_cfg) {
3218 goto done;
3219 }
3220 }
3221 ovsdb_idl_wait(idl);
3222 poll_block();
3223 }
3224 done: ;
3225 }
b86b43aa 3226 ovsdb_idl_destroy(idl);
b54e22e9 3227
c75d1511 3228 exit(EXIT_SUCCESS);
87b23a01
BP
3229
3230try_again:
3231 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3232 * resources and return so that the caller can try again. */
b7b6e2c4
JP
3233 if (txn) {
3234 ovsdb_idl_txn_abort(txn);
3235 ovsdb_idl_txn_destroy(txn);
3236 }
87b23a01
BP
3237 ovsdb_symbol_table_destroy(symtab);
3238 for (c = commands; c < &commands[n_commands]; c++) {
3239 ds_destroy(&c->output);
3240 }
3241 free(error);
c75d1511
BP
3242}
3243
f8ff4bc4
BP
3244static const struct vsctl_command_syntax all_commands[] = {
3245 /* Open vSwitch commands. */
e5e12280 3246 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
f8ff4bc4
BP
3247
3248 /* Bridge commands. */
e5e12280
BP
3249 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3250 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3251 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3252 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3253 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3254 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
fbd8715e
BP
3255 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3256 cmd_br_set_external_id, NULL, "", RW},
e5e12280
BP
3257 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3258 cmd_br_get_external_id, NULL, "", RO},
f8ff4bc4
BP
3259
3260 /* Port commands. */
e5e12280
BP
3261 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3262 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3263 RW},
3264 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3265 "--may-exist,--fake-iface", RW},
3266 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3267 "--if-exists,--with-iface", RW},
3268 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
f8ff4bc4
BP
3269
3270 /* Interface commands. */
e5e12280
BP
3271 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3272 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
f8ff4bc4
BP
3273
3274 /* Controller commands. */
4e3e7ff9
BP
3275 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3276 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3277 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3278 "", RW},
e5e12280
BP
3279 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3280 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3281 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
f8ff4bc4
BP
3282
3283 /* SSL commands. */
e5e12280
BP
3284 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3285 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3286 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
f8ff4bc4 3287
18ee958b 3288 /* Switch commands. */
e5e12280 3289 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
18ee958b 3290
f8ff4bc4 3291 /* Parameter commands. */
e5e12280
BP
3292 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3293 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "", RO},
3294 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3295 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3296 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3297 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3298 {"create", 2, INT_MAX, NULL, cmd_create, post_create, "--id=", RW},
3299 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3300 RW},
3301 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3302 RO},
3303
3304 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},
f8ff4bc4 3305};
5d9cb63c 3306