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