]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-vsctl.c
Merge "citrix" branch into "master".
[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 char *
496 vsctl_context_to_string(const struct vsctl_context *ctx)
497 {
498 const struct shash_node *node;
499 struct svec words;
500 char *s;
501 int i;
502
503 svec_init(&words);
504 SHASH_FOR_EACH (node, &ctx->options) {
505 svec_add(&words, node->name);
506 }
507 for (i = 0; i < ctx->argc; i++) {
508 svec_add(&words, ctx->argv[i]);
509 }
510 svec_terminate(&words);
511
512 s = process_escape_args(words.names);
513
514 svec_destroy(&words);
515
516 return s;
517 }
518
519 static struct vsctl_bridge *
520 add_bridge(struct vsctl_info *b,
521 struct ovsrec_bridge *br_cfg, const char *name,
522 struct vsctl_bridge *parent, int vlan)
523 {
524 struct vsctl_bridge *br = xmalloc(sizeof *br);
525 br->br_cfg = br_cfg;
526 br->name = xstrdup(name);
527 br->parent = parent;
528 br->vlan = vlan;
529 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
530 shash_add(&b->bridges, br->name, br);
531 return br;
532 }
533
534 static bool
535 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
536 {
537 return (port_cfg->fake_bridge
538 && port_cfg->tag
539 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
540 }
541
542 static struct vsctl_bridge *
543 find_vlan_bridge(struct vsctl_info *info,
544 struct vsctl_bridge *parent, int vlan)
545 {
546 struct shash_node *node;
547
548 SHASH_FOR_EACH (node, &info->bridges) {
549 struct vsctl_bridge *br = node->data;
550 if (br->parent == parent && br->vlan == vlan) {
551 return br;
552 }
553 }
554
555 return NULL;
556 }
557
558 static void
559 free_info(struct vsctl_info *info)
560 {
561 struct shash_node *node;
562
563 SHASH_FOR_EACH (node, &info->bridges) {
564 struct vsctl_bridge *bridge = node->data;
565 free(bridge->name);
566 free(bridge);
567 }
568 shash_destroy(&info->bridges);
569
570 SHASH_FOR_EACH (node, &info->ports) {
571 struct vsctl_port *port = node->data;
572 free(port);
573 }
574 shash_destroy(&info->ports);
575
576 SHASH_FOR_EACH (node, &info->ifaces) {
577 struct vsctl_iface *iface = node->data;
578 free(iface);
579 }
580 shash_destroy(&info->ifaces);
581 }
582
583 static void
584 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
585 {
586 struct shash bridges, ports;
587 size_t i;
588
589 shash_init(&info->bridges);
590 shash_init(&info->ports);
591 shash_init(&info->ifaces);
592
593 info->ctrl = ovs->controller;
594
595 shash_init(&bridges);
596 shash_init(&ports);
597 for (i = 0; i < ovs->n_bridges; i++) {
598 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
599 struct vsctl_bridge *br;
600 size_t j;
601
602 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
603 VLOG_WARN("%s: database contains duplicate bridge name",
604 br_cfg->name);
605 continue;
606 }
607 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
608 if (!br) {
609 continue;
610 }
611
612 for (j = 0; j < br_cfg->n_ports; j++) {
613 struct ovsrec_port *port_cfg = br_cfg->ports[j];
614
615 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
616 VLOG_WARN("%s: database contains duplicate port name",
617 port_cfg->name);
618 continue;
619 }
620
621 if (port_is_fake_bridge(port_cfg)
622 && shash_add_once(&bridges, port_cfg->name, NULL)) {
623 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
624 }
625 }
626 }
627 shash_destroy(&bridges);
628 shash_destroy(&ports);
629
630 shash_init(&bridges);
631 shash_init(&ports);
632 for (i = 0; i < ovs->n_bridges; i++) {
633 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
634 struct vsctl_bridge *br;
635 size_t j;
636
637 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
638 continue;
639 }
640 br = shash_find_data(&info->bridges, br_cfg->name);
641 for (j = 0; j < br_cfg->n_ports; j++) {
642 struct ovsrec_port *port_cfg = br_cfg->ports[j];
643 struct vsctl_port *port;
644 size_t k;
645
646 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
647 continue;
648 }
649
650 if (port_is_fake_bridge(port_cfg)
651 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
652 continue;
653 }
654
655 port = xmalloc(sizeof *port);
656 port->port_cfg = port_cfg;
657 if (port_cfg->tag
658 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
659 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
660 if (!port->bridge) {
661 port->bridge = br;
662 }
663 } else {
664 port->bridge = br;
665 }
666 shash_add(&info->ports, port_cfg->name, port);
667
668 for (k = 0; k < port_cfg->n_interfaces; k++) {
669 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
670 struct vsctl_iface *iface;
671
672 if (shash_find(&info->ifaces, iface_cfg->name)) {
673 VLOG_WARN("%s: database contains duplicate interface name",
674 iface_cfg->name);
675 continue;
676 }
677
678 iface = xmalloc(sizeof *iface);
679 iface->iface_cfg = iface_cfg;
680 iface->port = port;
681 shash_add(&info->ifaces, iface_cfg->name, iface);
682 }
683 }
684 }
685 shash_destroy(&bridges);
686 shash_destroy(&ports);
687 }
688
689 static void
690 check_conflicts(struct vsctl_info *info, const char *name,
691 char *msg)
692 {
693 struct vsctl_iface *iface;
694 struct vsctl_port *port;
695
696 if (shash_find(&info->bridges, name)) {
697 vsctl_fatal("%s because a bridge named %s already exists",
698 msg, name);
699 }
700
701 port = shash_find_data(&info->ports, name);
702 if (port) {
703 vsctl_fatal("%s because a port named %s already exists on "
704 "bridge %s", msg, name, port->bridge->name);
705 }
706
707 iface = shash_find_data(&info->ifaces, name);
708 if (iface) {
709 vsctl_fatal("%s because an interface named %s already exists "
710 "on bridge %s", msg, name, iface->port->bridge->name);
711 }
712
713 free(msg);
714 }
715
716 static struct vsctl_bridge *
717 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
718 {
719 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
720 if (must_exist && !br) {
721 vsctl_fatal("no bridge named %s", name);
722 }
723 return br;
724 }
725
726 static struct vsctl_bridge *
727 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
728 {
729 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
730 if (br && br->parent) {
731 vsctl_fatal("%s is a fake bridge", name);
732 }
733 return br;
734 }
735
736 static struct vsctl_port *
737 find_port(struct vsctl_info *info, const char *name, bool must_exist)
738 {
739 struct vsctl_port *port = shash_find_data(&info->ports, name);
740 if (port && !strcmp(name, port->bridge->name)) {
741 port = NULL;
742 }
743 if (must_exist && !port) {
744 vsctl_fatal("no port named %s", name);
745 }
746 return port;
747 }
748
749 static struct vsctl_iface *
750 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
751 {
752 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
753 if (iface && !strcmp(name, iface->port->bridge->name)) {
754 iface = NULL;
755 }
756 if (must_exist && !iface) {
757 vsctl_fatal("no interface named %s", name);
758 }
759 return iface;
760 }
761
762 static void
763 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
764 {
765 struct ovsrec_port **ports;
766 size_t i;
767
768 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
769 for (i = 0; i < br->n_ports; i++) {
770 ports[i] = br->ports[i];
771 }
772 ports[br->n_ports] = port;
773 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
774 free(ports);
775 }
776
777 static void
778 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
779 {
780 struct ovsrec_port **ports;
781 size_t i, n;
782
783 ports = xmalloc(sizeof *br->ports * br->n_ports);
784 for (i = n = 0; i < br->n_ports; i++) {
785 if (br->ports[i] != port) {
786 ports[n++] = br->ports[i];
787 }
788 }
789 ovsrec_bridge_set_ports(br, ports, n);
790 free(ports);
791 }
792
793 static void
794 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
795 struct ovsrec_bridge *bridge)
796 {
797 struct ovsrec_bridge **bridges;
798 size_t i;
799
800 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
801 for (i = 0; i < ovs->n_bridges; i++) {
802 bridges[i] = ovs->bridges[i];
803 }
804 bridges[ovs->n_bridges] = bridge;
805 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
806 free(bridges);
807 }
808
809 static void
810 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
811 struct ovsrec_bridge *bridge)
812 {
813 struct ovsrec_bridge **bridges;
814 size_t i, n;
815
816 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
817 for (i = n = 0; i < ovs->n_bridges; i++) {
818 if (ovs->bridges[i] != bridge) {
819 bridges[n++] = ovs->bridges[i];
820 }
821 }
822 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
823 free(bridges);
824 }
825
826 static void
827 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
828 {
829 }
830
831 static void
832 cmd_add_br(struct vsctl_context *ctx)
833 {
834 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
835 const char *br_name, *parent_name;
836 struct vsctl_info info;
837 int vlan;
838
839 br_name = ctx->argv[1];
840 if (ctx->argc == 2) {
841 parent_name = NULL;
842 vlan = 0;
843 } else if (ctx->argc == 4) {
844 parent_name = ctx->argv[2];
845 vlan = atoi(ctx->argv[3]);
846 if (vlan < 1 || vlan > 4095) {
847 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
848 }
849 } else {
850 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
851 ctx->argv[0]);
852 }
853
854 get_info(ctx->ovs, &info);
855 if (may_exist) {
856 struct vsctl_bridge *br;
857
858 br = find_bridge(&info, br_name, false);
859 if (br) {
860 if (!parent_name) {
861 if (br->parent) {
862 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
863 "a VLAN bridge for VLAN %d",
864 br_name, br_name, br->vlan);
865 }
866 } else {
867 if (!br->parent) {
868 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
869 "is not a VLAN bridge",
870 br_name, parent_name, vlan, br_name);
871 } else if (strcmp(br->parent->name, parent_name)) {
872 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
873 "has the wrong parent %s",
874 br_name, parent_name, vlan,
875 br_name, br->parent->name);
876 } else if (br->vlan != vlan) {
877 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
878 "is a VLAN bridge for the wrong VLAN %d",
879 br_name, parent_name, vlan, br_name, br->vlan);
880 }
881 }
882 return;
883 }
884 }
885 check_conflicts(&info, br_name,
886 xasprintf("cannot create a bridge named %s", br_name));
887
888 if (!parent_name) {
889 struct ovsrec_port *port;
890 struct ovsrec_interface *iface;
891 struct ovsrec_bridge *br;
892
893 iface = ovsrec_interface_insert(ctx->txn);
894 ovsrec_interface_set_name(iface, br_name);
895
896 port = ovsrec_port_insert(ctx->txn);
897 ovsrec_port_set_name(port, br_name);
898 ovsrec_port_set_interfaces(port, &iface, 1);
899
900 br = ovsrec_bridge_insert(ctx->txn);
901 ovsrec_bridge_set_name(br, br_name);
902 ovsrec_bridge_set_ports(br, &port, 1);
903
904 ovs_insert_bridge(ctx->ovs, br);
905 } else {
906 struct vsctl_bridge *parent;
907 struct ovsrec_port *port;
908 struct ovsrec_interface *iface;
909 struct ovsrec_bridge *br;
910 int64_t tag = vlan;
911
912 parent = find_bridge(&info, parent_name, false);
913 if (parent && parent->vlan) {
914 vsctl_fatal("cannot create bridge with fake bridge as parent");
915 }
916 if (!parent) {
917 vsctl_fatal("parent bridge %s does not exist", parent_name);
918 }
919 br = parent->br_cfg;
920
921 iface = ovsrec_interface_insert(ctx->txn);
922 ovsrec_interface_set_name(iface, br_name);
923 ovsrec_interface_set_type(iface, "internal");
924
925 port = ovsrec_port_insert(ctx->txn);
926 ovsrec_port_set_name(port, br_name);
927 ovsrec_port_set_interfaces(port, &iface, 1);
928 ovsrec_port_set_fake_bridge(port, true);
929 ovsrec_port_set_tag(port, &tag, 1);
930
931 bridge_insert_port(br, port);
932 }
933
934 free_info(&info);
935 }
936
937 static void
938 del_port(struct vsctl_info *info, struct vsctl_port *port)
939 {
940 struct shash_node *node;
941
942 SHASH_FOR_EACH (node, &info->ifaces) {
943 struct vsctl_iface *iface = node->data;
944 if (iface->port == port) {
945 ovsrec_interface_delete(iface->iface_cfg);
946 }
947 }
948 ovsrec_port_delete(port->port_cfg);
949
950 bridge_delete_port((port->bridge->parent
951 ? port->bridge->parent->br_cfg
952 : port->bridge->br_cfg), port->port_cfg);
953 }
954
955 static void
956 cmd_del_br(struct vsctl_context *ctx)
957 {
958 bool must_exist = !shash_find(&ctx->options, "--if-exists");
959 struct vsctl_bridge *bridge;
960 struct vsctl_info info;
961
962 get_info(ctx->ovs, &info);
963 bridge = find_bridge(&info, ctx->argv[1], must_exist);
964 if (bridge) {
965 struct shash_node *node;
966
967 SHASH_FOR_EACH (node, &info.ports) {
968 struct vsctl_port *port = node->data;
969 if (port->bridge == bridge || port->bridge->parent == bridge
970 || !strcmp(port->port_cfg->name, bridge->name)) {
971 del_port(&info, port);
972 }
973 }
974 if (bridge->br_cfg) {
975 ovsrec_bridge_delete(bridge->br_cfg);
976 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
977 }
978 }
979 free_info(&info);
980 }
981
982 static void
983 output_sorted(struct svec *svec, struct ds *output)
984 {
985 const char *name;
986 size_t i;
987
988 svec_sort(svec);
989 SVEC_FOR_EACH (i, name, svec) {
990 ds_put_format(output, "%s\n", name);
991 }
992 }
993
994 static void
995 cmd_list_br(struct vsctl_context *ctx)
996 {
997 struct shash_node *node;
998 struct vsctl_info info;
999 struct svec bridges;
1000
1001 get_info(ctx->ovs, &info);
1002
1003 svec_init(&bridges);
1004 SHASH_FOR_EACH (node, &info.bridges) {
1005 struct vsctl_bridge *br = node->data;
1006 svec_add(&bridges, br->name);
1007 }
1008 output_sorted(&bridges, &ctx->output);
1009 svec_destroy(&bridges);
1010
1011 free_info(&info);
1012 }
1013
1014 static void
1015 cmd_br_exists(struct vsctl_context *ctx)
1016 {
1017 struct vsctl_info info;
1018
1019 get_info(ctx->ovs, &info);
1020 if (!find_bridge(&info, ctx->argv[1], false)) {
1021 vsctl_exit(2);
1022 }
1023 free_info(&info);
1024 }
1025
1026 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1027 * equals 'a', false otherwise. */
1028 static bool
1029 key_matches(const char *a,
1030 const char *b_prefix, size_t b_prefix_len, const char *b)
1031 {
1032 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1033 }
1034
1035 static void
1036 set_external_id(char **old_keys, char **old_values, size_t old_n,
1037 char *key, char *value,
1038 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1039 {
1040 char **new_keys;
1041 char **new_values;
1042 size_t new_n;
1043 size_t i;
1044
1045 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1046 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1047 new_n = 0;
1048 for (i = 0; i < old_n; i++) {
1049 if (strcmp(key, old_keys[i])) {
1050 new_keys[new_n] = old_keys[i];
1051 new_values[new_n] = old_values[i];
1052 new_n++;
1053 }
1054 }
1055 if (value) {
1056 new_keys[new_n] = key;
1057 new_values[new_n] = value;
1058 new_n++;
1059 }
1060 *new_keysp = new_keys;
1061 *new_valuesp = new_values;
1062 *new_np = new_n;
1063 }
1064
1065 static void
1066 cmd_br_set_external_id(struct vsctl_context *ctx)
1067 {
1068 struct vsctl_info info;
1069 struct vsctl_bridge *bridge;
1070 char **keys, **values;
1071 size_t n;
1072
1073 get_info(ctx->ovs, &info);
1074 bridge = find_bridge(&info, ctx->argv[1], true);
1075 if (bridge->br_cfg) {
1076 set_external_id(bridge->br_cfg->key_external_ids,
1077 bridge->br_cfg->value_external_ids,
1078 bridge->br_cfg->n_external_ids,
1079 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1080 &keys, &values, &n);
1081 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1082 } else {
1083 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1084 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1085 set_external_id(port->port_cfg->key_external_ids,
1086 port->port_cfg->value_external_ids,
1087 port->port_cfg->n_external_ids,
1088 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1089 &keys, &values, &n);
1090 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1091 free(key);
1092 }
1093 free(keys);
1094 free(values);
1095
1096 free_info(&info);
1097 }
1098
1099 static void
1100 get_external_id(char **keys, char **values, size_t n,
1101 const char *prefix, const char *key,
1102 struct ds *output)
1103 {
1104 size_t prefix_len = strlen(prefix);
1105 struct svec svec;
1106 size_t i;
1107
1108 svec_init(&svec);
1109 for (i = 0; i < n; i++) {
1110 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1111 svec_add_nocopy(&svec, xasprintf("%s=%s",
1112 keys[i] + prefix_len, values[i]));
1113 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1114 svec_add(&svec, values[i]);
1115 break;
1116 }
1117 }
1118 output_sorted(&svec, output);
1119 svec_destroy(&svec);
1120 }
1121
1122 static void
1123 cmd_br_get_external_id(struct vsctl_context *ctx)
1124 {
1125 struct vsctl_info info;
1126 struct vsctl_bridge *bridge;
1127
1128 get_info(ctx->ovs, &info);
1129 bridge = find_bridge(&info, ctx->argv[1], true);
1130 if (bridge->br_cfg) {
1131 get_external_id(bridge->br_cfg->key_external_ids,
1132 bridge->br_cfg->value_external_ids,
1133 bridge->br_cfg->n_external_ids,
1134 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1135 &ctx->output);
1136 } else {
1137 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1138 get_external_id(port->port_cfg->key_external_ids,
1139 port->port_cfg->value_external_ids,
1140 port->port_cfg->n_external_ids,
1141 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1142 }
1143 free_info(&info);
1144 }
1145
1146
1147 static void
1148 cmd_list_ports(struct vsctl_context *ctx)
1149 {
1150 struct vsctl_bridge *br;
1151 struct shash_node *node;
1152 struct vsctl_info info;
1153 struct svec ports;
1154
1155 get_info(ctx->ovs, &info);
1156 br = find_bridge(&info, ctx->argv[1], true);
1157
1158 svec_init(&ports);
1159 SHASH_FOR_EACH (node, &info.ports) {
1160 struct vsctl_port *port = node->data;
1161
1162 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1163 svec_add(&ports, port->port_cfg->name);
1164 }
1165 }
1166 output_sorted(&ports, &ctx->output);
1167 svec_destroy(&ports);
1168
1169 free_info(&info);
1170 }
1171
1172 static void
1173 add_port(struct vsctl_context *ctx,
1174 const char *br_name, const char *port_name,
1175 bool may_exist, bool fake_iface,
1176 char *iface_names[], int n_ifaces)
1177 {
1178 struct vsctl_info info;
1179 struct vsctl_bridge *bridge;
1180 struct ovsrec_interface **ifaces;
1181 struct ovsrec_port *port;
1182 size_t i;
1183
1184 get_info(ctx->ovs, &info);
1185 if (may_exist) {
1186 struct vsctl_port *port;
1187
1188 port = find_port(&info, port_name, false);
1189 if (port) {
1190 struct svec want_names, have_names;
1191 size_t i;
1192
1193 svec_init(&want_names);
1194 for (i = 0; i < n_ifaces; i++) {
1195 svec_add(&want_names, iface_names[i]);
1196 }
1197 svec_sort(&want_names);
1198
1199 svec_init(&have_names);
1200 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1201 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1202 }
1203 svec_sort(&have_names);
1204
1205 if (strcmp(port->bridge->name, br_name)) {
1206 char *command = vsctl_context_to_string(ctx);
1207 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1208 command, port_name, port->bridge->name);
1209 }
1210
1211 if (!svec_equal(&want_names, &have_names)) {
1212 char *have_names_string = svec_join(&have_names, ", ", "");
1213 char *command = vsctl_context_to_string(ctx);
1214
1215 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1216 command, port_name, have_names_string);
1217 }
1218
1219 svec_destroy(&want_names);
1220 svec_destroy(&have_names);
1221
1222 return;
1223 }
1224 }
1225 check_conflicts(&info, port_name,
1226 xasprintf("cannot create a port named %s", port_name));
1227 for (i = 0; i < n_ifaces; i++) {
1228 check_conflicts(&info, iface_names[i],
1229 xasprintf("cannot create an interface named %s",
1230 iface_names[i]));
1231 }
1232 bridge = find_bridge(&info, br_name, true);
1233
1234 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1235 for (i = 0; i < n_ifaces; i++) {
1236 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1237 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1238 }
1239
1240 port = ovsrec_port_insert(ctx->txn);
1241 ovsrec_port_set_name(port, port_name);
1242 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1243 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1244 free(ifaces);
1245
1246 if (bridge->vlan) {
1247 int64_t tag = bridge->vlan;
1248 ovsrec_port_set_tag(port, &tag, 1);
1249 }
1250
1251 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1252 : bridge->br_cfg), port);
1253
1254 free_info(&info);
1255 }
1256
1257 static void
1258 cmd_add_port(struct vsctl_context *ctx)
1259 {
1260 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1261
1262 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1263 &ctx->argv[2], 1);
1264 }
1265
1266 static void
1267 cmd_add_bond(struct vsctl_context *ctx)
1268 {
1269 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1270 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1271
1272 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1273 &ctx->argv[3], ctx->argc - 3);
1274 }
1275
1276 static void
1277 cmd_del_port(struct vsctl_context *ctx)
1278 {
1279 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1280 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1281 struct vsctl_port *port;
1282 struct vsctl_info info;
1283
1284 get_info(ctx->ovs, &info);
1285 if (!with_iface) {
1286 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1287 } else {
1288 const char *target = ctx->argv[ctx->argc - 1];
1289 struct vsctl_iface *iface;
1290
1291 port = find_port(&info, target, false);
1292 if (!port) {
1293 iface = find_iface(&info, target, false);
1294 if (iface) {
1295 port = iface->port;
1296 }
1297 }
1298 if (must_exist && !port) {
1299 vsctl_fatal("no port or interface named %s", target);
1300 }
1301 }
1302
1303 if (port) {
1304 if (ctx->argc == 3) {
1305 struct vsctl_bridge *bridge;
1306
1307 bridge = find_bridge(&info, ctx->argv[1], true);
1308 if (port->bridge != bridge) {
1309 if (port->bridge->parent == bridge) {
1310 vsctl_fatal("bridge %s does not have a port %s (although "
1311 "its parent bridge %s does)",
1312 ctx->argv[1], ctx->argv[2],
1313 bridge->parent->name);
1314 } else {
1315 vsctl_fatal("bridge %s does not have a port %s",
1316 ctx->argv[1], ctx->argv[2]);
1317 }
1318 }
1319 }
1320
1321 del_port(&info, port);
1322 }
1323
1324 free_info(&info);
1325 }
1326
1327 static void
1328 cmd_port_to_br(struct vsctl_context *ctx)
1329 {
1330 struct vsctl_port *port;
1331 struct vsctl_info info;
1332
1333 get_info(ctx->ovs, &info);
1334 port = find_port(&info, ctx->argv[1], true);
1335 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1336 free_info(&info);
1337 }
1338
1339 static void
1340 cmd_br_to_vlan(struct vsctl_context *ctx)
1341 {
1342 struct vsctl_bridge *bridge;
1343 struct vsctl_info info;
1344
1345 get_info(ctx->ovs, &info);
1346 bridge = find_bridge(&info, ctx->argv[1], true);
1347 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1348 free_info(&info);
1349 }
1350
1351 static void
1352 cmd_br_to_parent(struct vsctl_context *ctx)
1353 {
1354 struct vsctl_bridge *bridge;
1355 struct vsctl_info info;
1356
1357 get_info(ctx->ovs, &info);
1358 bridge = find_bridge(&info, ctx->argv[1], true);
1359 if (bridge->parent) {
1360 bridge = bridge->parent;
1361 }
1362 ds_put_format(&ctx->output, "%s\n", bridge->name);
1363 free_info(&info);
1364 }
1365
1366 static void
1367 cmd_list_ifaces(struct vsctl_context *ctx)
1368 {
1369 struct vsctl_bridge *br;
1370 struct shash_node *node;
1371 struct vsctl_info info;
1372 struct svec ifaces;
1373
1374 get_info(ctx->ovs, &info);
1375 br = find_bridge(&info, ctx->argv[1], true);
1376
1377 svec_init(&ifaces);
1378 SHASH_FOR_EACH (node, &info.ifaces) {
1379 struct vsctl_iface *iface = node->data;
1380
1381 if (strcmp(iface->iface_cfg->name, br->name)
1382 && br == iface->port->bridge) {
1383 svec_add(&ifaces, iface->iface_cfg->name);
1384 }
1385 }
1386 output_sorted(&ifaces, &ctx->output);
1387 svec_destroy(&ifaces);
1388
1389 free_info(&info);
1390 }
1391
1392 static void
1393 cmd_iface_to_br(struct vsctl_context *ctx)
1394 {
1395 struct vsctl_iface *iface;
1396 struct vsctl_info info;
1397
1398 get_info(ctx->ovs, &info);
1399 iface = find_iface(&info, ctx->argv[1], true);
1400 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1401 free_info(&info);
1402 }
1403
1404 static void
1405 cmd_get_controller(struct vsctl_context *ctx)
1406 {
1407 struct vsctl_info info;
1408
1409 get_info(ctx->ovs, &info);
1410
1411 if (ctx->argc == 1) {
1412 /* Return the controller from the "Open_vSwitch" table */
1413 if (info.ctrl) {
1414 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1415 }
1416 } else {
1417 /* Return the controller for a particular bridge. */
1418 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1419
1420 /* If no controller is explicitly defined for the requested
1421 * bridge, fallback to the "Open_vSwitch" table's controller. */
1422 if (br->ctrl) {
1423 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1424 } else if (info.ctrl) {
1425 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1426 }
1427 }
1428
1429 free_info(&info);
1430 }
1431
1432 static void
1433 cmd_del_controller(struct vsctl_context *ctx)
1434 {
1435 struct vsctl_info info;
1436
1437 get_info(ctx->ovs, &info);
1438
1439 if (ctx->argc == 1) {
1440 if (info.ctrl) {
1441 ovsrec_controller_delete(info.ctrl);
1442 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1443 }
1444 } else {
1445 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1446
1447 if (br->ctrl) {
1448 ovsrec_controller_delete(br->ctrl);
1449 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1450 }
1451 }
1452
1453 free_info(&info);
1454 }
1455
1456 static void
1457 cmd_set_controller(struct vsctl_context *ctx)
1458 {
1459 struct vsctl_info info;
1460 struct ovsrec_controller *ctrl;
1461
1462 get_info(ctx->ovs, &info);
1463
1464 if (ctx->argc == 2) {
1465 /* Set the controller in the "Open_vSwitch" table. */
1466 if (info.ctrl) {
1467 ovsrec_controller_delete(info.ctrl);
1468 }
1469 ctrl = ovsrec_controller_insert(ctx->txn);
1470 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1471 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1472 } else {
1473 /* Set the controller for a particular bridge. */
1474 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1475
1476 if (br->ctrl) {
1477 ovsrec_controller_delete(br->ctrl);
1478 }
1479 ctrl = ovsrec_controller_insert(ctx->txn);
1480 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1481 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1482 }
1483
1484 free_info(&info);
1485 }
1486
1487 static void
1488 cmd_get_fail_mode(struct vsctl_context *ctx)
1489 {
1490 struct vsctl_info info;
1491 const char *fail_mode = NULL;
1492
1493 get_info(ctx->ovs, &info);
1494
1495 if (ctx->argc == 1) {
1496 /* Return the fail-mode from the "Open_vSwitch" table */
1497 if (info.ctrl && info.ctrl->fail_mode) {
1498 fail_mode = info.ctrl->fail_mode;
1499 }
1500 } else {
1501 /* Return the fail-mode for a particular bridge. */
1502 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1503
1504 /* If no controller or fail-mode is explicitly defined for the
1505 * requested bridge, fallback to the "Open_vSwitch" table's
1506 * setting. */
1507 if (br->ctrl && br->ctrl->fail_mode) {
1508 fail_mode = br->ctrl->fail_mode;
1509 } else if (info.ctrl && info.ctrl->fail_mode) {
1510 fail_mode = info.ctrl->fail_mode;
1511 }
1512 }
1513
1514 if (fail_mode && strlen(fail_mode)) {
1515 ds_put_format(&ctx->output, "%s\n", fail_mode);
1516 }
1517
1518 free_info(&info);
1519 }
1520
1521 static void
1522 cmd_del_fail_mode(struct vsctl_context *ctx)
1523 {
1524 struct vsctl_info info;
1525
1526 get_info(ctx->ovs, &info);
1527
1528 if (ctx->argc == 1) {
1529 if (info.ctrl && info.ctrl->fail_mode) {
1530 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1531 }
1532 } else {
1533 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1534
1535 if (br->ctrl && br->ctrl->fail_mode) {
1536 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1537 }
1538 }
1539
1540 free_info(&info);
1541 }
1542
1543 static void
1544 cmd_set_fail_mode(struct vsctl_context *ctx)
1545 {
1546 struct vsctl_info info;
1547 const char *fail_mode;
1548
1549 get_info(ctx->ovs, &info);
1550
1551 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1552
1553 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1554 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1555 }
1556
1557 if (ctx->argc == 2) {
1558 /* Set the fail-mode in the "Open_vSwitch" table. */
1559 if (!info.ctrl) {
1560 vsctl_fatal("no controller declared");
1561 }
1562 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1563 } else {
1564 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1565
1566 if (!br->ctrl) {
1567 vsctl_fatal("no controller declared for %s", br->name);
1568 }
1569 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1570 }
1571
1572 free_info(&info);
1573 }
1574
1575 static void
1576 cmd_get_ssl(struct vsctl_context *ctx)
1577 {
1578 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1579
1580 if (ssl) {
1581 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1582 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1583 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1584 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1585 ssl->bootstrap_ca_cert ? "true" : "false");
1586 }
1587 }
1588
1589 static void
1590 cmd_del_ssl(struct vsctl_context *ctx)
1591 {
1592 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1593
1594 if (ssl) {
1595 ovsrec_ssl_delete(ssl);
1596 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1597 }
1598 }
1599
1600 static void
1601 cmd_set_ssl(struct vsctl_context *ctx)
1602 {
1603 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1604 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1605
1606 if (ssl) {
1607 ovsrec_ssl_delete(ssl);
1608 }
1609 ssl = ovsrec_ssl_insert(ctx->txn);
1610
1611 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1612 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1613 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1614
1615 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1616
1617 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1618 }
1619 \f
1620 /* Parameter commands. */
1621
1622 struct vsctl_row_id {
1623 const struct ovsdb_idl_table_class *table;
1624 const struct ovsdb_idl_column *name_column;
1625 const struct ovsdb_idl_column *uuid_column;
1626 };
1627
1628 struct vsctl_table_class {
1629 struct ovsdb_idl_table_class *class;
1630 struct vsctl_row_id row_ids[2];
1631 };
1632
1633 static const struct vsctl_table_class tables[] = {
1634 {&ovsrec_table_bridge,
1635 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1636 {NULL, NULL, NULL}}},
1637
1638 {&ovsrec_table_controller,
1639 {{&ovsrec_table_bridge,
1640 &ovsrec_bridge_col_name,
1641 &ovsrec_bridge_col_controller},
1642 {&ovsrec_table_open_vswitch,
1643 NULL,
1644 &ovsrec_open_vswitch_col_controller}}},
1645
1646 {&ovsrec_table_interface,
1647 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1648 {NULL, NULL, NULL}}},
1649
1650 {&ovsrec_table_mirror,
1651 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1652 {NULL, NULL, NULL}}},
1653
1654 {&ovsrec_table_netflow,
1655 {{&ovsrec_table_bridge,
1656 &ovsrec_bridge_col_name,
1657 &ovsrec_bridge_col_netflow},
1658 {NULL, NULL, NULL}}},
1659
1660 {&ovsrec_table_open_vswitch,
1661 {{&ovsrec_table_open_vswitch, NULL, NULL},
1662 {NULL, NULL, NULL}}},
1663
1664 {&ovsrec_table_port,
1665 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1666 {NULL, NULL, NULL}}},
1667
1668 {&ovsrec_table_ssl,
1669 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1670
1671 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1672 };
1673
1674 static void
1675 die_if_error(char *error)
1676 {
1677 if (error) {
1678 vsctl_fatal("%s", error);
1679 }
1680 }
1681
1682 static int
1683 to_lower_and_underscores(unsigned c)
1684 {
1685 return c == '-' ? '_' : tolower(c);
1686 }
1687
1688 static unsigned int
1689 score_partial_match(const char *name, const char *s)
1690 {
1691 int score;
1692
1693 if (!strcmp(name, s)) {
1694 return UINT_MAX;
1695 }
1696 for (score = 0; ; score++, name++, s++) {
1697 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1698 break;
1699 } else if (*name == '\0') {
1700 return UINT_MAX - 1;
1701 }
1702 }
1703 return *s == '\0' ? score : 0;
1704 }
1705
1706 static const struct vsctl_table_class *
1707 get_table(const char *table_name)
1708 {
1709 const struct vsctl_table_class *table;
1710 const struct vsctl_table_class *best_match = NULL;
1711 unsigned int best_score = 0;
1712
1713 for (table = tables; table->class; table++) {
1714 unsigned int score = score_partial_match(table->class->name,
1715 table_name);
1716 if (score > best_score) {
1717 best_match = table;
1718 best_score = score;
1719 } else if (score == best_score) {
1720 best_match = NULL;
1721 }
1722 }
1723 if (best_match) {
1724 return best_match;
1725 } else if (best_score) {
1726 vsctl_fatal("multiple table names match \"%s\"", table_name);
1727 } else {
1728 vsctl_fatal("unknown table \"%s\"", table_name);
1729 }
1730 }
1731
1732 static const struct ovsdb_idl_row *
1733 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1734 const struct vsctl_row_id *id, const char *record_id)
1735 {
1736 const struct ovsdb_idl_row *referrer, *final;
1737
1738 if (!id->table) {
1739 return NULL;
1740 }
1741
1742 if (!id->name_column) {
1743 if (strcmp(record_id, ".")) {
1744 return NULL;
1745 }
1746 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1747 if (!referrer || ovsdb_idl_next_row(referrer)) {
1748 return NULL;
1749 }
1750 } else {
1751 const struct ovsdb_idl_row *row;
1752 unsigned int best_score = 0;
1753
1754 /* It might make sense to relax this assertion. */
1755 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1756
1757 referrer = NULL;
1758 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1759 row != NULL && best_score != UINT_MAX;
1760 row = ovsdb_idl_next_row(row))
1761 {
1762 struct ovsdb_datum name;
1763
1764 ovsdb_idl_txn_read(row, id->name_column, &name);
1765 if (name.n == 1) {
1766 unsigned int score = score_partial_match(name.keys[0].string,
1767 record_id);
1768 if (score > best_score) {
1769 referrer = row;
1770 best_score = score;
1771 } else if (score == best_score) {
1772 referrer = NULL;
1773 }
1774 }
1775 ovsdb_datum_destroy(&name, &id->name_column->type);
1776 }
1777 if (best_score && !referrer) {
1778 vsctl_fatal("multiple rows in %s match \"%s\"",
1779 table->class->name, record_id);
1780 }
1781 }
1782 if (!referrer) {
1783 return NULL;
1784 }
1785
1786 final = NULL;
1787 if (id->uuid_column) {
1788 struct ovsdb_datum uuid;
1789
1790 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1791 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1792
1793 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1794 if (uuid.n == 1) {
1795 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1796 &uuid.keys[0].uuid);
1797 }
1798 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1799 } else {
1800 final = referrer;
1801 }
1802
1803 return final;
1804 }
1805
1806 static const struct ovsdb_idl_row *
1807 get_row(struct vsctl_context *ctx,
1808 const struct vsctl_table_class *table, const char *record_id)
1809 {
1810 const struct ovsdb_idl_row *row;
1811 struct uuid uuid;
1812
1813 if (uuid_from_string(&uuid, record_id)) {
1814 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1815 } else {
1816 int i;
1817
1818 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1819 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1820 if (row) {
1821 break;
1822 }
1823 }
1824 }
1825 return row;
1826 }
1827
1828 static const struct ovsdb_idl_row *
1829 must_get_row(struct vsctl_context *ctx,
1830 const struct vsctl_table_class *table, const char *record_id)
1831 {
1832 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1833 if (!row) {
1834 vsctl_fatal("no row \"%s\" in table %s",
1835 record_id, table->class->name);
1836 }
1837 return row;
1838 }
1839
1840 static char *
1841 get_column(const struct vsctl_table_class *table, const char *column_name,
1842 const struct ovsdb_idl_column **columnp)
1843 {
1844 const struct ovsdb_idl_column *best_match = NULL;
1845 unsigned int best_score = 0;
1846 size_t i;
1847
1848 for (i = 0; i < table->class->n_columns; i++) {
1849 const struct ovsdb_idl_column *column = &table->class->columns[i];
1850 unsigned int score = score_partial_match(column->name, 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
1859 *columnp = best_match;
1860 if (best_match) {
1861 return NULL;
1862 } else if (best_score) {
1863 return xasprintf("%s contains more than one column whose name "
1864 "matches \"%s\"", table->class->name, column_name);
1865 } else {
1866 return xasprintf("%s does not contain a column whose name matches "
1867 "\"%s\"", table->class->name, column_name);
1868 }
1869 }
1870
1871 static char * WARN_UNUSED_RESULT
1872 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1873 const struct ovsdb_idl_column **columnp,
1874 char **keyp, char **valuep)
1875 {
1876 const char *p = arg;
1877 char *error;
1878
1879 assert(columnp || keyp);
1880 if (keyp) {
1881 *keyp = NULL;
1882 }
1883 if (valuep) {
1884 *valuep = NULL;
1885 }
1886
1887 /* Parse column name. */
1888 if (columnp) {
1889 char *column_name;
1890
1891 error = ovsdb_token_parse(&p, &column_name);
1892 if (error) {
1893 goto error;
1894 }
1895 if (column_name[0] == '\0') {
1896 free(column_name);
1897 error = xasprintf("%s: missing column name", arg);
1898 goto error;
1899 }
1900 error = get_column(table, column_name, columnp);
1901 free(column_name);
1902 if (error) {
1903 goto error;
1904 }
1905 }
1906
1907 /* Parse key string. */
1908 if (*p == ':' || !columnp) {
1909 if (columnp) {
1910 p++;
1911 } else if (!keyp) {
1912 error = xasprintf("%s: key not accepted here", arg);
1913 goto error;
1914 }
1915 error = ovsdb_token_parse(&p, keyp);
1916 if (error) {
1917 goto error;
1918 }
1919 } else if (keyp) {
1920 *keyp = NULL;
1921 }
1922
1923 /* Parse value string. */
1924 if (*p == '=') {
1925 if (!valuep) {
1926 error = xasprintf("%s: value not accepted here", arg);
1927 goto error;
1928 }
1929 *valuep = xstrdup(p + 1);
1930 } else {
1931 if (valuep) {
1932 *valuep = NULL;
1933 }
1934 if (*p != '\0') {
1935 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1936 arg, p);
1937 goto error;
1938 }
1939 }
1940 return NULL;
1941
1942 error:
1943 if (columnp) {
1944 *columnp = NULL;
1945 }
1946 if (keyp) {
1947 free(*keyp);
1948 *keyp = NULL;
1949 }
1950 if (valuep) {
1951 free(*valuep);
1952 *valuep = NULL;
1953 }
1954 return error;
1955 }
1956
1957 static void
1958 cmd_get(struct vsctl_context *ctx)
1959 {
1960 bool if_exists = shash_find(&ctx->options, "--if-exists");
1961 const char *table_name = ctx->argv[1];
1962 const char *record_id = ctx->argv[2];
1963 const struct vsctl_table_class *table;
1964 const struct ovsdb_idl_row *row;
1965 struct ds *out = &ctx->output;
1966 int i;
1967
1968 table = get_table(table_name);
1969 row = must_get_row(ctx, table, record_id);
1970 for (i = 3; i < ctx->argc; i++) {
1971 const struct ovsdb_idl_column *column;
1972 struct ovsdb_datum datum;
1973 char *key_string;
1974
1975 die_if_error(parse_column_key_value(ctx->argv[i], table,
1976 &column, &key_string, NULL));
1977
1978 ovsdb_idl_txn_read(row, column, &datum);
1979 if (key_string) {
1980 union ovsdb_atom key;
1981 unsigned int idx;
1982
1983 if (column->type.value.type == OVSDB_TYPE_VOID) {
1984 vsctl_fatal("cannot specify key to get for non-map column %s",
1985 column->name);
1986 }
1987
1988 die_if_error(ovsdb_atom_from_string(&key,
1989 &column->type.key,
1990 key_string));
1991
1992 idx = ovsdb_datum_find_key(&datum, &key,
1993 column->type.key.type);
1994 if (idx == UINT_MAX) {
1995 if (!if_exists) {
1996 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
1997 key_string, table->class->name, record_id,
1998 column->name);
1999 }
2000 } else {
2001 ovsdb_atom_to_string(&datum.values[idx],
2002 column->type.value.type, out);
2003 }
2004 ovsdb_atom_destroy(&key, column->type.key.type);
2005 } else {
2006 ovsdb_datum_to_string(&datum, &column->type, out);
2007 }
2008 ds_put_char(out, '\n');
2009 ovsdb_datum_destroy(&datum, &column->type);
2010
2011 free(key_string);
2012 }
2013 }
2014
2015 static void
2016 list_record(const struct vsctl_table_class *table,
2017 const struct ovsdb_idl_row *row, struct ds *out)
2018 {
2019 size_t i;
2020
2021 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2022 UUID_ARGS(&row->uuid));
2023 for (i = 0; i < table->class->n_columns; i++) {
2024 const struct ovsdb_idl_column *column = &table->class->columns[i];
2025 struct ovsdb_datum datum;
2026
2027 ovsdb_idl_txn_read(row, column, &datum);
2028
2029 ds_put_format(out, "%-20s: ", column->name);
2030 ovsdb_datum_to_string(&datum, &column->type, out);
2031 ds_put_char(out, '\n');
2032
2033 ovsdb_datum_destroy(&datum, &column->type);
2034 }
2035 }
2036
2037 static void
2038 cmd_list(struct vsctl_context *ctx)
2039 {
2040 const char *table_name = ctx->argv[1];
2041 const struct vsctl_table_class *table;
2042 struct ds *out = &ctx->output;
2043 int i;
2044
2045 table = get_table(table_name);
2046 if (ctx->argc > 2) {
2047 for (i = 2; i < ctx->argc; i++) {
2048 if (i > 2) {
2049 ds_put_char(out, '\n');
2050 }
2051 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2052 }
2053 } else {
2054 const struct ovsdb_idl_row *row;
2055 bool first;
2056
2057 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2058 row != NULL;
2059 row = ovsdb_idl_next_row(row), first = false) {
2060 if (!first) {
2061 ds_put_char(out, '\n');
2062 }
2063 list_record(table, row, out);
2064 }
2065 }
2066 }
2067
2068 static void
2069 set_column(const struct vsctl_table_class *table,
2070 const struct ovsdb_idl_row *row, const char *arg)
2071 {
2072 const struct ovsdb_idl_column *column;
2073 char *key_string, *value_string;
2074 char *error;
2075
2076 error = parse_column_key_value(arg, table, &column, &key_string,
2077 &value_string);
2078 die_if_error(error);
2079 if (!value_string) {
2080 vsctl_fatal("%s: missing value", arg);
2081 }
2082
2083 if (key_string) {
2084 union ovsdb_atom key, value;
2085 struct ovsdb_datum old, new;
2086
2087 if (column->type.value.type == OVSDB_TYPE_VOID) {
2088 vsctl_fatal("cannot specify key to set for non-map column %s",
2089 column->name);
2090 }
2091
2092 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2093 key_string));
2094 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2095 value_string));
2096
2097 ovsdb_datum_init_empty(&new);
2098 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2099
2100 ovsdb_atom_destroy(&key, column->type.key.type);
2101 ovsdb_atom_destroy(&value, column->type.value.type);
2102
2103 ovsdb_idl_txn_read(row, column, &old);
2104 ovsdb_datum_union(&old, &new, &column->type, true);
2105 ovsdb_idl_txn_write(row, column, &old);
2106
2107 ovsdb_datum_destroy(&new, &column->type);
2108 } else {
2109 struct ovsdb_datum datum;
2110
2111 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2112 value_string));
2113 ovsdb_idl_txn_write(row, column, &datum);
2114 }
2115
2116 free(key_string);
2117 free(value_string);
2118 }
2119
2120 static void
2121 cmd_set(struct vsctl_context *ctx)
2122 {
2123 const char *table_name = ctx->argv[1];
2124 const char *record_id = ctx->argv[2];
2125 const struct vsctl_table_class *table;
2126 const struct ovsdb_idl_row *row;
2127 int i;
2128
2129 table = get_table(table_name);
2130 row = must_get_row(ctx, table, record_id);
2131 for (i = 3; i < ctx->argc; i++) {
2132 set_column(table, row, ctx->argv[i]);
2133 }
2134 }
2135
2136 static void
2137 cmd_add(struct vsctl_context *ctx)
2138 {
2139 const char *table_name = ctx->argv[1];
2140 const char *record_id = ctx->argv[2];
2141 const char *column_name = ctx->argv[3];
2142 const struct vsctl_table_class *table;
2143 const struct ovsdb_idl_column *column;
2144 const struct ovsdb_idl_row *row;
2145 const struct ovsdb_type *type;
2146 struct ovsdb_datum old;
2147 int i;
2148
2149 table = get_table(table_name);
2150 row = must_get_row(ctx, table, record_id);
2151 die_if_error(get_column(table, column_name, &column));
2152
2153 type = &column->type;
2154 ovsdb_idl_txn_read(row, column, &old);
2155 for (i = 4; i < ctx->argc; i++) {
2156 struct ovsdb_type add_type;
2157 struct ovsdb_datum add;
2158
2159 add_type = *type;
2160 add_type.n_min = 1;
2161 add_type.n_max = UINT_MAX;
2162 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2163 ovsdb_datum_union(&old, &add, type, false);
2164 ovsdb_datum_destroy(&add, type);
2165 }
2166 if (old.n > type->n_max) {
2167 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2168 "table %s but the maximum number is %u",
2169 old.n,
2170 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2171 column->name, table->class->name, type->n_max);
2172 }
2173 ovsdb_idl_txn_write(row, column, &old);
2174 }
2175
2176 static void
2177 cmd_remove(struct vsctl_context *ctx)
2178 {
2179 const char *table_name = ctx->argv[1];
2180 const char *record_id = ctx->argv[2];
2181 const char *column_name = ctx->argv[3];
2182 const struct vsctl_table_class *table;
2183 const struct ovsdb_idl_column *column;
2184 const struct ovsdb_idl_row *row;
2185 const struct ovsdb_type *type;
2186 struct ovsdb_datum old;
2187 int i;
2188
2189 table = get_table(table_name);
2190 row = must_get_row(ctx, table, record_id);
2191 die_if_error(get_column(table, column_name, &column));
2192
2193 type = &column->type;
2194 ovsdb_idl_txn_read(row, column, &old);
2195 for (i = 4; i < ctx->argc; i++) {
2196 struct ovsdb_type rm_type;
2197 struct ovsdb_datum rm;
2198 char *error;
2199
2200 rm_type = *type;
2201 rm_type.n_min = 1;
2202 rm_type.n_max = UINT_MAX;
2203 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2204 if (error && ovsdb_type_is_map(&rm_type)) {
2205 free(error);
2206 rm_type.value.type = OVSDB_TYPE_VOID;
2207 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2208 }
2209 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2210 ovsdb_datum_destroy(&rm, &rm_type);
2211 }
2212 if (old.n < type->n_min) {
2213 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2214 "table %s but the minimun number is %u",
2215 old.n,
2216 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2217 column->name, table->class->name, type->n_min);
2218 }
2219 ovsdb_idl_txn_write(row, column, &old);
2220 }
2221
2222 static void
2223 cmd_clear(struct vsctl_context *ctx)
2224 {
2225 const char *table_name = ctx->argv[1];
2226 const char *record_id = ctx->argv[2];
2227 const struct vsctl_table_class *table;
2228 const struct ovsdb_idl_row *row;
2229 int i;
2230
2231 table = get_table(table_name);
2232 row = must_get_row(ctx, table, record_id);
2233 for (i = 3; i < ctx->argc; i++) {
2234 const struct ovsdb_idl_column *column;
2235 const struct ovsdb_type *type;
2236 struct ovsdb_datum datum;
2237
2238 die_if_error(get_column(table, ctx->argv[i], &column));
2239
2240 type = &column->type;
2241 if (type->n_min > 0) {
2242 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2243 "of table %s, which is not allowed to be empty",
2244 column->name, table->class->name);
2245 }
2246
2247 ovsdb_datum_init_empty(&datum);
2248 ovsdb_idl_txn_write(row, column, &datum);
2249 }
2250 }
2251
2252 static void
2253 cmd_create(struct vsctl_context *ctx)
2254 {
2255 const char *table_name = ctx->argv[1];
2256 const struct vsctl_table_class *table;
2257 const struct ovsdb_idl_row *row;
2258 int i;
2259
2260 table = get_table(table_name);
2261 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2262 for (i = 2; i < ctx->argc; i++) {
2263 set_column(table, row, ctx->argv[i]);
2264 }
2265 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2266 }
2267
2268 /* This function may be used as the 'postprocess' function for commands that
2269 * insert new rows into the database. It expects that the command's 'run'
2270 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2271 * sole output. It replaces that output by the row's permanent UUID assigned
2272 * by the database server and appends a new-line.
2273 *
2274 * Currently we use this only for "create", because the higher-level commands
2275 * are supposed to be independent of the actual structure of the vswitch
2276 * configuration. */
2277 static void
2278 post_create(struct vsctl_context *ctx)
2279 {
2280 const struct uuid *real;
2281 struct uuid dummy;
2282
2283 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2284 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2285 if (real) {
2286 ds_clear(&ctx->output);
2287 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2288 }
2289 ds_put_char(&ctx->output, '\n');
2290 }
2291
2292 static void
2293 cmd_destroy(struct vsctl_context *ctx)
2294 {
2295 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2296 const char *table_name = ctx->argv[1];
2297 const struct vsctl_table_class *table;
2298 int i;
2299
2300 table = get_table(table_name);
2301 for (i = 2; i < ctx->argc; i++) {
2302 const struct ovsdb_idl_row *row;
2303
2304 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2305 if (row) {
2306 ovsdb_idl_txn_delete(row);
2307 }
2308 }
2309 }
2310 \f
2311 static struct json *
2312 where_uuid_equals(const struct uuid *uuid)
2313 {
2314 return
2315 json_array_create_1(
2316 json_array_create_3(
2317 json_string_create("_uuid"),
2318 json_string_create("=="),
2319 json_array_create_2(
2320 json_string_create("uuid"),
2321 json_string_create_nocopy(
2322 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2323 }
2324
2325 static void
2326 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2327 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2328 const struct ovsrec_open_vswitch *ovs)
2329 {
2330 ctx->argc = command->argc;
2331 ctx->argv = command->argv;
2332 ctx->options = command->options;
2333
2334 ds_swap(&ctx->output, &command->output);
2335 ctx->idl = idl;
2336 ctx->txn = txn;
2337 ctx->ovs = ovs;
2338
2339 }
2340
2341 static void
2342 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2343 {
2344 ds_swap(&ctx->output, &command->output);
2345 }
2346
2347 static void
2348 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2349 struct ovsdb_idl *idl)
2350 {
2351 struct ovsdb_idl_txn *txn;
2352 const struct ovsrec_open_vswitch *ovs;
2353 enum ovsdb_idl_txn_status status;
2354 struct vsctl_command *c;
2355 int64_t next_cfg = 0;
2356 char *error;
2357
2358 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2359 if (dry_run) {
2360 ovsdb_idl_txn_set_dry_run(txn);
2361 }
2362
2363 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2364
2365 ovs = ovsrec_open_vswitch_first(idl);
2366 if (!ovs) {
2367 /* XXX add verification that table is empty */
2368 ovs = ovsrec_open_vswitch_insert(txn);
2369 }
2370
2371 if (wait_for_reload) {
2372 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2373 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2374 json_destroy(where);
2375 }
2376
2377 for (c = commands; c < &commands[n_commands]; c++) {
2378 struct vsctl_context ctx;
2379
2380 ds_init(&c->output);
2381 vsctl_context_init(&ctx, c, idl, txn, ovs);
2382 (c->syntax->run)(&ctx);
2383 vsctl_context_done(&ctx, c);
2384 }
2385
2386 status = ovsdb_idl_txn_commit_block(txn);
2387 if (wait_for_reload && status == TXN_SUCCESS) {
2388 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2389 }
2390 for (c = commands; c < &commands[n_commands]; c++) {
2391 if (c->syntax->postprocess) {
2392 struct vsctl_context ctx;
2393
2394 vsctl_context_init(&ctx, c, idl, txn, ovs);
2395 (c->syntax->postprocess)(&ctx);
2396 vsctl_context_done(&ctx, c);
2397 }
2398 }
2399 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2400 ovsdb_idl_txn_destroy(txn);
2401 the_idl_txn = NULL;
2402
2403 switch (status) {
2404 case TXN_INCOMPLETE:
2405 NOT_REACHED();
2406
2407 case TXN_ABORTED:
2408 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2409 vsctl_fatal("transaction aborted");
2410
2411 case TXN_UNCHANGED:
2412 case TXN_SUCCESS:
2413 break;
2414
2415 case TXN_TRY_AGAIN:
2416 for (c = commands; c < &commands[n_commands]; c++) {
2417 ds_destroy(&c->output);
2418 }
2419 free(error);
2420 return;
2421
2422 case TXN_ERROR:
2423 vsctl_fatal("transaction error: %s", error);
2424
2425 default:
2426 NOT_REACHED();
2427 }
2428 free(error);
2429
2430 for (c = commands; c < &commands[n_commands]; c++) {
2431 struct ds *ds = &c->output;
2432 if (oneline) {
2433 size_t j;
2434
2435 ds_chomp(ds, '\n');
2436 for (j = 0; j < ds->length; j++) {
2437 int c = ds->string[j];
2438 switch (c) {
2439 case '\n':
2440 fputs("\\n", stdout);
2441 break;
2442
2443 case '\\':
2444 fputs("\\\\", stdout);
2445 break;
2446
2447 default:
2448 putchar(c);
2449 }
2450 }
2451 putchar('\n');
2452 } else {
2453 fputs(ds_cstr(ds), stdout);
2454 }
2455 ds_destroy(&c->output);
2456 shash_destroy(&c->options);
2457 }
2458 free(commands);
2459
2460 if (wait_for_reload && status != TXN_UNCHANGED) {
2461 for (;;) {
2462 const struct ovsrec_open_vswitch *ovs;
2463
2464 ovsdb_idl_run(idl);
2465 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2466 if (ovs->cur_cfg >= next_cfg) {
2467 goto done;
2468 }
2469 }
2470 ovsdb_idl_wait(idl);
2471 poll_block();
2472 }
2473 done: ;
2474 }
2475 ovsdb_idl_destroy(idl);
2476
2477 exit(EXIT_SUCCESS);
2478 }
2479
2480 static const struct vsctl_command_syntax all_commands[] = {
2481 /* Open vSwitch commands. */
2482 {"init", 0, 0, cmd_init, NULL, ""},
2483
2484 /* Bridge commands. */
2485 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2486 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2487 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2488 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2489 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2490 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2491 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2492 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2493
2494 /* Port commands. */
2495 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2496 {"add-port", 2, 2, cmd_add_port, NULL, "--may-exist"},
2497 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2498 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2499 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2500
2501 /* Interface commands. */
2502 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2503 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2504
2505 /* Controller commands. */
2506 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2507 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2508 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2509 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2510 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2511 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2512
2513 /* SSL commands. */
2514 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2515 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2516 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2517
2518 /* Parameter commands. */
2519 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2520 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2521 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2522 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2523 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2524 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2525 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2526 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2527
2528 {NULL, 0, 0, NULL, NULL, NULL},
2529 };
2530