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