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