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"
36 #include "ovsdb-data.h"
37 #include "ovsdb-idl.h"
38 #include "poll-loop.h"
41 #include "stream-ssl.h"
45 #include "lib/vtep-idl.h"
52 VLOG_DEFINE_THIS_MODULE(vtep_ctl);
54 /* vtep_ctl_fatal() also logs the error, so it is preferred in this file. */
55 #define ovs_fatal please_use_vtep_ctl_fatal_instead_of_ovs_fatal
57 struct vtep_ctl_context;
59 /* A command supported by vtep-ctl. */
60 struct vtep_ctl_command_syntax {
61 const char *name; /* e.g. "add-ps" */
62 int min_args; /* Min number of arguments following name. */
63 int max_args; /* Max number of arguments following name. */
65 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
66 * each column or table in ctx->idl that it uses. */
67 void (*prerequisites)(struct vtep_ctl_context *ctx);
69 /* Does the actual work of the command and puts the command's output, if
70 * any, in ctx->output or ctx->table.
72 * Alternatively, if some prerequisite of the command is not met and the
73 * caller should wait for something to change and then retry, it may set
74 * ctx->try_again to true. (Only the "wait-until" command currently does
76 void (*run)(struct vtep_ctl_context *ctx);
78 /* If nonnull, called after the transaction has been successfully
79 * committed. ctx->output is the output from the "run" function, which
80 * this function may modify and otherwise postprocess as needed. (Only the
81 * "create" command currently does any postprocessing.) */
82 void (*postprocess)(struct vtep_ctl_context *ctx);
84 /* A comma-separated list of supported options, e.g. "--a,--b", or the
85 * empty string if the command does not support any options. */
87 enum { RO, RW } mode; /* Does this command modify the database? */
90 struct vtep_ctl_command {
91 /* Data that remains constant after initialization. */
92 const struct vtep_ctl_command_syntax *syntax;
97 /* Data modified by commands. */
102 /* --db: The database server to contact. */
103 static const char *db;
105 /* --oneline: Write each command's output as a single line? */
108 /* --dry-run: Do not commit any changes. */
111 /* --timeout: Time to wait for a connection to 'db'. */
114 /* Format for table output. */
115 static struct table_style table_style = TABLE_STYLE_DEFAULT;
117 /* All supported commands. */
118 static const struct vtep_ctl_command_syntax all_commands[];
120 /* The IDL we're using and the current transaction, if any.
121 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
122 * Other code should use its context arguments. */
123 static struct ovsdb_idl *the_idl;
124 static struct ovsdb_idl_txn *the_idl_txn;
126 static void vtep_ctl_exit(int status) NO_RETURN;
127 static void vtep_ctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
128 static char *default_db(void);
129 static void usage(void) NO_RETURN;
130 static void parse_options(int argc, char *argv[], struct shash *local_options);
131 static bool might_write_to_db(char **argv);
133 static struct vtep_ctl_command *parse_commands(int argc, char *argv[],
134 struct shash *local_options,
135 size_t *n_commandsp);
136 static void parse_command(int argc, char *argv[], struct shash *local_options,
137 struct vtep_ctl_command *);
138 static const struct vtep_ctl_command_syntax *find_command(const char *name);
139 static void run_prerequisites(struct vtep_ctl_command[], size_t n_commands,
141 static void do_vtep_ctl(const char *args, struct vtep_ctl_command *, size_t n,
144 static const struct vtep_ctl_table_class *get_table(const char *table_name);
145 static void set_column(const struct vtep_ctl_table_class *,
146 const struct ovsdb_idl_row *, const char *arg,
147 struct ovsdb_symbol_table *);
149 static bool is_condition_satisfied(const struct vtep_ctl_table_class *,
150 const struct ovsdb_idl_row *,
152 struct ovsdb_symbol_table *);
154 static struct vtep_ctl_lswitch *find_lswitch(struct vtep_ctl_context *,
159 main(int argc, char *argv[])
161 extern struct vlog_module VLM_reconnect;
162 struct ovsdb_idl *idl;
163 struct vtep_ctl_command *commands;
164 struct shash local_options;
169 set_program_name(argv[0]);
170 signal(SIGPIPE, SIG_IGN);
171 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
172 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
175 /* Log our arguments. This is often valuable for debugging systems. */
176 args = process_escape_args(argv);
177 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
179 /* Parse command line. */
180 shash_init(&local_options);
181 parse_options(argc, argv, &local_options);
182 commands = parse_commands(argc - optind, argv + optind, &local_options,
189 /* Initialize IDL. */
190 idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
191 run_prerequisites(commands, n_commands, idl);
193 /* Execute the commands.
195 * 'seqno' is the database sequence number for which we last tried to
196 * execute our transaction. There's no point in trying to commit more than
197 * once for any given sequence number, because if the transaction fails
198 * it's because the database changed and we need to obtain an up-to-date
199 * view of the database before we try the transaction again. */
200 seqno = ovsdb_idl_get_seqno(idl);
204 if (seqno != ovsdb_idl_get_seqno(idl)) {
205 seqno = ovsdb_idl_get_seqno(idl);
206 do_vtep_ctl(args, commands, n_commands, idl);
209 if (seqno == ovsdb_idl_get_seqno(idl)) {
216 static struct option *
217 find_option(const char *name, struct option *options, size_t n_options)
221 for (i = 0; i < n_options; i++) {
222 if (!strcmp(options[i].name, name)) {
229 static struct option *
230 add_option(struct option **optionsp, size_t *n_optionsp,
231 size_t *allocated_optionsp)
233 if (*n_optionsp >= *allocated_optionsp) {
234 *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
237 return &(*optionsp)[(*n_optionsp)++];
241 parse_options(int argc, char *argv[], struct shash *local_options)
244 OPT_DB = UCHAR_MAX + 1,
253 static const struct option global_long_options[] = {
254 {"db", required_argument, NULL, OPT_DB},
255 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
256 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
257 {"oneline", no_argument, NULL, OPT_ONELINE},
258 {"timeout", required_argument, NULL, 't'},
259 {"help", no_argument, NULL, 'h'},
260 {"version", no_argument, NULL, 'V'},
263 STREAM_SSL_LONG_OPTIONS,
264 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
267 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
268 char *tmp, *short_options;
270 const struct vtep_ctl_command_syntax *p;
271 struct option *options, *o;
272 size_t allocated_options;
276 tmp = long_options_to_short_options(global_long_options);
277 short_options = xasprintf("+%s", tmp);
280 /* We want to parse both global and command-specific options here, but
281 * getopt_long() isn't too convenient for the job. We copy our global
282 * options into a dynamic array, then append all of the command-specific
284 options = xmemdup(global_long_options, sizeof global_long_options);
285 allocated_options = ARRAY_SIZE(global_long_options);
286 n_options = n_global_long_options;
287 for (p = all_commands; p->name; p++) {
289 char *save_ptr = NULL;
293 s = xstrdup(p->options);
294 for (name = strtok_r(s, ",", &save_ptr); name != NULL;
295 name = strtok_r(NULL, ",", &save_ptr)) {
299 ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
302 equals = strchr(name, '=');
304 has_arg = required_argument;
307 has_arg = no_argument;
310 o = find_option(name, options, n_options);
312 ovs_assert(o - options >= n_global_long_options);
313 ovs_assert(o->has_arg == has_arg);
315 o = add_option(&options, &n_options, &allocated_options);
316 o->name = xstrdup(name);
317 o->has_arg = has_arg;
326 o = add_option(&options, &n_options, &allocated_options);
327 memset(o, 0, sizeof *o);
329 table_style.format = TF_LIST;
335 c = getopt_long(argc, argv, short_options, options, &idx);
350 vlog_set_levels(&VLM_vtep_ctl, VLF_SYSLOG, VLL_WARN);
358 if (shash_find(local_options, options[idx].name)) {
359 vtep_ctl_fatal("'%s' option specified multiple times",
362 shash_add_nocopy(local_options,
363 xasprintf("--%s", options[idx].name),
364 optarg ? xstrdup(optarg) : NULL);
371 ovs_print_version(0, 0);
375 timeout = strtoul(optarg, NULL, 10);
377 vtep_ctl_fatal("value %s on -t or --timeout is invalid",
383 TABLE_OPTION_HANDLERS(&table_style)
385 STREAM_SSL_OPTION_HANDLERS
387 case OPT_PEER_CA_CERT:
388 stream_ssl_set_peer_ca_cert_file(optarg);
404 for (i = n_global_long_options; options[i].name; i++) {
405 free(CONST_CAST(char *, options[i].name));
410 static struct vtep_ctl_command *
411 parse_commands(int argc, char *argv[], struct shash *local_options,
414 struct vtep_ctl_command *commands;
415 size_t n_commands, allocated_commands;
419 n_commands = allocated_commands = 0;
421 for (start = i = 0; i <= argc; i++) {
422 if (i == argc || !strcmp(argv[i], "--")) {
424 if (n_commands >= allocated_commands) {
425 struct vtep_ctl_command *c;
427 commands = x2nrealloc(commands, &allocated_commands,
429 for (c = commands; c < &commands[n_commands]; c++) {
430 shash_moved(&c->options);
433 parse_command(i - start, &argv[start], local_options,
434 &commands[n_commands++]);
435 } else if (!shash_is_empty(local_options)) {
436 vtep_ctl_fatal("missing command name (use --help for help)");
442 vtep_ctl_fatal("missing command name (use --help for help)");
444 *n_commandsp = n_commands;
449 parse_command(int argc, char *argv[], struct shash *local_options,
450 struct vtep_ctl_command *command)
452 const struct vtep_ctl_command_syntax *p;
453 struct shash_node *node;
457 shash_init(&command->options);
458 shash_swap(local_options, &command->options);
459 for (i = 0; i < argc; i++) {
460 const char *option = argv[i];
464 if (option[0] != '-') {
468 equals = strchr(option, '=');
470 key = xmemdup0(option, equals - option);
471 value = xstrdup(equals + 1);
473 key = xstrdup(option);
477 if (shash_find(&command->options, key)) {
478 vtep_ctl_fatal("'%s' option specified multiple times", argv[i]);
480 shash_add_nocopy(&command->options, key, value);
483 vtep_ctl_fatal("missing command name (use --help for help)");
486 p = find_command(argv[i]);
488 vtep_ctl_fatal("unknown command '%s'; use --help for help", argv[i]);
491 SHASH_FOR_EACH (node, &command->options) {
492 const char *s = strstr(p->options, node->name);
493 int end = s ? s[strlen(node->name)] : EOF;
495 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
496 vtep_ctl_fatal("'%s' command has no '%s' option",
497 argv[i], node->name);
499 if ((end == '=') != (node->data != NULL)) {
501 vtep_ctl_fatal("missing argument to '%s' option on '%s' "
502 "command", node->name, argv[i]);
504 vtep_ctl_fatal("'%s' option on '%s' does not accept an "
505 "argument", node->name, argv[i]);
510 n_arg = argc - i - 1;
511 if (n_arg < p->min_args) {
512 vtep_ctl_fatal("'%s' command requires at least %d arguments",
513 p->name, p->min_args);
514 } else if (n_arg > p->max_args) {
517 for (j = i + 1; j < argc; j++) {
518 if (argv[j][0] == '-') {
519 vtep_ctl_fatal("'%s' command takes at most %d arguments "
520 "(note that options must precede command "
521 "names and follow a \"--\" argument)",
522 p->name, p->max_args);
526 vtep_ctl_fatal("'%s' command takes at most %d arguments",
527 p->name, p->max_args);
531 command->argc = n_arg + 1;
532 command->argv = &argv[i];
535 /* Returns the "struct vtep_ctl_command_syntax" for a given command 'name', or a
536 * null pointer if there is none. */
537 static const struct vtep_ctl_command_syntax *
538 find_command(const char *name)
540 static struct shash commands = SHASH_INITIALIZER(&commands);
542 if (shash_is_empty(&commands)) {
543 const struct vtep_ctl_command_syntax *p;
545 for (p = all_commands; p->name; p++) {
546 shash_add_assert(&commands, p->name, p);
550 return shash_find_data(&commands, name);
554 vtep_ctl_fatal(const char *format, ...)
559 va_start(args, format);
560 message = xvasprintf(format, args);
563 vlog_set_levels(&VLM_vtep_ctl, VLF_CONSOLE, VLL_OFF);
564 VLOG_ERR("%s", message);
565 ovs_error(0, "%s", message);
566 vtep_ctl_exit(EXIT_FAILURE);
569 /* Frees the current transaction and the underlying IDL and then calls
572 * Freeing the transaction and the IDL is not strictly necessary, but it makes
573 * for a clean memory leak report from valgrind in the normal case. That makes
574 * it easier to notice real memory leaks. */
576 vtep_ctl_exit(int status)
579 ovsdb_idl_txn_abort(the_idl_txn);
580 ovsdb_idl_txn_destroy(the_idl_txn);
582 ovsdb_idl_destroy(the_idl);
590 %s: VTEP configuration utility\n\
591 usage: %s [OPTIONS] COMMAND [ARG...]\n\
594 get-manager print the managers\n\
595 del-manager delete the managers\n\
596 set-manager TARGET... set the list of managers to TARGET...\n\
598 Physical Switch commands:\n\
599 add-ps PS create a new physical switch named PS\n\
600 del-ps PS delete PS and all of its ports\n\
601 list-ps print the names of all the physical switches\n\
602 ps-exists PS exit 2 if PS does not exist\n\
605 list-ports PS print the names of all the ports on PS\n\
606 add-port PS PORT add network device PORT to PS\n\
607 del-port PS PORT delete PORT from PS\n\
609 Logical Switch commands:\n\
610 add-ls LS create a new logical switch named LS\n\
611 del-ls LS delete LS and all of its ports\n\
612 list-ls print the names of all the logical switches\n\
613 ls-exists LS exit 2 if LS does not exist\n\
614 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
615 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
616 list-bindings PS PORT list bindings for PORT on PS\n\
618 MAC binding commands:\n\
619 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
620 del-ucast-local LS MAC del ucast local entry from LS\n\
621 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
622 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
623 clear-local-macs LS clear local mac entries\n\
624 list-local-macs LS list local mac entries\n\
625 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
626 del-ucast-remote LS MAC del ucast remote entry from LS\n\
627 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
628 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
629 clear-remote-macs LS clear remote mac entries\n\
630 list-remote-macs LS list remote mac entries\n\
632 Database commands:\n\
633 list TBL [REC] list RECord (or all records) in TBL\n\
634 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
635 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
636 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
637 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
638 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
639 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
640 create TBL COL[:KEY]=VALUE create and initialize new record\n\
641 destroy TBL REC delete RECord from TBL\n\
642 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
643 Potentially unsafe database commands require --force option.\n\
646 --db=DATABASE connect to DATABASE\n\
648 -t, --timeout=SECS wait at most SECS seconds\n\
649 --dry-run do not commit changes to database\n\
650 --oneline print exactly one line of output per command\n",
651 program_name, program_name, default_db());
654 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
655 stream_usage("database", true, true, false);
658 -h, --help display this help message\n\
659 -V, --version display version information\n");
668 def = xasprintf("unix:%s/db.sock", ovs_rundir());
673 /* Returns true if it looks like this set of arguments might modify the
674 * database, otherwise false. (Not very smart, so it's prone to false
677 might_write_to_db(char **argv)
679 for (; *argv; argv++) {
680 const struct vtep_ctl_command_syntax *p = find_command(*argv);
681 if (p && p->mode == RW) {
688 struct vtep_ctl_context {
692 struct shash options;
694 /* Modifiable state. */
697 struct ovsdb_idl *idl;
698 struct ovsdb_idl_txn *txn;
699 struct ovsdb_symbol_table *symtab;
700 const struct vteprec_global *vtep_global;
703 /* A cache of the contents of the database.
705 * A command that needs to use any of this information must first
706 * call vtep_ctl_context_populate_cache(). A command that changes
707 * anything that could invalidate the cache must either call
708 * vtep_ctl_context_invalidate_cache() or manually update the cache
709 * to maintain its correctness. */
711 struct shash pswitches; /* Maps from physical switch name to
712 * struct vtep_ctl_pswitch. */
713 struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
715 struct shash lswitches; /* Maps from logical switch name to
716 * struct vtep_ctl_lswitch. */
717 struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
718 * struct vteprec_physical_locator. */
720 /* A command may set this member to true if some prerequisite is not met
721 * and the caller should wait for something to change and then retry. */
725 struct vtep_ctl_pswitch {
726 const struct vteprec_physical_switch *ps_cfg;
728 struct list ports; /* Contains "struct vteprec_physical_port"s. */
731 struct vtep_ctl_port {
732 struct list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
733 const struct vteprec_physical_port *port_cfg;
734 struct vtep_ctl_pswitch *ps;
735 struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
738 struct vtep_ctl_lswitch {
739 const struct vteprec_logical_switch *ls_cfg;
741 struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
742 struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
743 struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
744 struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
747 struct vtep_ctl_mcast_mac {
748 const struct vteprec_mcast_macs_local *local_cfg;
749 const struct vteprec_mcast_macs_remote *remote_cfg;
751 const struct vteprec_physical_locator_set *ploc_set_cfg;
752 struct list locators; /* Contains 'vtep_ctl_ploc's. */
755 struct vtep_ctl_ploc {
756 struct list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
758 const struct vteprec_physical_locator *ploc_cfg;
762 verify_ports(struct vtep_ctl_context *ctx)
764 if (!ctx->verified_ports) {
765 const struct vteprec_physical_switch *ps;
767 vteprec_global_verify_switches(ctx->vtep_global);
768 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, ctx->idl) {
769 vteprec_physical_switch_verify_ports(ps);
772 ctx->verified_ports = true;
776 static struct vtep_ctl_port *
777 add_port_to_cache(struct vtep_ctl_context *ctx,
778 struct vtep_ctl_pswitch *ps,
779 struct vteprec_physical_port *port_cfg)
781 char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
782 struct vtep_ctl_port *port;
784 port = xmalloc(sizeof *port);
785 list_push_back(&ps->ports, &port->ports_node);
786 port->port_cfg = port_cfg;
788 shash_add(&ctx->ports, cache_name, port);
790 shash_init(&port->bindings);
796 del_cached_port(struct vtep_ctl_context *ctx, struct vtep_ctl_port *port)
798 char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
800 list_remove(&port->ports_node);
801 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
802 vteprec_physical_port_delete(port->port_cfg);
807 static struct vtep_ctl_pswitch *
808 add_pswitch_to_cache(struct vtep_ctl_context *ctx,
809 struct vteprec_physical_switch *ps_cfg)
811 struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
813 ps->name = xstrdup(ps_cfg->name);
814 list_init(&ps->ports);
815 shash_add(&ctx->pswitches, ps->name, ps);
820 vtep_delete_pswitch(const struct vteprec_global *vtep_global,
821 const struct vteprec_physical_switch *ps)
823 struct vteprec_physical_switch **pswitches;
826 pswitches = xmalloc(sizeof *vtep_global->switches
827 * vtep_global->n_switches);
828 for (i = n = 0; i < vtep_global->n_switches; i++) {
829 if (vtep_global->switches[i] != ps) {
830 pswitches[n++] = vtep_global->switches[i];
833 vteprec_global_set_switches(vtep_global, pswitches, n);
838 del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
840 ovs_assert(list_is_empty(&ps->ports));
842 vteprec_physical_switch_delete(ps->ps_cfg);
843 vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
845 shash_find_and_delete(&ctx->pswitches, ps->name);
850 static struct vtep_ctl_lswitch *
851 add_lswitch_to_cache(struct vtep_ctl_context *ctx,
852 const struct vteprec_logical_switch *ls_cfg)
854 struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
856 ls->name = xstrdup(ls_cfg->name);
857 shash_add(&ctx->lswitches, ls->name, ls);
858 shash_init(&ls->ucast_local);
859 shash_init(&ls->ucast_remote);
860 shash_init(&ls->mcast_local);
861 shash_init(&ls->mcast_remote);
866 del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
869 vteprec_logical_switch_delete(ls->ls_cfg);
871 shash_find_and_delete(&ctx->lswitches, ls->name);
877 commit_ls_bindings(struct vtep_ctl_port *port)
879 struct vteprec_logical_switch **binding_values;
880 int64_t *binding_keys;
882 struct shash_node *node;
885 n_bindings = shash_count(&port->bindings);
886 binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
887 binding_values = xmalloc(n_bindings * sizeof *binding_values);
890 SHASH_FOR_EACH(node, &port->bindings) {
891 struct vtep_ctl_lswitch *ls_entry = node->data;
893 binding_keys[i] = strtoll(node->name, NULL, 0);
894 binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
898 vteprec_physical_port_set_vlan_bindings(port->port_cfg,
899 binding_keys, binding_values,
901 free(binding_values);
906 add_ls_binding_to_cache(struct vtep_ctl_port *port,
908 struct vtep_ctl_lswitch *ls)
910 if (shash_find(&port->bindings, vlan)) {
911 vtep_ctl_fatal("multiple bindings for vlan %s", vlan);
914 shash_add(&port->bindings, vlan, ls);
918 del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
920 if (!shash_find(&port->bindings, vlan)) {
921 vtep_ctl_fatal("no binding for vlan %s", vlan);
924 shash_find_and_delete(&port->bindings, vlan);
927 static struct vteprec_physical_locator *
928 find_ploc(struct vtep_ctl_context *ctx, const char *encap,
931 struct vteprec_physical_locator *ploc;
932 char *name = xasprintf("%s+%s", encap, dst_ip);
934 ovs_assert(ctx->cache_valid);
936 ploc = shash_find_data(&ctx->plocs, name);
943 add_ploc_to_cache(struct vtep_ctl_context *ctx,
944 struct vteprec_physical_locator *ploc)
946 char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
947 struct vteprec_physical_locator *orig_ploc;
949 orig_ploc = find_ploc(ctx, ploc->encapsulation_type, ploc->dst_ip);
951 shash_add(&ctx->plocs, name, ploc);
958 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
959 struct vteprec_physical_locator *ploc_cfg)
961 struct vtep_ctl_ploc *ploc;
963 ploc = xmalloc(sizeof *ploc);
964 ploc->ploc_cfg = ploc_cfg;
965 list_push_back(&mcast_mac->locators, &ploc->locators_node);
969 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
970 struct vteprec_physical_locator *ploc_cfg)
972 struct vtep_ctl_ploc *ploc;
974 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
975 if (ploc->ploc_cfg == ploc_cfg) {
976 list_remove(&ploc->locators_node);
983 static struct vtep_ctl_mcast_mac *
984 add_mcast_mac_to_cache(struct vtep_ctl_context *ctx,
985 struct vtep_ctl_lswitch *ls, const char *mac,
986 struct vteprec_physical_locator_set *ploc_set_cfg,
989 struct vtep_ctl_mcast_mac *mcast_mac;
990 struct shash *mcast_shash;
993 mcast_mac = xmalloc(sizeof *mcast_mac);
994 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
996 mcast_mac->ploc_set_cfg = ploc_set_cfg;
997 list_init(&mcast_mac->locators);
998 shash_add(mcast_shash, mac, mcast_mac);
1000 for (i = 0; i < ploc_set_cfg->n_locators; i++) {
1001 struct vteprec_physical_locator *ploc_cfg;
1003 ploc_cfg = ploc_set_cfg->locators[i];
1004 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1005 add_ploc_to_cache(ctx, ploc_cfg);
1012 vtep_ctl_context_invalidate_cache(struct vtep_ctl_context *ctx)
1014 struct shash_node *node;
1016 if (!ctx->cache_valid) {
1019 ctx->cache_valid = false;
1021 SHASH_FOR_EACH (node, &ctx->pswitches) {
1022 struct vtep_ctl_pswitch *ps = node->data;
1026 shash_destroy(&ctx->pswitches);
1028 SHASH_FOR_EACH (node, &ctx->ports) {
1029 struct vtep_ctl_port *port = node->data;
1030 shash_destroy(&port->bindings);
1032 shash_destroy_free_data(&ctx->ports);
1034 SHASH_FOR_EACH (node, &ctx->lswitches) {
1035 struct vtep_ctl_lswitch *ls = node->data;
1036 struct shash_node *node2, *next_node2;
1038 shash_destroy(&ls->ucast_local);
1039 shash_destroy(&ls->ucast_remote);
1041 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
1042 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
1043 struct vtep_ctl_ploc *ploc, *next_ploc;
1045 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
1046 &mcast_mac->locators) {
1051 shash_destroy(&ls->mcast_local);
1053 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
1054 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
1055 struct vtep_ctl_ploc *ploc, *next_ploc;
1057 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
1058 &mcast_mac->locators) {
1063 shash_destroy(&ls->mcast_remote);
1068 shash_destroy(&ctx->lswitches);
1069 shash_destroy(&ctx->plocs);
1073 pre_get_info(struct vtep_ctl_context *ctx)
1075 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
1077 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
1078 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
1080 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
1081 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
1083 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
1085 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
1086 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
1087 ovsdb_idl_add_column(ctx->idl,
1088 &vteprec_ucast_macs_local_col_logical_switch);
1090 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
1091 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
1092 ovsdb_idl_add_column(ctx->idl,
1093 &vteprec_ucast_macs_remote_col_logical_switch);
1095 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
1096 ovsdb_idl_add_column(ctx->idl,
1097 &vteprec_mcast_macs_local_col_locator_set);
1098 ovsdb_idl_add_column(ctx->idl,
1099 &vteprec_mcast_macs_local_col_logical_switch);
1101 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
1102 ovsdb_idl_add_column(ctx->idl,
1103 &vteprec_mcast_macs_remote_col_locator_set);
1104 ovsdb_idl_add_column(ctx->idl,
1105 &vteprec_mcast_macs_remote_col_logical_switch);
1107 ovsdb_idl_add_column(ctx->idl,
1108 &vteprec_physical_locator_set_col_locators);
1110 ovsdb_idl_add_column(ctx->idl,
1111 &vteprec_physical_locator_col_dst_ip);
1112 ovsdb_idl_add_column(ctx->idl,
1113 &vteprec_physical_locator_col_encapsulation_type);
1117 vtep_ctl_context_populate_cache(struct vtep_ctl_context *ctx)
1119 const struct vteprec_global *vtep_global = ctx->vtep_global;
1120 const struct vteprec_logical_switch *ls_cfg;
1121 const struct vteprec_ucast_macs_local *ucast_local_cfg;
1122 const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
1123 const struct vteprec_mcast_macs_local *mcast_local_cfg;
1124 const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
1125 struct sset pswitches, ports, lswitches;
1128 if (ctx->cache_valid) {
1129 /* Cache is already populated. */
1132 ctx->cache_valid = true;
1133 shash_init(&ctx->pswitches);
1134 shash_init(&ctx->ports);
1135 shash_init(&ctx->lswitches);
1136 shash_init(&ctx->plocs);
1138 sset_init(&pswitches);
1140 for (i = 0; i < vtep_global->n_switches; i++) {
1141 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1142 struct vtep_ctl_pswitch *ps;
1145 if (!sset_add(&pswitches, ps_cfg->name)) {
1146 VLOG_WARN("%s: database contains duplicate physical switch name",
1150 ps = add_pswitch_to_cache(ctx, ps_cfg);
1155 for (j = 0; j < ps_cfg->n_ports; j++) {
1156 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1158 if (!sset_add(&ports, port_cfg->name)) {
1159 /* Duplicate port name. (We will warn about that later.) */
1164 sset_destroy(&pswitches);
1165 sset_destroy(&ports);
1167 sset_init(&lswitches);
1168 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
1169 if (!sset_add(&lswitches, ls_cfg->name)) {
1170 VLOG_WARN("%s: database contains duplicate logical switch name",
1174 add_lswitch_to_cache(ctx, ls_cfg);
1176 sset_destroy(&lswitches);
1178 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
1179 struct vtep_ctl_lswitch *ls;
1181 if (!ucast_local_cfg->logical_switch) {
1184 ls = find_lswitch(ctx, ucast_local_cfg->logical_switch->name, false);
1189 if (ucast_local_cfg->locator) {
1190 add_ploc_to_cache(ctx, ucast_local_cfg->locator);
1193 shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
1196 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
1197 struct vtep_ctl_lswitch *ls;
1199 if (!ucast_remote_cfg->logical_switch) {
1202 ls = find_lswitch(ctx, ucast_remote_cfg->logical_switch->name, false);
1207 if (ucast_remote_cfg->locator) {
1208 add_ploc_to_cache(ctx, ucast_remote_cfg->locator);
1211 shash_add(&ls->ucast_remote, ucast_remote_cfg->MAC, ucast_remote_cfg);
1214 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg, ctx->idl) {
1215 struct vtep_ctl_mcast_mac *mcast_mac;
1216 struct vtep_ctl_lswitch *ls;
1218 if (!mcast_local_cfg->logical_switch) {
1221 ls = find_lswitch(ctx, mcast_local_cfg->logical_switch->name, false);
1226 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mcast_local_cfg->MAC,
1227 mcast_local_cfg->locator_set,
1229 mcast_mac->local_cfg = mcast_local_cfg;
1232 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg, ctx->idl) {
1233 struct vtep_ctl_mcast_mac *mcast_mac;
1234 struct vtep_ctl_lswitch *ls;
1236 if (!mcast_remote_cfg->logical_switch) {
1239 ls = find_lswitch(ctx, mcast_remote_cfg->logical_switch->name, false);
1244 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mcast_remote_cfg->MAC,
1245 mcast_remote_cfg->locator_set,
1247 mcast_mac->remote_cfg = mcast_remote_cfg;
1250 sset_init(&pswitches);
1251 for (i = 0; i < vtep_global->n_switches; i++) {
1252 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1253 struct vtep_ctl_pswitch *ps;
1256 if (!sset_add(&pswitches, ps_cfg->name)) {
1259 ps = shash_find_data(&ctx->pswitches, ps_cfg->name);
1260 for (j = 0; j < ps_cfg->n_ports; j++) {
1261 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1262 struct vtep_ctl_port *port;
1265 port = shash_find_data(&ctx->ports, port_cfg->name);
1267 if (port_cfg == port->port_cfg) {
1268 VLOG_WARN("%s: port is in multiple physical switches "
1270 port_cfg->name, ps->name, port->ps->name);
1272 /* Log as an error because this violates the database's
1273 * uniqueness constraints, so the database server shouldn't
1274 * have allowed it. */
1275 VLOG_ERR("%s: database contains duplicate port name",
1281 port = add_port_to_cache(ctx, ps, port_cfg);
1283 for (k = 0; k < port_cfg->n_vlan_bindings; k++) {
1284 struct vteprec_logical_switch *ls_cfg;
1285 struct vtep_ctl_lswitch *ls;
1288 vlan = xasprintf("%"PRId64, port_cfg->key_vlan_bindings[k]);
1289 if (shash_find(&port->bindings, vlan)) {
1290 vtep_ctl_fatal("multiple bindings for vlan %s", vlan);
1293 ls_cfg = port_cfg->value_vlan_bindings[k];
1294 ls = find_lswitch(ctx, ls_cfg->name, true);
1296 shash_add_nocopy(&port->bindings, vlan, ls);
1300 sset_destroy(&pswitches);
1303 static struct vtep_ctl_pswitch *
1304 find_pswitch(struct vtep_ctl_context *ctx, const char *name, bool must_exist)
1306 struct vtep_ctl_pswitch *ps;
1308 ovs_assert(ctx->cache_valid);
1310 ps = shash_find_data(&ctx->pswitches, name);
1311 if (must_exist && !ps) {
1312 vtep_ctl_fatal("no physical switch named %s", name);
1314 vteprec_global_verify_switches(ctx->vtep_global);
1318 static struct vtep_ctl_port *
1319 find_port(struct vtep_ctl_context *ctx, const char *ps_name,
1320 const char *port_name, bool must_exist)
1322 char *cache_name = xasprintf("%s+%s", ps_name, port_name);
1323 struct vtep_ctl_port *port;
1325 ovs_assert(ctx->cache_valid);
1327 port = shash_find_data(&ctx->ports, cache_name);
1328 if (port && !strcmp(port_name, port->ps->name)) {
1332 if (must_exist && !port) {
1333 vtep_ctl_fatal("no port named %s", port_name);
1340 pswitch_insert_port(const struct vteprec_physical_switch *ps,
1341 struct vteprec_physical_port *port)
1343 struct vteprec_physical_port **ports;
1346 ports = xmalloc(sizeof *ps->ports * (ps->n_ports + 1));
1347 for (i = 0; i < ps->n_ports; i++) {
1348 ports[i] = ps->ports[i];
1350 ports[ps->n_ports] = port;
1351 vteprec_physical_switch_set_ports(ps, ports, ps->n_ports + 1);
1356 pswitch_delete_port(const struct vteprec_physical_switch *ps,
1357 const struct vteprec_physical_port *port)
1359 struct vteprec_physical_port **ports;
1362 ports = xmalloc(sizeof *ps->ports * ps->n_ports);
1363 for (i = n = 0; i < ps->n_ports; i++) {
1364 if (ps->ports[i] != port) {
1365 ports[n++] = ps->ports[i];
1368 vteprec_physical_switch_set_ports(ps, ports, n);
1373 vtep_insert_pswitch(const struct vteprec_global *vtep_global,
1374 struct vteprec_physical_switch *ps)
1376 struct vteprec_physical_switch **pswitches;
1379 pswitches = xmalloc(sizeof *vtep_global->switches
1380 * (vtep_global->n_switches + 1));
1381 for (i = 0; i < vtep_global->n_switches; i++) {
1382 pswitches[i] = vtep_global->switches[i];
1384 pswitches[vtep_global->n_switches] = ps;
1385 vteprec_global_set_switches(vtep_global, pswitches,
1386 vtep_global->n_switches + 1);
1391 cmd_add_ps(struct vtep_ctl_context *ctx)
1393 const char *ps_name = ctx->argv[1];
1394 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1395 struct vteprec_physical_switch *ps;
1397 vtep_ctl_context_populate_cache(ctx);
1398 if (find_pswitch(ctx, ps_name, false)) {
1400 vtep_ctl_fatal("cannot create physical switch %s because it "
1401 "already exists", ps_name);
1406 ps = vteprec_physical_switch_insert(ctx->txn);
1407 vteprec_physical_switch_set_name(ps, ps_name);
1409 vtep_insert_pswitch(ctx->vtep_global, ps);
1411 vtep_ctl_context_invalidate_cache(ctx);
1415 del_port(struct vtep_ctl_context *ctx, struct vtep_ctl_port *port)
1417 pswitch_delete_port(port->ps->ps_cfg, port->port_cfg);
1418 del_cached_port(ctx, port);
1422 del_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
1424 struct vtep_ctl_port *port, *next_port;
1426 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &ps->ports) {
1427 del_port(ctx, port);
1430 del_cached_pswitch(ctx, ps);
1434 cmd_del_ps(struct vtep_ctl_context *ctx)
1436 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1437 struct vtep_ctl_pswitch *ps;
1439 vtep_ctl_context_populate_cache(ctx);
1440 ps = find_pswitch(ctx, ctx->argv[1], must_exist);
1442 del_pswitch(ctx, ps);
1447 output_sorted(struct svec *svec, struct ds *output)
1453 SVEC_FOR_EACH (i, name, svec) {
1454 ds_put_format(output, "%s\n", name);
1459 cmd_list_ps(struct vtep_ctl_context *ctx)
1461 struct shash_node *node;
1462 struct svec pswitches;
1464 vtep_ctl_context_populate_cache(ctx);
1466 svec_init(&pswitches);
1467 SHASH_FOR_EACH (node, &ctx->pswitches) {
1468 struct vtep_ctl_pswitch *ps = node->data;
1470 svec_add(&pswitches, ps->name);
1472 output_sorted(&pswitches, &ctx->output);
1473 svec_destroy(&pswitches);
1477 cmd_ps_exists(struct vtep_ctl_context *ctx)
1479 vtep_ctl_context_populate_cache(ctx);
1480 if (!find_pswitch(ctx, ctx->argv[1], false)) {
1486 cmd_list_ports(struct vtep_ctl_context *ctx)
1488 struct vtep_ctl_pswitch *ps;
1489 struct vtep_ctl_port *port;
1492 vtep_ctl_context_populate_cache(ctx);
1493 ps = find_pswitch(ctx, ctx->argv[1], true);
1494 vteprec_physical_switch_verify_ports(ps->ps_cfg);
1497 LIST_FOR_EACH (port, ports_node, &ps->ports) {
1498 if (strcmp(port->port_cfg->name, ps->name)) {
1499 svec_add(&ports, port->port_cfg->name);
1502 output_sorted(&ports, &ctx->output);
1503 svec_destroy(&ports);
1507 add_port(struct vtep_ctl_context *ctx, const char *ps_name,
1508 const char *port_name, bool may_exist)
1510 struct vtep_ctl_port *vtep_ctl_port;
1511 struct vtep_ctl_pswitch *ps;
1512 struct vteprec_physical_port *port;
1514 vtep_ctl_context_populate_cache(ctx);
1516 vtep_ctl_port = find_port(ctx, ps_name, port_name, false);
1517 if (vtep_ctl_port) {
1519 vtep_ctl_fatal("cannot create a port named %s on %s because a "
1520 "port with that name already exists",
1521 port_name, ps_name);
1526 ps = find_pswitch(ctx, ps_name, true);
1528 port = vteprec_physical_port_insert(ctx->txn);
1529 vteprec_physical_port_set_name(port, port_name);
1531 pswitch_insert_port(ps->ps_cfg, port);
1533 add_port_to_cache(ctx, ps, port);
1537 cmd_add_port(struct vtep_ctl_context *ctx)
1539 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1540 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist);
1544 cmd_del_port(struct vtep_ctl_context *ctx)
1546 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1547 struct vtep_ctl_port *port;
1549 vtep_ctl_context_populate_cache(ctx);
1551 port = find_port(ctx, ctx->argv[1], ctx->argv[2], must_exist);
1553 if (ctx->argc == 3) {
1554 struct vtep_ctl_pswitch *ps;
1556 ps = find_pswitch(ctx, ctx->argv[1], true);
1557 if (port->ps != ps) {
1558 vtep_ctl_fatal("physical switch %s does not have a port %s",
1559 ctx->argv[1], ctx->argv[2]);
1563 del_port(ctx, port);
1567 static struct vtep_ctl_lswitch *
1568 find_lswitch(struct vtep_ctl_context *ctx, const char *name, bool must_exist)
1570 struct vtep_ctl_lswitch *ls;
1572 ovs_assert(ctx->cache_valid);
1574 ls = shash_find_data(&ctx->lswitches, name);
1575 if (must_exist && !ls) {
1576 vtep_ctl_fatal("no logical switch named %s", name);
1582 cmd_add_ls(struct vtep_ctl_context *ctx)
1584 const char *ls_name = ctx->argv[1];
1585 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1586 struct vteprec_logical_switch *ls;
1588 vtep_ctl_context_populate_cache(ctx);
1589 if (find_lswitch(ctx, ls_name, false)) {
1591 vtep_ctl_fatal("cannot create logical switch %s because it "
1592 "already exists", ls_name);
1597 ls = vteprec_logical_switch_insert(ctx->txn);
1598 vteprec_logical_switch_set_name(ls, ls_name);
1600 vtep_ctl_context_invalidate_cache(ctx);
1604 del_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
1606 del_cached_lswitch(ctx, ls);
1610 cmd_del_ls(struct vtep_ctl_context *ctx)
1612 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1613 struct vtep_ctl_lswitch *ls;
1615 vtep_ctl_context_populate_cache(ctx);
1616 ls = find_lswitch(ctx, ctx->argv[1], must_exist);
1618 del_lswitch(ctx, ls);
1623 cmd_list_ls(struct vtep_ctl_context *ctx)
1625 struct shash_node *node;
1626 struct svec lswitches;
1628 vtep_ctl_context_populate_cache(ctx);
1630 svec_init(&lswitches);
1631 SHASH_FOR_EACH (node, &ctx->lswitches) {
1632 struct vtep_ctl_lswitch *ls = node->data;
1634 svec_add(&lswitches, ls->name);
1636 output_sorted(&lswitches, &ctx->output);
1637 svec_destroy(&lswitches);
1641 cmd_ls_exists(struct vtep_ctl_context *ctx)
1643 vtep_ctl_context_populate_cache(ctx);
1644 if (!find_lswitch(ctx, ctx->argv[1], false)) {
1650 cmd_list_bindings(struct vtep_ctl_context *ctx)
1652 const struct shash_node *node;
1653 struct vtep_ctl_port *port;
1654 struct svec bindings;
1656 vtep_ctl_context_populate_cache(ctx);
1657 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1659 svec_init(&bindings);
1660 SHASH_FOR_EACH (node, &port->bindings) {
1661 struct vtep_ctl_lswitch *lswitch = node->data;
1664 binding = xasprintf("%04lld %s", strtoll(node->name, NULL, 0),
1666 svec_add_nocopy(&bindings, binding);
1668 output_sorted(&bindings, &ctx->output);
1669 svec_destroy(&bindings);
1673 cmd_bind_ls(struct vtep_ctl_context *ctx)
1675 struct vtep_ctl_lswitch *ls;
1676 struct vtep_ctl_port *port;
1679 vtep_ctl_context_populate_cache(ctx);
1681 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1682 vlan = ctx->argv[3];
1683 ls = find_lswitch(ctx, ctx->argv[4], true);
1685 add_ls_binding_to_cache(port, vlan, ls);
1686 commit_ls_bindings(port);
1688 vtep_ctl_context_invalidate_cache(ctx);
1692 cmd_unbind_ls(struct vtep_ctl_context *ctx)
1694 struct vtep_ctl_port *port;
1697 vtep_ctl_context_populate_cache(ctx);
1699 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1700 vlan = ctx->argv[3];
1702 del_cached_ls_binding(port, vlan);
1703 commit_ls_bindings(port);
1705 vtep_ctl_context_invalidate_cache(ctx);
1709 add_ucast_entry(struct vtep_ctl_context *ctx, bool local)
1711 struct vtep_ctl_lswitch *ls;
1715 struct vteprec_physical_locator *ploc_cfg;
1717 vtep_ctl_context_populate_cache(ctx);
1719 ls = find_lswitch(ctx, ctx->argv[1], true);
1722 if (ctx->argc == 4) {
1723 encap = "vxlan_over_ipv4";
1724 dst_ip = ctx->argv[3];
1726 encap = ctx->argv[3];
1727 dst_ip = ctx->argv[4];
1730 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1732 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1733 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1734 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1736 add_ploc_to_cache(ctx, ploc_cfg);
1740 struct vteprec_ucast_macs_local *ucast_cfg;
1742 ucast_cfg = shash_find_data(&ls->ucast_local, mac);
1744 ucast_cfg = vteprec_ucast_macs_local_insert(ctx->txn);
1745 vteprec_ucast_macs_local_set_MAC(ucast_cfg, mac);
1746 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg, ls->ls_cfg);
1747 shash_add(&ls->ucast_local, mac, ucast_cfg);
1749 vteprec_ucast_macs_local_set_locator(ucast_cfg, ploc_cfg);
1751 struct vteprec_ucast_macs_remote *ucast_cfg;
1753 ucast_cfg = shash_find_data(&ls->ucast_remote, mac);
1755 ucast_cfg = vteprec_ucast_macs_remote_insert(ctx->txn);
1756 vteprec_ucast_macs_remote_set_MAC(ucast_cfg, mac);
1757 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg, ls->ls_cfg);
1758 shash_add(&ls->ucast_remote, mac, ucast_cfg);
1760 vteprec_ucast_macs_remote_set_locator(ucast_cfg, ploc_cfg);
1763 vtep_ctl_context_invalidate_cache(ctx);
1767 cmd_add_ucast_local(struct vtep_ctl_context *ctx)
1769 add_ucast_entry(ctx, true);
1773 cmd_add_ucast_remote(struct vtep_ctl_context *ctx)
1775 add_ucast_entry(ctx, false);
1779 del_ucast_entry(struct vtep_ctl_context *ctx, bool local)
1781 struct vtep_ctl_lswitch *ls;
1782 struct shash *ucast_shash;
1783 struct shash_node *node;
1785 vtep_ctl_context_populate_cache(ctx);
1787 ls = find_lswitch(ctx, ctx->argv[1], true);
1788 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1790 node = shash_find(ucast_shash, ctx->argv[2]);
1796 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1797 vteprec_ucast_macs_local_delete(ucast_cfg);
1799 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1800 vteprec_ucast_macs_remote_delete(ucast_cfg);
1802 shash_delete(ucast_shash, node);
1804 vtep_ctl_context_invalidate_cache(ctx);
1808 cmd_del_ucast_local(struct vtep_ctl_context *ctx)
1810 del_ucast_entry(ctx, true);
1814 cmd_del_ucast_remote(struct vtep_ctl_context *ctx)
1816 del_ucast_entry(ctx, false);
1820 commit_mcast_entries(struct vtep_ctl_mcast_mac *mcast_mac)
1822 struct vtep_ctl_ploc *ploc;
1823 struct vteprec_physical_locator **locators = NULL;
1827 n_locators = list_size(&mcast_mac->locators);
1828 ovs_assert(n_locators);
1830 locators = xmalloc(n_locators * sizeof *locators);
1833 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
1834 locators[i] = (struct vteprec_physical_locator *)ploc->ploc_cfg;
1838 vteprec_physical_locator_set_set_locators(mcast_mac->ploc_set_cfg,
1846 add_mcast_entry(struct vtep_ctl_context *ctx,
1847 struct vtep_ctl_lswitch *ls, const char *mac,
1848 const char *encap, const char *dst_ip, bool local)
1850 struct shash *mcast_shash;
1851 struct vtep_ctl_mcast_mac *mcast_mac;
1852 struct vteprec_physical_locator *ploc_cfg;
1853 struct vteprec_physical_locator_set *ploc_set_cfg;
1855 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1857 /* Physical locator sets are immutable, so allocate a new one. */
1858 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1860 mcast_mac = shash_find_data(mcast_shash, mac);
1862 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mac, ploc_set_cfg, local);
1865 mcast_mac->local_cfg = vteprec_mcast_macs_local_insert(ctx->txn);
1866 vteprec_mcast_macs_local_set_MAC(mcast_mac->local_cfg, mac);
1867 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1869 vteprec_mcast_macs_local_set_logical_switch(mcast_mac->local_cfg,
1871 mcast_mac->remote_cfg = NULL;
1873 mcast_mac->remote_cfg = vteprec_mcast_macs_remote_insert(ctx->txn);
1874 vteprec_mcast_macs_remote_set_MAC(mcast_mac->remote_cfg, mac);
1875 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1877 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac->remote_cfg,
1879 mcast_mac->local_cfg = NULL;
1882 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1884 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1887 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1892 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1894 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1895 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1896 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1898 add_ploc_to_cache(ctx, ploc_cfg);
1901 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1902 commit_mcast_entries(mcast_mac);
1906 del_mcast_entry(struct vtep_ctl_context *ctx,
1907 struct vtep_ctl_lswitch *ls, const char *mac,
1908 const char *encap, const char *dst_ip, bool local)
1910 struct vtep_ctl_mcast_mac *mcast_mac;
1911 struct shash *mcast_shash;
1912 struct vteprec_physical_locator *ploc_cfg;
1913 struct vteprec_physical_locator_set *ploc_set_cfg;
1915 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1917 mcast_mac = shash_find_data(mcast_shash, mac);
1922 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1924 /* Couldn't find the physical locator, so just ignore. */
1928 /* Physical locator sets are immutable, so allocate a new one. */
1929 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1930 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1932 del_ploc_from_mcast_mac(mcast_mac, ploc_cfg);
1933 if (list_is_empty(&mcast_mac->locators)) {
1934 struct shash_node *node = shash_find(mcast_shash, mac);
1936 vteprec_physical_locator_set_delete(ploc_set_cfg);
1939 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1941 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1945 shash_delete(mcast_shash, node);
1948 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1951 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1954 commit_mcast_entries(mcast_mac);
1959 add_del_mcast_entry(struct vtep_ctl_context *ctx, bool add, bool local)
1961 struct vtep_ctl_lswitch *ls;
1966 vtep_ctl_context_populate_cache(ctx);
1968 ls = find_lswitch(ctx, ctx->argv[1], true);
1971 if (ctx->argc == 4) {
1972 encap = "vxlan_over_ipv4";
1973 dst_ip = ctx->argv[3];
1975 encap = ctx->argv[3];
1976 dst_ip = ctx->argv[4];
1980 add_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1982 del_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1985 vtep_ctl_context_invalidate_cache(ctx);
1989 cmd_add_mcast_local(struct vtep_ctl_context *ctx)
1991 add_del_mcast_entry(ctx, true, true);
1995 cmd_add_mcast_remote(struct vtep_ctl_context *ctx)
1997 add_del_mcast_entry(ctx, true, false);
2001 cmd_del_mcast_local(struct vtep_ctl_context *ctx)
2003 add_del_mcast_entry(ctx, false, true);
2007 cmd_del_mcast_remote(struct vtep_ctl_context *ctx)
2009 add_del_mcast_entry(ctx, false, false);
2013 clear_macs(struct vtep_ctl_context *ctx, bool local)
2015 struct vtep_ctl_lswitch *ls;
2016 const struct shash_node *node;
2017 struct shash *ucast_shash;
2018 struct shash *mcast_shash;
2020 vtep_ctl_context_populate_cache(ctx);
2021 ls = find_lswitch(ctx, ctx->argv[1], true);
2023 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2024 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2026 SHASH_FOR_EACH (node, ucast_shash) {
2028 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
2029 vteprec_ucast_macs_local_delete(ucast_cfg);
2031 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
2032 vteprec_ucast_macs_remote_delete(ucast_cfg);
2036 SHASH_FOR_EACH (node, mcast_shash) {
2037 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2039 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
2041 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
2045 vtep_ctl_context_invalidate_cache(ctx);
2049 cmd_clear_local_macs(struct vtep_ctl_context *ctx)
2051 clear_macs(ctx, true);
2055 cmd_clear_remote_macs(struct vtep_ctl_context *ctx)
2057 clear_macs(ctx, false);
2061 list_macs(struct vtep_ctl_context *ctx, bool local)
2063 struct vtep_ctl_lswitch *ls;
2064 const struct shash_node *node;
2065 struct shash *ucast_shash;
2066 struct svec ucast_macs;
2067 struct shash *mcast_shash;
2068 struct svec mcast_macs;
2070 vtep_ctl_context_populate_cache(ctx);
2071 ls = find_lswitch(ctx, ctx->argv[1], true);
2073 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2074 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2076 svec_init(&ucast_macs);
2077 SHASH_FOR_EACH (node, ucast_shash) {
2078 struct vteprec_ucast_macs_local *ucast_local = node->data;
2079 struct vteprec_ucast_macs_remote *ucast_remote = node->data;
2080 struct vteprec_physical_locator *ploc_cfg;
2083 ploc_cfg = local ? ucast_local->locator : ucast_remote->locator;
2085 entry = xasprintf(" %s -> %s/%s", node->name,
2086 ploc_cfg->encapsulation_type, ploc_cfg->dst_ip);
2087 svec_add_nocopy(&ucast_macs, entry);
2089 ds_put_format(&ctx->output, "ucast-mac-%s\n", local ? "local" : "remote");
2090 output_sorted(&ucast_macs, &ctx->output);
2091 ds_put_char(&ctx->output, '\n');
2092 svec_destroy(&ucast_macs);
2094 svec_init(&mcast_macs);
2095 SHASH_FOR_EACH (node, mcast_shash) {
2096 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2097 struct vtep_ctl_ploc *ploc;
2100 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
2101 entry = xasprintf(" %s -> %s/%s", node->name,
2102 ploc->ploc_cfg->encapsulation_type,
2103 ploc->ploc_cfg->dst_ip);
2104 svec_add_nocopy(&mcast_macs, entry);
2107 ds_put_format(&ctx->output, "mcast-mac-%s\n", local ? "local" : "remote");
2108 output_sorted(&mcast_macs, &ctx->output);
2109 ds_put_char(&ctx->output, '\n');
2110 svec_destroy(&mcast_macs);
2114 cmd_list_local_macs(struct vtep_ctl_context *ctx)
2116 list_macs(ctx, true);
2120 cmd_list_remote_macs(struct vtep_ctl_context *ctx)
2122 list_macs(ctx, false);
2126 verify_managers(const struct vteprec_global *vtep_global)
2130 vteprec_global_verify_managers(vtep_global);
2132 for (i = 0; i < vtep_global->n_managers; ++i) {
2133 const struct vteprec_manager *mgr = vtep_global->managers[i];
2135 vteprec_manager_verify_target(mgr);
2140 pre_manager(struct vtep_ctl_context *ctx)
2142 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_managers);
2143 ovsdb_idl_add_column(ctx->idl, &vteprec_manager_col_target);
2147 cmd_get_manager(struct vtep_ctl_context *ctx)
2149 const struct vteprec_global *vtep_global = ctx->vtep_global;
2150 struct svec targets;
2153 verify_managers(vtep_global);
2155 /* Print the targets in sorted order for reproducibility. */
2156 svec_init(&targets);
2158 for (i = 0; i < vtep_global->n_managers; i++) {
2159 svec_add(&targets, vtep_global->managers[i]->target);
2162 svec_sort_unique(&targets);
2163 for (i = 0; i < targets.n; i++) {
2164 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2166 svec_destroy(&targets);
2170 delete_managers(const struct vtep_ctl_context *ctx)
2172 const struct vteprec_global *vtep_global = ctx->vtep_global;
2175 /* Delete Manager rows pointed to by 'managers' column. */
2176 for (i = 0; i < vtep_global->n_managers; i++) {
2177 vteprec_manager_delete(vtep_global->managers[i]);
2180 /* Delete 'Manager' row refs in 'managers' column. */
2181 vteprec_global_set_managers(vtep_global, NULL, 0);
2185 cmd_del_manager(struct vtep_ctl_context *ctx)
2187 const struct vteprec_global *vtep_global = ctx->vtep_global;
2189 verify_managers(vtep_global);
2190 delete_managers(ctx);
2194 insert_managers(struct vtep_ctl_context *ctx, char *targets[], size_t n)
2196 struct vteprec_manager **managers;
2199 /* Insert each manager in a new row in Manager table. */
2200 managers = xmalloc(n * sizeof *managers);
2201 for (i = 0; i < n; i++) {
2202 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2203 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2205 managers[i] = vteprec_manager_insert(ctx->txn);
2206 vteprec_manager_set_target(managers[i], targets[i]);
2209 /* Store uuids of new Manager rows in 'managers' column. */
2210 vteprec_global_set_managers(ctx->vtep_global, managers, n);
2215 cmd_set_manager(struct vtep_ctl_context *ctx)
2217 const size_t n = ctx->argc - 1;
2219 verify_managers(ctx->vtep_global);
2220 delete_managers(ctx);
2221 insert_managers(ctx, &ctx->argv[1], n);
2224 /* Parameter commands. */
2226 struct vtep_ctl_row_id {
2227 const struct ovsdb_idl_table_class *table;
2228 const struct ovsdb_idl_column *name_column;
2229 const struct ovsdb_idl_column *uuid_column;
2232 struct vtep_ctl_table_class {
2233 struct ovsdb_idl_table_class *class;
2234 struct vtep_ctl_row_id row_ids[2];
2237 static const struct vtep_ctl_table_class tables[] = {
2238 {&vteprec_table_global,
2239 {{&vteprec_table_global, NULL, NULL},
2240 {NULL, NULL, NULL}}},
2242 {&vteprec_table_logical_binding_stats,
2243 {{NULL, NULL, NULL},
2244 {NULL, NULL, NULL}}},
2246 {&vteprec_table_logical_switch,
2247 {{&vteprec_table_logical_switch, &vteprec_logical_switch_col_name, NULL},
2248 {NULL, NULL, NULL}}},
2250 {&vteprec_table_ucast_macs_local,
2251 {{NULL, NULL, NULL},
2252 {NULL, NULL, NULL}}},
2254 {&vteprec_table_ucast_macs_remote,
2255 {{NULL, NULL, NULL},
2256 {NULL, NULL, NULL}}},
2258 {&vteprec_table_mcast_macs_local,
2259 {{NULL, NULL, NULL},
2260 {NULL, NULL, NULL}}},
2262 {&vteprec_table_mcast_macs_remote,
2263 {{NULL, NULL, NULL},
2264 {NULL, NULL, NULL}}},
2266 {&vteprec_table_manager,
2267 {{&vteprec_table_manager, &vteprec_manager_col_target, NULL},
2268 {NULL, NULL, NULL}}},
2270 {&vteprec_table_physical_locator,
2271 {{NULL, NULL, NULL},
2272 {NULL, NULL, NULL}}},
2274 {&vteprec_table_physical_locator_set,
2275 {{NULL, NULL, NULL},
2276 {NULL, NULL, NULL}}},
2278 {&vteprec_table_physical_port,
2279 {{&vteprec_table_physical_port, &vteprec_physical_port_col_name, NULL},
2280 {NULL, NULL, NULL}}},
2282 {&vteprec_table_physical_switch,
2283 {{&vteprec_table_physical_switch, &vteprec_physical_switch_col_name, NULL},
2284 {NULL, NULL, NULL}}},
2286 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2290 die_if_error(char *error)
2293 vtep_ctl_fatal("%s", error);
2298 to_lower_and_underscores(unsigned c)
2300 return c == '-' ? '_' : tolower(c);
2304 score_partial_match(const char *name, const char *s)
2308 if (!strcmp(name, s)) {
2311 for (score = 0; ; score++, name++, s++) {
2312 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2314 } else if (*name == '\0') {
2315 return UINT_MAX - 1;
2318 return *s == '\0' ? score : 0;
2321 static const struct vtep_ctl_table_class *
2322 get_table(const char *table_name)
2324 const struct vtep_ctl_table_class *table;
2325 const struct vtep_ctl_table_class *best_match = NULL;
2326 unsigned int best_score = 0;
2328 for (table = tables; table->class; table++) {
2329 unsigned int score = score_partial_match(table->class->name,
2331 if (score > best_score) {
2334 } else if (score == best_score) {
2340 } else if (best_score) {
2341 vtep_ctl_fatal("multiple table names match \"%s\"", table_name);
2343 vtep_ctl_fatal("unknown table \"%s\"", table_name);
2347 static const struct vtep_ctl_table_class *
2348 pre_get_table(struct vtep_ctl_context *ctx, const char *table_name)
2350 const struct vtep_ctl_table_class *table_class;
2353 table_class = get_table(table_name);
2354 ovsdb_idl_add_table(ctx->idl, table_class->class);
2356 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2357 const struct vtep_ctl_row_id *id = &table_class->row_ids[i];
2359 ovsdb_idl_add_table(ctx->idl, id->table);
2361 if (id->name_column) {
2362 ovsdb_idl_add_column(ctx->idl, id->name_column);
2364 if (id->uuid_column) {
2365 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2372 static const struct ovsdb_idl_row *
2373 get_row_by_id(struct vtep_ctl_context *ctx, const struct vtep_ctl_table_class *table,
2374 const struct vtep_ctl_row_id *id, const char *record_id)
2376 const struct ovsdb_idl_row *referrer, *final;
2382 if (!id->name_column) {
2383 if (strcmp(record_id, ".")) {
2386 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2387 if (!referrer || ovsdb_idl_next_row(referrer)) {
2391 const struct ovsdb_idl_row *row;
2394 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2396 row = ovsdb_idl_next_row(row))
2398 const struct ovsdb_datum *name;
2400 name = ovsdb_idl_get(row, id->name_column,
2401 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2402 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2404 vtep_ctl_fatal("multiple rows in %s match \"%s\"",
2405 table->class->name, record_id);
2416 if (id->uuid_column) {
2417 const struct ovsdb_datum *uuid;
2419 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2420 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2421 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2423 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2424 &uuid->keys[0].uuid);
2433 static const struct ovsdb_idl_row *
2434 get_row (struct vtep_ctl_context *ctx,
2435 const struct vtep_ctl_table_class *table, const char *record_id)
2437 const struct ovsdb_idl_row *row;
2440 if (uuid_from_string(&uuid, record_id)) {
2441 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2445 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2446 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2455 static const struct ovsdb_idl_row *
2456 must_get_row(struct vtep_ctl_context *ctx,
2457 const struct vtep_ctl_table_class *table, const char *record_id)
2459 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2461 vtep_ctl_fatal("no row \"%s\" in table %s",
2462 record_id, table->class->name);
2468 get_column(const struct vtep_ctl_table_class *table, const char *column_name,
2469 const struct ovsdb_idl_column **columnp)
2471 const struct ovsdb_idl_column *best_match = NULL;
2472 unsigned int best_score = 0;
2475 for (i = 0; i < table->class->n_columns; i++) {
2476 const struct ovsdb_idl_column *column = &table->class->columns[i];
2477 unsigned int score = score_partial_match(column->name, column_name);
2478 if (score > best_score) {
2479 best_match = column;
2481 } else if (score == best_score) {
2486 *columnp = best_match;
2489 } else if (best_score) {
2490 return xasprintf("%s contains more than one column whose name "
2491 "matches \"%s\"", table->class->name, column_name);
2493 return xasprintf("%s does not contain a column whose name matches "
2494 "\"%s\"", table->class->name, column_name);
2498 static struct ovsdb_symbol *
2499 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2501 struct ovsdb_symbol *symbol;
2504 vtep_ctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2508 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2511 symbol = ovsdb_symbol_table_insert(symtab, id);
2512 if (symbol->created) {
2513 vtep_ctl_fatal("row id \"%s\" may only be specified on one --id option",
2516 symbol->created = true;
2521 pre_get_column(struct vtep_ctl_context *ctx,
2522 const struct vtep_ctl_table_class *table, const char *column_name,
2523 const struct ovsdb_idl_column **columnp)
2525 die_if_error(get_column(table, column_name, columnp));
2526 ovsdb_idl_add_column(ctx->idl, *columnp);
2530 missing_operator_error(const char *arg, const char **allowed_operators,
2536 ds_put_format(&s, "%s: argument does not end in ", arg);
2537 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2538 if (n_allowed == 2) {
2539 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2540 } else if (n_allowed > 2) {
2543 for (i = 1; i < n_allowed - 1; i++) {
2544 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2546 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2548 ds_put_format(&s, " followed by a value.");
2550 return ds_steal_cstr(&s);
2553 /* Breaks 'arg' apart into a number of fields in the following order:
2555 * - The name of a column in 'table', stored into '*columnp'. The column
2556 * name may be abbreviated.
2558 * - Optionally ':' followed by a key string. The key is stored as a
2559 * malloc()'d string into '*keyp', or NULL if no key is present in
2562 * - If 'valuep' is nonnull, an operator followed by a value string. The
2563 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2564 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2565 * index of the operator within 'allowed_operators' is stored into
2566 * '*operatorp'. The value is stored as a malloc()'d string into
2567 * '*valuep', or NULL if no value is present in 'arg'.
2569 * On success, returns NULL. On failure, returned a malloc()'d string error
2570 * message and stores NULL into all of the nonnull output arguments. */
2571 static char * WARN_UNUSED_RESULT
2572 parse_column_key_value(const char *arg,
2573 const struct vtep_ctl_table_class *table,
2574 const struct ovsdb_idl_column **columnp, char **keyp,
2576 const char **allowed_operators, size_t n_allowed,
2579 const char *p = arg;
2583 ovs_assert(!(operatorp && !valuep));
2589 /* Parse column name. */
2590 error = ovsdb_token_parse(&p, &column_name);
2594 if (column_name[0] == '\0') {
2596 error = xasprintf("%s: missing column name", arg);
2599 error = get_column(table, column_name, columnp);
2605 /* Parse key string. */
2608 error = ovsdb_token_parse(&p, keyp);
2614 /* Parse value string. */
2620 if (!allowed_operators) {
2621 static const char *equals = "=";
2622 allowed_operators = =
2628 for (i = 0; i < n_allowed; i++) {
2629 const char *op = allowed_operators[i];
2630 size_t op_len = strlen(op);
2632 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2638 error = missing_operator_error(arg, allowed_operators, n_allowed);
2645 *valuep = xstrdup(p + best_len);
2648 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2669 static const struct ovsdb_idl_column *
2670 pre_parse_column_key_value(struct vtep_ctl_context *ctx,
2672 const struct vtep_ctl_table_class *table)
2674 const struct ovsdb_idl_column *column;
2679 die_if_error(ovsdb_token_parse(&p, &column_name));
2680 if (column_name[0] == '\0') {
2681 vtep_ctl_fatal("%s: missing column name", arg);
2684 pre_get_column(ctx, table, column_name, &column);
2691 check_mutable(const struct vtep_ctl_table_class *table,
2692 const struct ovsdb_idl_column *column)
2694 if (!column->mutable) {
2695 vtep_ctl_fatal("cannot modify read-only column %s in table %s",
2696 column->name, table->class->name);
2701 pre_cmd_get(struct vtep_ctl_context *ctx)
2703 const char *id = shash_find_data(&ctx->options, "--id");
2704 const char *table_name = ctx->argv[1];
2705 const struct vtep_ctl_table_class *table;
2708 /* Using "get" without --id or a column name could possibly make sense.
2709 * Maybe, for example, a vtep-ctl run wants to assert that a row exists.
2710 * But it is unlikely that an interactive user would want to do that, so
2711 * issue a warning if we're running on a terminal. */
2712 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2713 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2714 "possibly erroneous");
2717 table = pre_get_table(ctx, table_name);
2718 for (i = 3; i < ctx->argc; i++) {
2719 if (!strcasecmp(ctx->argv[i], "_uuid")
2720 || !strcasecmp(ctx->argv[i], "-uuid")) {
2724 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2729 cmd_get(struct vtep_ctl_context *ctx)
2731 const char *id = shash_find_data(&ctx->options, "--id");
2732 bool if_exists = shash_find(&ctx->options, "--if-exists");
2733 const char *table_name = ctx->argv[1];
2734 const char *record_id = ctx->argv[2];
2735 const struct vtep_ctl_table_class *table;
2736 const struct ovsdb_idl_row *row;
2737 struct ds *out = &ctx->output;
2740 table = get_table(table_name);
2741 row = must_get_row(ctx, table, record_id);
2743 struct ovsdb_symbol *symbol;
2746 symbol = create_symbol(ctx->symtab, id, &new);
2748 vtep_ctl_fatal("row id \"%s\" specified on \"get\" command was used "
2749 "before it was defined", id);
2751 symbol->uuid = row->uuid;
2753 /* This symbol refers to a row that already exists, so disable warnings
2754 * about it being unreferenced. */
2755 symbol->strong_ref = true;
2757 for (i = 3; i < ctx->argc; i++) {
2758 const struct ovsdb_idl_column *column;
2759 const struct ovsdb_datum *datum;
2762 /* Special case for obtaining the UUID of a row. We can't just do this
2763 * through parse_column_key_value() below since it returns a "struct
2764 * ovsdb_idl_column" and the UUID column doesn't have one. */
2765 if (!strcasecmp(ctx->argv[i], "_uuid")
2766 || !strcasecmp(ctx->argv[i], "-uuid")) {
2767 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2771 die_if_error(parse_column_key_value(ctx->argv[i], table,
2772 &column, &key_string,
2773 NULL, NULL, 0, NULL));
2775 ovsdb_idl_txn_verify(row, column);
2776 datum = ovsdb_idl_read(row, column);
2778 union ovsdb_atom key;
2781 if (column->type.value.type == OVSDB_TYPE_VOID) {
2782 vtep_ctl_fatal("cannot specify key to get for non-map column %s",
2786 die_if_error(ovsdb_atom_from_string(&key,
2788 key_string, ctx->symtab));
2790 idx = ovsdb_datum_find_key(datum, &key,
2791 column->type.key.type);
2792 if (idx == UINT_MAX) {
2794 vtep_ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2795 key_string, table->class->name, record_id,
2799 ovsdb_atom_to_string(&datum->values[idx],
2800 column->type.value.type, out);
2802 ovsdb_atom_destroy(&key, column->type.key.type);
2804 ovsdb_datum_to_string(datum, &column->type, out);
2806 ds_put_char(out, '\n');
2813 parse_column_names(const char *column_names,
2814 const struct vtep_ctl_table_class *table,
2815 const struct ovsdb_idl_column ***columnsp,
2818 const struct ovsdb_idl_column **columns;
2821 if (!column_names) {
2824 n_columns = table->class->n_columns + 1;
2825 columns = xmalloc(n_columns * sizeof *columns);
2827 for (i = 0; i < table->class->n_columns; i++) {
2828 columns[i + 1] = &table->class->columns[i];
2831 char *s = xstrdup(column_names);
2832 size_t allocated_columns;
2833 char *save_ptr = NULL;
2837 allocated_columns = n_columns = 0;
2838 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2839 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2840 const struct ovsdb_idl_column *column;
2842 if (!strcasecmp(column_name, "_uuid")) {
2845 die_if_error(get_column(table, column_name, &column));
2847 if (n_columns >= allocated_columns) {
2848 columns = x2nrealloc(columns, &allocated_columns,
2851 columns[n_columns++] = column;
2856 vtep_ctl_fatal("must specify at least one column name");
2859 *columnsp = columns;
2860 *n_columnsp = n_columns;
2865 pre_list_columns(struct vtep_ctl_context *ctx,
2866 const struct vtep_ctl_table_class *table,
2867 const char *column_names)
2869 const struct ovsdb_idl_column **columns;
2873 parse_column_names(column_names, table, &columns, &n_columns);
2874 for (i = 0; i < n_columns; i++) {
2876 ovsdb_idl_add_column(ctx->idl, columns[i]);
2883 pre_cmd_list(struct vtep_ctl_context *ctx)
2885 const char *column_names = shash_find_data(&ctx->options, "--columns");
2886 const char *table_name = ctx->argv[1];
2887 const struct vtep_ctl_table_class *table;
2889 table = pre_get_table(ctx, table_name);
2890 pre_list_columns(ctx, table, column_names);
2893 static struct table *
2894 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2899 out = xmalloc(sizeof *out);
2902 for (i = 0; i < n_columns; i++) {
2903 const struct ovsdb_idl_column *column = columns[i];
2904 const char *column_name = column ? column->name : "_uuid";
2906 table_add_column(out, "%s", column_name);
2913 list_record(const struct ovsdb_idl_row *row,
2914 const struct ovsdb_idl_column **columns, size_t n_columns,
2920 for (i = 0; i < n_columns; i++) {
2921 const struct ovsdb_idl_column *column = columns[i];
2922 struct cell *cell = table_add_cell(out);
2925 struct ovsdb_datum datum;
2926 union ovsdb_atom atom;
2928 atom.uuid = row->uuid;
2931 datum.values = NULL;
2934 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2935 cell->type = &ovsdb_type_uuid;
2937 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2939 cell->json = ovsdb_datum_to_json(datum, &column->type);
2940 cell->type = &column->type;
2946 cmd_list(struct vtep_ctl_context *ctx)
2948 const char *column_names = shash_find_data(&ctx->options, "--columns");
2949 const struct ovsdb_idl_column **columns;
2950 const char *table_name = ctx->argv[1];
2951 const struct vtep_ctl_table_class *table;
2956 table = get_table(table_name);
2957 parse_column_names(column_names, table, &columns, &n_columns);
2958 out = ctx->table = list_make_table(columns, n_columns);
2959 if (ctx->argc > 2) {
2960 for (i = 2; i < ctx->argc; i++) {
2961 list_record(must_get_row(ctx, table, ctx->argv[i]),
2962 columns, n_columns, out);
2965 const struct ovsdb_idl_row *row;
2967 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
2968 row = ovsdb_idl_next_row(row)) {
2969 list_record(row, columns, n_columns, out);
2976 pre_cmd_find(struct vtep_ctl_context *ctx)
2978 const char *column_names = shash_find_data(&ctx->options, "--columns");
2979 const char *table_name = ctx->argv[1];
2980 const struct vtep_ctl_table_class *table;
2983 table = pre_get_table(ctx, table_name);
2984 pre_list_columns(ctx, table, column_names);
2985 for (i = 2; i < ctx->argc; i++) {
2986 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2991 cmd_find(struct vtep_ctl_context *ctx)
2993 const char *column_names = shash_find_data(&ctx->options, "--columns");
2994 const struct ovsdb_idl_column **columns;
2995 const char *table_name = ctx->argv[1];
2996 const struct vtep_ctl_table_class *table;
2997 const struct ovsdb_idl_row *row;
3001 table = get_table(table_name);
3002 parse_column_names(column_names, table, &columns, &n_columns);
3003 out = ctx->table = list_make_table(columns, n_columns);
3004 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3005 row = ovsdb_idl_next_row(row)) {
3008 for (i = 2; i < ctx->argc; i++) {
3009 if (!is_condition_satisfied(table, row, ctx->argv[i],
3014 list_record(row, columns, n_columns, out);
3022 pre_cmd_set(struct vtep_ctl_context *ctx)
3024 const char *table_name = ctx->argv[1];
3025 const struct vtep_ctl_table_class *table;
3028 table = pre_get_table(ctx, table_name);
3029 for (i = 3; i < ctx->argc; i++) {
3030 const struct ovsdb_idl_column *column;
3032 column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
3033 check_mutable(table, column);
3038 set_column(const struct vtep_ctl_table_class *table,
3039 const struct ovsdb_idl_row *row, const char *arg,
3040 struct ovsdb_symbol_table *symtab)
3042 const struct ovsdb_idl_column *column;
3043 char *key_string, *value_string;
3046 error = parse_column_key_value(arg, table, &column, &key_string,
3047 NULL, NULL, 0, &value_string);
3048 die_if_error(error);
3049 if (!value_string) {
3050 vtep_ctl_fatal("%s: missing value", arg);
3054 union ovsdb_atom key, value;
3055 struct ovsdb_datum datum;
3057 if (column->type.value.type == OVSDB_TYPE_VOID) {
3058 vtep_ctl_fatal("cannot specify key to set for non-map column %s",
3062 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3063 key_string, symtab));
3064 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3065 value_string, symtab));
3067 ovsdb_datum_init_empty(&datum);
3068 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3070 ovsdb_atom_destroy(&key, column->type.key.type);
3071 ovsdb_atom_destroy(&value, column->type.value.type);
3073 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3074 &column->type, false);
3075 ovsdb_idl_txn_write(row, column, &datum);
3077 struct ovsdb_datum datum;
3079 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3080 value_string, symtab));
3081 ovsdb_idl_txn_write(row, column, &datum);
3089 cmd_set(struct vtep_ctl_context *ctx)
3091 const char *table_name = ctx->argv[1];
3092 const char *record_id = ctx->argv[2];
3093 const struct vtep_ctl_table_class *table;
3094 const struct ovsdb_idl_row *row;
3097 table = get_table(table_name);
3098 row = must_get_row(ctx, table, record_id);
3099 for (i = 3; i < ctx->argc; i++) {
3100 set_column(table, row, ctx->argv[i], ctx->symtab);
3105 pre_cmd_add(struct vtep_ctl_context *ctx)
3107 const char *table_name = ctx->argv[1];
3108 const char *column_name = ctx->argv[3];
3109 const struct vtep_ctl_table_class *table;
3110 const struct ovsdb_idl_column *column;
3112 table = pre_get_table(ctx, table_name);
3113 pre_get_column(ctx, table, column_name, &column);
3114 check_mutable(table, column);
3118 cmd_add(struct vtep_ctl_context *ctx)
3120 const char *table_name = ctx->argv[1];
3121 const char *record_id = ctx->argv[2];
3122 const char *column_name = ctx->argv[3];
3123 const struct vtep_ctl_table_class *table;
3124 const struct ovsdb_idl_column *column;
3125 const struct ovsdb_idl_row *row;
3126 const struct ovsdb_type *type;
3127 struct ovsdb_datum old;
3130 table = get_table(table_name);
3131 row = must_get_row(ctx, table, record_id);
3132 die_if_error(get_column(table, column_name, &column));
3134 type = &column->type;
3135 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3136 for (i = 4; i < ctx->argc; i++) {
3137 struct ovsdb_type add_type;
3138 struct ovsdb_datum add;
3142 add_type.n_max = UINT_MAX;
3143 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3145 ovsdb_datum_union(&old, &add, type, false);
3146 ovsdb_datum_destroy(&add, type);
3148 if (old.n > type->n_max) {
3149 vtep_ctl_fatal("\"add\" operation would put %u %s in column %s of "
3150 "table %s but the maximum number is %u",
3152 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3153 column->name, table->class->name, type->n_max);
3155 ovsdb_idl_txn_verify(row, column);
3156 ovsdb_idl_txn_write(row, column, &old);
3160 pre_cmd_remove(struct vtep_ctl_context *ctx)
3162 const char *table_name = ctx->argv[1];
3163 const char *column_name = ctx->argv[3];
3164 const struct vtep_ctl_table_class *table;
3165 const struct ovsdb_idl_column *column;
3167 table = pre_get_table(ctx, table_name);
3168 pre_get_column(ctx, table, column_name, &column);
3169 check_mutable(table, column);
3173 cmd_remove(struct vtep_ctl_context *ctx)
3175 const char *table_name = ctx->argv[1];
3176 const char *record_id = ctx->argv[2];
3177 const char *column_name = ctx->argv[3];
3178 const struct vtep_ctl_table_class *table;
3179 const struct ovsdb_idl_column *column;
3180 const struct ovsdb_idl_row *row;
3181 const struct ovsdb_type *type;
3182 struct ovsdb_datum old;
3185 table = get_table(table_name);
3186 row = must_get_row(ctx, table, record_id);
3187 die_if_error(get_column(table, column_name, &column));
3189 type = &column->type;
3190 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3191 for (i = 4; i < ctx->argc; i++) {
3192 struct ovsdb_type rm_type;
3193 struct ovsdb_datum rm;
3198 rm_type.n_max = UINT_MAX;
3199 error = ovsdb_datum_from_string(&rm, &rm_type,
3200 ctx->argv[i], ctx->symtab);
3201 if (error && ovsdb_type_is_map(&rm_type)) {
3203 rm_type.value.type = OVSDB_TYPE_VOID;
3204 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3205 ctx->argv[i], ctx->symtab));
3207 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3208 ovsdb_datum_destroy(&rm, &rm_type);
3210 if (old.n < type->n_min) {
3211 vtep_ctl_fatal("\"remove\" operation would put %u %s in column %s of "
3212 "table %s but the minimum number is %u",
3214 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3215 column->name, table->class->name, type->n_min);
3217 ovsdb_idl_txn_verify(row, column);
3218 ovsdb_idl_txn_write(row, column, &old);
3222 pre_cmd_clear(struct vtep_ctl_context *ctx)
3224 const char *table_name = ctx->argv[1];
3225 const struct vtep_ctl_table_class *table;
3228 table = pre_get_table(ctx, table_name);
3229 for (i = 3; i < ctx->argc; i++) {
3230 const struct ovsdb_idl_column *column;
3232 pre_get_column(ctx, table, ctx->argv[i], &column);
3233 check_mutable(table, column);
3238 cmd_clear(struct vtep_ctl_context *ctx)
3240 const char *table_name = ctx->argv[1];
3241 const char *record_id = ctx->argv[2];
3242 const struct vtep_ctl_table_class *table;
3243 const struct ovsdb_idl_row *row;
3246 table = get_table(table_name);
3247 row = must_get_row(ctx, table, record_id);
3248 for (i = 3; i < ctx->argc; i++) {
3249 const struct ovsdb_idl_column *column;
3250 const struct ovsdb_type *type;
3251 struct ovsdb_datum datum;
3253 die_if_error(get_column(table, ctx->argv[i], &column));
3255 type = &column->type;
3256 if (type->n_min > 0) {
3257 vtep_ctl_fatal("\"clear\" operation cannot be applied to column %s "
3258 "of table %s, which is not allowed to be empty",
3259 column->name, table->class->name);
3262 ovsdb_datum_init_empty(&datum);
3263 ovsdb_idl_txn_write(row, column, &datum);
3268 pre_create(struct vtep_ctl_context *ctx)
3270 const char *id = shash_find_data(&ctx->options, "--id");
3271 const char *table_name = ctx->argv[1];
3272 const struct vtep_ctl_table_class *table;
3274 table = get_table(table_name);
3275 if (!id && !table->class->is_root) {
3276 VLOG_WARN("applying \"create\" command to table %s without --id "
3277 "option will have no effect", table->class->name);
3282 cmd_create(struct vtep_ctl_context *ctx)
3284 const char *id = shash_find_data(&ctx->options, "--id");
3285 const char *table_name = ctx->argv[1];
3286 const struct vtep_ctl_table_class *table = get_table(table_name);
3287 const struct ovsdb_idl_row *row;
3288 const struct uuid *uuid;
3292 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3293 if (table->class->is_root) {
3294 /* This table is in the root set, meaning that rows created in it
3295 * won't disappear even if they are unreferenced, so disable
3296 * warnings about that by pretending that there is a reference. */
3297 symbol->strong_ref = true;
3299 uuid = &symbol->uuid;
3304 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3305 for (i = 2; i < ctx->argc; i++) {
3306 set_column(table, row, ctx->argv[i], ctx->symtab);
3308 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3311 /* This function may be used as the 'postprocess' function for commands that
3312 * insert new rows into the database. It expects that the command's 'run'
3313 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3314 * sole output. It replaces that output by the row's permanent UUID assigned
3315 * by the database server and appends a new-line.
3317 * Currently we use this only for "create", because the higher-level commands
3318 * are supposed to be independent of the actual structure of the VTEP
3321 post_create(struct vtep_ctl_context *ctx)
3323 const struct uuid *real;
3326 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3329 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3331 ds_clear(&ctx->output);
3332 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3334 ds_put_char(&ctx->output, '\n');
3338 pre_cmd_destroy(struct vtep_ctl_context *ctx)
3340 const char *table_name = ctx->argv[1];
3342 pre_get_table(ctx, table_name);
3346 cmd_destroy(struct vtep_ctl_context *ctx)
3348 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3349 bool delete_all = shash_find(&ctx->options, "--all");
3350 const char *table_name = ctx->argv[1];
3351 const struct vtep_ctl_table_class *table;
3354 table = get_table(table_name);
3356 if (delete_all && ctx->argc > 2) {
3357 vtep_ctl_fatal("--all and records argument should not be specified together");
3360 if (delete_all && !must_exist) {
3361 vtep_ctl_fatal("--all and --if-exists should not be specified together");
3365 const struct ovsdb_idl_row *row;
3366 const struct ovsdb_idl_row *next_row;
3368 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3370 next_row = ovsdb_idl_next_row(row);
3371 ovsdb_idl_txn_delete(row);
3375 for (i = 2; i < ctx->argc; i++) {
3376 const struct ovsdb_idl_row *row;
3378 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3380 ovsdb_idl_txn_delete(row);
3387 RELOP(RELOP_EQ, "=") \
3388 RELOP(RELOP_NE, "!=") \
3389 RELOP(RELOP_LT, "<") \
3390 RELOP(RELOP_GT, ">") \
3391 RELOP(RELOP_LE, "<=") \
3392 RELOP(RELOP_GE, ">=") \
3393 RELOP(RELOP_SET_EQ, "{=}") \
3394 RELOP(RELOP_SET_NE, "{!=}") \
3395 RELOP(RELOP_SET_LT, "{<}") \
3396 RELOP(RELOP_SET_GT, "{>}") \
3397 RELOP(RELOP_SET_LE, "{<=}") \
3398 RELOP(RELOP_SET_GE, "{>=}")
3401 #define RELOP(ENUM, STRING) ENUM,
3407 is_set_operator(enum relop op)
3409 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3410 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3411 op == RELOP_SET_LE || op == RELOP_SET_GE);
3415 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3416 const struct ovsdb_type *type, enum relop op)
3421 return ovsdb_datum_compare_3way(a, b, type) == 0;
3424 return ovsdb_datum_compare_3way(a, b, type) != 0;
3426 return ovsdb_datum_compare_3way(a, b, type) < 0;
3428 return ovsdb_datum_compare_3way(a, b, type) > 0;
3430 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3432 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3435 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3437 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3439 return ovsdb_datum_includes_all(a, b, type);
3441 return ovsdb_datum_includes_all(b, a, type);
3449 is_condition_satisfied(const struct vtep_ctl_table_class *table,
3450 const struct ovsdb_idl_row *row, const char *arg,
3451 struct ovsdb_symbol_table *symtab)
3453 static const char *operators[] = {
3454 #define RELOP(ENUM, STRING) STRING,
3459 const struct ovsdb_idl_column *column;
3460 const struct ovsdb_datum *have_datum;
3461 char *key_string, *value_string;
3462 struct ovsdb_type type;
3467 error = parse_column_key_value(arg, table, &column, &key_string,
3468 &operator, operators, ARRAY_SIZE(operators),
3470 die_if_error(error);
3471 if (!value_string) {
3472 vtep_ctl_fatal("%s: missing value", arg);
3475 type = column->type;
3476 type.n_max = UINT_MAX;
3478 have_datum = ovsdb_idl_read(row, column);
3480 union ovsdb_atom want_key;
3481 struct ovsdb_datum b;
3484 if (column->type.value.type == OVSDB_TYPE_VOID) {
3485 vtep_ctl_fatal("cannot specify key to check for non-map column %s",
3489 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3490 key_string, symtab));
3492 type.key = type.value;
3493 type.value.type = OVSDB_TYPE_VOID;
3494 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3496 idx = ovsdb_datum_find_key(have_datum,
3497 &want_key, column->type.key.type);
3498 if (idx == UINT_MAX && !is_set_operator(operator)) {
3501 struct ovsdb_datum a;
3503 if (idx != UINT_MAX) {
3505 a.keys = &have_datum->values[idx];
3513 retval = evaluate_relop(&a, &b, &type, operator);
3516 ovsdb_atom_destroy(&want_key, column->type.key.type);
3517 ovsdb_datum_destroy(&b, &type);
3519 struct ovsdb_datum want_datum;
3521 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3522 value_string, symtab));
3523 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3524 ovsdb_datum_destroy(&want_datum, &column->type);
3534 pre_cmd_wait_until(struct vtep_ctl_context *ctx)
3536 const char *table_name = ctx->argv[1];
3537 const struct vtep_ctl_table_class *table;
3540 table = pre_get_table(ctx, table_name);
3542 for (i = 3; i < ctx->argc; i++) {
3543 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3548 cmd_wait_until(struct vtep_ctl_context *ctx)
3550 const char *table_name = ctx->argv[1];
3551 const char *record_id = ctx->argv[2];
3552 const struct vtep_ctl_table_class *table;
3553 const struct ovsdb_idl_row *row;
3556 table = get_table(table_name);
3558 row = get_row(ctx, table, record_id);
3560 ctx->try_again = true;
3564 for (i = 3; i < ctx->argc; i++) {
3565 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3566 ctx->try_again = true;
3572 /* Prepares 'ctx', which has already been initialized with
3573 * vtep_ctl_context_init(), for processing 'command'. */
3575 vtep_ctl_context_init_command(struct vtep_ctl_context *ctx,
3576 struct vtep_ctl_command *command)
3578 ctx->argc = command->argc;
3579 ctx->argv = command->argv;
3580 ctx->options = command->options;
3582 ds_swap(&ctx->output, &command->output);
3583 ctx->table = command->table;
3585 ctx->verified_ports = false;
3587 ctx->try_again = false;
3590 /* Prepares 'ctx' for processing commands, initializing its members with the
3591 * values passed in as arguments.
3593 * If 'command' is nonnull, calls vtep_ctl_context_init_command() to prepare for
3594 * that particular command. */
3596 vtep_ctl_context_init(struct vtep_ctl_context *ctx,
3597 struct vtep_ctl_command *command,
3598 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3599 const struct vteprec_global *vtep_global,
3600 struct ovsdb_symbol_table *symtab)
3603 vtep_ctl_context_init_command(ctx, command);
3607 ctx->vtep_global = vtep_global;
3608 ctx->symtab = symtab;
3609 ctx->cache_valid = false;
3612 /* Completes processing of 'command' within 'ctx'. */
3614 vtep_ctl_context_done_command(struct vtep_ctl_context *ctx,
3615 struct vtep_ctl_command *command)
3617 ds_swap(&ctx->output, &command->output);
3618 command->table = ctx->table;
3621 /* Finishes up with 'ctx'.
3623 * If command is nonnull, first calls vtep_ctl_context_done_command() to complete
3624 * processing that command within 'ctx'. */
3626 vtep_ctl_context_done(struct vtep_ctl_context *ctx, struct vtep_ctl_command *command)
3629 vtep_ctl_context_done_command(ctx, command);
3634 run_prerequisites(struct vtep_ctl_command *commands, size_t n_commands,
3635 struct ovsdb_idl *idl)
3637 struct vtep_ctl_command *c;
3639 ovsdb_idl_add_table(idl, &vteprec_table_global);
3640 for (c = commands; c < &commands[n_commands]; c++) {
3641 if (c->syntax->prerequisites) {
3642 struct vtep_ctl_context ctx;
3644 ds_init(&c->output);
3647 vtep_ctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3648 (c->syntax->prerequisites)(&ctx);
3649 vtep_ctl_context_done(&ctx, c);
3651 ovs_assert(!c->output.string);
3652 ovs_assert(!c->table);
3658 do_vtep_ctl(const char *args, struct vtep_ctl_command *commands,
3659 size_t n_commands, struct ovsdb_idl *idl)
3661 struct ovsdb_idl_txn *txn;
3662 const struct vteprec_global *vtep_global;
3663 enum ovsdb_idl_txn_status status;
3664 struct ovsdb_symbol_table *symtab;
3665 struct vtep_ctl_context ctx;
3666 struct vtep_ctl_command *c;
3667 struct shash_node *node;
3670 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3672 ovsdb_idl_txn_set_dry_run(txn);
3675 ovsdb_idl_txn_add_comment(txn, "vtep-ctl: %s", args);
3677 vtep_global = vteprec_global_first(idl);
3679 /* XXX add verification that table is empty */
3680 vtep_global = vteprec_global_insert(txn);
3683 symtab = ovsdb_symbol_table_create();
3684 for (c = commands; c < &commands[n_commands]; c++) {
3685 ds_init(&c->output);
3688 vtep_ctl_context_init(&ctx, NULL, idl, txn, vtep_global, symtab);
3689 for (c = commands; c < &commands[n_commands]; c++) {
3690 vtep_ctl_context_init_command(&ctx, c);
3691 if (c->syntax->run) {
3692 (c->syntax->run)(&ctx);
3694 vtep_ctl_context_done_command(&ctx, c);
3696 if (ctx.try_again) {
3697 vtep_ctl_context_done(&ctx, NULL);
3701 vtep_ctl_context_done(&ctx, NULL);
3703 SHASH_FOR_EACH (node, &symtab->sh) {
3704 struct ovsdb_symbol *symbol = node->data;
3705 if (!symbol->created) {
3706 vtep_ctl_fatal("row id \"%s\" is referenced but never created "
3707 "(e.g. with \"-- --id=%s create ...\")",
3708 node->name, node->name);
3710 if (!symbol->strong_ref) {
3711 if (!symbol->weak_ref) {
3712 VLOG_WARN("row id \"%s\" was created but no reference to it "
3713 "was inserted, so it will not actually appear in "
3714 "the database", node->name);
3716 VLOG_WARN("row id \"%s\" was created but only a weak "
3717 "reference to it was inserted, so it will not "
3718 "actually appear in the database", node->name);
3723 status = ovsdb_idl_txn_commit_block(txn);
3724 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3725 for (c = commands; c < &commands[n_commands]; c++) {
3726 if (c->syntax->postprocess) {
3727 struct vtep_ctl_context ctx;
3729 vtep_ctl_context_init(&ctx, c, idl, txn, vtep_global, symtab);
3730 (c->syntax->postprocess)(&ctx);
3731 vtep_ctl_context_done(&ctx, c);
3735 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3736 ovsdb_idl_txn_destroy(txn);
3737 txn = the_idl_txn = NULL;
3740 case TXN_UNCOMMITTED:
3741 case TXN_INCOMPLETE:
3745 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3746 vtep_ctl_fatal("transaction aborted");
3756 vtep_ctl_fatal("transaction error: %s", error);
3758 case TXN_NOT_LOCKED:
3759 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3760 vtep_ctl_fatal("database not locked");
3767 ovsdb_symbol_table_destroy(symtab);
3769 for (c = commands; c < &commands[n_commands]; c++) {
3770 struct ds *ds = &c->output;
3773 table_print(c->table, &table_style);
3774 } else if (oneline) {
3778 for (j = 0; j < ds->length; j++) {
3779 int ch = ds->string[j];
3782 fputs("\\n", stdout);
3786 fputs("\\\\", stdout);
3795 fputs(ds_cstr(ds), stdout);
3797 ds_destroy(&c->output);
3798 table_destroy(c->table);
3801 shash_destroy_free_data(&c->options);
3805 ovsdb_idl_destroy(idl);
3810 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3811 * resources and return so that the caller can try again. */
3813 ovsdb_idl_txn_abort(txn);
3814 ovsdb_idl_txn_destroy(txn);
3816 ovsdb_symbol_table_destroy(symtab);
3817 for (c = commands; c < &commands[n_commands]; c++) {
3818 ds_destroy(&c->output);
3819 table_destroy(c->table);
3825 static const struct vtep_ctl_command_syntax all_commands[] = {
3826 /* Physical Switch commands. */
3827 {"add-ps", 1, 1, pre_get_info, cmd_add_ps, NULL, "--may-exist", RW},
3828 {"del-ps", 1, 1, pre_get_info, cmd_del_ps, NULL, "--if-exists", RW},
3829 {"list-ps", 0, 0, pre_get_info, cmd_list_ps, NULL, "", RO},
3830 {"ps-exists", 1, 1, pre_get_info, cmd_ps_exists, NULL, "", RO},
3832 /* Port commands. */
3833 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3834 {"add-port", 2, 2, pre_get_info, cmd_add_port, NULL, "--may-exist",
3836 {"del-port", 2, 2, pre_get_info, cmd_del_port, NULL, "--if-exists", RW},
3838 /* Logical Switch commands. */
3839 {"add-ls", 1, 1, pre_get_info, cmd_add_ls, NULL, "--may-exist", RW},
3840 {"del-ls", 1, 1, pre_get_info, cmd_del_ls, NULL, "--if-exists", RW},
3841 {"list-ls", 0, 0, pre_get_info, cmd_list_ls, NULL, "", RO},
3842 {"ls-exists", 1, 1, pre_get_info, cmd_ls_exists, NULL, "", RO},
3843 {"list-bindings", 2, 2, pre_get_info, cmd_list_bindings, NULL, "", RO},
3844 {"bind-ls", 4, 4, pre_get_info, cmd_bind_ls, NULL, "", RO},
3845 {"unbind-ls", 3, 3, pre_get_info, cmd_unbind_ls, NULL, "", RO},
3847 /* MAC binding commands. */
3848 {"add-ucast-local", 3, 4, pre_get_info, cmd_add_ucast_local, NULL, "", RW},
3849 {"del-ucast-local", 2, 2, pre_get_info, cmd_del_ucast_local, NULL, "", RW},
3850 {"add-mcast-local", 3, 4, pre_get_info, cmd_add_mcast_local, NULL, "", RW},
3851 {"del-mcast-local", 3, 4, pre_get_info, cmd_del_mcast_local, NULL, "", RW},
3852 {"clear-local-macs", 1, 1, pre_get_info, cmd_clear_local_macs, NULL, "",
3854 {"list-local-macs", 1, 1, pre_get_info, cmd_list_local_macs, NULL, "", RO},
3855 {"add-ucast-remote", 3, 4, pre_get_info, cmd_add_ucast_remote, NULL, "",
3857 {"del-ucast-remote", 2, 2, pre_get_info, cmd_del_ucast_remote, NULL, "",
3859 {"add-mcast-remote", 3, 4, pre_get_info, cmd_add_mcast_remote, NULL, "",
3861 {"del-mcast-remote", 3, 4, pre_get_info, cmd_del_mcast_remote, NULL, "",
3863 {"clear-remote-macs", 1, 1, pre_get_info, cmd_clear_remote_macs, NULL, "",
3865 {"list-remote-macs", 1, 1, pre_get_info, cmd_list_remote_macs, NULL, "",
3868 /* Manager commands. */
3869 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3870 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
3871 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3873 /* Database commands. */
3874 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
3875 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3876 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3877 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3878 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3879 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3880 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3881 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3882 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3883 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
3884 "--if-exists,--all", RW},
3885 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3888 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},