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