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