]>
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> | |
ad83bfa6 | 25 | #include <regex.h> |
c75d1511 BP |
26 | #include <signal.h> |
27 | #include <stdarg.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | ||
31 | #include "command-line.h" | |
32 | #include "compiler.h" | |
33 | #include "dirs.h" | |
34 | #include "dynamic-string.h" | |
b54e22e9 | 35 | #include "json.h" |
ad83bfa6 | 36 | #include "ovsdb-data.h" |
c75d1511 BP |
37 | #include "ovsdb-idl.h" |
38 | #include "poll-loop.h" | |
dfbe07ba | 39 | #include "svec.h" |
c75d1511 BP |
40 | #include "vswitchd/vswitch-idl.h" |
41 | #include "timeval.h" | |
42 | #include "util.h" | |
43 | ||
44 | #include "vlog.h" | |
45 | #define THIS_MODULE VLM_vsctl | |
46 | ||
47 | /* --db: The database server to contact. */ | |
48 | static const char *db; | |
49 | ||
50 | /* --oneline: Write each command's output as a single line? */ | |
51 | static bool oneline; | |
52 | ||
577aebdf BP |
53 | /* --dry-run: Do not commit any changes. */ |
54 | static bool dry_run; | |
55 | ||
b54e22e9 BP |
56 | /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */ |
57 | static bool wait_for_reload = true; | |
58 | ||
a39a859a JP |
59 | /* --timeout: Time to wait for a connection to 'db'. */ |
60 | static int timeout = 5; | |
61 | ||
c88b6a27 | 62 | static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN; |
c75d1511 BP |
63 | static char *default_db(void); |
64 | static void usage(void) NO_RETURN; | |
65 | static void parse_options(int argc, char *argv[]); | |
66 | ||
dfbe07ba | 67 | static void check_vsctl_command(int argc, char *argv[]); |
c75d1511 BP |
68 | static void do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl); |
69 | ||
70 | int | |
71 | main(int argc, char *argv[]) | |
72 | { | |
73 | struct ovsdb_idl *idl; | |
74 | unsigned int seqno; | |
dfbe07ba BP |
75 | struct ds args; |
76 | int start, n_commands; | |
c75d1511 | 77 | int trials; |
dfbe07ba | 78 | int i; |
c75d1511 BP |
79 | |
80 | set_program_name(argv[0]); | |
81 | signal(SIGPIPE, SIG_IGN); | |
82 | time_init(); | |
83 | vlog_init(); | |
dfbe07ba BP |
84 | vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN); |
85 | vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN); | |
c75d1511 BP |
86 | parse_options(argc, argv); |
87 | ||
a39a859a JP |
88 | if (timeout) { |
89 | time_alarm(timeout); | |
90 | } | |
91 | ||
dfbe07ba BP |
92 | /* Log our arguments. This is often valuable for debugging systems. */ |
93 | ds_init(&args); | |
94 | for (i = 1; i < argc; i++) { | |
95 | ds_put_format(&args, " %s", argv[i]); | |
96 | } | |
97 | VLOG_INFO("Called as%s", ds_cstr(&args)); | |
98 | ds_destroy(&args); | |
99 | ||
100 | /* Do basic command syntax checking. */ | |
101 | n_commands = 0; | |
102 | for (start = i = optind; i <= argc; i++) { | |
103 | if (i == argc || !strcmp(argv[i], "--")) { | |
104 | if (i > start) { | |
105 | check_vsctl_command(i - start, &argv[start]); | |
106 | n_commands++; | |
107 | } | |
108 | start = i + 1; | |
109 | } | |
110 | } | |
111 | if (!n_commands) { | |
c88b6a27 | 112 | vsctl_fatal("missing command name (use --help for help)"); |
dfbe07ba BP |
113 | } |
114 | ||
524555d1 | 115 | /* Now execute the commands. */ |
c75d1511 BP |
116 | idl = ovsdb_idl_create(db, &ovsrec_idl_class); |
117 | seqno = ovsdb_idl_get_seqno(idl); | |
118 | trials = 0; | |
119 | for (;;) { | |
120 | unsigned int new_seqno; | |
121 | ||
122 | ovsdb_idl_run(idl); | |
123 | new_seqno = ovsdb_idl_get_seqno(idl); | |
124 | if (new_seqno != seqno) { | |
125 | if (++trials > 5) { | |
c88b6a27 | 126 | vsctl_fatal("too many database inconsistency failures"); |
c75d1511 BP |
127 | } |
128 | do_vsctl(argc - optind, argv + optind, idl); | |
129 | seqno = new_seqno; | |
130 | } | |
131 | ||
132 | ovsdb_idl_wait(idl); | |
133 | poll_block(); | |
134 | } | |
135 | } | |
136 | ||
c88b6a27 BP |
137 | static void |
138 | vsctl_fatal(const char *format, ...) | |
139 | { | |
140 | char *message; | |
141 | va_list args; | |
142 | ||
143 | va_start(args, format); | |
144 | message = xvasprintf(format, args); | |
145 | va_end(args); | |
146 | ||
147 | vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER); | |
148 | VLOG_ERR("%s", message); | |
149 | ovs_fatal(0, "%s", message); | |
150 | } | |
151 | ||
c75d1511 BP |
152 | static void |
153 | parse_options(int argc, char *argv[]) | |
154 | { | |
155 | enum { | |
156 | OPT_DB = UCHAR_MAX + 1, | |
157 | OPT_ONELINE, | |
0c3dd1e1 | 158 | OPT_NO_SYSLOG, |
577aebdf | 159 | OPT_NO_WAIT, |
e26b5a06 JP |
160 | OPT_DRY_RUN, |
161 | VLOG_OPTION_ENUMS | |
c75d1511 BP |
162 | }; |
163 | static struct option long_options[] = { | |
164 | {"db", required_argument, 0, OPT_DB}, | |
dfbe07ba | 165 | {"no-syslog", no_argument, 0, OPT_NO_SYSLOG}, |
0c3dd1e1 | 166 | {"no-wait", no_argument, 0, OPT_NO_WAIT}, |
577aebdf | 167 | {"dry-run", no_argument, 0, OPT_DRY_RUN}, |
c75d1511 | 168 | {"oneline", no_argument, 0, OPT_ONELINE}, |
342045e1 | 169 | {"timeout", required_argument, 0, 't'}, |
c75d1511 BP |
170 | {"help", no_argument, 0, 'h'}, |
171 | {"version", no_argument, 0, 'V'}, | |
e26b5a06 | 172 | VLOG_LONG_OPTIONS, |
c75d1511 BP |
173 | {0, 0, 0, 0}, |
174 | }; | |
c75d1511 | 175 | |
342045e1 | 176 | |
c75d1511 BP |
177 | for (;;) { |
178 | int c; | |
179 | ||
342045e1 | 180 | c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL); |
c75d1511 BP |
181 | if (c == -1) { |
182 | break; | |
183 | } | |
184 | ||
185 | switch (c) { | |
186 | case OPT_DB: | |
187 | db = optarg; | |
188 | break; | |
189 | ||
190 | case OPT_ONELINE: | |
191 | oneline = true; | |
192 | break; | |
193 | ||
dfbe07ba BP |
194 | case OPT_NO_SYSLOG: |
195 | vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN); | |
196 | break; | |
197 | ||
0c3dd1e1 | 198 | case OPT_NO_WAIT: |
b54e22e9 | 199 | wait_for_reload = false; |
0c3dd1e1 BP |
200 | break; |
201 | ||
577aebdf BP |
202 | case OPT_DRY_RUN: |
203 | dry_run = true; | |
204 | break; | |
205 | ||
c75d1511 BP |
206 | case 'h': |
207 | usage(); | |
208 | ||
209 | case 'V': | |
210 | OVS_PRINT_VERSION(0, 0); | |
211 | exit(EXIT_SUCCESS); | |
212 | ||
342045e1 BP |
213 | case 't': |
214 | timeout = strtoul(optarg, NULL, 10); | |
a39a859a JP |
215 | if (timeout < 0) { |
216 | ovs_fatal(0, "value %s on -t or --timeout is invalid", | |
342045e1 | 217 | optarg); |
342045e1 BP |
218 | } |
219 | break; | |
220 | ||
e26b5a06 | 221 | VLOG_OPTION_HANDLERS |
c75d1511 BP |
222 | |
223 | case '?': | |
224 | exit(EXIT_FAILURE); | |
225 | ||
226 | default: | |
227 | abort(); | |
228 | } | |
229 | } | |
c75d1511 BP |
230 | |
231 | if (!db) { | |
232 | db = default_db(); | |
233 | } | |
234 | } | |
235 | ||
236 | static void | |
237 | usage(void) | |
238 | { | |
239 | printf("%s: ovs-vswitchd management utility\n" | |
240 | "usage: %s [OPTIONS] COMMAND [ARG...]\n", | |
241 | program_name, program_name); | |
242 | printf("\nBridge commands:\n" | |
243 | " add-br BRIDGE " | |
244 | "create a new bridge named BRIDGE\n" | |
245 | " add-br BRIDGE PARENT VLAN " | |
246 | "create new fake bridge BRIDGE in PARENT on VLAN\n" | |
247 | " del-br BRIDGE " | |
248 | "delete BRIDGE and all of its ports\n" | |
249 | " list-br " | |
250 | "print the names of all the bridges\n" | |
251 | " br-exists BRIDGE " | |
252 | "test whether BRIDGE exists\n" | |
253 | " br-to-vlan BRIDGE " | |
254 | "print the VLAN which BRIDGE is on\n" | |
255 | " br-to-parent BRIDGE " | |
68be4616 BP |
256 | "print the parent of BRIDGE\n" |
257 | " br-set-external-id BRIDGE KEY VALUE" | |
258 | " set KEY on BRIDGE to VALUE\n" | |
259 | " br-set-external-id BRIDGE KEY" | |
260 | " unset KEY on BRIDGE\n" | |
261 | " br-get-external-id BRIDGE KEY" | |
262 | " print value of KEY on BRIDGE\n" | |
263 | " br-get-external-id BRIDGE" | |
264 | " list key-value pairs on BRIDGE\n" | |
265 | ); | |
c75d1511 BP |
266 | printf("\nPort commands:\n" |
267 | " list-ports BRIDGE " | |
268 | "print the names of all the ports on BRIDGE\n" | |
269 | " add-port BRIDGE PORT " | |
270 | "add network device PORT to BRIDGE\n" | |
271 | " add-bond BRIDGE PORT IFACE... " | |
272 | "add new bonded port PORT in BRIDGE from IFACES\n" | |
273 | " del-port [BRIDGE] PORT " | |
274 | "delete PORT (which may be bonded) from BRIDGE\n" | |
275 | " port-to-br PORT " | |
276 | "print name of bridge that contains PORT\n" | |
68be4616 BP |
277 | " port-set-external-id PORT KEY VALUE" |
278 | " set KEY on PORT to VALUE\n" | |
279 | " port-set-external-id PORT KEY" | |
280 | " unset KEY on PORT\n" | |
281 | " port-get-external-id PORT KEY" | |
282 | " print value of KEY on PORT\n" | |
283 | " port-get-external-id PORT" | |
284 | " list key-value pairs on PORT\n" | |
285 | "A bond is considered to be a single port.\n" | |
286 | ); | |
c75d1511 BP |
287 | printf("\nInterface commands (a bond consists of multiple interfaces):\n" |
288 | " list-ifaces BRIDGE " | |
289 | "print the names of all the interfaces on BRIDGE\n" | |
290 | " iface-to-br IFACE " | |
68be4616 BP |
291 | "print name of bridge that contains IFACE\n" |
292 | " iface-set-external-id IFACE KEY VALUE" | |
293 | " set KEY on IFACE to VALUE\n" | |
294 | " iface-set-external-id IFACE KEY" | |
295 | " unset KEY on IFACE\n" | |
296 | " iface-get-external-id IFACE KEY" | |
297 | " print value of KEY on IFACE\n" | |
298 | " iface-get-external-id IFACE" | |
299 | " list key-value pairs on IFACE\n" | |
300 | ); | |
5aa00635 JP |
301 | printf("\nController commands:\n" |
302 | " get-controller [BRIDGE] " | |
303 | "print the controller for BRIDGE\n" | |
304 | " del-controller [BRIDGE] " | |
305 | "delete the controller for BRIDGE\n" | |
dd8ac6fe | 306 | " set-controller [BRIDGE] TARGET " |
5aa00635 JP |
307 | "set the controller for BRIDGE to TARGET\n" |
308 | " get-fail-mode [BRIDGE] " | |
309 | "print the fail-mode for BRIDGE\n" | |
310 | " del-fail-mode [BRIDGE] " | |
311 | "delete the fail-mode for BRIDGE\n" | |
dd8ac6fe | 312 | " set-fail-mode [BRIDGE] MODE " |
5aa00635 JP |
313 | "set the fail-mode for BRIDGE to MODE\n" |
314 | ); | |
dd8ac6fe JP |
315 | printf("\nSSL commands:\n" |
316 | " get-ssl " | |
317 | "print the SSL configuration\n" | |
318 | " del-ssl " | |
319 | "delete the SSL configuration\n" | |
320 | " set-ssl PRIV-KEY CERT CA-CERT " | |
321 | "set the SSL configuration\n" | |
322 | ); | |
c75d1511 BP |
323 | printf("\nOptions:\n" |
324 | " --db=DATABASE " | |
325 | "connect to DATABASE\n" | |
326 | " " | |
327 | "(default: %s)\n" | |
328 | " --oneline " | |
329 | "print exactly one line of output per command\n", | |
330 | default_db()); | |
331 | vlog_usage(); | |
332 | printf("\nOther options:\n" | |
333 | " -h, --help " | |
334 | "display this help message\n" | |
335 | " -V, --version " | |
336 | "display version information\n"); | |
337 | exit(EXIT_SUCCESS); | |
338 | } | |
339 | ||
340 | static char * | |
341 | default_db(void) | |
342 | { | |
343 | static char *def; | |
344 | if (!def) { | |
dfbe07ba | 345 | def = xasprintf("unix:%s/ovsdb-server", ovs_rundir); |
c75d1511 BP |
346 | } |
347 | return def; | |
348 | } | |
349 | \f | |
5d9cb63c BP |
350 | struct vsctl_context { |
351 | int argc; | |
352 | char **argv; | |
ad83bfa6 | 353 | struct ovsdb_idl *idl; |
5d9cb63c BP |
354 | const struct ovsrec_open_vswitch *ovs; |
355 | struct ds output; | |
356 | struct shash options; | |
357 | }; | |
358 | ||
c75d1511 BP |
359 | struct vsctl_bridge { |
360 | struct ovsrec_bridge *br_cfg; | |
361 | char *name; | |
5aa00635 | 362 | struct ovsrec_controller *ctrl; |
c75d1511 BP |
363 | struct vsctl_bridge *parent; |
364 | int vlan; | |
365 | }; | |
366 | ||
367 | struct vsctl_port { | |
368 | struct ovsrec_port *port_cfg; | |
369 | struct vsctl_bridge *bridge; | |
370 | }; | |
371 | ||
372 | struct vsctl_iface { | |
373 | struct ovsrec_interface *iface_cfg; | |
374 | struct vsctl_port *port; | |
375 | }; | |
376 | ||
377 | struct vsctl_info { | |
378 | struct shash bridges; | |
379 | struct shash ports; | |
380 | struct shash ifaces; | |
5aa00635 | 381 | struct ovsrec_controller *ctrl; |
c75d1511 BP |
382 | }; |
383 | ||
384 | static struct ovsdb_idl_txn * | |
385 | txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs) | |
386 | { | |
387 | return ovsdb_idl_txn_get(&ovs->header_); | |
388 | } | |
389 | ||
390 | static struct vsctl_bridge * | |
391 | add_bridge(struct vsctl_info *b, | |
392 | struct ovsrec_bridge *br_cfg, const char *name, | |
393 | struct vsctl_bridge *parent, int vlan) | |
394 | { | |
395 | struct vsctl_bridge *br = xmalloc(sizeof *br); | |
396 | br->br_cfg = br_cfg; | |
397 | br->name = xstrdup(name); | |
398 | br->parent = parent; | |
399 | br->vlan = vlan; | |
175106cb | 400 | br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller; |
c75d1511 BP |
401 | shash_add(&b->bridges, br->name, br); |
402 | return br; | |
403 | } | |
404 | ||
405 | static bool | |
406 | port_is_fake_bridge(const struct ovsrec_port *port_cfg) | |
407 | { | |
408 | return (port_cfg->fake_bridge | |
409 | && port_cfg->tag | |
410 | && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095); | |
411 | } | |
412 | ||
413 | static struct vsctl_bridge * | |
414 | find_vlan_bridge(struct vsctl_info *info, | |
415 | struct vsctl_bridge *parent, int vlan) | |
416 | { | |
417 | struct shash_node *node; | |
418 | ||
419 | SHASH_FOR_EACH (node, &info->bridges) { | |
420 | struct vsctl_bridge *br = node->data; | |
421 | if (br->parent == parent && br->vlan == vlan) { | |
422 | return br; | |
423 | } | |
424 | } | |
425 | ||
426 | return NULL; | |
427 | } | |
428 | ||
429 | static void | |
430 | free_info(struct vsctl_info *info) | |
431 | { | |
432 | struct shash_node *node; | |
433 | ||
434 | SHASH_FOR_EACH (node, &info->bridges) { | |
435 | struct vsctl_bridge *bridge = node->data; | |
436 | free(bridge->name); | |
437 | free(bridge); | |
438 | } | |
439 | shash_destroy(&info->bridges); | |
440 | ||
441 | SHASH_FOR_EACH (node, &info->ports) { | |
442 | struct vsctl_port *port = node->data; | |
443 | free(port); | |
444 | } | |
445 | shash_destroy(&info->ports); | |
446 | ||
447 | SHASH_FOR_EACH (node, &info->ifaces) { | |
448 | struct vsctl_iface *iface = node->data; | |
449 | free(iface); | |
450 | } | |
451 | shash_destroy(&info->ifaces); | |
452 | } | |
453 | ||
454 | static void | |
455 | get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info) | |
456 | { | |
457 | struct shash bridges, ports; | |
458 | size_t i; | |
459 | ||
460 | shash_init(&info->bridges); | |
461 | shash_init(&info->ports); | |
462 | shash_init(&info->ifaces); | |
463 | ||
5aa00635 JP |
464 | info->ctrl = ovs->controller; |
465 | ||
c75d1511 BP |
466 | shash_init(&bridges); |
467 | shash_init(&ports); | |
468 | for (i = 0; i < ovs->n_bridges; i++) { | |
469 | struct ovsrec_bridge *br_cfg = ovs->bridges[i]; | |
470 | struct vsctl_bridge *br; | |
471 | size_t j; | |
472 | ||
473 | if (!shash_add_once(&bridges, br_cfg->name, NULL)) { | |
474 | VLOG_WARN("%s: database contains duplicate bridge name", | |
475 | br_cfg->name); | |
476 | continue; | |
477 | } | |
478 | br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0); | |
479 | if (!br) { | |
480 | continue; | |
481 | } | |
482 | ||
483 | for (j = 0; j < br_cfg->n_ports; j++) { | |
484 | struct ovsrec_port *port_cfg = br_cfg->ports[j]; | |
485 | ||
486 | if (!shash_add_once(&ports, port_cfg->name, NULL)) { | |
487 | VLOG_WARN("%s: database contains duplicate port name", | |
488 | port_cfg->name); | |
489 | continue; | |
490 | } | |
491 | ||
492 | if (port_is_fake_bridge(port_cfg) | |
dfbe07ba | 493 | && shash_add_once(&bridges, port_cfg->name, NULL)) { |
c75d1511 BP |
494 | add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag); |
495 | } | |
496 | } | |
497 | } | |
498 | shash_destroy(&bridges); | |
499 | shash_destroy(&ports); | |
500 | ||
501 | shash_init(&bridges); | |
502 | shash_init(&ports); | |
503 | for (i = 0; i < ovs->n_bridges; i++) { | |
504 | struct ovsrec_bridge *br_cfg = ovs->bridges[i]; | |
505 | struct vsctl_bridge *br; | |
506 | size_t j; | |
507 | ||
508 | if (!shash_add_once(&bridges, br_cfg->name, NULL)) { | |
509 | continue; | |
510 | } | |
511 | br = shash_find_data(&info->bridges, br_cfg->name); | |
512 | for (j = 0; j < br_cfg->n_ports; j++) { | |
513 | struct ovsrec_port *port_cfg = br_cfg->ports[j]; | |
514 | struct vsctl_port *port; | |
515 | size_t k; | |
516 | ||
517 | if (!shash_add_once(&ports, port_cfg->name, NULL)) { | |
518 | continue; | |
519 | } | |
520 | ||
521 | if (port_is_fake_bridge(port_cfg) | |
dfbe07ba | 522 | && !shash_add_once(&bridges, port_cfg->name, NULL)) { |
c75d1511 BP |
523 | continue; |
524 | } | |
525 | ||
526 | port = xmalloc(sizeof *port); | |
527 | port->port_cfg = port_cfg; | |
528 | if (port_cfg->tag | |
529 | && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) { | |
530 | port->bridge = find_vlan_bridge(info, br, *port_cfg->tag); | |
531 | if (!port->bridge) { | |
532 | port->bridge = br; | |
533 | } | |
534 | } else { | |
535 | port->bridge = br; | |
536 | } | |
537 | shash_add(&info->ports, port_cfg->name, port); | |
538 | ||
539 | for (k = 0; k < port_cfg->n_interfaces; k++) { | |
540 | struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k]; | |
541 | struct vsctl_iface *iface; | |
542 | ||
543 | if (shash_find(&info->ifaces, iface_cfg->name)) { | |
544 | VLOG_WARN("%s: database contains duplicate interface name", | |
545 | iface_cfg->name); | |
546 | continue; | |
547 | } | |
548 | ||
549 | iface = xmalloc(sizeof *iface); | |
550 | iface->iface_cfg = iface_cfg; | |
551 | iface->port = port; | |
dfbe07ba | 552 | shash_add(&info->ifaces, iface_cfg->name, iface); |
c75d1511 BP |
553 | } |
554 | } | |
555 | } | |
556 | shash_destroy(&bridges); | |
557 | shash_destroy(&ports); | |
558 | } | |
559 | ||
560 | static void | |
561 | check_conflicts(struct vsctl_info *info, const char *name, | |
562 | char *msg) | |
563 | { | |
564 | struct vsctl_iface *iface; | |
565 | struct vsctl_port *port; | |
566 | ||
567 | if (shash_find(&info->bridges, name)) { | |
c88b6a27 BP |
568 | vsctl_fatal("%s because a bridge named %s already exists", |
569 | msg, name); | |
c75d1511 BP |
570 | } |
571 | ||
572 | port = shash_find_data(&info->ports, name); | |
573 | if (port) { | |
c88b6a27 BP |
574 | vsctl_fatal("%s because a port named %s already exists on " |
575 | "bridge %s", msg, name, port->bridge->name); | |
c75d1511 BP |
576 | } |
577 | ||
578 | iface = shash_find_data(&info->ifaces, name); | |
579 | if (iface) { | |
c88b6a27 BP |
580 | vsctl_fatal("%s because an interface named %s already exists " |
581 | "on bridge %s", msg, name, iface->port->bridge->name); | |
c75d1511 BP |
582 | } |
583 | ||
584 | free(msg); | |
585 | } | |
586 | ||
587 | static struct vsctl_bridge * | |
01845ce8 | 588 | find_bridge(struct vsctl_info *info, const char *name, bool must_exist) |
c75d1511 BP |
589 | { |
590 | struct vsctl_bridge *br = shash_find_data(&info->bridges, name); | |
01845ce8 | 591 | if (must_exist && !br) { |
c88b6a27 | 592 | vsctl_fatal("no bridge named %s", name); |
c75d1511 BP |
593 | } |
594 | return br; | |
595 | } | |
596 | ||
975ac531 JP |
597 | static struct vsctl_bridge * |
598 | find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist) | |
599 | { | |
600 | struct vsctl_bridge *br = find_bridge(info, name, must_exist); | |
601 | if (br && br->parent) { | |
602 | vsctl_fatal("%s is a fake bridge", name); | |
603 | } | |
604 | return br; | |
605 | } | |
606 | ||
c75d1511 | 607 | static struct vsctl_port * |
01845ce8 | 608 | find_port(struct vsctl_info *info, const char *name, bool must_exist) |
c75d1511 BP |
609 | { |
610 | struct vsctl_port *port = shash_find_data(&info->ports, name); | |
460aad80 | 611 | if (port && !strcmp(name, port->bridge->name)) { |
01845ce8 BP |
612 | port = NULL; |
613 | } | |
614 | if (must_exist && !port) { | |
c88b6a27 | 615 | vsctl_fatal("no port named %s", name); |
c75d1511 BP |
616 | } |
617 | return port; | |
618 | } | |
619 | ||
620 | static struct vsctl_iface * | |
01845ce8 | 621 | find_iface(struct vsctl_info *info, const char *name, bool must_exist) |
c75d1511 BP |
622 | { |
623 | struct vsctl_iface *iface = shash_find_data(&info->ifaces, name); | |
460aad80 | 624 | if (iface && !strcmp(name, iface->port->bridge->name)) { |
01845ce8 BP |
625 | iface = NULL; |
626 | } | |
627 | if (must_exist && !iface) { | |
c88b6a27 | 628 | vsctl_fatal("no interface named %s", name); |
c75d1511 BP |
629 | } |
630 | return iface; | |
631 | } | |
632 | ||
633 | static void | |
634 | bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port) | |
635 | { | |
636 | struct ovsrec_port **ports; | |
637 | size_t i; | |
638 | ||
639 | ports = xmalloc(sizeof *br->ports * (br->n_ports + 1)); | |
640 | for (i = 0; i < br->n_ports; i++) { | |
641 | ports[i] = br->ports[i]; | |
642 | } | |
c75d1511 BP |
643 | ports[br->n_ports] = port; |
644 | ovsrec_bridge_set_ports(br, ports, br->n_ports + 1); | |
645 | free(ports); | |
646 | } | |
647 | ||
648 | static void | |
649 | bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port) | |
650 | { | |
651 | struct ovsrec_port **ports; | |
652 | size_t i, n; | |
653 | ||
654 | ports = xmalloc(sizeof *br->ports * br->n_ports); | |
655 | for (i = n = 0; i < br->n_ports; i++) { | |
656 | if (br->ports[i] != port) { | |
657 | ports[n++] = br->ports[i]; | |
658 | } | |
659 | } | |
660 | ovsrec_bridge_set_ports(br, ports, n); | |
661 | free(ports); | |
662 | } | |
663 | ||
664 | static void | |
665 | ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs, | |
666 | struct ovsrec_bridge *bridge) | |
667 | { | |
668 | struct ovsrec_bridge **bridges; | |
669 | size_t i; | |
670 | ||
671 | bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1)); | |
672 | for (i = 0; i < ovs->n_bridges; i++) { | |
673 | bridges[i] = ovs->bridges[i]; | |
674 | } | |
675 | bridges[ovs->n_bridges] = bridge; | |
676 | ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1); | |
677 | free(bridges); | |
678 | } | |
679 | ||
680 | static void | |
681 | ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs, | |
682 | struct ovsrec_bridge *bridge) | |
683 | { | |
684 | struct ovsrec_bridge **bridges; | |
685 | size_t i, n; | |
686 | ||
687 | bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges); | |
688 | for (i = n = 0; i < ovs->n_bridges; i++) { | |
689 | if (ovs->bridges[i] != bridge) { | |
690 | bridges[n++] = ovs->bridges[i]; | |
691 | } | |
692 | } | |
693 | ovsrec_open_vswitch_set_bridges(ovs, bridges, n); | |
694 | free(bridges); | |
695 | } | |
696 | ||
524555d1 BP |
697 | static void |
698 | cmd_init(struct vsctl_context *ctx UNUSED) | |
699 | { | |
700 | } | |
701 | ||
c75d1511 | 702 | static void |
5d9cb63c | 703 | cmd_add_br(struct vsctl_context *ctx) |
c75d1511 | 704 | { |
5d9cb63c | 705 | const char *br_name = ctx->argv[1]; |
c75d1511 BP |
706 | struct vsctl_info info; |
707 | ||
5d9cb63c | 708 | get_info(ctx->ovs, &info); |
c75d1511 BP |
709 | check_conflicts(&info, br_name, |
710 | xasprintf("cannot create a bridge named %s", br_name)); | |
711 | ||
5d9cb63c | 712 | if (ctx->argc == 2) { |
c75d1511 BP |
713 | struct ovsrec_bridge *br; |
714 | struct ovsrec_port *port; | |
715 | struct ovsrec_interface *iface; | |
716 | ||
5d9cb63c | 717 | iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs)); |
c75d1511 BP |
718 | ovsrec_interface_set_name(iface, br_name); |
719 | ||
5d9cb63c | 720 | port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs)); |
c75d1511 BP |
721 | ovsrec_port_set_name(port, br_name); |
722 | ovsrec_port_set_interfaces(port, &iface, 1); | |
723 | ||
5d9cb63c | 724 | br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs)); |
c75d1511 BP |
725 | ovsrec_bridge_set_name(br, br_name); |
726 | ovsrec_bridge_set_ports(br, &port, 1); | |
727 | ||
5d9cb63c BP |
728 | ovs_insert_bridge(ctx->ovs, br); |
729 | } else if (ctx->argc == 3) { | |
c88b6a27 BP |
730 | vsctl_fatal("'%s' command takes exactly 1 or 3 arguments", |
731 | ctx->argv[0]); | |
5d9cb63c BP |
732 | } else if (ctx->argc == 4) { |
733 | const char *parent_name = ctx->argv[2]; | |
734 | int vlan = atoi(ctx->argv[3]); | |
c75d1511 BP |
735 | struct ovsrec_bridge *br; |
736 | struct vsctl_bridge *parent; | |
737 | struct ovsrec_port *port; | |
738 | struct ovsrec_interface *iface; | |
739 | int64_t tag = vlan; | |
740 | ||
741 | if (vlan < 1 || vlan > 4095) { | |
c88b6a27 | 742 | vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]); |
c75d1511 BP |
743 | } |
744 | ||
01845ce8 | 745 | parent = find_bridge(&info, parent_name, false); |
c75d1511 | 746 | if (parent && parent->vlan) { |
11aa5627 | 747 | vsctl_fatal("cannot create bridge with fake bridge as parent"); |
c75d1511 BP |
748 | } |
749 | if (!parent) { | |
c88b6a27 | 750 | vsctl_fatal("parent bridge %s does not exist", parent_name); |
c75d1511 BP |
751 | } |
752 | br = parent->br_cfg; | |
753 | ||
5d9cb63c | 754 | iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs)); |
c75d1511 BP |
755 | ovsrec_interface_set_name(iface, br_name); |
756 | ovsrec_interface_set_type(iface, "internal"); | |
757 | ||
5d9cb63c | 758 | port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs)); |
c75d1511 BP |
759 | ovsrec_port_set_name(port, br_name); |
760 | ovsrec_port_set_interfaces(port, &iface, 1); | |
761 | ovsrec_port_set_fake_bridge(port, true); | |
762 | ovsrec_port_set_tag(port, &tag, 1); | |
dfbe07ba BP |
763 | |
764 | bridge_insert_port(br, port); | |
c75d1511 BP |
765 | } else { |
766 | NOT_REACHED(); | |
767 | } | |
768 | ||
769 | free_info(&info); | |
770 | } | |
771 | ||
772 | static void | |
773 | del_port(struct vsctl_info *info, struct vsctl_port *port) | |
774 | { | |
775 | struct shash_node *node; | |
776 | ||
777 | SHASH_FOR_EACH (node, &info->ifaces) { | |
778 | struct vsctl_iface *iface = node->data; | |
779 | if (iface->port == port) { | |
780 | ovsrec_interface_delete(iface->iface_cfg); | |
781 | } | |
782 | } | |
783 | ovsrec_port_delete(port->port_cfg); | |
784 | ||
785 | bridge_delete_port((port->bridge->parent | |
786 | ? port->bridge->parent->br_cfg | |
787 | : port->bridge->br_cfg), port->port_cfg); | |
788 | } | |
789 | ||
790 | static void | |
5d9cb63c | 791 | cmd_del_br(struct vsctl_context *ctx) |
c75d1511 | 792 | { |
460aad80 | 793 | bool must_exist = !shash_find(&ctx->options, "--if-exists"); |
c75d1511 | 794 | struct vsctl_bridge *bridge; |
460aad80 | 795 | struct vsctl_info info; |
c75d1511 | 796 | |
5d9cb63c | 797 | get_info(ctx->ovs, &info); |
460aad80 BP |
798 | bridge = find_bridge(&info, ctx->argv[1], must_exist); |
799 | if (bridge) { | |
800 | struct shash_node *node; | |
801 | ||
802 | SHASH_FOR_EACH (node, &info.ports) { | |
803 | struct vsctl_port *port = node->data; | |
804 | if (port->bridge == bridge | |
805 | || !strcmp(port->port_cfg->name, bridge->name)) { | |
806 | del_port(&info, port); | |
807 | } | |
808 | } | |
809 | if (bridge->br_cfg) { | |
810 | ovsrec_bridge_delete(bridge->br_cfg); | |
811 | ovs_delete_bridge(ctx->ovs, bridge->br_cfg); | |
c75d1511 | 812 | } |
c75d1511 BP |
813 | } |
814 | free_info(&info); | |
815 | } | |
816 | ||
dfbe07ba BP |
817 | static void |
818 | output_sorted(struct svec *svec, struct ds *output) | |
819 | { | |
820 | const char *name; | |
821 | size_t i; | |
822 | ||
823 | svec_sort(svec); | |
824 | SVEC_FOR_EACH (i, name, svec) { | |
825 | ds_put_format(output, "%s\n", name); | |
826 | } | |
827 | } | |
828 | ||
c75d1511 | 829 | static void |
5d9cb63c | 830 | cmd_list_br(struct vsctl_context *ctx) |
c75d1511 BP |
831 | { |
832 | struct shash_node *node; | |
833 | struct vsctl_info info; | |
dfbe07ba | 834 | struct svec bridges; |
c75d1511 | 835 | |
5d9cb63c | 836 | get_info(ctx->ovs, &info); |
dfbe07ba BP |
837 | |
838 | svec_init(&bridges); | |
c75d1511 BP |
839 | SHASH_FOR_EACH (node, &info.bridges) { |
840 | struct vsctl_bridge *br = node->data; | |
dfbe07ba | 841 | svec_add(&bridges, br->name); |
c75d1511 | 842 | } |
5d9cb63c | 843 | output_sorted(&bridges, &ctx->output); |
dfbe07ba BP |
844 | svec_destroy(&bridges); |
845 | ||
c75d1511 BP |
846 | free_info(&info); |
847 | } | |
848 | ||
849 | static void | |
5d9cb63c | 850 | cmd_br_exists(struct vsctl_context *ctx) |
c75d1511 BP |
851 | { |
852 | struct vsctl_info info; | |
853 | ||
5d9cb63c | 854 | get_info(ctx->ovs, &info); |
01845ce8 | 855 | if (!find_bridge(&info, ctx->argv[1], false)) { |
c75d1511 BP |
856 | exit(2); |
857 | } | |
858 | free_info(&info); | |
859 | } | |
860 | ||
457e1eb0 BP |
861 | /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b' |
862 | * equals 'a', false otherwise. */ | |
863 | static bool | |
864 | key_matches(const char *a, | |
865 | const char *b_prefix, size_t b_prefix_len, const char *b) | |
866 | { | |
867 | return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b); | |
868 | } | |
869 | ||
870 | static void | |
871 | set_external_id(char **old_keys, char **old_values, size_t old_n, | |
872 | char *key, char *value, | |
873 | char ***new_keysp, char ***new_valuesp, size_t *new_np) | |
874 | { | |
875 | char **new_keys; | |
876 | char **new_values; | |
877 | size_t new_n; | |
878 | size_t i; | |
879 | ||
880 | new_keys = xmalloc(sizeof *new_keys * (old_n + 1)); | |
881 | new_values = xmalloc(sizeof *new_values * (old_n + 1)); | |
882 | new_n = 0; | |
883 | for (i = 0; i < old_n; i++) { | |
884 | if (strcmp(key, old_keys[i])) { | |
885 | new_keys[new_n] = old_keys[i]; | |
886 | new_values[new_n] = old_values[i]; | |
887 | new_n++; | |
888 | } | |
889 | } | |
890 | if (value) { | |
891 | new_keys[new_n] = key; | |
892 | new_values[new_n] = value; | |
893 | new_n++; | |
894 | } | |
895 | *new_keysp = new_keys; | |
896 | *new_valuesp = new_values; | |
897 | *new_np = new_n; | |
898 | } | |
899 | ||
900 | static void | |
5d9cb63c | 901 | cmd_br_set_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
902 | { |
903 | struct vsctl_info info; | |
904 | struct vsctl_bridge *bridge; | |
905 | char **keys, **values; | |
906 | size_t n; | |
907 | ||
5d9cb63c | 908 | get_info(ctx->ovs, &info); |
01845ce8 | 909 | bridge = find_bridge(&info, ctx->argv[1], true); |
457e1eb0 BP |
910 | if (bridge->br_cfg) { |
911 | set_external_id(bridge->br_cfg->key_external_ids, | |
912 | bridge->br_cfg->value_external_ids, | |
913 | bridge->br_cfg->n_external_ids, | |
5d9cb63c | 914 | ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL, |
457e1eb0 BP |
915 | &keys, &values, &n); |
916 | ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n); | |
917 | } else { | |
5d9cb63c BP |
918 | char *key = xasprintf("fake-bridge-%s", ctx->argv[2]); |
919 | struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]); | |
457e1eb0 BP |
920 | set_external_id(port->port_cfg->key_external_ids, |
921 | port->port_cfg->value_external_ids, | |
922 | port->port_cfg->n_external_ids, | |
5d9cb63c | 923 | key, ctx->argc >= 4 ? ctx->argv[3] : NULL, |
457e1eb0 BP |
924 | &keys, &values, &n); |
925 | ovsrec_port_set_external_ids(port->port_cfg, keys, values, n); | |
926 | free(key); | |
927 | } | |
928 | free(keys); | |
929 | free(values); | |
930 | ||
931 | free_info(&info); | |
932 | } | |
933 | ||
934 | static void | |
935 | get_external_id(char **keys, char **values, size_t n, | |
936 | const char *prefix, const char *key, | |
937 | struct ds *output) | |
938 | { | |
939 | size_t prefix_len = strlen(prefix); | |
940 | struct svec svec; | |
941 | size_t i; | |
942 | ||
943 | svec_init(&svec); | |
944 | for (i = 0; i < n; i++) { | |
945 | if (!key && !strncmp(keys[i], prefix, prefix_len)) { | |
946 | svec_add_nocopy(&svec, xasprintf("%s=%s", | |
947 | keys[i] + prefix_len, values[i])); | |
948 | } else if (key_matches(keys[i], prefix, prefix_len, key)) { | |
949 | svec_add(&svec, values[i]); | |
950 | break; | |
951 | } | |
952 | } | |
953 | output_sorted(&svec, output); | |
954 | svec_destroy(&svec); | |
955 | } | |
956 | ||
957 | static void | |
5d9cb63c | 958 | cmd_br_get_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
959 | { |
960 | struct vsctl_info info; | |
961 | struct vsctl_bridge *bridge; | |
962 | ||
5d9cb63c | 963 | get_info(ctx->ovs, &info); |
01845ce8 | 964 | bridge = find_bridge(&info, ctx->argv[1], true); |
457e1eb0 BP |
965 | if (bridge->br_cfg) { |
966 | get_external_id(bridge->br_cfg->key_external_ids, | |
967 | bridge->br_cfg->value_external_ids, | |
968 | bridge->br_cfg->n_external_ids, | |
5d9cb63c BP |
969 | "", ctx->argc >= 3 ? ctx->argv[2] : NULL, |
970 | &ctx->output); | |
457e1eb0 | 971 | } else { |
5d9cb63c | 972 | struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]); |
457e1eb0 BP |
973 | get_external_id(port->port_cfg->key_external_ids, |
974 | port->port_cfg->value_external_ids, | |
975 | port->port_cfg->n_external_ids, | |
5d9cb63c | 976 | "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output); |
457e1eb0 BP |
977 | } |
978 | free_info(&info); | |
979 | } | |
980 | ||
981 | ||
c75d1511 | 982 | static void |
5d9cb63c | 983 | cmd_list_ports(struct vsctl_context *ctx) |
c75d1511 BP |
984 | { |
985 | struct vsctl_bridge *br; | |
986 | struct shash_node *node; | |
987 | struct vsctl_info info; | |
dfbe07ba | 988 | struct svec ports; |
c75d1511 | 989 | |
5d9cb63c | 990 | get_info(ctx->ovs, &info); |
01845ce8 | 991 | br = find_bridge(&info, ctx->argv[1], true); |
dfbe07ba BP |
992 | |
993 | svec_init(&ports); | |
c75d1511 BP |
994 | SHASH_FOR_EACH (node, &info.ports) { |
995 | struct vsctl_port *port = node->data; | |
996 | ||
997 | if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) { | |
dfbe07ba | 998 | svec_add(&ports, port->port_cfg->name); |
c75d1511 BP |
999 | } |
1000 | } | |
5d9cb63c | 1001 | output_sorted(&ports, &ctx->output); |
dfbe07ba BP |
1002 | svec_destroy(&ports); |
1003 | ||
c75d1511 BP |
1004 | free_info(&info); |
1005 | } | |
1006 | ||
1007 | static void | |
1008 | add_port(const struct ovsrec_open_vswitch *ovs, | |
b4182c7f | 1009 | const char *br_name, const char *port_name, bool fake_iface, |
c75d1511 BP |
1010 | char *iface_names[], int n_ifaces) |
1011 | { | |
1012 | struct vsctl_info info; | |
1013 | struct vsctl_bridge *bridge; | |
1014 | struct ovsrec_interface **ifaces; | |
1015 | struct ovsrec_port *port; | |
1016 | size_t i; | |
1017 | ||
1018 | get_info(ovs, &info); | |
1019 | check_conflicts(&info, port_name, | |
1020 | xasprintf("cannot create a port named %s", port_name)); | |
1021 | /* XXX need to check for conflicts on interfaces too */ | |
01845ce8 | 1022 | bridge = find_bridge(&info, br_name, true); |
c75d1511 BP |
1023 | |
1024 | ifaces = xmalloc(n_ifaces * sizeof *ifaces); | |
1025 | for (i = 0; i < n_ifaces; i++) { | |
1026 | ifaces[i] = ovsrec_interface_insert(txn_from_openvswitch(ovs)); | |
1027 | ovsrec_interface_set_name(ifaces[i], iface_names[i]); | |
1028 | } | |
1029 | ||
1030 | port = ovsrec_port_insert(txn_from_openvswitch(ovs)); | |
1031 | ovsrec_port_set_name(port, port_name); | |
1032 | ovsrec_port_set_interfaces(port, ifaces, n_ifaces); | |
b4182c7f | 1033 | ovsrec_port_set_bond_fake_iface(port, fake_iface); |
a0a9f31d JP |
1034 | free(ifaces); |
1035 | ||
c75d1511 BP |
1036 | if (bridge->vlan) { |
1037 | int64_t tag = bridge->vlan; | |
1038 | ovsrec_port_set_tag(port, &tag, 1); | |
1039 | } | |
1040 | ||
1041 | bridge_insert_port((bridge->parent ? bridge->parent->br_cfg | |
1042 | : bridge->br_cfg), port); | |
1043 | ||
1044 | free_info(&info); | |
1045 | } | |
1046 | ||
1047 | static void | |
5d9cb63c | 1048 | cmd_add_port(struct vsctl_context *ctx) |
c75d1511 | 1049 | { |
b4182c7f | 1050 | add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], false, &ctx->argv[2], 1); |
c75d1511 BP |
1051 | } |
1052 | ||
1053 | static void | |
5d9cb63c | 1054 | cmd_add_bond(struct vsctl_context *ctx) |
c75d1511 | 1055 | { |
b4182c7f JP |
1056 | bool fake_iface = shash_find(&ctx->options, "--fake-iface"); |
1057 | ||
1058 | add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], fake_iface, | |
1059 | &ctx->argv[3], ctx->argc - 3); | |
c75d1511 BP |
1060 | } |
1061 | ||
1062 | static void | |
5d9cb63c | 1063 | cmd_del_port(struct vsctl_context *ctx) |
c75d1511 | 1064 | { |
460aad80 | 1065 | bool must_exist = !shash_find(&ctx->options, "--if-exists"); |
c75d1511 BP |
1066 | struct vsctl_info info; |
1067 | ||
5d9cb63c BP |
1068 | get_info(ctx->ovs, &info); |
1069 | if (ctx->argc == 2) { | |
460aad80 BP |
1070 | struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist); |
1071 | if (port) { | |
1072 | del_port(&info, port); | |
1073 | } | |
5d9cb63c | 1074 | } else if (ctx->argc == 3) { |
01845ce8 | 1075 | struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true); |
460aad80 BP |
1076 | struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist); |
1077 | ||
1078 | if (port) { | |
1079 | if (port->bridge == bridge) { | |
1080 | del_port(&info, port); | |
1081 | } else if (port->bridge->parent == bridge) { | |
c88b6a27 BP |
1082 | vsctl_fatal("bridge %s does not have a port %s (although its " |
1083 | "parent bridge %s does)", | |
1084 | ctx->argv[1], ctx->argv[2], bridge->parent->name); | |
460aad80 | 1085 | } else { |
c88b6a27 BP |
1086 | vsctl_fatal("bridge %s does not have a port %s", |
1087 | ctx->argv[1], ctx->argv[2]); | |
460aad80 | 1088 | } |
c75d1511 BP |
1089 | } |
1090 | } | |
1091 | free_info(&info); | |
1092 | } | |
1093 | ||
1094 | static void | |
5d9cb63c | 1095 | cmd_port_to_br(struct vsctl_context *ctx) |
c75d1511 BP |
1096 | { |
1097 | struct vsctl_port *port; | |
1098 | struct vsctl_info info; | |
1099 | ||
5d9cb63c | 1100 | get_info(ctx->ovs, &info); |
01845ce8 | 1101 | port = find_port(&info, ctx->argv[1], true); |
5d9cb63c | 1102 | ds_put_format(&ctx->output, "%s\n", port->bridge->name); |
c75d1511 BP |
1103 | free_info(&info); |
1104 | } | |
1105 | ||
457e1eb0 | 1106 | static void |
5d9cb63c | 1107 | cmd_port_set_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
1108 | { |
1109 | struct vsctl_info info; | |
1110 | struct vsctl_port *port; | |
1111 | char **keys, **values; | |
1112 | size_t n; | |
1113 | ||
5d9cb63c | 1114 | get_info(ctx->ovs, &info); |
01845ce8 | 1115 | port = find_port(&info, ctx->argv[1], true); |
457e1eb0 BP |
1116 | set_external_id(port->port_cfg->key_external_ids, |
1117 | port->port_cfg->value_external_ids, | |
1118 | port->port_cfg->n_external_ids, | |
5d9cb63c | 1119 | ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL, |
457e1eb0 BP |
1120 | &keys, &values, &n); |
1121 | ovsrec_port_set_external_ids(port->port_cfg, keys, values, n); | |
1122 | free(keys); | |
1123 | free(values); | |
1124 | ||
1125 | free_info(&info); | |
1126 | } | |
1127 | ||
1128 | static void | |
5d9cb63c | 1129 | cmd_port_get_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
1130 | { |
1131 | struct vsctl_info info; | |
1132 | struct vsctl_port *port; | |
1133 | ||
5d9cb63c | 1134 | get_info(ctx->ovs, &info); |
01845ce8 | 1135 | port = find_port(&info, ctx->argv[1], true); |
457e1eb0 BP |
1136 | get_external_id(port->port_cfg->key_external_ids, |
1137 | port->port_cfg->value_external_ids, | |
1138 | port->port_cfg->n_external_ids, | |
5d9cb63c | 1139 | "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output); |
457e1eb0 BP |
1140 | free_info(&info); |
1141 | } | |
1142 | ||
c75d1511 | 1143 | static void |
5d9cb63c | 1144 | cmd_br_to_vlan(struct vsctl_context *ctx) |
c75d1511 BP |
1145 | { |
1146 | struct vsctl_bridge *bridge; | |
1147 | struct vsctl_info info; | |
1148 | ||
5d9cb63c | 1149 | get_info(ctx->ovs, &info); |
01845ce8 | 1150 | bridge = find_bridge(&info, ctx->argv[1], true); |
5d9cb63c | 1151 | ds_put_format(&ctx->output, "%d\n", bridge->vlan); |
c75d1511 BP |
1152 | free_info(&info); |
1153 | } | |
1154 | ||
1155 | static void | |
5d9cb63c | 1156 | cmd_br_to_parent(struct vsctl_context *ctx) |
c75d1511 BP |
1157 | { |
1158 | struct vsctl_bridge *bridge; | |
1159 | struct vsctl_info info; | |
1160 | ||
5d9cb63c | 1161 | get_info(ctx->ovs, &info); |
01845ce8 | 1162 | bridge = find_bridge(&info, ctx->argv[1], true); |
c75d1511 BP |
1163 | if (bridge->parent) { |
1164 | bridge = bridge->parent; | |
1165 | } | |
5d9cb63c | 1166 | ds_put_format(&ctx->output, "%s\n", bridge->name); |
c75d1511 BP |
1167 | free_info(&info); |
1168 | } | |
1169 | ||
1170 | static void | |
5d9cb63c | 1171 | cmd_list_ifaces(struct vsctl_context *ctx) |
c75d1511 BP |
1172 | { |
1173 | struct vsctl_bridge *br; | |
1174 | struct shash_node *node; | |
1175 | struct vsctl_info info; | |
dfbe07ba | 1176 | struct svec ifaces; |
c75d1511 | 1177 | |
5d9cb63c | 1178 | get_info(ctx->ovs, &info); |
01845ce8 | 1179 | br = find_bridge(&info, ctx->argv[1], true); |
dfbe07ba BP |
1180 | |
1181 | svec_init(&ifaces); | |
c75d1511 BP |
1182 | SHASH_FOR_EACH (node, &info.ifaces) { |
1183 | struct vsctl_iface *iface = node->data; | |
1184 | ||
dfbe07ba BP |
1185 | if (strcmp(iface->iface_cfg->name, br->name) |
1186 | && br == iface->port->bridge) { | |
1187 | svec_add(&ifaces, iface->iface_cfg->name); | |
c75d1511 BP |
1188 | } |
1189 | } | |
5d9cb63c | 1190 | output_sorted(&ifaces, &ctx->output); |
dfbe07ba BP |
1191 | svec_destroy(&ifaces); |
1192 | ||
c75d1511 BP |
1193 | free_info(&info); |
1194 | } | |
1195 | ||
1196 | static void | |
5d9cb63c | 1197 | cmd_iface_to_br(struct vsctl_context *ctx) |
c75d1511 BP |
1198 | { |
1199 | struct vsctl_iface *iface; | |
1200 | struct vsctl_info info; | |
1201 | ||
5d9cb63c | 1202 | get_info(ctx->ovs, &info); |
01845ce8 | 1203 | iface = find_iface(&info, ctx->argv[1], true); |
5d9cb63c | 1204 | ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name); |
c75d1511 BP |
1205 | free_info(&info); |
1206 | } | |
457e1eb0 BP |
1207 | |
1208 | static void | |
5d9cb63c | 1209 | cmd_iface_set_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
1210 | { |
1211 | struct vsctl_info info; | |
1212 | struct vsctl_iface *iface; | |
1213 | char **keys, **values; | |
1214 | size_t n; | |
1215 | ||
5d9cb63c | 1216 | get_info(ctx->ovs, &info); |
01845ce8 | 1217 | iface = find_iface(&info, ctx->argv[1], true); |
457e1eb0 BP |
1218 | set_external_id(iface->iface_cfg->key_external_ids, |
1219 | iface->iface_cfg->value_external_ids, | |
1220 | iface->iface_cfg->n_external_ids, | |
5d9cb63c | 1221 | ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL, |
457e1eb0 BP |
1222 | &keys, &values, &n); |
1223 | ovsrec_interface_set_external_ids(iface->iface_cfg, keys, values, n); | |
1224 | free(keys); | |
1225 | free(values); | |
1226 | ||
1227 | free_info(&info); | |
1228 | } | |
1229 | ||
1230 | static void | |
5d9cb63c | 1231 | cmd_iface_get_external_id(struct vsctl_context *ctx) |
457e1eb0 BP |
1232 | { |
1233 | struct vsctl_info info; | |
1234 | struct vsctl_iface *iface; | |
1235 | ||
5d9cb63c | 1236 | get_info(ctx->ovs, &info); |
01845ce8 | 1237 | iface = find_iface(&info, ctx->argv[1], true); |
457e1eb0 BP |
1238 | get_external_id(iface->iface_cfg->key_external_ids, |
1239 | iface->iface_cfg->value_external_ids, | |
1240 | iface->iface_cfg->n_external_ids, | |
5d9cb63c | 1241 | "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output); |
457e1eb0 BP |
1242 | free_info(&info); |
1243 | } | |
5aa00635 JP |
1244 | |
1245 | static void | |
1246 | cmd_get_controller(struct vsctl_context *ctx) | |
1247 | { | |
1248 | struct vsctl_info info; | |
1249 | ||
1250 | get_info(ctx->ovs, &info); | |
1251 | ||
1252 | if (ctx->argc == 1) { | |
1253 | /* Return the controller from the "Open_vSwitch" table */ | |
1254 | if (info.ctrl) { | |
1255 | ds_put_format(&ctx->output, "%s\n", info.ctrl->target); | |
1256 | } | |
1257 | } else { | |
1258 | /* Return the controller for a particular bridge. */ | |
1259 | struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true); | |
1260 | ||
1261 | /* If no controller is explicitly defined for the requested | |
1262 | * bridge, fallback to the "Open_vSwitch" table's controller. */ | |
1263 | if (br->ctrl) { | |
1264 | ds_put_format(&ctx->output, "%s\n", br->ctrl->target); | |
1265 | } else if (info.ctrl) { | |
1266 | ds_put_format(&ctx->output, "%s\n", info.ctrl->target); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | free_info(&info); | |
1271 | } | |
1272 | ||
1273 | static void | |
1274 | cmd_del_controller(struct vsctl_context *ctx) | |
1275 | { | |
1276 | struct vsctl_info info; | |
1277 | ||
1278 | get_info(ctx->ovs, &info); | |
1279 | ||
1280 | if (ctx->argc == 1) { | |
1281 | if (info.ctrl) { | |
1282 | ovsrec_controller_delete(info.ctrl); | |
1283 | ovsrec_open_vswitch_set_controller(ctx->ovs, NULL); | |
1284 | } | |
1285 | } else { | |
975ac531 | 1286 | struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true); |
5aa00635 JP |
1287 | |
1288 | if (br->ctrl) { | |
1289 | ovsrec_controller_delete(br->ctrl); | |
1290 | ovsrec_bridge_set_controller(br->br_cfg, NULL); | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | free_info(&info); | |
1295 | } | |
1296 | ||
1297 | static void | |
1298 | cmd_set_controller(struct vsctl_context *ctx) | |
1299 | { | |
1300 | struct vsctl_info info; | |
1301 | struct ovsrec_controller *ctrl; | |
1302 | ||
1303 | get_info(ctx->ovs, &info); | |
1304 | ||
1305 | if (ctx->argc == 2) { | |
1306 | /* Set the controller in the "Open_vSwitch" table. */ | |
1307 | if (info.ctrl) { | |
1308 | ovsrec_controller_delete(info.ctrl); | |
1309 | } | |
1310 | ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs)); | |
1311 | ovsrec_controller_set_target(ctrl, ctx->argv[1]); | |
1312 | ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl); | |
1313 | } else { | |
1314 | /* Set the controller for a particular bridge. */ | |
975ac531 | 1315 | struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true); |
5aa00635 JP |
1316 | |
1317 | if (br->ctrl) { | |
1318 | ovsrec_controller_delete(br->ctrl); | |
1319 | } | |
1320 | ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs)); | |
1321 | ovsrec_controller_set_target(ctrl, ctx->argv[2]); | |
1322 | ovsrec_bridge_set_controller(br->br_cfg, ctrl); | |
1323 | } | |
1324 | ||
1325 | free_info(&info); | |
1326 | } | |
1327 | ||
1328 | static void | |
1329 | cmd_get_fail_mode(struct vsctl_context *ctx) | |
1330 | { | |
1331 | struct vsctl_info info; | |
1332 | const char *fail_mode = NULL; | |
1333 | ||
1334 | get_info(ctx->ovs, &info); | |
1335 | ||
1336 | if (ctx->argc == 1) { | |
1337 | /* Return the fail-mode from the "Open_vSwitch" table */ | |
1338 | if (info.ctrl && info.ctrl->fail_mode) { | |
1339 | fail_mode = info.ctrl->fail_mode; | |
1340 | } | |
1341 | } else { | |
1342 | /* Return the fail-mode for a particular bridge. */ | |
1343 | struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true); | |
1344 | ||
1345 | /* If no controller or fail-mode is explicitly defined for the | |
1346 | * requested bridge, fallback to the "Open_vSwitch" table's | |
1347 | * setting. */ | |
1348 | if (br->ctrl && br->ctrl->fail_mode) { | |
1349 | fail_mode = br->ctrl->fail_mode; | |
1350 | } else if (info.ctrl && info.ctrl->fail_mode) { | |
1351 | fail_mode = info.ctrl->fail_mode; | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | if (fail_mode && strlen(fail_mode)) { | |
d35a4ea8 | 1356 | ds_put_format(&ctx->output, "%s\n", fail_mode); |
5aa00635 JP |
1357 | } |
1358 | ||
1359 | free_info(&info); | |
1360 | } | |
1361 | ||
1362 | static void | |
1363 | cmd_del_fail_mode(struct vsctl_context *ctx) | |
1364 | { | |
1365 | struct vsctl_info info; | |
1366 | ||
1367 | get_info(ctx->ovs, &info); | |
1368 | ||
1369 | if (ctx->argc == 1) { | |
1370 | if (info.ctrl && info.ctrl->fail_mode) { | |
1371 | ovsrec_controller_set_fail_mode(info.ctrl, NULL); | |
1372 | } | |
1373 | } else { | |
975ac531 | 1374 | struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true); |
5aa00635 JP |
1375 | |
1376 | if (br->ctrl && br->ctrl->fail_mode) { | |
1377 | ovsrec_controller_set_fail_mode(br->ctrl, NULL); | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | free_info(&info); | |
1382 | } | |
1383 | ||
1384 | static void | |
1385 | cmd_set_fail_mode(struct vsctl_context *ctx) | |
1386 | { | |
1387 | struct vsctl_info info; | |
1388 | const char *fail_mode; | |
1389 | ||
1390 | get_info(ctx->ovs, &info); | |
1391 | ||
1392 | fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2]; | |
1393 | ||
1394 | if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) { | |
1395 | vsctl_fatal("fail-mode must be \"standalone\" or \"secure\""); | |
1396 | } | |
1397 | ||
1398 | if (ctx->argc == 2) { | |
1399 | /* Set the fail-mode in the "Open_vSwitch" table. */ | |
1400 | if (!info.ctrl) { | |
1401 | vsctl_fatal("no controller declared"); | |
1402 | } | |
1403 | ovsrec_controller_set_fail_mode(info.ctrl, fail_mode); | |
1404 | } else { | |
975ac531 | 1405 | struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true); |
5aa00635 JP |
1406 | |
1407 | if (!br->ctrl) { | |
1408 | vsctl_fatal("no controller declared for %s", br->name); | |
1409 | } | |
1410 | ovsrec_controller_set_fail_mode(br->ctrl, fail_mode); | |
1411 | } | |
1412 | ||
1413 | free_info(&info); | |
1414 | } | |
dd8ac6fe JP |
1415 | |
1416 | static void | |
1417 | cmd_get_ssl(struct vsctl_context *ctx) | |
1418 | { | |
1419 | struct ovsrec_ssl *ssl = ctx->ovs->ssl; | |
1420 | ||
1421 | if (ssl) { | |
1422 | ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key); | |
1423 | ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate); | |
1424 | ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert); | |
1425 | ds_put_format(&ctx->output, "Bootstrap: %s\n", | |
1426 | ssl->bootstrap_ca_cert ? "true" : "false"); | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | static void | |
1431 | cmd_del_ssl(struct vsctl_context *ctx) | |
1432 | { | |
1433 | struct ovsrec_ssl *ssl = ctx->ovs->ssl; | |
1434 | ||
1435 | if (ssl) { | |
1436 | ovsrec_ssl_delete(ssl); | |
1437 | ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL); | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | static void | |
1442 | cmd_set_ssl(struct vsctl_context *ctx) | |
1443 | { | |
1444 | bool bootstrap = shash_find(&ctx->options, "--bootstrap"); | |
1445 | struct ovsrec_ssl *ssl = ctx->ovs->ssl; | |
1446 | ||
1447 | if (ssl) { | |
1448 | ovsrec_ssl_delete(ssl); | |
1449 | } | |
1450 | ssl = ovsrec_ssl_insert(txn_from_openvswitch(ctx->ovs)); | |
1451 | ||
1452 | ovsrec_ssl_set_private_key(ssl, ctx->argv[1]); | |
1453 | ovsrec_ssl_set_certificate(ssl, ctx->argv[2]); | |
1454 | ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]); | |
1455 | ||
1456 | ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap); | |
1457 | ||
1458 | ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl); | |
1459 | } | |
c75d1511 | 1460 | \f |
ad83bfa6 BP |
1461 | /* Parameter commands. */ |
1462 | ||
1463 | /* POSIX extended regular expression for an 8-bit unsigned decimal integer. */ | |
1464 | #define OCTET_RE "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" | |
1465 | ||
1466 | /* POSIX extended regular expression for an IP address. */ | |
1467 | #define IP_RE "("OCTET_RE"\\."OCTET_RE"\\."OCTET_RE"\\."OCTET_RE")" | |
1468 | ||
1469 | /* POSIX extended regular expression for a netmask. */ | |
1470 | #define NETMASK_RE \ | |
1471 | "255.255.255."NETMASK_END_RE"|" \ | |
1472 | "255.255."NETMASK_END_RE".0|" \ | |
1473 | "255."NETMASK_END_RE".0.0|" \ | |
1474 | NETMASK_END_RE".0.0.0" | |
1475 | #define NETMASK_END_RE "(255|254|252|248|240|224|192|128|0)" | |
1476 | ||
1477 | /* POSIX extended regular expression for an Ethernet address. */ | |
1478 | #define XX_RE "[0-9a-fA-F][0-9a-fA-F]" | |
1479 | #define MAC_RE XX_RE":"XX_RE":"XX_RE":"XX_RE":"XX_RE":"XX_RE | |
1480 | ||
1481 | /* POSIX extended regular expression for a TCP or UDP port number. */ | |
1482 | #define PORT_RE \ | |
1483 | "([0-9]|" \ | |
1484 | "[1-9][0-9]|" \ | |
1485 | "[1-9][0-9][0-9]|" \ | |
1486 | "[1-9][0-9][0-9][0-9]|" \ | |
1487 | "[1-5][0-9][0-9][0-9][0-9]|" \ | |
1488 | "6[1-4][0-9][0-9][0-9]|" \ | |
1489 | "65[1-4][0-9][0-9]|" \ | |
1490 | "655[1-2][0-9]|" \ | |
1491 | "6553[1-5])" | |
1492 | ||
1493 | enum { | |
1494 | VSCF_READONLY = 1 << 0, | |
1495 | VSCF_HIDDEN = 1 << 1 | |
1496 | }; | |
1497 | ||
1498 | struct vsctl_column { | |
1499 | struct ovsdb_idl_column *idl; | |
1500 | int flags; | |
1501 | const char *constraint; | |
1502 | }; | |
1503 | ||
1504 | static const struct vsctl_column bridge_columns[] = { | |
1505 | {&ovsrec_bridge_col_datapath_id, VSCF_READONLY, NULL}, | |
1506 | {&ovsrec_bridge_col_name, VSCF_READONLY, NULL}, | |
1507 | {&ovsrec_bridge_col_mirrors, VSCF_READONLY, NULL}, | |
1508 | {&ovsrec_bridge_col_other_config, 0, NULL}, | |
73450047 | 1509 | {&ovsrec_bridge_col_flood_vlans, 0, "[1,4095]"}, |
ad83bfa6 BP |
1510 | {&ovsrec_bridge_col_controller, VSCF_READONLY, NULL}, |
1511 | {&ovsrec_bridge_col_netflow, VSCF_READONLY, NULL}, | |
1512 | {&ovsrec_bridge_col_external_ids, 0, NULL}, | |
1513 | {&ovsrec_bridge_col_ports, VSCF_READONLY, NULL}, | |
1514 | {NULL, 0, NULL}, | |
1515 | }; | |
1516 | ||
1517 | static const struct vsctl_column controller_columns[] = { | |
1518 | {&ovsrec_controller_col_connection_mode, 0, "in-band|out-of-band"}, | |
1519 | {&ovsrec_controller_col_controller_burst_limit, 0, "[25,]"}, | |
1520 | {&ovsrec_controller_col_controller_rate_limit, 0, "[100,]"}, | |
1521 | {&ovsrec_controller_col_discover_accept_regex, 0, NULL}, | |
1522 | {&ovsrec_controller_col_discover_update_resolv_conf, 0, NULL}, | |
1523 | {&ovsrec_controller_col_fail_mode, 0, "standalone|secure"}, | |
1524 | {&ovsrec_controller_col_inactivity_probe, 0, "[5000,]"}, | |
1525 | {&ovsrec_controller_col_local_gateway, 0, IP_RE}, | |
1526 | {&ovsrec_controller_col_local_ip, 0, IP_RE}, | |
1527 | {&ovsrec_controller_col_local_netmask, 0, NETMASK_RE}, | |
1528 | {&ovsrec_controller_col_max_backoff, 0, "[1000,]"}, | |
1529 | {&ovsrec_controller_col_target, 0, NULL}, | |
1530 | {NULL, 0, NULL}, | |
1531 | }; | |
1532 | ||
1533 | static const struct vsctl_column interface_columns[] = { | |
1534 | {&ovsrec_interface_col_external_ids, 0, NULL}, | |
1535 | {&ovsrec_interface_col_ingress_policing_burst, 0, "[10,]"}, | |
1536 | {&ovsrec_interface_col_ingress_policing_rate, 0, "[100,]"}, | |
1537 | {&ovsrec_interface_col_mac, 0, MAC_RE}, | |
1538 | {&ovsrec_interface_col_name, VSCF_READONLY, NULL}, | |
1539 | {&ovsrec_interface_col_ofport, VSCF_READONLY, NULL}, | |
1540 | {&ovsrec_interface_col_options, 0, NULL}, | |
1541 | {&ovsrec_interface_col_type, VSCF_READONLY, NULL}, | |
1542 | {NULL, 0, NULL}, | |
1543 | }; | |
1544 | ||
1545 | static const struct vsctl_column mirror_columns[] = { | |
1546 | {&ovsrec_mirror_col_name, VSCF_READONLY, NULL}, | |
1547 | {&ovsrec_mirror_col_output_port, 0, "Port"}, | |
1548 | {&ovsrec_mirror_col_output_vlan, 0, "[1,4095]"}, | |
1549 | {&ovsrec_mirror_col_select_dst_port, 0, "Port"}, | |
1550 | {&ovsrec_mirror_col_select_src_port, 0, "Port"}, | |
1551 | {&ovsrec_mirror_col_select_vlan, 0, "[1,4095]"}, | |
1552 | {NULL, 0, NULL}, | |
1553 | }; | |
1554 | ||
1555 | static const struct vsctl_column netflow_columns[] = { | |
1556 | {&ovsrec_netflow_col_active_timeout, 0, "[-1,]"}, | |
1557 | {&ovsrec_netflow_col_add_id_to_interface, 0, NULL}, | |
1558 | {&ovsrec_netflow_col_targets, 0, IP_RE":"PORT_RE}, | |
1559 | {&ovsrec_netflow_col_engine_type, 0, "[0,255]"}, | |
1560 | {&ovsrec_netflow_col_engine_id, 0, "[0,255]"}, | |
1561 | {NULL, 0, NULL}, | |
1562 | }; | |
1563 | ||
1564 | static const struct vsctl_column open_vswitch_columns[] = { | |
1565 | {&ovsrec_open_vswitch_col_bridges, VSCF_READONLY, NULL}, | |
1566 | {&ovsrec_open_vswitch_col_controller, VSCF_READONLY, NULL}, | |
1567 | {&ovsrec_open_vswitch_col_cur_cfg, VSCF_HIDDEN, NULL}, | |
1568 | {&ovsrec_open_vswitch_col_management_id, 0, "[0-9a-fA-F]{12}"}, | |
1569 | {&ovsrec_open_vswitch_col_managers, 0, "p?(ssl|tcp|unix):.*"}, | |
1570 | {&ovsrec_open_vswitch_col_next_cfg, VSCF_HIDDEN, NULL}, | |
1571 | {&ovsrec_open_vswitch_col_ssl, VSCF_READONLY, NULL}, | |
1572 | {NULL, 0, NULL}, | |
1573 | }; | |
1574 | ||
1575 | static const struct vsctl_column port_columns[] = { | |
73450047 | 1576 | {&ovsrec_port_col_bond_updelay, 0, "[0,]"}, |
ad83bfa6 BP |
1577 | {&ovsrec_port_col_bond_downdelay, 0, "[0,]"}, |
1578 | {&ovsrec_port_col_bond_fake_iface, VSCF_READONLY, NULL}, | |
1579 | {&ovsrec_port_col_external_ids, 0, NULL}, | |
1580 | {&ovsrec_port_col_fake_bridge, VSCF_READONLY, NULL}, | |
1581 | {&ovsrec_port_col_interfaces, VSCF_READONLY, NULL}, | |
1582 | {&ovsrec_port_col_mac, 0, MAC_RE}, | |
1583 | {&ovsrec_port_col_name, VSCF_READONLY, NULL}, | |
1584 | {&ovsrec_port_col_other_config, 0, NULL}, | |
1585 | {&ovsrec_port_col_tag, 0, "[0,4095]"}, | |
1586 | {&ovsrec_port_col_trunks, 0, "[0,4095]"}, | |
1587 | {NULL, 0, NULL}, | |
1588 | }; | |
1589 | ||
1590 | static const struct vsctl_column ssl_columns[] = { | |
1591 | {&ovsrec_ssl_col_bootstrap_ca_cert, 0, NULL}, | |
1592 | {&ovsrec_ssl_col_ca_cert, 0, NULL}, | |
1593 | {&ovsrec_ssl_col_certificate, 0, NULL}, | |
1594 | {&ovsrec_ssl_col_private_key, 0, NULL}, | |
1595 | {NULL, 0, NULL}, | |
1596 | }; | |
1597 | ||
1598 | struct vsctl_row_id { | |
1599 | const struct ovsdb_idl_table_class *table; | |
1600 | const struct ovsdb_idl_column *name_column; | |
1601 | const struct ovsdb_idl_column *uuid_column; | |
1602 | }; | |
1603 | ||
1604 | struct vsctl_table_class { | |
1605 | struct ovsdb_idl_table_class *class; | |
1606 | const struct vsctl_column *columns; | |
1607 | struct vsctl_row_id row_ids[2]; | |
1608 | }; | |
1609 | ||
1610 | static const struct vsctl_table_class tables[] = { | |
1611 | {&ovsrec_table_bridge, bridge_columns, | |
1612 | {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL}, | |
1613 | {NULL, NULL, NULL}}}, | |
1614 | ||
1615 | {&ovsrec_table_controller, controller_columns, | |
1616 | {{&ovsrec_table_bridge, | |
1617 | &ovsrec_bridge_col_name, | |
1618 | &ovsrec_bridge_col_controller}, | |
1619 | {&ovsrec_table_open_vswitch, | |
1620 | NULL, | |
1621 | &ovsrec_open_vswitch_col_controller}}}, | |
1622 | ||
1623 | {&ovsrec_table_interface, interface_columns, | |
1624 | {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL}, | |
1625 | {NULL, NULL, NULL}}}, | |
1626 | ||
1627 | {&ovsrec_table_mirror, mirror_columns, | |
1628 | {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL}, | |
1629 | {NULL, NULL, NULL}}}, | |
1630 | ||
1631 | {&ovsrec_table_netflow, netflow_columns, | |
1632 | {{&ovsrec_table_bridge, | |
1633 | &ovsrec_bridge_col_name, | |
1634 | &ovsrec_bridge_col_netflow}, | |
1635 | {NULL, NULL, NULL}}}, | |
1636 | ||
1637 | {&ovsrec_table_open_vswitch, open_vswitch_columns, | |
1638 | {{&ovsrec_table_open_vswitch, NULL, NULL}, | |
1639 | {NULL, NULL, NULL}}}, | |
1640 | ||
1641 | {&ovsrec_table_port, port_columns, | |
1642 | {{&ovsrec_table_port, &ovsrec_port_col_name, NULL}, | |
1643 | {NULL, NULL, NULL}}}, | |
1644 | ||
1645 | {&ovsrec_table_ssl, ssl_columns, | |
1646 | {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}}, | |
1647 | ||
1648 | {NULL, NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}} | |
1649 | }; | |
1650 | ||
1bc6ff29 BP |
1651 | static void |
1652 | die_if_error(char *error) | |
1653 | { | |
1654 | if (error) { | |
1655 | ovs_fatal(0, "%s", error); | |
1656 | } | |
1657 | } | |
1658 | ||
ad83bfa6 BP |
1659 | static int |
1660 | to_lower_and_underscores(unsigned c) | |
1661 | { | |
1662 | return c == '-' ? '_' : tolower(c); | |
1663 | } | |
1664 | ||
1665 | static unsigned int | |
1666 | score_partial_match(const char *name, const char *s) | |
1667 | { | |
1668 | int score; | |
1669 | ||
5128bd9c BP |
1670 | if (!strcmp(name, s)) { |
1671 | return UINT_MAX; | |
1672 | } | |
ad83bfa6 BP |
1673 | for (score = 0; ; score++, name++, s++) { |
1674 | if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) { | |
1675 | break; | |
1676 | } else if (*name == '\0') { | |
5128bd9c | 1677 | return UINT_MAX - 1; |
ad83bfa6 BP |
1678 | } |
1679 | } | |
1680 | return *s == '\0' ? score : 0; | |
1681 | } | |
1682 | ||
1683 | static const struct vsctl_table_class * | |
1684 | get_table(const char *table_name) | |
1685 | { | |
1686 | const struct vsctl_table_class *table; | |
1687 | const struct vsctl_table_class *best_match = NULL; | |
1688 | unsigned int best_score = 0; | |
1689 | ||
1690 | for (table = tables; table->class; table++) { | |
1691 | unsigned int score = score_partial_match(table->class->name, | |
1692 | table_name); | |
1693 | if (score > best_score) { | |
1694 | best_match = table; | |
1695 | best_score = score; | |
1696 | } else if (score == best_score) { | |
1697 | best_match = NULL; | |
1698 | } | |
1699 | } | |
1700 | if (best_match) { | |
1701 | return best_match; | |
1702 | } else if (best_score) { | |
1703 | ovs_fatal(0, "multiple table names match \"%s\"", table_name); | |
1704 | } else { | |
1705 | ovs_fatal(0, "unknown table \"%s\"", table_name); | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | static const struct ovsdb_idl_row * | |
1710 | get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table, | |
1711 | const struct vsctl_row_id *id, const char *record_id) | |
1712 | { | |
1713 | const struct ovsdb_idl_row *referrer, *final; | |
1714 | ||
1715 | if (!id->table) { | |
1716 | return NULL; | |
1717 | } | |
1718 | ||
1719 | if (!id->name_column) { | |
1720 | if (strcmp(record_id, ".")) { | |
1721 | return NULL; | |
1722 | } | |
1723 | referrer = ovsdb_idl_first_row(ctx->idl, id->table); | |
1724 | if (!referrer || ovsdb_idl_next_row(referrer)) { | |
1725 | return NULL; | |
1726 | } | |
1727 | } else { | |
1728 | const struct ovsdb_idl_row *row; | |
1729 | unsigned int best_score = 0; | |
1730 | ||
1731 | /* It might make sense to relax this assertion. */ | |
1732 | assert(id->name_column->type.key_type == OVSDB_TYPE_STRING); | |
1733 | ||
1734 | referrer = NULL; | |
1735 | for (row = ovsdb_idl_first_row(ctx->idl, id->table); | |
1736 | row != NULL && best_score != UINT_MAX; | |
1737 | row = ovsdb_idl_next_row(row)) | |
1738 | { | |
1739 | struct ovsdb_datum name; | |
1740 | ||
1741 | ovsdb_idl_txn_read(row, id->name_column, &name); | |
1742 | if (name.n == 1) { | |
1743 | unsigned int score = score_partial_match(name.keys[0].string, | |
1744 | record_id); | |
1745 | if (score > best_score) { | |
1746 | referrer = row; | |
1747 | best_score = score; | |
1748 | } else if (score == best_score) { | |
1749 | referrer = NULL; | |
1750 | } | |
1751 | } | |
1752 | ovsdb_datum_destroy(&name, &id->name_column->type); | |
1753 | } | |
1754 | } | |
1755 | if (!referrer) { | |
1756 | return NULL; | |
1757 | } | |
1758 | ||
93255bc5 | 1759 | final = NULL; |
ad83bfa6 BP |
1760 | if (id->uuid_column) { |
1761 | struct ovsdb_datum uuid; | |
1762 | ||
1763 | assert(id->uuid_column->type.key_type == OVSDB_TYPE_UUID); | |
1764 | assert(id->uuid_column->type.value_type == OVSDB_TYPE_VOID); | |
1765 | ||
1766 | ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid); | |
1767 | if (uuid.n == 1) { | |
1768 | final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, | |
1769 | &uuid.keys[0].uuid); | |
1770 | } | |
1771 | ovsdb_datum_destroy(&uuid, &id->uuid_column->type); | |
1772 | } else { | |
1773 | final = referrer; | |
1774 | } | |
1775 | ||
1776 | return final; | |
1777 | } | |
1778 | ||
1779 | static const struct ovsdb_idl_row * | |
1780 | get_row(struct vsctl_context *ctx, | |
1781 | const struct vsctl_table_class *table, const char *record_id) | |
1782 | { | |
1783 | const struct ovsdb_idl_row *row; | |
1784 | struct uuid uuid; | |
1785 | ||
1786 | if (uuid_from_string(&uuid, record_id)) { | |
1787 | row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid); | |
1788 | } else { | |
1789 | int i; | |
1790 | ||
1791 | for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) { | |
1792 | row = get_row_by_id(ctx, table, &table->row_ids[i], record_id); | |
1793 | if (row) { | |
1794 | break; | |
1795 | } | |
1796 | } | |
1797 | } | |
1798 | if (!row) { | |
1799 | ovs_fatal(0, "no row \"%s\" in table %s", | |
1800 | record_id, table->class->name); | |
1801 | } | |
1802 | return row; | |
1803 | } | |
1804 | ||
1bc6ff29 BP |
1805 | static char * |
1806 | get_column(const struct vsctl_table_class *table, const char *column_name, | |
1807 | const struct vsctl_column **columnp) | |
ad83bfa6 BP |
1808 | { |
1809 | const struct vsctl_column *column; | |
1810 | const struct vsctl_column *best_match = NULL; | |
1811 | unsigned int best_score = 0; | |
1812 | ||
1813 | for (column = table->columns; column->idl; column++) { | |
1814 | if (!(column->flags & VSCF_HIDDEN)) { | |
1815 | unsigned int score = score_partial_match(column->idl->name, | |
1816 | column_name); | |
1817 | if (score > best_score) { | |
1818 | best_match = column; | |
1819 | best_score = score; | |
1820 | } else if (score == best_score) { | |
1821 | best_match = NULL; | |
1822 | } | |
1823 | } | |
1824 | } | |
1bc6ff29 BP |
1825 | |
1826 | *columnp = best_match; | |
ad83bfa6 | 1827 | if (best_match) { |
1bc6ff29 | 1828 | return NULL; |
ad83bfa6 | 1829 | } else if (best_score) { |
1bc6ff29 BP |
1830 | return xasprintf("%s contains more than one column whose name " |
1831 | "matches \"%s\"", table->class->name, column_name); | |
ad83bfa6 | 1832 | } else { |
1bc6ff29 BP |
1833 | return xasprintf("%s does not contain a column whose name matches " |
1834 | "\"%s\"", table->class->name, column_name); | |
ad83bfa6 BP |
1835 | } |
1836 | } | |
1837 | ||
1bc6ff29 | 1838 | static char * WARN_UNUSED_RESULT |
ad83bfa6 BP |
1839 | parse_column_key_value(const char *arg, const struct vsctl_table_class *table, |
1840 | const struct vsctl_column **columnp, | |
1841 | char **keyp, char **valuep) | |
1842 | { | |
1843 | const char *p = arg; | |
1bc6ff29 | 1844 | char *error; |
ad83bfa6 BP |
1845 | |
1846 | assert(columnp || keyp); | |
1bc6ff29 BP |
1847 | if (keyp) { |
1848 | *keyp = NULL; | |
1849 | } | |
1850 | if (valuep) { | |
1851 | *valuep = NULL; | |
1852 | } | |
ad83bfa6 BP |
1853 | |
1854 | /* Parse column name. */ | |
1855 | if (columnp) { | |
1856 | char *column_name; | |
1857 | ||
557e3718 | 1858 | error = ovsdb_token_parse(&p, &column_name); |
1bc6ff29 BP |
1859 | if (error) { |
1860 | goto error; | |
1861 | } | |
ad83bfa6 | 1862 | if (column_name[0] == '\0') { |
1bc6ff29 BP |
1863 | free(column_name); |
1864 | error = xasprintf("%s: missing column name", arg); | |
1865 | goto error; | |
1866 | } | |
1867 | error = get_column(table, column_name, columnp); | |
1868 | if (error) { | |
1869 | goto error; | |
ad83bfa6 | 1870 | } |
ad83bfa6 BP |
1871 | free(column_name); |
1872 | } | |
1873 | ||
1874 | /* Parse key string. */ | |
1875 | if (*p == ':' || !columnp) { | |
1876 | if (columnp) { | |
1877 | p++; | |
1878 | } else if (!keyp) { | |
1bc6ff29 BP |
1879 | error = xasprintf("%s: key not accepted here", arg); |
1880 | goto error; | |
1881 | } | |
1882 | error = ovsdb_token_parse(&p, keyp); | |
1883 | if (error) { | |
1884 | goto error; | |
ad83bfa6 | 1885 | } |
ad83bfa6 BP |
1886 | } else if (keyp) { |
1887 | *keyp = NULL; | |
1888 | } | |
1889 | ||
1890 | /* Parse value string. */ | |
1891 | if (*p == '=') { | |
1892 | if (!valuep) { | |
1bc6ff29 BP |
1893 | error = xasprintf("%s: value not accepted here", arg); |
1894 | goto error; | |
ad83bfa6 BP |
1895 | } |
1896 | *valuep = xstrdup(p + 1); | |
ad83bfa6 BP |
1897 | } else { |
1898 | if (valuep) { | |
1899 | *valuep = NULL; | |
1900 | } | |
1bc6ff29 BP |
1901 | if (*p != '\0') { |
1902 | error = xasprintf("%s: trailing garbage in argument at offset %td", | |
1903 | arg, p - arg); | |
1904 | goto error; | |
1905 | } | |
1906 | } | |
1907 | return NULL; | |
1908 | ||
1909 | error: | |
1910 | if (columnp) { | |
1911 | *columnp = NULL; | |
1912 | } | |
1913 | if (keyp) { | |
1914 | free(*keyp); | |
1915 | *keyp = NULL; | |
1916 | } | |
1917 | if (valuep) { | |
1918 | free(*valuep); | |
1919 | *valuep = NULL; | |
ad83bfa6 | 1920 | } |
1bc6ff29 | 1921 | return error; |
ad83bfa6 BP |
1922 | } |
1923 | ||
1924 | static void | |
1925 | cmd_get(struct vsctl_context *ctx) | |
1926 | { | |
1927 | const char *table_name = ctx->argv[1]; | |
1928 | const char *record_id = ctx->argv[2]; | |
1929 | const struct vsctl_table_class *table; | |
1930 | const struct ovsdb_idl_row *row; | |
1931 | struct ds *out = &ctx->output; | |
1932 | int i; | |
1933 | ||
1934 | table = get_table(table_name); | |
1935 | row = get_row(ctx, table, record_id); | |
1936 | for (i = 3; i < ctx->argc; i++) { | |
1937 | const struct vsctl_column *column; | |
1938 | struct ovsdb_datum datum; | |
1939 | char *key_string; | |
1940 | ||
1bc6ff29 BP |
1941 | die_if_error(parse_column_key_value(ctx->argv[i], table, |
1942 | &column, &key_string, NULL)); | |
ad83bfa6 BP |
1943 | |
1944 | ovsdb_idl_txn_read(row, column->idl, &datum); | |
1945 | if (key_string) { | |
1946 | union ovsdb_atom key; | |
1947 | unsigned int idx; | |
1948 | ||
1949 | if (column->idl->type.value_type == OVSDB_TYPE_VOID) { | |
1950 | ovs_fatal(0, "cannot specify key to get for non-map column %s", | |
1951 | column->idl->name); | |
1952 | } | |
1953 | ||
1bc6ff29 BP |
1954 | die_if_error(ovsdb_atom_from_string(&key, |
1955 | column->idl->type.key_type, | |
1956 | key_string)); | |
ad83bfa6 BP |
1957 | |
1958 | idx = ovsdb_datum_find_key(&datum, &key, | |
1959 | column->idl->type.key_type); | |
1960 | if (idx == UINT_MAX) { | |
1961 | ovs_fatal(0, "no key %s in %s record \"%s\" column %s", | |
1962 | key_string, table_name, record_id, | |
1963 | column->idl->name); | |
1964 | ||
1965 | } | |
1966 | ovsdb_atom_to_string(&datum.values[idx], | |
1967 | column->idl->type.value_type, out); | |
1968 | ||
1969 | ovsdb_atom_destroy(&key, column->idl->type.key_type); | |
1970 | } else { | |
1971 | ovsdb_datum_to_string(&datum, &column->idl->type, out); | |
1972 | } | |
1973 | ds_put_char(out, '\n'); | |
1974 | ovsdb_datum_destroy(&datum, &column->idl->type); | |
1975 | ||
1976 | free(key_string); | |
1977 | } | |
1978 | } | |
1979 | ||
1980 | static void | |
1bc6ff29 BP |
1981 | list_record(const struct vsctl_table_class *table, |
1982 | const struct ovsdb_idl_row *row, struct ds *out) | |
ad83bfa6 BP |
1983 | { |
1984 | const struct vsctl_column *column; | |
1985 | ||
c611c9d0 BP |
1986 | ds_put_format(out, "%-20s (RO): "UUID_FMT"\n", "_uuid", |
1987 | UUID_ARGS(&row->uuid)); | |
ad83bfa6 BP |
1988 | for (column = table->columns; column->idl; column++) { |
1989 | struct ovsdb_datum datum; | |
1990 | ||
1991 | if (column->flags & VSCF_HIDDEN) { | |
1992 | continue; | |
1993 | } | |
1994 | ||
1995 | ovsdb_idl_txn_read(row, column->idl, &datum); | |
1996 | ||
1997 | ds_put_format(out, "%-20s (%s): ", column->idl->name, | |
1998 | column->flags & VSCF_READONLY ? "RO" : "RW"); | |
1999 | ovsdb_datum_to_string(&datum, &column->idl->type, out); | |
2000 | ds_put_char(out, '\n'); | |
2001 | ||
2002 | ovsdb_datum_destroy(&datum, &column->idl->type); | |
2003 | } | |
2004 | } | |
2005 | ||
2006 | static void | |
2007 | cmd_list(struct vsctl_context *ctx) | |
2008 | { | |
2009 | const char *table_name = ctx->argv[1]; | |
2010 | const struct vsctl_table_class *table; | |
2011 | struct ds *out = &ctx->output; | |
2012 | int i; | |
2013 | ||
2014 | table = get_table(table_name); | |
2015 | if (ctx->argc > 2) { | |
2016 | for (i = 2; i < ctx->argc; i++) { | |
2017 | if (i > 2) { | |
2018 | ds_put_char(out, '\n'); | |
2019 | } | |
2020 | list_record(table, get_row(ctx, table, ctx->argv[i]), out); | |
2021 | } | |
2022 | } else { | |
2023 | const struct ovsdb_idl_row *row; | |
2024 | bool first; | |
2025 | ||
2026 | for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true; | |
2027 | row != NULL; | |
2028 | row = ovsdb_idl_next_row(row), first = false) { | |
2029 | if (!first) { | |
2030 | ds_put_char(out, '\n'); | |
2031 | } | |
2032 | list_record(table, row, out); | |
2033 | } | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | static void | |
2038 | check_string_constraint(const struct ovsdb_datum *datum, | |
2039 | const char *constraint) | |
2040 | { | |
2041 | unsigned int i; | |
2042 | char *regex; | |
2043 | regex_t re; | |
2044 | int retval; | |
2045 | ||
2046 | regex = xasprintf("^%s$", constraint); | |
2047 | retval = regcomp(&re, regex, REG_NOSUB | REG_EXTENDED); | |
2048 | if (retval) { | |
2049 | size_t length = regerror(retval, &re, NULL, 0); | |
2050 | char *buffer = xmalloc(length); | |
2051 | regerror(retval, &re, buffer, length); | |
2052 | ovs_fatal(0, "internal error compiling regular expression %s: %s", | |
2053 | regex, buffer); | |
2054 | } | |
2055 | ||
2056 | for (i = 0; i < datum->n; i++) { | |
2057 | const char *key = datum->keys[i].string; | |
2058 | if (regexec(&re, key, 0, NULL, 0)) { | |
2059 | ovs_fatal(0, "%s is not valid (it does not match %s)", key, regex); | |
2060 | } | |
2061 | } | |
2062 | free(regex); | |
2063 | regfree(&re); | |
2064 | } | |
2065 | ||
2066 | static void | |
2067 | check_integer_constraint(const struct ovsdb_datum *datum, | |
2068 | const char *constraint) | |
2069 | { | |
2070 | int64_t min, max; | |
2071 | unsigned int i; | |
2072 | int n = -1; | |
2073 | ||
2074 | sscanf(constraint, "[%"SCNd64",%"SCNd64"]%n", &min, &max, &n); | |
2075 | if (n == -1) { | |
2076 | sscanf(constraint, "[%"SCNd64",]%n", &min, &n); | |
2077 | if (n == -1) { | |
2078 | sscanf(constraint, "[,%"SCNd64"]%n", &max, &n); | |
2079 | if (n == -1) { | |
2080 | VLOG_DBG("internal error: bad integer contraint \"%s\"", | |
2081 | constraint); | |
2082 | return; | |
2083 | } else { | |
2084 | min = INT64_MIN; | |
2085 | } | |
2086 | } else { | |
2087 | max = INT64_MAX; | |
2088 | } | |
2089 | } | |
2090 | ||
2091 | for (i = 0; i < datum->n; i++) { | |
2092 | int64_t value = datum->keys[i].integer; | |
2093 | if (value < min || value > max) { | |
2094 | if (max == INT64_MAX) { | |
2095 | ovs_fatal(0, "%"PRId64" is less than the minimum " | |
2096 | "allowed value %"PRId64, value, min); | |
2097 | } else if (min == INT64_MIN) { | |
2098 | ovs_fatal(0, "%"PRId64" is greater than the maximum " | |
2099 | "allowed value %"PRId64, value, max); | |
2100 | } else { | |
2101 | ovs_fatal(0, "%"PRId64" is outside the valid range %"PRId64" " | |
2102 | "to %"PRId64" (inclusive)", value, min, max); | |
2103 | } | |
2104 | } | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | static void | |
2109 | check_constraint(const struct ovsdb_datum *datum, | |
2110 | const struct ovsdb_type *type, const char *constraint) | |
2111 | { | |
2112 | if (constraint && datum->n) { | |
2113 | if (type->key_type == OVSDB_TYPE_STRING) { | |
2114 | check_string_constraint(datum, constraint); | |
2115 | } else if (type->key_type == OVSDB_TYPE_INTEGER) { | |
2116 | check_integer_constraint(datum, constraint); | |
2117 | } | |
2118 | } | |
2119 | } | |
2120 | ||
2121 | static void | |
557e3718 BP |
2122 | set_column(const struct vsctl_table_class *table, |
2123 | const struct ovsdb_idl_row *row, | |
2124 | const char *arg, bool force) | |
ad83bfa6 | 2125 | { |
557e3718 BP |
2126 | const struct vsctl_column *column; |
2127 | char *key_string, *value_string; | |
2128 | char *error; | |
ad83bfa6 | 2129 | |
557e3718 BP |
2130 | error = parse_column_key_value(arg, table, &column, &key_string, |
2131 | &value_string); | |
2132 | die_if_error(error); | |
2133 | if (column->flags & VSCF_READONLY && !force) { | |
2134 | ovs_fatal(0, "%s: cannot modify read-only column %s in table %s", | |
2135 | arg, column->idl->name, table->class->name); | |
2136 | } | |
2137 | if (!value_string) { | |
2138 | ovs_fatal(0, "%s: missing value", arg); | |
2139 | } | |
ad83bfa6 | 2140 | |
557e3718 BP |
2141 | if (key_string) { |
2142 | union ovsdb_atom key, value; | |
2143 | struct ovsdb_datum old, new; | |
2144 | ||
2145 | if (column->idl->type.value_type == OVSDB_TYPE_VOID) { | |
2146 | ovs_fatal(0, "cannot specify key to set for non-map column %s", | |
2147 | column->idl->name); | |
ad83bfa6 BP |
2148 | } |
2149 | ||
557e3718 BP |
2150 | die_if_error(ovsdb_atom_from_string(&key, |
2151 | column->idl->type.key_type, | |
2152 | key_string)); | |
2153 | die_if_error(ovsdb_atom_from_string(&value, | |
2154 | column->idl->type.value_type, | |
2155 | value_string)); | |
ad83bfa6 | 2156 | |
557e3718 BP |
2157 | ovsdb_datum_init_empty(&new); |
2158 | ovsdb_datum_add_unsafe(&new, &key, &value, &column->idl->type); | |
ad83bfa6 | 2159 | |
557e3718 BP |
2160 | ovsdb_idl_txn_read(row, column->idl, &old); |
2161 | ovsdb_datum_union(&old, &new, &column->idl->type, true); | |
2162 | ovsdb_idl_txn_write(row, column->idl, &old); | |
ad83bfa6 | 2163 | |
557e3718 BP |
2164 | ovsdb_datum_destroy(&new, &column->idl->type); |
2165 | } else { | |
2166 | struct ovsdb_datum datum; | |
ad83bfa6 | 2167 | |
557e3718 BP |
2168 | die_if_error(ovsdb_datum_from_string(&datum, &column->idl->type, |
2169 | value_string)); | |
2170 | if (!force) { | |
2171 | check_constraint(&datum, &column->idl->type, | |
2172 | column->constraint); | |
2173 | } | |
2174 | ovsdb_idl_txn_write(row, column->idl, &datum); | |
2175 | } | |
ad83bfa6 | 2176 | |
557e3718 BP |
2177 | free(key_string); |
2178 | } | |
ad83bfa6 | 2179 | |
557e3718 BP |
2180 | static void |
2181 | cmd_set(struct vsctl_context *ctx) | |
2182 | { | |
2183 | bool force = shash_find(&ctx->options, "--force"); | |
2184 | const char *table_name = ctx->argv[1]; | |
2185 | const char *record_id = ctx->argv[2]; | |
2186 | const struct vsctl_table_class *table; | |
2187 | const struct ovsdb_idl_row *row; | |
2188 | int i; | |
ad83bfa6 | 2189 | |
557e3718 BP |
2190 | table = get_table(table_name); |
2191 | row = get_row(ctx, table, record_id); | |
2192 | for (i = 3; i < ctx->argc; i++) { | |
2193 | set_column(table, row, ctx->argv[i], force); | |
ad83bfa6 BP |
2194 | } |
2195 | } | |
2196 | ||
2197 | static void | |
2198 | cmd_add(struct vsctl_context *ctx) | |
2199 | { | |
71bd6358 | 2200 | bool force = shash_find(&ctx->options, "--force"); |
ad83bfa6 BP |
2201 | const char *table_name = ctx->argv[1]; |
2202 | const char *record_id = ctx->argv[2]; | |
2203 | const char *column_name = ctx->argv[3]; | |
2204 | const struct vsctl_table_class *table; | |
2205 | const struct vsctl_column *column; | |
2206 | const struct ovsdb_idl_row *row; | |
2207 | const struct ovsdb_type *type; | |
2208 | struct ovsdb_datum old; | |
2209 | int i; | |
2210 | ||
2211 | table = get_table(table_name); | |
2212 | row = get_row(ctx, table, record_id); | |
1bc6ff29 | 2213 | die_if_error(get_column(table, column_name, &column)); |
ad83bfa6 BP |
2214 | type = &column->idl->type; |
2215 | ovsdb_idl_txn_read(row, column->idl, &old); | |
2216 | for (i = 4; i < ctx->argc; i++) { | |
2217 | struct ovsdb_type add_type; | |
2218 | struct ovsdb_datum add; | |
2219 | ||
71bd6358 | 2220 | if (column->flags & VSCF_READONLY && !force) { |
ad83bfa6 BP |
2221 | ovs_fatal(0, "%s: cannot modify read-only column %s in table %s", |
2222 | ctx->argv[i], column->idl->name, table_name); | |
2223 | } | |
2224 | ||
2225 | add_type = *type; | |
2226 | add_type.n_min = 1; | |
2227 | add_type.n_max = UINT_MAX; | |
1bc6ff29 | 2228 | die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i])); |
ad83bfa6 BP |
2229 | ovsdb_datum_union(&old, &add, type, false); |
2230 | ovsdb_datum_destroy(&add, type); | |
2231 | } | |
2232 | if (old.n > type->n_max) { | |
2233 | ovs_fatal(0, "\"add\" operation would put %u %s in column %s of " | |
2234 | "table %s but at most %u are allowed", | |
2235 | old.n, | |
2236 | type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs", | |
2237 | column->idl->name, table_name, type->n_max); | |
2238 | } | |
2239 | ovsdb_idl_txn_write(row, column->idl, &old); | |
2240 | } | |
90c4bd00 BP |
2241 | |
2242 | static void | |
2243 | cmd_remove(struct vsctl_context *ctx) | |
2244 | { | |
71bd6358 | 2245 | bool force = shash_find(&ctx->options, "--force"); |
90c4bd00 BP |
2246 | const char *table_name = ctx->argv[1]; |
2247 | const char *record_id = ctx->argv[2]; | |
2248 | const char *column_name = ctx->argv[3]; | |
2249 | const struct vsctl_table_class *table; | |
2250 | const struct vsctl_column *column; | |
2251 | const struct ovsdb_idl_row *row; | |
2252 | const struct ovsdb_type *type; | |
2253 | struct ovsdb_datum old; | |
2254 | int i; | |
2255 | ||
2256 | table = get_table(table_name); | |
2257 | row = get_row(ctx, table, record_id); | |
2258 | die_if_error(get_column(table, column_name, &column)); | |
2259 | type = &column->idl->type; | |
2260 | ovsdb_idl_txn_read(row, column->idl, &old); | |
2261 | for (i = 4; i < ctx->argc; i++) { | |
2262 | struct ovsdb_type rm_type; | |
2263 | struct ovsdb_datum rm; | |
2264 | char *error; | |
2265 | ||
71bd6358 | 2266 | if (column->flags & VSCF_READONLY && !force) { |
90c4bd00 BP |
2267 | ovs_fatal(0, "%s: cannot modify read-only column %s in table %s", |
2268 | ctx->argv[i], column->idl->name, table_name); | |
2269 | } | |
2270 | ||
2271 | rm_type = *type; | |
2272 | rm_type.n_min = 1; | |
2273 | rm_type.n_max = UINT_MAX; | |
2274 | error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]); | |
2275 | if (error && ovsdb_type_is_map(&rm_type)) { | |
2276 | free(error); | |
2277 | rm_type.value_type = OVSDB_TYPE_VOID; | |
2278 | die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i])); | |
2279 | } | |
2280 | ovsdb_datum_subtract(&old, type, &rm, &rm_type); | |
2281 | ovsdb_datum_destroy(&rm, &rm_type); | |
2282 | } | |
2283 | if (old.n < type->n_min) { | |
2284 | ovs_fatal(0, "\"remove\" operation would put %u %s in column %s of " | |
2285 | "table %s but at least %u are required", | |
2286 | old.n, | |
2287 | type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs", | |
2288 | column->idl->name, table_name, type->n_min); | |
2289 | } | |
2290 | ovsdb_idl_txn_write(row, column->idl, &old); | |
2291 | } | |
2292 | ||
2293 | static void | |
2294 | cmd_clear(struct vsctl_context *ctx) | |
2295 | { | |
71bd6358 | 2296 | bool force = shash_find(&ctx->options, "--force"); |
90c4bd00 BP |
2297 | const char *table_name = ctx->argv[1]; |
2298 | const char *record_id = ctx->argv[2]; | |
2299 | const struct vsctl_table_class *table; | |
2300 | const struct ovsdb_idl_row *row; | |
2301 | int i; | |
2302 | ||
2303 | table = get_table(table_name); | |
2304 | row = get_row(ctx, table, record_id); | |
2305 | for (i = 3; i < ctx->argc; i++) { | |
2306 | const struct vsctl_column *column; | |
2307 | const struct ovsdb_type *type; | |
2308 | struct ovsdb_datum datum; | |
2309 | ||
2310 | die_if_error(get_column(table, ctx->argv[i], &column)); | |
2311 | ||
2312 | type = &column->idl->type; | |
71bd6358 | 2313 | if (column->flags & VSCF_READONLY && !force) { |
90c4bd00 BP |
2314 | ovs_fatal(0, "%s: cannot modify read-only column %s in table %s", |
2315 | ctx->argv[i], column->idl->name, table_name); | |
2316 | } else if (type->n_min > 0) { | |
2317 | ovs_fatal(0, "\"clear\" operation cannot be applied to column %s " | |
2318 | "of table %s, which is not allowed to be empty", | |
2319 | column->idl->name, table_name); | |
2320 | } | |
2321 | ||
2322 | ovsdb_datum_init_empty(&datum); | |
2323 | ovsdb_idl_txn_write(row, column->idl, &datum); | |
2324 | } | |
2325 | } | |
557e3718 BP |
2326 | |
2327 | static void | |
2328 | cmd_create(struct vsctl_context *ctx) | |
2329 | { | |
2330 | bool force = shash_find(&ctx->options, "--force"); | |
2331 | const char *table_name = ctx->argv[1]; | |
2332 | const struct vsctl_table_class *table; | |
2333 | const struct ovsdb_idl_row *row; | |
2334 | int i; | |
2335 | ||
2336 | if (!force) { | |
2337 | ovs_fatal(0, "\"create\" requires --force"); | |
2338 | } | |
2339 | ||
2340 | table = get_table(table_name); | |
2341 | row = ovsdb_idl_txn_insert(txn_from_openvswitch(ctx->ovs), table->class); | |
2342 | for (i = 2; i < ctx->argc; i++) { | |
2343 | set_column(table, row, ctx->argv[i], force); | |
2344 | } | |
2345 | } | |
ad83bfa6 | 2346 | \f |
5d9cb63c | 2347 | typedef void vsctl_handler_func(struct vsctl_context *); |
dfbe07ba | 2348 | |
c75d1511 BP |
2349 | struct vsctl_command { |
2350 | const char *name; | |
2351 | int min_args; | |
2352 | int max_args; | |
dfbe07ba | 2353 | vsctl_handler_func *handler; |
5d9cb63c | 2354 | const char *options; |
c75d1511 BP |
2355 | }; |
2356 | ||
2357 | static void run_vsctl_command(int argc, char *argv[], | |
ad83bfa6 BP |
2358 | const struct ovsrec_open_vswitch *, |
2359 | struct ovsdb_idl *, struct ds *output); | |
c75d1511 | 2360 | |
b54e22e9 BP |
2361 | static struct json * |
2362 | where_uuid_equals(const struct uuid *uuid) | |
2363 | { | |
2364 | return | |
2365 | json_array_create_1( | |
2366 | json_array_create_3( | |
2367 | json_string_create("_uuid"), | |
2368 | json_string_create("=="), | |
2369 | json_array_create_2( | |
2370 | json_string_create("uuid"), | |
2371 | json_string_create_nocopy( | |
2372 | xasprintf(UUID_FMT, UUID_ARGS(uuid)))))); | |
2373 | } | |
2374 | ||
c75d1511 BP |
2375 | static void |
2376 | do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl) | |
2377 | { | |
2378 | struct ovsdb_idl_txn *txn; | |
2379 | const struct ovsrec_open_vswitch *ovs; | |
2380 | enum ovsdb_idl_txn_status status; | |
d171b584 | 2381 | struct ds comment, *output; |
84a0ee89 | 2382 | int64_t next_cfg = 0; |
c75d1511 BP |
2383 | int n_output; |
2384 | int i, start; | |
2385 | ||
524555d1 | 2386 | txn = ovsdb_idl_txn_create(idl); |
577aebdf BP |
2387 | if (dry_run) { |
2388 | ovsdb_idl_txn_set_dry_run(txn); | |
2389 | } | |
524555d1 | 2390 | |
d171b584 BP |
2391 | ds_init(&comment); |
2392 | ds_put_cstr(&comment, "ovs-vsctl:"); | |
2393 | for (i = 0; i < argc; i++) { | |
2394 | ds_put_format(&comment, " %s", argv[i]); | |
2395 | } | |
2396 | ovsdb_idl_txn_add_comment(txn, ds_cstr(&comment)); | |
2397 | ds_destroy(&comment); | |
2398 | ||
c75d1511 BP |
2399 | ovs = ovsrec_open_vswitch_first(idl); |
2400 | if (!ovs) { | |
524555d1 BP |
2401 | /* XXX add verification that table is empty */ |
2402 | ovs = ovsrec_open_vswitch_insert(txn); | |
c75d1511 BP |
2403 | } |
2404 | ||
b54e22e9 BP |
2405 | if (wait_for_reload) { |
2406 | struct json *where = where_uuid_equals(&ovs->header_.uuid); | |
ad83bfa6 | 2407 | ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where); |
b54e22e9 BP |
2408 | json_destroy(where); |
2409 | } | |
2410 | ||
c75d1511 BP |
2411 | output = xmalloc(argc * sizeof *output); |
2412 | n_output = 0; | |
dfbe07ba BP |
2413 | for (start = i = 0; i <= argc; i++) { |
2414 | if (i == argc || !strcmp(argv[i], "--")) { | |
c75d1511 BP |
2415 | if (i > start) { |
2416 | ds_init(&output[n_output]); | |
ad83bfa6 | 2417 | run_vsctl_command(i - start, &argv[start], ovs, idl, |
c75d1511 BP |
2418 | &output[n_output++]); |
2419 | } | |
2420 | start = i + 1; | |
2421 | } | |
2422 | } | |
c75d1511 BP |
2423 | |
2424 | while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) { | |
2425 | ovsdb_idl_run(idl); | |
2426 | ovsdb_idl_wait(idl); | |
586bb84a | 2427 | ovsdb_idl_txn_wait(txn); |
c75d1511 BP |
2428 | poll_block(); |
2429 | } | |
b54e22e9 BP |
2430 | if (wait_for_reload && status == TXN_SUCCESS) { |
2431 | next_cfg = ovsdb_idl_txn_get_increment_new_value(txn); | |
2432 | } | |
c75d1511 BP |
2433 | ovsdb_idl_txn_destroy(txn); |
2434 | ||
2435 | switch (status) { | |
2436 | case TXN_INCOMPLETE: | |
2437 | NOT_REACHED(); | |
2438 | ||
2439 | case TXN_ABORTED: | |
2440 | /* Should not happen--we never call ovsdb_idl_txn_abort(). */ | |
c88b6a27 | 2441 | vsctl_fatal("transaction aborted"); |
c75d1511 | 2442 | |
b54e22e9 | 2443 | case TXN_UNCHANGED: |
c75d1511 BP |
2444 | case TXN_SUCCESS: |
2445 | break; | |
2446 | ||
2447 | case TXN_TRY_AGAIN: | |
2448 | for (i = 0; i < n_output; i++) { | |
2449 | ds_destroy(&output[i]); | |
2450 | } | |
2451 | return; | |
2452 | ||
2453 | case TXN_ERROR: | |
c88b6a27 | 2454 | vsctl_fatal("transaction error"); |
c75d1511 BP |
2455 | |
2456 | default: | |
2457 | NOT_REACHED(); | |
2458 | } | |
2459 | ||
2460 | for (i = 0; i < n_output; i++) { | |
2461 | struct ds *ds = &output[i]; | |
2462 | if (oneline) { | |
2463 | size_t j; | |
2464 | ||
2465 | ds_chomp(ds, '\n'); | |
2466 | for (j = 0; j < ds->length; j++) { | |
2467 | int c = ds->string[j]; | |
2468 | switch (c) { | |
2469 | case '\n': | |
2470 | fputs("\\n", stdout); | |
2471 | break; | |
2472 | ||
2473 | case '\\': | |
2474 | fputs("\\\\", stdout); | |
2475 | break; | |
2476 | ||
2477 | default: | |
2478 | putchar(c); | |
2479 | } | |
2480 | } | |
2481 | putchar('\n'); | |
2482 | } else { | |
2483 | fputs(ds_cstr(ds), stdout); | |
2484 | } | |
2485 | } | |
b54e22e9 BP |
2486 | |
2487 | if (wait_for_reload && status != TXN_UNCHANGED) { | |
2488 | for (;;) { | |
2489 | const struct ovsrec_open_vswitch *ovs; | |
2490 | ||
2491 | ovsdb_idl_run(idl); | |
2492 | OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) { | |
2493 | if (ovs->cur_cfg >= next_cfg) { | |
2494 | goto done; | |
2495 | } | |
2496 | } | |
2497 | ovsdb_idl_wait(idl); | |
2498 | poll_block(); | |
2499 | } | |
2500 | done: ; | |
2501 | } | |
2502 | ||
c75d1511 BP |
2503 | exit(EXIT_SUCCESS); |
2504 | } | |
2505 | ||
dfbe07ba | 2506 | static vsctl_handler_func * |
5d9cb63c | 2507 | get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx) |
c75d1511 BP |
2508 | { |
2509 | static const struct vsctl_command all_commands[] = { | |
524555d1 BP |
2510 | /* Open vSwitch commands. */ |
2511 | {"init", 0, 0, cmd_init, ""}, | |
2512 | ||
457e1eb0 | 2513 | /* Bridge commands. */ |
5d9cb63c | 2514 | {"add-br", 1, 3, cmd_add_br, ""}, |
460aad80 | 2515 | {"del-br", 1, 1, cmd_del_br, "--if-exists"}, |
5d9cb63c BP |
2516 | {"list-br", 0, 0, cmd_list_br, ""}, |
2517 | {"br-exists", 1, 1, cmd_br_exists, ""}, | |
2518 | {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""}, | |
2519 | {"br-to-parent", 1, 1, cmd_br_to_parent, ""}, | |
2520 | {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""}, | |
2521 | {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""}, | |
457e1eb0 BP |
2522 | |
2523 | /* Port commands. */ | |
5d9cb63c BP |
2524 | {"list-ports", 1, 1, cmd_list_ports, ""}, |
2525 | {"add-port", 2, 2, cmd_add_port, ""}, | |
b4182c7f | 2526 | {"add-bond", 4, INT_MAX, cmd_add_bond, "--fake-iface"}, |
460aad80 | 2527 | {"del-port", 1, 2, cmd_del_port, "--if-exists"}, |
5d9cb63c BP |
2528 | {"port-to-br", 1, 1, cmd_port_to_br, ""}, |
2529 | {"port-set-external-id", 2, 3, cmd_port_set_external_id, ""}, | |
2530 | {"port-get-external-id", 1, 2, cmd_port_get_external_id, ""}, | |
457e1eb0 BP |
2531 | |
2532 | /* Interface commands. */ | |
5d9cb63c BP |
2533 | {"list-ifaces", 1, 1, cmd_list_ifaces, ""}, |
2534 | {"iface-to-br", 1, 1, cmd_iface_to_br, ""}, | |
2535 | {"iface-set-external-id", 2, 3, cmd_iface_set_external_id, ""}, | |
2536 | {"iface-get-external-id", 1, 2, cmd_iface_get_external_id, ""}, | |
5aa00635 JP |
2537 | |
2538 | /* Controller commands. */ | |
2539 | {"get-controller", 0, 1, cmd_get_controller, ""}, | |
2540 | {"del-controller", 0, 1, cmd_del_controller, ""}, | |
2541 | {"set-controller", 1, 2, cmd_set_controller, ""}, | |
2542 | {"get-fail-mode", 0, 1, cmd_get_fail_mode, ""}, | |
2543 | {"del-fail-mode", 0, 1, cmd_del_fail_mode, ""}, | |
2544 | {"set-fail-mode", 1, 2, cmd_set_fail_mode, ""}, | |
dd8ac6fe JP |
2545 | |
2546 | /* SSL commands. */ | |
2547 | {"get-ssl", 0, 0, cmd_get_ssl, ""}, | |
2548 | {"del-ssl", 0, 0, cmd_del_ssl, ""}, | |
2549 | {"set-ssl", 3, 3, cmd_set_ssl, "--bootstrap"}, | |
ad83bfa6 BP |
2550 | |
2551 | /* Parameter commands. */ | |
2552 | {"get", 3, INT_MAX, cmd_get, ""}, | |
2553 | {"list", 1, INT_MAX, cmd_list, ""}, | |
71bd6358 BP |
2554 | {"set", 3, INT_MAX, cmd_set, "--force"}, |
2555 | {"add", 4, INT_MAX, cmd_add, "--force"}, | |
2556 | {"remove", 4, INT_MAX, cmd_remove, "--force"}, | |
2557 | {"clear", 3, INT_MAX, cmd_clear, "--force"}, | |
557e3718 | 2558 | {"create", 2, INT_MAX, cmd_create, "--force"}, |
c75d1511 BP |
2559 | }; |
2560 | ||
2561 | const struct vsctl_command *p; | |
5d9cb63c BP |
2562 | int i; |
2563 | ||
2564 | shash_init(&ctx->options); | |
2565 | for (i = 0; i < argc; i++) { | |
2566 | if (argv[i][0] != '-') { | |
2567 | break; | |
2568 | } | |
2569 | if (!shash_add_once(&ctx->options, argv[i], NULL)) { | |
c88b6a27 | 2570 | vsctl_fatal("'%s' option specified multiple times", argv[i]); |
5d9cb63c BP |
2571 | } |
2572 | } | |
2573 | if (i == argc) { | |
c88b6a27 | 2574 | vsctl_fatal("missing command name"); |
5d9cb63c | 2575 | } |
c75d1511 | 2576 | |
dfbe07ba | 2577 | for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) { |
5d9cb63c BP |
2578 | if (!strcmp(p->name, argv[i])) { |
2579 | struct shash_node *node; | |
2580 | int n_arg; | |
2581 | ||
2582 | SHASH_FOR_EACH (node, &ctx->options) { | |
2583 | const char *s = strstr(p->options, node->name); | |
2584 | int end = s ? s[strlen(node->name)] : EOF; | |
2585 | if (end != ',' && end != ' ' && end != '\0') { | |
c88b6a27 BP |
2586 | vsctl_fatal("'%s' command has no '%s' option", |
2587 | argv[i], node->name); | |
5d9cb63c BP |
2588 | } |
2589 | } | |
2590 | ||
2591 | n_arg = argc - i - 1; | |
c75d1511 | 2592 | if (n_arg < p->min_args) { |
c88b6a27 BP |
2593 | vsctl_fatal("'%s' command requires at least %d arguments", |
2594 | p->name, p->min_args); | |
c75d1511 | 2595 | } else if (n_arg > p->max_args) { |
c88b6a27 BP |
2596 | vsctl_fatal("'%s' command takes at most %d arguments", |
2597 | p->name, p->max_args); | |
c75d1511 | 2598 | } else { |
5d9cb63c BP |
2599 | ctx->argc = n_arg + 1; |
2600 | ctx->argv = &argv[i]; | |
dfbe07ba | 2601 | return p->handler; |
c75d1511 BP |
2602 | } |
2603 | } | |
2604 | } | |
2605 | ||
c88b6a27 | 2606 | vsctl_fatal("unknown command '%s'; use --help for help", argv[i]); |
c75d1511 | 2607 | } |
dfbe07ba BP |
2608 | |
2609 | static void | |
2610 | check_vsctl_command(int argc, char *argv[]) | |
2611 | { | |
5d9cb63c BP |
2612 | struct vsctl_context ctx; |
2613 | ||
2614 | get_vsctl_handler(argc, argv, &ctx); | |
2615 | shash_destroy(&ctx.options); | |
dfbe07ba BP |
2616 | } |
2617 | ||
2618 | static void | |
2619 | run_vsctl_command(int argc, char *argv[], | |
ad83bfa6 BP |
2620 | const struct ovsrec_open_vswitch *ovs, |
2621 | struct ovsdb_idl *idl, struct ds *output) | |
dfbe07ba | 2622 | { |
5d9cb63c BP |
2623 | vsctl_handler_func *function; |
2624 | struct vsctl_context ctx; | |
2625 | ||
2626 | function = get_vsctl_handler(argc, argv, &ctx); | |
2627 | ctx.ovs = ovs; | |
ad83bfa6 | 2628 | ctx.idl = idl; |
5d9cb63c BP |
2629 | ds_init(&ctx.output); |
2630 | function(&ctx); | |
2631 | *output = ctx.output; | |
2632 | shash_destroy(&ctx.options); | |
dfbe07ba | 2633 | } |