2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
30 #include "db-ctl-base.h"
32 #include "command-line.h"
34 #include "dynamic-string.h"
35 #include "fatal-signal.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-idl.h"
40 #include "poll-loop.h"
43 #include "stream-ssl.h"
47 #include "vtep/vtep-idl.h"
51 #include "openvswitch/vconn.h"
52 #include "openvswitch/vlog.h"
54 VLOG_DEFINE_THIS_MODULE(vtep_ctl
);
56 struct vtep_ctl_context
;
58 /* --db: The database server to contact. */
59 static const char *db
;
61 /* --oneline: Write each command's output as a single line? */
64 /* --dry-run: Do not commit any changes. */
67 /* --timeout: Time to wait for a connection to 'db'. */
70 /* Format for table output. */
71 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
73 /* The IDL we're using and the current transaction, if any.
74 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
75 * Other code should use its context arguments. */
76 static struct ovsdb_idl
*the_idl
;
77 static struct ovsdb_idl_txn
*the_idl_txn
;
79 OVS_NO_RETURN
static void vtep_ctl_exit(int status
);
80 static void vtep_ctl_cmd_init(void);
81 OVS_NO_RETURN
static void usage(void);
82 static void parse_options(int argc
, char *argv
[], struct shash
*local_options
);
83 static void run_prerequisites(struct ctl_command
[], size_t n_commands
,
85 static void do_vtep_ctl(const char *args
, struct ctl_command
*, size_t n
,
87 static struct vtep_ctl_lswitch
*find_lswitch(struct vtep_ctl_context
*,
90 static struct vtep_ctl_lrouter
*find_lrouter(struct vtep_ctl_context
*,
95 main(int argc
, char *argv
[])
97 struct ovsdb_idl
*idl
;
98 struct ctl_command
*commands
;
99 struct shash local_options
;
104 set_program_name(argv
[0]);
105 fatal_ignore_sigpipe();
106 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
107 vlog_set_levels_from_string_assert("reconnect:warn");
112 /* Log our arguments. This is often valuable for debugging systems. */
113 args
= process_escape_args(argv
);
114 VLOG(ctl_might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
116 /* Parse command line. */
117 shash_init(&local_options
);
118 parse_options(argc
, argv
, &local_options
);
119 commands
= ctl_parse_commands(argc
- optind
, argv
+ optind
, &local_options
,
126 /* Initialize IDL. */
127 idl
= the_idl
= ovsdb_idl_create(db
, &vteprec_idl_class
, false, false);
128 run_prerequisites(commands
, n_commands
, idl
);
130 /* Execute the commands.
132 * 'seqno' is the database sequence number for which we last tried to
133 * execute our transaction. There's no point in trying to commit more than
134 * once for any given sequence number, because if the transaction fails
135 * it's because the database changed and we need to obtain an up-to-date
136 * view of the database before we try the transaction again. */
137 seqno
= ovsdb_idl_get_seqno(idl
);
140 if (!ovsdb_idl_is_alive(idl
)) {
141 int retval
= ovsdb_idl_get_last_error(idl
);
142 ctl_fatal("%s: database connection failed (%s)",
143 db
, ovs_retval_to_string(retval
));
146 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
147 seqno
= ovsdb_idl_get_seqno(idl
);
148 do_vtep_ctl(args
, commands
, n_commands
, idl
);
151 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
159 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
162 OPT_DB
= UCHAR_MAX
+ 1,
171 static const struct option global_long_options
[] = {
172 {"db", required_argument
, NULL
, OPT_DB
},
173 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
174 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
175 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
176 {"timeout", required_argument
, NULL
, 't'},
177 {"help", no_argument
, NULL
, 'h'},
178 {"version", no_argument
, NULL
, 'V'},
181 STREAM_SSL_LONG_OPTIONS
,
182 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
185 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
186 char *tmp
, *short_options
;
188 struct option
*options
;
189 size_t allocated_options
;
193 tmp
= ovs_cmdl_long_options_to_short_options(global_long_options
);
194 short_options
= xasprintf("+%s", tmp
);
197 /* We want to parse both global and command-specific options here, but
198 * getopt_long() isn't too convenient for the job. We copy our global
199 * options into a dynamic array, then append all of the command-specific
201 options
= xmemdup(global_long_options
, sizeof global_long_options
);
202 allocated_options
= ARRAY_SIZE(global_long_options
);
203 n_options
= n_global_long_options
;
204 ctl_add_cmd_options(&options
, &n_options
, &allocated_options
, OPT_LOCAL
);
205 table_style
.format
= TF_LIST
;
211 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
226 vlog_set_levels(&this_module
, VLF_SYSLOG
, VLL_WARN
);
234 if (shash_find(local_options
, options
[idx
].name
)) {
235 ctl_fatal("'%s' option specified multiple times",
238 shash_add_nocopy(local_options
,
239 xasprintf("--%s", options
[idx
].name
),
240 optarg
? xstrdup(optarg
) : NULL
);
247 ovs_print_version(0, 0);
248 printf("DB Schema %s\n", vteprec_get_db_version());
252 timeout
= strtoul(optarg
, NULL
, 10);
254 ctl_fatal("value %s on -t or --timeout is invalid",
260 TABLE_OPTION_HANDLERS(&table_style
)
262 STREAM_SSL_OPTION_HANDLERS
264 case OPT_PEER_CA_CERT
:
265 stream_ssl_set_peer_ca_cert_file(optarg
);
278 db
= ctl_default_db();
281 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
282 free(CONST_CAST(char *, options
[i
].name
));
287 /* Frees the current transaction and the underlying IDL and then calls
290 * Freeing the transaction and the IDL is not strictly necessary, but it makes
291 * for a clean memory leak report from valgrind in the normal case. That makes
292 * it easier to notice real memory leaks. */
294 vtep_ctl_exit(int status
)
297 ovsdb_idl_txn_abort(the_idl_txn
);
298 ovsdb_idl_txn_destroy(the_idl_txn
);
300 ovsdb_idl_destroy(the_idl
);
308 %s: VTEP configuration utility\n\
309 usage: %s [OPTIONS] COMMAND [ARG...]\n\
312 show print overview of database contents\n\
315 get-manager print the managers\n\
316 del-manager delete the managers\n\
317 set-manager TARGET... set the list of managers to TARGET...\n\
319 Physical Switch commands:\n\
320 add-ps PS create a new physical switch named PS\n\
321 del-ps PS delete PS and all of its ports\n\
322 list-ps print the names of all the physical switches\n\
323 ps-exists PS exit 2 if PS does not exist\n\
326 list-ports PS print the names of all the ports on PS\n\
327 add-port PS PORT add network device PORT to PS\n\
328 del-port PS PORT delete PORT from PS\n\
330 Logical Switch commands:\n\
331 add-ls LS create a new logical switch named LS\n\
332 del-ls LS delete LS and all of its ports\n\
333 list-ls print the names of all the logical switches\n\
334 ls-exists LS exit 2 if LS does not exist\n\
335 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
336 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
337 list-bindings PS PORT list bindings for PORT on PS\n\
339 Logical Router commands:\n\
340 add-lr LR create a new logical router named LR\n\
341 del-lr LR delete LR\n\
342 list-lr print the names of all the logical routers\n\
343 lr-exists LR exit 2 if LR does not exist\n\
345 MAC binding commands:\n\
346 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
347 del-ucast-local LS MAC del ucast local entry from LS\n\
348 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
349 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
350 clear-local-macs LS clear local mac entries\n\
351 list-local-macs LS list local mac entries\n\
352 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
353 del-ucast-remote LS MAC del ucast remote entry from LS\n\
354 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
355 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
356 clear-remote-macs LS clear remote mac entries\n\
357 list-remote-macs LS list remote mac entries\n\
362 --db=DATABASE connect to DATABASE\n\
364 -t, --timeout=SECS wait at most SECS seconds\n\
365 --dry-run do not commit changes to database\n\
366 --oneline print exactly one line of output per command\n",
367 program_name
, program_name
, ctl_get_db_cmd_usage(), ctl_default_db());
370 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
371 stream_usage("database", true, true, false);
374 -h, --help display this help message\n\
375 -V, --version display version information\n");
380 static struct cmd_show_table cmd_show_tables
[] = {
381 {&vteprec_table_global
,
383 {&vteprec_global_col_managers
,
384 &vteprec_global_col_switches
,
389 {&vteprec_table_manager
,
390 &vteprec_manager_col_target
,
391 {&vteprec_manager_col_is_connected
,
397 {&vteprec_table_physical_switch
,
398 &vteprec_physical_switch_col_name
,
399 {&vteprec_physical_switch_col_management_ips
,
400 &vteprec_physical_switch_col_tunnel_ips
,
401 &vteprec_physical_switch_col_ports
},
405 {&vteprec_table_physical_port
,
406 &vteprec_physical_port_col_name
,
407 {&vteprec_physical_port_col_vlan_bindings
,
413 {&vteprec_table_logical_switch
,
414 &vteprec_logical_switch_col_name
,
421 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
424 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
425 struct vtep_ctl_context
{
426 struct ctl_context base
;
428 /* Modifiable state. */
429 const struct vteprec_global
*vtep_global
;
432 /* A cache of the contents of the database.
434 * A command that needs to use any of this information must first
435 * call vtep_ctl_context_populate_cache(). A command that changes
436 * anything that could invalidate the cache must either call
437 * vtep_ctl_context_invalidate_cache() or manually update the cache
438 * to maintain its correctness. */
440 struct shash pswitches
; /* Maps from physical switch name to
441 * struct vtep_ctl_pswitch. */
442 struct shash ports
; /* Maps from port name to struct vtep_ctl_port. */
444 struct shash lswitches
; /* Maps from logical switch name to
445 * struct vtep_ctl_lswitch. */
446 struct shash plocs
; /* Maps from "<encap>+<dst_ip>" to
447 * struct vteprec_physical_locator. */
448 struct shash lrouters
; /* Maps from logical router name to
449 * struct vtep_ctl_lrouter. */
452 /* Casts 'base' into 'struct vtep_ctl_context'. */
453 static struct vtep_ctl_context
*
454 vtep_ctl_context_cast(struct ctl_context
*base
)
456 return CONTAINER_OF(base
, struct vtep_ctl_context
, base
);
459 struct vtep_ctl_pswitch
{
460 const struct vteprec_physical_switch
*ps_cfg
;
462 struct ovs_list ports
; /* Contains "struct vteprec_physical_port"s. */
465 struct vtep_ctl_port
{
466 struct ovs_list ports_node
; /* In struct vtep_ctl_pswitch's 'ports' list. */
467 const struct vteprec_physical_port
*port_cfg
;
468 struct vtep_ctl_pswitch
*ps
;
469 struct shash bindings
; /* Maps from vlan to vtep_ctl_lswitch. */
472 struct vtep_ctl_lswitch
{
473 const struct vteprec_logical_switch
*ls_cfg
;
475 struct shash ucast_local
; /* Maps from mac to vteprec_ucast_macs_local. */
476 struct shash ucast_remote
; /* Maps from mac to vteprec_ucast_macs_remote.*/
477 struct shash mcast_local
; /* Maps from mac to vtep_ctl_mcast_mac. */
478 struct shash mcast_remote
; /* Maps from mac to vtep_ctl_mcast_mac. */
481 struct vtep_ctl_lrouter
{
482 const struct vteprec_logical_router
*lr_cfg
;
486 struct vtep_ctl_mcast_mac
{
487 const struct vteprec_mcast_macs_local
*local_cfg
;
488 const struct vteprec_mcast_macs_remote
*remote_cfg
;
490 const struct vteprec_physical_locator_set
*ploc_set_cfg
;
491 struct ovs_list locators
; /* Contains 'vtep_ctl_ploc's. */
494 struct vtep_ctl_ploc
{
495 struct ovs_list locators_node
; /* In struct vtep_ctl_ploc_set's 'locators'
497 const struct vteprec_physical_locator
*ploc_cfg
;
501 verify_ports(struct vtep_ctl_context
*vtepctl_ctx
)
503 if (!vtepctl_ctx
->verified_ports
) {
504 const struct vteprec_physical_switch
*ps
;
506 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
507 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps
, vtepctl_ctx
->base
.idl
) {
508 vteprec_physical_switch_verify_ports(ps
);
511 vtepctl_ctx
->verified_ports
= true;
515 static struct vtep_ctl_port
*
516 add_port_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
517 struct vtep_ctl_pswitch
*ps
,
518 struct vteprec_physical_port
*port_cfg
)
520 char *cache_name
= xasprintf("%s+%s", ps
->name
, port_cfg
->name
);
521 struct vtep_ctl_port
*port
;
523 port
= xmalloc(sizeof *port
);
524 list_push_back(&ps
->ports
, &port
->ports_node
);
525 port
->port_cfg
= port_cfg
;
527 shash_add(&vtepctl_ctx
->ports
, cache_name
, port
);
529 shash_init(&port
->bindings
);
535 del_cached_port(struct vtep_ctl_context
*vtepctl_ctx
,
536 struct vtep_ctl_port
*port
)
538 char *cache_name
= xasprintf("%s+%s", port
->ps
->name
, port
->port_cfg
->name
);
540 list_remove(&port
->ports_node
);
541 shash_find_and_delete(&vtepctl_ctx
->ports
, cache_name
);
542 vteprec_physical_port_delete(port
->port_cfg
);
548 add_pswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
549 struct vteprec_physical_switch
*ps_cfg
)
551 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
553 ps
->name
= xstrdup(ps_cfg
->name
);
554 list_init(&ps
->ports
);
555 shash_add(&vtepctl_ctx
->pswitches
, ps
->name
, ps
);
559 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
560 const struct vteprec_physical_switch
*ps
)
562 struct vteprec_physical_switch
**pswitches
;
565 pswitches
= xmalloc(sizeof *vtep_global
->switches
566 * vtep_global
->n_switches
);
567 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
568 if (vtep_global
->switches
[i
] != ps
) {
569 pswitches
[n
++] = vtep_global
->switches
[i
];
572 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
577 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
579 ovs_assert(list_is_empty(&ps
->ports
));
581 vteprec_physical_switch_delete(ps
->ps_cfg
);
582 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
584 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
589 static struct vtep_ctl_lswitch
*
590 add_lswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
591 const struct vteprec_logical_switch
*ls_cfg
)
593 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
595 ls
->name
= xstrdup(ls_cfg
->name
);
596 shash_add(&vtepctl_ctx
->lswitches
, ls
->name
, ls
);
597 shash_init(&ls
->ucast_local
);
598 shash_init(&ls
->ucast_remote
);
599 shash_init(&ls
->mcast_local
);
600 shash_init(&ls
->mcast_remote
);
605 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
608 vteprec_logical_switch_delete(ls
->ls_cfg
);
610 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
616 commit_ls_bindings(struct vtep_ctl_port
*port
)
618 struct vteprec_logical_switch
**binding_values
;
619 int64_t *binding_keys
;
621 struct shash_node
*node
;
624 n_bindings
= shash_count(&port
->bindings
);
625 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
626 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
629 SHASH_FOR_EACH(node
, &port
->bindings
) {
630 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
632 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
633 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
637 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
638 binding_keys
, binding_values
,
640 free(binding_values
);
645 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
647 struct vtep_ctl_lswitch
*ls
)
649 if (shash_find(&port
->bindings
, vlan
)) {
650 ctl_fatal("multiple bindings for vlan %s", vlan
);
653 shash_add(&port
->bindings
, vlan
, ls
);
657 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
659 if (!shash_find(&port
->bindings
, vlan
)) {
660 ctl_fatal("no binding for vlan %s", vlan
);
663 shash_find_and_delete(&port
->bindings
, vlan
);
666 static struct vtep_ctl_lrouter
*
667 add_lrouter_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
668 const struct vteprec_logical_router
*lr_cfg
)
670 struct vtep_ctl_lrouter
*lr
= xmalloc(sizeof *lr
);
672 lr
->name
= xstrdup(lr_cfg
->name
);
673 shash_add(&vtepctl_ctx
->lrouters
, lr
->name
, lr
);
678 del_cached_lrouter(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lrouter
*lr
)
681 vteprec_logical_router_delete(lr
->lr_cfg
);
683 shash_find_and_delete(&ctx
->lrouters
, lr
->name
);
688 static struct vteprec_physical_locator
*
689 find_ploc(struct vtep_ctl_context
*vtepctl_ctx
, const char *encap
,
692 struct vteprec_physical_locator
*ploc
;
693 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
695 ovs_assert(vtepctl_ctx
->cache_valid
);
697 ploc
= shash_find_data(&vtepctl_ctx
->plocs
, name
);
704 add_ploc_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
705 struct vteprec_physical_locator
*ploc
)
707 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
708 struct vteprec_physical_locator
*orig_ploc
;
710 orig_ploc
= find_ploc(vtepctl_ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
712 shash_add(&vtepctl_ctx
->plocs
, name
, ploc
);
719 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
720 struct vteprec_physical_locator
*ploc_cfg
)
722 struct vtep_ctl_ploc
*ploc
;
724 ploc
= xmalloc(sizeof *ploc
);
725 ploc
->ploc_cfg
= ploc_cfg
;
726 list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
730 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
731 struct vteprec_physical_locator
*ploc_cfg
)
733 struct vtep_ctl_ploc
*ploc
;
735 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
736 if (ploc
->ploc_cfg
== ploc_cfg
) {
737 list_remove(&ploc
->locators_node
);
744 static struct vtep_ctl_mcast_mac
*
745 add_mcast_mac_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
746 struct vtep_ctl_lswitch
*ls
, const char *mac
,
747 struct vteprec_physical_locator_set
*ploc_set_cfg
,
750 struct vtep_ctl_mcast_mac
*mcast_mac
;
751 struct shash
*mcast_shash
;
754 mcast_mac
= xmalloc(sizeof *mcast_mac
);
755 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
757 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
758 list_init(&mcast_mac
->locators
);
759 shash_add(mcast_shash
, mac
, mcast_mac
);
761 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
762 struct vteprec_physical_locator
*ploc_cfg
;
764 ploc_cfg
= ploc_set_cfg
->locators
[i
];
765 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
766 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
773 vtep_ctl_context_invalidate_cache(struct ctl_context
*ctx
)
775 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
776 struct shash_node
*node
;
778 if (!vtepctl_ctx
->cache_valid
) {
781 vtepctl_ctx
->cache_valid
= false;
783 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
784 struct vtep_ctl_pswitch
*ps
= node
->data
;
788 shash_destroy(&vtepctl_ctx
->pswitches
);
790 SHASH_FOR_EACH (node
, &vtepctl_ctx
->ports
) {
791 struct vtep_ctl_port
*port
= node
->data
;
792 shash_destroy(&port
->bindings
);
794 shash_destroy_free_data(&vtepctl_ctx
->ports
);
796 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
797 struct vtep_ctl_lswitch
*ls
= node
->data
;
798 struct shash_node
*node2
, *next_node2
;
800 shash_destroy(&ls
->ucast_local
);
801 shash_destroy(&ls
->ucast_remote
);
803 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
804 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
805 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
807 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
808 &mcast_mac
->locators
) {
813 shash_destroy(&ls
->mcast_local
);
815 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
816 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
817 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
819 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
820 &mcast_mac
->locators
) {
825 shash_destroy(&ls
->mcast_remote
);
830 shash_destroy(&vtepctl_ctx
->lswitches
);
831 shash_destroy(&vtepctl_ctx
->plocs
);
833 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
834 struct vtep_ctl_lrouter
*lr
= node
->data
;
838 shash_destroy(&vtepctl_ctx
->lrouters
);
842 pre_get_info(struct ctl_context
*ctx
)
844 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
846 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
847 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
848 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_tunnels
);
850 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
851 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
853 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
855 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_router_col_name
);
857 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
858 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
859 ovsdb_idl_add_column(ctx
->idl
,
860 &vteprec_ucast_macs_local_col_logical_switch
);
862 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
863 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
864 ovsdb_idl_add_column(ctx
->idl
,
865 &vteprec_ucast_macs_remote_col_logical_switch
);
867 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
868 ovsdb_idl_add_column(ctx
->idl
,
869 &vteprec_mcast_macs_local_col_locator_set
);
870 ovsdb_idl_add_column(ctx
->idl
,
871 &vteprec_mcast_macs_local_col_logical_switch
);
873 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
874 ovsdb_idl_add_column(ctx
->idl
,
875 &vteprec_mcast_macs_remote_col_locator_set
);
876 ovsdb_idl_add_column(ctx
->idl
,
877 &vteprec_mcast_macs_remote_col_logical_switch
);
879 ovsdb_idl_add_column(ctx
->idl
,
880 &vteprec_physical_locator_set_col_locators
);
882 ovsdb_idl_add_column(ctx
->idl
,
883 &vteprec_physical_locator_col_dst_ip
);
884 ovsdb_idl_add_column(ctx
->idl
,
885 &vteprec_physical_locator_col_encapsulation_type
);
887 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_local
);
888 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_remote
);
892 vtep_ctl_context_populate_cache(struct ctl_context
*ctx
)
894 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
895 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
896 const struct vteprec_logical_switch
*ls_cfg
;
897 const struct vteprec_logical_router
*lr_cfg
;
898 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
899 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
900 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
901 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
902 const struct vteprec_tunnel
*tunnel_cfg
;
903 struct sset pswitches
, ports
, lswitches
;
904 struct sset lrouters
;
907 if (vtepctl_ctx
->cache_valid
) {
908 /* Cache is already populated. */
911 vtepctl_ctx
->cache_valid
= true;
912 shash_init(&vtepctl_ctx
->pswitches
);
913 shash_init(&vtepctl_ctx
->ports
);
914 shash_init(&vtepctl_ctx
->lswitches
);
915 shash_init(&vtepctl_ctx
->plocs
);
916 shash_init(&vtepctl_ctx
->lrouters
);
918 sset_init(&pswitches
);
920 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
921 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
924 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
925 VLOG_WARN("%s: database contains duplicate physical switch name",
929 add_pswitch_to_cache(vtepctl_ctx
, ps_cfg
);
931 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
932 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
934 if (!sset_add(&ports
, port_cfg
->name
)) {
935 /* Duplicate port name. (We will warn about that later.) */
940 sset_destroy(&pswitches
);
941 sset_destroy(&ports
);
943 sset_init(&lswitches
);
944 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
945 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
946 VLOG_WARN("%s: database contains duplicate logical switch name",
950 add_lswitch_to_cache(vtepctl_ctx
, ls_cfg
);
952 sset_destroy(&lswitches
);
954 sset_init(&lrouters
);
955 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg
, ctx
->idl
) {
956 if (!sset_add(&lrouters
, lr_cfg
->name
)) {
957 VLOG_WARN("%s: database contains duplicate logical router name",
961 add_lrouter_to_cache(vtepctl_ctx
, lr_cfg
);
963 sset_destroy(&lrouters
);
965 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
966 struct vtep_ctl_lswitch
*ls
;
968 if (!ucast_local_cfg
->logical_switch
) {
971 ls
= find_lswitch(vtepctl_ctx
, ucast_local_cfg
->logical_switch
->name
,
977 if (ucast_local_cfg
->locator
) {
978 add_ploc_to_cache(vtepctl_ctx
, ucast_local_cfg
->locator
);
981 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
984 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
985 struct vtep_ctl_lswitch
*ls
;
987 if (!ucast_remote_cfg
->logical_switch
) {
990 ls
= find_lswitch(vtepctl_ctx
, ucast_remote_cfg
->logical_switch
->name
,
996 if (ucast_remote_cfg
->locator
) {
997 add_ploc_to_cache(vtepctl_ctx
, ucast_remote_cfg
->locator
);
1000 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
1003 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
1004 struct vtep_ctl_mcast_mac
*mcast_mac
;
1005 struct vtep_ctl_lswitch
*ls
;
1007 if (!mcast_local_cfg
->logical_switch
) {
1010 ls
= find_lswitch(vtepctl_ctx
, mcast_local_cfg
->logical_switch
->name
,
1016 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_local_cfg
->MAC
,
1017 mcast_local_cfg
->locator_set
,
1019 mcast_mac
->local_cfg
= mcast_local_cfg
;
1022 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
1023 struct vtep_ctl_mcast_mac
*mcast_mac
;
1024 struct vtep_ctl_lswitch
*ls
;
1026 if (!mcast_remote_cfg
->logical_switch
) {
1029 ls
= find_lswitch(vtepctl_ctx
, mcast_remote_cfg
->logical_switch
->name
,
1035 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_remote_cfg
->MAC
,
1036 mcast_remote_cfg
->locator_set
,
1038 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
1041 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg
, ctx
->idl
) {
1042 if (tunnel_cfg
->local
) {
1043 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->local
);
1045 if (tunnel_cfg
->remote
) {
1046 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->remote
);
1050 sset_init(&pswitches
);
1051 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1052 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1053 struct vtep_ctl_pswitch
*ps
;
1056 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1059 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, ps_cfg
->name
);
1060 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1061 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1062 struct vtep_ctl_port
*port
;
1065 port
= shash_find_data(&vtepctl_ctx
->ports
, port_cfg
->name
);
1067 if (port_cfg
== port
->port_cfg
) {
1068 VLOG_WARN("%s: port is in multiple physical switches "
1070 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1072 /* Log as an error because this violates the database's
1073 * uniqueness constraints, so the database server shouldn't
1074 * have allowed it. */
1075 VLOG_ERR("%s: database contains duplicate port name",
1081 port
= add_port_to_cache(vtepctl_ctx
, ps
, port_cfg
);
1083 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
1084 struct vteprec_logical_switch
*ls_cfg
;
1085 struct vtep_ctl_lswitch
*ls
;
1088 vlan
= xasprintf("%"PRId64
, port_cfg
->key_vlan_bindings
[k
]);
1089 if (shash_find(&port
->bindings
, vlan
)) {
1090 ctl_fatal("multiple bindings for vlan %s", vlan
);
1093 ls_cfg
= port_cfg
->value_vlan_bindings
[k
];
1094 ls
= find_lswitch(vtepctl_ctx
, ls_cfg
->name
, true);
1096 shash_add_nocopy(&port
->bindings
, vlan
, ls
);
1100 sset_destroy(&pswitches
);
1103 static struct vtep_ctl_pswitch
*
1104 find_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, const char *name
, bool must_exist
)
1106 struct vtep_ctl_pswitch
*ps
;
1108 ovs_assert(vtepctl_ctx
->cache_valid
);
1110 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, name
);
1111 if (must_exist
&& !ps
) {
1112 ctl_fatal("no physical switch named %s", name
);
1114 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
1118 static struct vtep_ctl_port
*
1119 find_port(struct vtep_ctl_context
*vtepctl_ctx
, const char *ps_name
,
1120 const char *port_name
, bool must_exist
)
1122 char *cache_name
= xasprintf("%s+%s", ps_name
, port_name
);
1123 struct vtep_ctl_port
*port
;
1125 ovs_assert(vtepctl_ctx
->cache_valid
);
1127 port
= shash_find_data(&vtepctl_ctx
->ports
, cache_name
);
1128 if (port
&& !strcmp(port_name
, port
->ps
->name
)) {
1132 if (must_exist
&& !port
) {
1133 ctl_fatal("no port named %s", port_name
);
1135 verify_ports(vtepctl_ctx
);
1140 pswitch_insert_port(const struct vteprec_physical_switch
*ps
,
1141 struct vteprec_physical_port
*port
)
1143 struct vteprec_physical_port
**ports
;
1146 ports
= xmalloc(sizeof *ps
->ports
* (ps
->n_ports
+ 1));
1147 for (i
= 0; i
< ps
->n_ports
; i
++) {
1148 ports
[i
] = ps
->ports
[i
];
1150 ports
[ps
->n_ports
] = port
;
1151 vteprec_physical_switch_set_ports(ps
, ports
, ps
->n_ports
+ 1);
1156 pswitch_delete_port(const struct vteprec_physical_switch
*ps
,
1157 const struct vteprec_physical_port
*port
)
1159 struct vteprec_physical_port
**ports
;
1162 ports
= xmalloc(sizeof *ps
->ports
* ps
->n_ports
);
1163 for (i
= n
= 0; i
< ps
->n_ports
; i
++) {
1164 if (ps
->ports
[i
] != port
) {
1165 ports
[n
++] = ps
->ports
[i
];
1168 vteprec_physical_switch_set_ports(ps
, ports
, n
);
1173 vtep_insert_pswitch(const struct vteprec_global
*vtep_global
,
1174 struct vteprec_physical_switch
*ps
)
1176 struct vteprec_physical_switch
**pswitches
;
1179 pswitches
= xmalloc(sizeof *vtep_global
->switches
1180 * (vtep_global
->n_switches
+ 1));
1181 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1182 pswitches
[i
] = vtep_global
->switches
[i
];
1184 pswitches
[vtep_global
->n_switches
] = ps
;
1185 vteprec_global_set_switches(vtep_global
, pswitches
,
1186 vtep_global
->n_switches
+ 1);
1191 cmd_add_ps(struct ctl_context
*ctx
)
1193 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1194 const char *ps_name
= ctx
->argv
[1];
1195 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1196 struct vteprec_physical_switch
*ps
;
1198 vtep_ctl_context_populate_cache(ctx
);
1199 if (find_pswitch(vtepctl_ctx
, ps_name
, false)) {
1201 ctl_fatal("cannot create physical switch %s because it "
1202 "already exists", ps_name
);
1207 ps
= vteprec_physical_switch_insert(ctx
->txn
);
1208 vteprec_physical_switch_set_name(ps
, ps_name
);
1210 vtep_insert_pswitch(vtepctl_ctx
->vtep_global
, ps
);
1212 vtep_ctl_context_invalidate_cache(ctx
);
1216 del_port(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_port
*port
)
1218 pswitch_delete_port(port
->ps
->ps_cfg
, port
->port_cfg
);
1219 del_cached_port(vtepctl_ctx
, port
);
1223 del_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_pswitch
*ps
)
1225 struct vtep_ctl_port
*port
, *next_port
;
1227 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &ps
->ports
) {
1228 del_port(vtepctl_ctx
, port
);
1231 del_cached_pswitch(vtepctl_ctx
, ps
);
1235 cmd_del_ps(struct ctl_context
*ctx
)
1237 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1238 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1239 struct vtep_ctl_pswitch
*ps
;
1241 vtep_ctl_context_populate_cache(ctx
);
1242 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1244 del_pswitch(vtepctl_ctx
, ps
);
1249 output_sorted(struct svec
*svec
, struct ds
*output
)
1255 SVEC_FOR_EACH (i
, name
, svec
) {
1256 ds_put_format(output
, "%s\n", name
);
1261 cmd_list_ps(struct ctl_context
*ctx
)
1263 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1264 struct shash_node
*node
;
1265 struct svec pswitches
;
1267 vtep_ctl_context_populate_cache(ctx
);
1269 svec_init(&pswitches
);
1270 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
1271 struct vtep_ctl_pswitch
*ps
= node
->data
;
1273 svec_add(&pswitches
, ps
->name
);
1275 output_sorted(&pswitches
, &ctx
->output
);
1276 svec_destroy(&pswitches
);
1280 cmd_ps_exists(struct ctl_context
*ctx
)
1282 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1284 vtep_ctl_context_populate_cache(ctx
);
1285 if (!find_pswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1291 cmd_list_ports(struct ctl_context
*ctx
)
1293 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1294 struct vtep_ctl_pswitch
*ps
;
1295 struct vtep_ctl_port
*port
;
1298 vtep_ctl_context_populate_cache(ctx
);
1299 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1300 vteprec_physical_switch_verify_ports(ps
->ps_cfg
);
1303 LIST_FOR_EACH (port
, ports_node
, &ps
->ports
) {
1304 if (strcmp(port
->port_cfg
->name
, ps
->name
)) {
1305 svec_add(&ports
, port
->port_cfg
->name
);
1308 output_sorted(&ports
, &ctx
->output
);
1309 svec_destroy(&ports
);
1313 add_port(struct ctl_context
*ctx
, const char *ps_name
,
1314 const char *port_name
, bool may_exist
)
1316 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1317 struct vtep_ctl_port
*vtep_ctl_port
;
1318 struct vtep_ctl_pswitch
*ps
;
1319 struct vteprec_physical_port
*port
;
1321 vtep_ctl_context_populate_cache(ctx
);
1323 vtep_ctl_port
= find_port(vtepctl_ctx
, ps_name
, port_name
, false);
1324 if (vtep_ctl_port
) {
1326 ctl_fatal("cannot create a port named %s on %s because a "
1327 "port with that name already exists",
1328 port_name
, ps_name
);
1333 ps
= find_pswitch(vtepctl_ctx
, ps_name
, true);
1335 port
= vteprec_physical_port_insert(ctx
->txn
);
1336 vteprec_physical_port_set_name(port
, port_name
);
1338 pswitch_insert_port(ps
->ps_cfg
, port
);
1340 add_port_to_cache(vtepctl_ctx
, ps
, port
);
1344 cmd_add_port(struct ctl_context
*ctx
)
1346 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1348 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
);
1352 cmd_del_port(struct ctl_context
*ctx
)
1354 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1355 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1356 struct vtep_ctl_port
*port
;
1358 vtep_ctl_context_populate_cache(ctx
);
1360 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], must_exist
);
1362 if (ctx
->argc
== 3) {
1363 struct vtep_ctl_pswitch
*ps
;
1365 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1366 if (port
->ps
!= ps
) {
1367 ctl_fatal("physical switch %s does not have a port %s",
1368 ctx
->argv
[1], ctx
->argv
[2]);
1372 del_port(vtepctl_ctx
, port
);
1376 static struct vtep_ctl_lswitch
*
1377 find_lswitch(struct vtep_ctl_context
*vtepctl_ctx
,
1378 const char *name
, bool must_exist
)
1380 struct vtep_ctl_lswitch
*ls
;
1382 ovs_assert(vtepctl_ctx
->cache_valid
);
1384 ls
= shash_find_data(&vtepctl_ctx
->lswitches
, name
);
1385 if (must_exist
&& !ls
) {
1386 ctl_fatal("no logical switch named %s", name
);
1392 cmd_add_ls(struct ctl_context
*ctx
)
1394 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1395 const char *ls_name
= ctx
->argv
[1];
1396 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1397 struct vteprec_logical_switch
*ls
;
1399 vtep_ctl_context_populate_cache(ctx
);
1400 if (find_lswitch(vtepctl_ctx
, ls_name
, false)) {
1402 ctl_fatal("cannot create logical switch %s because it "
1403 "already exists", ls_name
);
1408 ls
= vteprec_logical_switch_insert(ctx
->txn
);
1409 vteprec_logical_switch_set_name(ls
, ls_name
);
1411 vtep_ctl_context_invalidate_cache(ctx
);
1415 del_lswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lswitch
*ls
)
1417 del_cached_lswitch(vtepctl_ctx
, ls
);
1421 cmd_del_ls(struct ctl_context
*ctx
)
1423 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1424 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1425 struct vtep_ctl_lswitch
*ls
;
1427 vtep_ctl_context_populate_cache(ctx
);
1428 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1430 del_lswitch(vtepctl_ctx
, ls
);
1435 cmd_list_ls(struct ctl_context
*ctx
)
1437 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1438 struct shash_node
*node
;
1439 struct svec lswitches
;
1441 vtep_ctl_context_populate_cache(ctx
);
1443 svec_init(&lswitches
);
1444 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
1445 struct vtep_ctl_lswitch
*ls
= node
->data
;
1447 svec_add(&lswitches
, ls
->name
);
1449 output_sorted(&lswitches
, &ctx
->output
);
1450 svec_destroy(&lswitches
);
1454 cmd_ls_exists(struct ctl_context
*ctx
)
1456 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1458 vtep_ctl_context_populate_cache(ctx
);
1459 if (!find_lswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1465 cmd_list_bindings(struct ctl_context
*ctx
)
1467 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1468 const struct shash_node
*node
;
1469 struct vtep_ctl_port
*port
;
1470 struct svec bindings
;
1472 vtep_ctl_context_populate_cache(ctx
);
1473 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1475 svec_init(&bindings
);
1476 SHASH_FOR_EACH (node
, &port
->bindings
) {
1477 struct vtep_ctl_lswitch
*lswitch
= node
->data
;
1480 binding
= xasprintf("%04lld %s", strtoll(node
->name
, NULL
, 0),
1482 svec_add_nocopy(&bindings
, binding
);
1484 output_sorted(&bindings
, &ctx
->output
);
1485 svec_destroy(&bindings
);
1489 cmd_bind_ls(struct ctl_context
*ctx
)
1491 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1492 struct vtep_ctl_lswitch
*ls
;
1493 struct vtep_ctl_port
*port
;
1496 vtep_ctl_context_populate_cache(ctx
);
1498 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1499 vlan
= ctx
->argv
[3];
1500 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[4], true);
1502 add_ls_binding_to_cache(port
, vlan
, ls
);
1503 commit_ls_bindings(port
);
1505 vtep_ctl_context_invalidate_cache(ctx
);
1509 cmd_unbind_ls(struct ctl_context
*ctx
)
1511 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1512 struct vtep_ctl_port
*port
;
1515 vtep_ctl_context_populate_cache(ctx
);
1517 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1518 vlan
= ctx
->argv
[3];
1520 del_cached_ls_binding(port
, vlan
);
1521 commit_ls_bindings(port
);
1523 vtep_ctl_context_invalidate_cache(ctx
);
1526 static struct vtep_ctl_lrouter
*
1527 find_lrouter(struct vtep_ctl_context
*vtepctl_ctx
,
1528 const char *name
, bool must_exist
)
1530 struct vtep_ctl_lrouter
*lr
;
1532 ovs_assert(vtepctl_ctx
->cache_valid
);
1534 lr
= shash_find_data(&vtepctl_ctx
->lrouters
, name
);
1535 if (must_exist
&& !lr
) {
1536 ctl_fatal("no logical router named %s", name
);
1542 cmd_add_lr(struct ctl_context
*ctx
)
1544 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1545 const char *lr_name
= ctx
->argv
[1];
1546 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1547 struct vteprec_logical_router
*lr
;
1549 vtep_ctl_context_populate_cache(ctx
);
1550 if (find_lrouter(vtepctl_ctx
, lr_name
, false)) {
1552 ctl_fatal("cannot create logical router %s because it "
1553 "already exists", lr_name
);
1558 lr
= vteprec_logical_router_insert(ctx
->txn
);
1559 vteprec_logical_router_set_name(lr
, lr_name
);
1561 vtep_ctl_context_invalidate_cache(ctx
);
1565 del_lrouter(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lrouter
*lr
)
1567 del_cached_lrouter(vtepctl_ctx
, lr
);
1571 cmd_del_lr(struct ctl_context
*ctx
)
1573 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1574 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1575 struct vtep_ctl_lrouter
*lr
;
1577 vtep_ctl_context_populate_cache(ctx
);
1578 lr
= find_lrouter(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1580 del_lrouter(vtepctl_ctx
, lr
);
1585 cmd_list_lr(struct ctl_context
*ctx
)
1587 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1588 struct shash_node
*node
;
1589 struct svec lrouters
;
1591 vtep_ctl_context_populate_cache(ctx
);
1593 svec_init(&lrouters
);
1594 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
1595 struct vtep_ctl_lrouter
*lr
= node
->data
;
1597 svec_add(&lrouters
, lr
->name
);
1599 output_sorted(&lrouters
, &ctx
->output
);
1600 svec_destroy(&lrouters
);
1604 cmd_lr_exists(struct ctl_context
*ctx
)
1606 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1608 vtep_ctl_context_populate_cache(ctx
);
1609 if (!find_lrouter(vtepctl_ctx
, ctx
->argv
[1], false)) {
1615 add_ucast_entry(struct ctl_context
*ctx
, bool local
)
1617 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1618 struct vtep_ctl_lswitch
*ls
;
1622 struct vteprec_physical_locator
*ploc_cfg
;
1624 vtep_ctl_context_populate_cache(ctx
);
1626 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1629 if (ctx
->argc
== 4) {
1630 encap
= "vxlan_over_ipv4";
1631 dst_ip
= ctx
->argv
[3];
1633 encap
= ctx
->argv
[3];
1634 dst_ip
= ctx
->argv
[4];
1637 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1639 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1640 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1641 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1643 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1647 struct vteprec_ucast_macs_local
*ucast_cfg
;
1649 ucast_cfg
= shash_find_data(&ls
->ucast_local
, mac
);
1651 ucast_cfg
= vteprec_ucast_macs_local_insert(ctx
->txn
);
1652 vteprec_ucast_macs_local_set_MAC(ucast_cfg
, mac
);
1653 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1654 shash_add(&ls
->ucast_local
, mac
, ucast_cfg
);
1656 vteprec_ucast_macs_local_set_locator(ucast_cfg
, ploc_cfg
);
1658 struct vteprec_ucast_macs_remote
*ucast_cfg
;
1660 ucast_cfg
= shash_find_data(&ls
->ucast_remote
, mac
);
1662 ucast_cfg
= vteprec_ucast_macs_remote_insert(ctx
->txn
);
1663 vteprec_ucast_macs_remote_set_MAC(ucast_cfg
, mac
);
1664 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1665 shash_add(&ls
->ucast_remote
, mac
, ucast_cfg
);
1667 vteprec_ucast_macs_remote_set_locator(ucast_cfg
, ploc_cfg
);
1670 vtep_ctl_context_invalidate_cache(ctx
);
1674 cmd_add_ucast_local(struct ctl_context
*ctx
)
1676 add_ucast_entry(ctx
, true);
1680 cmd_add_ucast_remote(struct ctl_context
*ctx
)
1682 add_ucast_entry(ctx
, false);
1686 del_ucast_entry(struct ctl_context
*ctx
, bool local
)
1688 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1689 struct vtep_ctl_lswitch
*ls
;
1690 struct shash
*ucast_shash
;
1691 struct shash_node
*node
;
1693 vtep_ctl_context_populate_cache(ctx
);
1695 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1696 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1698 node
= shash_find(ucast_shash
, ctx
->argv
[2]);
1704 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1705 vteprec_ucast_macs_local_delete(ucast_cfg
);
1707 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1708 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1710 shash_delete(ucast_shash
, node
);
1712 vtep_ctl_context_invalidate_cache(ctx
);
1716 cmd_del_ucast_local(struct ctl_context
*ctx
)
1718 del_ucast_entry(ctx
, true);
1722 cmd_del_ucast_remote(struct ctl_context
*ctx
)
1724 del_ucast_entry(ctx
, false);
1728 commit_mcast_entries(struct vtep_ctl_mcast_mac
*mcast_mac
)
1730 struct vtep_ctl_ploc
*ploc
;
1731 struct vteprec_physical_locator
**locators
= NULL
;
1735 n_locators
= list_size(&mcast_mac
->locators
);
1736 ovs_assert(n_locators
);
1738 locators
= xmalloc(n_locators
* sizeof *locators
);
1741 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1742 locators
[i
] = (struct vteprec_physical_locator
*)ploc
->ploc_cfg
;
1746 vteprec_physical_locator_set_set_locators(mcast_mac
->ploc_set_cfg
,
1754 add_mcast_entry(struct ctl_context
*ctx
,
1755 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1756 const char *encap
, const char *dst_ip
, bool local
)
1758 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1759 struct shash
*mcast_shash
;
1760 struct vtep_ctl_mcast_mac
*mcast_mac
;
1761 struct vteprec_physical_locator
*ploc_cfg
;
1762 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1764 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1766 /* Physical locator sets are immutable, so allocate a new one. */
1767 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1769 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1771 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mac
, ploc_set_cfg
,
1775 mcast_mac
->local_cfg
= vteprec_mcast_macs_local_insert(ctx
->txn
);
1776 vteprec_mcast_macs_local_set_MAC(mcast_mac
->local_cfg
, mac
);
1777 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1779 vteprec_mcast_macs_local_set_logical_switch(mcast_mac
->local_cfg
,
1781 mcast_mac
->remote_cfg
= NULL
;
1783 mcast_mac
->remote_cfg
= vteprec_mcast_macs_remote_insert(ctx
->txn
);
1784 vteprec_mcast_macs_remote_set_MAC(mcast_mac
->remote_cfg
, mac
);
1785 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1787 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac
->remote_cfg
,
1789 mcast_mac
->local_cfg
= NULL
;
1792 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1794 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1797 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1802 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1804 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1805 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1806 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1808 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1811 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1812 commit_mcast_entries(mcast_mac
);
1816 del_mcast_entry(struct ctl_context
*ctx
,
1817 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1818 const char *encap
, const char *dst_ip
, bool local
)
1820 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1821 struct vtep_ctl_mcast_mac
*mcast_mac
;
1822 struct shash
*mcast_shash
;
1823 struct vteprec_physical_locator
*ploc_cfg
;
1824 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1826 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1828 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1833 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1835 /* Couldn't find the physical locator, so just ignore. */
1839 /* Physical locator sets are immutable, so allocate a new one. */
1840 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1841 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1843 del_ploc_from_mcast_mac(mcast_mac
, ploc_cfg
);
1844 if (list_is_empty(&mcast_mac
->locators
)) {
1845 struct shash_node
*node
= shash_find(mcast_shash
, mac
);
1847 vteprec_physical_locator_set_delete(ploc_set_cfg
);
1850 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1852 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1856 shash_delete(mcast_shash
, node
);
1859 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1862 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1865 commit_mcast_entries(mcast_mac
);
1870 add_del_mcast_entry(struct ctl_context
*ctx
, bool add
, bool local
)
1872 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1873 struct vtep_ctl_lswitch
*ls
;
1878 vtep_ctl_context_populate_cache(ctx
);
1880 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1883 if (ctx
->argc
== 4) {
1884 encap
= "vxlan_over_ipv4";
1885 dst_ip
= ctx
->argv
[3];
1887 encap
= ctx
->argv
[3];
1888 dst_ip
= ctx
->argv
[4];
1892 add_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1894 del_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1897 vtep_ctl_context_invalidate_cache(ctx
);
1901 cmd_add_mcast_local(struct ctl_context
*ctx
)
1903 add_del_mcast_entry(ctx
, true, true);
1907 cmd_add_mcast_remote(struct ctl_context
*ctx
)
1909 add_del_mcast_entry(ctx
, true, false);
1913 cmd_del_mcast_local(struct ctl_context
*ctx
)
1915 add_del_mcast_entry(ctx
, false, true);
1919 cmd_del_mcast_remote(struct ctl_context
*ctx
)
1921 add_del_mcast_entry(ctx
, false, false);
1925 clear_macs(struct ctl_context
*ctx
, bool local
)
1927 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1928 struct vtep_ctl_lswitch
*ls
;
1929 const struct shash_node
*node
;
1930 struct shash
*ucast_shash
;
1931 struct shash
*mcast_shash
;
1933 vtep_ctl_context_populate_cache(ctx
);
1934 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1936 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1937 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1939 SHASH_FOR_EACH (node
, ucast_shash
) {
1941 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1942 vteprec_ucast_macs_local_delete(ucast_cfg
);
1944 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1945 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1949 SHASH_FOR_EACH (node
, mcast_shash
) {
1950 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
1952 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1954 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1958 vtep_ctl_context_invalidate_cache(ctx
);
1962 cmd_clear_local_macs(struct ctl_context
*ctx
)
1964 clear_macs(ctx
, true);
1968 cmd_clear_remote_macs(struct ctl_context
*ctx
)
1970 clear_macs(ctx
, false);
1974 list_macs(struct ctl_context
*ctx
, bool local
)
1976 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1977 struct vtep_ctl_lswitch
*ls
;
1978 const struct shash_node
*node
;
1979 struct shash
*ucast_shash
;
1980 struct svec ucast_macs
;
1981 struct shash
*mcast_shash
;
1982 struct svec mcast_macs
;
1984 vtep_ctl_context_populate_cache(ctx
);
1985 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1987 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1988 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1990 svec_init(&ucast_macs
);
1991 SHASH_FOR_EACH (node
, ucast_shash
) {
1992 struct vteprec_ucast_macs_local
*ucast_local
= node
->data
;
1993 struct vteprec_ucast_macs_remote
*ucast_remote
= node
->data
;
1994 struct vteprec_physical_locator
*ploc_cfg
;
1997 ploc_cfg
= local
? ucast_local
->locator
: ucast_remote
->locator
;
1999 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2000 ploc_cfg
->encapsulation_type
, ploc_cfg
->dst_ip
);
2001 svec_add_nocopy(&ucast_macs
, entry
);
2003 ds_put_format(&ctx
->output
, "ucast-mac-%s\n", local
? "local" : "remote");
2004 output_sorted(&ucast_macs
, &ctx
->output
);
2005 ds_put_char(&ctx
->output
, '\n');
2006 svec_destroy(&ucast_macs
);
2008 svec_init(&mcast_macs
);
2009 SHASH_FOR_EACH (node
, mcast_shash
) {
2010 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
2011 struct vtep_ctl_ploc
*ploc
;
2014 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
2015 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2016 ploc
->ploc_cfg
->encapsulation_type
,
2017 ploc
->ploc_cfg
->dst_ip
);
2018 svec_add_nocopy(&mcast_macs
, entry
);
2021 ds_put_format(&ctx
->output
, "mcast-mac-%s\n", local
? "local" : "remote");
2022 output_sorted(&mcast_macs
, &ctx
->output
);
2023 ds_put_char(&ctx
->output
, '\n');
2024 svec_destroy(&mcast_macs
);
2028 cmd_list_local_macs(struct ctl_context
*ctx
)
2030 list_macs(ctx
, true);
2034 cmd_list_remote_macs(struct ctl_context
*ctx
)
2036 list_macs(ctx
, false);
2040 verify_managers(const struct vteprec_global
*vtep_global
)
2044 vteprec_global_verify_managers(vtep_global
);
2046 for (i
= 0; i
< vtep_global
->n_managers
; ++i
) {
2047 const struct vteprec_manager
*mgr
= vtep_global
->managers
[i
];
2049 vteprec_manager_verify_target(mgr
);
2054 pre_manager(struct ctl_context
*ctx
)
2056 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_managers
);
2057 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_target
);
2061 cmd_get_manager(struct ctl_context
*ctx
)
2063 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2064 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2065 struct svec targets
;
2068 verify_managers(vtep_global
);
2070 /* Print the targets in sorted order for reproducibility. */
2071 svec_init(&targets
);
2073 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2074 svec_add(&targets
, vtep_global
->managers
[i
]->target
);
2077 svec_sort_unique(&targets
);
2078 for (i
= 0; i
< targets
.n
; i
++) {
2079 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2081 svec_destroy(&targets
);
2085 delete_managers(const struct vtep_ctl_context
*vtepctl_ctx
)
2087 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2090 /* Delete Manager rows pointed to by 'managers' column. */
2091 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2092 vteprec_manager_delete(vtep_global
->managers
[i
]);
2095 /* Delete 'Manager' row refs in 'managers' column. */
2096 vteprec_global_set_managers(vtep_global
, NULL
, 0);
2100 cmd_del_manager(struct ctl_context
*ctx
)
2102 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2103 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2105 verify_managers(vtep_global
);
2106 delete_managers(vtepctl_ctx
);
2110 insert_managers(struct vtep_ctl_context
*vtepctl_ctx
, char *targets
[], size_t n
)
2112 struct vteprec_manager
**managers
;
2115 /* Insert each manager in a new row in Manager table. */
2116 managers
= xmalloc(n
* sizeof *managers
);
2117 for (i
= 0; i
< n
; i
++) {
2118 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2119 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2121 managers
[i
] = vteprec_manager_insert(vtepctl_ctx
->base
.txn
);
2122 vteprec_manager_set_target(managers
[i
], targets
[i
]);
2125 /* Store uuids of new Manager rows in 'managers' column. */
2126 vteprec_global_set_managers(vtepctl_ctx
->vtep_global
, managers
, n
);
2131 cmd_set_manager(struct ctl_context
*ctx
)
2133 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2134 const size_t n
= ctx
->argc
- 1;
2136 verify_managers(vtepctl_ctx
->vtep_global
);
2137 delete_managers(vtepctl_ctx
);
2138 insert_managers(vtepctl_ctx
, &ctx
->argv
[1], n
);
2141 /* Parameter commands. */
2142 static const struct ctl_table_class tables
[] = {
2143 {&vteprec_table_global
,
2144 {{&vteprec_table_global
, NULL
, NULL
},
2145 {NULL
, NULL
, NULL
}}},
2147 {&vteprec_table_logical_binding_stats
,
2148 {{NULL
, NULL
, NULL
},
2149 {NULL
, NULL
, NULL
}}},
2151 {&vteprec_table_logical_switch
,
2152 {{&vteprec_table_logical_switch
, &vteprec_logical_switch_col_name
, NULL
},
2153 {NULL
, NULL
, NULL
}}},
2155 {&vteprec_table_ucast_macs_local
,
2156 {{NULL
, NULL
, NULL
},
2157 {NULL
, NULL
, NULL
}}},
2159 {&vteprec_table_ucast_macs_remote
,
2160 {{NULL
, NULL
, NULL
},
2161 {NULL
, NULL
, NULL
}}},
2163 {&vteprec_table_mcast_macs_local
,
2164 {{NULL
, NULL
, NULL
},
2165 {NULL
, NULL
, NULL
}}},
2167 {&vteprec_table_mcast_macs_remote
,
2168 {{NULL
, NULL
, NULL
},
2169 {NULL
, NULL
, NULL
}}},
2171 {&vteprec_table_manager
,
2172 {{&vteprec_table_manager
, &vteprec_manager_col_target
, NULL
},
2173 {NULL
, NULL
, NULL
}}},
2175 {&vteprec_table_physical_locator
,
2176 {{NULL
, NULL
, NULL
},
2177 {NULL
, NULL
, NULL
}}},
2179 {&vteprec_table_physical_locator_set
,
2180 {{NULL
, NULL
, NULL
},
2181 {NULL
, NULL
, NULL
}}},
2183 {&vteprec_table_physical_port
,
2184 {{&vteprec_table_physical_port
, &vteprec_physical_port_col_name
, NULL
},
2185 {NULL
, NULL
, NULL
}}},
2187 {&vteprec_table_physical_switch
,
2188 {{&vteprec_table_physical_switch
, &vteprec_physical_switch_col_name
, NULL
},
2189 {NULL
, NULL
, NULL
}}},
2191 {&vteprec_table_tunnel
,
2192 {{NULL
, NULL
, NULL
},
2193 {NULL
, NULL
, NULL
}}},
2195 {&vteprec_table_logical_router
,
2196 {{&vteprec_table_logical_router
, &vteprec_logical_router_col_name
, NULL
},
2197 {NULL
, NULL
, NULL
}}},
2199 {&vteprec_table_arp_sources_local
,
2200 {{NULL
, NULL
, NULL
},
2201 {NULL
, NULL
, NULL
}}},
2203 {&vteprec_table_arp_sources_remote
,
2204 {{NULL
, NULL
, NULL
},
2205 {NULL
, NULL
, NULL
}}},
2207 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, 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
);
2440 static const struct ctl_command_syntax vtep_commands
[] = {
2441 /* Physical Switch commands. */
2442 {"add-ps", 1, 1, NULL
, pre_get_info
, cmd_add_ps
, NULL
, "--may-exist", RW
},
2443 {"del-ps", 1, 1, NULL
, pre_get_info
, cmd_del_ps
, NULL
, "--if-exists", RW
},
2444 {"list-ps", 0, 0, NULL
, pre_get_info
, cmd_list_ps
, NULL
, "", RO
},
2445 {"ps-exists", 1, 1, NULL
, pre_get_info
, cmd_ps_exists
, NULL
, "", RO
},
2447 /* Port commands. */
2448 {"list-ports", 1, 1, NULL
, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
2449 {"add-port", 2, 2, NULL
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
2451 {"del-port", 2, 2, NULL
, pre_get_info
, cmd_del_port
, NULL
, "--if-exists",
2454 /* Logical Switch commands. */
2455 {"add-ls", 1, 1, NULL
, pre_get_info
, cmd_add_ls
, NULL
, "--may-exist", RW
},
2456 {"del-ls", 1, 1, NULL
, pre_get_info
, cmd_del_ls
, NULL
, "--if-exists", RW
},
2457 {"list-ls", 0, 0, NULL
, pre_get_info
, cmd_list_ls
, NULL
, "", RO
},
2458 {"ls-exists", 1, 1, NULL
, pre_get_info
, cmd_ls_exists
, NULL
, "", RO
},
2459 {"list-bindings", 2, 2, NULL
, pre_get_info
, cmd_list_bindings
, NULL
, "", RO
},
2460 {"bind-ls", 4, 4, NULL
, pre_get_info
, cmd_bind_ls
, NULL
, "", RO
},
2461 {"unbind-ls", 3, 3, NULL
, pre_get_info
, cmd_unbind_ls
, NULL
, "", RO
},
2463 /* Logical Router commands. */
2464 {"add-lr", 1, 1, NULL
, pre_get_info
, cmd_add_lr
, NULL
, "--may-exist", RW
},
2465 {"del-lr", 1, 1, NULL
, pre_get_info
, cmd_del_lr
, NULL
, "--if-exists", RW
},
2466 {"list-lr", 0, 0, NULL
, pre_get_info
, cmd_list_lr
, NULL
, "", RO
},
2467 {"lr-exists", 1, 1, NULL
, pre_get_info
, cmd_lr_exists
, NULL
, "", RO
},
2469 /* MAC binding commands. */
2470 {"add-ucast-local", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_local
, NULL
,
2472 {"del-ucast-local", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_local
, NULL
,
2474 {"add-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_local
, NULL
,
2476 {"del-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_local
, NULL
,
2478 {"clear-local-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_local_macs
, NULL
,
2480 {"list-local-macs", 1, 1, NULL
, pre_get_info
, cmd_list_local_macs
, NULL
,
2482 {"add-ucast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_remote
, NULL
,
2484 {"del-ucast-remote", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_remote
, NULL
,
2486 {"add-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_remote
, NULL
,
2488 {"del-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_remote
, NULL
,
2490 {"clear-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_remote_macs
, NULL
,
2492 {"list-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_list_remote_macs
, NULL
,
2495 /* Manager commands. */
2496 {"get-manager", 0, 0, NULL
, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2497 {"del-manager", 0, 0, NULL
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2498 {"set-manager", 1, INT_MAX
, NULL
, pre_manager
, cmd_set_manager
, NULL
, "",
2501 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2504 /* Registers vsctl and common db commands. */
2506 vtep_ctl_cmd_init(void)
2508 ctl_init(tables
, cmd_show_tables
, vtep_ctl_exit
);
2509 ctl_register_commands(vtep_commands
);