]> git.proxmox.com Git - mirror_ovs.git/blob - vtep/vtep-ctl.c
Refer to database manpages in *ctl manpages
[mirror_ovs.git] / vtep / vtep-ctl.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015, 2016, 2017 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 "db-ctl-base.h"
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "openvswitch/dynamic-string.h"
35 #include "fatal-signal.h"
36 #include "hash.h"
37 #include "openvswitch/json.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-idl.h"
40 #include "openvswitch/poll-loop.h"
41 #include "process.h"
42 #include "stream.h"
43 #include "stream-ssl.h"
44 #include "smap.h"
45 #include "sset.h"
46 #include "svec.h"
47 #include "vtep/vtep-idl.h"
48 #include "table.h"
49 #include "timeval.h"
50 #include "util.h"
51 #include "openvswitch/vconn.h"
52 #include "openvswitch/vlog.h"
53
54 VLOG_DEFINE_THIS_MODULE(vtep_ctl);
55
56 struct vtep_ctl_context;
57
58 /* --db: The database server to contact. */
59 static const char *db;
60
61 /* --oneline: Write each command's output as a single line? */
62 static bool oneline;
63
64 /* --dry-run: Do not commit any changes. */
65 static bool dry_run;
66
67 /* --timeout: Time to wait for a connection to 'db'. */
68 static int timeout;
69
70 /* Format for table output. */
71 static struct table_style table_style = TABLE_STYLE_DEFAULT;
72
73 /* The IDL we're using and the current transaction, if any.
74 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
75 * Other code should use its context arguments. */
76 static struct ovsdb_idl *the_idl;
77 static struct ovsdb_idl_txn *the_idl_txn;
78
79 OVS_NO_RETURN static void vtep_ctl_exit(int status);
80 static void vtep_ctl_cmd_init(void);
81 OVS_NO_RETURN static void usage(void);
82 static void parse_options(int argc, char *argv[], struct shash *local_options);
83 static void run_prerequisites(struct ctl_command[], size_t n_commands,
84 struct ovsdb_idl *);
85 static bool do_vtep_ctl(const char *args, struct ctl_command *, size_t n,
86 struct ovsdb_idl *);
87 static struct vtep_ctl_lswitch *find_lswitch(struct vtep_ctl_context *,
88 const char *name,
89 bool must_exist);
90 static struct vtep_ctl_lrouter *find_lrouter(struct vtep_ctl_context *,
91 const char *name,
92 bool must_exist);
93
94 int
95 main(int argc, char *argv[])
96 {
97 struct ovsdb_idl *idl;
98 struct ctl_command *commands;
99 struct shash local_options;
100 unsigned int seqno;
101 size_t n_commands;
102 char *args;
103
104 set_program_name(argv[0]);
105 fatal_ignore_sigpipe();
106 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
107 vlog_set_levels_from_string_assert("reconnect:warn");
108
109 vtep_ctl_cmd_init();
110
111 /* Log our arguments. This is often valuable for debugging systems. */
112 args = process_escape_args(argv);
113 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
114
115 /* Parse command line. */
116 shash_init(&local_options);
117 parse_options(argc, argv, &local_options);
118 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
119 &n_commands);
120
121 if (timeout) {
122 time_alarm(timeout);
123 }
124
125 /* Initialize IDL. */
126 idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
127 run_prerequisites(commands, n_commands, idl);
128
129 /* Execute the commands.
130 *
131 * 'seqno' is the database sequence number for which we last tried to
132 * execute our transaction. There's no point in trying to commit more than
133 * once for any given sequence number, because if the transaction fails
134 * it's because the database changed and we need to obtain an up-to-date
135 * view of the database before we try the transaction again. */
136 seqno = ovsdb_idl_get_seqno(idl);
137 for (;;) {
138 ovsdb_idl_run(idl);
139 if (!ovsdb_idl_is_alive(idl)) {
140 int retval = ovsdb_idl_get_last_error(idl);
141 ctl_fatal("%s: database connection failed (%s)",
142 db, ovs_retval_to_string(retval));
143 }
144
145 if (seqno != ovsdb_idl_get_seqno(idl)) {
146 seqno = ovsdb_idl_get_seqno(idl);
147 if (do_vtep_ctl(args, commands, n_commands, idl)) {
148 free(args);
149 exit(EXIT_SUCCESS);
150 }
151 }
152
153 if (seqno == ovsdb_idl_get_seqno(idl)) {
154 ovsdb_idl_wait(idl);
155 poll_block();
156 }
157 }
158 }
159
160 static void
161 parse_options(int argc, char *argv[], struct shash *local_options)
162 {
163 enum {
164 OPT_DB = UCHAR_MAX + 1,
165 OPT_ONELINE,
166 OPT_NO_SYSLOG,
167 OPT_DRY_RUN,
168 OPT_PEER_CA_CERT,
169 OPT_LOCAL,
170 VLOG_OPTION_ENUMS,
171 TABLE_OPTION_ENUMS,
172 SSL_OPTION_ENUMS,
173 };
174 static const struct option global_long_options[] = {
175 {"db", required_argument, NULL, OPT_DB},
176 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
177 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
178 {"oneline", no_argument, NULL, OPT_ONELINE},
179 {"timeout", required_argument, NULL, 't'},
180 {"help", no_argument, NULL, 'h'},
181 {"version", no_argument, NULL, 'V'},
182 VLOG_LONG_OPTIONS,
183 TABLE_LONG_OPTIONS,
184 STREAM_SSL_LONG_OPTIONS,
185 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
186 {NULL, 0, NULL, 0},
187 };
188 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
189 char *tmp, *short_options;
190
191 struct option *options;
192 size_t allocated_options;
193 size_t n_options;
194 size_t i;
195
196 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
197 short_options = xasprintf("+%s", tmp);
198 free(tmp);
199
200 /* We want to parse both global and command-specific options here, but
201 * getopt_long() isn't too convenient for the job. We copy our global
202 * options into a dynamic array, then append all of the command-specific
203 * options. */
204 options = xmemdup(global_long_options, sizeof global_long_options);
205 allocated_options = ARRAY_SIZE(global_long_options);
206 n_options = n_global_long_options;
207 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
208
209 for (;;) {
210 int idx;
211 int c;
212
213 c = getopt_long(argc, argv, short_options, options, &idx);
214 if (c == -1) {
215 break;
216 }
217
218 switch (c) {
219 case OPT_DB:
220 db = optarg;
221 break;
222
223 case OPT_ONELINE:
224 oneline = true;
225 break;
226
227 case OPT_NO_SYSLOG:
228 vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
229 break;
230
231 case OPT_DRY_RUN:
232 dry_run = true;
233 break;
234
235 case OPT_LOCAL:
236 if (shash_find(local_options, options[idx].name)) {
237 ctl_fatal("'%s' option specified multiple times",
238 options[idx].name);
239 }
240 shash_add_nocopy(local_options,
241 xasprintf("--%s", options[idx].name),
242 nullable_xstrdup(optarg));
243 break;
244
245 case 'h':
246 usage();
247
248 case 'V':
249 ovs_print_version(0, 0);
250 printf("DB Schema %s\n", vteprec_get_db_version());
251 exit(EXIT_SUCCESS);
252
253 case 't':
254 timeout = strtoul(optarg, NULL, 10);
255 if (timeout < 0) {
256 ctl_fatal("value %s on -t or --timeout is invalid",
257 optarg);
258 }
259 break;
260
261 VLOG_OPTION_HANDLERS
262 TABLE_OPTION_HANDLERS(&table_style)
263
264 STREAM_SSL_OPTION_HANDLERS
265
266 case OPT_PEER_CA_CERT:
267 stream_ssl_set_peer_ca_cert_file(optarg);
268 break;
269
270 case '?':
271 exit(EXIT_FAILURE);
272
273 default:
274 abort();
275 }
276 }
277 free(short_options);
278
279 if (!db) {
280 db = ctl_default_db();
281 }
282
283 for (i = n_global_long_options; options[i].name; i++) {
284 free(CONST_CAST(char *, options[i].name));
285 }
286 free(options);
287 }
288
289 /* Frees the current transaction and the underlying IDL and then calls
290 * exit(status).
291 *
292 * Freeing the transaction and the IDL is not strictly necessary, but it makes
293 * for a clean memory leak report from valgrind in the normal case. That makes
294 * it easier to notice real memory leaks. */
295 static void
296 vtep_ctl_exit(int status)
297 {
298 if (the_idl_txn) {
299 ovsdb_idl_txn_abort(the_idl_txn);
300 ovsdb_idl_txn_destroy(the_idl_txn);
301 }
302 ovsdb_idl_destroy(the_idl);
303 exit(status);
304 }
305
306 static void
307 usage(void)
308 {
309 printf("\
310 %s: VTEP configuration utility\n\
311 usage: %s [OPTIONS] COMMAND [ARG...]\n\
312 \n\
313 VTEP commands:\n\
314 show print overview of database contents\n\
315 \n\
316 Manager commands:\n\
317 get-manager print the managers\n\
318 del-manager delete the managers\n\
319 set-manager TARGET... set the list of managers to TARGET...\n\
320 \n\
321 Physical Switch commands:\n\
322 add-ps PS create a new physical switch named PS\n\
323 del-ps PS delete PS and all of its ports\n\
324 list-ps print the names of all the physical switches\n\
325 ps-exists PS exit 2 if PS does not exist\n\
326 \n\
327 Port commands:\n\
328 list-ports PS print the names of all the ports on PS\n\
329 add-port PS PORT add network device PORT to PS\n\
330 del-port PS PORT delete PORT from PS\n\
331 \n\
332 Logical Switch commands:\n\
333 add-ls LS create a new logical switch named LS\n\
334 del-ls LS delete LS and all of its ports\n\
335 list-ls print the names of all the logical switches\n\
336 ls-exists LS exit 2 if LS does not exist\n\
337 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
338 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
339 list-bindings PS PORT list bindings for PORT on PS\n\
340 set-replication-mode LS MODE set replication mode on LS\n\
341 get-replication-mode LS get replication mode on LS\n\
342 \n\
343 Logical Router commands:\n\
344 add-lr LR create a new logical router named LR\n\
345 del-lr LR delete LR\n\
346 list-lr print the names of all the logical routers\n\
347 lr-exists LR exit 2 if LR does not exist\n\
348 \n\
349 MAC binding commands:\n\
350 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
351 del-ucast-local LS MAC del ucast local entry from LS\n\
352 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
353 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
354 clear-local-macs LS clear local mac entries\n\
355 list-local-macs LS list local mac entries\n\
356 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
357 del-ucast-remote LS MAC del ucast remote entry from LS\n\
358 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
359 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
360 clear-remote-macs LS clear remote mac entries\n\
361 list-remote-macs LS list remote mac entries\n\
362 \n\
363 %s\
364 %s\
365 \n\
366 Options:\n\
367 --db=DATABASE connect to DATABASE\n\
368 (default: %s)\n\
369 -t, --timeout=SECS wait at most SECS seconds\n\
370 --dry-run do not commit changes to database\n\
371 --oneline print exactly one line of output per command\n",
372 program_name, program_name, ctl_get_db_cmd_usage(),
373 ctl_list_db_tables_usage(), ctl_default_db());
374 table_usage();
375 vlog_usage();
376 printf("\
377 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
378 stream_usage("database", true, true, false);
379 printf("\n\
380 Other options:\n\
381 -h, --help display this help message\n\
382 -V, --version display version information\n");
383 exit(EXIT_SUCCESS);
384 }
385
386 \f
387 static struct cmd_show_table cmd_show_tables[] = {
388 {&vteprec_table_global,
389 NULL,
390 {&vteprec_global_col_managers,
391 &vteprec_global_col_switches,
392 NULL},
393 {NULL, NULL, NULL}
394 },
395
396 {&vteprec_table_manager,
397 &vteprec_manager_col_target,
398 {&vteprec_manager_col_is_connected,
399 NULL,
400 NULL},
401 {NULL, NULL, NULL}
402 },
403
404 {&vteprec_table_physical_switch,
405 &vteprec_physical_switch_col_name,
406 {&vteprec_physical_switch_col_management_ips,
407 &vteprec_physical_switch_col_tunnel_ips,
408 &vteprec_physical_switch_col_ports},
409 {NULL, NULL, NULL}
410 },
411
412 {&vteprec_table_physical_port,
413 &vteprec_physical_port_col_name,
414 {&vteprec_physical_port_col_vlan_bindings,
415 NULL,
416 NULL},
417 {NULL, NULL, NULL}
418 },
419
420 {&vteprec_table_logical_switch,
421 &vteprec_logical_switch_col_name,
422 {NULL,
423 NULL,
424 NULL},
425 {NULL, NULL, NULL}
426 },
427
428 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
429 };
430
431 /* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
432 struct vtep_ctl_context {
433 struct ctl_context base;
434
435 /* Modifiable state. */
436 const struct vteprec_global *vtep_global;
437 bool verified_ports;
438
439 /* A cache of the contents of the database.
440 *
441 * A command that needs to use any of this information must first
442 * call vtep_ctl_context_populate_cache(). A command that changes
443 * anything that could invalidate the cache must either call
444 * vtep_ctl_context_invalidate_cache() or manually update the cache
445 * to maintain its correctness. */
446 bool cache_valid;
447 struct shash pswitches; /* Maps from physical switch name to
448 * struct vtep_ctl_pswitch. */
449 struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
450
451 struct shash lswitches; /* Maps from logical switch name to
452 * struct vtep_ctl_lswitch. */
453 struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
454 * struct vteprec_physical_locator. */
455 struct shash lrouters; /* Maps from logical router name to
456 * struct vtep_ctl_lrouter. */
457 };
458
459 /* Casts 'base' into 'struct vtep_ctl_context'. */
460 static struct vtep_ctl_context *
461 vtep_ctl_context_cast(struct ctl_context *base)
462 {
463 return CONTAINER_OF(base, struct vtep_ctl_context, base);
464 }
465
466 struct vtep_ctl_pswitch {
467 const struct vteprec_physical_switch *ps_cfg;
468 char *name;
469 struct ovs_list ports; /* Contains "struct vteprec_physical_port"s. */
470 };
471
472 struct vtep_ctl_port {
473 struct ovs_list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
474 const struct vteprec_physical_port *port_cfg;
475 struct vtep_ctl_pswitch *ps;
476 struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
477 };
478
479 struct vtep_ctl_lswitch {
480 const struct vteprec_logical_switch *ls_cfg;
481 char *name;
482 struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
483 struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
484 struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
485 struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
486 };
487
488 struct vtep_ctl_lrouter {
489 const struct vteprec_logical_router *lr_cfg;
490 char *name;
491 };
492
493 struct vtep_ctl_mcast_mac {
494 const struct vteprec_mcast_macs_local *local_cfg;
495 const struct vteprec_mcast_macs_remote *remote_cfg;
496
497 const struct vteprec_physical_locator_set *ploc_set_cfg;
498 struct ovs_list locators; /* Contains 'vtep_ctl_ploc's. */
499 };
500
501 struct vtep_ctl_ploc {
502 struct ovs_list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
503 list. */
504 const struct vteprec_physical_locator *ploc_cfg;
505 };
506
507 static void
508 verify_ports(struct vtep_ctl_context *vtepctl_ctx)
509 {
510 if (!vtepctl_ctx->verified_ports) {
511 const struct vteprec_physical_switch *ps;
512
513 vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
514 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, vtepctl_ctx->base.idl) {
515 vteprec_physical_switch_verify_ports(ps);
516 }
517
518 vtepctl_ctx->verified_ports = true;
519 }
520 }
521
522 static struct vtep_ctl_port *
523 add_port_to_cache(struct vtep_ctl_context *vtepctl_ctx,
524 struct vtep_ctl_pswitch *ps,
525 struct vteprec_physical_port *port_cfg)
526 {
527 char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
528 struct vtep_ctl_port *port;
529
530 port = xmalloc(sizeof *port);
531 ovs_list_push_back(&ps->ports, &port->ports_node);
532 port->port_cfg = port_cfg;
533 port->ps = ps;
534 shash_add(&vtepctl_ctx->ports, cache_name, port);
535 free(cache_name);
536 shash_init(&port->bindings);
537
538 return port;
539 }
540
541 static void
542 del_cached_port(struct vtep_ctl_context *vtepctl_ctx,
543 struct vtep_ctl_port *port)
544 {
545 char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
546
547 ovs_list_remove(&port->ports_node);
548 shash_find_and_delete(&vtepctl_ctx->ports, cache_name);
549 vteprec_physical_port_delete(port->port_cfg);
550 shash_destroy(&port->bindings);
551 free(cache_name);
552 free(port);
553 }
554
555 static void
556 add_pswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
557 struct vteprec_physical_switch *ps_cfg)
558 {
559 struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
560 ps->ps_cfg = ps_cfg;
561 ps->name = xstrdup(ps_cfg->name);
562 ovs_list_init(&ps->ports);
563 shash_add(&vtepctl_ctx->pswitches, ps->name, ps);
564 }
565
566 static void
567 vtep_delete_pswitch(const struct vteprec_global *vtep_global,
568 const struct vteprec_physical_switch *ps)
569 {
570 struct vteprec_physical_switch **pswitches;
571 size_t i, n;
572
573 pswitches = xmalloc(sizeof *vtep_global->switches
574 * vtep_global->n_switches);
575 for (i = n = 0; i < vtep_global->n_switches; i++) {
576 if (vtep_global->switches[i] != ps) {
577 pswitches[n++] = vtep_global->switches[i];
578 }
579 }
580 vteprec_global_set_switches(vtep_global, pswitches, n);
581 free(pswitches);
582 }
583
584 static void
585 del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
586 {
587 ovs_assert(ovs_list_is_empty(&ps->ports));
588 if (ps->ps_cfg) {
589 vteprec_physical_switch_delete(ps->ps_cfg);
590 vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
591 }
592 shash_find_and_delete(&ctx->pswitches, ps->name);
593 free(ps->name);
594 free(ps);
595 }
596
597 static struct vtep_ctl_lswitch *
598 add_lswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
599 const struct vteprec_logical_switch *ls_cfg)
600 {
601 struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
602 ls->ls_cfg = ls_cfg;
603 ls->name = xstrdup(ls_cfg->name);
604 shash_add(&vtepctl_ctx->lswitches, ls->name, ls);
605 shash_init(&ls->ucast_local);
606 shash_init(&ls->ucast_remote);
607 shash_init(&ls->mcast_local);
608 shash_init(&ls->mcast_remote);
609 return ls;
610 }
611
612 static void
613 del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
614 {
615 if (ls->ls_cfg) {
616 vteprec_logical_switch_delete(ls->ls_cfg);
617 }
618 shash_find_and_delete(&ctx->lswitches, ls->name);
619 free(ls->name);
620 free(ls);
621 }
622
623 static void
624 commit_ls_bindings(struct vtep_ctl_port *port)
625 {
626 struct vteprec_logical_switch **binding_values;
627 int64_t *binding_keys;
628 size_t n_bindings;
629 struct shash_node *node;
630 int i;
631
632 n_bindings = shash_count(&port->bindings);
633 binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
634 binding_values = xmalloc(n_bindings * sizeof *binding_values);
635
636 i = 0;
637 SHASH_FOR_EACH(node, &port->bindings) {
638 struct vtep_ctl_lswitch *ls_entry = node->data;
639
640 binding_keys[i] = strtoll(node->name, NULL, 0);
641 binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
642 i++;
643 }
644
645 vteprec_physical_port_set_vlan_bindings(port->port_cfg,
646 binding_keys, binding_values,
647 n_bindings);
648 free(binding_values);
649 free(binding_keys);
650 }
651
652 static void
653 add_ls_binding_to_cache(struct vtep_ctl_port *port,
654 const char *vlan,
655 struct vtep_ctl_lswitch *ls)
656 {
657 if (shash_find(&port->bindings, vlan)) {
658 ctl_fatal("multiple bindings for vlan %s", vlan);
659 }
660
661 shash_add(&port->bindings, vlan, ls);
662 }
663
664 static void
665 del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
666 {
667 if (!shash_find(&port->bindings, vlan)) {
668 ctl_fatal("no binding for vlan %s", vlan);
669 }
670
671 shash_find_and_delete(&port->bindings, vlan);
672 }
673
674 static struct vtep_ctl_lrouter *
675 add_lrouter_to_cache(struct vtep_ctl_context *vtepctl_ctx,
676 const struct vteprec_logical_router *lr_cfg)
677 {
678 struct vtep_ctl_lrouter *lr = xmalloc(sizeof *lr);
679 lr->lr_cfg = lr_cfg;
680 lr->name = xstrdup(lr_cfg->name);
681 shash_add(&vtepctl_ctx->lrouters, lr->name, lr);
682 return lr;
683 }
684
685 static void
686 del_cached_lrouter(struct vtep_ctl_context *ctx, struct vtep_ctl_lrouter *lr)
687 {
688 if (lr->lr_cfg) {
689 vteprec_logical_router_delete(lr->lr_cfg);
690 }
691 shash_find_and_delete(&ctx->lrouters, lr->name);
692 free(lr->name);
693 free(lr);
694 }
695
696 static struct vteprec_physical_locator *
697 find_ploc(struct vtep_ctl_context *vtepctl_ctx, const char *encap,
698 const char *dst_ip)
699 {
700 struct vteprec_physical_locator *ploc;
701 char *name = xasprintf("%s+%s", encap, dst_ip);
702
703 ovs_assert(vtepctl_ctx->cache_valid);
704
705 ploc = shash_find_data(&vtepctl_ctx->plocs, name);
706 free(name);
707
708 return ploc;
709 }
710
711 static void
712 add_ploc_to_cache(struct vtep_ctl_context *vtepctl_ctx,
713 struct vteprec_physical_locator *ploc)
714 {
715 char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
716 struct vteprec_physical_locator *orig_ploc;
717
718 orig_ploc = find_ploc(vtepctl_ctx, ploc->encapsulation_type, ploc->dst_ip);
719 if (!orig_ploc) {
720 shash_add(&vtepctl_ctx->plocs, name, ploc);
721 }
722
723 free(name);
724 }
725
726 static void
727 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
728 struct vteprec_physical_locator *ploc_cfg)
729 {
730 struct vtep_ctl_ploc *ploc;
731
732 ploc = xmalloc(sizeof *ploc);
733 ploc->ploc_cfg = ploc_cfg;
734 ovs_list_push_back(&mcast_mac->locators, &ploc->locators_node);
735 }
736
737 static void
738 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
739 struct vteprec_physical_locator *ploc_cfg)
740 {
741 struct vtep_ctl_ploc *ploc;
742
743 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
744 if (ploc->ploc_cfg == ploc_cfg) {
745 ovs_list_remove(&ploc->locators_node);
746 free(ploc);
747 return;
748 }
749 }
750 }
751
752 static struct vtep_ctl_mcast_mac *
753 add_mcast_mac_to_cache(struct vtep_ctl_context *vtepctl_ctx,
754 struct vtep_ctl_lswitch *ls, const char *mac,
755 struct vteprec_physical_locator_set *ploc_set_cfg,
756 bool local)
757 {
758 struct vtep_ctl_mcast_mac *mcast_mac;
759 struct shash *mcast_shash;
760 size_t i;
761
762 mcast_mac = xmalloc(sizeof *mcast_mac);
763 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
764
765 mcast_mac->ploc_set_cfg = ploc_set_cfg;
766 ovs_list_init(&mcast_mac->locators);
767 shash_add(mcast_shash, mac, mcast_mac);
768
769 for (i = 0; i < ploc_set_cfg->n_locators; i++) {
770 struct vteprec_physical_locator *ploc_cfg;
771
772 ploc_cfg = ploc_set_cfg->locators[i];
773 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
774 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
775 }
776
777 return mcast_mac;
778 }
779
780 static void
781 vtep_ctl_context_invalidate_cache(struct ctl_context *ctx)
782 {
783 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
784 struct shash_node *node;
785
786 if (!vtepctl_ctx->cache_valid) {
787 return;
788 }
789 vtepctl_ctx->cache_valid = false;
790
791 SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
792 struct vtep_ctl_pswitch *ps = node->data;
793 free(ps->name);
794 free(ps);
795 }
796 shash_destroy(&vtepctl_ctx->pswitches);
797
798 SHASH_FOR_EACH (node, &vtepctl_ctx->ports) {
799 struct vtep_ctl_port *port = node->data;
800 shash_destroy(&port->bindings);
801 }
802 shash_destroy_free_data(&vtepctl_ctx->ports);
803
804 SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
805 struct vtep_ctl_lswitch *ls = node->data;
806 struct shash_node *node2, *next_node2;
807
808 shash_destroy(&ls->ucast_local);
809 shash_destroy(&ls->ucast_remote);
810
811 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
812 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
813 struct vtep_ctl_ploc *ploc, *next_ploc;
814
815 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
816 &mcast_mac->locators) {
817 free(ploc);
818 }
819 free(mcast_mac);
820 }
821 shash_destroy(&ls->mcast_local);
822
823 SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
824 struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
825 struct vtep_ctl_ploc *ploc, *next_ploc;
826
827 LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
828 &mcast_mac->locators) {
829 free(ploc);
830 }
831 free(mcast_mac);
832 }
833 shash_destroy(&ls->mcast_remote);
834
835 free(ls->name);
836 free(ls);
837 }
838 shash_destroy(&vtepctl_ctx->lswitches);
839 shash_destroy(&vtepctl_ctx->plocs);
840
841 SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
842 struct vtep_ctl_lrouter *lr = node->data;
843 free(lr->name);
844 free(lr);
845 }
846 shash_destroy(&vtepctl_ctx->lrouters);
847 }
848
849 static void
850 pre_get_info(struct ctl_context *ctx)
851 {
852 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
853
854 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
855 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
856 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_tunnels);
857
858 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
859 ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
860
861 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
862 ovsdb_idl_add_column(ctx->idl,
863 &vteprec_logical_switch_col_replication_mode);
864
865 ovsdb_idl_add_column(ctx->idl, &vteprec_logical_router_col_name);
866
867 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
868 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
869 ovsdb_idl_add_column(ctx->idl,
870 &vteprec_ucast_macs_local_col_logical_switch);
871
872 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
873 ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
874 ovsdb_idl_add_column(ctx->idl,
875 &vteprec_ucast_macs_remote_col_logical_switch);
876
877 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
878 ovsdb_idl_add_column(ctx->idl,
879 &vteprec_mcast_macs_local_col_locator_set);
880 ovsdb_idl_add_column(ctx->idl,
881 &vteprec_mcast_macs_local_col_logical_switch);
882
883 ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
884 ovsdb_idl_add_column(ctx->idl,
885 &vteprec_mcast_macs_remote_col_locator_set);
886 ovsdb_idl_add_column(ctx->idl,
887 &vteprec_mcast_macs_remote_col_logical_switch);
888
889 ovsdb_idl_add_column(ctx->idl,
890 &vteprec_physical_locator_set_col_locators);
891
892 ovsdb_idl_add_column(ctx->idl,
893 &vteprec_physical_locator_col_dst_ip);
894 ovsdb_idl_add_column(ctx->idl,
895 &vteprec_physical_locator_col_encapsulation_type);
896
897 ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_local);
898 ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_remote);
899 }
900
901 static void
902 vtep_ctl_context_populate_cache(struct ctl_context *ctx)
903 {
904 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
905 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
906 const struct vteprec_logical_switch *ls_cfg;
907 const struct vteprec_logical_router *lr_cfg;
908 const struct vteprec_ucast_macs_local *ucast_local_cfg;
909 const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
910 const struct vteprec_mcast_macs_local *mcast_local_cfg;
911 const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
912 const struct vteprec_tunnel *tunnel_cfg;
913 struct sset pswitches, ports, lswitches;
914 struct sset lrouters;
915 size_t i;
916
917 if (vtepctl_ctx->cache_valid) {
918 /* Cache is already populated. */
919 return;
920 }
921 vtepctl_ctx->cache_valid = true;
922 shash_init(&vtepctl_ctx->pswitches);
923 shash_init(&vtepctl_ctx->ports);
924 shash_init(&vtepctl_ctx->lswitches);
925 shash_init(&vtepctl_ctx->plocs);
926 shash_init(&vtepctl_ctx->lrouters);
927
928 sset_init(&pswitches);
929 sset_init(&ports);
930 for (i = 0; i < vtep_global->n_switches; i++) {
931 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
932 size_t j;
933
934 if (!sset_add(&pswitches, ps_cfg->name)) {
935 VLOG_WARN("%s: database contains duplicate physical switch name",
936 ps_cfg->name);
937 continue;
938 }
939 add_pswitch_to_cache(vtepctl_ctx, ps_cfg);
940
941 for (j = 0; j < ps_cfg->n_ports; j++) {
942 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
943
944 if (!sset_add(&ports, port_cfg->name)) {
945 /* Duplicate port name. (We will warn about that later.) */
946 continue;
947 }
948 }
949 }
950 sset_destroy(&pswitches);
951 sset_destroy(&ports);
952
953 sset_init(&lswitches);
954 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
955 if (!sset_add(&lswitches, ls_cfg->name)) {
956 VLOG_WARN("%s: database contains duplicate logical switch name",
957 ls_cfg->name);
958 continue;
959 }
960 add_lswitch_to_cache(vtepctl_ctx, ls_cfg);
961 }
962 sset_destroy(&lswitches);
963
964 sset_init(&lrouters);
965 VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg, ctx->idl) {
966 if (!sset_add(&lrouters, lr_cfg->name)) {
967 VLOG_WARN("%s: database contains duplicate logical router name",
968 lr_cfg->name);
969 continue;
970 }
971 add_lrouter_to_cache(vtepctl_ctx, lr_cfg);
972 }
973 sset_destroy(&lrouters);
974
975 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
976 struct vtep_ctl_lswitch *ls;
977
978 if (!ucast_local_cfg->logical_switch) {
979 continue;
980 }
981 ls = find_lswitch(vtepctl_ctx, ucast_local_cfg->logical_switch->name,
982 false);
983 if (!ls) {
984 continue;
985 }
986
987 if (ucast_local_cfg->locator) {
988 add_ploc_to_cache(vtepctl_ctx, ucast_local_cfg->locator);
989 }
990
991 shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
992 }
993
994 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
995 struct vtep_ctl_lswitch *ls;
996
997 if (!ucast_remote_cfg->logical_switch) {
998 continue;
999 }
1000 ls = find_lswitch(vtepctl_ctx, ucast_remote_cfg->logical_switch->name,
1001 false);
1002 if (!ls) {
1003 continue;
1004 }
1005
1006 if (ucast_remote_cfg->locator) {
1007 add_ploc_to_cache(vtepctl_ctx, ucast_remote_cfg->locator);
1008 }
1009
1010 shash_add(&ls->ucast_remote, ucast_remote_cfg->MAC, ucast_remote_cfg);
1011 }
1012
1013 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg, ctx->idl) {
1014 struct vtep_ctl_mcast_mac *mcast_mac;
1015 struct vtep_ctl_lswitch *ls;
1016
1017 if (!mcast_local_cfg->logical_switch) {
1018 continue;
1019 }
1020 ls = find_lswitch(vtepctl_ctx, mcast_local_cfg->logical_switch->name,
1021 false);
1022 if (!ls) {
1023 continue;
1024 }
1025
1026 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mcast_local_cfg->MAC,
1027 mcast_local_cfg->locator_set,
1028 true);
1029 mcast_mac->local_cfg = mcast_local_cfg;
1030 }
1031
1032 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg, ctx->idl) {
1033 struct vtep_ctl_mcast_mac *mcast_mac;
1034 struct vtep_ctl_lswitch *ls;
1035
1036 if (!mcast_remote_cfg->logical_switch) {
1037 continue;
1038 }
1039 ls = find_lswitch(vtepctl_ctx, mcast_remote_cfg->logical_switch->name,
1040 false);
1041 if (!ls) {
1042 continue;
1043 }
1044
1045 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mcast_remote_cfg->MAC,
1046 mcast_remote_cfg->locator_set,
1047 false);
1048 mcast_mac->remote_cfg = mcast_remote_cfg;
1049 }
1050
1051 VTEPREC_TUNNEL_FOR_EACH (tunnel_cfg, ctx->idl) {
1052 if (tunnel_cfg->local) {
1053 add_ploc_to_cache(vtepctl_ctx, tunnel_cfg->local);
1054 }
1055 if (tunnel_cfg->remote) {
1056 add_ploc_to_cache(vtepctl_ctx, tunnel_cfg->remote);
1057 }
1058 }
1059
1060 sset_init(&pswitches);
1061 for (i = 0; i < vtep_global->n_switches; i++) {
1062 struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
1063 struct vtep_ctl_pswitch *ps;
1064 size_t j;
1065
1066 if (!sset_add(&pswitches, ps_cfg->name)) {
1067 continue;
1068 }
1069 ps = shash_find_data(&vtepctl_ctx->pswitches, ps_cfg->name);
1070 for (j = 0; j < ps_cfg->n_ports; j++) {
1071 struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
1072 struct vtep_ctl_port *port;
1073 size_t k;
1074
1075 port = shash_find_data(&vtepctl_ctx->ports, port_cfg->name);
1076 if (port) {
1077 if (port_cfg == port->port_cfg) {
1078 VLOG_WARN("%s: port is in multiple physical switches "
1079 "(%s and %s)",
1080 port_cfg->name, ps->name, port->ps->name);
1081 } else {
1082 /* Log as an error because this violates the database's
1083 * uniqueness constraints, so the database server shouldn't
1084 * have allowed it. */
1085 VLOG_ERR("%s: database contains duplicate port name",
1086 port_cfg->name);
1087 }
1088 continue;
1089 }
1090
1091 port = add_port_to_cache(vtepctl_ctx, ps, port_cfg);
1092
1093 for (k = 0; k < port_cfg->n_vlan_bindings; k++) {
1094 struct vtep_ctl_lswitch *ls;
1095 char *vlan;
1096
1097 vlan = xasprintf("%"PRId64, port_cfg->key_vlan_bindings[k]);
1098 if (shash_find(&port->bindings, vlan)) {
1099 ctl_fatal("multiple bindings for vlan %s", vlan);
1100 }
1101
1102 ls_cfg = port_cfg->value_vlan_bindings[k];
1103 ls = find_lswitch(vtepctl_ctx, ls_cfg->name, true);
1104
1105 shash_add_nocopy(&port->bindings, vlan, ls);
1106 }
1107 }
1108 }
1109 sset_destroy(&pswitches);
1110 }
1111
1112 static struct vtep_ctl_pswitch *
1113 find_pswitch(struct vtep_ctl_context *vtepctl_ctx, const char *name, bool must_exist)
1114 {
1115 struct vtep_ctl_pswitch *ps;
1116
1117 ovs_assert(vtepctl_ctx->cache_valid);
1118
1119 ps = shash_find_data(&vtepctl_ctx->pswitches, name);
1120 if (must_exist && !ps) {
1121 ctl_fatal("no physical switch named %s", name);
1122 }
1123 vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
1124 return ps;
1125 }
1126
1127 static struct vtep_ctl_port *
1128 find_port(struct vtep_ctl_context *vtepctl_ctx, const char *ps_name,
1129 const char *port_name, bool must_exist)
1130 {
1131 char *cache_name = xasprintf("%s+%s", ps_name, port_name);
1132 struct vtep_ctl_port *port;
1133
1134 ovs_assert(vtepctl_ctx->cache_valid);
1135
1136 port = shash_find_data(&vtepctl_ctx->ports, cache_name);
1137 if (port && !strcmp(port_name, port->ps->name)) {
1138 port = NULL;
1139 }
1140 free(cache_name);
1141 if (must_exist && !port) {
1142 ctl_fatal("no port named %s", port_name);
1143 }
1144 verify_ports(vtepctl_ctx);
1145 return port;
1146 }
1147
1148 static void
1149 pswitch_insert_port(const struct vteprec_physical_switch *ps,
1150 struct vteprec_physical_port *port)
1151 {
1152 struct vteprec_physical_port **ports;
1153 size_t i;
1154
1155 ports = xmalloc(sizeof *ps->ports * (ps->n_ports + 1));
1156 for (i = 0; i < ps->n_ports; i++) {
1157 ports[i] = ps->ports[i];
1158 }
1159 ports[ps->n_ports] = port;
1160 vteprec_physical_switch_set_ports(ps, ports, ps->n_ports + 1);
1161 free(ports);
1162 }
1163
1164 static void
1165 pswitch_delete_port(const struct vteprec_physical_switch *ps,
1166 const struct vteprec_physical_port *port)
1167 {
1168 struct vteprec_physical_port **ports;
1169 size_t i, n;
1170
1171 ports = xmalloc(sizeof *ps->ports * ps->n_ports);
1172 for (i = n = 0; i < ps->n_ports; i++) {
1173 if (ps->ports[i] != port) {
1174 ports[n++] = ps->ports[i];
1175 }
1176 }
1177 vteprec_physical_switch_set_ports(ps, ports, n);
1178 free(ports);
1179 }
1180
1181 static void
1182 vtep_insert_pswitch(const struct vteprec_global *vtep_global,
1183 struct vteprec_physical_switch *ps)
1184 {
1185 struct vteprec_physical_switch **pswitches;
1186 size_t i;
1187
1188 pswitches = xmalloc(sizeof *vtep_global->switches
1189 * (vtep_global->n_switches + 1));
1190 for (i = 0; i < vtep_global->n_switches; i++) {
1191 pswitches[i] = vtep_global->switches[i];
1192 }
1193 pswitches[vtep_global->n_switches] = ps;
1194 vteprec_global_set_switches(vtep_global, pswitches,
1195 vtep_global->n_switches + 1);
1196 free(pswitches);
1197 }
1198
1199 static void
1200 cmd_add_ps(struct ctl_context *ctx)
1201 {
1202 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1203 const char *ps_name = ctx->argv[1];
1204 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1205 struct vteprec_physical_switch *ps;
1206
1207 vtep_ctl_context_populate_cache(ctx);
1208 if (find_pswitch(vtepctl_ctx, ps_name, false)) {
1209 if (!may_exist) {
1210 ctl_fatal("cannot create physical switch %s because it "
1211 "already exists", ps_name);
1212 }
1213 return;
1214 }
1215
1216 ps = vteprec_physical_switch_insert(ctx->txn);
1217 vteprec_physical_switch_set_name(ps, ps_name);
1218
1219 vtep_insert_pswitch(vtepctl_ctx->vtep_global, ps);
1220
1221 vtep_ctl_context_invalidate_cache(ctx);
1222 }
1223
1224 static void
1225 del_port(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_port *port)
1226 {
1227 pswitch_delete_port(port->ps->ps_cfg, port->port_cfg);
1228 del_cached_port(vtepctl_ctx, port);
1229 }
1230
1231 static void
1232 del_pswitch(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_pswitch *ps)
1233 {
1234 struct vtep_ctl_port *port, *next_port;
1235
1236 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &ps->ports) {
1237 del_port(vtepctl_ctx, port);
1238 }
1239
1240 del_cached_pswitch(vtepctl_ctx, ps);
1241 }
1242
1243 static void
1244 cmd_del_ps(struct ctl_context *ctx)
1245 {
1246 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1247 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1248 struct vtep_ctl_pswitch *ps;
1249
1250 vtep_ctl_context_populate_cache(ctx);
1251 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], must_exist);
1252 if (ps) {
1253 del_pswitch(vtepctl_ctx, ps);
1254 }
1255 }
1256
1257 static void
1258 output_sorted(struct svec *svec, struct ds *output)
1259 {
1260 const char *name;
1261 size_t i;
1262
1263 svec_sort(svec);
1264 SVEC_FOR_EACH (i, name, svec) {
1265 ds_put_format(output, "%s\n", name);
1266 }
1267 }
1268
1269 static void
1270 cmd_list_ps(struct ctl_context *ctx)
1271 {
1272 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1273 struct shash_node *node;
1274 struct svec pswitches;
1275
1276 vtep_ctl_context_populate_cache(ctx);
1277
1278 svec_init(&pswitches);
1279 SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
1280 struct vtep_ctl_pswitch *ps = node->data;
1281
1282 svec_add(&pswitches, ps->name);
1283 }
1284 output_sorted(&pswitches, &ctx->output);
1285 svec_destroy(&pswitches);
1286 }
1287
1288 static void
1289 cmd_ps_exists(struct ctl_context *ctx)
1290 {
1291 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1292
1293 vtep_ctl_context_populate_cache(ctx);
1294 if (!find_pswitch(vtepctl_ctx, ctx->argv[1], false)) {
1295 vtep_ctl_exit(2);
1296 }
1297 }
1298
1299 static void
1300 cmd_list_ports(struct ctl_context *ctx)
1301 {
1302 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1303 struct vtep_ctl_pswitch *ps;
1304 struct vtep_ctl_port *port;
1305 struct svec ports;
1306
1307 vtep_ctl_context_populate_cache(ctx);
1308 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], true);
1309 vteprec_physical_switch_verify_ports(ps->ps_cfg);
1310
1311 svec_init(&ports);
1312 LIST_FOR_EACH (port, ports_node, &ps->ports) {
1313 if (strcmp(port->port_cfg->name, ps->name)) {
1314 svec_add(&ports, port->port_cfg->name);
1315 }
1316 }
1317 output_sorted(&ports, &ctx->output);
1318 svec_destroy(&ports);
1319 }
1320
1321 static void
1322 add_port(struct ctl_context *ctx, const char *ps_name,
1323 const char *port_name, bool may_exist)
1324 {
1325 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1326 struct vtep_ctl_port *vtep_ctl_port;
1327 struct vtep_ctl_pswitch *ps;
1328 struct vteprec_physical_port *port;
1329
1330 vtep_ctl_context_populate_cache(ctx);
1331
1332 vtep_ctl_port = find_port(vtepctl_ctx, ps_name, port_name, false);
1333 if (vtep_ctl_port) {
1334 if (!may_exist) {
1335 ctl_fatal("cannot create a port named %s on %s because a "
1336 "port with that name already exists",
1337 port_name, ps_name);
1338 }
1339 return;
1340 }
1341
1342 ps = find_pswitch(vtepctl_ctx, ps_name, true);
1343
1344 port = vteprec_physical_port_insert(ctx->txn);
1345 vteprec_physical_port_set_name(port, port_name);
1346
1347 pswitch_insert_port(ps->ps_cfg, port);
1348
1349 add_port_to_cache(vtepctl_ctx, ps, port);
1350 }
1351
1352 static void
1353 cmd_add_port(struct ctl_context *ctx)
1354 {
1355 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1356
1357 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist);
1358 }
1359
1360 static void
1361 cmd_del_port(struct ctl_context *ctx)
1362 {
1363 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1364 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1365 struct vtep_ctl_port *port;
1366
1367 vtep_ctl_context_populate_cache(ctx);
1368
1369 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], must_exist);
1370 if (port) {
1371 if (ctx->argc == 3) {
1372 struct vtep_ctl_pswitch *ps;
1373
1374 ps = find_pswitch(vtepctl_ctx, ctx->argv[1], true);
1375 if (port->ps != ps) {
1376 ctl_fatal("physical switch %s does not have a port %s",
1377 ctx->argv[1], ctx->argv[2]);
1378 }
1379 }
1380
1381 del_port(vtepctl_ctx, port);
1382 }
1383 }
1384
1385 static struct vtep_ctl_lswitch *
1386 find_lswitch(struct vtep_ctl_context *vtepctl_ctx,
1387 const char *name, bool must_exist)
1388 {
1389 struct vtep_ctl_lswitch *ls;
1390
1391 ovs_assert(vtepctl_ctx->cache_valid);
1392
1393 ls = shash_find_data(&vtepctl_ctx->lswitches, name);
1394 if (must_exist && !ls) {
1395 ctl_fatal("no logical switch named %s", name);
1396 }
1397 return ls;
1398 }
1399
1400 static void
1401 cmd_add_ls(struct ctl_context *ctx)
1402 {
1403 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1404 const char *ls_name = ctx->argv[1];
1405 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1406 struct vteprec_logical_switch *ls;
1407
1408 vtep_ctl_context_populate_cache(ctx);
1409 if (find_lswitch(vtepctl_ctx, ls_name, false)) {
1410 if (!may_exist) {
1411 ctl_fatal("cannot create logical switch %s because it "
1412 "already exists", ls_name);
1413 }
1414 return;
1415 }
1416
1417 ls = vteprec_logical_switch_insert(ctx->txn);
1418 vteprec_logical_switch_set_name(ls, ls_name);
1419
1420 vtep_ctl_context_invalidate_cache(ctx);
1421 }
1422
1423 static void
1424 del_lswitch(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_lswitch *ls)
1425 {
1426 del_cached_lswitch(vtepctl_ctx, ls);
1427 }
1428
1429 static void
1430 cmd_del_ls(struct ctl_context *ctx)
1431 {
1432 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1433 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1434 struct vtep_ctl_lswitch *ls;
1435
1436 vtep_ctl_context_populate_cache(ctx);
1437 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], must_exist);
1438 if (ls) {
1439 del_lswitch(vtepctl_ctx, ls);
1440 }
1441 }
1442
1443 static void
1444 cmd_list_ls(struct ctl_context *ctx)
1445 {
1446 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1447 struct shash_node *node;
1448 struct svec lswitches;
1449
1450 vtep_ctl_context_populate_cache(ctx);
1451
1452 svec_init(&lswitches);
1453 SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
1454 struct vtep_ctl_lswitch *ls = node->data;
1455
1456 svec_add(&lswitches, ls->name);
1457 }
1458 output_sorted(&lswitches, &ctx->output);
1459 svec_destroy(&lswitches);
1460 }
1461
1462 static void
1463 cmd_ls_exists(struct ctl_context *ctx)
1464 {
1465 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1466
1467 vtep_ctl_context_populate_cache(ctx);
1468 if (!find_lswitch(vtepctl_ctx, ctx->argv[1], false)) {
1469 vtep_ctl_exit(2);
1470 }
1471 }
1472
1473 static void
1474 cmd_list_bindings(struct ctl_context *ctx)
1475 {
1476 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1477 const struct shash_node *node;
1478 struct vtep_ctl_port *port;
1479 struct svec bindings;
1480
1481 vtep_ctl_context_populate_cache(ctx);
1482 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1483
1484 svec_init(&bindings);
1485 SHASH_FOR_EACH (node, &port->bindings) {
1486 struct vtep_ctl_lswitch *lswitch = node->data;
1487 char *binding;
1488
1489 binding = xasprintf("%04lld %s", strtoll(node->name, NULL, 0),
1490 lswitch->name);
1491 svec_add_nocopy(&bindings, binding);
1492 }
1493 output_sorted(&bindings, &ctx->output);
1494 svec_destroy(&bindings);
1495 }
1496
1497 static void
1498 cmd_bind_ls(struct ctl_context *ctx)
1499 {
1500 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1501 struct vtep_ctl_lswitch *ls;
1502 struct vtep_ctl_port *port;
1503 const char *vlan;
1504
1505 vtep_ctl_context_populate_cache(ctx);
1506
1507 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1508 vlan = ctx->argv[3];
1509 ls = find_lswitch(vtepctl_ctx, ctx->argv[4], true);
1510
1511 add_ls_binding_to_cache(port, vlan, ls);
1512 commit_ls_bindings(port);
1513
1514 vtep_ctl_context_invalidate_cache(ctx);
1515 }
1516
1517 static void
1518 cmd_unbind_ls(struct ctl_context *ctx)
1519 {
1520 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1521 struct vtep_ctl_port *port;
1522 const char *vlan;
1523
1524 vtep_ctl_context_populate_cache(ctx);
1525
1526 port = find_port(vtepctl_ctx, ctx->argv[1], ctx->argv[2], true);
1527 vlan = ctx->argv[3];
1528
1529 del_cached_ls_binding(port, vlan);
1530 commit_ls_bindings(port);
1531
1532 vtep_ctl_context_invalidate_cache(ctx);
1533 }
1534
1535 static void
1536 cmd_set_replication_mode(struct ctl_context *ctx)
1537 {
1538 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1539 struct vtep_ctl_lswitch *ls;
1540 const char *ls_name = ctx->argv[1];
1541
1542 vtep_ctl_context_populate_cache(ctx);
1543
1544 if (strcmp(ctx->argv[2], "service_node") &&
1545 strcmp(ctx->argv[2], "source_node")) {
1546 ctl_fatal("Replication mode must be 'service_node' or 'source_node'");
1547 }
1548
1549 ls = find_lswitch(vtepctl_ctx, ls_name, true);
1550 vteprec_logical_switch_set_replication_mode(ls->ls_cfg, ctx->argv[2]);
1551
1552 vtep_ctl_context_invalidate_cache(ctx);
1553 }
1554
1555 static void
1556 cmd_get_replication_mode(struct ctl_context *ctx)
1557 {
1558 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1559 struct vtep_ctl_lswitch *ls;
1560 const char *ls_name = ctx->argv[1];
1561
1562 vtep_ctl_context_populate_cache(ctx);
1563
1564 ls = find_lswitch(vtepctl_ctx, ls_name, true);
1565 ds_put_format(&ctx->output, "%s\n", ls->ls_cfg->replication_mode);
1566 }
1567
1568 static struct vtep_ctl_lrouter *
1569 find_lrouter(struct vtep_ctl_context *vtepctl_ctx,
1570 const char *name, bool must_exist)
1571 {
1572 struct vtep_ctl_lrouter *lr;
1573
1574 ovs_assert(vtepctl_ctx->cache_valid);
1575
1576 lr = shash_find_data(&vtepctl_ctx->lrouters, name);
1577 if (must_exist && !lr) {
1578 ctl_fatal("no logical router named %s", name);
1579 }
1580 return lr;
1581 }
1582
1583 static void
1584 cmd_add_lr(struct ctl_context *ctx)
1585 {
1586 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1587 const char *lr_name = ctx->argv[1];
1588 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1589 struct vteprec_logical_router *lr;
1590
1591 vtep_ctl_context_populate_cache(ctx);
1592 if (find_lrouter(vtepctl_ctx, lr_name, false)) {
1593 if (!may_exist) {
1594 ctl_fatal("cannot create logical router %s because it "
1595 "already exists", lr_name);
1596 }
1597 return;
1598 }
1599
1600 lr = vteprec_logical_router_insert(ctx->txn);
1601 vteprec_logical_router_set_name(lr, lr_name);
1602
1603 vtep_ctl_context_invalidate_cache(ctx);
1604 }
1605
1606 static void
1607 del_lrouter(struct vtep_ctl_context *vtepctl_ctx, struct vtep_ctl_lrouter *lr)
1608 {
1609 del_cached_lrouter(vtepctl_ctx, lr);
1610 }
1611
1612 static void
1613 cmd_del_lr(struct ctl_context *ctx)
1614 {
1615 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1616 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1617 struct vtep_ctl_lrouter *lr;
1618
1619 vtep_ctl_context_populate_cache(ctx);
1620 lr = find_lrouter(vtepctl_ctx, ctx->argv[1], must_exist);
1621 if (lr) {
1622 del_lrouter(vtepctl_ctx, lr);
1623 }
1624 }
1625
1626 static void
1627 cmd_list_lr(struct ctl_context *ctx)
1628 {
1629 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1630 struct shash_node *node;
1631 struct svec lrouters;
1632
1633 vtep_ctl_context_populate_cache(ctx);
1634
1635 svec_init(&lrouters);
1636 SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
1637 struct vtep_ctl_lrouter *lr = node->data;
1638
1639 svec_add(&lrouters, lr->name);
1640 }
1641 output_sorted(&lrouters, &ctx->output);
1642 svec_destroy(&lrouters);
1643 }
1644
1645 static void
1646 cmd_lr_exists(struct ctl_context *ctx)
1647 {
1648 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1649
1650 vtep_ctl_context_populate_cache(ctx);
1651 if (!find_lrouter(vtepctl_ctx, ctx->argv[1], false)) {
1652 vtep_ctl_exit(2);
1653 }
1654 }
1655
1656 static void
1657 add_ucast_entry(struct ctl_context *ctx, bool local)
1658 {
1659 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1660 struct vtep_ctl_lswitch *ls;
1661 const char *mac;
1662 const char *encap;
1663 const char *dst_ip;
1664 struct vteprec_physical_locator *ploc_cfg;
1665
1666 vtep_ctl_context_populate_cache(ctx);
1667
1668 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1669 mac = ctx->argv[2];
1670
1671 if (ctx->argc == 4) {
1672 encap = "vxlan_over_ipv4";
1673 dst_ip = ctx->argv[3];
1674 } else {
1675 encap = ctx->argv[3];
1676 dst_ip = ctx->argv[4];
1677 }
1678
1679 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1680 if (!ploc_cfg) {
1681 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1682 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1683 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1684
1685 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
1686 }
1687
1688 if (local) {
1689 struct vteprec_ucast_macs_local *ucast_cfg;
1690
1691 ucast_cfg = shash_find_data(&ls->ucast_local, mac);
1692 if (!ucast_cfg) {
1693 ucast_cfg = vteprec_ucast_macs_local_insert(ctx->txn);
1694 vteprec_ucast_macs_local_set_MAC(ucast_cfg, mac);
1695 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg, ls->ls_cfg);
1696 shash_add(&ls->ucast_local, mac, ucast_cfg);
1697 }
1698 vteprec_ucast_macs_local_set_locator(ucast_cfg, ploc_cfg);
1699 } else {
1700 struct vteprec_ucast_macs_remote *ucast_cfg;
1701
1702 ucast_cfg = shash_find_data(&ls->ucast_remote, mac);
1703 if (!ucast_cfg) {
1704 ucast_cfg = vteprec_ucast_macs_remote_insert(ctx->txn);
1705 vteprec_ucast_macs_remote_set_MAC(ucast_cfg, mac);
1706 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg, ls->ls_cfg);
1707 shash_add(&ls->ucast_remote, mac, ucast_cfg);
1708 }
1709 vteprec_ucast_macs_remote_set_locator(ucast_cfg, ploc_cfg);
1710 }
1711
1712 vtep_ctl_context_invalidate_cache(ctx);
1713 }
1714
1715 static void
1716 cmd_add_ucast_local(struct ctl_context *ctx)
1717 {
1718 add_ucast_entry(ctx, true);
1719 }
1720
1721 static void
1722 cmd_add_ucast_remote(struct ctl_context *ctx)
1723 {
1724 add_ucast_entry(ctx, false);
1725 }
1726
1727 static void
1728 del_ucast_entry(struct ctl_context *ctx, bool local)
1729 {
1730 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1731 struct vtep_ctl_lswitch *ls;
1732 struct shash *ucast_shash;
1733 struct shash_node *node;
1734
1735 vtep_ctl_context_populate_cache(ctx);
1736
1737 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1738 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1739
1740 node = shash_find(ucast_shash, ctx->argv[2]);
1741 if (!node) {
1742 return;
1743 }
1744
1745 if (local) {
1746 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1747 vteprec_ucast_macs_local_delete(ucast_cfg);
1748 } else {
1749 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1750 vteprec_ucast_macs_remote_delete(ucast_cfg);
1751 }
1752 shash_delete(ucast_shash, node);
1753
1754 vtep_ctl_context_invalidate_cache(ctx);
1755 }
1756
1757 static void
1758 cmd_del_ucast_local(struct ctl_context *ctx)
1759 {
1760 del_ucast_entry(ctx, true);
1761 }
1762
1763 static void
1764 cmd_del_ucast_remote(struct ctl_context *ctx)
1765 {
1766 del_ucast_entry(ctx, false);
1767 }
1768
1769 static void
1770 commit_mcast_entries(struct vtep_ctl_mcast_mac *mcast_mac)
1771 {
1772 struct vtep_ctl_ploc *ploc;
1773 struct vteprec_physical_locator **locators = NULL;
1774 size_t n_locators;
1775 int i;
1776
1777 n_locators = ovs_list_size(&mcast_mac->locators);
1778 ovs_assert(n_locators);
1779
1780 locators = xmalloc(n_locators * sizeof *locators);
1781
1782 i = 0;
1783 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
1784 locators[i] = (struct vteprec_physical_locator *)ploc->ploc_cfg;
1785 i++;
1786 }
1787
1788 vteprec_physical_locator_set_set_locators(mcast_mac->ploc_set_cfg,
1789 locators,
1790 n_locators);
1791
1792 free(locators);
1793 }
1794
1795 static void
1796 add_mcast_entry(struct ctl_context *ctx,
1797 struct vtep_ctl_lswitch *ls, const char *mac,
1798 const char *encap, const char *dst_ip, bool local)
1799 {
1800 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1801 struct shash *mcast_shash;
1802 struct vtep_ctl_mcast_mac *mcast_mac;
1803 struct vteprec_physical_locator *ploc_cfg;
1804 struct vteprec_physical_locator_set *ploc_set_cfg;
1805
1806 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1807
1808 /* Physical locator sets are immutable, so allocate a new one. */
1809 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1810
1811 mcast_mac = shash_find_data(mcast_shash, mac);
1812 if (!mcast_mac) {
1813 mcast_mac = add_mcast_mac_to_cache(vtepctl_ctx, ls, mac, ploc_set_cfg,
1814 local);
1815
1816 if (local) {
1817 mcast_mac->local_cfg = vteprec_mcast_macs_local_insert(ctx->txn);
1818 vteprec_mcast_macs_local_set_MAC(mcast_mac->local_cfg, mac);
1819 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1820 ploc_set_cfg);
1821 vteprec_mcast_macs_local_set_logical_switch(mcast_mac->local_cfg,
1822 ls->ls_cfg);
1823 mcast_mac->remote_cfg = NULL;
1824 } else {
1825 mcast_mac->remote_cfg = vteprec_mcast_macs_remote_insert(ctx->txn);
1826 vteprec_mcast_macs_remote_set_MAC(mcast_mac->remote_cfg, mac);
1827 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1828 ploc_set_cfg);
1829 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac->remote_cfg,
1830 ls->ls_cfg);
1831 mcast_mac->local_cfg = NULL;
1832 }
1833 } else {
1834 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1835 if (local) {
1836 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1837 ploc_set_cfg);
1838 } else {
1839 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1840 ploc_set_cfg);
1841 }
1842 }
1843
1844 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1845 if (!ploc_cfg) {
1846 ploc_cfg = vteprec_physical_locator_insert(ctx->txn);
1847 vteprec_physical_locator_set_dst_ip(ploc_cfg, dst_ip);
1848 vteprec_physical_locator_set_encapsulation_type(ploc_cfg, encap);
1849
1850 add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
1851 }
1852
1853 add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
1854 commit_mcast_entries(mcast_mac);
1855 }
1856
1857 static void
1858 del_mcast_entry(struct ctl_context *ctx,
1859 struct vtep_ctl_lswitch *ls, const char *mac,
1860 const char *encap, const char *dst_ip, bool local)
1861 {
1862 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1863 struct vtep_ctl_mcast_mac *mcast_mac;
1864 struct shash *mcast_shash;
1865 struct vteprec_physical_locator *ploc_cfg;
1866 struct vteprec_physical_locator_set *ploc_set_cfg;
1867
1868 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1869
1870 mcast_mac = shash_find_data(mcast_shash, mac);
1871 if (!mcast_mac) {
1872 return;
1873 }
1874
1875 ploc_cfg = find_ploc(vtepctl_ctx, encap, dst_ip);
1876 if (!ploc_cfg) {
1877 /* Couldn't find the physical locator, so just ignore. */
1878 return;
1879 }
1880
1881 /* Physical locator sets are immutable, so allocate a new one. */
1882 ploc_set_cfg = vteprec_physical_locator_set_insert(ctx->txn);
1883 mcast_mac->ploc_set_cfg = ploc_set_cfg;
1884
1885 del_ploc_from_mcast_mac(mcast_mac, ploc_cfg);
1886 if (ovs_list_is_empty(&mcast_mac->locators)) {
1887 struct shash_node *node = shash_find(mcast_shash, mac);
1888
1889 vteprec_physical_locator_set_delete(ploc_set_cfg);
1890
1891 if (local) {
1892 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1893 } else {
1894 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1895 }
1896
1897 free(node->data);
1898 shash_delete(mcast_shash, node);
1899 } else {
1900 if (local) {
1901 vteprec_mcast_macs_local_set_locator_set(mcast_mac->local_cfg,
1902 ploc_set_cfg);
1903 } else {
1904 vteprec_mcast_macs_remote_set_locator_set(mcast_mac->remote_cfg,
1905 ploc_set_cfg);
1906 }
1907 commit_mcast_entries(mcast_mac);
1908 }
1909 }
1910
1911 static void
1912 add_del_mcast_entry(struct ctl_context *ctx, bool add, bool local)
1913 {
1914 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1915 struct vtep_ctl_lswitch *ls;
1916 const char *mac;
1917 const char *encap;
1918 const char *dst_ip;
1919
1920 vtep_ctl_context_populate_cache(ctx);
1921
1922 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1923 mac = ctx->argv[2];
1924
1925 if (ctx->argc == 4) {
1926 encap = "vxlan_over_ipv4";
1927 dst_ip = ctx->argv[3];
1928 } else {
1929 encap = ctx->argv[3];
1930 dst_ip = ctx->argv[4];
1931 }
1932
1933 if (add) {
1934 add_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1935 } else {
1936 del_mcast_entry(ctx, ls, mac, encap, dst_ip, local);
1937 }
1938
1939 vtep_ctl_context_invalidate_cache(ctx);
1940 }
1941
1942 static void
1943 cmd_add_mcast_local(struct ctl_context *ctx)
1944 {
1945 add_del_mcast_entry(ctx, true, true);
1946 }
1947
1948 static void
1949 cmd_add_mcast_remote(struct ctl_context *ctx)
1950 {
1951 add_del_mcast_entry(ctx, true, false);
1952 }
1953
1954 static void
1955 cmd_del_mcast_local(struct ctl_context *ctx)
1956 {
1957 add_del_mcast_entry(ctx, false, true);
1958 }
1959
1960 static void
1961 cmd_del_mcast_remote(struct ctl_context *ctx)
1962 {
1963 add_del_mcast_entry(ctx, false, false);
1964 }
1965
1966 static void
1967 clear_macs(struct ctl_context *ctx, bool local)
1968 {
1969 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
1970 struct vtep_ctl_lswitch *ls;
1971 const struct shash_node *node;
1972 struct shash *ucast_shash;
1973 struct shash *mcast_shash;
1974
1975 vtep_ctl_context_populate_cache(ctx);
1976 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
1977
1978 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
1979 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
1980
1981 SHASH_FOR_EACH (node, ucast_shash) {
1982 if (local) {
1983 struct vteprec_ucast_macs_local *ucast_cfg = node->data;
1984 vteprec_ucast_macs_local_delete(ucast_cfg);
1985 } else {
1986 struct vteprec_ucast_macs_remote *ucast_cfg = node->data;
1987 vteprec_ucast_macs_remote_delete(ucast_cfg);
1988 }
1989 }
1990
1991 SHASH_FOR_EACH (node, mcast_shash) {
1992 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
1993 if (local) {
1994 vteprec_mcast_macs_local_delete(mcast_mac->local_cfg);
1995 } else {
1996 vteprec_mcast_macs_remote_delete(mcast_mac->remote_cfg);
1997 }
1998 }
1999
2000 vtep_ctl_context_invalidate_cache(ctx);
2001 }
2002
2003 static void
2004 cmd_clear_local_macs(struct ctl_context *ctx)
2005 {
2006 clear_macs(ctx, true);
2007 }
2008
2009 static void
2010 cmd_clear_remote_macs(struct ctl_context *ctx)
2011 {
2012 clear_macs(ctx, false);
2013 }
2014
2015 static void
2016 list_macs(struct ctl_context *ctx, bool local)
2017 {
2018 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2019 struct vtep_ctl_lswitch *ls;
2020 const struct shash_node *node;
2021 struct shash *ucast_shash;
2022 struct svec ucast_macs;
2023 struct shash *mcast_shash;
2024 struct svec mcast_macs;
2025
2026 vtep_ctl_context_populate_cache(ctx);
2027 ls = find_lswitch(vtepctl_ctx, ctx->argv[1], true);
2028
2029 ucast_shash = local ? &ls->ucast_local : &ls->ucast_remote;
2030 mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
2031
2032 svec_init(&ucast_macs);
2033 SHASH_FOR_EACH (node, ucast_shash) {
2034 struct vteprec_ucast_macs_local *ucast_local = node->data;
2035 struct vteprec_ucast_macs_remote *ucast_remote = node->data;
2036 struct vteprec_physical_locator *ploc_cfg;
2037 char *entry;
2038
2039 ploc_cfg = local ? ucast_local->locator : ucast_remote->locator;
2040
2041 entry = xasprintf(" %s -> %s/%s", node->name,
2042 ploc_cfg->encapsulation_type, ploc_cfg->dst_ip);
2043 svec_add_nocopy(&ucast_macs, entry);
2044 }
2045 ds_put_format(&ctx->output, "ucast-mac-%s\n", local ? "local" : "remote");
2046 output_sorted(&ucast_macs, &ctx->output);
2047 ds_put_char(&ctx->output, '\n');
2048 svec_destroy(&ucast_macs);
2049
2050 svec_init(&mcast_macs);
2051 SHASH_FOR_EACH (node, mcast_shash) {
2052 struct vtep_ctl_mcast_mac *mcast_mac = node->data;
2053 struct vtep_ctl_ploc *ploc;
2054 char *entry;
2055
2056 LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
2057 entry = xasprintf(" %s -> %s/%s", node->name,
2058 ploc->ploc_cfg->encapsulation_type,
2059 ploc->ploc_cfg->dst_ip);
2060 svec_add_nocopy(&mcast_macs, entry);
2061 }
2062 }
2063 ds_put_format(&ctx->output, "mcast-mac-%s\n", local ? "local" : "remote");
2064 output_sorted(&mcast_macs, &ctx->output);
2065 ds_put_char(&ctx->output, '\n');
2066 svec_destroy(&mcast_macs);
2067 }
2068
2069 static void
2070 cmd_list_local_macs(struct ctl_context *ctx)
2071 {
2072 list_macs(ctx, true);
2073 }
2074
2075 static void
2076 cmd_list_remote_macs(struct ctl_context *ctx)
2077 {
2078 list_macs(ctx, false);
2079 }
2080
2081 static void
2082 verify_managers(const struct vteprec_global *vtep_global)
2083 {
2084 size_t i;
2085
2086 vteprec_global_verify_managers(vtep_global);
2087
2088 for (i = 0; i < vtep_global->n_managers; ++i) {
2089 const struct vteprec_manager *mgr = vtep_global->managers[i];
2090
2091 vteprec_manager_verify_target(mgr);
2092 }
2093 }
2094
2095 static void
2096 pre_manager(struct ctl_context *ctx)
2097 {
2098 ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_managers);
2099 ovsdb_idl_add_column(ctx->idl, &vteprec_manager_col_target);
2100 }
2101
2102 static void
2103 cmd_get_manager(struct ctl_context *ctx)
2104 {
2105 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2106 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2107 struct svec targets;
2108 size_t i;
2109
2110 verify_managers(vtep_global);
2111
2112 /* Print the targets in sorted order for reproducibility. */
2113 svec_init(&targets);
2114
2115 for (i = 0; i < vtep_global->n_managers; i++) {
2116 svec_add(&targets, vtep_global->managers[i]->target);
2117 }
2118
2119 svec_sort_unique(&targets);
2120 for (i = 0; i < targets.n; i++) {
2121 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2122 }
2123 svec_destroy(&targets);
2124 }
2125
2126 static void
2127 delete_managers(const struct vtep_ctl_context *vtepctl_ctx)
2128 {
2129 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2130 size_t i;
2131
2132 /* Delete Manager rows pointed to by 'managers' column. */
2133 for (i = 0; i < vtep_global->n_managers; i++) {
2134 vteprec_manager_delete(vtep_global->managers[i]);
2135 }
2136
2137 /* Delete 'Manager' row refs in 'managers' column. */
2138 vteprec_global_set_managers(vtep_global, NULL, 0);
2139 }
2140
2141 static void
2142 cmd_del_manager(struct ctl_context *ctx)
2143 {
2144 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2145 const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
2146
2147 verify_managers(vtep_global);
2148 delete_managers(vtepctl_ctx);
2149 }
2150
2151 static void
2152 insert_managers(struct vtep_ctl_context *vtepctl_ctx, char *targets[], size_t n)
2153 {
2154 struct vteprec_manager **managers;
2155 size_t i;
2156
2157 /* Insert each manager in a new row in Manager table. */
2158 managers = xmalloc(n * sizeof *managers);
2159 for (i = 0; i < n; i++) {
2160 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2161 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2162 }
2163 managers[i] = vteprec_manager_insert(vtepctl_ctx->base.txn);
2164 vteprec_manager_set_target(managers[i], targets[i]);
2165 }
2166
2167 /* Store uuids of new Manager rows in 'managers' column. */
2168 vteprec_global_set_managers(vtepctl_ctx->vtep_global, managers, n);
2169 free(managers);
2170 }
2171
2172 static void
2173 cmd_set_manager(struct ctl_context *ctx)
2174 {
2175 struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
2176 const size_t n = ctx->argc - 1;
2177
2178 verify_managers(vtepctl_ctx->vtep_global);
2179 delete_managers(vtepctl_ctx);
2180 insert_managers(vtepctl_ctx, &ctx->argv[1], n);
2181 }
2182
2183 /* Parameter commands. */
2184 static const struct ctl_table_class tables[VTEPREC_N_TABLES] = {
2185 [VTEPREC_TABLE_LOGICAL_SWITCH].row_ids[0]
2186 = {&vteprec_logical_switch_col_name, NULL, NULL},
2187
2188 [VTEPREC_TABLE_MANAGER].row_ids[0]
2189 = {&vteprec_manager_col_target, NULL, NULL},
2190
2191 [VTEPREC_TABLE_PHYSICAL_PORT].row_ids[0]
2192 = {&vteprec_physical_port_col_name, NULL, NULL},
2193
2194 [VTEPREC_TABLE_PHYSICAL_SWITCH].row_ids[0]
2195 = {&vteprec_physical_switch_col_name, NULL, NULL},
2196
2197 [VTEPREC_TABLE_LOGICAL_ROUTER].row_ids[0]
2198 = {&vteprec_logical_router_col_name, NULL, NULL},
2199 };
2200
2201 \f
2202 static void
2203 vtep_ctl_context_init_command(struct vtep_ctl_context *vtepctl_ctx,
2204 struct ctl_command *command)
2205 {
2206 ctl_context_init_command(&vtepctl_ctx->base, command);
2207 vtepctl_ctx->verified_ports = false;
2208
2209 }
2210
2211 static void
2212 vtep_ctl_context_init(struct vtep_ctl_context *vtepctl_ctx,
2213 struct ctl_command *command,
2214 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2215 const struct vteprec_global *vtep_global,
2216 struct ovsdb_symbol_table *symtab)
2217 {
2218 ctl_context_init(&vtepctl_ctx->base, command, idl, txn, symtab,
2219 vtep_ctl_context_invalidate_cache);
2220 if (command) {
2221 vtepctl_ctx->verified_ports = false;
2222 }
2223 vtepctl_ctx->vtep_global = vtep_global;
2224 vtepctl_ctx->cache_valid = false;
2225 }
2226
2227 static void
2228 vtep_ctl_context_done_command(struct vtep_ctl_context *vtepctl_ctx,
2229 struct ctl_command *command)
2230 {
2231 ctl_context_done_command(&vtepctl_ctx->base, command);
2232 }
2233
2234 static void
2235 vtep_ctl_context_done(struct vtep_ctl_context *vtepctl_ctx,
2236 struct ctl_command *command)
2237 {
2238 ctl_context_done(&vtepctl_ctx->base, command);
2239 }
2240
2241 static void
2242 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2243 struct ovsdb_idl *idl)
2244 {
2245 struct ctl_command *c;
2246
2247 ovsdb_idl_add_table(idl, &vteprec_table_global);
2248 for (c = commands; c < &commands[n_commands]; c++) {
2249 if (c->syntax->prerequisites) {
2250 struct vtep_ctl_context vtepctl_ctx;
2251
2252 ds_init(&c->output);
2253 c->table = NULL;
2254
2255 vtep_ctl_context_init(&vtepctl_ctx, c, idl, NULL, NULL, NULL);
2256 (c->syntax->prerequisites)(&vtepctl_ctx.base);
2257 vtep_ctl_context_done(&vtepctl_ctx, c);
2258
2259 ovs_assert(!c->output.string);
2260 ovs_assert(!c->table);
2261 }
2262 }
2263 }
2264
2265 static bool
2266 do_vtep_ctl(const char *args, struct ctl_command *commands,
2267 size_t n_commands, struct ovsdb_idl *idl)
2268 {
2269 struct ovsdb_idl_txn *txn;
2270 const struct vteprec_global *vtep_global;
2271 enum ovsdb_idl_txn_status status;
2272 struct ovsdb_symbol_table *symtab;
2273 struct vtep_ctl_context vtepctl_ctx;
2274 struct ctl_command *c;
2275 struct shash_node *node;
2276 char *error = NULL;
2277
2278 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2279 if (dry_run) {
2280 ovsdb_idl_txn_set_dry_run(txn);
2281 }
2282
2283 ovsdb_idl_txn_add_comment(txn, "vtep-ctl: %s", args);
2284
2285 vtep_global = vteprec_global_first(idl);
2286 if (!vtep_global) {
2287 /* XXX add verification that table is empty */
2288 vtep_global = vteprec_global_insert(txn);
2289 }
2290
2291 symtab = ovsdb_symbol_table_create();
2292 for (c = commands; c < &commands[n_commands]; c++) {
2293 ds_init(&c->output);
2294 c->table = NULL;
2295 }
2296 vtep_ctl_context_init(&vtepctl_ctx, NULL, idl, txn, vtep_global, symtab);
2297 for (c = commands; c < &commands[n_commands]; c++) {
2298 vtep_ctl_context_init_command(&vtepctl_ctx, c);
2299 if (c->syntax->run) {
2300 (c->syntax->run)(&vtepctl_ctx.base);
2301 }
2302 vtep_ctl_context_done_command(&vtepctl_ctx, c);
2303
2304 if (vtepctl_ctx.base.try_again) {
2305 vtep_ctl_context_done(&vtepctl_ctx, NULL);
2306 goto try_again;
2307 }
2308 }
2309 vtep_ctl_context_done(&vtepctl_ctx, NULL);
2310
2311 SHASH_FOR_EACH (node, &symtab->sh) {
2312 struct ovsdb_symbol *symbol = node->data;
2313 if (!symbol->created) {
2314 ctl_fatal("row id \"%s\" is referenced but never created "
2315 "(e.g. with \"-- --id=%s create ...\")",
2316 node->name, node->name);
2317 }
2318 if (!symbol->strong_ref) {
2319 if (!symbol->weak_ref) {
2320 VLOG_WARN("row id \"%s\" was created but no reference to it "
2321 "was inserted, so it will not actually appear in "
2322 "the database", node->name);
2323 } else {
2324 VLOG_WARN("row id \"%s\" was created but only a weak "
2325 "reference to it was inserted, so it will not "
2326 "actually appear in the database", node->name);
2327 }
2328 }
2329 }
2330
2331 status = ovsdb_idl_txn_commit_block(txn);
2332 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2333 for (c = commands; c < &commands[n_commands]; c++) {
2334 if (c->syntax->postprocess) {
2335 vtep_ctl_context_init(&vtepctl_ctx, c, idl, txn, vtep_global, symtab);
2336 (c->syntax->postprocess)(&vtepctl_ctx.base);
2337 vtep_ctl_context_done(&vtepctl_ctx, c);
2338 }
2339 }
2340 }
2341 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2342 ovsdb_idl_txn_destroy(txn);
2343 txn = the_idl_txn = NULL;
2344
2345 switch (status) {
2346 case TXN_UNCOMMITTED:
2347 case TXN_INCOMPLETE:
2348 OVS_NOT_REACHED();
2349
2350 case TXN_ABORTED:
2351 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2352 ctl_fatal("transaction aborted");
2353
2354 case TXN_UNCHANGED:
2355 case TXN_SUCCESS:
2356 break;
2357
2358 case TXN_TRY_AGAIN:
2359 goto try_again;
2360
2361 case TXN_ERROR:
2362 ctl_fatal("transaction error: %s", error);
2363
2364 case TXN_NOT_LOCKED:
2365 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2366 ctl_fatal("database not locked");
2367
2368 default:
2369 OVS_NOT_REACHED();
2370 }
2371 free(error);
2372
2373 ovsdb_symbol_table_destroy(symtab);
2374
2375 for (c = commands; c < &commands[n_commands]; c++) {
2376 struct ds *ds = &c->output;
2377
2378 if (c->table) {
2379 table_print(c->table, &table_style);
2380 } else if (oneline) {
2381 size_t j;
2382
2383 ds_chomp(ds, '\n');
2384 for (j = 0; j < ds->length; j++) {
2385 int ch = ds->string[j];
2386 switch (ch) {
2387 case '\n':
2388 fputs("\\n", stdout);
2389 break;
2390
2391 case '\\':
2392 fputs("\\\\", stdout);
2393 break;
2394
2395 default:
2396 putchar(ch);
2397 }
2398 }
2399 putchar('\n');
2400 } else {
2401 fputs(ds_cstr(ds), stdout);
2402 }
2403 ds_destroy(&c->output);
2404 table_destroy(c->table);
2405 free(c->table);
2406
2407 shash_destroy_free_data(&c->options);
2408 }
2409 free(commands);
2410
2411 ovsdb_idl_destroy(idl);
2412
2413 return true;
2414
2415 try_again:
2416 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2417 * resources and return so that the caller can try again. */
2418 if (txn) {
2419 ovsdb_idl_txn_abort(txn);
2420 ovsdb_idl_txn_destroy(txn);
2421 }
2422 ovsdb_symbol_table_destroy(symtab);
2423 for (c = commands; c < &commands[n_commands]; c++) {
2424 ds_destroy(&c->output);
2425 table_destroy(c->table);
2426 free(c->table);
2427 }
2428 free(error);
2429 return false;
2430 }
2431
2432 static const struct ctl_command_syntax vtep_commands[] = {
2433 /* Physical Switch commands. */
2434 {"add-ps", 1, 1, NULL, pre_get_info, cmd_add_ps, NULL, "--may-exist", RW},
2435 {"del-ps", 1, 1, NULL, pre_get_info, cmd_del_ps, NULL, "--if-exists", RW},
2436 {"list-ps", 0, 0, NULL, pre_get_info, cmd_list_ps, NULL, "", RO},
2437 {"ps-exists", 1, 1, NULL, pre_get_info, cmd_ps_exists, NULL, "", RO},
2438
2439 /* Port commands. */
2440 {"list-ports", 1, 1, NULL, pre_get_info, cmd_list_ports, NULL, "", RO},
2441 {"add-port", 2, 2, NULL, pre_get_info, cmd_add_port, NULL, "--may-exist",
2442 RW},
2443 {"del-port", 2, 2, NULL, pre_get_info, cmd_del_port, NULL, "--if-exists",
2444 RW},
2445
2446 /* Logical Switch commands. */
2447 {"add-ls", 1, 1, NULL, pre_get_info, cmd_add_ls, NULL, "--may-exist", RW},
2448 {"del-ls", 1, 1, NULL, pre_get_info, cmd_del_ls, NULL, "--if-exists", RW},
2449 {"list-ls", 0, 0, NULL, pre_get_info, cmd_list_ls, NULL, "", RO},
2450 {"ls-exists", 1, 1, NULL, pre_get_info, cmd_ls_exists, NULL, "", RO},
2451 {"list-bindings", 2, 2, NULL, pre_get_info, cmd_list_bindings, NULL, "", RO},
2452 {"bind-ls", 4, 4, NULL, pre_get_info, cmd_bind_ls, NULL, "", RO},
2453 {"unbind-ls", 3, 3, NULL, pre_get_info, cmd_unbind_ls, NULL, "", RO},
2454 {"set-replication-mode", 2, 2, "LS MODE", pre_get_info,
2455 cmd_set_replication_mode, NULL, "", RW},
2456 {"get-replication-mode", 1, 1, "LS", pre_get_info,
2457 cmd_get_replication_mode, NULL, "", RO},
2458
2459 /* Logical Router commands. */
2460 {"add-lr", 1, 1, NULL, pre_get_info, cmd_add_lr, NULL, "--may-exist", RW},
2461 {"del-lr", 1, 1, NULL, pre_get_info, cmd_del_lr, NULL, "--if-exists", RW},
2462 {"list-lr", 0, 0, NULL, pre_get_info, cmd_list_lr, NULL, "", RO},
2463 {"lr-exists", 1, 1, NULL, pre_get_info, cmd_lr_exists, NULL, "", RO},
2464
2465 /* MAC binding commands. */
2466 {"add-ucast-local", 3, 4, NULL, pre_get_info, cmd_add_ucast_local, NULL,
2467 "", RW},
2468 {"del-ucast-local", 2, 2, NULL, pre_get_info, cmd_del_ucast_local, NULL,
2469 "", RW},
2470 {"add-mcast-local", 3, 4, NULL, pre_get_info, cmd_add_mcast_local, NULL,
2471 "", RW},
2472 {"del-mcast-local", 3, 4, NULL, pre_get_info, cmd_del_mcast_local, NULL,
2473 "", RW},
2474 {"clear-local-macs", 1, 1, NULL, pre_get_info, cmd_clear_local_macs, NULL,
2475 "", RO},
2476 {"list-local-macs", 1, 1, NULL, pre_get_info, cmd_list_local_macs, NULL,
2477 "", RO},
2478 {"add-ucast-remote", 3, 4, NULL, pre_get_info, cmd_add_ucast_remote, NULL,
2479 "", RW},
2480 {"del-ucast-remote", 2, 2, NULL, pre_get_info, cmd_del_ucast_remote, NULL,
2481 "", RW},
2482 {"add-mcast-remote", 3, 4, NULL, pre_get_info, cmd_add_mcast_remote, NULL,
2483 "", RW},
2484 {"del-mcast-remote", 3, 4, NULL, pre_get_info, cmd_del_mcast_remote, NULL,
2485 "", RW},
2486 {"clear-remote-macs", 1, 1, NULL, pre_get_info, cmd_clear_remote_macs, NULL,
2487 "", RO},
2488 {"list-remote-macs", 1, 1, NULL, pre_get_info, cmd_list_remote_macs, NULL,
2489 "", RO},
2490
2491 /* Manager commands. */
2492 {"get-manager", 0, 0, NULL, pre_manager, cmd_get_manager, NULL, "", RO},
2493 {"del-manager", 0, 0, NULL, pre_manager, cmd_del_manager, NULL, "", RW},
2494 {"set-manager", 1, INT_MAX, NULL, pre_manager, cmd_set_manager, NULL, "",
2495 RW},
2496
2497 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2498 };
2499
2500 /* Registers vsctl and common db commands. */
2501 static void
2502 vtep_ctl_cmd_init(void)
2503 {
2504 ctl_init(&vteprec_idl_class, vteprec_table_classes, tables,
2505 cmd_show_tables, vtep_ctl_exit);
2506 ctl_register_commands(vtep_commands);
2507 }