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