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