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