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