2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "db-ctl-base.h"
32 #include "command-line.h"
34 #include "openvswitch/dynamic-string.h"
35 #include "fatal-signal.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-idl.h"
40 #include "poll-loop.h"
43 #include "stream-ssl.h"
47 #include "vtep/vtep-idl.h"
51 #include "openvswitch/vconn.h"
52 #include "openvswitch/vlog.h"
54 VLOG_DEFINE_THIS_MODULE(vtep_ctl);
56 struct vtep_ctl_context;
58 /* --db: The database server to contact. */
59 static const char *db;
61 /* --oneline: Write each command's output as a single line? */
64 /* --dry-run: Do not commit any changes. */
67 /* --timeout: Time to wait for a connection to 'db'. */
70 /* Format for table output. */
71 static struct table_style table_style = TABLE_STYLE_DEFAULT;
73 /* The IDL we're using and the current transaction, if any.
74 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
75 * Other code should use its context arguments. */
76 static struct ovsdb_idl *the_idl;
77 static struct ovsdb_idl_txn *the_idl_txn;
79 OVS_NO_RETURN static void vtep_ctl_exit(int status);
80 static void vtep_ctl_cmd_init(void);
81 OVS_NO_RETURN static void usage(void);
82 static void parse_options(int argc, char *argv[], struct shash *local_options);
83 static void run_prerequisites(struct ctl_command[], size_t n_commands,
85 static void do_vtep_ctl(const char *args, struct ctl_command *, size_t n,
87 static struct vtep_ctl_lswitch *find_lswitch(struct vtep_ctl_context *,
90 static struct vtep_ctl_lrouter *find_lrouter(struct vtep_ctl_context *,
95 main(int argc, char *argv[])
97 struct ovsdb_idl *idl;
98 struct ctl_command *commands;
99 struct shash local_options;
104 set_program_name(argv[0]);
105 fatal_ignore_sigpipe();
106 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
107 vlog_set_levels_from_string_assert("reconnect:warn");
112 /* Log our arguments. This is often valuable for debugging systems. */
113 args = process_escape_args(argv);
114 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
116 /* Parse command line. */
117 shash_init(&local_options);
118 parse_options(argc, argv, &local_options);
119 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
126 /* Initialize IDL. */
127 idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
128 run_prerequisites(commands, n_commands, idl);
130 /* Execute the commands.
132 * 'seqno' is the database sequence number for which we last tried to
133 * execute our transaction. There's no point in trying to commit more than
134 * once for any given sequence number, because if the transaction fails
135 * it's because the database changed and we need to obtain an up-to-date
136 * view of the database before we try the transaction again. */
137 seqno = ovsdb_idl_get_seqno(idl);
140 if (!ovsdb_idl_is_alive(idl)) {
141 int retval = ovsdb_idl_get_last_error(idl);
142 ctl_fatal("%s: database connection failed (%s)",
143 db, ovs_retval_to_string(retval));
146 if (seqno != ovsdb_idl_get_seqno(idl)) {
147 seqno = ovsdb_idl_get_seqno(idl);
148 do_vtep_ctl(args, commands, n_commands, idl);
151 if (seqno == ovsdb_idl_get_seqno(idl)) {
159 parse_options(int argc, char *argv[], struct shash *local_options)
162 OPT_DB = UCHAR_MAX + 1,
171 static const struct option global_long_options[] = {
172 {"db", required_argument, NULL, OPT_DB},
173 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
174 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
175 {"oneline", no_argument, NULL, OPT_ONELINE},
176 {"timeout", required_argument, NULL, 't'},
177 {"help", no_argument, NULL, 'h'},
178 {"version", no_argument, NULL, 'V'},
181 STREAM_SSL_LONG_OPTIONS,
182 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
185 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
186 char *tmp, *short_options;
188 struct option *options;
189 size_t allocated_options;
193 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
194 short_options = xasprintf("+%s", tmp);
197 /* We want to parse both global and command-specific options here, but
198 * getopt_long() isn't too convenient for the job. We copy our global
199 * options into a dynamic array, then append all of the command-specific
201 options = xmemdup(global_long_options, sizeof global_long_options);
202 allocated_options = ARRAY_SIZE(global_long_options);
203 n_options = n_global_long_options;
204 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
205 table_style.format = TF_LIST;
211 c = getopt_long(argc, argv, short_options, options, &idx);
226 vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
234 if (shash_find(local_options, options[idx].name)) {
235 ctl_fatal("'%s' option specified multiple times",
238 shash_add_nocopy(local_options,
239 xasprintf("--%s", options[idx].name),
240 nullable_xstrdup(optarg));
247 ovs_print_version(0, 0);
248 printf("DB Schema %s\n", vteprec_get_db_version());
252 timeout = strtoul(optarg, NULL, 10);
254 ctl_fatal("value %s on -t or --timeout is invalid",
260 TABLE_OPTION_HANDLERS(&table_style)
262 STREAM_SSL_OPTION_HANDLERS
264 case OPT_PEER_CA_CERT:
265 stream_ssl_set_peer_ca_cert_file(optarg);
278 db = ctl_default_db();
281 for (i = n_global_long_options; options[i].name; i++) {
282 free(CONST_CAST(char *, options[i].name));
287 /* Frees the current transaction and the underlying IDL and then calls
290 * Freeing the transaction and the IDL is not strictly necessary, but it makes
291 * for a clean memory leak report from valgrind in the normal case. That makes
292 * it easier to notice real memory leaks. */
294 vtep_ctl_exit(int status)
297 ovsdb_idl_txn_abort(the_idl_txn);
298 ovsdb_idl_txn_destroy(the_idl_txn);
300 ovsdb_idl_destroy(the_idl);
308 %s: VTEP configuration utility\n\
309 usage: %s [OPTIONS] COMMAND [ARG...]\n\
312 show print overview of database contents\n\
315 get-manager print the managers\n\
316 del-manager delete the managers\n\
317 set-manager TARGET... set the list of managers to TARGET...\n\
319 Physical Switch commands:\n\
320 add-ps PS create a new physical switch named PS\n\
321 del-ps PS delete PS and all of its ports\n\
322 list-ps print the names of all the physical switches\n\
323 ps-exists PS exit 2 if PS does not exist\n\
326 list-ports PS print the names of all the ports on PS\n\
327 add-port PS PORT add network device PORT to PS\n\
328 del-port PS PORT delete PORT from PS\n\
330 Logical Switch commands:\n\
331 add-ls LS create a new logical switch named LS\n\
332 del-ls LS delete LS and all of its ports\n\
333 list-ls print the names of all the logical switches\n\
334 ls-exists LS exit 2 if LS does not exist\n\
335 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
336 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
337 list-bindings PS PORT list bindings for PORT on PS\n\
338 set-replication-mode LS MODE set replication mode on LS\n\
339 get-replication-mode LS get replication mode on LS\n\
341 Logical Router commands:\n\
342 add-lr LR create a new logical router named LR\n\
343 del-lr LR delete LR\n\
344 list-lr print the names of all the logical routers\n\
345 lr-exists LR exit 2 if LR does not exist\n\
347 MAC binding commands:\n\
348 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
349 del-ucast-local LS MAC del ucast local entry from LS\n\
350 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
351 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
352 clear-local-macs LS clear local mac entries\n\
353 list-local-macs LS list local mac entries\n\
354 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
355 del-ucast-remote LS MAC del ucast remote entry from LS\n\
356 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
357 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
358 clear-remote-macs LS clear remote mac entries\n\
359 list-remote-macs LS list remote mac entries\n\
364 --db=DATABASE connect to DATABASE\n\
366 -t, --timeout=SECS wait at most SECS seconds\n\
367 --dry-run do not commit changes to database\n\
368 --oneline print exactly one line of output per command\n",
369 program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
372 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
373 stream_usage("database", true, true, false);
376 -h, --help display this help message\n\
377 -V, --version display version information\n");
382 static struct cmd_show_table cmd_show_tables[] = {
383 {&vteprec_table_global,
385 {&vteprec_global_col_managers,
386 &vteprec_global_col_switches,
391 {&vteprec_table_manager,
392 &vteprec_manager_col_target,
393 {&vteprec_manager_col_is_connected,
399 {&vteprec_table_physical_switch,
400 &vteprec_physical_switch_col_name,
401 {&vteprec_physical_switch_col_management_ips,
402 &vteprec_physical_switch_col_tunnel_ips,
403 &vteprec_physical_switch_col_ports},
407 {&vteprec_table_physical_port,
408 &vteprec_physical_port_col_name,
409 {&vteprec_physical_port_col_vlan_bindings,
415 {&vteprec_table_logical_switch,
416 &vteprec_logical_switch_col_name,
423 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
426 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
427 struct vtep_ctl_context {
428 struct ctl_context base;
430 /* Modifiable state. */
431 const struct vteprec_global *vtep_global;
434 /* A cache of the contents of the database.
436 * A command that needs to use any of this information must first
437 * call vtep_ctl_context_populate_cache(). A command that changes
438 * anything that could invalidate the cache must either call
439 * vtep_ctl_context_invalidate_cache() or manually update the cache
440 * to maintain its correctness. */
442 struct shash pswitches; /* Maps from physical switch name to
443 * struct vtep_ctl_pswitch. */
444 struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
446 struct shash lswitches; /* Maps from logical switch name to
447 * struct vtep_ctl_lswitch. */
448 struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
449 * struct vteprec_physical_locator. */
450 struct shash lrouters; /* Maps from logical router name to
451 * struct vtep_ctl_lrouter. */
454 /* Casts 'base' into 'struct vtep_ctl_context'. */
455 static struct vtep_ctl_context *
456 vtep_ctl_context_cast(struct ctl_context *base)
458 return CONTAINER_OF(base, struct vtep_ctl_context, base);
461 struct vtep_ctl_pswitch {
462 const struct vteprec_physical_switch *ps_cfg;
464 struct ovs_list ports; /* Contains "struct vteprec_physical_port"s. */
467 struct vtep_ctl_port {
468 struct ovs_list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
469 const struct vteprec_physical_port *port_cfg;
470 struct vtep_ctl_pswitch *ps;
471 struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
474 struct vtep_ctl_lswitch {
475 const struct vteprec_logical_switch *ls_cfg;
477 struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
478 struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
479 struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
480 struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
483 struct vtep_ctl_lrouter {
484 const struct vteprec_logical_router *lr_cfg;
488 struct vtep_ctl_mcast_mac {
489 const struct vteprec_mcast_macs_local *local_cfg;
490 const struct vteprec_mcast_macs_remote *remote_cfg;
492 const struct vteprec_physical_locator_set *ploc_set_cfg;
493 struct ovs_list locators; /* Contains 'vtep_ctl_ploc's. */
496 struct vtep_ctl_ploc {
497 struct ovs_list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
499 const struct vteprec_physical_locator *ploc_cfg;
503 verify_ports(struct vtep_ctl_context *vtepctl_ctx)
505 if (!vtepctl_ctx->verified_ports) {
506 const struct vteprec_physical_switch *ps;
508 vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
509 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, vtepctl_ctx->base.idl) {
510 vteprec_physical_switch_verify_ports(ps);
513 vtepctl_ctx->verified_ports = true;
517 static struct vtep_ctl_port *
518 add_port_to_cache(struct vtep_ctl_context *vtepctl_ctx,
519 struct vtep_ctl_pswitch *ps,
520 struct vteprec_physical_port *port_cfg)
522 char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
523 struct vtep_ctl_port *port;
525 port = xmalloc(sizeof *port);
526 ovs_list_push_back(&ps->ports, &port->ports_node);
527 port->port_cfg = port_cfg;
529 shash_add(&vtepctl_ctx->ports, cache_name, port);
531 shash_init(&port->bindings);
537 del_cached_port(struct vtep_ctl_context *vtepctl_ctx,
538 struct vtep_ctl_port *port)
540 char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
542 ovs_list_remove(&port->ports_node);
543 shash_find_and_delete(&vtepctl_ctx->ports, cache_name);
544 vteprec_physical_port_delete(port->port_cfg);
550 add_pswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
551 struct vteprec_physical_switch *ps_cfg)
553 struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
555 ps->name = xstrdup(ps_cfg->name);
556 ovs_list_init(&ps->ports);
557 shash_add(&vtepctl_ctx->pswitches, ps->name, ps);
561 vtep_delete_pswitch(const struct vteprec_global *vtep_global,
562 const struct vteprec_physical_switch *ps)
564 struct vteprec_physical_switch **pswitches;
567 pswitches = xmalloc(sizeof *vtep_global->switches
568 * vtep_global->n_switches);
569 for (i = n = 0; i < vtep_global->n_switches; i++) {
570 if (vtep_global->switches[i] != ps) {
571 pswitches[n++] = vtep_global->switches[i];
574 vteprec_global_set_switches(vtep_global, pswitches, n);
579 del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
581 ovs_assert(ovs_list_is_empty(&ps->ports));
583 vteprec_physical_switch_delete(ps->ps_cfg);
584 vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
586 shash_find_and_delete(&ctx->pswitches, ps->name);
591 static struct vtep_ctl_lswitch *
592 add_lswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
593 const struct vteprec_logical_switch *ls_cfg)
595 struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
597 ls->name = xstrdup(ls_cfg->name);
598 shash_add(&vtepctl_ctx->lswitches, ls->name, ls);
599 shash_init(&ls->ucast_local);
600 shash_init(&ls->ucast_remote);
601 shash_init(&ls->mcast_local);
602 shash_init(&ls->mcast_remote);
607 del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
610 vteprec_logical_switch_delete(ls->ls_cfg);
612 shash_find_and_delete(&ctx->lswitches, ls->name);
618 commit_ls_bindings(struct vtep_ctl_port *port)
620 struct vteprec_logical_switch **binding_values;
621 int64_t *binding_keys;
623 struct shash_node *node;
626 n_bindings = shash_count(&port->bindings);
627 binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
628 binding_values = xmalloc(n_bindings * sizeof *binding_values);
631 SHASH_FOR_EACH(node, &port->bindings) {
632 struct vtep_ctl_lswitch *ls_entry = node->data;
634 binding_keys[i] = strtoll(node->name, NULL, 0);
635 binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
639 vteprec_physical_port_set_vlan_bindings(port->port_cfg,
640 binding_keys, binding_values,
642 free(binding_values);
647 add_ls_binding_to_cache(struct vtep_ctl_port *port,
649 struct vtep_ctl_lswitch *ls)
651 if (shash_find(&port->bindings, vlan)) {
652 ctl_fatal("multiple bindings for vlan %s", vlan);
655 shash_add(&port->bindings, vlan, ls);
659 del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
661 if (!shash_find(&port->bindings, vlan)) {
662 ctl_fatal("no binding for vlan %s", vlan);
665 shash_find_and_delete(&port->bindings, vlan);
668 static struct vtep_ctl_lrouter *
669 add_lrouter_to_cache(struct vtep_ctl_context *vtepctl_ctx,
670 const struct vteprec_logical_router *lr_cfg)
672 struct vtep_ctl_lrouter *lr = xmalloc(sizeof *lr);
674 lr->name = xstrdup(lr_cfg->name);
675 shash_add(&vtepctl_ctx->lrouters, lr->name, lr);
680 del_cached_lrouter(struct vtep_ctl_context *ctx, struct vtep_ctl_lrouter *lr)
683 vteprec_logical_router_delete(lr->lr_cfg);
685 shash_find_and_delete(&ctx->lrouters, lr->name);
690 static struct vteprec_physical_locator *
691 find_ploc(struct vtep_ctl_context *vtepctl_ctx, const char *encap,
694 struct vteprec_physical_locator *ploc;
695 char *name = xasprintf("%s+%s", encap, dst_ip);
697 ovs_assert(vtepctl_ctx->cache_valid);
699 ploc = shash_find_data(&vtepctl_ctx->plocs, name);
706 add_ploc_to_cache(struct vtep_ctl_context *vtepctl_ctx,
707 struct vteprec_physical_locator *ploc)
709 char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
710 struct vteprec_physical_locator *orig_ploc;
712 orig_ploc = find_ploc(vtepctl_ctx, ploc->encapsulation_type, ploc->dst_ip);
714 shash_add(&vtepctl_ctx->plocs, name, ploc);
721 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
722 struct vteprec_physical_locator *ploc_cfg)
724 struct vtep_ctl_ploc *ploc;
726 ploc = xmalloc(sizeof *ploc);
727 ploc->ploc_cfg = ploc_cfg;
728 ovs_list_push_back(&mcast_mac->locators, &ploc->locators_node);
732 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
733 struct vteprec_physical_locator *ploc_cfg)
735 struct vtep_ctl_ploc *ploc;
737 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
738 if (ploc->ploc_cfg == ploc_cfg) {
739 ovs_list_remove(&ploc->locators_node);
746 static struct vtep_ctl_mcast_mac *
747 add_mcast_mac_to_cache(struct vtep_ctl_context *vtepctl_ctx,
748 struct vtep_ctl_lswitch *ls, const char *mac,
749 struct vteprec_physical_locator_set *ploc_set_cfg,
752 struct vtep_ctl_mcast_mac *mcast_mac;
753 struct shash *mcast_shash;
756 mcast_mac = xmalloc(sizeof *mcast_mac);
757 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
759 mcast_mac->ploc_set_cfg = ploc_set_cfg;
760 ovs_list_init(&mcast_mac->locators);
761 shash_add(mcast_shash, mac, mcast_mac);
763 for (i = 0; i < ploc_set_cfg->n_locators; i++) {
764 struct vteprec_physical_locator *ploc_cfg;
766 ploc_cfg = ploc_set_cfg->locators[i];
767 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
768 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
775 vtep_ctl_context_invalidate_cache(struct ctl_context *ctx)
777 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
778 struct shash_node *node;
780 if (!vtepctl_ctx->cache_valid) {
783 vtepctl_ctx->cache_valid = false;
785 SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
786 struct vtep_ctl_pswitch *ps = node->data;
790 shash_destroy(&vtepctl_ctx->pswitches);
792 SHASH_FOR_EACH (node, &vtepctl_ctx->ports) {
793 struct vtep_ctl_port *port = node->data;
794 shash_destroy(&port->bindings);
796 shash_destroy_free_data(&vtepctl_ctx->ports);
798 SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
799 struct vtep_ctl_lswitch *ls = node->data;
800 struct shash_node *node2, *next_node2;
802 shash_destroy(&ls->ucast_local);
803 shash_destroy(&ls->ucast_remote);
805 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
806 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
807 struct vtep_ctl_ploc *ploc, *next_ploc;
809 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
810 &mcast_mac->locators) {
815 shash_destroy(&ls->mcast_local);
817 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
818 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
819 struct vtep_ctl_ploc *ploc, *next_ploc;
821 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
822 &mcast_mac->locators) {
827 shash_destroy(&ls->mcast_remote);
832 shash_destroy(&vtepctl_ctx->lswitches);
833 shash_destroy(&vtepctl_ctx->plocs);
835 SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
836 struct vtep_ctl_lrouter *lr = node->data;
840 shash_destroy(&vtepctl_ctx->lrouters);
844 pre_get_info(struct ctl_context *ctx)
846 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
848 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
849 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
850 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_tunnels);
852 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
853 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
855 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
856 ovsdb_idl_add_column(ctx->idl,
857 &vteprec_logical_switch_col_replication_mode);
859 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_router_col_name);
861 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
862 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
863 ovsdb_idl_add_column(ctx->idl,
864 &vteprec_ucast_macs_local_col_logical_switch);
866 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
867 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
868 ovsdb_idl_add_column(ctx->idl,
869 &vteprec_ucast_macs_remote_col_logical_switch);
871 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
872 ovsdb_idl_add_column(ctx->idl,
873 &vteprec_mcast_macs_local_col_locator_set);
874 ovsdb_idl_add_column(ctx->idl,
875 &vteprec_mcast_macs_local_col_logical_switch);
877 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
878 ovsdb_idl_add_column(ctx->idl,
879 &vteprec_mcast_macs_remote_col_locator_set);
880 ovsdb_idl_add_column(ctx->idl,
881 &vteprec_mcast_macs_remote_col_logical_switch);
883 ovsdb_idl_add_column(ctx->idl,
884 &vteprec_physical_locator_set_col_locators);
886 ovsdb_idl_add_column(ctx->idl,
887 &vteprec_physical_locator_col_dst_ip);
888 ovsdb_idl_add_column(ctx->idl,
889 &vteprec_physical_locator_col_encapsulation_type);
891 ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_local);
892 ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_remote);
896 vtep_ctl_context_populate_cache(struct ctl_context *ctx)
898 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
899 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
900 const struct vteprec_logical_switch *ls_cfg;
901 const struct vteprec_logical_router *lr_cfg;
902 const struct vteprec_ucast_macs_local *ucast_local_cfg;
903 const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
904 const struct vteprec_mcast_macs_local *mcast_local_cfg;
905 const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
906 const struct vteprec_tunnel *tunnel_cfg;
907 struct sset pswitches, ports, lswitches;
908 struct sset lrouters;
911 if (vtepctl_ctx->cache_valid) {
912 /* Cache is already populated. */
915 vtepctl_ctx->cache_valid = true;
916 shash_init(&vtepctl_ctx->pswitches);
917 shash_init(&vtepctl_ctx->ports);
918 shash_init(&vtepctl_ctx->lswitches);
919 shash_init(&vtepctl_ctx->plocs);
920 shash_init(&vtepctl_ctx->lrouters);
922 sset_init(&pswitches);
924 for (i = 0; i < vtep_global->n_switches; i++) {
925 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
928 if (!sset_add(&pswitches, ps_cfg->name)) {
929 VLOG_WARN("%s: database contains duplicate physical switch name",
933 add_pswitch_to_cache(vtepctl_ctx, ps_cfg);
935 for (j = 0; j < ps_cfg->n_ports; j++) {
936 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
938 if (!sset_add(&ports, port_cfg->name)) {
939 /* Duplicate port name. (We will warn about that later.) */
944 sset_destroy(&pswitches);
945 sset_destroy(&ports);
947 sset_init(&lswitches);
948 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
949 if (!sset_add(&lswitches, ls_cfg->name)) {
950 VLOG_WARN("%s: database contains duplicate logical switch name",
954 add_lswitch_to_cache(vtepctl_ctx, ls_cfg);
956 sset_destroy(&lswitches);
958 sset_init(&lrouters);
959 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg, ctx->idl) {
960 if (!sset_add(&lrouters, lr_cfg->name)) {
961 VLOG_WARN("%s: database contains duplicate logical router name",
965 add_lrouter_to_cache(vtepctl_ctx, lr_cfg);
967 sset_destroy(&lrouters);
969 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
970 struct vtep_ctl_lswitch *ls;
972 if (!ucast_local_cfg->logical_switch) {
975 ls = find_lswitch(vtepctl_ctx, ucast_local_cfg->logical_switch->name,
981 if (ucast_local_cfg->locator) {
982 add_ploc_to_cache(vtepctl_ctx, ucast_local_cfg->locator);
985 shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
988 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
989 struct vtep_ctl_lswitch *ls;
991 if (!ucast_remote_cfg->logical_switch) {
994 ls = find_lswitch(vtepctl_ctx, ucast_remote_cfg->logical_switch->name,
1000 if (ucast_remote_cfg->locator) {
1001 add_ploc_to_cache(vtepctl_ctx, ucast_remote_cfg->locator);
1004 shash_add(&ls->ucast_remote, ucast_remote_cfg->MAC, ucast_remote_cfg);
1007 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg, ctx->idl) {
1008 struct vtep_ctl_mcast_mac *mcast_mac;
1009 struct vtep_ctl_lswitch *ls;
1011 if (!mcast_local_cfg->logical_switch) {
1014 ls = find_lswitch(vtepctl_ctx, mcast_local_cfg->logical_switch->name,
1020 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mcast_local_cfg->MAC,
1021 mcast_local_cfg->locator_set,
1023 mcast_mac->local_cfg = mcast_local_cfg;
1026 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg, ctx->idl) {
1027 struct vtep_ctl_mcast_mac *mcast_mac;
1028 struct vtep_ctl_lswitch *ls;
1030 if (!mcast_remote_cfg->logical_switch) {
1033 ls = find_lswitch(vtepctl_ctx, mcast_remote_cfg->logical_switch->name,
1039 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mcast_remote_cfg->MAC,
1040 mcast_remote_cfg->locator_set,
1042 mcast_mac->remote_cfg = mcast_remote_cfg;
1045 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg, ctx->idl) {
1046 if (tunnel_cfg->local) {
1047 add_ploc_to_cache(vtepctl_ctx, tunnel_cfg->local);
1049 if (tunnel_cfg->remote) {
1050 add_ploc_to_cache(vtepctl_ctx, tunnel_cfg->remote);
1054 sset_init(&pswitches);
1055 for (i = 0; i < vtep_global->n_switches; i++) {
1056 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1057 struct vtep_ctl_pswitch *ps;
1060 if (!sset_add(&pswitches, ps_cfg->name)) {
1063 ps = shash_find_data(&vtepctl_ctx->pswitches, ps_cfg->name);
1064 for (j = 0; j < ps_cfg->n_ports; j++) {
1065 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1066 struct vtep_ctl_port *port;
1069 port = shash_find_data(&vtepctl_ctx->ports, port_cfg->name);
1071 if (port_cfg == port->port_cfg) {
1072 VLOG_WARN("%s: port is in multiple physical switches "
1074 port_cfg->name, ps->name, port->ps->name);
1076 /* Log as an error because this violates the database's
1077 * uniqueness constraints, so the database server shouldn't
1078 * have allowed it. */
1079 VLOG_ERR("%s: database contains duplicate port name",
1085 port = add_port_to_cache(vtepctl_ctx, ps, port_cfg);
1087 for (k = 0; k < port_cfg->n_vlan_bindings; k++) {
1088 struct vteprec_logical_switch *ls_cfg;
1089 struct vtep_ctl_lswitch *ls;
1092 vlan = xasprintf("%"PRId64, port_cfg->key_vlan_bindings[k]);
1093 if (shash_find(&port->bindings, vlan)) {
1094 ctl_fatal("multiple bindings for vlan %s", vlan);
1097 ls_cfg = port_cfg->value_vlan_bindings[k];
1098 ls = find_lswitch(vtepctl_ctx, ls_cfg->name, true);
1100 shash_add_nocopy(&port->bindings, vlan, ls);
1104 sset_destroy(&pswitches);
1107 static struct vtep_ctl_pswitch *
1108 find_pswitch(struct vtep_ctl_context *vtepctl_ctx, const char *name, bool must_exist)
1110 struct vtep_ctl_pswitch *ps;
1112 ovs_assert(vtepctl_ctx->cache_valid);
1114 ps = shash_find_data(&vtepctl_ctx->pswitches, name);
1115 if (must_exist && !ps) {
1116 ctl_fatal("no physical switch named %s", name);
1118 vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
1122 static struct vtep_ctl_port *
1123 find_port(struct vtep_ctl_context *vtepctl_ctx, const char *ps_name,
1124 const char *port_name, bool must_exist)
1126 char *cache_name = xasprintf("%s+%s", ps_name, port_name);
1127 struct vtep_ctl_port *port;
1129 ovs_assert(vtepctl_ctx->cache_valid);
1131 port = shash_find_data(&vtepctl_ctx->ports, cache_name);
1132 if (port && !strcmp(port_name, port->ps->name)) {
1136 if (must_exist && !port) {
1137 ctl_fatal("no port named %s", port_name);
1139 verify_ports(vtepctl_ctx);
1144 pswitch_insert_port(const struct vteprec_physical_switch *ps,
1145 struct vteprec_physical_port *port)
1147 struct vteprec_physical_port **ports;
1150 ports = xmalloc(sizeof *ps->ports * (ps->n_ports + 1));
1151 for (i = 0; i < ps->n_ports; i++) {
1152 ports[i] = ps->ports[i];
1154 ports[ps->n_ports] = port;
1155 vteprec_physical_switch_set_ports(ps, ports, ps->n_ports + 1);
1160 pswitch_delete_port(const struct vteprec_physical_switch *ps,
1161 const struct vteprec_physical_port *port)
1163 struct vteprec_physical_port **ports;
1166 ports = xmalloc(sizeof *ps->ports * ps->n_ports);
1167 for (i = n = 0; i < ps->n_ports; i++) {
1168 if (ps->ports[i] != port) {
1169 ports[n++] = ps->ports[i];
1172 vteprec_physical_switch_set_ports(ps, ports, n);
1177 vtep_insert_pswitch(const struct vteprec_global *vtep_global,
1178 struct vteprec_physical_switch *ps)
1180 struct vteprec_physical_switch **pswitches;
1183 pswitches = xmalloc(sizeof *vtep_global->switches
1184 * (vtep_global->n_switches + 1));
1185 for (i = 0; i < vtep_global->n_switches; i++) {
1186 pswitches[i] = vtep_global->switches[i];
1188 pswitches[vtep_global->n_switches] = ps;
1189 vteprec_global_set_switches(vtep_global, pswitches,
1190 vtep_global->n_switches + 1);
1195 cmd_add_ps(struct ctl_context *ctx)
1197 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1198 const char *ps_name = ctx->argv[1];
1199 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1200 struct vteprec_physical_switch *ps;
1202 vtep_ctl_context_populate_cache(ctx);
1203 if (find_pswitch(vtepctl_ctx, ps_name, false)) {
1205 ctl_fatal("cannot create physical switch %s because it "
1206 "already exists", ps_name);
1211 ps = vteprec_physical_switch_insert(ctx->txn);
1212 vteprec_physical_switch_set_name(ps, ps_name);
1214 vtep_insert_pswitch(vtepctl_ctx->vtep_global, ps);
1216 vtep_ctl_context_invalidate_cache(ctx);
1220 del_port(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_port *port)
1222 pswitch_delete_port(port->ps->ps_cfg, port->port_cfg);
1223 del_cached_port(vtepctl_ctx, port);
1227 del_pswitch(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_pswitch *ps)
1229 struct vtep_ctl_port *port, *next_port;
1231 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &ps->ports) {
1232 del_port(vtepctl_ctx, port);
1235 del_cached_pswitch(vtepctl_ctx, ps);
1239 cmd_del_ps(struct ctl_context *ctx)
1241 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1242 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1243 struct vtep_ctl_pswitch *ps;
1245 vtep_ctl_context_populate_cache(ctx);
1246 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], must_exist);
1248 del_pswitch(vtepctl_ctx, ps);
1253 output_sorted(struct svec *svec, struct ds *output)
1259 SVEC_FOR_EACH (i, name, svec) {
1260 ds_put_format(output, "%s\n", name);
1265 cmd_list_ps(struct ctl_context *ctx)
1267 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1268 struct shash_node *node;
1269 struct svec pswitches;
1271 vtep_ctl_context_populate_cache(ctx);
1273 svec_init(&pswitches);
1274 SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
1275 struct vtep_ctl_pswitch *ps = node->data;
1277 svec_add(&pswitches, ps->name);
1279 output_sorted(&pswitches, &ctx->output);
1280 svec_destroy(&pswitches);
1284 cmd_ps_exists(struct ctl_context *ctx)
1286 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1288 vtep_ctl_context_populate_cache(ctx);
1289 if (!find_pswitch(vtepctl_ctx, ctx->argv[1], false)) {
1295 cmd_list_ports(struct ctl_context *ctx)
1297 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1298 struct vtep_ctl_pswitch *ps;
1299 struct vtep_ctl_port *port;
1302 vtep_ctl_context_populate_cache(ctx);
1303 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], true);
1304 vteprec_physical_switch_verify_ports(ps->ps_cfg);
1307 LIST_FOR_EACH (port, ports_node, &ps->ports) {
1308 if (strcmp(port->port_cfg->name, ps->name)) {
1309 svec_add(&ports, port->port_cfg->name);
1312 output_sorted(&ports, &ctx->output);
1313 svec_destroy(&ports);
1317 add_port(struct ctl_context *ctx, const char *ps_name,
1318 const char *port_name, bool may_exist)
1320 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1321 struct vtep_ctl_port *vtep_ctl_port;
1322 struct vtep_ctl_pswitch *ps;
1323 struct vteprec_physical_port *port;
1325 vtep_ctl_context_populate_cache(ctx);
1327 vtep_ctl_port = find_port(vtepctl_ctx, ps_name, port_name, false);
1328 if (vtep_ctl_port) {
1330 ctl_fatal("cannot create a port named %s on %s because a "
1331 "port with that name already exists",
1332 port_name, ps_name);
1337 ps = find_pswitch(vtepctl_ctx, ps_name, true);
1339 port = vteprec_physical_port_insert(ctx->txn);
1340 vteprec_physical_port_set_name(port, port_name);
1342 pswitch_insert_port(ps->ps_cfg, port);
1344 add_port_to_cache(vtepctl_ctx, ps, port);
1348 cmd_add_port(struct ctl_context *ctx)
1350 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1352 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist);
1356 cmd_del_port(struct ctl_context *ctx)
1358 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1359 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1360 struct vtep_ctl_port *port;
1362 vtep_ctl_context_populate_cache(ctx);
1364 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], must_exist);
1366 if (ctx->argc == 3) {
1367 struct vtep_ctl_pswitch *ps;
1369 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], true);
1370 if (port->ps != ps) {
1371 ctl_fatal("physical switch %s does not have a port %s",
1372 ctx->argv[1], ctx->argv[2]);
1376 del_port(vtepctl_ctx, port);
1380 static struct vtep_ctl_lswitch *
1381 find_lswitch(struct vtep_ctl_context *vtepctl_ctx,
1382 const char *name, bool must_exist)
1384 struct vtep_ctl_lswitch *ls;
1386 ovs_assert(vtepctl_ctx->cache_valid);
1388 ls = shash_find_data(&vtepctl_ctx->lswitches, name);
1389 if (must_exist && !ls) {
1390 ctl_fatal("no logical switch named %s", name);
1396 cmd_add_ls(struct ctl_context *ctx)
1398 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1399 const char *ls_name = ctx->argv[1];
1400 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1401 struct vteprec_logical_switch *ls;
1403 vtep_ctl_context_populate_cache(ctx);
1404 if (find_lswitch(vtepctl_ctx, ls_name, false)) {
1406 ctl_fatal("cannot create logical switch %s because it "
1407 "already exists", ls_name);
1412 ls = vteprec_logical_switch_insert(ctx->txn);
1413 vteprec_logical_switch_set_name(ls, ls_name);
1415 vtep_ctl_context_invalidate_cache(ctx);
1419 del_lswitch(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_lswitch *ls)
1421 del_cached_lswitch(vtepctl_ctx, ls);
1425 cmd_del_ls(struct ctl_context *ctx)
1427 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1428 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1429 struct vtep_ctl_lswitch *ls;
1431 vtep_ctl_context_populate_cache(ctx);
1432 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], must_exist);
1434 del_lswitch(vtepctl_ctx, ls);
1439 cmd_list_ls(struct ctl_context *ctx)
1441 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1442 struct shash_node *node;
1443 struct svec lswitches;
1445 vtep_ctl_context_populate_cache(ctx);
1447 svec_init(&lswitches);
1448 SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
1449 struct vtep_ctl_lswitch *ls = node->data;
1451 svec_add(&lswitches, ls->name);
1453 output_sorted(&lswitches, &ctx->output);
1454 svec_destroy(&lswitches);
1458 cmd_ls_exists(struct ctl_context *ctx)
1460 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1462 vtep_ctl_context_populate_cache(ctx);
1463 if (!find_lswitch(vtepctl_ctx, ctx->argv[1], false)) {
1469 cmd_list_bindings(struct ctl_context *ctx)
1471 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1472 const struct shash_node *node;
1473 struct vtep_ctl_port *port;
1474 struct svec bindings;
1476 vtep_ctl_context_populate_cache(ctx);
1477 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1479 svec_init(&bindings);
1480 SHASH_FOR_EACH (node, &port->bindings) {
1481 struct vtep_ctl_lswitch *lswitch = node->data;
1484 binding = xasprintf("%04lld %s", strtoll(node->name, NULL, 0),
1486 svec_add_nocopy(&bindings, binding);
1488 output_sorted(&bindings, &ctx->output);
1489 svec_destroy(&bindings);
1493 cmd_bind_ls(struct ctl_context *ctx)
1495 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1496 struct vtep_ctl_lswitch *ls;
1497 struct vtep_ctl_port *port;
1500 vtep_ctl_context_populate_cache(ctx);
1502 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1503 vlan = ctx->argv[3];
1504 ls = find_lswitch(vtepctl_ctx, ctx->argv[4], true);
1506 add_ls_binding_to_cache(port, vlan, ls);
1507 commit_ls_bindings(port);
1509 vtep_ctl_context_invalidate_cache(ctx);
1513 cmd_unbind_ls(struct ctl_context *ctx)
1515 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1516 struct vtep_ctl_port *port;
1519 vtep_ctl_context_populate_cache(ctx);
1521 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1522 vlan = ctx->argv[3];
1524 del_cached_ls_binding(port, vlan);
1525 commit_ls_bindings(port);
1527 vtep_ctl_context_invalidate_cache(ctx);
1531 cmd_set_replication_mode(struct ctl_context *ctx)
1533 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1534 struct vtep_ctl_lswitch *ls;
1535 const char *ls_name = ctx->argv[1];
1537 vtep_ctl_context_populate_cache(ctx);
1539 if (strcmp(ctx->argv[2], "service_node") &&
1540 strcmp(ctx->argv[2], "source_node")) {
1541 ctl_fatal("Replication mode must be 'service_node' or 'source_node'");
1544 ls = find_lswitch(vtepctl_ctx, ls_name, true);
1545 vteprec_logical_switch_set_replication_mode(ls->ls_cfg, ctx->argv[2]);
1547 vtep_ctl_context_invalidate_cache(ctx);
1551 cmd_get_replication_mode(struct ctl_context *ctx)
1553 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1554 struct vtep_ctl_lswitch *ls;
1555 const char *ls_name = ctx->argv[1];
1557 vtep_ctl_context_populate_cache(ctx);
1559 ls = find_lswitch(vtepctl_ctx, ls_name, true);
1560 ds_put_format(&ctx->output, "%s\n", ls->ls_cfg->replication_mode);
1563 static struct vtep_ctl_lrouter *
1564 find_lrouter(struct vtep_ctl_context *vtepctl_ctx,
1565 const char *name, bool must_exist)
1567 struct vtep_ctl_lrouter *lr;
1569 ovs_assert(vtepctl_ctx->cache_valid);
1571 lr = shash_find_data(&vtepctl_ctx->lrouters, name);
1572 if (must_exist && !lr) {
1573 ctl_fatal("no logical router named %s", name);
1579 cmd_add_lr(struct ctl_context *ctx)
1581 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1582 const char *lr_name = ctx->argv[1];
1583 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1584 struct vteprec_logical_router *lr;
1586 vtep_ctl_context_populate_cache(ctx);
1587 if (find_lrouter(vtepctl_ctx, lr_name, false)) {
1589 ctl_fatal("cannot create logical router %s because it "
1590 "already exists", lr_name);
1595 lr = vteprec_logical_router_insert(ctx->txn);
1596 vteprec_logical_router_set_name(lr, lr_name);
1598 vtep_ctl_context_invalidate_cache(ctx);
1602 del_lrouter(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_lrouter *lr)
1604 del_cached_lrouter(vtepctl_ctx, lr);
1608 cmd_del_lr(struct ctl_context *ctx)
1610 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1611 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1612 struct vtep_ctl_lrouter *lr;
1614 vtep_ctl_context_populate_cache(ctx);
1615 lr = find_lrouter(vtepctl_ctx, ctx->argv[1], must_exist);
1617 del_lrouter(vtepctl_ctx, lr);
1622 cmd_list_lr(struct ctl_context *ctx)
1624 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1625 struct shash_node *node;
1626 struct svec lrouters;
1628 vtep_ctl_context_populate_cache(ctx);
1630 svec_init(&lrouters);
1631 SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
1632 struct vtep_ctl_lrouter *lr = node->data;
1634 svec_add(&lrouters, lr->name);
1636 output_sorted(&lrouters, &ctx->output);
1637 svec_destroy(&lrouters);
1641 cmd_lr_exists(struct ctl_context *ctx)
1643 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1645 vtep_ctl_context_populate_cache(ctx);
1646 if (!find_lrouter(vtepctl_ctx, ctx->argv[1], false)) {
1652 add_ucast_entry(struct ctl_context *ctx, bool local)
1654 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1655 struct vtep_ctl_lswitch *ls;
1659 struct vteprec_physical_locator *ploc_cfg;
1661 vtep_ctl_context_populate_cache(ctx);
1663 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1666 if (ctx->argc == 4) {
1667 encap = "vxlan_over_ipv4";
1668 dst_ip = ctx->argv[3];
1670 encap = ctx->argv[3];
1671 dst_ip = ctx->argv[4];
1674 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1676 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1677 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1678 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1680 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
1684 struct vteprec_ucast_macs_local *ucast_cfg;
1686 ucast_cfg = shash_find_data(&ls->ucast_local, mac);
1688 ucast_cfg = vteprec_ucast_macs_local_insert(ctx->txn);
1689 vteprec_ucast_macs_local_set_MAC(ucast_cfg, mac);
1690 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg, ls->ls_cfg);
1691 shash_add(&ls->ucast_local, mac, ucast_cfg);
1693 vteprec_ucast_macs_local_set_locator(ucast_cfg, ploc_cfg);
1695 struct vteprec_ucast_macs_remote *ucast_cfg;
1697 ucast_cfg = shash_find_data(&ls->ucast_remote, mac);
1699 ucast_cfg = vteprec_ucast_macs_remote_insert(ctx->txn);
1700 vteprec_ucast_macs_remote_set_MAC(ucast_cfg, mac);
1701 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg, ls->ls_cfg);
1702 shash_add(&ls->ucast_remote, mac, ucast_cfg);
1704 vteprec_ucast_macs_remote_set_locator(ucast_cfg, ploc_cfg);
1707 vtep_ctl_context_invalidate_cache(ctx);
1711 cmd_add_ucast_local(struct ctl_context *ctx)
1713 add_ucast_entry(ctx, true);
1717 cmd_add_ucast_remote(struct ctl_context *ctx)
1719 add_ucast_entry(ctx, false);
1723 del_ucast_entry(struct ctl_context *ctx, bool local)
1725 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1726 struct vtep_ctl_lswitch *ls;
1727 struct shash *ucast_shash;
1728 struct shash_node *node;
1730 vtep_ctl_context_populate_cache(ctx);
1732 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1733 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1735 node = shash_find(ucast_shash, ctx->argv[2]);
1741 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1742 vteprec_ucast_macs_local_delete(ucast_cfg);
1744 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1745 vteprec_ucast_macs_remote_delete(ucast_cfg);
1747 shash_delete(ucast_shash, node);
1749 vtep_ctl_context_invalidate_cache(ctx);
1753 cmd_del_ucast_local(struct ctl_context *ctx)
1755 del_ucast_entry(ctx, true);
1759 cmd_del_ucast_remote(struct ctl_context *ctx)
1761 del_ucast_entry(ctx, false);
1765 commit_mcast_entries(struct vtep_ctl_mcast_mac *mcast_mac)
1767 struct vtep_ctl_ploc *ploc;
1768 struct vteprec_physical_locator **locators = NULL;
1772 n_locators = ovs_list_size(&mcast_mac->locators);
1773 ovs_assert(n_locators);
1775 locators = xmalloc(n_locators * sizeof *locators);
1778 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
1779 locators[i] = (struct vteprec_physical_locator *)ploc->ploc_cfg;
1783 vteprec_physical_locator_set_set_locators(mcast_mac->ploc_set_cfg,
1791 add_mcast_entry(struct ctl_context *ctx,
1792 struct vtep_ctl_lswitch *ls, const char *mac,
1793 const char *encap, const char *dst_ip, bool local)
1795 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1796 struct shash *mcast_shash;
1797 struct vtep_ctl_mcast_mac *mcast_mac;
1798 struct vteprec_physical_locator *ploc_cfg;
1799 struct vteprec_physical_locator_set *ploc_set_cfg;
1801 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1803 /* Physical locator sets are immutable, so allocate a new one. */
1804 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1806 mcast_mac = shash_find_data(mcast_shash, mac);
1808 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mac, ploc_set_cfg,
1812 mcast_mac->local_cfg = vteprec_mcast_macs_local_insert(ctx->txn);
1813 vteprec_mcast_macs_local_set_MAC(mcast_mac->local_cfg, mac);
1814 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1816 vteprec_mcast_macs_local_set_logical_switch(mcast_mac->local_cfg,
1818 mcast_mac->remote_cfg = NULL;
1820 mcast_mac->remote_cfg = vteprec_mcast_macs_remote_insert(ctx->txn);
1821 vteprec_mcast_macs_remote_set_MAC(mcast_mac->remote_cfg, mac);
1822 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1824 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac->remote_cfg,
1826 mcast_mac->local_cfg = NULL;
1829 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1831 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1834 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1839 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1841 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1842 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1843 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1845 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
1848 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1849 commit_mcast_entries(mcast_mac);
1853 del_mcast_entry(struct ctl_context *ctx,
1854 struct vtep_ctl_lswitch *ls, const char *mac,
1855 const char *encap, const char *dst_ip, bool local)
1857 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1858 struct vtep_ctl_mcast_mac *mcast_mac;
1859 struct shash *mcast_shash;
1860 struct vteprec_physical_locator *ploc_cfg;
1861 struct vteprec_physical_locator_set *ploc_set_cfg;
1863 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1865 mcast_mac = shash_find_data(mcast_shash, mac);
1870 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1872 /* Couldn't find the physical locator, so just ignore. */
1876 /* Physical locator sets are immutable, so allocate a new one. */
1877 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1878 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1880 del_ploc_from_mcast_mac(mcast_mac, ploc_cfg);
1881 if (ovs_list_is_empty(&mcast_mac->locators)) {
1882 struct shash_node *node = shash_find(mcast_shash, mac);
1884 vteprec_physical_locator_set_delete(ploc_set_cfg);
1887 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1889 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1893 shash_delete(mcast_shash, node);
1896 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1899 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1902 commit_mcast_entries(mcast_mac);
1907 add_del_mcast_entry(struct ctl_context *ctx, bool add, bool local)
1909 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1910 struct vtep_ctl_lswitch *ls;
1915 vtep_ctl_context_populate_cache(ctx);
1917 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1920 if (ctx->argc == 4) {
1921 encap = "vxlan_over_ipv4";
1922 dst_ip = ctx->argv[3];
1924 encap = ctx->argv[3];
1925 dst_ip = ctx->argv[4];
1929 add_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1931 del_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1934 vtep_ctl_context_invalidate_cache(ctx);
1938 cmd_add_mcast_local(struct ctl_context *ctx)
1940 add_del_mcast_entry(ctx, true, true);
1944 cmd_add_mcast_remote(struct ctl_context *ctx)
1946 add_del_mcast_entry(ctx, true, false);
1950 cmd_del_mcast_local(struct ctl_context *ctx)
1952 add_del_mcast_entry(ctx, false, true);
1956 cmd_del_mcast_remote(struct ctl_context *ctx)
1958 add_del_mcast_entry(ctx, false, false);
1962 clear_macs(struct ctl_context *ctx, bool local)
1964 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1965 struct vtep_ctl_lswitch *ls;
1966 const struct shash_node *node;
1967 struct shash *ucast_shash;
1968 struct shash *mcast_shash;
1970 vtep_ctl_context_populate_cache(ctx);
1971 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1973 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1974 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1976 SHASH_FOR_EACH (node, ucast_shash) {
1978 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1979 vteprec_ucast_macs_local_delete(ucast_cfg);
1981 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1982 vteprec_ucast_macs_remote_delete(ucast_cfg);
1986 SHASH_FOR_EACH (node, mcast_shash) {
1987 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
1989 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1991 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1995 vtep_ctl_context_invalidate_cache(ctx);
1999 cmd_clear_local_macs(struct ctl_context *ctx)
2001 clear_macs(ctx, true);
2005 cmd_clear_remote_macs(struct ctl_context *ctx)
2007 clear_macs(ctx, false);
2011 list_macs(struct ctl_context *ctx, bool local)
2013 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2014 struct vtep_ctl_lswitch *ls;
2015 const struct shash_node *node;
2016 struct shash *ucast_shash;
2017 struct svec ucast_macs;
2018 struct shash *mcast_shash;
2019 struct svec mcast_macs;
2021 vtep_ctl_context_populate_cache(ctx);
2022 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
2024 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2025 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2027 svec_init(&ucast_macs);
2028 SHASH_FOR_EACH (node, ucast_shash) {
2029 struct vteprec_ucast_macs_local *ucast_local = node->data;
2030 struct vteprec_ucast_macs_remote *ucast_remote = node->data;
2031 struct vteprec_physical_locator *ploc_cfg;
2034 ploc_cfg = local ? ucast_local->locator : ucast_remote->locator;
2036 entry = xasprintf(" %s -> %s/%s", node->name,
2037 ploc_cfg->encapsulation_type, ploc_cfg->dst_ip);
2038 svec_add_nocopy(&ucast_macs, entry);
2040 ds_put_format(&ctx->output, "ucast-mac-%s\n", local ? "local" : "remote");
2041 output_sorted(&ucast_macs, &ctx->output);
2042 ds_put_char(&ctx->output, '\n');
2043 svec_destroy(&ucast_macs);
2045 svec_init(&mcast_macs);
2046 SHASH_FOR_EACH (node, mcast_shash) {
2047 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2048 struct vtep_ctl_ploc *ploc;
2051 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
2052 entry = xasprintf(" %s -> %s/%s", node->name,
2053 ploc->ploc_cfg->encapsulation_type,
2054 ploc->ploc_cfg->dst_ip);
2055 svec_add_nocopy(&mcast_macs, entry);
2058 ds_put_format(&ctx->output, "mcast-mac-%s\n", local ? "local" : "remote");
2059 output_sorted(&mcast_macs, &ctx->output);
2060 ds_put_char(&ctx->output, '\n');
2061 svec_destroy(&mcast_macs);
2065 cmd_list_local_macs(struct ctl_context *ctx)
2067 list_macs(ctx, true);
2071 cmd_list_remote_macs(struct ctl_context *ctx)
2073 list_macs(ctx, false);
2077 verify_managers(const struct vteprec_global *vtep_global)
2081 vteprec_global_verify_managers(vtep_global);
2083 for (i = 0; i < vtep_global->n_managers; ++i) {
2084 const struct vteprec_manager *mgr = vtep_global->managers[i];
2086 vteprec_manager_verify_target(mgr);
2091 pre_manager(struct ctl_context *ctx)
2093 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_managers);
2094 ovsdb_idl_add_column(ctx->idl, &vteprec_manager_col_target);
2098 cmd_get_manager(struct ctl_context *ctx)
2100 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2101 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2102 struct svec targets;
2105 verify_managers(vtep_global);
2107 /* Print the targets in sorted order for reproducibility. */
2108 svec_init(&targets);
2110 for (i = 0; i < vtep_global->n_managers; i++) {
2111 svec_add(&targets, vtep_global->managers[i]->target);
2114 svec_sort_unique(&targets);
2115 for (i = 0; i < targets.n; i++) {
2116 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2118 svec_destroy(&targets);
2122 delete_managers(const struct vtep_ctl_context *vtepctl_ctx)
2124 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2127 /* Delete Manager rows pointed to by 'managers' column. */
2128 for (i = 0; i < vtep_global->n_managers; i++) {
2129 vteprec_manager_delete(vtep_global->managers[i]);
2132 /* Delete 'Manager' row refs in 'managers' column. */
2133 vteprec_global_set_managers(vtep_global, NULL, 0);
2137 cmd_del_manager(struct ctl_context *ctx)
2139 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2140 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2142 verify_managers(vtep_global);
2143 delete_managers(vtepctl_ctx);
2147 insert_managers(struct vtep_ctl_context *vtepctl_ctx, char *targets[], size_t n)
2149 struct vteprec_manager **managers;
2152 /* Insert each manager in a new row in Manager table. */
2153 managers = xmalloc(n * sizeof *managers);
2154 for (i = 0; i < n; i++) {
2155 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2156 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2158 managers[i] = vteprec_manager_insert(vtepctl_ctx->base.txn);
2159 vteprec_manager_set_target(managers[i], targets[i]);
2162 /* Store uuids of new Manager rows in 'managers' column. */
2163 vteprec_global_set_managers(vtepctl_ctx->vtep_global, managers, n);
2168 cmd_set_manager(struct ctl_context *ctx)
2170 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2171 const size_t n = ctx->argc - 1;
2173 verify_managers(vtepctl_ctx->vtep_global);
2174 delete_managers(vtepctl_ctx);
2175 insert_managers(vtepctl_ctx, &ctx->argv[1], n);
2178 /* Parameter commands. */
2179 static const struct ctl_table_class tables[] = {
2180 {&vteprec_table_global,
2181 {{&vteprec_table_global, NULL, NULL},
2182 {NULL, NULL, NULL}}},
2184 {&vteprec_table_logical_binding_stats,
2185 {{NULL, NULL, NULL},
2186 {NULL, NULL, NULL}}},
2188 {&vteprec_table_logical_switch,
2189 {{&vteprec_table_logical_switch, &vteprec_logical_switch_col_name, NULL},
2190 {NULL, NULL, NULL}}},
2192 {&vteprec_table_ucast_macs_local,
2193 {{NULL, NULL, NULL},
2194 {NULL, NULL, NULL}}},
2196 {&vteprec_table_ucast_macs_remote,
2197 {{NULL, NULL, NULL},
2198 {NULL, NULL, NULL}}},
2200 {&vteprec_table_mcast_macs_local,
2201 {{NULL, NULL, NULL},
2202 {NULL, NULL, NULL}}},
2204 {&vteprec_table_mcast_macs_remote,
2205 {{NULL, NULL, NULL},
2206 {NULL, NULL, NULL}}},
2208 {&vteprec_table_manager,
2209 {{&vteprec_table_manager, &vteprec_manager_col_target, NULL},
2210 {NULL, NULL, NULL}}},
2212 {&vteprec_table_physical_locator,
2213 {{NULL, NULL, NULL},
2214 {NULL, NULL, NULL}}},
2216 {&vteprec_table_physical_locator_set,
2217 {{NULL, NULL, NULL},
2218 {NULL, NULL, NULL}}},
2220 {&vteprec_table_physical_port,
2221 {{&vteprec_table_physical_port, &vteprec_physical_port_col_name, NULL},
2222 {NULL, NULL, NULL}}},
2224 {&vteprec_table_physical_switch,
2225 {{&vteprec_table_physical_switch, &vteprec_physical_switch_col_name, NULL},
2226 {NULL, NULL, NULL}}},
2228 {&vteprec_table_tunnel,
2229 {{NULL, NULL, NULL},
2230 {NULL, NULL, NULL}}},
2232 {&vteprec_table_logical_router,
2233 {{&vteprec_table_logical_router, &vteprec_logical_router_col_name, NULL},
2234 {NULL, NULL, NULL}}},
2236 {&vteprec_table_arp_sources_local,
2237 {{NULL, NULL, NULL},
2238 {NULL, NULL, NULL}}},
2240 {&vteprec_table_arp_sources_remote,
2241 {{NULL, NULL, NULL},
2242 {NULL, NULL, NULL}}},
2244 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2249 vtep_ctl_context_init_command(struct vtep_ctl_context *vtepctl_ctx,
2250 struct ctl_command *command)
2252 ctl_context_init_command(&vtepctl_ctx->base, command);
2253 vtepctl_ctx->verified_ports = false;
2258 vtep_ctl_context_init(struct vtep_ctl_context *vtepctl_ctx,
2259 struct ctl_command *command,
2260 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2261 const struct vteprec_global *vtep_global,
2262 struct ovsdb_symbol_table *symtab)
2264 ctl_context_init(&vtepctl_ctx->base, command, idl, txn, symtab,
2265 vtep_ctl_context_invalidate_cache);
2267 vtepctl_ctx->verified_ports = false;
2269 vtepctl_ctx->vtep_global = vtep_global;
2270 vtepctl_ctx->cache_valid = false;
2274 vtep_ctl_context_done_command(struct vtep_ctl_context *vtepctl_ctx,
2275 struct ctl_command *command)
2277 ctl_context_done_command(&vtepctl_ctx->base, command);
2281 vtep_ctl_context_done(struct vtep_ctl_context *vtepctl_ctx,
2282 struct ctl_command *command)
2284 ctl_context_done(&vtepctl_ctx->base, command);
2288 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2289 struct ovsdb_idl *idl)
2291 struct ctl_command *c;
2293 ovsdb_idl_add_table(idl, &vteprec_table_global);
2294 for (c = commands; c < &commands[n_commands]; c++) {
2295 if (c->syntax->prerequisites) {
2296 struct vtep_ctl_context vtepctl_ctx;
2298 ds_init(&c->output);
2301 vtep_ctl_context_init(&vtepctl_ctx, c, idl, NULL, NULL, NULL);
2302 (c->syntax->prerequisites)(&vtepctl_ctx.base);
2303 vtep_ctl_context_done(&vtepctl_ctx, c);
2305 ovs_assert(!c->output.string);
2306 ovs_assert(!c->table);
2312 do_vtep_ctl(const char *args, struct ctl_command *commands,
2313 size_t n_commands, struct ovsdb_idl *idl)
2315 struct ovsdb_idl_txn *txn;
2316 const struct vteprec_global *vtep_global;
2317 enum ovsdb_idl_txn_status status;
2318 struct ovsdb_symbol_table *symtab;
2319 struct vtep_ctl_context vtepctl_ctx;
2320 struct ctl_command *c;
2321 struct shash_node *node;
2324 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2326 ovsdb_idl_txn_set_dry_run(txn);
2329 ovsdb_idl_txn_add_comment(txn, "vtep-ctl: %s", args);
2331 vtep_global = vteprec_global_first(idl);
2333 /* XXX add verification that table is empty */
2334 vtep_global = vteprec_global_insert(txn);
2337 symtab = ovsdb_symbol_table_create();
2338 for (c = commands; c < &commands[n_commands]; c++) {
2339 ds_init(&c->output);
2342 vtep_ctl_context_init(&vtepctl_ctx, NULL, idl, txn, vtep_global, symtab);
2343 for (c = commands; c < &commands[n_commands]; c++) {
2344 vtep_ctl_context_init_command(&vtepctl_ctx, c);
2345 if (c->syntax->run) {
2346 (c->syntax->run)(&vtepctl_ctx.base);
2348 vtep_ctl_context_done_command(&vtepctl_ctx, c);
2350 if (vtepctl_ctx.base.try_again) {
2351 vtep_ctl_context_done(&vtepctl_ctx, NULL);
2355 vtep_ctl_context_done(&vtepctl_ctx, NULL);
2357 SHASH_FOR_EACH (node, &symtab->sh) {
2358 struct ovsdb_symbol *symbol = node->data;
2359 if (!symbol->created) {
2360 ctl_fatal("row id \"%s\" is referenced but never created "
2361 "(e.g. with \"-- --id=%s create ...\")",
2362 node->name, node->name);
2364 if (!symbol->strong_ref) {
2365 if (!symbol->weak_ref) {
2366 VLOG_WARN("row id \"%s\" was created but no reference to it "
2367 "was inserted, so it will not actually appear in "
2368 "the database", node->name);
2370 VLOG_WARN("row id \"%s\" was created but only a weak "
2371 "reference to it was inserted, so it will not "
2372 "actually appear in the database", node->name);
2377 status = ovsdb_idl_txn_commit_block(txn);
2378 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2379 for (c = commands; c < &commands[n_commands]; c++) {
2380 if (c->syntax->postprocess) {
2381 vtep_ctl_context_init(&vtepctl_ctx, c, idl, txn, vtep_global, symtab);
2382 (c->syntax->postprocess)(&vtepctl_ctx.base);
2383 vtep_ctl_context_done(&vtepctl_ctx, c);
2387 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2388 ovsdb_idl_txn_destroy(txn);
2389 txn = the_idl_txn = NULL;
2392 case TXN_UNCOMMITTED:
2393 case TXN_INCOMPLETE:
2397 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2398 ctl_fatal("transaction aborted");
2408 ctl_fatal("transaction error: %s", error);
2410 case TXN_NOT_LOCKED:
2411 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2412 ctl_fatal("database not locked");
2419 ovsdb_symbol_table_destroy(symtab);
2421 for (c = commands; c < &commands[n_commands]; c++) {
2422 struct ds *ds = &c->output;
2425 table_print(c->table, &table_style);
2426 } else if (oneline) {
2430 for (j = 0; j < ds->length; j++) {
2431 int ch = ds->string[j];
2434 fputs("\\n", stdout);
2438 fputs("\\\\", stdout);
2447 fputs(ds_cstr(ds), stdout);
2449 ds_destroy(&c->output);
2450 table_destroy(c->table);
2453 shash_destroy_free_data(&c->options);
2457 ovsdb_idl_destroy(idl);
2462 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2463 * resources and return so that the caller can try again. */
2465 ovsdb_idl_txn_abort(txn);
2466 ovsdb_idl_txn_destroy(txn);
2468 ovsdb_symbol_table_destroy(symtab);
2469 for (c = commands; c < &commands[n_commands]; c++) {
2470 ds_destroy(&c->output);
2471 table_destroy(c->table);
2477 static const struct ctl_command_syntax vtep_commands[] = {
2478 /* Physical Switch commands. */
2479 {"add-ps", 1, 1, NULL, pre_get_info, cmd_add_ps, NULL, "--may-exist", RW},
2480 {"del-ps", 1, 1, NULL, pre_get_info, cmd_del_ps, NULL, "--if-exists", RW},
2481 {"list-ps", 0, 0, NULL, pre_get_info, cmd_list_ps, NULL, "", RO},
2482 {"ps-exists", 1, 1, NULL, pre_get_info, cmd_ps_exists, NULL, "", RO},
2484 /* Port commands. */
2485 {"list-ports", 1, 1, NULL, pre_get_info, cmd_list_ports, NULL, "", RO},
2486 {"add-port", 2, 2, NULL, pre_get_info, cmd_add_port, NULL, "--may-exist",
2488 {"del-port", 2, 2, NULL, pre_get_info, cmd_del_port, NULL, "--if-exists",
2491 /* Logical Switch commands. */
2492 {"add-ls", 1, 1, NULL, pre_get_info, cmd_add_ls, NULL, "--may-exist", RW},
2493 {"del-ls", 1, 1, NULL, pre_get_info, cmd_del_ls, NULL, "--if-exists", RW},
2494 {"list-ls", 0, 0, NULL, pre_get_info, cmd_list_ls, NULL, "", RO},
2495 {"ls-exists", 1, 1, NULL, pre_get_info, cmd_ls_exists, NULL, "", RO},
2496 {"list-bindings", 2, 2, NULL, pre_get_info, cmd_list_bindings, NULL, "", RO},
2497 {"bind-ls", 4, 4, NULL, pre_get_info, cmd_bind_ls, NULL, "", RO},
2498 {"unbind-ls", 3, 3, NULL, pre_get_info, cmd_unbind_ls, NULL, "", RO},
2499 {"set-replication-mode", 2, 2, "LS MODE", pre_get_info,
2500 cmd_set_replication_mode, NULL, "", RW},
2501 {"get-replication-mode", 1, 1, "LS", pre_get_info,
2502 cmd_get_replication_mode, NULL, "", RO},
2504 /* Logical Router commands. */
2505 {"add-lr", 1, 1, NULL, pre_get_info, cmd_add_lr, NULL, "--may-exist", RW},
2506 {"del-lr", 1, 1, NULL, pre_get_info, cmd_del_lr, NULL, "--if-exists", RW},
2507 {"list-lr", 0, 0, NULL, pre_get_info, cmd_list_lr, NULL, "", RO},
2508 {"lr-exists", 1, 1, NULL, pre_get_info, cmd_lr_exists, NULL, "", RO},
2510 /* MAC binding commands. */
2511 {"add-ucast-local", 3, 4, NULL, pre_get_info, cmd_add_ucast_local, NULL,
2513 {"del-ucast-local", 2, 2, NULL, pre_get_info, cmd_del_ucast_local, NULL,
2515 {"add-mcast-local", 3, 4, NULL, pre_get_info, cmd_add_mcast_local, NULL,
2517 {"del-mcast-local", 3, 4, NULL, pre_get_info, cmd_del_mcast_local, NULL,
2519 {"clear-local-macs", 1, 1, NULL, pre_get_info, cmd_clear_local_macs, NULL,
2521 {"list-local-macs", 1, 1, NULL, pre_get_info, cmd_list_local_macs, NULL,
2523 {"add-ucast-remote", 3, 4, NULL, pre_get_info, cmd_add_ucast_remote, NULL,
2525 {"del-ucast-remote", 2, 2, NULL, pre_get_info, cmd_del_ucast_remote, NULL,
2527 {"add-mcast-remote", 3, 4, NULL, pre_get_info, cmd_add_mcast_remote, NULL,
2529 {"del-mcast-remote", 3, 4, NULL, pre_get_info, cmd_del_mcast_remote, NULL,
2531 {"clear-remote-macs", 1, 1, NULL, pre_get_info, cmd_clear_remote_macs, NULL,
2533 {"list-remote-macs", 1, 1, NULL, pre_get_info, cmd_list_remote_macs, NULL,
2536 /* Manager commands. */
2537 {"get-manager", 0, 0, NULL, pre_manager, cmd_get_manager, NULL, "", RO},
2538 {"del-manager", 0, 0, NULL, pre_manager, cmd_del_manager, NULL, "", RW},
2539 {"set-manager", 1, INT_MAX, NULL, pre_manager, cmd_set_manager, NULL, "",
2542 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2545 /* Registers vsctl and common db commands. */
2547 vtep_ctl_cmd_init(void)
2549 ctl_init(tables, cmd_show_tables, vtep_ctl_exit);
2550 ctl_register_commands(vtep_commands);