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