2 * Copyright (c) 2009, 2010, 2011, 2012 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 "command-line.h"
33 #include "dynamic-string.h"
34 #include "fatal-signal.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-idl.h"
39 #include "poll-loop.h"
42 #include "stream-ssl.h"
46 #include "lib/vtep-idl.h"
53 VLOG_DEFINE_THIS_MODULE(vtep_ctl);
55 /* vtep_ctl_fatal() also logs the error, so it is preferred in this file. */
56 #define ovs_fatal please_use_vtep_ctl_fatal_instead_of_ovs_fatal
58 struct vtep_ctl_context;
60 /* A command supported by vtep-ctl. */
61 struct vtep_ctl_command_syntax {
62 const char *name; /* e.g. "add-ps" */
63 int min_args; /* Min number of arguments following name. */
64 int max_args; /* Max number of arguments following name. */
66 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
67 * each column or table in ctx->idl that it uses. */
68 void (*prerequisites)(struct vtep_ctl_context *ctx);
70 /* Does the actual work of the command and puts the command's output, if
71 * any, in ctx->output or ctx->table.
73 * Alternatively, if some prerequisite of the command is not met and the
74 * caller should wait for something to change and then retry, it may set
75 * ctx->try_again to true. (Only the "wait-until" command currently does
77 void (*run)(struct vtep_ctl_context *ctx);
79 /* If nonnull, called after the transaction has been successfully
80 * committed. ctx->output is the output from the "run" function, which
81 * this function may modify and otherwise postprocess as needed. (Only the
82 * "create" command currently does any postprocessing.) */
83 void (*postprocess)(struct vtep_ctl_context *ctx);
85 /* A comma-separated list of supported options, e.g. "--a,--b", or the
86 * empty string if the command does not support any options. */
88 enum { RO, RW } mode; /* Does this command modify the database? */
91 struct vtep_ctl_command {
92 /* Data that remains constant after initialization. */
93 const struct vtep_ctl_command_syntax *syntax;
98 /* Data modified by commands. */
103 /* --db: The database server to contact. */
104 static const char *db;
106 /* --oneline: Write each command's output as a single line? */
109 /* --dry-run: Do not commit any changes. */
112 /* --timeout: Time to wait for a connection to 'db'. */
115 /* Format for table output. */
116 static struct table_style table_style = TABLE_STYLE_DEFAULT;
118 /* All supported commands. */
119 static const struct vtep_ctl_command_syntax all_commands[];
121 /* The IDL we're using and the current transaction, if any.
122 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
123 * Other code should use its context arguments. */
124 static struct ovsdb_idl *the_idl;
125 static struct ovsdb_idl_txn *the_idl_txn;
127 static void vtep_ctl_exit(int status) NO_RETURN;
128 static void vtep_ctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
129 static char *default_db(void);
130 static void usage(void) NO_RETURN;
131 static void parse_options(int argc, char *argv[], struct shash *local_options);
132 static bool might_write_to_db(char **argv);
134 static struct vtep_ctl_command *parse_commands(int argc, char *argv[],
135 struct shash *local_options,
136 size_t *n_commandsp);
137 static void parse_command(int argc, char *argv[], struct shash *local_options,
138 struct vtep_ctl_command *);
139 static const struct vtep_ctl_command_syntax *find_command(const char *name);
140 static void run_prerequisites(struct vtep_ctl_command[], size_t n_commands,
142 static void do_vtep_ctl(const char *args, struct vtep_ctl_command *, size_t n,
145 static const struct vtep_ctl_table_class *get_table(const char *table_name);
146 static void set_column(const struct vtep_ctl_table_class *,
147 const struct ovsdb_idl_row *, const char *arg,
148 struct ovsdb_symbol_table *);
150 static bool is_condition_satisfied(const struct vtep_ctl_table_class *,
151 const struct ovsdb_idl_row *,
153 struct ovsdb_symbol_table *);
155 static struct vtep_ctl_lswitch *find_lswitch(struct vtep_ctl_context *,
160 main(int argc, char *argv[])
162 extern struct vlog_module VLM_reconnect;
163 struct ovsdb_idl *idl;
164 struct vtep_ctl_command *commands;
165 struct shash local_options;
170 set_program_name(argv[0]);
171 fatal_ignore_sigpipe();
172 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
173 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
176 /* Log our arguments. This is often valuable for debugging systems. */
177 args = process_escape_args(argv);
178 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
180 /* Parse command line. */
181 shash_init(&local_options);
182 parse_options(argc, argv, &local_options);
183 commands = parse_commands(argc - optind, argv + optind, &local_options,
190 /* Initialize IDL. */
191 idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
192 run_prerequisites(commands, n_commands, idl);
194 /* Execute the commands.
196 * 'seqno' is the database sequence number for which we last tried to
197 * execute our transaction. There's no point in trying to commit more than
198 * once for any given sequence number, because if the transaction fails
199 * it's because the database changed and we need to obtain an up-to-date
200 * view of the database before we try the transaction again. */
201 seqno = ovsdb_idl_get_seqno(idl);
205 if (seqno != ovsdb_idl_get_seqno(idl)) {
206 seqno = ovsdb_idl_get_seqno(idl);
207 do_vtep_ctl(args, commands, n_commands, idl);
210 if (seqno == ovsdb_idl_get_seqno(idl)) {
217 static struct option *
218 find_option(const char *name, struct option *options, size_t n_options)
222 for (i = 0; i < n_options; i++) {
223 if (!strcmp(options[i].name, name)) {
230 static struct option *
231 add_option(struct option **optionsp, size_t *n_optionsp,
232 size_t *allocated_optionsp)
234 if (*n_optionsp >= *allocated_optionsp) {
235 *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
238 return &(*optionsp)[(*n_optionsp)++];
242 parse_options(int argc, char *argv[], struct shash *local_options)
245 OPT_DB = UCHAR_MAX + 1,
254 static const struct option global_long_options[] = {
255 {"db", required_argument, NULL, OPT_DB},
256 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
257 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
258 {"oneline", no_argument, NULL, OPT_ONELINE},
259 {"timeout", required_argument, NULL, 't'},
260 {"help", no_argument, NULL, 'h'},
261 {"version", no_argument, NULL, 'V'},
264 STREAM_SSL_LONG_OPTIONS,
265 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
268 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
269 char *tmp, *short_options;
271 const struct vtep_ctl_command_syntax *p;
272 struct option *options, *o;
273 size_t allocated_options;
277 tmp = long_options_to_short_options(global_long_options);
278 short_options = xasprintf("+%s", tmp);
281 /* We want to parse both global and command-specific options here, but
282 * getopt_long() isn't too convenient for the job. We copy our global
283 * options into a dynamic array, then append all of the command-specific
285 options = xmemdup(global_long_options, sizeof global_long_options);
286 allocated_options = ARRAY_SIZE(global_long_options);
287 n_options = n_global_long_options;
288 for (p = all_commands; p->name; p++) {
290 char *save_ptr = NULL;
294 s = xstrdup(p->options);
295 for (name = strtok_r(s, ",", &save_ptr); name != NULL;
296 name = strtok_r(NULL, ",", &save_ptr)) {
300 ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
303 equals = strchr(name, '=');
305 has_arg = required_argument;
308 has_arg = no_argument;
311 o = find_option(name, options, n_options);
313 ovs_assert(o - options >= n_global_long_options);
314 ovs_assert(o->has_arg == has_arg);
316 o = add_option(&options, &n_options, &allocated_options);
317 o->name = xstrdup(name);
318 o->has_arg = has_arg;
327 o = add_option(&options, &n_options, &allocated_options);
328 memset(o, 0, sizeof *o);
330 table_style.format = TF_LIST;
336 c = getopt_long(argc, argv, short_options, options, &idx);
351 vlog_set_levels(&VLM_vtep_ctl, VLF_SYSLOG, VLL_WARN);
359 if (shash_find(local_options, options[idx].name)) {
360 vtep_ctl_fatal("'%s' option specified multiple times",
363 shash_add_nocopy(local_options,
364 xasprintf("--%s", options[idx].name),
365 optarg ? xstrdup(optarg) : NULL);
372 ovs_print_version(0, 0);
376 timeout = strtoul(optarg, NULL, 10);
378 vtep_ctl_fatal("value %s on -t or --timeout is invalid",
384 TABLE_OPTION_HANDLERS(&table_style)
386 STREAM_SSL_OPTION_HANDLERS
388 case OPT_PEER_CA_CERT:
389 stream_ssl_set_peer_ca_cert_file(optarg);
405 for (i = n_global_long_options; options[i].name; i++) {
406 free(CONST_CAST(char *, options[i].name));
411 static struct vtep_ctl_command *
412 parse_commands(int argc, char *argv[], struct shash *local_options,
415 struct vtep_ctl_command *commands;
416 size_t n_commands, allocated_commands;
420 n_commands = allocated_commands = 0;
422 for (start = i = 0; i <= argc; i++) {
423 if (i == argc || !strcmp(argv[i], "--")) {
425 if (n_commands >= allocated_commands) {
426 struct vtep_ctl_command *c;
428 commands = x2nrealloc(commands, &allocated_commands,
430 for (c = commands; c < &commands[n_commands]; c++) {
431 shash_moved(&c->options);
434 parse_command(i - start, &argv[start], local_options,
435 &commands[n_commands++]);
436 } else if (!shash_is_empty(local_options)) {
437 vtep_ctl_fatal("missing command name (use --help for help)");
443 vtep_ctl_fatal("missing command name (use --help for help)");
445 *n_commandsp = n_commands;
450 parse_command(int argc, char *argv[], struct shash *local_options,
451 struct vtep_ctl_command *command)
453 const struct vtep_ctl_command_syntax *p;
454 struct shash_node *node;
458 shash_init(&command->options);
459 shash_swap(local_options, &command->options);
460 for (i = 0; i < argc; i++) {
461 const char *option = argv[i];
465 if (option[0] != '-') {
469 equals = strchr(option, '=');
471 key = xmemdup0(option, equals - option);
472 value = xstrdup(equals + 1);
474 key = xstrdup(option);
478 if (shash_find(&command->options, key)) {
479 vtep_ctl_fatal("'%s' option specified multiple times", argv[i]);
481 shash_add_nocopy(&command->options, key, value);
484 vtep_ctl_fatal("missing command name (use --help for help)");
487 p = find_command(argv[i]);
489 vtep_ctl_fatal("unknown command '%s'; use --help for help", argv[i]);
492 SHASH_FOR_EACH (node, &command->options) {
493 const char *s = strstr(p->options, node->name);
494 int end = s ? s[strlen(node->name)] : EOF;
496 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
497 vtep_ctl_fatal("'%s' command has no '%s' option",
498 argv[i], node->name);
500 if ((end == '=') != (node->data != NULL)) {
502 vtep_ctl_fatal("missing argument to '%s' option on '%s' "
503 "command", node->name, argv[i]);
505 vtep_ctl_fatal("'%s' option on '%s' does not accept an "
506 "argument", node->name, argv[i]);
511 n_arg = argc - i - 1;
512 if (n_arg < p->min_args) {
513 vtep_ctl_fatal("'%s' command requires at least %d arguments",
514 p->name, p->min_args);
515 } else if (n_arg > p->max_args) {
518 for (j = i + 1; j < argc; j++) {
519 if (argv[j][0] == '-') {
520 vtep_ctl_fatal("'%s' command takes at most %d arguments "
521 "(note that options must precede command "
522 "names and follow a \"--\" argument)",
523 p->name, p->max_args);
527 vtep_ctl_fatal("'%s' command takes at most %d arguments",
528 p->name, p->max_args);
532 command->argc = n_arg + 1;
533 command->argv = &argv[i];
536 /* Returns the "struct vtep_ctl_command_syntax" for a given command 'name', or a
537 * null pointer if there is none. */
538 static const struct vtep_ctl_command_syntax *
539 find_command(const char *name)
541 static struct shash commands = SHASH_INITIALIZER(&commands);
543 if (shash_is_empty(&commands)) {
544 const struct vtep_ctl_command_syntax *p;
546 for (p = all_commands; p->name; p++) {
547 shash_add_assert(&commands, p->name, p);
551 return shash_find_data(&commands, name);
555 vtep_ctl_fatal(const char *format, ...)
560 va_start(args, format);
561 message = xvasprintf(format, args);
564 vlog_set_levels(&VLM_vtep_ctl, VLF_CONSOLE, VLL_OFF);
565 VLOG_ERR("%s", message);
566 ovs_error(0, "%s", message);
567 vtep_ctl_exit(EXIT_FAILURE);
570 /* Frees the current transaction and the underlying IDL and then calls
573 * Freeing the transaction and the IDL is not strictly necessary, but it makes
574 * for a clean memory leak report from valgrind in the normal case. That makes
575 * it easier to notice real memory leaks. */
577 vtep_ctl_exit(int status)
580 ovsdb_idl_txn_abort(the_idl_txn);
581 ovsdb_idl_txn_destroy(the_idl_txn);
583 ovsdb_idl_destroy(the_idl);
591 %s: VTEP configuration utility\n\
592 usage: %s [OPTIONS] COMMAND [ARG...]\n\
595 get-manager print the managers\n\
596 del-manager delete the managers\n\
597 set-manager TARGET... set the list of managers to TARGET...\n\
599 Physical Switch commands:\n\
600 add-ps PS create a new physical switch named PS\n\
601 del-ps PS delete PS and all of its ports\n\
602 list-ps print the names of all the physical switches\n\
603 ps-exists PS exit 2 if PS does not exist\n\
606 list-ports PS print the names of all the ports on PS\n\
607 add-port PS PORT add network device PORT to PS\n\
608 del-port PS PORT delete PORT from PS\n\
610 Logical Switch commands:\n\
611 add-ls LS create a new logical switch named LS\n\
612 del-ls LS delete LS and all of its ports\n\
613 list-ls print the names of all the logical switches\n\
614 ls-exists LS exit 2 if LS does not exist\n\
615 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
616 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
617 list-bindings PS PORT list bindings for PORT on PS\n\
619 MAC binding commands:\n\
620 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
621 del-ucast-local LS MAC del ucast local entry from LS\n\
622 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
623 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
624 clear-local-macs LS clear local mac entries\n\
625 list-local-macs LS list local mac entries\n\
626 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
627 del-ucast-remote LS MAC del ucast remote entry from LS\n\
628 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
629 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
630 clear-remote-macs LS clear remote mac entries\n\
631 list-remote-macs LS list remote mac entries\n\
633 Database commands:\n\
634 list TBL [REC] list RECord (or all records) in TBL\n\
635 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
636 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
637 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
638 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
639 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
640 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
641 create TBL COL[:KEY]=VALUE create and initialize new record\n\
642 destroy TBL REC delete RECord from TBL\n\
643 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
644 Potentially unsafe database commands require --force option.\n\
647 --db=DATABASE connect to DATABASE\n\
649 -t, --timeout=SECS wait at most SECS seconds\n\
650 --dry-run do not commit changes to database\n\
651 --oneline print exactly one line of output per command\n",
652 program_name, program_name, default_db());
655 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
656 stream_usage("database", true, true, false);
659 -h, --help display this help message\n\
660 -V, --version display version information\n");
669 def = xasprintf("unix:%s/db.sock", ovs_rundir());
674 /* Returns true if it looks like this set of arguments might modify the
675 * database, otherwise false. (Not very smart, so it's prone to false
678 might_write_to_db(char **argv)
680 for (; *argv; argv++) {
681 const struct vtep_ctl_command_syntax *p = find_command(*argv);
682 if (p && p->mode == RW) {
689 struct vtep_ctl_context {
693 struct shash options;
695 /* Modifiable state. */
698 struct ovsdb_idl *idl;
699 struct ovsdb_idl_txn *txn;
700 struct ovsdb_symbol_table *symtab;
701 const struct vteprec_global *vtep_global;
704 /* A cache of the contents of the database.
706 * A command that needs to use any of this information must first
707 * call vtep_ctl_context_populate_cache(). A command that changes
708 * anything that could invalidate the cache must either call
709 * vtep_ctl_context_invalidate_cache() or manually update the cache
710 * to maintain its correctness. */
712 struct shash pswitches; /* Maps from physical switch name to
713 * struct vtep_ctl_pswitch. */
714 struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
716 struct shash lswitches; /* Maps from logical switch name to
717 * struct vtep_ctl_lswitch. */
718 struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
719 * struct vteprec_physical_locator. */
721 /* A command may set this member to true if some prerequisite is not met
722 * and the caller should wait for something to change and then retry. */
726 struct vtep_ctl_pswitch {
727 const struct vteprec_physical_switch *ps_cfg;
729 struct list ports; /* Contains "struct vteprec_physical_port"s. */
732 struct vtep_ctl_port {
733 struct list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
734 const struct vteprec_physical_port *port_cfg;
735 struct vtep_ctl_pswitch *ps;
736 struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
739 struct vtep_ctl_lswitch {
740 const struct vteprec_logical_switch *ls_cfg;
742 struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
743 struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
744 struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
745 struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
748 struct vtep_ctl_mcast_mac {
749 const struct vteprec_mcast_macs_local *local_cfg;
750 const struct vteprec_mcast_macs_remote *remote_cfg;
752 const struct vteprec_physical_locator_set *ploc_set_cfg;
753 struct list locators; /* Contains 'vtep_ctl_ploc's. */
756 struct vtep_ctl_ploc {
757 struct list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
759 const struct vteprec_physical_locator *ploc_cfg;
763 verify_ports(struct vtep_ctl_context *ctx)
765 if (!ctx->verified_ports) {
766 const struct vteprec_physical_switch *ps;
768 vteprec_global_verify_switches(ctx->vtep_global);
769 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, ctx->idl) {
770 vteprec_physical_switch_verify_ports(ps);
773 ctx->verified_ports = true;
777 static struct vtep_ctl_port *
778 add_port_to_cache(struct vtep_ctl_context *ctx,
779 struct vtep_ctl_pswitch *ps,
780 struct vteprec_physical_port *port_cfg)
782 char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
783 struct vtep_ctl_port *port;
785 port = xmalloc(sizeof *port);
786 list_push_back(&ps->ports, &port->ports_node);
787 port->port_cfg = port_cfg;
789 shash_add(&ctx->ports, cache_name, port);
791 shash_init(&port->bindings);
797 del_cached_port(struct vtep_ctl_context *ctx, struct vtep_ctl_port *port)
799 char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
801 list_remove(&port->ports_node);
802 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
803 vteprec_physical_port_delete(port->port_cfg);
808 static struct vtep_ctl_pswitch *
809 add_pswitch_to_cache(struct vtep_ctl_context *ctx,
810 struct vteprec_physical_switch *ps_cfg)
812 struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
814 ps->name = xstrdup(ps_cfg->name);
815 list_init(&ps->ports);
816 shash_add(&ctx->pswitches, ps->name, ps);
821 vtep_delete_pswitch(const struct vteprec_global *vtep_global,
822 const struct vteprec_physical_switch *ps)
824 struct vteprec_physical_switch **pswitches;
827 pswitches = xmalloc(sizeof *vtep_global->switches
828 * vtep_global->n_switches);
829 for (i = n = 0; i < vtep_global->n_switches; i++) {
830 if (vtep_global->switches[i] != ps) {
831 pswitches[n++] = vtep_global->switches[i];
834 vteprec_global_set_switches(vtep_global, pswitches, n);
839 del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
841 ovs_assert(list_is_empty(&ps->ports));
843 vteprec_physical_switch_delete(ps->ps_cfg);
844 vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
846 shash_find_and_delete(&ctx->pswitches, ps->name);
851 static struct vtep_ctl_lswitch *
852 add_lswitch_to_cache(struct vtep_ctl_context *ctx,
853 const struct vteprec_logical_switch *ls_cfg)
855 struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
857 ls->name = xstrdup(ls_cfg->name);
858 shash_add(&ctx->lswitches, ls->name, ls);
859 shash_init(&ls->ucast_local);
860 shash_init(&ls->ucast_remote);
861 shash_init(&ls->mcast_local);
862 shash_init(&ls->mcast_remote);
867 del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
870 vteprec_logical_switch_delete(ls->ls_cfg);
872 shash_find_and_delete(&ctx->lswitches, ls->name);
878 commit_ls_bindings(struct vtep_ctl_port *port)
880 struct vteprec_logical_switch **binding_values;
881 int64_t *binding_keys;
883 struct shash_node *node;
886 n_bindings = shash_count(&port->bindings);
887 binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
888 binding_values = xmalloc(n_bindings * sizeof *binding_values);
891 SHASH_FOR_EACH(node, &port->bindings) {
892 struct vtep_ctl_lswitch *ls_entry = node->data;
894 binding_keys[i] = strtoll(node->name, NULL, 0);
895 binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
899 vteprec_physical_port_set_vlan_bindings(port->port_cfg,
900 binding_keys, binding_values,
902 free(binding_values);
907 add_ls_binding_to_cache(struct vtep_ctl_port *port,
909 struct vtep_ctl_lswitch *ls)
911 if (shash_find(&port->bindings, vlan)) {
912 vtep_ctl_fatal("multiple bindings for vlan %s", vlan);
915 shash_add(&port->bindings, vlan, ls);
919 del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
921 if (!shash_find(&port->bindings, vlan)) {
922 vtep_ctl_fatal("no binding for vlan %s", vlan);
925 shash_find_and_delete(&port->bindings, vlan);
928 static struct vteprec_physical_locator *
929 find_ploc(struct vtep_ctl_context *ctx, const char *encap,
932 struct vteprec_physical_locator *ploc;
933 char *name = xasprintf("%s+%s", encap, dst_ip);
935 ovs_assert(ctx->cache_valid);
937 ploc = shash_find_data(&ctx->plocs, name);
944 add_ploc_to_cache(struct vtep_ctl_context *ctx,
945 struct vteprec_physical_locator *ploc)
947 char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
948 struct vteprec_physical_locator *orig_ploc;
950 orig_ploc = find_ploc(ctx, ploc->encapsulation_type, ploc->dst_ip);
952 shash_add(&ctx->plocs, name, ploc);
959 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
960 struct vteprec_physical_locator *ploc_cfg)
962 struct vtep_ctl_ploc *ploc;
964 ploc = xmalloc(sizeof *ploc);
965 ploc->ploc_cfg = ploc_cfg;
966 list_push_back(&mcast_mac->locators, &ploc->locators_node);
970 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
971 struct vteprec_physical_locator *ploc_cfg)
973 struct vtep_ctl_ploc *ploc;
975 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
976 if (ploc->ploc_cfg == ploc_cfg) {
977 list_remove(&ploc->locators_node);
984 static struct vtep_ctl_mcast_mac *
985 add_mcast_mac_to_cache(struct vtep_ctl_context *ctx,
986 struct vtep_ctl_lswitch *ls, const char *mac,
987 struct vteprec_physical_locator_set *ploc_set_cfg,
990 struct vtep_ctl_mcast_mac *mcast_mac;
991 struct shash *mcast_shash;
994 mcast_mac = xmalloc(sizeof *mcast_mac);
995 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
997 mcast_mac->ploc_set_cfg = ploc_set_cfg;
998 list_init(&mcast_mac->locators);
999 shash_add(mcast_shash, mac, mcast_mac);
1001 for (i = 0; i < ploc_set_cfg->n_locators; i++) {
1002 struct vteprec_physical_locator *ploc_cfg;
1004 ploc_cfg = ploc_set_cfg->locators[i];
1005 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1006 add_ploc_to_cache(ctx, ploc_cfg);
1013 vtep_ctl_context_invalidate_cache(struct vtep_ctl_context *ctx)
1015 struct shash_node *node;
1017 if (!ctx->cache_valid) {
1020 ctx->cache_valid = false;
1022 SHASH_FOR_EACH (node, &ctx->pswitches) {
1023 struct vtep_ctl_pswitch *ps = node->data;
1027 shash_destroy(&ctx->pswitches);
1029 SHASH_FOR_EACH (node, &ctx->ports) {
1030 struct vtep_ctl_port *port = node->data;
1031 shash_destroy(&port->bindings);
1033 shash_destroy_free_data(&ctx->ports);
1035 SHASH_FOR_EACH (node, &ctx->lswitches) {
1036 struct vtep_ctl_lswitch *ls = node->data;
1037 struct shash_node *node2, *next_node2;
1039 shash_destroy(&ls->ucast_local);
1040 shash_destroy(&ls->ucast_remote);
1042 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
1043 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
1044 struct vtep_ctl_ploc *ploc, *next_ploc;
1046 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
1047 &mcast_mac->locators) {
1052 shash_destroy(&ls->mcast_local);
1054 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
1055 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
1056 struct vtep_ctl_ploc *ploc, *next_ploc;
1058 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
1059 &mcast_mac->locators) {
1064 shash_destroy(&ls->mcast_remote);
1069 shash_destroy(&ctx->lswitches);
1070 shash_destroy(&ctx->plocs);
1074 pre_get_info(struct vtep_ctl_context *ctx)
1076 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
1078 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
1079 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
1081 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
1082 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
1084 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
1086 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
1087 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
1088 ovsdb_idl_add_column(ctx->idl,
1089 &vteprec_ucast_macs_local_col_logical_switch);
1091 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
1092 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
1093 ovsdb_idl_add_column(ctx->idl,
1094 &vteprec_ucast_macs_remote_col_logical_switch);
1096 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
1097 ovsdb_idl_add_column(ctx->idl,
1098 &vteprec_mcast_macs_local_col_locator_set);
1099 ovsdb_idl_add_column(ctx->idl,
1100 &vteprec_mcast_macs_local_col_logical_switch);
1102 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
1103 ovsdb_idl_add_column(ctx->idl,
1104 &vteprec_mcast_macs_remote_col_locator_set);
1105 ovsdb_idl_add_column(ctx->idl,
1106 &vteprec_mcast_macs_remote_col_logical_switch);
1108 ovsdb_idl_add_column(ctx->idl,
1109 &vteprec_physical_locator_set_col_locators);
1111 ovsdb_idl_add_column(ctx->idl,
1112 &vteprec_physical_locator_col_dst_ip);
1113 ovsdb_idl_add_column(ctx->idl,
1114 &vteprec_physical_locator_col_encapsulation_type);
1118 vtep_ctl_context_populate_cache(struct vtep_ctl_context *ctx)
1120 const struct vteprec_global *vtep_global = ctx->vtep_global;
1121 const struct vteprec_logical_switch *ls_cfg;
1122 const struct vteprec_ucast_macs_local *ucast_local_cfg;
1123 const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
1124 const struct vteprec_mcast_macs_local *mcast_local_cfg;
1125 const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
1126 struct sset pswitches, ports, lswitches;
1129 if (ctx->cache_valid) {
1130 /* Cache is already populated. */
1133 ctx->cache_valid = true;
1134 shash_init(&ctx->pswitches);
1135 shash_init(&ctx->ports);
1136 shash_init(&ctx->lswitches);
1137 shash_init(&ctx->plocs);
1139 sset_init(&pswitches);
1141 for (i = 0; i < vtep_global->n_switches; i++) {
1142 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1143 struct vtep_ctl_pswitch *ps;
1146 if (!sset_add(&pswitches, ps_cfg->name)) {
1147 VLOG_WARN("%s: database contains duplicate physical switch name",
1151 ps = add_pswitch_to_cache(ctx, ps_cfg);
1156 for (j = 0; j < ps_cfg->n_ports; j++) {
1157 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1159 if (!sset_add(&ports, port_cfg->name)) {
1160 /* Duplicate port name. (We will warn about that later.) */
1165 sset_destroy(&pswitches);
1166 sset_destroy(&ports);
1168 sset_init(&lswitches);
1169 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
1170 if (!sset_add(&lswitches, ls_cfg->name)) {
1171 VLOG_WARN("%s: database contains duplicate logical switch name",
1175 add_lswitch_to_cache(ctx, ls_cfg);
1177 sset_destroy(&lswitches);
1179 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
1180 struct vtep_ctl_lswitch *ls;
1182 if (!ucast_local_cfg->logical_switch) {
1185 ls = find_lswitch(ctx, ucast_local_cfg->logical_switch->name, false);
1190 if (ucast_local_cfg->locator) {
1191 add_ploc_to_cache(ctx, ucast_local_cfg->locator);
1194 shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
1197 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
1198 struct vtep_ctl_lswitch *ls;
1200 if (!ucast_remote_cfg->logical_switch) {
1203 ls = find_lswitch(ctx, ucast_remote_cfg->logical_switch->name, false);
1208 if (ucast_remote_cfg->locator) {
1209 add_ploc_to_cache(ctx, ucast_remote_cfg->locator);
1212 shash_add(&ls->ucast_remote, ucast_remote_cfg->MAC, ucast_remote_cfg);
1215 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg, ctx->idl) {
1216 struct vtep_ctl_mcast_mac *mcast_mac;
1217 struct vtep_ctl_lswitch *ls;
1219 if (!mcast_local_cfg->logical_switch) {
1222 ls = find_lswitch(ctx, mcast_local_cfg->logical_switch->name, false);
1227 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mcast_local_cfg->MAC,
1228 mcast_local_cfg->locator_set,
1230 mcast_mac->local_cfg = mcast_local_cfg;
1233 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg, ctx->idl) {
1234 struct vtep_ctl_mcast_mac *mcast_mac;
1235 struct vtep_ctl_lswitch *ls;
1237 if (!mcast_remote_cfg->logical_switch) {
1240 ls = find_lswitch(ctx, mcast_remote_cfg->logical_switch->name, false);
1245 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mcast_remote_cfg->MAC,
1246 mcast_remote_cfg->locator_set,
1248 mcast_mac->remote_cfg = mcast_remote_cfg;
1251 sset_init(&pswitches);
1252 for (i = 0; i < vtep_global->n_switches; i++) {
1253 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1254 struct vtep_ctl_pswitch *ps;
1257 if (!sset_add(&pswitches, ps_cfg->name)) {
1260 ps = shash_find_data(&ctx->pswitches, ps_cfg->name);
1261 for (j = 0; j < ps_cfg->n_ports; j++) {
1262 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1263 struct vtep_ctl_port *port;
1266 port = shash_find_data(&ctx->ports, port_cfg->name);
1268 if (port_cfg == port->port_cfg) {
1269 VLOG_WARN("%s: port is in multiple physical switches "
1271 port_cfg->name, ps->name, port->ps->name);
1273 /* Log as an error because this violates the database's
1274 * uniqueness constraints, so the database server shouldn't
1275 * have allowed it. */
1276 VLOG_ERR("%s: database contains duplicate port name",
1282 port = add_port_to_cache(ctx, ps, port_cfg);
1284 for (k = 0; k < port_cfg->n_vlan_bindings; k++) {
1285 struct vteprec_logical_switch *ls_cfg;
1286 struct vtep_ctl_lswitch *ls;
1289 vlan = xasprintf("%"PRId64, port_cfg->key_vlan_bindings[k]);
1290 if (shash_find(&port->bindings, vlan)) {
1291 vtep_ctl_fatal("multiple bindings for vlan %s", vlan);
1294 ls_cfg = port_cfg->value_vlan_bindings[k];
1295 ls = find_lswitch(ctx, ls_cfg->name, true);
1297 shash_add_nocopy(&port->bindings, vlan, ls);
1301 sset_destroy(&pswitches);
1304 static struct vtep_ctl_pswitch *
1305 find_pswitch(struct vtep_ctl_context *ctx, const char *name, bool must_exist)
1307 struct vtep_ctl_pswitch *ps;
1309 ovs_assert(ctx->cache_valid);
1311 ps = shash_find_data(&ctx->pswitches, name);
1312 if (must_exist && !ps) {
1313 vtep_ctl_fatal("no physical switch named %s", name);
1315 vteprec_global_verify_switches(ctx->vtep_global);
1319 static struct vtep_ctl_port *
1320 find_port(struct vtep_ctl_context *ctx, const char *ps_name,
1321 const char *port_name, bool must_exist)
1323 char *cache_name = xasprintf("%s+%s", ps_name, port_name);
1324 struct vtep_ctl_port *port;
1326 ovs_assert(ctx->cache_valid);
1328 port = shash_find_data(&ctx->ports, cache_name);
1329 if (port && !strcmp(port_name, port->ps->name)) {
1333 if (must_exist && !port) {
1334 vtep_ctl_fatal("no port named %s", port_name);
1341 pswitch_insert_port(const struct vteprec_physical_switch *ps,
1342 struct vteprec_physical_port *port)
1344 struct vteprec_physical_port **ports;
1347 ports = xmalloc(sizeof *ps->ports * (ps->n_ports + 1));
1348 for (i = 0; i < ps->n_ports; i++) {
1349 ports[i] = ps->ports[i];
1351 ports[ps->n_ports] = port;
1352 vteprec_physical_switch_set_ports(ps, ports, ps->n_ports + 1);
1357 pswitch_delete_port(const struct vteprec_physical_switch *ps,
1358 const struct vteprec_physical_port *port)
1360 struct vteprec_physical_port **ports;
1363 ports = xmalloc(sizeof *ps->ports * ps->n_ports);
1364 for (i = n = 0; i < ps->n_ports; i++) {
1365 if (ps->ports[i] != port) {
1366 ports[n++] = ps->ports[i];
1369 vteprec_physical_switch_set_ports(ps, ports, n);
1374 vtep_insert_pswitch(const struct vteprec_global *vtep_global,
1375 struct vteprec_physical_switch *ps)
1377 struct vteprec_physical_switch **pswitches;
1380 pswitches = xmalloc(sizeof *vtep_global->switches
1381 * (vtep_global->n_switches + 1));
1382 for (i = 0; i < vtep_global->n_switches; i++) {
1383 pswitches[i] = vtep_global->switches[i];
1385 pswitches[vtep_global->n_switches] = ps;
1386 vteprec_global_set_switches(vtep_global, pswitches,
1387 vtep_global->n_switches + 1);
1392 cmd_add_ps(struct vtep_ctl_context *ctx)
1394 const char *ps_name = ctx->argv[1];
1395 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1396 struct vteprec_physical_switch *ps;
1398 vtep_ctl_context_populate_cache(ctx);
1399 if (find_pswitch(ctx, ps_name, false)) {
1401 vtep_ctl_fatal("cannot create physical switch %s because it "
1402 "already exists", ps_name);
1407 ps = vteprec_physical_switch_insert(ctx->txn);
1408 vteprec_physical_switch_set_name(ps, ps_name);
1410 vtep_insert_pswitch(ctx->vtep_global, ps);
1412 vtep_ctl_context_invalidate_cache(ctx);
1416 del_port(struct vtep_ctl_context *ctx, struct vtep_ctl_port *port)
1418 pswitch_delete_port(port->ps->ps_cfg, port->port_cfg);
1419 del_cached_port(ctx, port);
1423 del_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
1425 struct vtep_ctl_port *port, *next_port;
1427 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &ps->ports) {
1428 del_port(ctx, port);
1431 del_cached_pswitch(ctx, ps);
1435 cmd_del_ps(struct vtep_ctl_context *ctx)
1437 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1438 struct vtep_ctl_pswitch *ps;
1440 vtep_ctl_context_populate_cache(ctx);
1441 ps = find_pswitch(ctx, ctx->argv[1], must_exist);
1443 del_pswitch(ctx, ps);
1448 output_sorted(struct svec *svec, struct ds *output)
1454 SVEC_FOR_EACH (i, name, svec) {
1455 ds_put_format(output, "%s\n", name);
1460 cmd_list_ps(struct vtep_ctl_context *ctx)
1462 struct shash_node *node;
1463 struct svec pswitches;
1465 vtep_ctl_context_populate_cache(ctx);
1467 svec_init(&pswitches);
1468 SHASH_FOR_EACH (node, &ctx->pswitches) {
1469 struct vtep_ctl_pswitch *ps = node->data;
1471 svec_add(&pswitches, ps->name);
1473 output_sorted(&pswitches, &ctx->output);
1474 svec_destroy(&pswitches);
1478 cmd_ps_exists(struct vtep_ctl_context *ctx)
1480 vtep_ctl_context_populate_cache(ctx);
1481 if (!find_pswitch(ctx, ctx->argv[1], false)) {
1487 cmd_list_ports(struct vtep_ctl_context *ctx)
1489 struct vtep_ctl_pswitch *ps;
1490 struct vtep_ctl_port *port;
1493 vtep_ctl_context_populate_cache(ctx);
1494 ps = find_pswitch(ctx, ctx->argv[1], true);
1495 vteprec_physical_switch_verify_ports(ps->ps_cfg);
1498 LIST_FOR_EACH (port, ports_node, &ps->ports) {
1499 if (strcmp(port->port_cfg->name, ps->name)) {
1500 svec_add(&ports, port->port_cfg->name);
1503 output_sorted(&ports, &ctx->output);
1504 svec_destroy(&ports);
1508 add_port(struct vtep_ctl_context *ctx, const char *ps_name,
1509 const char *port_name, bool may_exist)
1511 struct vtep_ctl_port *vtep_ctl_port;
1512 struct vtep_ctl_pswitch *ps;
1513 struct vteprec_physical_port *port;
1515 vtep_ctl_context_populate_cache(ctx);
1517 vtep_ctl_port = find_port(ctx, ps_name, port_name, false);
1518 if (vtep_ctl_port) {
1520 vtep_ctl_fatal("cannot create a port named %s on %s because a "
1521 "port with that name already exists",
1522 port_name, ps_name);
1527 ps = find_pswitch(ctx, ps_name, true);
1529 port = vteprec_physical_port_insert(ctx->txn);
1530 vteprec_physical_port_set_name(port, port_name);
1532 pswitch_insert_port(ps->ps_cfg, port);
1534 add_port_to_cache(ctx, ps, port);
1538 cmd_add_port(struct vtep_ctl_context *ctx)
1540 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1541 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist);
1545 cmd_del_port(struct vtep_ctl_context *ctx)
1547 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1548 struct vtep_ctl_port *port;
1550 vtep_ctl_context_populate_cache(ctx);
1552 port = find_port(ctx, ctx->argv[1], ctx->argv[2], must_exist);
1554 if (ctx->argc == 3) {
1555 struct vtep_ctl_pswitch *ps;
1557 ps = find_pswitch(ctx, ctx->argv[1], true);
1558 if (port->ps != ps) {
1559 vtep_ctl_fatal("physical switch %s does not have a port %s",
1560 ctx->argv[1], ctx->argv[2]);
1564 del_port(ctx, port);
1568 static struct vtep_ctl_lswitch *
1569 find_lswitch(struct vtep_ctl_context *ctx, const char *name, bool must_exist)
1571 struct vtep_ctl_lswitch *ls;
1573 ovs_assert(ctx->cache_valid);
1575 ls = shash_find_data(&ctx->lswitches, name);
1576 if (must_exist && !ls) {
1577 vtep_ctl_fatal("no logical switch named %s", name);
1583 cmd_add_ls(struct vtep_ctl_context *ctx)
1585 const char *ls_name = ctx->argv[1];
1586 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1587 struct vteprec_logical_switch *ls;
1589 vtep_ctl_context_populate_cache(ctx);
1590 if (find_lswitch(ctx, ls_name, false)) {
1592 vtep_ctl_fatal("cannot create logical switch %s because it "
1593 "already exists", ls_name);
1598 ls = vteprec_logical_switch_insert(ctx->txn);
1599 vteprec_logical_switch_set_name(ls, ls_name);
1601 vtep_ctl_context_invalidate_cache(ctx);
1605 del_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
1607 del_cached_lswitch(ctx, ls);
1611 cmd_del_ls(struct vtep_ctl_context *ctx)
1613 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1614 struct vtep_ctl_lswitch *ls;
1616 vtep_ctl_context_populate_cache(ctx);
1617 ls = find_lswitch(ctx, ctx->argv[1], must_exist);
1619 del_lswitch(ctx, ls);
1624 cmd_list_ls(struct vtep_ctl_context *ctx)
1626 struct shash_node *node;
1627 struct svec lswitches;
1629 vtep_ctl_context_populate_cache(ctx);
1631 svec_init(&lswitches);
1632 SHASH_FOR_EACH (node, &ctx->lswitches) {
1633 struct vtep_ctl_lswitch *ls = node->data;
1635 svec_add(&lswitches, ls->name);
1637 output_sorted(&lswitches, &ctx->output);
1638 svec_destroy(&lswitches);
1642 cmd_ls_exists(struct vtep_ctl_context *ctx)
1644 vtep_ctl_context_populate_cache(ctx);
1645 if (!find_lswitch(ctx, ctx->argv[1], false)) {
1651 cmd_list_bindings(struct vtep_ctl_context *ctx)
1653 const struct shash_node *node;
1654 struct vtep_ctl_port *port;
1655 struct svec bindings;
1657 vtep_ctl_context_populate_cache(ctx);
1658 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1660 svec_init(&bindings);
1661 SHASH_FOR_EACH (node, &port->bindings) {
1662 struct vtep_ctl_lswitch *lswitch = node->data;
1665 binding = xasprintf("%04lld %s", strtoll(node->name, NULL, 0),
1667 svec_add_nocopy(&bindings, binding);
1669 output_sorted(&bindings, &ctx->output);
1670 svec_destroy(&bindings);
1674 cmd_bind_ls(struct vtep_ctl_context *ctx)
1676 struct vtep_ctl_lswitch *ls;
1677 struct vtep_ctl_port *port;
1680 vtep_ctl_context_populate_cache(ctx);
1682 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1683 vlan = ctx->argv[3];
1684 ls = find_lswitch(ctx, ctx->argv[4], true);
1686 add_ls_binding_to_cache(port, vlan, ls);
1687 commit_ls_bindings(port);
1689 vtep_ctl_context_invalidate_cache(ctx);
1693 cmd_unbind_ls(struct vtep_ctl_context *ctx)
1695 struct vtep_ctl_port *port;
1698 vtep_ctl_context_populate_cache(ctx);
1700 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1701 vlan = ctx->argv[3];
1703 del_cached_ls_binding(port, vlan);
1704 commit_ls_bindings(port);
1706 vtep_ctl_context_invalidate_cache(ctx);
1710 add_ucast_entry(struct vtep_ctl_context *ctx, bool local)
1712 struct vtep_ctl_lswitch *ls;
1716 struct vteprec_physical_locator *ploc_cfg;
1718 vtep_ctl_context_populate_cache(ctx);
1720 ls = find_lswitch(ctx, ctx->argv[1], true);
1723 if (ctx->argc == 4) {
1724 encap = "vxlan_over_ipv4";
1725 dst_ip = ctx->argv[3];
1727 encap = ctx->argv[3];
1728 dst_ip = ctx->argv[4];
1731 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1733 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1734 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1735 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1737 add_ploc_to_cache(ctx, ploc_cfg);
1741 struct vteprec_ucast_macs_local *ucast_cfg;
1743 ucast_cfg = shash_find_data(&ls->ucast_local, mac);
1745 ucast_cfg = vteprec_ucast_macs_local_insert(ctx->txn);
1746 vteprec_ucast_macs_local_set_MAC(ucast_cfg, mac);
1747 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg, ls->ls_cfg);
1748 shash_add(&ls->ucast_local, mac, ucast_cfg);
1750 vteprec_ucast_macs_local_set_locator(ucast_cfg, ploc_cfg);
1752 struct vteprec_ucast_macs_remote *ucast_cfg;
1754 ucast_cfg = shash_find_data(&ls->ucast_remote, mac);
1756 ucast_cfg = vteprec_ucast_macs_remote_insert(ctx->txn);
1757 vteprec_ucast_macs_remote_set_MAC(ucast_cfg, mac);
1758 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg, ls->ls_cfg);
1759 shash_add(&ls->ucast_remote, mac, ucast_cfg);
1761 vteprec_ucast_macs_remote_set_locator(ucast_cfg, ploc_cfg);
1764 vtep_ctl_context_invalidate_cache(ctx);
1768 cmd_add_ucast_local(struct vtep_ctl_context *ctx)
1770 add_ucast_entry(ctx, true);
1774 cmd_add_ucast_remote(struct vtep_ctl_context *ctx)
1776 add_ucast_entry(ctx, false);
1780 del_ucast_entry(struct vtep_ctl_context *ctx, bool local)
1782 struct vtep_ctl_lswitch *ls;
1783 struct shash *ucast_shash;
1784 struct shash_node *node;
1786 vtep_ctl_context_populate_cache(ctx);
1788 ls = find_lswitch(ctx, ctx->argv[1], true);
1789 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1791 node = shash_find(ucast_shash, ctx->argv[2]);
1797 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1798 vteprec_ucast_macs_local_delete(ucast_cfg);
1800 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1801 vteprec_ucast_macs_remote_delete(ucast_cfg);
1803 shash_delete(ucast_shash, node);
1805 vtep_ctl_context_invalidate_cache(ctx);
1809 cmd_del_ucast_local(struct vtep_ctl_context *ctx)
1811 del_ucast_entry(ctx, true);
1815 cmd_del_ucast_remote(struct vtep_ctl_context *ctx)
1817 del_ucast_entry(ctx, false);
1821 commit_mcast_entries(struct vtep_ctl_mcast_mac *mcast_mac)
1823 struct vtep_ctl_ploc *ploc;
1824 struct vteprec_physical_locator **locators = NULL;
1828 n_locators = list_size(&mcast_mac->locators);
1829 ovs_assert(n_locators);
1831 locators = xmalloc(n_locators * sizeof *locators);
1834 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
1835 locators[i] = (struct vteprec_physical_locator *)ploc->ploc_cfg;
1839 vteprec_physical_locator_set_set_locators(mcast_mac->ploc_set_cfg,
1847 add_mcast_entry(struct vtep_ctl_context *ctx,
1848 struct vtep_ctl_lswitch *ls, const char *mac,
1849 const char *encap, const char *dst_ip, bool local)
1851 struct shash *mcast_shash;
1852 struct vtep_ctl_mcast_mac *mcast_mac;
1853 struct vteprec_physical_locator *ploc_cfg;
1854 struct vteprec_physical_locator_set *ploc_set_cfg;
1856 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1858 /* Physical locator sets are immutable, so allocate a new one. */
1859 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1861 mcast_mac = shash_find_data(mcast_shash, mac);
1863 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mac, ploc_set_cfg, local);
1866 mcast_mac->local_cfg = vteprec_mcast_macs_local_insert(ctx->txn);
1867 vteprec_mcast_macs_local_set_MAC(mcast_mac->local_cfg, mac);
1868 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1870 vteprec_mcast_macs_local_set_logical_switch(mcast_mac->local_cfg,
1872 mcast_mac->remote_cfg = NULL;
1874 mcast_mac->remote_cfg = vteprec_mcast_macs_remote_insert(ctx->txn);
1875 vteprec_mcast_macs_remote_set_MAC(mcast_mac->remote_cfg, mac);
1876 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1878 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac->remote_cfg,
1880 mcast_mac->local_cfg = NULL;
1883 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1885 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1888 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1893 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1895 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1896 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1897 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1899 add_ploc_to_cache(ctx, ploc_cfg);
1902 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1903 commit_mcast_entries(mcast_mac);
1907 del_mcast_entry(struct vtep_ctl_context *ctx,
1908 struct vtep_ctl_lswitch *ls, const char *mac,
1909 const char *encap, const char *dst_ip, bool local)
1911 struct vtep_ctl_mcast_mac *mcast_mac;
1912 struct shash *mcast_shash;
1913 struct vteprec_physical_locator *ploc_cfg;
1914 struct vteprec_physical_locator_set *ploc_set_cfg;
1916 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1918 mcast_mac = shash_find_data(mcast_shash, mac);
1923 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1925 /* Couldn't find the physical locator, so just ignore. */
1929 /* Physical locator sets are immutable, so allocate a new one. */
1930 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1931 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1933 del_ploc_from_mcast_mac(mcast_mac, ploc_cfg);
1934 if (list_is_empty(&mcast_mac->locators)) {
1935 struct shash_node *node = shash_find(mcast_shash, mac);
1937 vteprec_physical_locator_set_delete(ploc_set_cfg);
1940 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1942 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1946 shash_delete(mcast_shash, node);
1949 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1952 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1955 commit_mcast_entries(mcast_mac);
1960 add_del_mcast_entry(struct vtep_ctl_context *ctx, bool add, bool local)
1962 struct vtep_ctl_lswitch *ls;
1967 vtep_ctl_context_populate_cache(ctx);
1969 ls = find_lswitch(ctx, ctx->argv[1], true);
1972 if (ctx->argc == 4) {
1973 encap = "vxlan_over_ipv4";
1974 dst_ip = ctx->argv[3];
1976 encap = ctx->argv[3];
1977 dst_ip = ctx->argv[4];
1981 add_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1983 del_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1986 vtep_ctl_context_invalidate_cache(ctx);
1990 cmd_add_mcast_local(struct vtep_ctl_context *ctx)
1992 add_del_mcast_entry(ctx, true, true);
1996 cmd_add_mcast_remote(struct vtep_ctl_context *ctx)
1998 add_del_mcast_entry(ctx, true, false);
2002 cmd_del_mcast_local(struct vtep_ctl_context *ctx)
2004 add_del_mcast_entry(ctx, false, true);
2008 cmd_del_mcast_remote(struct vtep_ctl_context *ctx)
2010 add_del_mcast_entry(ctx, false, false);
2014 clear_macs(struct vtep_ctl_context *ctx, bool local)
2016 struct vtep_ctl_lswitch *ls;
2017 const struct shash_node *node;
2018 struct shash *ucast_shash;
2019 struct shash *mcast_shash;
2021 vtep_ctl_context_populate_cache(ctx);
2022 ls = find_lswitch(ctx, ctx->argv[1], true);
2024 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2025 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2027 SHASH_FOR_EACH (node, ucast_shash) {
2029 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
2030 vteprec_ucast_macs_local_delete(ucast_cfg);
2032 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
2033 vteprec_ucast_macs_remote_delete(ucast_cfg);
2037 SHASH_FOR_EACH (node, mcast_shash) {
2038 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2040 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
2042 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
2046 vtep_ctl_context_invalidate_cache(ctx);
2050 cmd_clear_local_macs(struct vtep_ctl_context *ctx)
2052 clear_macs(ctx, true);
2056 cmd_clear_remote_macs(struct vtep_ctl_context *ctx)
2058 clear_macs(ctx, false);
2062 list_macs(struct vtep_ctl_context *ctx, bool local)
2064 struct vtep_ctl_lswitch *ls;
2065 const struct shash_node *node;
2066 struct shash *ucast_shash;
2067 struct svec ucast_macs;
2068 struct shash *mcast_shash;
2069 struct svec mcast_macs;
2071 vtep_ctl_context_populate_cache(ctx);
2072 ls = find_lswitch(ctx, ctx->argv[1], true);
2074 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2075 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2077 svec_init(&ucast_macs);
2078 SHASH_FOR_EACH (node, ucast_shash) {
2079 struct vteprec_ucast_macs_local *ucast_local = node->data;
2080 struct vteprec_ucast_macs_remote *ucast_remote = node->data;
2081 struct vteprec_physical_locator *ploc_cfg;
2084 ploc_cfg = local ? ucast_local->locator : ucast_remote->locator;
2086 entry = xasprintf(" %s -> %s/%s", node->name,
2087 ploc_cfg->encapsulation_type, ploc_cfg->dst_ip);
2088 svec_add_nocopy(&ucast_macs, entry);
2090 ds_put_format(&ctx->output, "ucast-mac-%s\n", local ? "local" : "remote");
2091 output_sorted(&ucast_macs, &ctx->output);
2092 ds_put_char(&ctx->output, '\n');
2093 svec_destroy(&ucast_macs);
2095 svec_init(&mcast_macs);
2096 SHASH_FOR_EACH (node, mcast_shash) {
2097 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2098 struct vtep_ctl_ploc *ploc;
2101 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
2102 entry = xasprintf(" %s -> %s/%s", node->name,
2103 ploc->ploc_cfg->encapsulation_type,
2104 ploc->ploc_cfg->dst_ip);
2105 svec_add_nocopy(&mcast_macs, entry);
2108 ds_put_format(&ctx->output, "mcast-mac-%s\n", local ? "local" : "remote");
2109 output_sorted(&mcast_macs, &ctx->output);
2110 ds_put_char(&ctx->output, '\n');
2111 svec_destroy(&mcast_macs);
2115 cmd_list_local_macs(struct vtep_ctl_context *ctx)
2117 list_macs(ctx, true);
2121 cmd_list_remote_macs(struct vtep_ctl_context *ctx)
2123 list_macs(ctx, false);
2127 verify_managers(const struct vteprec_global *vtep_global)
2131 vteprec_global_verify_managers(vtep_global);
2133 for (i = 0; i < vtep_global->n_managers; ++i) {
2134 const struct vteprec_manager *mgr = vtep_global->managers[i];
2136 vteprec_manager_verify_target(mgr);
2141 pre_manager(struct vtep_ctl_context *ctx)
2143 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_managers);
2144 ovsdb_idl_add_column(ctx->idl, &vteprec_manager_col_target);
2148 cmd_get_manager(struct vtep_ctl_context *ctx)
2150 const struct vteprec_global *vtep_global = ctx->vtep_global;
2151 struct svec targets;
2154 verify_managers(vtep_global);
2156 /* Print the targets in sorted order for reproducibility. */
2157 svec_init(&targets);
2159 for (i = 0; i < vtep_global->n_managers; i++) {
2160 svec_add(&targets, vtep_global->managers[i]->target);
2163 svec_sort_unique(&targets);
2164 for (i = 0; i < targets.n; i++) {
2165 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2167 svec_destroy(&targets);
2171 delete_managers(const struct vtep_ctl_context *ctx)
2173 const struct vteprec_global *vtep_global = ctx->vtep_global;
2176 /* Delete Manager rows pointed to by 'managers' column. */
2177 for (i = 0; i < vtep_global->n_managers; i++) {
2178 vteprec_manager_delete(vtep_global->managers[i]);
2181 /* Delete 'Manager' row refs in 'managers' column. */
2182 vteprec_global_set_managers(vtep_global, NULL, 0);
2186 cmd_del_manager(struct vtep_ctl_context *ctx)
2188 const struct vteprec_global *vtep_global = ctx->vtep_global;
2190 verify_managers(vtep_global);
2191 delete_managers(ctx);
2195 insert_managers(struct vtep_ctl_context *ctx, char *targets[], size_t n)
2197 struct vteprec_manager **managers;
2200 /* Insert each manager in a new row in Manager table. */
2201 managers = xmalloc(n * sizeof *managers);
2202 for (i = 0; i < n; i++) {
2203 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2204 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2206 managers[i] = vteprec_manager_insert(ctx->txn);
2207 vteprec_manager_set_target(managers[i], targets[i]);
2210 /* Store uuids of new Manager rows in 'managers' column. */
2211 vteprec_global_set_managers(ctx->vtep_global, managers, n);
2216 cmd_set_manager(struct vtep_ctl_context *ctx)
2218 const size_t n = ctx->argc - 1;
2220 verify_managers(ctx->vtep_global);
2221 delete_managers(ctx);
2222 insert_managers(ctx, &ctx->argv[1], n);
2225 /* Parameter commands. */
2227 struct vtep_ctl_row_id {
2228 const struct ovsdb_idl_table_class *table;
2229 const struct ovsdb_idl_column *name_column;
2230 const struct ovsdb_idl_column *uuid_column;
2233 struct vtep_ctl_table_class {
2234 struct ovsdb_idl_table_class *class;
2235 struct vtep_ctl_row_id row_ids[2];
2238 static const struct vtep_ctl_table_class tables[] = {
2239 {&vteprec_table_global,
2240 {{&vteprec_table_global, NULL, NULL},
2241 {NULL, NULL, NULL}}},
2243 {&vteprec_table_logical_binding_stats,
2244 {{NULL, NULL, NULL},
2245 {NULL, NULL, NULL}}},
2247 {&vteprec_table_logical_switch,
2248 {{&vteprec_table_logical_switch, &vteprec_logical_switch_col_name, NULL},
2249 {NULL, NULL, NULL}}},
2251 {&vteprec_table_ucast_macs_local,
2252 {{NULL, NULL, NULL},
2253 {NULL, NULL, NULL}}},
2255 {&vteprec_table_ucast_macs_remote,
2256 {{NULL, NULL, NULL},
2257 {NULL, NULL, NULL}}},
2259 {&vteprec_table_mcast_macs_local,
2260 {{NULL, NULL, NULL},
2261 {NULL, NULL, NULL}}},
2263 {&vteprec_table_mcast_macs_remote,
2264 {{NULL, NULL, NULL},
2265 {NULL, NULL, NULL}}},
2267 {&vteprec_table_manager,
2268 {{&vteprec_table_manager, &vteprec_manager_col_target, NULL},
2269 {NULL, NULL, NULL}}},
2271 {&vteprec_table_physical_locator,
2272 {{NULL, NULL, NULL},
2273 {NULL, NULL, NULL}}},
2275 {&vteprec_table_physical_locator_set,
2276 {{NULL, NULL, NULL},
2277 {NULL, NULL, NULL}}},
2279 {&vteprec_table_physical_port,
2280 {{&vteprec_table_physical_port, &vteprec_physical_port_col_name, NULL},
2281 {NULL, NULL, NULL}}},
2283 {&vteprec_table_physical_switch,
2284 {{&vteprec_table_physical_switch, &vteprec_physical_switch_col_name, NULL},
2285 {NULL, NULL, NULL}}},
2287 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2291 die_if_error(char *error)
2294 vtep_ctl_fatal("%s", error);
2299 to_lower_and_underscores(unsigned c)
2301 return c == '-' ? '_' : tolower(c);
2305 score_partial_match(const char *name, const char *s)
2309 if (!strcmp(name, s)) {
2312 for (score = 0; ; score++, name++, s++) {
2313 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2315 } else if (*name == '\0') {
2316 return UINT_MAX - 1;
2319 return *s == '\0' ? score : 0;
2322 static const struct vtep_ctl_table_class *
2323 get_table(const char *table_name)
2325 const struct vtep_ctl_table_class *table;
2326 const struct vtep_ctl_table_class *best_match = NULL;
2327 unsigned int best_score = 0;
2329 for (table = tables; table->class; table++) {
2330 unsigned int score = score_partial_match(table->class->name,
2332 if (score > best_score) {
2335 } else if (score == best_score) {
2341 } else if (best_score) {
2342 vtep_ctl_fatal("multiple table names match \"%s\"", table_name);
2344 vtep_ctl_fatal("unknown table \"%s\"", table_name);
2348 static const struct vtep_ctl_table_class *
2349 pre_get_table(struct vtep_ctl_context *ctx, const char *table_name)
2351 const struct vtep_ctl_table_class *table_class;
2354 table_class = get_table(table_name);
2355 ovsdb_idl_add_table(ctx->idl, table_class->class);
2357 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2358 const struct vtep_ctl_row_id *id = &table_class->row_ids[i];
2360 ovsdb_idl_add_table(ctx->idl, id->table);
2362 if (id->name_column) {
2363 ovsdb_idl_add_column(ctx->idl, id->name_column);
2365 if (id->uuid_column) {
2366 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2373 static const struct ovsdb_idl_row *
2374 get_row_by_id(struct vtep_ctl_context *ctx, const struct vtep_ctl_table_class *table,
2375 const struct vtep_ctl_row_id *id, const char *record_id)
2377 const struct ovsdb_idl_row *referrer, *final;
2383 if (!id->name_column) {
2384 if (strcmp(record_id, ".")) {
2387 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2388 if (!referrer || ovsdb_idl_next_row(referrer)) {
2392 const struct ovsdb_idl_row *row;
2395 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2397 row = ovsdb_idl_next_row(row))
2399 const struct ovsdb_datum *name;
2401 name = ovsdb_idl_get(row, id->name_column,
2402 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2403 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2405 vtep_ctl_fatal("multiple rows in %s match \"%s\"",
2406 table->class->name, record_id);
2417 if (id->uuid_column) {
2418 const struct ovsdb_datum *uuid;
2420 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2421 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2422 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2424 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2425 &uuid->keys[0].uuid);
2434 static const struct ovsdb_idl_row *
2435 get_row (struct vtep_ctl_context *ctx,
2436 const struct vtep_ctl_table_class *table, const char *record_id)
2438 const struct ovsdb_idl_row *row;
2441 if (uuid_from_string(&uuid, record_id)) {
2442 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2446 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2447 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2456 static const struct ovsdb_idl_row *
2457 must_get_row(struct vtep_ctl_context *ctx,
2458 const struct vtep_ctl_table_class *table, const char *record_id)
2460 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2462 vtep_ctl_fatal("no row \"%s\" in table %s",
2463 record_id, table->class->name);
2469 get_column(const struct vtep_ctl_table_class *table, const char *column_name,
2470 const struct ovsdb_idl_column **columnp)
2472 const struct ovsdb_idl_column *best_match = NULL;
2473 unsigned int best_score = 0;
2476 for (i = 0; i < table->class->n_columns; i++) {
2477 const struct ovsdb_idl_column *column = &table->class->columns[i];
2478 unsigned int score = score_partial_match(column->name, column_name);
2479 if (score > best_score) {
2480 best_match = column;
2482 } else if (score == best_score) {
2487 *columnp = best_match;
2490 } else if (best_score) {
2491 return xasprintf("%s contains more than one column whose name "
2492 "matches \"%s\"", table->class->name, column_name);
2494 return xasprintf("%s does not contain a column whose name matches "
2495 "\"%s\"", table->class->name, column_name);
2499 static struct ovsdb_symbol *
2500 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2502 struct ovsdb_symbol *symbol;
2505 vtep_ctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2509 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2512 symbol = ovsdb_symbol_table_insert(symtab, id);
2513 if (symbol->created) {
2514 vtep_ctl_fatal("row id \"%s\" may only be specified on one --id option",
2517 symbol->created = true;
2522 pre_get_column(struct vtep_ctl_context *ctx,
2523 const struct vtep_ctl_table_class *table, const char *column_name,
2524 const struct ovsdb_idl_column **columnp)
2526 die_if_error(get_column(table, column_name, columnp));
2527 ovsdb_idl_add_column(ctx->idl, *columnp);
2531 missing_operator_error(const char *arg, const char **allowed_operators,
2537 ds_put_format(&s, "%s: argument does not end in ", arg);
2538 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2539 if (n_allowed == 2) {
2540 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2541 } else if (n_allowed > 2) {
2544 for (i = 1; i < n_allowed - 1; i++) {
2545 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2547 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2549 ds_put_format(&s, " followed by a value.");
2551 return ds_steal_cstr(&s);
2554 /* Breaks 'arg' apart into a number of fields in the following order:
2556 * - The name of a column in 'table', stored into '*columnp'. The column
2557 * name may be abbreviated.
2559 * - Optionally ':' followed by a key string. The key is stored as a
2560 * malloc()'d string into '*keyp', or NULL if no key is present in
2563 * - If 'valuep' is nonnull, an operator followed by a value string. The
2564 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2565 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2566 * index of the operator within 'allowed_operators' is stored into
2567 * '*operatorp'. The value is stored as a malloc()'d string into
2568 * '*valuep', or NULL if no value is present in 'arg'.
2570 * On success, returns NULL. On failure, returned a malloc()'d string error
2571 * message and stores NULL into all of the nonnull output arguments. */
2572 static char * WARN_UNUSED_RESULT
2573 parse_column_key_value(const char *arg,
2574 const struct vtep_ctl_table_class *table,
2575 const struct ovsdb_idl_column **columnp, char **keyp,
2577 const char **allowed_operators, size_t n_allowed,
2580 const char *p = arg;
2584 ovs_assert(!(operatorp && !valuep));
2590 /* Parse column name. */
2591 error = ovsdb_token_parse(&p, &column_name);
2595 if (column_name[0] == '\0') {
2597 error = xasprintf("%s: missing column name", arg);
2600 error = get_column(table, column_name, columnp);
2606 /* Parse key string. */
2609 error = ovsdb_token_parse(&p, keyp);
2615 /* Parse value string. */
2621 if (!allowed_operators) {
2622 static const char *equals = "=";
2623 allowed_operators = =
2629 for (i = 0; i < n_allowed; i++) {
2630 const char *op = allowed_operators[i];
2631 size_t op_len = strlen(op);
2633 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2639 error = missing_operator_error(arg, allowed_operators, n_allowed);
2646 *valuep = xstrdup(p + best_len);
2649 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2670 static const struct ovsdb_idl_column *
2671 pre_parse_column_key_value(struct vtep_ctl_context *ctx,
2673 const struct vtep_ctl_table_class *table)
2675 const struct ovsdb_idl_column *column;
2680 die_if_error(ovsdb_token_parse(&p, &column_name));
2681 if (column_name[0] == '\0') {
2682 vtep_ctl_fatal("%s: missing column name", arg);
2685 pre_get_column(ctx, table, column_name, &column);
2692 check_mutable(const struct vtep_ctl_table_class *table,
2693 const struct ovsdb_idl_column *column)
2695 if (!column->mutable) {
2696 vtep_ctl_fatal("cannot modify read-only column %s in table %s",
2697 column->name, table->class->name);
2702 pre_cmd_get(struct vtep_ctl_context *ctx)
2704 const char *id = shash_find_data(&ctx->options, "--id");
2705 const char *table_name = ctx->argv[1];
2706 const struct vtep_ctl_table_class *table;
2709 /* Using "get" without --id or a column name could possibly make sense.
2710 * Maybe, for example, a vtep-ctl run wants to assert that a row exists.
2711 * But it is unlikely that an interactive user would want to do that, so
2712 * issue a warning if we're running on a terminal. */
2713 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2714 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2715 "possibly erroneous");
2718 table = pre_get_table(ctx, table_name);
2719 for (i = 3; i < ctx->argc; i++) {
2720 if (!strcasecmp(ctx->argv[i], "_uuid")
2721 || !strcasecmp(ctx->argv[i], "-uuid")) {
2725 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2730 cmd_get(struct vtep_ctl_context *ctx)
2732 const char *id = shash_find_data(&ctx->options, "--id");
2733 bool if_exists = shash_find(&ctx->options, "--if-exists");
2734 const char *table_name = ctx->argv[1];
2735 const char *record_id = ctx->argv[2];
2736 const struct vtep_ctl_table_class *table;
2737 const struct ovsdb_idl_row *row;
2738 struct ds *out = &ctx->output;
2741 table = get_table(table_name);
2742 row = must_get_row(ctx, table, record_id);
2744 struct ovsdb_symbol *symbol;
2747 symbol = create_symbol(ctx->symtab, id, &new);
2749 vtep_ctl_fatal("row id \"%s\" specified on \"get\" command was used "
2750 "before it was defined", id);
2752 symbol->uuid = row->uuid;
2754 /* This symbol refers to a row that already exists, so disable warnings
2755 * about it being unreferenced. */
2756 symbol->strong_ref = true;
2758 for (i = 3; i < ctx->argc; i++) {
2759 const struct ovsdb_idl_column *column;
2760 const struct ovsdb_datum *datum;
2763 /* Special case for obtaining the UUID of a row. We can't just do this
2764 * through parse_column_key_value() below since it returns a "struct
2765 * ovsdb_idl_column" and the UUID column doesn't have one. */
2766 if (!strcasecmp(ctx->argv[i], "_uuid")
2767 || !strcasecmp(ctx->argv[i], "-uuid")) {
2768 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2772 die_if_error(parse_column_key_value(ctx->argv[i], table,
2773 &column, &key_string,
2774 NULL, NULL, 0, NULL));
2776 ovsdb_idl_txn_verify(row, column);
2777 datum = ovsdb_idl_read(row, column);
2779 union ovsdb_atom key;
2782 if (column->type.value.type == OVSDB_TYPE_VOID) {
2783 vtep_ctl_fatal("cannot specify key to get for non-map column %s",
2787 die_if_error(ovsdb_atom_from_string(&key,
2789 key_string, ctx->symtab));
2791 idx = ovsdb_datum_find_key(datum, &key,
2792 column->type.key.type);
2793 if (idx == UINT_MAX) {
2795 vtep_ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2796 key_string, table->class->name, record_id,
2800 ovsdb_atom_to_string(&datum->values[idx],
2801 column->type.value.type, out);
2803 ovsdb_atom_destroy(&key, column->type.key.type);
2805 ovsdb_datum_to_string(datum, &column->type, out);
2807 ds_put_char(out, '\n');
2814 parse_column_names(const char *column_names,
2815 const struct vtep_ctl_table_class *table,
2816 const struct ovsdb_idl_column ***columnsp,
2819 const struct ovsdb_idl_column **columns;
2822 if (!column_names) {
2825 n_columns = table->class->n_columns + 1;
2826 columns = xmalloc(n_columns * sizeof *columns);
2828 for (i = 0; i < table->class->n_columns; i++) {
2829 columns[i + 1] = &table->class->columns[i];
2832 char *s = xstrdup(column_names);
2833 size_t allocated_columns;
2834 char *save_ptr = NULL;
2838 allocated_columns = n_columns = 0;
2839 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2840 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2841 const struct ovsdb_idl_column *column;
2843 if (!strcasecmp(column_name, "_uuid")) {
2846 die_if_error(get_column(table, column_name, &column));
2848 if (n_columns >= allocated_columns) {
2849 columns = x2nrealloc(columns, &allocated_columns,
2852 columns[n_columns++] = column;
2857 vtep_ctl_fatal("must specify at least one column name");
2860 *columnsp = columns;
2861 *n_columnsp = n_columns;
2866 pre_list_columns(struct vtep_ctl_context *ctx,
2867 const struct vtep_ctl_table_class *table,
2868 const char *column_names)
2870 const struct ovsdb_idl_column **columns;
2874 parse_column_names(column_names, table, &columns, &n_columns);
2875 for (i = 0; i < n_columns; i++) {
2877 ovsdb_idl_add_column(ctx->idl, columns[i]);
2884 pre_cmd_list(struct vtep_ctl_context *ctx)
2886 const char *column_names = shash_find_data(&ctx->options, "--columns");
2887 const char *table_name = ctx->argv[1];
2888 const struct vtep_ctl_table_class *table;
2890 table = pre_get_table(ctx, table_name);
2891 pre_list_columns(ctx, table, column_names);
2894 static struct table *
2895 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2900 out = xmalloc(sizeof *out);
2903 for (i = 0; i < n_columns; i++) {
2904 const struct ovsdb_idl_column *column = columns[i];
2905 const char *column_name = column ? column->name : "_uuid";
2907 table_add_column(out, "%s", column_name);
2914 list_record(const struct ovsdb_idl_row *row,
2915 const struct ovsdb_idl_column **columns, size_t n_columns,
2921 for (i = 0; i < n_columns; i++) {
2922 const struct ovsdb_idl_column *column = columns[i];
2923 struct cell *cell = table_add_cell(out);
2926 struct ovsdb_datum datum;
2927 union ovsdb_atom atom;
2929 atom.uuid = row->uuid;
2932 datum.values = NULL;
2935 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2936 cell->type = &ovsdb_type_uuid;
2938 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2940 cell->json = ovsdb_datum_to_json(datum, &column->type);
2941 cell->type = &column->type;
2947 cmd_list(struct vtep_ctl_context *ctx)
2949 const char *column_names = shash_find_data(&ctx->options, "--columns");
2950 const struct ovsdb_idl_column **columns;
2951 const char *table_name = ctx->argv[1];
2952 const struct vtep_ctl_table_class *table;
2957 table = get_table(table_name);
2958 parse_column_names(column_names, table, &columns, &n_columns);
2959 out = ctx->table = list_make_table(columns, n_columns);
2960 if (ctx->argc > 2) {
2961 for (i = 2; i < ctx->argc; i++) {
2962 list_record(must_get_row(ctx, table, ctx->argv[i]),
2963 columns, n_columns, out);
2966 const struct ovsdb_idl_row *row;
2968 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
2969 row = ovsdb_idl_next_row(row)) {
2970 list_record(row, columns, n_columns, out);
2977 pre_cmd_find(struct vtep_ctl_context *ctx)
2979 const char *column_names = shash_find_data(&ctx->options, "--columns");
2980 const char *table_name = ctx->argv[1];
2981 const struct vtep_ctl_table_class *table;
2984 table = pre_get_table(ctx, table_name);
2985 pre_list_columns(ctx, table, column_names);
2986 for (i = 2; i < ctx->argc; i++) {
2987 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2992 cmd_find(struct vtep_ctl_context *ctx)
2994 const char *column_names = shash_find_data(&ctx->options, "--columns");
2995 const struct ovsdb_idl_column **columns;
2996 const char *table_name = ctx->argv[1];
2997 const struct vtep_ctl_table_class *table;
2998 const struct ovsdb_idl_row *row;
3002 table = get_table(table_name);
3003 parse_column_names(column_names, table, &columns, &n_columns);
3004 out = ctx->table = list_make_table(columns, n_columns);
3005 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3006 row = ovsdb_idl_next_row(row)) {
3009 for (i = 2; i < ctx->argc; i++) {
3010 if (!is_condition_satisfied(table, row, ctx->argv[i],
3015 list_record(row, columns, n_columns, out);
3023 pre_cmd_set(struct vtep_ctl_context *ctx)
3025 const char *table_name = ctx->argv[1];
3026 const struct vtep_ctl_table_class *table;
3029 table = pre_get_table(ctx, table_name);
3030 for (i = 3; i < ctx->argc; i++) {
3031 const struct ovsdb_idl_column *column;
3033 column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
3034 check_mutable(table, column);
3039 set_column(const struct vtep_ctl_table_class *table,
3040 const struct ovsdb_idl_row *row, const char *arg,
3041 struct ovsdb_symbol_table *symtab)
3043 const struct ovsdb_idl_column *column;
3044 char *key_string, *value_string;
3047 error = parse_column_key_value(arg, table, &column, &key_string,
3048 NULL, NULL, 0, &value_string);
3049 die_if_error(error);
3050 if (!value_string) {
3051 vtep_ctl_fatal("%s: missing value", arg);
3055 union ovsdb_atom key, value;
3056 struct ovsdb_datum datum;
3058 if (column->type.value.type == OVSDB_TYPE_VOID) {
3059 vtep_ctl_fatal("cannot specify key to set for non-map column %s",
3063 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3064 key_string, symtab));
3065 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3066 value_string, symtab));
3068 ovsdb_datum_init_empty(&datum);
3069 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3071 ovsdb_atom_destroy(&key, column->type.key.type);
3072 ovsdb_atom_destroy(&value, column->type.value.type);
3074 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3075 &column->type, false);
3076 ovsdb_idl_txn_write(row, column, &datum);
3078 struct ovsdb_datum datum;
3080 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3081 value_string, symtab));
3082 ovsdb_idl_txn_write(row, column, &datum);
3090 cmd_set(struct vtep_ctl_context *ctx)
3092 const char *table_name = ctx->argv[1];
3093 const char *record_id = ctx->argv[2];
3094 const struct vtep_ctl_table_class *table;
3095 const struct ovsdb_idl_row *row;
3098 table = get_table(table_name);
3099 row = must_get_row(ctx, table, record_id);
3100 for (i = 3; i < ctx->argc; i++) {
3101 set_column(table, row, ctx->argv[i], ctx->symtab);
3106 pre_cmd_add(struct vtep_ctl_context *ctx)
3108 const char *table_name = ctx->argv[1];
3109 const char *column_name = ctx->argv[3];
3110 const struct vtep_ctl_table_class *table;
3111 const struct ovsdb_idl_column *column;
3113 table = pre_get_table(ctx, table_name);
3114 pre_get_column(ctx, table, column_name, &column);
3115 check_mutable(table, column);
3119 cmd_add(struct vtep_ctl_context *ctx)
3121 const char *table_name = ctx->argv[1];
3122 const char *record_id = ctx->argv[2];
3123 const char *column_name = ctx->argv[3];
3124 const struct vtep_ctl_table_class *table;
3125 const struct ovsdb_idl_column *column;
3126 const struct ovsdb_idl_row *row;
3127 const struct ovsdb_type *type;
3128 struct ovsdb_datum old;
3131 table = get_table(table_name);
3132 row = must_get_row(ctx, table, record_id);
3133 die_if_error(get_column(table, column_name, &column));
3135 type = &column->type;
3136 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3137 for (i = 4; i < ctx->argc; i++) {
3138 struct ovsdb_type add_type;
3139 struct ovsdb_datum add;
3143 add_type.n_max = UINT_MAX;
3144 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3146 ovsdb_datum_union(&old, &add, type, false);
3147 ovsdb_datum_destroy(&add, type);
3149 if (old.n > type->n_max) {
3150 vtep_ctl_fatal("\"add\" operation would put %u %s in column %s of "
3151 "table %s but the maximum number is %u",
3153 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3154 column->name, table->class->name, type->n_max);
3156 ovsdb_idl_txn_verify(row, column);
3157 ovsdb_idl_txn_write(row, column, &old);
3161 pre_cmd_remove(struct vtep_ctl_context *ctx)
3163 const char *table_name = ctx->argv[1];
3164 const char *column_name = ctx->argv[3];
3165 const struct vtep_ctl_table_class *table;
3166 const struct ovsdb_idl_column *column;
3168 table = pre_get_table(ctx, table_name);
3169 pre_get_column(ctx, table, column_name, &column);
3170 check_mutable(table, column);
3174 cmd_remove(struct vtep_ctl_context *ctx)
3176 const char *table_name = ctx->argv[1];
3177 const char *record_id = ctx->argv[2];
3178 const char *column_name = ctx->argv[3];
3179 const struct vtep_ctl_table_class *table;
3180 const struct ovsdb_idl_column *column;
3181 const struct ovsdb_idl_row *row;
3182 const struct ovsdb_type *type;
3183 struct ovsdb_datum old;
3186 table = get_table(table_name);
3187 row = must_get_row(ctx, table, record_id);
3188 die_if_error(get_column(table, column_name, &column));
3190 type = &column->type;
3191 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3192 for (i = 4; i < ctx->argc; i++) {
3193 struct ovsdb_type rm_type;
3194 struct ovsdb_datum rm;
3199 rm_type.n_max = UINT_MAX;
3200 error = ovsdb_datum_from_string(&rm, &rm_type,
3201 ctx->argv[i], ctx->symtab);
3202 if (error && ovsdb_type_is_map(&rm_type)) {
3204 rm_type.value.type = OVSDB_TYPE_VOID;
3205 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3206 ctx->argv[i], ctx->symtab));
3208 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3209 ovsdb_datum_destroy(&rm, &rm_type);
3211 if (old.n < type->n_min) {
3212 vtep_ctl_fatal("\"remove\" operation would put %u %s in column %s of "
3213 "table %s but the minimum number is %u",
3215 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3216 column->name, table->class->name, type->n_min);
3218 ovsdb_idl_txn_verify(row, column);
3219 ovsdb_idl_txn_write(row, column, &old);
3223 pre_cmd_clear(struct vtep_ctl_context *ctx)
3225 const char *table_name = ctx->argv[1];
3226 const struct vtep_ctl_table_class *table;
3229 table = pre_get_table(ctx, table_name);
3230 for (i = 3; i < ctx->argc; i++) {
3231 const struct ovsdb_idl_column *column;
3233 pre_get_column(ctx, table, ctx->argv[i], &column);
3234 check_mutable(table, column);
3239 cmd_clear(struct vtep_ctl_context *ctx)
3241 const char *table_name = ctx->argv[1];
3242 const char *record_id = ctx->argv[2];
3243 const struct vtep_ctl_table_class *table;
3244 const struct ovsdb_idl_row *row;
3247 table = get_table(table_name);
3248 row = must_get_row(ctx, table, record_id);
3249 for (i = 3; i < ctx->argc; i++) {
3250 const struct ovsdb_idl_column *column;
3251 const struct ovsdb_type *type;
3252 struct ovsdb_datum datum;
3254 die_if_error(get_column(table, ctx->argv[i], &column));
3256 type = &column->type;
3257 if (type->n_min > 0) {
3258 vtep_ctl_fatal("\"clear\" operation cannot be applied to column %s "
3259 "of table %s, which is not allowed to be empty",
3260 column->name, table->class->name);
3263 ovsdb_datum_init_empty(&datum);
3264 ovsdb_idl_txn_write(row, column, &datum);
3269 pre_create(struct vtep_ctl_context *ctx)
3271 const char *id = shash_find_data(&ctx->options, "--id");
3272 const char *table_name = ctx->argv[1];
3273 const struct vtep_ctl_table_class *table;
3275 table = get_table(table_name);
3276 if (!id && !table->class->is_root) {
3277 VLOG_WARN("applying \"create\" command to table %s without --id "
3278 "option will have no effect", table->class->name);
3283 cmd_create(struct vtep_ctl_context *ctx)
3285 const char *id = shash_find_data(&ctx->options, "--id");
3286 const char *table_name = ctx->argv[1];
3287 const struct vtep_ctl_table_class *table = get_table(table_name);
3288 const struct ovsdb_idl_row *row;
3289 const struct uuid *uuid;
3293 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3294 if (table->class->is_root) {
3295 /* This table is in the root set, meaning that rows created in it
3296 * won't disappear even if they are unreferenced, so disable
3297 * warnings about that by pretending that there is a reference. */
3298 symbol->strong_ref = true;
3300 uuid = &symbol->uuid;
3305 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3306 for (i = 2; i < ctx->argc; i++) {
3307 set_column(table, row, ctx->argv[i], ctx->symtab);
3309 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3312 /* This function may be used as the 'postprocess' function for commands that
3313 * insert new rows into the database. It expects that the command's 'run'
3314 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3315 * sole output. It replaces that output by the row's permanent UUID assigned
3316 * by the database server and appends a new-line.
3318 * Currently we use this only for "create", because the higher-level commands
3319 * are supposed to be independent of the actual structure of the VTEP
3322 post_create(struct vtep_ctl_context *ctx)
3324 const struct uuid *real;
3327 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3330 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3332 ds_clear(&ctx->output);
3333 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3335 ds_put_char(&ctx->output, '\n');
3339 pre_cmd_destroy(struct vtep_ctl_context *ctx)
3341 const char *table_name = ctx->argv[1];
3343 pre_get_table(ctx, table_name);
3347 cmd_destroy(struct vtep_ctl_context *ctx)
3349 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3350 bool delete_all = shash_find(&ctx->options, "--all");
3351 const char *table_name = ctx->argv[1];
3352 const struct vtep_ctl_table_class *table;
3355 table = get_table(table_name);
3357 if (delete_all && ctx->argc > 2) {
3358 vtep_ctl_fatal("--all and records argument should not be specified together");
3361 if (delete_all && !must_exist) {
3362 vtep_ctl_fatal("--all and --if-exists should not be specified together");
3366 const struct ovsdb_idl_row *row;
3367 const struct ovsdb_idl_row *next_row;
3369 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3371 next_row = ovsdb_idl_next_row(row);
3372 ovsdb_idl_txn_delete(row);
3376 for (i = 2; i < ctx->argc; i++) {
3377 const struct ovsdb_idl_row *row;
3379 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3381 ovsdb_idl_txn_delete(row);
3388 RELOP(RELOP_EQ, "=") \
3389 RELOP(RELOP_NE, "!=") \
3390 RELOP(RELOP_LT, "<") \
3391 RELOP(RELOP_GT, ">") \
3392 RELOP(RELOP_LE, "<=") \
3393 RELOP(RELOP_GE, ">=") \
3394 RELOP(RELOP_SET_EQ, "{=}") \
3395 RELOP(RELOP_SET_NE, "{!=}") \
3396 RELOP(RELOP_SET_LT, "{<}") \
3397 RELOP(RELOP_SET_GT, "{>}") \
3398 RELOP(RELOP_SET_LE, "{<=}") \
3399 RELOP(RELOP_SET_GE, "{>=}")
3402 #define RELOP(ENUM, STRING) ENUM,
3408 is_set_operator(enum relop op)
3410 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3411 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3412 op == RELOP_SET_LE || op == RELOP_SET_GE);
3416 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3417 const struct ovsdb_type *type, enum relop op)
3422 return ovsdb_datum_compare_3way(a, b, type) == 0;
3425 return ovsdb_datum_compare_3way(a, b, type) != 0;
3427 return ovsdb_datum_compare_3way(a, b, type) < 0;
3429 return ovsdb_datum_compare_3way(a, b, type) > 0;
3431 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3433 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3436 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3438 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3440 return ovsdb_datum_includes_all(a, b, type);
3442 return ovsdb_datum_includes_all(b, a, type);
3450 is_condition_satisfied(const struct vtep_ctl_table_class *table,
3451 const struct ovsdb_idl_row *row, const char *arg,
3452 struct ovsdb_symbol_table *symtab)
3454 static const char *operators[] = {
3455 #define RELOP(ENUM, STRING) STRING,
3460 const struct ovsdb_idl_column *column;
3461 const struct ovsdb_datum *have_datum;
3462 char *key_string, *value_string;
3463 struct ovsdb_type type;
3468 error = parse_column_key_value(arg, table, &column, &key_string,
3469 &operator, operators, ARRAY_SIZE(operators),
3471 die_if_error(error);
3472 if (!value_string) {
3473 vtep_ctl_fatal("%s: missing value", arg);
3476 type = column->type;
3477 type.n_max = UINT_MAX;
3479 have_datum = ovsdb_idl_read(row, column);
3481 union ovsdb_atom want_key;
3482 struct ovsdb_datum b;
3485 if (column->type.value.type == OVSDB_TYPE_VOID) {
3486 vtep_ctl_fatal("cannot specify key to check for non-map column %s",
3490 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3491 key_string, symtab));
3493 type.key = type.value;
3494 type.value.type = OVSDB_TYPE_VOID;
3495 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3497 idx = ovsdb_datum_find_key(have_datum,
3498 &want_key, column->type.key.type);
3499 if (idx == UINT_MAX && !is_set_operator(operator)) {
3502 struct ovsdb_datum a;
3504 if (idx != UINT_MAX) {
3506 a.keys = &have_datum->values[idx];
3514 retval = evaluate_relop(&a, &b, &type, operator);
3517 ovsdb_atom_destroy(&want_key, column->type.key.type);
3518 ovsdb_datum_destroy(&b, &type);
3520 struct ovsdb_datum want_datum;
3522 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3523 value_string, symtab));
3524 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3525 ovsdb_datum_destroy(&want_datum, &column->type);
3535 pre_cmd_wait_until(struct vtep_ctl_context *ctx)
3537 const char *table_name = ctx->argv[1];
3538 const struct vtep_ctl_table_class *table;
3541 table = pre_get_table(ctx, table_name);
3543 for (i = 3; i < ctx->argc; i++) {
3544 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3549 cmd_wait_until(struct vtep_ctl_context *ctx)
3551 const char *table_name = ctx->argv[1];
3552 const char *record_id = ctx->argv[2];
3553 const struct vtep_ctl_table_class *table;
3554 const struct ovsdb_idl_row *row;
3557 table = get_table(table_name);
3559 row = get_row(ctx, table, record_id);
3561 ctx->try_again = true;
3565 for (i = 3; i < ctx->argc; i++) {
3566 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3567 ctx->try_again = true;
3573 /* Prepares 'ctx', which has already been initialized with
3574 * vtep_ctl_context_init(), for processing 'command'. */
3576 vtep_ctl_context_init_command(struct vtep_ctl_context *ctx,
3577 struct vtep_ctl_command *command)
3579 ctx->argc = command->argc;
3580 ctx->argv = command->argv;
3581 ctx->options = command->options;
3583 ds_swap(&ctx->output, &command->output);
3584 ctx->table = command->table;
3586 ctx->verified_ports = false;
3588 ctx->try_again = false;
3591 /* Prepares 'ctx' for processing commands, initializing its members with the
3592 * values passed in as arguments.
3594 * If 'command' is nonnull, calls vtep_ctl_context_init_command() to prepare for
3595 * that particular command. */
3597 vtep_ctl_context_init(struct vtep_ctl_context *ctx,
3598 struct vtep_ctl_command *command,
3599 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3600 const struct vteprec_global *vtep_global,
3601 struct ovsdb_symbol_table *symtab)
3604 vtep_ctl_context_init_command(ctx, command);
3608 ctx->vtep_global = vtep_global;
3609 ctx->symtab = symtab;
3610 ctx->cache_valid = false;
3613 /* Completes processing of 'command' within 'ctx'. */
3615 vtep_ctl_context_done_command(struct vtep_ctl_context *ctx,
3616 struct vtep_ctl_command *command)
3618 ds_swap(&ctx->output, &command->output);
3619 command->table = ctx->table;
3622 /* Finishes up with 'ctx'.
3624 * If command is nonnull, first calls vtep_ctl_context_done_command() to complete
3625 * processing that command within 'ctx'. */
3627 vtep_ctl_context_done(struct vtep_ctl_context *ctx, struct vtep_ctl_command *command)
3630 vtep_ctl_context_done_command(ctx, command);
3635 run_prerequisites(struct vtep_ctl_command *commands, size_t n_commands,
3636 struct ovsdb_idl *idl)
3638 struct vtep_ctl_command *c;
3640 ovsdb_idl_add_table(idl, &vteprec_table_global);
3641 for (c = commands; c < &commands[n_commands]; c++) {
3642 if (c->syntax->prerequisites) {
3643 struct vtep_ctl_context ctx;
3645 ds_init(&c->output);
3648 vtep_ctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3649 (c->syntax->prerequisites)(&ctx);
3650 vtep_ctl_context_done(&ctx, c);
3652 ovs_assert(!c->output.string);
3653 ovs_assert(!c->table);
3659 do_vtep_ctl(const char *args, struct vtep_ctl_command *commands,
3660 size_t n_commands, struct ovsdb_idl *idl)
3662 struct ovsdb_idl_txn *txn;
3663 const struct vteprec_global *vtep_global;
3664 enum ovsdb_idl_txn_status status;
3665 struct ovsdb_symbol_table *symtab;
3666 struct vtep_ctl_context ctx;
3667 struct vtep_ctl_command *c;
3668 struct shash_node *node;
3671 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3673 ovsdb_idl_txn_set_dry_run(txn);
3676 ovsdb_idl_txn_add_comment(txn, "vtep-ctl: %s", args);
3678 vtep_global = vteprec_global_first(idl);
3680 /* XXX add verification that table is empty */
3681 vtep_global = vteprec_global_insert(txn);
3684 symtab = ovsdb_symbol_table_create();
3685 for (c = commands; c < &commands[n_commands]; c++) {
3686 ds_init(&c->output);
3689 vtep_ctl_context_init(&ctx, NULL, idl, txn, vtep_global, symtab);
3690 for (c = commands; c < &commands[n_commands]; c++) {
3691 vtep_ctl_context_init_command(&ctx, c);
3692 if (c->syntax->run) {
3693 (c->syntax->run)(&ctx);
3695 vtep_ctl_context_done_command(&ctx, c);
3697 if (ctx.try_again) {
3698 vtep_ctl_context_done(&ctx, NULL);
3702 vtep_ctl_context_done(&ctx, NULL);
3704 SHASH_FOR_EACH (node, &symtab->sh) {
3705 struct ovsdb_symbol *symbol = node->data;
3706 if (!symbol->created) {
3707 vtep_ctl_fatal("row id \"%s\" is referenced but never created "
3708 "(e.g. with \"-- --id=%s create ...\")",
3709 node->name, node->name);
3711 if (!symbol->strong_ref) {
3712 if (!symbol->weak_ref) {
3713 VLOG_WARN("row id \"%s\" was created but no reference to it "
3714 "was inserted, so it will not actually appear in "
3715 "the database", node->name);
3717 VLOG_WARN("row id \"%s\" was created but only a weak "
3718 "reference to it was inserted, so it will not "
3719 "actually appear in the database", node->name);
3724 status = ovsdb_idl_txn_commit_block(txn);
3725 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3726 for (c = commands; c < &commands[n_commands]; c++) {
3727 if (c->syntax->postprocess) {
3728 struct vtep_ctl_context ctx;
3730 vtep_ctl_context_init(&ctx, c, idl, txn, vtep_global, symtab);
3731 (c->syntax->postprocess)(&ctx);
3732 vtep_ctl_context_done(&ctx, c);
3736 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3737 ovsdb_idl_txn_destroy(txn);
3738 txn = the_idl_txn = NULL;
3741 case TXN_UNCOMMITTED:
3742 case TXN_INCOMPLETE:
3746 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3747 vtep_ctl_fatal("transaction aborted");
3757 vtep_ctl_fatal("transaction error: %s", error);
3759 case TXN_NOT_LOCKED:
3760 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3761 vtep_ctl_fatal("database not locked");
3768 ovsdb_symbol_table_destroy(symtab);
3770 for (c = commands; c < &commands[n_commands]; c++) {
3771 struct ds *ds = &c->output;
3774 table_print(c->table, &table_style);
3775 } else if (oneline) {
3779 for (j = 0; j < ds->length; j++) {
3780 int ch = ds->string[j];
3783 fputs("\\n", stdout);
3787 fputs("\\\\", stdout);
3796 fputs(ds_cstr(ds), stdout);
3798 ds_destroy(&c->output);
3799 table_destroy(c->table);
3802 shash_destroy_free_data(&c->options);
3806 ovsdb_idl_destroy(idl);
3811 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3812 * resources and return so that the caller can try again. */
3814 ovsdb_idl_txn_abort(txn);
3815 ovsdb_idl_txn_destroy(txn);
3817 ovsdb_symbol_table_destroy(symtab);
3818 for (c = commands; c < &commands[n_commands]; c++) {
3819 ds_destroy(&c->output);
3820 table_destroy(c->table);
3826 static const struct vtep_ctl_command_syntax all_commands[] = {
3827 /* Physical Switch commands. */
3828 {"add-ps", 1, 1, pre_get_info, cmd_add_ps, NULL, "--may-exist", RW},
3829 {"del-ps", 1, 1, pre_get_info, cmd_del_ps, NULL, "--if-exists", RW},
3830 {"list-ps", 0, 0, pre_get_info, cmd_list_ps, NULL, "", RO},
3831 {"ps-exists", 1, 1, pre_get_info, cmd_ps_exists, NULL, "", RO},
3833 /* Port commands. */
3834 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3835 {"add-port", 2, 2, pre_get_info, cmd_add_port, NULL, "--may-exist",
3837 {"del-port", 2, 2, pre_get_info, cmd_del_port, NULL, "--if-exists", RW},
3839 /* Logical Switch commands. */
3840 {"add-ls", 1, 1, pre_get_info, cmd_add_ls, NULL, "--may-exist", RW},
3841 {"del-ls", 1, 1, pre_get_info, cmd_del_ls, NULL, "--if-exists", RW},
3842 {"list-ls", 0, 0, pre_get_info, cmd_list_ls, NULL, "", RO},
3843 {"ls-exists", 1, 1, pre_get_info, cmd_ls_exists, NULL, "", RO},
3844 {"list-bindings", 2, 2, pre_get_info, cmd_list_bindings, NULL, "", RO},
3845 {"bind-ls", 4, 4, pre_get_info, cmd_bind_ls, NULL, "", RO},
3846 {"unbind-ls", 3, 3, pre_get_info, cmd_unbind_ls, NULL, "", RO},
3848 /* MAC binding commands. */
3849 {"add-ucast-local", 3, 4, pre_get_info, cmd_add_ucast_local, NULL, "", RW},
3850 {"del-ucast-local", 2, 2, pre_get_info, cmd_del_ucast_local, NULL, "", RW},
3851 {"add-mcast-local", 3, 4, pre_get_info, cmd_add_mcast_local, NULL, "", RW},
3852 {"del-mcast-local", 3, 4, pre_get_info, cmd_del_mcast_local, NULL, "", RW},
3853 {"clear-local-macs", 1, 1, pre_get_info, cmd_clear_local_macs, NULL, "",
3855 {"list-local-macs", 1, 1, pre_get_info, cmd_list_local_macs, NULL, "", RO},
3856 {"add-ucast-remote", 3, 4, pre_get_info, cmd_add_ucast_remote, NULL, "",
3858 {"del-ucast-remote", 2, 2, pre_get_info, cmd_del_ucast_remote, NULL, "",
3860 {"add-mcast-remote", 3, 4, pre_get_info, cmd_add_mcast_remote, NULL, "",
3862 {"del-mcast-remote", 3, 4, pre_get_info, cmd_del_mcast_remote, NULL, "",
3864 {"clear-remote-macs", 1, 1, pre_get_info, cmd_clear_remote_macs, NULL, "",
3866 {"list-remote-macs", 1, 1, pre_get_info, cmd_list_remote_macs, NULL, "",
3869 /* Manager commands. */
3870 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3871 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
3872 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3874 /* Database commands. */
3875 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
3876 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3877 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3878 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3879 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3880 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3881 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3882 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3883 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3884 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
3885 "--if-exists,--all", RW},
3886 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3889 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},