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