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.
23 #include "db-ctl-base.h"
25 #include "command-line.h"
28 #include "dynamic-string.h"
29 #include "fatal-signal.h"
32 #include "openvswitch/vlog.h"
33 #include "ovsdb-data.h"
34 #include "ovsdb-idl.h"
35 #include "ovsdb-idl-provider.h"
41 VLOG_DEFINE_THIS_MODULE(db_ctl_base
);
43 /* The IDL we're using and the current transaction, if any.
44 * This is for use by ctl_exit() only, to allow it to clean up.
45 * Other code should use its context arguments. */
46 struct ovsdb_idl
*the_idl
;
47 struct ovsdb_idl_txn
*the_idl_txn
;
49 /* Represents all tables in the schema. User must define 'tables'
50 * in implementation and supply via clt_init(). The definition must end
51 * with an all-NULL entry. */
52 static const struct ctl_table_class
*tables
;
54 static struct shash all_commands
= SHASH_INITIALIZER(&all_commands
);
55 static const struct ctl_table_class
*get_table(const char *table_name
);
56 static void set_column(const struct ctl_table_class
*,
57 const struct ovsdb_idl_row
*, const char *,
58 struct ovsdb_symbol_table
*);
61 static struct option
*
62 find_option(const char *name
, struct option
*options
, size_t n_options
)
66 for (i
= 0; i
< n_options
; i
++) {
67 if (!strcmp(options
[i
].name
, name
)) {
74 static struct option
*
75 add_option(struct option
**optionsp
, size_t *n_optionsp
,
76 size_t *allocated_optionsp
)
78 if (*n_optionsp
>= *allocated_optionsp
) {
79 *optionsp
= x2nrealloc(*optionsp
, allocated_optionsp
,
82 return &(*optionsp
)[(*n_optionsp
)++];
85 /* Converts the command arguments into format that can be parsed by
86 * bash completion script.
88 * Therein, arguments will be attached with following prefixes:
90 * !argument :: The argument is required
91 * ?argument :: The argument is optional
92 * *argument :: The argument may appear any number (0 or more) times
93 * +argument :: The argument may appear one or more times
97 print_command_arguments(const struct ctl_command_syntax
*command
)
100 * The argument string is parsed in reverse. We use a stack 'oew_stack' to
101 * keep track of nested optionals. Whenever a ']' is encountered, we push
102 * a bit to 'oew_stack'. The bit is set to 1 if the ']' is not nested.
103 * Subsequently, we pop an entry everytime '[' is met.
105 * We use 'whole_word_is_optional' value to decide whether or not a ! or +
106 * should be added on encountering a space: if the optional surrounds the
107 * whole word then it shouldn't be, but if it is only a part of the word
108 * (i.e. [key=]value), it should be.
110 uint32_t oew_stack
= 0;
112 const char *arguments
= command
->arguments
;
113 int length
= strlen(arguments
);
118 /* Output buffer, written backward from end. */
119 char *output
= xmalloc(2 * length
);
120 char *outp
= output
+ 2 * length
;
123 bool in_repeated
= false;
124 bool whole_word_is_optional
= false;
126 for (const char *inp
= arguments
+ length
; inp
> arguments
; ) {
130 if (inp
[1] == '\0' || inp
[1] == ' ' || inp
[1] == '.') {
135 /* Checks if the whole word is optional, and sets the
136 * 'whole_word_is_optional' accordingly. */
137 if ((inp
== arguments
|| inp
[-1] == ' ') && oew_stack
& 1) {
138 *--outp
= in_repeated
? '*' : '?';
139 whole_word_is_optional
= true;
142 whole_word_is_optional
= false;
147 if (!whole_word_is_optional
) {
148 *--outp
= in_repeated
? '+' : '!';
152 whole_word_is_optional
= false;
162 if (arguments
[0] != '[' && outp
!= output
+ 2 * length
- 1) {
163 *--outp
= in_repeated
? '+' : '!';
170 die_if_error(char *error
)
173 ctl_fatal("%s", error
);
178 to_lower_and_underscores(unsigned c
)
180 return c
== '-' ? '_' : tolower(c
);
184 score_partial_match(const char *name
, const char *s
)
188 if (!strcmp(name
, s
)) {
191 for (score
= 0; ; score
++, name
++, s
++) {
192 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
194 } else if (*name
== '\0') {
198 return *s
== '\0' ? score
: 0;
201 static struct ovsdb_symbol
*
202 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
204 struct ovsdb_symbol
*symbol
;
207 ctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
211 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
214 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
215 if (symbol
->created
) {
216 ctl_fatal("row id \"%s\" may only be specified on one --id option",
219 symbol
->created
= true;
223 static const struct ovsdb_idl_row
*
224 get_row_by_id(struct ctl_context
*ctx
, const struct ctl_table_class
*table
,
225 const struct ctl_row_id
*id
, const char *record_id
)
227 const struct ovsdb_idl_row
*referrer
, *final
;
233 if (!id
->name_column
) {
234 if (strcmp(record_id
, ".")) {
237 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
238 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
242 const struct ovsdb_idl_row
*row
;
245 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
247 row
= ovsdb_idl_next_row(row
))
249 const struct ovsdb_datum
*name
;
251 name
= ovsdb_idl_get(row
, id
->name_column
,
252 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
253 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
255 ctl_fatal("multiple rows in %s match \"%s\"",
256 table
->class->name
, record_id
);
267 if (id
->uuid_column
) {
268 const struct ovsdb_datum
*uuid
;
270 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
271 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
272 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
274 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
275 &uuid
->keys
[0].uuid
);
284 static const struct ovsdb_idl_row
*
285 get_row(struct ctl_context
*ctx
,
286 const struct ctl_table_class
*table
, const char *record_id
,
289 const struct ovsdb_idl_row
*row
;
293 if (uuid_from_string(&uuid
, record_id
)) {
294 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
299 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
300 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
306 if (must_exist
&& !row
) {
307 ctl_fatal("no row \"%s\" in table %s",
308 record_id
, table
->class->name
);
314 get_column(const struct ctl_table_class
*table
, const char *column_name
,
315 const struct ovsdb_idl_column
**columnp
)
317 const struct ovsdb_idl_column
*best_match
= NULL
;
318 unsigned int best_score
= 0;
321 for (i
= 0; i
< table
->class->n_columns
; i
++) {
322 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
323 unsigned int score
= score_partial_match(column
->name
, column_name
);
324 if (score
> best_score
) {
327 } else if (score
== best_score
) {
332 *columnp
= best_match
;
335 } else if (best_score
) {
336 return xasprintf("%s contains more than one column whose name "
337 "matches \"%s\"", table
->class->name
, column_name
);
339 return xasprintf("%s does not contain a column whose name matches "
340 "\"%s\"", table
->class->name
, column_name
);
345 pre_get_column(struct ctl_context
*ctx
,
346 const struct ctl_table_class
*table
, const char *column_name
,
347 const struct ovsdb_idl_column
**columnp
)
349 die_if_error(get_column(table
, column_name
, columnp
));
350 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
353 static const struct ctl_table_class
*
354 pre_get_table(struct ctl_context
*ctx
, const char *table_name
)
356 const struct ctl_table_class
*table_class
;
359 table_class
= get_table(table_name
);
360 ovsdb_idl_add_table(ctx
->idl
, table_class
->class);
362 for (i
= 0; i
< ARRAY_SIZE(table_class
->row_ids
); i
++) {
363 const struct ctl_row_id
*id
= &table_class
->row_ids
[i
];
365 ovsdb_idl_add_table(ctx
->idl
, id
->table
);
367 if (id
->name_column
) {
368 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
370 if (id
->uuid_column
) {
371 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
379 missing_operator_error(const char *arg
, const char **allowed_operators
,
385 ds_put_format(&s
, "%s: argument does not end in ", arg
);
386 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
387 if (n_allowed
== 2) {
388 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
389 } else if (n_allowed
> 2) {
392 for (i
= 1; i
< n_allowed
- 1; i
++) {
393 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
395 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
397 ds_put_format(&s
, " followed by a value.");
399 return ds_steal_cstr(&s
);
402 /* Breaks 'arg' apart into a number of fields in the following order:
404 * - The name of a column in 'table', stored into '*columnp'. The column
405 * name may be abbreviated.
407 * - Optionally ':' followed by a key string. The key is stored as a
408 * malloc()'d string into '*keyp', or NULL if no key is present in
411 * - If 'valuep' is nonnull, an operator followed by a value string. The
412 * allowed operators are the 'n_allowed' string in 'allowed_operators',
413 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
414 * index of the operator within 'allowed_operators' is stored into
415 * '*operatorp'. The value is stored as a malloc()'d string into
416 * '*valuep', or NULL if no value is present in 'arg'.
418 * On success, returns NULL. On failure, returned a malloc()'d string error
419 * message and stores NULL into all of the nonnull output arguments. */
420 static char * OVS_WARN_UNUSED_RESULT
421 parse_column_key_value(const char *arg
,
422 const struct ctl_table_class
*table
,
423 const struct ovsdb_idl_column
**columnp
, char **keyp
,
425 const char **allowed_operators
, size_t n_allowed
,
432 ovs_assert(!(operatorp
&& !valuep
));
438 /* Parse column name. */
439 error
= ovsdb_token_parse(&p
, &column_name
);
443 if (column_name
[0] == '\0') {
445 error
= xasprintf("%s: missing column name", arg
);
448 error
= get_column(table
, column_name
, columnp
);
454 /* Parse key string. */
457 error
= ovsdb_token_parse(&p
, keyp
);
463 /* Parse value string. */
469 if (!allowed_operators
) {
470 static const char *equals
= "=";
471 allowed_operators
= &equals
;
477 for (i
= 0; i
< n_allowed
; i
++) {
478 const char *op
= allowed_operators
[i
];
479 size_t op_len
= strlen(op
);
481 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
487 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
494 *valuep
= xstrdup(p
+ best_len
);
497 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
518 static const struct ovsdb_idl_column
*
519 pre_parse_column_key_value(struct ctl_context
*ctx
,
521 const struct ctl_table_class
*table
)
523 const struct ovsdb_idl_column
*column
;
528 die_if_error(ovsdb_token_parse(&p
, &column_name
));
529 if (column_name
[0] == '\0') {
530 ctl_fatal("%s: missing column name", arg
);
533 pre_get_column(ctx
, table
, column_name
, &column
);
540 check_mutable(const struct ovsdb_idl_row
*row
,
541 const struct ovsdb_idl_column
*column
)
543 if (!ovsdb_idl_is_mutable(row
, column
)) {
544 ctl_fatal("cannot modify read-only column %s in table %s",
545 column
->name
, row
->table
->class->name
);
550 RELOP(RELOP_EQ, "=") \
551 RELOP(RELOP_NE, "!=") \
552 RELOP(RELOP_LT, "<") \
553 RELOP(RELOP_GT, ">") \
554 RELOP(RELOP_LE, "<=") \
555 RELOP(RELOP_GE, ">=") \
556 RELOP(RELOP_SET_EQ, "{=}") \
557 RELOP(RELOP_SET_NE, "{!=}") \
558 RELOP(RELOP_SET_LT, "{<}") \
559 RELOP(RELOP_SET_GT, "{>}") \
560 RELOP(RELOP_SET_LE, "{<=}") \
561 RELOP(RELOP_SET_GE, "{>=}")
564 #define RELOP(ENUM, STRING) ENUM,
570 is_set_operator(enum relop op
)
572 return (op
== RELOP_SET_EQ
|| op
== RELOP_SET_NE
||
573 op
== RELOP_SET_LT
|| op
== RELOP_SET_GT
||
574 op
== RELOP_SET_LE
|| op
== RELOP_SET_GE
);
578 evaluate_relop(const struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
579 const struct ovsdb_type
*type
, enum relop op
)
584 return ovsdb_datum_compare_3way(a
, b
, type
) == 0;
587 return ovsdb_datum_compare_3way(a
, b
, type
) != 0;
589 return ovsdb_datum_compare_3way(a
, b
, type
) < 0;
591 return ovsdb_datum_compare_3way(a
, b
, type
) > 0;
593 return ovsdb_datum_compare_3way(a
, b
, type
) <= 0;
595 return ovsdb_datum_compare_3way(a
, b
, type
) >= 0;
598 return b
->n
> a
->n
&& ovsdb_datum_includes_all(a
, b
, type
);
600 return a
->n
> b
->n
&& ovsdb_datum_includes_all(b
, a
, type
);
602 return ovsdb_datum_includes_all(a
, b
, type
);
604 return ovsdb_datum_includes_all(b
, a
, type
);
612 is_condition_satisfied(const struct ctl_table_class
*table
,
613 const struct ovsdb_idl_row
*row
, const char *arg
,
614 struct ovsdb_symbol_table
*symtab
)
616 static const char *operators
[] = {
617 #define RELOP(ENUM, STRING) STRING,
622 const struct ovsdb_idl_column
*column
;
623 const struct ovsdb_datum
*have_datum
;
624 char *key_string
, *value_string
;
625 struct ovsdb_type type
;
630 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
631 &operator, operators
, ARRAY_SIZE(operators
),
635 ctl_fatal("%s: missing value", arg
);
639 type
.n_max
= UINT_MAX
;
641 have_datum
= ovsdb_idl_read(row
, column
);
643 union ovsdb_atom want_key
;
644 struct ovsdb_datum b
;
647 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
648 ctl_fatal("cannot specify key to check for non-map column %s",
652 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
653 key_string
, symtab
));
655 type
.key
= type
.value
;
656 type
.value
.type
= OVSDB_TYPE_VOID
;
657 die_if_error(ovsdb_datum_from_string(&b
, &type
, value_string
, symtab
));
659 idx
= ovsdb_datum_find_key(have_datum
,
660 &want_key
, column
->type
.key
.type
);
661 if (idx
== UINT_MAX
&& !is_set_operator(operator)) {
664 struct ovsdb_datum a
;
666 if (idx
!= UINT_MAX
) {
668 a
.keys
= &have_datum
->values
[idx
];
676 retval
= evaluate_relop(&a
, &b
, &type
, operator);
679 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
680 ovsdb_datum_destroy(&b
, &type
);
682 struct ovsdb_datum want_datum
;
684 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
685 value_string
, symtab
));
686 retval
= evaluate_relop(have_datum
, &want_datum
, &type
, operator);
687 ovsdb_datum_destroy(&want_datum
, &column
->type
);
697 invalidate_cache(struct ctl_context
*ctx
)
699 if (ctx
->invalidate_cache
) {
700 (ctx
->invalidate_cache
)(ctx
);
705 pre_cmd_get(struct ctl_context
*ctx
)
707 const char *id
= shash_find_data(&ctx
->options
, "--id");
708 const char *table_name
= ctx
->argv
[1];
709 const struct ctl_table_class
*table
;
712 /* Using "get" without --id or a column name could possibly make sense.
713 * Maybe, for example, a *ctl command run wants to assert that a row
714 * exists. But it is unlikely that an interactive user would want to do
715 * that, so issue a warning if we're running on a terminal. */
716 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
717 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
718 "possibly erroneous");
721 table
= pre_get_table(ctx
, table_name
);
722 for (i
= 3; i
< ctx
->argc
; i
++) {
723 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
724 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
728 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
733 cmd_get(struct ctl_context
*ctx
)
735 const char *id
= shash_find_data(&ctx
->options
, "--id");
736 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
737 const char *table_name
= ctx
->argv
[1];
738 const char *record_id
= ctx
->argv
[2];
739 const struct ctl_table_class
*table
;
740 const struct ovsdb_idl_row
*row
;
741 struct ds
*out
= &ctx
->output
;
744 if (id
&& !must_exist
) {
745 ctl_fatal("--if-exists and --id may not be specified together");
748 table
= get_table(table_name
);
749 row
= get_row(ctx
, table
, record_id
, must_exist
);
755 struct ovsdb_symbol
*symbol
;
758 symbol
= create_symbol(ctx
->symtab
, id
, &new);
760 ctl_fatal("row id \"%s\" specified on \"get\" command was used "
761 "before it was defined", id
);
763 symbol
->uuid
= row
->uuid
;
765 /* This symbol refers to a row that already exists, so disable warnings
766 * about it being unreferenced. */
767 symbol
->strong_ref
= true;
769 for (i
= 3; i
< ctx
->argc
; i
++) {
770 const struct ovsdb_idl_column
*column
;
771 const struct ovsdb_datum
*datum
;
774 /* Special case for obtaining the UUID of a row. We can't just do this
775 * through parse_column_key_value() below since it returns a "struct
776 * ovsdb_idl_column" and the UUID column doesn't have one. */
777 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
778 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
779 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
783 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
784 &column
, &key_string
,
785 NULL
, NULL
, 0, NULL
));
787 ovsdb_idl_txn_verify(row
, column
);
788 datum
= ovsdb_idl_read(row
, column
);
790 union ovsdb_atom key
;
793 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
794 ctl_fatal("cannot specify key to get for non-map column %s",
798 die_if_error(ovsdb_atom_from_string(&key
,
800 key_string
, ctx
->symtab
));
802 idx
= ovsdb_datum_find_key(datum
, &key
,
803 column
->type
.key
.type
);
804 if (idx
== UINT_MAX
) {
806 ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
807 key_string
, table
->class->name
, record_id
,
811 ovsdb_atom_to_string(&datum
->values
[idx
],
812 column
->type
.value
.type
, out
);
814 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
816 ovsdb_datum_to_string(datum
, &column
->type
, out
);
818 ds_put_char(out
, '\n');
825 parse_column_names(const char *column_names
,
826 const struct ctl_table_class
*table
,
827 const struct ovsdb_idl_column
***columnsp
,
830 const struct ovsdb_idl_column
**columns
;
836 n_columns
= table
->class->n_columns
+ 1;
837 columns
= xmalloc(n_columns
* sizeof *columns
);
839 for (i
= 0; i
< table
->class->n_columns
; i
++) {
840 columns
[i
+ 1] = &table
->class->columns
[i
];
843 char *s
= xstrdup(column_names
);
844 size_t allocated_columns
;
845 char *save_ptr
= NULL
;
849 allocated_columns
= n_columns
= 0;
850 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
851 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
852 const struct ovsdb_idl_column
*column
;
854 if (!strcasecmp(column_name
, "_uuid")) {
857 die_if_error(get_column(table
, column_name
, &column
));
859 if (n_columns
>= allocated_columns
) {
860 columns
= x2nrealloc(columns
, &allocated_columns
,
863 columns
[n_columns
++] = column
;
868 ctl_fatal("must specify at least one column name");
872 *n_columnsp
= n_columns
;
876 pre_list_columns(struct ctl_context
*ctx
,
877 const struct ctl_table_class
*table
,
878 const char *column_names
)
880 const struct ovsdb_idl_column
**columns
;
884 parse_column_names(column_names
, table
, &columns
, &n_columns
);
885 for (i
= 0; i
< n_columns
; i
++) {
887 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
894 pre_cmd_list(struct ctl_context
*ctx
)
896 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
897 const char *table_name
= ctx
->argv
[1];
898 const struct ctl_table_class
*table
;
900 table
= pre_get_table(ctx
, table_name
);
901 pre_list_columns(ctx
, table
, column_names
);
904 static struct table
*
905 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
910 out
= xmalloc(sizeof *out
);
913 for (i
= 0; i
< n_columns
; i
++) {
914 const struct ovsdb_idl_column
*column
= columns
[i
];
915 const char *column_name
= column
? column
->name
: "_uuid";
917 table_add_column(out
, "%s", column_name
);
924 list_record(const struct ovsdb_idl_row
*row
,
925 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
935 for (i
= 0; i
< n_columns
; i
++) {
936 const struct ovsdb_idl_column
*column
= columns
[i
];
937 struct cell
*cell
= table_add_cell(out
);
940 struct ovsdb_datum datum
;
941 union ovsdb_atom atom
;
943 atom
.uuid
= row
->uuid
;
949 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
950 cell
->type
= &ovsdb_type_uuid
;
952 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
954 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
955 cell
->type
= &column
->type
;
961 cmd_list(struct ctl_context
*ctx
)
963 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
964 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
965 const struct ovsdb_idl_column
**columns
;
966 const char *table_name
= ctx
->argv
[1];
967 const struct ctl_table_class
*table
;
972 table
= get_table(table_name
);
973 parse_column_names(column_names
, table
, &columns
, &n_columns
);
974 out
= ctx
->table
= list_make_table(columns
, n_columns
);
976 for (i
= 2; i
< ctx
->argc
; i
++) {
977 list_record(get_row(ctx
, table
, ctx
->argv
[i
], must_exist
),
978 columns
, n_columns
, out
);
981 const struct ovsdb_idl_row
*row
;
983 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
!= NULL
;
984 row
= ovsdb_idl_next_row(row
)) {
985 list_record(row
, columns
, n_columns
, out
);
991 /* Finds and returns the "struct ctl_table_class *" with 'table_name' by
992 * searching the 'tables'. */
993 static const struct ctl_table_class
*
994 get_table(const char *table_name
)
996 const struct ctl_table_class
*table
;
997 const struct ctl_table_class
*best_match
= NULL
;
998 unsigned int best_score
= 0;
1000 for (table
= tables
; table
->class; table
++) {
1001 unsigned int score
= score_partial_match(table
->class->name
,
1003 if (score
> best_score
) {
1006 } else if (score
== best_score
) {
1012 } else if (best_score
) {
1013 ctl_fatal("multiple table names match \"%s\"", table_name
);
1015 ctl_fatal("unknown table \"%s\"", table_name
);
1021 pre_cmd_find(struct ctl_context
*ctx
)
1023 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1024 const char *table_name
= ctx
->argv
[1];
1025 const struct ctl_table_class
*table
;
1028 table
= pre_get_table(ctx
, table_name
);
1029 pre_list_columns(ctx
, table
, column_names
);
1030 for (i
= 2; i
< ctx
->argc
; i
++) {
1031 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1036 cmd_find(struct ctl_context
*ctx
)
1038 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1039 const struct ovsdb_idl_column
**columns
;
1040 const char *table_name
= ctx
->argv
[1];
1041 const struct ctl_table_class
*table
;
1042 const struct ovsdb_idl_row
*row
;
1046 table
= get_table(table_name
);
1047 parse_column_names(column_names
, table
, &columns
, &n_columns
);
1048 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1049 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
;
1050 row
= ovsdb_idl_next_row(row
)) {
1053 for (i
= 2; i
< ctx
->argc
; i
++) {
1054 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
],
1059 list_record(row
, columns
, n_columns
, out
);
1066 /* Sets the column of 'row' in 'table'. */
1068 set_column(const struct ctl_table_class
*table
,
1069 const struct ovsdb_idl_row
*row
, const char *arg
,
1070 struct ovsdb_symbol_table
*symtab
)
1072 const struct ovsdb_idl_column
*column
;
1073 char *key_string
, *value_string
;
1076 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
1077 NULL
, NULL
, 0, &value_string
);
1078 die_if_error(error
);
1079 if (!value_string
) {
1080 ctl_fatal("%s: missing value", arg
);
1082 check_mutable(row
, column
);
1085 union ovsdb_atom key
, value
;
1086 struct ovsdb_datum datum
;
1088 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
1089 ctl_fatal("cannot specify key to set for non-map column %s",
1093 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
1094 key_string
, symtab
));
1095 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
1096 value_string
, symtab
));
1098 ovsdb_datum_init_empty(&datum
);
1099 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
1101 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1102 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
1104 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
1105 &column
->type
, false);
1106 ovsdb_idl_txn_verify(row
, column
);
1107 ovsdb_idl_txn_write(row
, column
, &datum
);
1109 struct ovsdb_datum datum
;
1111 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
1112 value_string
, symtab
));
1113 ovsdb_idl_txn_write(row
, column
, &datum
);
1121 pre_cmd_set(struct ctl_context
*ctx
)
1123 const char *table_name
= ctx
->argv
[1];
1124 const struct ctl_table_class
*table
;
1127 table
= pre_get_table(ctx
, table_name
);
1128 for (i
= 3; i
< ctx
->argc
; i
++) {
1129 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1134 cmd_set(struct ctl_context
*ctx
)
1136 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1137 const char *table_name
= ctx
->argv
[1];
1138 const char *record_id
= ctx
->argv
[2];
1139 const struct ctl_table_class
*table
;
1140 const struct ovsdb_idl_row
*row
;
1143 table
= get_table(table_name
);
1144 row
= get_row(ctx
, table
, record_id
, must_exist
);
1149 for (i
= 3; i
< ctx
->argc
; i
++) {
1150 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
1153 invalidate_cache(ctx
);
1157 pre_cmd_add(struct ctl_context
*ctx
)
1159 const char *table_name
= ctx
->argv
[1];
1160 const char *column_name
= ctx
->argv
[3];
1161 const struct ctl_table_class
*table
;
1162 const struct ovsdb_idl_column
*column
;
1164 table
= pre_get_table(ctx
, table_name
);
1165 pre_get_column(ctx
, table
, column_name
, &column
);
1169 cmd_add(struct ctl_context
*ctx
)
1171 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1172 const char *table_name
= ctx
->argv
[1];
1173 const char *record_id
= ctx
->argv
[2];
1174 const char *column_name
= ctx
->argv
[3];
1175 const struct ctl_table_class
*table
;
1176 const struct ovsdb_idl_column
*column
;
1177 const struct ovsdb_idl_row
*row
;
1178 const struct ovsdb_type
*type
;
1179 struct ovsdb_datum old
;
1182 table
= get_table(table_name
);
1183 die_if_error(get_column(table
, column_name
, &column
));
1184 row
= get_row(ctx
, table
, record_id
, must_exist
);
1188 check_mutable(row
, column
);
1190 type
= &column
->type
;
1191 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1192 for (i
= 4; i
< ctx
->argc
; i
++) {
1193 struct ovsdb_type add_type
;
1194 struct ovsdb_datum add
;
1198 add_type
.n_max
= UINT_MAX
;
1199 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
1201 ovsdb_datum_union(&old
, &add
, type
, false);
1202 ovsdb_datum_destroy(&add
, type
);
1204 if (old
.n
> type
->n_max
) {
1205 ctl_fatal("\"add\" operation would put %u %s in column %s of "
1206 "table %s but the maximum number is %u",
1208 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1209 column
->name
, table
->class->name
, type
->n_max
);
1211 ovsdb_idl_txn_verify(row
, column
);
1212 ovsdb_idl_txn_write(row
, column
, &old
);
1214 invalidate_cache(ctx
);
1218 pre_cmd_remove(struct ctl_context
*ctx
)
1220 const char *table_name
= ctx
->argv
[1];
1221 const char *column_name
= ctx
->argv
[3];
1222 const struct ctl_table_class
*table
;
1223 const struct ovsdb_idl_column
*column
;
1225 table
= pre_get_table(ctx
, table_name
);
1226 pre_get_column(ctx
, table
, column_name
, &column
);
1230 cmd_remove(struct ctl_context
*ctx
)
1232 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1233 const char *table_name
= ctx
->argv
[1];
1234 const char *record_id
= ctx
->argv
[2];
1235 const char *column_name
= ctx
->argv
[3];
1236 const struct ctl_table_class
*table
;
1237 const struct ovsdb_idl_column
*column
;
1238 const struct ovsdb_idl_row
*row
;
1239 const struct ovsdb_type
*type
;
1240 struct ovsdb_datum old
;
1243 table
= get_table(table_name
);
1244 die_if_error(get_column(table
, column_name
, &column
));
1245 row
= get_row(ctx
, table
, record_id
, must_exist
);
1249 check_mutable(row
, column
);
1251 type
= &column
->type
;
1252 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1253 for (i
= 4; i
< ctx
->argc
; i
++) {
1254 struct ovsdb_type rm_type
;
1255 struct ovsdb_datum rm
;
1260 rm_type
.n_max
= UINT_MAX
;
1261 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
1262 ctx
->argv
[i
], ctx
->symtab
);
1265 if (ovsdb_type_is_map(&rm_type
)) {
1266 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
1268 die_if_error(ovsdb_datum_from_string(
1269 &rm
, &rm_type
, ctx
->argv
[i
], ctx
->symtab
));
1271 ctl_fatal("%s", error
);
1274 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
1275 ovsdb_datum_destroy(&rm
, &rm_type
);
1277 if (old
.n
< type
->n_min
) {
1278 ctl_fatal("\"remove\" operation would put %u %s in column %s of "
1279 "table %s but the minimum number is %u",
1281 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1282 column
->name
, table
->class->name
, type
->n_min
);
1284 ovsdb_idl_txn_verify(row
, column
);
1285 ovsdb_idl_txn_write(row
, column
, &old
);
1287 invalidate_cache(ctx
);
1291 pre_cmd_clear(struct ctl_context
*ctx
)
1293 const char *table_name
= ctx
->argv
[1];
1294 const struct ctl_table_class
*table
;
1297 table
= pre_get_table(ctx
, table_name
);
1298 for (i
= 3; i
< ctx
->argc
; i
++) {
1299 const struct ovsdb_idl_column
*column
;
1301 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
1306 cmd_clear(struct ctl_context
*ctx
)
1308 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1309 const char *table_name
= ctx
->argv
[1];
1310 const char *record_id
= ctx
->argv
[2];
1311 const struct ctl_table_class
*table
;
1312 const struct ovsdb_idl_row
*row
;
1315 table
= get_table(table_name
);
1316 row
= get_row(ctx
, table
, record_id
, must_exist
);
1321 for (i
= 3; i
< ctx
->argc
; i
++) {
1322 const struct ovsdb_idl_column
*column
;
1323 const struct ovsdb_type
*type
;
1324 struct ovsdb_datum datum
;
1326 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
1327 check_mutable(row
, column
);
1329 type
= &column
->type
;
1330 if (type
->n_min
> 0) {
1331 ctl_fatal("\"clear\" operation cannot be applied to column %s "
1332 "of table %s, which is not allowed to be empty",
1333 column
->name
, table
->class->name
);
1336 ovsdb_datum_init_empty(&datum
);
1337 ovsdb_idl_txn_write(row
, column
, &datum
);
1340 invalidate_cache(ctx
);
1344 pre_create(struct ctl_context
*ctx
)
1346 const char *id
= shash_find_data(&ctx
->options
, "--id");
1347 const char *table_name
= ctx
->argv
[1];
1348 const struct ctl_table_class
*table
;
1350 table
= get_table(table_name
);
1351 if (!id
&& !table
->class->is_root
) {
1352 VLOG_WARN("applying \"create\" command to table %s without --id "
1353 "option will have no effect", table
->class->name
);
1358 cmd_create(struct ctl_context
*ctx
)
1360 const char *id
= shash_find_data(&ctx
->options
, "--id");
1361 const char *table_name
= ctx
->argv
[1];
1362 const struct ctl_table_class
*table
= get_table(table_name
);
1363 const struct ovsdb_idl_row
*row
;
1364 const struct uuid
*uuid
;
1368 struct ovsdb_symbol
*symbol
= create_symbol(ctx
->symtab
, id
, NULL
);
1369 if (table
->class->is_root
) {
1370 /* This table is in the root set, meaning that rows created in it
1371 * won't disappear even if they are unreferenced, so disable
1372 * warnings about that by pretending that there is a reference. */
1373 symbol
->strong_ref
= true;
1375 uuid
= &symbol
->uuid
;
1380 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
1381 for (i
= 2; i
< ctx
->argc
; i
++) {
1382 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
1384 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1387 /* This function may be used as the 'postprocess' function for commands that
1388 * insert new rows into the database. It expects that the command's 'run'
1389 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
1390 * sole output. It replaces that output by the row's permanent UUID assigned
1391 * by the database server and appends a new-line.
1393 * Currently we use this only for "create", because the higher-level commands
1394 * are supposed to be independent of the actual structure of the vswitch
1397 post_create(struct ctl_context
*ctx
)
1399 const struct uuid
*real
;
1402 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
1405 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
1407 ds_clear(&ctx
->output
);
1408 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
1410 ds_put_char(&ctx
->output
, '\n');
1414 pre_cmd_destroy(struct ctl_context
*ctx
)
1416 const char *table_name
= ctx
->argv
[1];
1418 pre_get_table(ctx
, table_name
);
1422 cmd_destroy(struct ctl_context
*ctx
)
1424 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1425 bool delete_all
= shash_find(&ctx
->options
, "--all");
1426 const char *table_name
= ctx
->argv
[1];
1427 const struct ctl_table_class
*table
;
1430 table
= get_table(table_name
);
1432 if (delete_all
&& ctx
->argc
> 2) {
1433 ctl_fatal("--all and records argument should not be specified together");
1436 if (delete_all
&& !must_exist
) {
1437 ctl_fatal("--all and --if-exists should not be specified together");
1441 const struct ovsdb_idl_row
*row
;
1442 const struct ovsdb_idl_row
*next_row
;
1444 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class);
1446 next_row
= ovsdb_idl_next_row(row
);
1447 ovsdb_idl_txn_delete(row
);
1451 for (i
= 2; i
< ctx
->argc
; i
++) {
1452 const struct ovsdb_idl_row
*row
;
1454 row
= get_row(ctx
, table
, ctx
->argv
[i
], must_exist
);
1456 ovsdb_idl_txn_delete(row
);
1460 invalidate_cache(ctx
);
1464 pre_cmd_wait_until(struct ctl_context
*ctx
)
1466 const char *table_name
= ctx
->argv
[1];
1467 const struct ctl_table_class
*table
;
1470 table
= pre_get_table(ctx
, table_name
);
1472 for (i
= 3; i
< ctx
->argc
; i
++) {
1473 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1478 cmd_wait_until(struct ctl_context
*ctx
)
1480 const char *table_name
= ctx
->argv
[1];
1481 const char *record_id
= ctx
->argv
[2];
1482 const struct ctl_table_class
*table
;
1483 const struct ovsdb_idl_row
*row
;
1486 table
= get_table(table_name
);
1488 row
= get_row(ctx
, table
, record_id
, false);
1490 ctx
->try_again
= true;
1494 for (i
= 3; i
< ctx
->argc
; i
++) {
1495 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
1496 ctx
->try_again
= true;
1502 /* Parses one command. */
1504 parse_command(int argc
, char *argv
[], struct shash
*local_options
,
1505 struct ctl_command
*command
)
1507 const struct ctl_command_syntax
*p
;
1508 struct shash_node
*node
;
1512 shash_init(&command
->options
);
1513 shash_swap(local_options
, &command
->options
);
1514 for (i
= 0; i
< argc
; i
++) {
1515 const char *option
= argv
[i
];
1519 if (option
[0] != '-') {
1523 equals
= strchr(option
, '=');
1525 key
= xmemdup0(option
, equals
- option
);
1526 value
= xstrdup(equals
+ 1);
1528 key
= xstrdup(option
);
1532 if (shash_find(&command
->options
, key
)) {
1533 ctl_fatal("'%s' option specified multiple times", argv
[i
]);
1535 shash_add_nocopy(&command
->options
, key
, value
);
1538 ctl_fatal("missing command name (use --help for help)");
1541 p
= shash_find_data(&all_commands
, argv
[i
]);
1543 ctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
1546 SHASH_FOR_EACH (node
, &command
->options
) {
1547 const char *s
= strstr(p
->options
, node
->name
);
1548 int end
= s
? s
[strlen(node
->name
)] : EOF
;
1550 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
1551 ctl_fatal("'%s' command has no '%s' option",
1552 argv
[i
], node
->name
);
1554 if ((end
== '=') != (node
->data
!= NULL
)) {
1556 ctl_fatal("missing argument to '%s' option on '%s' "
1557 "command", node
->name
, argv
[i
]);
1559 ctl_fatal("'%s' option on '%s' does not accept an "
1560 "argument", node
->name
, argv
[i
]);
1565 n_arg
= argc
- i
- 1;
1566 if (n_arg
< p
->min_args
) {
1567 ctl_fatal("'%s' command requires at least %d arguments",
1568 p
->name
, p
->min_args
);
1569 } else if (n_arg
> p
->max_args
) {
1572 for (j
= i
+ 1; j
< argc
; j
++) {
1573 if (argv
[j
][0] == '-') {
1574 ctl_fatal("'%s' command takes at most %d arguments "
1575 "(note that options must precede command "
1576 "names and follow a \"--\" argument)",
1577 p
->name
, p
->max_args
);
1581 ctl_fatal("'%s' command takes at most %d arguments",
1582 p
->name
, p
->max_args
);
1585 command
->syntax
= p
;
1586 command
->argc
= n_arg
+ 1;
1587 command
->argv
= &argv
[i
];
1591 pre_cmd_show(struct ctl_context
*ctx
)
1593 struct cmd_show_table
*show
;
1595 for (show
= cmd_show_tables
; show
->table
; show
++) {
1598 ovsdb_idl_add_table(ctx
->idl
, show
->table
);
1599 if (show
->name_column
) {
1600 ovsdb_idl_add_column(ctx
->idl
, show
->name_column
);
1602 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1603 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1605 ovsdb_idl_add_column(ctx
->idl
, column
);
1611 static struct cmd_show_table
*
1612 cmd_show_find_table_by_row(const struct ovsdb_idl_row
*row
)
1614 struct cmd_show_table
*show
;
1616 for (show
= cmd_show_tables
; show
->table
; show
++) {
1617 if (show
->table
== row
->table
->class) {
1624 static struct cmd_show_table
*
1625 cmd_show_find_table_by_name(const char *name
)
1627 struct cmd_show_table
*show
;
1629 for (show
= cmd_show_tables
; show
->table
; show
++) {
1630 if (!strcmp(show
->table
->name
, name
)) {
1638 cmd_show_row(struct ctl_context
*ctx
, const struct ovsdb_idl_row
*row
,
1641 struct cmd_show_table
*show
= cmd_show_find_table_by_row(row
);
1644 ds_put_char_multiple(&ctx
->output
, ' ', level
* 4);
1645 if (show
&& show
->name_column
) {
1646 const struct ovsdb_datum
*datum
;
1648 ds_put_format(&ctx
->output
, "%s ", show
->table
->name
);
1649 datum
= ovsdb_idl_read(row
, show
->name_column
);
1650 ovsdb_datum_to_string(datum
, &show
->name_column
->type
, &ctx
->output
);
1652 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1654 ds_put_char(&ctx
->output
, '\n');
1656 if (!show
|| show
->recurse
) {
1660 show
->recurse
= true;
1661 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1662 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1663 const struct ovsdb_datum
*datum
;
1669 datum
= ovsdb_idl_read(row
, column
);
1670 if (column
->type
.key
.type
== OVSDB_TYPE_UUID
&&
1671 column
->type
.key
.u
.uuid
.refTableName
) {
1672 struct cmd_show_table
*ref_show
;
1675 ref_show
= cmd_show_find_table_by_name(
1676 column
->type
.key
.u
.uuid
.refTableName
);
1678 for (j
= 0; j
< datum
->n
; j
++) {
1679 const struct ovsdb_idl_row
*ref_row
;
1681 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1683 &datum
->keys
[j
].uuid
);
1685 cmd_show_row(ctx
, ref_row
, level
+ 1);
1690 } else if (ovsdb_type_is_map(&column
->type
) &&
1691 column
->type
.value
.type
== OVSDB_TYPE_UUID
&&
1692 column
->type
.value
.u
.uuid
.refTableName
) {
1693 struct cmd_show_table
*ref_show
;
1696 /* Prints the key to ref'ed table name map if the ref'ed table
1697 * is also defined in 'cmd_show_tables'. */
1698 ref_show
= cmd_show_find_table_by_name(
1699 column
->type
.value
.u
.uuid
.refTableName
);
1700 if (ref_show
&& ref_show
->name_column
) {
1701 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1702 ds_put_format(&ctx
->output
, "%s:\n", column
->name
);
1703 for (j
= 0; j
< datum
->n
; j
++) {
1704 const struct ovsdb_idl_row
*ref_row
;
1706 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1708 &datum
->values
[j
].uuid
);
1710 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 2) * 4);
1711 ovsdb_atom_to_string(&datum
->keys
[j
], column
->type
.key
.type
,
1713 ds_put_char(&ctx
->output
, '=');
1715 const struct ovsdb_datum
*ref_datum
;
1717 ref_datum
= ovsdb_idl_read(ref_row
,
1718 ref_show
->name_column
);
1719 ovsdb_datum_to_string(ref_datum
,
1720 &ref_show
->name_column
->type
,
1723 ds_put_cstr(&ctx
->output
, "\"<null>\"");
1725 ds_put_char(&ctx
->output
, '\n');
1731 if (!ovsdb_datum_is_default(datum
, &column
->type
)) {
1732 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1733 ds_put_format(&ctx
->output
, "%s: ", column
->name
);
1734 ovsdb_datum_to_string(datum
, &column
->type
, &ctx
->output
);
1735 ds_put_char(&ctx
->output
, '\n');
1738 show
->recurse
= false;
1742 cmd_show(struct ctl_context
*ctx
)
1744 const struct ovsdb_idl_row
*row
;
1746 for (row
= ovsdb_idl_first_row(ctx
->idl
, cmd_show_tables
[0].table
);
1747 row
; row
= ovsdb_idl_next_row(row
)) {
1748 cmd_show_row(ctx
, row
, 0);
1753 /* Given pointer to dynamic array 'options_p', array's current size
1754 * 'allocated_options_p' and number of added options 'n_options_p',
1755 * adds all command options to the array. Enlarges the array if
1758 ctl_add_cmd_options(struct option
**options_p
, size_t *n_options_p
,
1759 size_t *allocated_options_p
, int opt_val
)
1762 const struct shash_node
*node
;
1763 size_t n_existing_options
= *n_options_p
;
1765 SHASH_FOR_EACH (node
, &all_commands
) {
1766 const struct ctl_command_syntax
*p
= node
->data
;
1768 if (p
->options
[0]) {
1769 char *save_ptr
= NULL
;
1773 s
= xstrdup(p
->options
);
1774 for (name
= strtok_r(s
, ",", &save_ptr
); name
!= NULL
;
1775 name
= strtok_r(NULL
, ",", &save_ptr
)) {
1779 ovs_assert(name
[0] == '-' && name
[1] == '-' && name
[2]);
1782 equals
= strchr(name
, '=');
1784 has_arg
= required_argument
;
1787 has_arg
= no_argument
;
1790 o
= find_option(name
, *options_p
, *n_options_p
);
1792 ovs_assert(o
- *options_p
>= n_existing_options
);
1793 ovs_assert(o
->has_arg
== has_arg
);
1795 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
1796 o
->name
= xstrdup(name
);
1797 o
->has_arg
= has_arg
;
1806 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
1807 memset(o
, 0, sizeof *o
);
1810 /* Parses command-line input for commands. */
1811 struct ctl_command
*
1812 ctl_parse_commands(int argc
, char *argv
[], struct shash
*local_options
,
1813 size_t *n_commandsp
)
1815 struct ctl_command
*commands
;
1816 size_t n_commands
, allocated_commands
;
1820 n_commands
= allocated_commands
= 0;
1822 for (start
= i
= 0; i
<= argc
; i
++) {
1823 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
1825 if (n_commands
>= allocated_commands
) {
1826 struct ctl_command
*c
;
1828 commands
= x2nrealloc(commands
, &allocated_commands
,
1830 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
1831 shash_moved(&c
->options
);
1834 parse_command(i
- start
, &argv
[start
], local_options
,
1835 &commands
[n_commands
++]);
1836 } else if (!shash_is_empty(local_options
)) {
1837 ctl_fatal("missing command name (use --help for help)");
1843 ctl_fatal("missing command name (use --help for help)");
1845 *n_commandsp
= n_commands
;
1849 /* Prints all registered commands. */
1851 ctl_print_commands(void)
1853 const struct shash_node
*node
;
1855 SHASH_FOR_EACH (node
, &all_commands
) {
1856 const struct ctl_command_syntax
*p
= node
->data
;
1857 char *options
= xstrdup(p
->options
);
1858 char *options_begin
= options
;
1861 for (item
= strsep(&options
, ","); item
!= NULL
;
1862 item
= strsep(&options
, ",")) {
1863 if (item
[0] != '\0') {
1864 printf("[%s] ", item
);
1867 printf(",%s,", p
->name
);
1868 print_command_arguments(p
);
1871 free(options_begin
);
1877 /* Given array of options 'options', prints them. */
1879 ctl_print_options(const struct option
*options
)
1881 for (; options
->name
; options
++) {
1882 const struct option
*o
= options
;
1884 printf("--%s%s\n", o
->name
, o
->has_arg
? "=ARG" : "");
1885 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
1886 printf("-%c%s\n", o
->val
, o
->has_arg
? " ARG" : "");
1893 /* Returns the default local database path. */
1895 ctl_default_db(void)
1899 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
1904 /* Returns true if it looks like this set of arguments might modify the
1905 * database, otherwise false. (Not very smart, so it's prone to false
1908 ctl_might_write_to_db(char **argv
)
1910 for (; *argv
; argv
++) {
1911 const struct ctl_command_syntax
*p
= shash_find_data(&all_commands
,
1913 if (p
&& p
->mode
== RW
) {
1921 ctl_fatal(const char *format
, ...)
1926 va_start(args
, format
);
1927 message
= xvasprintf(format
, args
);
1930 vlog_set_levels(&VLM_db_ctl_base
, VLF_CONSOLE
, VLL_OFF
);
1931 VLOG_ERR("%s", message
);
1932 ovs_error(0, "%s", message
);
1933 ctl_exit(EXIT_FAILURE
);
1936 /* Frees the current transaction and the underlying IDL and then calls
1939 * Freeing the transaction and the IDL is not strictly necessary, but it makes
1940 * for a clean memory leak report from valgrind in the normal case. That makes
1941 * it easier to notice real memory leaks. */
1943 ctl_exit(int status
)
1946 ovsdb_idl_txn_abort(the_idl_txn
);
1947 ovsdb_idl_txn_destroy(the_idl_txn
);
1949 ovsdb_idl_destroy(the_idl
);
1953 /* Comman database commands to be registered. */
1954 static const struct ctl_command_syntax db_ctl_commands
[] = {
1955 {"comment", 0, INT_MAX
, "[ARG]...", NULL
, NULL
, NULL
, "", RO
},
1956 {"get", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get
, cmd_get
,
1957 NULL
, "--if-exists,--id=", RO
},
1958 {"list", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_list
, cmd_list
, NULL
,
1959 "--if-exists,--columns=", RO
},
1960 {"find", 1, INT_MAX
, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find
,
1961 cmd_find
, NULL
, "--columns=", RO
},
1962 {"set", 3, INT_MAX
, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set
,
1963 cmd_set
, NULL
, "--if-exists", RW
},
1964 {"add", 4, INT_MAX
, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add
,
1965 cmd_add
, NULL
, "--if-exists", RW
},
1966 {"remove", 4, INT_MAX
, "TABLE RECORD COLUMN KEY|VALUE|KEY=VALUE...",
1967 pre_cmd_remove
, cmd_remove
, NULL
, "--if-exists", RW
},
1968 {"clear", 3, INT_MAX
, "TABLE RECORD COLUMN...", pre_cmd_clear
, cmd_clear
,
1969 NULL
, "--if-exists", RW
},
1970 {"create", 2, INT_MAX
, "TABLE COLUMN[:KEY]=VALUE...", pre_create
,
1971 cmd_create
, post_create
, "--id=", RW
},
1972 {"destroy", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_destroy
, cmd_destroy
,
1973 NULL
, "--if-exists,--all", RW
},
1974 {"wait-until", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]=VALUE]...",
1975 pre_cmd_wait_until
, cmd_wait_until
, NULL
, "", RO
},
1976 {"show", 0, 0, "", pre_cmd_show
, cmd_show
, NULL
, "", RO
},
1977 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
1980 /* Registers commands represented by 'struct ctl_command_syntax's to
1981 * 'all_commands'. The last element of 'commands' must be an all-NULL
1984 ctl_register_commands(const struct ctl_command_syntax
*commands
)
1986 const struct ctl_command_syntax
*p
;
1988 for (p
= commands
; p
->name
; p
++) {
1989 shash_add_assert(&all_commands
, p
->name
, p
);
1993 /* Registers the 'db_ctl_commands' to 'all_commands'. */
1995 ctl_init(const struct ctl_table_class tables_
[])
1998 ctl_register_commands(db_ctl_commands
);
2001 /* Returns the text for the database commands usage. */
2003 ctl_get_db_cmd_usage(void)
2005 return "Database commands:\n\
2006 list TBL [REC] list RECord (or all records) in TBL\n\
2007 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
2008 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
2009 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
2010 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
2011 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
2012 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
2013 create TBL COL[:KEY]=VALUE create and initialize new record\n\
2014 destroy TBL REC delete RECord from TBL\n\
2015 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
2016 Potentially unsafe database commands require --force option.\n";
2019 /* Initializes 'ctx' from 'command'. */
2021 ctl_context_init_command(struct ctl_context
*ctx
,
2022 struct ctl_command
*command
)
2024 ctx
->argc
= command
->argc
;
2025 ctx
->argv
= command
->argv
;
2026 ctx
->options
= command
->options
;
2028 ds_swap(&ctx
->output
, &command
->output
);
2029 ctx
->table
= command
->table
;
2030 ctx
->try_again
= false;
2033 /* Initializes the entire 'ctx'. */
2035 ctl_context_init(struct ctl_context
*ctx
, struct ctl_command
*command
,
2036 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2037 struct ovsdb_symbol_table
*symtab
,
2038 void (*invalidate_cache
)(struct ctl_context
*))
2041 ctl_context_init_command(ctx
, command
);
2045 ctx
->symtab
= symtab
;
2046 ctx
->invalidate_cache
= invalidate_cache
;
2049 /* Completes processing of 'command' within 'ctx'. */
2051 ctl_context_done_command(struct ctl_context
*ctx
,
2052 struct ctl_command
*command
)
2054 ds_swap(&ctx
->output
, &command
->output
);
2055 command
->table
= ctx
->table
;
2058 /* Finishes up with 'ctx'.
2060 * If command is nonnull, first calls ctl_context_done_command() to complete
2061 * processing that command within 'ctx'. */
2063 ctl_context_done(struct ctl_context
*ctx
,
2064 struct ctl_command
*command
)
2067 ctl_context_done_command(ctx
, command
);
2069 invalidate_cache(ctx
);
2072 void ctl_set_column(const char *table_name
,
2073 const struct ovsdb_idl_row
*row
, const char *arg
,
2074 struct ovsdb_symbol_table
*symtab
)
2076 set_column(get_table(table_name
), row
, arg
, symtab
);