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 set-manager TARGET... set the list of managers to TARGET...\n\
321 Physical Switch commands:\n\
322 add-ps PS create a new physical switch named PS\n\
323 del-ps PS delete PS and all of its ports\n\
324 list-ps print the names of all the physical switches\n\
325 ps-exists PS exit 2 if PS does not exist\n\
328 list-ports PS print the names of all the ports on PS\n\
329 add-port PS PORT add network device PORT to PS\n\
330 del-port PS PORT delete PORT from PS\n\
332 Logical Switch commands:\n\
333 add-ls LS create a new logical switch named LS\n\
334 del-ls LS delete LS and all of its ports\n\
335 list-ls print the names of all the logical switches\n\
336 ls-exists LS exit 2 if LS does not exist\n\
337 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
338 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
339 list-bindings PS PORT list bindings for PORT on PS\n\
340 set-replication-mode LS MODE set replication mode on LS\n\
341 get-replication-mode LS get replication mode on LS\n\
343 Logical Router commands:\n\
344 add-lr LR create a new logical router named LR\n\
345 del-lr LR delete LR\n\
346 list-lr print the names of all the logical routers\n\
347 lr-exists LR exit 2 if LR does not exist\n\
349 MAC binding commands:\n\
350 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
351 del-ucast-local LS MAC del ucast local entry from LS\n\
352 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
353 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
354 clear-local-macs LS clear local mac entries\n\
355 list-local-macs LS list local mac entries\n\
356 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
357 del-ucast-remote LS MAC del ucast remote entry from LS\n\
358 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
359 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
360 clear-remote-macs LS clear remote mac entries\n\
361 list-remote-macs LS list remote mac entries\n\
367 --db=DATABASE connect to DATABASE\n\
369 -t, --timeout=SECS wait at most SECS seconds\n\
370 --dry-run do not commit changes to database\n\
371 --oneline print exactly one line of output per command\n",
372 program_name
, program_name
, ctl_get_db_cmd_usage(),
373 ctl_list_db_tables_usage(), ctl_default_db());
377 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
378 stream_usage("database", true, true, false);
381 -h, --help display this help message\n\
382 -V, --version display version information\n");
387 static struct cmd_show_table cmd_show_tables
[] = {
388 {&vteprec_table_global
,
390 {&vteprec_global_col_managers
,
391 &vteprec_global_col_switches
,
396 {&vteprec_table_manager
,
397 &vteprec_manager_col_target
,
398 {&vteprec_manager_col_is_connected
,
404 {&vteprec_table_physical_switch
,
405 &vteprec_physical_switch_col_name
,
406 {&vteprec_physical_switch_col_management_ips
,
407 &vteprec_physical_switch_col_tunnel_ips
,
408 &vteprec_physical_switch_col_ports
},
412 {&vteprec_table_physical_port
,
413 &vteprec_physical_port_col_name
,
414 {&vteprec_physical_port_col_vlan_bindings
,
420 {&vteprec_table_logical_switch
,
421 &vteprec_logical_switch_col_name
,
428 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
431 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
432 struct vtep_ctl_context
{
433 struct ctl_context base
;
435 /* Modifiable state. */
436 const struct vteprec_global
*vtep_global
;
439 /* A cache of the contents of the database.
441 * A command that needs to use any of this information must first
442 * call vtep_ctl_context_populate_cache(). A command that changes
443 * anything that could invalidate the cache must either call
444 * vtep_ctl_context_invalidate_cache() or manually update the cache
445 * to maintain its correctness. */
447 struct shash pswitches
; /* Maps from physical switch name to
448 * struct vtep_ctl_pswitch. */
449 struct shash ports
; /* Maps from port name to struct vtep_ctl_port. */
451 struct shash lswitches
; /* Maps from logical switch name to
452 * struct vtep_ctl_lswitch. */
453 struct shash plocs
; /* Maps from "<encap>+<dst_ip>" to
454 * struct vteprec_physical_locator. */
455 struct shash lrouters
; /* Maps from logical router name to
456 * struct vtep_ctl_lrouter. */
459 /* Casts 'base' into 'struct vtep_ctl_context'. */
460 static struct vtep_ctl_context
*
461 vtep_ctl_context_cast(struct ctl_context
*base
)
463 return CONTAINER_OF(base
, struct vtep_ctl_context
, base
);
466 struct vtep_ctl_pswitch
{
467 const struct vteprec_physical_switch
*ps_cfg
;
469 struct ovs_list ports
; /* Contains "struct vteprec_physical_port"s. */
472 struct vtep_ctl_port
{
473 struct ovs_list ports_node
; /* In struct vtep_ctl_pswitch's 'ports' list. */
474 const struct vteprec_physical_port
*port_cfg
;
475 struct vtep_ctl_pswitch
*ps
;
476 struct shash bindings
; /* Maps from vlan to vtep_ctl_lswitch. */
479 struct vtep_ctl_lswitch
{
480 const struct vteprec_logical_switch
*ls_cfg
;
482 struct shash ucast_local
; /* Maps from mac to vteprec_ucast_macs_local. */
483 struct shash ucast_remote
; /* Maps from mac to vteprec_ucast_macs_remote.*/
484 struct shash mcast_local
; /* Maps from mac to vtep_ctl_mcast_mac. */
485 struct shash mcast_remote
; /* Maps from mac to vtep_ctl_mcast_mac. */
488 struct vtep_ctl_lrouter
{
489 const struct vteprec_logical_router
*lr_cfg
;
493 struct vtep_ctl_mcast_mac
{
494 const struct vteprec_mcast_macs_local
*local_cfg
;
495 const struct vteprec_mcast_macs_remote
*remote_cfg
;
497 const struct vteprec_physical_locator_set
*ploc_set_cfg
;
498 struct ovs_list locators
; /* Contains 'vtep_ctl_ploc's. */
501 struct vtep_ctl_ploc
{
502 struct ovs_list locators_node
; /* In struct vtep_ctl_ploc_set's 'locators'
504 const struct vteprec_physical_locator
*ploc_cfg
;
508 verify_ports(struct vtep_ctl_context
*vtepctl_ctx
)
510 if (!vtepctl_ctx
->verified_ports
) {
511 const struct vteprec_physical_switch
*ps
;
513 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
514 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps
, vtepctl_ctx
->base
.idl
) {
515 vteprec_physical_switch_verify_ports(ps
);
518 vtepctl_ctx
->verified_ports
= true;
522 static struct vtep_ctl_port
*
523 add_port_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
524 struct vtep_ctl_pswitch
*ps
,
525 struct vteprec_physical_port
*port_cfg
)
527 char *cache_name
= xasprintf("%s+%s", ps
->name
, port_cfg
->name
);
528 struct vtep_ctl_port
*port
;
530 port
= xmalloc(sizeof *port
);
531 ovs_list_push_back(&ps
->ports
, &port
->ports_node
);
532 port
->port_cfg
= port_cfg
;
534 shash_add(&vtepctl_ctx
->ports
, cache_name
, port
);
536 shash_init(&port
->bindings
);
542 del_cached_port(struct vtep_ctl_context
*vtepctl_ctx
,
543 struct vtep_ctl_port
*port
)
545 char *cache_name
= xasprintf("%s+%s", port
->ps
->name
, port
->port_cfg
->name
);
547 ovs_list_remove(&port
->ports_node
);
548 shash_find_and_delete(&vtepctl_ctx
->ports
, cache_name
);
549 vteprec_physical_port_delete(port
->port_cfg
);
550 shash_destroy(&port
->bindings
);
556 add_pswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
557 struct vteprec_physical_switch
*ps_cfg
)
559 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
561 ps
->name
= xstrdup(ps_cfg
->name
);
562 ovs_list_init(&ps
->ports
);
563 shash_add(&vtepctl_ctx
->pswitches
, ps
->name
, ps
);
567 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
568 const struct vteprec_physical_switch
*ps
)
570 struct vteprec_physical_switch
**pswitches
;
573 pswitches
= xmalloc(sizeof *vtep_global
->switches
574 * vtep_global
->n_switches
);
575 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
576 if (vtep_global
->switches
[i
] != ps
) {
577 pswitches
[n
++] = vtep_global
->switches
[i
];
580 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
585 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
587 ovs_assert(ovs_list_is_empty(&ps
->ports
));
589 vteprec_physical_switch_delete(ps
->ps_cfg
);
590 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
592 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
597 static struct vtep_ctl_lswitch
*
598 add_lswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
599 const struct vteprec_logical_switch
*ls_cfg
)
601 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
603 ls
->name
= xstrdup(ls_cfg
->name
);
604 shash_add(&vtepctl_ctx
->lswitches
, ls
->name
, ls
);
605 shash_init(&ls
->ucast_local
);
606 shash_init(&ls
->ucast_remote
);
607 shash_init(&ls
->mcast_local
);
608 shash_init(&ls
->mcast_remote
);
613 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
616 vteprec_logical_switch_delete(ls
->ls_cfg
);
618 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
624 commit_ls_bindings(struct vtep_ctl_port
*port
)
626 struct vteprec_logical_switch
**binding_values
;
627 int64_t *binding_keys
;
629 struct shash_node
*node
;
632 n_bindings
= shash_count(&port
->bindings
);
633 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
634 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
637 SHASH_FOR_EACH(node
, &port
->bindings
) {
638 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
640 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
641 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
645 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
646 binding_keys
, binding_values
,
648 free(binding_values
);
653 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
655 struct vtep_ctl_lswitch
*ls
)
657 if (shash_find(&port
->bindings
, vlan
)) {
658 ctl_fatal("multiple bindings for vlan %s", vlan
);
661 shash_add(&port
->bindings
, vlan
, ls
);
665 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
667 if (!shash_find(&port
->bindings
, vlan
)) {
668 ctl_fatal("no binding for vlan %s", vlan
);
671 shash_find_and_delete(&port
->bindings
, vlan
);
674 static struct vtep_ctl_lrouter
*
675 add_lrouter_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
676 const struct vteprec_logical_router
*lr_cfg
)
678 struct vtep_ctl_lrouter
*lr
= xmalloc(sizeof *lr
);
680 lr
->name
= xstrdup(lr_cfg
->name
);
681 shash_add(&vtepctl_ctx
->lrouters
, lr
->name
, lr
);
686 del_cached_lrouter(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lrouter
*lr
)
689 vteprec_logical_router_delete(lr
->lr_cfg
);
691 shash_find_and_delete(&ctx
->lrouters
, lr
->name
);
696 static struct vteprec_physical_locator
*
697 find_ploc(struct vtep_ctl_context
*vtepctl_ctx
, const char *encap
,
700 struct vteprec_physical_locator
*ploc
;
701 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
703 ovs_assert(vtepctl_ctx
->cache_valid
);
705 ploc
= shash_find_data(&vtepctl_ctx
->plocs
, name
);
712 add_ploc_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
713 struct vteprec_physical_locator
*ploc
)
715 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
716 struct vteprec_physical_locator
*orig_ploc
;
718 orig_ploc
= find_ploc(vtepctl_ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
720 shash_add(&vtepctl_ctx
->plocs
, name
, ploc
);
727 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
728 struct vteprec_physical_locator
*ploc_cfg
)
730 struct vtep_ctl_ploc
*ploc
;
732 ploc
= xmalloc(sizeof *ploc
);
733 ploc
->ploc_cfg
= ploc_cfg
;
734 ovs_list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
738 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
739 struct vteprec_physical_locator
*ploc_cfg
)
741 struct vtep_ctl_ploc
*ploc
;
743 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
744 if (ploc
->ploc_cfg
== ploc_cfg
) {
745 ovs_list_remove(&ploc
->locators_node
);
752 static struct vtep_ctl_mcast_mac
*
753 add_mcast_mac_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
754 struct vtep_ctl_lswitch
*ls
, const char *mac
,
755 struct vteprec_physical_locator_set
*ploc_set_cfg
,
758 struct vtep_ctl_mcast_mac
*mcast_mac
;
759 struct shash
*mcast_shash
;
762 mcast_mac
= xmalloc(sizeof *mcast_mac
);
763 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
765 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
766 ovs_list_init(&mcast_mac
->locators
);
767 shash_add(mcast_shash
, mac
, mcast_mac
);
769 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
770 struct vteprec_physical_locator
*ploc_cfg
;
772 ploc_cfg
= ploc_set_cfg
->locators
[i
];
773 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
774 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
781 vtep_ctl_context_invalidate_cache(struct ctl_context
*ctx
)
783 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
784 struct shash_node
*node
;
786 if (!vtepctl_ctx
->cache_valid
) {
789 vtepctl_ctx
->cache_valid
= false;
791 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
792 struct vtep_ctl_pswitch
*ps
= node
->data
;
796 shash_destroy(&vtepctl_ctx
->pswitches
);
798 SHASH_FOR_EACH (node
, &vtepctl_ctx
->ports
) {
799 struct vtep_ctl_port
*port
= node
->data
;
800 shash_destroy(&port
->bindings
);
802 shash_destroy_free_data(&vtepctl_ctx
->ports
);
804 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
805 struct vtep_ctl_lswitch
*ls
= node
->data
;
806 struct shash_node
*node2
, *next_node2
;
808 shash_destroy(&ls
->ucast_local
);
809 shash_destroy(&ls
->ucast_remote
);
811 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
812 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
813 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
815 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
816 &mcast_mac
->locators
) {
821 shash_destroy(&ls
->mcast_local
);
823 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
824 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
825 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
827 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
828 &mcast_mac
->locators
) {
833 shash_destroy(&ls
->mcast_remote
);
838 shash_destroy(&vtepctl_ctx
->lswitches
);
839 shash_destroy(&vtepctl_ctx
->plocs
);
841 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
842 struct vtep_ctl_lrouter
*lr
= node
->data
;
846 shash_destroy(&vtepctl_ctx
->lrouters
);
850 pre_get_info(struct ctl_context
*ctx
)
852 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
854 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
855 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
856 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_tunnels
);
858 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
859 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
861 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
862 ovsdb_idl_add_column(ctx
->idl
,
863 &vteprec_logical_switch_col_replication_mode
);
865 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_router_col_name
);
867 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
868 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
869 ovsdb_idl_add_column(ctx
->idl
,
870 &vteprec_ucast_macs_local_col_logical_switch
);
872 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
873 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
874 ovsdb_idl_add_column(ctx
->idl
,
875 &vteprec_ucast_macs_remote_col_logical_switch
);
877 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
878 ovsdb_idl_add_column(ctx
->idl
,
879 &vteprec_mcast_macs_local_col_locator_set
);
880 ovsdb_idl_add_column(ctx
->idl
,
881 &vteprec_mcast_macs_local_col_logical_switch
);
883 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
884 ovsdb_idl_add_column(ctx
->idl
,
885 &vteprec_mcast_macs_remote_col_locator_set
);
886 ovsdb_idl_add_column(ctx
->idl
,
887 &vteprec_mcast_macs_remote_col_logical_switch
);
889 ovsdb_idl_add_column(ctx
->idl
,
890 &vteprec_physical_locator_set_col_locators
);
892 ovsdb_idl_add_column(ctx
->idl
,
893 &vteprec_physical_locator_col_dst_ip
);
894 ovsdb_idl_add_column(ctx
->idl
,
895 &vteprec_physical_locator_col_encapsulation_type
);
897 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_local
);
898 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_remote
);
902 vtep_ctl_context_populate_cache(struct ctl_context
*ctx
)
904 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
905 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
906 const struct vteprec_logical_switch
*ls_cfg
;
907 const struct vteprec_logical_router
*lr_cfg
;
908 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
909 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
910 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
911 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
912 const struct vteprec_tunnel
*tunnel_cfg
;
913 struct sset pswitches
, ports
, lswitches
;
914 struct sset lrouters
;
917 if (vtepctl_ctx
->cache_valid
) {
918 /* Cache is already populated. */
921 vtepctl_ctx
->cache_valid
= true;
922 shash_init(&vtepctl_ctx
->pswitches
);
923 shash_init(&vtepctl_ctx
->ports
);
924 shash_init(&vtepctl_ctx
->lswitches
);
925 shash_init(&vtepctl_ctx
->plocs
);
926 shash_init(&vtepctl_ctx
->lrouters
);
928 sset_init(&pswitches
);
930 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
931 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
934 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
935 VLOG_WARN("%s: database contains duplicate physical switch name",
939 add_pswitch_to_cache(vtepctl_ctx
, ps_cfg
);
941 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
942 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
944 if (!sset_add(&ports
, port_cfg
->name
)) {
945 /* Duplicate port name. (We will warn about that later.) */
950 sset_destroy(&pswitches
);
951 sset_destroy(&ports
);
953 sset_init(&lswitches
);
954 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
955 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
956 VLOG_WARN("%s: database contains duplicate logical switch name",
960 add_lswitch_to_cache(vtepctl_ctx
, ls_cfg
);
962 sset_destroy(&lswitches
);
964 sset_init(&lrouters
);
965 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg
, ctx
->idl
) {
966 if (!sset_add(&lrouters
, lr_cfg
->name
)) {
967 VLOG_WARN("%s: database contains duplicate logical router name",
971 add_lrouter_to_cache(vtepctl_ctx
, lr_cfg
);
973 sset_destroy(&lrouters
);
975 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
976 struct vtep_ctl_lswitch
*ls
;
978 if (!ucast_local_cfg
->logical_switch
) {
981 ls
= find_lswitch(vtepctl_ctx
, ucast_local_cfg
->logical_switch
->name
,
987 if (ucast_local_cfg
->locator
) {
988 add_ploc_to_cache(vtepctl_ctx
, ucast_local_cfg
->locator
);
991 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
994 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
995 struct vtep_ctl_lswitch
*ls
;
997 if (!ucast_remote_cfg
->logical_switch
) {
1000 ls
= find_lswitch(vtepctl_ctx
, ucast_remote_cfg
->logical_switch
->name
,
1006 if (ucast_remote_cfg
->locator
) {
1007 add_ploc_to_cache(vtepctl_ctx
, ucast_remote_cfg
->locator
);
1010 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
1013 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
1014 struct vtep_ctl_mcast_mac
*mcast_mac
;
1015 struct vtep_ctl_lswitch
*ls
;
1017 if (!mcast_local_cfg
->logical_switch
) {
1020 ls
= find_lswitch(vtepctl_ctx
, mcast_local_cfg
->logical_switch
->name
,
1026 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_local_cfg
->MAC
,
1027 mcast_local_cfg
->locator_set
,
1029 mcast_mac
->local_cfg
= mcast_local_cfg
;
1032 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
1033 struct vtep_ctl_mcast_mac
*mcast_mac
;
1034 struct vtep_ctl_lswitch
*ls
;
1036 if (!mcast_remote_cfg
->logical_switch
) {
1039 ls
= find_lswitch(vtepctl_ctx
, mcast_remote_cfg
->logical_switch
->name
,
1045 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_remote_cfg
->MAC
,
1046 mcast_remote_cfg
->locator_set
,
1048 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
1051 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg
, ctx
->idl
) {
1052 if (tunnel_cfg
->local
) {
1053 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->local
);
1055 if (tunnel_cfg
->remote
) {
1056 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->remote
);
1060 sset_init(&pswitches
);
1061 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1062 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1063 struct vtep_ctl_pswitch
*ps
;
1066 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1069 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, ps_cfg
->name
);
1070 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1071 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1072 struct vtep_ctl_port
*port
;
1075 port
= shash_find_data(&vtepctl_ctx
->ports
, port_cfg
->name
);
1077 if (port_cfg
== port
->port_cfg
) {
1078 VLOG_WARN("%s: port is in multiple physical switches "
1080 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1082 /* Log as an error because this violates the database's
1083 * uniqueness constraints, so the database server shouldn't
1084 * have allowed it. */
1085 VLOG_ERR("%s: database contains duplicate port name",
1091 port
= add_port_to_cache(vtepctl_ctx
, ps
, port_cfg
);
1093 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
1094 struct vtep_ctl_lswitch
*ls
;
1097 vlan
= xasprintf("%"PRId64
, port_cfg
->key_vlan_bindings
[k
]);
1098 if (shash_find(&port
->bindings
, vlan
)) {
1099 ctl_fatal("multiple bindings for vlan %s", vlan
);
1102 ls_cfg
= port_cfg
->value_vlan_bindings
[k
];
1103 ls
= find_lswitch(vtepctl_ctx
, ls_cfg
->name
, true);
1105 shash_add_nocopy(&port
->bindings
, vlan
, ls
);
1109 sset_destroy(&pswitches
);
1112 static struct vtep_ctl_pswitch
*
1113 find_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, const char *name
, bool must_exist
)
1115 struct vtep_ctl_pswitch
*ps
;
1117 ovs_assert(vtepctl_ctx
->cache_valid
);
1119 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, name
);
1120 if (must_exist
&& !ps
) {
1121 ctl_fatal("no physical switch named %s", name
);
1123 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
1127 static struct vtep_ctl_port
*
1128 find_port(struct vtep_ctl_context
*vtepctl_ctx
, const char *ps_name
,
1129 const char *port_name
, bool must_exist
)
1131 char *cache_name
= xasprintf("%s+%s", ps_name
, port_name
);
1132 struct vtep_ctl_port
*port
;
1134 ovs_assert(vtepctl_ctx
->cache_valid
);
1136 port
= shash_find_data(&vtepctl_ctx
->ports
, cache_name
);
1137 if (port
&& !strcmp(port_name
, port
->ps
->name
)) {
1141 if (must_exist
&& !port
) {
1142 ctl_fatal("no port named %s", port_name
);
1144 verify_ports(vtepctl_ctx
);
1149 pswitch_insert_port(const struct vteprec_physical_switch
*ps
,
1150 struct vteprec_physical_port
*port
)
1152 struct vteprec_physical_port
**ports
;
1155 ports
= xmalloc(sizeof *ps
->ports
* (ps
->n_ports
+ 1));
1156 for (i
= 0; i
< ps
->n_ports
; i
++) {
1157 ports
[i
] = ps
->ports
[i
];
1159 ports
[ps
->n_ports
] = port
;
1160 vteprec_physical_switch_set_ports(ps
, ports
, ps
->n_ports
+ 1);
1165 pswitch_delete_port(const struct vteprec_physical_switch
*ps
,
1166 const struct vteprec_physical_port
*port
)
1168 struct vteprec_physical_port
**ports
;
1171 ports
= xmalloc(sizeof *ps
->ports
* ps
->n_ports
);
1172 for (i
= n
= 0; i
< ps
->n_ports
; i
++) {
1173 if (ps
->ports
[i
] != port
) {
1174 ports
[n
++] = ps
->ports
[i
];
1177 vteprec_physical_switch_set_ports(ps
, ports
, n
);
1182 vtep_insert_pswitch(const struct vteprec_global
*vtep_global
,
1183 struct vteprec_physical_switch
*ps
)
1185 struct vteprec_physical_switch
**pswitches
;
1188 pswitches
= xmalloc(sizeof *vtep_global
->switches
1189 * (vtep_global
->n_switches
+ 1));
1190 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1191 pswitches
[i
] = vtep_global
->switches
[i
];
1193 pswitches
[vtep_global
->n_switches
] = ps
;
1194 vteprec_global_set_switches(vtep_global
, pswitches
,
1195 vtep_global
->n_switches
+ 1);
1200 cmd_add_ps(struct ctl_context
*ctx
)
1202 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1203 const char *ps_name
= ctx
->argv
[1];
1204 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1205 struct vteprec_physical_switch
*ps
;
1207 vtep_ctl_context_populate_cache(ctx
);
1208 if (find_pswitch(vtepctl_ctx
, ps_name
, false)) {
1210 ctl_fatal("cannot create physical switch %s because it "
1211 "already exists", ps_name
);
1216 ps
= vteprec_physical_switch_insert(ctx
->txn
);
1217 vteprec_physical_switch_set_name(ps
, ps_name
);
1219 vtep_insert_pswitch(vtepctl_ctx
->vtep_global
, ps
);
1221 vtep_ctl_context_invalidate_cache(ctx
);
1225 del_port(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_port
*port
)
1227 pswitch_delete_port(port
->ps
->ps_cfg
, port
->port_cfg
);
1228 del_cached_port(vtepctl_ctx
, port
);
1232 del_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_pswitch
*ps
)
1234 struct vtep_ctl_port
*port
, *next_port
;
1236 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &ps
->ports
) {
1237 del_port(vtepctl_ctx
, port
);
1240 del_cached_pswitch(vtepctl_ctx
, ps
);
1244 cmd_del_ps(struct ctl_context
*ctx
)
1246 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1247 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1248 struct vtep_ctl_pswitch
*ps
;
1250 vtep_ctl_context_populate_cache(ctx
);
1251 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1253 del_pswitch(vtepctl_ctx
, ps
);
1258 output_sorted(struct svec
*svec
, struct ds
*output
)
1264 SVEC_FOR_EACH (i
, name
, svec
) {
1265 ds_put_format(output
, "%s\n", name
);
1270 cmd_list_ps(struct ctl_context
*ctx
)
1272 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1273 struct shash_node
*node
;
1274 struct svec pswitches
;
1276 vtep_ctl_context_populate_cache(ctx
);
1278 svec_init(&pswitches
);
1279 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
1280 struct vtep_ctl_pswitch
*ps
= node
->data
;
1282 svec_add(&pswitches
, ps
->name
);
1284 output_sorted(&pswitches
, &ctx
->output
);
1285 svec_destroy(&pswitches
);
1289 cmd_ps_exists(struct ctl_context
*ctx
)
1291 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1293 vtep_ctl_context_populate_cache(ctx
);
1294 if (!find_pswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1300 cmd_list_ports(struct ctl_context
*ctx
)
1302 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1303 struct vtep_ctl_pswitch
*ps
;
1304 struct vtep_ctl_port
*port
;
1307 vtep_ctl_context_populate_cache(ctx
);
1308 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1309 vteprec_physical_switch_verify_ports(ps
->ps_cfg
);
1312 LIST_FOR_EACH (port
, ports_node
, &ps
->ports
) {
1313 if (strcmp(port
->port_cfg
->name
, ps
->name
)) {
1314 svec_add(&ports
, port
->port_cfg
->name
);
1317 output_sorted(&ports
, &ctx
->output
);
1318 svec_destroy(&ports
);
1322 add_port(struct ctl_context
*ctx
, const char *ps_name
,
1323 const char *port_name
, bool may_exist
)
1325 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1326 struct vtep_ctl_port
*vtep_ctl_port
;
1327 struct vtep_ctl_pswitch
*ps
;
1328 struct vteprec_physical_port
*port
;
1330 vtep_ctl_context_populate_cache(ctx
);
1332 vtep_ctl_port
= find_port(vtepctl_ctx
, ps_name
, port_name
, false);
1333 if (vtep_ctl_port
) {
1335 ctl_fatal("cannot create a port named %s on %s because a "
1336 "port with that name already exists",
1337 port_name
, ps_name
);
1342 ps
= find_pswitch(vtepctl_ctx
, ps_name
, true);
1344 port
= vteprec_physical_port_insert(ctx
->txn
);
1345 vteprec_physical_port_set_name(port
, port_name
);
1347 pswitch_insert_port(ps
->ps_cfg
, port
);
1349 add_port_to_cache(vtepctl_ctx
, ps
, port
);
1353 cmd_add_port(struct ctl_context
*ctx
)
1355 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1357 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
);
1361 cmd_del_port(struct ctl_context
*ctx
)
1363 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1364 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1365 struct vtep_ctl_port
*port
;
1367 vtep_ctl_context_populate_cache(ctx
);
1369 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], must_exist
);
1371 if (ctx
->argc
== 3) {
1372 struct vtep_ctl_pswitch
*ps
;
1374 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1375 if (port
->ps
!= ps
) {
1376 ctl_fatal("physical switch %s does not have a port %s",
1377 ctx
->argv
[1], ctx
->argv
[2]);
1381 del_port(vtepctl_ctx
, port
);
1385 static struct vtep_ctl_lswitch
*
1386 find_lswitch(struct vtep_ctl_context
*vtepctl_ctx
,
1387 const char *name
, bool must_exist
)
1389 struct vtep_ctl_lswitch
*ls
;
1391 ovs_assert(vtepctl_ctx
->cache_valid
);
1393 ls
= shash_find_data(&vtepctl_ctx
->lswitches
, name
);
1394 if (must_exist
&& !ls
) {
1395 ctl_fatal("no logical switch named %s", name
);
1401 cmd_add_ls(struct ctl_context
*ctx
)
1403 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1404 const char *ls_name
= ctx
->argv
[1];
1405 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1406 struct vteprec_logical_switch
*ls
;
1408 vtep_ctl_context_populate_cache(ctx
);
1409 if (find_lswitch(vtepctl_ctx
, ls_name
, false)) {
1411 ctl_fatal("cannot create logical switch %s because it "
1412 "already exists", ls_name
);
1417 ls
= vteprec_logical_switch_insert(ctx
->txn
);
1418 vteprec_logical_switch_set_name(ls
, ls_name
);
1420 vtep_ctl_context_invalidate_cache(ctx
);
1424 del_lswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lswitch
*ls
)
1426 del_cached_lswitch(vtepctl_ctx
, ls
);
1430 cmd_del_ls(struct ctl_context
*ctx
)
1432 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1433 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1434 struct vtep_ctl_lswitch
*ls
;
1436 vtep_ctl_context_populate_cache(ctx
);
1437 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1439 del_lswitch(vtepctl_ctx
, ls
);
1444 cmd_list_ls(struct ctl_context
*ctx
)
1446 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1447 struct shash_node
*node
;
1448 struct svec lswitches
;
1450 vtep_ctl_context_populate_cache(ctx
);
1452 svec_init(&lswitches
);
1453 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
1454 struct vtep_ctl_lswitch
*ls
= node
->data
;
1456 svec_add(&lswitches
, ls
->name
);
1458 output_sorted(&lswitches
, &ctx
->output
);
1459 svec_destroy(&lswitches
);
1463 cmd_ls_exists(struct ctl_context
*ctx
)
1465 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1467 vtep_ctl_context_populate_cache(ctx
);
1468 if (!find_lswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1474 cmd_list_bindings(struct ctl_context
*ctx
)
1476 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1477 const struct shash_node
*node
;
1478 struct vtep_ctl_port
*port
;
1479 struct svec bindings
;
1481 vtep_ctl_context_populate_cache(ctx
);
1482 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1484 svec_init(&bindings
);
1485 SHASH_FOR_EACH (node
, &port
->bindings
) {
1486 struct vtep_ctl_lswitch
*lswitch
= node
->data
;
1489 binding
= xasprintf("%04lld %s", strtoll(node
->name
, NULL
, 0),
1491 svec_add_nocopy(&bindings
, binding
);
1493 output_sorted(&bindings
, &ctx
->output
);
1494 svec_destroy(&bindings
);
1498 cmd_bind_ls(struct ctl_context
*ctx
)
1500 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1501 struct vtep_ctl_lswitch
*ls
;
1502 struct vtep_ctl_port
*port
;
1505 vtep_ctl_context_populate_cache(ctx
);
1507 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1508 vlan
= ctx
->argv
[3];
1509 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[4], true);
1511 add_ls_binding_to_cache(port
, vlan
, ls
);
1512 commit_ls_bindings(port
);
1514 vtep_ctl_context_invalidate_cache(ctx
);
1518 cmd_unbind_ls(struct ctl_context
*ctx
)
1520 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1521 struct vtep_ctl_port
*port
;
1524 vtep_ctl_context_populate_cache(ctx
);
1526 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1527 vlan
= ctx
->argv
[3];
1529 del_cached_ls_binding(port
, vlan
);
1530 commit_ls_bindings(port
);
1532 vtep_ctl_context_invalidate_cache(ctx
);
1536 cmd_set_replication_mode(struct ctl_context
*ctx
)
1538 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1539 struct vtep_ctl_lswitch
*ls
;
1540 const char *ls_name
= ctx
->argv
[1];
1542 vtep_ctl_context_populate_cache(ctx
);
1544 if (strcmp(ctx
->argv
[2], "service_node") &&
1545 strcmp(ctx
->argv
[2], "source_node")) {
1546 ctl_fatal("Replication mode must be 'service_node' or 'source_node'");
1549 ls
= find_lswitch(vtepctl_ctx
, ls_name
, true);
1550 vteprec_logical_switch_set_replication_mode(ls
->ls_cfg
, ctx
->argv
[2]);
1552 vtep_ctl_context_invalidate_cache(ctx
);
1556 cmd_get_replication_mode(struct ctl_context
*ctx
)
1558 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1559 struct vtep_ctl_lswitch
*ls
;
1560 const char *ls_name
= ctx
->argv
[1];
1562 vtep_ctl_context_populate_cache(ctx
);
1564 ls
= find_lswitch(vtepctl_ctx
, ls_name
, true);
1565 ds_put_format(&ctx
->output
, "%s\n", ls
->ls_cfg
->replication_mode
);
1568 static struct vtep_ctl_lrouter
*
1569 find_lrouter(struct vtep_ctl_context
*vtepctl_ctx
,
1570 const char *name
, bool must_exist
)
1572 struct vtep_ctl_lrouter
*lr
;
1574 ovs_assert(vtepctl_ctx
->cache_valid
);
1576 lr
= shash_find_data(&vtepctl_ctx
->lrouters
, name
);
1577 if (must_exist
&& !lr
) {
1578 ctl_fatal("no logical router named %s", name
);
1584 cmd_add_lr(struct ctl_context
*ctx
)
1586 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1587 const char *lr_name
= ctx
->argv
[1];
1588 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1589 struct vteprec_logical_router
*lr
;
1591 vtep_ctl_context_populate_cache(ctx
);
1592 if (find_lrouter(vtepctl_ctx
, lr_name
, false)) {
1594 ctl_fatal("cannot create logical router %s because it "
1595 "already exists", lr_name
);
1600 lr
= vteprec_logical_router_insert(ctx
->txn
);
1601 vteprec_logical_router_set_name(lr
, lr_name
);
1603 vtep_ctl_context_invalidate_cache(ctx
);
1607 del_lrouter(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lrouter
*lr
)
1609 del_cached_lrouter(vtepctl_ctx
, lr
);
1613 cmd_del_lr(struct ctl_context
*ctx
)
1615 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1616 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1617 struct vtep_ctl_lrouter
*lr
;
1619 vtep_ctl_context_populate_cache(ctx
);
1620 lr
= find_lrouter(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1622 del_lrouter(vtepctl_ctx
, lr
);
1627 cmd_list_lr(struct ctl_context
*ctx
)
1629 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1630 struct shash_node
*node
;
1631 struct svec lrouters
;
1633 vtep_ctl_context_populate_cache(ctx
);
1635 svec_init(&lrouters
);
1636 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
1637 struct vtep_ctl_lrouter
*lr
= node
->data
;
1639 svec_add(&lrouters
, lr
->name
);
1641 output_sorted(&lrouters
, &ctx
->output
);
1642 svec_destroy(&lrouters
);
1646 cmd_lr_exists(struct ctl_context
*ctx
)
1648 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1650 vtep_ctl_context_populate_cache(ctx
);
1651 if (!find_lrouter(vtepctl_ctx
, ctx
->argv
[1], false)) {
1657 add_ucast_entry(struct ctl_context
*ctx
, bool local
)
1659 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1660 struct vtep_ctl_lswitch
*ls
;
1664 struct vteprec_physical_locator
*ploc_cfg
;
1666 vtep_ctl_context_populate_cache(ctx
);
1668 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1671 if (ctx
->argc
== 4) {
1672 encap
= "vxlan_over_ipv4";
1673 dst_ip
= ctx
->argv
[3];
1675 encap
= ctx
->argv
[3];
1676 dst_ip
= ctx
->argv
[4];
1679 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1681 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1682 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1683 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1685 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1689 struct vteprec_ucast_macs_local
*ucast_cfg
;
1691 ucast_cfg
= shash_find_data(&ls
->ucast_local
, mac
);
1693 ucast_cfg
= vteprec_ucast_macs_local_insert(ctx
->txn
);
1694 vteprec_ucast_macs_local_set_MAC(ucast_cfg
, mac
);
1695 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1696 shash_add(&ls
->ucast_local
, mac
, ucast_cfg
);
1698 vteprec_ucast_macs_local_set_locator(ucast_cfg
, ploc_cfg
);
1700 struct vteprec_ucast_macs_remote
*ucast_cfg
;
1702 ucast_cfg
= shash_find_data(&ls
->ucast_remote
, mac
);
1704 ucast_cfg
= vteprec_ucast_macs_remote_insert(ctx
->txn
);
1705 vteprec_ucast_macs_remote_set_MAC(ucast_cfg
, mac
);
1706 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1707 shash_add(&ls
->ucast_remote
, mac
, ucast_cfg
);
1709 vteprec_ucast_macs_remote_set_locator(ucast_cfg
, ploc_cfg
);
1712 vtep_ctl_context_invalidate_cache(ctx
);
1716 cmd_add_ucast_local(struct ctl_context
*ctx
)
1718 add_ucast_entry(ctx
, true);
1722 cmd_add_ucast_remote(struct ctl_context
*ctx
)
1724 add_ucast_entry(ctx
, false);
1728 del_ucast_entry(struct ctl_context
*ctx
, bool local
)
1730 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1731 struct vtep_ctl_lswitch
*ls
;
1732 struct shash
*ucast_shash
;
1733 struct shash_node
*node
;
1735 vtep_ctl_context_populate_cache(ctx
);
1737 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1738 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1740 node
= shash_find(ucast_shash
, ctx
->argv
[2]);
1746 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1747 vteprec_ucast_macs_local_delete(ucast_cfg
);
1749 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1750 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1752 shash_delete(ucast_shash
, node
);
1754 vtep_ctl_context_invalidate_cache(ctx
);
1758 cmd_del_ucast_local(struct ctl_context
*ctx
)
1760 del_ucast_entry(ctx
, true);
1764 cmd_del_ucast_remote(struct ctl_context
*ctx
)
1766 del_ucast_entry(ctx
, false);
1770 commit_mcast_entries(struct vtep_ctl_mcast_mac
*mcast_mac
)
1772 struct vtep_ctl_ploc
*ploc
;
1773 struct vteprec_physical_locator
**locators
= NULL
;
1777 n_locators
= ovs_list_size(&mcast_mac
->locators
);
1778 ovs_assert(n_locators
);
1780 locators
= xmalloc(n_locators
* sizeof *locators
);
1783 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1784 locators
[i
] = (struct vteprec_physical_locator
*)ploc
->ploc_cfg
;
1788 vteprec_physical_locator_set_set_locators(mcast_mac
->ploc_set_cfg
,
1796 add_mcast_entry(struct ctl_context
*ctx
,
1797 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1798 const char *encap
, const char *dst_ip
, bool local
)
1800 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1801 struct shash
*mcast_shash
;
1802 struct vtep_ctl_mcast_mac
*mcast_mac
;
1803 struct vteprec_physical_locator
*ploc_cfg
;
1804 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1806 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1808 /* Physical locator sets are immutable, so allocate a new one. */
1809 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1811 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1813 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mac
, ploc_set_cfg
,
1817 mcast_mac
->local_cfg
= vteprec_mcast_macs_local_insert(ctx
->txn
);
1818 vteprec_mcast_macs_local_set_MAC(mcast_mac
->local_cfg
, mac
);
1819 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1821 vteprec_mcast_macs_local_set_logical_switch(mcast_mac
->local_cfg
,
1823 mcast_mac
->remote_cfg
= NULL
;
1825 mcast_mac
->remote_cfg
= vteprec_mcast_macs_remote_insert(ctx
->txn
);
1826 vteprec_mcast_macs_remote_set_MAC(mcast_mac
->remote_cfg
, mac
);
1827 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1829 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac
->remote_cfg
,
1831 mcast_mac
->local_cfg
= NULL
;
1834 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1836 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1839 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1844 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1846 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1847 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1848 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1850 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1853 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1854 commit_mcast_entries(mcast_mac
);
1858 del_mcast_entry(struct ctl_context
*ctx
,
1859 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1860 const char *encap
, const char *dst_ip
, bool local
)
1862 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1863 struct vtep_ctl_mcast_mac
*mcast_mac
;
1864 struct shash
*mcast_shash
;
1865 struct vteprec_physical_locator
*ploc_cfg
;
1866 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1868 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1870 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1875 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1877 /* Couldn't find the physical locator, so just ignore. */
1881 /* Physical locator sets are immutable, so allocate a new one. */
1882 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1883 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1885 del_ploc_from_mcast_mac(mcast_mac
, ploc_cfg
);
1886 if (ovs_list_is_empty(&mcast_mac
->locators
)) {
1887 struct shash_node
*node
= shash_find(mcast_shash
, mac
);
1889 vteprec_physical_locator_set_delete(ploc_set_cfg
);
1892 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1894 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1898 shash_delete(mcast_shash
, node
);
1901 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1904 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1907 commit_mcast_entries(mcast_mac
);
1912 add_del_mcast_entry(struct ctl_context
*ctx
, bool add
, bool local
)
1914 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1915 struct vtep_ctl_lswitch
*ls
;
1920 vtep_ctl_context_populate_cache(ctx
);
1922 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1925 if (ctx
->argc
== 4) {
1926 encap
= "vxlan_over_ipv4";
1927 dst_ip
= ctx
->argv
[3];
1929 encap
= ctx
->argv
[3];
1930 dst_ip
= ctx
->argv
[4];
1934 add_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1936 del_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1939 vtep_ctl_context_invalidate_cache(ctx
);
1943 cmd_add_mcast_local(struct ctl_context
*ctx
)
1945 add_del_mcast_entry(ctx
, true, true);
1949 cmd_add_mcast_remote(struct ctl_context
*ctx
)
1951 add_del_mcast_entry(ctx
, true, false);
1955 cmd_del_mcast_local(struct ctl_context
*ctx
)
1957 add_del_mcast_entry(ctx
, false, true);
1961 cmd_del_mcast_remote(struct ctl_context
*ctx
)
1963 add_del_mcast_entry(ctx
, false, false);
1967 clear_macs(struct ctl_context
*ctx
, bool local
)
1969 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1970 struct vtep_ctl_lswitch
*ls
;
1971 const struct shash_node
*node
;
1972 struct shash
*ucast_shash
;
1973 struct shash
*mcast_shash
;
1975 vtep_ctl_context_populate_cache(ctx
);
1976 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1978 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1979 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1981 SHASH_FOR_EACH (node
, ucast_shash
) {
1983 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1984 vteprec_ucast_macs_local_delete(ucast_cfg
);
1986 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1987 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1991 SHASH_FOR_EACH (node
, mcast_shash
) {
1992 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
1994 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1996 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
2000 vtep_ctl_context_invalidate_cache(ctx
);
2004 cmd_clear_local_macs(struct ctl_context
*ctx
)
2006 clear_macs(ctx
, true);
2010 cmd_clear_remote_macs(struct ctl_context
*ctx
)
2012 clear_macs(ctx
, false);
2016 list_macs(struct ctl_context
*ctx
, bool local
)
2018 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2019 struct vtep_ctl_lswitch
*ls
;
2020 const struct shash_node
*node
;
2021 struct shash
*ucast_shash
;
2022 struct svec ucast_macs
;
2023 struct shash
*mcast_shash
;
2024 struct svec mcast_macs
;
2026 vtep_ctl_context_populate_cache(ctx
);
2027 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
2029 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
2030 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
2032 svec_init(&ucast_macs
);
2033 SHASH_FOR_EACH (node
, ucast_shash
) {
2034 struct vteprec_ucast_macs_local
*ucast_local
= node
->data
;
2035 struct vteprec_ucast_macs_remote
*ucast_remote
= node
->data
;
2036 struct vteprec_physical_locator
*ploc_cfg
;
2039 ploc_cfg
= local
? ucast_local
->locator
: ucast_remote
->locator
;
2041 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2042 ploc_cfg
->encapsulation_type
, ploc_cfg
->dst_ip
);
2043 svec_add_nocopy(&ucast_macs
, entry
);
2045 ds_put_format(&ctx
->output
, "ucast-mac-%s\n", local
? "local" : "remote");
2046 output_sorted(&ucast_macs
, &ctx
->output
);
2047 ds_put_char(&ctx
->output
, '\n');
2048 svec_destroy(&ucast_macs
);
2050 svec_init(&mcast_macs
);
2051 SHASH_FOR_EACH (node
, mcast_shash
) {
2052 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
2053 struct vtep_ctl_ploc
*ploc
;
2056 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
2057 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2058 ploc
->ploc_cfg
->encapsulation_type
,
2059 ploc
->ploc_cfg
->dst_ip
);
2060 svec_add_nocopy(&mcast_macs
, entry
);
2063 ds_put_format(&ctx
->output
, "mcast-mac-%s\n", local
? "local" : "remote");
2064 output_sorted(&mcast_macs
, &ctx
->output
);
2065 ds_put_char(&ctx
->output
, '\n');
2066 svec_destroy(&mcast_macs
);
2070 cmd_list_local_macs(struct ctl_context
*ctx
)
2072 list_macs(ctx
, true);
2076 cmd_list_remote_macs(struct ctl_context
*ctx
)
2078 list_macs(ctx
, false);
2082 verify_managers(const struct vteprec_global
*vtep_global
)
2086 vteprec_global_verify_managers(vtep_global
);
2088 for (i
= 0; i
< vtep_global
->n_managers
; ++i
) {
2089 const struct vteprec_manager
*mgr
= vtep_global
->managers
[i
];
2091 vteprec_manager_verify_target(mgr
);
2096 pre_manager(struct ctl_context
*ctx
)
2098 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_managers
);
2099 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_target
);
2103 cmd_get_manager(struct ctl_context
*ctx
)
2105 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2106 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2107 struct svec targets
;
2110 verify_managers(vtep_global
);
2112 /* Print the targets in sorted order for reproducibility. */
2113 svec_init(&targets
);
2115 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2116 svec_add(&targets
, vtep_global
->managers
[i
]->target
);
2119 svec_sort_unique(&targets
);
2120 for (i
= 0; i
< targets
.n
; i
++) {
2121 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2123 svec_destroy(&targets
);
2127 delete_managers(const struct vtep_ctl_context
*vtepctl_ctx
)
2129 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2132 /* Delete Manager rows pointed to by 'managers' column. */
2133 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2134 vteprec_manager_delete(vtep_global
->managers
[i
]);
2137 /* Delete 'Manager' row refs in 'managers' column. */
2138 vteprec_global_set_managers(vtep_global
, NULL
, 0);
2142 cmd_del_manager(struct ctl_context
*ctx
)
2144 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2145 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2147 verify_managers(vtep_global
);
2148 delete_managers(vtepctl_ctx
);
2152 insert_managers(struct vtep_ctl_context
*vtepctl_ctx
, char *targets
[], size_t n
)
2154 struct vteprec_manager
**managers
;
2157 /* Insert each manager in a new row in Manager table. */
2158 managers
= xmalloc(n
* sizeof *managers
);
2159 for (i
= 0; i
< n
; i
++) {
2160 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2161 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2163 managers
[i
] = vteprec_manager_insert(vtepctl_ctx
->base
.txn
);
2164 vteprec_manager_set_target(managers
[i
], targets
[i
]);
2167 /* Store uuids of new Manager rows in 'managers' column. */
2168 vteprec_global_set_managers(vtepctl_ctx
->vtep_global
, managers
, n
);
2173 cmd_set_manager(struct ctl_context
*ctx
)
2175 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2176 const size_t n
= ctx
->argc
- 1;
2178 verify_managers(vtepctl_ctx
->vtep_global
);
2179 delete_managers(vtepctl_ctx
);
2180 insert_managers(vtepctl_ctx
, &ctx
->argv
[1], n
);
2183 /* Parameter commands. */
2184 static const struct ctl_table_class tables
[VTEPREC_N_TABLES
] = {
2185 [VTEPREC_TABLE_LOGICAL_SWITCH
].row_ids
[0]
2186 = {&vteprec_logical_switch_col_name
, NULL
, NULL
},
2188 [VTEPREC_TABLE_MANAGER
].row_ids
[0]
2189 = {&vteprec_manager_col_target
, NULL
, NULL
},
2191 [VTEPREC_TABLE_PHYSICAL_PORT
].row_ids
[0]
2192 = {&vteprec_physical_port_col_name
, NULL
, NULL
},
2194 [VTEPREC_TABLE_PHYSICAL_SWITCH
].row_ids
[0]
2195 = {&vteprec_physical_switch_col_name
, NULL
, NULL
},
2197 [VTEPREC_TABLE_LOGICAL_ROUTER
].row_ids
[0]
2198 = {&vteprec_logical_router_col_name
, NULL
, NULL
},
2203 vtep_ctl_context_init_command(struct vtep_ctl_context
*vtepctl_ctx
,
2204 struct ctl_command
*command
)
2206 ctl_context_init_command(&vtepctl_ctx
->base
, command
);
2207 vtepctl_ctx
->verified_ports
= false;
2212 vtep_ctl_context_init(struct vtep_ctl_context
*vtepctl_ctx
,
2213 struct ctl_command
*command
,
2214 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2215 const struct vteprec_global
*vtep_global
,
2216 struct ovsdb_symbol_table
*symtab
)
2218 ctl_context_init(&vtepctl_ctx
->base
, command
, idl
, txn
, symtab
,
2219 vtep_ctl_context_invalidate_cache
);
2221 vtepctl_ctx
->verified_ports
= false;
2223 vtepctl_ctx
->vtep_global
= vtep_global
;
2224 vtepctl_ctx
->cache_valid
= false;
2228 vtep_ctl_context_done_command(struct vtep_ctl_context
*vtepctl_ctx
,
2229 struct ctl_command
*command
)
2231 ctl_context_done_command(&vtepctl_ctx
->base
, command
);
2235 vtep_ctl_context_done(struct vtep_ctl_context
*vtepctl_ctx
,
2236 struct ctl_command
*command
)
2238 ctl_context_done(&vtepctl_ctx
->base
, command
);
2242 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2243 struct ovsdb_idl
*idl
)
2245 struct ctl_command
*c
;
2247 ovsdb_idl_add_table(idl
, &vteprec_table_global
);
2248 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2249 if (c
->syntax
->prerequisites
) {
2250 struct vtep_ctl_context vtepctl_ctx
;
2252 ds_init(&c
->output
);
2255 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2256 (c
->syntax
->prerequisites
)(&vtepctl_ctx
.base
);
2257 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2259 ovs_assert(!c
->output
.string
);
2260 ovs_assert(!c
->table
);
2266 do_vtep_ctl(const char *args
, struct ctl_command
*commands
,
2267 size_t n_commands
, struct ovsdb_idl
*idl
)
2269 struct ovsdb_idl_txn
*txn
;
2270 const struct vteprec_global
*vtep_global
;
2271 enum ovsdb_idl_txn_status status
;
2272 struct ovsdb_symbol_table
*symtab
;
2273 struct vtep_ctl_context vtepctl_ctx
;
2274 struct ctl_command
*c
;
2275 struct shash_node
*node
;
2278 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2280 ovsdb_idl_txn_set_dry_run(txn
);
2283 ovsdb_idl_txn_add_comment(txn
, "vtep-ctl: %s", args
);
2285 vtep_global
= vteprec_global_first(idl
);
2287 /* XXX add verification that table is empty */
2288 vtep_global
= vteprec_global_insert(txn
);
2291 symtab
= ovsdb_symbol_table_create();
2292 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2293 ds_init(&c
->output
);
2296 vtep_ctl_context_init(&vtepctl_ctx
, NULL
, idl
, txn
, vtep_global
, symtab
);
2297 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2298 vtep_ctl_context_init_command(&vtepctl_ctx
, c
);
2299 if (c
->syntax
->run
) {
2300 (c
->syntax
->run
)(&vtepctl_ctx
.base
);
2302 vtep_ctl_context_done_command(&vtepctl_ctx
, c
);
2304 if (vtepctl_ctx
.base
.try_again
) {
2305 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2309 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2311 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2312 struct ovsdb_symbol
*symbol
= node
->data
;
2313 if (!symbol
->created
) {
2314 ctl_fatal("row id \"%s\" is referenced but never created "
2315 "(e.g. with \"-- --id=%s create ...\")",
2316 node
->name
, node
->name
);
2318 if (!symbol
->strong_ref
) {
2319 if (!symbol
->weak_ref
) {
2320 VLOG_WARN("row id \"%s\" was created but no reference to it "
2321 "was inserted, so it will not actually appear in "
2322 "the database", node
->name
);
2324 VLOG_WARN("row id \"%s\" was created but only a weak "
2325 "reference to it was inserted, so it will not "
2326 "actually appear in the database", node
->name
);
2331 status
= ovsdb_idl_txn_commit_block(txn
);
2332 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2333 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2334 if (c
->syntax
->postprocess
) {
2335 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, txn
, vtep_global
, symtab
);
2336 (c
->syntax
->postprocess
)(&vtepctl_ctx
.base
);
2337 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2341 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2342 ovsdb_idl_txn_destroy(txn
);
2343 txn
= the_idl_txn
= NULL
;
2346 case TXN_UNCOMMITTED
:
2347 case TXN_INCOMPLETE
:
2351 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2352 ctl_fatal("transaction aborted");
2362 ctl_fatal("transaction error: %s", error
);
2364 case TXN_NOT_LOCKED
:
2365 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2366 ctl_fatal("database not locked");
2373 ovsdb_symbol_table_destroy(symtab
);
2375 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2376 struct ds
*ds
= &c
->output
;
2379 table_print(c
->table
, &table_style
);
2380 } else if (oneline
) {
2384 for (j
= 0; j
< ds
->length
; j
++) {
2385 int ch
= ds
->string
[j
];
2388 fputs("\\n", stdout
);
2392 fputs("\\\\", stdout
);
2401 fputs(ds_cstr(ds
), stdout
);
2403 ds_destroy(&c
->output
);
2404 table_destroy(c
->table
);
2407 shash_destroy_free_data(&c
->options
);
2411 ovsdb_idl_destroy(idl
);
2416 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2417 * resources and return so that the caller can try again. */
2419 ovsdb_idl_txn_abort(txn
);
2420 ovsdb_idl_txn_destroy(txn
);
2422 ovsdb_symbol_table_destroy(symtab
);
2423 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2424 ds_destroy(&c
->output
);
2425 table_destroy(c
->table
);
2432 static const struct ctl_command_syntax vtep_commands
[] = {
2433 /* Physical Switch commands. */
2434 {"add-ps", 1, 1, NULL
, pre_get_info
, cmd_add_ps
, NULL
, "--may-exist", RW
},
2435 {"del-ps", 1, 1, NULL
, pre_get_info
, cmd_del_ps
, NULL
, "--if-exists", RW
},
2436 {"list-ps", 0, 0, NULL
, pre_get_info
, cmd_list_ps
, NULL
, "", RO
},
2437 {"ps-exists", 1, 1, NULL
, pre_get_info
, cmd_ps_exists
, NULL
, "", RO
},
2439 /* Port commands. */
2440 {"list-ports", 1, 1, NULL
, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
2441 {"add-port", 2, 2, NULL
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
2443 {"del-port", 2, 2, NULL
, pre_get_info
, cmd_del_port
, NULL
, "--if-exists",
2446 /* Logical Switch commands. */
2447 {"add-ls", 1, 1, NULL
, pre_get_info
, cmd_add_ls
, NULL
, "--may-exist", RW
},
2448 {"del-ls", 1, 1, NULL
, pre_get_info
, cmd_del_ls
, NULL
, "--if-exists", RW
},
2449 {"list-ls", 0, 0, NULL
, pre_get_info
, cmd_list_ls
, NULL
, "", RO
},
2450 {"ls-exists", 1, 1, NULL
, pre_get_info
, cmd_ls_exists
, NULL
, "", RO
},
2451 {"list-bindings", 2, 2, NULL
, pre_get_info
, cmd_list_bindings
, NULL
, "", RO
},
2452 {"bind-ls", 4, 4, NULL
, pre_get_info
, cmd_bind_ls
, NULL
, "", RO
},
2453 {"unbind-ls", 3, 3, NULL
, pre_get_info
, cmd_unbind_ls
, NULL
, "", RO
},
2454 {"set-replication-mode", 2, 2, "LS MODE", pre_get_info
,
2455 cmd_set_replication_mode
, NULL
, "", RW
},
2456 {"get-replication-mode", 1, 1, "LS", pre_get_info
,
2457 cmd_get_replication_mode
, NULL
, "", RO
},
2459 /* Logical Router commands. */
2460 {"add-lr", 1, 1, NULL
, pre_get_info
, cmd_add_lr
, NULL
, "--may-exist", RW
},
2461 {"del-lr", 1, 1, NULL
, pre_get_info
, cmd_del_lr
, NULL
, "--if-exists", RW
},
2462 {"list-lr", 0, 0, NULL
, pre_get_info
, cmd_list_lr
, NULL
, "", RO
},
2463 {"lr-exists", 1, 1, NULL
, pre_get_info
, cmd_lr_exists
, NULL
, "", RO
},
2465 /* MAC binding commands. */
2466 {"add-ucast-local", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_local
, NULL
,
2468 {"del-ucast-local", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_local
, NULL
,
2470 {"add-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_local
, NULL
,
2472 {"del-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_local
, NULL
,
2474 {"clear-local-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_local_macs
, NULL
,
2476 {"list-local-macs", 1, 1, NULL
, pre_get_info
, cmd_list_local_macs
, NULL
,
2478 {"add-ucast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_remote
, NULL
,
2480 {"del-ucast-remote", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_remote
, NULL
,
2482 {"add-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_remote
, NULL
,
2484 {"del-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_remote
, NULL
,
2486 {"clear-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_remote_macs
, NULL
,
2488 {"list-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_list_remote_macs
, NULL
,
2491 /* Manager commands. */
2492 {"get-manager", 0, 0, NULL
, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2493 {"del-manager", 0, 0, NULL
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2494 {"set-manager", 1, INT_MAX
, NULL
, pre_manager
, cmd_set_manager
, NULL
, "",
2497 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2500 /* Registers vsctl and common db commands. */
2502 vtep_ctl_cmd_init(void)
2504 ctl_init(&vteprec_idl_class
, vteprec_table_classes
, tables
,
2505 cmd_show_tables
, vtep_ctl_exit
);
2506 ctl_register_commands(vtep_commands
);