2 * Copyright (c) 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "db-ctl-base.h"
32 #include "command-line.h"
34 #include "dynamic-string.h"
35 #include "fatal-signal.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-idl.h"
39 #include "poll-loop.h"
43 #include "stream-ssl.h"
48 #include "openvswitch/vlog.h"
49 #include "ovn/lib/ovn-sb-idl.h"
51 VLOG_DEFINE_THIS_MODULE(sbctl);
55 /* --db: The database server to contact. */
56 static const char *db;
58 /* --oneline: Write each command's output as a single line? */
61 /* --dry-run: Do not commit any changes. */
64 /* --timeout: Time to wait for a connection to 'db'. */
67 /* Format for table output. */
68 static struct table_style table_style = TABLE_STYLE_DEFAULT;
70 /* The IDL we're using and the current transaction, if any.
71 * This is for use by sbctl_exit() only, to allow it to clean up.
72 * Other code should use its context arguments. */
73 static struct ovsdb_idl *the_idl;
74 static struct ovsdb_idl_txn *the_idl_txn;
75 OVS_NO_RETURN static void sbctl_exit(int status);
77 static void sbctl_cmd_init(void);
78 OVS_NO_RETURN static void usage(void);
79 static void parse_options(int argc, char *argv[], struct shash *local_options);
80 static const char *sbctl_default_db(void);
81 static void run_prerequisites(struct ctl_command[], size_t n_commands,
83 static void do_sbctl(const char *args, struct ctl_command *, size_t n,
87 main(int argc, char *argv[])
89 extern struct vlog_module VLM_reconnect;
90 struct ovsdb_idl *idl;
91 struct ctl_command *commands;
92 struct shash local_options;
97 set_program_name(argv[0]);
98 fatal_ignore_sigpipe();
99 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
100 vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
105 /* Log our arguments. This is often valuable for debugging systems. */
106 args = process_escape_args(argv);
107 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
109 /* Parse command line. */
110 shash_init(&local_options);
111 parse_options(argc, argv, &local_options);
112 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
119 /* Initialize IDL. */
120 idl = the_idl = ovsdb_idl_create(db, &sbrec_idl_class, false, false);
121 run_prerequisites(commands, n_commands, idl);
123 /* Execute the commands.
125 * 'seqno' is the database sequence number for which we last tried to
126 * execute our transaction. There's no point in trying to commit more than
127 * once for any given sequence number, because if the transaction fails
128 * it's because the database changed and we need to obtain an up-to-date
129 * view of the database before we try the transaction again. */
130 seqno = ovsdb_idl_get_seqno(idl);
133 if (!ovsdb_idl_is_alive(idl)) {
134 int retval = ovsdb_idl_get_last_error(idl);
135 ctl_fatal("%s: database connection failed (%s)",
136 db, ovs_retval_to_string(retval));
139 if (seqno != ovsdb_idl_get_seqno(idl)) {
140 seqno = ovsdb_idl_get_seqno(idl);
141 do_sbctl(args, commands, n_commands, idl);
144 if (seqno == ovsdb_idl_get_seqno(idl)) {
152 sbctl_default_db(void)
156 def = getenv("OVN_SB_DB");
158 def = ctl_default_db();
165 parse_options(int argc, char *argv[], struct shash *local_options)
168 OPT_DB = UCHAR_MAX + 1,
179 static const struct option global_long_options[] = {
180 {"db", required_argument, NULL, OPT_DB},
181 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
182 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
183 {"oneline", no_argument, NULL, OPT_ONELINE},
184 {"timeout", required_argument, NULL, 't'},
185 {"help", no_argument, NULL, 'h'},
186 {"commands", no_argument, NULL, OPT_COMMANDS},
187 {"options", no_argument, NULL, OPT_OPTIONS},
188 {"version", no_argument, NULL, 'V'},
190 STREAM_SSL_LONG_OPTIONS,
194 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
195 char *tmp, *short_options;
197 struct option *options;
198 size_t allocated_options;
202 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
203 short_options = xasprintf("+%s", tmp);
206 /* We want to parse both global and command-specific options here, but
207 * getopt_long() isn't too convenient for the job. We copy our global
208 * options into a dynamic array, then append all of the command-specific
210 options = xmemdup(global_long_options, sizeof global_long_options);
211 allocated_options = ARRAY_SIZE(global_long_options);
212 n_options = n_global_long_options;
213 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
214 table_style.format = TF_LIST;
220 c = getopt_long(argc, argv, short_options, options, &idx);
235 vlog_set_levels(&VLM_sbctl, VLF_SYSLOG, VLL_WARN);
243 if (shash_find(local_options, options[idx].name)) {
244 ctl_fatal("'%s' option specified multiple times",
247 shash_add_nocopy(local_options,
248 xasprintf("--%s", options[idx].name),
249 optarg ? xstrdup(optarg) : NULL);
256 ctl_print_commands();
259 ctl_print_options(global_long_options);
262 ovs_print_version(0, 0);
263 printf("DB Schema %s\n", sbrec_get_db_version());
267 timeout = strtoul(optarg, NULL, 10);
269 ctl_fatal("value %s on -t or --timeout is invalid", optarg);
274 TABLE_OPTION_HANDLERS(&table_style)
275 STREAM_SSL_OPTION_HANDLERS
287 db = sbctl_default_db();
290 for (i = n_global_long_options; options[i].name; i++) {
291 free(CONST_CAST(char *, options[i].name));
300 %s: OVN southbound DB management utility\n\
302 For debugging and testing only, not for use in production.\n\
304 usage: %s [OPTIONS] COMMAND [ARG...]\n\
307 show print overview of database contents\n\
310 chassis-add CHASSIS ENCAP-TYPE ENCAP-IP create a new chassis named\n\
311 CHASSIS with ENCAP-TYPE tunnels\n\
313 chassis-del CHASSIS delete CHASSIS and all of its encaps\n\
316 Port binding commands:\n\
317 lport-bind LPORT CHASSIS bind logical port LPORT to CHASSIS\n\
318 lport-unbind LPORT reset the port binding of logical port LPORT\n\
320 Logical flow commands:\n\
321 lflow-list [DATAPATH] List logical flows for all or a single datapath\n\
322 dump-flows [DATAPATH] Alias for lflow-list\n\
327 --db=DATABASE connect to DATABASE\n\
329 -t, --timeout=SECS wait at most SECS seconds\n\
330 --dry-run do not commit changes to database\n\
331 --oneline print exactly one line of output per command\n",
332 program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
335 --no-syslog equivalent to --verbose=sbctl:syslog:warn\n");
338 -h, --help display this help message\n\
339 -V, --version display version information\n");
340 stream_usage("database", true, true, false);
345 /* ovs-sbctl specific context. Inherits the 'struct ctl_context' as base. */
346 struct sbctl_context {
347 struct ctl_context base;
349 /* A cache of the contents of the database.
351 * A command that needs to use any of this information must first call
352 * sbctl_context_populate_cache(). A command that changes anything that
353 * could invalidate the cache must either call
354 * sbctl_context_invalidate_cache() or manually update the cache to
355 * maintain its correctness. */
357 /* Maps from chassis name to struct sbctl_chassis. */
358 struct shash chassis;
359 /* Maps from lport name to struct sbctl_port_binding. */
360 struct shash port_bindings;
363 /* Casts 'base' into 'struct sbctl_context'. */
364 static struct sbctl_context *
365 sbctl_context_cast(struct ctl_context *base)
367 return CONTAINER_OF(base, struct sbctl_context, base);
370 struct sbctl_chassis {
371 const struct sbrec_chassis *ch_cfg;
374 struct sbctl_port_binding {
375 const struct sbrec_port_binding *bd_cfg;
379 sbctl_context_invalidate_cache(struct ctl_context *ctx)
381 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
383 if (!sbctl_ctx->cache_valid) {
386 sbctl_ctx->cache_valid = false;
387 shash_destroy_free_data(&sbctl_ctx->chassis);
388 shash_destroy_free_data(&sbctl_ctx->port_bindings);
392 sbctl_context_populate_cache(struct ctl_context *ctx)
394 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
395 const struct sbrec_chassis *chassis_rec;
396 const struct sbrec_port_binding *port_binding_rec;
397 struct sset chassis, port_bindings;
399 if (sbctl_ctx->cache_valid) {
400 /* Cache is already populated. */
403 sbctl_ctx->cache_valid = true;
404 shash_init(&sbctl_ctx->chassis);
405 shash_init(&sbctl_ctx->port_bindings);
407 SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->idl) {
408 struct sbctl_chassis *ch;
410 if (!sset_add(&chassis, chassis_rec->name)) {
411 VLOG_WARN("database contains duplicate chassis name (%s)",
416 ch = xmalloc(sizeof *ch);
417 ch->ch_cfg = chassis_rec;
418 shash_add(&sbctl_ctx->chassis, chassis_rec->name, ch);
420 sset_destroy(&chassis);
422 sset_init(&port_bindings);
423 SBREC_PORT_BINDING_FOR_EACH(port_binding_rec, ctx->idl) {
424 struct sbctl_port_binding *bd;
426 if (!sset_add(&port_bindings, port_binding_rec->logical_port)) {
427 VLOG_WARN("database contains duplicate port binding for logical "
429 port_binding_rec->logical_port);
433 bd = xmalloc(sizeof *bd);
434 bd->bd_cfg = port_binding_rec;
435 shash_add(&sbctl_ctx->port_bindings, port_binding_rec->logical_port,
438 sset_destroy(&port_bindings);
442 check_conflicts(struct sbctl_context *sbctl_ctx, const char *name,
445 if (shash_find(&sbctl_ctx->chassis, name)) {
446 ctl_fatal("%s because a chassis named %s already exists",
452 static struct sbctl_chassis *
453 find_chassis(struct sbctl_context *sbctl_ctx, const char *name,
456 struct sbctl_chassis *sbctl_ch;
458 ovs_assert(sbctl_ctx->cache_valid);
460 sbctl_ch = shash_find_data(&sbctl_ctx->chassis, name);
461 if (must_exist && !sbctl_ch) {
462 ctl_fatal("no chassis named %s", name);
468 static struct sbctl_port_binding *
469 find_port_binding(struct sbctl_context *sbctl_ctx, const char *name,
472 struct sbctl_port_binding *bd;
474 ovs_assert(sbctl_ctx->cache_valid);
476 bd = shash_find_data(&sbctl_ctx->port_bindings, name);
477 if (must_exist && !bd) {
478 ctl_fatal("no port named %s", name);
485 pre_get_info(struct ctl_context *ctx)
487 ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_col_name);
488 ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_col_encaps);
490 ovsdb_idl_add_column(ctx->idl, &sbrec_encap_col_type);
491 ovsdb_idl_add_column(ctx->idl, &sbrec_encap_col_ip);
493 ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_logical_port);
494 ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_chassis);
496 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_logical_datapath);
497 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_pipeline);
498 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_actions);
499 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_priority);
500 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_table_id);
501 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_match);
502 ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_external_ids);
505 static struct cmd_show_table cmd_show_tables[] = {
506 {&sbrec_table_chassis,
507 &sbrec_chassis_col_name,
508 {&sbrec_chassis_col_encaps,
511 {&sbrec_table_port_binding,
512 &sbrec_port_binding_col_logical_port,
513 &sbrec_port_binding_col_chassis}},
516 &sbrec_encap_col_type,
517 {&sbrec_encap_col_ip,
518 &sbrec_encap_col_options,
522 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}},
526 cmd_chassis_add(struct ctl_context *ctx)
528 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
529 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
530 const char *ch_name, *encap_types, *encap_ip;
532 ch_name = ctx->argv[1];
533 encap_types = ctx->argv[2];
534 encap_ip = ctx->argv[3];
536 sbctl_context_populate_cache(ctx);
538 struct sbctl_chassis *sbctl_ch;
540 sbctl_ch = find_chassis(sbctl_ctx, ch_name, false);
545 check_conflicts(sbctl_ctx, ch_name,
546 xasprintf("cannot create a chassis named %s", ch_name));
548 char *tokstr = xstrdup(encap_types);
549 char *token, *save_ptr = NULL;
550 struct sset encap_set = SSET_INITIALIZER(&encap_set);
551 for (token = strtok_r(tokstr, ",", &save_ptr); token != NULL;
552 token = strtok_r(NULL, ",", &save_ptr)) {
553 sset_add(&encap_set, token);
557 size_t n_encaps = sset_count(&encap_set);
558 struct sbrec_encap **encaps = xmalloc(n_encaps * sizeof *encaps);
559 const char *encap_type;
561 SSET_FOR_EACH (encap_type, &encap_set){
562 encaps[i] = sbrec_encap_insert(ctx->txn);
564 sbrec_encap_set_type(encaps[i], encap_type);
565 sbrec_encap_set_ip(encaps[i], encap_ip);
568 sset_destroy(&encap_set);
570 struct sbrec_chassis *ch = sbrec_chassis_insert(ctx->txn);
571 sbrec_chassis_set_name(ch, ch_name);
572 sbrec_chassis_set_encaps(ch, encaps, n_encaps);
575 sbctl_context_invalidate_cache(ctx);
579 cmd_chassis_del(struct ctl_context *ctx)
581 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
582 bool must_exist = !shash_find(&ctx->options, "--if-exists");
583 struct sbctl_chassis *sbctl_ch;
585 sbctl_context_populate_cache(ctx);
586 sbctl_ch = find_chassis(sbctl_ctx, ctx->argv[1], must_exist);
588 if (sbctl_ch->ch_cfg) {
591 for (i = 0; i < sbctl_ch->ch_cfg->n_encaps; i++) {
592 sbrec_encap_delete(sbctl_ch->ch_cfg->encaps[i]);
594 sbrec_chassis_delete(sbctl_ch->ch_cfg);
596 shash_find_and_delete(&sbctl_ctx->chassis, ctx->argv[1]);
602 cmd_lport_bind(struct ctl_context *ctx)
604 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
605 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
606 struct sbctl_chassis *sbctl_ch;
607 struct sbctl_port_binding *sbctl_bd;
608 char *lport_name, *ch_name;
610 /* port_binding must exist, chassis must exist! */
611 lport_name = ctx->argv[1];
612 ch_name = ctx->argv[2];
614 sbctl_context_populate_cache(ctx);
615 sbctl_bd = find_port_binding(sbctl_ctx, lport_name, true);
616 sbctl_ch = find_chassis(sbctl_ctx, ch_name, true);
618 if (sbctl_bd->bd_cfg->chassis) {
619 if (may_exist && sbctl_bd->bd_cfg->chassis == sbctl_ch->ch_cfg) {
622 ctl_fatal("lport (%s) has already been binded to chassis (%s)",
623 lport_name, sbctl_bd->bd_cfg->chassis->name);
626 sbrec_port_binding_set_chassis(sbctl_bd->bd_cfg, sbctl_ch->ch_cfg);
627 sbctl_context_invalidate_cache(ctx);
631 cmd_lport_unbind(struct ctl_context *ctx)
633 struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
634 bool must_exist = !shash_find(&ctx->options, "--if-exists");
635 struct sbctl_port_binding *sbctl_bd;
638 lport_name = ctx->argv[1];
639 sbctl_context_populate_cache(ctx);
640 sbctl_bd = find_port_binding(sbctl_ctx, lport_name, must_exist);
642 sbrec_port_binding_set_chassis(sbctl_bd->bd_cfg, NULL);
651 /* Help ensure we catch any future pipeline values */
653 pipeline_encode(const char *pl)
655 if (!strcmp(pl, "ingress")) {
657 } else if (!strcmp(pl, "egress")) {
665 lflow_cmp(const void *lf1_, const void *lf2_)
667 const struct sbrec_logical_flow *const *lf1p = lf1_;
668 const struct sbrec_logical_flow *const *lf2p = lf2_;
669 const struct sbrec_logical_flow *lf1 = *lf1p;
670 const struct sbrec_logical_flow *lf2 = *lf2p;
672 int pl1 = pipeline_encode(lf1->pipeline);
673 int pl2 = pipeline_encode(lf2->pipeline);
684 CMP(uuid_compare_3way(&lf1->logical_datapath->header_.uuid,
685 &lf2->logical_datapath->header_.uuid));
687 CMP(lf1->table_id > lf2->table_id ? 1 :
688 (lf1->table_id < lf2->table_id ? -1 : 0));
689 CMP(lf1->priority > lf2->priority ? -1 :
690 (lf1->priority < lf2->priority ? 1 : 0));
691 CMP(strcmp(lf1->match, lf2->match));
699 cmd_lflow_list(struct ctl_context *ctx)
701 const char *datapath = ctx->argc == 2 ? ctx->argv[1] : NULL;
702 struct uuid datapath_uuid = { .parts = { 0, }};
703 const struct sbrec_logical_flow **lflows;
704 const struct sbrec_logical_flow *lflow;
705 size_t n_flows = 0, n_capacity = 64;
707 if (datapath && !uuid_from_string(&datapath_uuid, datapath)) {
708 VLOG_ERR("Invalid format of datapath UUID");
712 lflows = xmalloc(sizeof *lflows * n_capacity);
713 SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->idl) {
714 if (n_flows == n_capacity) {
715 lflows = x2nrealloc(lflows, &n_capacity, sizeof *lflows);
717 lflows[n_flows] = lflow;
721 qsort(lflows, n_flows, sizeof *lflows, lflow_cmp);
723 const char *cur_pipeline = "";
725 for (i = 0; i < n_flows; i++) {
727 if (datapath && !uuid_equals(&datapath_uuid,
728 &lflow->logical_datapath->header_.uuid)) {
731 if (strcmp(cur_pipeline, lflow->pipeline)) {
732 printf("Datapath: " UUID_FMT " Pipeline: %s\n",
733 UUID_ARGS(&lflow->logical_datapath->header_.uuid),
735 cur_pipeline = lflow->pipeline;
738 const char *table_name = smap_get(&lflow->external_ids, "stage-name");
739 printf(" table=%" PRId64 "(%8s), priority=%5" PRId64
740 ", match=(%s), action=(%s)\n",
741 lflow->table_id, table_name ? table_name : "",
742 lflow->priority, lflow->match, lflow->actions);
749 static const struct ctl_table_class tables[] = {
750 {&sbrec_table_chassis,
751 {{&sbrec_table_chassis, &sbrec_chassis_col_name, NULL},
752 {NULL, NULL, NULL}}},
756 {NULL, NULL, NULL}}},
758 {&sbrec_table_logical_flow,
759 {{&sbrec_table_logical_flow, NULL,
760 &sbrec_logical_flow_col_logical_datapath},
761 {NULL, NULL, NULL}}},
763 {&sbrec_table_multicast_group,
765 {NULL, NULL, NULL}}},
767 {&sbrec_table_datapath_binding,
769 {NULL, NULL, NULL}}},
771 {&sbrec_table_port_binding,
772 {{&sbrec_table_port_binding, &sbrec_port_binding_col_logical_port, NULL},
773 {NULL, NULL, NULL}}},
775 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
780 sbctl_context_init_command(struct sbctl_context *sbctl_ctx,
781 struct ctl_command *command)
783 ctl_context_init_command(&sbctl_ctx->base, command);
787 sbctl_context_init(struct sbctl_context *sbctl_ctx,
788 struct ctl_command *command, struct ovsdb_idl *idl,
789 struct ovsdb_idl_txn *txn,
790 struct ovsdb_symbol_table *symtab)
792 ctl_context_init(&sbctl_ctx->base, command, idl, txn, symtab,
793 sbctl_context_invalidate_cache);
794 sbctl_ctx->cache_valid = false;
798 sbctl_context_done_command(struct sbctl_context *sbctl_ctx,
799 struct ctl_command *command)
801 ctl_context_done_command(&sbctl_ctx->base, command);
805 sbctl_context_done(struct sbctl_context *sbctl_ctx,
806 struct ctl_command *command)
808 ctl_context_done(&sbctl_ctx->base, command);
812 run_prerequisites(struct ctl_command *commands, size_t n_commands,
813 struct ovsdb_idl *idl)
815 struct ctl_command *c;
817 for (c = commands; c < &commands[n_commands]; c++) {
818 if (c->syntax->prerequisites) {
819 struct sbctl_context sbctl_ctx;
824 sbctl_context_init(&sbctl_ctx, c, idl, NULL, NULL);
825 (c->syntax->prerequisites)(&sbctl_ctx.base);
826 sbctl_context_done(&sbctl_ctx, c);
828 ovs_assert(!c->output.string);
829 ovs_assert(!c->table);
835 do_sbctl(const char *args, struct ctl_command *commands, size_t n_commands,
836 struct ovsdb_idl *idl)
838 struct ovsdb_idl_txn *txn;
839 enum ovsdb_idl_txn_status status;
840 struct ovsdb_symbol_table *symtab;
841 struct sbctl_context sbctl_ctx;
842 struct ctl_command *c;
843 struct shash_node *node;
846 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
848 ovsdb_idl_txn_set_dry_run(txn);
851 ovsdb_idl_txn_add_comment(txn, "ovs-sbctl: %s", args);
853 symtab = ovsdb_symbol_table_create();
854 for (c = commands; c < &commands[n_commands]; c++) {
858 sbctl_context_init(&sbctl_ctx, NULL, idl, txn, symtab);
859 for (c = commands; c < &commands[n_commands]; c++) {
860 sbctl_context_init_command(&sbctl_ctx, c);
861 if (c->syntax->run) {
862 (c->syntax->run)(&sbctl_ctx.base);
864 sbctl_context_done_command(&sbctl_ctx, c);
866 if (sbctl_ctx.base.try_again) {
867 sbctl_context_done(&sbctl_ctx, NULL);
871 sbctl_context_done(&sbctl_ctx, NULL);
873 SHASH_FOR_EACH (node, &symtab->sh) {
874 struct ovsdb_symbol *symbol = node->data;
875 if (!symbol->created) {
876 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
877 "with \"-- --id=%s create ...\")",
878 node->name, node->name);
880 if (!symbol->strong_ref) {
881 if (!symbol->weak_ref) {
882 VLOG_WARN("row id \"%s\" was created but no reference to it "
883 "was inserted, so it will not actually appear in "
884 "the database", node->name);
886 VLOG_WARN("row id \"%s\" was created but only a weak "
887 "reference to it was inserted, so it will not "
888 "actually appear in the database", node->name);
893 status = ovsdb_idl_txn_commit_block(txn);
894 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
895 for (c = commands; c < &commands[n_commands]; c++) {
896 if (c->syntax->postprocess) {
897 sbctl_context_init(&sbctl_ctx, c, idl, txn, symtab);
898 (c->syntax->postprocess)(&sbctl_ctx.base);
899 sbctl_context_done(&sbctl_ctx, c);
903 error = xstrdup(ovsdb_idl_txn_get_error(txn));
906 case TXN_UNCOMMITTED:
911 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
912 ctl_fatal("transaction aborted");
922 ctl_fatal("transaction error: %s", error);
925 /* Should not happen--we never call ovsdb_idl_set_lock(). */
926 ctl_fatal("database not locked");
933 ovsdb_symbol_table_destroy(symtab);
935 for (c = commands; c < &commands[n_commands]; c++) {
936 struct ds *ds = &c->output;
939 table_print(c->table, &table_style);
940 } else if (oneline) {
944 for (j = 0; j < ds->length; j++) {
945 int ch = ds->string[j];
948 fputs("\\n", stdout);
952 fputs("\\\\", stdout);
961 fputs(ds_cstr(ds), stdout);
963 ds_destroy(&c->output);
964 table_destroy(c->table);
967 shash_destroy_free_data(&c->options);
970 ovsdb_idl_txn_destroy(txn);
971 ovsdb_idl_destroy(idl);
976 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
977 * resources and return so that the caller can try again. */
979 ovsdb_idl_txn_abort(txn);
980 ovsdb_idl_txn_destroy(txn);
983 ovsdb_symbol_table_destroy(symtab);
984 for (c = commands; c < &commands[n_commands]; c++) {
985 ds_destroy(&c->output);
986 table_destroy(c->table);
992 /* Frees the current transaction and the underlying IDL and then calls
995 * Freeing the transaction and the IDL is not strictly necessary, but it makes
996 * for a clean memory leak report from valgrind in the normal case. That makes
997 * it easier to notice real memory leaks. */
999 sbctl_exit(int status)
1002 ovsdb_idl_txn_abort(the_idl_txn);
1003 ovsdb_idl_txn_destroy(the_idl_txn);
1005 ovsdb_idl_destroy(the_idl);
1009 static const struct ctl_command_syntax sbctl_commands[] = {
1010 /* Chassis commands. */
1011 {"chassis-add", 3, 3, "CHASSIS ENCAP-TYPE ENCAP-IP", pre_get_info,
1012 cmd_chassis_add, NULL, "--may-exist", RW},
1013 {"chassis-del", 1, 1, "CHASSIS", pre_get_info, cmd_chassis_del, NULL,
1016 /* Port binding commands. */
1017 {"lport-bind", 2, 2, "LPORT CHASSIS", pre_get_info, cmd_lport_bind, NULL,
1019 {"lport-unbind", 1, 1, "LPORT", pre_get_info, cmd_lport_unbind, NULL,
1022 /* Logical flow commands */
1023 {"lflow-list", 0, 1, "[DATAPATH]", pre_get_info, cmd_lflow_list, NULL,
1025 {"dump-flows", 0, 1, "[DATAPATH]", pre_get_info, cmd_lflow_list, NULL,
1026 "", RO}, /* Friendly alias for lflow-list */
1028 /* SSL commands (To Be Added). */
1030 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
1033 /* Registers sbctl and common db commands. */
1035 sbctl_cmd_init(void)
1037 ctl_init(tables, cmd_show_tables, sbctl_exit);
1038 ctl_register_commands(sbctl_commands);