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