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