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