2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015 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
*,
92 main(int argc
, char *argv
[])
94 extern struct vlog_module VLM_reconnect
;
95 struct ovsdb_idl
*idl
;
96 struct ctl_command
*commands
;
97 struct shash local_options
;
102 set_program_name(argv
[0]);
103 fatal_ignore_sigpipe();
104 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
105 vlog_set_levels(&VLM_reconnect
, VLF_ANY_DESTINATION
, VLL_WARN
);
110 /* Log our arguments. This is often valuable for debugging systems. */
111 args
= process_escape_args(argv
);
112 VLOG(ctl_might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
114 /* Parse command line. */
115 shash_init(&local_options
);
116 parse_options(argc
, argv
, &local_options
);
117 commands
= ctl_parse_commands(argc
- optind
, argv
+ optind
, &local_options
,
124 /* Initialize IDL. */
125 idl
= the_idl
= ovsdb_idl_create(db
, &vteprec_idl_class
, false, false);
126 run_prerequisites(commands
, n_commands
, idl
);
128 /* Execute the commands.
130 * 'seqno' is the database sequence number for which we last tried to
131 * execute our transaction. There's no point in trying to commit more than
132 * once for any given sequence number, because if the transaction fails
133 * it's because the database changed and we need to obtain an up-to-date
134 * view of the database before we try the transaction again. */
135 seqno
= ovsdb_idl_get_seqno(idl
);
139 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
140 seqno
= ovsdb_idl_get_seqno(idl
);
141 do_vtep_ctl(args
, commands
, n_commands
, idl
);
144 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
152 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
155 OPT_DB
= UCHAR_MAX
+ 1,
164 static const struct option global_long_options
[] = {
165 {"db", required_argument
, NULL
, OPT_DB
},
166 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
167 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
168 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
169 {"timeout", required_argument
, NULL
, 't'},
170 {"help", no_argument
, NULL
, 'h'},
171 {"version", no_argument
, NULL
, 'V'},
174 STREAM_SSL_LONG_OPTIONS
,
175 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
178 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
179 char *tmp
, *short_options
;
181 struct option
*options
;
182 size_t allocated_options
;
186 tmp
= ovs_cmdl_long_options_to_short_options(global_long_options
);
187 short_options
= xasprintf("+%s", tmp
);
190 /* We want to parse both global and command-specific options here, but
191 * getopt_long() isn't too convenient for the job. We copy our global
192 * options into a dynamic array, then append all of the command-specific
194 options
= xmemdup(global_long_options
, sizeof global_long_options
);
195 allocated_options
= ARRAY_SIZE(global_long_options
);
196 n_options
= n_global_long_options
;
197 ctl_add_cmd_options(&options
, &n_options
, &allocated_options
, OPT_LOCAL
);
198 table_style
.format
= TF_LIST
;
204 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
219 vlog_set_levels(&VLM_vtep_ctl
, VLF_SYSLOG
, VLL_WARN
);
227 if (shash_find(local_options
, options
[idx
].name
)) {
228 ctl_fatal("'%s' option specified multiple times",
231 shash_add_nocopy(local_options
,
232 xasprintf("--%s", options
[idx
].name
),
233 optarg
? xstrdup(optarg
) : NULL
);
240 ovs_print_version(0, 0);
241 printf("DB Schema %s\n", vteprec_get_db_version());
245 timeout
= strtoul(optarg
, NULL
, 10);
247 ctl_fatal("value %s on -t or --timeout is invalid",
253 TABLE_OPTION_HANDLERS(&table_style
)
255 STREAM_SSL_OPTION_HANDLERS
257 case OPT_PEER_CA_CERT
:
258 stream_ssl_set_peer_ca_cert_file(optarg
);
271 db
= ctl_default_db();
274 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
275 free(CONST_CAST(char *, options
[i
].name
));
280 /* Frees the current transaction and the underlying IDL and then calls
283 * Freeing the transaction and the IDL is not strictly necessary, but it makes
284 * for a clean memory leak report from valgrind in the normal case. That makes
285 * it easier to notice real memory leaks. */
287 vtep_ctl_exit(int status
)
290 ovsdb_idl_txn_abort(the_idl_txn
);
291 ovsdb_idl_txn_destroy(the_idl_txn
);
293 ovsdb_idl_destroy(the_idl
);
301 %s: VTEP configuration utility\n\
302 usage: %s [OPTIONS] COMMAND [ARG...]\n\
305 show print overview of database contents\n\
308 get-manager print the managers\n\
309 del-manager delete the managers\n\
310 set-manager TARGET... set the list of managers to TARGET...\n\
312 Physical Switch commands:\n\
313 add-ps PS create a new physical switch named PS\n\
314 del-ps PS delete PS and all of its ports\n\
315 list-ps print the names of all the physical switches\n\
316 ps-exists PS exit 2 if PS does not exist\n\
319 list-ports PS print the names of all the ports on PS\n\
320 add-port PS PORT add network device PORT to PS\n\
321 del-port PS PORT delete PORT from PS\n\
323 Logical Switch commands:\n\
324 add-ls LS create a new logical switch named LS\n\
325 del-ls LS delete LS and all of its ports\n\
326 list-ls print the names of all the logical switches\n\
327 ls-exists LS exit 2 if LS does not exist\n\
328 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
329 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
330 list-bindings PS PORT list bindings for PORT on PS\n\
332 MAC binding commands:\n\
333 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
334 del-ucast-local LS MAC del ucast local entry from LS\n\
335 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
336 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
337 clear-local-macs LS clear local mac entries\n\
338 list-local-macs LS list local mac entries\n\
339 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
340 del-ucast-remote LS MAC del ucast remote entry from LS\n\
341 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
342 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
343 clear-remote-macs LS clear remote mac entries\n\
344 list-remote-macs LS list remote mac entries\n\
349 --db=DATABASE connect to DATABASE\n\
351 -t, --timeout=SECS wait at most SECS seconds\n\
352 --dry-run do not commit changes to database\n\
353 --oneline print exactly one line of output per command\n",
354 program_name
, program_name
, ctl_get_db_cmd_usage(), ctl_default_db());
357 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
358 stream_usage("database", true, true, false);
361 -h, --help display this help message\n\
362 -V, --version display version information\n");
367 struct cmd_show_table cmd_show_tables
[] = {
368 {&vteprec_table_global
,
370 {&vteprec_global_col_managers
,
371 &vteprec_global_col_switches
,
375 {&vteprec_table_manager
,
376 &vteprec_manager_col_target
,
377 {&vteprec_manager_col_is_connected
,
382 {&vteprec_table_physical_switch
,
383 &vteprec_physical_switch_col_name
,
384 {&vteprec_physical_switch_col_management_ips
,
385 &vteprec_physical_switch_col_tunnel_ips
,
386 &vteprec_physical_switch_col_ports
},
389 {&vteprec_table_physical_port
,
390 &vteprec_physical_port_col_name
,
391 {&vteprec_physical_port_col_vlan_bindings
,
396 {&vteprec_table_logical_switch
,
397 &vteprec_logical_switch_col_name
,
403 {NULL
, NULL
, {NULL
, NULL
, NULL
}, false}
406 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
407 struct vtep_ctl_context
{
408 struct ctl_context base
;
410 /* Modifiable state. */
411 const struct vteprec_global
*vtep_global
;
414 /* A cache of the contents of the database.
416 * A command that needs to use any of this information must first
417 * call vtep_ctl_context_populate_cache(). A command that changes
418 * anything that could invalidate the cache must either call
419 * vtep_ctl_context_invalidate_cache() or manually update the cache
420 * to maintain its correctness. */
422 struct shash pswitches
; /* Maps from physical switch name to
423 * struct vtep_ctl_pswitch. */
424 struct shash ports
; /* Maps from port name to struct vtep_ctl_port. */
426 struct shash lswitches
; /* Maps from logical switch name to
427 * struct vtep_ctl_lswitch. */
428 struct shash plocs
; /* Maps from "<encap>+<dst_ip>" to
429 * struct vteprec_physical_locator. */
432 /* Casts 'base' into 'strcut vtep_ctl_context'. */
433 static struct vtep_ctl_context
*
434 vtep_ctl_context_cast(struct ctl_context
*base
)
436 return CONTAINER_OF(base
, struct vtep_ctl_context
, base
);
439 struct vtep_ctl_pswitch
{
440 const struct vteprec_physical_switch
*ps_cfg
;
442 struct ovs_list ports
; /* Contains "struct vteprec_physical_port"s. */
445 struct vtep_ctl_port
{
446 struct ovs_list ports_node
; /* In struct vtep_ctl_pswitch's 'ports' list. */
447 const struct vteprec_physical_port
*port_cfg
;
448 struct vtep_ctl_pswitch
*ps
;
449 struct shash bindings
; /* Maps from vlan to vtep_ctl_lswitch. */
452 struct vtep_ctl_lswitch
{
453 const struct vteprec_logical_switch
*ls_cfg
;
455 struct shash ucast_local
; /* Maps from mac to vteprec_ucast_macs_local. */
456 struct shash ucast_remote
; /* Maps from mac to vteprec_ucast_macs_remote.*/
457 struct shash mcast_local
; /* Maps from mac to vtep_ctl_mcast_mac. */
458 struct shash mcast_remote
; /* Maps from mac to vtep_ctl_mcast_mac. */
461 struct vtep_ctl_mcast_mac
{
462 const struct vteprec_mcast_macs_local
*local_cfg
;
463 const struct vteprec_mcast_macs_remote
*remote_cfg
;
465 const struct vteprec_physical_locator_set
*ploc_set_cfg
;
466 struct ovs_list locators
; /* Contains 'vtep_ctl_ploc's. */
469 struct vtep_ctl_ploc
{
470 struct ovs_list locators_node
; /* In struct vtep_ctl_ploc_set's 'locators'
472 const struct vteprec_physical_locator
*ploc_cfg
;
476 verify_ports(struct vtep_ctl_context
*vtepctl_ctx
)
478 if (!vtepctl_ctx
->verified_ports
) {
479 const struct vteprec_physical_switch
*ps
;
481 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
482 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps
, vtepctl_ctx
->base
.idl
) {
483 vteprec_physical_switch_verify_ports(ps
);
486 vtepctl_ctx
->verified_ports
= true;
490 static struct vtep_ctl_port
*
491 add_port_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
492 struct vtep_ctl_pswitch
*ps
,
493 struct vteprec_physical_port
*port_cfg
)
495 char *cache_name
= xasprintf("%s+%s", ps
->name
, port_cfg
->name
);
496 struct vtep_ctl_port
*port
;
498 port
= xmalloc(sizeof *port
);
499 list_push_back(&ps
->ports
, &port
->ports_node
);
500 port
->port_cfg
= port_cfg
;
502 shash_add(&vtepctl_ctx
->ports
, cache_name
, port
);
504 shash_init(&port
->bindings
);
510 del_cached_port(struct vtep_ctl_context
*vtepctl_ctx
,
511 struct vtep_ctl_port
*port
)
513 char *cache_name
= xasprintf("%s+%s", port
->ps
->name
, port
->port_cfg
->name
);
515 list_remove(&port
->ports_node
);
516 shash_find_and_delete(&vtepctl_ctx
->ports
, cache_name
);
517 vteprec_physical_port_delete(port
->port_cfg
);
523 add_pswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
524 struct vteprec_physical_switch
*ps_cfg
)
526 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
528 ps
->name
= xstrdup(ps_cfg
->name
);
529 list_init(&ps
->ports
);
530 shash_add(&vtepctl_ctx
->pswitches
, ps
->name
, ps
);
534 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
535 const struct vteprec_physical_switch
*ps
)
537 struct vteprec_physical_switch
**pswitches
;
540 pswitches
= xmalloc(sizeof *vtep_global
->switches
541 * vtep_global
->n_switches
);
542 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
543 if (vtep_global
->switches
[i
] != ps
) {
544 pswitches
[n
++] = vtep_global
->switches
[i
];
547 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
552 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
554 ovs_assert(list_is_empty(&ps
->ports
));
556 vteprec_physical_switch_delete(ps
->ps_cfg
);
557 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
559 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
564 static struct vtep_ctl_lswitch
*
565 add_lswitch_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
566 const struct vteprec_logical_switch
*ls_cfg
)
568 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
570 ls
->name
= xstrdup(ls_cfg
->name
);
571 shash_add(&vtepctl_ctx
->lswitches
, ls
->name
, ls
);
572 shash_init(&ls
->ucast_local
);
573 shash_init(&ls
->ucast_remote
);
574 shash_init(&ls
->mcast_local
);
575 shash_init(&ls
->mcast_remote
);
580 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
583 vteprec_logical_switch_delete(ls
->ls_cfg
);
585 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
591 commit_ls_bindings(struct vtep_ctl_port
*port
)
593 struct vteprec_logical_switch
**binding_values
;
594 int64_t *binding_keys
;
596 struct shash_node
*node
;
599 n_bindings
= shash_count(&port
->bindings
);
600 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
601 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
604 SHASH_FOR_EACH(node
, &port
->bindings
) {
605 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
607 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
608 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
612 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
613 binding_keys
, binding_values
,
615 free(binding_values
);
620 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
622 struct vtep_ctl_lswitch
*ls
)
624 if (shash_find(&port
->bindings
, vlan
)) {
625 ctl_fatal("multiple bindings for vlan %s", vlan
);
628 shash_add(&port
->bindings
, vlan
, ls
);
632 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
634 if (!shash_find(&port
->bindings
, vlan
)) {
635 ctl_fatal("no binding for vlan %s", vlan
);
638 shash_find_and_delete(&port
->bindings
, vlan
);
641 static struct vteprec_physical_locator
*
642 find_ploc(struct vtep_ctl_context
*vtepctl_ctx
, const char *encap
,
645 struct vteprec_physical_locator
*ploc
;
646 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
648 ovs_assert(vtepctl_ctx
->cache_valid
);
650 ploc
= shash_find_data(&vtepctl_ctx
->plocs
, name
);
657 add_ploc_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
658 struct vteprec_physical_locator
*ploc
)
660 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
661 struct vteprec_physical_locator
*orig_ploc
;
663 orig_ploc
= find_ploc(vtepctl_ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
665 shash_add(&vtepctl_ctx
->plocs
, name
, ploc
);
672 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
673 struct vteprec_physical_locator
*ploc_cfg
)
675 struct vtep_ctl_ploc
*ploc
;
677 ploc
= xmalloc(sizeof *ploc
);
678 ploc
->ploc_cfg
= ploc_cfg
;
679 list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
683 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
684 struct vteprec_physical_locator
*ploc_cfg
)
686 struct vtep_ctl_ploc
*ploc
;
688 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
689 if (ploc
->ploc_cfg
== ploc_cfg
) {
690 list_remove(&ploc
->locators_node
);
697 static struct vtep_ctl_mcast_mac
*
698 add_mcast_mac_to_cache(struct vtep_ctl_context
*vtepctl_ctx
,
699 struct vtep_ctl_lswitch
*ls
, const char *mac
,
700 struct vteprec_physical_locator_set
*ploc_set_cfg
,
703 struct vtep_ctl_mcast_mac
*mcast_mac
;
704 struct shash
*mcast_shash
;
707 mcast_mac
= xmalloc(sizeof *mcast_mac
);
708 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
710 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
711 list_init(&mcast_mac
->locators
);
712 shash_add(mcast_shash
, mac
, mcast_mac
);
714 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
715 struct vteprec_physical_locator
*ploc_cfg
;
717 ploc_cfg
= ploc_set_cfg
->locators
[i
];
718 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
719 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
726 vtep_ctl_context_invalidate_cache(struct ctl_context
*ctx
)
728 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
729 struct shash_node
*node
;
731 if (!vtepctl_ctx
->cache_valid
) {
734 vtepctl_ctx
->cache_valid
= false;
736 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
737 struct vtep_ctl_pswitch
*ps
= node
->data
;
741 shash_destroy(&vtepctl_ctx
->pswitches
);
743 SHASH_FOR_EACH (node
, &vtepctl_ctx
->ports
) {
744 struct vtep_ctl_port
*port
= node
->data
;
745 shash_destroy(&port
->bindings
);
747 shash_destroy_free_data(&vtepctl_ctx
->ports
);
749 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
750 struct vtep_ctl_lswitch
*ls
= node
->data
;
751 struct shash_node
*node2
, *next_node2
;
753 shash_destroy(&ls
->ucast_local
);
754 shash_destroy(&ls
->ucast_remote
);
756 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
757 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
758 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
760 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
761 &mcast_mac
->locators
) {
766 shash_destroy(&ls
->mcast_local
);
768 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
769 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
770 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
772 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
773 &mcast_mac
->locators
) {
778 shash_destroy(&ls
->mcast_remote
);
783 shash_destroy(&vtepctl_ctx
->lswitches
);
784 shash_destroy(&vtepctl_ctx
->plocs
);
788 pre_get_info(struct ctl_context
*ctx
)
790 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
792 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
793 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
794 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_tunnels
);
796 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
797 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
799 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
801 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
802 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
803 ovsdb_idl_add_column(ctx
->idl
,
804 &vteprec_ucast_macs_local_col_logical_switch
);
806 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
807 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
808 ovsdb_idl_add_column(ctx
->idl
,
809 &vteprec_ucast_macs_remote_col_logical_switch
);
811 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
812 ovsdb_idl_add_column(ctx
->idl
,
813 &vteprec_mcast_macs_local_col_locator_set
);
814 ovsdb_idl_add_column(ctx
->idl
,
815 &vteprec_mcast_macs_local_col_logical_switch
);
817 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
818 ovsdb_idl_add_column(ctx
->idl
,
819 &vteprec_mcast_macs_remote_col_locator_set
);
820 ovsdb_idl_add_column(ctx
->idl
,
821 &vteprec_mcast_macs_remote_col_logical_switch
);
823 ovsdb_idl_add_column(ctx
->idl
,
824 &vteprec_physical_locator_set_col_locators
);
826 ovsdb_idl_add_column(ctx
->idl
,
827 &vteprec_physical_locator_col_dst_ip
);
828 ovsdb_idl_add_column(ctx
->idl
,
829 &vteprec_physical_locator_col_encapsulation_type
);
831 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_local
);
832 ovsdb_idl_add_column(ctx
->idl
, &vteprec_tunnel_col_remote
);
836 vtep_ctl_context_populate_cache(struct ctl_context
*ctx
)
838 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
839 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
840 const struct vteprec_logical_switch
*ls_cfg
;
841 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
842 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
843 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
844 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
845 const struct vteprec_tunnel
*tunnel_cfg
;
846 struct sset pswitches
, ports
, lswitches
;
849 if (vtepctl_ctx
->cache_valid
) {
850 /* Cache is already populated. */
853 vtepctl_ctx
->cache_valid
= true;
854 shash_init(&vtepctl_ctx
->pswitches
);
855 shash_init(&vtepctl_ctx
->ports
);
856 shash_init(&vtepctl_ctx
->lswitches
);
857 shash_init(&vtepctl_ctx
->plocs
);
859 sset_init(&pswitches
);
861 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
862 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
865 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
866 VLOG_WARN("%s: database contains duplicate physical switch name",
870 add_pswitch_to_cache(vtepctl_ctx
, ps_cfg
);
872 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
873 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
875 if (!sset_add(&ports
, port_cfg
->name
)) {
876 /* Duplicate port name. (We will warn about that later.) */
881 sset_destroy(&pswitches
);
882 sset_destroy(&ports
);
884 sset_init(&lswitches
);
885 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
886 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
887 VLOG_WARN("%s: database contains duplicate logical switch name",
891 add_lswitch_to_cache(vtepctl_ctx
, ls_cfg
);
893 sset_destroy(&lswitches
);
895 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
896 struct vtep_ctl_lswitch
*ls
;
898 if (!ucast_local_cfg
->logical_switch
) {
901 ls
= find_lswitch(vtepctl_ctx
, ucast_local_cfg
->logical_switch
->name
,
907 if (ucast_local_cfg
->locator
) {
908 add_ploc_to_cache(vtepctl_ctx
, ucast_local_cfg
->locator
);
911 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
914 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
915 struct vtep_ctl_lswitch
*ls
;
917 if (!ucast_remote_cfg
->logical_switch
) {
920 ls
= find_lswitch(vtepctl_ctx
, ucast_remote_cfg
->logical_switch
->name
,
926 if (ucast_remote_cfg
->locator
) {
927 add_ploc_to_cache(vtepctl_ctx
, ucast_remote_cfg
->locator
);
930 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
933 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
934 struct vtep_ctl_mcast_mac
*mcast_mac
;
935 struct vtep_ctl_lswitch
*ls
;
937 if (!mcast_local_cfg
->logical_switch
) {
940 ls
= find_lswitch(vtepctl_ctx
, mcast_local_cfg
->logical_switch
->name
,
946 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_local_cfg
->MAC
,
947 mcast_local_cfg
->locator_set
,
949 mcast_mac
->local_cfg
= mcast_local_cfg
;
952 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
953 struct vtep_ctl_mcast_mac
*mcast_mac
;
954 struct vtep_ctl_lswitch
*ls
;
956 if (!mcast_remote_cfg
->logical_switch
) {
959 ls
= find_lswitch(vtepctl_ctx
, mcast_remote_cfg
->logical_switch
->name
,
965 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mcast_remote_cfg
->MAC
,
966 mcast_remote_cfg
->locator_set
,
968 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
971 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg
, ctx
->idl
) {
972 if (tunnel_cfg
->local
) {
973 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->local
);
975 if (tunnel_cfg
->remote
) {
976 add_ploc_to_cache(vtepctl_ctx
, tunnel_cfg
->remote
);
980 sset_init(&pswitches
);
981 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
982 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
983 struct vtep_ctl_pswitch
*ps
;
986 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
989 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, ps_cfg
->name
);
990 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
991 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
992 struct vtep_ctl_port
*port
;
995 port
= shash_find_data(&vtepctl_ctx
->ports
, port_cfg
->name
);
997 if (port_cfg
== port
->port_cfg
) {
998 VLOG_WARN("%s: port is in multiple physical switches "
1000 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1002 /* Log as an error because this violates the database's
1003 * uniqueness constraints, so the database server shouldn't
1004 * have allowed it. */
1005 VLOG_ERR("%s: database contains duplicate port name",
1011 port
= add_port_to_cache(vtepctl_ctx
, ps
, port_cfg
);
1013 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
1014 struct vteprec_logical_switch
*ls_cfg
;
1015 struct vtep_ctl_lswitch
*ls
;
1018 vlan
= xasprintf("%"PRId64
, port_cfg
->key_vlan_bindings
[k
]);
1019 if (shash_find(&port
->bindings
, vlan
)) {
1020 ctl_fatal("multiple bindings for vlan %s", vlan
);
1023 ls_cfg
= port_cfg
->value_vlan_bindings
[k
];
1024 ls
= find_lswitch(vtepctl_ctx
, ls_cfg
->name
, true);
1026 shash_add_nocopy(&port
->bindings
, vlan
, ls
);
1030 sset_destroy(&pswitches
);
1033 static struct vtep_ctl_pswitch
*
1034 find_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, const char *name
, bool must_exist
)
1036 struct vtep_ctl_pswitch
*ps
;
1038 ovs_assert(vtepctl_ctx
->cache_valid
);
1040 ps
= shash_find_data(&vtepctl_ctx
->pswitches
, name
);
1041 if (must_exist
&& !ps
) {
1042 ctl_fatal("no physical switch named %s", name
);
1044 vteprec_global_verify_switches(vtepctl_ctx
->vtep_global
);
1048 static struct vtep_ctl_port
*
1049 find_port(struct vtep_ctl_context
*vtepctl_ctx
, const char *ps_name
,
1050 const char *port_name
, bool must_exist
)
1052 char *cache_name
= xasprintf("%s+%s", ps_name
, port_name
);
1053 struct vtep_ctl_port
*port
;
1055 ovs_assert(vtepctl_ctx
->cache_valid
);
1057 port
= shash_find_data(&vtepctl_ctx
->ports
, cache_name
);
1058 if (port
&& !strcmp(port_name
, port
->ps
->name
)) {
1062 if (must_exist
&& !port
) {
1063 ctl_fatal("no port named %s", port_name
);
1065 verify_ports(vtepctl_ctx
);
1070 pswitch_insert_port(const struct vteprec_physical_switch
*ps
,
1071 struct vteprec_physical_port
*port
)
1073 struct vteprec_physical_port
**ports
;
1076 ports
= xmalloc(sizeof *ps
->ports
* (ps
->n_ports
+ 1));
1077 for (i
= 0; i
< ps
->n_ports
; i
++) {
1078 ports
[i
] = ps
->ports
[i
];
1080 ports
[ps
->n_ports
] = port
;
1081 vteprec_physical_switch_set_ports(ps
, ports
, ps
->n_ports
+ 1);
1086 pswitch_delete_port(const struct vteprec_physical_switch
*ps
,
1087 const struct vteprec_physical_port
*port
)
1089 struct vteprec_physical_port
**ports
;
1092 ports
= xmalloc(sizeof *ps
->ports
* ps
->n_ports
);
1093 for (i
= n
= 0; i
< ps
->n_ports
; i
++) {
1094 if (ps
->ports
[i
] != port
) {
1095 ports
[n
++] = ps
->ports
[i
];
1098 vteprec_physical_switch_set_ports(ps
, ports
, n
);
1103 vtep_insert_pswitch(const struct vteprec_global
*vtep_global
,
1104 struct vteprec_physical_switch
*ps
)
1106 struct vteprec_physical_switch
**pswitches
;
1109 pswitches
= xmalloc(sizeof *vtep_global
->switches
1110 * (vtep_global
->n_switches
+ 1));
1111 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1112 pswitches
[i
] = vtep_global
->switches
[i
];
1114 pswitches
[vtep_global
->n_switches
] = ps
;
1115 vteprec_global_set_switches(vtep_global
, pswitches
,
1116 vtep_global
->n_switches
+ 1);
1121 cmd_add_ps(struct ctl_context
*ctx
)
1123 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1124 const char *ps_name
= ctx
->argv
[1];
1125 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1126 struct vteprec_physical_switch
*ps
;
1128 vtep_ctl_context_populate_cache(ctx
);
1129 if (find_pswitch(vtepctl_ctx
, ps_name
, false)) {
1131 ctl_fatal("cannot create physical switch %s because it "
1132 "already exists", ps_name
);
1137 ps
= vteprec_physical_switch_insert(ctx
->txn
);
1138 vteprec_physical_switch_set_name(ps
, ps_name
);
1140 vtep_insert_pswitch(vtepctl_ctx
->vtep_global
, ps
);
1142 vtep_ctl_context_invalidate_cache(ctx
);
1146 del_port(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_port
*port
)
1148 pswitch_delete_port(port
->ps
->ps_cfg
, port
->port_cfg
);
1149 del_cached_port(vtepctl_ctx
, port
);
1153 del_pswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_pswitch
*ps
)
1155 struct vtep_ctl_port
*port
, *next_port
;
1157 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &ps
->ports
) {
1158 del_port(vtepctl_ctx
, port
);
1161 del_cached_pswitch(vtepctl_ctx
, ps
);
1165 cmd_del_ps(struct ctl_context
*ctx
)
1167 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1168 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1169 struct vtep_ctl_pswitch
*ps
;
1171 vtep_ctl_context_populate_cache(ctx
);
1172 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1174 del_pswitch(vtepctl_ctx
, ps
);
1179 output_sorted(struct svec
*svec
, struct ds
*output
)
1185 SVEC_FOR_EACH (i
, name
, svec
) {
1186 ds_put_format(output
, "%s\n", name
);
1191 cmd_list_ps(struct ctl_context
*ctx
)
1193 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1194 struct shash_node
*node
;
1195 struct svec pswitches
;
1197 vtep_ctl_context_populate_cache(ctx
);
1199 svec_init(&pswitches
);
1200 SHASH_FOR_EACH (node
, &vtepctl_ctx
->pswitches
) {
1201 struct vtep_ctl_pswitch
*ps
= node
->data
;
1203 svec_add(&pswitches
, ps
->name
);
1205 output_sorted(&pswitches
, &ctx
->output
);
1206 svec_destroy(&pswitches
);
1210 cmd_ps_exists(struct ctl_context
*ctx
)
1212 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1214 vtep_ctl_context_populate_cache(ctx
);
1215 if (!find_pswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1221 cmd_list_ports(struct ctl_context
*ctx
)
1223 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1224 struct vtep_ctl_pswitch
*ps
;
1225 struct vtep_ctl_port
*port
;
1228 vtep_ctl_context_populate_cache(ctx
);
1229 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1230 vteprec_physical_switch_verify_ports(ps
->ps_cfg
);
1233 LIST_FOR_EACH (port
, ports_node
, &ps
->ports
) {
1234 if (strcmp(port
->port_cfg
->name
, ps
->name
)) {
1235 svec_add(&ports
, port
->port_cfg
->name
);
1238 output_sorted(&ports
, &ctx
->output
);
1239 svec_destroy(&ports
);
1243 add_port(struct ctl_context
*ctx
, const char *ps_name
,
1244 const char *port_name
, bool may_exist
)
1246 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1247 struct vtep_ctl_port
*vtep_ctl_port
;
1248 struct vtep_ctl_pswitch
*ps
;
1249 struct vteprec_physical_port
*port
;
1251 vtep_ctl_context_populate_cache(ctx
);
1253 vtep_ctl_port
= find_port(vtepctl_ctx
, ps_name
, port_name
, false);
1254 if (vtep_ctl_port
) {
1256 ctl_fatal("cannot create a port named %s on %s because a "
1257 "port with that name already exists",
1258 port_name
, ps_name
);
1263 ps
= find_pswitch(vtepctl_ctx
, ps_name
, true);
1265 port
= vteprec_physical_port_insert(ctx
->txn
);
1266 vteprec_physical_port_set_name(port
, port_name
);
1268 pswitch_insert_port(ps
->ps_cfg
, port
);
1270 add_port_to_cache(vtepctl_ctx
, ps
, port
);
1274 cmd_add_port(struct ctl_context
*ctx
)
1276 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1278 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
);
1282 cmd_del_port(struct ctl_context
*ctx
)
1284 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1285 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1286 struct vtep_ctl_port
*port
;
1288 vtep_ctl_context_populate_cache(ctx
);
1290 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], must_exist
);
1292 if (ctx
->argc
== 3) {
1293 struct vtep_ctl_pswitch
*ps
;
1295 ps
= find_pswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1296 if (port
->ps
!= ps
) {
1297 ctl_fatal("physical switch %s does not have a port %s",
1298 ctx
->argv
[1], ctx
->argv
[2]);
1302 del_port(vtepctl_ctx
, port
);
1306 static struct vtep_ctl_lswitch
*
1307 find_lswitch(struct vtep_ctl_context
*vtepctl_ctx
,
1308 const char *name
, bool must_exist
)
1310 struct vtep_ctl_lswitch
*ls
;
1312 ovs_assert(vtepctl_ctx
->cache_valid
);
1314 ls
= shash_find_data(&vtepctl_ctx
->lswitches
, name
);
1315 if (must_exist
&& !ls
) {
1316 ctl_fatal("no logical switch named %s", name
);
1322 cmd_add_ls(struct ctl_context
*ctx
)
1324 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1325 const char *ls_name
= ctx
->argv
[1];
1326 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1327 struct vteprec_logical_switch
*ls
;
1329 vtep_ctl_context_populate_cache(ctx
);
1330 if (find_lswitch(vtepctl_ctx
, ls_name
, false)) {
1332 ctl_fatal("cannot create logical switch %s because it "
1333 "already exists", ls_name
);
1338 ls
= vteprec_logical_switch_insert(ctx
->txn
);
1339 vteprec_logical_switch_set_name(ls
, ls_name
);
1341 vtep_ctl_context_invalidate_cache(ctx
);
1345 del_lswitch(struct vtep_ctl_context
*vtepctl_ctx
, struct vtep_ctl_lswitch
*ls
)
1347 del_cached_lswitch(vtepctl_ctx
, ls
);
1351 cmd_del_ls(struct ctl_context
*ctx
)
1353 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1354 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1355 struct vtep_ctl_lswitch
*ls
;
1357 vtep_ctl_context_populate_cache(ctx
);
1358 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], must_exist
);
1360 del_lswitch(vtepctl_ctx
, ls
);
1365 cmd_list_ls(struct ctl_context
*ctx
)
1367 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1368 struct shash_node
*node
;
1369 struct svec lswitches
;
1371 vtep_ctl_context_populate_cache(ctx
);
1373 svec_init(&lswitches
);
1374 SHASH_FOR_EACH (node
, &vtepctl_ctx
->lswitches
) {
1375 struct vtep_ctl_lswitch
*ls
= node
->data
;
1377 svec_add(&lswitches
, ls
->name
);
1379 output_sorted(&lswitches
, &ctx
->output
);
1380 svec_destroy(&lswitches
);
1384 cmd_ls_exists(struct ctl_context
*ctx
)
1386 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1388 vtep_ctl_context_populate_cache(ctx
);
1389 if (!find_lswitch(vtepctl_ctx
, ctx
->argv
[1], false)) {
1395 cmd_list_bindings(struct ctl_context
*ctx
)
1397 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1398 const struct shash_node
*node
;
1399 struct vtep_ctl_port
*port
;
1400 struct svec bindings
;
1402 vtep_ctl_context_populate_cache(ctx
);
1403 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1405 svec_init(&bindings
);
1406 SHASH_FOR_EACH (node
, &port
->bindings
) {
1407 struct vtep_ctl_lswitch
*lswitch
= node
->data
;
1410 binding
= xasprintf("%04lld %s", strtoll(node
->name
, NULL
, 0),
1412 svec_add_nocopy(&bindings
, binding
);
1414 output_sorted(&bindings
, &ctx
->output
);
1415 svec_destroy(&bindings
);
1419 cmd_bind_ls(struct ctl_context
*ctx
)
1421 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1422 struct vtep_ctl_lswitch
*ls
;
1423 struct vtep_ctl_port
*port
;
1426 vtep_ctl_context_populate_cache(ctx
);
1428 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1429 vlan
= ctx
->argv
[3];
1430 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[4], true);
1432 add_ls_binding_to_cache(port
, vlan
, ls
);
1433 commit_ls_bindings(port
);
1435 vtep_ctl_context_invalidate_cache(ctx
);
1439 cmd_unbind_ls(struct ctl_context
*ctx
)
1441 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1442 struct vtep_ctl_port
*port
;
1445 vtep_ctl_context_populate_cache(ctx
);
1447 port
= find_port(vtepctl_ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1448 vlan
= ctx
->argv
[3];
1450 del_cached_ls_binding(port
, vlan
);
1451 commit_ls_bindings(port
);
1453 vtep_ctl_context_invalidate_cache(ctx
);
1457 add_ucast_entry(struct ctl_context
*ctx
, bool local
)
1459 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1460 struct vtep_ctl_lswitch
*ls
;
1464 struct vteprec_physical_locator
*ploc_cfg
;
1466 vtep_ctl_context_populate_cache(ctx
);
1468 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1471 if (ctx
->argc
== 4) {
1472 encap
= "vxlan_over_ipv4";
1473 dst_ip
= ctx
->argv
[3];
1475 encap
= ctx
->argv
[3];
1476 dst_ip
= ctx
->argv
[4];
1479 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1481 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1482 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1483 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1485 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1489 struct vteprec_ucast_macs_local
*ucast_cfg
;
1491 ucast_cfg
= shash_find_data(&ls
->ucast_local
, mac
);
1493 ucast_cfg
= vteprec_ucast_macs_local_insert(ctx
->txn
);
1494 vteprec_ucast_macs_local_set_MAC(ucast_cfg
, mac
);
1495 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1496 shash_add(&ls
->ucast_local
, mac
, ucast_cfg
);
1498 vteprec_ucast_macs_local_set_locator(ucast_cfg
, ploc_cfg
);
1500 struct vteprec_ucast_macs_remote
*ucast_cfg
;
1502 ucast_cfg
= shash_find_data(&ls
->ucast_remote
, mac
);
1504 ucast_cfg
= vteprec_ucast_macs_remote_insert(ctx
->txn
);
1505 vteprec_ucast_macs_remote_set_MAC(ucast_cfg
, mac
);
1506 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1507 shash_add(&ls
->ucast_remote
, mac
, ucast_cfg
);
1509 vteprec_ucast_macs_remote_set_locator(ucast_cfg
, ploc_cfg
);
1512 vtep_ctl_context_invalidate_cache(ctx
);
1516 cmd_add_ucast_local(struct ctl_context
*ctx
)
1518 add_ucast_entry(ctx
, true);
1522 cmd_add_ucast_remote(struct ctl_context
*ctx
)
1524 add_ucast_entry(ctx
, false);
1528 del_ucast_entry(struct ctl_context
*ctx
, bool local
)
1530 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1531 struct vtep_ctl_lswitch
*ls
;
1532 struct shash
*ucast_shash
;
1533 struct shash_node
*node
;
1535 vtep_ctl_context_populate_cache(ctx
);
1537 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1538 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1540 node
= shash_find(ucast_shash
, ctx
->argv
[2]);
1546 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1547 vteprec_ucast_macs_local_delete(ucast_cfg
);
1549 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1550 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1552 shash_delete(ucast_shash
, node
);
1554 vtep_ctl_context_invalidate_cache(ctx
);
1558 cmd_del_ucast_local(struct ctl_context
*ctx
)
1560 del_ucast_entry(ctx
, true);
1564 cmd_del_ucast_remote(struct ctl_context
*ctx
)
1566 del_ucast_entry(ctx
, false);
1570 commit_mcast_entries(struct vtep_ctl_mcast_mac
*mcast_mac
)
1572 struct vtep_ctl_ploc
*ploc
;
1573 struct vteprec_physical_locator
**locators
= NULL
;
1577 n_locators
= list_size(&mcast_mac
->locators
);
1578 ovs_assert(n_locators
);
1580 locators
= xmalloc(n_locators
* sizeof *locators
);
1583 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1584 locators
[i
] = (struct vteprec_physical_locator
*)ploc
->ploc_cfg
;
1588 vteprec_physical_locator_set_set_locators(mcast_mac
->ploc_set_cfg
,
1596 add_mcast_entry(struct ctl_context
*ctx
,
1597 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1598 const char *encap
, const char *dst_ip
, bool local
)
1600 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1601 struct shash
*mcast_shash
;
1602 struct vtep_ctl_mcast_mac
*mcast_mac
;
1603 struct vteprec_physical_locator
*ploc_cfg
;
1604 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1606 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1608 /* Physical locator sets are immutable, so allocate a new one. */
1609 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1611 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1613 mcast_mac
= add_mcast_mac_to_cache(vtepctl_ctx
, ls
, mac
, ploc_set_cfg
,
1617 mcast_mac
->local_cfg
= vteprec_mcast_macs_local_insert(ctx
->txn
);
1618 vteprec_mcast_macs_local_set_MAC(mcast_mac
->local_cfg
, mac
);
1619 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1621 vteprec_mcast_macs_local_set_logical_switch(mcast_mac
->local_cfg
,
1623 mcast_mac
->remote_cfg
= NULL
;
1625 mcast_mac
->remote_cfg
= vteprec_mcast_macs_remote_insert(ctx
->txn
);
1626 vteprec_mcast_macs_remote_set_MAC(mcast_mac
->remote_cfg
, mac
);
1627 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1629 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac
->remote_cfg
,
1631 mcast_mac
->local_cfg
= NULL
;
1634 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1636 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1639 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1644 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1646 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1647 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1648 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1650 add_ploc_to_cache(vtepctl_ctx
, ploc_cfg
);
1653 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1654 commit_mcast_entries(mcast_mac
);
1658 del_mcast_entry(struct ctl_context
*ctx
,
1659 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1660 const char *encap
, const char *dst_ip
, bool local
)
1662 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1663 struct vtep_ctl_mcast_mac
*mcast_mac
;
1664 struct shash
*mcast_shash
;
1665 struct vteprec_physical_locator
*ploc_cfg
;
1666 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1668 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1670 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1675 ploc_cfg
= find_ploc(vtepctl_ctx
, encap
, dst_ip
);
1677 /* Couldn't find the physical locator, so just ignore. */
1681 /* Physical locator sets are immutable, so allocate a new one. */
1682 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1683 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1685 del_ploc_from_mcast_mac(mcast_mac
, ploc_cfg
);
1686 if (list_is_empty(&mcast_mac
->locators
)) {
1687 struct shash_node
*node
= shash_find(mcast_shash
, mac
);
1689 vteprec_physical_locator_set_delete(ploc_set_cfg
);
1692 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1694 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1698 shash_delete(mcast_shash
, node
);
1701 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1704 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1707 commit_mcast_entries(mcast_mac
);
1712 add_del_mcast_entry(struct ctl_context
*ctx
, bool add
, bool local
)
1714 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1715 struct vtep_ctl_lswitch
*ls
;
1720 vtep_ctl_context_populate_cache(ctx
);
1722 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1725 if (ctx
->argc
== 4) {
1726 encap
= "vxlan_over_ipv4";
1727 dst_ip
= ctx
->argv
[3];
1729 encap
= ctx
->argv
[3];
1730 dst_ip
= ctx
->argv
[4];
1734 add_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1736 del_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1739 vtep_ctl_context_invalidate_cache(ctx
);
1743 cmd_add_mcast_local(struct ctl_context
*ctx
)
1745 add_del_mcast_entry(ctx
, true, true);
1749 cmd_add_mcast_remote(struct ctl_context
*ctx
)
1751 add_del_mcast_entry(ctx
, true, false);
1755 cmd_del_mcast_local(struct ctl_context
*ctx
)
1757 add_del_mcast_entry(ctx
, false, true);
1761 cmd_del_mcast_remote(struct ctl_context
*ctx
)
1763 add_del_mcast_entry(ctx
, false, false);
1767 clear_macs(struct ctl_context
*ctx
, bool local
)
1769 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1770 struct vtep_ctl_lswitch
*ls
;
1771 const struct shash_node
*node
;
1772 struct shash
*ucast_shash
;
1773 struct shash
*mcast_shash
;
1775 vtep_ctl_context_populate_cache(ctx
);
1776 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1778 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1779 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1781 SHASH_FOR_EACH (node
, ucast_shash
) {
1783 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1784 vteprec_ucast_macs_local_delete(ucast_cfg
);
1786 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1787 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1791 SHASH_FOR_EACH (node
, mcast_shash
) {
1792 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
1794 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1796 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1800 vtep_ctl_context_invalidate_cache(ctx
);
1804 cmd_clear_local_macs(struct ctl_context
*ctx
)
1806 clear_macs(ctx
, true);
1810 cmd_clear_remote_macs(struct ctl_context
*ctx
)
1812 clear_macs(ctx
, false);
1816 list_macs(struct ctl_context
*ctx
, bool local
)
1818 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1819 struct vtep_ctl_lswitch
*ls
;
1820 const struct shash_node
*node
;
1821 struct shash
*ucast_shash
;
1822 struct svec ucast_macs
;
1823 struct shash
*mcast_shash
;
1824 struct svec mcast_macs
;
1826 vtep_ctl_context_populate_cache(ctx
);
1827 ls
= find_lswitch(vtepctl_ctx
, ctx
->argv
[1], true);
1829 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1830 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1832 svec_init(&ucast_macs
);
1833 SHASH_FOR_EACH (node
, ucast_shash
) {
1834 struct vteprec_ucast_macs_local
*ucast_local
= node
->data
;
1835 struct vteprec_ucast_macs_remote
*ucast_remote
= node
->data
;
1836 struct vteprec_physical_locator
*ploc_cfg
;
1839 ploc_cfg
= local
? ucast_local
->locator
: ucast_remote
->locator
;
1841 entry
= xasprintf(" %s -> %s/%s", node
->name
,
1842 ploc_cfg
->encapsulation_type
, ploc_cfg
->dst_ip
);
1843 svec_add_nocopy(&ucast_macs
, entry
);
1845 ds_put_format(&ctx
->output
, "ucast-mac-%s\n", local
? "local" : "remote");
1846 output_sorted(&ucast_macs
, &ctx
->output
);
1847 ds_put_char(&ctx
->output
, '\n');
1848 svec_destroy(&ucast_macs
);
1850 svec_init(&mcast_macs
);
1851 SHASH_FOR_EACH (node
, mcast_shash
) {
1852 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
1853 struct vtep_ctl_ploc
*ploc
;
1856 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1857 entry
= xasprintf(" %s -> %s/%s", node
->name
,
1858 ploc
->ploc_cfg
->encapsulation_type
,
1859 ploc
->ploc_cfg
->dst_ip
);
1860 svec_add_nocopy(&mcast_macs
, entry
);
1863 ds_put_format(&ctx
->output
, "mcast-mac-%s\n", local
? "local" : "remote");
1864 output_sorted(&mcast_macs
, &ctx
->output
);
1865 ds_put_char(&ctx
->output
, '\n');
1866 svec_destroy(&mcast_macs
);
1870 cmd_list_local_macs(struct ctl_context
*ctx
)
1872 list_macs(ctx
, true);
1876 cmd_list_remote_macs(struct ctl_context
*ctx
)
1878 list_macs(ctx
, false);
1882 verify_managers(const struct vteprec_global
*vtep_global
)
1886 vteprec_global_verify_managers(vtep_global
);
1888 for (i
= 0; i
< vtep_global
->n_managers
; ++i
) {
1889 const struct vteprec_manager
*mgr
= vtep_global
->managers
[i
];
1891 vteprec_manager_verify_target(mgr
);
1896 pre_manager(struct ctl_context
*ctx
)
1898 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_managers
);
1899 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_target
);
1903 cmd_get_manager(struct ctl_context
*ctx
)
1905 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1906 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
1907 struct svec targets
;
1910 verify_managers(vtep_global
);
1912 /* Print the targets in sorted order for reproducibility. */
1913 svec_init(&targets
);
1915 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
1916 svec_add(&targets
, vtep_global
->managers
[i
]->target
);
1919 svec_sort_unique(&targets
);
1920 for (i
= 0; i
< targets
.n
; i
++) {
1921 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
1923 svec_destroy(&targets
);
1927 delete_managers(const struct vtep_ctl_context
*vtepctl_ctx
)
1929 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
1932 /* Delete Manager rows pointed to by 'managers' column. */
1933 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
1934 vteprec_manager_delete(vtep_global
->managers
[i
]);
1937 /* Delete 'Manager' row refs in 'managers' column. */
1938 vteprec_global_set_managers(vtep_global
, NULL
, 0);
1942 cmd_del_manager(struct ctl_context
*ctx
)
1944 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1945 const struct vteprec_global
*vtep_global
= vtepctl_ctx
->vtep_global
;
1947 verify_managers(vtep_global
);
1948 delete_managers(vtepctl_ctx
);
1952 insert_managers(struct vtep_ctl_context
*vtepctl_ctx
, char *targets
[], size_t n
)
1954 struct vteprec_manager
**managers
;
1957 /* Insert each manager in a new row in Manager table. */
1958 managers
= xmalloc(n
* sizeof *managers
);
1959 for (i
= 0; i
< n
; i
++) {
1960 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
1961 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
1963 managers
[i
] = vteprec_manager_insert(vtepctl_ctx
->base
.txn
);
1964 vteprec_manager_set_target(managers
[i
], targets
[i
]);
1967 /* Store uuids of new Manager rows in 'managers' column. */
1968 vteprec_global_set_managers(vtepctl_ctx
->vtep_global
, managers
, n
);
1973 cmd_set_manager(struct ctl_context
*ctx
)
1975 struct vtep_ctl_context
*vtepctl_ctx
= vtep_ctl_context_cast(ctx
);
1976 const size_t n
= ctx
->argc
- 1;
1978 verify_managers(vtepctl_ctx
->vtep_global
);
1979 delete_managers(vtepctl_ctx
);
1980 insert_managers(vtepctl_ctx
, &ctx
->argv
[1], n
);
1983 /* Parameter commands. */
1984 static const struct ctl_table_class tables
[] = {
1985 {&vteprec_table_global
,
1986 {{&vteprec_table_global
, NULL
, NULL
},
1987 {NULL
, NULL
, NULL
}}},
1989 {&vteprec_table_logical_binding_stats
,
1990 {{NULL
, NULL
, NULL
},
1991 {NULL
, NULL
, NULL
}}},
1993 {&vteprec_table_logical_switch
,
1994 {{&vteprec_table_logical_switch
, &vteprec_logical_switch_col_name
, NULL
},
1995 {NULL
, NULL
, NULL
}}},
1997 {&vteprec_table_ucast_macs_local
,
1998 {{NULL
, NULL
, NULL
},
1999 {NULL
, NULL
, NULL
}}},
2001 {&vteprec_table_ucast_macs_remote
,
2002 {{NULL
, NULL
, NULL
},
2003 {NULL
, NULL
, NULL
}}},
2005 {&vteprec_table_mcast_macs_local
,
2006 {{NULL
, NULL
, NULL
},
2007 {NULL
, NULL
, NULL
}}},
2009 {&vteprec_table_mcast_macs_remote
,
2010 {{NULL
, NULL
, NULL
},
2011 {NULL
, NULL
, NULL
}}},
2013 {&vteprec_table_manager
,
2014 {{&vteprec_table_manager
, &vteprec_manager_col_target
, NULL
},
2015 {NULL
, NULL
, NULL
}}},
2017 {&vteprec_table_physical_locator
,
2018 {{NULL
, NULL
, NULL
},
2019 {NULL
, NULL
, NULL
}}},
2021 {&vteprec_table_physical_locator_set
,
2022 {{NULL
, NULL
, NULL
},
2023 {NULL
, NULL
, NULL
}}},
2025 {&vteprec_table_physical_port
,
2026 {{&vteprec_table_physical_port
, &vteprec_physical_port_col_name
, NULL
},
2027 {NULL
, NULL
, NULL
}}},
2029 {&vteprec_table_physical_switch
,
2030 {{&vteprec_table_physical_switch
, &vteprec_physical_switch_col_name
, NULL
},
2031 {NULL
, NULL
, NULL
}}},
2033 {&vteprec_table_tunnel
,
2034 {{NULL
, NULL
, NULL
},
2035 {NULL
, NULL
, NULL
}}},
2037 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
2042 vtep_ctl_context_init_command(struct vtep_ctl_context
*vtepctl_ctx
,
2043 struct ctl_command
*command
)
2045 ctl_context_init_command(&vtepctl_ctx
->base
, command
);
2046 vtepctl_ctx
->verified_ports
= false;
2051 vtep_ctl_context_init(struct vtep_ctl_context
*vtepctl_ctx
,
2052 struct ctl_command
*command
,
2053 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2054 const struct vteprec_global
*vtep_global
,
2055 struct ovsdb_symbol_table
*symtab
)
2057 ctl_context_init(&vtepctl_ctx
->base
, command
, idl
, txn
, symtab
,
2058 vtep_ctl_context_invalidate_cache
);
2060 vtepctl_ctx
->verified_ports
= false;
2062 vtepctl_ctx
->vtep_global
= vtep_global
;
2063 vtepctl_ctx
->cache_valid
= false;
2067 vtep_ctl_context_done_command(struct vtep_ctl_context
*vtepctl_ctx
,
2068 struct ctl_command
*command
)
2070 ctl_context_done_command(&vtepctl_ctx
->base
, command
);
2074 vtep_ctl_context_done(struct vtep_ctl_context
*vtepctl_ctx
,
2075 struct ctl_command
*command
)
2077 ctl_context_done(&vtepctl_ctx
->base
, command
);
2081 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
2082 struct ovsdb_idl
*idl
)
2084 struct ctl_command
*c
;
2086 ovsdb_idl_add_table(idl
, &vteprec_table_global
);
2087 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2088 if (c
->syntax
->prerequisites
) {
2089 struct vtep_ctl_context vtepctl_ctx
;
2091 ds_init(&c
->output
);
2094 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, NULL
, NULL
, NULL
);
2095 (c
->syntax
->prerequisites
)(&vtepctl_ctx
.base
);
2096 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2098 ovs_assert(!c
->output
.string
);
2099 ovs_assert(!c
->table
);
2105 do_vtep_ctl(const char *args
, struct ctl_command
*commands
,
2106 size_t n_commands
, struct ovsdb_idl
*idl
)
2108 struct ovsdb_idl_txn
*txn
;
2109 const struct vteprec_global
*vtep_global
;
2110 enum ovsdb_idl_txn_status status
;
2111 struct ovsdb_symbol_table
*symtab
;
2112 struct vtep_ctl_context vtepctl_ctx
;
2113 struct ctl_command
*c
;
2114 struct shash_node
*node
;
2117 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
2119 ovsdb_idl_txn_set_dry_run(txn
);
2122 ovsdb_idl_txn_add_comment(txn
, "vtep-ctl: %s", args
);
2124 vtep_global
= vteprec_global_first(idl
);
2126 /* XXX add verification that table is empty */
2127 vtep_global
= vteprec_global_insert(txn
);
2130 symtab
= ovsdb_symbol_table_create();
2131 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2132 ds_init(&c
->output
);
2135 vtep_ctl_context_init(&vtepctl_ctx
, NULL
, idl
, txn
, vtep_global
, symtab
);
2136 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2137 vtep_ctl_context_init_command(&vtepctl_ctx
, c
);
2138 if (c
->syntax
->run
) {
2139 (c
->syntax
->run
)(&vtepctl_ctx
.base
);
2141 vtep_ctl_context_done_command(&vtepctl_ctx
, c
);
2143 if (vtepctl_ctx
.base
.try_again
) {
2144 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2148 vtep_ctl_context_done(&vtepctl_ctx
, NULL
);
2150 SHASH_FOR_EACH (node
, &symtab
->sh
) {
2151 struct ovsdb_symbol
*symbol
= node
->data
;
2152 if (!symbol
->created
) {
2153 ctl_fatal("row id \"%s\" is referenced but never created "
2154 "(e.g. with \"-- --id=%s create ...\")",
2155 node
->name
, node
->name
);
2157 if (!symbol
->strong_ref
) {
2158 if (!symbol
->weak_ref
) {
2159 VLOG_WARN("row id \"%s\" was created but no reference to it "
2160 "was inserted, so it will not actually appear in "
2161 "the database", node
->name
);
2163 VLOG_WARN("row id \"%s\" was created but only a weak "
2164 "reference to it was inserted, so it will not "
2165 "actually appear in the database", node
->name
);
2170 status
= ovsdb_idl_txn_commit_block(txn
);
2171 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
2172 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2173 if (c
->syntax
->postprocess
) {
2174 vtep_ctl_context_init(&vtepctl_ctx
, c
, idl
, txn
, vtep_global
, symtab
);
2175 (c
->syntax
->postprocess
)(&vtepctl_ctx
.base
);
2176 vtep_ctl_context_done(&vtepctl_ctx
, c
);
2180 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
2181 ovsdb_idl_txn_destroy(txn
);
2182 txn
= the_idl_txn
= NULL
;
2185 case TXN_UNCOMMITTED
:
2186 case TXN_INCOMPLETE
:
2190 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2191 ctl_fatal("transaction aborted");
2201 ctl_fatal("transaction error: %s", error
);
2203 case TXN_NOT_LOCKED
:
2204 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2205 ctl_fatal("database not locked");
2212 ovsdb_symbol_table_destroy(symtab
);
2214 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2215 struct ds
*ds
= &c
->output
;
2218 table_print(c
->table
, &table_style
);
2219 } else if (oneline
) {
2223 for (j
= 0; j
< ds
->length
; j
++) {
2224 int ch
= ds
->string
[j
];
2227 fputs("\\n", stdout
);
2231 fputs("\\\\", stdout
);
2240 fputs(ds_cstr(ds
), stdout
);
2242 ds_destroy(&c
->output
);
2243 table_destroy(c
->table
);
2246 shash_destroy_free_data(&c
->options
);
2250 ovsdb_idl_destroy(idl
);
2255 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2256 * resources and return so that the caller can try again. */
2258 ovsdb_idl_txn_abort(txn
);
2259 ovsdb_idl_txn_destroy(txn
);
2261 ovsdb_symbol_table_destroy(symtab
);
2262 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2263 ds_destroy(&c
->output
);
2264 table_destroy(c
->table
);
2270 static const struct ctl_command_syntax vtep_commands
[] = {
2271 /* Physical Switch commands. */
2272 {"add-ps", 1, 1, NULL
, pre_get_info
, cmd_add_ps
, NULL
, "--may-exist", RW
},
2273 {"del-ps", 1, 1, NULL
, pre_get_info
, cmd_del_ps
, NULL
, "--if-exists", RW
},
2274 {"list-ps", 0, 0, NULL
, pre_get_info
, cmd_list_ps
, NULL
, "", RO
},
2275 {"ps-exists", 1, 1, NULL
, pre_get_info
, cmd_ps_exists
, NULL
, "", RO
},
2277 /* Port commands. */
2278 {"list-ports", 1, 1, NULL
, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
2279 {"add-port", 2, 2, NULL
, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
2281 {"del-port", 2, 2, NULL
, pre_get_info
, cmd_del_port
, NULL
, "--if-exists",
2284 /* Logical Switch commands. */
2285 {"add-ls", 1, 1, NULL
, pre_get_info
, cmd_add_ls
, NULL
, "--may-exist", RW
},
2286 {"del-ls", 1, 1, NULL
, pre_get_info
, cmd_del_ls
, NULL
, "--if-exists", RW
},
2287 {"list-ls", 0, 0, NULL
, pre_get_info
, cmd_list_ls
, NULL
, "", RO
},
2288 {"ls-exists", 1, 1, NULL
, pre_get_info
, cmd_ls_exists
, NULL
, "", RO
},
2289 {"list-bindings", 2, 2, NULL
, pre_get_info
, cmd_list_bindings
, NULL
, "", RO
},
2290 {"bind-ls", 4, 4, NULL
, pre_get_info
, cmd_bind_ls
, NULL
, "", RO
},
2291 {"unbind-ls", 3, 3, NULL
, pre_get_info
, cmd_unbind_ls
, NULL
, "", RO
},
2293 /* MAC binding commands. */
2294 {"add-ucast-local", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_local
, NULL
,
2296 {"del-ucast-local", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_local
, NULL
,
2298 {"add-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_local
, NULL
,
2300 {"del-mcast-local", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_local
, NULL
,
2302 {"clear-local-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_local_macs
, NULL
,
2304 {"list-local-macs", 1, 1, NULL
, pre_get_info
, cmd_list_local_macs
, NULL
,
2306 {"add-ucast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_ucast_remote
, NULL
,
2308 {"del-ucast-remote", 2, 2, NULL
, pre_get_info
, cmd_del_ucast_remote
, NULL
,
2310 {"add-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_add_mcast_remote
, NULL
,
2312 {"del-mcast-remote", 3, 4, NULL
, pre_get_info
, cmd_del_mcast_remote
, NULL
,
2314 {"clear-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_clear_remote_macs
, NULL
,
2316 {"list-remote-macs", 1, 1, NULL
, pre_get_info
, cmd_list_remote_macs
, NULL
,
2319 /* Manager commands. */
2320 {"get-manager", 0, 0, NULL
, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
2321 {"del-manager", 0, 0, NULL
, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
2322 {"set-manager", 1, INT_MAX
, NULL
, pre_manager
, cmd_set_manager
, NULL
, "",
2325 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2328 /* Registers vsctl and common db commands. */
2330 vtep_ctl_cmd_init(void)
2332 ctl_init(tables
, vtep_ctl_exit
);
2333 ctl_register_commands(vtep_commands
);