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