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