2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015 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 "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 extern struct vlog_module VLM_reconnect;
98 struct ovsdb_idl *idl;
99 struct ctl_command *commands;
100 struct shash local_options;
105 set_program_name(argv[0]);
106 fatal_ignore_sigpipe();
107 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
108 vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
113 /* Log our arguments. This is often valuable for debugging systems. */
114 args = process_escape_args(argv);
115 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
117 /* Parse command line. */
118 shash_init(&local_options);
119 parse_options(argc, argv, &local_options);
120 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
127 /* Initialize IDL. */
128 idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
129 run_prerequisites(commands, n_commands, idl);
131 /* Execute the commands.
133 * 'seqno' is the database sequence number for which we last tried to
134 * execute our transaction. There's no point in trying to commit more than
135 * once for any given sequence number, because if the transaction fails
136 * it's because the database changed and we need to obtain an up-to-date
137 * view of the database before we try the transaction again. */
138 seqno = ovsdb_idl_get_seqno(idl);
141 if (!ovsdb_idl_is_alive(idl)) {
142 int retval = ovsdb_idl_get_last_error(idl);
143 ctl_fatal("%s: database connection failed (%s)",
144 db, ovs_retval_to_string(retval));
147 if (seqno != ovsdb_idl_get_seqno(idl)) {
148 seqno = ovsdb_idl_get_seqno(idl);
149 do_vtep_ctl(args, commands, n_commands, idl);
152 if (seqno == ovsdb_idl_get_seqno(idl)) {
160 parse_options(int argc, char *argv[], struct shash *local_options)
163 OPT_DB = UCHAR_MAX + 1,
172 static const struct option global_long_options[] = {
173 {"db", required_argument, NULL, OPT_DB},
174 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
175 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
176 {"oneline", no_argument, NULL, OPT_ONELINE},
177 {"timeout", required_argument, NULL, 't'},
178 {"help", no_argument, NULL, 'h'},
179 {"version", no_argument, NULL, 'V'},
182 STREAM_SSL_LONG_OPTIONS,
183 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
186 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
187 char *tmp, *short_options;
189 struct option *options;
190 size_t allocated_options;
194 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
195 short_options = xasprintf("+%s", tmp);
198 /* We want to parse both global and command-specific options here, but
199 * getopt_long() isn't too convenient for the job. We copy our global
200 * options into a dynamic array, then append all of the command-specific
202 options = xmemdup(global_long_options, sizeof global_long_options);
203 allocated_options = ARRAY_SIZE(global_long_options);
204 n_options = n_global_long_options;
205 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
206 table_style.format = TF_LIST;
212 c = getopt_long(argc, argv, short_options, options, &idx);
227 vlog_set_levels(&VLM_vtep_ctl, VLF_SYSLOG, VLL_WARN);
235 if (shash_find(local_options, options[idx].name)) {
236 ctl_fatal("'%s' option specified multiple times",
239 shash_add_nocopy(local_options,
240 xasprintf("--%s", options[idx].name),
241 optarg ? xstrdup(optarg) : NULL);
248 ovs_print_version(0, 0);
249 printf("DB Schema %s\n", vteprec_get_db_version());
253 timeout = strtoul(optarg, NULL, 10);
255 ctl_fatal("value %s on -t or --timeout is invalid",
261 TABLE_OPTION_HANDLERS(&table_style)
263 STREAM_SSL_OPTION_HANDLERS
265 case OPT_PEER_CA_CERT:
266 stream_ssl_set_peer_ca_cert_file(optarg);
279 db = ctl_default_db();
282 for (i = n_global_long_options; options[i].name; i++) {
283 free(CONST_CAST(char *, options[i].name));
288 /* Frees the current transaction and the underlying IDL and then calls
291 * Freeing the transaction and the IDL is not strictly necessary, but it makes
292 * for a clean memory leak report from valgrind in the normal case. That makes
293 * it easier to notice real memory leaks. */
295 vtep_ctl_exit(int status)
298 ovsdb_idl_txn_abort(the_idl_txn);
299 ovsdb_idl_txn_destroy(the_idl_txn);
301 ovsdb_idl_destroy(the_idl);
309 %s: VTEP configuration utility\n\
310 usage: %s [OPTIONS] COMMAND [ARG...]\n\
313 show print overview of database contents\n\
316 get-manager print the managers\n\
317 del-manager delete the managers\n\
318 set-manager TARGET... set the list of managers to TARGET...\n\
320 Physical Switch commands:\n\
321 add-ps PS create a new physical switch named PS\n\
322 del-ps PS delete PS and all of its ports\n\
323 list-ps print the names of all the physical switches\n\
324 ps-exists PS exit 2 if PS does not exist\n\
327 list-ports PS print the names of all the ports on PS\n\
328 add-port PS PORT add network device PORT to PS\n\
329 del-port PS PORT delete PORT from PS\n\
331 Logical Switch commands:\n\
332 add-ls LS create a new logical switch named LS\n\
333 del-ls LS delete LS and all of its ports\n\
334 list-ls print the names of all the logical switches\n\
335 ls-exists LS exit 2 if LS does not exist\n\
336 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
337 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
338 list-bindings PS PORT list bindings for PORT on PS\n\
340 Logical Router commands:\n\
341 add-lr LR create a new logical router named LR\n\
342 del-lr LR delete LR\n\
343 list-lr print the names of all the logical routers\n\
344 lr-exists LR exit 2 if LR does not exist\n\
346 MAC binding commands:\n\
347 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
348 del-ucast-local LS MAC del ucast local entry from LS\n\
349 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
350 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
351 clear-local-macs LS clear local mac entries\n\
352 list-local-macs LS list local mac entries\n\
353 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
354 del-ucast-remote LS MAC del ucast remote entry from LS\n\
355 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
356 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
357 clear-remote-macs LS clear remote mac entries\n\
358 list-remote-macs LS list remote mac entries\n\
363 --db=DATABASE connect to DATABASE\n\
365 -t, --timeout=SECS wait at most SECS seconds\n\
366 --dry-run do not commit changes to database\n\
367 --oneline print exactly one line of output per command\n",
368 program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
371 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
372 stream_usage("database", true, true, false);
375 -h, --help display this help message\n\
376 -V, --version display version information\n");
381 static struct cmd_show_table cmd_show_tables[] = {
382 {&vteprec_table_global,
384 {&vteprec_global_col_managers,
385 &vteprec_global_col_switches,
390 {&vteprec_table_manager,
391 &vteprec_manager_col_target,
392 {&vteprec_manager_col_is_connected,
398 {&vteprec_table_physical_switch,
399 &vteprec_physical_switch_col_name,
400 {&vteprec_physical_switch_col_management_ips,
401 &vteprec_physical_switch_col_tunnel_ips,
402 &vteprec_physical_switch_col_ports},
406 {&vteprec_table_physical_port,
407 &vteprec_physical_port_col_name,
408 {&vteprec_physical_port_col_vlan_bindings,
414 {&vteprec_table_logical_switch,
415 &vteprec_logical_switch_col_name,
422 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
425 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
426 struct vtep_ctl_context {
427 struct ctl_context base;
429 /* Modifiable state. */
430 const struct vteprec_global *vtep_global;
433 /* A cache of the contents of the database.
435 * A command that needs to use any of this information must first
436 * call vtep_ctl_context_populate_cache(). A command that changes
437 * anything that could invalidate the cache must either call
438 * vtep_ctl_context_invalidate_cache() or manually update the cache
439 * to maintain its correctness. */
441 struct shash pswitches; /* Maps from physical switch name to
442 * struct vtep_ctl_pswitch. */
443 struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
445 struct shash lswitches; /* Maps from logical switch name to
446 * struct vtep_ctl_lswitch. */
447 struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
448 * struct vteprec_physical_locator. */
449 struct shash lrouters; /* Maps from logical router name to
450 * struct vtep_ctl_lrouter. */
453 /* Casts 'base' into 'struct vtep_ctl_context'. */
454 static struct vtep_ctl_context *
455 vtep_ctl_context_cast(struct ctl_context *base)
457 return CONTAINER_OF(base, struct vtep_ctl_context, base);
460 struct vtep_ctl_pswitch {
461 const struct vteprec_physical_switch *ps_cfg;
463 struct ovs_list ports; /* Contains "struct vteprec_physical_port"s. */
466 struct vtep_ctl_port {
467 struct ovs_list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
468 const struct vteprec_physical_port *port_cfg;
469 struct vtep_ctl_pswitch *ps;
470 struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
473 struct vtep_ctl_lswitch {
474 const struct vteprec_logical_switch *ls_cfg;
476 struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
477 struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
478 struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
479 struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
482 struct vtep_ctl_lrouter {
483 const struct vteprec_logical_router *lr_cfg;
487 struct vtep_ctl_mcast_mac {
488 const struct vteprec_mcast_macs_local *local_cfg;
489 const struct vteprec_mcast_macs_remote *remote_cfg;
491 const struct vteprec_physical_locator_set *ploc_set_cfg;
492 struct ovs_list locators; /* Contains 'vtep_ctl_ploc's. */
495 struct vtep_ctl_ploc {
496 struct ovs_list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
498 const struct vteprec_physical_locator *ploc_cfg;
502 verify_ports(struct vtep_ctl_context *vtepctl_ctx)
504 if (!vtepctl_ctx->verified_ports) {
505 const struct vteprec_physical_switch *ps;
507 vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
508 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, vtepctl_ctx->base.idl) {
509 vteprec_physical_switch_verify_ports(ps);
512 vtepctl_ctx->verified_ports = true;
516 static struct vtep_ctl_port *
517 add_port_to_cache(struct vtep_ctl_context *vtepctl_ctx,
518 struct vtep_ctl_pswitch *ps,
519 struct vteprec_physical_port *port_cfg)
521 char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
522 struct vtep_ctl_port *port;
524 port = xmalloc(sizeof *port);
525 list_push_back(&ps->ports, &port->ports_node);
526 port->port_cfg = port_cfg;
528 shash_add(&vtepctl_ctx->ports, cache_name, port);
530 shash_init(&port->bindings);
536 del_cached_port(struct vtep_ctl_context *vtepctl_ctx,
537 struct vtep_ctl_port *port)
539 char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
541 list_remove(&port->ports_node);
542 shash_find_and_delete(&vtepctl_ctx->ports, cache_name);
543 vteprec_physical_port_delete(port->port_cfg);
549 add_pswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
550 struct vteprec_physical_switch *ps_cfg)
552 struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
554 ps->name = xstrdup(ps_cfg->name);
555 list_init(&ps->ports);
556 shash_add(&vtepctl_ctx->pswitches, ps->name, ps);
560 vtep_delete_pswitch(const struct vteprec_global *vtep_global,
561 const struct vteprec_physical_switch *ps)
563 struct vteprec_physical_switch **pswitches;
566 pswitches = xmalloc(sizeof *vtep_global->switches
567 * vtep_global->n_switches);
568 for (i = n = 0; i < vtep_global->n_switches; i++) {
569 if (vtep_global->switches[i] != ps) {
570 pswitches[n++] = vtep_global->switches[i];
573 vteprec_global_set_switches(vtep_global, pswitches, n);
578 del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
580 ovs_assert(list_is_empty(&ps->ports));
582 vteprec_physical_switch_delete(ps->ps_cfg);
583 vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
585 shash_find_and_delete(&ctx->pswitches, ps->name);
590 static struct vtep_ctl_lswitch *
591 add_lswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
592 const struct vteprec_logical_switch *ls_cfg)
594 struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
596 ls->name = xstrdup(ls_cfg->name);
597 shash_add(&vtepctl_ctx->lswitches, ls->name, ls);
598 shash_init(&ls->ucast_local);
599 shash_init(&ls->ucast_remote);
600 shash_init(&ls->mcast_local);
601 shash_init(&ls->mcast_remote);
606 del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
609 vteprec_logical_switch_delete(ls->ls_cfg);
611 shash_find_and_delete(&ctx->lswitches, ls->name);
617 commit_ls_bindings(struct vtep_ctl_port *port)
619 struct vteprec_logical_switch **binding_values;
620 int64_t *binding_keys;
622 struct shash_node *node;
625 n_bindings = shash_count(&port->bindings);
626 binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
627 binding_values = xmalloc(n_bindings * sizeof *binding_values);
630 SHASH_FOR_EACH(node, &port->bindings) {
631 struct vtep_ctl_lswitch *ls_entry = node->data;
633 binding_keys[i] = strtoll(node->name, NULL, 0);
634 binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
638 vteprec_physical_port_set_vlan_bindings(port->port_cfg,
639 binding_keys, binding_values,
641 free(binding_values);
646 add_ls_binding_to_cache(struct vtep_ctl_port *port,
648 struct vtep_ctl_lswitch *ls)
650 if (shash_find(&port->bindings, vlan)) {
651 ctl_fatal("multiple bindings for vlan %s", vlan);
654 shash_add(&port->bindings, vlan, ls);
658 del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
660 if (!shash_find(&port->bindings, vlan)) {
661 ctl_fatal("no binding for vlan %s", vlan);
664 shash_find_and_delete(&port->bindings, vlan);
667 static struct vtep_ctl_lrouter *
668 add_lrouter_to_cache(struct vtep_ctl_context *vtepctl_ctx,
669 const struct vteprec_logical_router *lr_cfg)
671 struct vtep_ctl_lrouter *lr = xmalloc(sizeof *lr);
673 lr->name = xstrdup(lr_cfg->name);
674 shash_add(&vtepctl_ctx->lrouters, lr->name, lr);
679 del_cached_lrouter(struct vtep_ctl_context *ctx, struct vtep_ctl_lrouter *lr)
682 vteprec_logical_router_delete(lr->lr_cfg);
684 shash_find_and_delete(&ctx->lrouters, lr->name);
689 static struct vteprec_physical_locator *
690 find_ploc(struct vtep_ctl_context *vtepctl_ctx, const char *encap,
693 struct vteprec_physical_locator *ploc;
694 char *name = xasprintf("%s+%s", encap, dst_ip);
696 ovs_assert(vtepctl_ctx->cache_valid);
698 ploc = shash_find_data(&vtepctl_ctx->plocs, name);
705 add_ploc_to_cache(struct vtep_ctl_context *vtepctl_ctx,
706 struct vteprec_physical_locator *ploc)
708 char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
709 struct vteprec_physical_locator *orig_ploc;
711 orig_ploc = find_ploc(vtepctl_ctx, ploc->encapsulation_type, ploc->dst_ip);
713 shash_add(&vtepctl_ctx->plocs, name, ploc);
720 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
721 struct vteprec_physical_locator *ploc_cfg)
723 struct vtep_ctl_ploc *ploc;
725 ploc = xmalloc(sizeof *ploc);
726 ploc->ploc_cfg = ploc_cfg;
727 list_push_back(&mcast_mac->locators, &ploc->locators_node);
731 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
732 struct vteprec_physical_locator *ploc_cfg)
734 struct vtep_ctl_ploc *ploc;
736 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
737 if (ploc->ploc_cfg == ploc_cfg) {
738 list_remove(&ploc->locators_node);
745 static struct vtep_ctl_mcast_mac *
746 add_mcast_mac_to_cache(struct vtep_ctl_context *vtepctl_ctx,
747 struct vtep_ctl_lswitch *ls, const char *mac,
748 struct vteprec_physical_locator_set *ploc_set_cfg,
751 struct vtep_ctl_mcast_mac *mcast_mac;
752 struct shash *mcast_shash;
755 mcast_mac = xmalloc(sizeof *mcast_mac);
756 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
758 mcast_mac->ploc_set_cfg = ploc_set_cfg;
759 list_init(&mcast_mac->locators);
760 shash_add(mcast_shash, mac, mcast_mac);
762 for (i = 0; i < ploc_set_cfg->n_locators; i++) {
763 struct vteprec_physical_locator *ploc_cfg;
765 ploc_cfg = ploc_set_cfg->locators[i];
766 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
767 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
774 vtep_ctl_context_invalidate_cache(struct ctl_context *ctx)
776 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
777 struct shash_node *node;
779 if (!vtepctl_ctx->cache_valid) {
782 vtepctl_ctx->cache_valid = false;
784 SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
785 struct vtep_ctl_pswitch *ps = node->data;
789 shash_destroy(&vtepctl_ctx->pswitches);
791 SHASH_FOR_EACH (node, &vtepctl_ctx->ports) {
792 struct vtep_ctl_port *port = node->data;
793 shash_destroy(&port->bindings);
795 shash_destroy_free_data(&vtepctl_ctx->ports);
797 SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
798 struct vtep_ctl_lswitch *ls = node->data;
799 struct shash_node *node2, *next_node2;
801 shash_destroy(&ls->ucast_local);
802 shash_destroy(&ls->ucast_remote);
804 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
805 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
806 struct vtep_ctl_ploc *ploc, *next_ploc;
808 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
809 &mcast_mac->locators) {
814 shash_destroy(&ls->mcast_local);
816 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
817 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
818 struct vtep_ctl_ploc *ploc, *next_ploc;
820 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
821 &mcast_mac->locators) {
826 shash_destroy(&ls->mcast_remote);
831 shash_destroy(&vtepctl_ctx->lswitches);
832 shash_destroy(&vtepctl_ctx->plocs);
834 SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
835 struct vtep_ctl_lrouter *lr = node->data;
839 shash_destroy(&vtepctl_ctx->lrouters);
843 pre_get_info(struct ctl_context *ctx)
845 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
847 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
848 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
849 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_tunnels);
851 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
852 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
854 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
856 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_router_col_name);
858 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
859 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
860 ovsdb_idl_add_column(ctx->idl,
861 &vteprec_ucast_macs_local_col_logical_switch);
863 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
864 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
865 ovsdb_idl_add_column(ctx->idl,
866 &vteprec_ucast_macs_remote_col_logical_switch);
868 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
869 ovsdb_idl_add_column(ctx->idl,
870 &vteprec_mcast_macs_local_col_locator_set);
871 ovsdb_idl_add_column(ctx->idl,
872 &vteprec_mcast_macs_local_col_logical_switch);
874 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
875 ovsdb_idl_add_column(ctx->idl,
876 &vteprec_mcast_macs_remote_col_locator_set);
877 ovsdb_idl_add_column(ctx->idl,
878 &vteprec_mcast_macs_remote_col_logical_switch);
880 ovsdb_idl_add_column(ctx->idl,
881 &vteprec_physical_locator_set_col_locators);
883 ovsdb_idl_add_column(ctx->idl,
884 &vteprec_physical_locator_col_dst_ip);
885 ovsdb_idl_add_column(ctx->idl,
886 &vteprec_physical_locator_col_encapsulation_type);
888 ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_local);
889 ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_remote);
893 vtep_ctl_context_populate_cache(struct ctl_context *ctx)
895 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
896 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
897 const struct vteprec_logical_switch *ls_cfg;
898 const struct vteprec_logical_router *lr_cfg;
899 const struct vteprec_ucast_macs_local *ucast_local_cfg;
900 const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
901 const struct vteprec_mcast_macs_local *mcast_local_cfg;
902 const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
903 const struct vteprec_tunnel *tunnel_cfg;
904 struct sset pswitches, ports, lswitches;
905 struct sset lrouters;
908 if (vtepctl_ctx->cache_valid) {
909 /* Cache is already populated. */
912 vtepctl_ctx->cache_valid = true;
913 shash_init(&vtepctl_ctx->pswitches);
914 shash_init(&vtepctl_ctx->ports);
915 shash_init(&vtepctl_ctx->lswitches);
916 shash_init(&vtepctl_ctx->plocs);
917 shash_init(&vtepctl_ctx->lrouters);
919 sset_init(&pswitches);
921 for (i = 0; i < vtep_global->n_switches; i++) {
922 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
925 if (!sset_add(&pswitches, ps_cfg->name)) {
926 VLOG_WARN("%s: database contains duplicate physical switch name",
930 add_pswitch_to_cache(vtepctl_ctx, ps_cfg);
932 for (j = 0; j < ps_cfg->n_ports; j++) {
933 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
935 if (!sset_add(&ports, port_cfg->name)) {
936 /* Duplicate port name. (We will warn about that later.) */
941 sset_destroy(&pswitches);
942 sset_destroy(&ports);
944 sset_init(&lswitches);
945 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
946 if (!sset_add(&lswitches, ls_cfg->name)) {
947 VLOG_WARN("%s: database contains duplicate logical switch name",
951 add_lswitch_to_cache(vtepctl_ctx, ls_cfg);
953 sset_destroy(&lswitches);
955 sset_init(&lrouters);
956 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg, ctx->idl) {
957 if (!sset_add(&lrouters, lr_cfg->name)) {
958 VLOG_WARN("%s: database contains duplicate logical router name",
962 add_lrouter_to_cache(vtepctl_ctx, lr_cfg);
964 sset_destroy(&lrouters);
966 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
967 struct vtep_ctl_lswitch *ls;
969 if (!ucast_local_cfg->logical_switch) {
972 ls = find_lswitch(vtepctl_ctx, ucast_local_cfg->logical_switch->name,
978 if (ucast_local_cfg->locator) {
979 add_ploc_to_cache(vtepctl_ctx, ucast_local_cfg->locator);
982 shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
985 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
986 struct vtep_ctl_lswitch *ls;
988 if (!ucast_remote_cfg->logical_switch) {
991 ls = find_lswitch(vtepctl_ctx, ucast_remote_cfg->logical_switch->name,
997 if (ucast_remote_cfg->locator) {
998 add_ploc_to_cache(vtepctl_ctx, ucast_remote_cfg->locator);
1001 shash_add(&ls->ucast_remote, ucast_remote_cfg->MAC, ucast_remote_cfg);
1004 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg, ctx->idl) {
1005 struct vtep_ctl_mcast_mac *mcast_mac;
1006 struct vtep_ctl_lswitch *ls;
1008 if (!mcast_local_cfg->logical_switch) {
1011 ls = find_lswitch(vtepctl_ctx, mcast_local_cfg->logical_switch->name,
1017 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mcast_local_cfg->MAC,
1018 mcast_local_cfg->locator_set,
1020 mcast_mac->local_cfg = mcast_local_cfg;
1023 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg, ctx->idl) {
1024 struct vtep_ctl_mcast_mac *mcast_mac;
1025 struct vtep_ctl_lswitch *ls;
1027 if (!mcast_remote_cfg->logical_switch) {
1030 ls = find_lswitch(vtepctl_ctx, mcast_remote_cfg->logical_switch->name,
1036 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mcast_remote_cfg->MAC,
1037 mcast_remote_cfg->locator_set,
1039 mcast_mac->remote_cfg = mcast_remote_cfg;
1042 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg, ctx->idl) {
1043 if (tunnel_cfg->local) {
1044 add_ploc_to_cache(vtepctl_ctx, tunnel_cfg->local);
1046 if (tunnel_cfg->remote) {
1047 add_ploc_to_cache(vtepctl_ctx, tunnel_cfg->remote);
1051 sset_init(&pswitches);
1052 for (i = 0; i < vtep_global->n_switches; i++) {
1053 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1054 struct vtep_ctl_pswitch *ps;
1057 if (!sset_add(&pswitches, ps_cfg->name)) {
1060 ps = shash_find_data(&vtepctl_ctx->pswitches, ps_cfg->name);
1061 for (j = 0; j < ps_cfg->n_ports; j++) {
1062 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1063 struct vtep_ctl_port *port;
1066 port = shash_find_data(&vtepctl_ctx->ports, port_cfg->name);
1068 if (port_cfg == port->port_cfg) {
1069 VLOG_WARN("%s: port is in multiple physical switches "
1071 port_cfg->name, ps->name, port->ps->name);
1073 /* Log as an error because this violates the database's
1074 * uniqueness constraints, so the database server shouldn't
1075 * have allowed it. */
1076 VLOG_ERR("%s: database contains duplicate port name",
1082 port = add_port_to_cache(vtepctl_ctx, ps, port_cfg);
1084 for (k = 0; k < port_cfg->n_vlan_bindings; k++) {
1085 struct vteprec_logical_switch *ls_cfg;
1086 struct vtep_ctl_lswitch *ls;
1089 vlan = xasprintf("%"PRId64, port_cfg->key_vlan_bindings[k]);
1090 if (shash_find(&port->bindings, vlan)) {
1091 ctl_fatal("multiple bindings for vlan %s", vlan);
1094 ls_cfg = port_cfg->value_vlan_bindings[k];
1095 ls = find_lswitch(vtepctl_ctx, ls_cfg->name, true);
1097 shash_add_nocopy(&port->bindings, vlan, ls);
1101 sset_destroy(&pswitches);
1104 static struct vtep_ctl_pswitch *
1105 find_pswitch(struct vtep_ctl_context *vtepctl_ctx, const char *name, bool must_exist)
1107 struct vtep_ctl_pswitch *ps;
1109 ovs_assert(vtepctl_ctx->cache_valid);
1111 ps = shash_find_data(&vtepctl_ctx->pswitches, name);
1112 if (must_exist && !ps) {
1113 ctl_fatal("no physical switch named %s", name);
1115 vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
1119 static struct vtep_ctl_port *
1120 find_port(struct vtep_ctl_context *vtepctl_ctx, const char *ps_name,
1121 const char *port_name, bool must_exist)
1123 char *cache_name = xasprintf("%s+%s", ps_name, port_name);
1124 struct vtep_ctl_port *port;
1126 ovs_assert(vtepctl_ctx->cache_valid);
1128 port = shash_find_data(&vtepctl_ctx->ports, cache_name);
1129 if (port && !strcmp(port_name, port->ps->name)) {
1133 if (must_exist && !port) {
1134 ctl_fatal("no port named %s", port_name);
1136 verify_ports(vtepctl_ctx);
1141 pswitch_insert_port(const struct vteprec_physical_switch *ps,
1142 struct vteprec_physical_port *port)
1144 struct vteprec_physical_port **ports;
1147 ports = xmalloc(sizeof *ps->ports * (ps->n_ports + 1));
1148 for (i = 0; i < ps->n_ports; i++) {
1149 ports[i] = ps->ports[i];
1151 ports[ps->n_ports] = port;
1152 vteprec_physical_switch_set_ports(ps, ports, ps->n_ports + 1);
1157 pswitch_delete_port(const struct vteprec_physical_switch *ps,
1158 const struct vteprec_physical_port *port)
1160 struct vteprec_physical_port **ports;
1163 ports = xmalloc(sizeof *ps->ports * ps->n_ports);
1164 for (i = n = 0; i < ps->n_ports; i++) {
1165 if (ps->ports[i] != port) {
1166 ports[n++] = ps->ports[i];
1169 vteprec_physical_switch_set_ports(ps, ports, n);
1174 vtep_insert_pswitch(const struct vteprec_global *vtep_global,
1175 struct vteprec_physical_switch *ps)
1177 struct vteprec_physical_switch **pswitches;
1180 pswitches = xmalloc(sizeof *vtep_global->switches
1181 * (vtep_global->n_switches + 1));
1182 for (i = 0; i < vtep_global->n_switches; i++) {
1183 pswitches[i] = vtep_global->switches[i];
1185 pswitches[vtep_global->n_switches] = ps;
1186 vteprec_global_set_switches(vtep_global, pswitches,
1187 vtep_global->n_switches + 1);
1192 cmd_add_ps(struct ctl_context *ctx)
1194 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1195 const char *ps_name = ctx->argv[1];
1196 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1197 struct vteprec_physical_switch *ps;
1199 vtep_ctl_context_populate_cache(ctx);
1200 if (find_pswitch(vtepctl_ctx, ps_name, false)) {
1202 ctl_fatal("cannot create physical switch %s because it "
1203 "already exists", ps_name);
1208 ps = vteprec_physical_switch_insert(ctx->txn);
1209 vteprec_physical_switch_set_name(ps, ps_name);
1211 vtep_insert_pswitch(vtepctl_ctx->vtep_global, ps);
1213 vtep_ctl_context_invalidate_cache(ctx);
1217 del_port(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_port *port)
1219 pswitch_delete_port(port->ps->ps_cfg, port->port_cfg);
1220 del_cached_port(vtepctl_ctx, port);
1224 del_pswitch(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_pswitch *ps)
1226 struct vtep_ctl_port *port, *next_port;
1228 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &ps->ports) {
1229 del_port(vtepctl_ctx, port);
1232 del_cached_pswitch(vtepctl_ctx, ps);
1236 cmd_del_ps(struct ctl_context *ctx)
1238 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1239 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1240 struct vtep_ctl_pswitch *ps;
1242 vtep_ctl_context_populate_cache(ctx);
1243 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], must_exist);
1245 del_pswitch(vtepctl_ctx, ps);
1250 output_sorted(struct svec *svec, struct ds *output)
1256 SVEC_FOR_EACH (i, name, svec) {
1257 ds_put_format(output, "%s\n", name);
1262 cmd_list_ps(struct ctl_context *ctx)
1264 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1265 struct shash_node *node;
1266 struct svec pswitches;
1268 vtep_ctl_context_populate_cache(ctx);
1270 svec_init(&pswitches);
1271 SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
1272 struct vtep_ctl_pswitch *ps = node->data;
1274 svec_add(&pswitches, ps->name);
1276 output_sorted(&pswitches, &ctx->output);
1277 svec_destroy(&pswitches);
1281 cmd_ps_exists(struct ctl_context *ctx)
1283 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1285 vtep_ctl_context_populate_cache(ctx);
1286 if (!find_pswitch(vtepctl_ctx, ctx->argv[1], false)) {
1292 cmd_list_ports(struct ctl_context *ctx)
1294 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1295 struct vtep_ctl_pswitch *ps;
1296 struct vtep_ctl_port *port;
1299 vtep_ctl_context_populate_cache(ctx);
1300 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], true);
1301 vteprec_physical_switch_verify_ports(ps->ps_cfg);
1304 LIST_FOR_EACH (port, ports_node, &ps->ports) {
1305 if (strcmp(port->port_cfg->name, ps->name)) {
1306 svec_add(&ports, port->port_cfg->name);
1309 output_sorted(&ports, &ctx->output);
1310 svec_destroy(&ports);
1314 add_port(struct ctl_context *ctx, const char *ps_name,
1315 const char *port_name, bool may_exist)
1317 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1318 struct vtep_ctl_port *vtep_ctl_port;
1319 struct vtep_ctl_pswitch *ps;
1320 struct vteprec_physical_port *port;
1322 vtep_ctl_context_populate_cache(ctx);
1324 vtep_ctl_port = find_port(vtepctl_ctx, ps_name, port_name, false);
1325 if (vtep_ctl_port) {
1327 ctl_fatal("cannot create a port named %s on %s because a "
1328 "port with that name already exists",
1329 port_name, ps_name);
1334 ps = find_pswitch(vtepctl_ctx, ps_name, true);
1336 port = vteprec_physical_port_insert(ctx->txn);
1337 vteprec_physical_port_set_name(port, port_name);
1339 pswitch_insert_port(ps->ps_cfg, port);
1341 add_port_to_cache(vtepctl_ctx, ps, port);
1345 cmd_add_port(struct ctl_context *ctx)
1347 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1349 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist);
1353 cmd_del_port(struct ctl_context *ctx)
1355 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1356 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1357 struct vtep_ctl_port *port;
1359 vtep_ctl_context_populate_cache(ctx);
1361 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], must_exist);
1363 if (ctx->argc == 3) {
1364 struct vtep_ctl_pswitch *ps;
1366 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], true);
1367 if (port->ps != ps) {
1368 ctl_fatal("physical switch %s does not have a port %s",
1369 ctx->argv[1], ctx->argv[2]);
1373 del_port(vtepctl_ctx, port);
1377 static struct vtep_ctl_lswitch *
1378 find_lswitch(struct vtep_ctl_context *vtepctl_ctx,
1379 const char *name, bool must_exist)
1381 struct vtep_ctl_lswitch *ls;
1383 ovs_assert(vtepctl_ctx->cache_valid);
1385 ls = shash_find_data(&vtepctl_ctx->lswitches, name);
1386 if (must_exist && !ls) {
1387 ctl_fatal("no logical switch named %s", name);
1393 cmd_add_ls(struct ctl_context *ctx)
1395 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1396 const char *ls_name = ctx->argv[1];
1397 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1398 struct vteprec_logical_switch *ls;
1400 vtep_ctl_context_populate_cache(ctx);
1401 if (find_lswitch(vtepctl_ctx, ls_name, false)) {
1403 ctl_fatal("cannot create logical switch %s because it "
1404 "already exists", ls_name);
1409 ls = vteprec_logical_switch_insert(ctx->txn);
1410 vteprec_logical_switch_set_name(ls, ls_name);
1412 vtep_ctl_context_invalidate_cache(ctx);
1416 del_lswitch(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_lswitch *ls)
1418 del_cached_lswitch(vtepctl_ctx, ls);
1422 cmd_del_ls(struct ctl_context *ctx)
1424 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1425 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1426 struct vtep_ctl_lswitch *ls;
1428 vtep_ctl_context_populate_cache(ctx);
1429 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], must_exist);
1431 del_lswitch(vtepctl_ctx, ls);
1436 cmd_list_ls(struct ctl_context *ctx)
1438 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1439 struct shash_node *node;
1440 struct svec lswitches;
1442 vtep_ctl_context_populate_cache(ctx);
1444 svec_init(&lswitches);
1445 SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
1446 struct vtep_ctl_lswitch *ls = node->data;
1448 svec_add(&lswitches, ls->name);
1450 output_sorted(&lswitches, &ctx->output);
1451 svec_destroy(&lswitches);
1455 cmd_ls_exists(struct ctl_context *ctx)
1457 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1459 vtep_ctl_context_populate_cache(ctx);
1460 if (!find_lswitch(vtepctl_ctx, ctx->argv[1], false)) {
1466 cmd_list_bindings(struct ctl_context *ctx)
1468 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1469 const struct shash_node *node;
1470 struct vtep_ctl_port *port;
1471 struct svec bindings;
1473 vtep_ctl_context_populate_cache(ctx);
1474 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1476 svec_init(&bindings);
1477 SHASH_FOR_EACH (node, &port->bindings) {
1478 struct vtep_ctl_lswitch *lswitch = node->data;
1481 binding = xasprintf("%04lld %s", strtoll(node->name, NULL, 0),
1483 svec_add_nocopy(&bindings, binding);
1485 output_sorted(&bindings, &ctx->output);
1486 svec_destroy(&bindings);
1490 cmd_bind_ls(struct ctl_context *ctx)
1492 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1493 struct vtep_ctl_lswitch *ls;
1494 struct vtep_ctl_port *port;
1497 vtep_ctl_context_populate_cache(ctx);
1499 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1500 vlan = ctx->argv[3];
1501 ls = find_lswitch(vtepctl_ctx, ctx->argv[4], true);
1503 add_ls_binding_to_cache(port, vlan, ls);
1504 commit_ls_bindings(port);
1506 vtep_ctl_context_invalidate_cache(ctx);
1510 cmd_unbind_ls(struct ctl_context *ctx)
1512 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1513 struct vtep_ctl_port *port;
1516 vtep_ctl_context_populate_cache(ctx);
1518 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1519 vlan = ctx->argv[3];
1521 del_cached_ls_binding(port, vlan);
1522 commit_ls_bindings(port);
1524 vtep_ctl_context_invalidate_cache(ctx);
1527 static struct vtep_ctl_lrouter *
1528 find_lrouter(struct vtep_ctl_context *vtepctl_ctx,
1529 const char *name, bool must_exist)
1531 struct vtep_ctl_lrouter *lr;
1533 ovs_assert(vtepctl_ctx->cache_valid);
1535 lr = shash_find_data(&vtepctl_ctx->lrouters, name);
1536 if (must_exist && !lr) {
1537 ctl_fatal("no logical router named %s", name);
1543 cmd_add_lr(struct ctl_context *ctx)
1545 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1546 const char *lr_name = ctx->argv[1];
1547 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1548 struct vteprec_logical_router *lr;
1550 vtep_ctl_context_populate_cache(ctx);
1551 if (find_lrouter(vtepctl_ctx, lr_name, false)) {
1553 ctl_fatal("cannot create logical router %s because it "
1554 "already exists", lr_name);
1559 lr = vteprec_logical_router_insert(ctx->txn);
1560 vteprec_logical_router_set_name(lr, lr_name);
1562 vtep_ctl_context_invalidate_cache(ctx);
1566 del_lrouter(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_lrouter *lr)
1568 del_cached_lrouter(vtepctl_ctx, lr);
1572 cmd_del_lr(struct ctl_context *ctx)
1574 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1575 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1576 struct vtep_ctl_lrouter *lr;
1578 vtep_ctl_context_populate_cache(ctx);
1579 lr = find_lrouter(vtepctl_ctx, ctx->argv[1], must_exist);
1581 del_lrouter(vtepctl_ctx, lr);
1586 cmd_list_lr(struct ctl_context *ctx)
1588 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1589 struct shash_node *node;
1590 struct svec lrouters;
1592 vtep_ctl_context_populate_cache(ctx);
1594 svec_init(&lrouters);
1595 SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
1596 struct vtep_ctl_lrouter *lr = node->data;
1598 svec_add(&lrouters, lr->name);
1600 output_sorted(&lrouters, &ctx->output);
1601 svec_destroy(&lrouters);
1605 cmd_lr_exists(struct ctl_context *ctx)
1607 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1609 vtep_ctl_context_populate_cache(ctx);
1610 if (!find_lrouter(vtepctl_ctx, ctx->argv[1], false)) {
1616 add_ucast_entry(struct ctl_context *ctx, bool local)
1618 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1619 struct vtep_ctl_lswitch *ls;
1623 struct vteprec_physical_locator *ploc_cfg;
1625 vtep_ctl_context_populate_cache(ctx);
1627 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1630 if (ctx->argc == 4) {
1631 encap = "vxlan_over_ipv4";
1632 dst_ip = ctx->argv[3];
1634 encap = ctx->argv[3];
1635 dst_ip = ctx->argv[4];
1638 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1640 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1641 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1642 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1644 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
1648 struct vteprec_ucast_macs_local *ucast_cfg;
1650 ucast_cfg = shash_find_data(&ls->ucast_local, mac);
1652 ucast_cfg = vteprec_ucast_macs_local_insert(ctx->txn);
1653 vteprec_ucast_macs_local_set_MAC(ucast_cfg, mac);
1654 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg, ls->ls_cfg);
1655 shash_add(&ls->ucast_local, mac, ucast_cfg);
1657 vteprec_ucast_macs_local_set_locator(ucast_cfg, ploc_cfg);
1659 struct vteprec_ucast_macs_remote *ucast_cfg;
1661 ucast_cfg = shash_find_data(&ls->ucast_remote, mac);
1663 ucast_cfg = vteprec_ucast_macs_remote_insert(ctx->txn);
1664 vteprec_ucast_macs_remote_set_MAC(ucast_cfg, mac);
1665 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg, ls->ls_cfg);
1666 shash_add(&ls->ucast_remote, mac, ucast_cfg);
1668 vteprec_ucast_macs_remote_set_locator(ucast_cfg, ploc_cfg);
1671 vtep_ctl_context_invalidate_cache(ctx);
1675 cmd_add_ucast_local(struct ctl_context *ctx)
1677 add_ucast_entry(ctx, true);
1681 cmd_add_ucast_remote(struct ctl_context *ctx)
1683 add_ucast_entry(ctx, false);
1687 del_ucast_entry(struct ctl_context *ctx, bool local)
1689 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1690 struct vtep_ctl_lswitch *ls;
1691 struct shash *ucast_shash;
1692 struct shash_node *node;
1694 vtep_ctl_context_populate_cache(ctx);
1696 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1697 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1699 node = shash_find(ucast_shash, ctx->argv[2]);
1705 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1706 vteprec_ucast_macs_local_delete(ucast_cfg);
1708 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1709 vteprec_ucast_macs_remote_delete(ucast_cfg);
1711 shash_delete(ucast_shash, node);
1713 vtep_ctl_context_invalidate_cache(ctx);
1717 cmd_del_ucast_local(struct ctl_context *ctx)
1719 del_ucast_entry(ctx, true);
1723 cmd_del_ucast_remote(struct ctl_context *ctx)
1725 del_ucast_entry(ctx, false);
1729 commit_mcast_entries(struct vtep_ctl_mcast_mac *mcast_mac)
1731 struct vtep_ctl_ploc *ploc;
1732 struct vteprec_physical_locator **locators = NULL;
1736 n_locators = list_size(&mcast_mac->locators);
1737 ovs_assert(n_locators);
1739 locators = xmalloc(n_locators * sizeof *locators);
1742 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
1743 locators[i] = (struct vteprec_physical_locator *)ploc->ploc_cfg;
1747 vteprec_physical_locator_set_set_locators(mcast_mac->ploc_set_cfg,
1755 add_mcast_entry(struct ctl_context *ctx,
1756 struct vtep_ctl_lswitch *ls, const char *mac,
1757 const char *encap, const char *dst_ip, bool local)
1759 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1760 struct shash *mcast_shash;
1761 struct vtep_ctl_mcast_mac *mcast_mac;
1762 struct vteprec_physical_locator *ploc_cfg;
1763 struct vteprec_physical_locator_set *ploc_set_cfg;
1765 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1767 /* Physical locator sets are immutable, so allocate a new one. */
1768 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1770 mcast_mac = shash_find_data(mcast_shash, mac);
1772 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mac, ploc_set_cfg,
1776 mcast_mac->local_cfg = vteprec_mcast_macs_local_insert(ctx->txn);
1777 vteprec_mcast_macs_local_set_MAC(mcast_mac->local_cfg, mac);
1778 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1780 vteprec_mcast_macs_local_set_logical_switch(mcast_mac->local_cfg,
1782 mcast_mac->remote_cfg = NULL;
1784 mcast_mac->remote_cfg = vteprec_mcast_macs_remote_insert(ctx->txn);
1785 vteprec_mcast_macs_remote_set_MAC(mcast_mac->remote_cfg, mac);
1786 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1788 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac->remote_cfg,
1790 mcast_mac->local_cfg = NULL;
1793 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1795 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1798 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1803 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1805 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1806 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1807 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1809 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
1812 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1813 commit_mcast_entries(mcast_mac);
1817 del_mcast_entry(struct ctl_context *ctx,
1818 struct vtep_ctl_lswitch *ls, const char *mac,
1819 const char *encap, const char *dst_ip, bool local)
1821 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1822 struct vtep_ctl_mcast_mac *mcast_mac;
1823 struct shash *mcast_shash;
1824 struct vteprec_physical_locator *ploc_cfg;
1825 struct vteprec_physical_locator_set *ploc_set_cfg;
1827 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1829 mcast_mac = shash_find_data(mcast_shash, mac);
1834 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1836 /* Couldn't find the physical locator, so just ignore. */
1840 /* Physical locator sets are immutable, so allocate a new one. */
1841 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1842 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1844 del_ploc_from_mcast_mac(mcast_mac, ploc_cfg);
1845 if (list_is_empty(&mcast_mac->locators)) {
1846 struct shash_node *node = shash_find(mcast_shash, mac);
1848 vteprec_physical_locator_set_delete(ploc_set_cfg);
1851 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1853 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1857 shash_delete(mcast_shash, node);
1860 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1863 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1866 commit_mcast_entries(mcast_mac);
1871 add_del_mcast_entry(struct ctl_context *ctx, bool add, bool local)
1873 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1874 struct vtep_ctl_lswitch *ls;
1879 vtep_ctl_context_populate_cache(ctx);
1881 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1884 if (ctx->argc == 4) {
1885 encap = "vxlan_over_ipv4";
1886 dst_ip = ctx->argv[3];
1888 encap = ctx->argv[3];
1889 dst_ip = ctx->argv[4];
1893 add_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1895 del_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1898 vtep_ctl_context_invalidate_cache(ctx);
1902 cmd_add_mcast_local(struct ctl_context *ctx)
1904 add_del_mcast_entry(ctx, true, true);
1908 cmd_add_mcast_remote(struct ctl_context *ctx)
1910 add_del_mcast_entry(ctx, true, false);
1914 cmd_del_mcast_local(struct ctl_context *ctx)
1916 add_del_mcast_entry(ctx, false, true);
1920 cmd_del_mcast_remote(struct ctl_context *ctx)
1922 add_del_mcast_entry(ctx, false, false);
1926 clear_macs(struct ctl_context *ctx, bool local)
1928 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1929 struct vtep_ctl_lswitch *ls;
1930 const struct shash_node *node;
1931 struct shash *ucast_shash;
1932 struct shash *mcast_shash;
1934 vtep_ctl_context_populate_cache(ctx);
1935 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1937 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1938 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1940 SHASH_FOR_EACH (node, ucast_shash) {
1942 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1943 vteprec_ucast_macs_local_delete(ucast_cfg);
1945 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1946 vteprec_ucast_macs_remote_delete(ucast_cfg);
1950 SHASH_FOR_EACH (node, mcast_shash) {
1951 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
1953 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1955 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1959 vtep_ctl_context_invalidate_cache(ctx);
1963 cmd_clear_local_macs(struct ctl_context *ctx)
1965 clear_macs(ctx, true);
1969 cmd_clear_remote_macs(struct ctl_context *ctx)
1971 clear_macs(ctx, false);
1975 list_macs(struct ctl_context *ctx, bool local)
1977 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1978 struct vtep_ctl_lswitch *ls;
1979 const struct shash_node *node;
1980 struct shash *ucast_shash;
1981 struct svec ucast_macs;
1982 struct shash *mcast_shash;
1983 struct svec mcast_macs;
1985 vtep_ctl_context_populate_cache(ctx);
1986 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1988 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1989 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1991 svec_init(&ucast_macs);
1992 SHASH_FOR_EACH (node, ucast_shash) {
1993 struct vteprec_ucast_macs_local *ucast_local = node->data;
1994 struct vteprec_ucast_macs_remote *ucast_remote = node->data;
1995 struct vteprec_physical_locator *ploc_cfg;
1998 ploc_cfg = local ? ucast_local->locator : ucast_remote->locator;
2000 entry = xasprintf(" %s -> %s/%s", node->name,
2001 ploc_cfg->encapsulation_type, ploc_cfg->dst_ip);
2002 svec_add_nocopy(&ucast_macs, entry);
2004 ds_put_format(&ctx->output, "ucast-mac-%s\n", local ? "local" : "remote");
2005 output_sorted(&ucast_macs, &ctx->output);
2006 ds_put_char(&ctx->output, '\n');
2007 svec_destroy(&ucast_macs);
2009 svec_init(&mcast_macs);
2010 SHASH_FOR_EACH (node, mcast_shash) {
2011 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2012 struct vtep_ctl_ploc *ploc;
2015 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
2016 entry = xasprintf(" %s -> %s/%s", node->name,
2017 ploc->ploc_cfg->encapsulation_type,
2018 ploc->ploc_cfg->dst_ip);
2019 svec_add_nocopy(&mcast_macs, entry);
2022 ds_put_format(&ctx->output, "mcast-mac-%s\n", local ? "local" : "remote");
2023 output_sorted(&mcast_macs, &ctx->output);
2024 ds_put_char(&ctx->output, '\n');
2025 svec_destroy(&mcast_macs);
2029 cmd_list_local_macs(struct ctl_context *ctx)
2031 list_macs(ctx, true);
2035 cmd_list_remote_macs(struct ctl_context *ctx)
2037 list_macs(ctx, false);
2041 verify_managers(const struct vteprec_global *vtep_global)
2045 vteprec_global_verify_managers(vtep_global);
2047 for (i = 0; i < vtep_global->n_managers; ++i) {
2048 const struct vteprec_manager *mgr = vtep_global->managers[i];
2050 vteprec_manager_verify_target(mgr);
2055 pre_manager(struct ctl_context *ctx)
2057 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_managers);
2058 ovsdb_idl_add_column(ctx->idl, &vteprec_manager_col_target);
2062 cmd_get_manager(struct ctl_context *ctx)
2064 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2065 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2066 struct svec targets;
2069 verify_managers(vtep_global);
2071 /* Print the targets in sorted order for reproducibility. */
2072 svec_init(&targets);
2074 for (i = 0; i < vtep_global->n_managers; i++) {
2075 svec_add(&targets, vtep_global->managers[i]->target);
2078 svec_sort_unique(&targets);
2079 for (i = 0; i < targets.n; i++) {
2080 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2082 svec_destroy(&targets);
2086 delete_managers(const struct vtep_ctl_context *vtepctl_ctx)
2088 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2091 /* Delete Manager rows pointed to by 'managers' column. */
2092 for (i = 0; i < vtep_global->n_managers; i++) {
2093 vteprec_manager_delete(vtep_global->managers[i]);
2096 /* Delete 'Manager' row refs in 'managers' column. */
2097 vteprec_global_set_managers(vtep_global, NULL, 0);
2101 cmd_del_manager(struct ctl_context *ctx)
2103 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2104 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2106 verify_managers(vtep_global);
2107 delete_managers(vtepctl_ctx);
2111 insert_managers(struct vtep_ctl_context *vtepctl_ctx, char *targets[], size_t n)
2113 struct vteprec_manager **managers;
2116 /* Insert each manager in a new row in Manager table. */
2117 managers = xmalloc(n * sizeof *managers);
2118 for (i = 0; i < n; i++) {
2119 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2120 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2122 managers[i] = vteprec_manager_insert(vtepctl_ctx->base.txn);
2123 vteprec_manager_set_target(managers[i], targets[i]);
2126 /* Store uuids of new Manager rows in 'managers' column. */
2127 vteprec_global_set_managers(vtepctl_ctx->vtep_global, managers, n);
2132 cmd_set_manager(struct ctl_context *ctx)
2134 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2135 const size_t n = ctx->argc - 1;
2137 verify_managers(vtepctl_ctx->vtep_global);
2138 delete_managers(vtepctl_ctx);
2139 insert_managers(vtepctl_ctx, &ctx->argv[1], n);
2142 /* Parameter commands. */
2143 static const struct ctl_table_class tables[] = {
2144 {&vteprec_table_global,
2145 {{&vteprec_table_global, NULL, NULL},
2146 {NULL, NULL, NULL}}},
2148 {&vteprec_table_logical_binding_stats,
2149 {{NULL, NULL, NULL},
2150 {NULL, NULL, NULL}}},
2152 {&vteprec_table_logical_switch,
2153 {{&vteprec_table_logical_switch, &vteprec_logical_switch_col_name, NULL},
2154 {NULL, NULL, NULL}}},
2156 {&vteprec_table_ucast_macs_local,
2157 {{NULL, NULL, NULL},
2158 {NULL, NULL, NULL}}},
2160 {&vteprec_table_ucast_macs_remote,
2161 {{NULL, NULL, NULL},
2162 {NULL, NULL, NULL}}},
2164 {&vteprec_table_mcast_macs_local,
2165 {{NULL, NULL, NULL},
2166 {NULL, NULL, NULL}}},
2168 {&vteprec_table_mcast_macs_remote,
2169 {{NULL, NULL, NULL},
2170 {NULL, NULL, NULL}}},
2172 {&vteprec_table_manager,
2173 {{&vteprec_table_manager, &vteprec_manager_col_target, NULL},
2174 {NULL, NULL, NULL}}},
2176 {&vteprec_table_physical_locator,
2177 {{NULL, NULL, NULL},
2178 {NULL, NULL, NULL}}},
2180 {&vteprec_table_physical_locator_set,
2181 {{NULL, NULL, NULL},
2182 {NULL, NULL, NULL}}},
2184 {&vteprec_table_physical_port,
2185 {{&vteprec_table_physical_port, &vteprec_physical_port_col_name, NULL},
2186 {NULL, NULL, NULL}}},
2188 {&vteprec_table_physical_switch,
2189 {{&vteprec_table_physical_switch, &vteprec_physical_switch_col_name, NULL},
2190 {NULL, NULL, NULL}}},
2192 {&vteprec_table_tunnel,
2193 {{NULL, NULL, NULL},
2194 {NULL, NULL, NULL}}},
2196 {&vteprec_table_logical_router,
2197 {{&vteprec_table_logical_router, &vteprec_logical_router_col_name, NULL},
2198 {NULL, NULL, NULL}}},
2200 {&vteprec_table_arp_sources_local,
2201 {{NULL, NULL, NULL},
2202 {NULL, NULL, NULL}}},
2204 {&vteprec_table_arp_sources_remote,
2205 {{NULL, NULL, NULL},
2206 {NULL, NULL, NULL}}},
2208 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2213 vtep_ctl_context_init_command(struct vtep_ctl_context *vtepctl_ctx,
2214 struct ctl_command *command)
2216 ctl_context_init_command(&vtepctl_ctx->base, command);
2217 vtepctl_ctx->verified_ports = false;
2222 vtep_ctl_context_init(struct vtep_ctl_context *vtepctl_ctx,
2223 struct ctl_command *command,
2224 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2225 const struct vteprec_global *vtep_global,
2226 struct ovsdb_symbol_table *symtab)
2228 ctl_context_init(&vtepctl_ctx->base, command, idl, txn, symtab,
2229 vtep_ctl_context_invalidate_cache);
2231 vtepctl_ctx->verified_ports = false;
2233 vtepctl_ctx->vtep_global = vtep_global;
2234 vtepctl_ctx->cache_valid = false;
2238 vtep_ctl_context_done_command(struct vtep_ctl_context *vtepctl_ctx,
2239 struct ctl_command *command)
2241 ctl_context_done_command(&vtepctl_ctx->base, command);
2245 vtep_ctl_context_done(struct vtep_ctl_context *vtepctl_ctx,
2246 struct ctl_command *command)
2248 ctl_context_done(&vtepctl_ctx->base, command);
2252 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2253 struct ovsdb_idl *idl)
2255 struct ctl_command *c;
2257 ovsdb_idl_add_table(idl, &vteprec_table_global);
2258 for (c = commands; c < &commands[n_commands]; c++) {
2259 if (c->syntax->prerequisites) {
2260 struct vtep_ctl_context vtepctl_ctx;
2262 ds_init(&c->output);
2265 vtep_ctl_context_init(&vtepctl_ctx, c, idl, NULL, NULL, NULL);
2266 (c->syntax->prerequisites)(&vtepctl_ctx.base);
2267 vtep_ctl_context_done(&vtepctl_ctx, c);
2269 ovs_assert(!c->output.string);
2270 ovs_assert(!c->table);
2276 do_vtep_ctl(const char *args, struct ctl_command *commands,
2277 size_t n_commands, struct ovsdb_idl *idl)
2279 struct ovsdb_idl_txn *txn;
2280 const struct vteprec_global *vtep_global;
2281 enum ovsdb_idl_txn_status status;
2282 struct ovsdb_symbol_table *symtab;
2283 struct vtep_ctl_context vtepctl_ctx;
2284 struct ctl_command *c;
2285 struct shash_node *node;
2288 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2290 ovsdb_idl_txn_set_dry_run(txn);
2293 ovsdb_idl_txn_add_comment(txn, "vtep-ctl: %s", args);
2295 vtep_global = vteprec_global_first(idl);
2297 /* XXX add verification that table is empty */
2298 vtep_global = vteprec_global_insert(txn);
2301 symtab = ovsdb_symbol_table_create();
2302 for (c = commands; c < &commands[n_commands]; c++) {
2303 ds_init(&c->output);
2306 vtep_ctl_context_init(&vtepctl_ctx, NULL, idl, txn, vtep_global, symtab);
2307 for (c = commands; c < &commands[n_commands]; c++) {
2308 vtep_ctl_context_init_command(&vtepctl_ctx, c);
2309 if (c->syntax->run) {
2310 (c->syntax->run)(&vtepctl_ctx.base);
2312 vtep_ctl_context_done_command(&vtepctl_ctx, c);
2314 if (vtepctl_ctx.base.try_again) {
2315 vtep_ctl_context_done(&vtepctl_ctx, NULL);
2319 vtep_ctl_context_done(&vtepctl_ctx, NULL);
2321 SHASH_FOR_EACH (node, &symtab->sh) {
2322 struct ovsdb_symbol *symbol = node->data;
2323 if (!symbol->created) {
2324 ctl_fatal("row id \"%s\" is referenced but never created "
2325 "(e.g. with \"-- --id=%s create ...\")",
2326 node->name, node->name);
2328 if (!symbol->strong_ref) {
2329 if (!symbol->weak_ref) {
2330 VLOG_WARN("row id \"%s\" was created but no reference to it "
2331 "was inserted, so it will not actually appear in "
2332 "the database", node->name);
2334 VLOG_WARN("row id \"%s\" was created but only a weak "
2335 "reference to it was inserted, so it will not "
2336 "actually appear in the database", node->name);
2341 status = ovsdb_idl_txn_commit_block(txn);
2342 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2343 for (c = commands; c < &commands[n_commands]; c++) {
2344 if (c->syntax->postprocess) {
2345 vtep_ctl_context_init(&vtepctl_ctx, c, idl, txn, vtep_global, symtab);
2346 (c->syntax->postprocess)(&vtepctl_ctx.base);
2347 vtep_ctl_context_done(&vtepctl_ctx, c);
2351 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2352 ovsdb_idl_txn_destroy(txn);
2353 txn = the_idl_txn = NULL;
2356 case TXN_UNCOMMITTED:
2357 case TXN_INCOMPLETE:
2361 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2362 ctl_fatal("transaction aborted");
2372 ctl_fatal("transaction error: %s", error);
2374 case TXN_NOT_LOCKED:
2375 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2376 ctl_fatal("database not locked");
2383 ovsdb_symbol_table_destroy(symtab);
2385 for (c = commands; c < &commands[n_commands]; c++) {
2386 struct ds *ds = &c->output;
2389 table_print(c->table, &table_style);
2390 } else if (oneline) {
2394 for (j = 0; j < ds->length; j++) {
2395 int ch = ds->string[j];
2398 fputs("\\n", stdout);
2402 fputs("\\\\", stdout);
2411 fputs(ds_cstr(ds), stdout);
2413 ds_destroy(&c->output);
2414 table_destroy(c->table);
2417 shash_destroy_free_data(&c->options);
2421 ovsdb_idl_destroy(idl);
2426 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2427 * resources and return so that the caller can try again. */
2429 ovsdb_idl_txn_abort(txn);
2430 ovsdb_idl_txn_destroy(txn);
2432 ovsdb_symbol_table_destroy(symtab);
2433 for (c = commands; c < &commands[n_commands]; c++) {
2434 ds_destroy(&c->output);
2435 table_destroy(c->table);
2441 static const struct ctl_command_syntax vtep_commands[] = {
2442 /* Physical Switch commands. */
2443 {"add-ps", 1, 1, NULL, pre_get_info, cmd_add_ps, NULL, "--may-exist", RW},
2444 {"del-ps", 1, 1, NULL, pre_get_info, cmd_del_ps, NULL, "--if-exists", RW},
2445 {"list-ps", 0, 0, NULL, pre_get_info, cmd_list_ps, NULL, "", RO},
2446 {"ps-exists", 1, 1, NULL, pre_get_info, cmd_ps_exists, NULL, "", RO},
2448 /* Port commands. */
2449 {"list-ports", 1, 1, NULL, pre_get_info, cmd_list_ports, NULL, "", RO},
2450 {"add-port", 2, 2, NULL, pre_get_info, cmd_add_port, NULL, "--may-exist",
2452 {"del-port", 2, 2, NULL, pre_get_info, cmd_del_port, NULL, "--if-exists",
2455 /* Logical Switch commands. */
2456 {"add-ls", 1, 1, NULL, pre_get_info, cmd_add_ls, NULL, "--may-exist", RW},
2457 {"del-ls", 1, 1, NULL, pre_get_info, cmd_del_ls, NULL, "--if-exists", RW},
2458 {"list-ls", 0, 0, NULL, pre_get_info, cmd_list_ls, NULL, "", RO},
2459 {"ls-exists", 1, 1, NULL, pre_get_info, cmd_ls_exists, NULL, "", RO},
2460 {"list-bindings", 2, 2, NULL, pre_get_info, cmd_list_bindings, NULL, "", RO},
2461 {"bind-ls", 4, 4, NULL, pre_get_info, cmd_bind_ls, NULL, "", RO},
2462 {"unbind-ls", 3, 3, NULL, pre_get_info, cmd_unbind_ls, NULL, "", RO},
2464 /* Logical Router commands. */
2465 {"add-lr", 1, 1, NULL, pre_get_info, cmd_add_lr, NULL, "--may-exist", RW},
2466 {"del-lr", 1, 1, NULL, pre_get_info, cmd_del_lr, NULL, "--if-exists", RW},
2467 {"list-lr", 0, 0, NULL, pre_get_info, cmd_list_lr, NULL, "", RO},
2468 {"lr-exists", 1, 1, NULL, pre_get_info, cmd_lr_exists, NULL, "", RO},
2470 /* MAC binding commands. */
2471 {"add-ucast-local", 3, 4, NULL, pre_get_info, cmd_add_ucast_local, NULL,
2473 {"del-ucast-local", 2, 2, NULL, pre_get_info, cmd_del_ucast_local, NULL,
2475 {"add-mcast-local", 3, 4, NULL, pre_get_info, cmd_add_mcast_local, NULL,
2477 {"del-mcast-local", 3, 4, NULL, pre_get_info, cmd_del_mcast_local, NULL,
2479 {"clear-local-macs", 1, 1, NULL, pre_get_info, cmd_clear_local_macs, NULL,
2481 {"list-local-macs", 1, 1, NULL, pre_get_info, cmd_list_local_macs, NULL,
2483 {"add-ucast-remote", 3, 4, NULL, pre_get_info, cmd_add_ucast_remote, NULL,
2485 {"del-ucast-remote", 2, 2, NULL, pre_get_info, cmd_del_ucast_remote, NULL,
2487 {"add-mcast-remote", 3, 4, NULL, pre_get_info, cmd_add_mcast_remote, NULL,
2489 {"del-mcast-remote", 3, 4, NULL, pre_get_info, cmd_del_mcast_remote, NULL,
2491 {"clear-remote-macs", 1, 1, NULL, pre_get_info, cmd_clear_remote_macs, NULL,
2493 {"list-remote-macs", 1, 1, NULL, pre_get_info, cmd_list_remote_macs, NULL,
2496 /* Manager commands. */
2497 {"get-manager", 0, 0, NULL, pre_manager, cmd_get_manager, NULL, "", RO},
2498 {"del-manager", 0, 0, NULL, pre_manager, cmd_del_manager, NULL, "", RW},
2499 {"set-manager", 1, INT_MAX, NULL, pre_manager, cmd_set_manager, NULL, "",
2502 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2505 /* Registers vsctl and common db commands. */
2507 vtep_ctl_cmd_init(void)
2509 ctl_init(tables, cmd_show_tables, vtep_ctl_exit);
2510 ctl_register_commands(vtep_commands);