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\
366 --db=DATABASE connect to DATABASE\n\
368 -t, --timeout=SECS wait at most SECS seconds\n\
369 --dry-run do not commit changes to database\n\
370 --oneline print exactly one line of output per command\n",
371 program_name
, program_name
, ctl_get_db_cmd_usage(), ctl_default_db());
375 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
376 stream_usage("database", true, true, false);
379 -h, --help display this help message\n\
380 -V, --version display version information\n");
385 static struct cmd_show_table cmd_show_tables
[] = {
386 {&vteprec_table_global
,
388 {&vteprec_global_col_managers
,
389 &vteprec_global_col_switches
,
394 {&vteprec_table_manager
,
395 &vteprec_manager_col_target
,
396 {&vteprec_manager_col_is_connected
,
402 {&vteprec_table_physical_switch
,
403 &vteprec_physical_switch_col_name
,
404 {&vteprec_physical_switch_col_management_ips
,
405 &vteprec_physical_switch_col_tunnel_ips
,
406 &vteprec_physical_switch_col_ports
},
410 {&vteprec_table_physical_port
,
411 &vteprec_physical_port_col_name
,
412 {&vteprec_physical_port_col_vlan_bindings
,
418 {&vteprec_table_logical_switch
,
419 &vteprec_logical_switch_col_name
,
426 {NULL
, NULL
, {NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}
429 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
430 struct vtep_ctl_context
{
431 struct ctl_context base
;
433 /* Modifiable state. */
434 const struct vteprec_global
*vtep_global
;
437 /* A cache of the contents of the database.
439 * A command that needs to use any of this information must first
440 * call vtep_ctl_context_populate_cache(). A command that changes
441 * anything that could invalidate the cache must either call
442 * vtep_ctl_context_invalidate_cache() or manually update the cache
443 * to maintain its correctness. */
445 struct shash pswitches
; /* Maps from physical switch name to
446 * struct vtep_ctl_pswitch. */
447 struct shash ports
; /* Maps from port name to struct vtep_ctl_port. */
449 struct shash lswitches
; /* Maps from logical switch name to
450 * struct vtep_ctl_lswitch. */
451 struct shash plocs
; /* Maps from "<encap>+<dst_ip>" to
452 * struct vteprec_physical_locator. */
453 struct shash lrouters
; /* Maps from logical router name to
454 * struct vtep_ctl_lrouter. */
457 /* Casts 'base' into 'struct vtep_ctl_context'. */
458 static struct vtep_ctl_context
*
459 vtep_ctl_context_cast(struct ctl_context
*base
)
461 return CONTAINER_OF(base
, struct vtep_ctl_context
, base
);
464 struct vtep_ctl_pswitch
{
465 const struct vteprec_physical_switch
*ps_cfg
;
467 struct ovs_list ports
; /* Contains "struct vteprec_physical_port"s. */
470 struct vtep_ctl_port
{
471 struct ovs_list ports_node
; /* In struct vtep_ctl_pswitch's 'ports' list. */
472 const struct vteprec_physical_port
*port_cfg
;
473 struct vtep_ctl_pswitch
*ps
;
474 struct shash bindings
; /* Maps from vlan to vtep_ctl_lswitch. */
477 struct vtep_ctl_lswitch
{
478 const struct vteprec_logical_switch
*ls_cfg
;
480 struct shash ucast_local
; /* Maps from mac to vteprec_ucast_macs_local. */
481 struct shash ucast_remote
; /* Maps from mac to vteprec_ucast_macs_remote.*/
482 struct shash mcast_local
; /* Maps from mac to vtep_ctl_mcast_mac. */
483 struct shash mcast_remote
; /* Maps from mac to vtep_ctl_mcast_mac. */
486 struct vtep_ctl_lrouter
{
487 const struct vteprec_logical_router
*lr_cfg
;
491 struct vtep_ctl_mcast_mac
{
492 const struct vteprec_mcast_macs_local
*local_cfg
;
493 const struct vteprec_mcast_macs_remote
*remote_cfg
;
495 const struct vteprec_physical_locator_set
*ploc_set_cfg
;
496 struct ovs_list locators
; /* Contains 'vtep_ctl_ploc's. */
499 struct vtep_ctl_ploc
{
500 struct ovs_list locators_node
; /* In struct vtep_ctl_ploc_set's 'locators'
502 const struct vteprec_physical_locator
*ploc_cfg
;
506 verify_ports(struct vtep_ctl_context
*vtepctl_ctx
)
508 if (!vtepctl_ctx
->verified_ports
) {
509 const struct vteprec_physical_switch
*ps
;
511 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
512 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps
, vtepctl_ctx
->base
.idl
) {
513 vteprec_physical_switch_verify_ports(ps
);
516 vtepctl_ctx
->verified_ports
= true;
520 static struct vtep_ctl_port
*
521 add_port_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
522 struct vtep_ctl_pswitch
*ps
,
523 struct vteprec_physical_port
*port_cfg
)
525 char *cache_name
= xasprintf("%s+%s", ps
->name
, port_cfg
->name
);
526 struct vtep_ctl_port
*port
;
528 port
= xmalloc(sizeof *port
);
529 ovs_list_push_back(&ps
->ports
, &port
->ports_node
);
530 port
->port_cfg
= port_cfg
;
532 shash_add(&vtepctl_ctx
->ports
, cache_name
, port
);
534 shash_init(&port
->bindings
);
540 del_cached_port(struct vtep_ctl_context
*vtepctl_ctx
,
541 struct vtep_ctl_port
*port
)
543 char *cache_name
= xasprintf("%s+%s", port
->ps
->name
, port
->port_cfg
->name
);
545 ovs_list_remove(&port
->ports_node
);
546 shash_find_and_delete(&vtepctl_ctx
->ports
, cache_name
);
547 vteprec_physical_port_delete(port
->port_cfg
);
548 shash_destroy(&port
->bindings
);
554 add_pswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
555 struct vteprec_physical_switch
*ps_cfg
)
557 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
559 ps
->name
= xstrdup(ps_cfg
->name
);
560 ovs_list_init(&ps
->ports
);
561 shash_add(&vtepctl_ctx
->pswitches
, ps
->name
, ps
);
565 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
566 const struct vteprec_physical_switch
*ps
)
568 struct vteprec_physical_switch
**pswitches
;
571 pswitches
= xmalloc(sizeof *vtep_global
->switches
572 * vtep_global
->n_switches
);
573 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
574 if (vtep_global
->switches
[i
] != ps
) {
575 pswitches
[n
++] = vtep_global
->switches
[i
];
578 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
583 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
585 ovs_assert(ovs_list_is_empty(&ps
->ports
));
587 vteprec_physical_switch_delete(ps
->ps_cfg
);
588 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
590 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
595 static struct vtep_ctl_lswitch
*
596 add_lswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
597 const struct vteprec_logical_switch
*ls_cfg
)
599 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
601 ls
->name
= xstrdup(ls_cfg
->name
);
602 shash_add(&vtepctl_ctx
->lswitches
, ls
->name
, ls
);
603 shash_init(&ls
->ucast_local
);
604 shash_init(&ls
->ucast_remote
);
605 shash_init(&ls
->mcast_local
);
606 shash_init(&ls
->mcast_remote
);
611 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
614 vteprec_logical_switch_delete(ls
->ls_cfg
);
616 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
622 commit_ls_bindings(struct vtep_ctl_port
*port
)
624 struct vteprec_logical_switch
**binding_values
;
625 int64_t *binding_keys
;
627 struct shash_node
*node
;
630 n_bindings
= shash_count(&port
->bindings
);
631 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
632 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
635 SHASH_FOR_EACH(node
, &port
->bindings
) {
636 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
638 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
639 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
643 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
644 binding_keys
, binding_values
,
646 free(binding_values
);
651 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
653 struct vtep_ctl_lswitch
*ls
)
655 if (shash_find(&port
->bindings
, vlan
)) {
656 ctl_fatal("multiple bindings for vlan %s", vlan
);
659 shash_add(&port
->bindings
, vlan
, ls
);
663 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
665 if (!shash_find(&port
->bindings
, vlan
)) {
666 ctl_fatal("no binding for vlan %s", vlan
);
669 shash_find_and_delete(&port
->bindings
, vlan
);
672 static struct vtep_ctl_lrouter
*
673 add_lrouter_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
674 const struct vteprec_logical_router
*lr_cfg
)
676 struct vtep_ctl_lrouter
*lr
= xmalloc(sizeof *lr
);
678 lr
->name
= xstrdup(lr_cfg
->name
);
679 shash_add(&vtepctl_ctx
->lrouters
, lr
->name
, lr
);
684 del_cached_lrouter(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lrouter
*lr
)
687 vteprec_logical_router_delete(lr
->lr_cfg
);
689 shash_find_and_delete(&ctx
->lrouters
, lr
->name
);
694 static struct vteprec_physical_locator
*
695 find_ploc(struct vtep_ctl_context
*vtepctl_ctx
, const char *encap
,
698 struct vteprec_physical_locator
*ploc
;
699 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
701 ovs_assert(vtepctl_ctx
->cache_valid
);
703 ploc
= shash_find_data(&vtepctl_ctx
->plocs
, name
);
710 add_ploc_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
711 struct vteprec_physical_locator
*ploc
)
713 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
714 struct vteprec_physical_locator
*orig_ploc
;
716 orig_ploc
= find_ploc(vtepctl_ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
718 shash_add(&vtepctl_ctx
->plocs
, name
, ploc
);
725 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
726 struct vteprec_physical_locator
*ploc_cfg
)
728 struct vtep_ctl_ploc
*ploc
;
730 ploc
= xmalloc(sizeof *ploc
);
731 ploc
->ploc_cfg
= ploc_cfg
;
732 ovs_list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
736 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
737 struct vteprec_physical_locator
*ploc_cfg
)
739 struct vtep_ctl_ploc
*ploc
;
741 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
742 if (ploc
->ploc_cfg
== ploc_cfg
) {
743 ovs_list_remove(&ploc
->locators_node
);
750 static struct vtep_ctl_mcast_mac
*
751 add_mcast_mac_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
752 struct vtep_ctl_lswitch
*ls
, const char *mac
,
753 struct vteprec_physical_locator_set
*ploc_set_cfg
,
756 struct vtep_ctl_mcast_mac
*mcast_mac
;
757 struct shash
*mcast_shash
;
760 mcast_mac
= xmalloc(sizeof *mcast_mac
);
761 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
763 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
764 ovs_list_init(&mcast_mac
->locators
);
765 shash_add(mcast_shash
, mac
, mcast_mac
);
767 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
768 struct vteprec_physical_locator
*ploc_cfg
;
770 ploc_cfg
= ploc_set_cfg
->locators
[i
];
771 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
772 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
779 vtep_ctl_context_invalidate_cache(struct ctl_context
*ctx
)
781 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
782 struct shash_node
*node
;
784 if (!vtepctl_ctx
->cache_valid
) {
787 vtepctl_ctx
->cache_valid
= false;
789 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
790 struct vtep_ctl_pswitch
*ps
= node
->data
;
794 shash_destroy(&vtepctl_ctx
->pswitches
);
796 SHASH_FOR_EACH (node
, &vtepctl_ctx
->ports
) {
797 struct vtep_ctl_port
*port
= node
->data
;
798 shash_destroy(&port
->bindings
);
800 shash_destroy_free_data(&vtepctl_ctx
->ports
);
802 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
803 struct vtep_ctl_lswitch
*ls
= node
->data
;
804 struct shash_node
*node2
, *next_node2
;
806 shash_destroy(&ls
->ucast_local
);
807 shash_destroy(&ls
->ucast_remote
);
809 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
810 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
811 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
813 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
814 &mcast_mac
->locators
) {
819 shash_destroy(&ls
->mcast_local
);
821 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
822 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
823 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
825 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
826 &mcast_mac
->locators
) {
831 shash_destroy(&ls
->mcast_remote
);
836 shash_destroy(&vtepctl_ctx
->lswitches
);
837 shash_destroy(&vtepctl_ctx
->plocs
);
839 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
840 struct vtep_ctl_lrouter
*lr
= node
->data
;
844 shash_destroy(&vtepctl_ctx
->lrouters
);
848 pre_get_info(struct ctl_context
*ctx
)
850 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
852 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
853 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
854 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_tunnels
);
856 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
857 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
859 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
860 ovsdb_idl_add_column(ctx
->idl
,
861 &vteprec_logical_switch_col_replication_mode
);
863 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_router_col_name
);
865 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
866 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
867 ovsdb_idl_add_column(ctx
->idl
,
868 &vteprec_ucast_macs_local_col_logical_switch
);
870 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
871 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
872 ovsdb_idl_add_column(ctx
->idl
,
873 &vteprec_ucast_macs_remote_col_logical_switch
);
875 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
876 ovsdb_idl_add_column(ctx
->idl
,
877 &vteprec_mcast_macs_local_col_locator_set
);
878 ovsdb_idl_add_column(ctx
->idl
,
879 &vteprec_mcast_macs_local_col_logical_switch
);
881 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
882 ovsdb_idl_add_column(ctx
->idl
,
883 &vteprec_mcast_macs_remote_col_locator_set
);
884 ovsdb_idl_add_column(ctx
->idl
,
885 &vteprec_mcast_macs_remote_col_logical_switch
);
887 ovsdb_idl_add_column(ctx
->idl
,
888 &vteprec_physical_locator_set_col_locators
);
890 ovsdb_idl_add_column(ctx
->idl
,
891 &vteprec_physical_locator_col_dst_ip
);
892 ovsdb_idl_add_column(ctx
->idl
,
893 &vteprec_physical_locator_col_encapsulation_type
);
895 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_local
);
896 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_remote
);
900 vtep_ctl_context_populate_cache(struct ctl_context
*ctx
)
902 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
903 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
904 const struct vteprec_logical_switch
*ls_cfg
;
905 const struct vteprec_logical_router
*lr_cfg
;
906 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
907 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
908 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
909 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
910 const struct vteprec_tunnel
*tunnel_cfg
;
911 struct sset pswitches
, ports
, lswitches
;
912 struct sset lrouters
;
915 if (vtepctl_ctx
->cache_valid
) {
916 /* Cache is already populated. */
919 vtepctl_ctx
->cache_valid
= true;
920 shash_init(&vtepctl_ctx
->pswitches
);
921 shash_init(&vtepctl_ctx
->ports
);
922 shash_init(&vtepctl_ctx
->lswitches
);
923 shash_init(&vtepctl_ctx
->plocs
);
924 shash_init(&vtepctl_ctx
->lrouters
);
926 sset_init(&pswitches
);
928 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
929 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
932 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
933 VLOG_WARN("%s: database contains duplicate physical switch name",
937 add_pswitch_to_cache(vtepctl_ctx
, ps_cfg
);
939 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
940 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
942 if (!sset_add(&ports
, port_cfg
->name
)) {
943 /* Duplicate port name. (We will warn about that later.) */
948 sset_destroy(&pswitches
);
949 sset_destroy(&ports
);
951 sset_init(&lswitches
);
952 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
953 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
954 VLOG_WARN("%s: database contains duplicate logical switch name",
958 add_lswitch_to_cache(vtepctl_ctx
, ls_cfg
);
960 sset_destroy(&lswitches
);
962 sset_init(&lrouters
);
963 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg
, ctx
->idl
) {
964 if (!sset_add(&lrouters
, lr_cfg
->name
)) {
965 VLOG_WARN("%s: database contains duplicate logical router name",
969 add_lrouter_to_cache(vtepctl_ctx
, lr_cfg
);
971 sset_destroy(&lrouters
);
973 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
974 struct vtep_ctl_lswitch
*ls
;
976 if (!ucast_local_cfg
->logical_switch
) {
979 ls
= find_lswitch(vtepctl_ctx
, ucast_local_cfg
->logical_switch
->name
,
985 if (ucast_local_cfg
->locator
) {
986 add_ploc_to_cache(vtepctl_ctx
, ucast_local_cfg
->locator
);
989 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
992 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
993 struct vtep_ctl_lswitch
*ls
;
995 if (!ucast_remote_cfg
->logical_switch
) {
998 ls
= find_lswitch(vtepctl_ctx
, ucast_remote_cfg
->logical_switch
->name
,
1004 if (ucast_remote_cfg
->locator
) {
1005 add_ploc_to_cache(vtepctl_ctx
, ucast_remote_cfg
->locator
);
1008 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
1011 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
1012 struct vtep_ctl_mcast_mac
*mcast_mac
;
1013 struct vtep_ctl_lswitch
*ls
;
1015 if (!mcast_local_cfg
->logical_switch
) {
1018 ls
= find_lswitch(vtepctl_ctx
, mcast_local_cfg
->logical_switch
->name
,
1024 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_local_cfg
->MAC
,
1025 mcast_local_cfg
->locator_set
,
1027 mcast_mac
->local_cfg
= mcast_local_cfg
;
1030 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
1031 struct vtep_ctl_mcast_mac
*mcast_mac
;
1032 struct vtep_ctl_lswitch
*ls
;
1034 if (!mcast_remote_cfg
->logical_switch
) {
1037 ls
= find_lswitch(vtepctl_ctx
, mcast_remote_cfg
->logical_switch
->name
,
1043 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_remote_cfg
->MAC
,
1044 mcast_remote_cfg
->locator_set
,
1046 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
1049 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg
, ctx
->idl
) {
1050 if (tunnel_cfg
->local
) {
1051 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->local
);
1053 if (tunnel_cfg
->remote
) {
1054 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->remote
);
1058 sset_init(&pswitches
);
1059 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1060 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1061 struct vtep_ctl_pswitch
*ps
;
1064 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1067 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, ps_cfg
->name
);
1068 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1069 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1070 struct vtep_ctl_port
*port
;
1073 port
= shash_find_data(&vtepctl_ctx
->ports
, port_cfg
->name
);
1075 if (port_cfg
== port
->port_cfg
) {
1076 VLOG_WARN("%s: port is in multiple physical switches "
1078 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1080 /* Log as an error because this violates the database's
1081 * uniqueness constraints, so the database server shouldn't
1082 * have allowed it. */
1083 VLOG_ERR("%s: database contains duplicate port name",
1089 port
= add_port_to_cache(vtepctl_ctx
, ps
, port_cfg
);
1091 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
1092 struct vtep_ctl_lswitch
*ls
;
1095 vlan
= xasprintf("%"PRId64
, port_cfg
->key_vlan_bindings
[k
]);
1096 if (shash_find(&port
->bindings
, vlan
)) {
1097 ctl_fatal("multiple bindings for vlan %s", vlan
);
1100 ls_cfg
= port_cfg
->value_vlan_bindings
[k
];
1101 ls
= find_lswitch(vtepctl_ctx
, ls_cfg
->name
, true);
1103 shash_add_nocopy(&port
->bindings
, vlan
, ls
);
1107 sset_destroy(&pswitches
);
1110 static struct vtep_ctl_pswitch
*
1111 find_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, const char *name
, bool must_exist
)
1113 struct vtep_ctl_pswitch
*ps
;
1115 ovs_assert(vtepctl_ctx
->cache_valid
);
1117 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, name
);
1118 if (must_exist
&& !ps
) {
1119 ctl_fatal("no physical switch named %s", name
);
1121 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
1125 static struct vtep_ctl_port
*
1126 find_port(struct vtep_ctl_context
*vtepctl_ctx
, const char *ps_name
,
1127 const char *port_name
, bool must_exist
)
1129 char *cache_name
= xasprintf("%s+%s", ps_name
, port_name
);
1130 struct vtep_ctl_port
*port
;
1132 ovs_assert(vtepctl_ctx
->cache_valid
);
1134 port
= shash_find_data(&vtepctl_ctx
->ports
, cache_name
);
1135 if (port
&& !strcmp(port_name
, port
->ps
->name
)) {
1139 if (must_exist
&& !port
) {
1140 ctl_fatal("no port named %s", port_name
);
1142 verify_ports(vtepctl_ctx
);
1147 pswitch_insert_port(const struct vteprec_physical_switch
*ps
,
1148 struct vteprec_physical_port
*port
)
1150 struct vteprec_physical_port
**ports
;
1153 ports
= xmalloc(sizeof *ps
->ports
* (ps
->n_ports
+ 1));
1154 for (i
= 0; i
< ps
->n_ports
; i
++) {
1155 ports
[i
] = ps
->ports
[i
];
1157 ports
[ps
->n_ports
] = port
;
1158 vteprec_physical_switch_set_ports(ps
, ports
, ps
->n_ports
+ 1);
1163 pswitch_delete_port(const struct vteprec_physical_switch
*ps
,
1164 const struct vteprec_physical_port
*port
)
1166 struct vteprec_physical_port
**ports
;
1169 ports
= xmalloc(sizeof *ps
->ports
* ps
->n_ports
);
1170 for (i
= n
= 0; i
< ps
->n_ports
; i
++) {
1171 if (ps
->ports
[i
] != port
) {
1172 ports
[n
++] = ps
->ports
[i
];
1175 vteprec_physical_switch_set_ports(ps
, ports
, n
);
1180 vtep_insert_pswitch(const struct vteprec_global
*vtep_global
,
1181 struct vteprec_physical_switch
*ps
)
1183 struct vteprec_physical_switch
**pswitches
;
1186 pswitches
= xmalloc(sizeof *vtep_global
->switches
1187 * (vtep_global
->n_switches
+ 1));
1188 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1189 pswitches
[i
] = vtep_global
->switches
[i
];
1191 pswitches
[vtep_global
->n_switches
] = ps
;
1192 vteprec_global_set_switches(vtep_global
, pswitches
,
1193 vtep_global
->n_switches
+ 1);
1198 cmd_add_ps(struct ctl_context
*ctx
)
1200 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1201 const char *ps_name
= ctx
->argv
[1];
1202 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1203 struct vteprec_physical_switch
*ps
;
1205 vtep_ctl_context_populate_cache(ctx
);
1206 if (find_pswitch(vtepctl_ctx
, ps_name
, false)) {
1208 ctl_fatal("cannot create physical switch %s because it "
1209 "already exists", ps_name
);
1214 ps
= vteprec_physical_switch_insert(ctx
->txn
);
1215 vteprec_physical_switch_set_name(ps
, ps_name
);
1217 vtep_insert_pswitch(vtepctl_ctx
->vtep_global
, ps
);
1219 vtep_ctl_context_invalidate_cache(ctx
);
1223 del_port(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_port
*port
)
1225 pswitch_delete_port(port
->ps
->ps_cfg
, port
->port_cfg
);
1226 del_cached_port(vtepctl_ctx
, port
);
1230 del_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_pswitch
*ps
)
1232 struct vtep_ctl_port
*port
, *next_port
;
1234 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &ps
->ports
) {
1235 del_port(vtepctl_ctx
, port
);
1238 del_cached_pswitch(vtepctl_ctx
, ps
);
1242 cmd_del_ps(struct ctl_context
*ctx
)
1244 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1245 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1246 struct vtep_ctl_pswitch
*ps
;
1248 vtep_ctl_context_populate_cache(ctx
);
1249 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1251 del_pswitch(vtepctl_ctx
, ps
);
1256 output_sorted(struct svec
*svec
, struct ds
*output
)
1262 SVEC_FOR_EACH (i
, name
, svec
) {
1263 ds_put_format(output
, "%s\n", name
);
1268 cmd_list_ps(struct ctl_context
*ctx
)
1270 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1271 struct shash_node
*node
;
1272 struct svec pswitches
;
1274 vtep_ctl_context_populate_cache(ctx
);
1276 svec_init(&pswitches
);
1277 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
1278 struct vtep_ctl_pswitch
*ps
= node
->data
;
1280 svec_add(&pswitches
, ps
->name
);
1282 output_sorted(&pswitches
, &ctx
->output
);
1283 svec_destroy(&pswitches
);
1287 cmd_ps_exists(struct ctl_context
*ctx
)
1289 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1291 vtep_ctl_context_populate_cache(ctx
);
1292 if (!find_pswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1298 cmd_list_ports(struct ctl_context
*ctx
)
1300 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1301 struct vtep_ctl_pswitch
*ps
;
1302 struct vtep_ctl_port
*port
;
1305 vtep_ctl_context_populate_cache(ctx
);
1306 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1307 vteprec_physical_switch_verify_ports(ps
->ps_cfg
);
1310 LIST_FOR_EACH (port
, ports_node
, &ps
->ports
) {
1311 if (strcmp(port
->port_cfg
->name
, ps
->name
)) {
1312 svec_add(&ports
, port
->port_cfg
->name
);
1315 output_sorted(&ports
, &ctx
->output
);
1316 svec_destroy(&ports
);
1320 add_port(struct ctl_context
*ctx
, const char *ps_name
,
1321 const char *port_name
, bool may_exist
)
1323 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1324 struct vtep_ctl_port
*vtep_ctl_port
;
1325 struct vtep_ctl_pswitch
*ps
;
1326 struct vteprec_physical_port
*port
;
1328 vtep_ctl_context_populate_cache(ctx
);
1330 vtep_ctl_port
= find_port(vtepctl_ctx
, ps_name
, port_name
, false);
1331 if (vtep_ctl_port
) {
1333 ctl_fatal("cannot create a port named %s on %s because a "
1334 "port with that name already exists",
1335 port_name
, ps_name
);
1340 ps
= find_pswitch(vtepctl_ctx
, ps_name
, true);
1342 port
= vteprec_physical_port_insert(ctx
->txn
);
1343 vteprec_physical_port_set_name(port
, port_name
);
1345 pswitch_insert_port(ps
->ps_cfg
, port
);
1347 add_port_to_cache(vtepctl_ctx
, ps
, port
);
1351 cmd_add_port(struct ctl_context
*ctx
)
1353 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1355 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
);
1359 cmd_del_port(struct ctl_context
*ctx
)
1361 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1362 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1363 struct vtep_ctl_port
*port
;
1365 vtep_ctl_context_populate_cache(ctx
);
1367 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], must_exist
);
1369 if (ctx
->argc
== 3) {
1370 struct vtep_ctl_pswitch
*ps
;
1372 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1373 if (port
->ps
!= ps
) {
1374 ctl_fatal("physical switch %s does not have a port %s",
1375 ctx
->argv
[1], ctx
->argv
[2]);
1379 del_port(vtepctl_ctx
, port
);
1383 static struct vtep_ctl_lswitch
*
1384 find_lswitch(struct vtep_ctl_context
*vtepctl_ctx
,
1385 const char *name
, bool must_exist
)
1387 struct vtep_ctl_lswitch
*ls
;
1389 ovs_assert(vtepctl_ctx
->cache_valid
);
1391 ls
= shash_find_data(&vtepctl_ctx
->lswitches
, name
);
1392 if (must_exist
&& !ls
) {
1393 ctl_fatal("no logical switch named %s", name
);
1399 cmd_add_ls(struct ctl_context
*ctx
)
1401 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1402 const char *ls_name
= ctx
->argv
[1];
1403 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1404 struct vteprec_logical_switch
*ls
;
1406 vtep_ctl_context_populate_cache(ctx
);
1407 if (find_lswitch(vtepctl_ctx
, ls_name
, false)) {
1409 ctl_fatal("cannot create logical switch %s because it "
1410 "already exists", ls_name
);
1415 ls
= vteprec_logical_switch_insert(ctx
->txn
);
1416 vteprec_logical_switch_set_name(ls
, ls_name
);
1418 vtep_ctl_context_invalidate_cache(ctx
);
1422 del_lswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lswitch
*ls
)
1424 del_cached_lswitch(vtepctl_ctx
, ls
);
1428 cmd_del_ls(struct ctl_context
*ctx
)
1430 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1431 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1432 struct vtep_ctl_lswitch
*ls
;
1434 vtep_ctl_context_populate_cache(ctx
);
1435 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1437 del_lswitch(vtepctl_ctx
, ls
);
1442 cmd_list_ls(struct ctl_context
*ctx
)
1444 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1445 struct shash_node
*node
;
1446 struct svec lswitches
;
1448 vtep_ctl_context_populate_cache(ctx
);
1450 svec_init(&lswitches
);
1451 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
1452 struct vtep_ctl_lswitch
*ls
= node
->data
;
1454 svec_add(&lswitches
, ls
->name
);
1456 output_sorted(&lswitches
, &ctx
->output
);
1457 svec_destroy(&lswitches
);
1461 cmd_ls_exists(struct ctl_context
*ctx
)
1463 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1465 vtep_ctl_context_populate_cache(ctx
);
1466 if (!find_lswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1472 cmd_list_bindings(struct ctl_context
*ctx
)
1474 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1475 const struct shash_node
*node
;
1476 struct vtep_ctl_port
*port
;
1477 struct svec bindings
;
1479 vtep_ctl_context_populate_cache(ctx
);
1480 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1482 svec_init(&bindings
);
1483 SHASH_FOR_EACH (node
, &port
->bindings
) {
1484 struct vtep_ctl_lswitch
*lswitch
= node
->data
;
1487 binding
= xasprintf("%04lld %s", strtoll(node
->name
, NULL
, 0),
1489 svec_add_nocopy(&bindings
, binding
);
1491 output_sorted(&bindings
, &ctx
->output
);
1492 svec_destroy(&bindings
);
1496 cmd_bind_ls(struct ctl_context
*ctx
)
1498 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1499 struct vtep_ctl_lswitch
*ls
;
1500 struct vtep_ctl_port
*port
;
1503 vtep_ctl_context_populate_cache(ctx
);
1505 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1506 vlan
= ctx
->argv
[3];
1507 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[4], true);
1509 add_ls_binding_to_cache(port
, vlan
, ls
);
1510 commit_ls_bindings(port
);
1512 vtep_ctl_context_invalidate_cache(ctx
);
1516 cmd_unbind_ls(struct ctl_context
*ctx
)
1518 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1519 struct vtep_ctl_port
*port
;
1522 vtep_ctl_context_populate_cache(ctx
);
1524 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1525 vlan
= ctx
->argv
[3];
1527 del_cached_ls_binding(port
, vlan
);
1528 commit_ls_bindings(port
);
1530 vtep_ctl_context_invalidate_cache(ctx
);
1534 cmd_set_replication_mode(struct ctl_context
*ctx
)
1536 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1537 struct vtep_ctl_lswitch
*ls
;
1538 const char *ls_name
= ctx
->argv
[1];
1540 vtep_ctl_context_populate_cache(ctx
);
1542 if (strcmp(ctx
->argv
[2], "service_node") &&
1543 strcmp(ctx
->argv
[2], "source_node")) {
1544 ctl_fatal("Replication mode must be 'service_node' or 'source_node'");
1547 ls
= find_lswitch(vtepctl_ctx
, ls_name
, true);
1548 vteprec_logical_switch_set_replication_mode(ls
->ls_cfg
, ctx
->argv
[2]);
1550 vtep_ctl_context_invalidate_cache(ctx
);
1554 cmd_get_replication_mode(struct ctl_context
*ctx
)
1556 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1557 struct vtep_ctl_lswitch
*ls
;
1558 const char *ls_name
= ctx
->argv
[1];
1560 vtep_ctl_context_populate_cache(ctx
);
1562 ls
= find_lswitch(vtepctl_ctx
, ls_name
, true);
1563 ds_put_format(&ctx
->output
, "%s\n", ls
->ls_cfg
->replication_mode
);
1566 static struct vtep_ctl_lrouter
*
1567 find_lrouter(struct vtep_ctl_context
*vtepctl_ctx
,
1568 const char *name
, bool must_exist
)
1570 struct vtep_ctl_lrouter
*lr
;
1572 ovs_assert(vtepctl_ctx
->cache_valid
);
1574 lr
= shash_find_data(&vtepctl_ctx
->lrouters
, name
);
1575 if (must_exist
&& !lr
) {
1576 ctl_fatal("no logical router named %s", name
);
1582 cmd_add_lr(struct ctl_context
*ctx
)
1584 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1585 const char *lr_name
= ctx
->argv
[1];
1586 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1587 struct vteprec_logical_router
*lr
;
1589 vtep_ctl_context_populate_cache(ctx
);
1590 if (find_lrouter(vtepctl_ctx
, lr_name
, false)) {
1592 ctl_fatal("cannot create logical router %s because it "
1593 "already exists", lr_name
);
1598 lr
= vteprec_logical_router_insert(ctx
->txn
);
1599 vteprec_logical_router_set_name(lr
, lr_name
);
1601 vtep_ctl_context_invalidate_cache(ctx
);
1605 del_lrouter(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lrouter
*lr
)
1607 del_cached_lrouter(vtepctl_ctx
, lr
);
1611 cmd_del_lr(struct ctl_context
*ctx
)
1613 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1614 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1615 struct vtep_ctl_lrouter
*lr
;
1617 vtep_ctl_context_populate_cache(ctx
);
1618 lr
= find_lrouter(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1620 del_lrouter(vtepctl_ctx
, lr
);
1625 cmd_list_lr(struct ctl_context
*ctx
)
1627 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1628 struct shash_node
*node
;
1629 struct svec lrouters
;
1631 vtep_ctl_context_populate_cache(ctx
);
1633 svec_init(&lrouters
);
1634 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lrouters
) {
1635 struct vtep_ctl_lrouter
*lr
= node
->data
;
1637 svec_add(&lrouters
, lr
->name
);
1639 output_sorted(&lrouters
, &ctx
->output
);
1640 svec_destroy(&lrouters
);
1644 cmd_lr_exists(struct ctl_context
*ctx
)
1646 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1648 vtep_ctl_context_populate_cache(ctx
);
1649 if (!find_lrouter(vtepctl_ctx
, ctx
->argv
[1], false)) {
1655 add_ucast_entry(struct ctl_context
*ctx
, bool local
)
1657 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1658 struct vtep_ctl_lswitch
*ls
;
1662 struct vteprec_physical_locator
*ploc_cfg
;
1664 vtep_ctl_context_populate_cache(ctx
);
1666 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1669 if (ctx
->argc
== 4) {
1670 encap
= "vxlan_over_ipv4";
1671 dst_ip
= ctx
->argv
[3];
1673 encap
= ctx
->argv
[3];
1674 dst_ip
= ctx
->argv
[4];
1677 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1679 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1680 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1681 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1683 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1687 struct vteprec_ucast_macs_local
*ucast_cfg
;
1689 ucast_cfg
= shash_find_data(&ls
->ucast_local
, mac
);
1691 ucast_cfg
= vteprec_ucast_macs_local_insert(ctx
->txn
);
1692 vteprec_ucast_macs_local_set_MAC(ucast_cfg
, mac
);
1693 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1694 shash_add(&ls
->ucast_local
, mac
, ucast_cfg
);
1696 vteprec_ucast_macs_local_set_locator(ucast_cfg
, ploc_cfg
);
1698 struct vteprec_ucast_macs_remote
*ucast_cfg
;
1700 ucast_cfg
= shash_find_data(&ls
->ucast_remote
, mac
);
1702 ucast_cfg
= vteprec_ucast_macs_remote_insert(ctx
->txn
);
1703 vteprec_ucast_macs_remote_set_MAC(ucast_cfg
, mac
);
1704 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1705 shash_add(&ls
->ucast_remote
, mac
, ucast_cfg
);
1707 vteprec_ucast_macs_remote_set_locator(ucast_cfg
, ploc_cfg
);
1710 vtep_ctl_context_invalidate_cache(ctx
);
1714 cmd_add_ucast_local(struct ctl_context
*ctx
)
1716 add_ucast_entry(ctx
, true);
1720 cmd_add_ucast_remote(struct ctl_context
*ctx
)
1722 add_ucast_entry(ctx
, false);
1726 del_ucast_entry(struct ctl_context
*ctx
, bool local
)
1728 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1729 struct vtep_ctl_lswitch
*ls
;
1730 struct shash
*ucast_shash
;
1731 struct shash_node
*node
;
1733 vtep_ctl_context_populate_cache(ctx
);
1735 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1736 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1738 node
= shash_find(ucast_shash
, ctx
->argv
[2]);
1744 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1745 vteprec_ucast_macs_local_delete(ucast_cfg
);
1747 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1748 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1750 shash_delete(ucast_shash
, node
);
1752 vtep_ctl_context_invalidate_cache(ctx
);
1756 cmd_del_ucast_local(struct ctl_context
*ctx
)
1758 del_ucast_entry(ctx
, true);
1762 cmd_del_ucast_remote(struct ctl_context
*ctx
)
1764 del_ucast_entry(ctx
, false);
1768 commit_mcast_entries(struct vtep_ctl_mcast_mac
*mcast_mac
)
1770 struct vtep_ctl_ploc
*ploc
;
1771 struct vteprec_physical_locator
**locators
= NULL
;
1775 n_locators
= ovs_list_size(&mcast_mac
->locators
);
1776 ovs_assert(n_locators
);
1778 locators
= xmalloc(n_locators
* sizeof *locators
);
1781 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1782 locators
[i
] = (struct vteprec_physical_locator
*)ploc
->ploc_cfg
;
1786 vteprec_physical_locator_set_set_locators(mcast_mac
->ploc_set_cfg
,
1794 add_mcast_entry(struct ctl_context
*ctx
,
1795 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1796 const char *encap
, const char *dst_ip
, bool local
)
1798 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1799 struct shash
*mcast_shash
;
1800 struct vtep_ctl_mcast_mac
*mcast_mac
;
1801 struct vteprec_physical_locator
*ploc_cfg
;
1802 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1804 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1806 /* Physical locator sets are immutable, so allocate a new one. */
1807 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1809 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1811 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mac
, ploc_set_cfg
,
1815 mcast_mac
->local_cfg
= vteprec_mcast_macs_local_insert(ctx
->txn
);
1816 vteprec_mcast_macs_local_set_MAC(mcast_mac
->local_cfg
, mac
);
1817 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1819 vteprec_mcast_macs_local_set_logical_switch(mcast_mac
->local_cfg
,
1821 mcast_mac
->remote_cfg
= NULL
;
1823 mcast_mac
->remote_cfg
= vteprec_mcast_macs_remote_insert(ctx
->txn
);
1824 vteprec_mcast_macs_remote_set_MAC(mcast_mac
->remote_cfg
, mac
);
1825 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1827 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac
->remote_cfg
,
1829 mcast_mac
->local_cfg
= NULL
;
1832 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1834 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1837 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1842 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1844 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1845 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1846 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1848 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1851 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1852 commit_mcast_entries(mcast_mac
);
1856 del_mcast_entry(struct ctl_context
*ctx
,
1857 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1858 const char *encap
, const char *dst_ip
, bool local
)
1860 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1861 struct vtep_ctl_mcast_mac
*mcast_mac
;
1862 struct shash
*mcast_shash
;
1863 struct vteprec_physical_locator
*ploc_cfg
;
1864 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1866 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1868 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1873 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1875 /* Couldn't find the physical locator, so just ignore. */
1879 /* Physical locator sets are immutable, so allocate a new one. */
1880 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1881 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1883 del_ploc_from_mcast_mac(mcast_mac
, ploc_cfg
);
1884 if (ovs_list_is_empty(&mcast_mac
->locators
)) {
1885 struct shash_node
*node
= shash_find(mcast_shash
, mac
);
1887 vteprec_physical_locator_set_delete(ploc_set_cfg
);
1890 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1892 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1896 shash_delete(mcast_shash
, node
);
1899 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1902 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1905 commit_mcast_entries(mcast_mac
);
1910 add_del_mcast_entry(struct ctl_context
*ctx
, bool add
, bool local
)
1912 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1913 struct vtep_ctl_lswitch
*ls
;
1918 vtep_ctl_context_populate_cache(ctx
);
1920 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1923 if (ctx
->argc
== 4) {
1924 encap
= "vxlan_over_ipv4";
1925 dst_ip
= ctx
->argv
[3];
1927 encap
= ctx
->argv
[3];
1928 dst_ip
= ctx
->argv
[4];
1932 add_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1934 del_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1937 vtep_ctl_context_invalidate_cache(ctx
);
1941 cmd_add_mcast_local(struct ctl_context
*ctx
)
1943 add_del_mcast_entry(ctx
, true, true);
1947 cmd_add_mcast_remote(struct ctl_context
*ctx
)
1949 add_del_mcast_entry(ctx
, true, false);
1953 cmd_del_mcast_local(struct ctl_context
*ctx
)
1955 add_del_mcast_entry(ctx
, false, true);
1959 cmd_del_mcast_remote(struct ctl_context
*ctx
)
1961 add_del_mcast_entry(ctx
, false, false);
1965 clear_macs(struct ctl_context
*ctx
, bool local
)
1967 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1968 struct vtep_ctl_lswitch
*ls
;
1969 const struct shash_node
*node
;
1970 struct shash
*ucast_shash
;
1971 struct shash
*mcast_shash
;
1973 vtep_ctl_context_populate_cache(ctx
);
1974 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1976 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1977 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1979 SHASH_FOR_EACH (node
, ucast_shash
) {
1981 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1982 vteprec_ucast_macs_local_delete(ucast_cfg
);
1984 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1985 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1989 SHASH_FOR_EACH (node
, mcast_shash
) {
1990 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
1992 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1994 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1998 vtep_ctl_context_invalidate_cache(ctx
);
2002 cmd_clear_local_macs(struct ctl_context
*ctx
)
2004 clear_macs(ctx
, true);
2008 cmd_clear_remote_macs(struct ctl_context
*ctx
)
2010 clear_macs(ctx
, false);
2014 list_macs(struct ctl_context
*ctx
, bool local
)
2016 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2017 struct vtep_ctl_lswitch
*ls
;
2018 const struct shash_node
*node
;
2019 struct shash
*ucast_shash
;
2020 struct svec ucast_macs
;
2021 struct shash
*mcast_shash
;
2022 struct svec mcast_macs
;
2024 vtep_ctl_context_populate_cache(ctx
);
2025 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
2027 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
2028 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
2030 svec_init(&ucast_macs
);
2031 SHASH_FOR_EACH (node
, ucast_shash
) {
2032 struct vteprec_ucast_macs_local
*ucast_local
= node
->data
;
2033 struct vteprec_ucast_macs_remote
*ucast_remote
= node
->data
;
2034 struct vteprec_physical_locator
*ploc_cfg
;
2037 ploc_cfg
= local
? ucast_local
->locator
: ucast_remote
->locator
;
2039 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2040 ploc_cfg
->encapsulation_type
, ploc_cfg
->dst_ip
);
2041 svec_add_nocopy(&ucast_macs
, entry
);
2043 ds_put_format(&ctx
->output
, "ucast-mac-%s\n", local
? "local" : "remote");
2044 output_sorted(&ucast_macs
, &ctx
->output
);
2045 ds_put_char(&ctx
->output
, '\n');
2046 svec_destroy(&ucast_macs
);
2048 svec_init(&mcast_macs
);
2049 SHASH_FOR_EACH (node
, mcast_shash
) {
2050 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
2051 struct vtep_ctl_ploc
*ploc
;
2054 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
2055 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2056 ploc
->ploc_cfg
->encapsulation_type
,
2057 ploc
->ploc_cfg
->dst_ip
);
2058 svec_add_nocopy(&mcast_macs
, entry
);
2061 ds_put_format(&ctx
->output
, "mcast-mac-%s\n", local
? "local" : "remote");
2062 output_sorted(&mcast_macs
, &ctx
->output
);
2063 ds_put_char(&ctx
->output
, '\n');
2064 svec_destroy(&mcast_macs
);
2068 cmd_list_local_macs(struct ctl_context
*ctx
)
2070 list_macs(ctx
, true);
2074 cmd_list_remote_macs(struct ctl_context
*ctx
)
2076 list_macs(ctx
, false);
2080 verify_managers(const struct vteprec_global
*vtep_global
)
2084 vteprec_global_verify_managers(vtep_global
);
2086 for (i
= 0; i
< vtep_global
->n_managers
; ++i
) {
2087 const struct vteprec_manager
*mgr
= vtep_global
->managers
[i
];
2089 vteprec_manager_verify_target(mgr
);
2094 pre_manager(struct ctl_context
*ctx
)
2096 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_managers
);
2097 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_target
);
2101 cmd_get_manager(struct ctl_context
*ctx
)
2103 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2104 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2105 struct svec targets
;
2108 verify_managers(vtep_global
);
2110 /* Print the targets in sorted order for reproducibility. */
2111 svec_init(&targets
);
2113 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2114 svec_add(&targets
, vtep_global
->managers
[i
]->target
);
2117 svec_sort_unique(&targets
);
2118 for (i
= 0; i
< targets
.n
; i
++) {
2119 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2121 svec_destroy(&targets
);
2125 delete_managers(const struct vtep_ctl_context
*vtepctl_ctx
)
2127 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2130 /* Delete Manager rows pointed to by 'managers' column. */
2131 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2132 vteprec_manager_delete(vtep_global
->managers
[i
]);
2135 /* Delete 'Manager' row refs in 'managers' column. */
2136 vteprec_global_set_managers(vtep_global
, NULL
, 0);
2140 cmd_del_manager(struct ctl_context
*ctx
)
2142 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2143 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
2145 verify_managers(vtep_global
);
2146 delete_managers(vtepctl_ctx
);
2150 insert_managers(struct vtep_ctl_context
*vtepctl_ctx
, char *targets
[], size_t n
)
2152 struct vteprec_manager
**managers
;
2155 /* Insert each manager in a new row in Manager table. */
2156 managers
= xmalloc(n
* sizeof *managers
);
2157 for (i
= 0; i
< n
; i
++) {
2158 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2159 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2161 managers
[i
] = vteprec_manager_insert(vtepctl_ctx
->base
.txn
);
2162 vteprec_manager_set_target(managers
[i
], targets
[i
]);
2165 /* Store uuids of new Manager rows in 'managers' column. */
2166 vteprec_global_set_managers(vtepctl_ctx
->vtep_global
, managers
, n
);
2171 cmd_set_manager(struct ctl_context
*ctx
)
2173 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
2174 const size_t n
= ctx
->argc
- 1;
2176 verify_managers(vtepctl_ctx
->vtep_global
);
2177 delete_managers(vtepctl_ctx
);
2178 insert_managers(vtepctl_ctx
, &ctx
->argv
[1], n
);
2181 /* Parameter commands. */
2182 static const struct ctl_table_class tables
[VTEPREC_N_TABLES
] = {
2183 [VTEPREC_TABLE_LOGICAL_SWITCH
].row_ids
[0]
2184 = {&vteprec_logical_switch_col_name
, NULL
, NULL
},
2186 [VTEPREC_TABLE_MANAGER
].row_ids
[0]
2187 = {&vteprec_manager_col_target
, NULL
, NULL
},
2189 [VTEPREC_TABLE_PHYSICAL_PORT
].row_ids
[0]
2190 = {&vteprec_physical_port_col_name
, NULL
, NULL
},
2192 [VTEPREC_TABLE_PHYSICAL_SWITCH
].row_ids
[0]
2193 = {&vteprec_physical_switch_col_name
, NULL
, NULL
},
2195 [VTEPREC_TABLE_LOGICAL_ROUTER
].row_ids
[0]
2196 = {&vteprec_logical_router_col_name
, NULL
, NULL
},
2201 vtep_ctl_context_init_command(struct vtep_ctl_context
*vtepctl_ctx
,
2202 struct ctl_command
*command
)
2204 ctl_context_init_command(&vtepctl_ctx
->base
, command
);
2205 vtepctl_ctx
->verified_ports
= false;
2210 vtep_ctl_context_init(struct vtep_ctl_context
*vtepctl_ctx
,
2211 struct ctl_command
*command
,
2212 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2213 const struct vteprec_global
*vtep_global
,
2214 struct ovsdb_symbol_table
*symtab
)
2216 ctl_context_init(&vtepctl_ctx
->base
, command
, idl
, txn
, symtab
,
2217 vtep_ctl_context_invalidate_cache
);
2219 vtepctl_ctx
->verified_ports
= false;
2221 vtepctl_ctx
->vtep_global
= vtep_global
;
2222 vtepctl_ctx
->cache_valid
= false;
2226 vtep_ctl_context_done_command(struct vtep_ctl_context
*vtepctl_ctx
,
2227 struct ctl_command
*command
)
2229 ctl_context_done_command(&vtepctl_ctx
->base
, command
);
2233 vtep_ctl_context_done(struct vtep_ctl_context
*vtepctl_ctx
,
2234 struct ctl_command
*command
)
2236 ctl_context_done(&vtepctl_ctx
->base
, command
);
2240 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2241 struct ovsdb_idl
*idl
)
2243 struct ctl_command
*c
;
2245 ovsdb_idl_add_table(idl
, &vteprec_table_global
);
2246 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2247 if (c
->syntax
->prerequisites
) {
2248 struct vtep_ctl_context vtepctl_ctx
;
2250 ds_init(&c
->output
);
2253 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2254 (c
->syntax
->prerequisites
)(&vtepctl_ctx
.base
);
2255 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2257 ovs_assert(!c
->output
.string
);
2258 ovs_assert(!c
->table
);
2264 do_vtep_ctl(const char *args
, struct ctl_command
*commands
,
2265 size_t n_commands
, struct ovsdb_idl
*idl
)
2267 struct ovsdb_idl_txn
*txn
;
2268 const struct vteprec_global
*vtep_global
;
2269 enum ovsdb_idl_txn_status status
;
2270 struct ovsdb_symbol_table
*symtab
;
2271 struct vtep_ctl_context vtepctl_ctx
;
2272 struct ctl_command
*c
;
2273 struct shash_node
*node
;
2276 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2278 ovsdb_idl_txn_set_dry_run(txn
);
2281 ovsdb_idl_txn_add_comment(txn
, "vtep-ctl: %s", args
);
2283 vtep_global
= vteprec_global_first(idl
);
2285 /* XXX add verification that table is empty */
2286 vtep_global
= vteprec_global_insert(txn
);
2289 symtab
= ovsdb_symbol_table_create();
2290 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2291 ds_init(&c
->output
);
2294 vtep_ctl_context_init(&vtepctl_ctx
, NULL
, idl
, txn
, vtep_global
, symtab
);
2295 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2296 vtep_ctl_context_init_command(&vtepctl_ctx
, c
);
2297 if (c
->syntax
->run
) {
2298 (c
->syntax
->run
)(&vtepctl_ctx
.base
);
2300 vtep_ctl_context_done_command(&vtepctl_ctx
, c
);
2302 if (vtepctl_ctx
.base
.try_again
) {
2303 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2307 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2309 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2310 struct ovsdb_symbol
*symbol
= node
->data
;
2311 if (!symbol
->created
) {
2312 ctl_fatal("row id \"%s\" is referenced but never created "
2313 "(e.g. with \"-- --id=%s create ...\")",
2314 node
->name
, node
->name
);
2316 if (!symbol
->strong_ref
) {
2317 if (!symbol
->weak_ref
) {
2318 VLOG_WARN("row id \"%s\" was created but no reference to it "
2319 "was inserted, so it will not actually appear in "
2320 "the database", node
->name
);
2322 VLOG_WARN("row id \"%s\" was created but only a weak "
2323 "reference to it was inserted, so it will not "
2324 "actually appear in the database", node
->name
);
2329 status
= ovsdb_idl_txn_commit_block(txn
);
2330 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2331 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2332 if (c
->syntax
->postprocess
) {
2333 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, txn
, vtep_global
, symtab
);
2334 (c
->syntax
->postprocess
)(&vtepctl_ctx
.base
);
2335 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2339 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2340 ovsdb_idl_txn_destroy(txn
);
2341 txn
= the_idl_txn
= NULL
;
2344 case TXN_UNCOMMITTED
:
2345 case TXN_INCOMPLETE
:
2349 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2350 ctl_fatal("transaction aborted");
2360 ctl_fatal("transaction error: %s", error
);
2362 case TXN_NOT_LOCKED
:
2363 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2364 ctl_fatal("database not locked");
2371 ovsdb_symbol_table_destroy(symtab
);
2373 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2374 struct ds
*ds
= &c
->output
;
2377 table_print(c
->table
, &table_style
);
2378 } else if (oneline
) {
2382 for (j
= 0; j
< ds
->length
; j
++) {
2383 int ch
= ds
->string
[j
];
2386 fputs("\\n", stdout
);
2390 fputs("\\\\", stdout
);
2399 fputs(ds_cstr(ds
), stdout
);
2401 ds_destroy(&c
->output
);
2402 table_destroy(c
->table
);
2405 shash_destroy_free_data(&c
->options
);
2409 ovsdb_idl_destroy(idl
);
2414 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2415 * resources and return so that the caller can try again. */
2417 ovsdb_idl_txn_abort(txn
);
2418 ovsdb_idl_txn_destroy(txn
);
2420 ovsdb_symbol_table_destroy(symtab
);
2421 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2422 ds_destroy(&c
->output
);
2423 table_destroy(c
->table
);
2430 static const struct ctl_command_syntax vtep_commands
[] = {
2431 /* Physical Switch commands. */
2432 {"add-ps", 1, 1, NULL
, pre_get_info
, cmd_add_ps
, NULL
, "--may-exist", RW
},
2433 {"del-ps", 1, 1, NULL
, pre_get_info
, cmd_del_ps
, NULL
, "--if-exists", RW
},
2434 {"list-ps", 0, 0, NULL
, pre_get_info
, cmd_list_ps
, NULL
, "", RO
},
2435 {"ps-exists", 1, 1, NULL
, pre_get_info
, cmd_ps_exists
, NULL
, "", RO
},
2437 /* Port commands. */
2438 {"list-ports", 1, 1, NULL
, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
2439 {"add-port", 2, 2, NULL
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
2441 {"del-port", 2, 2, NULL
, pre_get_info
, cmd_del_port
, NULL
, "--if-exists",
2444 /* Logical Switch commands. */
2445 {"add-ls", 1, 1, NULL
, pre_get_info
, cmd_add_ls
, NULL
, "--may-exist", RW
},
2446 {"del-ls", 1, 1, NULL
, pre_get_info
, cmd_del_ls
, NULL
, "--if-exists", RW
},
2447 {"list-ls", 0, 0, NULL
, pre_get_info
, cmd_list_ls
, NULL
, "", RO
},
2448 {"ls-exists", 1, 1, NULL
, pre_get_info
, cmd_ls_exists
, NULL
, "", RO
},
2449 {"list-bindings", 2, 2, NULL
, pre_get_info
, cmd_list_bindings
, NULL
, "", RO
},
2450 {"bind-ls", 4, 4, NULL
, pre_get_info
, cmd_bind_ls
, NULL
, "", RO
},
2451 {"unbind-ls", 3, 3, NULL
, pre_get_info
, cmd_unbind_ls
, NULL
, "", RO
},
2452 {"set-replication-mode", 2, 2, "LS MODE", pre_get_info
,
2453 cmd_set_replication_mode
, NULL
, "", RW
},
2454 {"get-replication-mode", 1, 1, "LS", pre_get_info
,
2455 cmd_get_replication_mode
, NULL
, "", RO
},
2457 /* Logical Router commands. */
2458 {"add-lr", 1, 1, NULL
, pre_get_info
, cmd_add_lr
, NULL
, "--may-exist", RW
},
2459 {"del-lr", 1, 1, NULL
, pre_get_info
, cmd_del_lr
, NULL
, "--if-exists", RW
},
2460 {"list-lr", 0, 0, NULL
, pre_get_info
, cmd_list_lr
, NULL
, "", RO
},
2461 {"lr-exists", 1, 1, NULL
, pre_get_info
, cmd_lr_exists
, NULL
, "", RO
},
2463 /* MAC binding commands. */
2464 {"add-ucast-local", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_local
, NULL
,
2466 {"del-ucast-local", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_local
, NULL
,
2468 {"add-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_local
, NULL
,
2470 {"del-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_local
, NULL
,
2472 {"clear-local-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_local_macs
, NULL
,
2474 {"list-local-macs", 1, 1, NULL
, pre_get_info
, cmd_list_local_macs
, NULL
,
2476 {"add-ucast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_remote
, NULL
,
2478 {"del-ucast-remote", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_remote
, NULL
,
2480 {"add-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_remote
, NULL
,
2482 {"del-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_remote
, NULL
,
2484 {"clear-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_remote_macs
, NULL
,
2486 {"list-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_list_remote_macs
, NULL
,
2489 /* Manager commands. */
2490 {"get-manager", 0, 0, NULL
, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2491 {"del-manager", 0, 0, NULL
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2492 {"set-manager", 1, INT_MAX
, NULL
, pre_manager
, cmd_set_manager
, NULL
, "",
2495 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2498 /* Registers vsctl and common db commands. */
2500 vtep_ctl_cmd_init(void)
2502 ctl_init(vteprec_table_classes
, tables
, cmd_show_tables
, vtep_ctl_exit
);
2503 ctl_register_commands(vtep_commands
);