]>
Commit | Line | Data |
---|---|---|
c75d1511 | 1 | /* |
f158c54a | 2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. |
c75d1511 BP |
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 <assert.h> | |
ad83bfa6 | 20 | #include <ctype.h> |
c75d1511 | 21 | #include <errno.h> |
ad83bfa6 | 22 | #include <float.h> |
c75d1511 BP |
23 | #include <getopt.h> |
24 | #include <inttypes.h> | |
25 | #include <signal.h> | |
26 | #include <stdarg.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | ||
30 | #include "command-line.h" | |
31 | #include "compiler.h" | |
32 | #include "dirs.h" | |
33 | #include "dynamic-string.h" | |
b54e22e9 | 34 | #include "json.h" |
ad83bfa6 | 35 | #include "ovsdb-data.h" |
c75d1511 BP |
36 | #include "ovsdb-idl.h" |
37 | #include "poll-loop.h" | |
f8ff4bc4 | 38 | #include "process.h" |
218a6f59 | 39 | #include "stream-ssl.h" |
dfbe07ba | 40 | #include "svec.h" |
c75d1511 | 41 | #include "vswitchd/vswitch-idl.h" |
e051b42c | 42 | #include "table.h" |
c75d1511 BP |
43 | #include "timeval.h" |
44 | #include "util.h" | |
c75d1511 | 45 | #include "vlog.h" |
5136ce49 | 46 | |
d98e6007 | 47 | VLOG_DEFINE_THIS_MODULE(vsctl); |
c75d1511 | 48 | |
def90f62 BP |
49 | /* vsctl_fatal() also logs the error, so it is preferred in this file. */ |
50 | #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal | |
51 | ||
f8ff4bc4 BP |
52 | struct vsctl_context; |
53 | ||
e5e12280 | 54 | /* A command supported by ovs-vsctl. */ |
f8ff4bc4 | 55 | struct vsctl_command_syntax { |
e5e12280 BP |
56 | const char *name; /* e.g. "add-br" */ |
57 | int min_args; /* Min number of arguments following name. */ | |
58 | int max_args; /* Max number of arguments following name. */ | |
59 | ||
60 | /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for | |
61 | * each column or table in ctx->idl that it uses. */ | |
62 | void (*prerequisites)(struct vsctl_context *ctx); | |
63 | ||
64 | /* Does the actual work of the command and puts the command's output, if | |
e051b42c | 65 | * any, in ctx->output or ctx->table. |
e5e12280 BP |
66 | * |
67 | * Alternatively, if some prerequisite of the command is not met and the | |
68 | * caller should wait for something to change and then retry, it may set | |
69 | * ctx->try_again to true. (Only the "wait-until" command currently does | |
70 | * this.) */ | |
71 | void (*run)(struct vsctl_context *ctx); | |
72 | ||
73 | /* If nonnull, called after the transaction has been successfully | |
74 | * committed. ctx->output is the output from the "run" function, which | |
75 | * this function may modify and otherwise postprocess as needed. (Only the | |
76 | * "create" command currently does any postprocessing.) */ | |
77 | void (*postprocess)(struct vsctl_context *ctx); | |
78 | ||
79 | /* A comma-separated list of supported options, e.g. "--a,--b", or the | |
80 | * empty string if the command does not support any options. */ | |
f8ff4bc4 | 81 | const char *options; |
0c18b5a0 | 82 | enum { RO, RW } mode; /* Does this command modify the database? */ |
f8ff4bc4 BP |
83 | }; |
84 | ||
85 | struct vsctl_command { | |
86 | /* Data that remains constant after initialization. */ | |
87 | const struct vsctl_command_syntax *syntax; | |
88 | int argc; | |
89 | char **argv; | |
90 | struct shash options; | |
91 | ||
92 | /* Data modified by commands. */ | |
93 | struct ds output; | |
e051b42c | 94 | struct table *table; |
f8ff4bc4 BP |
95 | }; |
96 | ||
c75d1511 BP |
97 | /* --db: The database server to contact. */ |
98 | static const char *db; | |
99 | ||
100 | /* --oneline: Write each command's output as a single line? */ | |
101 | static bool oneline; | |
102 | ||
577aebdf BP |
103 | /* --dry-run: Do not commit any changes. */ |
104 | static bool dry_run; | |
105 | ||
b54e22e9 BP |
106 | /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */ |
107 | static bool wait_for_reload = true; | |
108 | ||
a39a859a | 109 | /* --timeout: Time to wait for a connection to 'db'. */ |
6b7b9d34 | 110 | static int timeout; |
a39a859a | 111 | |
e051b42c BP |
112 | /* Format for table output. */ |
113 | static struct table_style table_style = TABLE_STYLE_DEFAULT; | |
114 | ||
f8ff4bc4 BP |
115 | /* All supported commands. */ |
116 | static const struct vsctl_command_syntax all_commands[]; | |
117 | ||
1d48b4be BP |
118 | /* The IDL we're using and the current transaction, if any. |
119 | * This is for use by vsctl_exit() only, to allow it to clean up. | |
120 | * Other code should use its context arguments. */ | |
121 | static struct ovsdb_idl *the_idl; | |
122 | static struct ovsdb_idl_txn *the_idl_txn; | |
123 | ||
124 | static void vsctl_exit(int status) NO_RETURN; | |
c88b6a27 | 125 | static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN; |
c75d1511 BP |
126 | static char *default_db(void); |
127 | static void usage(void) NO_RETURN; | |
128 | static void parse_options(int argc, char *argv[]); | |
0c18b5a0 | 129 | static bool might_write_to_db(char **argv); |
c75d1511 | 130 | |
f8ff4bc4 BP |
131 | static struct vsctl_command *parse_commands(int argc, char *argv[], |
132 | size_t *n_commandsp); | |
133 | static void parse_command(int argc, char *argv[], struct vsctl_command *); | |
1998cd4d | 134 | static const struct vsctl_command_syntax *find_command(const char *name); |
e5e12280 BP |
135 | static void run_prerequisites(struct vsctl_command[], size_t n_commands, |
136 | struct ovsdb_idl *); | |
f8ff4bc4 BP |
137 | static void do_vsctl(const char *args, |
138 | struct vsctl_command *, size_t n_commands, | |
139 | struct ovsdb_idl *); | |
c75d1511 | 140 | |
18b239f5 BP |
141 | static const struct vsctl_table_class *get_table(const char *table_name); |
142 | static void set_column(const struct vsctl_table_class *, | |
ce5a3e38 BP |
143 | const struct ovsdb_idl_row *, const char *arg, |
144 | struct ovsdb_symbol_table *); | |
18b239f5 | 145 | |
0a140468 BP |
146 | static bool is_condition_satisfied(const struct vsctl_table_class *, |
147 | const struct ovsdb_idl_row *, | |
148 | const char *arg, | |
149 | struct ovsdb_symbol_table *); | |
150 | ||
c75d1511 BP |
151 | int |
152 | main(int argc, char *argv[]) | |
153 | { | |
480ce8ab | 154 | extern struct vlog_module VLM_reconnect; |
c75d1511 | 155 | struct ovsdb_idl *idl; |
f8ff4bc4 BP |
156 | struct vsctl_command *commands; |
157 | size_t n_commands; | |
158 | char *args; | |
c75d1511 BP |
159 | |
160 | set_program_name(argv[0]); | |
161 | signal(SIGPIPE, SIG_IGN); | |
480ce8ab BP |
162 | vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN); |
163 | vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN); | |
bd76d25d | 164 | ovsrec_init(); |
f8ff4bc4 BP |
165 | |
166 | /* Log our arguments. This is often valuable for debugging systems. */ | |
167 | args = process_escape_args(argv); | |
0c18b5a0 | 168 | VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args); |
f8ff4bc4 BP |
169 | |
170 | /* Parse command line. */ | |
c75d1511 | 171 | parse_options(argc, argv); |
f8ff4bc4 | 172 | commands = parse_commands(argc - optind, argv + optind, &n_commands); |
c75d1511 | 173 | |
a39a859a JP |
174 | if (timeout) { |
175 | time_alarm(timeout); | |
176 | } | |
177 | ||
e5e12280 BP |
178 | /* Initialize IDL. */ |
179 | idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false); | |
180 | run_prerequisites(commands, n_commands, idl); | |
181 | ||
524555d1 | 182 | /* Now execute the commands. */ |
c75d1511 | 183 | for (;;) { |
4ea21243 | 184 | if (ovsdb_idl_run(idl)) { |
f8ff4bc4 | 185 | do_vsctl(args, commands, n_commands, idl); |
c75d1511 BP |
186 | } |
187 | ||
188 | ovsdb_idl_wait(idl); | |
189 | poll_block(); | |
190 | } | |
191 | } | |
192 | ||
193 | static void | |
194 | parse_options(int argc, char *argv[]) | |
195 | { | |
196 | enum { | |
197 | OPT_DB = UCHAR_MAX + 1, | |
198 | OPT_ONELINE, | |
0c3dd1e1 | 199 | OPT_NO_SYSLOG, |
577aebdf | 200 | OPT_NO_WAIT, |
e26b5a06 | 201 | OPT_DRY_RUN, |
218a6f59 | 202 | OPT_PEER_CA_CERT, |
e051b42c BP |
203 | VLOG_OPTION_ENUMS, |
204 | TABLE_OPTION_ENUMS | |
c75d1511 BP |
205 | }; |
206 | static struct option long_options[] = { | |
207 | {"db", required_argument, 0, OPT_DB}, | |
dfbe07ba | 208 | {"no-syslog", no_argument, 0, OPT_NO_SYSLOG}, |
0c3dd1e1 | 209 | {"no-wait", no_argument, 0, OPT_NO_WAIT}, |
577aebdf | 210 | {"dry-run", no_argument, 0, OPT_DRY_RUN}, |
c75d1511 | 211 | {"oneline", no_argument, 0, OPT_ONELINE}, |
342045e1 | 212 | {"timeout", required_argument, 0, 't'}, |
c75d1511 BP |
213 | {"help", no_argument, 0, 'h'}, |
214 | {"version", no_argument, 0, 'V'}, | |
e26b5a06 | 215 | VLOG_LONG_OPTIONS, |
e051b42c | 216 | TABLE_LONG_OPTIONS, |
218a6f59 BP |
217 | #ifdef HAVE_OPENSSL |
218 | STREAM_SSL_LONG_OPTIONS | |
219 | {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT}, | |
220 | #endif | |
c75d1511 BP |
221 | {0, 0, 0, 0}, |
222 | }; | |
a2a9d2d9 | 223 | char *tmp, *short_options; |
c75d1511 | 224 | |
a2a9d2d9 BP |
225 | tmp = long_options_to_short_options(long_options); |
226 | short_options = xasprintf("+%s", tmp); | |
227 | free(tmp); | |
342045e1 | 228 | |
e051b42c BP |
229 | table_style.format = TF_LIST; |
230 | ||
c75d1511 BP |
231 | for (;;) { |
232 | int c; | |
233 | ||
a2a9d2d9 | 234 | c = getopt_long(argc, argv, short_options, long_options, NULL); |
c75d1511 BP |
235 | if (c == -1) { |
236 | break; | |
237 | } | |
238 | ||
239 | switch (c) { | |
240 | case OPT_DB: | |
241 | db = optarg; | |
242 | break; | |
243 | ||
244 | case OPT_ONELINE: | |
245 | oneline = true; | |
246 | break; | |
247 | ||
dfbe07ba | 248 | case OPT_NO_SYSLOG: |
480ce8ab | 249 | vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN); |
dfbe07ba BP |
250 | break; |
251 | ||
0c3dd1e1 | 252 | case OPT_NO_WAIT: |
b54e22e9 | 253 | wait_for_reload = false; |
0c3dd1e1 BP |
254 | break; |
255 | ||
577aebdf BP |
256 | case OPT_DRY_RUN: |
257 | dry_run = true; | |
258 | break; | |
259 | ||
c75d1511 BP |
260 | case 'h': |
261 | usage(); | |
262 | ||
263 | case 'V': | |
264 | OVS_PRINT_VERSION(0, 0); | |
265 | exit(EXIT_SUCCESS); | |
266 | ||
342045e1 BP |
267 | case 't': |
268 | timeout = strtoul(optarg, NULL, 10); | |
a39a859a | 269 | if (timeout < 0) { |
def90f62 BP |
270 | vsctl_fatal("value %s on -t or --timeout is invalid", |
271 | optarg); | |
342045e1 BP |
272 | } |
273 | break; | |
274 | ||
e26b5a06 | 275 | VLOG_OPTION_HANDLERS |
e051b42c | 276 | TABLE_OPTION_HANDLERS(&table_style) |
c75d1511 | 277 | |
218a6f59 BP |
278 | #ifdef HAVE_OPENSSL |
279 | STREAM_SSL_OPTION_HANDLERS | |
280 | ||
281 | case OPT_PEER_CA_CERT: | |
282 | stream_ssl_set_peer_ca_cert_file(optarg); | |
283 | break; | |
284 | #endif | |
285 | ||
c75d1511 BP |
286 | case '?': |
287 | exit(EXIT_FAILURE); | |
288 | ||
289 | default: | |
290 | abort(); | |
291 | } | |
292 | } | |
a2a9d2d9 | 293 | free(short_options); |
c75d1511 BP |
294 | |
295 | if (!db) { | |
296 | db = default_db(); | |
297 | } | |
298 | } | |
299 | ||
f8ff4bc4 BP |
300 | static struct vsctl_command * |
301 | parse_commands(int argc, char *argv[], size_t *n_commandsp) | |
302 | { | |
303 | struct vsctl_command *commands; | |
304 | size_t n_commands, allocated_commands; | |
305 | int i, start; | |
306 | ||
307 | commands = NULL; | |
308 | n_commands = allocated_commands = 0; | |
309 | ||
310 | for (start = i = 0; i <= argc; i++) { | |
311 | if (i == argc || !strcmp(argv[i], "--")) { | |
312 | if (i > start) { | |
313 | if (n_commands >= allocated_commands) { | |
314 | struct vsctl_command *c; | |
315 | ||
316 | commands = x2nrealloc(commands, &allocated_commands, | |
317 | sizeof *commands); | |
318 | for (c = commands; c < &commands[n_commands]; c++) { | |
319 | shash_moved(&c->options); | |
320 | } | |
321 | } | |
322 | parse_command(i - start, &argv[start], | |
323 | &commands[n_commands++]); | |
324 | } | |
325 | start = i + 1; | |
326 | } | |
327 | } | |
328 | if (!n_commands) { | |
329 | vsctl_fatal("missing command name (use --help for help)"); | |
330 | } | |
331 | *n_commandsp = n_commands; | |
332 | return commands; | |
333 | } | |
334 | ||
335 | static void | |
336 | parse_command(int argc, char *argv[], struct vsctl_command *command) | |
337 | { | |
338 | const struct vsctl_command_syntax *p; | |
1998cd4d BP |
339 | struct shash_node *node; |
340 | int n_arg; | |
f8ff4bc4 BP |
341 | int i; |
342 | ||
343 | shash_init(&command->options); | |
344 | for (i = 0; i < argc; i++) { | |
4a033593 BP |
345 | const char *option = argv[i]; |
346 | const char *equals; | |
347 | char *key, *value; | |
348 | ||
349 | if (option[0] != '-') { | |
f8ff4bc4 BP |
350 | break; |
351 | } | |
4a033593 BP |
352 | |
353 | equals = strchr(option, '='); | |
354 | if (equals) { | |
355 | key = xmemdup0(option, equals - option); | |
356 | value = xstrdup(equals + 1); | |
357 | } else { | |
358 | key = xstrdup(option); | |
359 | value = NULL; | |
360 | } | |
361 | ||
362 | if (shash_find(&command->options, key)) { | |
f8ff4bc4 BP |
363 | vsctl_fatal("'%s' option specified multiple times", argv[i]); |
364 | } | |
4a033593 | 365 | shash_add_nocopy(&command->options, key, value); |
f8ff4bc4 BP |
366 | } |
367 | if (i == argc) { | |
368 | vsctl_fatal("missing command name"); | |
369 | } | |
370 | ||
1998cd4d BP |
371 | p = find_command(argv[i]); |
372 | if (!p) { | |
373 | vsctl_fatal("unknown command '%s'; use --help for help", argv[i]); | |
374 | } | |
f8ff4bc4 | 375 | |
1998cd4d BP |
376 | SHASH_FOR_EACH (node, &command->options) { |
377 | const char *s = strstr(p->options, node->name); | |
378 | int end = s ? s[strlen(node->name)] : EOF; | |
4a033593 | 379 | |
1998cd4d BP |
380 | if (end != '=' && end != ',' && end != ' ' && end != '\0') { |
381 | vsctl_fatal("'%s' command has no '%s' option", | |
382 | argv[i], node->name); | |
383 | } | |
384 | if ((end == '=') != (node->data != NULL)) { | |
385 | if (end == '=') { | |
386 | vsctl_fatal("missing argument to '%s' option on '%s' " | |
387 | "command", node->name, argv[i]); | |
388 | } else { | |
389 | vsctl_fatal("'%s' option on '%s' does not accept an " | |
390 | "argument", node->name, argv[i]); | |
f8ff4bc4 | 391 | } |
1998cd4d BP |
392 | } |
393 | } | |
f8ff4bc4 | 394 | |
1998cd4d BP |
395 | n_arg = argc - i - 1; |
396 | if (n_arg < p->min_args) { | |
397 | vsctl_fatal("'%s' command requires at least %d arguments", | |
398 | p->name, p->min_args); | |
399 | } else if (n_arg > p->max_args) { | |
400 | int j; | |
401 | ||
402 | for (j = i + 1; j < argc; j++) { | |
403 | if (argv[j][0] == '-') { | |
404 | vsctl_fatal("'%s' command takes at most %d arguments " | |
405 | "(note that options must precede command " | |
406 | "names and follow a \"--\" argument)", | |
f8ff4bc4 | 407 | p->name, p->max_args); |
f8ff4bc4 BP |
408 | } |
409 | } | |
1998cd4d BP |
410 | |
411 | vsctl_fatal("'%s' command takes at most %d arguments", | |
412 | p->name, p->max_args); | |
413 | } | |
414 | ||
415 | command->syntax = p; | |
416 | command->argc = n_arg + 1; | |
417 | command->argv = &argv[i]; | |
418 | } | |
419 | ||
420 | /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a | |
421 | * null pointer if there is none. */ | |
422 | static const struct vsctl_command_syntax * | |
423 | find_command(const char *name) | |
424 | { | |
425 | static struct shash commands = SHASH_INITIALIZER(&commands); | |
426 | ||
427 | if (shash_is_empty(&commands)) { | |
428 | const struct vsctl_command_syntax *p; | |
429 | ||
430 | for (p = all_commands; p->name; p++) { | |
431 | shash_add_assert(&commands, p->name, p); | |
432 | } | |
f8ff4bc4 BP |
433 | } |
434 | ||
1998cd4d | 435 | return shash_find_data(&commands, name); |
f8ff4bc4 BP |
436 | } |
437 | ||
438 | static void | |
439 | vsctl_fatal(const char *format, ...) | |
440 | { | |
441 | char *message; | |
442 | va_list args; | |
443 | ||
444 | va_start(args, format); | |
445 | message = xvasprintf(format, args); | |
446 | va_end(args); | |
447 | ||
480ce8ab | 448 | vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER); |
f8ff4bc4 | 449 | VLOG_ERR("%s", message); |
def90f62 | 450 | ovs_error(0, "%s", message); |
1d48b4be BP |
451 | vsctl_exit(EXIT_FAILURE); |
452 | } | |
453 | ||
454 | /* Frees the current transaction and the underlying IDL and then calls | |
455 | * exit(status). | |
456 | * | |
457 | * Freeing the transaction and the IDL is not strictly necessary, but it makes | |
458 | * for a clean memory leak report from valgrind in the normal case. That makes | |
459 | * it easier to notice real memory leaks. */ | |
460 | static void | |
461 | vsctl_exit(int status) | |
462 | { | |
463 | if (the_idl_txn) { | |
464 | ovsdb_idl_txn_abort(the_idl_txn); | |
465 | ovsdb_idl_txn_destroy(the_idl_txn); | |
466 | } | |
467 | ovsdb_idl_destroy(the_idl); | |
468 | exit(status); | |
f8ff4bc4 BP |
469 | } |
470 | ||
c75d1511 BP |
471 | static void |
472 | usage(void) | |
473 | { | |
8f7501e8 BP |
474 | printf("\ |
475 | %s: ovs-vswitchd management utility\n\ | |
476 | usage: %s [OPTIONS] COMMAND [ARG...]\n\ | |
477 | \n\ | |
478 | Bridge commands:\n\ | |
479 | add-br BRIDGE create a new bridge named BRIDGE\n\ | |
480 | add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\ | |
481 | del-br BRIDGE delete BRIDGE and all of its ports\n\ | |
482 | list-br print the names of all the bridges\n\ | |
483 | br-exists BRIDGE test whether BRIDGE exists\n\ | |
484 | br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\ | |
485 | br-to-parent BRIDGE print the parent of BRIDGE\n\ | |
486 | br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\ | |
487 | br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\ | |
488 | br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\ | |
489 | br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\ | |
490 | \n\ | |
491 | Port commands:\n\ | |
492 | list-ports BRIDGE print the names of all the ports on BRIDGE\n\ | |
493 | add-port BRIDGE PORT add network device PORT to BRIDGE\n\ | |
494 | add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\ | |
495 | del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\ | |
496 | port-to-br PORT print name of bridge that contains PORT\n\ | |
8f7501e8 BP |
497 | A bond is considered to be a single port.\n\ |
498 | \n\ | |
499 | Interface commands (a bond consists of multiple interfaces):\n\ | |
500 | list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\ | |
501 | iface-to-br IFACE print name of bridge that contains IFACE\n\ | |
8f7501e8 BP |
502 | \n\ |
503 | Controller commands:\n\ | |
1a048029 JP |
504 | get-controller BRIDGE print the controller for BRIDGE\n\ |
505 | del-controller BRIDGE delete the controller for BRIDGE\n\ | |
506 | set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\ | |
507 | get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\ | |
508 | del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\ | |
509 | set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\ | |
8f7501e8 | 510 | \n\ |
24b8b259 AE |
511 | Manager commands:\n\ |
512 | get-manager print all manager(s)\n\ | |
513 | del-manager delete all manager(s)\n\ | |
514 | set-manager TARGET... set the list of manager(s) to TARGET(s)\n\ | |
515 | \n\ | |
8f7501e8 BP |
516 | SSL commands:\n\ |
517 | get-ssl print the SSL configuration\n\ | |
518 | del-ssl delete the SSL configuration\n\ | |
519 | set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\ | |
520 | \n\ | |
18ee958b JP |
521 | Switch commands:\n\ |
522 | emer-reset reset switch to known good state\n\ | |
523 | \n\ | |
8f7501e8 BP |
524 | Database commands:\n\ |
525 | list TBL [REC] list RECord (or all records) in TBL\n\ | |
0a140468 | 526 | find TBL CONDITION... list records satisfying CONDITION in TBL\n\ |
4f1361e8 | 527 | get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\ |
8f7501e8 BP |
528 | set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\ |
529 | add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\ | |
530 | remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\ | |
531 | clear TBL REC COL clear values from COLumn in RECord in TBL\n\ | |
532 | create TBL COL[:KEY]=VALUE create and initialize new record\n\ | |
4f1361e8 | 533 | destroy TBL REC delete RECord from TBL\n\ |
7db03f7c | 534 | wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\ |
8f7501e8 BP |
535 | Potentially unsafe database commands require --force option.\n\ |
536 | \n\ | |
537 | Options:\n\ | |
538 | --db=DATABASE connect to DATABASE\n\ | |
539 | (default: %s)\n\ | |
540 | --oneline print exactly one line of output per command\n", | |
541 | program_name, program_name, default_db()); | |
c75d1511 | 542 | vlog_usage(); |
8f7501e8 BP |
543 | printf("\n\ |
544 | Other options:\n\ | |
545 | -h, --help display this help message\n\ | |
546 | -V, --version display version information\n"); | |
c75d1511 BP |
547 | exit(EXIT_SUCCESS); |
548 | } | |
549 | ||
550 | static char * | |
551 | default_db(void) | |
552 | { | |
553 | static char *def; | |
554 | if (!def) { | |
b43c6fe2 | 555 | def = xasprintf("unix:%s/db.sock", ovs_rundir()); |
c75d1511 BP |
556 | } |
557 | return def; | |
558 | } | |
0c18b5a0 BP |
559 | |
560 | /* Returns true if it looks like this set of arguments might modify the | |
561 | * database, otherwise false. (Not very smart, so it's prone to false | |
562 | * positives.) */ | |
563 | static bool | |
564 | might_write_to_db(char **argv) | |
565 | { | |
566 | for (; *argv; argv++) { | |
567 | const struct vsctl_command_syntax *p = find_command(*argv); | |
568 | if (p && p->mode == RW) { | |
569 | return true; | |
570 | } | |
571 | } | |
572 | return false; | |
573 | } | |
c75d1511 | 574 | \f |
5d9cb63c | 575 | struct vsctl_context { |
f8ff4bc4 | 576 | /* Read-only. */ |
5d9cb63c BP |
577 | int argc; |
578 | char **argv; | |
f8ff4bc4 BP |
579 | struct shash options; |
580 | ||
581 | /* Modifiable state. */ | |
582 | struct ds output; | |
e051b42c | 583 | struct table *table; |
ad83bfa6 | 584 | struct ovsdb_idl *idl; |
f8ff4bc4 | 585 | struct ovsdb_idl_txn *txn; |
ce5a3e38 | 586 | struct ovsdb_symbol_table *symtab; |
5d9cb63c | 587 | const struct ovsrec_open_vswitch *ovs; |
f74055e7 | 588 | bool verified_ports; |
87b23a01 BP |
589 | |
590 | /* A command may set this member to true if some prerequisite is not met | |
591 | * and the caller should wait for something to change and then retry. */ | |
592 | bool try_again; | |
5d9cb63c BP |
593 | }; |
594 | ||
c75d1511 BP |
595 | struct vsctl_bridge { |
596 | struct ovsrec_bridge *br_cfg; | |
597 | char *name; | |
76ce9432 | 598 | struct ovsrec_controller **ctrl; |
31681a5d | 599 | char *fail_mode; |
76ce9432 | 600 | size_t n_ctrl; |
c75d1511 BP |
601 | struct vsctl_bridge *parent; |
602 | int vlan; | |
603 | }; | |
604 | ||
605 | struct vsctl_port { | |
606 | struct ovsrec_port *port_cfg; | |
607 | struct vsctl_bridge *bridge; | |
608 | }; | |
609 | ||
610 | struct vsctl_iface { | |
611 | struct ovsrec_interface *iface_cfg; | |
612 | struct vsctl_port *port; | |
613 | }; | |
614 | ||
615 | struct vsctl_info { | |
1588bb8d | 616 | struct vsctl_context *ctx; |
e5e12280 BP |
617 | struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */ |
618 | struct shash ports; /* Maps from port name to struct vsctl_port. */ | |
619 | struct shash ifaces; /* Maps from port name to struct vsctl_iface. */ | |
c75d1511 BP |
620 | }; |
621 | ||
bb1c67c8 BP |
622 | static char * |
623 | vsctl_context_to_string(const struct vsctl_context *ctx) | |
624 | { | |
625 | const struct shash_node *node; | |
626 | struct svec words; | |
627 | char *s; | |
628 | int i; | |
629 | ||
630 | svec_init(&words); | |
631 | SHASH_FOR_EACH (node, &ctx->options) { | |
632 | svec_add(&words, node->name); | |
633 | } | |
634 | for (i = 0; i < ctx->argc; i++) { | |
635 | svec_add(&words, ctx->argv[i]); | |
636 | } | |
637 | svec_terminate(&words); | |
638 | ||
639 | s = process_escape_args(words.names); | |
640 | ||
641 | svec_destroy(&words); | |
642 | ||
643 | return s; | |
644 | } | |
645 | ||
f74055e7 BP |
646 | static void |
647 | verify_ports(struct vsctl_context *ctx) | |
648 | { | |
649 | if (!ctx->verified_ports) { | |
650 | const struct ovsrec_bridge *bridge; | |
651 | const struct ovsrec_port *port; | |
652 | ||
653 | ovsrec_open_vswitch_verify_bridges(ctx->ovs); | |
654 | OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) { | |
655 | ovsrec_bridge_verify_ports(bridge); | |
656 | } | |
657 | OVSREC_PORT_FOR_EACH (port, ctx->idl) { | |
658 | ovsrec_port_verify_interfaces(port); | |
659 | } | |
660 | ||
661 | ctx->verified_ports = true; | |
662 | } | |
663 | } | |
664 | ||
c75d1511 BP |
665 | static struct vsctl_bridge * |
666 | add_bridge(struct vsctl_info *b, | |
667 | struct ovsrec_bridge *br_cfg, const char *name, | |
668 | struct vsctl_bridge *parent, int vlan) | |
669 | { | |
670 | struct vsctl_bridge *br = xmalloc(sizeof *br); | |
671 | br->br_cfg = br_cfg; | |
672 | br->name = xstrdup(name); | |
673 | br->parent = parent; | |
674 | br->vlan = vlan; | |
76ce9432 BP |
675 | if (parent) { |
676 | br->ctrl = parent->br_cfg->controller; | |
677 | br->n_ctrl = parent->br_cfg->n_controller; | |
31681a5d | 678 | br->fail_mode = parent->br_cfg->fail_mode; |
76ce9432 BP |
679 | } else { |
680 | br->ctrl = br_cfg->controller; | |
681 | br->n_ctrl = br_cfg->n_controller; | |
31681a5d | 682 | br->fail_mode = br_cfg->fail_mode; |
76ce9432 | 683 | } |
c75d1511 BP |
684 | shash_add(&b->bridges, br->name, br); |
685 | return br; | |
686 | } | |
687 | ||
688 | static bool | |
689 | port_is_fake_bridge(const struct ovsrec_port *port_cfg) | |
690 | { | |
691 | return (port_cfg->fake_bridge | |
692 | && port_cfg->tag | |
693 | && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095); | |
694 | } | |
695 | ||
696 | static struct vsctl_bridge * | |
697 | find_vlan_bridge(struct vsctl_info *info, | |
698 | struct vsctl_bridge *parent, int vlan) | |
699 | { | |
700 | struct shash_node *node; | |
701 | ||
702 | SHASH_FOR_EACH (node, &info->bridges) { | |
703 | struct vsctl_bridge *br = node->data; | |
704 | if (br->parent == parent && br->vlan == vlan) { | |
705 | return br; | |
706 | } | |
707 | } | |
708 | ||
709 | return NULL; | |
710 | } | |
711 | ||
712 | static void | |
713 | free_info(struct vsctl_info *info) | |
714 | { | |
715 | struct shash_node *node; | |
716 | ||
717 | SHASH_FOR_EACH (node, &info->bridges) { | |
718 | struct vsctl_bridge *bridge = node->data; | |
719 | free(bridge->name); | |
720 | free(bridge); | |
721 | } | |
722 | shash_destroy(&info->bridges); | |
723 | ||
506051fc BP |
724 | shash_destroy_free_data(&info->ports); |
725 | shash_destroy_free_data(&info->ifaces); | |
c75d1511 BP |
726 | } |
727 | ||
e5e12280 BP |
728 | static void |
729 | pre_get_info(struct vsctl_context *ctx) | |
730 | { | |
731 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges); | |
732 | ||
733 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name); | |
734 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller); | |
735 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode); | |
736 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports); | |
737 | ||
738 | ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name); | |
739 | ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge); | |
740 | ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag); | |
741 | ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces); | |
742 | ||
743 | ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name); | |
744 | } | |
745 | ||
c75d1511 | 746 | static void |
1588bb8d | 747 | get_info(struct vsctl_context *ctx, struct vsctl_info *info) |
c75d1511 | 748 | { |
1588bb8d | 749 | const struct ovsrec_open_vswitch *ovs = ctx->ovs; |
c75d1511 BP |
750 | struct shash bridges, ports; |
751 | size_t i; | |
752 | ||
1588bb8d | 753 | info->ctx = ctx; |
c75d1511 BP |
754 | shash_init(&info->bridges); |
755 | shash_init(&info->ports); | |
756 | shash_init(&info->ifaces); | |
757 | ||
758 | shash_init(&bridges); | |
759 | shash_init(&ports); | |
760 | for (i = 0; i < ovs->n_bridges; i++) { | |
761 | struct ovsrec_bridge *br_cfg = ovs->bridges[i]; | |
762 | struct vsctl_bridge *br; | |
763 | size_t j; | |
764 | ||
765 | if (!shash_add_once(&bridges, br_cfg->name, NULL)) { | |
766 | VLOG_WARN("%s: database contains duplicate bridge name", | |
767 | br_cfg->name); | |
768 | continue; | |
769 | } | |
770 | br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0); | |
771 | if (!br) { | |
772 | continue; | |
773 | } | |
774 | ||
775 | for (j = 0; j < br_cfg->n_ports; j++) { | |
776 | struct ovsrec_port *port_cfg = br_cfg->ports[j]; | |
777 | ||
778 | if (!shash_add_once(&ports, port_cfg->name, NULL)) { | |
779 | VLOG_WARN("%s: database contains duplicate port name", | |
780 | port_cfg->name); | |
781 | continue; | |
782 | } | |
783 | ||
784 | if (port_is_fake_bridge(port_cfg) | |
dfbe07ba | 785 | && shash_add_once(&bridges, port_cfg->name, NULL)) { |
c75d1511 BP |
786 | add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag); |
787 | } | |
788 | } | |
789 | } | |
790 | shash_destroy(&bridges); | |
791 | shash_destroy(&ports); | |
792 | ||
793 | shash_init(&bridges); | |
794 | shash_init(&ports); | |
795 | for (i = 0; i < ovs->n_bridges; i++) { | |
796 | struct ovsrec_bridge *br_cfg = ovs->bridges[i]; | |
797 | struct vsctl_bridge *br; | |
798 | size_t j; | |
799 | ||
800 | if (!shash_add_once(&bridges, br_cfg->name, NULL)) { | |
801 | continue; | |
802 | } | |
803 | br = shash_find_data(&info->bridges, br_cfg->name); | |
804 | for (j = 0; j < br_cfg->n_ports; j++) { | |
805 | struct ovsrec_port *port_cfg = br_cfg->ports[j]; | |
806 | struct vsctl_port *port; | |
807 | size_t k; | |
808 | ||
809 | if (!shash_add_once(&ports, port_cfg->name, NULL)) { | |
810 | continue; | |
811 | } | |
812 | ||
813 | if (port_is_fake_bridge(port_cfg) | |
dfbe07ba | 814 | && !shash_add_once(&bridges, port_cfg->name, NULL)) { |
c75d1511 BP |
815 | continue; |
816 | } | |
817 | ||
818 | port = xmalloc(sizeof *port); | |
819 | port->port_cfg = port_cfg; | |
820 | if (port_cfg->tag | |
821 | && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) { | |
822 | port->bridge = find_vlan_bridge(info, br, *port_cfg->tag); | |
823 | if (!port->bridge) { | |
824 | port->bridge = br; | |
825 | } | |
826 | } else { | |
827 | port->bridge = br; | |
828 | } | |
829 | shash_add(&info->ports, port_cfg->name, port); | |
830 | ||
831 | for (k = 0; k < port_cfg->n_interfaces; k++) { | |
832 | struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k]; | |
833 | struct vsctl_iface *iface; | |
834 | ||
835 | if (shash_find(&info->ifaces, iface_cfg->name)) { | |
836 | VLOG_WARN("%s: database contains duplicate interface name", | |
837 | iface_cfg->name); | |
838 | continue; | |
839 | } | |
840 | ||
841 | iface = xmalloc(sizeof *iface); | |
842 | iface->iface_cfg = iface_cfg; | |
843 | iface->port = port; | |
dfbe07ba | 844 | shash_add(&info->ifaces, iface_cfg->name, iface); |
c75d1511 BP |
845 | } |
846 | } | |
847 | } | |
848 | shash_destroy(&bridges); | |
849 | shash_destroy(&ports); | |
850 | } | |
851 | ||
852 | static void | |
853 | check_conflicts(struct vsctl_info *info, const char *name, | |
854 | char *msg) | |
855 | { | |
856 | struct vsctl_iface *iface; | |
857 | struct vsctl_port *port; | |
858 | ||
f74055e7 BP |
859 | verify_ports(info->ctx); |
860 | ||
c75d1511 | 861 | if (shash_find(&info->bridges, name)) { |
c88b6a27 BP |
862 | vsctl_fatal("%s because a bridge named %s already exists", |
863 | msg, name); | |
c75d1511 BP |
864 | } |
865 | ||
866 | port = shash_find_data(&info->ports, name); | |
867 | if (port) { | |
c88b6a27 BP |
868 | vsctl_fatal("%s because a port named %s already exists on " |
869 | "bridge %s", msg, name, port->bridge->name); | |
c75d1511 BP |
870 | } |
871 | ||
872 | iface = shash_find_data(&info->ifaces, name); | |
873 | if (iface) { | |
c88b6a27 BP |
874 | vsctl_fatal("%s because an interface named %s already exists " |
875 | "on bridge %s", msg, name, iface->port->bridge->name); | |
c75d1511 BP |
876 | } |
877 | ||
878 | free(msg); | |
879 | } | |
880 | ||
881 | static struct vsctl_bridge * | |
01845ce8 | 882 | find_bridge(struct vsctl_info *info, const char *name, bool must_exist) |
c75d1511 BP |
883 | { |
884 | struct vsctl_bridge *br = shash_find_data(&info->bridges, name); | |
01845ce8 | 885 | if (must_exist && !br) { |
c88b6a27 | 886 | vsctl_fatal("no bridge named %s", name); |
c75d1511 | 887 | } |
f74055e7 | 888 | ovsrec_open_vswitch_verify_bridges(info->ctx->ovs); |
c75d1511 BP |
889 | return br; |
890 | } | |
891 | ||
975ac531 JP |
892 | static struct vsctl_bridge * |
893 | find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist) | |
894 | { | |
895 | struct vsctl_bridge *br = find_bridge(info, name, must_exist); | |
896 | if (br && br->parent) { | |
897 | vsctl_fatal("%s is a fake bridge", name); | |
898 | } | |
899 | return br; | |
900 | } | |
901 | ||
c75d1511 | 902 | static struct vsctl_port * |
01845ce8 | 903 | find_port(struct vsctl_info *info, const char *name, bool must_exist) |
c75d1511 BP |
904 | { |
905 | struct vsctl_port *port = shash_find_data(&info->ports, name); | |
460aad80 | 906 | if (port && !strcmp(name, port->bridge->name)) { |
01845ce8 BP |
907 | port = NULL; |
908 | } | |
909 | if (must_exist && !port) { | |
c88b6a27 | 910 | vsctl_fatal("no port named %s", name); |
c75d1511 | 911 | } |
f74055e7 | 912 | verify_ports(info->ctx); |
c75d1511 BP |
913 | return port; |
914 | } | |
915 | ||
916 | static struct vsctl_iface * | |
01845ce8 | 917 | find_iface(struct vsctl_info *info, const char *name, bool must_exist) |
c75d1511 BP |
918 | { |
919 | struct vsctl_iface *iface = shash_find_data(&info->ifaces, name); | |
460aad80 | 920 | if (iface && !strcmp(name, iface->port->bridge->name)) { |
01845ce8 BP |
921 | iface = NULL; |
922 | } | |
923 | if (must_exist && !iface) { | |
c88b6a27 | 924 | vsctl_fatal("no interface named %s", name); |
c75d1511 | 925 | } |
f74055e7 | 926 | verify_ports(info->ctx); |
c75d1511 BP |
927 | return iface; |
928 | } | |
929 | ||
930 | static void | |
931 | bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port) | |
932 | { | |
933 | struct ovsrec_port **ports; | |
934 | size_t i; | |
935 | ||
936 | ports = xmalloc(sizeof *br->ports * (br->n_ports + 1)); | |
937 | for (i = 0; i < br->n_ports; i++) { | |
938 | ports[i] = br->ports[i]; | |
939 | } | |
c75d1511 BP |
940 | ports[br->n_ports] = port; |
941 | ovsrec_bridge_set_ports(br, ports, br->n_ports + 1); | |
942 | free(ports); | |
943 | } | |
944 | ||
945 | static void | |
946 | bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port) | |
947 | { | |
948 | struct ovsrec_port **ports; | |
949 | size_t i, n; | |
950 | ||
951 | ports = xmalloc(sizeof *br->ports * br->n_ports); | |
952 | for (i = n = 0; i < br->n_ports; i++) { | |
953 | if (br->ports[i] != port) { | |
954 | ports[n++] = br->ports[i]; | |
955 | } | |
956 | } | |
957 | ovsrec_bridge_set_ports(br, ports, n); | |
958 | free(ports); | |
959 | } | |
960 | ||
961 | static void | |
962 | ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs, | |
963 | struct ovsrec_bridge *bridge) | |
964 | { | |
965 | struct ovsrec_bridge **bridges; | |
966 | size_t i; | |
967 | ||
968 | bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1)); | |
969 | for (i = 0; i < ovs->n_bridges; i++) { | |
970 | bridges[i] = ovs->bridges[i]; | |
971 | } | |
972 | bridges[ovs->n_bridges] = bridge; | |
973 | ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1); | |
974 | free(bridges); | |
975 | } | |
976 | ||
977 | static void | |
978 | ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs, | |
979 | struct ovsrec_bridge *bridge) | |
980 | { | |
981 | struct ovsrec_bridge **bridges; | |
982 | size_t i, n; | |
983 | ||
984 | bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges); | |
985 | for (i = n = 0; i < ovs->n_bridges; i++) { | |
986 | if (ovs->bridges[i] != bridge) { | |
987 | bridges[n++] = ovs->bridges[i]; | |
988 | } | |
989 | } | |
990 | ovsrec_open_vswitch_set_bridges(ovs, bridges, n); | |
991 | free(bridges); | |
992 | } | |
993 | ||
524555d1 | 994 | static void |
c69ee87c | 995 | cmd_init(struct vsctl_context *ctx OVS_UNUSED) |
524555d1 BP |
996 | { |
997 | } | |
998 | ||
e5e12280 BP |
999 | static void |
1000 | pre_cmd_emer_reset(struct vsctl_context *ctx) | |
1001 | { | |
1002 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers); | |
87824b0b | 1003 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options); |
e5e12280 BP |
1004 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl); |
1005 | ||
1006 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller); | |
f67e3b66 | 1007 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode); |
e5e12280 BP |
1008 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors); |
1009 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow); | |
1010 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow); | |
1011 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans); | |
1012 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config); | |
1013 | ||
1014 | ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config); | |
1015 | ||
1016 | ovsdb_idl_add_column(ctx->idl, | |
1017 | &ovsrec_interface_col_ingress_policing_rate); | |
1018 | ovsdb_idl_add_column(ctx->idl, | |
1019 | &ovsrec_interface_col_ingress_policing_burst); | |
1020 | } | |
1021 | ||
18ee958b JP |
1022 | static void |
1023 | cmd_emer_reset(struct vsctl_context *ctx) | |
1024 | { | |
1025 | const struct ovsdb_idl *idl = ctx->idl; | |
1026 | const struct ovsrec_bridge *br; | |
1027 | const struct ovsrec_port *port; | |
1028 | const struct ovsrec_interface *iface; | |
1029 | const struct ovsrec_mirror *mirror, *next_mirror; | |
1030 | const struct ovsrec_controller *ctrl, *next_ctrl; | |
87824b0b | 1031 | const struct ovsrec_manager *mgr, *next_mgr; |
18ee958b JP |
1032 | const struct ovsrec_netflow *nf, *next_nf; |
1033 | const struct ovsrec_ssl *ssl, *next_ssl; | |
1034 | const struct ovsrec_sflow *sflow, *next_sflow; | |
1035 | ||
18ee958b JP |
1036 | /* Reset the Open_vSwitch table. */ |
1037 | ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0); | |
87824b0b | 1038 | ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0); |
18ee958b JP |
1039 | ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL); |
1040 | ||
1041 | OVSREC_BRIDGE_FOR_EACH (br, idl) { | |
1042 | int i; | |
1043 | char *hw_key = "hwaddr"; | |
1044 | char *hw_val = NULL; | |
1045 | ||
1046 | ovsrec_bridge_set_controller(br, NULL, 0); | |
f67e3b66 | 1047 | ovsrec_bridge_set_fail_mode(br, NULL); |
18ee958b JP |
1048 | ovsrec_bridge_set_mirrors(br, NULL, 0); |
1049 | ovsrec_bridge_set_netflow(br, NULL); | |
1050 | ovsrec_bridge_set_sflow(br, NULL); | |
1051 | ovsrec_bridge_set_flood_vlans(br, NULL, 0); | |
1052 | ||
1053 | /* We only want to save the "hwaddr" key from other_config. */ | |
1054 | for (i=0; i < br->n_other_config; i++) { | |
1055 | if (!strcmp(br->key_other_config[i], hw_key)) { | |
1056 | hw_val = br->value_other_config[i]; | |
1057 | break; | |
1058 | } | |
1059 | } | |
1060 | if (hw_val) { | |
1061 | char *val = xstrdup(hw_val); | |
1062 | ovsrec_bridge_set_other_config(br, &hw_key, &val, 1); | |
1063 | free(val); | |
1064 | } else { | |
1065 | ovsrec_bridge_set_other_config(br, NULL, NULL, 0); | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | OVSREC_PORT_FOR_EACH (port, idl) { | |
1070 | ovsrec_port_set_other_config(port, NULL, NULL, 0); | |
1071 | } | |
1072 | ||
1073 | OVSREC_INTERFACE_FOR_EACH (iface, idl) { | |
1074 | /* xxx What do we do about gre/patch devices created by mgr? */ | |
1075 | ||
1076 | ovsrec_interface_set_ingress_policing_rate(iface, 0); | |
1077 | ovsrec_interface_set_ingress_policing_burst(iface, 0); | |
1078 | } | |
1079 | ||
1080 | OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) { | |
1081 | ovsrec_mirror_delete(mirror); | |
1082 | } | |
1083 | ||
1084 | OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) { | |
1085 | ovsrec_controller_delete(ctrl); | |
1086 | } | |
1087 | ||
87824b0b AE |
1088 | OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) { |
1089 | ovsrec_manager_delete(mgr); | |
1090 | } | |
1091 | ||
18ee958b JP |
1092 | OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) { |
1093 | ovsrec_netflow_delete(nf); | |
1094 | } | |
1095 | ||
1096 | OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) { | |
1097 | ovsrec_ssl_delete(ssl); | |
1098 | } | |
1099 | ||
1100 | OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) { | |
1101 | ovsrec_sflow_delete(sflow); | |
1102 | } | |
1103 | } | |
1104 | ||
c75d1511 | 1105 | static void |
5d9cb63c | 1106 | cmd_add_br(struct vsctl_context *ctx) |
c75d1511 | 1107 | { |
aeee85aa | 1108 | bool may_exist = shash_find(&ctx->options, "--may-exist") != 0; |
b89d8339 | 1109 | const char *br_name, *parent_name; |
c75d1511 | 1110 | struct vsctl_info info; |
b89d8339 | 1111 | int vlan; |
c75d1511 | 1112 | |
aeee85aa BP |
1113 | br_name = ctx->argv[1]; |
1114 | if (ctx->argc == 2) { | |
1115 | parent_name = NULL; | |
1116 | vlan = 0; | |
1117 | } else if (ctx->argc == 4) { | |
1118 | parent_name = ctx->argv[2]; | |
1119 | vlan = atoi(ctx->argv[3]); | |
1120 | if (vlan < 1 || vlan > 4095) { | |
1121 | vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]); | |
1122 | } | |
1123 | } else { | |
1124 | vsctl_fatal("'%s' command takes exactly 1 or 3 arguments", | |
1125 | ctx->argv[0]); | |
1126 | } | |
1127 | ||
1588bb8d | 1128 | get_info(ctx, &info); |
aeee85aa BP |
1129 | if (may_exist) { |
1130 | struct vsctl_bridge *br; | |
1131 | ||
1132 | br = find_bridge(&info, br_name, false); | |
1133 | if (br) { | |
1134 | if (!parent_name) { | |
1135 | if (br->parent) { | |
1136 | vsctl_fatal("\"--may-exist add-br %s\" but %s is " | |
1137 | "a VLAN bridge for VLAN %d", | |
1138 | br_name, br_name, br->vlan); | |
1139 | } | |
1140 | } else { | |
1141 | if (!br->parent) { | |
1142 | vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s " | |
1143 | "is not a VLAN bridge", | |
1144 | br_name, parent_name, vlan, br_name); | |
1145 | } else if (strcmp(br->parent->name, parent_name)) { | |
1146 | vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s " | |
1147 | "has the wrong parent %s", | |
1148 | br_name, parent_name, vlan, | |
1149 | br_name, br->parent->name); | |
1150 | } else if (br->vlan != vlan) { | |
1151 | vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s " | |
1152 | "is a VLAN bridge for the wrong VLAN %d", | |
1153 | br_name, parent_name, vlan, br_name, br->vlan); | |
1154 | } | |
1155 | } | |
1156 | return; | |
1157 | } | |
1158 | } | |
c75d1511 BP |
1159 | check_conflicts(&info, br_name, |
1160 | xasprintf("cannot create a bridge named %s", br_name)); | |
1161 | ||
aeee85aa | 1162 | if (!parent_name) { |
c75d1511 BP |
1163 | struct ovsrec_port *port; |
1164 | struct ovsrec_interface *iface; | |
aeee85aa | 1165 | struct ovsrec_bridge *br; |
c75d1511 | 1166 | |
f8ff4bc4 | 1167 | iface = ovsrec_interface_insert(ctx->txn); |
c75d1511 | 1168 | ovsrec_interface_set_name(iface, br_name); |
9106d88c | 1169 | ovsrec_interface_set_type(iface, "internal"); |
c75d1511 | 1170 | |
f8ff4bc4 | 1171 | port = ovsrec_port_insert(ctx->txn); |
c75d1511 BP |
1172 | ovsrec_port_set_name(port, br_name); |
1173 | ovsrec_port_set_interfaces(port, &iface, 1); | |
1174 | ||
f8ff4bc4 | 1175 | br = ovsrec_bridge_insert(ctx->txn); |
c75d1511 BP |
1176 | ovsrec_bridge_set_name(br, br_name); |
1177 | ovsrec_bridge_set_ports(br, &port, 1); | |
1178 | ||
5d9cb63c | 1179 | ovs_insert_bridge(ctx->ovs, br); |
aeee85aa | 1180 | } else { |
c75d1511 BP |
1181 | struct vsctl_bridge *parent; |
1182 | struct ovsrec_port *port; | |
1183 | struct ovsrec_interface *iface; | |
aeee85aa | 1184 | struct ovsrec_bridge *br; |
c75d1511 BP |
1185 | int64_t tag = vlan; |
1186 | ||
01845ce8 | 1187 | parent = find_bridge(&info, parent_name, false); |
c75d1511 | 1188 | if (parent && parent->vlan) { |
11aa5627 | 1189 | vsctl_fatal("cannot create bridge with fake bridge as parent"); |
c75d1511 BP |
1190 | } |
1191 | if (!parent) { | |
c88b6a27 | 1192 | vsctl_fatal("parent bridge %s does not exist", parent_name); |
c75d1511 BP |
1193 | } |
1194 | br = parent->br_cfg; | |
1195 | ||
f8ff4bc4 | 1196 | iface = ovsrec_interface_insert(ctx->txn); |
c75d1511 BP |
1197 | ovsrec_interface_set_name(iface, br_name); |
1198 | ovsrec_interface_set_type(iface, "internal"); | |
1199 | ||
f8ff4bc4 | 1200 | port = ovsrec_port_insert(ctx->txn); |
c75d1511 BP |
1201 | ovsrec_port_set_name(port, br_name); |
1202 | ovsrec_port_set_interfaces(port, &iface, 1); | |
1203 | ovsrec_port_set_fake_bridge(port, true); | |
1204 | ovsrec_port_set_tag(port, &tag, 1); | |
dfbe07ba BP |
1205 | |
1206 | bridge_insert_port(br, port); | |
c75d1511 BP |
1207 | } |
1208 | ||
1209 | free_info(&info); | |
1210 | } | |
1211 | ||
1212 | static void | |
1213 | del_port(struct vsctl_info *info, struct vsctl_port *port) | |
1214 | { | |
1215 | struct shash_node *node; | |
1216 | ||
1217 | SHASH_FOR_EACH (node, &info->ifaces) { | |
1218 | struct vsctl_iface *iface = node->data; | |
1219 | if (iface->port == port) { | |
1220 | ovsrec_interface_delete(iface->iface_cfg); | |
1221 | } | |
1222 | } | |
1223 | ovsrec_port_delete(port->port_cfg); | |
1224 | ||
1225 | bridge_delete_port((port->bridge->parent | |
1226 | ? port->bridge->parent->br_cfg | |
1227 | : port->bridge->br_cfg), port->port_cfg); | |
1228 | } | |
1229 | ||
1230 | static void | |
5d9cb63c | 1231 | cmd_del_br(struct vsctl_context *ctx) |
c75d1511 | 1232 | { |
460aad80 | 1233 | bool must_exist = !shash_find(&ctx->options, "--if-exists"); |
c75d1511 | 1234 | struct vsctl_bridge *bridge; |
460aad80 | 1235 | struct vsctl_info info; |
c75d1511 | 1236 | |
1588bb8d | 1237 | get_info(ctx, &info); |
460aad80 BP |
1238 | bridge = find_bridge(&info, ctx->argv[1], must_exist); |
1239 | if (bridge) { | |
1240 | struct shash_node *node; | |
1241 | ||
1242 | SHASH_FOR_EACH (node, &info.ports) { | |
1243 | struct vsctl_port *port = node->data; | |
8cf8cc66 | 1244 | if (port->bridge == bridge || port->bridge->parent == bridge |
460aad80 BP |
1245 | || !strcmp(port->port_cfg->name, bridge->name)) { |
1246 | del_port(&info, port); | |
1247 | } | |
1248 | } | |
1249 | if (bridge->br_cfg) { | |
1250 | ovsrec_bridge_delete(bridge->br_cfg); | |
1251 | ovs_delete_bridge(ctx->ovs, bridge->br_cfg); | |
c75d1511 | 1252 | } |
c75d1511 BP |
1253 | } |
1254 | free_info(&info); | |
1255 | } | |
1256 | ||
dfbe07ba BP |
1257 | static void |
1258 | output_sorted(struct svec *svec, struct ds *output) | |
1259 | { | |
1260 | const char *name; | |
1261 | size_t i; | |
1262 | ||
1263 | svec_sort(svec); | |
1264 | SVEC_FOR_EACH (i, name, svec) { | |
1265 | ds_put_format(output, "%s\n", name); | |
1266 | } | |
1267 | } | |
1268 | ||
c75d1511 | 1269 | static void |
5d9cb63c | 1270 | cmd_list_br(struct vsctl_context *ctx) |
c75d1511 BP |
1271 | { |
1272 | struct shash_node *node; | |
1273 | struct vsctl_info info; | |
dfbe07ba | 1274 | struct svec bridges; |
c75d1511 | 1275 | |
1588bb8d | 1276 | get_info(ctx, &info); |
dfbe07ba BP |
1277 | |
1278 | svec_init(&bridges); | |
c75d1511 BP |
1279 | SHASH_FOR_EACH (node, &info.bridges) { |
1280 | struct vsctl_bridge *br = node->data; | |
dfbe07ba | 1281 | svec_add(&bridges, br->name); |
c75d1511 | 1282 | } |
5d9cb63c | 1283 | output_sorted(&bridges, &ctx->output); |
dfbe07ba BP |
1284 | svec_destroy(&bridges); |
1285 | ||
c75d1511 BP |
1286 | free_info(&info); |
1287 | } | |
1288 | ||
1289 | static void | |
5d9cb63c | 1290 | cmd_br_exists(struct vsctl_context *ctx) |
c75d1511 BP |
1291 | { |
1292 | struct vsctl_info info; | |
1293 | ||
1588bb8d | 1294 | get_info(ctx, &info); |
01845ce8 | 1295 | if (!find_bridge(&info, ctx->argv[1], false)) { |
1d48b4be | 1296 | vsctl_exit(2); |
c75d1511 BP |
1297 | } |
1298 | free_info(&info); | |
1299 | } | |
1300 | ||
457e1eb0 BP |
1301 | /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b' |
1302 | * equals 'a', false otherwise. */ | |
1303 | static bool | |
1304 | key_matches(const char *a, | |
1305 | const char *b_prefix, size_t b_prefix_len, const char *b) | |
1306 | { | |
1307 | return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b); | |
1308 | } | |
1309 | ||
1310 | static void | |
1311 | set_external_id(char **old_keys, char **old_values, size_t old_n, | |
1312 | char *key, char *value, | |
1313 | char ***new_keysp, char ***new_valuesp, size_t *new_np) | |
1314 | { | |
1315 | char **new_keys; | |
1316 | char **new_values; | |
1317 | size_t new_n; | |
1318 | size_t i; | |
1319 | ||
1320 | new_keys = xmalloc(sizeof *new_keys * (old_n + 1)); | |
1321 | new_values = xmalloc(sizeof *new_values * (old_n + 1)); | |
1322 | new_n = 0; | |
1323 | for (i = 0; i < old_n; i++) { | |
1324 | if (strcmp(key, old_keys[i])) { | |
1325 | new_keys[new_n] = old_keys[i]; | |
1326 | new_values[new_n] = old_values[i]; | |
1327 | new_n++; | |
1328 | } | |
1329 | } | |
1330 | if (value) { | |
1331 | new_keys[new_n] = key; | |
1332 | new_values[new_n] = value; | |
1333 | new_n++; | |
1334 | } | |
1335 | *new_keysp = new_keys; | |
1336 | *new_valuesp = new_values; | |
1337 | *new_np = new_n; | |
1338 | } | |
1339 | ||
e5e12280 BP |
1340 | static void |
1341 | pre_cmd_br_set_external_id(struct vsctl_context *ctx) | |
1342 | { | |
1343 | pre_get_info(ctx); | |
1344 | ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids); | |
1345 | ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids); | |
1346 | } | |
1347 | ||
457e1eb0 | 1348 | static void |
5d9cb63c | 1349 | cmd_br_set_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
1350 | { |
1351 | struct vsctl_info info; | |
1352 | struct vsctl_bridge *bridge; | |
1353 | char **keys, **values; | |
1354 | size_t n; | |
1355 | ||
1588bb8d | 1356 | get_info(ctx, &info); |
01845ce8 | 1357 | bridge = find_bridge(&info, ctx->argv[1], true); |
457e1eb0 BP |
1358 | if (bridge->br_cfg) { |
1359 | set_external_id(bridge->br_cfg->key_external_ids, | |
1360 | bridge->br_cfg->value_external_ids, | |
1361 | bridge->br_cfg->n_external_ids, | |
5d9cb63c | 1362 | ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL, |
457e1eb0 | 1363 | &keys, &values, &n); |
f74055e7 | 1364 | ovsrec_bridge_verify_external_ids(bridge->br_cfg); |
457e1eb0 BP |
1365 | ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n); |
1366 | } else { | |
5d9cb63c BP |
1367 | char *key = xasprintf("fake-bridge-%s", ctx->argv[2]); |
1368 | struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]); | |
457e1eb0 BP |
1369 | set_external_id(port->port_cfg->key_external_ids, |
1370 | port->port_cfg->value_external_ids, | |
1371 | port->port_cfg->n_external_ids, | |
5d9cb63c | 1372 | key, ctx->argc >= 4 ? ctx->argv[3] : NULL, |
457e1eb0 | 1373 | &keys, &values, &n); |
f74055e7 | 1374 | ovsrec_port_verify_external_ids(port->port_cfg); |
457e1eb0 BP |
1375 | ovsrec_port_set_external_ids(port->port_cfg, keys, values, n); |
1376 | free(key); | |
1377 | } | |
1378 | free(keys); | |
1379 | free(values); | |
1380 | ||
1381 | free_info(&info); | |
1382 | } | |
1383 | ||
1384 | static void | |
1385 | get_external_id(char **keys, char **values, size_t n, | |
1386 | const char *prefix, const char *key, | |
1387 | struct ds *output) | |
1388 | { | |
1389 | size_t prefix_len = strlen(prefix); | |
1390 | struct svec svec; | |
1391 | size_t i; | |
1392 | ||
1393 | svec_init(&svec); | |
1394 | for (i = 0; i < n; i++) { | |
1395 | if (!key && !strncmp(keys[i], prefix, prefix_len)) { | |
1396 | svec_add_nocopy(&svec, xasprintf("%s=%s", | |
1397 | keys[i] + prefix_len, values[i])); | |
1398 | } else if (key_matches(keys[i], prefix, prefix_len, key)) { | |
1399 | svec_add(&svec, values[i]); | |
1400 | break; | |
1401 | } | |
1402 | } | |
1403 | output_sorted(&svec, output); | |
1404 | svec_destroy(&svec); | |
1405 | } | |
1406 | ||
e5e12280 BP |
1407 | static void |
1408 | pre_cmd_br_get_external_id(struct vsctl_context *ctx) | |
1409 | { | |
1410 | pre_cmd_br_set_external_id(ctx); | |
1411 | } | |
1412 | ||
457e1eb0 | 1413 | static void |
5d9cb63c | 1414 | cmd_br_get_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
1415 | { |
1416 | struct vsctl_info info; | |
1417 | struct vsctl_bridge *bridge; | |
1418 | ||
1588bb8d | 1419 | get_info(ctx, &info); |
01845ce8 | 1420 | bridge = find_bridge(&info, ctx->argv[1], true); |
457e1eb0 | 1421 | if (bridge->br_cfg) { |
f74055e7 | 1422 | ovsrec_bridge_verify_external_ids(bridge->br_cfg); |
457e1eb0 BP |
1423 | get_external_id(bridge->br_cfg->key_external_ids, |
1424 | bridge->br_cfg->value_external_ids, | |
1425 | bridge->br_cfg->n_external_ids, | |
5d9cb63c BP |
1426 | "", ctx->argc >= 3 ? ctx->argv[2] : NULL, |
1427 | &ctx->output); | |
457e1eb0 | 1428 | } else { |
5d9cb63c | 1429 | struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]); |
f74055e7 | 1430 | ovsrec_port_verify_external_ids(port->port_cfg); |
457e1eb0 BP |
1431 | get_external_id(port->port_cfg->key_external_ids, |
1432 | port->port_cfg->value_external_ids, | |
1433 | port->port_cfg->n_external_ids, | |
5d9cb63c | 1434 | "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output); |
457e1eb0 BP |
1435 | } |
1436 | free_info(&info); | |
1437 | } | |
1438 | ||
1439 | ||
c75d1511 | 1440 | static void |
5d9cb63c | 1441 | cmd_list_ports(struct vsctl_context *ctx) |
c75d1511 BP |
1442 | { |
1443 | struct vsctl_bridge *br; | |
1444 | struct shash_node *node; | |
1445 | struct vsctl_info info; | |
dfbe07ba | 1446 | struct svec ports; |
c75d1511 | 1447 | |
1588bb8d | 1448 | get_info(ctx, &info); |
01845ce8 | 1449 | br = find_bridge(&info, ctx->argv[1], true); |
f74055e7 | 1450 | ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg); |
dfbe07ba BP |
1451 | |
1452 | svec_init(&ports); | |
c75d1511 BP |
1453 | SHASH_FOR_EACH (node, &info.ports) { |
1454 | struct vsctl_port *port = node->data; | |
1455 | ||
1456 | if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) { | |
dfbe07ba | 1457 | svec_add(&ports, port->port_cfg->name); |
c75d1511 BP |
1458 | } |
1459 | } | |
5d9cb63c | 1460 | output_sorted(&ports, &ctx->output); |
dfbe07ba BP |
1461 | svec_destroy(&ports); |
1462 | ||
c75d1511 BP |
1463 | free_info(&info); |
1464 | } | |
1465 | ||
1466 | static void | |
f8ff4bc4 | 1467 | add_port(struct vsctl_context *ctx, |
bb1c67c8 BP |
1468 | const char *br_name, const char *port_name, |
1469 | bool may_exist, bool fake_iface, | |
18b239f5 BP |
1470 | char *iface_names[], int n_ifaces, |
1471 | char *settings[], int n_settings) | |
c75d1511 BP |
1472 | { |
1473 | struct vsctl_info info; | |
1474 | struct vsctl_bridge *bridge; | |
1475 | struct ovsrec_interface **ifaces; | |
1476 | struct ovsrec_port *port; | |
1477 | size_t i; | |
1478 | ||
1588bb8d | 1479 | get_info(ctx, &info); |
bb1c67c8 | 1480 | if (may_exist) { |
2a022368 | 1481 | struct vsctl_port *vsctl_port; |
bb1c67c8 | 1482 | |
2a022368 BP |
1483 | vsctl_port = find_port(&info, port_name, false); |
1484 | if (vsctl_port) { | |
bb1c67c8 | 1485 | struct svec want_names, have_names; |
bb1c67c8 BP |
1486 | |
1487 | svec_init(&want_names); | |
1488 | for (i = 0; i < n_ifaces; i++) { | |
1489 | svec_add(&want_names, iface_names[i]); | |
1490 | } | |
1491 | svec_sort(&want_names); | |
1492 | ||
1493 | svec_init(&have_names); | |
2a022368 BP |
1494 | for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) { |
1495 | svec_add(&have_names, | |
1496 | vsctl_port->port_cfg->interfaces[i]->name); | |
bb1c67c8 BP |
1497 | } |
1498 | svec_sort(&have_names); | |
1499 | ||
2a022368 | 1500 | if (strcmp(vsctl_port->bridge->name, br_name)) { |
bb1c67c8 BP |
1501 | char *command = vsctl_context_to_string(ctx); |
1502 | vsctl_fatal("\"%s\" but %s is actually attached to bridge %s", | |
2a022368 | 1503 | command, port_name, vsctl_port->bridge->name); |
bb1c67c8 BP |
1504 | } |
1505 | ||
1506 | if (!svec_equal(&want_names, &have_names)) { | |
1507 | char *have_names_string = svec_join(&have_names, ", ", ""); | |
1508 | char *command = vsctl_context_to_string(ctx); | |
1509 | ||
1510 | vsctl_fatal("\"%s\" but %s actually has interface(s) %s", | |
1511 | command, port_name, have_names_string); | |
1512 | } | |
1513 | ||
1514 | svec_destroy(&want_names); | |
1515 | svec_destroy(&have_names); | |
1516 | ||
1517 | return; | |
1518 | } | |
1519 | } | |
c75d1511 BP |
1520 | check_conflicts(&info, port_name, |
1521 | xasprintf("cannot create a port named %s", port_name)); | |
bb1c67c8 BP |
1522 | for (i = 0; i < n_ifaces; i++) { |
1523 | check_conflicts(&info, iface_names[i], | |
1524 | xasprintf("cannot create an interface named %s", | |
1525 | iface_names[i])); | |
1526 | } | |
01845ce8 | 1527 | bridge = find_bridge(&info, br_name, true); |
c75d1511 BP |
1528 | |
1529 | ifaces = xmalloc(n_ifaces * sizeof *ifaces); | |
1530 | for (i = 0; i < n_ifaces; i++) { | |
f8ff4bc4 | 1531 | ifaces[i] = ovsrec_interface_insert(ctx->txn); |
c75d1511 BP |
1532 | ovsrec_interface_set_name(ifaces[i], iface_names[i]); |
1533 | } | |
1534 | ||
f8ff4bc4 | 1535 | port = ovsrec_port_insert(ctx->txn); |
c75d1511 BP |
1536 | ovsrec_port_set_name(port, port_name); |
1537 | ovsrec_port_set_interfaces(port, ifaces, n_ifaces); | |
b4182c7f | 1538 | ovsrec_port_set_bond_fake_iface(port, fake_iface); |
a0a9f31d JP |
1539 | free(ifaces); |
1540 | ||
c75d1511 BP |
1541 | if (bridge->vlan) { |
1542 | int64_t tag = bridge->vlan; | |
1543 | ovsrec_port_set_tag(port, &tag, 1); | |
1544 | } | |
1545 | ||
18b239f5 | 1546 | for (i = 0; i < n_settings; i++) { |
ce5a3e38 BP |
1547 | set_column(get_table("Port"), &port->header_, settings[i], |
1548 | ctx->symtab); | |
18b239f5 BP |
1549 | } |
1550 | ||
c75d1511 BP |
1551 | bridge_insert_port((bridge->parent ? bridge->parent->br_cfg |
1552 | : bridge->br_cfg), port); | |
1553 | ||
1554 | free_info(&info); | |
1555 | } | |
1556 | ||
1557 | static void | |
5d9cb63c | 1558 | cmd_add_port(struct vsctl_context *ctx) |
c75d1511 | 1559 | { |
bb1c67c8 BP |
1560 | bool may_exist = shash_find(&ctx->options, "--may-exist") != 0; |
1561 | ||
1562 | add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false, | |
18b239f5 | 1563 | &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3); |
c75d1511 BP |
1564 | } |
1565 | ||
1566 | static void | |
5d9cb63c | 1567 | cmd_add_bond(struct vsctl_context *ctx) |
c75d1511 | 1568 | { |
bb1c67c8 | 1569 | bool may_exist = shash_find(&ctx->options, "--may-exist") != 0; |
b4182c7f | 1570 | bool fake_iface = shash_find(&ctx->options, "--fake-iface"); |
18b239f5 BP |
1571 | int n_ifaces; |
1572 | int i; | |
1573 | ||
1574 | n_ifaces = ctx->argc - 3; | |
1575 | for (i = 3; i < ctx->argc; i++) { | |
1576 | if (strchr(ctx->argv[i], '=')) { | |
1577 | n_ifaces = i - 3; | |
1578 | break; | |
1579 | } | |
1580 | } | |
1581 | if (n_ifaces < 2) { | |
1582 | vsctl_fatal("add-bond requires at least 2 interfaces, but only " | |
1583 | "%d were specified", n_ifaces); | |
1584 | } | |
b4182c7f | 1585 | |
bb1c67c8 | 1586 | add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface, |
18b239f5 BP |
1587 | &ctx->argv[3], n_ifaces, |
1588 | &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces); | |
c75d1511 BP |
1589 | } |
1590 | ||
1591 | static void | |
5d9cb63c | 1592 | cmd_del_port(struct vsctl_context *ctx) |
c75d1511 | 1593 | { |
460aad80 | 1594 | bool must_exist = !shash_find(&ctx->options, "--if-exists"); |
7c79588e BP |
1595 | bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL; |
1596 | struct vsctl_port *port; | |
c75d1511 BP |
1597 | struct vsctl_info info; |
1598 | ||
1588bb8d | 1599 | get_info(ctx, &info); |
7c79588e BP |
1600 | if (!with_iface) { |
1601 | port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist); | |
1602 | } else { | |
1603 | const char *target = ctx->argv[ctx->argc - 1]; | |
1604 | struct vsctl_iface *iface; | |
1605 | ||
1606 | port = find_port(&info, target, false); | |
1607 | if (!port) { | |
1608 | iface = find_iface(&info, target, false); | |
1609 | if (iface) { | |
1610 | port = iface->port; | |
1611 | } | |
1612 | } | |
1613 | if (must_exist && !port) { | |
1614 | vsctl_fatal("no port or interface named %s", target); | |
460aad80 | 1615 | } |
7c79588e | 1616 | } |
460aad80 | 1617 | |
7c79588e BP |
1618 | if (port) { |
1619 | if (ctx->argc == 3) { | |
1620 | struct vsctl_bridge *bridge; | |
1621 | ||
1622 | bridge = find_bridge(&info, ctx->argv[1], true); | |
1623 | if (port->bridge != bridge) { | |
1624 | if (port->bridge->parent == bridge) { | |
1625 | vsctl_fatal("bridge %s does not have a port %s (although " | |
1626 | "its parent bridge %s does)", | |
1627 | ctx->argv[1], ctx->argv[2], | |
1628 | bridge->parent->name); | |
1629 | } else { | |
1630 | vsctl_fatal("bridge %s does not have a port %s", | |
1631 | ctx->argv[1], ctx->argv[2]); | |
1632 | } | |
460aad80 | 1633 | } |
c75d1511 | 1634 | } |
7c79588e BP |
1635 | |
1636 | del_port(&info, port); | |
c75d1511 | 1637 | } |
7c79588e | 1638 | |
c75d1511 BP |
1639 | free_info(&info); |
1640 | } | |
1641 | ||
1642 | static void | |
5d9cb63c | 1643 | cmd_port_to_br(struct vsctl_context *ctx) |
c75d1511 BP |
1644 | { |
1645 | struct vsctl_port *port; | |
1646 | struct vsctl_info info; | |
1647 | ||
1588bb8d | 1648 | get_info(ctx, &info); |
01845ce8 | 1649 | port = find_port(&info, ctx->argv[1], true); |
5d9cb63c | 1650 | ds_put_format(&ctx->output, "%s\n", port->bridge->name); |
c75d1511 BP |
1651 | free_info(&info); |
1652 | } | |
1653 | ||
1654 | static void | |
5d9cb63c | 1655 | cmd_br_to_vlan(struct vsctl_context *ctx) |
c75d1511 BP |
1656 | { |
1657 | struct vsctl_bridge *bridge; | |
1658 | struct vsctl_info info; | |
1659 | ||
1588bb8d | 1660 | get_info(ctx, &info); |
01845ce8 | 1661 | bridge = find_bridge(&info, ctx->argv[1], true); |
5d9cb63c | 1662 | ds_put_format(&ctx->output, "%d\n", bridge->vlan); |
c75d1511 BP |
1663 | free_info(&info); |
1664 | } | |
1665 | ||
1666 | static void | |
5d9cb63c | 1667 | cmd_br_to_parent(struct vsctl_context *ctx) |
c75d1511 BP |
1668 | { |
1669 | struct vsctl_bridge *bridge; | |
1670 | struct vsctl_info info; | |
1671 | ||
1588bb8d | 1672 | get_info(ctx, &info); |
01845ce8 | 1673 | bridge = find_bridge(&info, ctx->argv[1], true); |
c75d1511 BP |
1674 | if (bridge->parent) { |
1675 | bridge = bridge->parent; | |
1676 | } | |
5d9cb63c | 1677 | ds_put_format(&ctx->output, "%s\n", bridge->name); |
c75d1511 BP |
1678 | free_info(&info); |
1679 | } | |
1680 | ||
1681 | static void | |
5d9cb63c | 1682 | cmd_list_ifaces(struct vsctl_context *ctx) |
c75d1511 BP |
1683 | { |
1684 | struct vsctl_bridge *br; | |
1685 | struct shash_node *node; | |
1686 | struct vsctl_info info; | |
dfbe07ba | 1687 | struct svec ifaces; |
c75d1511 | 1688 | |
1588bb8d | 1689 | get_info(ctx, &info); |
01845ce8 | 1690 | br = find_bridge(&info, ctx->argv[1], true); |
f74055e7 | 1691 | verify_ports(ctx); |
dfbe07ba BP |
1692 | |
1693 | svec_init(&ifaces); | |
c75d1511 BP |
1694 | SHASH_FOR_EACH (node, &info.ifaces) { |
1695 | struct vsctl_iface *iface = node->data; | |
1696 | ||
dfbe07ba BP |
1697 | if (strcmp(iface->iface_cfg->name, br->name) |
1698 | && br == iface->port->bridge) { | |
1699 | svec_add(&ifaces, iface->iface_cfg->name); | |
c75d1511 BP |
1700 | } |
1701 | } | |
5d9cb63c | 1702 | output_sorted(&ifaces, &ctx->output); |
dfbe07ba BP |
1703 | svec_destroy(&ifaces); |
1704 | ||
c75d1511 BP |
1705 | free_info(&info); |
1706 | } | |
1707 | ||
1708 | static void | |
5d9cb63c | 1709 | cmd_iface_to_br(struct vsctl_context *ctx) |
c75d1511 BP |
1710 | { |
1711 | struct vsctl_iface *iface; | |
1712 | struct vsctl_info info; | |
1713 | ||
1588bb8d | 1714 | get_info(ctx, &info); |
01845ce8 | 1715 | iface = find_iface(&info, ctx->argv[1], true); |
5d9cb63c | 1716 | ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name); |
c75d1511 BP |
1717 | free_info(&info); |
1718 | } | |
457e1eb0 | 1719 | |
f74055e7 BP |
1720 | static void |
1721 | verify_controllers(struct ovsrec_bridge *bridge) | |
1722 | { | |
1723 | if (bridge) { | |
1724 | size_t i; | |
1725 | ||
1726 | ovsrec_bridge_verify_controller(bridge); | |
1727 | for (i = 0; i < bridge->n_controller; i++) { | |
1728 | ovsrec_controller_verify_target(bridge->controller[i]); | |
1729 | } | |
1730 | } | |
1731 | } | |
1732 | ||
4e3e7ff9 BP |
1733 | static void |
1734 | pre_controller(struct vsctl_context *ctx) | |
1735 | { | |
1736 | pre_get_info(ctx); | |
1737 | ||
1738 | ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target); | |
1739 | } | |
1740 | ||
76ce9432 | 1741 | static void |
1a048029 | 1742 | cmd_get_controller(struct vsctl_context *ctx) |
76ce9432 | 1743 | { |
1a048029 JP |
1744 | struct vsctl_info info; |
1745 | struct vsctl_bridge *br; | |
76ce9432 BP |
1746 | struct svec targets; |
1747 | size_t i; | |
1748 | ||
1588bb8d | 1749 | get_info(ctx, &info); |
1a048029 | 1750 | br = find_bridge(&info, ctx->argv[1], true); |
f74055e7 | 1751 | verify_controllers(br->br_cfg); |
1a048029 JP |
1752 | |
1753 | /* Print the targets in sorted order for reproducibility. */ | |
76ce9432 | 1754 | svec_init(&targets); |
1a048029 JP |
1755 | for (i = 0; i < br->n_ctrl; i++) { |
1756 | svec_add(&targets, br->ctrl[i]->target); | |
76ce9432 BP |
1757 | } |
1758 | ||
1759 | svec_sort(&targets); | |
1760 | for (i = 0; i < targets.n; i++) { | |
1761 | ds_put_format(&ctx->output, "%s\n", targets.names[i]); | |
1762 | } | |
1763 | svec_destroy(&targets); | |
5aa00635 JP |
1764 | |
1765 | free_info(&info); | |
1766 | } | |
1767 | ||
76ce9432 BP |
1768 | static void |
1769 | delete_controllers(struct ovsrec_controller **controllers, | |
1770 | size_t n_controllers) | |
1771 | { | |
1772 | size_t i; | |
1773 | ||
1774 | for (i = 0; i < n_controllers; i++) { | |
1775 | ovsrec_controller_delete(controllers[i]); | |
1776 | } | |
1777 | } | |
1778 | ||
5aa00635 JP |
1779 | static void |
1780 | cmd_del_controller(struct vsctl_context *ctx) | |
1781 | { | |
1782 | struct vsctl_info info; | |
1a048029 | 1783 | struct vsctl_bridge *br; |
5aa00635 | 1784 | |
1588bb8d | 1785 | get_info(ctx, &info); |
1a048029 | 1786 | br = find_real_bridge(&info, ctx->argv[1], true); |
f74055e7 | 1787 | verify_controllers(br->br_cfg); |
5aa00635 | 1788 | |
1a048029 JP |
1789 | if (br->ctrl) { |
1790 | delete_controllers(br->ctrl, br->n_ctrl); | |
1791 | ovsrec_bridge_set_controller(br->br_cfg, NULL, 0); | |
5aa00635 JP |
1792 | } |
1793 | ||
1794 | free_info(&info); | |
1795 | } | |
1796 | ||
76ce9432 BP |
1797 | static struct ovsrec_controller ** |
1798 | insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n) | |
1799 | { | |
1800 | struct ovsrec_controller **controllers; | |
1801 | size_t i; | |
1802 | ||
1803 | controllers = xmalloc(n * sizeof *controllers); | |
1804 | for (i = 0; i < n; i++) { | |
1805 | controllers[i] = ovsrec_controller_insert(txn); | |
1806 | ovsrec_controller_set_target(controllers[i], targets[i]); | |
1807 | } | |
1808 | ||
1809 | return controllers; | |
1810 | } | |
1811 | ||
5aa00635 JP |
1812 | static void |
1813 | cmd_set_controller(struct vsctl_context *ctx) | |
1814 | { | |
1815 | struct vsctl_info info; | |
1a048029 JP |
1816 | struct vsctl_bridge *br; |
1817 | struct ovsrec_controller **controllers; | |
1818 | size_t n; | |
5aa00635 | 1819 | |
1588bb8d | 1820 | get_info(ctx, &info); |
1a048029 | 1821 | br = find_real_bridge(&info, ctx->argv[1], true); |
f74055e7 | 1822 | verify_controllers(br->br_cfg); |
5aa00635 | 1823 | |
1a048029 | 1824 | delete_controllers(br->ctrl, br->n_ctrl); |
76ce9432 | 1825 | |
1a048029 JP |
1826 | n = ctx->argc - 2; |
1827 | controllers = insert_controllers(ctx->txn, &ctx->argv[2], n); | |
1828 | ovsrec_bridge_set_controller(br->br_cfg, controllers, n); | |
1829 | free(controllers); | |
5aa00635 JP |
1830 | |
1831 | free_info(&info); | |
1832 | } | |
1833 | ||
1834 | static void | |
1835 | cmd_get_fail_mode(struct vsctl_context *ctx) | |
1836 | { | |
1837 | struct vsctl_info info; | |
1a048029 | 1838 | struct vsctl_bridge *br; |
5aa00635 | 1839 | |
1588bb8d | 1840 | get_info(ctx, &info); |
1a048029 | 1841 | br = find_bridge(&info, ctx->argv[1], true); |
5aa00635 | 1842 | |
f74055e7 BP |
1843 | if (br->br_cfg) { |
1844 | ovsrec_bridge_verify_fail_mode(br->br_cfg); | |
1845 | } | |
31681a5d JP |
1846 | if (br->fail_mode && strlen(br->fail_mode)) { |
1847 | ds_put_format(&ctx->output, "%s\n", br->fail_mode); | |
5aa00635 JP |
1848 | } |
1849 | ||
1850 | free_info(&info); | |
1851 | } | |
1852 | ||
1853 | static void | |
1854 | cmd_del_fail_mode(struct vsctl_context *ctx) | |
1855 | { | |
1856 | struct vsctl_info info; | |
1a048029 | 1857 | struct vsctl_bridge *br; |
5aa00635 | 1858 | |
1588bb8d | 1859 | get_info(ctx, &info); |
1a048029 | 1860 | br = find_real_bridge(&info, ctx->argv[1], true); |
5aa00635 | 1861 | |
31681a5d | 1862 | ovsrec_bridge_set_fail_mode(br->br_cfg, NULL); |
5aa00635 JP |
1863 | |
1864 | free_info(&info); | |
1865 | } | |
1866 | ||
1867 | static void | |
1868 | cmd_set_fail_mode(struct vsctl_context *ctx) | |
1869 | { | |
1870 | struct vsctl_info info; | |
1a048029 JP |
1871 | struct vsctl_bridge *br; |
1872 | const char *fail_mode = ctx->argv[2]; | |
5aa00635 | 1873 | |
1588bb8d | 1874 | get_info(ctx, &info); |
1a048029 | 1875 | br = find_real_bridge(&info, ctx->argv[1], true); |
5aa00635 JP |
1876 | |
1877 | if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) { | |
1878 | vsctl_fatal("fail-mode must be \"standalone\" or \"secure\""); | |
1879 | } | |
1880 | ||
31681a5d | 1881 | ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode); |
5aa00635 JP |
1882 | |
1883 | free_info(&info); | |
1884 | } | |
dd8ac6fe | 1885 | |
24b8b259 AE |
1886 | static void |
1887 | verify_managers(const struct ovsrec_open_vswitch *ovs) | |
1888 | { | |
1889 | size_t i; | |
1890 | ||
1891 | ovsrec_open_vswitch_verify_managers(ovs); | |
1892 | ovsrec_open_vswitch_verify_manager_options(ovs); | |
1893 | ||
1894 | for (i = 0; i < ovs->n_manager_options; ++i) { | |
1895 | const struct ovsrec_manager *mgr = ovs->manager_options[i]; | |
1896 | ||
1897 | ovsrec_manager_verify_target(mgr); | |
1898 | } | |
1899 | } | |
1900 | ||
1901 | static void | |
1902 | pre_manager(struct vsctl_context *ctx) | |
1903 | { | |
1904 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers); | |
1905 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options); | |
1906 | ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target); | |
1907 | } | |
1908 | ||
1909 | static void | |
1910 | cmd_get_manager(struct vsctl_context *ctx) | |
1911 | { | |
1912 | const struct ovsrec_open_vswitch *ovs = ctx->ovs; | |
1913 | struct svec targets; | |
1914 | size_t i; | |
1915 | ||
1916 | verify_managers(ovs); | |
1917 | ||
1918 | /* Print the targets in sorted order for reproducibility. */ | |
1919 | svec_init(&targets); | |
1920 | ||
1921 | /* First, add all targets found in deprecated 'managers' column. */ | |
1922 | for (i = 0; i < ovs->n_managers; i++) { | |
1923 | svec_add(&targets, ovs->managers[i]); | |
1924 | } | |
1925 | ||
1926 | /* Second, add all targets pointed to by 'manager_options' column. */ | |
1927 | for (i = 0; i < ovs->n_manager_options; i++) { | |
1928 | svec_add(&targets, ovs->manager_options[i]->target); | |
1929 | } | |
1930 | ||
1931 | svec_sort_unique(&targets); | |
1932 | for (i = 0; i < targets.n; i++) { | |
1933 | ds_put_format(&ctx->output, "%s\n", targets.names[i]); | |
1934 | } | |
1935 | svec_destroy(&targets); | |
1936 | } | |
1937 | ||
1938 | static void | |
1939 | delete_managers(const struct vsctl_context *ctx) | |
1940 | { | |
1941 | const struct ovsrec_open_vswitch *ovs = ctx->ovs; | |
1942 | size_t i; | |
1943 | ||
1944 | /* Delete manager targets in deprecated 'managers' column. */ | |
1945 | ovsrec_open_vswitch_set_managers(ovs, NULL, 0); | |
1946 | ||
1947 | /* Delete Manager rows pointed to by 'manager_options' column. */ | |
1948 | for (i = 0; i < ovs->n_manager_options; i++) { | |
1949 | ovsrec_manager_delete(ovs->manager_options[i]); | |
1950 | } | |
1951 | ||
1952 | /* Delete 'Manager' row refs in 'manager_options' column. */ | |
1953 | ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0); | |
1954 | } | |
1955 | ||
1956 | static void | |
1957 | cmd_del_manager(struct vsctl_context *ctx) | |
1958 | { | |
1959 | const struct ovsrec_open_vswitch *ovs = ctx->ovs; | |
1960 | ||
1961 | verify_managers(ovs); | |
1962 | delete_managers(ctx); | |
1963 | } | |
1964 | ||
1965 | static void | |
1966 | insert_managers(struct vsctl_context *ctx, char *targets[], size_t n) | |
1967 | { | |
1968 | struct ovsrec_manager **managers; | |
1969 | size_t i; | |
1970 | ||
1971 | /* Store in deprecated 'manager' column. */ | |
1972 | ovsrec_open_vswitch_set_managers(ctx->ovs, targets, n); | |
1973 | ||
1974 | /* Insert each manager in a new row in Manager table. */ | |
1975 | managers = xmalloc(n * sizeof *managers); | |
1976 | for (i = 0; i < n; i++) { | |
1977 | managers[i] = ovsrec_manager_insert(ctx->txn); | |
1978 | ovsrec_manager_set_target(managers[i], targets[i]); | |
1979 | } | |
1980 | ||
1981 | /* Store uuids of new Manager rows in 'manager_options' column. */ | |
1982 | ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n); | |
1983 | free(managers); | |
1984 | } | |
1985 | ||
1986 | static void | |
1987 | cmd_set_manager(struct vsctl_context *ctx) | |
1988 | { | |
1989 | const size_t n = ctx->argc - 1; | |
1990 | ||
1991 | verify_managers(ctx->ovs); | |
1992 | delete_managers(ctx); | |
1993 | insert_managers(ctx, &ctx->argv[1], n); | |
1994 | } | |
1995 | ||
e5e12280 BP |
1996 | static void |
1997 | pre_cmd_get_ssl(struct vsctl_context *ctx) | |
1998 | { | |
1999 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl); | |
2000 | ||
2001 | ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key); | |
2002 | ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate); | |
2003 | ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert); | |
2004 | ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert); | |
2005 | } | |
2006 | ||
dd8ac6fe JP |
2007 | static void |
2008 | cmd_get_ssl(struct vsctl_context *ctx) | |
2009 | { | |
2010 | struct ovsrec_ssl *ssl = ctx->ovs->ssl; | |
2011 | ||
f74055e7 | 2012 | ovsrec_open_vswitch_verify_ssl(ctx->ovs); |
dd8ac6fe | 2013 | if (ssl) { |
f74055e7 BP |
2014 | ovsrec_ssl_verify_private_key(ssl); |
2015 | ovsrec_ssl_verify_certificate(ssl); | |
2016 | ovsrec_ssl_verify_ca_cert(ssl); | |
2017 | ovsrec_ssl_verify_bootstrap_ca_cert(ssl); | |
2018 | ||
dd8ac6fe JP |
2019 | ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key); |
2020 | ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate); | |
2021 | ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert); | |
2022 | ds_put_format(&ctx->output, "Bootstrap: %s\n", | |
2023 | ssl->bootstrap_ca_cert ? "true" : "false"); | |
2024 | } | |
2025 | } | |
2026 | ||
e5e12280 BP |
2027 | static void |
2028 | pre_cmd_del_ssl(struct vsctl_context *ctx) | |
2029 | { | |
2030 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl); | |
2031 | } | |
2032 | ||
dd8ac6fe JP |
2033 | static void |
2034 | cmd_del_ssl(struct vsctl_context *ctx) | |
2035 | { | |
2036 | struct ovsrec_ssl *ssl = ctx->ovs->ssl; | |
2037 | ||
2038 | if (ssl) { | |
f74055e7 | 2039 | ovsrec_open_vswitch_verify_ssl(ctx->ovs); |
dd8ac6fe JP |
2040 | ovsrec_ssl_delete(ssl); |
2041 | ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL); | |
2042 | } | |
2043 | } | |
2044 | ||
e5e12280 BP |
2045 | static void |
2046 | pre_cmd_set_ssl(struct vsctl_context *ctx) | |
2047 | { | |
2048 | ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl); | |
2049 | } | |
2050 | ||
dd8ac6fe JP |
2051 | static void |
2052 | cmd_set_ssl(struct vsctl_context *ctx) | |
2053 | { | |
2054 | bool bootstrap = shash_find(&ctx->options, "--bootstrap"); | |
2055 | struct ovsrec_ssl *ssl = ctx->ovs->ssl; | |
2056 | ||
f74055e7 | 2057 | ovsrec_open_vswitch_verify_ssl(ctx->ovs); |
dd8ac6fe JP |
2058 | if (ssl) { |
2059 | ovsrec_ssl_delete(ssl); | |
2060 | } | |
f8ff4bc4 | 2061 | ssl = ovsrec_ssl_insert(ctx->txn); |
dd8ac6fe JP |
2062 | |
2063 | ovsrec_ssl_set_private_key(ssl, ctx->argv[1]); | |
2064 | ovsrec_ssl_set_certificate(ssl, ctx->argv[2]); | |
2065 | ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]); | |
2066 | ||
2067 | ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap); | |
2068 | ||
2069 | ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl); | |
2070 | } | |
c75d1511 | 2071 | \f |
ad83bfa6 BP |
2072 | /* Parameter commands. */ |
2073 | ||
ad83bfa6 BP |
2074 | struct vsctl_row_id { |
2075 | const struct ovsdb_idl_table_class *table; | |
2076 | const struct ovsdb_idl_column *name_column; | |
2077 | const struct ovsdb_idl_column *uuid_column; | |
2078 | }; | |
2079 | ||
2080 | struct vsctl_table_class { | |
2081 | struct ovsdb_idl_table_class *class; | |
ad83bfa6 BP |
2082 | struct vsctl_row_id row_ids[2]; |
2083 | }; | |
2084 | ||
2085 | static const struct vsctl_table_class tables[] = { | |
bd76d25d | 2086 | {&ovsrec_table_bridge, |
ad83bfa6 BP |
2087 | {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL}, |
2088 | {NULL, NULL, NULL}}}, | |
2089 | ||
bd76d25d | 2090 | {&ovsrec_table_controller, |
ad83bfa6 BP |
2091 | {{&ovsrec_table_bridge, |
2092 | &ovsrec_bridge_col_name, | |
1a048029 | 2093 | &ovsrec_bridge_col_controller}}}, |
ad83bfa6 | 2094 | |
bd76d25d | 2095 | {&ovsrec_table_interface, |
ad83bfa6 BP |
2096 | {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL}, |
2097 | {NULL, NULL, NULL}}}, | |
2098 | ||
bd76d25d | 2099 | {&ovsrec_table_mirror, |
ad83bfa6 BP |
2100 | {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL}, |
2101 | {NULL, NULL, NULL}}}, | |
2102 | ||
94db5407 BP |
2103 | {&ovsrec_table_manager, |
2104 | {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL}, | |
2105 | {NULL, NULL, NULL}}}, | |
2106 | ||
bd76d25d | 2107 | {&ovsrec_table_netflow, |
ad83bfa6 BP |
2108 | {{&ovsrec_table_bridge, |
2109 | &ovsrec_bridge_col_name, | |
2110 | &ovsrec_bridge_col_netflow}, | |
2111 | {NULL, NULL, NULL}}}, | |
2112 | ||
bd76d25d | 2113 | {&ovsrec_table_open_vswitch, |
ad83bfa6 BP |
2114 | {{&ovsrec_table_open_vswitch, NULL, NULL}, |
2115 | {NULL, NULL, NULL}}}, | |
2116 | ||
bd76d25d | 2117 | {&ovsrec_table_port, |
ad83bfa6 BP |
2118 | {{&ovsrec_table_port, &ovsrec_port_col_name, NULL}, |
2119 | {NULL, NULL, NULL}}}, | |
2120 | ||
c1c9c9c4 BP |
2121 | {&ovsrec_table_qos, |
2122 | {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos}, | |
2123 | {NULL, NULL, NULL}}}, | |
2124 | ||
b31bcf60 EJ |
2125 | {&ovsrec_table_monitor, |
2126 | {{&ovsrec_table_interface, | |
2127 | &ovsrec_interface_col_name, | |
2128 | &ovsrec_interface_col_monitor}, | |
2129 | {NULL, NULL, NULL}}}, | |
2130 | ||
2131 | {&ovsrec_table_maintenance_point, | |
2132 | {{NULL, NULL, NULL}, | |
2133 | {NULL, NULL, NULL}}}, | |
2134 | ||
c1c9c9c4 BP |
2135 | {&ovsrec_table_queue, |
2136 | {{NULL, NULL, NULL}, | |
2137 | {NULL, NULL, NULL}}}, | |
2138 | ||
bd76d25d | 2139 | {&ovsrec_table_ssl, |
ad83bfa6 BP |
2140 | {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}}, |
2141 | ||
d01600a2 BP |
2142 | {&ovsrec_table_sflow, |
2143 | {{&ovsrec_table_bridge, | |
2144 | &ovsrec_bridge_col_name, | |
2145 | &ovsrec_bridge_col_sflow}, | |
2146 | {NULL, NULL, NULL}}}, | |
2147 | ||
bd76d25d | 2148 | {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}} |
ad83bfa6 BP |
2149 | }; |
2150 | ||
1bc6ff29 BP |
2151 | static void |
2152 | die_if_error(char *error) | |
2153 | { | |
2154 | if (error) { | |
def90f62 | 2155 | vsctl_fatal("%s", error); |
1bc6ff29 BP |
2156 | } |
2157 | } | |
2158 | ||
ad83bfa6 BP |
2159 | static int |
2160 | to_lower_and_underscores(unsigned c) | |
2161 | { | |
2162 | return c == '-' ? '_' : tolower(c); | |
2163 | } | |
2164 | ||
2165 | static unsigned int | |
2166 | score_partial_match(const char *name, const char *s) | |
2167 | { | |
2168 | int score; | |
2169 | ||
5128bd9c BP |
2170 | if (!strcmp(name, s)) { |
2171 | return UINT_MAX; | |
2172 | } | |
ad83bfa6 BP |
2173 | for (score = 0; ; score++, name++, s++) { |
2174 | if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) { | |
2175 | break; | |
2176 | } else if (*name == '\0') { | |
5128bd9c | 2177 | return UINT_MAX - 1; |
ad83bfa6 BP |
2178 | } |
2179 | } | |
2180 | return *s == '\0' ? score : 0; | |
2181 | } | |
2182 | ||
2183 | static const struct vsctl_table_class * | |
2184 | get_table(const char *table_name) | |
2185 | { | |
2186 | const struct vsctl_table_class *table; | |
2187 | const struct vsctl_table_class *best_match = NULL; | |
2188 | unsigned int best_score = 0; | |
2189 | ||
2190 | for (table = tables; table->class; table++) { | |
2191 | unsigned int score = score_partial_match(table->class->name, | |
2192 | table_name); | |
2193 | if (score > best_score) { | |
2194 | best_match = table; | |
2195 | best_score = score; | |
2196 | } else if (score == best_score) { | |
2197 | best_match = NULL; | |
2198 | } | |
2199 | } | |
2200 | if (best_match) { | |
2201 | return best_match; | |
2202 | } else if (best_score) { | |
def90f62 | 2203 | vsctl_fatal("multiple table names match \"%s\"", table_name); |
ad83bfa6 | 2204 | } else { |
def90f62 | 2205 | vsctl_fatal("unknown table \"%s\"", table_name); |
ad83bfa6 BP |
2206 | } |
2207 | } | |
2208 | ||
e5e12280 BP |
2209 | static const struct vsctl_table_class * |
2210 | pre_get_table(struct vsctl_context *ctx, const char *table_name) | |
2211 | { | |
2212 | const struct vsctl_table_class *table_class; | |
2213 | int i; | |
2214 | ||
2215 | table_class = get_table(table_name); | |
2216 | ovsdb_idl_add_table(ctx->idl, table_class->class); | |
2217 | ||
2218 | for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) { | |
2219 | const struct vsctl_row_id *id = &table_class->row_ids[i]; | |
2220 | if (id->table) { | |
2221 | ovsdb_idl_add_table(ctx->idl, id->table); | |
2222 | } | |
2223 | if (id->name_column) { | |
2224 | ovsdb_idl_add_column(ctx->idl, id->name_column); | |
2225 | } | |
2226 | if (id->uuid_column) { | |
2227 | ovsdb_idl_add_column(ctx->idl, id->uuid_column); | |
2228 | } | |
2229 | } | |
2230 | ||
2231 | return table_class; | |
2232 | } | |
2233 | ||
ad83bfa6 BP |
2234 | static const struct ovsdb_idl_row * |
2235 | get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table, | |
e111e681 | 2236 | const struct vsctl_row_id *id, const char *record_id) |
ad83bfa6 BP |
2237 | { |
2238 | const struct ovsdb_idl_row *referrer, *final; | |
2239 | ||
2240 | if (!id->table) { | |
2241 | return NULL; | |
2242 | } | |
2243 | ||
2244 | if (!id->name_column) { | |
2245 | if (strcmp(record_id, ".")) { | |
2246 | return NULL; | |
2247 | } | |
2248 | referrer = ovsdb_idl_first_row(ctx->idl, id->table); | |
2249 | if (!referrer || ovsdb_idl_next_row(referrer)) { | |
2250 | return NULL; | |
2251 | } | |
2252 | } else { | |
2253 | const struct ovsdb_idl_row *row; | |
ad83bfa6 | 2254 | |
ad83bfa6 BP |
2255 | referrer = NULL; |
2256 | for (row = ovsdb_idl_first_row(ctx->idl, id->table); | |
e111e681 | 2257 | row != NULL; |
ad83bfa6 BP |
2258 | row = ovsdb_idl_next_row(row)) |
2259 | { | |
8c3c2f30 | 2260 | const struct ovsdb_datum *name; |
ad83bfa6 | 2261 | |
8c3c2f30 BP |
2262 | name = ovsdb_idl_get(row, id->name_column, |
2263 | OVSDB_TYPE_STRING, OVSDB_TYPE_VOID); | |
e111e681 BP |
2264 | if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) { |
2265 | if (referrer) { | |
2266 | vsctl_fatal("multiple rows in %s match \"%s\"", | |
2267 | table->class->name, record_id); | |
ad83bfa6 | 2268 | } |
e111e681 | 2269 | referrer = row; |
ad83bfa6 | 2270 | } |
ad83bfa6 BP |
2271 | } |
2272 | } | |
2273 | if (!referrer) { | |
2274 | return NULL; | |
2275 | } | |
2276 | ||
93255bc5 | 2277 | final = NULL; |
ad83bfa6 | 2278 | if (id->uuid_column) { |
8c3c2f30 | 2279 | const struct ovsdb_datum *uuid; |
ad83bfa6 | 2280 | |
f74055e7 | 2281 | ovsdb_idl_txn_verify(referrer, id->uuid_column); |
8c3c2f30 BP |
2282 | uuid = ovsdb_idl_get(referrer, id->uuid_column, |
2283 | OVSDB_TYPE_UUID, OVSDB_TYPE_VOID); | |
2284 | if (uuid->n == 1) { | |
ad83bfa6 | 2285 | final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, |
8c3c2f30 | 2286 | &uuid->keys[0].uuid); |
ad83bfa6 | 2287 | } |
ad83bfa6 BP |
2288 | } else { |
2289 | final = referrer; | |
2290 | } | |
2291 | ||
2292 | return final; | |
2293 | } | |
2294 | ||
2295 | static const struct ovsdb_idl_row * | |
e111e681 BP |
2296 | get_row (struct vsctl_context *ctx, |
2297 | const struct vsctl_table_class *table, const char *record_id) | |
ad83bfa6 BP |
2298 | { |
2299 | const struct ovsdb_idl_row *row; | |
2300 | struct uuid uuid; | |
2301 | ||
2302 | if (uuid_from_string(&uuid, record_id)) { | |
2303 | row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid); | |
2304 | } else { | |
2305 | int i; | |
2306 | ||
2307 | for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) { | |
e111e681 | 2308 | row = get_row_by_id(ctx, table, &table->row_ids[i], record_id); |
ad83bfa6 BP |
2309 | if (row) { |
2310 | break; | |
2311 | } | |
2312 | } | |
2313 | } | |
b7f74b6f BP |
2314 | return row; |
2315 | } | |
2316 | ||
2317 | static const struct ovsdb_idl_row * | |
2318 | must_get_row(struct vsctl_context *ctx, | |
2319 | const struct vsctl_table_class *table, const char *record_id) | |
2320 | { | |
2321 | const struct ovsdb_idl_row *row = get_row(ctx, table, record_id); | |
ad83bfa6 | 2322 | if (!row) { |
def90f62 BP |
2323 | vsctl_fatal("no row \"%s\" in table %s", |
2324 | record_id, table->class->name); | |
ad83bfa6 BP |
2325 | } |
2326 | return row; | |
2327 | } | |
2328 | ||
1bc6ff29 BP |
2329 | static char * |
2330 | get_column(const struct vsctl_table_class *table, const char *column_name, | |
bd76d25d | 2331 | const struct ovsdb_idl_column **columnp) |
ad83bfa6 | 2332 | { |
bd76d25d | 2333 | const struct ovsdb_idl_column *best_match = NULL; |
ad83bfa6 | 2334 | unsigned int best_score = 0; |
bd76d25d | 2335 | size_t i; |
ad83bfa6 | 2336 | |
bd76d25d BP |
2337 | for (i = 0; i < table->class->n_columns; i++) { |
2338 | const struct ovsdb_idl_column *column = &table->class->columns[i]; | |
2339 | unsigned int score = score_partial_match(column->name, column_name); | |
2340 | if (score > best_score) { | |
2341 | best_match = column; | |
2342 | best_score = score; | |
2343 | } else if (score == best_score) { | |
2344 | best_match = NULL; | |
ad83bfa6 BP |
2345 | } |
2346 | } | |
1bc6ff29 BP |
2347 | |
2348 | *columnp = best_match; | |
ad83bfa6 | 2349 | if (best_match) { |
1bc6ff29 | 2350 | return NULL; |
ad83bfa6 | 2351 | } else if (best_score) { |
1bc6ff29 BP |
2352 | return xasprintf("%s contains more than one column whose name " |
2353 | "matches \"%s\"", table->class->name, column_name); | |
ad83bfa6 | 2354 | } else { |
1bc6ff29 BP |
2355 | return xasprintf("%s does not contain a column whose name matches " |
2356 | "\"%s\"", table->class->name, column_name); | |
ad83bfa6 BP |
2357 | } |
2358 | } | |
2359 | ||
aed133bf BP |
2360 | static struct uuid * |
2361 | create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp) | |
2362 | { | |
2363 | struct ovsdb_symbol *symbol; | |
2364 | ||
2365 | if (id[0] != '@') { | |
2366 | vsctl_fatal("row id \"%s\" does not begin with \"@\"", id); | |
2367 | } | |
2368 | ||
2369 | if (newp) { | |
2370 | *newp = ovsdb_symbol_table_get(symtab, id) == NULL; | |
2371 | } | |
2372 | ||
2373 | symbol = ovsdb_symbol_table_insert(symtab, id); | |
2374 | if (symbol->used) { | |
2375 | vsctl_fatal("row id \"%s\" may only be specified on one --id option", | |
2376 | id); | |
2377 | } | |
2378 | symbol->used = true; | |
2379 | return &symbol->uuid; | |
2380 | } | |
2381 | ||
e5e12280 BP |
2382 | static void |
2383 | pre_get_column(struct vsctl_context *ctx, | |
2384 | const struct vsctl_table_class *table, const char *column_name, | |
2385 | const struct ovsdb_idl_column **columnp) | |
2386 | { | |
2387 | die_if_error(get_column(table, column_name, columnp)); | |
2388 | ovsdb_idl_add_column(ctx->idl, *columnp); | |
2389 | } | |
2390 | ||
e89e5374 BP |
2391 | static char * |
2392 | missing_operator_error(const char *arg, const char **allowed_operators, | |
2393 | size_t n_allowed) | |
2394 | { | |
2395 | struct ds s; | |
2396 | ||
2397 | ds_init(&s); | |
2398 | ds_put_format(&s, "%s: argument does not end in ", arg); | |
2399 | ds_put_format(&s, "\"%s\"", allowed_operators[0]); | |
2400 | if (n_allowed == 2) { | |
2401 | ds_put_format(&s, " or \"%s\"", allowed_operators[1]); | |
2402 | } else if (n_allowed > 2) { | |
2403 | size_t i; | |
2404 | ||
2405 | for (i = 1; i < n_allowed - 1; i++) { | |
2406 | ds_put_format(&s, ", \"%s\"", allowed_operators[i]); | |
2407 | } | |
2408 | ds_put_format(&s, ", or \"%s\"", allowed_operators[i]); | |
2409 | } | |
2410 | ds_put_format(&s, " followed by a value."); | |
2411 | ||
2412 | return ds_steal_cstr(&s); | |
2413 | } | |
2414 | ||
2415 | /* Breaks 'arg' apart into a number of fields in the following order: | |
2416 | * | |
2417 | * - If 'columnp' is nonnull, the name of a column in 'table'. The column | |
2418 | * is stored into '*columnp'. The column name may be abbreviated. | |
2419 | * | |
2420 | * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp' | |
2421 | * and 'keyp' are nonnull, then the column and key names are expected to | |
2422 | * be separated by ':'). The key is stored as a malloc()'d string into | |
2423 | * '*keyp', or NULL if no key is present in 'arg'. | |
2424 | * | |
2425 | * - If 'valuep' is nonnull, an operator followed by a value string. The | |
2426 | * allowed operators are the 'n_allowed' string in 'allowed_operators', | |
2427 | * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the | |
2428 | * operator is stored into '*operatorp' (one of the pointers from | |
2429 | * 'allowed_operators' is stored; nothing is malloc()'d). The value is | |
2430 | * stored as a malloc()'d string into '*valuep', or NULL if no value is | |
2431 | * present in 'arg'. | |
2432 | * | |
2433 | * At least 'columnp' or 'keyp' must be nonnull. | |
2434 | * | |
2435 | * On success, returns NULL. On failure, returned a malloc()'d string error | |
2436 | * message and stores NULL into all of the nonnull output arguments. */ | |
1bc6ff29 | 2437 | static char * WARN_UNUSED_RESULT |
e89e5374 BP |
2438 | parse_column_key_value(const char *arg, |
2439 | const struct vsctl_table_class *table, | |
2440 | const struct ovsdb_idl_column **columnp, char **keyp, | |
2441 | const char **operatorp, | |
2442 | const char **allowed_operators, size_t n_allowed, | |
2443 | char **valuep) | |
ad83bfa6 BP |
2444 | { |
2445 | const char *p = arg; | |
1bc6ff29 | 2446 | char *error; |
ad83bfa6 BP |
2447 | |
2448 | assert(columnp || keyp); | |
e89e5374 | 2449 | assert(!(operatorp && !valuep)); |
1bc6ff29 BP |
2450 | if (keyp) { |
2451 | *keyp = NULL; | |
2452 | } | |
2453 | if (valuep) { | |
2454 | *valuep = NULL; | |
2455 | } | |
ad83bfa6 BP |
2456 | |
2457 | /* Parse column name. */ | |
2458 | if (columnp) { | |
2459 | char *column_name; | |
2460 | ||
557e3718 | 2461 | error = ovsdb_token_parse(&p, &column_name); |
1bc6ff29 BP |
2462 | if (error) { |
2463 | goto error; | |
2464 | } | |
ad83bfa6 | 2465 | if (column_name[0] == '\0') { |
1bc6ff29 BP |
2466 | free(column_name); |
2467 | error = xasprintf("%s: missing column name", arg); | |
2468 | goto error; | |
2469 | } | |
2470 | error = get_column(table, column_name, columnp); | |
a3326252 | 2471 | free(column_name); |
1bc6ff29 BP |
2472 | if (error) { |
2473 | goto error; | |
ad83bfa6 | 2474 | } |
ad83bfa6 BP |
2475 | } |
2476 | ||
2477 | /* Parse key string. */ | |
2478 | if (*p == ':' || !columnp) { | |
2479 | if (columnp) { | |
2480 | p++; | |
2481 | } else if (!keyp) { | |
1bc6ff29 BP |
2482 | error = xasprintf("%s: key not accepted here", arg); |
2483 | goto error; | |
2484 | } | |
2485 | error = ovsdb_token_parse(&p, keyp); | |
2486 | if (error) { | |
2487 | goto error; | |
ad83bfa6 | 2488 | } |
ad83bfa6 BP |
2489 | } else if (keyp) { |
2490 | *keyp = NULL; | |
2491 | } | |
2492 | ||
2493 | /* Parse value string. */ | |
e89e5374 BP |
2494 | if (valuep) { |
2495 | const char *best; | |
2496 | size_t best_len; | |
2497 | size_t i; | |
2498 | ||
2499 | if (!allowed_operators) { | |
2500 | static const char *equals = "="; | |
2501 | allowed_operators = = | |
2502 | n_allowed = 1; | |
2503 | } | |
2504 | ||
2505 | best = NULL; | |
2506 | best_len = 0; | |
2507 | for (i = 0; i < n_allowed; i++) { | |
2508 | const char *op = allowed_operators[i]; | |
2509 | size_t op_len = strlen(op); | |
2510 | ||
7db03f7c | 2511 | if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) { |
e89e5374 BP |
2512 | best_len = op_len; |
2513 | best = op; | |
2514 | } | |
2515 | } | |
2516 | if (!best) { | |
2517 | error = missing_operator_error(arg, allowed_operators, n_allowed); | |
1bc6ff29 | 2518 | goto error; |
ad83bfa6 | 2519 | } |
e89e5374 BP |
2520 | |
2521 | if (operatorp) { | |
2522 | *operatorp = best; | |
2523 | } | |
7db03f7c | 2524 | *valuep = xstrdup(p + best_len); |
ad83bfa6 BP |
2525 | } else { |
2526 | if (valuep) { | |
2527 | *valuep = NULL; | |
2528 | } | |
1bc6ff29 | 2529 | if (*p != '\0') { |
c29a8ba8 BP |
2530 | error = xasprintf("%s: trailing garbage \"%s\" in argument", |
2531 | arg, p); | |
1bc6ff29 BP |
2532 | goto error; |
2533 | } | |
2534 | } | |
2535 | return NULL; | |
2536 | ||
2537 | error: | |
2538 | if (columnp) { | |
2539 | *columnp = NULL; | |
2540 | } | |
2541 | if (keyp) { | |
2542 | free(*keyp); | |
2543 | *keyp = NULL; | |
2544 | } | |
2545 | if (valuep) { | |
2546 | free(*valuep); | |
2547 | *valuep = NULL; | |
e89e5374 BP |
2548 | if (operatorp) { |
2549 | *operatorp = NULL; | |
2550 | } | |
ad83bfa6 | 2551 | } |
1bc6ff29 | 2552 | return error; |
ad83bfa6 BP |
2553 | } |
2554 | ||
e5e12280 BP |
2555 | static void |
2556 | pre_parse_column_key_value(struct vsctl_context *ctx, | |
2557 | const char *arg, | |
2558 | const struct vsctl_table_class *table) | |
2559 | { | |
2560 | const struct ovsdb_idl_column *column; | |
2561 | const char *p; | |
2562 | char *column_name; | |
2563 | ||
2564 | p = arg; | |
2565 | die_if_error(ovsdb_token_parse(&p, &column_name)); | |
2566 | if (column_name[0] == '\0') { | |
2567 | vsctl_fatal("%s: missing column name", arg); | |
2568 | } | |
2569 | ||
2570 | pre_get_column(ctx, table, column_name, &column); | |
2571 | free(column_name); | |
2572 | } | |
2573 | ||
2574 | static void | |
2575 | pre_cmd_get(struct vsctl_context *ctx) | |
2576 | { | |
2577 | const char *table_name = ctx->argv[1]; | |
2578 | const struct vsctl_table_class *table; | |
2579 | int i; | |
2580 | ||
2581 | table = pre_get_table(ctx, table_name); | |
2582 | for (i = 3; i < ctx->argc; i++) { | |
2583 | if (!strcasecmp(ctx->argv[i], "_uuid") | |
2584 | || !strcasecmp(ctx->argv[i], "-uuid")) { | |
2585 | continue; | |
2586 | } | |
2587 | ||
2588 | pre_parse_column_key_value(ctx, ctx->argv[i], table); | |
2589 | } | |
2590 | } | |
2591 | ||
ad83bfa6 BP |
2592 | static void |
2593 | cmd_get(struct vsctl_context *ctx) | |
2594 | { | |
aed133bf | 2595 | const char *id = shash_find_data(&ctx->options, "--id"); |
870aeb4a | 2596 | bool if_exists = shash_find(&ctx->options, "--if-exists"); |
ad83bfa6 BP |
2597 | const char *table_name = ctx->argv[1]; |
2598 | const char *record_id = ctx->argv[2]; | |
2599 | const struct vsctl_table_class *table; | |
2600 | const struct ovsdb_idl_row *row; | |
2601 | struct ds *out = &ctx->output; | |
2602 | int i; | |
2603 | ||
2604 | table = get_table(table_name); | |
b7f74b6f | 2605 | row = must_get_row(ctx, table, record_id); |
aed133bf BP |
2606 | if (id) { |
2607 | bool new; | |
2608 | ||
2609 | *create_symbol(ctx->symtab, id, &new) = row->uuid; | |
2610 | if (!new) { | |
2611 | vsctl_fatal("row id \"%s\" specified on \"get\" command was used " | |
2612 | "before it was defined", id); | |
2613 | } | |
2614 | } | |
ad83bfa6 | 2615 | for (i = 3; i < ctx->argc; i++) { |
bd76d25d | 2616 | const struct ovsdb_idl_column *column; |
8c3c2f30 | 2617 | const struct ovsdb_datum *datum; |
ad83bfa6 BP |
2618 | char *key_string; |
2619 | ||
f40a9b61 BP |
2620 | /* Special case for obtaining the UUID of a row. We can't just do this |
2621 | * through parse_column_key_value() below since it returns a "struct | |
2622 | * ovsdb_idl_column" and the UUID column doesn't have one. */ | |
2623 | if (!strcasecmp(ctx->argv[i], "_uuid") | |
2624 | || !strcasecmp(ctx->argv[i], "-uuid")) { | |
2625 | ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid)); | |
2626 | continue; | |
2627 | } | |
2628 | ||
1bc6ff29 | 2629 | die_if_error(parse_column_key_value(ctx->argv[i], table, |
e89e5374 BP |
2630 | &column, &key_string, |
2631 | NULL, NULL, 0, NULL)); | |
ad83bfa6 | 2632 | |
f74055e7 | 2633 | ovsdb_idl_txn_verify(row, column); |
8c3c2f30 | 2634 | datum = ovsdb_idl_read(row, column); |
ad83bfa6 BP |
2635 | if (key_string) { |
2636 | union ovsdb_atom key; | |
2637 | unsigned int idx; | |
2638 | ||
bd76d25d | 2639 | if (column->type.value.type == OVSDB_TYPE_VOID) { |
def90f62 | 2640 | vsctl_fatal("cannot specify key to get for non-map column %s", |
bd76d25d | 2641 | column->name); |
ad83bfa6 BP |
2642 | } |
2643 | ||
1bc6ff29 | 2644 | die_if_error(ovsdb_atom_from_string(&key, |
bd76d25d | 2645 | &column->type.key, |
ce5a3e38 | 2646 | key_string, ctx->symtab)); |
ad83bfa6 | 2647 | |
8c3c2f30 | 2648 | idx = ovsdb_datum_find_key(datum, &key, |
bd76d25d | 2649 | column->type.key.type); |
ad83bfa6 | 2650 | if (idx == UINT_MAX) { |
870aeb4a | 2651 | if (!if_exists) { |
def90f62 BP |
2652 | vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s", |
2653 | key_string, table->class->name, record_id, | |
bd76d25d | 2654 | column->name); |
870aeb4a BP |
2655 | } |
2656 | } else { | |
8c3c2f30 | 2657 | ovsdb_atom_to_string(&datum->values[idx], |
bd76d25d | 2658 | column->type.value.type, out); |
ad83bfa6 | 2659 | } |
bd76d25d | 2660 | ovsdb_atom_destroy(&key, column->type.key.type); |
ad83bfa6 | 2661 | } else { |
8c3c2f30 | 2662 | ovsdb_datum_to_string(datum, &column->type, out); |
ad83bfa6 BP |
2663 | } |
2664 | ds_put_char(out, '\n'); | |
ad83bfa6 BP |
2665 | |
2666 | free(key_string); | |
2667 | } | |
2668 | } | |
2669 | ||
9591fefe BP |
2670 | static void |
2671 | parse_column_names(const char *column_names, | |
2672 | const struct vsctl_table_class *table, | |
2673 | const struct ovsdb_idl_column ***columnsp, | |
2674 | size_t *n_columnsp) | |
2675 | { | |
2676 | const struct ovsdb_idl_column **columns; | |
2677 | size_t n_columns; | |
2678 | ||
2679 | if (!column_names) { | |
2680 | size_t i; | |
2681 | ||
2682 | n_columns = table->class->n_columns + 1; | |
2683 | columns = xmalloc(n_columns * sizeof *columns); | |
2684 | columns[0] = NULL; | |
2685 | for (i = 0; i < table->class->n_columns; i++) { | |
2686 | columns[i + 1] = &table->class->columns[i]; | |
2687 | } | |
2688 | } else { | |
2689 | char *s = xstrdup(column_names); | |
2690 | size_t allocated_columns; | |
2691 | char *save_ptr = NULL; | |
2692 | char *column_name; | |
2693 | ||
2694 | columns = NULL; | |
2695 | allocated_columns = n_columns = 0; | |
2696 | for (column_name = strtok_r(s, ", ", &save_ptr); column_name; | |
2697 | column_name = strtok_r(NULL, ", ", &save_ptr)) { | |
2698 | const struct ovsdb_idl_column *column; | |
2699 | ||
2700 | if (!strcasecmp(column_name, "_uuid")) { | |
2701 | column = NULL; | |
2702 | } else { | |
2703 | die_if_error(get_column(table, column_name, &column)); | |
2704 | } | |
2705 | if (n_columns >= allocated_columns) { | |
2706 | columns = x2nrealloc(columns, &allocated_columns, | |
2707 | sizeof *columns); | |
2708 | } | |
2709 | columns[n_columns++] = column; | |
2710 | } | |
2711 | free(s); | |
2712 | ||
2713 | if (!n_columns) { | |
2714 | vsctl_fatal("must specify at least one column name"); | |
2715 | } | |
2716 | } | |
2717 | *columnsp = columns; | |
2718 | *n_columnsp = n_columns; | |
2719 | } | |
2720 | ||
2721 | ||
2722 | static void | |
2723 | pre_list_columns(struct vsctl_context *ctx, | |
2724 | const struct vsctl_table_class *table, | |
2725 | const char *column_names) | |
2726 | { | |
2727 | const struct ovsdb_idl_column **columns; | |
2728 | size_t n_columns; | |
2729 | size_t i; | |
2730 | ||
2731 | parse_column_names(column_names, table, &columns, &n_columns); | |
2732 | for (i = 0; i < n_columns; i++) { | |
2733 | if (columns[i]) { | |
2734 | ovsdb_idl_add_column(ctx->idl, columns[i]); | |
2735 | } | |
2736 | } | |
2737 | free(columns); | |
2738 | } | |
2739 | ||
e5e12280 BP |
2740 | static void |
2741 | pre_cmd_list(struct vsctl_context *ctx) | |
2742 | { | |
9591fefe | 2743 | const char *column_names = shash_find_data(&ctx->options, "--columns"); |
e5e12280 BP |
2744 | const char *table_name = ctx->argv[1]; |
2745 | const struct vsctl_table_class *table; | |
e5e12280 BP |
2746 | |
2747 | table = pre_get_table(ctx, table_name); | |
9591fefe | 2748 | pre_list_columns(ctx, table, column_names); |
e5e12280 BP |
2749 | } |
2750 | ||
e051b42c BP |
2751 | static struct table * |
2752 | list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns) | |
2753 | { | |
2754 | struct table *out; | |
2755 | size_t i; | |
2756 | ||
2757 | out = xmalloc(sizeof *out); | |
2758 | table_init(out); | |
2759 | ||
2760 | for (i = 0; i < n_columns; i++) { | |
2761 | const struct ovsdb_idl_column *column = columns[i]; | |
2762 | const char *column_name = column ? column->name : "_uuid"; | |
2763 | ||
2764 | table_add_column(out, "%s", column_name); | |
2765 | } | |
2766 | ||
2767 | return out; | |
2768 | } | |
2769 | ||
ad83bfa6 | 2770 | static void |
9591fefe BP |
2771 | list_record(const struct ovsdb_idl_row *row, |
2772 | const struct ovsdb_idl_column **columns, size_t n_columns, | |
e051b42c | 2773 | struct table *out) |
ad83bfa6 | 2774 | { |
bd76d25d | 2775 | size_t i; |
ad83bfa6 | 2776 | |
e051b42c | 2777 | table_add_row(out); |
9591fefe BP |
2778 | for (i = 0; i < n_columns; i++) { |
2779 | const struct ovsdb_idl_column *column = columns[i]; | |
e051b42c | 2780 | struct cell *cell = table_add_cell(out); |
ad83bfa6 | 2781 | |
9591fefe | 2782 | if (!column) { |
e051b42c BP |
2783 | struct ovsdb_datum datum; |
2784 | union ovsdb_atom atom; | |
2785 | ||
2786 | atom.uuid = row->uuid; | |
2787 | ||
2788 | datum.keys = &atom; | |
2789 | datum.values = NULL; | |
2790 | datum.n = 1; | |
2791 | ||
2792 | cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid); | |
2793 | cell->type = &ovsdb_type_uuid; | |
9591fefe BP |
2794 | } else { |
2795 | const struct ovsdb_datum *datum = ovsdb_idl_read(row, column); | |
e051b42c BP |
2796 | |
2797 | cell->json = ovsdb_datum_to_json(datum, &column->type); | |
2798 | cell->type = &column->type; | |
9591fefe | 2799 | } |
ad83bfa6 BP |
2800 | } |
2801 | } | |
2802 | ||
2803 | static void | |
2804 | cmd_list(struct vsctl_context *ctx) | |
2805 | { | |
9591fefe BP |
2806 | const char *column_names = shash_find_data(&ctx->options, "--columns"); |
2807 | const struct ovsdb_idl_column **columns; | |
ad83bfa6 BP |
2808 | const char *table_name = ctx->argv[1]; |
2809 | const struct vsctl_table_class *table; | |
e051b42c | 2810 | struct table *out; |
9591fefe | 2811 | size_t n_columns; |
ad83bfa6 BP |
2812 | int i; |
2813 | ||
2814 | table = get_table(table_name); | |
9591fefe | 2815 | parse_column_names(column_names, table, &columns, &n_columns); |
e051b42c | 2816 | out = ctx->table = list_make_table(columns, n_columns); |
ad83bfa6 BP |
2817 | if (ctx->argc > 2) { |
2818 | for (i = 2; i < ctx->argc; i++) { | |
9591fefe BP |
2819 | list_record(must_get_row(ctx, table, ctx->argv[i]), |
2820 | columns, n_columns, out); | |
ad83bfa6 BP |
2821 | } |
2822 | } else { | |
2823 | const struct ovsdb_idl_row *row; | |
2824 | bool first; | |
2825 | ||
2826 | for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true; | |
2827 | row != NULL; | |
2828 | row = ovsdb_idl_next_row(row), first = false) { | |
9591fefe | 2829 | list_record(row, columns, n_columns, out); |
ad83bfa6 BP |
2830 | } |
2831 | } | |
9591fefe | 2832 | free(columns); |
ad83bfa6 BP |
2833 | } |
2834 | ||
0a140468 BP |
2835 | static void |
2836 | pre_cmd_find(struct vsctl_context *ctx) | |
2837 | { | |
2838 | const char *column_names = shash_find_data(&ctx->options, "--columns"); | |
2839 | const char *table_name = ctx->argv[1]; | |
2840 | const struct vsctl_table_class *table; | |
2841 | int i; | |
2842 | ||
2843 | table = pre_get_table(ctx, table_name); | |
2844 | pre_list_columns(ctx, table, column_names); | |
2845 | for (i = 2; i < ctx->argc; i++) { | |
2846 | pre_parse_column_key_value(ctx, ctx->argv[i], table); | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | static void | |
2851 | cmd_find(struct vsctl_context *ctx) | |
2852 | { | |
2853 | const char *column_names = shash_find_data(&ctx->options, "--columns"); | |
2854 | const struct ovsdb_idl_column **columns; | |
2855 | const char *table_name = ctx->argv[1]; | |
2856 | const struct vsctl_table_class *table; | |
2857 | const struct ovsdb_idl_row *row; | |
e051b42c | 2858 | struct table *out; |
0a140468 BP |
2859 | size_t n_columns; |
2860 | ||
2861 | table = get_table(table_name); | |
2862 | parse_column_names(column_names, table, &columns, &n_columns); | |
e051b42c | 2863 | out = ctx->table = list_make_table(columns, n_columns); |
0a140468 BP |
2864 | for (row = ovsdb_idl_first_row(ctx->idl, table->class); row; |
2865 | row = ovsdb_idl_next_row(row)) { | |
2866 | int i; | |
2867 | ||
2868 | for (i = 2; i < ctx->argc; i++) { | |
2869 | if (!is_condition_satisfied(table, row, ctx->argv[i], | |
2870 | ctx->symtab)) { | |
2871 | goto next_row; | |
2872 | } | |
2873 | } | |
0a140468 BP |
2874 | list_record(row, columns, n_columns, out); |
2875 | ||
2876 | next_row: ; | |
2877 | } | |
2878 | } | |
2879 | ||
e5e12280 BP |
2880 | static void |
2881 | pre_cmd_set(struct vsctl_context *ctx) | |
2882 | { | |
2883 | const char *table_name = ctx->argv[1]; | |
2884 | const struct vsctl_table_class *table; | |
2885 | int i; | |
2886 | ||
2887 | table = pre_get_table(ctx, table_name); | |
2888 | for (i = 3; i < ctx->argc; i++) { | |
2889 | pre_parse_column_key_value(ctx, ctx->argv[i], table); | |
2890 | } | |
2891 | } | |
2892 | ||
ad83bfa6 | 2893 | static void |
557e3718 | 2894 | set_column(const struct vsctl_table_class *table, |
ce5a3e38 BP |
2895 | const struct ovsdb_idl_row *row, const char *arg, |
2896 | struct ovsdb_symbol_table *symtab) | |
ad83bfa6 | 2897 | { |
bd76d25d | 2898 | const struct ovsdb_idl_column *column; |
557e3718 BP |
2899 | char *key_string, *value_string; |
2900 | char *error; | |
ad83bfa6 | 2901 | |
557e3718 | 2902 | error = parse_column_key_value(arg, table, &column, &key_string, |
e89e5374 | 2903 | NULL, NULL, 0, &value_string); |
557e3718 | 2904 | die_if_error(error); |
557e3718 | 2905 | if (!value_string) { |
def90f62 | 2906 | vsctl_fatal("%s: missing value", arg); |
557e3718 | 2907 | } |
ad83bfa6 | 2908 | |
557e3718 BP |
2909 | if (key_string) { |
2910 | union ovsdb_atom key, value; | |
8c3c2f30 | 2911 | struct ovsdb_datum datum; |
557e3718 | 2912 | |
bd76d25d | 2913 | if (column->type.value.type == OVSDB_TYPE_VOID) { |
def90f62 | 2914 | vsctl_fatal("cannot specify key to set for non-map column %s", |
bd76d25d | 2915 | column->name); |
ad83bfa6 BP |
2916 | } |
2917 | ||
bd76d25d | 2918 | die_if_error(ovsdb_atom_from_string(&key, &column->type.key, |
ce5a3e38 | 2919 | key_string, symtab)); |
bd76d25d | 2920 | die_if_error(ovsdb_atom_from_string(&value, &column->type.value, |
ce5a3e38 | 2921 | value_string, symtab)); |
ad83bfa6 | 2922 | |
8c3c2f30 BP |
2923 | ovsdb_datum_init_empty(&datum); |
2924 | ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type); | |
ad83bfa6 | 2925 | |
bd76d25d BP |
2926 | ovsdb_atom_destroy(&key, column->type.key.type); |
2927 | ovsdb_atom_destroy(&value, column->type.value.type); | |
a3326252 | 2928 | |
8c3c2f30 BP |
2929 | ovsdb_datum_union(&datum, ovsdb_idl_read(row, column), |
2930 | &column->type, false); | |
2931 | ovsdb_idl_txn_write(row, column, &datum); | |
557e3718 BP |
2932 | } else { |
2933 | struct ovsdb_datum datum; | |
ad83bfa6 | 2934 | |
bd76d25d | 2935 | die_if_error(ovsdb_datum_from_string(&datum, &column->type, |
ce5a3e38 | 2936 | value_string, symtab)); |
bd76d25d | 2937 | ovsdb_idl_txn_write(row, column, &datum); |
557e3718 | 2938 | } |
ad83bfa6 | 2939 | |
557e3718 | 2940 | free(key_string); |
a3326252 | 2941 | free(value_string); |
557e3718 | 2942 | } |
ad83bfa6 | 2943 | |
557e3718 BP |
2944 | static void |
2945 | cmd_set(struct vsctl_context *ctx) | |
2946 | { | |
557e3718 BP |
2947 | const char *table_name = ctx->argv[1]; |
2948 | const char *record_id = ctx->argv[2]; | |
2949 | const struct vsctl_table_class *table; | |
2950 | const struct ovsdb_idl_row *row; | |
2951 | int i; | |
ad83bfa6 | 2952 | |
557e3718 | 2953 | table = get_table(table_name); |
b7f74b6f | 2954 | row = must_get_row(ctx, table, record_id); |
557e3718 | 2955 | for (i = 3; i < ctx->argc; i++) { |
ce5a3e38 | 2956 | set_column(table, row, ctx->argv[i], ctx->symtab); |
ad83bfa6 BP |
2957 | } |
2958 | } | |
2959 | ||
e5e12280 BP |
2960 | static void |
2961 | pre_cmd_add(struct vsctl_context *ctx) | |
2962 | { | |
2963 | const char *table_name = ctx->argv[1]; | |
2964 | const char *column_name = ctx->argv[3]; | |
2965 | const struct vsctl_table_class *table; | |
2966 | const struct ovsdb_idl_column *column; | |
2967 | ||
2968 | table = pre_get_table(ctx, table_name); | |
2969 | pre_get_column(ctx, table, column_name, &column); | |
2970 | } | |
2971 | ||
ad83bfa6 BP |
2972 | static void |
2973 | cmd_add(struct vsctl_context *ctx) | |
2974 | { | |
2975 | const char *table_name = ctx->argv[1]; | |
2976 | const char *record_id = ctx->argv[2]; | |
2977 | const char *column_name = ctx->argv[3]; | |
2978 | const struct vsctl_table_class *table; | |
bd76d25d | 2979 | const struct ovsdb_idl_column *column; |
ad83bfa6 BP |
2980 | const struct ovsdb_idl_row *row; |
2981 | const struct ovsdb_type *type; | |
2982 | struct ovsdb_datum old; | |
2983 | int i; | |
2984 | ||
2985 | table = get_table(table_name); | |
b7f74b6f | 2986 | row = must_get_row(ctx, table, record_id); |
1bc6ff29 | 2987 | die_if_error(get_column(table, column_name, &column)); |
c29a8ba8 | 2988 | |
bd76d25d | 2989 | type = &column->type; |
8c3c2f30 | 2990 | ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type); |
ad83bfa6 BP |
2991 | for (i = 4; i < ctx->argc; i++) { |
2992 | struct ovsdb_type add_type; | |
2993 | struct ovsdb_datum add; | |
2994 | ||
ad83bfa6 BP |
2995 | add_type = *type; |
2996 | add_type.n_min = 1; | |
2997 | add_type.n_max = UINT_MAX; | |
ce5a3e38 BP |
2998 | die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i], |
2999 | ctx->symtab)); | |
ad83bfa6 BP |
3000 | ovsdb_datum_union(&old, &add, type, false); |
3001 | ovsdb_datum_destroy(&add, type); | |
3002 | } | |
3003 | if (old.n > type->n_max) { | |
def90f62 BP |
3004 | vsctl_fatal("\"add\" operation would put %u %s in column %s of " |
3005 | "table %s but the maximum number is %u", | |
3006 | old.n, | |
bd76d25d BP |
3007 | type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs", |
3008 | column->name, table->class->name, type->n_max); | |
ad83bfa6 | 3009 | } |
f74055e7 | 3010 | ovsdb_idl_txn_verify(row, column); |
bd76d25d | 3011 | ovsdb_idl_txn_write(row, column, &old); |
ad83bfa6 | 3012 | } |
90c4bd00 | 3013 | |
e5e12280 BP |
3014 | static void |
3015 | pre_cmd_remove(struct vsctl_context *ctx) | |
3016 | { | |
3017 | const char *table_name = ctx->argv[1]; | |
3018 | const char *column_name = ctx->argv[3]; | |
3019 | const struct vsctl_table_class *table; | |
3020 | const struct ovsdb_idl_column *column; | |
3021 | ||
3022 | table = pre_get_table(ctx, table_name); | |
3023 | pre_get_column(ctx, table, column_name, &column); | |
3024 | } | |
3025 | ||
90c4bd00 BP |
3026 | static void |
3027 | cmd_remove(struct vsctl_context *ctx) | |
3028 | { | |
3029 | const char *table_name = ctx->argv[1]; | |
3030 | const char *record_id = ctx->argv[2]; | |
3031 | const char *column_name = ctx->argv[3]; | |
3032 | const struct vsctl_table_class *table; | |
bd76d25d | 3033 | const struct ovsdb_idl_column *column; |
90c4bd00 BP |
3034 | const struct ovsdb_idl_row *row; |
3035 | const struct ovsdb_type *type; | |
3036 | struct ovsdb_datum old; | |
3037 | int i; | |
3038 | ||
3039 | table = get_table(table_name); | |
b7f74b6f | 3040 | row = must_get_row(ctx, table, record_id); |
90c4bd00 | 3041 | die_if_error(get_column(table, column_name, &column)); |
c29a8ba8 | 3042 | |
bd76d25d | 3043 | type = &column->type; |
8c3c2f30 | 3044 | ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type); |
90c4bd00 BP |
3045 | for (i = 4; i < ctx->argc; i++) { |
3046 | struct ovsdb_type rm_type; | |
3047 | struct ovsdb_datum rm; | |
3048 | char *error; | |
3049 | ||
90c4bd00 BP |
3050 | rm_type = *type; |
3051 | rm_type.n_min = 1; | |
3052 | rm_type.n_max = UINT_MAX; | |
ce5a3e38 BP |
3053 | error = ovsdb_datum_from_string(&rm, &rm_type, |
3054 | ctx->argv[i], ctx->symtab); | |
90c4bd00 BP |
3055 | if (error && ovsdb_type_is_map(&rm_type)) { |
3056 | free(error); | |
bd76d25d | 3057 | rm_type.value.type = OVSDB_TYPE_VOID; |
ce5a3e38 BP |
3058 | die_if_error(ovsdb_datum_from_string(&rm, &rm_type, |
3059 | ctx->argv[i], ctx->symtab)); | |
90c4bd00 BP |
3060 | } |
3061 | ovsdb_datum_subtract(&old, type, &rm, &rm_type); | |
3062 | ovsdb_datum_destroy(&rm, &rm_type); | |
3063 | } | |
3064 | if (old.n < type->n_min) { | |
def90f62 | 3065 | vsctl_fatal("\"remove\" operation would put %u %s in column %s of " |
f19f25a4 | 3066 | "table %s but the minimum number is %u", |
def90f62 | 3067 | old.n, |
bd76d25d BP |
3068 | type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs", |
3069 | column->name, table->class->name, type->n_min); | |
90c4bd00 | 3070 | } |
f74055e7 | 3071 | ovsdb_idl_txn_verify(row, column); |
bd76d25d | 3072 | ovsdb_idl_txn_write(row, column, &old); |
90c4bd00 BP |
3073 | } |
3074 | ||
e5e12280 BP |
3075 | static void |
3076 | pre_cmd_clear(struct vsctl_context *ctx) | |
3077 | { | |
3078 | const char *table_name = ctx->argv[1]; | |
3079 | const struct vsctl_table_class *table; | |
3080 | int i; | |
3081 | ||
3082 | table = pre_get_table(ctx, table_name); | |
3083 | for (i = 3; i < ctx->argc; i++) { | |
3084 | const struct ovsdb_idl_column *column; | |
3085 | ||
3086 | pre_get_column(ctx, table, ctx->argv[i], &column); | |
3087 | } | |
3088 | } | |
3089 | ||
90c4bd00 BP |
3090 | static void |
3091 | cmd_clear(struct vsctl_context *ctx) | |
3092 | { | |
3093 | const char *table_name = ctx->argv[1]; | |
3094 | const char *record_id = ctx->argv[2]; | |
3095 | const struct vsctl_table_class *table; | |
3096 | const struct ovsdb_idl_row *row; | |
3097 | int i; | |
3098 | ||
3099 | table = get_table(table_name); | |
b7f74b6f | 3100 | row = must_get_row(ctx, table, record_id); |
90c4bd00 | 3101 | for (i = 3; i < ctx->argc; i++) { |
bd76d25d | 3102 | const struct ovsdb_idl_column *column; |
90c4bd00 BP |
3103 | const struct ovsdb_type *type; |
3104 | struct ovsdb_datum datum; | |
3105 | ||
3106 | die_if_error(get_column(table, ctx->argv[i], &column)); | |
3107 | ||
bd76d25d BP |
3108 | type = &column->type; |
3109 | if (type->n_min > 0) { | |
def90f62 BP |
3110 | vsctl_fatal("\"clear\" operation cannot be applied to column %s " |
3111 | "of table %s, which is not allowed to be empty", | |
bd76d25d | 3112 | column->name, table->class->name); |
90c4bd00 BP |
3113 | } |
3114 | ||
3115 | ovsdb_datum_init_empty(&datum); | |
bd76d25d | 3116 | ovsdb_idl_txn_write(row, column, &datum); |
90c4bd00 BP |
3117 | } |
3118 | } | |
557e3718 BP |
3119 | |
3120 | static void | |
3121 | cmd_create(struct vsctl_context *ctx) | |
3122 | { | |
ce5a3e38 | 3123 | const char *id = shash_find_data(&ctx->options, "--id"); |
557e3718 BP |
3124 | const char *table_name = ctx->argv[1]; |
3125 | const struct vsctl_table_class *table; | |
3126 | const struct ovsdb_idl_row *row; | |
ce5a3e38 | 3127 | const struct uuid *uuid; |
557e3718 BP |
3128 | int i; |
3129 | ||
aed133bf | 3130 | uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL; |
ce5a3e38 | 3131 | |
557e3718 | 3132 | table = get_table(table_name); |
ce5a3e38 | 3133 | row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid); |
557e3718 | 3134 | for (i = 2; i < ctx->argc; i++) { |
ce5a3e38 | 3135 | set_column(table, row, ctx->argv[i], ctx->symtab); |
557e3718 | 3136 | } |
f8ff4bc4 | 3137 | ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid)); |
557e3718 | 3138 | } |
b7f74b6f | 3139 | |
3ef917b5 BP |
3140 | /* This function may be used as the 'postprocess' function for commands that |
3141 | * insert new rows into the database. It expects that the command's 'run' | |
3142 | * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's | |
3143 | * sole output. It replaces that output by the row's permanent UUID assigned | |
3144 | * by the database server and appends a new-line. | |
3145 | * | |
3146 | * Currently we use this only for "create", because the higher-level commands | |
3147 | * are supposed to be independent of the actual structure of the vswitch | |
3148 | * configuration. */ | |
3149 | static void | |
3150 | post_create(struct vsctl_context *ctx) | |
3151 | { | |
3152 | const struct uuid *real; | |
3153 | struct uuid dummy; | |
3154 | ||
3155 | uuid_from_string(&dummy, ds_cstr(&ctx->output)); | |
3156 | real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy); | |
3157 | if (real) { | |
3158 | ds_clear(&ctx->output); | |
3159 | ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real)); | |
3160 | } | |
3161 | ds_put_char(&ctx->output, '\n'); | |
3162 | } | |
3163 | ||
e5e12280 BP |
3164 | static void |
3165 | pre_cmd_destroy(struct vsctl_context *ctx) | |
3166 | { | |
3167 | const char *table_name = ctx->argv[1]; | |
3168 | ||
3169 | pre_get_table(ctx, table_name); | |
3170 | } | |
3171 | ||
b7f74b6f BP |
3172 | static void |
3173 | cmd_destroy(struct vsctl_context *ctx) | |
3174 | { | |
b7f74b6f BP |
3175 | bool must_exist = !shash_find(&ctx->options, "--if-exists"); |
3176 | const char *table_name = ctx->argv[1]; | |
3177 | const struct vsctl_table_class *table; | |
3178 | int i; | |
3179 | ||
b7f74b6f | 3180 | table = get_table(table_name); |
f8ff4bc4 | 3181 | for (i = 2; i < ctx->argc; i++) { |
b7f74b6f BP |
3182 | const struct ovsdb_idl_row *row; |
3183 | ||
3184 | row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]); | |
3185 | if (row) { | |
3186 | ovsdb_idl_txn_delete(row); | |
3187 | } | |
3188 | } | |
3189 | } | |
7db03f7c BP |
3190 | |
3191 | static bool | |
f158c54a BP |
3192 | is_condition_satisfied(const struct vsctl_table_class *table, |
3193 | const struct ovsdb_idl_row *row, const char *arg, | |
3194 | struct ovsdb_symbol_table *symtab) | |
7db03f7c BP |
3195 | { |
3196 | static const char *operators[] = { | |
3197 | "=", "!=", "<", ">", "<=", ">=" | |
3198 | }; | |
3199 | ||
3200 | const struct ovsdb_idl_column *column; | |
8c3c2f30 | 3201 | const struct ovsdb_datum *have_datum; |
7db03f7c | 3202 | char *key_string, *value_string; |
7db03f7c BP |
3203 | const char *operator; |
3204 | unsigned int idx; | |
3205 | char *error; | |
af9af3e2 | 3206 | int cmp = 0; |
7db03f7c BP |
3207 | |
3208 | error = parse_column_key_value(arg, table, &column, &key_string, | |
3209 | &operator, operators, ARRAY_SIZE(operators), | |
3210 | &value_string); | |
3211 | die_if_error(error); | |
3212 | if (!value_string) { | |
3213 | vsctl_fatal("%s: missing value", arg); | |
3214 | } | |
3215 | ||
8c3c2f30 | 3216 | have_datum = ovsdb_idl_read(row, column); |
7db03f7c BP |
3217 | if (key_string) { |
3218 | union ovsdb_atom want_key, want_value; | |
3219 | ||
3220 | if (column->type.value.type == OVSDB_TYPE_VOID) { | |
3221 | vsctl_fatal("cannot specify key to check for non-map column %s", | |
3222 | column->name); | |
3223 | } | |
3224 | ||
3225 | die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key, | |
3226 | key_string, symtab)); | |
3227 | die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value, | |
3228 | value_string, symtab)); | |
3229 | ||
8c3c2f30 | 3230 | idx = ovsdb_datum_find_key(have_datum, |
7db03f7c BP |
3231 | &want_key, column->type.key.type); |
3232 | if (idx != UINT_MAX) { | |
8c3c2f30 | 3233 | cmp = ovsdb_atom_compare_3way(&have_datum->values[idx], |
7db03f7c BP |
3234 | &want_value, |
3235 | column->type.value.type); | |
3236 | } | |
3237 | ||
3238 | ovsdb_atom_destroy(&want_key, column->type.key.type); | |
3239 | ovsdb_atom_destroy(&want_value, column->type.value.type); | |
3240 | } else { | |
3241 | struct ovsdb_datum want_datum; | |
3242 | ||
3243 | die_if_error(ovsdb_datum_from_string(&want_datum, &column->type, | |
3244 | value_string, symtab)); | |
3245 | idx = 0; | |
8c3c2f30 | 3246 | cmp = ovsdb_datum_compare_3way(have_datum, &want_datum, |
7db03f7c BP |
3247 | &column->type); |
3248 | ovsdb_datum_destroy(&want_datum, &column->type); | |
3249 | } | |
7db03f7c BP |
3250 | |
3251 | free(key_string); | |
3252 | free(value_string); | |
3253 | ||
3254 | return (idx == UINT_MAX ? false | |
3255 | : !strcmp(operator, "=") ? cmp == 0 | |
3256 | : !strcmp(operator, "!=") ? cmp != 0 | |
3257 | : !strcmp(operator, "<") ? cmp < 0 | |
3258 | : !strcmp(operator, ">") ? cmp > 0 | |
3259 | : !strcmp(operator, "<=") ? cmp <= 0 | |
3260 | : !strcmp(operator, ">=") ? cmp >= 0 | |
3261 | : (abort(), 0)); | |
3262 | } | |
3263 | ||
e5e12280 BP |
3264 | static void |
3265 | pre_cmd_wait_until(struct vsctl_context *ctx) | |
3266 | { | |
3267 | const char *table_name = ctx->argv[1]; | |
3268 | const struct vsctl_table_class *table; | |
3269 | int i; | |
3270 | ||
3271 | table = pre_get_table(ctx, table_name); | |
3272 | ||
3273 | for (i = 3; i < ctx->argc; i++) { | |
3274 | pre_parse_column_key_value(ctx, ctx->argv[i], table); | |
3275 | } | |
3276 | } | |
3277 | ||
7db03f7c BP |
3278 | static void |
3279 | cmd_wait_until(struct vsctl_context *ctx) | |
3280 | { | |
3281 | const char *table_name = ctx->argv[1]; | |
3282 | const char *record_id = ctx->argv[2]; | |
3283 | const struct vsctl_table_class *table; | |
3284 | const struct ovsdb_idl_row *row; | |
3285 | int i; | |
3286 | ||
3287 | table = get_table(table_name); | |
3288 | ||
e111e681 | 3289 | row = get_row(ctx, table, record_id); |
7db03f7c BP |
3290 | if (!row) { |
3291 | ctx->try_again = true; | |
3292 | return; | |
3293 | } | |
3294 | ||
3295 | for (i = 3; i < ctx->argc; i++) { | |
f158c54a | 3296 | if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) { |
7db03f7c BP |
3297 | ctx->try_again = true; |
3298 | return; | |
3299 | } | |
3300 | } | |
3301 | } | |
ad83bfa6 | 3302 | \f |
b54e22e9 BP |
3303 | static struct json * |
3304 | where_uuid_equals(const struct uuid *uuid) | |
3305 | { | |
3306 | return | |
3307 | json_array_create_1( | |
3308 | json_array_create_3( | |
3309 | json_string_create("_uuid"), | |
3310 | json_string_create("=="), | |
3311 | json_array_create_2( | |
3312 | json_string_create("uuid"), | |
3313 | json_string_create_nocopy( | |
3314 | xasprintf(UUID_FMT, UUID_ARGS(uuid)))))); | |
3315 | } | |
3316 | ||
c75d1511 | 3317 | static void |
f8ff4bc4 BP |
3318 | vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command, |
3319 | struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn, | |
ce5a3e38 | 3320 | const struct ovsrec_open_vswitch *ovs, |
e5e12280 | 3321 | struct ovsdb_symbol_table *symtab) |
f8ff4bc4 BP |
3322 | { |
3323 | ctx->argc = command->argc; | |
3324 | ctx->argv = command->argv; | |
3325 | ctx->options = command->options; | |
3326 | ||
3327 | ds_swap(&ctx->output, &command->output); | |
e051b42c | 3328 | ctx->table = command->table; |
f8ff4bc4 BP |
3329 | ctx->idl = idl; |
3330 | ctx->txn = txn; | |
3331 | ctx->ovs = ovs; | |
ce5a3e38 | 3332 | ctx->symtab = symtab; |
f74055e7 | 3333 | ctx->verified_ports = false; |
87b23a01 BP |
3334 | |
3335 | ctx->try_again = false; | |
f8ff4bc4 BP |
3336 | } |
3337 | ||
3338 | static void | |
3339 | vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command) | |
3340 | { | |
3341 | ds_swap(&ctx->output, &command->output); | |
e051b42c | 3342 | command->table = ctx->table; |
f8ff4bc4 BP |
3343 | } |
3344 | ||
e5e12280 BP |
3345 | static void |
3346 | run_prerequisites(struct vsctl_command *commands, size_t n_commands, | |
3347 | struct ovsdb_idl *idl) | |
3348 | { | |
3349 | struct vsctl_command *c; | |
3350 | ||
3351 | ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch); | |
b8fa7102 BP |
3352 | if (wait_for_reload) { |
3353 | ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg); | |
3354 | } | |
e5e12280 BP |
3355 | for (c = commands; c < &commands[n_commands]; c++) { |
3356 | if (c->syntax->prerequisites) { | |
3357 | struct vsctl_context ctx; | |
3358 | ||
3359 | ds_init(&c->output); | |
e051b42c | 3360 | c->table = NULL; |
e5e12280 BP |
3361 | |
3362 | vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL); | |
3363 | (c->syntax->prerequisites)(&ctx); | |
3364 | vsctl_context_done(&ctx, c); | |
3365 | ||
3366 | assert(!c->output.string); | |
e051b42c | 3367 | assert(!c->table); |
e5e12280 BP |
3368 | } |
3369 | } | |
3370 | } | |
3371 | ||
f8ff4bc4 BP |
3372 | static void |
3373 | do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands, | |
3374 | struct ovsdb_idl *idl) | |
c75d1511 BP |
3375 | { |
3376 | struct ovsdb_idl_txn *txn; | |
3377 | const struct ovsrec_open_vswitch *ovs; | |
3378 | enum ovsdb_idl_txn_status status; | |
ce5a3e38 BP |
3379 | struct ovsdb_symbol_table *symtab; |
3380 | const char *unused; | |
f8ff4bc4 | 3381 | struct vsctl_command *c; |
84a0ee89 | 3382 | int64_t next_cfg = 0; |
af9af3e2 | 3383 | char *error = NULL; |
c75d1511 | 3384 | |
1d48b4be | 3385 | txn = the_idl_txn = ovsdb_idl_txn_create(idl); |
577aebdf BP |
3386 | if (dry_run) { |
3387 | ovsdb_idl_txn_set_dry_run(txn); | |
3388 | } | |
524555d1 | 3389 | |
e1c0e2d1 | 3390 | ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args); |
d171b584 | 3391 | |
c75d1511 BP |
3392 | ovs = ovsrec_open_vswitch_first(idl); |
3393 | if (!ovs) { | |
524555d1 BP |
3394 | /* XXX add verification that table is empty */ |
3395 | ovs = ovsrec_open_vswitch_insert(txn); | |
c75d1511 BP |
3396 | } |
3397 | ||
b54e22e9 BP |
3398 | if (wait_for_reload) { |
3399 | struct json *where = where_uuid_equals(&ovs->header_.uuid); | |
ad83bfa6 | 3400 | ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where); |
b54e22e9 BP |
3401 | json_destroy(where); |
3402 | } | |
3403 | ||
ce5a3e38 | 3404 | symtab = ovsdb_symbol_table_create(); |
87b23a01 BP |
3405 | for (c = commands; c < &commands[n_commands]; c++) { |
3406 | ds_init(&c->output); | |
e051b42c | 3407 | c->table = NULL; |
87b23a01 | 3408 | } |
f8ff4bc4 BP |
3409 | for (c = commands; c < &commands[n_commands]; c++) { |
3410 | struct vsctl_context ctx; | |
3411 | ||
ce5a3e38 | 3412 | vsctl_context_init(&ctx, c, idl, txn, ovs, symtab); |
f8ff4bc4 BP |
3413 | (c->syntax->run)(&ctx); |
3414 | vsctl_context_done(&ctx, c); | |
87b23a01 BP |
3415 | |
3416 | if (ctx.try_again) { | |
3417 | goto try_again; | |
3418 | } | |
c75d1511 | 3419 | } |
c75d1511 | 3420 | |
af96ccd2 | 3421 | status = ovsdb_idl_txn_commit_block(txn); |
b54e22e9 BP |
3422 | if (wait_for_reload && status == TXN_SUCCESS) { |
3423 | next_cfg = ovsdb_idl_txn_get_increment_new_value(txn); | |
3424 | } | |
8d49c47a BP |
3425 | if (status == TXN_UNCHANGED || status == TXN_SUCCESS) { |
3426 | for (c = commands; c < &commands[n_commands]; c++) { | |
3427 | if (c->syntax->postprocess) { | |
3428 | struct vsctl_context ctx; | |
3429 | ||
ce5a3e38 | 3430 | vsctl_context_init(&ctx, c, idl, txn, ovs, symtab); |
8d49c47a BP |
3431 | (c->syntax->postprocess)(&ctx); |
3432 | vsctl_context_done(&ctx, c); | |
3433 | } | |
3da1c516 BP |
3434 | } |
3435 | } | |
91e310a5 | 3436 | error = xstrdup(ovsdb_idl_txn_get_error(txn)); |
c75d1511 | 3437 | ovsdb_idl_txn_destroy(txn); |
b7b6e2c4 | 3438 | txn = the_idl_txn = NULL; |
c75d1511 | 3439 | |
ce5a3e38 BP |
3440 | unused = ovsdb_symbol_table_find_unused(symtab); |
3441 | if (unused) { | |
3442 | vsctl_fatal("row id \"%s\" is referenced but never created (e.g. " | |
3443 | "with \"-- --id=%s create ...\")", unused, unused); | |
3444 | } | |
ce5a3e38 | 3445 | |
c75d1511 BP |
3446 | switch (status) { |
3447 | case TXN_INCOMPLETE: | |
3448 | NOT_REACHED(); | |
3449 | ||
3450 | case TXN_ABORTED: | |
3451 | /* Should not happen--we never call ovsdb_idl_txn_abort(). */ | |
c88b6a27 | 3452 | vsctl_fatal("transaction aborted"); |
c75d1511 | 3453 | |
b54e22e9 | 3454 | case TXN_UNCHANGED: |
c75d1511 BP |
3455 | case TXN_SUCCESS: |
3456 | break; | |
3457 | ||
3458 | case TXN_TRY_AGAIN: | |
87b23a01 | 3459 | goto try_again; |
c75d1511 BP |
3460 | |
3461 | case TXN_ERROR: | |
91e310a5 | 3462 | vsctl_fatal("transaction error: %s", error); |
c75d1511 BP |
3463 | |
3464 | default: | |
3465 | NOT_REACHED(); | |
3466 | } | |
91e310a5 | 3467 | free(error); |
c75d1511 | 3468 | |
87b23a01 BP |
3469 | ovsdb_symbol_table_destroy(symtab); |
3470 | ||
f8ff4bc4 BP |
3471 | for (c = commands; c < &commands[n_commands]; c++) { |
3472 | struct ds *ds = &c->output; | |
ce5a3e38 | 3473 | |
e051b42c BP |
3474 | if (c->table) { |
3475 | table_print(c->table, &table_style); | |
3476 | } else if (oneline) { | |
c75d1511 BP |
3477 | size_t j; |
3478 | ||
3479 | ds_chomp(ds, '\n'); | |
3480 | for (j = 0; j < ds->length; j++) { | |
2a022368 BP |
3481 | int ch = ds->string[j]; |
3482 | switch (ch) { | |
c75d1511 BP |
3483 | case '\n': |
3484 | fputs("\\n", stdout); | |
3485 | break; | |
3486 | ||
3487 | case '\\': | |
3488 | fputs("\\\\", stdout); | |
3489 | break; | |
3490 | ||
3491 | default: | |
2a022368 | 3492 | putchar(ch); |
c75d1511 BP |
3493 | } |
3494 | } | |
3495 | putchar('\n'); | |
3496 | } else { | |
3497 | fputs(ds_cstr(ds), stdout); | |
3498 | } | |
b86b43aa | 3499 | ds_destroy(&c->output); |
e051b42c BP |
3500 | table_destroy(c->table); |
3501 | free(c->table); | |
ce5a3e38 | 3502 | |
c56d226f | 3503 | smap_destroy(&c->options); |
c75d1511 | 3504 | } |
b86b43aa | 3505 | free(commands); |
b54e22e9 BP |
3506 | |
3507 | if (wait_for_reload && status != TXN_UNCHANGED) { | |
3508 | for (;;) { | |
b54e22e9 BP |
3509 | ovsdb_idl_run(idl); |
3510 | OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) { | |
3511 | if (ovs->cur_cfg >= next_cfg) { | |
3512 | goto done; | |
3513 | } | |
3514 | } | |
3515 | ovsdb_idl_wait(idl); | |
3516 | poll_block(); | |
3517 | } | |
3518 | done: ; | |
3519 | } | |
b86b43aa | 3520 | ovsdb_idl_destroy(idl); |
b54e22e9 | 3521 | |
c75d1511 | 3522 | exit(EXIT_SUCCESS); |
87b23a01 BP |
3523 | |
3524 | try_again: | |
3525 | /* Our transaction needs to be rerun, or a prerequisite was not met. Free | |
3526 | * resources and return so that the caller can try again. */ | |
b7b6e2c4 JP |
3527 | if (txn) { |
3528 | ovsdb_idl_txn_abort(txn); | |
3529 | ovsdb_idl_txn_destroy(txn); | |
3530 | } | |
87b23a01 BP |
3531 | ovsdb_symbol_table_destroy(symtab); |
3532 | for (c = commands; c < &commands[n_commands]; c++) { | |
3533 | ds_destroy(&c->output); | |
e051b42c BP |
3534 | table_destroy(c->table); |
3535 | free(c->table); | |
87b23a01 BP |
3536 | } |
3537 | free(error); | |
c75d1511 BP |
3538 | } |
3539 | ||
f8ff4bc4 BP |
3540 | static const struct vsctl_command_syntax all_commands[] = { |
3541 | /* Open vSwitch commands. */ | |
e5e12280 | 3542 | {"init", 0, 0, NULL, cmd_init, NULL, "", RW}, |
f8ff4bc4 BP |
3543 | |
3544 | /* Bridge commands. */ | |
e5e12280 BP |
3545 | {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW}, |
3546 | {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW}, | |
3547 | {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO}, | |
3548 | {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO}, | |
3549 | {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO}, | |
3550 | {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO}, | |
fbd8715e BP |
3551 | {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id, |
3552 | cmd_br_set_external_id, NULL, "", RW}, | |
e5e12280 BP |
3553 | {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id, |
3554 | cmd_br_get_external_id, NULL, "", RO}, | |
f8ff4bc4 BP |
3555 | |
3556 | /* Port commands. */ | |
e5e12280 BP |
3557 | {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO}, |
3558 | {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist", | |
3559 | RW}, | |
3560 | {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL, | |
3561 | "--may-exist,--fake-iface", RW}, | |
3562 | {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL, | |
3563 | "--if-exists,--with-iface", RW}, | |
3564 | {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO}, | |
f8ff4bc4 BP |
3565 | |
3566 | /* Interface commands. */ | |
e5e12280 BP |
3567 | {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO}, |
3568 | {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO}, | |
f8ff4bc4 BP |
3569 | |
3570 | /* Controller commands. */ | |
4e3e7ff9 BP |
3571 | {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO}, |
3572 | {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW}, | |
3573 | {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL, | |
3574 | "", RW}, | |
e5e12280 BP |
3575 | {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO}, |
3576 | {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW}, | |
3577 | {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW}, | |
f8ff4bc4 | 3578 | |
24b8b259 AE |
3579 | /* Manager commands. */ |
3580 | {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO}, | |
3581 | {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW}, | |
3582 | {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW}, | |
3583 | ||
f8ff4bc4 | 3584 | /* SSL commands. */ |
e5e12280 BP |
3585 | {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO}, |
3586 | {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW}, | |
3587 | {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW}, | |
f8ff4bc4 | 3588 | |
18ee958b | 3589 | /* Switch commands. */ |
e5e12280 | 3590 | {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW}, |
18ee958b | 3591 | |
f8ff4bc4 | 3592 | /* Parameter commands. */ |
e5e12280 | 3593 | {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO}, |
9591fefe | 3594 | {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO}, |
0a140468 | 3595 | {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO}, |
e5e12280 BP |
3596 | {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW}, |
3597 | {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW}, | |
3598 | {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW}, | |
3599 | {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW}, | |
3600 | {"create", 2, INT_MAX, NULL, cmd_create, post_create, "--id=", RW}, | |
3601 | {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists", | |
3602 | RW}, | |
3603 | {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "", | |
3604 | RO}, | |
3605 | ||
3606 | {NULL, 0, 0, NULL, NULL, NULL, NULL, RO}, | |
f8ff4bc4 | 3607 | }; |
5d9cb63c | 3608 |