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