2 * Copyright (c) 2015, 2016, 2017 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 "openvswitch/dynamic-string.h"
29 #include "fatal-signal.h"
31 #include "openvswitch/json.h"
32 #include "openvswitch/vlog.h"
33 #include "ovsdb-data.h"
34 #include "ovsdb-idl.h"
35 #include "ovsdb-idl-provider.h"
36 #include "openvswitch/shash.h"
43 VLOG_DEFINE_THIS_MODULE(db_ctl_base
);
45 /* This array defines the 'show' command output format. User can check the
46 * definition in utilities/ovs-vsctl.c as reference.
48 * Particularly, if an element in 'columns[]' represents a reference to
49 * another table, the referred table must also be defined as an entry in
50 * in 'cmd_show_tables[]'.
52 * The definition must end with an all-NULL entry. It is initalized once
53 * when ctl_init() is called.
56 static const struct cmd_show_table
*cmd_show_tables
;
58 /* ctl_exit() is called by ctl_fatal(). User can optionally supply an exit
59 * function ctl_exit_func() via ctl_init. If supplied, this function will
60 * be called by ctl_exit()
62 static void (*ctl_exit_func
)(int status
) = NULL
;
63 OVS_NO_RETURN
static void ctl_exit(int status
);
66 static const struct ovsdb_idl_class
*idl_class
;
68 /* Two arrays with 'n_classes' elements, which represent the tables in this
69 * database and how the user can refer to their rows. */
70 static const struct ctl_table_class
*ctl_classes
;
71 static const struct ovsdb_idl_table_class
*idl_classes
;
72 static size_t n_classes
;
74 static struct shash all_commands
= SHASH_INITIALIZER(&all_commands
);
75 static char *get_table(const char *, const struct ovsdb_idl_table_class
**);
76 static char *set_column(const struct ovsdb_idl_table_class
*,
77 const struct ovsdb_idl_row
*, const char *,
78 struct ovsdb_symbol_table
*);
81 static struct option
*
82 find_option(const char *name
, struct option
*options
, size_t n_options
)
86 for (i
= 0; i
< n_options
; i
++) {
87 if (!strcmp(options
[i
].name
, name
)) {
94 static struct option
*
95 add_option(struct option
**optionsp
, size_t *n_optionsp
,
96 size_t *allocated_optionsp
)
98 if (*n_optionsp
>= *allocated_optionsp
) {
99 *optionsp
= x2nrealloc(*optionsp
, allocated_optionsp
,
102 return &(*optionsp
)[(*n_optionsp
)++];
105 /* Converts the command arguments into format that can be parsed by
106 * bash completion script.
108 * Therein, arguments will be attached with following prefixes:
110 * !argument :: The argument is required
111 * ?argument :: The argument is optional
112 * *argument :: The argument may appear any number (0 or more) times
113 * +argument :: The argument may appear one or more times
117 print_command_arguments(const struct ctl_command_syntax
*command
)
120 * The argument string is parsed in reverse. We use a stack 'oew_stack' to
121 * keep track of nested optionals. Whenever a ']' is encountered, we push
122 * a bit to 'oew_stack'. The bit is set to 1 if the ']' is not nested.
123 * Subsequently, we pop an entry everytime '[' is met.
125 * We use 'whole_word_is_optional' value to decide whether or not a ! or +
126 * should be added on encountering a space: if the optional surrounds the
127 * whole word then it shouldn't be, but if it is only a part of the word
128 * (i.e. [key=]value), it should be.
130 uint32_t oew_stack
= 0;
132 const char *arguments
= command
->arguments
;
133 int length
= strlen(arguments
);
138 /* Output buffer, written backward from end. */
139 char *output
= xmalloc(2 * length
);
140 char *outp
= output
+ 2 * length
;
143 bool in_repeated
= false;
144 bool whole_word_is_optional
= false;
146 for (const char *inp
= arguments
+ length
; inp
> arguments
; ) {
150 if (inp
[1] == '\0' || inp
[1] == ' ' || inp
[1] == '.') {
155 /* Checks if the whole word is optional, and sets the
156 * 'whole_word_is_optional' accordingly. */
157 if ((inp
== arguments
|| inp
[-1] == ' ') && oew_stack
& 1) {
158 *--outp
= in_repeated
? '*' : '?';
159 whole_word_is_optional
= true;
162 whole_word_is_optional
= false;
167 if (!whole_word_is_optional
) {
168 *--outp
= in_repeated
? '+' : '!';
172 whole_word_is_optional
= false;
182 if (arguments
[0] != '[' && outp
!= output
+ 2 * length
- 1) {
183 *--outp
= in_repeated
? '+' : '!';
190 die_if_error(char *error
)
193 ctl_fatal("%s", error
);
198 to_lower_and_underscores(unsigned c
)
200 return c
== '-' ? '_' : tolower(c
);
203 /* Returns a score representing how well 's' matches 'name'. Higher return
204 * values indicate a better match. The order of the arguments is important:
205 * 'name' is the name of an entity such as a table or a column, and 's' is user
208 score_partial_match(const char *name
, const char *s
)
212 if (!strcmp(name
, s
)) {
215 for (score
= 0; ; score
++, name
++, s
++) {
216 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
218 } else if (*name
== '\0') {
222 return *s
== '\0' ? score
: 0;
225 /* Returns NULL and sets 'symbolp' and 'newp' if symbol was created
226 * successfully. Otherwise returns a malloc()'ed error message on failure. */
227 static char * OVS_WARN_UNUSED_RESULT
228 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
,
229 struct ovsdb_symbol
**symbolp
, bool *newp
)
231 struct ovsdb_symbol
*symbol
;
236 return xasprintf("row id \"%s\" does not begin with \"@\"", id
);
240 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
243 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
244 if (symbol
->created
) {
245 return xasprintf("row id \"%s\" may only be specified on one --id "
248 symbol
->created
= true;
254 record_id_equals(const union ovsdb_atom
*name
, enum ovsdb_atomic_type type
,
255 const char *record_id
)
257 if (type
== OVSDB_TYPE_STRING
) {
258 if (!strcmp(name
->string
, record_id
)) {
263 size_t len
= strlen(record_id
);
265 && uuid_from_string(&uuid
, name
->string
)
266 && !strncmp(name
->string
, record_id
, len
)) {
272 ovs_assert(type
== OVSDB_TYPE_INTEGER
);
273 return name
->integer
== strtoll(record_id
, NULL
, 10);
277 static const struct ovsdb_idl_row
*
278 get_row_by_id(struct ctl_context
*ctx
,
279 const struct ovsdb_idl_table_class
*table
,
280 const struct ctl_row_id
*id
, const char *record_id
,
281 bool *multiple_matches
)
283 ovs_assert(multiple_matches
);
284 *multiple_matches
= false;
286 if (!id
->name_column
) {
290 const struct ovsdb_idl_row
*referrer
= NULL
;
292 /* Figure out the 'key' and 'value' types for the column that we're going
293 * to look at. One of these ('name_type') is the type of the name we're
294 * going to compare against 'record_id'. */
295 enum ovsdb_atomic_type key
, value
, name_type
;
297 name_type
= key
= id
->name_column
->type
.key
.type
;
298 value
= OVSDB_TYPE_VOID
;
300 key
= OVSDB_TYPE_STRING
;
301 name_type
= value
= id
->name_column
->type
.value
.type
;
304 /* We only support integer and string names (so far). */
305 if (name_type
== OVSDB_TYPE_INTEGER
) {
306 if (!record_id
[0] || record_id
[strspn(record_id
, "0123456789")]) {
310 ovs_assert(name_type
== OVSDB_TYPE_STRING
);
313 const struct ovsdb_idl_class
*class = ovsdb_idl_get_class(ctx
->idl
);
314 const struct ovsdb_idl_table_class
*id_table
315 = ovsdb_idl_table_class_from_column(class, id
->name_column
);
316 for (const struct ovsdb_idl_row
*row
= ovsdb_idl_first_row(ctx
->idl
,
319 row
= ovsdb_idl_next_row(row
)) {
320 /* Pick out the name column's data. */
321 const struct ovsdb_datum
*datum
= ovsdb_idl_get(
322 row
, id
->name_column
, key
, value
);
324 /* Extract the name from the column. */
325 const union ovsdb_atom
*name
;
327 name
= datum
->n
== 1 ? &datum
->keys
[0] : NULL
;
329 const union ovsdb_atom key_atom
330 = { .string
= CONST_CAST(char *, id
->key
) };
331 unsigned int i
= ovsdb_datum_find_key(datum
, &key_atom
,
333 name
= i
== UINT_MAX
? NULL
: &datum
->values
[i
];
339 /* If the name equals 'record_id', take it. */
340 if (record_id_equals(name
, name_type
, record_id
)) {
342 *multiple_matches
= true;
352 const struct ovsdb_idl_row
*final
= referrer
;
353 if (id
->uuid_column
) {
354 const struct ovsdb_datum
*uuid
;
356 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
357 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
358 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
360 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
,
361 &uuid
->keys
[0].uuid
);
369 char * OVS_WARN_UNUSED_RESULT
370 ctl_get_row(struct ctl_context
*ctx
,
371 const struct ovsdb_idl_table_class
*table
, const char *record_id
,
372 bool must_exist
, const struct ovsdb_idl_row
**rowp
)
374 const struct ovsdb_idl_row
*row
= NULL
;
379 if (uuid_from_string(&uuid
, record_id
)) {
380 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
, &uuid
);
383 if (!strcmp(record_id
, ".")) {
384 row
= ovsdb_idl_first_row(ctx
->idl
, table
);
385 if (row
&& ovsdb_idl_next_row(row
)) {
391 const struct ctl_table_class
*ctl_class
392 = &ctl_classes
[table
- idl_classes
];
393 for (int i
= 0; i
< ARRAY_SIZE(ctl_class
->row_ids
); i
++) {
394 const struct ctl_row_id
*id
= &ctl_class
->row_ids
[i
];
395 bool multiple_matches
;
397 row
= get_row_by_id(ctx
, table
, id
, record_id
, &multiple_matches
);
398 if (multiple_matches
) {
399 const struct ovsdb_idl_class
*class =
400 ovsdb_idl_get_class(ctx
->idl
);
401 const struct ovsdb_idl_table_class
*table_class
=
402 ovsdb_idl_table_class_from_column(class, id
->name_column
);
403 return xasprintf("multiple rows in %s match \"%s\"",
404 table_class
->name
, record_id
);
411 if (!row
&& uuid_is_partial_string(record_id
) >= 4) {
412 for (const struct ovsdb_idl_row
*r
= ovsdb_idl_first_row(ctx
->idl
,
415 r
= ovsdb_idl_next_row(r
)) {
416 if (uuid_is_partial_match(&r
->uuid
, record_id
)) {
420 return xasprintf("%s contains 2 or more rows whose UUIDs "
421 "begin with %s: at least "UUID_FMT
" "
422 "and "UUID_FMT
, table
->name
, record_id
,
423 UUID_ARGS(&row
->uuid
),
424 UUID_ARGS(&r
->uuid
));
429 if (must_exist
&& !row
) {
430 return xasprintf("no row \"%s\" in table %s", record_id
, table
->name
);
438 get_column(const struct ovsdb_idl_table_class
*table
, const char *column_name
,
439 const struct ovsdb_idl_column
**columnp
)
441 const struct ovsdb_idl_column
*best_match
= NULL
;
442 unsigned int best_score
= 0;
445 for (i
= 0; i
< table
->n_columns
; i
++) {
446 const struct ovsdb_idl_column
*column
= &table
->columns
[i
];
447 unsigned int score
= score_partial_match(column
->name
, column_name
);
448 if (score
> best_score
) {
451 } else if (score
== best_score
) {
456 *columnp
= best_match
;
459 } else if (best_score
) {
460 return xasprintf("%s contains more than one column whose name "
461 "matches \"%s\"", table
->name
, column_name
);
463 return xasprintf("%s does not contain a column whose name matches "
464 "\"%s\"", table
->name
, column_name
);
468 static char * OVS_WARN_UNUSED_RESULT
469 pre_get_column(struct ctl_context
*ctx
,
470 const struct ovsdb_idl_table_class
*table
,
471 const char *column_name
,
472 const struct ovsdb_idl_column
**columnp
)
474 char *error
= get_column(table
, column_name
, columnp
);
478 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
482 static char * OVS_WARN_UNUSED_RESULT
483 pre_get_table(struct ctl_context
*ctx
, const char *table_name
,
484 const struct ovsdb_idl_table_class
**tablep
)
486 const struct ovsdb_idl_table_class
*table
;
487 char *error
= get_table(table_name
, &table
);
491 ovsdb_idl_add_table(ctx
->idl
, table
);
493 const struct ctl_table_class
*ctl
= &ctl_classes
[table
- idl_classes
];
494 for (int i
= 0; i
< ARRAY_SIZE(ctl
->row_ids
); i
++) {
495 const struct ctl_row_id
*id
= &ctl
->row_ids
[i
];
496 if (id
->name_column
) {
497 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
499 if (id
->uuid_column
) {
500 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
511 missing_operator_error(const char *arg
, const char **allowed_operators
,
517 ds_put_format(&s
, "%s: argument does not end in ", arg
);
518 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
519 if (n_allowed
== 2) {
520 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
521 } else if (n_allowed
> 2) {
524 for (i
= 1; i
< n_allowed
- 1; i
++) {
525 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
527 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
529 ds_put_format(&s
, " followed by a value.");
531 return ds_steal_cstr(&s
);
534 /* Breaks 'arg' apart into a number of fields in the following order:
536 * - The name of a column in 'table', stored into '*columnp'. The column
537 * name may be abbreviated.
539 * - Optionally ':' followed by a key string. The key is stored as a
540 * malloc()'d string into '*keyp', or NULL if no key is present in
543 * - If 'valuep' is nonnull, an operator followed by a value string. The
544 * allowed operators are the 'n_allowed' string in 'allowed_operators',
545 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
546 * index of the operator within 'allowed_operators' is stored into
547 * '*operatorp'. The value is stored as a malloc()'d string into
548 * '*valuep', or NULL if no value is present in 'arg'.
550 * On success, returns NULL. On failure, returned a malloc()'d string error
551 * message and stores NULL into all of the nonnull output arguments. */
552 static char * OVS_WARN_UNUSED_RESULT
553 parse_column_key_value(const char *arg
,
554 const struct ovsdb_idl_table_class
*table
,
555 const struct ovsdb_idl_column
**columnp
, char **keyp
,
557 const char **allowed_operators
, size_t n_allowed
,
564 ovs_assert(!(operatorp
&& !valuep
));
570 /* Parse column name. */
571 error
= ovsdb_token_parse(&p
, &column_name
);
575 if (column_name
[0] == '\0') {
577 error
= xasprintf("%s: missing column name", arg
);
580 error
= get_column(table
, column_name
, columnp
);
586 /* Parse key string. */
589 error
= ovsdb_token_parse(&p
, keyp
);
595 /* Parse value string. */
601 if (!allowed_operators
) {
602 static const char *equals
= "=";
603 allowed_operators
= &equals
;
609 for (i
= 0; i
< n_allowed
; i
++) {
610 const char *op
= allowed_operators
[i
];
611 size_t op_len
= strlen(op
);
613 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
619 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
626 *valuep
= xstrdup(p
+ best_len
);
629 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
650 static char * OVS_WARN_UNUSED_RESULT
651 pre_parse_column_key_value(struct ctl_context
*ctx
, const char *arg
,
652 const struct ovsdb_idl_table_class
*table
)
654 const struct ovsdb_idl_column
*column
;
656 char *column_name
= NULL
;
660 error
= ovsdb_token_parse(&p
, &column_name
);
664 if (column_name
[0] == '\0') {
665 error
= xasprintf("%s: missing column name", arg
);
669 error
= pre_get_column(ctx
, table
, column_name
, &column
);
676 /* Checks if the 'column' is mutable. Returns NULL if it is mutable, or a
677 * malloc()'ed error message otherwise. */
678 static char * OVS_WARN_UNUSED_RESULT
679 check_mutable(const struct ovsdb_idl_row
*row
,
680 const struct ovsdb_idl_column
*column
)
682 if (!ovsdb_idl_is_mutable(row
, column
)) {
683 return xasprintf("cannot modify read-only column %s in table %s",
684 column
->name
, row
->table
->class_
->name
);
690 RELOP(RELOP_EQ, "=") \
691 RELOP(RELOP_NE, "!=") \
692 RELOP(RELOP_LT, "<") \
693 RELOP(RELOP_GT, ">") \
694 RELOP(RELOP_LE, "<=") \
695 RELOP(RELOP_GE, ">=") \
696 RELOP(RELOP_SET_EQ, "{=}") \
697 RELOP(RELOP_SET_NE, "{!=}") \
698 RELOP(RELOP_SET_LT, "{<}") \
699 RELOP(RELOP_SET_GT, "{>}") \
700 RELOP(RELOP_SET_LE, "{<=}") \
701 RELOP(RELOP_SET_GE, "{>=}")
704 #define RELOP(ENUM, STRING) ENUM,
710 is_set_operator(enum relop op
)
712 return (op
== RELOP_SET_EQ
|| op
== RELOP_SET_NE
||
713 op
== RELOP_SET_LT
|| op
== RELOP_SET_GT
||
714 op
== RELOP_SET_LE
|| op
== RELOP_SET_GE
);
718 evaluate_relop(const struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
719 const struct ovsdb_type
*type
, enum relop op
)
724 return ovsdb_datum_compare_3way(a
, b
, type
) == 0;
727 return ovsdb_datum_compare_3way(a
, b
, type
) != 0;
729 return ovsdb_datum_compare_3way(a
, b
, type
) < 0;
731 return ovsdb_datum_compare_3way(a
, b
, type
) > 0;
733 return ovsdb_datum_compare_3way(a
, b
, type
) <= 0;
735 return ovsdb_datum_compare_3way(a
, b
, type
) >= 0;
738 return b
->n
> a
->n
&& ovsdb_datum_includes_all(a
, b
, type
);
740 return a
->n
> b
->n
&& ovsdb_datum_includes_all(b
, a
, type
);
742 return ovsdb_datum_includes_all(a
, b
, type
);
744 return ovsdb_datum_includes_all(b
, a
, type
);
751 /* Checks if given row satisfies the specified condition. Returns the result of
752 * evaluating the condition in 'satisfied' flag and NULL as a return value on
753 * success. On failure returns a malloc()'ed error message and 'satisfied'
754 * value is not modified. */
755 static char * OVS_WARN_UNUSED_RESULT
756 check_condition(const struct ovsdb_idl_table_class
*table
,
757 const struct ovsdb_idl_row
*row
, const char *arg
,
758 struct ovsdb_symbol_table
*symtab
, bool *satisfied
)
760 static const char *operators
[] = {
761 #define RELOP(ENUM, STRING) STRING,
766 const struct ovsdb_idl_column
*column
;
767 const struct ovsdb_datum
*have_datum
;
768 char *key_string
= NULL
;
769 char *value_string
= NULL
;
770 struct ovsdb_type type
;
775 ovs_assert(satisfied
);
777 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
778 &operator, operators
, ARRAY_SIZE(operators
),
784 error
= xasprintf("%s: missing value", arg
);
789 type
.n_max
= UINT_MAX
;
791 have_datum
= ovsdb_idl_read(row
, column
);
793 union ovsdb_atom want_key
;
794 struct ovsdb_datum b
;
797 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
798 error
= xasprintf("cannot specify key to check for non-map column "
803 error
= ovsdb_atom_from_string(&want_key
, NULL
, &column
->type
.key
,
809 type
.key
= type
.value
;
810 type
.value
.type
= OVSDB_TYPE_VOID
;
811 error
= ovsdb_datum_from_string(&b
, &type
, value_string
, symtab
);
816 idx
= ovsdb_datum_find_key(have_datum
,
817 &want_key
, column
->type
.key
.type
);
818 if (idx
== UINT_MAX
&& !is_set_operator(operator)) {
821 struct ovsdb_datum a
;
823 if (idx
!= UINT_MAX
) {
825 a
.keys
= &have_datum
->values
[idx
];
833 retval
= evaluate_relop(&a
, &b
, &type
, operator);
836 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
837 ovsdb_datum_destroy(&b
, &type
);
839 struct ovsdb_datum want_datum
;
841 error
= ovsdb_datum_from_string(&want_datum
, &column
->type
,
842 value_string
, symtab
);
846 retval
= evaluate_relop(have_datum
, &want_datum
, &type
, operator);
847 ovsdb_datum_destroy(&want_datum
, &column
->type
);
859 invalidate_cache(struct ctl_context
*ctx
)
861 if (ctx
->invalidate_cache_cb
) {
862 (ctx
->invalidate_cache_cb
)(ctx
);
867 pre_cmd_get(struct ctl_context
*ctx
)
869 const char *id
= shash_find_data(&ctx
->options
, "--id");
870 const char *table_name
= ctx
->argv
[1];
871 const struct ovsdb_idl_table_class
*table
;
874 /* Using "get" without --id or a column name could possibly make sense.
875 * Maybe, for example, a *ctl command run wants to assert that a row
876 * exists. But it is unlikely that an interactive user would want to do
877 * that, so issue a warning if we're running on a terminal. */
878 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
879 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
880 "possibly erroneous");
883 die_if_error(pre_get_table(ctx
, table_name
, &table
));
884 for (i
= 3; i
< ctx
->argc
; i
++) {
885 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
886 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
890 die_if_error(pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
));
895 cmd_get(struct ctl_context
*ctx
)
897 const char *id
= shash_find_data(&ctx
->options
, "--id");
898 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
899 const char *table_name
= ctx
->argv
[1];
900 const char *record_id
= ctx
->argv
[2];
901 const struct ovsdb_idl_table_class
*table
;
902 const struct ovsdb_idl_row
*row
;
903 struct ds
*out
= &ctx
->output
;
906 if (id
&& !must_exist
) {
907 ctl_fatal("--if-exists and --id may not be specified together");
910 die_if_error(get_table(table_name
, &table
));
911 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
917 struct ovsdb_symbol
*symbol
;
920 die_if_error(create_symbol(ctx
->symtab
, id
, &symbol
, &new));
922 ctl_fatal("row id \"%s\" specified on \"get\" command was used "
923 "before it was defined", id
);
925 symbol
->uuid
= row
->uuid
;
927 /* This symbol refers to a row that already exists, so disable warnings
928 * about it being unreferenced. */
929 symbol
->strong_ref
= true;
931 for (i
= 3; i
< ctx
->argc
; i
++) {
932 const struct ovsdb_idl_column
*column
;
933 const struct ovsdb_datum
*datum
;
936 /* Special case for obtaining the UUID of a row. We can't just do this
937 * through parse_column_key_value() below since it returns a "struct
938 * ovsdb_idl_column" and the UUID column doesn't have one. */
939 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
940 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
941 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
945 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
946 &column
, &key_string
,
947 NULL
, NULL
, 0, NULL
));
949 ovsdb_idl_txn_verify(row
, column
);
950 datum
= ovsdb_idl_read(row
, column
);
952 union ovsdb_atom key
;
955 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
956 ctl_fatal("cannot specify key to get for non-map column %s",
960 die_if_error(ovsdb_atom_from_string(&key
, NULL
,
962 key_string
, ctx
->symtab
));
964 idx
= ovsdb_datum_find_key(datum
, &key
,
965 column
->type
.key
.type
);
966 if (idx
== UINT_MAX
) {
968 ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
969 key_string
, table
->name
, record_id
,
973 ovsdb_atom_to_string(&datum
->values
[idx
],
974 column
->type
.value
.type
, out
);
976 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
978 ovsdb_datum_to_string(datum
, &column
->type
, out
);
980 ds_put_char(out
, '\n');
986 /* Returns NULL on success or malloc()'ed error message on failure. */
987 static char * OVS_WARN_UNUSED_RESULT
988 parse_column_names(const char *column_names
,
989 const struct ovsdb_idl_table_class
*table
,
990 const struct ovsdb_idl_column
***columnsp
,
993 const struct ovsdb_idl_column
**columns
;
999 n_columns
= table
->n_columns
+ 1;
1000 columns
= xmalloc(n_columns
* sizeof *columns
);
1002 for (i
= 0; i
< table
->n_columns
; i
++) {
1003 columns
[i
+ 1] = &table
->columns
[i
];
1006 char *s
= xstrdup(column_names
);
1007 size_t allocated_columns
;
1008 char *save_ptr
= NULL
;
1012 allocated_columns
= n_columns
= 0;
1013 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
1014 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
1015 const struct ovsdb_idl_column
*column
;
1017 if (!strcasecmp(column_name
, "_uuid")) {
1020 char *error
= get_column(table
, column_name
, &column
);
1027 if (n_columns
>= allocated_columns
) {
1028 columns
= x2nrealloc(columns
, &allocated_columns
,
1031 columns
[n_columns
++] = column
;
1036 return xstrdup("must specify at least one column name");
1039 *columnsp
= columns
;
1040 *n_columnsp
= n_columns
;
1044 static char * OVS_WARN_UNUSED_RESULT
1045 pre_list_columns(struct ctl_context
*ctx
,
1046 const struct ovsdb_idl_table_class
*table
,
1047 const char *column_names
)
1049 const struct ovsdb_idl_column
**columns
;
1054 error
= parse_column_names(column_names
, table
, &columns
, &n_columns
);
1058 for (i
= 0; i
< n_columns
; i
++) {
1060 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
1068 pre_cmd_list(struct ctl_context
*ctx
)
1070 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1071 const char *table_name
= ctx
->argv
[1];
1072 const struct ovsdb_idl_table_class
*table
;
1074 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1075 die_if_error(pre_list_columns(ctx
, table
, column_names
));
1078 static struct table
*
1079 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
1084 out
= xmalloc(sizeof *out
);
1087 for (i
= 0; i
< n_columns
; i
++) {
1088 const struct ovsdb_idl_column
*column
= columns
[i
];
1089 const char *column_name
= column
? column
->name
: "_uuid";
1091 table_add_column(out
, "%s", column_name
);
1098 list_record(const struct ovsdb_idl_row
*row
,
1099 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
1109 for (i
= 0; i
< n_columns
; i
++) {
1110 const struct ovsdb_idl_column
*column
= columns
[i
];
1111 struct cell
*cell
= table_add_cell(out
);
1114 struct ovsdb_datum datum
;
1115 union ovsdb_atom atom
;
1117 atom
.uuid
= row
->uuid
;
1120 datum
.values
= NULL
;
1123 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
1124 cell
->type
= &ovsdb_type_uuid
;
1126 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
1128 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
1129 cell
->type
= &column
->type
;
1135 cmd_list(struct ctl_context
*ctx
)
1137 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1138 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1139 const struct ovsdb_idl_column
**columns
;
1140 const char *table_name
= ctx
->argv
[1];
1141 const struct ovsdb_idl_table_class
*table
;
1146 die_if_error(get_table(table_name
, &table
));
1147 die_if_error(parse_column_names(column_names
, table
, &columns
, &n_columns
));
1148 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1149 if (ctx
->argc
> 2) {
1150 for (i
= 2; i
< ctx
->argc
; i
++) {
1151 const struct ovsdb_idl_row
*row
;
1153 die_if_error(ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
,
1155 list_record(row
, columns
, n_columns
, out
);
1158 const struct ovsdb_idl_row
*row
;
1160 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
!= NULL
;
1161 row
= ovsdb_idl_next_row(row
)) {
1162 list_record(row
, columns
, n_columns
, out
);
1168 /* Finds the "struct ovsdb_idl_table_class *" with 'table_name' by searching
1169 * the tables in these schema. Returns NULL and sets 'tablep' on success, or a
1170 * malloc()'ed error message on failure. */
1171 static char * OVS_WARN_UNUSED_RESULT
1172 get_table(const char *table_name
, const struct ovsdb_idl_table_class
**tablep
)
1174 const struct ovsdb_idl_table_class
*best_match
= NULL
;
1175 unsigned int best_score
= 0;
1178 for (const struct ovsdb_idl_table_class
*table
= idl_classes
;
1179 table
< &idl_classes
[n_classes
]; table
++) {
1180 unsigned int score
= score_partial_match(table
->name
, table_name
);
1181 if (score
> best_score
) {
1184 } else if (score
== best_score
) {
1189 *tablep
= best_match
;
1190 } else if (best_score
) {
1191 error
= xasprintf("multiple table names match \"%s\"", table_name
);
1193 error
= xasprintf("unknown table \"%s\"", table_name
);
1199 pre_cmd_find(struct ctl_context
*ctx
)
1201 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1202 const char *table_name
= ctx
->argv
[1];
1203 const struct ovsdb_idl_table_class
*table
;
1206 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1207 die_if_error(pre_list_columns(ctx
, table
, column_names
));
1208 for (i
= 2; i
< ctx
->argc
; i
++) {
1209 die_if_error(pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
));
1214 cmd_find(struct ctl_context
*ctx
)
1216 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1217 const struct ovsdb_idl_column
**columns
;
1218 const char *table_name
= ctx
->argv
[1];
1219 const struct ovsdb_idl_table_class
*table
;
1220 const struct ovsdb_idl_row
*row
;
1224 die_if_error(get_table(table_name
, &table
));
1225 die_if_error(parse_column_names(column_names
, table
, &columns
, &n_columns
));
1226 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1227 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
;
1228 row
= ovsdb_idl_next_row(row
)) {
1231 for (i
= 2; i
< ctx
->argc
; i
++) {
1234 die_if_error(check_condition(table
, row
, ctx
->argv
[i
], ctx
->symtab
,
1240 list_record(row
, columns
, n_columns
, out
);
1247 /* Sets the column of 'row' in 'table'. Returns NULL on success or a
1248 * malloc()'ed error message on failure. */
1249 static char * OVS_WARN_UNUSED_RESULT
1250 set_column(const struct ovsdb_idl_table_class
*table
,
1251 const struct ovsdb_idl_row
*row
, const char *arg
,
1252 struct ovsdb_symbol_table
*symtab
)
1254 const struct ovsdb_idl_column
*column
;
1255 char *key_string
= NULL
;
1256 char *value_string
= NULL
;
1259 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
1260 NULL
, NULL
, 0, &value_string
);
1264 if (!value_string
) {
1265 error
= xasprintf("%s: missing value", arg
);
1268 error
= check_mutable(row
, column
);
1274 union ovsdb_atom key
, value
;
1275 struct ovsdb_datum datum
;
1277 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
1278 error
= xasprintf("cannot specify key to set for non-map column "
1279 "%s", column
->name
);
1283 error
= ovsdb_atom_from_string(&key
, NULL
, &column
->type
.key
,
1284 key_string
, symtab
);
1288 error
= ovsdb_atom_from_string(&value
, NULL
, &column
->type
.value
,
1289 value_string
, symtab
);
1294 ovsdb_datum_init_empty(&datum
);
1295 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
, NULL
);
1297 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1298 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
1300 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
1301 &column
->type
, false);
1302 ovsdb_idl_txn_verify(row
, column
);
1303 ovsdb_idl_txn_write(row
, column
, &datum
);
1305 struct ovsdb_datum datum
;
1307 error
= ovsdb_datum_from_string(&datum
, &column
->type
,
1308 value_string
, symtab
);
1312 ovsdb_idl_txn_write(row
, column
, &datum
);
1323 pre_cmd_set(struct ctl_context
*ctx
)
1325 const char *table_name
= ctx
->argv
[1];
1326 const struct ovsdb_idl_table_class
*table
;
1329 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1330 for (i
= 3; i
< ctx
->argc
; i
++) {
1331 die_if_error(pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
));
1336 cmd_set(struct ctl_context
*ctx
)
1338 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1339 const char *table_name
= ctx
->argv
[1];
1340 const char *record_id
= ctx
->argv
[2];
1341 const struct ovsdb_idl_table_class
*table
;
1342 const struct ovsdb_idl_row
*row
;
1345 die_if_error(get_table(table_name
, &table
));
1346 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1351 for (i
= 3; i
< ctx
->argc
; i
++) {
1352 die_if_error(set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
));
1355 invalidate_cache(ctx
);
1359 pre_cmd_add(struct ctl_context
*ctx
)
1361 const char *table_name
= ctx
->argv
[1];
1362 const char *column_name
= ctx
->argv
[3];
1363 const struct ovsdb_idl_table_class
*table
;
1364 const struct ovsdb_idl_column
*column
;
1366 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1367 die_if_error(pre_get_column(ctx
, table
, column_name
, &column
));
1371 cmd_add(struct ctl_context
*ctx
)
1373 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1374 const char *table_name
= ctx
->argv
[1];
1375 const char *record_id
= ctx
->argv
[2];
1376 const char *column_name
= ctx
->argv
[3];
1377 const struct ovsdb_idl_table_class
*table
;
1378 const struct ovsdb_idl_column
*column
;
1379 const struct ovsdb_idl_row
*row
;
1380 const struct ovsdb_type
*type
;
1381 struct ovsdb_datum old
;
1384 die_if_error(get_table(table_name
, &table
));
1385 die_if_error(get_column(table
, column_name
, &column
));
1386 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1390 die_if_error(check_mutable(row
, column
));
1392 type
= &column
->type
;
1393 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1394 for (i
= 4; i
< ctx
->argc
; i
++) {
1395 struct ovsdb_type add_type
;
1396 struct ovsdb_datum add
;
1400 add_type
.n_max
= UINT_MAX
;
1401 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
1403 ovsdb_datum_union(&old
, &add
, type
, false);
1404 ovsdb_datum_destroy(&add
, type
);
1406 if (old
.n
> type
->n_max
) {
1407 ctl_fatal("\"add\" operation would put %u %s in column %s of "
1408 "table %s but the maximum number is %u",
1410 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1411 column
->name
, table
->name
, type
->n_max
);
1413 ovsdb_idl_txn_verify(row
, column
);
1414 ovsdb_idl_txn_write(row
, column
, &old
);
1416 invalidate_cache(ctx
);
1420 pre_cmd_remove(struct ctl_context
*ctx
)
1422 const char *table_name
= ctx
->argv
[1];
1423 const char *column_name
= ctx
->argv
[3];
1424 const struct ovsdb_idl_table_class
*table
;
1425 const struct ovsdb_idl_column
*column
;
1427 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1428 die_if_error(pre_get_column(ctx
, table
, column_name
, &column
));
1432 cmd_remove(struct ctl_context
*ctx
)
1434 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1435 const char *table_name
= ctx
->argv
[1];
1436 const char *record_id
= ctx
->argv
[2];
1437 const char *column_name
= ctx
->argv
[3];
1438 const struct ovsdb_idl_table_class
*table
;
1439 const struct ovsdb_idl_column
*column
;
1440 const struct ovsdb_idl_row
*row
;
1441 const struct ovsdb_type
*type
;
1442 struct ovsdb_datum old
;
1445 die_if_error(get_table(table_name
, &table
));
1446 die_if_error(get_column(table
, column_name
, &column
));
1447 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1451 die_if_error(check_mutable(row
, column
));
1453 type
= &column
->type
;
1454 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1455 for (i
= 4; i
< ctx
->argc
; i
++) {
1456 struct ovsdb_type rm_type
;
1457 struct ovsdb_datum rm
;
1462 rm_type
.n_max
= UINT_MAX
;
1463 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
1464 ctx
->argv
[i
], ctx
->symtab
);
1467 if (ovsdb_type_is_map(&rm_type
)) {
1468 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
1470 die_if_error(ovsdb_datum_from_string(
1471 &rm
, &rm_type
, ctx
->argv
[i
], ctx
->symtab
));
1473 ctl_fatal("%s", error
);
1476 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
1477 ovsdb_datum_destroy(&rm
, &rm_type
);
1479 if (old
.n
< type
->n_min
) {
1480 ctl_fatal("\"remove\" operation would put %u %s in column %s of "
1481 "table %s but the minimum number is %u",
1483 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1484 column
->name
, table
->name
, type
->n_min
);
1486 ovsdb_idl_txn_verify(row
, column
);
1487 ovsdb_idl_txn_write(row
, column
, &old
);
1489 invalidate_cache(ctx
);
1493 pre_cmd_clear(struct ctl_context
*ctx
)
1495 const char *table_name
= ctx
->argv
[1];
1496 const struct ovsdb_idl_table_class
*table
;
1499 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1500 for (i
= 3; i
< ctx
->argc
; i
++) {
1501 const struct ovsdb_idl_column
*column
;
1503 die_if_error(pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
));
1508 cmd_clear(struct ctl_context
*ctx
)
1510 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1511 const char *table_name
= ctx
->argv
[1];
1512 const char *record_id
= ctx
->argv
[2];
1513 const struct ovsdb_idl_table_class
*table
;
1514 const struct ovsdb_idl_row
*row
;
1517 die_if_error(get_table(table_name
, &table
));
1518 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1523 for (i
= 3; i
< ctx
->argc
; i
++) {
1524 const struct ovsdb_idl_column
*column
;
1525 const struct ovsdb_type
*type
;
1526 struct ovsdb_datum datum
;
1528 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
1529 die_if_error(check_mutable(row
, column
));
1531 type
= &column
->type
;
1532 if (type
->n_min
> 0) {
1533 ctl_fatal("\"clear\" operation cannot be applied to column %s "
1534 "of table %s, which is not allowed to be empty",
1535 column
->name
, table
->name
);
1538 ovsdb_datum_init_empty(&datum
);
1539 ovsdb_idl_txn_write(row
, column
, &datum
);
1542 invalidate_cache(ctx
);
1546 pre_create(struct ctl_context
*ctx
)
1548 const char *id
= shash_find_data(&ctx
->options
, "--id");
1549 const char *table_name
= ctx
->argv
[1];
1550 const struct ovsdb_idl_table_class
*table
;
1552 die_if_error(get_table(table_name
, &table
));
1553 if (!id
&& !table
->is_root
) {
1554 VLOG_WARN("applying \"create\" command to table %s without --id "
1555 "option will have no effect", table
->name
);
1560 cmd_create(struct ctl_context
*ctx
)
1562 const char *id
= shash_find_data(&ctx
->options
, "--id");
1563 const char *table_name
= ctx
->argv
[1];
1564 const struct ovsdb_idl_table_class
*table
;
1565 const struct ovsdb_idl_row
*row
;
1566 const struct uuid
*uuid
;
1569 die_if_error(get_table(table_name
, &table
));
1571 struct ovsdb_symbol
*symbol
;
1573 die_if_error(create_symbol(ctx
->symtab
, id
, &symbol
, NULL
));
1574 if (table
->is_root
) {
1575 /* This table is in the root set, meaning that rows created in it
1576 * won't disappear even if they are unreferenced, so disable
1577 * warnings about that by pretending that there is a reference. */
1578 symbol
->strong_ref
= true;
1580 uuid
= &symbol
->uuid
;
1585 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
, uuid
);
1586 for (i
= 2; i
< ctx
->argc
; i
++) {
1587 die_if_error(set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
));
1589 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1592 /* This function may be used as the 'postprocess' function for commands that
1593 * insert new rows into the database. It expects that the command's 'run'
1594 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
1595 * sole output. It replaces that output by the row's permanent UUID assigned
1596 * by the database server and appends a new-line.
1598 * Currently we use this only for "create", because the higher-level commands
1599 * are supposed to be independent of the actual structure of the vswitch
1602 post_create(struct ctl_context
*ctx
)
1604 const struct uuid
*real
;
1607 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
1610 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
1612 ds_clear(&ctx
->output
);
1613 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
1615 ds_put_char(&ctx
->output
, '\n');
1619 pre_cmd_destroy(struct ctl_context
*ctx
)
1621 const char *table_name
= ctx
->argv
[1];
1623 die_if_error(pre_get_table(ctx
, table_name
, NULL
));
1627 cmd_destroy(struct ctl_context
*ctx
)
1629 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1630 bool delete_all
= shash_find(&ctx
->options
, "--all");
1631 const char *table_name
= ctx
->argv
[1];
1632 const struct ovsdb_idl_table_class
*table
;
1635 die_if_error(get_table(table_name
, &table
));
1637 if (delete_all
&& ctx
->argc
> 2) {
1638 ctl_fatal("--all and records argument should not be specified together");
1641 if (delete_all
&& !must_exist
) {
1642 ctl_fatal("--all and --if-exists should not be specified together");
1646 const struct ovsdb_idl_row
*row
;
1647 const struct ovsdb_idl_row
*next_row
;
1649 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
);
1651 next_row
= ovsdb_idl_next_row(row
);
1652 ovsdb_idl_txn_delete(row
);
1656 for (i
= 2; i
< ctx
->argc
; i
++) {
1657 const struct ovsdb_idl_row
*row
;
1659 die_if_error(ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
,
1662 ovsdb_idl_txn_delete(row
);
1666 invalidate_cache(ctx
);
1670 pre_cmd_wait_until(struct ctl_context
*ctx
)
1672 const char *table_name
= ctx
->argv
[1];
1673 const struct ovsdb_idl_table_class
*table
;
1676 die_if_error(pre_get_table(ctx
, table_name
, &table
));
1678 for (i
= 3; i
< ctx
->argc
; i
++) {
1679 die_if_error(pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
));
1684 cmd_wait_until(struct ctl_context
*ctx
)
1686 const char *table_name
= ctx
->argv
[1];
1687 const char *record_id
= ctx
->argv
[2];
1688 const struct ovsdb_idl_table_class
*table
;
1689 const struct ovsdb_idl_row
*row
;
1692 die_if_error(get_table(table_name
, &table
));
1693 die_if_error(ctl_get_row(ctx
, table
, record_id
, false, &row
));
1695 ctx
->try_again
= true;
1699 for (i
= 3; i
< ctx
->argc
; i
++) {
1702 die_if_error(check_condition(table
, row
, ctx
->argv
[i
], ctx
->symtab
,
1705 ctx
->try_again
= true;
1711 /* Parses one command. */
1713 parse_command(int argc
, char *argv
[], struct shash
*local_options
,
1714 struct ctl_command
*command
)
1716 const struct ctl_command_syntax
*p
;
1717 struct shash_node
*node
;
1721 shash_init(&command
->options
);
1722 shash_swap(local_options
, &command
->options
);
1723 for (i
= 0; i
< argc
; i
++) {
1724 const char *option
= argv
[i
];
1728 if (option
[0] != '-') {
1732 equals
= strchr(option
, '=');
1734 key
= xmemdup0(option
, equals
- option
);
1735 value
= xstrdup(equals
+ 1);
1737 key
= xstrdup(option
);
1741 if (shash_find(&command
->options
, key
)) {
1742 ctl_fatal("'%s' option specified multiple times", argv
[i
]);
1744 shash_add_nocopy(&command
->options
, key
, value
);
1747 ctl_fatal("missing command name (use --help for help)");
1750 p
= shash_find_data(&all_commands
, argv
[i
]);
1752 ctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
1755 SHASH_FOR_EACH (node
, &command
->options
) {
1756 const char *s
= strstr(p
->options
, node
->name
);
1757 int end
= s
? s
[strlen(node
->name
)] : EOF
;
1759 if (!strchr("=,? ", end
)) {
1760 ctl_fatal("'%s' command has no '%s' option",
1761 argv
[i
], node
->name
);
1763 if (end
!= '?' && (end
== '=') != (node
->data
!= NULL
)) {
1765 ctl_fatal("missing argument to '%s' option on '%s' "
1766 "command", node
->name
, argv
[i
]);
1768 ctl_fatal("'%s' option on '%s' does not accept an "
1769 "argument", node
->name
, argv
[i
]);
1774 n_arg
= argc
- i
- 1;
1775 if (n_arg
< p
->min_args
) {
1776 ctl_fatal("'%s' command requires at least %d arguments",
1777 p
->name
, p
->min_args
);
1778 } else if (n_arg
> p
->max_args
) {
1781 for (j
= i
+ 1; j
< argc
; j
++) {
1782 if (argv
[j
][0] == '-') {
1783 ctl_fatal("'%s' command takes at most %d arguments "
1784 "(note that options must precede command "
1785 "names and follow a \"--\" argument)",
1786 p
->name
, p
->max_args
);
1790 ctl_fatal("'%s' command takes at most %d arguments",
1791 p
->name
, p
->max_args
);
1794 command
->syntax
= p
;
1795 command
->argc
= n_arg
+ 1;
1796 command
->argv
= &argv
[i
];
1800 pre_cmd_show(struct ctl_context
*ctx
)
1802 const struct cmd_show_table
*show
;
1804 for (show
= cmd_show_tables
; show
->table
; show
++) {
1807 ovsdb_idl_add_table(ctx
->idl
, show
->table
);
1808 if (show
->name_column
) {
1809 ovsdb_idl_add_column(ctx
->idl
, show
->name_column
);
1811 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1812 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1814 ovsdb_idl_add_column(ctx
->idl
, column
);
1817 if (show
->wref_table
.table
) {
1818 ovsdb_idl_add_table(ctx
->idl
, show
->wref_table
.table
);
1820 if (show
->wref_table
.name_column
) {
1821 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.name_column
);
1823 if (show
->wref_table
.wref_column
) {
1824 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.wref_column
);
1829 static const struct cmd_show_table
*
1830 cmd_show_find_table_by_row(const struct ovsdb_idl_row
*row
)
1832 const struct cmd_show_table
*show
;
1834 for (show
= cmd_show_tables
; show
->table
; show
++) {
1835 if (show
->table
== row
->table
->class_
) {
1842 static const struct cmd_show_table
*
1843 cmd_show_find_table_by_name(const char *name
)
1845 const struct cmd_show_table
*show
;
1847 for (show
= cmd_show_tables
; show
->table
; show
++) {
1848 if (!strcmp(show
->table
->name
, name
)) {
1855 /* Prints table entries that weak reference the 'cur_row'. */
1857 cmd_show_weak_ref(struct ctl_context
*ctx
, const struct cmd_show_table
*show
,
1858 const struct ovsdb_idl_row
*cur_row
, int level
)
1860 const struct ovsdb_idl_row
*row_wref
;
1861 const struct ovsdb_idl_table_class
*table
= show
->wref_table
.table
;
1862 const struct ovsdb_idl_column
*name_column
1863 = show
->wref_table
.name_column
;
1864 const struct ovsdb_idl_column
*wref_column
1865 = show
->wref_table
.wref_column
;
1867 if (!table
|| !name_column
|| !wref_column
) {
1871 for (row_wref
= ovsdb_idl_first_row(ctx
->idl
, table
); row_wref
;
1872 row_wref
= ovsdb_idl_next_row(row_wref
)) {
1873 const struct ovsdb_datum
*wref_datum
1874 = ovsdb_idl_read(row_wref
, wref_column
);
1875 /* If weak reference refers to the 'cur_row', prints it. */
1877 && uuid_equals(&cur_row
->uuid
, &wref_datum
->keys
[0].uuid
)) {
1878 const struct ovsdb_datum
*name_datum
1879 = ovsdb_idl_read(row_wref
, name_column
);
1880 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1881 ds_put_format(&ctx
->output
, "%s ", table
->name
);
1882 ovsdb_datum_to_string(name_datum
, &name_column
->type
, &ctx
->output
);
1883 ds_put_char(&ctx
->output
, '\n');
1888 /* 'shown' records the tables that has been displayed by the current
1889 * command to avoid duplicated prints.
1892 cmd_show_row(struct ctl_context
*ctx
, const struct ovsdb_idl_row
*row
,
1893 int level
, struct sset
*shown
)
1895 const struct cmd_show_table
*show
= cmd_show_find_table_by_row(row
);
1898 ds_put_char_multiple(&ctx
->output
, ' ', level
* 4);
1899 if (show
&& show
->name_column
) {
1900 const struct ovsdb_datum
*datum
;
1902 ds_put_format(&ctx
->output
, "%s ", show
->table
->name
);
1903 datum
= ovsdb_idl_read(row
, show
->name_column
);
1904 ovsdb_datum_to_string(datum
, &show
->name_column
->type
, &ctx
->output
);
1906 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1908 ds_put_char(&ctx
->output
, '\n');
1910 if (!show
|| sset_find(shown
, show
->table
->name
)) {
1914 sset_add(shown
, show
->table
->name
);
1915 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1916 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1917 const struct ovsdb_datum
*datum
;
1923 datum
= ovsdb_idl_read(row
, column
);
1924 if (column
->type
.key
.type
== OVSDB_TYPE_UUID
&&
1925 column
->type
.key
.uuid
.refTableName
) {
1926 const struct cmd_show_table
*ref_show
;
1929 ref_show
= cmd_show_find_table_by_name(
1930 column
->type
.key
.uuid
.refTableName
);
1932 for (j
= 0; j
< datum
->n
; j
++) {
1933 const struct ovsdb_idl_row
*ref_row
;
1935 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1937 &datum
->keys
[j
].uuid
);
1939 cmd_show_row(ctx
, ref_row
, level
+ 1, shown
);
1944 } else if (ovsdb_type_is_map(&column
->type
) &&
1945 column
->type
.value
.type
== OVSDB_TYPE_UUID
&&
1946 column
->type
.value
.uuid
.refTableName
) {
1947 const struct cmd_show_table
*ref_show
;
1950 /* Prints the key to ref'ed table name map if the ref'ed table
1951 * is also defined in 'cmd_show_tables'. */
1952 ref_show
= cmd_show_find_table_by_name(
1953 column
->type
.value
.uuid
.refTableName
);
1954 if (ref_show
&& ref_show
->name_column
) {
1955 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1956 ds_put_format(&ctx
->output
, "%s:\n", column
->name
);
1957 for (j
= 0; j
< datum
->n
; j
++) {
1958 const struct ovsdb_idl_row
*ref_row
;
1960 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1962 &datum
->values
[j
].uuid
);
1964 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 2) * 4);
1965 ovsdb_atom_to_string(&datum
->keys
[j
], column
->type
.key
.type
,
1967 ds_put_char(&ctx
->output
, '=');
1969 const struct ovsdb_datum
*ref_datum
;
1971 ref_datum
= ovsdb_idl_read(ref_row
,
1972 ref_show
->name_column
);
1973 ovsdb_datum_to_string(ref_datum
,
1974 &ref_show
->name_column
->type
,
1977 ds_put_cstr(&ctx
->output
, "\"<null>\"");
1979 ds_put_char(&ctx
->output
, '\n');
1985 if (!ovsdb_datum_is_default(datum
, &column
->type
)) {
1986 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1987 ds_put_format(&ctx
->output
, "%s: ", column
->name
);
1988 ovsdb_datum_to_string(datum
, &column
->type
, &ctx
->output
);
1989 ds_put_char(&ctx
->output
, '\n');
1992 cmd_show_weak_ref(ctx
, show
, row
, level
);
1993 sset_find_and_delete_assert(shown
, show
->table
->name
);
1997 cmd_show(struct ctl_context
*ctx
)
1999 const struct ovsdb_idl_row
*row
;
2000 struct sset shown
= SSET_INITIALIZER(&shown
);
2002 for (row
= ovsdb_idl_first_row(ctx
->idl
, cmd_show_tables
[0].table
);
2003 row
; row
= ovsdb_idl_next_row(row
)) {
2004 cmd_show_row(ctx
, row
, 0, &shown
);
2007 ovs_assert(sset_is_empty(&shown
));
2008 sset_destroy(&shown
);
2012 /* Given pointer to dynamic array 'options_p', array's current size
2013 * 'allocated_options_p' and number of added options 'n_options_p',
2014 * adds all command options to the array. Enlarges the array if
2017 ctl_add_cmd_options(struct option
**options_p
, size_t *n_options_p
,
2018 size_t *allocated_options_p
, int opt_val
)
2021 const struct shash_node
*node
;
2022 size_t n_existing_options
= *n_options_p
;
2024 SHASH_FOR_EACH (node
, &all_commands
) {
2025 const struct ctl_command_syntax
*p
= node
->data
;
2027 if (p
->options
[0]) {
2028 char *save_ptr
= NULL
;
2032 s
= xstrdup(p
->options
);
2033 for (name
= strtok_r(s
, ",", &save_ptr
); name
!= NULL
;
2034 name
= strtok_r(NULL
, ",", &save_ptr
)) {
2035 ovs_assert(name
[0] == '-' && name
[1] == '-' && name
[2]);
2038 size_t n
= strcspn(name
, "=?");
2039 int has_arg
= (name
[n
] == '\0' ? no_argument
2040 : name
[n
] == '=' ? required_argument
2041 : optional_argument
);
2044 o
= find_option(name
, *options_p
, *n_options_p
);
2046 ovs_assert(o
- *options_p
>= n_existing_options
);
2047 ovs_assert(o
->has_arg
== has_arg
);
2049 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
2050 o
->name
= xstrdup(name
);
2051 o
->has_arg
= has_arg
;
2060 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
2061 memset(o
, 0, sizeof *o
);
2064 /* Parses command-line input for commands. */
2065 struct ctl_command
*
2066 ctl_parse_commands(int argc
, char *argv
[], struct shash
*local_options
,
2067 size_t *n_commandsp
)
2069 struct ctl_command
*commands
;
2070 size_t n_commands
, allocated_commands
;
2074 n_commands
= allocated_commands
= 0;
2076 for (start
= i
= 0; i
<= argc
; i
++) {
2077 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
2079 if (n_commands
>= allocated_commands
) {
2080 struct ctl_command
*c
;
2082 commands
= x2nrealloc(commands
, &allocated_commands
,
2084 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2085 shash_moved(&c
->options
);
2088 parse_command(i
- start
, &argv
[start
], local_options
,
2089 &commands
[n_commands
++]);
2090 } else if (!shash_is_empty(local_options
)) {
2091 ctl_fatal("missing command name (use --help for help)");
2097 ctl_fatal("missing command name (use --help for help)");
2099 *n_commandsp
= n_commands
;
2103 /* Prints all registered commands. */
2105 ctl_print_commands(void)
2107 const struct shash_node
*node
;
2109 SHASH_FOR_EACH (node
, &all_commands
) {
2110 const struct ctl_command_syntax
*p
= node
->data
;
2111 char *options
= xstrdup(p
->options
);
2112 char *options_begin
= options
;
2115 for (item
= strsep(&options
, ","); item
!= NULL
;
2116 item
= strsep(&options
, ",")) {
2117 if (item
[0] != '\0') {
2118 printf("[%s] ", item
);
2121 printf(",%s,", p
->name
);
2122 print_command_arguments(p
);
2125 free(options_begin
);
2131 /* Given array of options 'options', prints them. */
2133 ctl_print_options(const struct option
*options
)
2135 for (; options
->name
; options
++) {
2136 const struct option
*o
= options
;
2138 printf("--%s%s\n", o
->name
, o
->has_arg
? "=ARG" : "");
2139 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
2140 printf("-%c%s\n", o
->val
, o
->has_arg
? " ARG" : "");
2147 /* Returns the default local database path. */
2149 ctl_default_db(void)
2153 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
2158 /* Returns true if it looks like this set of arguments might modify the
2159 * database, otherwise false. (Not very smart, so it's prone to false
2162 ctl_might_write_to_db(const struct ctl_command
*commands
, size_t n
)
2164 for (size_t i
= 0; i
< n
; i
++) {
2165 if (commands
[i
].syntax
->mode
== RW
) {
2173 ctl_fatal(const char *format
, ...)
2178 va_start(args
, format
);
2179 message
= xvasprintf(format
, args
);
2182 vlog_set_levels(&this_module
, VLF_CONSOLE
, VLL_OFF
);
2183 VLOG_ERR("%s", message
);
2184 ovs_error(0, "%s", message
);
2185 ctl_exit(EXIT_FAILURE
);
2188 /* Frees the current transaction and the underlying IDL and then calls
2191 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2192 * for a clean memory leak report from valgrind in the normal case. That makes
2193 * it easier to notice real memory leaks. */
2195 ctl_exit(int status
)
2197 if (ctl_exit_func
) {
2198 ctl_exit_func(status
);
2203 /* Comman database commands to be registered. */
2204 static const struct ctl_command_syntax db_ctl_commands
[] = {
2205 {"comment", 0, INT_MAX
, "[ARG]...", NULL
, NULL
, NULL
, "", RO
},
2206 {"get", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get
, cmd_get
,
2207 NULL
, "--if-exists,--id=", RO
},
2208 {"list", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_list
, cmd_list
, NULL
,
2209 "--if-exists,--columns=", RO
},
2210 {"find", 1, INT_MAX
, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find
,
2211 cmd_find
, NULL
, "--columns=", RO
},
2212 {"set", 3, INT_MAX
, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set
,
2213 cmd_set
, NULL
, "--if-exists", RW
},
2214 {"add", 4, INT_MAX
, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add
,
2215 cmd_add
, NULL
, "--if-exists", RW
},
2216 {"remove", 4, INT_MAX
, "TABLE RECORD COLUMN KEY|VALUE|KEY=VALUE...",
2217 pre_cmd_remove
, cmd_remove
, NULL
, "--if-exists", RW
},
2218 {"clear", 3, INT_MAX
, "TABLE RECORD COLUMN...", pre_cmd_clear
, cmd_clear
,
2219 NULL
, "--if-exists", RW
},
2220 {"create", 2, INT_MAX
, "TABLE COLUMN[:KEY]=VALUE...", pre_create
,
2221 cmd_create
, post_create
, "--id=", RW
},
2222 {"destroy", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_destroy
, cmd_destroy
,
2223 NULL
, "--if-exists,--all", RW
},
2224 {"wait-until", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]=VALUE]...",
2225 pre_cmd_wait_until
, cmd_wait_until
, NULL
, "", RO
},
2226 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2230 ctl_register_command(const struct ctl_command_syntax
*command
)
2232 shash_add_assert(&all_commands
, command
->name
, command
);
2235 /* Registers commands represented by 'struct ctl_command_syntax's to
2236 * 'all_commands'. The last element of 'commands' must be an all-NULL
2239 ctl_register_commands(const struct ctl_command_syntax
*commands
)
2241 const struct ctl_command_syntax
*p
;
2243 for (p
= commands
; p
->name
; p
++) {
2244 ctl_register_command(p
);
2248 /* Registers the 'db_ctl_commands' to 'all_commands'. */
2250 ctl_init__(const struct ovsdb_idl_class
*idl_class_
,
2251 const struct ctl_table_class
*ctl_classes_
,
2252 const struct cmd_show_table cmd_show_tables_
[],
2253 void (*ctl_exit_func_
)(int status
))
2255 idl_class
= idl_class_
;
2256 idl_classes
= idl_class_
->tables
;
2257 ctl_classes
= ctl_classes_
;
2258 n_classes
= idl_class
->n_tables
;
2259 ctl_exit_func
= ctl_exit_func_
;
2260 ctl_register_commands(db_ctl_commands
);
2262 cmd_show_tables
= cmd_show_tables_
;
2263 if (cmd_show_tables
) {
2264 static const struct ctl_command_syntax show
=
2265 {"show", 0, 0, "", pre_cmd_show
, cmd_show
, NULL
, "", RO
};
2266 ctl_register_command(&show
);
2270 /* Returns the text for the database commands usage. */
2272 ctl_get_db_cmd_usage(void)
2274 return "Database commands:\n\
2275 list TBL [REC] list RECord (or all records) in TBL\n\
2276 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
2277 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
2278 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
2279 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
2280 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
2281 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
2282 create TBL COL[:KEY]=VALUE create and initialize new record\n\
2283 destroy TBL REC delete RECord from TBL\n\
2284 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
2285 Potentially unsafe database commands require --force option.\n";
2289 ctl_list_db_tables_usage(void)
2291 static struct ds s
= DS_EMPTY_INITIALIZER
;
2296 ds_put_cstr(&s
, "Database commands may reference a row in each table in the following ways:\n");
2297 for (int i
= 0; i
< n_classes
; i
++) {
2298 struct svec options
= SVEC_EMPTY_INITIALIZER
;
2300 svec_add(&options
, "by UUID");
2301 if (idl_classes
[i
].is_singleton
) {
2302 svec_add(&options
, "as \".\"");
2305 for (int j
= 0; j
< ARRAY_SIZE(ctl_classes
[i
].row_ids
); j
++) {
2306 const struct ctl_row_id
*id
= &ctl_classes
[i
].row_ids
[j
];
2307 if (!id
->name_column
) {
2311 struct ds o
= DS_EMPTY_INITIALIZER
;
2312 if (id
->uuid_column
) {
2313 ds_put_format(&o
, "via \"%s\"", id
->uuid_column
->name
);
2314 const struct ovsdb_idl_table_class
*referrer
2315 = ovsdb_idl_table_class_from_column(idl_class
,
2317 if (referrer
!= &idl_classes
[i
]) {
2318 ds_put_format(&o
, " of %s", referrer
->name
);
2321 ds_put_format(&o
, " with matching \"%s:%s\"",
2322 id
->name_column
->name
, id
->key
);
2324 ds_put_format(&o
, " with matching \"%s\"", id
->name_column
->name
);
2326 } else if (id
->key
) {
2327 ds_put_format(&o
, "by \"%s:%s\"", id
->name_column
->name
, id
->key
);
2329 ds_put_format(&o
, "by \"%s\"", id
->name_column
->name
);
2331 svec_add_nocopy(&options
, ds_steal_cstr(&o
));
2334 ds_put_format(&s
, " %s:", idl_classes
[i
].name
);
2335 for (int j
= 0; j
< options
.n
; j
++) {
2336 ds_put_format(&s
, "\n %s", options
.names
[j
]);
2338 ds_put_char(&s
, '\n');
2339 svec_destroy(&options
);
2345 /* Initializes 'ctx' from 'command'. */
2347 ctl_context_init_command(struct ctl_context
*ctx
,
2348 struct ctl_command
*command
)
2350 ctx
->argc
= command
->argc
;
2351 ctx
->argv
= command
->argv
;
2352 ctx
->options
= command
->options
;
2354 ds_swap(&ctx
->output
, &command
->output
);
2355 ctx
->table
= command
->table
;
2356 ctx
->try_again
= false;
2359 /* Initializes the entire 'ctx'. */
2361 ctl_context_init(struct ctl_context
*ctx
, struct ctl_command
*command
,
2362 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2363 struct ovsdb_symbol_table
*symtab
,
2364 void (*invalidate_cache_cb
)(struct ctl_context
*))
2367 ctl_context_init_command(ctx
, command
);
2371 ctx
->symtab
= symtab
;
2372 ctx
->invalidate_cache_cb
= invalidate_cache_cb
;
2375 /* Completes processing of 'command' within 'ctx'. */
2377 ctl_context_done_command(struct ctl_context
*ctx
,
2378 struct ctl_command
*command
)
2380 ds_swap(&ctx
->output
, &command
->output
);
2381 command
->table
= ctx
->table
;
2384 /* Finishes up with 'ctx'.
2386 * If command is nonnull, first calls ctl_context_done_command() to complete
2387 * processing that command within 'ctx'. */
2389 ctl_context_done(struct ctl_context
*ctx
,
2390 struct ctl_command
*command
)
2393 ctl_context_done_command(ctx
, command
);
2395 invalidate_cache(ctx
);
2398 void ctl_set_column(const char *table_name
,
2399 const struct ovsdb_idl_row
*row
, const char *arg
,
2400 struct ovsdb_symbol_table
*symtab
)
2402 const struct ovsdb_idl_table_class
*table
;
2404 die_if_error(get_table(table_name
, &table
));
2405 die_if_error(set_column(table
, row
, arg
, symtab
));