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