2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015, 2016, 2017 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"
37 #include "openvswitch/json.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");
111 /* Log our arguments. This is often valuable for debugging systems. */
112 args
= process_escape_args(argv
);
113 VLOG(ctl_might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
115 /* Parse command line. */
116 shash_init(&local_options
);
117 parse_options(argc
, argv
, &local_options
);
118 commands
= ctl_parse_commands(argc
- optind
, argv
+ optind
, &local_options
,
125 /* Initialize IDL. */
126 idl
= the_idl
= ovsdb_idl_create(db
, &vteprec_idl_class
, false, false);
127 run_prerequisites(commands
, n_commands
, idl
);
129 /* Execute the commands.
131 * 'seqno' is the database sequence number for which we last tried to
132 * execute our transaction. There's no point in trying to commit more than
133 * once for any given sequence number, because if the transaction fails
134 * it's because the database changed and we need to obtain an up-to-date
135 * view of the database before we try the transaction again. */
136 seqno
= ovsdb_idl_get_seqno(idl
);
139 if (!ovsdb_idl_is_alive(idl
)) {
140 int retval
= ovsdb_idl_get_last_error(idl
);
141 ctl_fatal("%s: database connection failed (%s)",
142 db
, ovs_retval_to_string(retval
));
145 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
146 seqno
= ovsdb_idl_get_seqno(idl
);
147 do_vtep_ctl(args
, commands
, n_commands
, idl
);
150 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
158 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
161 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
);
210 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
225 vlog_set_levels(&this_module
, VLF_SYSLOG
, VLL_WARN
);
233 if (shash_find(local_options
, options
[idx
].name
)) {
234 ctl_fatal("'%s' option specified multiple times",
237 shash_add_nocopy(local_options
,
238 xasprintf("--%s", options
[idx
].name
),
239 nullable_xstrdup(optarg
));
246 ovs_print_version(0, 0);
247 printf("DB Schema %s\n", vteprec_get_db_version());
251 timeout
= strtoul(optarg
, NULL
, 10);
253 ctl_fatal("value %s on -t or --timeout is invalid",
259 TABLE_OPTION_HANDLERS(&table_style
)
261 STREAM_SSL_OPTION_HANDLERS
263 case OPT_PEER_CA_CERT
:
264 stream_ssl_set_peer_ca_cert_file(optarg
);
277 db
= ctl_default_db();
280 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
281 free(CONST_CAST(char *, options
[i
].name
));
286 /* Frees the current transaction and the underlying IDL and then calls
289 * Freeing the transaction and the IDL is not strictly necessary, but it makes
290 * for a clean memory leak report from valgrind in the normal case. That makes
291 * it easier to notice real memory leaks. */
293 vtep_ctl_exit(int status
)
296 ovsdb_idl_txn_abort(the_idl_txn
);
297 ovsdb_idl_txn_destroy(the_idl_txn
);
299 ovsdb_idl_destroy(the_idl
);
307 %s: VTEP configuration utility\n\
308 usage: %s [OPTIONS] COMMAND [ARG...]\n\
311 show print overview of database contents\n\
314 get-manager print the managers\n\
315 del-manager delete the managers\n\
316 set-manager TARGET... set the list of managers to TARGET...\n\
318 Physical Switch commands:\n\
319 add-ps PS create a new physical switch named PS\n\
320 del-ps PS delete PS and all of its ports\n\
321 list-ps print the names of all the physical switches\n\
322 ps-exists PS exit 2 if PS does not exist\n\
325 list-ports PS print the names of all the ports on PS\n\
326 add-port PS PORT add network device PORT to PS\n\
327 del-port PS PORT delete PORT from PS\n\
329 Logical Switch commands:\n\
330 add-ls LS create a new logical switch named LS\n\
331 del-ls LS delete LS and all of its ports\n\
332 list-ls print the names of all the logical switches\n\
333 ls-exists LS exit 2 if LS does not exist\n\
334 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
335 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
336 list-bindings PS PORT list bindings for PORT on PS\n\
337 set-replication-mode LS MODE set replication mode on LS\n\
338 get-replication-mode LS get replication mode on LS\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());
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
);
545 shash_destroy(&port
->bindings
);
551 add_pswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
552 struct vteprec_physical_switch
*ps_cfg
)
554 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
556 ps
->name
= xstrdup(ps_cfg
->name
);
557 ovs_list_init(&ps
->ports
);
558 shash_add(&vtepctl_ctx
->pswitches
, ps
->name
, ps
);
562 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
563 const struct vteprec_physical_switch
*ps
)
565 struct vteprec_physical_switch
**pswitches
;
568 pswitches
= xmalloc(sizeof *vtep_global
->switches
569 * vtep_global
->n_switches
);
570 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
571 if (vtep_global
->switches
[i
] != ps
) {
572 pswitches
[n
++] = vtep_global
->switches
[i
];
575 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
580 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
582 ovs_assert(ovs_list_is_empty(&ps
->ports
));
584 vteprec_physical_switch_delete(ps
->ps_cfg
);
585 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
587 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
592 static struct vtep_ctl_lswitch
*
593 add_lswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
594 const struct vteprec_logical_switch
*ls_cfg
)
596 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
598 ls
->name
= xstrdup(ls_cfg
->name
);
599 shash_add(&vtepctl_ctx
->lswitches
, ls
->name
, ls
);
600 shash_init(&ls
->ucast_local
);
601 shash_init(&ls
->ucast_remote
);
602 shash_init(&ls
->mcast_local
);
603 shash_init(&ls
->mcast_remote
);
608 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
611 vteprec_logical_switch_delete(ls
->ls_cfg
);
613 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
619 commit_ls_bindings(struct vtep_ctl_port
*port
)
621 struct vteprec_logical_switch
**binding_values
;
622 int64_t *binding_keys
;
624 struct shash_node
*node
;
627 n_bindings
= shash_count(&port
->bindings
);
628 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
629 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
632 SHASH_FOR_EACH(node
, &port
->bindings
) {
633 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
635 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
636 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
640 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
641 binding_keys
, binding_values
,
643 free(binding_values
);
648 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
650 struct vtep_ctl_lswitch
*ls
)
652 if (shash_find(&port
->bindings
, vlan
)) {
653 ctl_fatal("multiple bindings for vlan %s", vlan
);
656 shash_add(&port
->bindings
, vlan
, ls
);
660 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
662 if (!shash_find(&port
->bindings
, vlan
)) {
663 ctl_fatal("no binding for vlan %s", vlan
);
666 shash_find_and_delete(&port
->bindings
, vlan
);
669 static struct vtep_ctl_lrouter
*
670 add_lrouter_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
671 const struct vteprec_logical_router
*lr_cfg
)
673 struct vtep_ctl_lrouter
*lr
= xmalloc(sizeof *lr
);
675 lr
->name
= xstrdup(lr_cfg
->name
);
676 shash_add(&vtepctl_ctx
->lrouters
, lr
->name
, lr
);
681 del_cached_lrouter(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lrouter
*lr
)
684 vteprec_logical_router_delete(lr
->lr_cfg
);
686 shash_find_and_delete(&ctx
->lrouters
, lr
->name
);
691 static struct vteprec_physical_locator
*
692 find_ploc(struct vtep_ctl_context
*vtepctl_ctx
, const char *encap
,
695 struct vteprec_physical_locator
*ploc
;
696 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
698 ovs_assert(vtepctl_ctx
->cache_valid
);
700 ploc
= shash_find_data(&vtepctl_ctx
->plocs
, name
);
707 add_ploc_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
708 struct vteprec_physical_locator
*ploc
)
710 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
711 struct vteprec_physical_locator
*orig_ploc
;
713 orig_ploc
= find_ploc(vtepctl_ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
715 shash_add(&vtepctl_ctx
->plocs
, name
, ploc
);
722 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
723 struct vteprec_physical_locator
*ploc_cfg
)
725 struct vtep_ctl_ploc
*ploc
;
727 ploc
= xmalloc(sizeof *ploc
);
728 ploc
->ploc_cfg
= ploc_cfg
;
729 ovs_list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
733 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
734 struct vteprec_physical_locator
*ploc_cfg
)
736 struct vtep_ctl_ploc
*ploc
;
738 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
739 if (ploc
->ploc_cfg
== ploc_cfg
) {
740 ovs_list_remove(&ploc
->locators_node
);
747 static struct vtep_ctl_mcast_mac
*
748 add_mcast_mac_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
749 struct vtep_ctl_lswitch
*ls
, const char *mac
,
750 struct vteprec_physical_locator_set
*ploc_set_cfg
,
753 struct vtep_ctl_mcast_mac
*mcast_mac
;
754 struct shash
*mcast_shash
;
757 mcast_mac
= xmalloc(sizeof *mcast_mac
);
758 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
760 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
761 ovs_list_init(&mcast_mac
->locators
);
762 shash_add(mcast_shash
, mac
, mcast_mac
);
764 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
765 struct vteprec_physical_locator
*ploc_cfg
;
767 ploc_cfg
= ploc_set_cfg
->locators
[i
];
768 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
769 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
776 vtep_ctl_context_invalidate_cache(struct ctl_context
*ctx
)
778 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
779 struct shash_node
*node
;
781 if (!vtepctl_ctx
->cache_valid
) {
784 vtepctl_ctx
->cache_valid
= false;
786 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
787 struct vtep_ctl_pswitch
*ps
= node
->data
;
791 shash_destroy(&vtepctl_ctx
->pswitches
);
793 SHASH_FOR_EACH (node
, &vtepctl_ctx
->ports
) {
794 struct vtep_ctl_port
*port
= node
->data
;
795 shash_destroy(&port
->bindings
);
797 shash_destroy_free_data(&vtepctl_ctx
->ports
);
799 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
800 struct vtep_ctl_lswitch
*ls
= node
->data
;
801 struct shash_node
*node2
, *next_node2
;
803 shash_destroy(&ls
->ucast_local
);
804 shash_destroy(&ls
->ucast_remote
);
806 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
807 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
808 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
810 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
811 &mcast_mac
->locators
) {
816 shash_destroy(&ls
->mcast_local
);
818 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
819 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
820 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
822 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
823 &mcast_mac
->locators
) {
828 shash_destroy(&ls
->mcast_remote
);
833 shash_destroy(&vtepctl_ctx
->lswitches
);
834 shash_destroy(&vtepctl_ctx
->plocs
);
836 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
837 struct vtep_ctl_lrouter
*lr
= node
->data
;
841 shash_destroy(&vtepctl_ctx
->lrouters
);
845 pre_get_info(struct ctl_context
*ctx
)
847 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
849 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
850 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
851 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_tunnels
);
853 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
854 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
856 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
857 ovsdb_idl_add_column(ctx
->idl
,
858 &vteprec_logical_switch_col_replication_mode
);
860 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_router_col_name
);
862 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
863 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
864 ovsdb_idl_add_column(ctx
->idl
,
865 &vteprec_ucast_macs_local_col_logical_switch
);
867 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
868 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
869 ovsdb_idl_add_column(ctx
->idl
,
870 &vteprec_ucast_macs_remote_col_logical_switch
);
872 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
873 ovsdb_idl_add_column(ctx
->idl
,
874 &vteprec_mcast_macs_local_col_locator_set
);
875 ovsdb_idl_add_column(ctx
->idl
,
876 &vteprec_mcast_macs_local_col_logical_switch
);
878 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
879 ovsdb_idl_add_column(ctx
->idl
,
880 &vteprec_mcast_macs_remote_col_locator_set
);
881 ovsdb_idl_add_column(ctx
->idl
,
882 &vteprec_mcast_macs_remote_col_logical_switch
);
884 ovsdb_idl_add_column(ctx
->idl
,
885 &vteprec_physical_locator_set_col_locators
);
887 ovsdb_idl_add_column(ctx
->idl
,
888 &vteprec_physical_locator_col_dst_ip
);
889 ovsdb_idl_add_column(ctx
->idl
,
890 &vteprec_physical_locator_col_encapsulation_type
);
892 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_local
);
893 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_remote
);
897 vtep_ctl_context_populate_cache(struct ctl_context
*ctx
)
899 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
900 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
901 const struct vteprec_logical_switch
*ls_cfg
;
902 const struct vteprec_logical_router
*lr_cfg
;
903 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
904 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
905 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
906 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
907 const struct vteprec_tunnel
*tunnel_cfg
;
908 struct sset pswitches
, ports
, lswitches
;
909 struct sset lrouters
;
912 if (vtepctl_ctx
->cache_valid
) {
913 /* Cache is already populated. */
916 vtepctl_ctx
->cache_valid
= true;
917 shash_init(&vtepctl_ctx
->pswitches
);
918 shash_init(&vtepctl_ctx
->ports
);
919 shash_init(&vtepctl_ctx
->lswitches
);
920 shash_init(&vtepctl_ctx
->plocs
);
921 shash_init(&vtepctl_ctx
->lrouters
);
923 sset_init(&pswitches
);
925 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
926 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
929 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
930 VLOG_WARN("%s: database contains duplicate physical switch name",
934 add_pswitch_to_cache(vtepctl_ctx
, ps_cfg
);
936 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
937 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
939 if (!sset_add(&ports
, port_cfg
->name
)) {
940 /* Duplicate port name. (We will warn about that later.) */
945 sset_destroy(&pswitches
);
946 sset_destroy(&ports
);
948 sset_init(&lswitches
);
949 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
950 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
951 VLOG_WARN("%s: database contains duplicate logical switch name",
955 add_lswitch_to_cache(vtepctl_ctx
, ls_cfg
);
957 sset_destroy(&lswitches
);
959 sset_init(&lrouters
);
960 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg
, ctx
->idl
) {
961 if (!sset_add(&lrouters
, lr_cfg
->name
)) {
962 VLOG_WARN("%s: database contains duplicate logical router name",
966 add_lrouter_to_cache(vtepctl_ctx
, lr_cfg
);
968 sset_destroy(&lrouters
);
970 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
971 struct vtep_ctl_lswitch
*ls
;
973 if (!ucast_local_cfg
->logical_switch
) {
976 ls
= find_lswitch(vtepctl_ctx
, ucast_local_cfg
->logical_switch
->name
,
982 if (ucast_local_cfg
->locator
) {
983 add_ploc_to_cache(vtepctl_ctx
, ucast_local_cfg
->locator
);
986 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
989 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
990 struct vtep_ctl_lswitch
*ls
;
992 if (!ucast_remote_cfg
->logical_switch
) {
995 ls
= find_lswitch(vtepctl_ctx
, ucast_remote_cfg
->logical_switch
->name
,
1001 if (ucast_remote_cfg
->locator
) {
1002 add_ploc_to_cache(vtepctl_ctx
, ucast_remote_cfg
->locator
);
1005 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
1008 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
1009 struct vtep_ctl_mcast_mac
*mcast_mac
;
1010 struct vtep_ctl_lswitch
*ls
;
1012 if (!mcast_local_cfg
->logical_switch
) {
1015 ls
= find_lswitch(vtepctl_ctx
, mcast_local_cfg
->logical_switch
->name
,
1021 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_local_cfg
->MAC
,
1022 mcast_local_cfg
->locator_set
,
1024 mcast_mac
->local_cfg
= mcast_local_cfg
;
1027 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
1028 struct vtep_ctl_mcast_mac
*mcast_mac
;
1029 struct vtep_ctl_lswitch
*ls
;
1031 if (!mcast_remote_cfg
->logical_switch
) {
1034 ls
= find_lswitch(vtepctl_ctx
, mcast_remote_cfg
->logical_switch
->name
,
1040 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_remote_cfg
->MAC
,
1041 mcast_remote_cfg
->locator_set
,
1043 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
1046 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg
, ctx
->idl
) {
1047 if (tunnel_cfg
->local
) {
1048 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->local
);
1050 if (tunnel_cfg
->remote
) {
1051 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->remote
);
1055 sset_init(&pswitches
);
1056 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1057 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1058 struct vtep_ctl_pswitch
*ps
;
1061 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1064 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, ps_cfg
->name
);
1065 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1066 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1067 struct vtep_ctl_port
*port
;
1070 port
= shash_find_data(&vtepctl_ctx
->ports
, port_cfg
->name
);
1072 if (port_cfg
== port
->port_cfg
) {
1073 VLOG_WARN("%s: port is in multiple physical switches "
1075 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1077 /* Log as an error because this violates the database's
1078 * uniqueness constraints, so the database server shouldn't
1079 * have allowed it. */
1080 VLOG_ERR("%s: database contains duplicate port name",
1086 port
= add_port_to_cache(vtepctl_ctx
, ps
, port_cfg
);
1088 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
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
[VTEPREC_N_TABLES
] = {
2180 [VTEPREC_TABLE_LOGICAL_SWITCH
].row_ids
[0]
2181 = {&vteprec_logical_switch_col_name
, NULL
, NULL
},
2183 [VTEPREC_TABLE_MANAGER
].row_ids
[0]
2184 = {&vteprec_manager_col_target
, NULL
, NULL
},
2186 [VTEPREC_TABLE_PHYSICAL_PORT
].row_ids
[0]
2187 = {&vteprec_physical_port_col_name
, NULL
, NULL
},
2189 [VTEPREC_TABLE_PHYSICAL_SWITCH
].row_ids
[0]
2190 = {&vteprec_physical_switch_col_name
, NULL
, NULL
},
2192 [VTEPREC_TABLE_LOGICAL_ROUTER
].row_ids
[0]
2193 = {&vteprec_logical_router_col_name
, NULL
, NULL
},
2198 vtep_ctl_context_init_command(struct vtep_ctl_context
*vtepctl_ctx
,
2199 struct ctl_command
*command
)
2201 ctl_context_init_command(&vtepctl_ctx
->base
, command
);
2202 vtepctl_ctx
->verified_ports
= false;
2207 vtep_ctl_context_init(struct vtep_ctl_context
*vtepctl_ctx
,
2208 struct ctl_command
*command
,
2209 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2210 const struct vteprec_global
*vtep_global
,
2211 struct ovsdb_symbol_table
*symtab
)
2213 ctl_context_init(&vtepctl_ctx
->base
, command
, idl
, txn
, symtab
,
2214 vtep_ctl_context_invalidate_cache
);
2216 vtepctl_ctx
->verified_ports
= false;
2218 vtepctl_ctx
->vtep_global
= vtep_global
;
2219 vtepctl_ctx
->cache_valid
= false;
2223 vtep_ctl_context_done_command(struct vtep_ctl_context
*vtepctl_ctx
,
2224 struct ctl_command
*command
)
2226 ctl_context_done_command(&vtepctl_ctx
->base
, command
);
2230 vtep_ctl_context_done(struct vtep_ctl_context
*vtepctl_ctx
,
2231 struct ctl_command
*command
)
2233 ctl_context_done(&vtepctl_ctx
->base
, command
);
2237 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2238 struct ovsdb_idl
*idl
)
2240 struct ctl_command
*c
;
2242 ovsdb_idl_add_table(idl
, &vteprec_table_global
);
2243 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2244 if (c
->syntax
->prerequisites
) {
2245 struct vtep_ctl_context vtepctl_ctx
;
2247 ds_init(&c
->output
);
2250 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2251 (c
->syntax
->prerequisites
)(&vtepctl_ctx
.base
);
2252 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2254 ovs_assert(!c
->output
.string
);
2255 ovs_assert(!c
->table
);
2261 do_vtep_ctl(const char *args
, struct ctl_command
*commands
,
2262 size_t n_commands
, struct ovsdb_idl
*idl
)
2264 struct ovsdb_idl_txn
*txn
;
2265 const struct vteprec_global
*vtep_global
;
2266 enum ovsdb_idl_txn_status status
;
2267 struct ovsdb_symbol_table
*symtab
;
2268 struct vtep_ctl_context vtepctl_ctx
;
2269 struct ctl_command
*c
;
2270 struct shash_node
*node
;
2273 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2275 ovsdb_idl_txn_set_dry_run(txn
);
2278 ovsdb_idl_txn_add_comment(txn
, "vtep-ctl: %s", args
);
2280 vtep_global
= vteprec_global_first(idl
);
2282 /* XXX add verification that table is empty */
2283 vtep_global
= vteprec_global_insert(txn
);
2286 symtab
= ovsdb_symbol_table_create();
2287 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2288 ds_init(&c
->output
);
2291 vtep_ctl_context_init(&vtepctl_ctx
, NULL
, idl
, txn
, vtep_global
, symtab
);
2292 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2293 vtep_ctl_context_init_command(&vtepctl_ctx
, c
);
2294 if (c
->syntax
->run
) {
2295 (c
->syntax
->run
)(&vtepctl_ctx
.base
);
2297 vtep_ctl_context_done_command(&vtepctl_ctx
, c
);
2299 if (vtepctl_ctx
.base
.try_again
) {
2300 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2304 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2306 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2307 struct ovsdb_symbol
*symbol
= node
->data
;
2308 if (!symbol
->created
) {
2309 ctl_fatal("row id \"%s\" is referenced but never created "
2310 "(e.g. with \"-- --id=%s create ...\")",
2311 node
->name
, node
->name
);
2313 if (!symbol
->strong_ref
) {
2314 if (!symbol
->weak_ref
) {
2315 VLOG_WARN("row id \"%s\" was created but no reference to it "
2316 "was inserted, so it will not actually appear in "
2317 "the database", node
->name
);
2319 VLOG_WARN("row id \"%s\" was created but only a weak "
2320 "reference to it was inserted, so it will not "
2321 "actually appear in the database", node
->name
);
2326 status
= ovsdb_idl_txn_commit_block(txn
);
2327 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2328 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2329 if (c
->syntax
->postprocess
) {
2330 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, txn
, vtep_global
, symtab
);
2331 (c
->syntax
->postprocess
)(&vtepctl_ctx
.base
);
2332 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2336 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2337 ovsdb_idl_txn_destroy(txn
);
2338 txn
= the_idl_txn
= NULL
;
2341 case TXN_UNCOMMITTED
:
2342 case TXN_INCOMPLETE
:
2346 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2347 ctl_fatal("transaction aborted");
2357 ctl_fatal("transaction error: %s", error
);
2359 case TXN_NOT_LOCKED
:
2360 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2361 ctl_fatal("database not locked");
2368 ovsdb_symbol_table_destroy(symtab
);
2370 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2371 struct ds
*ds
= &c
->output
;
2374 table_print(c
->table
, &table_style
);
2375 } else if (oneline
) {
2379 for (j
= 0; j
< ds
->length
; j
++) {
2380 int ch
= ds
->string
[j
];
2383 fputs("\\n", stdout
);
2387 fputs("\\\\", stdout
);
2396 fputs(ds_cstr(ds
), stdout
);
2398 ds_destroy(&c
->output
);
2399 table_destroy(c
->table
);
2402 shash_destroy_free_data(&c
->options
);
2406 ovsdb_idl_destroy(idl
);
2411 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2412 * resources and return so that the caller can try again. */
2414 ovsdb_idl_txn_abort(txn
);
2415 ovsdb_idl_txn_destroy(txn
);
2417 ovsdb_symbol_table_destroy(symtab
);
2418 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2419 ds_destroy(&c
->output
);
2420 table_destroy(c
->table
);
2426 static const struct ctl_command_syntax vtep_commands
[] = {
2427 /* Physical Switch commands. */
2428 {"add-ps", 1, 1, NULL
, pre_get_info
, cmd_add_ps
, NULL
, "--may-exist", RW
},
2429 {"del-ps", 1, 1, NULL
, pre_get_info
, cmd_del_ps
, NULL
, "--if-exists", RW
},
2430 {"list-ps", 0, 0, NULL
, pre_get_info
, cmd_list_ps
, NULL
, "", RO
},
2431 {"ps-exists", 1, 1, NULL
, pre_get_info
, cmd_ps_exists
, NULL
, "", RO
},
2433 /* Port commands. */
2434 {"list-ports", 1, 1, NULL
, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
2435 {"add-port", 2, 2, NULL
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
2437 {"del-port", 2, 2, NULL
, pre_get_info
, cmd_del_port
, NULL
, "--if-exists",
2440 /* Logical Switch commands. */
2441 {"add-ls", 1, 1, NULL
, pre_get_info
, cmd_add_ls
, NULL
, "--may-exist", RW
},
2442 {"del-ls", 1, 1, NULL
, pre_get_info
, cmd_del_ls
, NULL
, "--if-exists", RW
},
2443 {"list-ls", 0, 0, NULL
, pre_get_info
, cmd_list_ls
, NULL
, "", RO
},
2444 {"ls-exists", 1, 1, NULL
, pre_get_info
, cmd_ls_exists
, NULL
, "", RO
},
2445 {"list-bindings", 2, 2, NULL
, pre_get_info
, cmd_list_bindings
, NULL
, "", RO
},
2446 {"bind-ls", 4, 4, NULL
, pre_get_info
, cmd_bind_ls
, NULL
, "", RO
},
2447 {"unbind-ls", 3, 3, NULL
, pre_get_info
, cmd_unbind_ls
, NULL
, "", RO
},
2448 {"set-replication-mode", 2, 2, "LS MODE", pre_get_info
,
2449 cmd_set_replication_mode
, NULL
, "", RW
},
2450 {"get-replication-mode", 1, 1, "LS", pre_get_info
,
2451 cmd_get_replication_mode
, NULL
, "", RO
},
2453 /* Logical Router commands. */
2454 {"add-lr", 1, 1, NULL
, pre_get_info
, cmd_add_lr
, NULL
, "--may-exist", RW
},
2455 {"del-lr", 1, 1, NULL
, pre_get_info
, cmd_del_lr
, NULL
, "--if-exists", RW
},
2456 {"list-lr", 0, 0, NULL
, pre_get_info
, cmd_list_lr
, NULL
, "", RO
},
2457 {"lr-exists", 1, 1, NULL
, pre_get_info
, cmd_lr_exists
, NULL
, "", RO
},
2459 /* MAC binding commands. */
2460 {"add-ucast-local", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_local
, NULL
,
2462 {"del-ucast-local", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_local
, NULL
,
2464 {"add-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_local
, NULL
,
2466 {"del-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_local
, NULL
,
2468 {"clear-local-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_local_macs
, NULL
,
2470 {"list-local-macs", 1, 1, NULL
, pre_get_info
, cmd_list_local_macs
, NULL
,
2472 {"add-ucast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_remote
, NULL
,
2474 {"del-ucast-remote", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_remote
, NULL
,
2476 {"add-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_remote
, NULL
,
2478 {"del-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_remote
, NULL
,
2480 {"clear-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_remote_macs
, NULL
,
2482 {"list-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_list_remote_macs
, NULL
,
2485 /* Manager commands. */
2486 {"get-manager", 0, 0, NULL
, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2487 {"del-manager", 0, 0, NULL
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2488 {"set-manager", 1, INT_MAX
, NULL
, pre_manager
, cmd_set_manager
, NULL
, "",
2491 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2494 /* Registers vsctl and common db commands. */
2496 vtep_ctl_cmd_init(void)
2498 ctl_init(vteprec_table_classes
, tables
, cmd_show_tables
, vtep_ctl_exit
);
2499 ctl_register_commands(vtep_commands
);