]> git.proxmox.com Git - mirror_ovs.git/blame - ovn/utilities/ovn-sbctl.c
lib: Move lib/poll-loop.h to include/openvswitch
[mirror_ovs.git] / ovn / utilities / ovn-sbctl.c
CommitLineData
fed00ab1 1/*
3f5b5f7b 2 * Copyright (c) 2015, 2016, 2017 Nicira, Inc.
fed00ab1
AW
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 <ctype.h>
20#include <errno.h>
21#include <float.h>
22#include <getopt.h>
23#include <inttypes.h>
24#include <signal.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
c2f4c39b 30#include "colors.h"
fed00ab1
AW
31#include "command-line.h"
32#include "compiler.h"
8ad609c0
BP
33#include "db-ctl-base.h"
34#include "dirs.h"
fed00ab1 35#include "fatal-signal.h"
8ad609c0 36#include "openvswitch/dynamic-string.h"
ee89ea7b 37#include "openvswitch/json.h"
c2f4c39b
BP
38#include "openvswitch/ofp-actions.h"
39#include "openvswitch/ofp-print.h"
8ad609c0 40#include "openvswitch/shash.h"
c2f4c39b 41#include "openvswitch/vconn.h"
8ad609c0
BP
42#include "openvswitch/vlog.h"
43#include "ovn/lib/ovn-sb-idl.h"
44#include "ovn/lib/ovn-util.h"
fed00ab1
AW
45#include "ovsdb-data.h"
46#include "ovsdb-idl.h"
fd016ae3 47#include "openvswitch/poll-loop.h"
fed00ab1
AW
48#include "process.h"
49#include "sset.h"
dda825b8 50#include "stream-ssl.h"
66cf8908 51#include "stream.h"
fed00ab1
AW
52#include "table.h"
53#include "timeval.h"
54#include "util.h"
10471820 55#include "svec.h"
fed00ab1
AW
56
57VLOG_DEFINE_THIS_MODULE(sbctl);
58
59struct sbctl_context;
60
61/* --db: The database server to contact. */
62static const char *db;
63
64/* --oneline: Write each command's output as a single line? */
65static bool oneline;
66
67/* --dry-run: Do not commit any changes. */
68static bool dry_run;
69
70/* --timeout: Time to wait for a connection to 'db'. */
71static int timeout;
72
73/* Format for table output. */
74static struct table_style table_style = TABLE_STYLE_DEFAULT;
75
76/* The IDL we're using and the current transaction, if any.
77 * This is for use by sbctl_exit() only, to allow it to clean up.
78 * Other code should use its context arguments. */
79static struct ovsdb_idl *the_idl;
80static struct ovsdb_idl_txn *the_idl_txn;
81OVS_NO_RETURN static void sbctl_exit(int status);
82
83static void sbctl_cmd_init(void);
84OVS_NO_RETURN static void usage(void);
85static void parse_options(int argc, char *argv[], struct shash *local_options);
86static void run_prerequisites(struct ctl_command[], size_t n_commands,
87 struct ovsdb_idl *);
8ee574c3 88static bool do_sbctl(const char *args, struct ctl_command *, size_t n,
fed00ab1
AW
89 struct ovsdb_idl *);
90
91int
92main(int argc, char *argv[])
93{
fed00ab1
AW
94 struct ovsdb_idl *idl;
95 struct ctl_command *commands;
96 struct shash local_options;
97 unsigned int seqno;
98 size_t n_commands;
99 char *args;
100
101 set_program_name(argv[0]);
102 fatal_ignore_sigpipe();
103 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
45863ce5 104 vlog_set_levels_from_string_assert("reconnect:warn");
fed00ab1
AW
105
106 sbctl_cmd_init();
107
108 /* Log our arguments. This is often valuable for debugging systems. */
109 args = process_escape_args(argv);
110 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
111
112 /* Parse command line. */
113 shash_init(&local_options);
114 parse_options(argc, argv, &local_options);
115 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
116 &n_commands);
117
118 if (timeout) {
119 time_alarm(timeout);
120 }
121
122 /* Initialize IDL. */
123 idl = the_idl = ovsdb_idl_create(db, &sbrec_idl_class, false, false);
124 run_prerequisites(commands, n_commands, idl);
125
126 /* Execute the commands.
127 *
128 * 'seqno' is the database sequence number for which we last tried to
129 * execute our transaction. There's no point in trying to commit more than
130 * once for any given sequence number, because if the transaction fails
131 * it's because the database changed and we need to obtain an up-to-date
132 * view of the database before we try the transaction again. */
133 seqno = ovsdb_idl_get_seqno(idl);
134 for (;;) {
135 ovsdb_idl_run(idl);
136 if (!ovsdb_idl_is_alive(idl)) {
137 int retval = ovsdb_idl_get_last_error(idl);
138 ctl_fatal("%s: database connection failed (%s)",
139 db, ovs_retval_to_string(retval));
140 }
141
142 if (seqno != ovsdb_idl_get_seqno(idl)) {
143 seqno = ovsdb_idl_get_seqno(idl);
8ee574c3
WT
144 if (do_sbctl(args, commands, n_commands, idl)) {
145 free(args);
146 exit(EXIT_SUCCESS);
147 }
fed00ab1
AW
148 }
149
150 if (seqno == ovsdb_idl_get_seqno(idl)) {
151 ovsdb_idl_wait(idl);
152 poll_block();
153 }
154 }
155}
156
157static void
158parse_options(int argc, char *argv[], struct shash *local_options)
159{
160 enum {
161 OPT_DB = UCHAR_MAX + 1,
162 OPT_ONELINE,
163 OPT_NO_SYSLOG,
164 OPT_DRY_RUN,
fed00ab1
AW
165 OPT_LOCAL,
166 OPT_COMMANDS,
167 OPT_OPTIONS,
168 VLOG_OPTION_ENUMS,
e18a1d08
ER
169 TABLE_OPTION_ENUMS,
170 SSL_OPTION_ENUMS,
fed00ab1
AW
171 };
172 static const struct option global_long_options[] = {
173 {"db", required_argument, NULL, OPT_DB},
174 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
175 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
176 {"oneline", no_argument, NULL, OPT_ONELINE},
177 {"timeout", required_argument, NULL, 't'},
178 {"help", no_argument, NULL, 'h'},
179 {"commands", no_argument, NULL, OPT_COMMANDS},
180 {"options", no_argument, NULL, OPT_OPTIONS},
181 {"version", no_argument, NULL, 'V'},
182 VLOG_LONG_OPTIONS,
dda825b8 183 STREAM_SSL_LONG_OPTIONS,
fed00ab1
AW
184 TABLE_LONG_OPTIONS,
185 {NULL, 0, NULL, 0},
186 };
187 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
188 char *tmp, *short_options;
189
190 struct option *options;
191 size_t allocated_options;
192 size_t n_options;
193 size_t i;
194
195 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
196 short_options = xasprintf("+%s", tmp);
197 free(tmp);
198
199 /* We want to parse both global and command-specific options here, but
200 * getopt_long() isn't too convenient for the job. We copy our global
201 * options into a dynamic array, then append all of the command-specific
202 * options. */
203 options = xmemdup(global_long_options, sizeof global_long_options);
204 allocated_options = ARRAY_SIZE(global_long_options);
205 n_options = n_global_long_options;
206 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
fed00ab1
AW
207
208 for (;;) {
209 int idx;
210 int c;
211
212 c = getopt_long(argc, argv, short_options, options, &idx);
213 if (c == -1) {
214 break;
215 }
216
217 switch (c) {
218 case OPT_DB:
219 db = optarg;
220 break;
221
222 case OPT_ONELINE:
223 oneline = true;
224 break;
225
226 case OPT_NO_SYSLOG:
922fed06 227 vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
fed00ab1
AW
228 break;
229
230 case OPT_DRY_RUN:
231 dry_run = true;
232 break;
233
234 case OPT_LOCAL:
235 if (shash_find(local_options, options[idx].name)) {
236 ctl_fatal("'%s' option specified multiple times",
237 options[idx].name);
238 }
239 shash_add_nocopy(local_options,
240 xasprintf("--%s", options[idx].name),
2225c0b9 241 nullable_xstrdup(optarg));
fed00ab1
AW
242 break;
243
244 case 'h':
245 usage();
246
247 case OPT_COMMANDS:
248 ctl_print_commands();
73c7216a 249 /* fall through */
fed00ab1
AW
250
251 case OPT_OPTIONS:
252 ctl_print_options(global_long_options);
73c7216a 253 /* fall through */
fed00ab1
AW
254
255 case 'V':
256 ovs_print_version(0, 0);
257 printf("DB Schema %s\n", sbrec_get_db_version());
258 exit(EXIT_SUCCESS);
259
260 case 't':
261 timeout = strtoul(optarg, NULL, 10);
262 if (timeout < 0) {
b31301bc 263 ctl_fatal("value %s on -t or --timeout is invalid", optarg);
fed00ab1
AW
264 }
265 break;
266
267 VLOG_OPTION_HANDLERS
268 TABLE_OPTION_HANDLERS(&table_style)
dda825b8 269 STREAM_SSL_OPTION_HANDLERS
fed00ab1
AW
270
271 case '?':
272 exit(EXIT_FAILURE);
273
274 default:
275 abort();
276 }
277 }
278 free(short_options);
279
280 if (!db) {
8ad609c0 281 db = default_sb_db();
fed00ab1
AW
282 }
283
284 for (i = n_global_long_options; options[i].name; i++) {
285 free(CONST_CAST(char *, options[i].name));
286 }
287 free(options);
288}
289
290static void
291usage(void)
292{
293 printf("\
66cf8908 294%s: OVN southbound DB management utility\n\
fed00ab1 295\n\
fed00ab1
AW
296usage: %s [OPTIONS] COMMAND [ARG...]\n\
297\n\
66cf8908 298General commands:\n\
fed00ab1
AW
299 show print overview of database contents\n\
300\n\
301Chassis commands:\n\
302 chassis-add CHASSIS ENCAP-TYPE ENCAP-IP create a new chassis named\n\
3c653533
JP
303 CHASSIS with ENCAP-TYPE tunnels\n\
304 and ENCAP-IP\n\
305 chassis-del CHASSIS delete CHASSIS and all of its encaps\n\
fed00ab1
AW
306 and gateway_ports\n\
307\n\
308Port binding commands:\n\
f0f96ba8
RB
309 lsp-bind PORT CHASSIS bind logical port PORT to CHASSIS\n\
310 lsp-unbind PORT reset the port binding of logical port PORT\n\
fed00ab1 311\n\
dc70b67b 312Logical flow commands:\n\
c80eac1f
BP
313 lflow-list [DATAPATH] [LFLOW...] List logical flows for DATAPATH\n\
314 dump-flows [DATAPATH] [LFLOW...] Alias for lflow-list\n\
dc70b67b 315\n\
10471820
LR
316Connection commands:\n\
317 get-connection print the connections\n\
318 del-connection delete the connections\n\
319 set-connection TARGET... set the list of connections to TARGET...\n\
320\n\
321SSL commands:\n\
322 get-ssl print the SSL configuration\n\
323 del-ssl delete the SSL configuration\n\
51af591b
LR
324 set-ssl PRIV-KEY CERT CA-CERT [SSL-PROTOS [SSL-CIPHERS]] \
325set the SSL configuration\n\
10471820 326\n\
fed00ab1
AW
327%s\
328\n\
329Options:\n\
330 --db=DATABASE connect to DATABASE\n\
331 (default: %s)\n\
66cf8908 332 -t, --timeout=SECS wait at most SECS seconds\n\
fed00ab1
AW
333 --dry-run do not commit changes to database\n\
334 --oneline print exactly one line of output per command\n",
8ad609c0
BP
335 program_name, program_name, ctl_get_db_cmd_usage(),
336 default_sb_db());
bcb58ce0 337 table_usage();
fed00ab1
AW
338 vlog_usage();
339 printf("\
340 --no-syslog equivalent to --verbose=sbctl:syslog:warn\n");
341 printf("\n\
342Other options:\n\
343 -h, --help display this help message\n\
344 -V, --version display version information\n");
66cf8908 345 stream_usage("database", true, true, false);
fed00ab1
AW
346 exit(EXIT_SUCCESS);
347}
348
349\f
350/* ovs-sbctl specific context. Inherits the 'struct ctl_context' as base. */
351struct sbctl_context {
352 struct ctl_context base;
353
354 /* A cache of the contents of the database.
355 *
356 * A command that needs to use any of this information must first call
357 * sbctl_context_populate_cache(). A command that changes anything that
358 * could invalidate the cache must either call
359 * sbctl_context_invalidate_cache() or manually update the cache to
360 * maintain its correctness. */
361 bool cache_valid;
362 /* Maps from chassis name to struct sbctl_chassis. */
363 struct shash chassis;
364 /* Maps from lport name to struct sbctl_port_binding. */
365 struct shash port_bindings;
366};
367
ec4eed45 368/* Casts 'base' into 'struct sbctl_context'. */
fed00ab1
AW
369static struct sbctl_context *
370sbctl_context_cast(struct ctl_context *base)
371{
372 return CONTAINER_OF(base, struct sbctl_context, base);
373}
374
375struct sbctl_chassis {
376 const struct sbrec_chassis *ch_cfg;
377};
378
379struct sbctl_port_binding {
380 const struct sbrec_port_binding *bd_cfg;
381};
382
383static void
384sbctl_context_invalidate_cache(struct ctl_context *ctx)
385{
386 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
387
388 if (!sbctl_ctx->cache_valid) {
389 return;
390 }
391 sbctl_ctx->cache_valid = false;
392 shash_destroy_free_data(&sbctl_ctx->chassis);
393 shash_destroy_free_data(&sbctl_ctx->port_bindings);
394}
395
396static void
397sbctl_context_populate_cache(struct ctl_context *ctx)
398{
399 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
400 const struct sbrec_chassis *chassis_rec;
401 const struct sbrec_port_binding *port_binding_rec;
402 struct sset chassis, port_bindings;
403
404 if (sbctl_ctx->cache_valid) {
405 /* Cache is already populated. */
406 return;
407 }
408 sbctl_ctx->cache_valid = true;
409 shash_init(&sbctl_ctx->chassis);
410 shash_init(&sbctl_ctx->port_bindings);
411 sset_init(&chassis);
412 SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->idl) {
413 struct sbctl_chassis *ch;
414
415 if (!sset_add(&chassis, chassis_rec->name)) {
416 VLOG_WARN("database contains duplicate chassis name (%s)",
417 chassis_rec->name);
418 continue;
419 }
420
421 ch = xmalloc(sizeof *ch);
422 ch->ch_cfg = chassis_rec;
423 shash_add(&sbctl_ctx->chassis, chassis_rec->name, ch);
424 }
425 sset_destroy(&chassis);
426
427 sset_init(&port_bindings);
428 SBREC_PORT_BINDING_FOR_EACH(port_binding_rec, ctx->idl) {
429 struct sbctl_port_binding *bd;
430
431 if (!sset_add(&port_bindings, port_binding_rec->logical_port)) {
432 VLOG_WARN("database contains duplicate port binding for logical "
433 "port (%s)",
434 port_binding_rec->logical_port);
435 continue;
436 }
437
438 bd = xmalloc(sizeof *bd);
439 bd->bd_cfg = port_binding_rec;
440 shash_add(&sbctl_ctx->port_bindings, port_binding_rec->logical_port,
441 bd);
442 }
443 sset_destroy(&port_bindings);
444}
445
446static void
447check_conflicts(struct sbctl_context *sbctl_ctx, const char *name,
448 char *msg)
449{
450 if (shash_find(&sbctl_ctx->chassis, name)) {
451 ctl_fatal("%s because a chassis named %s already exists",
452 msg, name);
453 }
454 free(msg);
455}
456
457static struct sbctl_chassis *
458find_chassis(struct sbctl_context *sbctl_ctx, const char *name,
459 bool must_exist)
460{
461 struct sbctl_chassis *sbctl_ch;
462
463 ovs_assert(sbctl_ctx->cache_valid);
464
465 sbctl_ch = shash_find_data(&sbctl_ctx->chassis, name);
466 if (must_exist && !sbctl_ch) {
467 ctl_fatal("no chassis named %s", name);
468 }
469
470 return sbctl_ch;
471}
472
473static struct sbctl_port_binding *
474find_port_binding(struct sbctl_context *sbctl_ctx, const char *name,
475 bool must_exist)
476{
477 struct sbctl_port_binding *bd;
478
479 ovs_assert(sbctl_ctx->cache_valid);
480
481 bd = shash_find_data(&sbctl_ctx->port_bindings, name);
482 if (must_exist && !bd) {
483 ctl_fatal("no port named %s", name);
484 }
485
486 return bd;
487}
488
489static void
490pre_get_info(struct ctl_context *ctx)
491{
492 ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_col_name);
493 ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_col_encaps);
494
495 ovsdb_idl_add_column(ctx->idl, &sbrec_encap_col_type);
496 ovsdb_idl_add_column(ctx->idl, &sbrec_encap_col_ip);
497
498 ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_logical_port);
499 ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_chassis);
dc70b67b
RB
500
501 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_logical_datapath);
502 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_pipeline);
503 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_actions);
504 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_priority);
505 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_table_id);
506 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_match);
ebbcddeb 507 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_external_ids);
0f8e9c12
BP
508
509 ovsdb_idl_add_column(ctx->idl, &sbrec_datapath_binding_col_external_ids);
fed00ab1
AW
510}
511
512static struct cmd_show_table cmd_show_tables[] = {
513 {&sbrec_table_chassis,
514 &sbrec_chassis_col_name,
2229f3ec
RB
515 {&sbrec_chassis_col_hostname,
516 &sbrec_chassis_col_encaps,
016e4684
AW
517 NULL},
518 {&sbrec_table_port_binding,
519 &sbrec_port_binding_col_logical_port,
520 &sbrec_port_binding_col_chassis}},
fed00ab1
AW
521
522 {&sbrec_table_encap,
523 &sbrec_encap_col_type,
524 {&sbrec_encap_col_ip,
525 &sbrec_encap_col_options,
016e4684
AW
526 NULL},
527 {NULL, NULL, NULL}},
fed00ab1 528
016e4684 529 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}},
fed00ab1
AW
530};
531
fa183acc
BP
532static void
533sbctl_init(struct ctl_context *ctx OVS_UNUSED)
534{
535}
536
fed00ab1
AW
537static void
538cmd_chassis_add(struct ctl_context *ctx)
539{
540 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
fed00ab1 541 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
3c653533 542 const char *ch_name, *encap_types, *encap_ip;
fed00ab1
AW
543
544 ch_name = ctx->argv[1];
3c653533 545 encap_types = ctx->argv[2];
fed00ab1
AW
546 encap_ip = ctx->argv[3];
547
548 sbctl_context_populate_cache(ctx);
549 if (may_exist) {
550 struct sbctl_chassis *sbctl_ch;
551
552 sbctl_ch = find_chassis(sbctl_ctx, ch_name, false);
553 if (sbctl_ch) {
554 return;
555 }
556 }
557 check_conflicts(sbctl_ctx, ch_name,
558 xasprintf("cannot create a chassis named %s", ch_name));
3c653533 559
63a10e1e
BP
560 struct sset encap_set;
561 sset_from_delimited_string(&encap_set, encap_types, ",");
3c653533
JP
562
563 size_t n_encaps = sset_count(&encap_set);
564 struct sbrec_encap **encaps = xmalloc(n_encaps * sizeof *encaps);
36283d78 565 const struct smap options = SMAP_CONST1(&options, "csum", "true");
3c653533
JP
566 const char *encap_type;
567 int i = 0;
568 SSET_FOR_EACH (encap_type, &encap_set){
569 encaps[i] = sbrec_encap_insert(ctx->txn);
570
571 sbrec_encap_set_type(encaps[i], encap_type);
572 sbrec_encap_set_ip(encaps[i], encap_ip);
36283d78 573 sbrec_encap_set_options(encaps[i], &options);
af0e9721 574 sbrec_encap_set_chassis_name(encaps[i], ch_name);
3c653533
JP
575 i++;
576 }
577 sset_destroy(&encap_set);
578
579 struct sbrec_chassis *ch = sbrec_chassis_insert(ctx->txn);
fed00ab1 580 sbrec_chassis_set_name(ch, ch_name);
3c653533
JP
581 sbrec_chassis_set_encaps(ch, encaps, n_encaps);
582 free(encaps);
583
fed00ab1
AW
584 sbctl_context_invalidate_cache(ctx);
585}
586
587static void
588cmd_chassis_del(struct ctl_context *ctx)
589{
590 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
591 bool must_exist = !shash_find(&ctx->options, "--if-exists");
592 struct sbctl_chassis *sbctl_ch;
593
594 sbctl_context_populate_cache(ctx);
595 sbctl_ch = find_chassis(sbctl_ctx, ctx->argv[1], must_exist);
596 if (sbctl_ch) {
597 if (sbctl_ch->ch_cfg) {
3b74b8c0
AW
598 size_t i;
599
600 for (i = 0; i < sbctl_ch->ch_cfg->n_encaps; i++) {
601 sbrec_encap_delete(sbctl_ch->ch_cfg->encaps[i]);
602 }
fed00ab1
AW
603 sbrec_chassis_delete(sbctl_ch->ch_cfg);
604 }
605 shash_find_and_delete(&sbctl_ctx->chassis, ctx->argv[1]);
606 free(sbctl_ch);
607 }
608}
609
610static void
f0f96ba8 611cmd_lsp_bind(struct ctl_context *ctx)
fed00ab1
AW
612{
613 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
614 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
615 struct sbctl_chassis *sbctl_ch;
616 struct sbctl_port_binding *sbctl_bd;
617 char *lport_name, *ch_name;
618
619 /* port_binding must exist, chassis must exist! */
620 lport_name = ctx->argv[1];
621 ch_name = ctx->argv[2];
622
623 sbctl_context_populate_cache(ctx);
624 sbctl_bd = find_port_binding(sbctl_ctx, lport_name, true);
625 sbctl_ch = find_chassis(sbctl_ctx, ch_name, true);
626
627 if (sbctl_bd->bd_cfg->chassis) {
628 if (may_exist && sbctl_bd->bd_cfg->chassis == sbctl_ch->ch_cfg) {
629 return;
630 } else {
631 ctl_fatal("lport (%s) has already been binded to chassis (%s)",
632 lport_name, sbctl_bd->bd_cfg->chassis->name);
633 }
634 }
635 sbrec_port_binding_set_chassis(sbctl_bd->bd_cfg, sbctl_ch->ch_cfg);
636 sbctl_context_invalidate_cache(ctx);
637}
638
639static void
f0f96ba8 640cmd_lsp_unbind(struct ctl_context *ctx)
fed00ab1
AW
641{
642 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
643 bool must_exist = !shash_find(&ctx->options, "--if-exists");
644 struct sbctl_port_binding *sbctl_bd;
645 char *lport_name;
646
647 lport_name = ctx->argv[1];
648 sbctl_context_populate_cache(ctx);
649 sbctl_bd = find_port_binding(sbctl_ctx, lport_name, must_exist);
650 if (sbctl_bd) {
651 sbrec_port_binding_set_chassis(sbctl_bd->bd_cfg, NULL);
652 }
653}
654
dc70b67b
RB
655enum {
656 PL_INGRESS,
657 PL_EGRESS,
658};
659
660/* Help ensure we catch any future pipeline values */
661static int
662pipeline_encode(const char *pl)
663{
664 if (!strcmp(pl, "ingress")) {
665 return PL_INGRESS;
666 } else if (!strcmp(pl, "egress")) {
667 return PL_EGRESS;
668 }
669
670 OVS_NOT_REACHED();
671}
672
673static int
674lflow_cmp(const void *lf1_, const void *lf2_)
675{
35107449
BP
676 const struct sbrec_logical_flow *const *lf1p = lf1_;
677 const struct sbrec_logical_flow *const *lf2p = lf2_;
678 const struct sbrec_logical_flow *lf1 = *lf1p;
679 const struct sbrec_logical_flow *lf2 = *lf2p;
dc70b67b
RB
680
681 int pl1 = pipeline_encode(lf1->pipeline);
682 int pl2 = pipeline_encode(lf2->pipeline);
683
684#define CMP(expr) \
685 do { \
686 int res; \
687 res = (expr); \
688 if (res) { \
689 return res; \
690 } \
691 } while (0)
692
693 CMP(uuid_compare_3way(&lf1->logical_datapath->header_.uuid,
694 &lf2->logical_datapath->header_.uuid));
695 CMP(pl1 - pl2);
696 CMP(lf1->table_id > lf2->table_id ? 1 :
697 (lf1->table_id < lf2->table_id ? -1 : 0));
698 CMP(lf1->priority > lf2->priority ? -1 :
699 (lf1->priority < lf2->priority ? 1 : 0));
700 CMP(strcmp(lf1->match, lf2->match));
701
702#undef CMP
703
704 return 0;
705}
706
c80eac1f
BP
707static char *
708parse_partial_uuid(char *s)
709{
710 /* Accept a full or partial UUID. */
2052f3a1 711 if (uuid_is_partial_string(s)) {
c80eac1f
BP
712 return s;
713 }
714
715 /* Accept a full or partial UUID prefixed by 0x, since "ovs-ofctl
716 * dump-flows" prints cookies prefixed by 0x. */
717 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')
2052f3a1 718 && uuid_is_partial_string(s + 2)) {
c80eac1f
BP
719 return s + 2;
720 }
721
722 /* Not a (partial) UUID. */
723 return NULL;
724}
725
8b8d09dc
HZ
726static const char *
727strip_leading_zero(const char *s)
728{
729 return s + strspn(s, "0");
730}
731
c80eac1f
BP
732static bool
733is_partial_uuid_match(const struct uuid *uuid, const char *match)
734{
735 char uuid_s[UUID_LEN + 1];
736 snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
737
4e3000a0
BP
738 /* We strip leading zeros because we want to accept cookie values derived
739 * from UUIDs, and cookie values are printed without leading zeros because
740 * they're just numbers. */
8b8d09dc
HZ
741 const char *s1 = strip_leading_zero(uuid_s);
742 const char *s2 = strip_leading_zero(match);
743
744 return !strncmp(s1, s2, strlen(s2));
c80eac1f
BP
745}
746
c2f4c39b
BP
747static char *
748default_ovs(void)
749{
750 return xasprintf("unix:%s/br-int.mgmt", ovs_rundir());
751}
752
753static struct vconn *
754sbctl_open_vconn(struct shash *options)
755{
756 struct shash_node *ovs = shash_find(options, "--ovs");
757 if (!ovs) {
758 return NULL;
759 }
760
761 char *remote = ovs->data ? xstrdup(ovs->data) : default_ovs();
762 struct vconn *vconn;
763 int retval = vconn_open_block(remote, 1 << OFP13_VERSION, 0, &vconn);
764 if (retval) {
765 VLOG_WARN("%s: connection failed (%s)", remote, ovs_strerror(retval));
766 }
767 free(remote);
768 return vconn;
769}
770
771static void
772sbctl_dump_openflow(struct vconn *vconn, const struct uuid *uuid, bool stats)
773{
774 struct ofputil_flow_stats_request fsr = {
775 .cookie = htonll(uuid->parts[0]),
776 .cookie_mask = OVS_BE64_MAX,
777 .out_port = OFPP_ANY,
778 .out_group = OFPG_ANY,
779 .table_id = OFPTT_ALL,
780 };
781
782 struct ofputil_flow_stats *fses;
783 size_t n_fses;
784 int error = vconn_dump_flows(vconn, &fsr, OFPUTIL_P_OF13_OXM,
785 &fses, &n_fses);
786 if (error) {
787 VLOG_WARN("%s: error obtaining flow stats (%s)",
788 vconn_get_name(vconn), ovs_strerror(error));
789 return;
790 }
791
792 if (n_fses) {
793 struct ds s = DS_EMPTY_INITIALIZER;
794 for (size_t i = 0; i < n_fses; i++) {
795 const struct ofputil_flow_stats *fs = &fses[i];
796
797 ds_clear(&s);
798 if (stats) {
1b3758c3 799 ofp_print_flow_stats(&s, fs, NULL, true);
c2f4c39b 800 } else {
1b3758c3 801 ds_put_format(&s, "%stable=%s%"PRIu8" ",
c2f4c39b 802 colors.special, colors.end, fs->table_id);
50f96b10 803 match_format(&fs->match, NULL, &s, OFP_DEFAULT_PRIORITY);
c2f4c39b
BP
804 if (ds_last(&s) != ' ') {
805 ds_put_char(&s, ' ');
806 }
807
808 ds_put_format(&s, "%sactions=%s", colors.actions, colors.end);
50f96b10 809 ofpacts_format(fs->ofpacts, fs->ofpacts_len, NULL, &s);
c2f4c39b 810 }
1b3758c3 811 printf(" %s\n", ds_cstr(&s));
c2f4c39b
BP
812 }
813 ds_destroy(&s);
814 }
815
816 for (size_t i = 0; i < n_fses; i++) {
817 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
818 }
819 free(fses);
820}
821
dc70b67b
RB
822static void
823cmd_lflow_list(struct ctl_context *ctx)
824{
c80eac1f
BP
825 const struct sbrec_datapath_binding *datapath = NULL;
826 if (ctx->argc > 1) {
bed7aef9
BP
827 datapath = (const struct sbrec_datapath_binding *)
828 ctl_get_row(ctx, &sbrec_table_datapath_binding,
829 ctx->argv[1], false);
c80eac1f
BP
830 if (datapath) {
831 ctx->argc--;
832 ctx->argv++;
833 }
834 }
dc70b67b 835
c80eac1f
BP
836 for (size_t i = 1; i < ctx->argc; i++) {
837 char *s = parse_partial_uuid(ctx->argv[i]);
838 if (!s) {
839 ctl_fatal("%s is not a UUID or the beginning of a UUID",
840 ctx->argv[i]);
841 }
842 ctx->argv[i] = s;
dc70b67b
RB
843 }
844
c2f4c39b
BP
845 struct vconn *vconn = sbctl_open_vconn(&ctx->options);
846 bool stats = shash_find(&ctx->options, "--stats") != NULL;
847
c80eac1f
BP
848 const struct sbrec_logical_flow **lflows = NULL;
849 size_t n_flows = 0;
850 size_t n_capacity = 0;
851 const struct sbrec_logical_flow *lflow;
dc70b67b 852 SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->idl) {
c80eac1f
BP
853 if (datapath && lflow->logical_datapath != datapath) {
854 continue;
855 }
856
dc70b67b
RB
857 if (n_flows == n_capacity) {
858 lflows = x2nrealloc(lflows, &n_capacity, sizeof *lflows);
859 }
860 lflows[n_flows] = lflow;
861 n_flows++;
862 }
65e907ab
WT
863
864 if (n_flows) {
865 qsort(lflows, n_flows, sizeof *lflows, lflow_cmp);
866 }
dc70b67b 867
c80eac1f
BP
868 bool print_uuid = shash_find(&ctx->options, "--uuid") != NULL;
869
870 const struct sbrec_logical_flow *prev = NULL;
871 for (size_t i = 0; i < n_flows; i++) {
dc70b67b 872 lflow = lflows[i];
c80eac1f
BP
873
874 /* Figure out whether to print this particular flow. By default, we
875 * print all flows, but if any UUIDs were listed on the command line
876 * then we only print the matching ones. */
877 bool include;
878 if (ctx->argc > 1) {
879 include = false;
880 for (size_t j = 1; j < ctx->argc; j++) {
881 if (is_partial_uuid_match(&lflow->header_.uuid,
882 ctx->argv[j])) {
883 include = true;
884 break;
885 }
886 }
887 } else {
888 include = true;
889 }
890 if (!include) {
dc70b67b
RB
891 continue;
892 }
c80eac1f
BP
893
894 /* Print a header line for this datapath or pipeline, if we haven't
895 * already done so. */
896 if (!prev
897 || prev->logical_datapath != lflow->logical_datapath
898 || strcmp(prev->pipeline, lflow->pipeline)) {
f658b27c
BP
899 printf("Datapath:");
900
901 const struct smap *ids = &lflow->logical_datapath->external_ids;
902 const char *name = smap_get(ids, "name");
903 const char *name2 = smap_get(ids, "name2");
904 if (name && name2) {
905 printf(" \"%s\" aka \"%s\"", name, name2);
906 } else if (name || name2) {
907 printf(" \"%s\"", name ? name : name2);
908 }
909 printf(" ("UUID_FMT") Pipeline: %s\n",
0f8e9c12
BP
910 UUID_ARGS(&lflow->logical_datapath->header_.uuid),
911 lflow->pipeline);
dc70b67b 912 }
ebbcddeb 913
c80eac1f
BP
914 /* Print the flow. */
915 printf(" ");
916 if (print_uuid) {
917 printf("uuid=0x%08"PRIx32", ", lflow->header_.uuid.parts[0]);
918 }
919 printf("table=%-2"PRId64"(%-19s), priority=%-5"PRId64
ebbcddeb 920 ", match=(%s), action=(%s)\n",
f99f67bd
BP
921 lflow->table_id,
922 smap_get_def(&lflow->external_ids, "stage-name", ""),
ebbcddeb 923 lflow->priority, lflow->match, lflow->actions);
c2f4c39b
BP
924 if (vconn) {
925 sbctl_dump_openflow(vconn, &lflow->header_.uuid, stats);
926 }
c80eac1f 927 prev = lflow;
dc70b67b
RB
928 }
929
c2f4c39b 930 vconn_close(vconn);
dc70b67b
RB
931 free(lflows);
932}
933
10471820
LR
934static void
935verify_connections(struct ctl_context *ctx)
936{
937 const struct sbrec_sb_global *sb_global = sbrec_sb_global_first(ctx->idl);
938 const struct sbrec_connection *conn;
939
940 sbrec_sb_global_verify_connections(sb_global);
941
942 SBREC_CONNECTION_FOR_EACH(conn, ctx->idl) {
943 sbrec_connection_verify_target(conn);
944 }
945}
946
947static void
948pre_connection(struct ctl_context *ctx)
949{
950 ovsdb_idl_add_column(ctx->idl, &sbrec_sb_global_col_connections);
951 ovsdb_idl_add_column(ctx->idl, &sbrec_connection_col_target);
952 ovsdb_idl_add_column(ctx->idl, &sbrec_connection_col_read_only);
fff48136 953 ovsdb_idl_add_column(ctx->idl, &sbrec_connection_col_role);
10471820
LR
954}
955
956static void
957cmd_get_connection(struct ctl_context *ctx)
958{
959 const struct sbrec_connection *conn;
960 struct svec targets;
961 size_t i;
962
963 verify_connections(ctx);
964
965 /* Print the targets in sorted order for reproducibility. */
966 svec_init(&targets);
967
968 SBREC_CONNECTION_FOR_EACH(conn, ctx->idl) {
969 char *s;
970
fff48136
LR
971 s = xasprintf("%s role=\"%s\" %s",
972 conn->read_only ? "read-only" : "read-write",
973 conn->role,
974 conn->target);
10471820
LR
975 svec_add(&targets, s);
976 free(s);
977 }
978
979 svec_sort_unique(&targets);
980 for (i = 0; i < targets.n; i++) {
981 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
982 }
983 svec_destroy(&targets);
984}
985
986static void
987delete_connections(struct ctl_context *ctx)
988{
989 const struct sbrec_sb_global *sb_global = sbrec_sb_global_first(ctx->idl);
990 const struct sbrec_connection *conn, *next;
991
992 /* Delete Manager rows pointed to by 'connection_options' column. */
993 SBREC_CONNECTION_FOR_EACH_SAFE(conn, next, ctx->idl) {
994 sbrec_connection_delete(conn);
995 }
996
997 /* Delete 'Manager' row refs in 'manager_options' column. */
998 sbrec_sb_global_set_connections(sb_global, NULL, 0);
999}
1000
1001static void
1002cmd_del_connection(struct ctl_context *ctx)
1003{
1004 verify_connections(ctx);
1005 delete_connections(ctx);
1006}
1007
1008static void
1009insert_connections(struct ctl_context *ctx, char *targets[], size_t n)
1010{
1011 const struct sbrec_sb_global *sb_global = sbrec_sb_global_first(ctx->idl);
1012 struct sbrec_connection **connections;
1013 size_t i, conns=0;
1014 bool read_only = false;
fff48136 1015 char *role = "";
10471820
LR
1016
1017 /* Insert each connection in a new row in Connection table. */
1018 connections = xmalloc(n * sizeof *connections);
1019 for (i = 0; i < n; i++) {
1020 if (!strcmp(targets[i], "read-only")) {
1021 read_only = true;
1022 continue;
1023 } else if (!strcmp(targets[i], "read-write")) {
1024 read_only = false;
1025 continue;
fff48136
LR
1026 } else if (!strncmp(targets[i], "role=", 5)) {
1027 role = targets[i] + 5;
1028 continue;
10471820
LR
1029 } else if (stream_verify_name(targets[i]) &&
1030 pstream_verify_name(targets[i])) {
1031 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1032 }
1033
1034 connections[conns] = sbrec_connection_insert(ctx->txn);
1035 sbrec_connection_set_target(connections[conns], targets[i]);
1036 sbrec_connection_set_read_only(connections[conns], read_only);
fff48136 1037 sbrec_connection_set_role(connections[conns], role);
10471820
LR
1038 conns++;
1039 }
1040
1041 /* Store uuids of new connection rows in 'connection' column. */
1042 sbrec_sb_global_set_connections(sb_global, connections, conns);
1043 free(connections);
1044}
1045
1046static void
1047cmd_set_connection(struct ctl_context *ctx)
1048{
1049 const size_t n = ctx->argc - 1;
1050
1051 verify_connections(ctx);
1052 delete_connections(ctx);
1053 insert_connections(ctx, &ctx->argv[1], n);
1054}
1055
1056static void
1057pre_cmd_get_ssl(struct ctl_context *ctx)
1058{
1059 ovsdb_idl_add_column(ctx->idl, &sbrec_sb_global_col_ssl);
1060
1061 ovsdb_idl_add_column(ctx->idl, &sbrec_ssl_col_private_key);
1062 ovsdb_idl_add_column(ctx->idl, &sbrec_ssl_col_certificate);
1063 ovsdb_idl_add_column(ctx->idl, &sbrec_ssl_col_ca_cert);
1064 ovsdb_idl_add_column(ctx->idl, &sbrec_ssl_col_bootstrap_ca_cert);
1065}
1066
1067static void
1068cmd_get_ssl(struct ctl_context *ctx)
1069{
1070 const struct sbrec_sb_global *sb_global = sbrec_sb_global_first(ctx->idl);
1071 const struct sbrec_ssl *ssl = sbrec_ssl_first(ctx->idl);
1072
1073 sbrec_sb_global_verify_ssl(sb_global);
1074 if (ssl) {
1075 sbrec_ssl_verify_private_key(ssl);
1076 sbrec_ssl_verify_certificate(ssl);
1077 sbrec_ssl_verify_ca_cert(ssl);
1078 sbrec_ssl_verify_bootstrap_ca_cert(ssl);
1079
1080 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1081 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1082 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1083 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1084 ssl->bootstrap_ca_cert ? "true" : "false");
1085 }
1086}
1087
1088static void
1089pre_cmd_del_ssl(struct ctl_context *ctx)
1090{
1091 ovsdb_idl_add_column(ctx->idl, &sbrec_sb_global_col_ssl);
1092}
1093
1094static void
1095cmd_del_ssl(struct ctl_context *ctx)
1096{
1097 const struct sbrec_sb_global *sb_global = sbrec_sb_global_first(ctx->idl);
1098 const struct sbrec_ssl *ssl = sbrec_ssl_first(ctx->idl);
1099
1100 if (ssl) {
1101 sbrec_sb_global_verify_ssl(sb_global);
1102 sbrec_ssl_delete(ssl);
1103 sbrec_sb_global_set_ssl(sb_global, NULL);
1104 }
1105}
1106
1107static void
1108pre_cmd_set_ssl(struct ctl_context *ctx)
1109{
1110 ovsdb_idl_add_column(ctx->idl, &sbrec_sb_global_col_ssl);
1111}
1112
1113static void
1114cmd_set_ssl(struct ctl_context *ctx)
1115{
1116 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1117 const struct sbrec_sb_global *sb_global = sbrec_sb_global_first(ctx->idl);
1118 const struct sbrec_ssl *ssl = sbrec_ssl_first(ctx->idl);
1119
1120 sbrec_sb_global_verify_ssl(sb_global);
1121 if (ssl) {
1122 sbrec_ssl_delete(ssl);
1123 }
1124 ssl = sbrec_ssl_insert(ctx->txn);
1125
1126 sbrec_ssl_set_private_key(ssl, ctx->argv[1]);
1127 sbrec_ssl_set_certificate(ssl, ctx->argv[2]);
1128 sbrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1129
1130 sbrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1131
51af591b
LR
1132 if (ctx->argc == 5) {
1133 sbrec_ssl_set_ssl_protocols(ssl, ctx->argv[4]);
1134 } else if (ctx->argc == 6) {
1135 sbrec_ssl_set_ssl_protocols(ssl, ctx->argv[4]);
1136 sbrec_ssl_set_ssl_ciphers(ssl, ctx->argv[5]);
1137 }
1138
10471820
LR
1139 sbrec_sb_global_set_ssl(sb_global, ssl);
1140}
1141
fed00ab1 1142\f
3f5b5f7b 1143static const struct ctl_table_class tables[SBREC_N_TABLES] = {
15931827 1144 [SBREC_TABLE_CHASSIS].row_ids[0] = {&sbrec_chassis_col_name, NULL, NULL},
fa183acc 1145
682b4171
BP
1146 [SBREC_TABLE_DATAPATH_BINDING].row_ids
1147 = {{&sbrec_datapath_binding_col_external_ids, "name", NULL},
f658b27c 1148 {&sbrec_datapath_binding_col_external_ids, "name2", NULL},
682b4171
BP
1149 {&sbrec_datapath_binding_col_external_ids, "logical-switch", NULL},
1150 {&sbrec_datapath_binding_col_external_ids, "logical-router", NULL}},
1151
f658b27c
BP
1152 [SBREC_TABLE_PORT_BINDING].row_ids
1153 = {{&sbrec_port_binding_col_logical_port, NULL, NULL},
1154 {&sbrec_port_binding_col_external_ids, "name", NULL}},
9c39b120 1155
3f5b5f7b 1156 [SBREC_TABLE_MAC_BINDING].row_ids[0] =
15931827 1157 {&sbrec_mac_binding_col_logical_port, NULL, NULL},
7fff4eb7 1158
15931827
BP
1159 [SBREC_TABLE_ADDRESS_SET].row_ids[0]
1160 = {&sbrec_address_set_col_name, NULL, NULL},
fed00ab1
AW
1161};
1162
1163\f
1164static void
1165sbctl_context_init_command(struct sbctl_context *sbctl_ctx,
1166 struct ctl_command *command)
1167{
1168 ctl_context_init_command(&sbctl_ctx->base, command);
1169}
1170
1171static void
1172sbctl_context_init(struct sbctl_context *sbctl_ctx,
1173 struct ctl_command *command, struct ovsdb_idl *idl,
1174 struct ovsdb_idl_txn *txn,
1175 struct ovsdb_symbol_table *symtab)
1176{
1177 ctl_context_init(&sbctl_ctx->base, command, idl, txn, symtab,
1178 sbctl_context_invalidate_cache);
1179 sbctl_ctx->cache_valid = false;
1180}
1181
1182static void
1183sbctl_context_done_command(struct sbctl_context *sbctl_ctx,
1184 struct ctl_command *command)
1185{
1186 ctl_context_done_command(&sbctl_ctx->base, command);
1187}
1188
1189static void
1190sbctl_context_done(struct sbctl_context *sbctl_ctx,
1191 struct ctl_command *command)
1192{
1193 ctl_context_done(&sbctl_ctx->base, command);
1194}
1195
1196static void
1197run_prerequisites(struct ctl_command *commands, size_t n_commands,
1198 struct ovsdb_idl *idl)
1199{
fa183acc 1200 ovsdb_idl_add_table(idl, &sbrec_table_sb_global);
fed00ab1 1201
fa183acc 1202 for (struct ctl_command *c = commands; c < &commands[n_commands]; c++) {
fed00ab1
AW
1203 if (c->syntax->prerequisites) {
1204 struct sbctl_context sbctl_ctx;
1205
1206 ds_init(&c->output);
1207 c->table = NULL;
1208
1209 sbctl_context_init(&sbctl_ctx, c, idl, NULL, NULL);
1210 (c->syntax->prerequisites)(&sbctl_ctx.base);
1211 sbctl_context_done(&sbctl_ctx, c);
1212
1213 ovs_assert(!c->output.string);
1214 ovs_assert(!c->table);
1215 }
1216 }
1217}
1218
8ee574c3 1219static bool
fed00ab1
AW
1220do_sbctl(const char *args, struct ctl_command *commands, size_t n_commands,
1221 struct ovsdb_idl *idl)
1222{
1223 struct ovsdb_idl_txn *txn;
1224 enum ovsdb_idl_txn_status status;
1225 struct ovsdb_symbol_table *symtab;
1226 struct sbctl_context sbctl_ctx;
1227 struct ctl_command *c;
1228 struct shash_node *node;
1229 char *error = NULL;
1230
1231 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
1232 if (dry_run) {
1233 ovsdb_idl_txn_set_dry_run(txn);
1234 }
1235
1236 ovsdb_idl_txn_add_comment(txn, "ovs-sbctl: %s", args);
1237
fa183acc
BP
1238 const struct sbrec_sb_global *sb = sbrec_sb_global_first(idl);
1239 if (!sb) {
1240 /* XXX add verification that table is empty */
b37b684e 1241 sbrec_sb_global_insert(txn);
fa183acc
BP
1242 }
1243
fed00ab1
AW
1244 symtab = ovsdb_symbol_table_create();
1245 for (c = commands; c < &commands[n_commands]; c++) {
1246 ds_init(&c->output);
1247 c->table = NULL;
1248 }
1249 sbctl_context_init(&sbctl_ctx, NULL, idl, txn, symtab);
1250 for (c = commands; c < &commands[n_commands]; c++) {
1251 sbctl_context_init_command(&sbctl_ctx, c);
1252 if (c->syntax->run) {
1253 (c->syntax->run)(&sbctl_ctx.base);
1254 }
1255 sbctl_context_done_command(&sbctl_ctx, c);
1256
1257 if (sbctl_ctx.base.try_again) {
1258 sbctl_context_done(&sbctl_ctx, NULL);
1259 goto try_again;
1260 }
1261 }
1262 sbctl_context_done(&sbctl_ctx, NULL);
1263
1264 SHASH_FOR_EACH (node, &symtab->sh) {
1265 struct ovsdb_symbol *symbol = node->data;
1266 if (!symbol->created) {
1267 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
b31301bc
BP
1268 "with \"-- --id=%s create ...\")",
1269 node->name, node->name);
fed00ab1
AW
1270 }
1271 if (!symbol->strong_ref) {
1272 if (!symbol->weak_ref) {
1273 VLOG_WARN("row id \"%s\" was created but no reference to it "
1274 "was inserted, so it will not actually appear in "
1275 "the database", node->name);
1276 } else {
1277 VLOG_WARN("row id \"%s\" was created but only a weak "
1278 "reference to it was inserted, so it will not "
1279 "actually appear in the database", node->name);
1280 }
1281 }
1282 }
1283
1284 status = ovsdb_idl_txn_commit_block(txn);
1285 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
1286 for (c = commands; c < &commands[n_commands]; c++) {
1287 if (c->syntax->postprocess) {
1288 sbctl_context_init(&sbctl_ctx, c, idl, txn, symtab);
1289 (c->syntax->postprocess)(&sbctl_ctx.base);
1290 sbctl_context_done(&sbctl_ctx, c);
1291 }
1292 }
1293 }
1294 error = xstrdup(ovsdb_idl_txn_get_error(txn));
1295
1296 switch (status) {
1297 case TXN_UNCOMMITTED:
1298 case TXN_INCOMPLETE:
1299 OVS_NOT_REACHED();
1300
1301 case TXN_ABORTED:
1302 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1303 ctl_fatal("transaction aborted");
1304
1305 case TXN_UNCHANGED:
1306 case TXN_SUCCESS:
1307 break;
1308
1309 case TXN_TRY_AGAIN:
1310 goto try_again;
1311
1312 case TXN_ERROR:
1313 ctl_fatal("transaction error: %s", error);
1314
1315 case TXN_NOT_LOCKED:
1316 /* Should not happen--we never call ovsdb_idl_set_lock(). */
1317 ctl_fatal("database not locked");
1318
1319 default:
1320 OVS_NOT_REACHED();
1321 }
1322 free(error);
1323
1324 ovsdb_symbol_table_destroy(symtab);
1325
1326 for (c = commands; c < &commands[n_commands]; c++) {
1327 struct ds *ds = &c->output;
1328
1329 if (c->table) {
1330 table_print(c->table, &table_style);
1331 } else if (oneline) {
1332 size_t j;
1333
1334 ds_chomp(ds, '\n');
1335 for (j = 0; j < ds->length; j++) {
1336 int ch = ds->string[j];
1337 switch (ch) {
1338 case '\n':
1339 fputs("\\n", stdout);
1340 break;
1341
1342 case '\\':
1343 fputs("\\\\", stdout);
1344 break;
1345
1346 default:
1347 putchar(ch);
1348 }
1349 }
1350 putchar('\n');
1351 } else {
1352 fputs(ds_cstr(ds), stdout);
1353 }
1354 ds_destroy(&c->output);
1355 table_destroy(c->table);
1356 free(c->table);
1357
1358 shash_destroy_free_data(&c->options);
1359 }
1360 free(commands);
1361 ovsdb_idl_txn_destroy(txn);
1362 ovsdb_idl_destroy(idl);
1363
8ee574c3 1364 return true;
fed00ab1
AW
1365
1366try_again:
1367 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
1368 * resources and return so that the caller can try again. */
6851589c
BP
1369 ovsdb_idl_txn_abort(txn);
1370 ovsdb_idl_txn_destroy(txn);
1371 the_idl_txn = NULL;
1372
fed00ab1
AW
1373 ovsdb_symbol_table_destroy(symtab);
1374 for (c = commands; c < &commands[n_commands]; c++) {
1375 ds_destroy(&c->output);
1376 table_destroy(c->table);
1377 free(c->table);
1378 }
1379 free(error);
8ee574c3 1380 return false;
fed00ab1
AW
1381}
1382
1383/* Frees the current transaction and the underlying IDL and then calls
1384 * exit(status).
1385 *
1386 * Freeing the transaction and the IDL is not strictly necessary, but it makes
1387 * for a clean memory leak report from valgrind in the normal case. That makes
1388 * it easier to notice real memory leaks. */
1389static void
1390sbctl_exit(int status)
1391{
1392 if (the_idl_txn) {
1393 ovsdb_idl_txn_abort(the_idl_txn);
1394 ovsdb_idl_txn_destroy(the_idl_txn);
1395 }
1396 ovsdb_idl_destroy(the_idl);
1397 exit(status);
1398}
1399
1400static const struct ctl_command_syntax sbctl_commands[] = {
fa183acc
BP
1401 { "init", 0, 0, "", NULL, sbctl_init, NULL, "", RW },
1402
fed00ab1
AW
1403 /* Chassis commands. */
1404 {"chassis-add", 3, 3, "CHASSIS ENCAP-TYPE ENCAP-IP", pre_get_info,
1405 cmd_chassis_add, NULL, "--may-exist", RW},
1406 {"chassis-del", 1, 1, "CHASSIS", pre_get_info, cmd_chassis_del, NULL,
1407 "--if-exists", RW},
1408
1409 /* Port binding commands. */
f0f96ba8 1410 {"lsp-bind", 2, 2, "PORT CHASSIS", pre_get_info, cmd_lsp_bind, NULL,
fed00ab1 1411 "--may-exist", RW},
f0f96ba8 1412 {"lsp-unbind", 1, 1, "PORT", pre_get_info, cmd_lsp_unbind, NULL,
fed00ab1
AW
1413 "--if-exists", RW},
1414
dc70b67b 1415 /* Logical flow commands */
c80eac1f
BP
1416 {"lflow-list", 0, INT_MAX, "[DATAPATH] [LFLOW...]",
1417 pre_get_info, cmd_lflow_list, NULL,
c2f4c39b 1418 "--uuid,--ovs?,--stats", RO},
c80eac1f
BP
1419 {"dump-flows", 0, INT_MAX, "[DATAPATH] [LFLOW...]",
1420 pre_get_info, cmd_lflow_list, NULL,
c2f4c39b 1421 "--uuid,--ovs?,--stats", RO}, /* Friendly alias for lflow-list */
dc70b67b 1422
10471820
LR
1423 /* Connection commands. */
1424 {"get-connection", 0, 0, "", pre_connection, cmd_get_connection, NULL, "", RO},
1425 {"del-connection", 0, 0, "", pre_connection, cmd_del_connection, NULL, "", RW},
1426 {"set-connection", 1, INT_MAX, "TARGET...", pre_connection, cmd_set_connection,
1427 NULL, "", RW},
1428
1429 /* SSL commands. */
1430 {"get-ssl", 0, 0, "", pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
1431 {"del-ssl", 0, 0, "", pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
51af591b
LR
1432 {"set-ssl", 3, 5,
1433 "PRIVATE-KEY CERTIFICATE CA-CERT [SSL-PROTOS [SSL-CIPHERS]]",
1434 pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
fed00ab1
AW
1435
1436 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
1437};
1438
1439/* Registers sbctl and common db commands. */
1440static void
1441sbctl_cmd_init(void)
1442{
3f5b5f7b 1443 ctl_init(sbrec_table_classes, tables, cmd_show_tables, sbctl_exit);
fed00ab1
AW
1444 ctl_register_commands(sbctl_commands);
1445}