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