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