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