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"
46 #include "openvswitch/vlog.h"
47 #include "ovn/lib/ovn-sb-idl.h"
49 VLOG_DEFINE_THIS_MODULE(sbctl
);
53 /* --db: The database server to contact. */
54 static const char *db
;
56 /* --oneline: Write each command's output as a single line? */
59 /* --dry-run: Do not commit any changes. */
62 /* --timeout: Time to wait for a connection to 'db'. */
65 /* Format for table output. */
66 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
68 /* The IDL we're using and the current transaction, if any.
69 * This is for use by sbctl_exit() only, to allow it to clean up.
70 * Other code should use its context arguments. */
71 static struct ovsdb_idl
*the_idl
;
72 static struct ovsdb_idl_txn
*the_idl_txn
;
73 OVS_NO_RETURN
static void sbctl_exit(int status
);
75 static void sbctl_cmd_init(void);
76 OVS_NO_RETURN
static void usage(void);
77 static void parse_options(int argc
, char *argv
[], struct shash
*local_options
);
78 static void run_prerequisites(struct ctl_command
[], size_t n_commands
,
80 static void do_sbctl(const char *args
, struct ctl_command
*, size_t n
,
84 main(int argc
, char *argv
[])
86 extern struct vlog_module VLM_reconnect
;
87 struct ovsdb_idl
*idl
;
88 struct ctl_command
*commands
;
89 struct shash local_options
;
94 set_program_name(argv
[0]);
95 fatal_ignore_sigpipe();
96 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
97 vlog_set_levels(&VLM_reconnect
, VLF_ANY_DESTINATION
, VLL_WARN
);
102 /* Log our arguments. This is often valuable for debugging systems. */
103 args
= process_escape_args(argv
);
104 VLOG(ctl_might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
106 /* Parse command line. */
107 shash_init(&local_options
);
108 parse_options(argc
, argv
, &local_options
);
109 commands
= ctl_parse_commands(argc
- optind
, argv
+ optind
, &local_options
,
116 /* Initialize IDL. */
117 idl
= the_idl
= ovsdb_idl_create(db
, &sbrec_idl_class
, false, false);
118 run_prerequisites(commands
, n_commands
, idl
);
120 /* Execute the commands.
122 * 'seqno' is the database sequence number for which we last tried to
123 * execute our transaction. There's no point in trying to commit more than
124 * once for any given sequence number, because if the transaction fails
125 * it's because the database changed and we need to obtain an up-to-date
126 * view of the database before we try the transaction again. */
127 seqno
= ovsdb_idl_get_seqno(idl
);
130 if (!ovsdb_idl_is_alive(idl
)) {
131 int retval
= ovsdb_idl_get_last_error(idl
);
132 ctl_fatal("%s: database connection failed (%s)",
133 db
, ovs_retval_to_string(retval
));
136 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
137 seqno
= ovsdb_idl_get_seqno(idl
);
138 do_sbctl(args
, commands
, n_commands
, idl
);
141 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
149 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
152 OPT_DB
= UCHAR_MAX
+ 1,
163 static const struct option global_long_options
[] = {
164 {"db", required_argument
, NULL
, OPT_DB
},
165 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
166 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
167 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
168 {"timeout", required_argument
, NULL
, 't'},
169 {"help", no_argument
, NULL
, 'h'},
170 {"commands", no_argument
, NULL
, OPT_COMMANDS
},
171 {"options", no_argument
, NULL
, OPT_OPTIONS
},
172 {"version", no_argument
, NULL
, 'V'},
177 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
178 char *tmp
, *short_options
;
180 struct option
*options
;
181 size_t allocated_options
;
185 tmp
= ovs_cmdl_long_options_to_short_options(global_long_options
);
186 short_options
= xasprintf("+%s", tmp
);
189 /* We want to parse both global and command-specific options here, but
190 * getopt_long() isn't too convenient for the job. We copy our global
191 * options into a dynamic array, then append all of the command-specific
193 options
= xmemdup(global_long_options
, sizeof global_long_options
);
194 allocated_options
= ARRAY_SIZE(global_long_options
);
195 n_options
= n_global_long_options
;
196 ctl_add_cmd_options(&options
, &n_options
, &allocated_options
, OPT_LOCAL
);
197 table_style
.format
= TF_LIST
;
203 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
218 vlog_set_levels(&VLM_sbctl
, VLF_SYSLOG
, VLL_WARN
);
226 if (shash_find(local_options
, options
[idx
].name
)) {
227 ctl_fatal("'%s' option specified multiple times",
230 shash_add_nocopy(local_options
,
231 xasprintf("--%s", options
[idx
].name
),
232 optarg
? xstrdup(optarg
) : NULL
);
239 ctl_print_commands();
242 ctl_print_options(global_long_options
);
245 ovs_print_version(0, 0);
246 printf("DB Schema %s\n", sbrec_get_db_version());
250 timeout
= strtoul(optarg
, NULL
, 10);
252 ctl_fatal("value %s on -t or --timeout is invalid",
258 TABLE_OPTION_HANDLERS(&table_style
)
270 db
= ctl_default_db();
273 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
274 free(CONST_CAST(char *, options
[i
].name
));
283 %s: ovs-vswitchd management utility\n\
285 for debugging and testing only, never use it in production\n\
287 usage: %s [OPTIONS] COMMAND [ARG...]\n\
289 SouthBound DB commands:\n\
290 show print overview of database contents\n\
293 chassis-add CHASSIS ENCAP-TYPE ENCAP-IP create a new chassis named\n\
294 CHASSIS with one encapsulation\n\
295 entry of ENCAP-TYPE and ENCAP-IP\n\
296 chassis-del CHASSIS delete CHASSIS and all of its encaps,\n\
299 Port binding commands:\n\
300 lport-bind LPORT CHASSIS bind logical port LPORT to CHASSIS\n\
301 lport-unbind LPORT reset the port binding of logical port LPORT\n\
303 Logical flow commands:\n\
304 lflow-list [DATAPATH] List logical flows for all or a single datapath\n\
305 dump-flows [DATAPATH] Alias for lflow-list\n\
310 --db=DATABASE connect to DATABASE\n\
312 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
313 --dry-run do not commit changes to database\n\
314 --oneline print exactly one line of output per command\n",
315 program_name
, program_name
, ctl_get_db_cmd_usage(), ctl_default_db());
318 --no-syslog equivalent to --verbose=sbctl:syslog:warn\n");
321 -h, --help display this help message\n\
322 -V, --version display version information\n");
327 /* ovs-sbctl specific context. Inherits the 'struct ctl_context' as base. */
328 struct sbctl_context
{
329 struct ctl_context base
;
331 /* A cache of the contents of the database.
333 * A command that needs to use any of this information must first call
334 * sbctl_context_populate_cache(). A command that changes anything that
335 * could invalidate the cache must either call
336 * sbctl_context_invalidate_cache() or manually update the cache to
337 * maintain its correctness. */
339 /* Maps from chassis name to struct sbctl_chassis. */
340 struct shash chassis
;
341 /* Maps from lport name to struct sbctl_port_binding. */
342 struct shash port_bindings
;
345 /* Casts 'base' into 'strcut sbctl_context'. */
346 static struct sbctl_context
*
347 sbctl_context_cast(struct ctl_context
*base
)
349 return CONTAINER_OF(base
, struct sbctl_context
, base
);
352 struct sbctl_chassis
{
353 const struct sbrec_chassis
*ch_cfg
;
356 struct sbctl_port_binding
{
357 const struct sbrec_port_binding
*bd_cfg
;
361 sbctl_context_invalidate_cache(struct ctl_context
*ctx
)
363 struct sbctl_context
*sbctl_ctx
= sbctl_context_cast(ctx
);
365 if (!sbctl_ctx
->cache_valid
) {
368 sbctl_ctx
->cache_valid
= false;
369 shash_destroy_free_data(&sbctl_ctx
->chassis
);
370 shash_destroy_free_data(&sbctl_ctx
->port_bindings
);
374 sbctl_context_populate_cache(struct ctl_context
*ctx
)
376 struct sbctl_context
*sbctl_ctx
= sbctl_context_cast(ctx
);
377 const struct sbrec_chassis
*chassis_rec
;
378 const struct sbrec_port_binding
*port_binding_rec
;
379 struct sset chassis
, port_bindings
;
381 if (sbctl_ctx
->cache_valid
) {
382 /* Cache is already populated. */
385 sbctl_ctx
->cache_valid
= true;
386 shash_init(&sbctl_ctx
->chassis
);
387 shash_init(&sbctl_ctx
->port_bindings
);
389 SBREC_CHASSIS_FOR_EACH(chassis_rec
, ctx
->idl
) {
390 struct sbctl_chassis
*ch
;
392 if (!sset_add(&chassis
, chassis_rec
->name
)) {
393 VLOG_WARN("database contains duplicate chassis name (%s)",
398 ch
= xmalloc(sizeof *ch
);
399 ch
->ch_cfg
= chassis_rec
;
400 shash_add(&sbctl_ctx
->chassis
, chassis_rec
->name
, ch
);
402 sset_destroy(&chassis
);
404 sset_init(&port_bindings
);
405 SBREC_PORT_BINDING_FOR_EACH(port_binding_rec
, ctx
->idl
) {
406 struct sbctl_port_binding
*bd
;
408 if (!sset_add(&port_bindings
, port_binding_rec
->logical_port
)) {
409 VLOG_WARN("database contains duplicate port binding for logical "
411 port_binding_rec
->logical_port
);
415 bd
= xmalloc(sizeof *bd
);
416 bd
->bd_cfg
= port_binding_rec
;
417 shash_add(&sbctl_ctx
->port_bindings
, port_binding_rec
->logical_port
,
420 sset_destroy(&port_bindings
);
424 check_conflicts(struct sbctl_context
*sbctl_ctx
, const char *name
,
427 if (shash_find(&sbctl_ctx
->chassis
, name
)) {
428 ctl_fatal("%s because a chassis named %s already exists",
434 static struct sbctl_chassis
*
435 find_chassis(struct sbctl_context
*sbctl_ctx
, const char *name
,
438 struct sbctl_chassis
*sbctl_ch
;
440 ovs_assert(sbctl_ctx
->cache_valid
);
442 sbctl_ch
= shash_find_data(&sbctl_ctx
->chassis
, name
);
443 if (must_exist
&& !sbctl_ch
) {
444 ctl_fatal("no chassis named %s", name
);
450 static struct sbctl_port_binding
*
451 find_port_binding(struct sbctl_context
*sbctl_ctx
, const char *name
,
454 struct sbctl_port_binding
*bd
;
456 ovs_assert(sbctl_ctx
->cache_valid
);
458 bd
= shash_find_data(&sbctl_ctx
->port_bindings
, name
);
459 if (must_exist
&& !bd
) {
460 ctl_fatal("no port named %s", name
);
467 pre_get_info(struct ctl_context
*ctx
)
469 ovsdb_idl_add_column(ctx
->idl
, &sbrec_chassis_col_name
);
470 ovsdb_idl_add_column(ctx
->idl
, &sbrec_chassis_col_encaps
);
472 ovsdb_idl_add_column(ctx
->idl
, &sbrec_encap_col_type
);
473 ovsdb_idl_add_column(ctx
->idl
, &sbrec_encap_col_ip
);
475 ovsdb_idl_add_column(ctx
->idl
, &sbrec_port_binding_col_logical_port
);
476 ovsdb_idl_add_column(ctx
->idl
, &sbrec_port_binding_col_chassis
);
478 ovsdb_idl_add_column(ctx
->idl
, &sbrec_logical_flow_col_logical_datapath
);
479 ovsdb_idl_add_column(ctx
->idl
, &sbrec_logical_flow_col_pipeline
);
480 ovsdb_idl_add_column(ctx
->idl
, &sbrec_logical_flow_col_actions
);
481 ovsdb_idl_add_column(ctx
->idl
, &sbrec_logical_flow_col_priority
);
482 ovsdb_idl_add_column(ctx
->idl
, &sbrec_logical_flow_col_table_id
);
483 ovsdb_idl_add_column(ctx
->idl
, &sbrec_logical_flow_col_match
);
486 static struct cmd_show_table cmd_show_tables
[] = {
487 {&sbrec_table_chassis
,
488 &sbrec_chassis_col_name
,
489 {&sbrec_chassis_col_encaps
,
494 &sbrec_encap_col_type
,
495 {&sbrec_encap_col_ip
,
496 &sbrec_encap_col_options
,
499 {NULL
, NULL
, {NULL
, NULL
, NULL
}},
503 cmd_chassis_add(struct ctl_context
*ctx
)
505 struct sbctl_context
*sbctl_ctx
= sbctl_context_cast(ctx
);
506 struct sbrec_chassis
*ch
;
507 struct sbrec_encap
*encap
;
508 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
509 const char *ch_name
, *encap_type
, *encap_ip
;
511 ch_name
= ctx
->argv
[1];
512 encap_type
= ctx
->argv
[2];
513 encap_ip
= ctx
->argv
[3];
515 sbctl_context_populate_cache(ctx
);
517 struct sbctl_chassis
*sbctl_ch
;
519 sbctl_ch
= find_chassis(sbctl_ctx
, ch_name
, false);
524 check_conflicts(sbctl_ctx
, ch_name
,
525 xasprintf("cannot create a chassis named %s", ch_name
));
526 ch
= sbrec_chassis_insert(ctx
->txn
);
527 sbrec_chassis_set_name(ch
, ch_name
);
528 encap
= sbrec_encap_insert(ctx
->txn
);
529 sbrec_encap_set_type(encap
, encap_type
);
530 sbrec_encap_set_ip(encap
, encap_ip
);
531 sbrec_chassis_set_encaps(ch
, &encap
, 1);
532 sbctl_context_invalidate_cache(ctx
);
536 cmd_chassis_del(struct ctl_context
*ctx
)
538 struct sbctl_context
*sbctl_ctx
= sbctl_context_cast(ctx
);
539 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
540 struct sbctl_chassis
*sbctl_ch
;
542 sbctl_context_populate_cache(ctx
);
543 sbctl_ch
= find_chassis(sbctl_ctx
, ctx
->argv
[1], must_exist
);
545 if (sbctl_ch
->ch_cfg
) {
546 sbrec_chassis_delete(sbctl_ch
->ch_cfg
);
548 shash_find_and_delete(&sbctl_ctx
->chassis
, ctx
->argv
[1]);
554 cmd_lport_bind(struct ctl_context
*ctx
)
556 struct sbctl_context
*sbctl_ctx
= sbctl_context_cast(ctx
);
557 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
558 struct sbctl_chassis
*sbctl_ch
;
559 struct sbctl_port_binding
*sbctl_bd
;
560 char *lport_name
, *ch_name
;
562 /* port_binding must exist, chassis must exist! */
563 lport_name
= ctx
->argv
[1];
564 ch_name
= ctx
->argv
[2];
566 sbctl_context_populate_cache(ctx
);
567 sbctl_bd
= find_port_binding(sbctl_ctx
, lport_name
, true);
568 sbctl_ch
= find_chassis(sbctl_ctx
, ch_name
, true);
570 if (sbctl_bd
->bd_cfg
->chassis
) {
571 if (may_exist
&& sbctl_bd
->bd_cfg
->chassis
== sbctl_ch
->ch_cfg
) {
574 ctl_fatal("lport (%s) has already been binded to chassis (%s)",
575 lport_name
, sbctl_bd
->bd_cfg
->chassis
->name
);
578 sbrec_port_binding_set_chassis(sbctl_bd
->bd_cfg
, sbctl_ch
->ch_cfg
);
579 sbctl_context_invalidate_cache(ctx
);
583 cmd_lport_unbind(struct ctl_context
*ctx
)
585 struct sbctl_context
*sbctl_ctx
= sbctl_context_cast(ctx
);
586 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
587 struct sbctl_port_binding
*sbctl_bd
;
590 lport_name
= ctx
->argv
[1];
591 sbctl_context_populate_cache(ctx
);
592 sbctl_bd
= find_port_binding(sbctl_ctx
, lport_name
, must_exist
);
594 sbrec_port_binding_set_chassis(sbctl_bd
->bd_cfg
, NULL
);
603 /* Help ensure we catch any future pipeline values */
605 pipeline_encode(const char *pl
)
607 if (!strcmp(pl
, "ingress")) {
609 } else if (!strcmp(pl
, "egress")) {
617 lflow_cmp(const void *lf1_
, const void *lf2_
)
619 const struct sbrec_logical_flow
*lf1
, *lf2
;
621 lf1
= *((struct sbrec_logical_flow
**) lf1_
);
622 lf2
= *((struct sbrec_logical_flow
**) lf2_
);
624 int pl1
= pipeline_encode(lf1
->pipeline
);
625 int pl2
= pipeline_encode(lf2
->pipeline
);
636 CMP(uuid_compare_3way(&lf1
->logical_datapath
->header_
.uuid
,
637 &lf2
->logical_datapath
->header_
.uuid
));
639 CMP(lf1
->table_id
> lf2
->table_id
? 1 :
640 (lf1
->table_id
< lf2
->table_id
? -1 : 0));
641 CMP(lf1
->priority
> lf2
->priority
? -1 :
642 (lf1
->priority
< lf2
->priority
? 1 : 0));
643 CMP(strcmp(lf1
->match
, lf2
->match
));
651 cmd_lflow_list(struct ctl_context
*ctx
)
653 const char *datapath
= ctx
->argc
== 2 ? ctx
->argv
[1] : NULL
;
654 struct uuid datapath_uuid
= { .parts
= { 0, }};
655 const struct sbrec_logical_flow
**lflows
;
656 const struct sbrec_logical_flow
*lflow
;
657 size_t n_flows
= 0, n_capacity
= 64;
659 if (datapath
&& !uuid_from_string(&datapath_uuid
, datapath
)) {
660 VLOG_ERR("Invalid format of datapath UUID");
664 lflows
= xmalloc(sizeof *lflows
* n_capacity
);
665 SBREC_LOGICAL_FLOW_FOR_EACH (lflow
, ctx
->idl
) {
666 if (n_flows
== n_capacity
) {
667 lflows
= x2nrealloc(lflows
, &n_capacity
, sizeof *lflows
);
669 lflows
[n_flows
] = lflow
;
673 qsort(lflows
, n_flows
, sizeof *lflows
, lflow_cmp
);
675 const char *cur_pipeline
= "";
677 for (i
= 0; i
< n_flows
; i
++) {
679 if (datapath
&& !uuid_equals(&datapath_uuid
,
680 &lflow
->logical_datapath
->header_
.uuid
)) {
683 if (strcmp(cur_pipeline
, lflow
->pipeline
)) {
684 printf("Datapath: " UUID_FMT
" Pipeline: %s\n",
685 UUID_ARGS(&lflow
->logical_datapath
->header_
.uuid
),
687 cur_pipeline
= lflow
->pipeline
;
689 printf(" table_id=%" PRId64
", priority=%3" PRId64
", "
690 "match=(%s), action=(%s)\n",
691 lflow
->table_id
, lflow
->priority
,
692 lflow
->match
, lflow
->actions
);
699 static const struct ctl_table_class tables
[] = {
700 {&sbrec_table_chassis
,
701 {{&sbrec_table_chassis
, &sbrec_chassis_col_name
, NULL
},
702 {NULL
, NULL
, NULL
}}},
706 {NULL
, NULL
, NULL
}}},
708 {&sbrec_table_logical_flow
,
709 {{&sbrec_table_logical_flow
, NULL
,
710 &sbrec_logical_flow_col_logical_datapath
},
711 {NULL
, NULL
, NULL
}}},
713 {&sbrec_table_multicast_group
,
715 {NULL
, NULL
, NULL
}}},
717 {&sbrec_table_datapath_binding
,
719 {NULL
, NULL
, NULL
}}},
721 {&sbrec_table_port_binding
,
722 {{&sbrec_table_port_binding
, &sbrec_port_binding_col_logical_port
, NULL
},
723 {NULL
, NULL
, NULL
}}},
725 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
730 sbctl_context_init_command(struct sbctl_context
*sbctl_ctx
,
731 struct ctl_command
*command
)
733 ctl_context_init_command(&sbctl_ctx
->base
, command
);
737 sbctl_context_init(struct sbctl_context
*sbctl_ctx
,
738 struct ctl_command
*command
, struct ovsdb_idl
*idl
,
739 struct ovsdb_idl_txn
*txn
,
740 struct ovsdb_symbol_table
*symtab
)
742 ctl_context_init(&sbctl_ctx
->base
, command
, idl
, txn
, symtab
,
743 sbctl_context_invalidate_cache
);
744 sbctl_ctx
->cache_valid
= false;
748 sbctl_context_done_command(struct sbctl_context
*sbctl_ctx
,
749 struct ctl_command
*command
)
751 ctl_context_done_command(&sbctl_ctx
->base
, command
);
755 sbctl_context_done(struct sbctl_context
*sbctl_ctx
,
756 struct ctl_command
*command
)
758 ctl_context_done(&sbctl_ctx
->base
, command
);
762 run_prerequisites(struct ctl_command
*commands
, size_t n_commands
,
763 struct ovsdb_idl
*idl
)
765 struct ctl_command
*c
;
767 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
768 if (c
->syntax
->prerequisites
) {
769 struct sbctl_context sbctl_ctx
;
774 sbctl_context_init(&sbctl_ctx
, c
, idl
, NULL
, NULL
);
775 (c
->syntax
->prerequisites
)(&sbctl_ctx
.base
);
776 sbctl_context_done(&sbctl_ctx
, c
);
778 ovs_assert(!c
->output
.string
);
779 ovs_assert(!c
->table
);
785 do_sbctl(const char *args
, struct ctl_command
*commands
, size_t n_commands
,
786 struct ovsdb_idl
*idl
)
788 struct ovsdb_idl_txn
*txn
;
789 enum ovsdb_idl_txn_status status
;
790 struct ovsdb_symbol_table
*symtab
;
791 struct sbctl_context sbctl_ctx
;
792 struct ctl_command
*c
;
793 struct shash_node
*node
;
796 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
798 ovsdb_idl_txn_set_dry_run(txn
);
801 ovsdb_idl_txn_add_comment(txn
, "ovs-sbctl: %s", args
);
803 symtab
= ovsdb_symbol_table_create();
804 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
808 sbctl_context_init(&sbctl_ctx
, NULL
, idl
, txn
, symtab
);
809 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
810 sbctl_context_init_command(&sbctl_ctx
, c
);
811 if (c
->syntax
->run
) {
812 (c
->syntax
->run
)(&sbctl_ctx
.base
);
814 sbctl_context_done_command(&sbctl_ctx
, c
);
816 if (sbctl_ctx
.base
.try_again
) {
817 sbctl_context_done(&sbctl_ctx
, NULL
);
821 sbctl_context_done(&sbctl_ctx
, NULL
);
823 SHASH_FOR_EACH (node
, &symtab
->sh
) {
824 struct ovsdb_symbol
*symbol
= node
->data
;
825 if (!symbol
->created
) {
826 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
827 "with \"-- --id=%s create ...\")",
828 node
->name
, node
->name
);
830 if (!symbol
->strong_ref
) {
831 if (!symbol
->weak_ref
) {
832 VLOG_WARN("row id \"%s\" was created but no reference to it "
833 "was inserted, so it will not actually appear in "
834 "the database", node
->name
);
836 VLOG_WARN("row id \"%s\" was created but only a weak "
837 "reference to it was inserted, so it will not "
838 "actually appear in the database", node
->name
);
843 status
= ovsdb_idl_txn_commit_block(txn
);
844 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
845 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
846 if (c
->syntax
->postprocess
) {
847 sbctl_context_init(&sbctl_ctx
, c
, idl
, txn
, symtab
);
848 (c
->syntax
->postprocess
)(&sbctl_ctx
.base
);
849 sbctl_context_done(&sbctl_ctx
, c
);
853 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
856 case TXN_UNCOMMITTED
:
861 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
862 ctl_fatal("transaction aborted");
872 ctl_fatal("transaction error: %s", error
);
875 /* Should not happen--we never call ovsdb_idl_set_lock(). */
876 ctl_fatal("database not locked");
883 ovsdb_symbol_table_destroy(symtab
);
885 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
886 struct ds
*ds
= &c
->output
;
889 table_print(c
->table
, &table_style
);
890 } else if (oneline
) {
894 for (j
= 0; j
< ds
->length
; j
++) {
895 int ch
= ds
->string
[j
];
898 fputs("\\n", stdout
);
902 fputs("\\\\", stdout
);
911 fputs(ds_cstr(ds
), stdout
);
913 ds_destroy(&c
->output
);
914 table_destroy(c
->table
);
917 shash_destroy_free_data(&c
->options
);
920 ovsdb_idl_txn_destroy(txn
);
921 ovsdb_idl_destroy(idl
);
926 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
927 * resources and return so that the caller can try again. */
929 ovsdb_idl_txn_abort(txn
);
930 ovsdb_idl_txn_destroy(txn
);
933 ovsdb_symbol_table_destroy(symtab
);
934 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
935 ds_destroy(&c
->output
);
936 table_destroy(c
->table
);
942 /* Frees the current transaction and the underlying IDL and then calls
945 * Freeing the transaction and the IDL is not strictly necessary, but it makes
946 * for a clean memory leak report from valgrind in the normal case. That makes
947 * it easier to notice real memory leaks. */
949 sbctl_exit(int status
)
952 ovsdb_idl_txn_abort(the_idl_txn
);
953 ovsdb_idl_txn_destroy(the_idl_txn
);
955 ovsdb_idl_destroy(the_idl
);
959 static const struct ctl_command_syntax sbctl_commands
[] = {
960 /* Chassis commands. */
961 {"chassis-add", 3, 3, "CHASSIS ENCAP-TYPE ENCAP-IP", pre_get_info
,
962 cmd_chassis_add
, NULL
, "--may-exist", RW
},
963 {"chassis-del", 1, 1, "CHASSIS", pre_get_info
, cmd_chassis_del
, NULL
,
966 /* Port binding commands. */
967 {"lport-bind", 2, 2, "LPORT CHASSIS", pre_get_info
, cmd_lport_bind
, NULL
,
969 {"lport-unbind", 1, 1, "LPORT", pre_get_info
, cmd_lport_unbind
, NULL
,
972 /* Logical flow commands */
973 {"lflow-list", 0, 1, "[DATAPATH]", pre_get_info
, cmd_lflow_list
, NULL
,
975 {"dump-flows", 0, 1, "[DATAPATH]", pre_get_info
, cmd_lflow_list
, NULL
,
976 "", RO
}, /* Friendly alias for lflow-list */
978 /* SSL commands (To Be Added). */
980 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
983 /* Registers sbctl and common db commands. */
987 ctl_init(tables
, cmd_show_tables
, sbctl_exit
);
988 ctl_register_commands(sbctl_commands
);