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