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