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