]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-vsctl.c
Revert "utilities/ovs-ctl: Force removal of ip_gre/gre"
[mirror_ovs.git] / utilities / ovs-vsctl.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
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 <ctype.h>
20 #include <errno.h>
21 #include <float.h>
22 #include <getopt.h>
23 #include <inttypes.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "db-ctl-base.h"
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "dirs.h"
35 #include "openvswitch/dynamic-string.h"
36 #include "fatal-signal.h"
37 #include "hash.h"
38 #include "openvswitch/json.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-idl.h"
41 #include "openvswitch/poll-loop.h"
42 #include "process.h"
43 #include "stream.h"
44 #include "stream-ssl.h"
45 #include "smap.h"
46 #include "sset.h"
47 #include "svec.h"
48 #include "lib/vswitch-idl.h"
49 #include "table.h"
50 #include "timeval.h"
51 #include "util.h"
52 #include "openvswitch/vconn.h"
53 #include "openvswitch/vlog.h"
54
55 VLOG_DEFINE_THIS_MODULE(vsctl);
56
57 struct vsctl_context;
58
59 /* --db: The database server to contact. */
60 static const char *db;
61
62 /* --oneline: Write each command's output as a single line? */
63 static bool oneline;
64
65 /* --dry-run: Do not commit any changes. */
66 static bool dry_run;
67
68 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
69 static bool wait_for_reload = true;
70
71 /* --timeout: Time to wait for a connection to 'db'. */
72 static int timeout;
73
74 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
75 * If false and --db says to use an active connection method (e.g. "unix:",
76 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
77 * error if the database server cannot be contacted (e.g. ovsdb-server is not
78 * running).
79 *
80 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
81 * wait. */
82 static bool retry;
83
84 /* Format for table output. */
85 static struct table_style table_style = TABLE_STYLE_DEFAULT;
86
87 static void vsctl_cmd_init(void);
88
89 /* The IDL we're using and the current transaction, if any.
90 * This is for use by vsctl_exit() only, to allow it to clean up.
91 * Other code should use its context arguments. */
92 static struct ovsdb_idl *the_idl;
93 static struct ovsdb_idl_txn *the_idl_txn;
94 OVS_NO_RETURN static void vsctl_exit(int status);
95
96 OVS_NO_RETURN static void usage(void);
97 static void parse_options(int argc, char *argv[], struct shash *local_options);
98 static void run_prerequisites(struct ctl_command[], size_t n_commands,
99 struct ovsdb_idl *);
100 static bool do_vsctl(const char *args, struct ctl_command *, size_t n,
101 struct ovsdb_idl *);
102
103 /* post_db_reload_check frame work is to allow ovs-vsctl to do additional
104 * checks after OVSDB transactions are successfully recorded and reload by
105 * ovs-vswitchd.
106 *
107 * For example, When a new interface is added to OVSDB, ovs-vswitchd will
108 * either store a positive values on successful implementing the new
109 * interface, or -1 on failure.
110 *
111 * Unless --no-wait command line option is specified,
112 * post_db_reload_do_checks() is called right after any configuration
113 * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
114 * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
115 * more detailed messages about those errors.
116 *
117 * Current implementation only check for Post OVSDB reload failures on new
118 * interface additions with 'add-br' and 'add-port' commands.
119 *
120 * post_db_reload_expect_iface()
121 *
122 * keep track of interfaces to be checked post OVSDB reload. */
123 static void post_db_reload_check_init(void);
124 static void post_db_reload_do_checks(const struct vsctl_context *);
125 static void post_db_reload_expect_iface(const struct ovsrec_interface *);
126
127 static struct uuid *neoteric_ifaces;
128 static size_t n_neoteric_ifaces;
129 static size_t allocated_neoteric_ifaces;
130
131 int
132 main(int argc, char *argv[])
133 {
134 struct ovsdb_idl *idl;
135 struct ctl_command *commands;
136 struct shash local_options;
137 unsigned int seqno;
138 size_t n_commands;
139
140 set_program_name(argv[0]);
141 fatal_ignore_sigpipe();
142 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
143 vlog_set_levels_from_string_assert("reconnect:warn");
144
145 vsctl_cmd_init();
146
147 /* Parse command line. */
148 char *args = process_escape_args(argv);
149 shash_init(&local_options);
150 parse_options(argc, argv, &local_options);
151 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
152 &n_commands);
153 VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
154 "Called as %s", args);
155
156 if (timeout) {
157 time_alarm(timeout);
158 }
159
160 /* Initialize IDL. */
161 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
162 run_prerequisites(commands, n_commands, idl);
163
164 /* Execute the commands.
165 *
166 * 'seqno' is the database sequence number for which we last tried to
167 * execute our transaction. There's no point in trying to commit more than
168 * once for any given sequence number, because if the transaction fails
169 * it's because the database changed and we need to obtain an up-to-date
170 * view of the database before we try the transaction again. */
171 seqno = ovsdb_idl_get_seqno(idl);
172 for (;;) {
173 ovsdb_idl_run(idl);
174 if (!ovsdb_idl_is_alive(idl)) {
175 int retval = ovsdb_idl_get_last_error(idl);
176 ctl_fatal("%s: database connection failed (%s)",
177 db, ovs_retval_to_string(retval));
178 }
179
180 if (seqno != ovsdb_idl_get_seqno(idl)) {
181 seqno = ovsdb_idl_get_seqno(idl);
182 if (do_vsctl(args, commands, n_commands, idl)) {
183 free(args);
184 exit(EXIT_SUCCESS);
185 }
186 }
187
188 if (seqno == ovsdb_idl_get_seqno(idl)) {
189 ovsdb_idl_wait(idl);
190 poll_block();
191 }
192 }
193 }
194
195 static void
196 parse_options(int argc, char *argv[], struct shash *local_options)
197 {
198 enum {
199 OPT_DB = UCHAR_MAX + 1,
200 OPT_ONELINE,
201 OPT_NO_SYSLOG,
202 OPT_NO_WAIT,
203 OPT_DRY_RUN,
204 OPT_BOOTSTRAP_CA_CERT,
205 OPT_PEER_CA_CERT,
206 OPT_LOCAL,
207 OPT_RETRY,
208 OPT_COMMANDS,
209 OPT_OPTIONS,
210 VLOG_OPTION_ENUMS,
211 TABLE_OPTION_ENUMS,
212 SSL_OPTION_ENUMS,
213 };
214 static const struct option global_long_options[] = {
215 {"db", required_argument, NULL, OPT_DB},
216 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
217 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
218 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
219 {"oneline", no_argument, NULL, OPT_ONELINE},
220 {"timeout", required_argument, NULL, 't'},
221 {"retry", no_argument, NULL, OPT_RETRY},
222 {"help", no_argument, NULL, 'h'},
223 {"commands", no_argument, NULL, OPT_COMMANDS},
224 {"options", no_argument, NULL, OPT_OPTIONS},
225 {"version", no_argument, NULL, 'V'},
226 VLOG_LONG_OPTIONS,
227 TABLE_LONG_OPTIONS,
228 STREAM_SSL_LONG_OPTIONS,
229 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
230 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
231 {NULL, 0, NULL, 0},
232 };
233 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
234 char *tmp, *short_options;
235
236 struct option *options;
237 size_t allocated_options;
238 size_t n_options;
239 size_t i;
240
241 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
242 short_options = xasprintf("+%s", tmp);
243 free(tmp);
244
245 /* We want to parse both global and command-specific options here, but
246 * getopt_long() isn't too convenient for the job. We copy our global
247 * options into a dynamic array, then append all of the command-specific
248 * options. */
249 options = xmemdup(global_long_options, sizeof global_long_options);
250 allocated_options = ARRAY_SIZE(global_long_options);
251 n_options = n_global_long_options;
252 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
253
254 for (;;) {
255 int idx;
256 int c;
257
258 c = getopt_long(argc, argv, short_options, options, &idx);
259 if (c == -1) {
260 break;
261 }
262
263 switch (c) {
264 case OPT_DB:
265 db = optarg;
266 break;
267
268 case OPT_ONELINE:
269 oneline = true;
270 break;
271
272 case OPT_NO_SYSLOG:
273 vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
274 break;
275
276 case OPT_NO_WAIT:
277 wait_for_reload = false;
278 break;
279
280 case OPT_DRY_RUN:
281 dry_run = true;
282 break;
283
284 case OPT_LOCAL:
285 if (shash_find(local_options, options[idx].name)) {
286 ctl_fatal("'%s' option specified multiple times",
287 options[idx].name);
288 }
289 shash_add_nocopy(local_options,
290 xasprintf("--%s", options[idx].name),
291 nullable_xstrdup(optarg));
292 break;
293
294 case 'h':
295 usage();
296
297 case OPT_COMMANDS:
298 ctl_print_commands();
299 /* fall through */
300
301 case OPT_OPTIONS:
302 ctl_print_options(global_long_options);
303 /* fall through */
304
305 case 'V':
306 ovs_print_version(0, 0);
307 printf("DB Schema %s\n", ovsrec_get_db_version());
308 exit(EXIT_SUCCESS);
309
310 case 't':
311 timeout = strtoul(optarg, NULL, 10);
312 if (timeout < 0) {
313 ctl_fatal("value %s on -t or --timeout is invalid",
314 optarg);
315 }
316 break;
317
318 case OPT_RETRY:
319 retry = true;
320 break;
321
322 VLOG_OPTION_HANDLERS
323 TABLE_OPTION_HANDLERS(&table_style)
324
325 STREAM_SSL_OPTION_HANDLERS
326
327 case OPT_PEER_CA_CERT:
328 stream_ssl_set_peer_ca_cert_file(optarg);
329 break;
330
331 case OPT_BOOTSTRAP_CA_CERT:
332 stream_ssl_set_ca_cert_file(optarg, true);
333 break;
334
335 case '?':
336 exit(EXIT_FAILURE);
337
338 default:
339 abort();
340 }
341 }
342 free(short_options);
343
344 if (!db) {
345 db = ctl_default_db();
346 }
347
348 for (i = n_global_long_options; options[i].name; i++) {
349 free(CONST_CAST(char *, options[i].name));
350 }
351 free(options);
352 }
353
354 static void
355 usage(void)
356 {
357 printf("\
358 %s: ovs-vswitchd management utility\n\
359 usage: %s [OPTIONS] COMMAND [ARG...]\n\
360 \n\
361 Open vSwitch commands:\n\
362 init initialize database, if not yet initialized\n\
363 show print overview of database contents\n\
364 emer-reset reset configuration to clean state\n\
365 \n\
366 Bridge commands:\n\
367 add-br BRIDGE create a new bridge named BRIDGE\n\
368 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
369 del-br BRIDGE delete BRIDGE and all of its ports\n\
370 list-br print the names of all the bridges\n\
371 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
372 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
373 br-to-parent BRIDGE print the parent of BRIDGE\n\
374 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
375 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
376 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
377 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
378 \n\
379 Port commands (a bond is considered to be a single port):\n\
380 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
381 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
382 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
383 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
384 port-to-br PORT print name of bridge that contains PORT\n\
385 \n\
386 Interface commands (a bond consists of multiple interfaces):\n\
387 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
388 iface-to-br IFACE print name of bridge that contains IFACE\n\
389 \n\
390 Controller commands:\n\
391 get-controller BRIDGE print the controllers for BRIDGE\n\
392 del-controller BRIDGE delete the controllers for BRIDGE\n\
393 [--inactivity-probe=MSECS]\n\
394 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
395 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
396 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
397 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
398 \n\
399 Manager commands:\n\
400 get-manager print the managers\n\
401 del-manager delete the managers\n\
402 [--inactivity-probe=MSECS]\n\
403 set-manager TARGET... set the list of managers to TARGET...\n\
404 \n\
405 SSL commands:\n\
406 get-ssl print the SSL configuration\n\
407 del-ssl delete the SSL configuration\n\
408 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
409 \n\
410 Auto Attach commands:\n\
411 add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
412 del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
413 get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
414 \n\
415 Switch commands:\n\
416 emer-reset reset switch to known good state\n\
417 \n\
418 %s\
419 %s\
420 \n\
421 Options:\n\
422 --db=DATABASE connect to DATABASE\n\
423 (default: %s)\n\
424 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
425 --retry keep trying to connect to server forever\n\
426 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
427 --dry-run do not commit changes to database\n\
428 --oneline print exactly one line of output per command\n",
429 program_name, program_name, ctl_get_db_cmd_usage(),
430 ctl_list_db_tables_usage(), ctl_default_db());
431 table_usage();
432 vlog_usage();
433 printf("\
434 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
435 stream_usage("database", true, true, true);
436 printf("\n\
437 Other options:\n\
438 -h, --help display this help message\n\
439 -V, --version display version information\n");
440 exit(EXIT_SUCCESS);
441 }
442
443 \f
444 /* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
445 struct vsctl_context {
446 struct ctl_context base;
447
448 /* Modifiable state. */
449 const struct ovsrec_open_vswitch *ovs;
450 bool verified_ports;
451
452 /* A cache of the contents of the database.
453 *
454 * A command that needs to use any of this information must first call
455 * vsctl_context_populate_cache(). A command that changes anything that
456 * could invalidate the cache must either call
457 * vsctl_context_invalidate_cache() or manually update the cache to
458 * maintain its correctness. */
459 bool cache_valid;
460 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
461 struct shash ports; /* Maps from port name to struct vsctl_port. */
462 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
463 };
464
465 struct vsctl_bridge {
466 struct ovsrec_bridge *br_cfg;
467 char *name;
468 struct ovs_list ports; /* Contains "struct vsctl_port"s. */
469
470 /* VLAN ("fake") bridge support.
471 *
472 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
473 * in either case. */
474 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
475 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
476 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
477 int vlan; /* VLAN VID (0...4095), or 0. */
478 };
479
480 struct vsctl_port {
481 struct ovs_list ports_node; /* In struct vsctl_bridge's 'ports' list. */
482 struct ovs_list ifaces; /* Contains "struct vsctl_iface"s. */
483 struct ovsrec_port *port_cfg;
484 struct vsctl_bridge *bridge;
485 };
486
487 struct vsctl_iface {
488 struct ovs_list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
489 struct ovsrec_interface *iface_cfg;
490 struct vsctl_port *port;
491 };
492
493 /* Casts 'base' into 'struct vsctl_context'. */
494 static struct vsctl_context *
495 vsctl_context_cast(struct ctl_context *base)
496 {
497 return CONTAINER_OF(base, struct vsctl_context, base);
498 }
499
500 static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
501 int vlan);
502
503 static char *
504 vsctl_context_to_string(const struct ctl_context *ctx)
505 {
506 const struct shash_node *node;
507 struct svec words;
508 char *s;
509 int i;
510
511 svec_init(&words);
512 SHASH_FOR_EACH (node, &ctx->options) {
513 svec_add(&words, node->name);
514 }
515 for (i = 0; i < ctx->argc; i++) {
516 svec_add(&words, ctx->argv[i]);
517 }
518 svec_terminate(&words);
519
520 s = process_escape_args(words.names);
521
522 svec_destroy(&words);
523
524 return s;
525 }
526
527 static void
528 verify_ports(struct vsctl_context *vsctl_ctx)
529 {
530 if (!vsctl_ctx->verified_ports) {
531 const struct ovsrec_bridge *bridge;
532 const struct ovsrec_port *port;
533
534 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
535 OVSREC_BRIDGE_FOR_EACH (bridge, vsctl_ctx->base.idl) {
536 ovsrec_bridge_verify_ports(bridge);
537 }
538 OVSREC_PORT_FOR_EACH (port, vsctl_ctx->base.idl) {
539 ovsrec_port_verify_interfaces(port);
540 }
541
542 vsctl_ctx->verified_ports = true;
543 }
544 }
545
546 static struct vsctl_bridge *
547 add_bridge_to_cache(struct vsctl_context *vsctl_ctx,
548 struct ovsrec_bridge *br_cfg, const char *name,
549 struct vsctl_bridge *parent, int vlan)
550 {
551 struct vsctl_bridge *br = xmalloc(sizeof *br);
552 br->br_cfg = br_cfg;
553 br->name = xstrdup(name);
554 ovs_list_init(&br->ports);
555 br->parent = parent;
556 br->vlan = vlan;
557 hmap_init(&br->children);
558 if (parent) {
559 struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
560 if (conflict) {
561 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
562 "for VLAN %d, but only one is allowed",
563 parent->name, name, conflict->name, vlan);
564 } else {
565 hmap_insert(&parent->children, &br->children_node,
566 hash_int(vlan, 0));
567 }
568 }
569 shash_add(&vsctl_ctx->bridges, br->name, br);
570 return br;
571 }
572
573 static void
574 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
575 struct ovsrec_bridge *bridge)
576 {
577 struct ovsrec_bridge **bridges;
578 size_t i, n;
579
580 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
581 for (i = n = 0; i < ovs->n_bridges; i++) {
582 if (ovs->bridges[i] != bridge) {
583 bridges[n++] = ovs->bridges[i];
584 }
585 }
586 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
587 free(bridges);
588 }
589
590 static void
591 del_cached_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
592 {
593 ovs_assert(ovs_list_is_empty(&br->ports));
594 ovs_assert(hmap_is_empty(&br->children));
595 if (br->parent) {
596 hmap_remove(&br->parent->children, &br->children_node);
597 }
598 if (br->br_cfg) {
599 ovsrec_bridge_delete(br->br_cfg);
600 ovs_delete_bridge(vsctl_ctx->ovs, br->br_cfg);
601 }
602 shash_find_and_delete(&vsctl_ctx->bridges, br->name);
603 hmap_destroy(&br->children);
604 free(br->name);
605 free(br);
606 }
607
608 static bool
609 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
610 {
611 return (port_cfg->fake_bridge
612 && port_cfg->tag
613 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
614 }
615
616 static struct vsctl_bridge *
617 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
618 {
619 struct vsctl_bridge *child;
620
621 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
622 &parent->children) {
623 if (child->vlan == vlan) {
624 return child;
625 }
626 }
627
628 return NULL;
629 }
630
631 static struct vsctl_port *
632 add_port_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *parent,
633 struct ovsrec_port *port_cfg)
634 {
635 struct vsctl_port *port;
636
637 if (port_cfg->tag
638 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
639 struct vsctl_bridge *vlan_bridge;
640
641 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
642 if (vlan_bridge) {
643 parent = vlan_bridge;
644 }
645 }
646
647 port = xmalloc(sizeof *port);
648 ovs_list_push_back(&parent->ports, &port->ports_node);
649 ovs_list_init(&port->ifaces);
650 port->port_cfg = port_cfg;
651 port->bridge = parent;
652 shash_add(&vsctl_ctx->ports, port_cfg->name, port);
653
654 return port;
655 }
656
657 static void
658 del_cached_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
659 {
660 ovs_assert(ovs_list_is_empty(&port->ifaces));
661 ovs_list_remove(&port->ports_node);
662 shash_find_and_delete(&vsctl_ctx->ports, port->port_cfg->name);
663 ovsrec_port_delete(port->port_cfg);
664 free(port);
665 }
666
667 static struct vsctl_iface *
668 add_iface_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_port *parent,
669 struct ovsrec_interface *iface_cfg)
670 {
671 struct vsctl_iface *iface;
672
673 iface = xmalloc(sizeof *iface);
674 ovs_list_push_back(&parent->ifaces, &iface->ifaces_node);
675 iface->iface_cfg = iface_cfg;
676 iface->port = parent;
677 shash_add(&vsctl_ctx->ifaces, iface_cfg->name, iface);
678
679 return iface;
680 }
681
682 static void
683 del_cached_iface(struct vsctl_context *vsctl_ctx, struct vsctl_iface *iface)
684 {
685 ovs_list_remove(&iface->ifaces_node);
686 shash_find_and_delete(&vsctl_ctx->ifaces, iface->iface_cfg->name);
687 ovsrec_interface_delete(iface->iface_cfg);
688 free(iface);
689 }
690
691 static void
692 vsctl_context_invalidate_cache(struct ctl_context *ctx)
693 {
694 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
695 struct shash_node *node;
696
697 if (!vsctl_ctx->cache_valid) {
698 return;
699 }
700 vsctl_ctx->cache_valid = false;
701
702 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
703 struct vsctl_bridge *bridge = node->data;
704 hmap_destroy(&bridge->children);
705 free(bridge->name);
706 free(bridge);
707 }
708 shash_destroy(&vsctl_ctx->bridges);
709
710 shash_destroy_free_data(&vsctl_ctx->ports);
711 shash_destroy_free_data(&vsctl_ctx->ifaces);
712 }
713
714 static void
715 pre_get_info(struct ctl_context *ctx)
716 {
717 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
718
719 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
720 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
721 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
722 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
723
724 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
725 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
726 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
727 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
728
729 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
730
731 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
732 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_error);
733 }
734
735 static void
736 vsctl_context_populate_cache(struct ctl_context *ctx)
737 {
738 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
739 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
740 struct sset bridges, ports;
741 size_t i;
742
743 if (vsctl_ctx->cache_valid) {
744 /* Cache is already populated. */
745 return;
746 }
747 vsctl_ctx->cache_valid = true;
748 shash_init(&vsctl_ctx->bridges);
749 shash_init(&vsctl_ctx->ports);
750 shash_init(&vsctl_ctx->ifaces);
751
752 sset_init(&bridges);
753 sset_init(&ports);
754 for (i = 0; i < ovs->n_bridges; i++) {
755 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
756 struct vsctl_bridge *br;
757 size_t j;
758
759 if (!sset_add(&bridges, br_cfg->name)) {
760 VLOG_WARN("%s: database contains duplicate bridge name",
761 br_cfg->name);
762 continue;
763 }
764 br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
765
766 for (j = 0; j < br_cfg->n_ports; j++) {
767 struct ovsrec_port *port_cfg = br_cfg->ports[j];
768
769 if (!sset_add(&ports, port_cfg->name)) {
770 /* Duplicate port name. (We will warn about that later.) */
771 continue;
772 }
773
774 if (port_is_fake_bridge(port_cfg)
775 && sset_add(&bridges, port_cfg->name)) {
776 add_bridge_to_cache(vsctl_ctx, NULL, port_cfg->name, br,
777 *port_cfg->tag);
778 }
779 }
780 }
781 sset_destroy(&bridges);
782 sset_destroy(&ports);
783
784 sset_init(&bridges);
785 for (i = 0; i < ovs->n_bridges; i++) {
786 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
787 struct vsctl_bridge *br;
788 size_t j;
789
790 if (!sset_add(&bridges, br_cfg->name)) {
791 continue;
792 }
793 br = shash_find_data(&vsctl_ctx->bridges, br_cfg->name);
794 for (j = 0; j < br_cfg->n_ports; j++) {
795 struct ovsrec_port *port_cfg = br_cfg->ports[j];
796 struct vsctl_port *port;
797 size_t k;
798
799 port = shash_find_data(&vsctl_ctx->ports, port_cfg->name);
800 if (port) {
801 if (port_cfg == port->port_cfg) {
802 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
803 port_cfg->name, br->name, port->bridge->name);
804 } else {
805 /* Log as an error because this violates the database's
806 * uniqueness constraints, so the database server shouldn't
807 * have allowed it. */
808 VLOG_ERR("%s: database contains duplicate port name",
809 port_cfg->name);
810 }
811 continue;
812 }
813
814 if (port_is_fake_bridge(port_cfg)
815 && !sset_add(&bridges, port_cfg->name)) {
816 continue;
817 }
818
819 port = add_port_to_cache(vsctl_ctx, br, port_cfg);
820 for (k = 0; k < port_cfg->n_interfaces; k++) {
821 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
822 struct vsctl_iface *iface;
823
824 iface = shash_find_data(&vsctl_ctx->ifaces, iface_cfg->name);
825 if (iface) {
826 if (iface_cfg == iface->iface_cfg) {
827 VLOG_WARN("%s: interface is in multiple ports "
828 "(%s and %s)",
829 iface_cfg->name,
830 iface->port->port_cfg->name,
831 port->port_cfg->name);
832 } else {
833 /* Log as an error because this violates the database's
834 * uniqueness constraints, so the database server
835 * shouldn't have allowed it. */
836 VLOG_ERR("%s: database contains duplicate interface "
837 "name", iface_cfg->name);
838 }
839 continue;
840 }
841
842 add_iface_to_cache(vsctl_ctx, port, iface_cfg);
843 }
844 }
845 }
846 sset_destroy(&bridges);
847 }
848
849 static void
850 check_conflicts(struct vsctl_context *vsctl_ctx, const char *name,
851 char *msg)
852 {
853 struct vsctl_iface *iface;
854 struct vsctl_port *port;
855
856 verify_ports(vsctl_ctx);
857
858 if (shash_find(&vsctl_ctx->bridges, name)) {
859 ctl_fatal("%s because a bridge named %s already exists",
860 msg, name);
861 }
862
863 port = shash_find_data(&vsctl_ctx->ports, name);
864 if (port) {
865 ctl_fatal("%s because a port named %s already exists on "
866 "bridge %s", msg, name, port->bridge->name);
867 }
868
869 iface = shash_find_data(&vsctl_ctx->ifaces, name);
870 if (iface) {
871 ctl_fatal("%s because an interface named %s already exists "
872 "on bridge %s", msg, name, iface->port->bridge->name);
873 }
874
875 free(msg);
876 }
877
878 static struct vsctl_bridge *
879 find_bridge(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
880 {
881 struct vsctl_bridge *br;
882
883 ovs_assert(vsctl_ctx->cache_valid);
884
885 br = shash_find_data(&vsctl_ctx->bridges, name);
886 if (must_exist && !br) {
887 ctl_fatal("no bridge named %s", name);
888 }
889 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
890 return br;
891 }
892
893 static struct vsctl_bridge *
894 find_real_bridge(struct vsctl_context *vsctl_ctx,
895 const char *name, bool must_exist)
896 {
897 struct vsctl_bridge *br = find_bridge(vsctl_ctx, name, must_exist);
898 if (br && br->parent) {
899 ctl_fatal("%s is a fake bridge", name);
900 }
901 return br;
902 }
903
904 static struct vsctl_port *
905 find_port(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
906 {
907 struct vsctl_port *port;
908
909 ovs_assert(vsctl_ctx->cache_valid);
910
911 port = shash_find_data(&vsctl_ctx->ports, name);
912 if (port && !strcmp(name, port->bridge->name)) {
913 port = NULL;
914 }
915 if (must_exist && !port) {
916 ctl_fatal("no port named %s", name);
917 }
918 verify_ports(vsctl_ctx);
919 return port;
920 }
921
922 static struct vsctl_iface *
923 find_iface(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
924 {
925 struct vsctl_iface *iface;
926
927 ovs_assert(vsctl_ctx->cache_valid);
928
929 iface = shash_find_data(&vsctl_ctx->ifaces, name);
930 if (iface && !strcmp(name, iface->port->bridge->name)) {
931 iface = NULL;
932 }
933 if (must_exist && !iface) {
934 ctl_fatal("no interface named %s", name);
935 }
936 verify_ports(vsctl_ctx);
937 return iface;
938 }
939
940 static void
941 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
942 {
943 struct ovsrec_port **ports;
944 size_t i;
945
946 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
947 for (i = 0; i < br->n_ports; i++) {
948 ports[i] = br->ports[i];
949 }
950 ports[br->n_ports] = port;
951 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
952 free(ports);
953 }
954
955 static void
956 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
957 {
958 struct ovsrec_port **ports;
959 size_t i, n;
960
961 ports = xmalloc(sizeof *br->ports * br->n_ports);
962 for (i = n = 0; i < br->n_ports; i++) {
963 if (br->ports[i] != port) {
964 ports[n++] = br->ports[i];
965 }
966 }
967 ovsrec_bridge_set_ports(br, ports, n);
968 free(ports);
969 }
970
971 static void
972 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
973 struct ovsrec_bridge *bridge)
974 {
975 struct ovsrec_bridge **bridges;
976 size_t i;
977
978 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
979 for (i = 0; i < ovs->n_bridges; i++) {
980 bridges[i] = ovs->bridges[i];
981 }
982 bridges[ovs->n_bridges] = bridge;
983 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
984 free(bridges);
985 }
986
987 static void
988 cmd_init(struct ctl_context *ctx OVS_UNUSED)
989 {
990 }
991
992 static struct cmd_show_table cmd_show_tables[] = {
993 {&ovsrec_table_open_vswitch,
994 NULL,
995 {&ovsrec_open_vswitch_col_manager_options,
996 &ovsrec_open_vswitch_col_bridges,
997 &ovsrec_open_vswitch_col_ovs_version},
998 {NULL, NULL, NULL}
999 },
1000
1001 {&ovsrec_table_bridge,
1002 &ovsrec_bridge_col_name,
1003 {&ovsrec_bridge_col_controller,
1004 &ovsrec_bridge_col_fail_mode,
1005 &ovsrec_bridge_col_ports},
1006 {NULL, NULL, NULL}
1007 },
1008
1009 {&ovsrec_table_port,
1010 &ovsrec_port_col_name,
1011 {&ovsrec_port_col_tag,
1012 &ovsrec_port_col_trunks,
1013 &ovsrec_port_col_interfaces},
1014 {NULL, NULL, NULL}
1015 },
1016
1017 {&ovsrec_table_interface,
1018 &ovsrec_interface_col_name,
1019 {&ovsrec_interface_col_type,
1020 &ovsrec_interface_col_options,
1021 &ovsrec_interface_col_error,
1022 &ovsrec_interface_col_bfd_status},
1023 {NULL, NULL, NULL}
1024 },
1025
1026 {&ovsrec_table_controller,
1027 &ovsrec_controller_col_target,
1028 {&ovsrec_controller_col_is_connected,
1029 NULL,
1030 NULL},
1031 {NULL, NULL, NULL}
1032 },
1033
1034 {&ovsrec_table_manager,
1035 &ovsrec_manager_col_target,
1036 {&ovsrec_manager_col_is_connected,
1037 NULL,
1038 NULL},
1039 {NULL, NULL, NULL}
1040 },
1041
1042 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
1043 };
1044
1045 static void
1046 pre_cmd_emer_reset(struct ctl_context *ctx)
1047 {
1048 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1049 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1050
1051 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1052 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1053 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1054 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1055 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1056 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ipfix);
1057 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1058 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1059
1060 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1061
1062 ovsdb_idl_add_column(ctx->idl,
1063 &ovsrec_interface_col_ingress_policing_rate);
1064 ovsdb_idl_add_column(ctx->idl,
1065 &ovsrec_interface_col_ingress_policing_burst);
1066 }
1067
1068 static void
1069 cmd_emer_reset(struct ctl_context *ctx)
1070 {
1071 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1072 const struct ovsdb_idl *idl = ctx->idl;
1073 const struct ovsrec_bridge *br;
1074 const struct ovsrec_port *port;
1075 const struct ovsrec_interface *iface;
1076 const struct ovsrec_mirror *mirror, *next_mirror;
1077 const struct ovsrec_controller *ctrl, *next_ctrl;
1078 const struct ovsrec_manager *mgr, *next_mgr;
1079 const struct ovsrec_netflow *nf, *next_nf;
1080 const struct ovsrec_ssl *ssl, *next_ssl;
1081 const struct ovsrec_sflow *sflow, *next_sflow;
1082 const struct ovsrec_ipfix *ipfix, *next_ipfix;
1083 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1084
1085 /* Reset the Open_vSwitch table. */
1086 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, NULL, 0);
1087 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
1088
1089 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1090 const char *hwaddr;
1091
1092 ovsrec_bridge_set_controller(br, NULL, 0);
1093 ovsrec_bridge_set_fail_mode(br, NULL);
1094 ovsrec_bridge_set_mirrors(br, NULL, 0);
1095 ovsrec_bridge_set_netflow(br, NULL);
1096 ovsrec_bridge_set_sflow(br, NULL);
1097 ovsrec_bridge_set_ipfix(br, NULL);
1098 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1099
1100 /* We only want to save the "hwaddr" key from other_config. */
1101 hwaddr = smap_get(&br->other_config, "hwaddr");
1102 if (hwaddr) {
1103 const struct smap smap = SMAP_CONST1(&smap, "hwaddr", hwaddr);
1104 ovsrec_bridge_set_other_config(br, &smap);
1105 } else {
1106 ovsrec_bridge_set_other_config(br, NULL);
1107 }
1108 }
1109
1110 OVSREC_PORT_FOR_EACH (port, idl) {
1111 ovsrec_port_set_other_config(port, NULL);
1112 }
1113
1114 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1115 /* xxx What do we do about gre/patch devices created by mgr? */
1116
1117 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1118 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1119 }
1120
1121 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1122 ovsrec_mirror_delete(mirror);
1123 }
1124
1125 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1126 ovsrec_controller_delete(ctrl);
1127 }
1128
1129 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1130 ovsrec_manager_delete(mgr);
1131 }
1132
1133 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1134 ovsrec_netflow_delete(nf);
1135 }
1136
1137 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1138 ovsrec_ssl_delete(ssl);
1139 }
1140
1141 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1142 ovsrec_sflow_delete(sflow);
1143 }
1144
1145 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix, next_ipfix, idl) {
1146 ovsrec_ipfix_delete(ipfix);
1147 }
1148
1149 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset, idl) {
1150 ovsrec_flow_sample_collector_set_delete(fscset);
1151 }
1152
1153 vsctl_context_invalidate_cache(ctx);
1154 }
1155
1156 static void
1157 cmd_add_br(struct ctl_context *ctx)
1158 {
1159 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1160 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1161 const char *br_name, *parent_name;
1162 struct ovsrec_interface *iface;
1163 int vlan;
1164
1165 br_name = ctx->argv[1];
1166 if (!br_name[0]) {
1167 ctl_fatal("bridge name must not be empty string");
1168 }
1169 if (ctx->argc == 2) {
1170 parent_name = NULL;
1171 vlan = 0;
1172 } else if (ctx->argc == 4) {
1173 parent_name = ctx->argv[2];
1174 vlan = atoi(ctx->argv[3]);
1175 if (vlan < 0 || vlan > 4095) {
1176 ctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1177 }
1178 } else {
1179 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1180 ctx->argv[0]);
1181 }
1182
1183 vsctl_context_populate_cache(ctx);
1184 if (may_exist) {
1185 struct vsctl_bridge *br;
1186
1187 br = find_bridge(vsctl_ctx, br_name, false);
1188 if (br) {
1189 if (!parent_name) {
1190 if (br->parent) {
1191 ctl_fatal("\"--may-exist add-br %s\" but %s is "
1192 "a VLAN bridge for VLAN %d",
1193 br_name, br_name, br->vlan);
1194 }
1195 } else {
1196 if (!br->parent) {
1197 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1198 "is not a VLAN bridge",
1199 br_name, parent_name, vlan, br_name);
1200 } else if (strcmp(br->parent->name, parent_name)) {
1201 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1202 "has the wrong parent %s",
1203 br_name, parent_name, vlan,
1204 br_name, br->parent->name);
1205 } else if (br->vlan != vlan) {
1206 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1207 "is a VLAN bridge for the wrong VLAN %d",
1208 br_name, parent_name, vlan, br_name, br->vlan);
1209 }
1210 }
1211 return;
1212 }
1213 }
1214 check_conflicts(vsctl_ctx, br_name,
1215 xasprintf("cannot create a bridge named %s", br_name));
1216
1217 if (!parent_name) {
1218 struct ovsrec_port *port;
1219 struct ovsrec_bridge *br;
1220
1221 iface = ovsrec_interface_insert(ctx->txn);
1222 ovsrec_interface_set_name(iface, br_name);
1223 ovsrec_interface_set_type(iface, "internal");
1224
1225 port = ovsrec_port_insert(ctx->txn);
1226 ovsrec_port_set_name(port, br_name);
1227 ovsrec_port_set_interfaces(port, &iface, 1);
1228
1229 br = ovsrec_bridge_insert(ctx->txn);
1230 ovsrec_bridge_set_name(br, br_name);
1231 ovsrec_bridge_set_ports(br, &port, 1);
1232
1233 ovs_insert_bridge(vsctl_ctx->ovs, br);
1234 } else {
1235 struct vsctl_bridge *conflict;
1236 struct vsctl_bridge *parent;
1237 struct ovsrec_port *port;
1238 struct ovsrec_bridge *br;
1239 int64_t tag = vlan;
1240
1241 parent = find_bridge(vsctl_ctx, parent_name, false);
1242 if (parent && parent->parent) {
1243 ctl_fatal("cannot create bridge with fake bridge as parent");
1244 }
1245 if (!parent) {
1246 ctl_fatal("parent bridge %s does not exist", parent_name);
1247 }
1248 conflict = find_vlan_bridge(parent, vlan);
1249 if (conflict) {
1250 ctl_fatal("bridge %s already has a child VLAN bridge %s "
1251 "on VLAN %d", parent_name, conflict->name, vlan);
1252 }
1253 br = parent->br_cfg;
1254
1255 iface = ovsrec_interface_insert(ctx->txn);
1256 ovsrec_interface_set_name(iface, br_name);
1257 ovsrec_interface_set_type(iface, "internal");
1258
1259 port = ovsrec_port_insert(ctx->txn);
1260 ovsrec_port_set_name(port, br_name);
1261 ovsrec_port_set_interfaces(port, &iface, 1);
1262 ovsrec_port_set_fake_bridge(port, true);
1263 ovsrec_port_set_tag(port, &tag, 1);
1264
1265 bridge_insert_port(br, port);
1266 }
1267
1268 post_db_reload_expect_iface(iface);
1269 vsctl_context_invalidate_cache(ctx);
1270 }
1271
1272 static void
1273 del_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
1274 {
1275 struct vsctl_iface *iface, *next_iface;
1276
1277 bridge_delete_port((port->bridge->parent
1278 ? port->bridge->parent->br_cfg
1279 : port->bridge->br_cfg), port->port_cfg);
1280
1281 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1282 del_cached_iface(vsctl_ctx, iface);
1283 }
1284 del_cached_port(vsctl_ctx, port);
1285 }
1286
1287 static void
1288 del_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
1289 {
1290 struct vsctl_bridge *child, *next_child;
1291 struct vsctl_port *port, *next_port;
1292 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1293
1294 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1295 del_bridge(vsctl_ctx, child);
1296 }
1297
1298 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1299 del_port(vsctl_ctx, port);
1300 }
1301
1302 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset,
1303 vsctl_ctx->base.idl) {
1304 if (fscset->bridge == br->br_cfg) {
1305 ovsrec_flow_sample_collector_set_delete(fscset);
1306 }
1307 }
1308
1309 del_cached_bridge(vsctl_ctx, br);
1310 }
1311
1312 static void
1313 cmd_del_br(struct ctl_context *ctx)
1314 {
1315 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1316 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1317 struct vsctl_bridge *bridge;
1318
1319 vsctl_context_populate_cache(ctx);
1320 bridge = find_bridge(vsctl_ctx, ctx->argv[1], must_exist);
1321 if (bridge) {
1322 del_bridge(vsctl_ctx, bridge);
1323 }
1324 }
1325
1326 static void
1327 output_sorted(struct svec *svec, struct ds *output)
1328 {
1329 const char *name;
1330 size_t i;
1331
1332 svec_sort(svec);
1333 SVEC_FOR_EACH (i, name, svec) {
1334 ds_put_format(output, "%s\n", name);
1335 }
1336 }
1337
1338 static void
1339 cmd_list_br(struct ctl_context *ctx)
1340 {
1341 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1342 struct shash_node *node;
1343 struct svec bridges;
1344 bool real = shash_find(&ctx->options, "--real");
1345 bool fake = shash_find(&ctx->options, "--fake");
1346
1347 /* If neither fake nor real were requested, return both. */
1348 if (!real && !fake) {
1349 real = fake = true;
1350 }
1351
1352 vsctl_context_populate_cache(ctx);
1353
1354 svec_init(&bridges);
1355 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
1356 struct vsctl_bridge *br = node->data;
1357
1358 if (br->parent ? fake : real) {
1359 svec_add(&bridges, br->name);
1360 }
1361 }
1362 output_sorted(&bridges, &ctx->output);
1363 svec_destroy(&bridges);
1364 }
1365
1366 static void
1367 cmd_br_exists(struct ctl_context *ctx)
1368 {
1369 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1370
1371 vsctl_context_populate_cache(ctx);
1372 if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
1373 vsctl_exit(2);
1374 }
1375 }
1376
1377 static void
1378 set_external_id(struct smap *old, struct smap *new,
1379 char *key, char *value)
1380 {
1381 smap_clone(new, old);
1382
1383 if (value) {
1384 smap_replace(new, key, value);
1385 } else {
1386 smap_remove(new, key);
1387 }
1388 }
1389
1390 static void
1391 pre_cmd_br_set_external_id(struct ctl_context *ctx)
1392 {
1393 pre_get_info(ctx);
1394 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1395 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1396 }
1397
1398 static void
1399 cmd_br_set_external_id(struct ctl_context *ctx)
1400 {
1401 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1402 struct vsctl_bridge *bridge;
1403 struct smap new;
1404
1405 vsctl_context_populate_cache(ctx);
1406 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1407 if (bridge->br_cfg) {
1408
1409 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1410 ctx->argc >= 4 ? ctx->argv[3] : NULL);
1411 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1412 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1413 } else {
1414 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1415 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1416 ctx->argv[1]);
1417 set_external_id(&port->port_cfg->external_ids, &new,
1418 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1419 ovsrec_port_verify_external_ids(port->port_cfg);
1420 ovsrec_port_set_external_ids(port->port_cfg, &new);
1421 free(key);
1422 }
1423 smap_destroy(&new);
1424 }
1425
1426 static void
1427 get_external_id(struct smap *smap, const char *prefix, const char *key,
1428 struct ds *output)
1429 {
1430 if (key) {
1431 char *prefix_key = xasprintf("%s%s", prefix, key);
1432 const char *value = smap_get(smap, prefix_key);
1433
1434 if (value) {
1435 ds_put_format(output, "%s\n", value);
1436 }
1437 free(prefix_key);
1438 } else {
1439 const struct smap_node **sorted = smap_sort(smap);
1440 size_t prefix_len = strlen(prefix);
1441 size_t i;
1442
1443 for (i = 0; i < smap_count(smap); i++) {
1444 const struct smap_node *node = sorted[i];
1445 if (!strncmp(node->key, prefix, prefix_len)) {
1446 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1447 node->value);
1448 }
1449 }
1450 free(sorted);
1451 }
1452 }
1453
1454 static void
1455 pre_cmd_br_get_external_id(struct ctl_context *ctx)
1456 {
1457 pre_cmd_br_set_external_id(ctx);
1458 }
1459
1460 static void
1461 cmd_br_get_external_id(struct ctl_context *ctx)
1462 {
1463 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1464 struct vsctl_bridge *bridge;
1465
1466 vsctl_context_populate_cache(ctx);
1467
1468 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1469 if (bridge->br_cfg) {
1470 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1471 get_external_id(&bridge->br_cfg->external_ids, "",
1472 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1473 } else {
1474 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1475 ctx->argv[1]);
1476 ovsrec_port_verify_external_ids(port->port_cfg);
1477 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1478 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1479 }
1480 }
1481
1482 static void
1483 cmd_list_ports(struct ctl_context *ctx)
1484 {
1485 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1486 struct vsctl_bridge *br;
1487 struct vsctl_port *port;
1488 struct svec ports;
1489
1490 vsctl_context_populate_cache(ctx);
1491 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1492 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1493
1494 svec_init(&ports);
1495 LIST_FOR_EACH (port, ports_node, &br->ports) {
1496 if (strcmp(port->port_cfg->name, br->name)) {
1497 svec_add(&ports, port->port_cfg->name);
1498 }
1499 }
1500 output_sorted(&ports, &ctx->output);
1501 svec_destroy(&ports);
1502 }
1503
1504 static void
1505 add_port(struct ctl_context *ctx,
1506 const char *br_name, const char *port_name,
1507 bool may_exist, bool fake_iface,
1508 char *iface_names[], int n_ifaces,
1509 char *settings[], int n_settings)
1510 {
1511 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1512 struct vsctl_bridge *bridge;
1513 struct ovsrec_interface **ifaces;
1514 struct ovsrec_port *port;
1515 size_t i;
1516
1517 if (!port_name[0]) {
1518 ctl_fatal("port name must not be empty string");
1519 }
1520 for (i = 0; i < n_ifaces; i++) {
1521 if (!iface_names[i][0]) {
1522 ctl_fatal("interface name must not be empty string");
1523 }
1524 }
1525
1526 vsctl_context_populate_cache(ctx);
1527 if (may_exist) {
1528 struct vsctl_port *vsctl_port;
1529
1530 vsctl_port = find_port(vsctl_ctx, port_name, false);
1531 if (vsctl_port) {
1532 struct svec want_names, have_names;
1533
1534 svec_init(&want_names);
1535 for (i = 0; i < n_ifaces; i++) {
1536 svec_add(&want_names, iface_names[i]);
1537 }
1538 svec_sort(&want_names);
1539
1540 svec_init(&have_names);
1541 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1542 svec_add(&have_names,
1543 vsctl_port->port_cfg->interfaces[i]->name);
1544 }
1545 svec_sort(&have_names);
1546
1547 if (strcmp(vsctl_port->bridge->name, br_name)) {
1548 char *command = vsctl_context_to_string(ctx);
1549 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1550 command, port_name, vsctl_port->bridge->name);
1551 }
1552
1553 if (!svec_equal(&want_names, &have_names)) {
1554 char *have_names_string = svec_join(&have_names, ", ", "");
1555 char *command = vsctl_context_to_string(ctx);
1556
1557 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1558 command, port_name, have_names_string);
1559 }
1560
1561 svec_destroy(&want_names);
1562 svec_destroy(&have_names);
1563
1564 return;
1565 }
1566 }
1567 check_conflicts(vsctl_ctx, port_name,
1568 xasprintf("cannot create a port named %s", port_name));
1569 for (i = 0; i < n_ifaces; i++) {
1570 check_conflicts(vsctl_ctx, iface_names[i],
1571 xasprintf("cannot create an interface named %s",
1572 iface_names[i]));
1573 }
1574 bridge = find_bridge(vsctl_ctx, br_name, true);
1575
1576 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1577 for (i = 0; i < n_ifaces; i++) {
1578 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1579 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1580 post_db_reload_expect_iface(ifaces[i]);
1581 }
1582
1583 port = ovsrec_port_insert(ctx->txn);
1584 ovsrec_port_set_name(port, port_name);
1585 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1586 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1587
1588 if (bridge->parent) {
1589 int64_t tag = bridge->vlan;
1590 ovsrec_port_set_tag(port, &tag, 1);
1591 }
1592
1593 for (i = 0; i < n_settings; i++) {
1594 ctl_set_column("Port", &port->header_, settings[i],
1595 ctx->symtab);
1596 }
1597
1598 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1599 : bridge->br_cfg), port);
1600
1601 struct vsctl_port *vsctl_port = add_port_to_cache(vsctl_ctx, bridge, port);
1602 for (i = 0; i < n_ifaces; i++) {
1603 add_iface_to_cache(vsctl_ctx, vsctl_port, ifaces[i]);
1604 }
1605 free(ifaces);
1606 }
1607
1608 static void
1609 cmd_add_port(struct ctl_context *ctx)
1610 {
1611 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1612
1613 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1614 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1615 }
1616
1617 static void
1618 cmd_add_bond(struct ctl_context *ctx)
1619 {
1620 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1621 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1622 int n_ifaces;
1623 int i;
1624
1625 n_ifaces = ctx->argc - 3;
1626 for (i = 3; i < ctx->argc; i++) {
1627 if (strchr(ctx->argv[i], '=')) {
1628 n_ifaces = i - 3;
1629 break;
1630 }
1631 }
1632 if (n_ifaces < 2) {
1633 ctl_fatal("add-bond requires at least 2 interfaces, but only "
1634 "%d were specified", n_ifaces);
1635 }
1636
1637 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1638 &ctx->argv[3], n_ifaces,
1639 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1640 }
1641
1642 static void
1643 cmd_add_bond_iface(struct ctl_context *ctx)
1644 {
1645 vsctl_context_populate_cache(ctx);
1646
1647 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1648 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1649 struct vsctl_port *port = find_port(vsctl_ctx, ctx->argv[1], true);
1650
1651 const char *iface_name = ctx->argv[2];
1652 if (may_exist) {
1653 struct vsctl_iface *iface = find_iface(vsctl_ctx, iface_name, false);
1654 if (iface) {
1655 if (iface->port == port) {
1656 return;
1657 }
1658 char *command = vsctl_context_to_string(ctx);
1659 ctl_fatal("\"%s\" but %s is actually attached to port %s",
1660 command, iface_name, iface->port->port_cfg->name);
1661 }
1662 }
1663 check_conflicts(vsctl_ctx, iface_name,
1664 xasprintf("cannot create an interface named %s",
1665 iface_name));
1666
1667 struct ovsrec_interface *iface = ovsrec_interface_insert(ctx->txn);
1668 ovsrec_interface_set_name(iface, iface_name);
1669 ovsrec_port_update_interfaces_addvalue(port->port_cfg, iface);
1670 post_db_reload_expect_iface(iface);
1671 add_iface_to_cache(vsctl_ctx, port, iface);
1672 }
1673
1674 static void
1675 cmd_del_bond_iface(struct ctl_context *ctx)
1676 {
1677 vsctl_context_populate_cache(ctx);
1678
1679 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1680 const char *iface_name = ctx->argv[ctx->argc - 1];
1681 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1682 struct vsctl_iface *iface = find_iface(vsctl_ctx, iface_name, must_exist);
1683 if (!iface) {
1684 ovs_assert(!must_exist);
1685 return;
1686 }
1687
1688 const char *port_name = ctx->argc > 2 ? ctx->argv[1] : NULL;
1689 if (port_name) {
1690 struct vsctl_port *port = find_port(vsctl_ctx, port_name, true);
1691 if (iface->port != port) {
1692 ctl_fatal("port %s does not have an interface %s",
1693 port_name, iface_name);
1694 }
1695 }
1696
1697 if (ovs_list_is_short(&iface->port->ifaces)) {
1698 ctl_fatal("cannot delete last interface from port %s",
1699 iface->port->port_cfg->name);
1700 }
1701
1702 ovsrec_port_update_interfaces_delvalue(iface->port->port_cfg,
1703 iface->iface_cfg);
1704 del_cached_iface(vsctl_ctx, iface);
1705 }
1706
1707 static void
1708 cmd_del_port(struct ctl_context *ctx)
1709 {
1710 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1711 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1712 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1713 const char *target = ctx->argv[ctx->argc - 1];
1714 struct vsctl_port *port;
1715
1716 vsctl_context_populate_cache(ctx);
1717 if (find_bridge(vsctl_ctx, target, false)) {
1718 if (must_exist) {
1719 ctl_fatal("cannot delete port %s because it is the local port "
1720 "for bridge %s (deleting this port requires deleting "
1721 "the entire bridge)", target, target);
1722 }
1723 port = NULL;
1724 } else if (!with_iface) {
1725 port = find_port(vsctl_ctx, target, must_exist);
1726 } else {
1727 struct vsctl_iface *iface;
1728
1729 port = find_port(vsctl_ctx, target, false);
1730 if (!port) {
1731 iface = find_iface(vsctl_ctx, target, false);
1732 if (iface) {
1733 port = iface->port;
1734 }
1735 }
1736 if (must_exist && !port) {
1737 ctl_fatal("no port or interface named %s", target);
1738 }
1739 }
1740
1741 if (port) {
1742 if (ctx->argc == 3) {
1743 struct vsctl_bridge *bridge;
1744
1745 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1746 if (port->bridge != bridge) {
1747 if (port->bridge->parent == bridge) {
1748 ctl_fatal("bridge %s does not have a port %s (although "
1749 "its child bridge %s does)",
1750 ctx->argv[1], ctx->argv[2],
1751 port->bridge->name);
1752 } else {
1753 ctl_fatal("bridge %s does not have a port %s",
1754 ctx->argv[1], ctx->argv[2]);
1755 }
1756 }
1757 }
1758
1759 del_port(vsctl_ctx, port);
1760 }
1761 }
1762
1763 static void
1764 cmd_port_to_br(struct ctl_context *ctx)
1765 {
1766 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1767 struct vsctl_port *port;
1768
1769 vsctl_context_populate_cache(ctx);
1770
1771 port = find_port(vsctl_ctx, ctx->argv[1], true);
1772 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1773 }
1774
1775 static void
1776 cmd_br_to_vlan(struct ctl_context *ctx)
1777 {
1778 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1779 struct vsctl_bridge *bridge;
1780
1781 vsctl_context_populate_cache(ctx);
1782
1783 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1784 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1785 }
1786
1787 static void
1788 cmd_br_to_parent(struct ctl_context *ctx)
1789 {
1790 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1791 struct vsctl_bridge *bridge;
1792
1793 vsctl_context_populate_cache(ctx);
1794
1795 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1796 if (bridge->parent) {
1797 bridge = bridge->parent;
1798 }
1799 ds_put_format(&ctx->output, "%s\n", bridge->name);
1800 }
1801
1802 static void
1803 cmd_list_ifaces(struct ctl_context *ctx)
1804 {
1805 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1806 struct vsctl_bridge *br;
1807 struct vsctl_port *port;
1808 struct svec ifaces;
1809
1810 vsctl_context_populate_cache(ctx);
1811
1812 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1813 verify_ports(vsctl_ctx);
1814
1815 svec_init(&ifaces);
1816 LIST_FOR_EACH (port, ports_node, &br->ports) {
1817 struct vsctl_iface *iface;
1818
1819 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1820 if (strcmp(iface->iface_cfg->name, br->name)) {
1821 svec_add(&ifaces, iface->iface_cfg->name);
1822 }
1823 }
1824 }
1825 output_sorted(&ifaces, &ctx->output);
1826 svec_destroy(&ifaces);
1827 }
1828
1829 static void
1830 cmd_iface_to_br(struct ctl_context *ctx)
1831 {
1832 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1833 struct vsctl_iface *iface;
1834
1835 vsctl_context_populate_cache(ctx);
1836
1837 iface = find_iface(vsctl_ctx, ctx->argv[1], true);
1838 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1839 }
1840
1841 static void
1842 verify_controllers(struct ovsrec_bridge *bridge)
1843 {
1844 size_t i;
1845
1846 ovsrec_bridge_verify_controller(bridge);
1847 for (i = 0; i < bridge->n_controller; i++) {
1848 ovsrec_controller_verify_target(bridge->controller[i]);
1849 }
1850 }
1851
1852 static void
1853 pre_controller(struct ctl_context *ctx)
1854 {
1855 pre_get_info(ctx);
1856
1857 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1858 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_inactivity_probe);
1859 }
1860
1861 static void
1862 cmd_get_controller(struct ctl_context *ctx)
1863 {
1864 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1865 struct vsctl_bridge *br;
1866 struct svec targets;
1867 size_t i;
1868
1869 vsctl_context_populate_cache(ctx);
1870
1871 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1872 if (br->parent) {
1873 br = br->parent;
1874 }
1875 verify_controllers(br->br_cfg);
1876
1877 /* Print the targets in sorted order for reproducibility. */
1878 svec_init(&targets);
1879 for (i = 0; i < br->br_cfg->n_controller; i++) {
1880 svec_add(&targets, br->br_cfg->controller[i]->target);
1881 }
1882
1883 svec_sort(&targets);
1884 for (i = 0; i < targets.n; i++) {
1885 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1886 }
1887 svec_destroy(&targets);
1888 }
1889
1890 static void
1891 delete_controllers(struct ovsrec_controller **controllers,
1892 size_t n_controllers)
1893 {
1894 size_t i;
1895
1896 for (i = 0; i < n_controllers; i++) {
1897 ovsrec_controller_delete(controllers[i]);
1898 }
1899 }
1900
1901 static void
1902 cmd_del_controller(struct ctl_context *ctx)
1903 {
1904 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1905 struct ovsrec_bridge *br;
1906
1907 vsctl_context_populate_cache(ctx);
1908
1909 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1910 verify_controllers(br);
1911
1912 if (br->controller) {
1913 delete_controllers(br->controller, br->n_controller);
1914 ovsrec_bridge_set_controller(br, NULL, 0);
1915 }
1916 }
1917
1918 static struct ovsrec_controller **
1919 insert_controllers(struct ctl_context *ctx, char *targets[], size_t n)
1920 {
1921 struct ovsrec_controller **controllers;
1922 size_t i;
1923 const char *inactivity_probe = shash_find_data(&ctx->options,
1924 "--inactivity-probe");
1925
1926 controllers = xmalloc(n * sizeof *controllers);
1927 for (i = 0; i < n; i++) {
1928 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
1929 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1930 }
1931 controllers[i] = ovsrec_controller_insert(ctx->txn);
1932 ovsrec_controller_set_target(controllers[i], targets[i]);
1933 if (inactivity_probe) {
1934 int64_t msecs = atoll(inactivity_probe);
1935 ovsrec_controller_set_inactivity_probe(controllers[i], &msecs, 1);
1936 }
1937 }
1938
1939 return controllers;
1940 }
1941
1942 static void
1943 cmd_set_controller(struct ctl_context *ctx)
1944 {
1945 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1946 struct ovsrec_controller **controllers;
1947 struct ovsrec_bridge *br;
1948 size_t n;
1949
1950 vsctl_context_populate_cache(ctx);
1951
1952 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1953 verify_controllers(br);
1954
1955 delete_controllers(br->controller, br->n_controller);
1956
1957 n = ctx->argc - 2;
1958 controllers = insert_controllers(ctx, &ctx->argv[2], n);
1959 ovsrec_bridge_set_controller(br, controllers, n);
1960 free(controllers);
1961 }
1962
1963 static void
1964 cmd_get_fail_mode(struct ctl_context *ctx)
1965 {
1966 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1967 struct vsctl_bridge *br;
1968 const char *fail_mode;
1969
1970 vsctl_context_populate_cache(ctx);
1971 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1972
1973 if (br->parent) {
1974 br = br->parent;
1975 }
1976 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1977
1978 fail_mode = br->br_cfg->fail_mode;
1979 if (fail_mode && strlen(fail_mode)) {
1980 ds_put_format(&ctx->output, "%s\n", fail_mode);
1981 }
1982 }
1983
1984 static void
1985 cmd_del_fail_mode(struct ctl_context *ctx)
1986 {
1987 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1988 struct vsctl_bridge *br;
1989
1990 vsctl_context_populate_cache(ctx);
1991
1992 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1993
1994 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1995 }
1996
1997 static void
1998 cmd_set_fail_mode(struct ctl_context *ctx)
1999 {
2000 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2001 struct vsctl_bridge *br;
2002 const char *fail_mode = ctx->argv[2];
2003
2004 vsctl_context_populate_cache(ctx);
2005
2006 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
2007
2008 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2009 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2010 }
2011
2012 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2013 }
2014
2015 static void
2016 verify_managers(const struct ovsrec_open_vswitch *ovs)
2017 {
2018 size_t i;
2019
2020 ovsrec_open_vswitch_verify_manager_options(ovs);
2021
2022 for (i = 0; i < ovs->n_manager_options; ++i) {
2023 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2024
2025 ovsrec_manager_verify_target(mgr);
2026 }
2027 }
2028
2029 static void
2030 pre_manager(struct ctl_context *ctx)
2031 {
2032 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2033 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2034 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_inactivity_probe);
2035 }
2036
2037 static void
2038 cmd_get_manager(struct ctl_context *ctx)
2039 {
2040 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2041 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
2042 struct svec targets;
2043 size_t i;
2044
2045 verify_managers(ovs);
2046
2047 /* Print the targets in sorted order for reproducibility. */
2048 svec_init(&targets);
2049
2050 for (i = 0; i < ovs->n_manager_options; i++) {
2051 svec_add(&targets, ovs->manager_options[i]->target);
2052 }
2053
2054 svec_sort_unique(&targets);
2055 for (i = 0; i < targets.n; i++) {
2056 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2057 }
2058 svec_destroy(&targets);
2059 }
2060
2061 static void
2062 delete_managers(const struct ovsrec_open_vswitch *ovs)
2063 {
2064 size_t i;
2065
2066 /* Delete Manager rows pointed to by 'manager_options' column. */
2067 for (i = 0; i < ovs->n_manager_options; i++) {
2068 ovsrec_manager_delete(ovs->manager_options[i]);
2069 }
2070
2071 /* Delete 'Manager' row refs in 'manager_options' column. */
2072 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2073 }
2074
2075 static void
2076 cmd_del_manager(struct ctl_context *ctx)
2077 {
2078 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2079 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
2080
2081 verify_managers(ovs);
2082 delete_managers(ovs);
2083 }
2084
2085 static void
2086 insert_managers(struct vsctl_context *vsctl_ctx, char *targets[], size_t n,
2087 struct shash *options)
2088 {
2089 struct ovsrec_manager **managers;
2090 size_t i;
2091 const char *inactivity_probe = shash_find_data(options,
2092 "--inactivity-probe");
2093
2094 /* Insert each manager in a new row in Manager table. */
2095 managers = xmalloc(n * sizeof *managers);
2096 for (i = 0; i < n; i++) {
2097 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2098 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2099 }
2100 managers[i] = ovsrec_manager_insert(vsctl_ctx->base.txn);
2101 ovsrec_manager_set_target(managers[i], targets[i]);
2102 if (inactivity_probe) {
2103 int64_t msecs = atoll(inactivity_probe);
2104 ovsrec_manager_set_inactivity_probe(managers[i], &msecs, 1);
2105 }
2106 }
2107
2108 /* Store uuids of new Manager rows in 'manager_options' column. */
2109 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, managers, n);
2110 free(managers);
2111 }
2112
2113 static void
2114 cmd_set_manager(struct ctl_context *ctx)
2115 {
2116 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2117 const size_t n = ctx->argc - 1;
2118
2119 verify_managers(vsctl_ctx->ovs);
2120 delete_managers(vsctl_ctx->ovs);
2121 insert_managers(vsctl_ctx, &ctx->argv[1], n, &ctx->options);
2122 }
2123
2124 static void
2125 pre_cmd_get_ssl(struct ctl_context *ctx)
2126 {
2127 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2128
2129 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2130 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2131 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2132 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2133 }
2134
2135 static void
2136 cmd_get_ssl(struct ctl_context *ctx)
2137 {
2138 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2139 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2140
2141 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2142 if (ssl) {
2143 ovsrec_ssl_verify_private_key(ssl);
2144 ovsrec_ssl_verify_certificate(ssl);
2145 ovsrec_ssl_verify_ca_cert(ssl);
2146 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2147
2148 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2149 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2150 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2151 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2152 ssl->bootstrap_ca_cert ? "true" : "false");
2153 }
2154 }
2155
2156 static void
2157 pre_cmd_del_ssl(struct ctl_context *ctx)
2158 {
2159 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2160 }
2161
2162 static void
2163 cmd_del_ssl(struct ctl_context *ctx)
2164 {
2165 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2166 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2167
2168 if (ssl) {
2169 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2170 ovsrec_ssl_delete(ssl);
2171 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
2172 }
2173 }
2174
2175 static void
2176 pre_cmd_set_ssl(struct ctl_context *ctx)
2177 {
2178 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2179 }
2180
2181 static void
2182 cmd_set_ssl(struct ctl_context *ctx)
2183 {
2184 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2185 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2186 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2187
2188 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2189 if (ssl) {
2190 ovsrec_ssl_delete(ssl);
2191 }
2192 ssl = ovsrec_ssl_insert(ctx->txn);
2193
2194 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2195 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2196 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2197
2198 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2199
2200 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, ssl);
2201 }
2202
2203 static void
2204 autoattach_insert_mapping(struct ovsrec_autoattach *aa,
2205 int64_t isid,
2206 int64_t vlan)
2207 {
2208 int64_t *key_mappings, *value_mappings;
2209 size_t i;
2210
2211 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings + 1));
2212 value_mappings = xmalloc(sizeof *aa->value_mappings * (aa->n_mappings + 1));
2213
2214 for (i = 0; i < aa->n_mappings; i++) {
2215 key_mappings[i] = aa->key_mappings[i];
2216 value_mappings[i] = aa->value_mappings[i];
2217 }
2218 key_mappings[aa->n_mappings] = isid;
2219 value_mappings[aa->n_mappings] = vlan;
2220
2221 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings,
2222 aa->n_mappings + 1);
2223
2224 free(key_mappings);
2225 free(value_mappings);
2226 }
2227
2228 static void
2229 cmd_add_aa_mapping(struct ctl_context *ctx)
2230 {
2231 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2232 struct vsctl_bridge *br;
2233 int64_t isid, vlan;
2234 char *nptr = NULL;
2235
2236 isid = strtoull(ctx->argv[2], &nptr, 10);
2237 if (nptr == ctx->argv[2] || nptr == NULL) {
2238 ctl_fatal("Invalid argument %s", ctx->argv[2]);
2239 return;
2240 }
2241
2242 vlan = strtoull(ctx->argv[3], &nptr, 10);
2243 if (nptr == ctx->argv[3] || nptr == NULL) {
2244 ctl_fatal("Invalid argument %s", ctx->argv[3]);
2245 return;
2246 }
2247
2248 vsctl_context_populate_cache(ctx);
2249
2250 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2251 if (br->parent) {
2252 br = br->parent;
2253 }
2254
2255 if (br->br_cfg) {
2256 if (!br->br_cfg->auto_attach) {
2257 struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
2258 ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
2259 }
2260 autoattach_insert_mapping(br->br_cfg->auto_attach, isid, vlan);
2261 }
2262 }
2263
2264 static void
2265 del_aa_mapping(struct ovsrec_autoattach *aa,
2266 int64_t isid,
2267 int64_t vlan)
2268 {
2269 int64_t *key_mappings, *value_mappings;
2270 size_t i, n;
2271
2272 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings));
2273 value_mappings = xmalloc(sizeof *value_mappings * (aa->n_mappings));
2274
2275 for (i = n = 0; i < aa->n_mappings; i++) {
2276 if (aa->key_mappings[i] != isid && aa->value_mappings[i] != vlan) {
2277 key_mappings[n] = aa->key_mappings[i];
2278 value_mappings[n++] = aa->value_mappings[i];
2279 }
2280 }
2281
2282 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings, n);
2283
2284 free(key_mappings);
2285 free(value_mappings);
2286 }
2287
2288 static void
2289 cmd_del_aa_mapping(struct ctl_context *ctx)
2290 {
2291 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2292 struct vsctl_bridge *br;
2293 int64_t isid, vlan;
2294 char *nptr = NULL;
2295
2296 isid = strtoull(ctx->argv[2], &nptr, 10);
2297 if (nptr == ctx->argv[2] || nptr == NULL) {
2298 ctl_fatal("Invalid argument %s", ctx->argv[2]);
2299 return;
2300 }
2301
2302 vlan = strtoull(ctx->argv[3], &nptr, 10);
2303 if (nptr == ctx->argv[3] || nptr == NULL) {
2304 ctl_fatal("Invalid argument %s", ctx->argv[3]);
2305 return;
2306 }
2307
2308 vsctl_context_populate_cache(ctx);
2309
2310 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2311 if (br->parent) {
2312 br = br->parent;
2313 }
2314
2315 if (br->br_cfg && br->br_cfg->auto_attach &&
2316 br->br_cfg->auto_attach->key_mappings &&
2317 br->br_cfg->auto_attach->value_mappings) {
2318 size_t i;
2319
2320 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2321 if (br->br_cfg->auto_attach->key_mappings[i] == isid &&
2322 br->br_cfg->auto_attach->value_mappings[i] == vlan) {
2323 del_aa_mapping(br->br_cfg->auto_attach, isid, vlan);
2324 break;
2325 }
2326 }
2327 }
2328 }
2329
2330 static void
2331 pre_aa_mapping(struct ctl_context *ctx)
2332 {
2333 pre_get_info(ctx);
2334
2335 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_auto_attach);
2336 ovsdb_idl_add_column(ctx->idl, &ovsrec_autoattach_col_mappings);
2337 }
2338
2339 static void
2340 verify_auto_attach(struct ovsrec_bridge *bridge)
2341 {
2342 if (bridge) {
2343 ovsrec_bridge_verify_auto_attach(bridge);
2344
2345 if (bridge->auto_attach) {
2346 ovsrec_autoattach_verify_mappings(bridge->auto_attach);
2347 }
2348 }
2349 }
2350
2351 static void
2352 cmd_get_aa_mapping(struct ctl_context *ctx)
2353 {
2354 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2355 struct vsctl_bridge *br;
2356
2357 vsctl_context_populate_cache(ctx);
2358
2359 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2360 if (br->parent) {
2361 br = br->parent;
2362 }
2363
2364 verify_auto_attach(br->br_cfg);
2365
2366 if (br->br_cfg && br->br_cfg->auto_attach &&
2367 br->br_cfg->auto_attach->key_mappings &&
2368 br->br_cfg->auto_attach->value_mappings) {
2369 size_t i;
2370
2371 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2372 ds_put_format(&ctx->output, "%"PRId64" %"PRId64"\n",
2373 br->br_cfg->auto_attach->key_mappings[i],
2374 br->br_cfg->auto_attach->value_mappings[i]);
2375 }
2376 }
2377 }
2378
2379 \f
2380 static const struct ctl_table_class tables[OVSREC_N_TABLES] = {
2381 [OVSREC_TABLE_BRIDGE].row_ids[0] = {&ovsrec_bridge_col_name, NULL, NULL},
2382
2383 [OVSREC_TABLE_CONTROLLER].row_ids[0]
2384 = {&ovsrec_bridge_col_name, NULL, &ovsrec_bridge_col_controller},
2385
2386 [OVSREC_TABLE_INTERFACE].row_ids[0]
2387 = {&ovsrec_interface_col_name, NULL, NULL},
2388
2389 [OVSREC_TABLE_MIRROR].row_ids[0] = {&ovsrec_mirror_col_name, NULL, NULL},
2390
2391 [OVSREC_TABLE_MANAGER].row_ids[0]
2392 = {&ovsrec_manager_col_target, NULL, NULL},
2393
2394 [OVSREC_TABLE_NETFLOW].row_ids[0]
2395 = {&ovsrec_bridge_col_name, NULL, &ovsrec_bridge_col_netflow},
2396
2397 [OVSREC_TABLE_PORT].row_ids[0] = {&ovsrec_port_col_name, NULL, NULL},
2398
2399 [OVSREC_TABLE_QOS].row_ids[0]
2400 = {&ovsrec_port_col_name, NULL, &ovsrec_port_col_qos},
2401
2402 [OVSREC_TABLE_SFLOW].row_ids[0]
2403 = {&ovsrec_bridge_col_name, NULL, &ovsrec_bridge_col_sflow},
2404
2405 [OVSREC_TABLE_FLOW_TABLE].row_ids[0]
2406 = {&ovsrec_flow_table_col_name, NULL, NULL},
2407
2408 [OVSREC_TABLE_IPFIX].row_ids[0]
2409 = {&ovsrec_bridge_col_name, NULL, &ovsrec_bridge_col_ipfix},
2410
2411 [OVSREC_TABLE_AUTOATTACH].row_ids[0]
2412 = {&ovsrec_bridge_col_name, NULL, &ovsrec_bridge_col_auto_attach},
2413
2414 [OVSREC_TABLE_FLOW_SAMPLE_COLLECTOR_SET].row_ids[0]
2415 = {&ovsrec_flow_sample_collector_set_col_id, NULL, NULL},
2416 };
2417
2418 static void
2419 post_db_reload_check_init(void)
2420 {
2421 n_neoteric_ifaces = 0;
2422 }
2423
2424 static void
2425 post_db_reload_expect_iface(const struct ovsrec_interface *iface)
2426 {
2427 if (n_neoteric_ifaces >= allocated_neoteric_ifaces) {
2428 neoteric_ifaces = x2nrealloc(neoteric_ifaces,
2429 &allocated_neoteric_ifaces,
2430 sizeof *neoteric_ifaces);
2431 }
2432 neoteric_ifaces[n_neoteric_ifaces++] = iface->header_.uuid;
2433 }
2434
2435 static void
2436 post_db_reload_do_checks(const struct vsctl_context *vsctl_ctx)
2437 {
2438 bool print_error = false;
2439 size_t i;
2440
2441 for (i = 0; i < n_neoteric_ifaces; i++) {
2442 const struct uuid *uuid;
2443
2444 uuid = ovsdb_idl_txn_get_insert_uuid(vsctl_ctx->base.txn,
2445 &neoteric_ifaces[i]);
2446 if (uuid) {
2447 const struct ovsrec_interface *iface;
2448
2449 iface = ovsrec_interface_get_for_uuid(vsctl_ctx->base.idl, uuid);
2450 if (iface && (!iface->ofport || *iface->ofport == -1)) {
2451 if (iface->error && *iface->error) {
2452 ovs_error(0, "Error detected while setting up '%s': %s. "
2453 "See ovs-vswitchd log for details.",
2454 iface->name, iface->error);
2455 } else {
2456 ovs_error(0, "Error detected while setting up '%s'. "
2457 "See ovs-vswitchd log for details.",
2458 iface->name);
2459 }
2460 print_error = true;
2461 }
2462 }
2463 }
2464
2465 if (print_error) {
2466 ovs_error(0, "The default log directory is \"%s\".", ovs_logdir());
2467 }
2468 }
2469
2470 \f
2471 static void
2472 vsctl_context_init_command(struct vsctl_context *vsctl_ctx,
2473 struct ctl_command *command)
2474 {
2475 ctl_context_init_command(&vsctl_ctx->base, command);
2476 vsctl_ctx->verified_ports = false;
2477 }
2478
2479 static void
2480 vsctl_context_init(struct vsctl_context *vsctl_ctx,
2481 struct ctl_command *command, struct ovsdb_idl *idl,
2482 struct ovsdb_idl_txn *txn,
2483 const struct ovsrec_open_vswitch *ovs,
2484 struct ovsdb_symbol_table *symtab)
2485 {
2486 ctl_context_init(&vsctl_ctx->base, command, idl, txn, symtab,
2487 vsctl_context_invalidate_cache);
2488 if (command) {
2489 vsctl_ctx->verified_ports = false;
2490 }
2491 vsctl_ctx->ovs = ovs;
2492 vsctl_ctx->cache_valid = false;
2493 }
2494
2495 static void
2496 vsctl_context_done_command(struct vsctl_context *vsctl_ctx,
2497 struct ctl_command *command)
2498 {
2499 ctl_context_done_command(&vsctl_ctx->base, command);
2500 }
2501
2502 static void
2503 vsctl_context_done(struct vsctl_context *vsctl_ctx,
2504 struct ctl_command *command)
2505 {
2506 ctl_context_done(&vsctl_ctx->base, command);
2507 }
2508
2509 static void
2510 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2511 struct ovsdb_idl *idl)
2512 {
2513 struct ctl_command *c;
2514
2515 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
2516 if (wait_for_reload) {
2517 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
2518 }
2519 for (c = commands; c < &commands[n_commands]; c++) {
2520 if (c->syntax->prerequisites) {
2521 struct vsctl_context vsctl_ctx;
2522
2523 ds_init(&c->output);
2524 c->table = NULL;
2525
2526 vsctl_context_init(&vsctl_ctx, c, idl, NULL, NULL, NULL);
2527 (c->syntax->prerequisites)(&vsctl_ctx.base);
2528 vsctl_context_done(&vsctl_ctx, c);
2529
2530 ovs_assert(!c->output.string);
2531 ovs_assert(!c->table);
2532 }
2533 }
2534 }
2535
2536 static char *
2537 vsctl_parent_process_info(void)
2538 {
2539 #ifdef __linux__
2540 pid_t parent_pid;
2541 struct ds s;
2542
2543 parent_pid = getppid();
2544 ds_init(&s);
2545
2546 /* Retrive the command line of the parent process, except the init
2547 * process since /proc/0 does not exist. */
2548 if (parent_pid) {
2549 char *procfile;
2550 FILE *f;
2551
2552 procfile = xasprintf("/proc/%d/cmdline", parent_pid);
2553
2554 f = fopen(procfile, "r");
2555 free(procfile);
2556 if (f) {
2557 ds_get_line(&s, f);
2558 fclose(f);
2559 }
2560 } else {
2561 ds_put_cstr(&s, "init");
2562 }
2563
2564 ds_put_format(&s, " (pid %d)", parent_pid);
2565 return ds_steal_cstr(&s);
2566 #else
2567 return NULL;
2568 #endif
2569 }
2570
2571 static bool
2572 do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
2573 struct ovsdb_idl *idl)
2574 {
2575 struct ovsdb_idl_txn *txn;
2576 const struct ovsrec_open_vswitch *ovs;
2577 enum ovsdb_idl_txn_status status;
2578 struct ovsdb_symbol_table *symtab;
2579 struct vsctl_context vsctl_ctx;
2580 struct ctl_command *c;
2581 struct shash_node *node;
2582 int64_t next_cfg = 0;
2583 char *error = NULL;
2584 char *ppid_info = NULL;
2585
2586 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2587 if (dry_run) {
2588 ovsdb_idl_txn_set_dry_run(txn);
2589 }
2590
2591 ppid_info = vsctl_parent_process_info();
2592 if (ppid_info) {
2593 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl (invoked by %s): %s",
2594 ppid_info, args);
2595 free(ppid_info);
2596 } else {
2597 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2598 }
2599
2600 ovs = ovsrec_open_vswitch_first(idl);
2601 if (!ovs) {
2602 /* XXX add verification that table is empty */
2603 ovs = ovsrec_open_vswitch_insert(txn);
2604 }
2605
2606 if (wait_for_reload) {
2607 ovsdb_idl_txn_increment(txn, &ovs->header_,
2608 &ovsrec_open_vswitch_col_next_cfg, false);
2609 }
2610
2611 post_db_reload_check_init();
2612 symtab = ovsdb_symbol_table_create();
2613 for (c = commands; c < &commands[n_commands]; c++) {
2614 ds_init(&c->output);
2615 c->table = NULL;
2616 }
2617 vsctl_context_init(&vsctl_ctx, NULL, idl, txn, ovs, symtab);
2618 for (c = commands; c < &commands[n_commands]; c++) {
2619 vsctl_context_init_command(&vsctl_ctx, c);
2620 if (c->syntax->run) {
2621 (c->syntax->run)(&vsctl_ctx.base);
2622 }
2623 vsctl_context_done_command(&vsctl_ctx, c);
2624
2625 if (vsctl_ctx.base.try_again) {
2626 vsctl_context_done(&vsctl_ctx, NULL);
2627 goto try_again;
2628 }
2629 }
2630 vsctl_context_done(&vsctl_ctx, NULL);
2631
2632 SHASH_FOR_EACH (node, &symtab->sh) {
2633 struct ovsdb_symbol *symbol = node->data;
2634 if (!symbol->created) {
2635 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2636 "with \"-- --id=%s create ...\")",
2637 node->name, node->name);
2638 }
2639 if (!symbol->strong_ref) {
2640 if (!symbol->weak_ref) {
2641 VLOG_WARN("row id \"%s\" was created but no reference to it "
2642 "was inserted, so it will not actually appear in "
2643 "the database", node->name);
2644 } else {
2645 VLOG_WARN("row id \"%s\" was created but only a weak "
2646 "reference to it was inserted, so it will not "
2647 "actually appear in the database", node->name);
2648 }
2649 }
2650 }
2651
2652 status = ovsdb_idl_txn_commit_block(txn);
2653 if (wait_for_reload && status == TXN_SUCCESS) {
2654 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2655 }
2656 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2657 for (c = commands; c < &commands[n_commands]; c++) {
2658 if (c->syntax->postprocess) {
2659 vsctl_context_init(&vsctl_ctx, c, idl, txn, ovs, symtab);
2660 (c->syntax->postprocess)(&vsctl_ctx.base);
2661 vsctl_context_done(&vsctl_ctx, c);
2662 }
2663 }
2664 }
2665 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2666
2667 switch (status) {
2668 case TXN_UNCOMMITTED:
2669 case TXN_INCOMPLETE:
2670 OVS_NOT_REACHED();
2671
2672 case TXN_ABORTED:
2673 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2674 ctl_fatal("transaction aborted");
2675
2676 case TXN_UNCHANGED:
2677 case TXN_SUCCESS:
2678 break;
2679
2680 case TXN_TRY_AGAIN:
2681 goto try_again;
2682
2683 case TXN_ERROR:
2684 ctl_fatal("transaction error: %s", error);
2685
2686 case TXN_NOT_LOCKED:
2687 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2688 ctl_fatal("database not locked");
2689
2690 default:
2691 OVS_NOT_REACHED();
2692 }
2693 free(error);
2694
2695 ovsdb_symbol_table_destroy(symtab);
2696
2697 for (c = commands; c < &commands[n_commands]; c++) {
2698 struct ds *ds = &c->output;
2699
2700 if (c->table) {
2701 table_print(c->table, &table_style);
2702 } else if (oneline) {
2703 size_t j;
2704
2705 ds_chomp(ds, '\n');
2706 for (j = 0; j < ds->length; j++) {
2707 int ch = ds->string[j];
2708 switch (ch) {
2709 case '\n':
2710 fputs("\\n", stdout);
2711 break;
2712
2713 case '\\':
2714 fputs("\\\\", stdout);
2715 break;
2716
2717 default:
2718 putchar(ch);
2719 }
2720 }
2721 putchar('\n');
2722 } else {
2723 fputs(ds_cstr(ds), stdout);
2724 }
2725 ds_destroy(&c->output);
2726 table_destroy(c->table);
2727 free(c->table);
2728
2729 shash_destroy_free_data(&c->options);
2730 }
2731 free(commands);
2732
2733 if (wait_for_reload && status != TXN_UNCHANGED) {
2734 /* Even, if --retry flag was not specified, ovs-vsctl still
2735 * has to retry to establish OVSDB connection, if wait_for_reload
2736 * was set. Otherwise, ovs-vsctl would end up waiting forever
2737 * until cur_cfg would be updated. */
2738 ovsdb_idl_enable_reconnect(idl);
2739 for (;;) {
2740 ovsdb_idl_run(idl);
2741 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2742 if (ovs->cur_cfg >= next_cfg) {
2743 post_db_reload_do_checks(&vsctl_ctx);
2744 goto done;
2745 }
2746 }
2747 ovsdb_idl_wait(idl);
2748 poll_block();
2749 }
2750 done: ;
2751 }
2752 ovsdb_idl_txn_destroy(txn);
2753 ovsdb_idl_destroy(idl);
2754
2755 return true;
2756
2757 try_again:
2758 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2759 * resources and return so that the caller can try again. */
2760 ovsdb_idl_txn_abort(txn);
2761 ovsdb_idl_txn_destroy(txn);
2762 the_idl_txn = NULL;
2763
2764 ovsdb_symbol_table_destroy(symtab);
2765 for (c = commands; c < &commands[n_commands]; c++) {
2766 ds_destroy(&c->output);
2767 table_destroy(c->table);
2768 free(c->table);
2769 }
2770 free(error);
2771 return false;
2772 }
2773
2774 /* Frees the current transaction and the underlying IDL and then calls
2775 * exit(status).
2776 *
2777 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2778 * for a clean memory leak report from valgrind in the normal case. That makes
2779 * it easier to notice real memory leaks. */
2780 static void
2781 vsctl_exit(int status)
2782 {
2783 if (the_idl_txn) {
2784 ovsdb_idl_txn_abort(the_idl_txn);
2785 ovsdb_idl_txn_destroy(the_idl_txn);
2786 }
2787 ovsdb_idl_destroy(the_idl);
2788 exit(status);
2789 }
2790
2791 /*
2792 * Developers who add new commands to the 'struct ctl_command_syntax' must
2793 * define the 'arguments' member of the struct. The following keywords are
2794 * available for composing the argument format:
2795 *
2796 * TABLE RECORD BRIDGE PARENT PORT
2797 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
2798 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
2799 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
2800 * TARGET NEW-* (e.g. NEW-PORT)
2801 *
2802 * For argument types not listed above, just uses 'ARG' as place holder.
2803 *
2804 * Encloses the keyword with '[]' if it is optional. Appends '...' to
2805 * keyword or enclosed keyword to indicate that the argument can be specified
2806 * multiple times.
2807 *
2808 * */
2809 static const struct ctl_command_syntax vsctl_commands[] = {
2810 /* Open vSwitch commands. */
2811 {"init", 0, 0, "", NULL, cmd_init, NULL, "", RW},
2812
2813 /* Bridge commands. */
2814 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info,
2815 cmd_add_br, NULL, "--may-exist", RW},
2816 {"del-br", 1, 1, "BRIDGE", pre_get_info, cmd_del_br,
2817 NULL, "--if-exists", RW},
2818 {"list-br", 0, 0, "", pre_get_info, cmd_list_br, NULL, "--real,--fake",
2819 RO},
2820 {"br-exists", 1, 1, "BRIDGE", pre_get_info, cmd_br_exists, NULL, "", RO},
2821 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_vlan, NULL, "",
2822 RO},
2823 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_parent, NULL,
2824 "", RO},
2825 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2826 pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
2827 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id,
2828 cmd_br_get_external_id, NULL, "", RO},
2829
2830 /* Port commands. */
2831 {"list-ports", 1, 1, "BRIDGE", pre_get_info, cmd_list_ports, NULL, "",
2832 RO},
2833 {"add-port", 2, INT_MAX, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2834 pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
2835 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info, cmd_del_port, NULL,
2836 "--if-exists,--with-iface", RW},
2837 {"port-to-br", 1, 1, "PORT", pre_get_info, cmd_port_to_br, NULL, "", RO},
2838
2839 /* Bond commands. */
2840 {"add-bond", 4, INT_MAX,
2841 "BRIDGE BOND IFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info,
2842 cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
2843 {"add-bond-iface", 2, 2, "BOND IFACE", pre_get_info, cmd_add_bond_iface,
2844 NULL, "--may-exist", RW},
2845 {"del-bond-iface", 1, 2, "[BOND] IFACE", pre_get_info, cmd_del_bond_iface,
2846 NULL, "--if-exists", RW},
2847
2848 /* Interface commands. */
2849 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info, cmd_list_ifaces, NULL, "",
2850 RO},
2851 {"iface-to-br", 1, 1, "IFACE", pre_get_info, cmd_iface_to_br, NULL, "",
2852 RO},
2853
2854 /* Controller commands. */
2855 {"get-controller", 1, 1, "BRIDGE", pre_controller, cmd_get_controller,
2856 NULL, "", RO},
2857 {"del-controller", 1, 1, "BRIDGE", pre_controller, cmd_del_controller,
2858 NULL, "", RW},
2859 {"set-controller", 1, INT_MAX, "BRIDGE TARGET...", pre_controller,
2860 cmd_set_controller, NULL, "--inactivity-probe=", RW},
2861 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_get_fail_mode, NULL,
2862 "", RO},
2863 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_del_fail_mode, NULL,
2864 "", RW},
2865 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info, cmd_set_fail_mode,
2866 NULL, "", RW},
2867
2868 /* Manager commands. */
2869 {"get-manager", 0, 0, "", pre_manager, cmd_get_manager, NULL, "", RO},
2870 {"del-manager", 0, 0, "", pre_manager, cmd_del_manager, NULL, "", RW},
2871 {"set-manager", 1, INT_MAX, "TARGET...", pre_manager, cmd_set_manager,
2872 NULL, "--inactivity-probe=", RW},
2873
2874 /* SSL commands. */
2875 {"get-ssl", 0, 0, "", pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
2876 {"del-ssl", 0, 0, "", pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
2877 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl,
2878 cmd_set_ssl, NULL, "--bootstrap", RW},
2879
2880 /* Auto Attach commands. */
2881 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_add_aa_mapping,
2882 NULL, "", RW},
2883 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_del_aa_mapping,
2884 NULL, "", RW},
2885 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping, cmd_get_aa_mapping,
2886 NULL, "", RO},
2887
2888 /* Switch commands. */
2889 {"emer-reset", 0, 0, "", pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
2890
2891 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2892 };
2893
2894 /* Registers vsctl and common db commands. */
2895 static void
2896 vsctl_cmd_init(void)
2897 {
2898 ctl_init(&ovsrec_idl_class, ovsrec_table_classes, tables, cmd_show_tables,
2899 vsctl_exit);
2900 ctl_register_commands(vsctl_commands);
2901 }