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 "openvswitch/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 bool 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 if (do_vtep_ctl(args
, commands
, n_commands
, idl
)) {
153 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
161 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
164 OPT_DB
= UCHAR_MAX
+ 1,
174 static const struct option global_long_options
[] = {
175 {"db", required_argument
, NULL
, OPT_DB
},
176 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
177 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
178 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
179 {"timeout", required_argument
, NULL
, 't'},
180 {"help", no_argument
, NULL
, 'h'},
181 {"version", no_argument
, NULL
, 'V'},
184 STREAM_SSL_LONG_OPTIONS
,
185 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
188 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
189 char *tmp
, *short_options
;
191 struct option
*options
;
192 size_t allocated_options
;
196 tmp
= ovs_cmdl_long_options_to_short_options(global_long_options
);
197 short_options
= xasprintf("+%s", tmp
);
200 /* We want to parse both global and command-specific options here, but
201 * getopt_long() isn't too convenient for the job. We copy our global
202 * options into a dynamic array, then append all of the command-specific
204 options
= xmemdup(global_long_options
, sizeof global_long_options
);
205 allocated_options
= ARRAY_SIZE(global_long_options
);
206 n_options
= n_global_long_options
;
207 ctl_add_cmd_options(&options
, &n_options
, &allocated_options
, OPT_LOCAL
);
213 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
228 vlog_set_levels(&this_module
, VLF_SYSLOG
, VLL_WARN
);
236 if (shash_find(local_options
, options
[idx
].name
)) {
237 ctl_fatal("'%s' option specified multiple times",
240 shash_add_nocopy(local_options
,
241 xasprintf("--%s", options
[idx
].name
),
242 nullable_xstrdup(optarg
));
249 ovs_print_version(0, 0);
250 printf("DB Schema %s\n", vteprec_get_db_version());
254 timeout
= strtoul(optarg
, NULL
, 10);
256 ctl_fatal("value %s on -t or --timeout is invalid",
262 TABLE_OPTION_HANDLERS(&table_style
)
264 STREAM_SSL_OPTION_HANDLERS
266 case OPT_PEER_CA_CERT
:
267 stream_ssl_set_peer_ca_cert_file(optarg
);
280 db
= ctl_default_db();
283 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
284 free(CONST_CAST(char *, options
[i
].name
));
289 /* Frees the current transaction and the underlying IDL and then calls
292 * Freeing the transaction and the IDL is not strictly necessary, but it makes
293 * for a clean memory leak report from valgrind in the normal case. That makes
294 * it easier to notice real memory leaks. */
296 vtep_ctl_exit(int status
)
299 ovsdb_idl_txn_abort(the_idl_txn
);
300 ovsdb_idl_txn_destroy(the_idl_txn
);
302 ovsdb_idl_destroy(the_idl
);
310 %s: VTEP configuration utility\n\
311 usage: %s [OPTIONS] COMMAND [ARG...]\n\
314 show print overview of database contents\n\
317 get-manager print the managers\n\
318 del-manager delete the managers\n\
319 [--inactivity-probe=MSECS]\n\
320 set-manager TARGET... set the list of managers to TARGET...\n\
322 Physical Switch commands:\n\
323 add-ps PS create a new physical switch named PS\n\
324 del-ps PS delete PS and all of its ports\n\
325 list-ps print the names of all the physical switches\n\
326 ps-exists PS exit 2 if PS does not exist\n\
329 list-ports PS print the names of all the ports on PS\n\
330 add-port PS PORT add network device PORT to PS\n\
331 del-port PS PORT delete PORT from PS\n\
333 Logical Switch commands:\n\
334 add-ls LS create a new logical switch named LS\n\
335 del-ls LS delete LS and all of its ports\n\
336 list-ls print the names of all the logical switches\n\
337 ls-exists LS exit 2 if LS does not exist\n\
338 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
339 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
340 list-bindings PS PORT list bindings for PORT on PS\n\
341 set-replication-mode LS MODE set replication mode on LS\n\
342 get-replication-mode LS get replication mode on LS\n\
344 Logical Router commands:\n\
345 add-lr LR create a new logical router named LR\n\
346 del-lr LR delete LR\n\
347 list-lr print the names of all the logical routers\n\
348 lr-exists LR exit 2 if LR does not exist\n\
350 MAC binding commands:\n\
351 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
352 del-ucast-local LS MAC del ucast local entry from LS\n\
353 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
354 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
355 clear-local-macs LS clear local mac entries\n\
356 list-local-macs LS list local mac entries\n\
357 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
358 del-ucast-remote LS MAC del ucast remote entry from LS\n\
359 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
360 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
361 clear-remote-macs LS clear remote mac entries\n\
362 list-remote-macs LS list remote mac entries\n\
368 --db=DATABASE connect to DATABASE\n\
370 -t, --timeout=SECS wait at most SECS seconds\n\
371 --dry-run do not commit changes to database\n\
372 --oneline print exactly one line of output per command\n",
373 program_name
, program_name
, ctl_get_db_cmd_usage(),
374 ctl_list_db_tables_usage(), ctl_default_db());
378 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
379 stream_usage("database", true, true, false);
382 -h, --help display this help message\n\
383 -V, --version display version information\n");
388 static struct cmd_show_table cmd_show_tables
[] = {
389 {&vteprec_table_global
,
391 {&vteprec_global_col_managers
,
392 &vteprec_global_col_switches
,
397 {&vteprec_table_manager
,
398 &vteprec_manager_col_target
,
399 {&vteprec_manager_col_is_connected
,
405 {&vteprec_table_physical_switch
,
406 &vteprec_physical_switch_col_name
,
407 {&vteprec_physical_switch_col_management_ips
,
408 &vteprec_physical_switch_col_tunnel_ips
,
409 &vteprec_physical_switch_col_ports
},
413 {&vteprec_table_physical_port
,
414 &vteprec_physical_port_col_name
,
415 {&vteprec_physical_port_col_vlan_bindings
,
421 {&vteprec_table_logical_switch
,
422 &vteprec_logical_switch_col_name
,
429 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
432 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
433 struct vtep_ctl_context
{
434 struct ctl_context base
;
436 /* Modifiable state. */
437 const struct vteprec_global
*vtep_global
;
440 /* A cache of the contents of the database.
442 * A command that needs to use any of this information must first
443 * call vtep_ctl_context_populate_cache(). A command that changes
444 * anything that could invalidate the cache must either call
445 * vtep_ctl_context_invalidate_cache() or manually update the cache
446 * to maintain its correctness. */
448 struct shash pswitches
; /* Maps from physical switch name to
449 * struct vtep_ctl_pswitch. */
450 struct shash ports
; /* Maps from port name to struct vtep_ctl_port. */
452 struct shash lswitches
; /* Maps from logical switch name to
453 * struct vtep_ctl_lswitch. */
454 struct shash plocs
; /* Maps from "<encap>+<dst_ip>" to
455 * struct vteprec_physical_locator. */
456 struct shash lrouters
; /* Maps from logical router name to
457 * struct vtep_ctl_lrouter. */
460 /* Casts 'base' into 'struct vtep_ctl_context'. */
461 static struct vtep_ctl_context
*
462 vtep_ctl_context_cast(struct ctl_context
*base
)
464 return CONTAINER_OF(base
, struct vtep_ctl_context
, base
);
467 struct vtep_ctl_pswitch
{
468 const struct vteprec_physical_switch
*ps_cfg
;
470 struct ovs_list ports
; /* Contains "struct vteprec_physical_port"s. */
473 struct vtep_ctl_port
{
474 struct ovs_list ports_node
; /* In struct vtep_ctl_pswitch's 'ports' list. */
475 const struct vteprec_physical_port
*port_cfg
;
476 struct vtep_ctl_pswitch
*ps
;
477 struct shash bindings
; /* Maps from vlan to vtep_ctl_lswitch. */
480 struct vtep_ctl_lswitch
{
481 const struct vteprec_logical_switch
*ls_cfg
;
483 struct shash ucast_local
; /* Maps from mac to vteprec_ucast_macs_local. */
484 struct shash ucast_remote
; /* Maps from mac to vteprec_ucast_macs_remote.*/
485 struct shash mcast_local
; /* Maps from mac to vtep_ctl_mcast_mac. */
486 struct shash mcast_remote
; /* Maps from mac to vtep_ctl_mcast_mac. */
489 struct vtep_ctl_lrouter
{
490 const struct vteprec_logical_router
*lr_cfg
;
494 struct vtep_ctl_mcast_mac
{
495 const struct vteprec_mcast_macs_local
*local_cfg
;
496 const struct vteprec_mcast_macs_remote
*remote_cfg
;
498 const struct vteprec_physical_locator_set
*ploc_set_cfg
;
499 struct ovs_list locators
; /* Contains 'vtep_ctl_ploc's. */
502 struct vtep_ctl_ploc
{
503 struct ovs_list locators_node
; /* In struct vtep_ctl_ploc_set's 'locators'
505 const struct vteprec_physical_locator
*ploc_cfg
;
509 verify_ports(struct vtep_ctl_context
*vtepctl_ctx
)
511 if (!vtepctl_ctx
->verified_ports
) {
512 const struct vteprec_physical_switch
*ps
;
514 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
515 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps
, vtepctl_ctx
->base
.idl
) {
516 vteprec_physical_switch_verify_ports(ps
);
519 vtepctl_ctx
->verified_ports
= true;
523 static struct vtep_ctl_port
*
524 add_port_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
525 struct vtep_ctl_pswitch
*ps
,
526 struct vteprec_physical_port
*port_cfg
)
528 char *cache_name
= xasprintf("%s+%s", ps
->name
, port_cfg
->name
);
529 struct vtep_ctl_port
*port
;
531 port
= xmalloc(sizeof *port
);
532 ovs_list_push_back(&ps
->ports
, &port
->ports_node
);
533 port
->port_cfg
= port_cfg
;
535 shash_add(&vtepctl_ctx
->ports
, cache_name
, port
);
537 shash_init(&port
->bindings
);
543 del_cached_port(struct vtep_ctl_context
*vtepctl_ctx
,
544 struct vtep_ctl_port
*port
)
546 char *cache_name
= xasprintf("%s+%s", port
->ps
->name
, port
->port_cfg
->name
);
548 ovs_list_remove(&port
->ports_node
);
549 shash_find_and_delete(&vtepctl_ctx
->ports
, cache_name
);
550 vteprec_physical_port_delete(port
->port_cfg
);
551 shash_destroy(&port
->bindings
);
557 add_pswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
558 struct vteprec_physical_switch
*ps_cfg
)
560 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
562 ps
->name
= xstrdup(ps_cfg
->name
);
563 ovs_list_init(&ps
->ports
);
564 shash_add(&vtepctl_ctx
->pswitches
, ps
->name
, ps
);
568 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
569 const struct vteprec_physical_switch
*ps
)
571 struct vteprec_physical_switch
**pswitches
;
574 pswitches
= xmalloc(sizeof *vtep_global
->switches
575 * vtep_global
->n_switches
);
576 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
577 if (vtep_global
->switches
[i
] != ps
) {
578 pswitches
[n
++] = vtep_global
->switches
[i
];
581 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
586 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
588 ovs_assert(ovs_list_is_empty(&ps
->ports
));
590 vteprec_physical_switch_delete(ps
->ps_cfg
);
591 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
593 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
598 static struct vtep_ctl_lswitch
*
599 add_lswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
600 const struct vteprec_logical_switch
*ls_cfg
)
602 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
604 ls
->name
= xstrdup(ls_cfg
->name
);
605 shash_add(&vtepctl_ctx
->lswitches
, ls
->name
, ls
);
606 shash_init(&ls
->ucast_local
);
607 shash_init(&ls
->ucast_remote
);
608 shash_init(&ls
->mcast_local
);
609 shash_init(&ls
->mcast_remote
);
614 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
617 vteprec_logical_switch_delete(ls
->ls_cfg
);
619 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
625 commit_ls_bindings(struct vtep_ctl_port
*port
)
627 struct vteprec_logical_switch
**binding_values
;
628 int64_t *binding_keys
;
630 struct shash_node
*node
;
633 n_bindings
= shash_count(&port
->bindings
);
634 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
635 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
638 SHASH_FOR_EACH(node
, &port
->bindings
) {
639 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
641 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
642 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
646 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
647 binding_keys
, binding_values
,
649 free(binding_values
);
654 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
656 struct vtep_ctl_lswitch
*ls
)
658 if (shash_find(&port
->bindings
, vlan
)) {
659 ctl_fatal("multiple bindings for vlan %s", vlan
);
662 shash_add(&port
->bindings
, vlan
, ls
);
666 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
668 if (!shash_find(&port
->bindings
, vlan
)) {
669 ctl_fatal("no binding for vlan %s", vlan
);
672 shash_find_and_delete(&port
->bindings
, vlan
);
675 static struct vtep_ctl_lrouter
*
676 add_lrouter_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
677 const struct vteprec_logical_router
*lr_cfg
)
679 struct vtep_ctl_lrouter
*lr
= xmalloc(sizeof *lr
);
681 lr
->name
= xstrdup(lr_cfg
->name
);
682 shash_add(&vtepctl_ctx
->lrouters
, lr
->name
, lr
);
687 del_cached_lrouter(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lrouter
*lr
)
690 vteprec_logical_router_delete(lr
->lr_cfg
);
692 shash_find_and_delete(&ctx
->lrouters
, lr
->name
);
697 static struct vteprec_physical_locator
*
698 find_ploc(struct vtep_ctl_context
*vtepctl_ctx
, const char *encap
,
701 struct vteprec_physical_locator
*ploc
;
702 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
704 ovs_assert(vtepctl_ctx
->cache_valid
);
706 ploc
= shash_find_data(&vtepctl_ctx
->plocs
, name
);
713 add_ploc_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
714 struct vteprec_physical_locator
*ploc
)
716 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
717 struct vteprec_physical_locator
*orig_ploc
;
719 orig_ploc
= find_ploc(vtepctl_ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
721 shash_add(&vtepctl_ctx
->plocs
, name
, ploc
);
728 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
729 struct vteprec_physical_locator
*ploc_cfg
)
731 struct vtep_ctl_ploc
*ploc
;
733 ploc
= xmalloc(sizeof *ploc
);
734 ploc
->ploc_cfg
= ploc_cfg
;
735 ovs_list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
739 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
740 struct vteprec_physical_locator
*ploc_cfg
)
742 struct vtep_ctl_ploc
*ploc
;
744 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
745 if (ploc
->ploc_cfg
== ploc_cfg
) {
746 ovs_list_remove(&ploc
->locators_node
);
753 static struct vtep_ctl_mcast_mac
*
754 add_mcast_mac_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
755 struct vtep_ctl_lswitch
*ls
, const char *mac
,
756 struct vteprec_physical_locator_set
*ploc_set_cfg
,
759 struct vtep_ctl_mcast_mac
*mcast_mac
;
760 struct shash
*mcast_shash
;
763 mcast_mac
= xmalloc(sizeof *mcast_mac
);
764 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
766 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
767 ovs_list_init(&mcast_mac
->locators
);
768 shash_add(mcast_shash
, mac
, mcast_mac
);
770 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
771 struct vteprec_physical_locator
*ploc_cfg
;
773 ploc_cfg
= ploc_set_cfg
->locators
[i
];
774 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
775 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
782 vtep_ctl_context_invalidate_cache(struct ctl_context
*ctx
)
784 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
785 struct shash_node
*node
;
787 if (!vtepctl_ctx
->cache_valid
) {
790 vtepctl_ctx
->cache_valid
= false;
792 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
793 struct vtep_ctl_pswitch
*ps
= node
->data
;
797 shash_destroy(&vtepctl_ctx
->pswitches
);
799 SHASH_FOR_EACH (node
, &vtepctl_ctx
->ports
) {
800 struct vtep_ctl_port
*port
= node
->data
;
801 shash_destroy(&port
->bindings
);
803 shash_destroy_free_data(&vtepctl_ctx
->ports
);
805 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
806 struct vtep_ctl_lswitch
*ls
= node
->data
;
807 struct shash_node
*node2
, *next_node2
;
809 shash_destroy(&ls
->ucast_local
);
810 shash_destroy(&ls
->ucast_remote
);
812 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
813 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
814 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
816 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
817 &mcast_mac
->locators
) {
822 shash_destroy(&ls
->mcast_local
);
824 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
825 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
826 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
828 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
829 &mcast_mac
->locators
) {
834 shash_destroy(&ls
->mcast_remote
);
839 shash_destroy(&vtepctl_ctx
->lswitches
);
840 shash_destroy(&vtepctl_ctx
->plocs
);
842 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
843 struct vtep_ctl_lrouter
*lr
= node
->data
;
847 shash_destroy(&vtepctl_ctx
->lrouters
);
851 pre_get_info(struct ctl_context
*ctx
)
853 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
855 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
856 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
857 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_tunnels
);
859 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
860 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
862 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
863 ovsdb_idl_add_column(ctx
->idl
,
864 &vteprec_logical_switch_col_replication_mode
);
866 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_router_col_name
);
868 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
869 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
870 ovsdb_idl_add_column(ctx
->idl
,
871 &vteprec_ucast_macs_local_col_logical_switch
);
873 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
874 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
875 ovsdb_idl_add_column(ctx
->idl
,
876 &vteprec_ucast_macs_remote_col_logical_switch
);
878 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
879 ovsdb_idl_add_column(ctx
->idl
,
880 &vteprec_mcast_macs_local_col_locator_set
);
881 ovsdb_idl_add_column(ctx
->idl
,
882 &vteprec_mcast_macs_local_col_logical_switch
);
884 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
885 ovsdb_idl_add_column(ctx
->idl
,
886 &vteprec_mcast_macs_remote_col_locator_set
);
887 ovsdb_idl_add_column(ctx
->idl
,
888 &vteprec_mcast_macs_remote_col_logical_switch
);
890 ovsdb_idl_add_column(ctx
->idl
,
891 &vteprec_physical_locator_set_col_locators
);
893 ovsdb_idl_add_column(ctx
->idl
,
894 &vteprec_physical_locator_col_dst_ip
);
895 ovsdb_idl_add_column(ctx
->idl
,
896 &vteprec_physical_locator_col_encapsulation_type
);
898 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_local
);
899 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_remote
);
903 vtep_ctl_context_populate_cache(struct ctl_context
*ctx
)
905 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
906 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
907 const struct vteprec_logical_switch
*ls_cfg
;
908 const struct vteprec_logical_router
*lr_cfg
;
909 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
910 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
911 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
912 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
913 const struct vteprec_tunnel
*tunnel_cfg
;
914 struct sset pswitches
, ports
, lswitches
;
915 struct sset lrouters
;
918 if (vtepctl_ctx
->cache_valid
) {
919 /* Cache is already populated. */
922 vtepctl_ctx
->cache_valid
= true;
923 shash_init(&vtepctl_ctx
->pswitches
);
924 shash_init(&vtepctl_ctx
->ports
);
925 shash_init(&vtepctl_ctx
->lswitches
);
926 shash_init(&vtepctl_ctx
->plocs
);
927 shash_init(&vtepctl_ctx
->lrouters
);
929 sset_init(&pswitches
);
931 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
932 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
935 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
936 VLOG_WARN("%s: database contains duplicate physical switch name",
940 add_pswitch_to_cache(vtepctl_ctx
, ps_cfg
);
942 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
943 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
945 if (!sset_add(&ports
, port_cfg
->name
)) {
946 /* Duplicate port name. (We will warn about that later.) */
951 sset_destroy(&pswitches
);
952 sset_destroy(&ports
);
954 sset_init(&lswitches
);
955 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
956 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
957 VLOG_WARN("%s: database contains duplicate logical switch name",
961 add_lswitch_to_cache(vtepctl_ctx
, ls_cfg
);
963 sset_destroy(&lswitches
);
965 sset_init(&lrouters
);
966 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg
, ctx
->idl
) {
967 if (!sset_add(&lrouters
, lr_cfg
->name
)) {
968 VLOG_WARN("%s: database contains duplicate logical router name",
972 add_lrouter_to_cache(vtepctl_ctx
, lr_cfg
);
974 sset_destroy(&lrouters
);
976 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
977 struct vtep_ctl_lswitch
*ls
;
979 if (!ucast_local_cfg
->logical_switch
) {
982 ls
= find_lswitch(vtepctl_ctx
, ucast_local_cfg
->logical_switch
->name
,
988 if (ucast_local_cfg
->locator
) {
989 add_ploc_to_cache(vtepctl_ctx
, ucast_local_cfg
->locator
);
992 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
995 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
996 struct vtep_ctl_lswitch
*ls
;
998 if (!ucast_remote_cfg
->logical_switch
) {
1001 ls
= find_lswitch(vtepctl_ctx
, ucast_remote_cfg
->logical_switch
->name
,
1007 if (ucast_remote_cfg
->locator
) {
1008 add_ploc_to_cache(vtepctl_ctx
, ucast_remote_cfg
->locator
);
1011 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
1014 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
1015 struct vtep_ctl_mcast_mac
*mcast_mac
;
1016 struct vtep_ctl_lswitch
*ls
;
1018 if (!mcast_local_cfg
->logical_switch
) {
1021 ls
= find_lswitch(vtepctl_ctx
, mcast_local_cfg
->logical_switch
->name
,
1027 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_local_cfg
->MAC
,
1028 mcast_local_cfg
->locator_set
,
1030 mcast_mac
->local_cfg
= mcast_local_cfg
;
1033 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
1034 struct vtep_ctl_mcast_mac
*mcast_mac
;
1035 struct vtep_ctl_lswitch
*ls
;
1037 if (!mcast_remote_cfg
->logical_switch
) {
1040 ls
= find_lswitch(vtepctl_ctx
, mcast_remote_cfg
->logical_switch
->name
,
1046 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_remote_cfg
->MAC
,
1047 mcast_remote_cfg
->locator_set
,
1049 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
1052 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg
, ctx
->idl
) {
1053 if (tunnel_cfg
->local
) {
1054 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->local
);
1056 if (tunnel_cfg
->remote
) {
1057 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->remote
);
1061 sset_init(&pswitches
);
1062 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1063 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1064 struct vtep_ctl_pswitch
*ps
;
1067 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1070 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, ps_cfg
->name
);
1071 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1072 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1073 struct vtep_ctl_port
*port
;
1076 port
= shash_find_data(&vtepctl_ctx
->ports
, port_cfg
->name
);
1078 if (port_cfg
== port
->port_cfg
) {
1079 VLOG_WARN("%s: port is in multiple physical switches "
1081 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1083 /* Log as an error because this violates the database's
1084 * uniqueness constraints, so the database server shouldn't
1085 * have allowed it. */
1086 VLOG_ERR("%s: database contains duplicate port name",
1092 port
= add_port_to_cache(vtepctl_ctx
, ps
, port_cfg
);
1094 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
1095 struct vtep_ctl_lswitch
*ls
;
1098 vlan
= xasprintf("%"PRId64
, port_cfg
->key_vlan_bindings
[k
]);
1099 if (shash_find(&port
->bindings
, vlan
)) {
1100 ctl_fatal("multiple bindings for vlan %s", vlan
);
1103 ls_cfg
= port_cfg
->value_vlan_bindings
[k
];
1104 ls
= find_lswitch(vtepctl_ctx
, ls_cfg
->name
, true);
1106 shash_add_nocopy(&port
->bindings
, vlan
, ls
);
1110 sset_destroy(&pswitches
);
1113 static struct vtep_ctl_pswitch
*
1114 find_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, const char *name
, bool must_exist
)
1116 struct vtep_ctl_pswitch
*ps
;
1118 ovs_assert(vtepctl_ctx
->cache_valid
);
1120 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, name
);
1121 if (must_exist
&& !ps
) {
1122 ctl_fatal("no physical switch named %s", name
);
1124 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
1128 static struct vtep_ctl_port
*
1129 find_port(struct vtep_ctl_context
*vtepctl_ctx
, const char *ps_name
,
1130 const char *port_name
, bool must_exist
)
1132 char *cache_name
= xasprintf("%s+%s", ps_name
, port_name
);
1133 struct vtep_ctl_port
*port
;
1135 ovs_assert(vtepctl_ctx
->cache_valid
);
1137 port
= shash_find_data(&vtepctl_ctx
->ports
, cache_name
);
1138 if (port
&& !strcmp(port_name
, port
->ps
->name
)) {
1142 if (must_exist
&& !port
) {
1143 ctl_fatal("no port named %s", port_name
);
1145 verify_ports(vtepctl_ctx
);
1150 pswitch_insert_port(const struct vteprec_physical_switch
*ps
,
1151 struct vteprec_physical_port
*port
)
1153 struct vteprec_physical_port
**ports
;
1156 ports
= xmalloc(sizeof *ps
->ports
* (ps
->n_ports
+ 1));
1157 for (i
= 0; i
< ps
->n_ports
; i
++) {
1158 ports
[i
] = ps
->ports
[i
];
1160 ports
[ps
->n_ports
] = port
;
1161 vteprec_physical_switch_set_ports(ps
, ports
, ps
->n_ports
+ 1);
1166 pswitch_delete_port(const struct vteprec_physical_switch
*ps
,
1167 const struct vteprec_physical_port
*port
)
1169 struct vteprec_physical_port
**ports
;
1172 ports
= xmalloc(sizeof *ps
->ports
* ps
->n_ports
);
1173 for (i
= n
= 0; i
< ps
->n_ports
; i
++) {
1174 if (ps
->ports
[i
] != port
) {
1175 ports
[n
++] = ps
->ports
[i
];
1178 vteprec_physical_switch_set_ports(ps
, ports
, n
);
1183 vtep_insert_pswitch(const struct vteprec_global
*vtep_global
,
1184 struct vteprec_physical_switch
*ps
)
1186 struct vteprec_physical_switch
**pswitches
;
1189 pswitches
= xmalloc(sizeof *vtep_global
->switches
1190 * (vtep_global
->n_switches
+ 1));
1191 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1192 pswitches
[i
] = vtep_global
->switches
[i
];
1194 pswitches
[vtep_global
->n_switches
] = ps
;
1195 vteprec_global_set_switches(vtep_global
, pswitches
,
1196 vtep_global
->n_switches
+ 1);
1201 cmd_add_ps(struct ctl_context
*ctx
)
1203 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1204 const char *ps_name
= ctx
->argv
[1];
1205 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1206 struct vteprec_physical_switch
*ps
;
1208 vtep_ctl_context_populate_cache(ctx
);
1209 if (find_pswitch(vtepctl_ctx
, ps_name
, false)) {
1211 ctl_fatal("cannot create physical switch %s because it "
1212 "already exists", ps_name
);
1217 ps
= vteprec_physical_switch_insert(ctx
->txn
);
1218 vteprec_physical_switch_set_name(ps
, ps_name
);
1220 vtep_insert_pswitch(vtepctl_ctx
->vtep_global
, ps
);
1222 vtep_ctl_context_invalidate_cache(ctx
);
1226 del_port(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_port
*port
)
1228 pswitch_delete_port(port
->ps
->ps_cfg
, port
->port_cfg
);
1229 del_cached_port(vtepctl_ctx
, port
);
1233 del_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_pswitch
*ps
)
1235 struct vtep_ctl_port
*port
, *next_port
;
1237 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &ps
->ports
) {
1238 del_port(vtepctl_ctx
, port
);
1241 del_cached_pswitch(vtepctl_ctx
, ps
);
1245 cmd_del_ps(struct ctl_context
*ctx
)
1247 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1248 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1249 struct vtep_ctl_pswitch
*ps
;
1251 vtep_ctl_context_populate_cache(ctx
);
1252 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1254 del_pswitch(vtepctl_ctx
, ps
);
1259 output_sorted(struct svec
*svec
, struct ds
*output
)
1265 SVEC_FOR_EACH (i
, name
, svec
) {
1266 ds_put_format(output
, "%s\n", name
);
1271 cmd_list_ps(struct ctl_context
*ctx
)
1273 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1274 struct shash_node
*node
;
1275 struct svec pswitches
;
1277 vtep_ctl_context_populate_cache(ctx
);
1279 svec_init(&pswitches
);
1280 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
1281 struct vtep_ctl_pswitch
*ps
= node
->data
;
1283 svec_add(&pswitches
, ps
->name
);
1285 output_sorted(&pswitches
, &ctx
->output
);
1286 svec_destroy(&pswitches
);
1290 cmd_ps_exists(struct ctl_context
*ctx
)
1292 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1294 vtep_ctl_context_populate_cache(ctx
);
1295 if (!find_pswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1301 cmd_list_ports(struct ctl_context
*ctx
)
1303 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1304 struct vtep_ctl_pswitch
*ps
;
1305 struct vtep_ctl_port
*port
;
1308 vtep_ctl_context_populate_cache(ctx
);
1309 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1310 vteprec_physical_switch_verify_ports(ps
->ps_cfg
);
1313 LIST_FOR_EACH (port
, ports_node
, &ps
->ports
) {
1314 if (strcmp(port
->port_cfg
->name
, ps
->name
)) {
1315 svec_add(&ports
, port
->port_cfg
->name
);
1318 output_sorted(&ports
, &ctx
->output
);
1319 svec_destroy(&ports
);
1323 add_port(struct ctl_context
*ctx
, const char *ps_name
,
1324 const char *port_name
, bool may_exist
)
1326 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1327 struct vtep_ctl_port
*vtep_ctl_port
;
1328 struct vtep_ctl_pswitch
*ps
;
1329 struct vteprec_physical_port
*port
;
1331 vtep_ctl_context_populate_cache(ctx
);
1333 vtep_ctl_port
= find_port(vtepctl_ctx
, ps_name
, port_name
, false);
1334 if (vtep_ctl_port
) {
1336 ctl_fatal("cannot create a port named %s on %s because a "
1337 "port with that name already exists",
1338 port_name
, ps_name
);
1343 ps
= find_pswitch(vtepctl_ctx
, ps_name
, true);
1345 port
= vteprec_physical_port_insert(ctx
->txn
);
1346 vteprec_physical_port_set_name(port
, port_name
);
1348 pswitch_insert_port(ps
->ps_cfg
, port
);
1350 add_port_to_cache(vtepctl_ctx
, ps
, port
);
1354 cmd_add_port(struct ctl_context
*ctx
)
1356 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1358 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
);
1362 cmd_del_port(struct ctl_context
*ctx
)
1364 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1365 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1366 struct vtep_ctl_port
*port
;
1368 vtep_ctl_context_populate_cache(ctx
);
1370 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], must_exist
);
1372 if (ctx
->argc
== 3) {
1373 struct vtep_ctl_pswitch
*ps
;
1375 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1376 if (port
->ps
!= ps
) {
1377 ctl_fatal("physical switch %s does not have a port %s",
1378 ctx
->argv
[1], ctx
->argv
[2]);
1382 del_port(vtepctl_ctx
, port
);
1386 static struct vtep_ctl_lswitch
*
1387 find_lswitch(struct vtep_ctl_context
*vtepctl_ctx
,
1388 const char *name
, bool must_exist
)
1390 struct vtep_ctl_lswitch
*ls
;
1392 ovs_assert(vtepctl_ctx
->cache_valid
);
1394 ls
= shash_find_data(&vtepctl_ctx
->lswitches
, name
);
1395 if (must_exist
&& !ls
) {
1396 ctl_fatal("no logical switch named %s", name
);
1402 cmd_add_ls(struct ctl_context
*ctx
)
1404 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1405 const char *ls_name
= ctx
->argv
[1];
1406 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1407 struct vteprec_logical_switch
*ls
;
1409 vtep_ctl_context_populate_cache(ctx
);
1410 if (find_lswitch(vtepctl_ctx
, ls_name
, false)) {
1412 ctl_fatal("cannot create logical switch %s because it "
1413 "already exists", ls_name
);
1418 ls
= vteprec_logical_switch_insert(ctx
->txn
);
1419 vteprec_logical_switch_set_name(ls
, ls_name
);
1421 vtep_ctl_context_invalidate_cache(ctx
);
1425 del_lswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lswitch
*ls
)
1427 del_cached_lswitch(vtepctl_ctx
, ls
);
1431 cmd_del_ls(struct ctl_context
*ctx
)
1433 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1434 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1435 struct vtep_ctl_lswitch
*ls
;
1437 vtep_ctl_context_populate_cache(ctx
);
1438 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1440 del_lswitch(vtepctl_ctx
, ls
);
1445 cmd_list_ls(struct ctl_context
*ctx
)
1447 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1448 struct shash_node
*node
;
1449 struct svec lswitches
;
1451 vtep_ctl_context_populate_cache(ctx
);
1453 svec_init(&lswitches
);
1454 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
1455 struct vtep_ctl_lswitch
*ls
= node
->data
;
1457 svec_add(&lswitches
, ls
->name
);
1459 output_sorted(&lswitches
, &ctx
->output
);
1460 svec_destroy(&lswitches
);
1464 cmd_ls_exists(struct ctl_context
*ctx
)
1466 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1468 vtep_ctl_context_populate_cache(ctx
);
1469 if (!find_lswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1475 cmd_list_bindings(struct ctl_context
*ctx
)
1477 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1478 const struct shash_node
*node
;
1479 struct vtep_ctl_port
*port
;
1480 struct svec bindings
;
1482 vtep_ctl_context_populate_cache(ctx
);
1483 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1485 svec_init(&bindings
);
1486 SHASH_FOR_EACH (node
, &port
->bindings
) {
1487 struct vtep_ctl_lswitch
*lswitch
= node
->data
;
1490 binding
= xasprintf("%04lld %s", strtoll(node
->name
, NULL
, 0),
1492 svec_add_nocopy(&bindings
, binding
);
1494 output_sorted(&bindings
, &ctx
->output
);
1495 svec_destroy(&bindings
);
1499 cmd_bind_ls(struct ctl_context
*ctx
)
1501 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1502 struct vtep_ctl_lswitch
*ls
;
1503 struct vtep_ctl_port
*port
;
1506 vtep_ctl_context_populate_cache(ctx
);
1508 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1509 vlan
= ctx
->argv
[3];
1510 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[4], true);
1512 add_ls_binding_to_cache(port
, vlan
, ls
);
1513 commit_ls_bindings(port
);
1515 vtep_ctl_context_invalidate_cache(ctx
);
1519 cmd_unbind_ls(struct ctl_context
*ctx
)
1521 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1522 struct vtep_ctl_port
*port
;
1525 vtep_ctl_context_populate_cache(ctx
);
1527 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1528 vlan
= ctx
->argv
[3];
1530 del_cached_ls_binding(port
, vlan
);
1531 commit_ls_bindings(port
);
1533 vtep_ctl_context_invalidate_cache(ctx
);
1537 cmd_set_replication_mode(struct ctl_context
*ctx
)
1539 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1540 struct vtep_ctl_lswitch
*ls
;
1541 const char *ls_name
= ctx
->argv
[1];
1543 vtep_ctl_context_populate_cache(ctx
);
1545 if (strcmp(ctx
->argv
[2], "service_node") &&
1546 strcmp(ctx
->argv
[2], "source_node")) {
1547 ctl_fatal("Replication mode must be 'service_node' or 'source_node'");
1550 ls
= find_lswitch(vtepctl_ctx
, ls_name
, true);
1551 vteprec_logical_switch_set_replication_mode(ls
->ls_cfg
, ctx
->argv
[2]);
1553 vtep_ctl_context_invalidate_cache(ctx
);
1557 cmd_get_replication_mode(struct ctl_context
*ctx
)
1559 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1560 struct vtep_ctl_lswitch
*ls
;
1561 const char *ls_name
= ctx
->argv
[1];
1563 vtep_ctl_context_populate_cache(ctx
);
1565 ls
= find_lswitch(vtepctl_ctx
, ls_name
, true);
1566 ds_put_format(&ctx
->output
, "%s\n", ls
->ls_cfg
->replication_mode
);
1569 static struct vtep_ctl_lrouter
*
1570 find_lrouter(struct vtep_ctl_context
*vtepctl_ctx
,
1571 const char *name
, bool must_exist
)
1573 struct vtep_ctl_lrouter
*lr
;
1575 ovs_assert(vtepctl_ctx
->cache_valid
);
1577 lr
= shash_find_data(&vtepctl_ctx
->lrouters
, name
);
1578 if (must_exist
&& !lr
) {
1579 ctl_fatal("no logical router named %s", name
);
1585 cmd_add_lr(struct ctl_context
*ctx
)
1587 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1588 const char *lr_name
= ctx
->argv
[1];
1589 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1590 struct vteprec_logical_router
*lr
;
1592 vtep_ctl_context_populate_cache(ctx
);
1593 if (find_lrouter(vtepctl_ctx
, lr_name
, false)) {
1595 ctl_fatal("cannot create logical router %s because it "
1596 "already exists", lr_name
);
1601 lr
= vteprec_logical_router_insert(ctx
->txn
);
1602 vteprec_logical_router_set_name(lr
, lr_name
);
1604 vtep_ctl_context_invalidate_cache(ctx
);
1608 del_lrouter(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lrouter
*lr
)
1610 del_cached_lrouter(vtepctl_ctx
, lr
);
1614 cmd_del_lr(struct ctl_context
*ctx
)
1616 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1617 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1618 struct vtep_ctl_lrouter
*lr
;
1620 vtep_ctl_context_populate_cache(ctx
);
1621 lr
= find_lrouter(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1623 del_lrouter(vtepctl_ctx
, lr
);
1628 cmd_list_lr(struct ctl_context
*ctx
)
1630 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1631 struct shash_node
*node
;
1632 struct svec lrouters
;
1634 vtep_ctl_context_populate_cache(ctx
);
1636 svec_init(&lrouters
);
1637 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
1638 struct vtep_ctl_lrouter
*lr
= node
->data
;
1640 svec_add(&lrouters
, lr
->name
);
1642 output_sorted(&lrouters
, &ctx
->output
);
1643 svec_destroy(&lrouters
);
1647 cmd_lr_exists(struct ctl_context
*ctx
)
1649 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1651 vtep_ctl_context_populate_cache(ctx
);
1652 if (!find_lrouter(vtepctl_ctx
, ctx
->argv
[1], false)) {
1658 add_ucast_entry(struct ctl_context
*ctx
, bool local
)
1660 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1661 struct vtep_ctl_lswitch
*ls
;
1665 struct vteprec_physical_locator
*ploc_cfg
;
1667 vtep_ctl_context_populate_cache(ctx
);
1669 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1672 if (ctx
->argc
== 4) {
1673 encap
= "vxlan_over_ipv4";
1674 dst_ip
= ctx
->argv
[3];
1676 encap
= ctx
->argv
[3];
1677 dst_ip
= ctx
->argv
[4];
1680 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1682 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1683 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1684 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1686 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1690 struct vteprec_ucast_macs_local
*ucast_cfg
;
1692 ucast_cfg
= shash_find_data(&ls
->ucast_local
, mac
);
1694 ucast_cfg
= vteprec_ucast_macs_local_insert(ctx
->txn
);
1695 vteprec_ucast_macs_local_set_MAC(ucast_cfg
, mac
);
1696 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1697 shash_add(&ls
->ucast_local
, mac
, ucast_cfg
);
1699 vteprec_ucast_macs_local_set_locator(ucast_cfg
, ploc_cfg
);
1701 struct vteprec_ucast_macs_remote
*ucast_cfg
;
1703 ucast_cfg
= shash_find_data(&ls
->ucast_remote
, mac
);
1705 ucast_cfg
= vteprec_ucast_macs_remote_insert(ctx
->txn
);
1706 vteprec_ucast_macs_remote_set_MAC(ucast_cfg
, mac
);
1707 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1708 shash_add(&ls
->ucast_remote
, mac
, ucast_cfg
);
1710 vteprec_ucast_macs_remote_set_locator(ucast_cfg
, ploc_cfg
);
1713 vtep_ctl_context_invalidate_cache(ctx
);
1717 cmd_add_ucast_local(struct ctl_context
*ctx
)
1719 add_ucast_entry(ctx
, true);
1723 cmd_add_ucast_remote(struct ctl_context
*ctx
)
1725 add_ucast_entry(ctx
, false);
1729 del_ucast_entry(struct ctl_context
*ctx
, bool local
)
1731 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1732 struct vtep_ctl_lswitch
*ls
;
1733 struct shash
*ucast_shash
;
1734 struct shash_node
*node
;
1736 vtep_ctl_context_populate_cache(ctx
);
1738 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1739 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1741 node
= shash_find(ucast_shash
, ctx
->argv
[2]);
1747 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1748 vteprec_ucast_macs_local_delete(ucast_cfg
);
1750 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1751 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1753 shash_delete(ucast_shash
, node
);
1755 vtep_ctl_context_invalidate_cache(ctx
);
1759 cmd_del_ucast_local(struct ctl_context
*ctx
)
1761 del_ucast_entry(ctx
, true);
1765 cmd_del_ucast_remote(struct ctl_context
*ctx
)
1767 del_ucast_entry(ctx
, false);
1771 commit_mcast_entries(struct vtep_ctl_mcast_mac
*mcast_mac
)
1773 struct vtep_ctl_ploc
*ploc
;
1774 struct vteprec_physical_locator
**locators
= NULL
;
1778 n_locators
= ovs_list_size(&mcast_mac
->locators
);
1779 ovs_assert(n_locators
);
1781 locators
= xmalloc(n_locators
* sizeof *locators
);
1784 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1785 locators
[i
] = (struct vteprec_physical_locator
*)ploc
->ploc_cfg
;
1789 vteprec_physical_locator_set_set_locators(mcast_mac
->ploc_set_cfg
,
1797 add_mcast_entry(struct ctl_context
*ctx
,
1798 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1799 const char *encap
, const char *dst_ip
, bool local
)
1801 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1802 struct shash
*mcast_shash
;
1803 struct vtep_ctl_mcast_mac
*mcast_mac
;
1804 struct vteprec_physical_locator
*ploc_cfg
;
1805 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1807 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1809 /* Physical locator sets are immutable, so allocate a new one. */
1810 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1812 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1814 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mac
, ploc_set_cfg
,
1818 mcast_mac
->local_cfg
= vteprec_mcast_macs_local_insert(ctx
->txn
);
1819 vteprec_mcast_macs_local_set_MAC(mcast_mac
->local_cfg
, mac
);
1820 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1822 vteprec_mcast_macs_local_set_logical_switch(mcast_mac
->local_cfg
,
1824 mcast_mac
->remote_cfg
= NULL
;
1826 mcast_mac
->remote_cfg
= vteprec_mcast_macs_remote_insert(ctx
->txn
);
1827 vteprec_mcast_macs_remote_set_MAC(mcast_mac
->remote_cfg
, mac
);
1828 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1830 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac
->remote_cfg
,
1832 mcast_mac
->local_cfg
= NULL
;
1835 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1837 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1840 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1845 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1847 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1848 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1849 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1851 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1854 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1855 commit_mcast_entries(mcast_mac
);
1859 del_mcast_entry(struct ctl_context
*ctx
,
1860 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1861 const char *encap
, const char *dst_ip
, bool local
)
1863 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1864 struct vtep_ctl_mcast_mac
*mcast_mac
;
1865 struct shash
*mcast_shash
;
1866 struct vteprec_physical_locator
*ploc_cfg
;
1867 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1869 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1871 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1876 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1878 /* Couldn't find the physical locator, so just ignore. */
1882 /* Physical locator sets are immutable, so allocate a new one. */
1883 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1884 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1886 del_ploc_from_mcast_mac(mcast_mac
, ploc_cfg
);
1887 if (ovs_list_is_empty(&mcast_mac
->locators
)) {
1888 struct shash_node
*node
= shash_find(mcast_shash
, mac
);
1890 vteprec_physical_locator_set_delete(ploc_set_cfg
);
1893 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1895 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1899 shash_delete(mcast_shash
, node
);
1902 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1905 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1908 commit_mcast_entries(mcast_mac
);
1913 add_del_mcast_entry(struct ctl_context
*ctx
, bool add
, bool local
)
1915 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1916 struct vtep_ctl_lswitch
*ls
;
1921 vtep_ctl_context_populate_cache(ctx
);
1923 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1926 if (ctx
->argc
== 4) {
1927 encap
= "vxlan_over_ipv4";
1928 dst_ip
= ctx
->argv
[3];
1930 encap
= ctx
->argv
[3];
1931 dst_ip
= ctx
->argv
[4];
1935 add_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1937 del_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1940 vtep_ctl_context_invalidate_cache(ctx
);
1944 cmd_add_mcast_local(struct ctl_context
*ctx
)
1946 add_del_mcast_entry(ctx
, true, true);
1950 cmd_add_mcast_remote(struct ctl_context
*ctx
)
1952 add_del_mcast_entry(ctx
, true, false);
1956 cmd_del_mcast_local(struct ctl_context
*ctx
)
1958 add_del_mcast_entry(ctx
, false, true);
1962 cmd_del_mcast_remote(struct ctl_context
*ctx
)
1964 add_del_mcast_entry(ctx
, false, false);
1968 clear_macs(struct ctl_context
*ctx
, bool local
)
1970 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1971 struct vtep_ctl_lswitch
*ls
;
1972 const struct shash_node
*node
;
1973 struct shash
*ucast_shash
;
1974 struct shash
*mcast_shash
;
1976 vtep_ctl_context_populate_cache(ctx
);
1977 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1979 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1980 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1982 SHASH_FOR_EACH (node
, ucast_shash
) {
1984 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1985 vteprec_ucast_macs_local_delete(ucast_cfg
);
1987 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1988 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1992 SHASH_FOR_EACH (node
, mcast_shash
) {
1993 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
1995 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1997 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
2001 vtep_ctl_context_invalidate_cache(ctx
);
2005 cmd_clear_local_macs(struct ctl_context
*ctx
)
2007 clear_macs(ctx
, true);
2011 cmd_clear_remote_macs(struct ctl_context
*ctx
)
2013 clear_macs(ctx
, false);
2017 list_macs(struct ctl_context
*ctx
, bool local
)
2019 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2020 struct vtep_ctl_lswitch
*ls
;
2021 const struct shash_node
*node
;
2022 struct shash
*ucast_shash
;
2023 struct svec ucast_macs
;
2024 struct shash
*mcast_shash
;
2025 struct svec mcast_macs
;
2027 vtep_ctl_context_populate_cache(ctx
);
2028 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
2030 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
2031 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
2033 svec_init(&ucast_macs
);
2034 SHASH_FOR_EACH (node
, ucast_shash
) {
2035 struct vteprec_ucast_macs_local
*ucast_local
= node
->data
;
2036 struct vteprec_ucast_macs_remote
*ucast_remote
= node
->data
;
2037 struct vteprec_physical_locator
*ploc_cfg
;
2040 ploc_cfg
= local
? ucast_local
->locator
: ucast_remote
->locator
;
2042 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2043 ploc_cfg
->encapsulation_type
, ploc_cfg
->dst_ip
);
2044 svec_add_nocopy(&ucast_macs
, entry
);
2046 ds_put_format(&ctx
->output
, "ucast-mac-%s\n", local
? "local" : "remote");
2047 output_sorted(&ucast_macs
, &ctx
->output
);
2048 ds_put_char(&ctx
->output
, '\n');
2049 svec_destroy(&ucast_macs
);
2051 svec_init(&mcast_macs
);
2052 SHASH_FOR_EACH (node
, mcast_shash
) {
2053 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
2054 struct vtep_ctl_ploc
*ploc
;
2057 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
2058 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2059 ploc
->ploc_cfg
->encapsulation_type
,
2060 ploc
->ploc_cfg
->dst_ip
);
2061 svec_add_nocopy(&mcast_macs
, entry
);
2064 ds_put_format(&ctx
->output
, "mcast-mac-%s\n", local
? "local" : "remote");
2065 output_sorted(&mcast_macs
, &ctx
->output
);
2066 ds_put_char(&ctx
->output
, '\n');
2067 svec_destroy(&mcast_macs
);
2071 cmd_list_local_macs(struct ctl_context
*ctx
)
2073 list_macs(ctx
, true);
2077 cmd_list_remote_macs(struct ctl_context
*ctx
)
2079 list_macs(ctx
, false);
2083 verify_managers(const struct vteprec_global
*vtep_global
)
2087 vteprec_global_verify_managers(vtep_global
);
2089 for (i
= 0; i
< vtep_global
->n_managers
; ++i
) {
2090 const struct vteprec_manager
*mgr
= vtep_global
->managers
[i
];
2092 vteprec_manager_verify_target(mgr
);
2097 pre_manager(struct ctl_context
*ctx
)
2099 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_managers
);
2100 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_target
);
2101 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_inactivity_probe
);
2105 cmd_get_manager(struct ctl_context
*ctx
)
2107 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2108 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2109 struct svec targets
;
2112 verify_managers(vtep_global
);
2114 /* Print the targets in sorted order for reproducibility. */
2115 svec_init(&targets
);
2117 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2118 svec_add(&targets
, vtep_global
->managers
[i
]->target
);
2121 svec_sort_unique(&targets
);
2122 for (i
= 0; i
< targets
.n
; i
++) {
2123 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2125 svec_destroy(&targets
);
2129 delete_managers(const struct vtep_ctl_context
*vtepctl_ctx
)
2131 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2134 /* Delete Manager rows pointed to by 'managers' column. */
2135 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2136 vteprec_manager_delete(vtep_global
->managers
[i
]);
2139 /* Delete 'Manager' row refs in 'managers' column. */
2140 vteprec_global_set_managers(vtep_global
, NULL
, 0);
2144 cmd_del_manager(struct ctl_context
*ctx
)
2146 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2147 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2149 verify_managers(vtep_global
);
2150 delete_managers(vtepctl_ctx
);
2154 insert_managers(struct vtep_ctl_context
*vtepctl_ctx
, char *targets
[],
2155 size_t n
, struct shash
*options
)
2157 struct vteprec_manager
**managers
;
2159 const char *inactivity_probe
= shash_find_data(options
,
2160 "--inactivity-probe");
2162 /* Insert each manager in a new row in Manager table. */
2163 managers
= xmalloc(n
* sizeof *managers
);
2164 for (i
= 0; i
< n
; i
++) {
2165 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2166 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2168 managers
[i
] = vteprec_manager_insert(vtepctl_ctx
->base
.txn
);
2169 vteprec_manager_set_target(managers
[i
], targets
[i
]);
2170 if (inactivity_probe
) {
2171 int64_t msecs
= atoll(inactivity_probe
);
2172 vteprec_manager_set_inactivity_probe(managers
[i
], &msecs
, 1);
2176 /* Store uuids of new Manager rows in 'managers' column. */
2177 vteprec_global_set_managers(vtepctl_ctx
->vtep_global
, managers
, n
);
2182 cmd_set_manager(struct ctl_context
*ctx
)
2184 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2185 const size_t n
= ctx
->argc
- 1;
2187 verify_managers(vtepctl_ctx
->vtep_global
);
2188 delete_managers(vtepctl_ctx
);
2189 insert_managers(vtepctl_ctx
, &ctx
->argv
[1], n
, &ctx
->options
);
2192 /* Parameter commands. */
2193 static const struct ctl_table_class tables
[VTEPREC_N_TABLES
] = {
2194 [VTEPREC_TABLE_LOGICAL_SWITCH
].row_ids
[0]
2195 = {&vteprec_logical_switch_col_name
, NULL
, NULL
},
2197 [VTEPREC_TABLE_MANAGER
].row_ids
[0]
2198 = {&vteprec_manager_col_target
, NULL
, NULL
},
2200 [VTEPREC_TABLE_PHYSICAL_PORT
].row_ids
[0]
2201 = {&vteprec_physical_port_col_name
, NULL
, NULL
},
2203 [VTEPREC_TABLE_PHYSICAL_SWITCH
].row_ids
[0]
2204 = {&vteprec_physical_switch_col_name
, NULL
, NULL
},
2206 [VTEPREC_TABLE_LOGICAL_ROUTER
].row_ids
[0]
2207 = {&vteprec_logical_router_col_name
, NULL
, NULL
},
2212 vtep_ctl_context_init_command(struct vtep_ctl_context
*vtepctl_ctx
,
2213 struct ctl_command
*command
)
2215 ctl_context_init_command(&vtepctl_ctx
->base
, command
);
2216 vtepctl_ctx
->verified_ports
= false;
2221 vtep_ctl_context_init(struct vtep_ctl_context
*vtepctl_ctx
,
2222 struct ctl_command
*command
,
2223 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2224 const struct vteprec_global
*vtep_global
,
2225 struct ovsdb_symbol_table
*symtab
)
2227 ctl_context_init(&vtepctl_ctx
->base
, command
, idl
, txn
, symtab
,
2228 vtep_ctl_context_invalidate_cache
);
2230 vtepctl_ctx
->verified_ports
= false;
2232 vtepctl_ctx
->vtep_global
= vtep_global
;
2233 vtepctl_ctx
->cache_valid
= false;
2237 vtep_ctl_context_done_command(struct vtep_ctl_context
*vtepctl_ctx
,
2238 struct ctl_command
*command
)
2240 ctl_context_done_command(&vtepctl_ctx
->base
, command
);
2244 vtep_ctl_context_done(struct vtep_ctl_context
*vtepctl_ctx
,
2245 struct ctl_command
*command
)
2247 ctl_context_done(&vtepctl_ctx
->base
, command
);
2251 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2252 struct ovsdb_idl
*idl
)
2254 struct ctl_command
*c
;
2256 ovsdb_idl_add_table(idl
, &vteprec_table_global
);
2257 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2258 if (c
->syntax
->prerequisites
) {
2259 struct vtep_ctl_context vtepctl_ctx
;
2261 ds_init(&c
->output
);
2264 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2265 (c
->syntax
->prerequisites
)(&vtepctl_ctx
.base
);
2266 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2268 ovs_assert(!c
->output
.string
);
2269 ovs_assert(!c
->table
);
2275 do_vtep_ctl(const char *args
, struct ctl_command
*commands
,
2276 size_t n_commands
, struct ovsdb_idl
*idl
)
2278 struct ovsdb_idl_txn
*txn
;
2279 const struct vteprec_global
*vtep_global
;
2280 enum ovsdb_idl_txn_status status
;
2281 struct ovsdb_symbol_table
*symtab
;
2282 struct vtep_ctl_context vtepctl_ctx
;
2283 struct ctl_command
*c
;
2284 struct shash_node
*node
;
2287 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2289 ovsdb_idl_txn_set_dry_run(txn
);
2292 ovsdb_idl_txn_add_comment(txn
, "vtep-ctl: %s", args
);
2294 vtep_global
= vteprec_global_first(idl
);
2296 /* XXX add verification that table is empty */
2297 vtep_global
= vteprec_global_insert(txn
);
2300 symtab
= ovsdb_symbol_table_create();
2301 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2302 ds_init(&c
->output
);
2305 vtep_ctl_context_init(&vtepctl_ctx
, NULL
, idl
, txn
, vtep_global
, symtab
);
2306 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2307 vtep_ctl_context_init_command(&vtepctl_ctx
, c
);
2308 if (c
->syntax
->run
) {
2309 (c
->syntax
->run
)(&vtepctl_ctx
.base
);
2311 vtep_ctl_context_done_command(&vtepctl_ctx
, c
);
2313 if (vtepctl_ctx
.base
.try_again
) {
2314 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2318 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2320 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2321 struct ovsdb_symbol
*symbol
= node
->data
;
2322 if (!symbol
->created
) {
2323 ctl_fatal("row id \"%s\" is referenced but never created "
2324 "(e.g. with \"-- --id=%s create ...\")",
2325 node
->name
, node
->name
);
2327 if (!symbol
->strong_ref
) {
2328 if (!symbol
->weak_ref
) {
2329 VLOG_WARN("row id \"%s\" was created but no reference to it "
2330 "was inserted, so it will not actually appear in "
2331 "the database", node
->name
);
2333 VLOG_WARN("row id \"%s\" was created but only a weak "
2334 "reference to it was inserted, so it will not "
2335 "actually appear in the database", node
->name
);
2340 status
= ovsdb_idl_txn_commit_block(txn
);
2341 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2342 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2343 if (c
->syntax
->postprocess
) {
2344 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, txn
, vtep_global
, symtab
);
2345 (c
->syntax
->postprocess
)(&vtepctl_ctx
.base
);
2346 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2350 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2351 ovsdb_idl_txn_destroy(txn
);
2352 txn
= the_idl_txn
= NULL
;
2355 case TXN_UNCOMMITTED
:
2356 case TXN_INCOMPLETE
:
2360 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2361 ctl_fatal("transaction aborted");
2371 ctl_fatal("transaction error: %s", error
);
2373 case TXN_NOT_LOCKED
:
2374 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2375 ctl_fatal("database not locked");
2382 ovsdb_symbol_table_destroy(symtab
);
2384 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2385 struct ds
*ds
= &c
->output
;
2388 table_print(c
->table
, &table_style
);
2389 } else if (oneline
) {
2393 for (j
= 0; j
< ds
->length
; j
++) {
2394 int ch
= ds
->string
[j
];
2397 fputs("\\n", stdout
);
2401 fputs("\\\\", stdout
);
2410 fputs(ds_cstr(ds
), stdout
);
2412 ds_destroy(&c
->output
);
2413 table_destroy(c
->table
);
2416 shash_destroy_free_data(&c
->options
);
2420 ovsdb_idl_destroy(idl
);
2425 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2426 * resources and return so that the caller can try again. */
2428 ovsdb_idl_txn_abort(txn
);
2429 ovsdb_idl_txn_destroy(txn
);
2431 ovsdb_symbol_table_destroy(symtab
);
2432 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2433 ds_destroy(&c
->output
);
2434 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
},
2463 {"set-replication-mode", 2, 2, "LS MODE", pre_get_info
,
2464 cmd_set_replication_mode
, NULL
, "", RW
},
2465 {"get-replication-mode", 1, 1, "LS", pre_get_info
,
2466 cmd_get_replication_mode
, NULL
, "", RO
},
2468 /* Logical Router commands. */
2469 {"add-lr", 1, 1, NULL
, pre_get_info
, cmd_add_lr
, NULL
, "--may-exist", RW
},
2470 {"del-lr", 1, 1, NULL
, pre_get_info
, cmd_del_lr
, NULL
, "--if-exists", RW
},
2471 {"list-lr", 0, 0, NULL
, pre_get_info
, cmd_list_lr
, NULL
, "", RO
},
2472 {"lr-exists", 1, 1, NULL
, pre_get_info
, cmd_lr_exists
, NULL
, "", RO
},
2474 /* MAC binding commands. */
2475 {"add-ucast-local", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_local
, NULL
,
2477 {"del-ucast-local", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_local
, NULL
,
2479 {"add-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_local
, NULL
,
2481 {"del-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_local
, NULL
,
2483 {"clear-local-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_local_macs
, NULL
,
2485 {"list-local-macs", 1, 1, NULL
, pre_get_info
, cmd_list_local_macs
, NULL
,
2487 {"add-ucast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_remote
, NULL
,
2489 {"del-ucast-remote", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_remote
, NULL
,
2491 {"add-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_remote
, NULL
,
2493 {"del-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_remote
, NULL
,
2495 {"clear-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_remote_macs
, NULL
,
2497 {"list-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_list_remote_macs
, NULL
,
2500 /* Manager commands. */
2501 {"get-manager", 0, 0, NULL
, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2502 {"del-manager", 0, 0, NULL
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2503 {"set-manager", 1, INT_MAX
, NULL
, pre_manager
, cmd_set_manager
, NULL
,
2504 "--inactivity-probe=", RW
},
2506 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2509 /* Registers vsctl and common db commands. */
2511 vtep_ctl_cmd_init(void)
2513 ctl_init(&vteprec_idl_class
, vteprec_table_classes
, tables
,
2514 cmd_show_tables
, vtep_ctl_exit
);
2515 ctl_register_commands(vtep_commands
);