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