]> git.proxmox.com Git - mirror_ovs.git/blame - vtep/vtep-ctl.c
ovs-vsctl: Print schema version when called with "--version".
[mirror_ovs.git] / vtep / vtep-ctl.c
CommitLineData
ffc759c6
JP
1/*
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
19#include <ctype.h>
20#include <errno.h>
21#include <float.h>
22#include <getopt.h>
23#include <inttypes.h>
24#include <signal.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include "command-line.h"
31#include "compiler.h"
32#include "dirs.h"
33#include "dynamic-string.h"
8a777cf6 34#include "fatal-signal.h"
ffc759c6
JP
35#include "hash.h"
36#include "json.h"
37#include "ovsdb-data.h"
38#include "ovsdb-idl.h"
39#include "poll-loop.h"
40#include "process.h"
41#include "stream.h"
42#include "stream-ssl.h"
43#include "smap.h"
44#include "sset.h"
45#include "svec.h"
46#include "lib/vtep-idl.h"
47#include "table.h"
48#include "timeval.h"
49#include "util.h"
50#include "vconn.h"
51#include "vlog.h"
52
53VLOG_DEFINE_THIS_MODULE(vtep_ctl);
54
55/* vtep_ctl_fatal() also logs the error, so it is preferred in this file. */
56#define ovs_fatal please_use_vtep_ctl_fatal_instead_of_ovs_fatal
57
58struct vtep_ctl_context;
59
60/* A command supported by vtep-ctl. */
61struct vtep_ctl_command_syntax {
62 const char *name; /* e.g. "add-ps" */
63 int min_args; /* Min number of arguments following name. */
64 int max_args; /* Max number of arguments following name. */
65
66 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
67 * each column or table in ctx->idl that it uses. */
68 void (*prerequisites)(struct vtep_ctl_context *ctx);
69
70 /* Does the actual work of the command and puts the command's output, if
71 * any, in ctx->output or ctx->table.
72 *
73 * Alternatively, if some prerequisite of the command is not met and the
74 * caller should wait for something to change and then retry, it may set
75 * ctx->try_again to true. (Only the "wait-until" command currently does
76 * this.) */
77 void (*run)(struct vtep_ctl_context *ctx);
78
79 /* If nonnull, called after the transaction has been successfully
80 * committed. ctx->output is the output from the "run" function, which
81 * this function may modify and otherwise postprocess as needed. (Only the
82 * "create" command currently does any postprocessing.) */
83 void (*postprocess)(struct vtep_ctl_context *ctx);
84
85 /* A comma-separated list of supported options, e.g. "--a,--b", or the
86 * empty string if the command does not support any options. */
87 const char *options;
88 enum { RO, RW } mode; /* Does this command modify the database? */
89};
90
91struct vtep_ctl_command {
92 /* Data that remains constant after initialization. */
93 const struct vtep_ctl_command_syntax *syntax;
94 int argc;
95 char **argv;
96 struct shash options;
97
98 /* Data modified by commands. */
99 struct ds output;
100 struct table *table;
101};
102
103/* --db: The database server to contact. */
104static const char *db;
105
106/* --oneline: Write each command's output as a single line? */
107static bool oneline;
108
109/* --dry-run: Do not commit any changes. */
110static bool dry_run;
111
112/* --timeout: Time to wait for a connection to 'db'. */
113static int timeout;
114
115/* Format for table output. */
116static struct table_style table_style = TABLE_STYLE_DEFAULT;
117
118/* All supported commands. */
119static const struct vtep_ctl_command_syntax all_commands[];
120
121/* The IDL we're using and the current transaction, if any.
122 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
123 * Other code should use its context arguments. */
124static struct ovsdb_idl *the_idl;
125static struct ovsdb_idl_txn *the_idl_txn;
126
127static void vtep_ctl_exit(int status) NO_RETURN;
128static void vtep_ctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
129static char *default_db(void);
130static void usage(void) NO_RETURN;
131static void parse_options(int argc, char *argv[], struct shash *local_options);
132static bool might_write_to_db(char **argv);
133
134static struct vtep_ctl_command *parse_commands(int argc, char *argv[],
135 struct shash *local_options,
136 size_t *n_commandsp);
137static void parse_command(int argc, char *argv[], struct shash *local_options,
138 struct vtep_ctl_command *);
139static const struct vtep_ctl_command_syntax *find_command(const char *name);
140static void run_prerequisites(struct vtep_ctl_command[], size_t n_commands,
141 struct ovsdb_idl *);
142static void do_vtep_ctl(const char *args, struct vtep_ctl_command *, size_t n,
143 struct ovsdb_idl *);
144
145static const struct vtep_ctl_table_class *get_table(const char *table_name);
146static void set_column(const struct vtep_ctl_table_class *,
147 const struct ovsdb_idl_row *, const char *arg,
148 struct ovsdb_symbol_table *);
149
150static bool is_condition_satisfied(const struct vtep_ctl_table_class *,
151 const struct ovsdb_idl_row *,
152 const char *arg,
153 struct ovsdb_symbol_table *);
154
155static struct vtep_ctl_lswitch *find_lswitch(struct vtep_ctl_context *,
156 const char *name,
157 bool must_exist);
158
159int
160main(int argc, char *argv[])
161{
162 extern struct vlog_module VLM_reconnect;
163 struct ovsdb_idl *idl;
164 struct vtep_ctl_command *commands;
165 struct shash local_options;
166 unsigned int seqno;
167 size_t n_commands;
168 char *args;
169
170 set_program_name(argv[0]);
8a777cf6 171 fatal_ignore_sigpipe();
ffc759c6
JP
172 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
173 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
174 vteprec_init();
175
176 /* Log our arguments. This is often valuable for debugging systems. */
177 args = process_escape_args(argv);
178 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
179
180 /* Parse command line. */
181 shash_init(&local_options);
182 parse_options(argc, argv, &local_options);
183 commands = parse_commands(argc - optind, argv + optind, &local_options,
184 &n_commands);
185
186 if (timeout) {
187 time_alarm(timeout);
188 }
189
190 /* Initialize IDL. */
191 idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
192 run_prerequisites(commands, n_commands, idl);
193
194 /* Execute the commands.
195 *
196 * 'seqno' is the database sequence number for which we last tried to
197 * execute our transaction. There's no point in trying to commit more than
198 * once for any given sequence number, because if the transaction fails
199 * it's because the database changed and we need to obtain an up-to-date
200 * view of the database before we try the transaction again. */
201 seqno = ovsdb_idl_get_seqno(idl);
202 for (;;) {
203 ovsdb_idl_run(idl);
204
205 if (seqno != ovsdb_idl_get_seqno(idl)) {
206 seqno = ovsdb_idl_get_seqno(idl);
207 do_vtep_ctl(args, commands, n_commands, idl);
208 }
209
210 if (seqno == ovsdb_idl_get_seqno(idl)) {
211 ovsdb_idl_wait(idl);
212 poll_block();
213 }
214 }
215}
216
217static struct option *
218find_option(const char *name, struct option *options, size_t n_options)
219{
220 size_t i;
221
222 for (i = 0; i < n_options; i++) {
223 if (!strcmp(options[i].name, name)) {
224 return &options[i];
225 }
226 }
227 return NULL;
228}
229
230static struct option *
231add_option(struct option **optionsp, size_t *n_optionsp,
232 size_t *allocated_optionsp)
233{
234 if (*n_optionsp >= *allocated_optionsp) {
235 *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
236 sizeof **optionsp);
237 }
238 return &(*optionsp)[(*n_optionsp)++];
239}
240
241static void
242parse_options(int argc, char *argv[], struct shash *local_options)
243{
244 enum {
245 OPT_DB = UCHAR_MAX + 1,
246 OPT_ONELINE,
247 OPT_NO_SYSLOG,
248 OPT_DRY_RUN,
249 OPT_PEER_CA_CERT,
250 OPT_LOCAL,
251 VLOG_OPTION_ENUMS,
252 TABLE_OPTION_ENUMS
253 };
254 static const struct option global_long_options[] = {
255 {"db", required_argument, NULL, OPT_DB},
256 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
257 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
258 {"oneline", no_argument, NULL, OPT_ONELINE},
259 {"timeout", required_argument, NULL, 't'},
260 {"help", no_argument, NULL, 'h'},
261 {"version", no_argument, NULL, 'V'},
262 VLOG_LONG_OPTIONS,
263 TABLE_LONG_OPTIONS,
264 STREAM_SSL_LONG_OPTIONS,
265 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
266 {NULL, 0, NULL, 0},
267 };
268 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
269 char *tmp, *short_options;
270
271 const struct vtep_ctl_command_syntax *p;
272 struct option *options, *o;
273 size_t allocated_options;
274 size_t n_options;
275 size_t i;
276
277 tmp = long_options_to_short_options(global_long_options);
278 short_options = xasprintf("+%s", tmp);
279 free(tmp);
280
281 /* We want to parse both global and command-specific options here, but
282 * getopt_long() isn't too convenient for the job. We copy our global
283 * options into a dynamic array, then append all of the command-specific
284 * options. */
285 options = xmemdup(global_long_options, sizeof global_long_options);
286 allocated_options = ARRAY_SIZE(global_long_options);
287 n_options = n_global_long_options;
288 for (p = all_commands; p->name; p++) {
289 if (p->options[0]) {
290 char *save_ptr = NULL;
291 char *name;
292 char *s;
293
294 s = xstrdup(p->options);
295 for (name = strtok_r(s, ",", &save_ptr); name != NULL;
296 name = strtok_r(NULL, ",", &save_ptr)) {
297 char *equals;
298 int has_arg;
299
300 ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
301 name += 2;
302
303 equals = strchr(name, '=');
304 if (equals) {
305 has_arg = required_argument;
306 *equals = '\0';
307 } else {
308 has_arg = no_argument;
309 }
310
311 o = find_option(name, options, n_options);
312 if (o) {
313 ovs_assert(o - options >= n_global_long_options);
314 ovs_assert(o->has_arg == has_arg);
315 } else {
316 o = add_option(&options, &n_options, &allocated_options);
317 o->name = xstrdup(name);
318 o->has_arg = has_arg;
319 o->flag = NULL;
320 o->val = OPT_LOCAL;
321 }
322 }
323
324 free(s);
325 }
326 }
327 o = add_option(&options, &n_options, &allocated_options);
328 memset(o, 0, sizeof *o);
329
330 table_style.format = TF_LIST;
331
332 for (;;) {
333 int idx;
334 int c;
335
336 c = getopt_long(argc, argv, short_options, options, &idx);
337 if (c == -1) {
338 break;
339 }
340
341 switch (c) {
342 case OPT_DB:
343 db = optarg;
344 break;
345
346 case OPT_ONELINE:
347 oneline = true;
348 break;
349
350 case OPT_NO_SYSLOG:
351 vlog_set_levels(&VLM_vtep_ctl, VLF_SYSLOG, VLL_WARN);
352 break;
353
354 case OPT_DRY_RUN:
355 dry_run = true;
356 break;
357
358 case OPT_LOCAL:
359 if (shash_find(local_options, options[idx].name)) {
360 vtep_ctl_fatal("'%s' option specified multiple times",
361 options[idx].name);
362 }
363 shash_add_nocopy(local_options,
364 xasprintf("--%s", options[idx].name),
365 optarg ? xstrdup(optarg) : NULL);
366 break;
367
368 case 'h':
369 usage();
370
371 case 'V':
372 ovs_print_version(0, 0);
373 exit(EXIT_SUCCESS);
374
375 case 't':
376 timeout = strtoul(optarg, NULL, 10);
377 if (timeout < 0) {
378 vtep_ctl_fatal("value %s on -t or --timeout is invalid",
379 optarg);
380 }
381 break;
382
383 VLOG_OPTION_HANDLERS
384 TABLE_OPTION_HANDLERS(&table_style)
385
386 STREAM_SSL_OPTION_HANDLERS
387
388 case OPT_PEER_CA_CERT:
389 stream_ssl_set_peer_ca_cert_file(optarg);
390 break;
391
392 case '?':
393 exit(EXIT_FAILURE);
394
395 default:
396 abort();
397 }
398 }
399 free(short_options);
400
401 if (!db) {
402 db = default_db();
403 }
404
405 for (i = n_global_long_options; options[i].name; i++) {
406 free(CONST_CAST(char *, options[i].name));
407 }
408 free(options);
409}
410
411static struct vtep_ctl_command *
412parse_commands(int argc, char *argv[], struct shash *local_options,
413 size_t *n_commandsp)
414{
415 struct vtep_ctl_command *commands;
416 size_t n_commands, allocated_commands;
417 int i, start;
418
419 commands = NULL;
420 n_commands = allocated_commands = 0;
421
422 for (start = i = 0; i <= argc; i++) {
423 if (i == argc || !strcmp(argv[i], "--")) {
424 if (i > start) {
425 if (n_commands >= allocated_commands) {
426 struct vtep_ctl_command *c;
427
428 commands = x2nrealloc(commands, &allocated_commands,
429 sizeof *commands);
430 for (c = commands; c < &commands[n_commands]; c++) {
431 shash_moved(&c->options);
432 }
433 }
434 parse_command(i - start, &argv[start], local_options,
435 &commands[n_commands++]);
436 } else if (!shash_is_empty(local_options)) {
437 vtep_ctl_fatal("missing command name (use --help for help)");
438 }
439 start = i + 1;
440 }
441 }
442 if (!n_commands) {
443 vtep_ctl_fatal("missing command name (use --help for help)");
444 }
445 *n_commandsp = n_commands;
446 return commands;
447}
448
449static void
450parse_command(int argc, char *argv[], struct shash *local_options,
451 struct vtep_ctl_command *command)
452{
453 const struct vtep_ctl_command_syntax *p;
454 struct shash_node *node;
455 int n_arg;
456 int i;
457
458 shash_init(&command->options);
459 shash_swap(local_options, &command->options);
460 for (i = 0; i < argc; i++) {
461 const char *option = argv[i];
462 const char *equals;
463 char *key, *value;
464
465 if (option[0] != '-') {
466 break;
467 }
468
469 equals = strchr(option, '=');
470 if (equals) {
471 key = xmemdup0(option, equals - option);
472 value = xstrdup(equals + 1);
473 } else {
474 key = xstrdup(option);
475 value = NULL;
476 }
477
478 if (shash_find(&command->options, key)) {
479 vtep_ctl_fatal("'%s' option specified multiple times", argv[i]);
480 }
481 shash_add_nocopy(&command->options, key, value);
482 }
483 if (i == argc) {
484 vtep_ctl_fatal("missing command name (use --help for help)");
485 }
486
487 p = find_command(argv[i]);
488 if (!p) {
489 vtep_ctl_fatal("unknown command '%s'; use --help for help", argv[i]);
490 }
491
492 SHASH_FOR_EACH (node, &command->options) {
493 const char *s = strstr(p->options, node->name);
494 int end = s ? s[strlen(node->name)] : EOF;
495
496 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
497 vtep_ctl_fatal("'%s' command has no '%s' option",
498 argv[i], node->name);
499 }
500 if ((end == '=') != (node->data != NULL)) {
501 if (end == '=') {
502 vtep_ctl_fatal("missing argument to '%s' option on '%s' "
503 "command", node->name, argv[i]);
504 } else {
505 vtep_ctl_fatal("'%s' option on '%s' does not accept an "
506 "argument", node->name, argv[i]);
507 }
508 }
509 }
510
511 n_arg = argc - i - 1;
512 if (n_arg < p->min_args) {
513 vtep_ctl_fatal("'%s' command requires at least %d arguments",
514 p->name, p->min_args);
515 } else if (n_arg > p->max_args) {
516 int j;
517
518 for (j = i + 1; j < argc; j++) {
519 if (argv[j][0] == '-') {
520 vtep_ctl_fatal("'%s' command takes at most %d arguments "
521 "(note that options must precede command "
522 "names and follow a \"--\" argument)",
523 p->name, p->max_args);
524 }
525 }
526
527 vtep_ctl_fatal("'%s' command takes at most %d arguments",
528 p->name, p->max_args);
529 }
530
531 command->syntax = p;
532 command->argc = n_arg + 1;
533 command->argv = &argv[i];
534}
535
536/* Returns the "struct vtep_ctl_command_syntax" for a given command 'name', or a
537 * null pointer if there is none. */
538static const struct vtep_ctl_command_syntax *
539find_command(const char *name)
540{
541 static struct shash commands = SHASH_INITIALIZER(&commands);
542
543 if (shash_is_empty(&commands)) {
544 const struct vtep_ctl_command_syntax *p;
545
546 for (p = all_commands; p->name; p++) {
547 shash_add_assert(&commands, p->name, p);
548 }
549 }
550
551 return shash_find_data(&commands, name);
552}
553
554static void
555vtep_ctl_fatal(const char *format, ...)
556{
557 char *message;
558 va_list args;
559
560 va_start(args, format);
561 message = xvasprintf(format, args);
562 va_end(args);
563
564 vlog_set_levels(&VLM_vtep_ctl, VLF_CONSOLE, VLL_OFF);
565 VLOG_ERR("%s", message);
566 ovs_error(0, "%s", message);
567 vtep_ctl_exit(EXIT_FAILURE);
568}
569
570/* Frees the current transaction and the underlying IDL and then calls
571 * exit(status).
572 *
573 * Freeing the transaction and the IDL is not strictly necessary, but it makes
574 * for a clean memory leak report from valgrind in the normal case. That makes
575 * it easier to notice real memory leaks. */
576static void
577vtep_ctl_exit(int status)
578{
579 if (the_idl_txn) {
580 ovsdb_idl_txn_abort(the_idl_txn);
581 ovsdb_idl_txn_destroy(the_idl_txn);
582 }
583 ovsdb_idl_destroy(the_idl);
584 exit(status);
585}
586
587static void
588usage(void)
589{
590 printf("\
591%s: VTEP configuration utility\n\
592usage: %s [OPTIONS] COMMAND [ARG...]\n\
593\n\
594Manager commands:\n\
595 get-manager print the managers\n\
596 del-manager delete the managers\n\
597 set-manager TARGET... set the list of managers to TARGET...\n\
598\n\
599Physical Switch commands:\n\
600 add-ps PS create a new physical switch named PS\n\
601 del-ps PS delete PS and all of its ports\n\
602 list-ps print the names of all the physical switches\n\
603 ps-exists PS exit 2 if PS does not exist\n\
604\n\
605Port commands:\n\
606 list-ports PS print the names of all the ports on PS\n\
607 add-port PS PORT add network device PORT to PS\n\
608 del-port PS PORT delete PORT from PS\n\
609\n\
610Logical Switch commands:\n\
611 add-ls LS create a new logical switch named LS\n\
612 del-ls LS delete LS and all of its ports\n\
613 list-ls print the names of all the logical switches\n\
614 ls-exists LS exit 2 if LS does not exist\n\
615 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
616 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
617 list-bindings PS PORT list bindings for PORT on PS\n\
618\n\
619MAC binding commands:\n\
620 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
621 del-ucast-local LS MAC del ucast local entry from LS\n\
622 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
623 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
624 clear-local-macs LS clear local mac entries\n\
625 list-local-macs LS list local mac entries\n\
626 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
627 del-ucast-remote LS MAC del ucast remote entry from LS\n\
628 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
629 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
630 clear-remote-macs LS clear remote mac entries\n\
631 list-remote-macs LS list remote mac entries\n\
632\n\
633Database commands:\n\
634 list TBL [REC] list RECord (or all records) in TBL\n\
635 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
636 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
637 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
638 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
639 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
640 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
641 create TBL COL[:KEY]=VALUE create and initialize new record\n\
642 destroy TBL REC delete RECord from TBL\n\
643 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
644Potentially unsafe database commands require --force option.\n\
645\n\
646Options:\n\
647 --db=DATABASE connect to DATABASE\n\
648 (default: %s)\n\
649 -t, --timeout=SECS wait at most SECS seconds\n\
650 --dry-run do not commit changes to database\n\
651 --oneline print exactly one line of output per command\n",
652 program_name, program_name, default_db());
653 vlog_usage();
654 printf("\
655 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
656 stream_usage("database", true, true, false);
657 printf("\n\
658Other options:\n\
659 -h, --help display this help message\n\
660 -V, --version display version information\n");
661 exit(EXIT_SUCCESS);
662}
663
664static char *
665default_db(void)
666{
667 static char *def;
668 if (!def) {
669 def = xasprintf("unix:%s/db.sock", ovs_rundir());
670 }
671 return def;
672}
673
674/* Returns true if it looks like this set of arguments might modify the
675 * database, otherwise false. (Not very smart, so it's prone to false
676 * positives.) */
677static bool
678might_write_to_db(char **argv)
679{
680 for (; *argv; argv++) {
681 const struct vtep_ctl_command_syntax *p = find_command(*argv);
682 if (p && p->mode == RW) {
683 return true;
684 }
685 }
686 return false;
687}
688\f
689struct vtep_ctl_context {
690 /* Read-only. */
691 int argc;
692 char **argv;
693 struct shash options;
694
695 /* Modifiable state. */
696 struct ds output;
697 struct table *table;
698 struct ovsdb_idl *idl;
699 struct ovsdb_idl_txn *txn;
700 struct ovsdb_symbol_table *symtab;
701 const struct vteprec_global *vtep_global;
702 bool verified_ports;
703
704 /* A cache of the contents of the database.
705 *
706 * A command that needs to use any of this information must first
707 * call vtep_ctl_context_populate_cache(). A command that changes
708 * anything that could invalidate the cache must either call
709 * vtep_ctl_context_invalidate_cache() or manually update the cache
710 * to maintain its correctness. */
711 bool cache_valid;
712 struct shash pswitches; /* Maps from physical switch name to
713 * struct vtep_ctl_pswitch. */
714 struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
715
716 struct shash lswitches; /* Maps from logical switch name to
717 * struct vtep_ctl_lswitch. */
718 struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
719 * struct vteprec_physical_locator. */
720
721 /* A command may set this member to true if some prerequisite is not met
722 * and the caller should wait for something to change and then retry. */
723 bool try_again;
724};
725
726struct vtep_ctl_pswitch {
727 const struct vteprec_physical_switch *ps_cfg;
728 char *name;
729 struct list ports; /* Contains "struct vteprec_physical_port"s. */
730};
731
732struct vtep_ctl_port {
733 struct list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
734 const struct vteprec_physical_port *port_cfg;
735 struct vtep_ctl_pswitch *ps;
736 struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
737};
738
739struct vtep_ctl_lswitch {
740 const struct vteprec_logical_switch *ls_cfg;
741 char *name;
742 struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
743 struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
744 struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
745 struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
746};
747
748struct vtep_ctl_mcast_mac {
749 const struct vteprec_mcast_macs_local *local_cfg;
750 const struct vteprec_mcast_macs_remote *remote_cfg;
751
752 const struct vteprec_physical_locator_set *ploc_set_cfg;
753 struct list locators; /* Contains 'vtep_ctl_ploc's. */
754};
755
756struct vtep_ctl_ploc {
757 struct list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
758 list. */
759 const struct vteprec_physical_locator *ploc_cfg;
760};
761
762static void
763verify_ports(struct vtep_ctl_context *ctx)
764{
765 if (!ctx->verified_ports) {
766 const struct vteprec_physical_switch *ps;
767
768 vteprec_global_verify_switches(ctx->vtep_global);
769 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, ctx->idl) {
770 vteprec_physical_switch_verify_ports(ps);
771 }
772
773 ctx->verified_ports = true;
774 }
775}
776
777static struct vtep_ctl_port *
778add_port_to_cache(struct vtep_ctl_context *ctx,
779 struct vtep_ctl_pswitch *ps,
780 struct vteprec_physical_port *port_cfg)
781{
782 char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
783 struct vtep_ctl_port *port;
784
785 port = xmalloc(sizeof *port);
786 list_push_back(&ps->ports, &port->ports_node);
787 port->port_cfg = port_cfg;
788 port->ps = ps;
789 shash_add(&ctx->ports, cache_name, port);
790 free(cache_name);
791 shash_init(&port->bindings);
792
793 return port;
794}
795
796static void
797del_cached_port(struct vtep_ctl_context *ctx, struct vtep_ctl_port *port)
798{
799 char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
800
801 list_remove(&port->ports_node);
802 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
803 vteprec_physical_port_delete(port->port_cfg);
804 free(cache_name);
805 free(port);
806}
807
808static struct vtep_ctl_pswitch *
809add_pswitch_to_cache(struct vtep_ctl_context *ctx,
810 struct vteprec_physical_switch *ps_cfg)
811{
812 struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
813 ps->ps_cfg = ps_cfg;
814 ps->name = xstrdup(ps_cfg->name);
815 list_init(&ps->ports);
816 shash_add(&ctx->pswitches, ps->name, ps);
817 return ps;
818}
819
820static void
821vtep_delete_pswitch(const struct vteprec_global *vtep_global,
822 const struct vteprec_physical_switch *ps)
823{
824 struct vteprec_physical_switch **pswitches;
825 size_t i, n;
826
827 pswitches = xmalloc(sizeof *vtep_global->switches
828 * vtep_global->n_switches);
829 for (i = n = 0; i < vtep_global->n_switches; i++) {
830 if (vtep_global->switches[i] != ps) {
831 pswitches[n++] = vtep_global->switches[i];
832 }
833 }
834 vteprec_global_set_switches(vtep_global, pswitches, n);
835 free(pswitches);
836}
837
838static void
839del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
840{
841 ovs_assert(list_is_empty(&ps->ports));
842 if (ps->ps_cfg) {
843 vteprec_physical_switch_delete(ps->ps_cfg);
844 vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
845 }
846 shash_find_and_delete(&ctx->pswitches, ps->name);
847 free(ps->name);
848 free(ps);
849}
850
851static struct vtep_ctl_lswitch *
852add_lswitch_to_cache(struct vtep_ctl_context *ctx,
853 const struct vteprec_logical_switch *ls_cfg)
854{
855 struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
856 ls->ls_cfg = ls_cfg;
857 ls->name = xstrdup(ls_cfg->name);
858 shash_add(&ctx->lswitches, ls->name, ls);
859 shash_init(&ls->ucast_local);
860 shash_init(&ls->ucast_remote);
861 shash_init(&ls->mcast_local);
862 shash_init(&ls->mcast_remote);
863 return ls;
864}
865
866static void
867del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
868{
869 if (ls->ls_cfg) {
870 vteprec_logical_switch_delete(ls->ls_cfg);
871 }
872 shash_find_and_delete(&ctx->lswitches, ls->name);
873 free(ls->name);
874 free(ls);
875}
876
877static void
878commit_ls_bindings(struct vtep_ctl_port *port)
879{
880 struct vteprec_logical_switch **binding_values;
881 int64_t *binding_keys;
882 size_t n_bindings;
883 struct shash_node *node;
884 int i;
885
886 n_bindings = shash_count(&port->bindings);
887 binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
888 binding_values = xmalloc(n_bindings * sizeof *binding_values);
889
890 i = 0;
891 SHASH_FOR_EACH(node, &port->bindings) {
892 struct vtep_ctl_lswitch *ls_entry = node->data;
893
894 binding_keys[i] = strtoll(node->name, NULL, 0);
895 binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
896 i++;
897 }
898
899 vteprec_physical_port_set_vlan_bindings(port->port_cfg,
900 binding_keys, binding_values,
901 n_bindings);
902 free(binding_values);
903 free(binding_keys);
904}
905
906static void
907add_ls_binding_to_cache(struct vtep_ctl_port *port,
908 const char *vlan,
909 struct vtep_ctl_lswitch *ls)
910{
911 if (shash_find(&port->bindings, vlan)) {
912 vtep_ctl_fatal("multiple bindings for vlan %s", vlan);
913 }
914
915 shash_add(&port->bindings, vlan, ls);
916}
917
918static void
919del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
920{
921 if (!shash_find(&port->bindings, vlan)) {
922 vtep_ctl_fatal("no binding for vlan %s", vlan);
923 }
924
925 shash_find_and_delete(&port->bindings, vlan);
926}
927
928static struct vteprec_physical_locator *
929find_ploc(struct vtep_ctl_context *ctx, const char *encap,
930 const char *dst_ip)
931{
932 struct vteprec_physical_locator *ploc;
933 char *name = xasprintf("%s+%s", encap, dst_ip);
934
935 ovs_assert(ctx->cache_valid);
936
937 ploc = shash_find_data(&ctx->plocs, name);
938 free(name);
939
940 return ploc;
941}
942
943static void
944add_ploc_to_cache(struct vtep_ctl_context *ctx,
945 struct vteprec_physical_locator *ploc)
946{
947 char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
948 struct vteprec_physical_locator *orig_ploc;
949
950 orig_ploc = find_ploc(ctx, ploc->encapsulation_type, ploc->dst_ip);
951 if (!orig_ploc) {
952 shash_add(&ctx->plocs, name, ploc);
953 }
954
955 free(name);
956}
957
958static void
959add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
960 struct vteprec_physical_locator *ploc_cfg)
961{
962 struct vtep_ctl_ploc *ploc;
963
964 ploc = xmalloc(sizeof *ploc);
965 ploc->ploc_cfg = ploc_cfg;
966 list_push_back(&mcast_mac->locators, &ploc->locators_node);
967}
968
969static void
970del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
971 struct vteprec_physical_locator *ploc_cfg)
972{
973 struct vtep_ctl_ploc *ploc;
974
975 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
976 if (ploc->ploc_cfg == ploc_cfg) {
977 list_remove(&ploc->locators_node);
978 free(ploc);
979 return;
980 }
981 }
982}
983
984static struct vtep_ctl_mcast_mac *
985add_mcast_mac_to_cache(struct vtep_ctl_context *ctx,
986 struct vtep_ctl_lswitch *ls, const char *mac,
987 struct vteprec_physical_locator_set *ploc_set_cfg,
988 bool local)
989{
990 struct vtep_ctl_mcast_mac *mcast_mac;
991 struct shash *mcast_shash;
992 size_t i;
993
994 mcast_mac = xmalloc(sizeof *mcast_mac);
995 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
996
997 mcast_mac->ploc_set_cfg = ploc_set_cfg;
998 list_init(&mcast_mac->locators);
999 shash_add(mcast_shash, mac, mcast_mac);
1000
1001 for (i = 0; i < ploc_set_cfg->n_locators; i++) {
1002 struct vteprec_physical_locator *ploc_cfg;
1003
1004 ploc_cfg = ploc_set_cfg->locators[i];
1005 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1006 add_ploc_to_cache(ctx, ploc_cfg);
1007 }
1008
1009 return mcast_mac;
1010}
1011
1012static void
1013vtep_ctl_context_invalidate_cache(struct vtep_ctl_context *ctx)
1014{
1015 struct shash_node *node;
1016
1017 if (!ctx->cache_valid) {
1018 return;
1019 }
1020 ctx->cache_valid = false;
1021
1022 SHASH_FOR_EACH (node, &ctx->pswitches) {
1023 struct vtep_ctl_pswitch *ps = node->data;
1024 free(ps->name);
1025 free(ps);
1026 }
1027 shash_destroy(&ctx->pswitches);
1028
1029 SHASH_FOR_EACH (node, &ctx->ports) {
1030 struct vtep_ctl_port *port = node->data;
1031 shash_destroy(&port->bindings);
1032 }
1033 shash_destroy_free_data(&ctx->ports);
1034
1035 SHASH_FOR_EACH (node, &ctx->lswitches) {
1036 struct vtep_ctl_lswitch *ls = node->data;
1037 struct shash_node *node2, *next_node2;
1038
1039 shash_destroy(&ls->ucast_local);
1040 shash_destroy(&ls->ucast_remote);
1041
1042 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
1043 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
1044 struct vtep_ctl_ploc *ploc, *next_ploc;
1045
1046 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
1047 &mcast_mac->locators) {
1048 free(ploc);
1049 }
1050 free(mcast_mac);
1051 }
1052 shash_destroy(&ls->mcast_local);
1053
1054 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
1055 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
1056 struct vtep_ctl_ploc *ploc, *next_ploc;
1057
1058 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
1059 &mcast_mac->locators) {
1060 free(ploc);
1061 }
1062 free(mcast_mac);
1063 }
1064 shash_destroy(&ls->mcast_remote);
1065
1066 free(ls->name);
1067 free(ls);
1068 }
1069 shash_destroy(&ctx->lswitches);
1070 shash_destroy(&ctx->plocs);
1071}
1072
1073static void
1074pre_get_info(struct vtep_ctl_context *ctx)
1075{
1076 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
1077
1078 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
1079 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
1080
1081 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
1082 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
1083
1084 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
1085
1086 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
1087 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
1088 ovsdb_idl_add_column(ctx->idl,
1089 &vteprec_ucast_macs_local_col_logical_switch);
1090
1091 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
1092 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
1093 ovsdb_idl_add_column(ctx->idl,
1094 &vteprec_ucast_macs_remote_col_logical_switch);
1095
1096 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
1097 ovsdb_idl_add_column(ctx->idl,
1098 &vteprec_mcast_macs_local_col_locator_set);
1099 ovsdb_idl_add_column(ctx->idl,
1100 &vteprec_mcast_macs_local_col_logical_switch);
1101
1102 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
1103 ovsdb_idl_add_column(ctx->idl,
1104 &vteprec_mcast_macs_remote_col_locator_set);
1105 ovsdb_idl_add_column(ctx->idl,
1106 &vteprec_mcast_macs_remote_col_logical_switch);
1107
1108 ovsdb_idl_add_column(ctx->idl,
1109 &vteprec_physical_locator_set_col_locators);
1110
1111 ovsdb_idl_add_column(ctx->idl,
1112 &vteprec_physical_locator_col_dst_ip);
1113 ovsdb_idl_add_column(ctx->idl,
1114 &vteprec_physical_locator_col_encapsulation_type);
1115}
1116
1117static void
1118vtep_ctl_context_populate_cache(struct vtep_ctl_context *ctx)
1119{
1120 const struct vteprec_global *vtep_global = ctx->vtep_global;
1121 const struct vteprec_logical_switch *ls_cfg;
1122 const struct vteprec_ucast_macs_local *ucast_local_cfg;
1123 const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
1124 const struct vteprec_mcast_macs_local *mcast_local_cfg;
1125 const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
1126 struct sset pswitches, ports, lswitches;
1127 size_t i;
1128
1129 if (ctx->cache_valid) {
1130 /* Cache is already populated. */
1131 return;
1132 }
1133 ctx->cache_valid = true;
1134 shash_init(&ctx->pswitches);
1135 shash_init(&ctx->ports);
1136 shash_init(&ctx->lswitches);
1137 shash_init(&ctx->plocs);
1138
1139 sset_init(&pswitches);
1140 sset_init(&ports);
1141 for (i = 0; i < vtep_global->n_switches; i++) {
1142 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1143 struct vtep_ctl_pswitch *ps;
1144 size_t j;
1145
1146 if (!sset_add(&pswitches, ps_cfg->name)) {
1147 VLOG_WARN("%s: database contains duplicate physical switch name",
1148 ps_cfg->name);
1149 continue;
1150 }
1151 ps = add_pswitch_to_cache(ctx, ps_cfg);
1152 if (!ps) {
1153 continue;
1154 }
1155
1156 for (j = 0; j < ps_cfg->n_ports; j++) {
1157 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1158
1159 if (!sset_add(&ports, port_cfg->name)) {
1160 /* Duplicate port name. (We will warn about that later.) */
1161 continue;
1162 }
1163 }
1164 }
1165 sset_destroy(&pswitches);
1166 sset_destroy(&ports);
1167
1168 sset_init(&lswitches);
1169 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
1170 if (!sset_add(&lswitches, ls_cfg->name)) {
1171 VLOG_WARN("%s: database contains duplicate logical switch name",
1172 ls_cfg->name);
1173 continue;
1174 }
1175 add_lswitch_to_cache(ctx, ls_cfg);
1176 }
1177 sset_destroy(&lswitches);
1178
1179 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
1180 struct vtep_ctl_lswitch *ls;
1181
1182 if (!ucast_local_cfg->logical_switch) {
1183 continue;
1184 }
1185 ls = find_lswitch(ctx, ucast_local_cfg->logical_switch->name, false);
1186 if (!ls) {
1187 continue;
1188 }
1189
1190 if (ucast_local_cfg->locator) {
1191 add_ploc_to_cache(ctx, ucast_local_cfg->locator);
1192 }
1193
1194 shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
1195 }
1196
1197 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
1198 struct vtep_ctl_lswitch *ls;
1199
1200 if (!ucast_remote_cfg->logical_switch) {
1201 continue;
1202 }
1203 ls = find_lswitch(ctx, ucast_remote_cfg->logical_switch->name, false);
1204 if (!ls) {
1205 continue;
1206 }
1207
1208 if (ucast_remote_cfg->locator) {
1209 add_ploc_to_cache(ctx, ucast_remote_cfg->locator);
1210 }
1211
1212 shash_add(&ls->ucast_remote, ucast_remote_cfg->MAC, ucast_remote_cfg);
1213 }
1214
1215 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg, ctx->idl) {
1216 struct vtep_ctl_mcast_mac *mcast_mac;
1217 struct vtep_ctl_lswitch *ls;
1218
1219 if (!mcast_local_cfg->logical_switch) {
1220 continue;
1221 }
1222 ls = find_lswitch(ctx, mcast_local_cfg->logical_switch->name, false);
1223 if (!ls) {
1224 continue;
1225 }
1226
1227 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mcast_local_cfg->MAC,
1228 mcast_local_cfg->locator_set,
1229 true);
1230 mcast_mac->local_cfg = mcast_local_cfg;
1231 }
1232
1233 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg, ctx->idl) {
1234 struct vtep_ctl_mcast_mac *mcast_mac;
1235 struct vtep_ctl_lswitch *ls;
1236
1237 if (!mcast_remote_cfg->logical_switch) {
1238 continue;
1239 }
1240 ls = find_lswitch(ctx, mcast_remote_cfg->logical_switch->name, false);
1241 if (!ls) {
1242 continue;
1243 }
1244
1245 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mcast_remote_cfg->MAC,
1246 mcast_remote_cfg->locator_set,
1247 false);
1248 mcast_mac->remote_cfg = mcast_remote_cfg;
1249 }
1250
1251 sset_init(&pswitches);
1252 for (i = 0; i < vtep_global->n_switches; i++) {
1253 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1254 struct vtep_ctl_pswitch *ps;
1255 size_t j;
1256
1257 if (!sset_add(&pswitches, ps_cfg->name)) {
1258 continue;
1259 }
1260 ps = shash_find_data(&ctx->pswitches, ps_cfg->name);
1261 for (j = 0; j < ps_cfg->n_ports; j++) {
1262 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1263 struct vtep_ctl_port *port;
1264 size_t k;
1265
1266 port = shash_find_data(&ctx->ports, port_cfg->name);
1267 if (port) {
1268 if (port_cfg == port->port_cfg) {
1269 VLOG_WARN("%s: port is in multiple physical switches "
1270 "(%s and %s)",
1271 port_cfg->name, ps->name, port->ps->name);
1272 } else {
1273 /* Log as an error because this violates the database's
1274 * uniqueness constraints, so the database server shouldn't
1275 * have allowed it. */
1276 VLOG_ERR("%s: database contains duplicate port name",
1277 port_cfg->name);
1278 }
1279 continue;
1280 }
1281
1282 port = add_port_to_cache(ctx, ps, port_cfg);
1283
1284 for (k = 0; k < port_cfg->n_vlan_bindings; k++) {
1285 struct vteprec_logical_switch *ls_cfg;
1286 struct vtep_ctl_lswitch *ls;
1287 char *vlan;
1288
1289 vlan = xasprintf("%"PRId64, port_cfg->key_vlan_bindings[k]);
1290 if (shash_find(&port->bindings, vlan)) {
1291 vtep_ctl_fatal("multiple bindings for vlan %s", vlan);
1292 }
1293
1294 ls_cfg = port_cfg->value_vlan_bindings[k];
1295 ls = find_lswitch(ctx, ls_cfg->name, true);
1296
1297 shash_add_nocopy(&port->bindings, vlan, ls);
1298 }
1299 }
1300 }
1301 sset_destroy(&pswitches);
1302}
1303
1304static struct vtep_ctl_pswitch *
1305find_pswitch(struct vtep_ctl_context *ctx, const char *name, bool must_exist)
1306{
1307 struct vtep_ctl_pswitch *ps;
1308
1309 ovs_assert(ctx->cache_valid);
1310
1311 ps = shash_find_data(&ctx->pswitches, name);
1312 if (must_exist && !ps) {
1313 vtep_ctl_fatal("no physical switch named %s", name);
1314 }
1315 vteprec_global_verify_switches(ctx->vtep_global);
1316 return ps;
1317}
1318
1319static struct vtep_ctl_port *
1320find_port(struct vtep_ctl_context *ctx, const char *ps_name,
1321 const char *port_name, bool must_exist)
1322{
1323 char *cache_name = xasprintf("%s+%s", ps_name, port_name);
1324 struct vtep_ctl_port *port;
1325
1326 ovs_assert(ctx->cache_valid);
1327
1328 port = shash_find_data(&ctx->ports, cache_name);
1329 if (port && !strcmp(port_name, port->ps->name)) {
1330 port = NULL;
1331 }
1332 free(cache_name);
1333 if (must_exist && !port) {
1334 vtep_ctl_fatal("no port named %s", port_name);
1335 }
1336 verify_ports(ctx);
1337 return port;
1338}
1339
1340static void
1341pswitch_insert_port(const struct vteprec_physical_switch *ps,
1342 struct vteprec_physical_port *port)
1343{
1344 struct vteprec_physical_port **ports;
1345 size_t i;
1346
1347 ports = xmalloc(sizeof *ps->ports * (ps->n_ports + 1));
1348 for (i = 0; i < ps->n_ports; i++) {
1349 ports[i] = ps->ports[i];
1350 }
1351 ports[ps->n_ports] = port;
1352 vteprec_physical_switch_set_ports(ps, ports, ps->n_ports + 1);
1353 free(ports);
1354}
1355
1356static void
1357pswitch_delete_port(const struct vteprec_physical_switch *ps,
1358 const struct vteprec_physical_port *port)
1359{
1360 struct vteprec_physical_port **ports;
1361 size_t i, n;
1362
1363 ports = xmalloc(sizeof *ps->ports * ps->n_ports);
1364 for (i = n = 0; i < ps->n_ports; i++) {
1365 if (ps->ports[i] != port) {
1366 ports[n++] = ps->ports[i];
1367 }
1368 }
1369 vteprec_physical_switch_set_ports(ps, ports, n);
1370 free(ports);
1371}
1372
1373static void
1374vtep_insert_pswitch(const struct vteprec_global *vtep_global,
1375 struct vteprec_physical_switch *ps)
1376{
1377 struct vteprec_physical_switch **pswitches;
1378 size_t i;
1379
1380 pswitches = xmalloc(sizeof *vtep_global->switches
1381 * (vtep_global->n_switches + 1));
1382 for (i = 0; i < vtep_global->n_switches; i++) {
1383 pswitches[i] = vtep_global->switches[i];
1384 }
1385 pswitches[vtep_global->n_switches] = ps;
1386 vteprec_global_set_switches(vtep_global, pswitches,
1387 vtep_global->n_switches + 1);
1388 free(pswitches);
1389}
1390
1391static void
1392cmd_add_ps(struct vtep_ctl_context *ctx)
1393{
1394 const char *ps_name = ctx->argv[1];
1395 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1396 struct vteprec_physical_switch *ps;
1397
1398 vtep_ctl_context_populate_cache(ctx);
1399 if (find_pswitch(ctx, ps_name, false)) {
1400 if (!may_exist) {
1401 vtep_ctl_fatal("cannot create physical switch %s because it "
1402 "already exists", ps_name);
1403 }
1404 return;
1405 }
1406
1407 ps = vteprec_physical_switch_insert(ctx->txn);
1408 vteprec_physical_switch_set_name(ps, ps_name);
1409
1410 vtep_insert_pswitch(ctx->vtep_global, ps);
1411
1412 vtep_ctl_context_invalidate_cache(ctx);
1413}
1414
1415static void
1416del_port(struct vtep_ctl_context *ctx, struct vtep_ctl_port *port)
1417{
1418 pswitch_delete_port(port->ps->ps_cfg, port->port_cfg);
1419 del_cached_port(ctx, port);
1420}
1421
1422static void
1423del_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
1424{
1425 struct vtep_ctl_port *port, *next_port;
1426
1427 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &ps->ports) {
1428 del_port(ctx, port);
1429 }
1430
1431 del_cached_pswitch(ctx, ps);
1432}
1433
1434static void
1435cmd_del_ps(struct vtep_ctl_context *ctx)
1436{
1437 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1438 struct vtep_ctl_pswitch *ps;
1439
1440 vtep_ctl_context_populate_cache(ctx);
1441 ps = find_pswitch(ctx, ctx->argv[1], must_exist);
1442 if (ps) {
1443 del_pswitch(ctx, ps);
1444 }
1445}
1446
1447static void
1448output_sorted(struct svec *svec, struct ds *output)
1449{
1450 const char *name;
1451 size_t i;
1452
1453 svec_sort(svec);
1454 SVEC_FOR_EACH (i, name, svec) {
1455 ds_put_format(output, "%s\n", name);
1456 }
1457}
1458
1459static void
1460cmd_list_ps(struct vtep_ctl_context *ctx)
1461{
1462 struct shash_node *node;
1463 struct svec pswitches;
1464
1465 vtep_ctl_context_populate_cache(ctx);
1466
1467 svec_init(&pswitches);
1468 SHASH_FOR_EACH (node, &ctx->pswitches) {
1469 struct vtep_ctl_pswitch *ps = node->data;
1470
1471 svec_add(&pswitches, ps->name);
1472 }
1473 output_sorted(&pswitches, &ctx->output);
1474 svec_destroy(&pswitches);
1475}
1476
1477static void
1478cmd_ps_exists(struct vtep_ctl_context *ctx)
1479{
1480 vtep_ctl_context_populate_cache(ctx);
1481 if (!find_pswitch(ctx, ctx->argv[1], false)) {
1482 vtep_ctl_exit(2);
1483 }
1484}
1485
1486static void
1487cmd_list_ports(struct vtep_ctl_context *ctx)
1488{
1489 struct vtep_ctl_pswitch *ps;
1490 struct vtep_ctl_port *port;
1491 struct svec ports;
1492
1493 vtep_ctl_context_populate_cache(ctx);
1494 ps = find_pswitch(ctx, ctx->argv[1], true);
1495 vteprec_physical_switch_verify_ports(ps->ps_cfg);
1496
1497 svec_init(&ports);
1498 LIST_FOR_EACH (port, ports_node, &ps->ports) {
1499 if (strcmp(port->port_cfg->name, ps->name)) {
1500 svec_add(&ports, port->port_cfg->name);
1501 }
1502 }
1503 output_sorted(&ports, &ctx->output);
1504 svec_destroy(&ports);
1505}
1506
1507static void
1508add_port(struct vtep_ctl_context *ctx, const char *ps_name,
1509 const char *port_name, bool may_exist)
1510{
1511 struct vtep_ctl_port *vtep_ctl_port;
1512 struct vtep_ctl_pswitch *ps;
1513 struct vteprec_physical_port *port;
1514
1515 vtep_ctl_context_populate_cache(ctx);
1516
1517 vtep_ctl_port = find_port(ctx, ps_name, port_name, false);
1518 if (vtep_ctl_port) {
1519 if (!may_exist) {
1520 vtep_ctl_fatal("cannot create a port named %s on %s because a "
1521 "port with that name already exists",
1522 port_name, ps_name);
1523 }
1524 return;
1525 }
1526
1527 ps = find_pswitch(ctx, ps_name, true);
1528
1529 port = vteprec_physical_port_insert(ctx->txn);
1530 vteprec_physical_port_set_name(port, port_name);
1531
1532 pswitch_insert_port(ps->ps_cfg, port);
1533
1534 add_port_to_cache(ctx, ps, port);
1535}
1536
1537static void
1538cmd_add_port(struct vtep_ctl_context *ctx)
1539{
1540 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1541 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist);
1542}
1543
1544static void
1545cmd_del_port(struct vtep_ctl_context *ctx)
1546{
1547 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1548 struct vtep_ctl_port *port;
1549
1550 vtep_ctl_context_populate_cache(ctx);
1551
1552 port = find_port(ctx, ctx->argv[1], ctx->argv[2], must_exist);
1553 if (port) {
1554 if (ctx->argc == 3) {
1555 struct vtep_ctl_pswitch *ps;
1556
1557 ps = find_pswitch(ctx, ctx->argv[1], true);
1558 if (port->ps != ps) {
1559 vtep_ctl_fatal("physical switch %s does not have a port %s",
1560 ctx->argv[1], ctx->argv[2]);
1561 }
1562 }
1563
1564 del_port(ctx, port);
1565 }
1566}
1567
1568static struct vtep_ctl_lswitch *
1569find_lswitch(struct vtep_ctl_context *ctx, const char *name, bool must_exist)
1570{
1571 struct vtep_ctl_lswitch *ls;
1572
1573 ovs_assert(ctx->cache_valid);
1574
1575 ls = shash_find_data(&ctx->lswitches, name);
1576 if (must_exist && !ls) {
1577 vtep_ctl_fatal("no logical switch named %s", name);
1578 }
1579 return ls;
1580}
1581
1582static void
1583cmd_add_ls(struct vtep_ctl_context *ctx)
1584{
1585 const char *ls_name = ctx->argv[1];
1586 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1587 struct vteprec_logical_switch *ls;
1588
1589 vtep_ctl_context_populate_cache(ctx);
1590 if (find_lswitch(ctx, ls_name, false)) {
1591 if (!may_exist) {
1592 vtep_ctl_fatal("cannot create logical switch %s because it "
1593 "already exists", ls_name);
1594 }
1595 return;
1596 }
1597
1598 ls = vteprec_logical_switch_insert(ctx->txn);
1599 vteprec_logical_switch_set_name(ls, ls_name);
1600
1601 vtep_ctl_context_invalidate_cache(ctx);
1602}
1603
1604static void
1605del_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
1606{
1607 del_cached_lswitch(ctx, ls);
1608}
1609
1610static void
1611cmd_del_ls(struct vtep_ctl_context *ctx)
1612{
1613 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1614 struct vtep_ctl_lswitch *ls;
1615
1616 vtep_ctl_context_populate_cache(ctx);
1617 ls = find_lswitch(ctx, ctx->argv[1], must_exist);
1618 if (ls) {
1619 del_lswitch(ctx, ls);
1620 }
1621}
1622
1623static void
1624cmd_list_ls(struct vtep_ctl_context *ctx)
1625{
1626 struct shash_node *node;
1627 struct svec lswitches;
1628
1629 vtep_ctl_context_populate_cache(ctx);
1630
1631 svec_init(&lswitches);
1632 SHASH_FOR_EACH (node, &ctx->lswitches) {
1633 struct vtep_ctl_lswitch *ls = node->data;
1634
1635 svec_add(&lswitches, ls->name);
1636 }
1637 output_sorted(&lswitches, &ctx->output);
1638 svec_destroy(&lswitches);
1639}
1640
1641static void
1642cmd_ls_exists(struct vtep_ctl_context *ctx)
1643{
1644 vtep_ctl_context_populate_cache(ctx);
1645 if (!find_lswitch(ctx, ctx->argv[1], false)) {
1646 vtep_ctl_exit(2);
1647 }
1648}
1649
1650static void
1651cmd_list_bindings(struct vtep_ctl_context *ctx)
1652{
1653 const struct shash_node *node;
1654 struct vtep_ctl_port *port;
1655 struct svec bindings;
1656
1657 vtep_ctl_context_populate_cache(ctx);
1658 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1659
1660 svec_init(&bindings);
1661 SHASH_FOR_EACH (node, &port->bindings) {
1662 struct vtep_ctl_lswitch *lswitch = node->data;
1663 char *binding;
1664
1665 binding = xasprintf("%04lld %s", strtoll(node->name, NULL, 0),
1666 lswitch->name);
1667 svec_add_nocopy(&bindings, binding);
1668 }
1669 output_sorted(&bindings, &ctx->output);
1670 svec_destroy(&bindings);
1671}
1672
1673static void
1674cmd_bind_ls(struct vtep_ctl_context *ctx)
1675{
1676 struct vtep_ctl_lswitch *ls;
1677 struct vtep_ctl_port *port;
1678 const char *vlan;
1679
1680 vtep_ctl_context_populate_cache(ctx);
1681
1682 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1683 vlan = ctx->argv[3];
1684 ls = find_lswitch(ctx, ctx->argv[4], true);
1685
1686 add_ls_binding_to_cache(port, vlan, ls);
1687 commit_ls_bindings(port);
1688
1689 vtep_ctl_context_invalidate_cache(ctx);
1690}
1691
1692static void
1693cmd_unbind_ls(struct vtep_ctl_context *ctx)
1694{
1695 struct vtep_ctl_port *port;
1696 const char *vlan;
1697
1698 vtep_ctl_context_populate_cache(ctx);
1699
1700 port = find_port(ctx, ctx->argv[1], ctx->argv[2], true);
1701 vlan = ctx->argv[3];
1702
1703 del_cached_ls_binding(port, vlan);
1704 commit_ls_bindings(port);
1705
1706 vtep_ctl_context_invalidate_cache(ctx);
1707}
1708
1709static void
1710add_ucast_entry(struct vtep_ctl_context *ctx, bool local)
1711{
1712 struct vtep_ctl_lswitch *ls;
1713 const char *mac;
1714 const char *encap;
1715 const char *dst_ip;
1716 struct vteprec_physical_locator *ploc_cfg;
1717
1718 vtep_ctl_context_populate_cache(ctx);
1719
1720 ls = find_lswitch(ctx, ctx->argv[1], true);
1721 mac = ctx->argv[2];
1722
1723 if (ctx->argc == 4) {
1724 encap = "vxlan_over_ipv4";
1725 dst_ip = ctx->argv[3];
1726 } else {
1727 encap = ctx->argv[3];
1728 dst_ip = ctx->argv[4];
1729 }
1730
1731 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1732 if (!ploc_cfg) {
1733 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1734 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1735 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1736
1737 add_ploc_to_cache(ctx, ploc_cfg);
1738 }
1739
1740 if (local) {
1741 struct vteprec_ucast_macs_local *ucast_cfg;
1742
1743 ucast_cfg = shash_find_data(&ls->ucast_local, mac);
1744 if (!ucast_cfg) {
1745 ucast_cfg = vteprec_ucast_macs_local_insert(ctx->txn);
1746 vteprec_ucast_macs_local_set_MAC(ucast_cfg, mac);
1747 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg, ls->ls_cfg);
1748 shash_add(&ls->ucast_local, mac, ucast_cfg);
1749 }
1750 vteprec_ucast_macs_local_set_locator(ucast_cfg, ploc_cfg);
1751 } else {
1752 struct vteprec_ucast_macs_remote *ucast_cfg;
1753
1754 ucast_cfg = shash_find_data(&ls->ucast_remote, mac);
1755 if (!ucast_cfg) {
1756 ucast_cfg = vteprec_ucast_macs_remote_insert(ctx->txn);
1757 vteprec_ucast_macs_remote_set_MAC(ucast_cfg, mac);
1758 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg, ls->ls_cfg);
1759 shash_add(&ls->ucast_remote, mac, ucast_cfg);
1760 }
1761 vteprec_ucast_macs_remote_set_locator(ucast_cfg, ploc_cfg);
1762 }
1763
1764 vtep_ctl_context_invalidate_cache(ctx);
1765}
1766
1767static void
1768cmd_add_ucast_local(struct vtep_ctl_context *ctx)
1769{
1770 add_ucast_entry(ctx, true);
1771}
1772
1773static void
1774cmd_add_ucast_remote(struct vtep_ctl_context *ctx)
1775{
1776 add_ucast_entry(ctx, false);
1777}
1778
1779static void
1780del_ucast_entry(struct vtep_ctl_context *ctx, bool local)
1781{
1782 struct vtep_ctl_lswitch *ls;
1783 struct shash *ucast_shash;
1784 struct shash_node *node;
1785
1786 vtep_ctl_context_populate_cache(ctx);
1787
1788 ls = find_lswitch(ctx, ctx->argv[1], true);
1789 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1790
1791 node = shash_find(ucast_shash, ctx->argv[2]);
1792 if (!node) {
1793 return;
1794 }
1795
1796 if (local) {
1797 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1798 vteprec_ucast_macs_local_delete(ucast_cfg);
1799 } else {
1800 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1801 vteprec_ucast_macs_remote_delete(ucast_cfg);
1802 }
1803 shash_delete(ucast_shash, node);
1804
1805 vtep_ctl_context_invalidate_cache(ctx);
1806}
1807
1808static void
1809cmd_del_ucast_local(struct vtep_ctl_context *ctx)
1810{
1811 del_ucast_entry(ctx, true);
1812}
1813
1814static void
1815cmd_del_ucast_remote(struct vtep_ctl_context *ctx)
1816{
1817 del_ucast_entry(ctx, false);
1818}
1819
1820static void
1821commit_mcast_entries(struct vtep_ctl_mcast_mac *mcast_mac)
1822{
1823 struct vtep_ctl_ploc *ploc;
1824 struct vteprec_physical_locator **locators = NULL;
1825 size_t n_locators;
1826 int i;
1827
1828 n_locators = list_size(&mcast_mac->locators);
1829 ovs_assert(n_locators);
1830
1831 locators = xmalloc(n_locators * sizeof *locators);
1832
1833 i = 0;
1834 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
1835 locators[i] = (struct vteprec_physical_locator *)ploc->ploc_cfg;
1836 i++;
1837 }
1838
1839 vteprec_physical_locator_set_set_locators(mcast_mac->ploc_set_cfg,
1840 locators,
1841 n_locators);
1842
1843 free(locators);
1844}
1845
1846static void
1847add_mcast_entry(struct vtep_ctl_context *ctx,
1848 struct vtep_ctl_lswitch *ls, const char *mac,
1849 const char *encap, const char *dst_ip, bool local)
1850{
1851 struct shash *mcast_shash;
1852 struct vtep_ctl_mcast_mac *mcast_mac;
1853 struct vteprec_physical_locator *ploc_cfg;
1854 struct vteprec_physical_locator_set *ploc_set_cfg;
1855
1856 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1857
1858 /* Physical locator sets are immutable, so allocate a new one. */
1859 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1860
1861 mcast_mac = shash_find_data(mcast_shash, mac);
1862 if (!mcast_mac) {
1863 mcast_mac = add_mcast_mac_to_cache(ctx, ls, mac, ploc_set_cfg, local);
1864
1865 if (local) {
1866 mcast_mac->local_cfg = vteprec_mcast_macs_local_insert(ctx->txn);
1867 vteprec_mcast_macs_local_set_MAC(mcast_mac->local_cfg, mac);
1868 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1869 ploc_set_cfg);
1870 vteprec_mcast_macs_local_set_logical_switch(mcast_mac->local_cfg,
1871 ls->ls_cfg);
1872 mcast_mac->remote_cfg = NULL;
1873 } else {
1874 mcast_mac->remote_cfg = vteprec_mcast_macs_remote_insert(ctx->txn);
1875 vteprec_mcast_macs_remote_set_MAC(mcast_mac->remote_cfg, mac);
1876 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1877 ploc_set_cfg);
1878 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac->remote_cfg,
1879 ls->ls_cfg);
1880 mcast_mac->local_cfg = NULL;
1881 }
1882 } else {
1883 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1884 if (local) {
1885 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1886 ploc_set_cfg);
1887 } else {
1888 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1889 ploc_set_cfg);
1890 }
1891 }
1892
1893 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1894 if (!ploc_cfg) {
1895 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1896 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1897 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1898
1899 add_ploc_to_cache(ctx, ploc_cfg);
1900 }
1901
1902 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1903 commit_mcast_entries(mcast_mac);
1904}
1905
1906static void
1907del_mcast_entry(struct vtep_ctl_context *ctx,
1908 struct vtep_ctl_lswitch *ls, const char *mac,
1909 const char *encap, const char *dst_ip, bool local)
1910{
1911 struct vtep_ctl_mcast_mac *mcast_mac;
1912 struct shash *mcast_shash;
1913 struct vteprec_physical_locator *ploc_cfg;
1914 struct vteprec_physical_locator_set *ploc_set_cfg;
1915
1916 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1917
1918 mcast_mac = shash_find_data(mcast_shash, mac);
1919 if (!mcast_mac) {
1920 return;
1921 }
1922
1923 ploc_cfg = find_ploc(ctx, encap, dst_ip);
1924 if (!ploc_cfg) {
1925 /* Couldn't find the physical locator, so just ignore. */
1926 return;
1927 }
1928
1929 /* Physical locator sets are immutable, so allocate a new one. */
1930 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1931 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1932
1933 del_ploc_from_mcast_mac(mcast_mac, ploc_cfg);
1934 if (list_is_empty(&mcast_mac->locators)) {
1935 struct shash_node *node = shash_find(mcast_shash, mac);
1936
1937 vteprec_physical_locator_set_delete(ploc_set_cfg);
1938
1939 if (local) {
1940 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1941 } else {
1942 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1943 }
1944
1945 free(node->data);
1946 shash_delete(mcast_shash, node);
1947 } else {
1948 if (local) {
1949 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1950 ploc_set_cfg);
1951 } else {
1952 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1953 ploc_set_cfg);
1954 }
1955 commit_mcast_entries(mcast_mac);
1956 }
1957}
1958
1959static void
1960add_del_mcast_entry(struct vtep_ctl_context *ctx, bool add, bool local)
1961{
1962 struct vtep_ctl_lswitch *ls;
1963 const char *mac;
1964 const char *encap;
1965 const char *dst_ip;
1966
1967 vtep_ctl_context_populate_cache(ctx);
1968
1969 ls = find_lswitch(ctx, ctx->argv[1], true);
1970 mac = ctx->argv[2];
1971
1972 if (ctx->argc == 4) {
1973 encap = "vxlan_over_ipv4";
1974 dst_ip = ctx->argv[3];
1975 } else {
1976 encap = ctx->argv[3];
1977 dst_ip = ctx->argv[4];
1978 }
1979
1980 if (add) {
1981 add_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1982 } else {
1983 del_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1984 }
1985
1986 vtep_ctl_context_invalidate_cache(ctx);
1987}
1988
1989static void
1990cmd_add_mcast_local(struct vtep_ctl_context *ctx)
1991{
1992 add_del_mcast_entry(ctx, true, true);
1993}
1994
1995static void
1996cmd_add_mcast_remote(struct vtep_ctl_context *ctx)
1997{
1998 add_del_mcast_entry(ctx, true, false);
1999}
2000
2001static void
2002cmd_del_mcast_local(struct vtep_ctl_context *ctx)
2003{
2004 add_del_mcast_entry(ctx, false, true);
2005}
2006
2007static void
2008cmd_del_mcast_remote(struct vtep_ctl_context *ctx)
2009{
2010 add_del_mcast_entry(ctx, false, false);
2011}
2012
2013static void
2014clear_macs(struct vtep_ctl_context *ctx, bool local)
2015{
2016 struct vtep_ctl_lswitch *ls;
2017 const struct shash_node *node;
2018 struct shash *ucast_shash;
2019 struct shash *mcast_shash;
2020
2021 vtep_ctl_context_populate_cache(ctx);
2022 ls = find_lswitch(ctx, ctx->argv[1], true);
2023
2024 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2025 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2026
2027 SHASH_FOR_EACH (node, ucast_shash) {
2028 if (local) {
2029 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
2030 vteprec_ucast_macs_local_delete(ucast_cfg);
2031 } else {
2032 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
2033 vteprec_ucast_macs_remote_delete(ucast_cfg);
2034 }
2035 }
2036
2037 SHASH_FOR_EACH (node, mcast_shash) {
2038 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2039 if (local) {
2040 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
2041 } else {
2042 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
2043 }
2044 }
2045
2046 vtep_ctl_context_invalidate_cache(ctx);
2047}
2048
2049static void
2050cmd_clear_local_macs(struct vtep_ctl_context *ctx)
2051{
2052 clear_macs(ctx, true);
2053}
2054
2055static void
2056cmd_clear_remote_macs(struct vtep_ctl_context *ctx)
2057{
2058 clear_macs(ctx, false);
2059}
2060
2061static void
2062list_macs(struct vtep_ctl_context *ctx, bool local)
2063{
2064 struct vtep_ctl_lswitch *ls;
2065 const struct shash_node *node;
2066 struct shash *ucast_shash;
2067 struct svec ucast_macs;
2068 struct shash *mcast_shash;
2069 struct svec mcast_macs;
2070
2071 vtep_ctl_context_populate_cache(ctx);
2072 ls = find_lswitch(ctx, ctx->argv[1], true);
2073
2074 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2075 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2076
2077 svec_init(&ucast_macs);
2078 SHASH_FOR_EACH (node, ucast_shash) {
2079 struct vteprec_ucast_macs_local *ucast_local = node->data;
2080 struct vteprec_ucast_macs_remote *ucast_remote = node->data;
2081 struct vteprec_physical_locator *ploc_cfg;
2082 char *entry;
2083
2084 ploc_cfg = local ? ucast_local->locator : ucast_remote->locator;
2085
2086 entry = xasprintf(" %s -> %s/%s", node->name,
2087 ploc_cfg->encapsulation_type, ploc_cfg->dst_ip);
2088 svec_add_nocopy(&ucast_macs, entry);
2089 }
2090 ds_put_format(&ctx->output, "ucast-mac-%s\n", local ? "local" : "remote");
2091 output_sorted(&ucast_macs, &ctx->output);
2092 ds_put_char(&ctx->output, '\n');
2093 svec_destroy(&ucast_macs);
2094
2095 svec_init(&mcast_macs);
2096 SHASH_FOR_EACH (node, mcast_shash) {
2097 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2098 struct vtep_ctl_ploc *ploc;
2099 char *entry;
2100
2101 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
2102 entry = xasprintf(" %s -> %s/%s", node->name,
2103 ploc->ploc_cfg->encapsulation_type,
2104 ploc->ploc_cfg->dst_ip);
2105 svec_add_nocopy(&mcast_macs, entry);
2106 }
2107 }
2108 ds_put_format(&ctx->output, "mcast-mac-%s\n", local ? "local" : "remote");
2109 output_sorted(&mcast_macs, &ctx->output);
2110 ds_put_char(&ctx->output, '\n');
2111 svec_destroy(&mcast_macs);
2112}
2113
2114static void
2115cmd_list_local_macs(struct vtep_ctl_context *ctx)
2116{
2117 list_macs(ctx, true);
2118}
2119
2120static void
2121cmd_list_remote_macs(struct vtep_ctl_context *ctx)
2122{
2123 list_macs(ctx, false);
2124}
2125
2126static void
2127verify_managers(const struct vteprec_global *vtep_global)
2128{
2129 size_t i;
2130
2131 vteprec_global_verify_managers(vtep_global);
2132
2133 for (i = 0; i < vtep_global->n_managers; ++i) {
2134 const struct vteprec_manager *mgr = vtep_global->managers[i];
2135
2136 vteprec_manager_verify_target(mgr);
2137 }
2138}
2139
2140static void
2141pre_manager(struct vtep_ctl_context *ctx)
2142{
2143 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_managers);
2144 ovsdb_idl_add_column(ctx->idl, &vteprec_manager_col_target);
2145}
2146
2147static void
2148cmd_get_manager(struct vtep_ctl_context *ctx)
2149{
2150 const struct vteprec_global *vtep_global = ctx->vtep_global;
2151 struct svec targets;
2152 size_t i;
2153
2154 verify_managers(vtep_global);
2155
2156 /* Print the targets in sorted order for reproducibility. */
2157 svec_init(&targets);
2158
2159 for (i = 0; i < vtep_global->n_managers; i++) {
2160 svec_add(&targets, vtep_global->managers[i]->target);
2161 }
2162
2163 svec_sort_unique(&targets);
2164 for (i = 0; i < targets.n; i++) {
2165 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2166 }
2167 svec_destroy(&targets);
2168}
2169
2170static void
2171delete_managers(const struct vtep_ctl_context *ctx)
2172{
2173 const struct vteprec_global *vtep_global = ctx->vtep_global;
2174 size_t i;
2175
2176 /* Delete Manager rows pointed to by 'managers' column. */
2177 for (i = 0; i < vtep_global->n_managers; i++) {
2178 vteprec_manager_delete(vtep_global->managers[i]);
2179 }
2180
2181 /* Delete 'Manager' row refs in 'managers' column. */
2182 vteprec_global_set_managers(vtep_global, NULL, 0);
2183}
2184
2185static void
2186cmd_del_manager(struct vtep_ctl_context *ctx)
2187{
2188 const struct vteprec_global *vtep_global = ctx->vtep_global;
2189
2190 verify_managers(vtep_global);
2191 delete_managers(ctx);
2192}
2193
2194static void
2195insert_managers(struct vtep_ctl_context *ctx, char *targets[], size_t n)
2196{
2197 struct vteprec_manager **managers;
2198 size_t i;
2199
2200 /* Insert each manager in a new row in Manager table. */
2201 managers = xmalloc(n * sizeof *managers);
2202 for (i = 0; i < n; i++) {
2203 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2204 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2205 }
2206 managers[i] = vteprec_manager_insert(ctx->txn);
2207 vteprec_manager_set_target(managers[i], targets[i]);
2208 }
2209
2210 /* Store uuids of new Manager rows in 'managers' column. */
2211 vteprec_global_set_managers(ctx->vtep_global, managers, n);
2212 free(managers);
2213}
2214
2215static void
2216cmd_set_manager(struct vtep_ctl_context *ctx)
2217{
2218 const size_t n = ctx->argc - 1;
2219
2220 verify_managers(ctx->vtep_global);
2221 delete_managers(ctx);
2222 insert_managers(ctx, &ctx->argv[1], n);
2223}
2224
2225/* Parameter commands. */
2226
2227struct vtep_ctl_row_id {
2228 const struct ovsdb_idl_table_class *table;
2229 const struct ovsdb_idl_column *name_column;
2230 const struct ovsdb_idl_column *uuid_column;
2231};
2232
2233struct vtep_ctl_table_class {
2234 struct ovsdb_idl_table_class *class;
2235 struct vtep_ctl_row_id row_ids[2];
2236};
2237
2238static const struct vtep_ctl_table_class tables[] = {
2239 {&vteprec_table_global,
2240 {{&vteprec_table_global, NULL, NULL},
2241 {NULL, NULL, NULL}}},
2242
2243 {&vteprec_table_logical_binding_stats,
2244 {{NULL, NULL, NULL},
2245 {NULL, NULL, NULL}}},
2246
2247 {&vteprec_table_logical_switch,
2248 {{&vteprec_table_logical_switch, &vteprec_logical_switch_col_name, NULL},
2249 {NULL, NULL, NULL}}},
2250
2251 {&vteprec_table_ucast_macs_local,
2252 {{NULL, NULL, NULL},
2253 {NULL, NULL, NULL}}},
2254
2255 {&vteprec_table_ucast_macs_remote,
2256 {{NULL, NULL, NULL},
2257 {NULL, NULL, NULL}}},
2258
2259 {&vteprec_table_mcast_macs_local,
2260 {{NULL, NULL, NULL},
2261 {NULL, NULL, NULL}}},
2262
2263 {&vteprec_table_mcast_macs_remote,
2264 {{NULL, NULL, NULL},
2265 {NULL, NULL, NULL}}},
2266
2267 {&vteprec_table_manager,
2268 {{&vteprec_table_manager, &vteprec_manager_col_target, NULL},
2269 {NULL, NULL, NULL}}},
2270
2271 {&vteprec_table_physical_locator,
2272 {{NULL, NULL, NULL},
2273 {NULL, NULL, NULL}}},
2274
2275 {&vteprec_table_physical_locator_set,
2276 {{NULL, NULL, NULL},
2277 {NULL, NULL, NULL}}},
2278
2279 {&vteprec_table_physical_port,
2280 {{&vteprec_table_physical_port, &vteprec_physical_port_col_name, NULL},
2281 {NULL, NULL, NULL}}},
2282
2283 {&vteprec_table_physical_switch,
2284 {{&vteprec_table_physical_switch, &vteprec_physical_switch_col_name, NULL},
2285 {NULL, NULL, NULL}}},
2286
2287 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2288};
2289
2290static void
2291die_if_error(char *error)
2292{
2293 if (error) {
2294 vtep_ctl_fatal("%s", error);
2295 }
2296}
2297
2298static int
2299to_lower_and_underscores(unsigned c)
2300{
2301 return c == '-' ? '_' : tolower(c);
2302}
2303
2304static unsigned int
2305score_partial_match(const char *name, const char *s)
2306{
2307 int score;
2308
2309 if (!strcmp(name, s)) {
2310 return UINT_MAX;
2311 }
2312 for (score = 0; ; score++, name++, s++) {
2313 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2314 break;
2315 } else if (*name == '\0') {
2316 return UINT_MAX - 1;
2317 }
2318 }
2319 return *s == '\0' ? score : 0;
2320}
2321
2322static const struct vtep_ctl_table_class *
2323get_table(const char *table_name)
2324{
2325 const struct vtep_ctl_table_class *table;
2326 const struct vtep_ctl_table_class *best_match = NULL;
2327 unsigned int best_score = 0;
2328
2329 for (table = tables; table->class; table++) {
2330 unsigned int score = score_partial_match(table->class->name,
2331 table_name);
2332 if (score > best_score) {
2333 best_match = table;
2334 best_score = score;
2335 } else if (score == best_score) {
2336 best_match = NULL;
2337 }
2338 }
2339 if (best_match) {
2340 return best_match;
2341 } else if (best_score) {
2342 vtep_ctl_fatal("multiple table names match \"%s\"", table_name);
2343 } else {
2344 vtep_ctl_fatal("unknown table \"%s\"", table_name);
2345 }
2346}
2347
2348static const struct vtep_ctl_table_class *
2349pre_get_table(struct vtep_ctl_context *ctx, const char *table_name)
2350{
2351 const struct vtep_ctl_table_class *table_class;
2352 int i;
2353
2354 table_class = get_table(table_name);
2355 ovsdb_idl_add_table(ctx->idl, table_class->class);
2356
2357 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2358 const struct vtep_ctl_row_id *id = &table_class->row_ids[i];
2359 if (id->table) {
2360 ovsdb_idl_add_table(ctx->idl, id->table);
2361 }
2362 if (id->name_column) {
2363 ovsdb_idl_add_column(ctx->idl, id->name_column);
2364 }
2365 if (id->uuid_column) {
2366 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2367 }
2368 }
2369
2370 return table_class;
2371}
2372
2373static const struct ovsdb_idl_row *
2374get_row_by_id(struct vtep_ctl_context *ctx, const struct vtep_ctl_table_class *table,
2375 const struct vtep_ctl_row_id *id, const char *record_id)
2376{
2377 const struct ovsdb_idl_row *referrer, *final;
2378
2379 if (!id->table) {
2380 return NULL;
2381 }
2382
2383 if (!id->name_column) {
2384 if (strcmp(record_id, ".")) {
2385 return NULL;
2386 }
2387 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2388 if (!referrer || ovsdb_idl_next_row(referrer)) {
2389 return NULL;
2390 }
2391 } else {
2392 const struct ovsdb_idl_row *row;
2393
2394 referrer = NULL;
2395 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2396 row != NULL;
2397 row = ovsdb_idl_next_row(row))
2398 {
2399 const struct ovsdb_datum *name;
2400
2401 name = ovsdb_idl_get(row, id->name_column,
2402 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2403 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2404 if (referrer) {
2405 vtep_ctl_fatal("multiple rows in %s match \"%s\"",
2406 table->class->name, record_id);
2407 }
2408 referrer = row;
2409 }
2410 }
2411 }
2412 if (!referrer) {
2413 return NULL;
2414 }
2415
2416 final = NULL;
2417 if (id->uuid_column) {
2418 const struct ovsdb_datum *uuid;
2419
2420 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2421 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2422 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2423 if (uuid->n == 1) {
2424 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2425 &uuid->keys[0].uuid);
2426 }
2427 } else {
2428 final = referrer;
2429 }
2430
2431 return final;
2432}
2433
2434static const struct ovsdb_idl_row *
2435get_row (struct vtep_ctl_context *ctx,
2436 const struct vtep_ctl_table_class *table, const char *record_id)
2437{
2438 const struct ovsdb_idl_row *row;
2439 struct uuid uuid;
2440
2441 if (uuid_from_string(&uuid, record_id)) {
2442 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2443 } else {
2444 int i;
2445
2446 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2447 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2448 if (row) {
2449 break;
2450 }
2451 }
2452 }
2453 return row;
2454}
2455
2456static const struct ovsdb_idl_row *
2457must_get_row(struct vtep_ctl_context *ctx,
2458 const struct vtep_ctl_table_class *table, const char *record_id)
2459{
2460 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2461 if (!row) {
2462 vtep_ctl_fatal("no row \"%s\" in table %s",
2463 record_id, table->class->name);
2464 }
2465 return row;
2466}
2467
2468static char *
2469get_column(const struct vtep_ctl_table_class *table, const char *column_name,
2470 const struct ovsdb_idl_column **columnp)
2471{
2472 const struct ovsdb_idl_column *best_match = NULL;
2473 unsigned int best_score = 0;
2474 size_t i;
2475
2476 for (i = 0; i < table->class->n_columns; i++) {
2477 const struct ovsdb_idl_column *column = &table->class->columns[i];
2478 unsigned int score = score_partial_match(column->name, column_name);
2479 if (score > best_score) {
2480 best_match = column;
2481 best_score = score;
2482 } else if (score == best_score) {
2483 best_match = NULL;
2484 }
2485 }
2486
2487 *columnp = best_match;
2488 if (best_match) {
2489 return NULL;
2490 } else if (best_score) {
2491 return xasprintf("%s contains more than one column whose name "
2492 "matches \"%s\"", table->class->name, column_name);
2493 } else {
2494 return xasprintf("%s does not contain a column whose name matches "
2495 "\"%s\"", table->class->name, column_name);
2496 }
2497}
2498
2499static struct ovsdb_symbol *
2500create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2501{
2502 struct ovsdb_symbol *symbol;
2503
2504 if (id[0] != '@') {
2505 vtep_ctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2506 }
2507
2508 if (newp) {
2509 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2510 }
2511
2512 symbol = ovsdb_symbol_table_insert(symtab, id);
2513 if (symbol->created) {
2514 vtep_ctl_fatal("row id \"%s\" may only be specified on one --id option",
2515 id);
2516 }
2517 symbol->created = true;
2518 return symbol;
2519}
2520
2521static void
2522pre_get_column(struct vtep_ctl_context *ctx,
2523 const struct vtep_ctl_table_class *table, const char *column_name,
2524 const struct ovsdb_idl_column **columnp)
2525{
2526 die_if_error(get_column(table, column_name, columnp));
2527 ovsdb_idl_add_column(ctx->idl, *columnp);
2528}
2529
2530static char *
2531missing_operator_error(const char *arg, const char **allowed_operators,
2532 size_t n_allowed)
2533{
2534 struct ds s;
2535
2536 ds_init(&s);
2537 ds_put_format(&s, "%s: argument does not end in ", arg);
2538 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2539 if (n_allowed == 2) {
2540 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2541 } else if (n_allowed > 2) {
2542 size_t i;
2543
2544 for (i = 1; i < n_allowed - 1; i++) {
2545 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2546 }
2547 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2548 }
2549 ds_put_format(&s, " followed by a value.");
2550
2551 return ds_steal_cstr(&s);
2552}
2553
2554/* Breaks 'arg' apart into a number of fields in the following order:
2555 *
2556 * - The name of a column in 'table', stored into '*columnp'. The column
2557 * name may be abbreviated.
2558 *
2559 * - Optionally ':' followed by a key string. The key is stored as a
2560 * malloc()'d string into '*keyp', or NULL if no key is present in
2561 * 'arg'.
2562 *
2563 * - If 'valuep' is nonnull, an operator followed by a value string. The
2564 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2565 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2566 * index of the operator within 'allowed_operators' is stored into
2567 * '*operatorp'. The value is stored as a malloc()'d string into
2568 * '*valuep', or NULL if no value is present in 'arg'.
2569 *
2570 * On success, returns NULL. On failure, returned a malloc()'d string error
2571 * message and stores NULL into all of the nonnull output arguments. */
2572static char * WARN_UNUSED_RESULT
2573parse_column_key_value(const char *arg,
2574 const struct vtep_ctl_table_class *table,
2575 const struct ovsdb_idl_column **columnp, char **keyp,
2576 int *operatorp,
2577 const char **allowed_operators, size_t n_allowed,
2578 char **valuep)
2579{
2580 const char *p = arg;
2581 char *column_name;
2582 char *error;
2583
2584 ovs_assert(!(operatorp && !valuep));
2585 *keyp = NULL;
2586 if (valuep) {
2587 *valuep = NULL;
2588 }
2589
2590 /* Parse column name. */
2591 error = ovsdb_token_parse(&p, &column_name);
2592 if (error) {
2593 goto error;
2594 }
2595 if (column_name[0] == '\0') {
2596 free(column_name);
2597 error = xasprintf("%s: missing column name", arg);
2598 goto error;
2599 }
2600 error = get_column(table, column_name, columnp);
2601 free(column_name);
2602 if (error) {
2603 goto error;
2604 }
2605
2606 /* Parse key string. */
2607 if (*p == ':') {
2608 p++;
2609 error = ovsdb_token_parse(&p, keyp);
2610 if (error) {
2611 goto error;
2612 }
2613 }
2614
2615 /* Parse value string. */
2616 if (valuep) {
2617 size_t best_len;
2618 size_t i;
2619 int best;
2620
2621 if (!allowed_operators) {
2622 static const char *equals = "=";
2623 allowed_operators = &equals;
2624 n_allowed = 1;
2625 }
2626
2627 best = -1;
2628 best_len = 0;
2629 for (i = 0; i < n_allowed; i++) {
2630 const char *op = allowed_operators[i];
2631 size_t op_len = strlen(op);
2632
2633 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2634 best_len = op_len;
2635 best = i;
2636 }
2637 }
2638 if (best < 0) {
2639 error = missing_operator_error(arg, allowed_operators, n_allowed);
2640 goto error;
2641 }
2642
2643 if (operatorp) {
2644 *operatorp = best;
2645 }
2646 *valuep = xstrdup(p + best_len);
2647 } else {
2648 if (*p != '\0') {
2649 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2650 arg, p);
2651 goto error;
2652 }
2653 }
2654 return NULL;
2655
2656error:
2657 *columnp = NULL;
2658 free(*keyp);
2659 *keyp = NULL;
2660 if (valuep) {
2661 free(*valuep);
2662 *valuep = NULL;
2663 if (operatorp) {
2664 *operatorp = -1;
2665 }
2666 }
2667 return error;
2668}
2669
2670static const struct ovsdb_idl_column *
2671pre_parse_column_key_value(struct vtep_ctl_context *ctx,
2672 const char *arg,
2673 const struct vtep_ctl_table_class *table)
2674{
2675 const struct ovsdb_idl_column *column;
2676 const char *p;
2677 char *column_name;
2678
2679 p = arg;
2680 die_if_error(ovsdb_token_parse(&p, &column_name));
2681 if (column_name[0] == '\0') {
2682 vtep_ctl_fatal("%s: missing column name", arg);
2683 }
2684
2685 pre_get_column(ctx, table, column_name, &column);
2686 free(column_name);
2687
2688 return column;
2689}
2690
2691static void
2692check_mutable(const struct vtep_ctl_table_class *table,
2693 const struct ovsdb_idl_column *column)
2694{
2695 if (!column->mutable) {
2696 vtep_ctl_fatal("cannot modify read-only column %s in table %s",
2697 column->name, table->class->name);
2698 }
2699}
2700
2701static void
2702pre_cmd_get(struct vtep_ctl_context *ctx)
2703{
2704 const char *id = shash_find_data(&ctx->options, "--id");
2705 const char *table_name = ctx->argv[1];
2706 const struct vtep_ctl_table_class *table;
2707 int i;
2708
2709 /* Using "get" without --id or a column name could possibly make sense.
2710 * Maybe, for example, a vtep-ctl run wants to assert that a row exists.
2711 * But it is unlikely that an interactive user would want to do that, so
2712 * issue a warning if we're running on a terminal. */
2713 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2714 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2715 "possibly erroneous");
2716 }
2717
2718 table = pre_get_table(ctx, table_name);
2719 for (i = 3; i < ctx->argc; i++) {
2720 if (!strcasecmp(ctx->argv[i], "_uuid")
2721 || !strcasecmp(ctx->argv[i], "-uuid")) {
2722 continue;
2723 }
2724
2725 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2726 }
2727}
2728
2729static void
2730cmd_get(struct vtep_ctl_context *ctx)
2731{
2732 const char *id = shash_find_data(&ctx->options, "--id");
2733 bool if_exists = shash_find(&ctx->options, "--if-exists");
2734 const char *table_name = ctx->argv[1];
2735 const char *record_id = ctx->argv[2];
2736 const struct vtep_ctl_table_class *table;
2737 const struct ovsdb_idl_row *row;
2738 struct ds *out = &ctx->output;
2739 int i;
2740
2741 table = get_table(table_name);
2742 row = must_get_row(ctx, table, record_id);
2743 if (id) {
2744 struct ovsdb_symbol *symbol;
2745 bool new;
2746
2747 symbol = create_symbol(ctx->symtab, id, &new);
2748 if (!new) {
2749 vtep_ctl_fatal("row id \"%s\" specified on \"get\" command was used "
2750 "before it was defined", id);
2751 }
2752 symbol->uuid = row->uuid;
2753
2754 /* This symbol refers to a row that already exists, so disable warnings
2755 * about it being unreferenced. */
2756 symbol->strong_ref = true;
2757 }
2758 for (i = 3; i < ctx->argc; i++) {
2759 const struct ovsdb_idl_column *column;
2760 const struct ovsdb_datum *datum;
2761 char *key_string;
2762
2763 /* Special case for obtaining the UUID of a row. We can't just do this
2764 * through parse_column_key_value() below since it returns a "struct
2765 * ovsdb_idl_column" and the UUID column doesn't have one. */
2766 if (!strcasecmp(ctx->argv[i], "_uuid")
2767 || !strcasecmp(ctx->argv[i], "-uuid")) {
2768 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2769 continue;
2770 }
2771
2772 die_if_error(parse_column_key_value(ctx->argv[i], table,
2773 &column, &key_string,
2774 NULL, NULL, 0, NULL));
2775
2776 ovsdb_idl_txn_verify(row, column);
2777 datum = ovsdb_idl_read(row, column);
2778 if (key_string) {
2779 union ovsdb_atom key;
2780 unsigned int idx;
2781
2782 if (column->type.value.type == OVSDB_TYPE_VOID) {
2783 vtep_ctl_fatal("cannot specify key to get for non-map column %s",
2784 column->name);
2785 }
2786
2787 die_if_error(ovsdb_atom_from_string(&key,
2788 &column->type.key,
2789 key_string, ctx->symtab));
2790
2791 idx = ovsdb_datum_find_key(datum, &key,
2792 column->type.key.type);
2793 if (idx == UINT_MAX) {
2794 if (!if_exists) {
2795 vtep_ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2796 key_string, table->class->name, record_id,
2797 column->name);
2798 }
2799 } else {
2800 ovsdb_atom_to_string(&datum->values[idx],
2801 column->type.value.type, out);
2802 }
2803 ovsdb_atom_destroy(&key, column->type.key.type);
2804 } else {
2805 ovsdb_datum_to_string(datum, &column->type, out);
2806 }
2807 ds_put_char(out, '\n');
2808
2809 free(key_string);
2810 }
2811}
2812
2813static void
2814parse_column_names(const char *column_names,
2815 const struct vtep_ctl_table_class *table,
2816 const struct ovsdb_idl_column ***columnsp,
2817 size_t *n_columnsp)
2818{
2819 const struct ovsdb_idl_column **columns;
2820 size_t n_columns;
2821
2822 if (!column_names) {
2823 size_t i;
2824
2825 n_columns = table->class->n_columns + 1;
2826 columns = xmalloc(n_columns * sizeof *columns);
2827 columns[0] = NULL;
2828 for (i = 0; i < table->class->n_columns; i++) {
2829 columns[i + 1] = &table->class->columns[i];
2830 }
2831 } else {
2832 char *s = xstrdup(column_names);
2833 size_t allocated_columns;
2834 char *save_ptr = NULL;
2835 char *column_name;
2836
2837 columns = NULL;
2838 allocated_columns = n_columns = 0;
2839 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2840 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2841 const struct ovsdb_idl_column *column;
2842
2843 if (!strcasecmp(column_name, "_uuid")) {
2844 column = NULL;
2845 } else {
2846 die_if_error(get_column(table, column_name, &column));
2847 }
2848 if (n_columns >= allocated_columns) {
2849 columns = x2nrealloc(columns, &allocated_columns,
2850 sizeof *columns);
2851 }
2852 columns[n_columns++] = column;
2853 }
2854 free(s);
2855
2856 if (!n_columns) {
2857 vtep_ctl_fatal("must specify at least one column name");
2858 }
2859 }
2860 *columnsp = columns;
2861 *n_columnsp = n_columns;
2862}
2863
2864
2865static void
2866pre_list_columns(struct vtep_ctl_context *ctx,
2867 const struct vtep_ctl_table_class *table,
2868 const char *column_names)
2869{
2870 const struct ovsdb_idl_column **columns;
2871 size_t n_columns;
2872 size_t i;
2873
2874 parse_column_names(column_names, table, &columns, &n_columns);
2875 for (i = 0; i < n_columns; i++) {
2876 if (columns[i]) {
2877 ovsdb_idl_add_column(ctx->idl, columns[i]);
2878 }
2879 }
2880 free(columns);
2881}
2882
2883static void
2884pre_cmd_list(struct vtep_ctl_context *ctx)
2885{
2886 const char *column_names = shash_find_data(&ctx->options, "--columns");
2887 const char *table_name = ctx->argv[1];
2888 const struct vtep_ctl_table_class *table;
2889
2890 table = pre_get_table(ctx, table_name);
2891 pre_list_columns(ctx, table, column_names);
2892}
2893
2894static struct table *
2895list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2896{
2897 struct table *out;
2898 size_t i;
2899
2900 out = xmalloc(sizeof *out);
2901 table_init(out);
2902
2903 for (i = 0; i < n_columns; i++) {
2904 const struct ovsdb_idl_column *column = columns[i];
2905 const char *column_name = column ? column->name : "_uuid";
2906
2907 table_add_column(out, "%s", column_name);
2908 }
2909
2910 return out;
2911}
2912
2913static void
2914list_record(const struct ovsdb_idl_row *row,
2915 const struct ovsdb_idl_column **columns, size_t n_columns,
2916 struct table *out)
2917{
2918 size_t i;
2919
2920 table_add_row(out);
2921 for (i = 0; i < n_columns; i++) {
2922 const struct ovsdb_idl_column *column = columns[i];
2923 struct cell *cell = table_add_cell(out);
2924
2925 if (!column) {
2926 struct ovsdb_datum datum;
2927 union ovsdb_atom atom;
2928
2929 atom.uuid = row->uuid;
2930
2931 datum.keys = &atom;
2932 datum.values = NULL;
2933 datum.n = 1;
2934
2935 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2936 cell->type = &ovsdb_type_uuid;
2937 } else {
2938 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2939
2940 cell->json = ovsdb_datum_to_json(datum, &column->type);
2941 cell->type = &column->type;
2942 }
2943 }
2944}
2945
2946static void
2947cmd_list(struct vtep_ctl_context *ctx)
2948{
2949 const char *column_names = shash_find_data(&ctx->options, "--columns");
2950 const struct ovsdb_idl_column **columns;
2951 const char *table_name = ctx->argv[1];
2952 const struct vtep_ctl_table_class *table;
2953 struct table *out;
2954 size_t n_columns;
2955 int i;
2956
2957 table = get_table(table_name);
2958 parse_column_names(column_names, table, &columns, &n_columns);
2959 out = ctx->table = list_make_table(columns, n_columns);
2960 if (ctx->argc > 2) {
2961 for (i = 2; i < ctx->argc; i++) {
2962 list_record(must_get_row(ctx, table, ctx->argv[i]),
2963 columns, n_columns, out);
2964 }
2965 } else {
2966 const struct ovsdb_idl_row *row;
2967
2968 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
2969 row = ovsdb_idl_next_row(row)) {
2970 list_record(row, columns, n_columns, out);
2971 }
2972 }
2973 free(columns);
2974}
2975
2976static void
2977pre_cmd_find(struct vtep_ctl_context *ctx)
2978{
2979 const char *column_names = shash_find_data(&ctx->options, "--columns");
2980 const char *table_name = ctx->argv[1];
2981 const struct vtep_ctl_table_class *table;
2982 int i;
2983
2984 table = pre_get_table(ctx, table_name);
2985 pre_list_columns(ctx, table, column_names);
2986 for (i = 2; i < ctx->argc; i++) {
2987 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2988 }
2989}
2990
2991static void
2992cmd_find(struct vtep_ctl_context *ctx)
2993{
2994 const char *column_names = shash_find_data(&ctx->options, "--columns");
2995 const struct ovsdb_idl_column **columns;
2996 const char *table_name = ctx->argv[1];
2997 const struct vtep_ctl_table_class *table;
2998 const struct ovsdb_idl_row *row;
2999 struct table *out;
3000 size_t n_columns;
3001
3002 table = get_table(table_name);
3003 parse_column_names(column_names, table, &columns, &n_columns);
3004 out = ctx->table = list_make_table(columns, n_columns);
3005 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3006 row = ovsdb_idl_next_row(row)) {
3007 int i;
3008
3009 for (i = 2; i < ctx->argc; i++) {
3010 if (!is_condition_satisfied(table, row, ctx->argv[i],
3011 ctx->symtab)) {
3012 goto next_row;
3013 }
3014 }
3015 list_record(row, columns, n_columns, out);
3016
3017 next_row: ;
3018 }
3019 free(columns);
3020}
3021
3022static void
3023pre_cmd_set(struct vtep_ctl_context *ctx)
3024{
3025 const char *table_name = ctx->argv[1];
3026 const struct vtep_ctl_table_class *table;
3027 int i;
3028
3029 table = pre_get_table(ctx, table_name);
3030 for (i = 3; i < ctx->argc; i++) {
3031 const struct ovsdb_idl_column *column;
3032
3033 column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
3034 check_mutable(table, column);
3035 }
3036}
3037
3038static void
3039set_column(const struct vtep_ctl_table_class *table,
3040 const struct ovsdb_idl_row *row, const char *arg,
3041 struct ovsdb_symbol_table *symtab)
3042{
3043 const struct ovsdb_idl_column *column;
3044 char *key_string, *value_string;
3045 char *error;
3046
3047 error = parse_column_key_value(arg, table, &column, &key_string,
3048 NULL, NULL, 0, &value_string);
3049 die_if_error(error);
3050 if (!value_string) {
3051 vtep_ctl_fatal("%s: missing value", arg);
3052 }
3053
3054 if (key_string) {
3055 union ovsdb_atom key, value;
3056 struct ovsdb_datum datum;
3057
3058 if (column->type.value.type == OVSDB_TYPE_VOID) {
3059 vtep_ctl_fatal("cannot specify key to set for non-map column %s",
3060 column->name);
3061 }
3062
3063 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3064 key_string, symtab));
3065 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3066 value_string, symtab));
3067
3068 ovsdb_datum_init_empty(&datum);
3069 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3070
3071 ovsdb_atom_destroy(&key, column->type.key.type);
3072 ovsdb_atom_destroy(&value, column->type.value.type);
3073
3074 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3075 &column->type, false);
3076 ovsdb_idl_txn_write(row, column, &datum);
3077 } else {
3078 struct ovsdb_datum datum;
3079
3080 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3081 value_string, symtab));
3082 ovsdb_idl_txn_write(row, column, &datum);
3083 }
3084
3085 free(key_string);
3086 free(value_string);
3087}
3088
3089static void
3090cmd_set(struct vtep_ctl_context *ctx)
3091{
3092 const char *table_name = ctx->argv[1];
3093 const char *record_id = ctx->argv[2];
3094 const struct vtep_ctl_table_class *table;
3095 const struct ovsdb_idl_row *row;
3096 int i;
3097
3098 table = get_table(table_name);
3099 row = must_get_row(ctx, table, record_id);
3100 for (i = 3; i < ctx->argc; i++) {
3101 set_column(table, row, ctx->argv[i], ctx->symtab);
3102 }
3103}
3104
3105static void
3106pre_cmd_add(struct vtep_ctl_context *ctx)
3107{
3108 const char *table_name = ctx->argv[1];
3109 const char *column_name = ctx->argv[3];
3110 const struct vtep_ctl_table_class *table;
3111 const struct ovsdb_idl_column *column;
3112
3113 table = pre_get_table(ctx, table_name);
3114 pre_get_column(ctx, table, column_name, &column);
3115 check_mutable(table, column);
3116}
3117
3118static void
3119cmd_add(struct vtep_ctl_context *ctx)
3120{
3121 const char *table_name = ctx->argv[1];
3122 const char *record_id = ctx->argv[2];
3123 const char *column_name = ctx->argv[3];
3124 const struct vtep_ctl_table_class *table;
3125 const struct ovsdb_idl_column *column;
3126 const struct ovsdb_idl_row *row;
3127 const struct ovsdb_type *type;
3128 struct ovsdb_datum old;
3129 int i;
3130
3131 table = get_table(table_name);
3132 row = must_get_row(ctx, table, record_id);
3133 die_if_error(get_column(table, column_name, &column));
3134
3135 type = &column->type;
3136 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3137 for (i = 4; i < ctx->argc; i++) {
3138 struct ovsdb_type add_type;
3139 struct ovsdb_datum add;
3140
3141 add_type = *type;
3142 add_type.n_min = 1;
3143 add_type.n_max = UINT_MAX;
3144 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3145 ctx->symtab));
3146 ovsdb_datum_union(&old, &add, type, false);
3147 ovsdb_datum_destroy(&add, type);
3148 }
3149 if (old.n > type->n_max) {
3150 vtep_ctl_fatal("\"add\" operation would put %u %s in column %s of "
3151 "table %s but the maximum number is %u",
3152 old.n,
3153 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3154 column->name, table->class->name, type->n_max);
3155 }
3156 ovsdb_idl_txn_verify(row, column);
3157 ovsdb_idl_txn_write(row, column, &old);
3158}
3159
3160static void
3161pre_cmd_remove(struct vtep_ctl_context *ctx)
3162{
3163 const char *table_name = ctx->argv[1];
3164 const char *column_name = ctx->argv[3];
3165 const struct vtep_ctl_table_class *table;
3166 const struct ovsdb_idl_column *column;
3167
3168 table = pre_get_table(ctx, table_name);
3169 pre_get_column(ctx, table, column_name, &column);
3170 check_mutable(table, column);
3171}
3172
3173static void
3174cmd_remove(struct vtep_ctl_context *ctx)
3175{
3176 const char *table_name = ctx->argv[1];
3177 const char *record_id = ctx->argv[2];
3178 const char *column_name = ctx->argv[3];
3179 const struct vtep_ctl_table_class *table;
3180 const struct ovsdb_idl_column *column;
3181 const struct ovsdb_idl_row *row;
3182 const struct ovsdb_type *type;
3183 struct ovsdb_datum old;
3184 int i;
3185
3186 table = get_table(table_name);
3187 row = must_get_row(ctx, table, record_id);
3188 die_if_error(get_column(table, column_name, &column));
3189
3190 type = &column->type;
3191 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3192 for (i = 4; i < ctx->argc; i++) {
3193 struct ovsdb_type rm_type;
3194 struct ovsdb_datum rm;
3195 char *error;
3196
3197 rm_type = *type;
3198 rm_type.n_min = 1;
3199 rm_type.n_max = UINT_MAX;
3200 error = ovsdb_datum_from_string(&rm, &rm_type,
3201 ctx->argv[i], ctx->symtab);
3202 if (error && ovsdb_type_is_map(&rm_type)) {
3203 free(error);
3204 rm_type.value.type = OVSDB_TYPE_VOID;
3205 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3206 ctx->argv[i], ctx->symtab));
3207 }
3208 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3209 ovsdb_datum_destroy(&rm, &rm_type);
3210 }
3211 if (old.n < type->n_min) {
3212 vtep_ctl_fatal("\"remove\" operation would put %u %s in column %s of "
3213 "table %s but the minimum number is %u",
3214 old.n,
3215 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3216 column->name, table->class->name, type->n_min);
3217 }
3218 ovsdb_idl_txn_verify(row, column);
3219 ovsdb_idl_txn_write(row, column, &old);
3220}
3221
3222static void
3223pre_cmd_clear(struct vtep_ctl_context *ctx)
3224{
3225 const char *table_name = ctx->argv[1];
3226 const struct vtep_ctl_table_class *table;
3227 int i;
3228
3229 table = pre_get_table(ctx, table_name);
3230 for (i = 3; i < ctx->argc; i++) {
3231 const struct ovsdb_idl_column *column;
3232
3233 pre_get_column(ctx, table, ctx->argv[i], &column);
3234 check_mutable(table, column);
3235 }
3236}
3237
3238static void
3239cmd_clear(struct vtep_ctl_context *ctx)
3240{
3241 const char *table_name = ctx->argv[1];
3242 const char *record_id = ctx->argv[2];
3243 const struct vtep_ctl_table_class *table;
3244 const struct ovsdb_idl_row *row;
3245 int i;
3246
3247 table = get_table(table_name);
3248 row = must_get_row(ctx, table, record_id);
3249 for (i = 3; i < ctx->argc; i++) {
3250 const struct ovsdb_idl_column *column;
3251 const struct ovsdb_type *type;
3252 struct ovsdb_datum datum;
3253
3254 die_if_error(get_column(table, ctx->argv[i], &column));
3255
3256 type = &column->type;
3257 if (type->n_min > 0) {
3258 vtep_ctl_fatal("\"clear\" operation cannot be applied to column %s "
3259 "of table %s, which is not allowed to be empty",
3260 column->name, table->class->name);
3261 }
3262
3263 ovsdb_datum_init_empty(&datum);
3264 ovsdb_idl_txn_write(row, column, &datum);
3265 }
3266}
3267
3268static void
3269pre_create(struct vtep_ctl_context *ctx)
3270{
3271 const char *id = shash_find_data(&ctx->options, "--id");
3272 const char *table_name = ctx->argv[1];
3273 const struct vtep_ctl_table_class *table;
3274
3275 table = get_table(table_name);
3276 if (!id && !table->class->is_root) {
3277 VLOG_WARN("applying \"create\" command to table %s without --id "
3278 "option will have no effect", table->class->name);
3279 }
3280}
3281
3282static void
3283cmd_create(struct vtep_ctl_context *ctx)
3284{
3285 const char *id = shash_find_data(&ctx->options, "--id");
3286 const char *table_name = ctx->argv[1];
3287 const struct vtep_ctl_table_class *table = get_table(table_name);
3288 const struct ovsdb_idl_row *row;
3289 const struct uuid *uuid;
3290 int i;
3291
3292 if (id) {
3293 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3294 if (table->class->is_root) {
3295 /* This table is in the root set, meaning that rows created in it
3296 * won't disappear even if they are unreferenced, so disable
3297 * warnings about that by pretending that there is a reference. */
3298 symbol->strong_ref = true;
3299 }
3300 uuid = &symbol->uuid;
3301 } else {
3302 uuid = NULL;
3303 }
3304
3305 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3306 for (i = 2; i < ctx->argc; i++) {
3307 set_column(table, row, ctx->argv[i], ctx->symtab);
3308 }
3309 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3310}
3311
3312/* This function may be used as the 'postprocess' function for commands that
3313 * insert new rows into the database. It expects that the command's 'run'
3314 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3315 * sole output. It replaces that output by the row's permanent UUID assigned
3316 * by the database server and appends a new-line.
3317 *
3318 * Currently we use this only for "create", because the higher-level commands
3319 * are supposed to be independent of the actual structure of the VTEP
3320 * configuration. */
3321static void
3322post_create(struct vtep_ctl_context *ctx)
3323{
3324 const struct uuid *real;
3325 struct uuid dummy;
3326
3327 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
428b2edd 3328 OVS_NOT_REACHED();
ffc759c6
JP
3329 }
3330 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3331 if (real) {
3332 ds_clear(&ctx->output);
3333 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3334 }
3335 ds_put_char(&ctx->output, '\n');
3336}
3337
3338static void
3339pre_cmd_destroy(struct vtep_ctl_context *ctx)
3340{
3341 const char *table_name = ctx->argv[1];
3342
3343 pre_get_table(ctx, table_name);
3344}
3345
3346static void
3347cmd_destroy(struct vtep_ctl_context *ctx)
3348{
3349 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3350 bool delete_all = shash_find(&ctx->options, "--all");
3351 const char *table_name = ctx->argv[1];
3352 const struct vtep_ctl_table_class *table;
3353 int i;
3354
3355 table = get_table(table_name);
3356
3357 if (delete_all && ctx->argc > 2) {
3358 vtep_ctl_fatal("--all and records argument should not be specified together");
3359 }
3360
3361 if (delete_all && !must_exist) {
3362 vtep_ctl_fatal("--all and --if-exists should not be specified together");
3363 }
3364
3365 if (delete_all) {
3366 const struct ovsdb_idl_row *row;
3367 const struct ovsdb_idl_row *next_row;
3368
3369 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3370 row;) {
3371 next_row = ovsdb_idl_next_row(row);
3372 ovsdb_idl_txn_delete(row);
3373 row = next_row;
3374 }
3375 } else {
3376 for (i = 2; i < ctx->argc; i++) {
3377 const struct ovsdb_idl_row *row;
3378
3379 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3380 if (row) {
3381 ovsdb_idl_txn_delete(row);
3382 }
3383 }
3384 }
3385}
3386
3387#define RELOPS \
3388 RELOP(RELOP_EQ, "=") \
3389 RELOP(RELOP_NE, "!=") \
3390 RELOP(RELOP_LT, "<") \
3391 RELOP(RELOP_GT, ">") \
3392 RELOP(RELOP_LE, "<=") \
3393 RELOP(RELOP_GE, ">=") \
3394 RELOP(RELOP_SET_EQ, "{=}") \
3395 RELOP(RELOP_SET_NE, "{!=}") \
3396 RELOP(RELOP_SET_LT, "{<}") \
3397 RELOP(RELOP_SET_GT, "{>}") \
3398 RELOP(RELOP_SET_LE, "{<=}") \
3399 RELOP(RELOP_SET_GE, "{>=}")
3400
3401enum relop {
3402#define RELOP(ENUM, STRING) ENUM,
3403 RELOPS
3404#undef RELOP
3405};
3406
3407static bool
3408is_set_operator(enum relop op)
3409{
3410 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3411 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3412 op == RELOP_SET_LE || op == RELOP_SET_GE);
3413}
3414
3415static bool
3416evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3417 const struct ovsdb_type *type, enum relop op)
3418{
3419 switch (op) {
3420 case RELOP_EQ:
3421 case RELOP_SET_EQ:
3422 return ovsdb_datum_compare_3way(a, b, type) == 0;
3423 case RELOP_NE:
3424 case RELOP_SET_NE:
3425 return ovsdb_datum_compare_3way(a, b, type) != 0;
3426 case RELOP_LT:
3427 return ovsdb_datum_compare_3way(a, b, type) < 0;
3428 case RELOP_GT:
3429 return ovsdb_datum_compare_3way(a, b, type) > 0;
3430 case RELOP_LE:
3431 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3432 case RELOP_GE:
3433 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3434
3435 case RELOP_SET_LT:
3436 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3437 case RELOP_SET_GT:
3438 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3439 case RELOP_SET_LE:
3440 return ovsdb_datum_includes_all(a, b, type);
3441 case RELOP_SET_GE:
3442 return ovsdb_datum_includes_all(b, a, type);
3443
3444 default:
428b2edd 3445 OVS_NOT_REACHED();
ffc759c6
JP
3446 }
3447}
3448
3449static bool
3450is_condition_satisfied(const struct vtep_ctl_table_class *table,
3451 const struct ovsdb_idl_row *row, const char *arg,
3452 struct ovsdb_symbol_table *symtab)
3453{
3454 static const char *operators[] = {
3455#define RELOP(ENUM, STRING) STRING,
3456 RELOPS
3457#undef RELOP
3458 };
3459
3460 const struct ovsdb_idl_column *column;
3461 const struct ovsdb_datum *have_datum;
3462 char *key_string, *value_string;
3463 struct ovsdb_type type;
3464 int operator;
3465 bool retval;
3466 char *error;
3467
3468 error = parse_column_key_value(arg, table, &column, &key_string,
3469 &operator, operators, ARRAY_SIZE(operators),
3470 &value_string);
3471 die_if_error(error);
3472 if (!value_string) {
3473 vtep_ctl_fatal("%s: missing value", arg);
3474 }
3475
3476 type = column->type;
3477 type.n_max = UINT_MAX;
3478
3479 have_datum = ovsdb_idl_read(row, column);
3480 if (key_string) {
3481 union ovsdb_atom want_key;
3482 struct ovsdb_datum b;
3483 unsigned int idx;
3484
3485 if (column->type.value.type == OVSDB_TYPE_VOID) {
3486 vtep_ctl_fatal("cannot specify key to check for non-map column %s",
3487 column->name);
3488 }
3489
3490 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3491 key_string, symtab));
3492
3493 type.key = type.value;
3494 type.value.type = OVSDB_TYPE_VOID;
3495 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3496
3497 idx = ovsdb_datum_find_key(have_datum,
3498 &want_key, column->type.key.type);
3499 if (idx == UINT_MAX && !is_set_operator(operator)) {
3500 retval = false;
3501 } else {
3502 struct ovsdb_datum a;
3503
3504 if (idx != UINT_MAX) {
3505 a.n = 1;
3506 a.keys = &have_datum->values[idx];
3507 a.values = NULL;
3508 } else {
3509 a.n = 0;
3510 a.keys = NULL;
3511 a.values = NULL;
3512 }
3513
3514 retval = evaluate_relop(&a, &b, &type, operator);
3515 }
3516
3517 ovsdb_atom_destroy(&want_key, column->type.key.type);
3518 ovsdb_datum_destroy(&b, &type);
3519 } else {
3520 struct ovsdb_datum want_datum;
3521
3522 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3523 value_string, symtab));
3524 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3525 ovsdb_datum_destroy(&want_datum, &column->type);
3526 }
3527
3528 free(key_string);
3529 free(value_string);
3530
3531 return retval;
3532}
3533
3534static void
3535pre_cmd_wait_until(struct vtep_ctl_context *ctx)
3536{
3537 const char *table_name = ctx->argv[1];
3538 const struct vtep_ctl_table_class *table;
3539 int i;
3540
3541 table = pre_get_table(ctx, table_name);
3542
3543 for (i = 3; i < ctx->argc; i++) {
3544 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3545 }
3546}
3547
3548static void
3549cmd_wait_until(struct vtep_ctl_context *ctx)
3550{
3551 const char *table_name = ctx->argv[1];
3552 const char *record_id = ctx->argv[2];
3553 const struct vtep_ctl_table_class *table;
3554 const struct ovsdb_idl_row *row;
3555 int i;
3556
3557 table = get_table(table_name);
3558
3559 row = get_row(ctx, table, record_id);
3560 if (!row) {
3561 ctx->try_again = true;
3562 return;
3563 }
3564
3565 for (i = 3; i < ctx->argc; i++) {
3566 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3567 ctx->try_again = true;
3568 return;
3569 }
3570 }
3571}
3572\f
3573/* Prepares 'ctx', which has already been initialized with
3574 * vtep_ctl_context_init(), for processing 'command'. */
3575static void
3576vtep_ctl_context_init_command(struct vtep_ctl_context *ctx,
3577 struct vtep_ctl_command *command)
3578{
3579 ctx->argc = command->argc;
3580 ctx->argv = command->argv;
3581 ctx->options = command->options;
3582
3583 ds_swap(&ctx->output, &command->output);
3584 ctx->table = command->table;
3585
3586 ctx->verified_ports = false;
3587
3588 ctx->try_again = false;
3589}
3590
3591/* Prepares 'ctx' for processing commands, initializing its members with the
3592 * values passed in as arguments.
3593 *
3594 * If 'command' is nonnull, calls vtep_ctl_context_init_command() to prepare for
3595 * that particular command. */
3596static void
3597vtep_ctl_context_init(struct vtep_ctl_context *ctx,
3598 struct vtep_ctl_command *command,
3599 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3600 const struct vteprec_global *vtep_global,
3601 struct ovsdb_symbol_table *symtab)
3602{
3603 if (command) {
3604 vtep_ctl_context_init_command(ctx, command);
3605 }
3606 ctx->idl = idl;
3607 ctx->txn = txn;
3608 ctx->vtep_global = vtep_global;
3609 ctx->symtab = symtab;
3610 ctx->cache_valid = false;
3611}
3612
3613/* Completes processing of 'command' within 'ctx'. */
3614static void
3615vtep_ctl_context_done_command(struct vtep_ctl_context *ctx,
3616 struct vtep_ctl_command *command)
3617{
3618 ds_swap(&ctx->output, &command->output);
3619 command->table = ctx->table;
3620}
3621
3622/* Finishes up with 'ctx'.
3623 *
3624 * If command is nonnull, first calls vtep_ctl_context_done_command() to complete
3625 * processing that command within 'ctx'. */
3626static void
3627vtep_ctl_context_done(struct vtep_ctl_context *ctx, struct vtep_ctl_command *command)
3628{
3629 if (command) {
3630 vtep_ctl_context_done_command(ctx, command);
3631 }
3632}
3633
3634static void
3635run_prerequisites(struct vtep_ctl_command *commands, size_t n_commands,
3636 struct ovsdb_idl *idl)
3637{
3638 struct vtep_ctl_command *c;
3639
3640 ovsdb_idl_add_table(idl, &vteprec_table_global);
3641 for (c = commands; c < &commands[n_commands]; c++) {
3642 if (c->syntax->prerequisites) {
3643 struct vtep_ctl_context ctx;
3644
3645 ds_init(&c->output);
3646 c->table = NULL;
3647
3648 vtep_ctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3649 (c->syntax->prerequisites)(&ctx);
3650 vtep_ctl_context_done(&ctx, c);
3651
3652 ovs_assert(!c->output.string);
3653 ovs_assert(!c->table);
3654 }
3655 }
3656}
3657
3658static void
3659do_vtep_ctl(const char *args, struct vtep_ctl_command *commands,
3660 size_t n_commands, struct ovsdb_idl *idl)
3661{
3662 struct ovsdb_idl_txn *txn;
3663 const struct vteprec_global *vtep_global;
3664 enum ovsdb_idl_txn_status status;
3665 struct ovsdb_symbol_table *symtab;
3666 struct vtep_ctl_context ctx;
3667 struct vtep_ctl_command *c;
3668 struct shash_node *node;
3669 char *error = NULL;
3670
3671 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3672 if (dry_run) {
3673 ovsdb_idl_txn_set_dry_run(txn);
3674 }
3675
3676 ovsdb_idl_txn_add_comment(txn, "vtep-ctl: %s", args);
3677
3678 vtep_global = vteprec_global_first(idl);
3679 if (!vtep_global) {
3680 /* XXX add verification that table is empty */
3681 vtep_global = vteprec_global_insert(txn);
3682 }
3683
3684 symtab = ovsdb_symbol_table_create();
3685 for (c = commands; c < &commands[n_commands]; c++) {
3686 ds_init(&c->output);
3687 c->table = NULL;
3688 }
3689 vtep_ctl_context_init(&ctx, NULL, idl, txn, vtep_global, symtab);
3690 for (c = commands; c < &commands[n_commands]; c++) {
3691 vtep_ctl_context_init_command(&ctx, c);
3692 if (c->syntax->run) {
3693 (c->syntax->run)(&ctx);
3694 }
3695 vtep_ctl_context_done_command(&ctx, c);
3696
3697 if (ctx.try_again) {
3698 vtep_ctl_context_done(&ctx, NULL);
3699 goto try_again;
3700 }
3701 }
3702 vtep_ctl_context_done(&ctx, NULL);
3703
3704 SHASH_FOR_EACH (node, &symtab->sh) {
3705 struct ovsdb_symbol *symbol = node->data;
3706 if (!symbol->created) {
3707 vtep_ctl_fatal("row id \"%s\" is referenced but never created "
3708 "(e.g. with \"-- --id=%s create ...\")",
3709 node->name, node->name);
3710 }
3711 if (!symbol->strong_ref) {
3712 if (!symbol->weak_ref) {
3713 VLOG_WARN("row id \"%s\" was created but no reference to it "
3714 "was inserted, so it will not actually appear in "
3715 "the database", node->name);
3716 } else {
3717 VLOG_WARN("row id \"%s\" was created but only a weak "
3718 "reference to it was inserted, so it will not "
3719 "actually appear in the database", node->name);
3720 }
3721 }
3722 }
3723
3724 status = ovsdb_idl_txn_commit_block(txn);
3725 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3726 for (c = commands; c < &commands[n_commands]; c++) {
3727 if (c->syntax->postprocess) {
3728 struct vtep_ctl_context ctx;
3729
3730 vtep_ctl_context_init(&ctx, c, idl, txn, vtep_global, symtab);
3731 (c->syntax->postprocess)(&ctx);
3732 vtep_ctl_context_done(&ctx, c);
3733 }
3734 }
3735 }
3736 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3737 ovsdb_idl_txn_destroy(txn);
3738 txn = the_idl_txn = NULL;
3739
3740 switch (status) {
3741 case TXN_UNCOMMITTED:
3742 case TXN_INCOMPLETE:
428b2edd 3743 OVS_NOT_REACHED();
ffc759c6
JP
3744
3745 case TXN_ABORTED:
3746 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3747 vtep_ctl_fatal("transaction aborted");
3748
3749 case TXN_UNCHANGED:
3750 case TXN_SUCCESS:
3751 break;
3752
3753 case TXN_TRY_AGAIN:
3754 goto try_again;
3755
3756 case TXN_ERROR:
3757 vtep_ctl_fatal("transaction error: %s", error);
3758
3759 case TXN_NOT_LOCKED:
3760 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3761 vtep_ctl_fatal("database not locked");
3762
3763 default:
428b2edd 3764 OVS_NOT_REACHED();
ffc759c6
JP
3765 }
3766 free(error);
3767
3768 ovsdb_symbol_table_destroy(symtab);
3769
3770 for (c = commands; c < &commands[n_commands]; c++) {
3771 struct ds *ds = &c->output;
3772
3773 if (c->table) {
3774 table_print(c->table, &table_style);
3775 } else if (oneline) {
3776 size_t j;
3777
3778 ds_chomp(ds, '\n');
3779 for (j = 0; j < ds->length; j++) {
3780 int ch = ds->string[j];
3781 switch (ch) {
3782 case '\n':
3783 fputs("\\n", stdout);
3784 break;
3785
3786 case '\\':
3787 fputs("\\\\", stdout);
3788 break;
3789
3790 default:
3791 putchar(ch);
3792 }
3793 }
3794 putchar('\n');
3795 } else {
3796 fputs(ds_cstr(ds), stdout);
3797 }
3798 ds_destroy(&c->output);
3799 table_destroy(c->table);
3800 free(c->table);
3801
3802 shash_destroy_free_data(&c->options);
3803 }
3804 free(commands);
3805
3806 ovsdb_idl_destroy(idl);
3807
3808 exit(EXIT_SUCCESS);
3809
3810try_again:
3811 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3812 * resources and return so that the caller can try again. */
3813 if (txn) {
3814 ovsdb_idl_txn_abort(txn);
3815 ovsdb_idl_txn_destroy(txn);
3816 }
3817 ovsdb_symbol_table_destroy(symtab);
3818 for (c = commands; c < &commands[n_commands]; c++) {
3819 ds_destroy(&c->output);
3820 table_destroy(c->table);
3821 free(c->table);
3822 }
3823 free(error);
3824}
3825
3826static const struct vtep_ctl_command_syntax all_commands[] = {
3827 /* Physical Switch commands. */
3828 {"add-ps", 1, 1, pre_get_info, cmd_add_ps, NULL, "--may-exist", RW},
3829 {"del-ps", 1, 1, pre_get_info, cmd_del_ps, NULL, "--if-exists", RW},
3830 {"list-ps", 0, 0, pre_get_info, cmd_list_ps, NULL, "", RO},
3831 {"ps-exists", 1, 1, pre_get_info, cmd_ps_exists, NULL, "", RO},
3832
3833 /* Port commands. */
3834 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3835 {"add-port", 2, 2, pre_get_info, cmd_add_port, NULL, "--may-exist",
3836 RW},
3837 {"del-port", 2, 2, pre_get_info, cmd_del_port, NULL, "--if-exists", RW},
3838
3839 /* Logical Switch commands. */
3840 {"add-ls", 1, 1, pre_get_info, cmd_add_ls, NULL, "--may-exist", RW},
3841 {"del-ls", 1, 1, pre_get_info, cmd_del_ls, NULL, "--if-exists", RW},
3842 {"list-ls", 0, 0, pre_get_info, cmd_list_ls, NULL, "", RO},
3843 {"ls-exists", 1, 1, pre_get_info, cmd_ls_exists, NULL, "", RO},
3844 {"list-bindings", 2, 2, pre_get_info, cmd_list_bindings, NULL, "", RO},
3845 {"bind-ls", 4, 4, pre_get_info, cmd_bind_ls, NULL, "", RO},
3846 {"unbind-ls", 3, 3, pre_get_info, cmd_unbind_ls, NULL, "", RO},
3847
3848 /* MAC binding commands. */
3849 {"add-ucast-local", 3, 4, pre_get_info, cmd_add_ucast_local, NULL, "", RW},
3850 {"del-ucast-local", 2, 2, pre_get_info, cmd_del_ucast_local, NULL, "", RW},
3851 {"add-mcast-local", 3, 4, pre_get_info, cmd_add_mcast_local, NULL, "", RW},
3852 {"del-mcast-local", 3, 4, pre_get_info, cmd_del_mcast_local, NULL, "", RW},
3853 {"clear-local-macs", 1, 1, pre_get_info, cmd_clear_local_macs, NULL, "",
3854 RO},
3855 {"list-local-macs", 1, 1, pre_get_info, cmd_list_local_macs, NULL, "", RO},
3856 {"add-ucast-remote", 3, 4, pre_get_info, cmd_add_ucast_remote, NULL, "",
3857 RW},
3858 {"del-ucast-remote", 2, 2, pre_get_info, cmd_del_ucast_remote, NULL, "",
3859 RW},
3860 {"add-mcast-remote", 3, 4, pre_get_info, cmd_add_mcast_remote, NULL, "",
3861 RW},
3862 {"del-mcast-remote", 3, 4, pre_get_info, cmd_del_mcast_remote, NULL, "",
3863 RW},
3864 {"clear-remote-macs", 1, 1, pre_get_info, cmd_clear_remote_macs, NULL, "",
3865 RO},
3866 {"list-remote-macs", 1, 1, pre_get_info, cmd_list_remote_macs, NULL, "",
3867 RO},
3868
3869 /* Manager commands. */
3870 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3871 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
3872 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3873
3874 /* Database commands. */
3875 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
3876 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3877 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3878 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3879 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3880 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3881 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3882 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3883 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3884 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
3885 "--if-exists,--all", RW},
3886 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3887 RO},
3888
3889 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},
3890};
3891