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