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
);
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 die_if_error(get_column(table
, column_name
, columnp
));
475 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
478 static const struct ovsdb_idl_table_class
*
479 pre_get_table(struct ctl_context
*ctx
, const char *table_name
)
481 const struct ovsdb_idl_table_class
*table
;
482 die_if_error(get_table(table_name
, &table
));
483 ovsdb_idl_add_table(ctx
->idl
, table
);
485 const struct ctl_table_class
*ctl
= &ctl_classes
[table
- idl_classes
];
486 for (int i
= 0; i
< ARRAY_SIZE(ctl
->row_ids
); i
++) {
487 const struct ctl_row_id
*id
= &ctl
->row_ids
[i
];
488 if (id
->name_column
) {
489 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
491 if (id
->uuid_column
) {
492 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
500 missing_operator_error(const char *arg
, const char **allowed_operators
,
506 ds_put_format(&s
, "%s: argument does not end in ", arg
);
507 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
508 if (n_allowed
== 2) {
509 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
510 } else if (n_allowed
> 2) {
513 for (i
= 1; i
< n_allowed
- 1; i
++) {
514 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
516 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
518 ds_put_format(&s
, " followed by a value.");
520 return ds_steal_cstr(&s
);
523 /* Breaks 'arg' apart into a number of fields in the following order:
525 * - The name of a column in 'table', stored into '*columnp'. The column
526 * name may be abbreviated.
528 * - Optionally ':' followed by a key string. The key is stored as a
529 * malloc()'d string into '*keyp', or NULL if no key is present in
532 * - If 'valuep' is nonnull, an operator followed by a value string. The
533 * allowed operators are the 'n_allowed' string in 'allowed_operators',
534 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
535 * index of the operator within 'allowed_operators' is stored into
536 * '*operatorp'. The value is stored as a malloc()'d string into
537 * '*valuep', or NULL if no value is present in 'arg'.
539 * On success, returns NULL. On failure, returned a malloc()'d string error
540 * message and stores NULL into all of the nonnull output arguments. */
541 static char * OVS_WARN_UNUSED_RESULT
542 parse_column_key_value(const char *arg
,
543 const struct ovsdb_idl_table_class
*table
,
544 const struct ovsdb_idl_column
**columnp
, char **keyp
,
546 const char **allowed_operators
, size_t n_allowed
,
553 ovs_assert(!(operatorp
&& !valuep
));
559 /* Parse column name. */
560 error
= ovsdb_token_parse(&p
, &column_name
);
564 if (column_name
[0] == '\0') {
566 error
= xasprintf("%s: missing column name", arg
);
569 error
= get_column(table
, column_name
, columnp
);
575 /* Parse key string. */
578 error
= ovsdb_token_parse(&p
, keyp
);
584 /* Parse value string. */
590 if (!allowed_operators
) {
591 static const char *equals
= "=";
592 allowed_operators
= &equals
;
598 for (i
= 0; i
< n_allowed
; i
++) {
599 const char *op
= allowed_operators
[i
];
600 size_t op_len
= strlen(op
);
602 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
608 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
615 *valuep
= xstrdup(p
+ best_len
);
618 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
639 static const struct ovsdb_idl_column
*
640 pre_parse_column_key_value(struct ctl_context
*ctx
,
642 const struct ovsdb_idl_table_class
*table
)
644 const struct ovsdb_idl_column
*column
;
649 die_if_error(ovsdb_token_parse(&p
, &column_name
));
650 if (column_name
[0] == '\0') {
651 ctl_fatal("%s: missing column name", arg
);
654 pre_get_column(ctx
, table
, column_name
, &column
);
660 /* Checks if the 'column' is mutable. Returns NULL if it is mutable, or a
661 * malloc()'ed error message otherwise. */
662 static char * OVS_WARN_UNUSED_RESULT
663 check_mutable(const struct ovsdb_idl_row
*row
,
664 const struct ovsdb_idl_column
*column
)
666 if (!ovsdb_idl_is_mutable(row
, column
)) {
667 return xasprintf("cannot modify read-only column %s in table %s",
668 column
->name
, row
->table
->class_
->name
);
674 RELOP(RELOP_EQ, "=") \
675 RELOP(RELOP_NE, "!=") \
676 RELOP(RELOP_LT, "<") \
677 RELOP(RELOP_GT, ">") \
678 RELOP(RELOP_LE, "<=") \
679 RELOP(RELOP_GE, ">=") \
680 RELOP(RELOP_SET_EQ, "{=}") \
681 RELOP(RELOP_SET_NE, "{!=}") \
682 RELOP(RELOP_SET_LT, "{<}") \
683 RELOP(RELOP_SET_GT, "{>}") \
684 RELOP(RELOP_SET_LE, "{<=}") \
685 RELOP(RELOP_SET_GE, "{>=}")
688 #define RELOP(ENUM, STRING) ENUM,
694 is_set_operator(enum relop op
)
696 return (op
== RELOP_SET_EQ
|| op
== RELOP_SET_NE
||
697 op
== RELOP_SET_LT
|| op
== RELOP_SET_GT
||
698 op
== RELOP_SET_LE
|| op
== RELOP_SET_GE
);
702 evaluate_relop(const struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
703 const struct ovsdb_type
*type
, enum relop op
)
708 return ovsdb_datum_compare_3way(a
, b
, type
) == 0;
711 return ovsdb_datum_compare_3way(a
, b
, type
) != 0;
713 return ovsdb_datum_compare_3way(a
, b
, type
) < 0;
715 return ovsdb_datum_compare_3way(a
, b
, type
) > 0;
717 return ovsdb_datum_compare_3way(a
, b
, type
) <= 0;
719 return ovsdb_datum_compare_3way(a
, b
, type
) >= 0;
722 return b
->n
> a
->n
&& ovsdb_datum_includes_all(a
, b
, type
);
724 return a
->n
> b
->n
&& ovsdb_datum_includes_all(b
, a
, type
);
726 return ovsdb_datum_includes_all(a
, b
, type
);
728 return ovsdb_datum_includes_all(b
, a
, type
);
735 /* Checks if given row satisfies the specified condition. Returns the result of
736 * evaluating the condition in 'satisfied' flag and NULL as a return value on
737 * success. On failure returns a malloc()'ed error message and 'satisfied'
738 * value is not modified. */
739 static char * OVS_WARN_UNUSED_RESULT
740 check_condition(const struct ovsdb_idl_table_class
*table
,
741 const struct ovsdb_idl_row
*row
, const char *arg
,
742 struct ovsdb_symbol_table
*symtab
, bool *satisfied
)
744 static const char *operators
[] = {
745 #define RELOP(ENUM, STRING) STRING,
750 const struct ovsdb_idl_column
*column
;
751 const struct ovsdb_datum
*have_datum
;
752 char *key_string
= NULL
;
753 char *value_string
= NULL
;
754 struct ovsdb_type type
;
759 ovs_assert(satisfied
);
761 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
762 &operator, operators
, ARRAY_SIZE(operators
),
768 error
= xasprintf("%s: missing value", arg
);
773 type
.n_max
= UINT_MAX
;
775 have_datum
= ovsdb_idl_read(row
, column
);
777 union ovsdb_atom want_key
;
778 struct ovsdb_datum b
;
781 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
782 error
= xasprintf("cannot specify key to check for non-map column "
787 error
= ovsdb_atom_from_string(&want_key
, NULL
, &column
->type
.key
,
793 type
.key
= type
.value
;
794 type
.value
.type
= OVSDB_TYPE_VOID
;
795 error
= ovsdb_datum_from_string(&b
, &type
, value_string
, symtab
);
800 idx
= ovsdb_datum_find_key(have_datum
,
801 &want_key
, column
->type
.key
.type
);
802 if (idx
== UINT_MAX
&& !is_set_operator(operator)) {
805 struct ovsdb_datum a
;
807 if (idx
!= UINT_MAX
) {
809 a
.keys
= &have_datum
->values
[idx
];
817 retval
= evaluate_relop(&a
, &b
, &type
, operator);
820 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
821 ovsdb_datum_destroy(&b
, &type
);
823 struct ovsdb_datum want_datum
;
825 error
= ovsdb_datum_from_string(&want_datum
, &column
->type
,
826 value_string
, symtab
);
830 retval
= evaluate_relop(have_datum
, &want_datum
, &type
, operator);
831 ovsdb_datum_destroy(&want_datum
, &column
->type
);
843 invalidate_cache(struct ctl_context
*ctx
)
845 if (ctx
->invalidate_cache_cb
) {
846 (ctx
->invalidate_cache_cb
)(ctx
);
851 pre_cmd_get(struct ctl_context
*ctx
)
853 const char *id
= shash_find_data(&ctx
->options
, "--id");
854 const char *table_name
= ctx
->argv
[1];
855 const struct ovsdb_idl_table_class
*table
;
858 /* Using "get" without --id or a column name could possibly make sense.
859 * Maybe, for example, a *ctl command run wants to assert that a row
860 * exists. But it is unlikely that an interactive user would want to do
861 * that, so issue a warning if we're running on a terminal. */
862 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
863 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
864 "possibly erroneous");
867 table
= pre_get_table(ctx
, table_name
);
868 for (i
= 3; i
< ctx
->argc
; i
++) {
869 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
870 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
874 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
879 cmd_get(struct ctl_context
*ctx
)
881 const char *id
= shash_find_data(&ctx
->options
, "--id");
882 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
883 const char *table_name
= ctx
->argv
[1];
884 const char *record_id
= ctx
->argv
[2];
885 const struct ovsdb_idl_table_class
*table
;
886 const struct ovsdb_idl_row
*row
;
887 struct ds
*out
= &ctx
->output
;
890 if (id
&& !must_exist
) {
891 ctl_fatal("--if-exists and --id may not be specified together");
894 die_if_error(get_table(table_name
, &table
));
895 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
901 struct ovsdb_symbol
*symbol
;
904 die_if_error(create_symbol(ctx
->symtab
, id
, &symbol
, &new));
906 ctl_fatal("row id \"%s\" specified on \"get\" command was used "
907 "before it was defined", id
);
909 symbol
->uuid
= row
->uuid
;
911 /* This symbol refers to a row that already exists, so disable warnings
912 * about it being unreferenced. */
913 symbol
->strong_ref
= true;
915 for (i
= 3; i
< ctx
->argc
; i
++) {
916 const struct ovsdb_idl_column
*column
;
917 const struct ovsdb_datum
*datum
;
920 /* Special case for obtaining the UUID of a row. We can't just do this
921 * through parse_column_key_value() below since it returns a "struct
922 * ovsdb_idl_column" and the UUID column doesn't have one. */
923 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
924 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
925 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
929 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
930 &column
, &key_string
,
931 NULL
, NULL
, 0, NULL
));
933 ovsdb_idl_txn_verify(row
, column
);
934 datum
= ovsdb_idl_read(row
, column
);
936 union ovsdb_atom key
;
939 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
940 ctl_fatal("cannot specify key to get for non-map column %s",
944 die_if_error(ovsdb_atom_from_string(&key
, NULL
,
946 key_string
, ctx
->symtab
));
948 idx
= ovsdb_datum_find_key(datum
, &key
,
949 column
->type
.key
.type
);
950 if (idx
== UINT_MAX
) {
952 ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
953 key_string
, table
->name
, record_id
,
957 ovsdb_atom_to_string(&datum
->values
[idx
],
958 column
->type
.value
.type
, out
);
960 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
962 ovsdb_datum_to_string(datum
, &column
->type
, out
);
964 ds_put_char(out
, '\n');
970 /* Returns NULL on success or malloc()'ed error message on failure. */
971 static char * OVS_WARN_UNUSED_RESULT
972 parse_column_names(const char *column_names
,
973 const struct ovsdb_idl_table_class
*table
,
974 const struct ovsdb_idl_column
***columnsp
,
977 const struct ovsdb_idl_column
**columns
;
983 n_columns
= table
->n_columns
+ 1;
984 columns
= xmalloc(n_columns
* sizeof *columns
);
986 for (i
= 0; i
< table
->n_columns
; i
++) {
987 columns
[i
+ 1] = &table
->columns
[i
];
990 char *s
= xstrdup(column_names
);
991 size_t allocated_columns
;
992 char *save_ptr
= NULL
;
996 allocated_columns
= n_columns
= 0;
997 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
998 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
999 const struct ovsdb_idl_column
*column
;
1001 if (!strcasecmp(column_name
, "_uuid")) {
1004 char *error
= get_column(table
, column_name
, &column
);
1011 if (n_columns
>= allocated_columns
) {
1012 columns
= x2nrealloc(columns
, &allocated_columns
,
1015 columns
[n_columns
++] = column
;
1020 return xstrdup("must specify at least one column name");
1023 *columnsp
= columns
;
1024 *n_columnsp
= n_columns
;
1029 pre_list_columns(struct ctl_context
*ctx
,
1030 const struct ovsdb_idl_table_class
*table
,
1031 const char *column_names
)
1033 const struct ovsdb_idl_column
**columns
;
1037 die_if_error(parse_column_names(column_names
, table
, &columns
, &n_columns
));
1038 for (i
= 0; i
< n_columns
; i
++) {
1040 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
1047 pre_cmd_list(struct ctl_context
*ctx
)
1049 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1050 const char *table_name
= ctx
->argv
[1];
1051 const struct ovsdb_idl_table_class
*table
;
1053 table
= pre_get_table(ctx
, table_name
);
1054 pre_list_columns(ctx
, table
, column_names
);
1057 static struct table
*
1058 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
1063 out
= xmalloc(sizeof *out
);
1066 for (i
= 0; i
< n_columns
; i
++) {
1067 const struct ovsdb_idl_column
*column
= columns
[i
];
1068 const char *column_name
= column
? column
->name
: "_uuid";
1070 table_add_column(out
, "%s", column_name
);
1077 list_record(const struct ovsdb_idl_row
*row
,
1078 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
1088 for (i
= 0; i
< n_columns
; i
++) {
1089 const struct ovsdb_idl_column
*column
= columns
[i
];
1090 struct cell
*cell
= table_add_cell(out
);
1093 struct ovsdb_datum datum
;
1094 union ovsdb_atom atom
;
1096 atom
.uuid
= row
->uuid
;
1099 datum
.values
= NULL
;
1102 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
1103 cell
->type
= &ovsdb_type_uuid
;
1105 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
1107 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
1108 cell
->type
= &column
->type
;
1114 cmd_list(struct ctl_context
*ctx
)
1116 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1117 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1118 const struct ovsdb_idl_column
**columns
;
1119 const char *table_name
= ctx
->argv
[1];
1120 const struct ovsdb_idl_table_class
*table
;
1125 die_if_error(get_table(table_name
, &table
));
1126 die_if_error(parse_column_names(column_names
, table
, &columns
, &n_columns
));
1127 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1128 if (ctx
->argc
> 2) {
1129 for (i
= 2; i
< ctx
->argc
; i
++) {
1130 const struct ovsdb_idl_row
*row
;
1132 die_if_error(ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
,
1134 list_record(row
, columns
, n_columns
, out
);
1137 const struct ovsdb_idl_row
*row
;
1139 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
!= NULL
;
1140 row
= ovsdb_idl_next_row(row
)) {
1141 list_record(row
, columns
, n_columns
, out
);
1147 /* Finds the "struct ovsdb_idl_table_class *" with 'table_name' by searching
1148 * the tables in these schema. Returns NULL and sets 'tablep' on success, or a
1149 * malloc()'ed error message on failure. */
1150 static char * OVS_WARN_UNUSED_RESULT
1151 get_table(const char *table_name
, const struct ovsdb_idl_table_class
**tablep
)
1153 const struct ovsdb_idl_table_class
*best_match
= NULL
;
1154 unsigned int best_score
= 0;
1157 for (const struct ovsdb_idl_table_class
*table
= idl_classes
;
1158 table
< &idl_classes
[n_classes
]; table
++) {
1159 unsigned int score
= score_partial_match(table
->name
, table_name
);
1160 if (score
> best_score
) {
1163 } else if (score
== best_score
) {
1168 *tablep
= best_match
;
1169 } else if (best_score
) {
1170 error
= xasprintf("multiple table names match \"%s\"", table_name
);
1172 error
= xasprintf("unknown table \"%s\"", table_name
);
1178 pre_cmd_find(struct ctl_context
*ctx
)
1180 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1181 const char *table_name
= ctx
->argv
[1];
1182 const struct ovsdb_idl_table_class
*table
;
1185 table
= pre_get_table(ctx
, table_name
);
1186 pre_list_columns(ctx
, table
, column_names
);
1187 for (i
= 2; i
< ctx
->argc
; i
++) {
1188 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1193 cmd_find(struct ctl_context
*ctx
)
1195 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1196 const struct ovsdb_idl_column
**columns
;
1197 const char *table_name
= ctx
->argv
[1];
1198 const struct ovsdb_idl_table_class
*table
;
1199 const struct ovsdb_idl_row
*row
;
1203 die_if_error(get_table(table_name
, &table
));
1204 die_if_error(parse_column_names(column_names
, table
, &columns
, &n_columns
));
1205 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1206 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
;
1207 row
= ovsdb_idl_next_row(row
)) {
1210 for (i
= 2; i
< ctx
->argc
; i
++) {
1213 die_if_error(check_condition(table
, row
, ctx
->argv
[i
], ctx
->symtab
,
1219 list_record(row
, columns
, n_columns
, out
);
1226 /* Sets the column of 'row' in 'table'. Returns NULL on success or a
1227 * malloc()'ed error message on failure. */
1228 static char * OVS_WARN_UNUSED_RESULT
1229 set_column(const struct ovsdb_idl_table_class
*table
,
1230 const struct ovsdb_idl_row
*row
, const char *arg
,
1231 struct ovsdb_symbol_table
*symtab
)
1233 const struct ovsdb_idl_column
*column
;
1234 char *key_string
= NULL
;
1235 char *value_string
= NULL
;
1238 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
1239 NULL
, NULL
, 0, &value_string
);
1243 if (!value_string
) {
1244 error
= xasprintf("%s: missing value", arg
);
1247 error
= check_mutable(row
, column
);
1253 union ovsdb_atom key
, value
;
1254 struct ovsdb_datum datum
;
1256 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
1257 error
= xasprintf("cannot specify key to set for non-map column "
1258 "%s", column
->name
);
1262 error
= ovsdb_atom_from_string(&key
, NULL
, &column
->type
.key
,
1263 key_string
, symtab
);
1267 error
= ovsdb_atom_from_string(&value
, NULL
, &column
->type
.value
,
1268 value_string
, symtab
);
1273 ovsdb_datum_init_empty(&datum
);
1274 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
, NULL
);
1276 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1277 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
1279 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
1280 &column
->type
, false);
1281 ovsdb_idl_txn_verify(row
, column
);
1282 ovsdb_idl_txn_write(row
, column
, &datum
);
1284 struct ovsdb_datum datum
;
1286 error
= ovsdb_datum_from_string(&datum
, &column
->type
,
1287 value_string
, symtab
);
1291 ovsdb_idl_txn_write(row
, column
, &datum
);
1302 pre_cmd_set(struct ctl_context
*ctx
)
1304 const char *table_name
= ctx
->argv
[1];
1305 const struct ovsdb_idl_table_class
*table
;
1308 table
= pre_get_table(ctx
, table_name
);
1309 for (i
= 3; i
< ctx
->argc
; i
++) {
1310 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1315 cmd_set(struct ctl_context
*ctx
)
1317 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1318 const char *table_name
= ctx
->argv
[1];
1319 const char *record_id
= ctx
->argv
[2];
1320 const struct ovsdb_idl_table_class
*table
;
1321 const struct ovsdb_idl_row
*row
;
1324 die_if_error(get_table(table_name
, &table
));
1325 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1330 for (i
= 3; i
< ctx
->argc
; i
++) {
1331 die_if_error(set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
));
1334 invalidate_cache(ctx
);
1338 pre_cmd_add(struct ctl_context
*ctx
)
1340 const char *table_name
= ctx
->argv
[1];
1341 const char *column_name
= ctx
->argv
[3];
1342 const struct ovsdb_idl_table_class
*table
;
1343 const struct ovsdb_idl_column
*column
;
1345 table
= pre_get_table(ctx
, table_name
);
1346 pre_get_column(ctx
, table
, column_name
, &column
);
1350 cmd_add(struct ctl_context
*ctx
)
1352 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1353 const char *table_name
= ctx
->argv
[1];
1354 const char *record_id
= ctx
->argv
[2];
1355 const char *column_name
= ctx
->argv
[3];
1356 const struct ovsdb_idl_table_class
*table
;
1357 const struct ovsdb_idl_column
*column
;
1358 const struct ovsdb_idl_row
*row
;
1359 const struct ovsdb_type
*type
;
1360 struct ovsdb_datum old
;
1363 die_if_error(get_table(table_name
, &table
));
1364 die_if_error(get_column(table
, column_name
, &column
));
1365 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1369 die_if_error(check_mutable(row
, column
));
1371 type
= &column
->type
;
1372 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1373 for (i
= 4; i
< ctx
->argc
; i
++) {
1374 struct ovsdb_type add_type
;
1375 struct ovsdb_datum add
;
1379 add_type
.n_max
= UINT_MAX
;
1380 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
1382 ovsdb_datum_union(&old
, &add
, type
, false);
1383 ovsdb_datum_destroy(&add
, type
);
1385 if (old
.n
> type
->n_max
) {
1386 ctl_fatal("\"add\" operation would put %u %s in column %s of "
1387 "table %s but the maximum number is %u",
1389 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1390 column
->name
, table
->name
, type
->n_max
);
1392 ovsdb_idl_txn_verify(row
, column
);
1393 ovsdb_idl_txn_write(row
, column
, &old
);
1395 invalidate_cache(ctx
);
1399 pre_cmd_remove(struct ctl_context
*ctx
)
1401 const char *table_name
= ctx
->argv
[1];
1402 const char *column_name
= ctx
->argv
[3];
1403 const struct ovsdb_idl_table_class
*table
;
1404 const struct ovsdb_idl_column
*column
;
1406 table
= pre_get_table(ctx
, table_name
);
1407 pre_get_column(ctx
, table
, column_name
, &column
);
1411 cmd_remove(struct ctl_context
*ctx
)
1413 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1414 const char *table_name
= ctx
->argv
[1];
1415 const char *record_id
= ctx
->argv
[2];
1416 const char *column_name
= ctx
->argv
[3];
1417 const struct ovsdb_idl_table_class
*table
;
1418 const struct ovsdb_idl_column
*column
;
1419 const struct ovsdb_idl_row
*row
;
1420 const struct ovsdb_type
*type
;
1421 struct ovsdb_datum old
;
1424 die_if_error(get_table(table_name
, &table
));
1425 die_if_error(get_column(table
, column_name
, &column
));
1426 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1430 die_if_error(check_mutable(row
, column
));
1432 type
= &column
->type
;
1433 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1434 for (i
= 4; i
< ctx
->argc
; i
++) {
1435 struct ovsdb_type rm_type
;
1436 struct ovsdb_datum rm
;
1441 rm_type
.n_max
= UINT_MAX
;
1442 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
1443 ctx
->argv
[i
], ctx
->symtab
);
1446 if (ovsdb_type_is_map(&rm_type
)) {
1447 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
1449 die_if_error(ovsdb_datum_from_string(
1450 &rm
, &rm_type
, ctx
->argv
[i
], ctx
->symtab
));
1452 ctl_fatal("%s", error
);
1455 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
1456 ovsdb_datum_destroy(&rm
, &rm_type
);
1458 if (old
.n
< type
->n_min
) {
1459 ctl_fatal("\"remove\" operation would put %u %s in column %s of "
1460 "table %s but the minimum number is %u",
1462 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1463 column
->name
, table
->name
, type
->n_min
);
1465 ovsdb_idl_txn_verify(row
, column
);
1466 ovsdb_idl_txn_write(row
, column
, &old
);
1468 invalidate_cache(ctx
);
1472 pre_cmd_clear(struct ctl_context
*ctx
)
1474 const char *table_name
= ctx
->argv
[1];
1475 const struct ovsdb_idl_table_class
*table
;
1478 table
= pre_get_table(ctx
, table_name
);
1479 for (i
= 3; i
< ctx
->argc
; i
++) {
1480 const struct ovsdb_idl_column
*column
;
1482 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
1487 cmd_clear(struct ctl_context
*ctx
)
1489 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1490 const char *table_name
= ctx
->argv
[1];
1491 const char *record_id
= ctx
->argv
[2];
1492 const struct ovsdb_idl_table_class
*table
;
1493 const struct ovsdb_idl_row
*row
;
1496 die_if_error(get_table(table_name
, &table
));
1497 die_if_error(ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
));
1502 for (i
= 3; i
< ctx
->argc
; i
++) {
1503 const struct ovsdb_idl_column
*column
;
1504 const struct ovsdb_type
*type
;
1505 struct ovsdb_datum datum
;
1507 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
1508 die_if_error(check_mutable(row
, column
));
1510 type
= &column
->type
;
1511 if (type
->n_min
> 0) {
1512 ctl_fatal("\"clear\" operation cannot be applied to column %s "
1513 "of table %s, which is not allowed to be empty",
1514 column
->name
, table
->name
);
1517 ovsdb_datum_init_empty(&datum
);
1518 ovsdb_idl_txn_write(row
, column
, &datum
);
1521 invalidate_cache(ctx
);
1525 pre_create(struct ctl_context
*ctx
)
1527 const char *id
= shash_find_data(&ctx
->options
, "--id");
1528 const char *table_name
= ctx
->argv
[1];
1529 const struct ovsdb_idl_table_class
*table
;
1531 die_if_error(get_table(table_name
, &table
));
1532 if (!id
&& !table
->is_root
) {
1533 VLOG_WARN("applying \"create\" command to table %s without --id "
1534 "option will have no effect", table
->name
);
1539 cmd_create(struct ctl_context
*ctx
)
1541 const char *id
= shash_find_data(&ctx
->options
, "--id");
1542 const char *table_name
= ctx
->argv
[1];
1543 const struct ovsdb_idl_table_class
*table
;
1544 const struct ovsdb_idl_row
*row
;
1545 const struct uuid
*uuid
;
1548 die_if_error(get_table(table_name
, &table
));
1550 struct ovsdb_symbol
*symbol
;
1552 die_if_error(create_symbol(ctx
->symtab
, id
, &symbol
, NULL
));
1553 if (table
->is_root
) {
1554 /* This table is in the root set, meaning that rows created in it
1555 * won't disappear even if they are unreferenced, so disable
1556 * warnings about that by pretending that there is a reference. */
1557 symbol
->strong_ref
= true;
1559 uuid
= &symbol
->uuid
;
1564 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
, uuid
);
1565 for (i
= 2; i
< ctx
->argc
; i
++) {
1566 die_if_error(set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
));
1568 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1571 /* This function may be used as the 'postprocess' function for commands that
1572 * insert new rows into the database. It expects that the command's 'run'
1573 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
1574 * sole output. It replaces that output by the row's permanent UUID assigned
1575 * by the database server and appends a new-line.
1577 * Currently we use this only for "create", because the higher-level commands
1578 * are supposed to be independent of the actual structure of the vswitch
1581 post_create(struct ctl_context
*ctx
)
1583 const struct uuid
*real
;
1586 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
1589 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
1591 ds_clear(&ctx
->output
);
1592 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
1594 ds_put_char(&ctx
->output
, '\n');
1598 pre_cmd_destroy(struct ctl_context
*ctx
)
1600 const char *table_name
= ctx
->argv
[1];
1602 pre_get_table(ctx
, table_name
);
1606 cmd_destroy(struct ctl_context
*ctx
)
1608 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1609 bool delete_all
= shash_find(&ctx
->options
, "--all");
1610 const char *table_name
= ctx
->argv
[1];
1611 const struct ovsdb_idl_table_class
*table
;
1614 die_if_error(get_table(table_name
, &table
));
1616 if (delete_all
&& ctx
->argc
> 2) {
1617 ctl_fatal("--all and records argument should not be specified together");
1620 if (delete_all
&& !must_exist
) {
1621 ctl_fatal("--all and --if-exists should not be specified together");
1625 const struct ovsdb_idl_row
*row
;
1626 const struct ovsdb_idl_row
*next_row
;
1628 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
);
1630 next_row
= ovsdb_idl_next_row(row
);
1631 ovsdb_idl_txn_delete(row
);
1635 for (i
= 2; i
< ctx
->argc
; i
++) {
1636 const struct ovsdb_idl_row
*row
;
1638 die_if_error(ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
,
1641 ovsdb_idl_txn_delete(row
);
1645 invalidate_cache(ctx
);
1649 pre_cmd_wait_until(struct ctl_context
*ctx
)
1651 const char *table_name
= ctx
->argv
[1];
1652 const struct ovsdb_idl_table_class
*table
;
1655 table
= pre_get_table(ctx
, table_name
);
1657 for (i
= 3; i
< ctx
->argc
; i
++) {
1658 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1663 cmd_wait_until(struct ctl_context
*ctx
)
1665 const char *table_name
= ctx
->argv
[1];
1666 const char *record_id
= ctx
->argv
[2];
1667 const struct ovsdb_idl_table_class
*table
;
1668 const struct ovsdb_idl_row
*row
;
1671 die_if_error(get_table(table_name
, &table
));
1672 die_if_error(ctl_get_row(ctx
, table
, record_id
, false, &row
));
1674 ctx
->try_again
= true;
1678 for (i
= 3; i
< ctx
->argc
; i
++) {
1681 die_if_error(check_condition(table
, row
, ctx
->argv
[i
], ctx
->symtab
,
1684 ctx
->try_again
= true;
1690 /* Parses one command. */
1692 parse_command(int argc
, char *argv
[], struct shash
*local_options
,
1693 struct ctl_command
*command
)
1695 const struct ctl_command_syntax
*p
;
1696 struct shash_node
*node
;
1700 shash_init(&command
->options
);
1701 shash_swap(local_options
, &command
->options
);
1702 for (i
= 0; i
< argc
; i
++) {
1703 const char *option
= argv
[i
];
1707 if (option
[0] != '-') {
1711 equals
= strchr(option
, '=');
1713 key
= xmemdup0(option
, equals
- option
);
1714 value
= xstrdup(equals
+ 1);
1716 key
= xstrdup(option
);
1720 if (shash_find(&command
->options
, key
)) {
1721 ctl_fatal("'%s' option specified multiple times", argv
[i
]);
1723 shash_add_nocopy(&command
->options
, key
, value
);
1726 ctl_fatal("missing command name (use --help for help)");
1729 p
= shash_find_data(&all_commands
, argv
[i
]);
1731 ctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
1734 SHASH_FOR_EACH (node
, &command
->options
) {
1735 const char *s
= strstr(p
->options
, node
->name
);
1736 int end
= s
? s
[strlen(node
->name
)] : EOF
;
1738 if (!strchr("=,? ", end
)) {
1739 ctl_fatal("'%s' command has no '%s' option",
1740 argv
[i
], node
->name
);
1742 if (end
!= '?' && (end
== '=') != (node
->data
!= NULL
)) {
1744 ctl_fatal("missing argument to '%s' option on '%s' "
1745 "command", node
->name
, argv
[i
]);
1747 ctl_fatal("'%s' option on '%s' does not accept an "
1748 "argument", node
->name
, argv
[i
]);
1753 n_arg
= argc
- i
- 1;
1754 if (n_arg
< p
->min_args
) {
1755 ctl_fatal("'%s' command requires at least %d arguments",
1756 p
->name
, p
->min_args
);
1757 } else if (n_arg
> p
->max_args
) {
1760 for (j
= i
+ 1; j
< argc
; j
++) {
1761 if (argv
[j
][0] == '-') {
1762 ctl_fatal("'%s' command takes at most %d arguments "
1763 "(note that options must precede command "
1764 "names and follow a \"--\" argument)",
1765 p
->name
, p
->max_args
);
1769 ctl_fatal("'%s' command takes at most %d arguments",
1770 p
->name
, p
->max_args
);
1773 command
->syntax
= p
;
1774 command
->argc
= n_arg
+ 1;
1775 command
->argv
= &argv
[i
];
1779 pre_cmd_show(struct ctl_context
*ctx
)
1781 const struct cmd_show_table
*show
;
1783 for (show
= cmd_show_tables
; show
->table
; show
++) {
1786 ovsdb_idl_add_table(ctx
->idl
, show
->table
);
1787 if (show
->name_column
) {
1788 ovsdb_idl_add_column(ctx
->idl
, show
->name_column
);
1790 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1791 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1793 ovsdb_idl_add_column(ctx
->idl
, column
);
1796 if (show
->wref_table
.table
) {
1797 ovsdb_idl_add_table(ctx
->idl
, show
->wref_table
.table
);
1799 if (show
->wref_table
.name_column
) {
1800 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.name_column
);
1802 if (show
->wref_table
.wref_column
) {
1803 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.wref_column
);
1808 static const struct cmd_show_table
*
1809 cmd_show_find_table_by_row(const struct ovsdb_idl_row
*row
)
1811 const struct cmd_show_table
*show
;
1813 for (show
= cmd_show_tables
; show
->table
; show
++) {
1814 if (show
->table
== row
->table
->class_
) {
1821 static const struct cmd_show_table
*
1822 cmd_show_find_table_by_name(const char *name
)
1824 const struct cmd_show_table
*show
;
1826 for (show
= cmd_show_tables
; show
->table
; show
++) {
1827 if (!strcmp(show
->table
->name
, name
)) {
1834 /* Prints table entries that weak reference the 'cur_row'. */
1836 cmd_show_weak_ref(struct ctl_context
*ctx
, const struct cmd_show_table
*show
,
1837 const struct ovsdb_idl_row
*cur_row
, int level
)
1839 const struct ovsdb_idl_row
*row_wref
;
1840 const struct ovsdb_idl_table_class
*table
= show
->wref_table
.table
;
1841 const struct ovsdb_idl_column
*name_column
1842 = show
->wref_table
.name_column
;
1843 const struct ovsdb_idl_column
*wref_column
1844 = show
->wref_table
.wref_column
;
1846 if (!table
|| !name_column
|| !wref_column
) {
1850 for (row_wref
= ovsdb_idl_first_row(ctx
->idl
, table
); row_wref
;
1851 row_wref
= ovsdb_idl_next_row(row_wref
)) {
1852 const struct ovsdb_datum
*wref_datum
1853 = ovsdb_idl_read(row_wref
, wref_column
);
1854 /* If weak reference refers to the 'cur_row', prints it. */
1856 && uuid_equals(&cur_row
->uuid
, &wref_datum
->keys
[0].uuid
)) {
1857 const struct ovsdb_datum
*name_datum
1858 = ovsdb_idl_read(row_wref
, name_column
);
1859 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1860 ds_put_format(&ctx
->output
, "%s ", table
->name
);
1861 ovsdb_datum_to_string(name_datum
, &name_column
->type
, &ctx
->output
);
1862 ds_put_char(&ctx
->output
, '\n');
1867 /* 'shown' records the tables that has been displayed by the current
1868 * command to avoid duplicated prints.
1871 cmd_show_row(struct ctl_context
*ctx
, const struct ovsdb_idl_row
*row
,
1872 int level
, struct sset
*shown
)
1874 const struct cmd_show_table
*show
= cmd_show_find_table_by_row(row
);
1877 ds_put_char_multiple(&ctx
->output
, ' ', level
* 4);
1878 if (show
&& show
->name_column
) {
1879 const struct ovsdb_datum
*datum
;
1881 ds_put_format(&ctx
->output
, "%s ", show
->table
->name
);
1882 datum
= ovsdb_idl_read(row
, show
->name_column
);
1883 ovsdb_datum_to_string(datum
, &show
->name_column
->type
, &ctx
->output
);
1885 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1887 ds_put_char(&ctx
->output
, '\n');
1889 if (!show
|| sset_find(shown
, show
->table
->name
)) {
1893 sset_add(shown
, show
->table
->name
);
1894 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1895 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1896 const struct ovsdb_datum
*datum
;
1902 datum
= ovsdb_idl_read(row
, column
);
1903 if (column
->type
.key
.type
== OVSDB_TYPE_UUID
&&
1904 column
->type
.key
.uuid
.refTableName
) {
1905 const struct cmd_show_table
*ref_show
;
1908 ref_show
= cmd_show_find_table_by_name(
1909 column
->type
.key
.uuid
.refTableName
);
1911 for (j
= 0; j
< datum
->n
; j
++) {
1912 const struct ovsdb_idl_row
*ref_row
;
1914 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1916 &datum
->keys
[j
].uuid
);
1918 cmd_show_row(ctx
, ref_row
, level
+ 1, shown
);
1923 } else if (ovsdb_type_is_map(&column
->type
) &&
1924 column
->type
.value
.type
== OVSDB_TYPE_UUID
&&
1925 column
->type
.value
.uuid
.refTableName
) {
1926 const struct cmd_show_table
*ref_show
;
1929 /* Prints the key to ref'ed table name map if the ref'ed table
1930 * is also defined in 'cmd_show_tables'. */
1931 ref_show
= cmd_show_find_table_by_name(
1932 column
->type
.value
.uuid
.refTableName
);
1933 if (ref_show
&& ref_show
->name_column
) {
1934 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1935 ds_put_format(&ctx
->output
, "%s:\n", column
->name
);
1936 for (j
= 0; j
< datum
->n
; j
++) {
1937 const struct ovsdb_idl_row
*ref_row
;
1939 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1941 &datum
->values
[j
].uuid
);
1943 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 2) * 4);
1944 ovsdb_atom_to_string(&datum
->keys
[j
], column
->type
.key
.type
,
1946 ds_put_char(&ctx
->output
, '=');
1948 const struct ovsdb_datum
*ref_datum
;
1950 ref_datum
= ovsdb_idl_read(ref_row
,
1951 ref_show
->name_column
);
1952 ovsdb_datum_to_string(ref_datum
,
1953 &ref_show
->name_column
->type
,
1956 ds_put_cstr(&ctx
->output
, "\"<null>\"");
1958 ds_put_char(&ctx
->output
, '\n');
1964 if (!ovsdb_datum_is_default(datum
, &column
->type
)) {
1965 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1966 ds_put_format(&ctx
->output
, "%s: ", column
->name
);
1967 ovsdb_datum_to_string(datum
, &column
->type
, &ctx
->output
);
1968 ds_put_char(&ctx
->output
, '\n');
1971 cmd_show_weak_ref(ctx
, show
, row
, level
);
1972 sset_find_and_delete_assert(shown
, show
->table
->name
);
1976 cmd_show(struct ctl_context
*ctx
)
1978 const struct ovsdb_idl_row
*row
;
1979 struct sset shown
= SSET_INITIALIZER(&shown
);
1981 for (row
= ovsdb_idl_first_row(ctx
->idl
, cmd_show_tables
[0].table
);
1982 row
; row
= ovsdb_idl_next_row(row
)) {
1983 cmd_show_row(ctx
, row
, 0, &shown
);
1986 ovs_assert(sset_is_empty(&shown
));
1987 sset_destroy(&shown
);
1991 /* Given pointer to dynamic array 'options_p', array's current size
1992 * 'allocated_options_p' and number of added options 'n_options_p',
1993 * adds all command options to the array. Enlarges the array if
1996 ctl_add_cmd_options(struct option
**options_p
, size_t *n_options_p
,
1997 size_t *allocated_options_p
, int opt_val
)
2000 const struct shash_node
*node
;
2001 size_t n_existing_options
= *n_options_p
;
2003 SHASH_FOR_EACH (node
, &all_commands
) {
2004 const struct ctl_command_syntax
*p
= node
->data
;
2006 if (p
->options
[0]) {
2007 char *save_ptr
= NULL
;
2011 s
= xstrdup(p
->options
);
2012 for (name
= strtok_r(s
, ",", &save_ptr
); name
!= NULL
;
2013 name
= strtok_r(NULL
, ",", &save_ptr
)) {
2014 ovs_assert(name
[0] == '-' && name
[1] == '-' && name
[2]);
2017 size_t n
= strcspn(name
, "=?");
2018 int has_arg
= (name
[n
] == '\0' ? no_argument
2019 : name
[n
] == '=' ? required_argument
2020 : optional_argument
);
2023 o
= find_option(name
, *options_p
, *n_options_p
);
2025 ovs_assert(o
- *options_p
>= n_existing_options
);
2026 ovs_assert(o
->has_arg
== has_arg
);
2028 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
2029 o
->name
= xstrdup(name
);
2030 o
->has_arg
= has_arg
;
2039 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
2040 memset(o
, 0, sizeof *o
);
2043 /* Parses command-line input for commands. */
2044 struct ctl_command
*
2045 ctl_parse_commands(int argc
, char *argv
[], struct shash
*local_options
,
2046 size_t *n_commandsp
)
2048 struct ctl_command
*commands
;
2049 size_t n_commands
, allocated_commands
;
2053 n_commands
= allocated_commands
= 0;
2055 for (start
= i
= 0; i
<= argc
; i
++) {
2056 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
2058 if (n_commands
>= allocated_commands
) {
2059 struct ctl_command
*c
;
2061 commands
= x2nrealloc(commands
, &allocated_commands
,
2063 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2064 shash_moved(&c
->options
);
2067 parse_command(i
- start
, &argv
[start
], local_options
,
2068 &commands
[n_commands
++]);
2069 } else if (!shash_is_empty(local_options
)) {
2070 ctl_fatal("missing command name (use --help for help)");
2076 ctl_fatal("missing command name (use --help for help)");
2078 *n_commandsp
= n_commands
;
2082 /* Prints all registered commands. */
2084 ctl_print_commands(void)
2086 const struct shash_node
*node
;
2088 SHASH_FOR_EACH (node
, &all_commands
) {
2089 const struct ctl_command_syntax
*p
= node
->data
;
2090 char *options
= xstrdup(p
->options
);
2091 char *options_begin
= options
;
2094 for (item
= strsep(&options
, ","); item
!= NULL
;
2095 item
= strsep(&options
, ",")) {
2096 if (item
[0] != '\0') {
2097 printf("[%s] ", item
);
2100 printf(",%s,", p
->name
);
2101 print_command_arguments(p
);
2104 free(options_begin
);
2110 /* Given array of options 'options', prints them. */
2112 ctl_print_options(const struct option
*options
)
2114 for (; options
->name
; options
++) {
2115 const struct option
*o
= options
;
2117 printf("--%s%s\n", o
->name
, o
->has_arg
? "=ARG" : "");
2118 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
2119 printf("-%c%s\n", o
->val
, o
->has_arg
? " ARG" : "");
2126 /* Returns the default local database path. */
2128 ctl_default_db(void)
2132 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
2137 /* Returns true if it looks like this set of arguments might modify the
2138 * database, otherwise false. (Not very smart, so it's prone to false
2141 ctl_might_write_to_db(const struct ctl_command
*commands
, size_t n
)
2143 for (size_t i
= 0; i
< n
; i
++) {
2144 if (commands
[i
].syntax
->mode
== RW
) {
2152 ctl_fatal(const char *format
, ...)
2157 va_start(args
, format
);
2158 message
= xvasprintf(format
, args
);
2161 vlog_set_levels(&this_module
, VLF_CONSOLE
, VLL_OFF
);
2162 VLOG_ERR("%s", message
);
2163 ovs_error(0, "%s", message
);
2164 ctl_exit(EXIT_FAILURE
);
2167 /* Frees the current transaction and the underlying IDL and then calls
2170 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2171 * for a clean memory leak report from valgrind in the normal case. That makes
2172 * it easier to notice real memory leaks. */
2174 ctl_exit(int status
)
2176 if (ctl_exit_func
) {
2177 ctl_exit_func(status
);
2182 /* Comman database commands to be registered. */
2183 static const struct ctl_command_syntax db_ctl_commands
[] = {
2184 {"comment", 0, INT_MAX
, "[ARG]...", NULL
, NULL
, NULL
, "", RO
},
2185 {"get", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get
, cmd_get
,
2186 NULL
, "--if-exists,--id=", RO
},
2187 {"list", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_list
, cmd_list
, NULL
,
2188 "--if-exists,--columns=", RO
},
2189 {"find", 1, INT_MAX
, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find
,
2190 cmd_find
, NULL
, "--columns=", RO
},
2191 {"set", 3, INT_MAX
, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set
,
2192 cmd_set
, NULL
, "--if-exists", RW
},
2193 {"add", 4, INT_MAX
, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add
,
2194 cmd_add
, NULL
, "--if-exists", RW
},
2195 {"remove", 4, INT_MAX
, "TABLE RECORD COLUMN KEY|VALUE|KEY=VALUE...",
2196 pre_cmd_remove
, cmd_remove
, NULL
, "--if-exists", RW
},
2197 {"clear", 3, INT_MAX
, "TABLE RECORD COLUMN...", pre_cmd_clear
, cmd_clear
,
2198 NULL
, "--if-exists", RW
},
2199 {"create", 2, INT_MAX
, "TABLE COLUMN[:KEY]=VALUE...", pre_create
,
2200 cmd_create
, post_create
, "--id=", RW
},
2201 {"destroy", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_destroy
, cmd_destroy
,
2202 NULL
, "--if-exists,--all", RW
},
2203 {"wait-until", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]=VALUE]...",
2204 pre_cmd_wait_until
, cmd_wait_until
, NULL
, "", RO
},
2205 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2209 ctl_register_command(const struct ctl_command_syntax
*command
)
2211 shash_add_assert(&all_commands
, command
->name
, command
);
2214 /* Registers commands represented by 'struct ctl_command_syntax's to
2215 * 'all_commands'. The last element of 'commands' must be an all-NULL
2218 ctl_register_commands(const struct ctl_command_syntax
*commands
)
2220 const struct ctl_command_syntax
*p
;
2222 for (p
= commands
; p
->name
; p
++) {
2223 ctl_register_command(p
);
2227 /* Registers the 'db_ctl_commands' to 'all_commands'. */
2229 ctl_init__(const struct ovsdb_idl_class
*idl_class_
,
2230 const struct ctl_table_class
*ctl_classes_
,
2231 const struct cmd_show_table cmd_show_tables_
[],
2232 void (*ctl_exit_func_
)(int status
))
2234 idl_class
= idl_class_
;
2235 idl_classes
= idl_class_
->tables
;
2236 ctl_classes
= ctl_classes_
;
2237 n_classes
= idl_class
->n_tables
;
2238 ctl_exit_func
= ctl_exit_func_
;
2239 ctl_register_commands(db_ctl_commands
);
2241 cmd_show_tables
= cmd_show_tables_
;
2242 if (cmd_show_tables
) {
2243 static const struct ctl_command_syntax show
=
2244 {"show", 0, 0, "", pre_cmd_show
, cmd_show
, NULL
, "", RO
};
2245 ctl_register_command(&show
);
2249 /* Returns the text for the database commands usage. */
2251 ctl_get_db_cmd_usage(void)
2253 return "Database commands:\n\
2254 list TBL [REC] list RECord (or all records) in TBL\n\
2255 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
2256 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
2257 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
2258 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
2259 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
2260 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
2261 create TBL COL[:KEY]=VALUE create and initialize new record\n\
2262 destroy TBL REC delete RECord from TBL\n\
2263 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
2264 Potentially unsafe database commands require --force option.\n";
2268 ctl_list_db_tables_usage(void)
2270 static struct ds s
= DS_EMPTY_INITIALIZER
;
2275 ds_put_cstr(&s
, "Database commands may reference a row in each table in the following ways:\n");
2276 for (int i
= 0; i
< n_classes
; i
++) {
2277 struct svec options
= SVEC_EMPTY_INITIALIZER
;
2279 svec_add(&options
, "by UUID");
2280 if (idl_classes
[i
].is_singleton
) {
2281 svec_add(&options
, "as \".\"");
2284 for (int j
= 0; j
< ARRAY_SIZE(ctl_classes
[i
].row_ids
); j
++) {
2285 const struct ctl_row_id
*id
= &ctl_classes
[i
].row_ids
[j
];
2286 if (!id
->name_column
) {
2290 struct ds o
= DS_EMPTY_INITIALIZER
;
2291 if (id
->uuid_column
) {
2292 ds_put_format(&o
, "via \"%s\"", id
->uuid_column
->name
);
2293 const struct ovsdb_idl_table_class
*referrer
2294 = ovsdb_idl_table_class_from_column(idl_class
,
2296 if (referrer
!= &idl_classes
[i
]) {
2297 ds_put_format(&o
, " of %s", referrer
->name
);
2300 ds_put_format(&o
, " with matching \"%s:%s\"",
2301 id
->name_column
->name
, id
->key
);
2303 ds_put_format(&o
, " with matching \"%s\"", id
->name_column
->name
);
2305 } else if (id
->key
) {
2306 ds_put_format(&o
, "by \"%s:%s\"", id
->name_column
->name
, id
->key
);
2308 ds_put_format(&o
, "by \"%s\"", id
->name_column
->name
);
2310 svec_add_nocopy(&options
, ds_steal_cstr(&o
));
2313 ds_put_format(&s
, " %s:", idl_classes
[i
].name
);
2314 for (int j
= 0; j
< options
.n
; j
++) {
2315 ds_put_format(&s
, "\n %s", options
.names
[j
]);
2317 ds_put_char(&s
, '\n');
2318 svec_destroy(&options
);
2324 /* Initializes 'ctx' from 'command'. */
2326 ctl_context_init_command(struct ctl_context
*ctx
,
2327 struct ctl_command
*command
)
2329 ctx
->argc
= command
->argc
;
2330 ctx
->argv
= command
->argv
;
2331 ctx
->options
= command
->options
;
2333 ds_swap(&ctx
->output
, &command
->output
);
2334 ctx
->table
= command
->table
;
2335 ctx
->try_again
= false;
2338 /* Initializes the entire 'ctx'. */
2340 ctl_context_init(struct ctl_context
*ctx
, struct ctl_command
*command
,
2341 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2342 struct ovsdb_symbol_table
*symtab
,
2343 void (*invalidate_cache_cb
)(struct ctl_context
*))
2346 ctl_context_init_command(ctx
, command
);
2350 ctx
->symtab
= symtab
;
2351 ctx
->invalidate_cache_cb
= invalidate_cache_cb
;
2354 /* Completes processing of 'command' within 'ctx'. */
2356 ctl_context_done_command(struct ctl_context
*ctx
,
2357 struct ctl_command
*command
)
2359 ds_swap(&ctx
->output
, &command
->output
);
2360 command
->table
= ctx
->table
;
2363 /* Finishes up with 'ctx'.
2365 * If command is nonnull, first calls ctl_context_done_command() to complete
2366 * processing that command within 'ctx'. */
2368 ctl_context_done(struct ctl_context
*ctx
,
2369 struct ctl_command
*command
)
2372 ctl_context_done_command(ctx
, command
);
2374 invalidate_cache(ctx
);
2377 void ctl_set_column(const char *table_name
,
2378 const struct ovsdb_idl_row
*row
, const char *arg
,
2379 struct ovsdb_symbol_table
*symtab
)
2381 const struct ovsdb_idl_table_class
*table
;
2383 die_if_error(get_table(table_name
, &table
));
2384 die_if_error(set_column(table
, row
, arg
, symtab
));