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"
42 VLOG_DEFINE_THIS_MODULE(db_ctl_base
);
44 /* This array defines the 'show' command output format. User can check the
45 * definition in utilities/ovs-vsctl.c as reference.
47 * Particularly, if an element in 'columns[]' represents a reference to
48 * another table, the referred table must also be defined as an entry in
49 * in 'cmd_show_tables[]'.
51 * The definition must end with an all-NULL entry. It is initalized once
52 * when ctl_init() is called.
55 static const struct cmd_show_table
*cmd_show_tables
;
57 /* ctl_exit() is called by ctl_fatal(). User can optionally supply an exit
58 * function ctl_exit_func() via ctl_init. If supplied, this function will
59 * be called by ctl_exit()
61 static void (*ctl_exit_func
)(int status
) = NULL
;
62 OVS_NO_RETURN
static void ctl_exit(int status
);
64 /* Two arrays with 'n_classes' elements, which represent the tables in this
65 * database and how the user can refer to their rows. */
66 static const struct ctl_table_class
*ctl_classes
;
67 static const struct ovsdb_idl_table_class
*idl_classes
;
68 static size_t n_classes
;
70 static struct shash all_commands
= SHASH_INITIALIZER(&all_commands
);
71 static const struct ovsdb_idl_table_class
*get_table(const char *table_name
);
72 static void set_column(const struct ovsdb_idl_table_class
*,
73 const struct ovsdb_idl_row
*, const char *,
74 struct ovsdb_symbol_table
*);
77 static struct option
*
78 find_option(const char *name
, struct option
*options
, size_t n_options
)
82 for (i
= 0; i
< n_options
; i
++) {
83 if (!strcmp(options
[i
].name
, name
)) {
90 static struct option
*
91 add_option(struct option
**optionsp
, size_t *n_optionsp
,
92 size_t *allocated_optionsp
)
94 if (*n_optionsp
>= *allocated_optionsp
) {
95 *optionsp
= x2nrealloc(*optionsp
, allocated_optionsp
,
98 return &(*optionsp
)[(*n_optionsp
)++];
101 /* Converts the command arguments into format that can be parsed by
102 * bash completion script.
104 * Therein, arguments will be attached with following prefixes:
106 * !argument :: The argument is required
107 * ?argument :: The argument is optional
108 * *argument :: The argument may appear any number (0 or more) times
109 * +argument :: The argument may appear one or more times
113 print_command_arguments(const struct ctl_command_syntax
*command
)
116 * The argument string is parsed in reverse. We use a stack 'oew_stack' to
117 * keep track of nested optionals. Whenever a ']' is encountered, we push
118 * a bit to 'oew_stack'. The bit is set to 1 if the ']' is not nested.
119 * Subsequently, we pop an entry everytime '[' is met.
121 * We use 'whole_word_is_optional' value to decide whether or not a ! or +
122 * should be added on encountering a space: if the optional surrounds the
123 * whole word then it shouldn't be, but if it is only a part of the word
124 * (i.e. [key=]value), it should be.
126 uint32_t oew_stack
= 0;
128 const char *arguments
= command
->arguments
;
129 int length
= strlen(arguments
);
134 /* Output buffer, written backward from end. */
135 char *output
= xmalloc(2 * length
);
136 char *outp
= output
+ 2 * length
;
139 bool in_repeated
= false;
140 bool whole_word_is_optional
= false;
142 for (const char *inp
= arguments
+ length
; inp
> arguments
; ) {
146 if (inp
[1] == '\0' || inp
[1] == ' ' || inp
[1] == '.') {
151 /* Checks if the whole word is optional, and sets the
152 * 'whole_word_is_optional' accordingly. */
153 if ((inp
== arguments
|| inp
[-1] == ' ') && oew_stack
& 1) {
154 *--outp
= in_repeated
? '*' : '?';
155 whole_word_is_optional
= true;
158 whole_word_is_optional
= false;
163 if (!whole_word_is_optional
) {
164 *--outp
= in_repeated
? '+' : '!';
168 whole_word_is_optional
= false;
178 if (arguments
[0] != '[' && outp
!= output
+ 2 * length
- 1) {
179 *--outp
= in_repeated
? '+' : '!';
186 die_if_error(char *error
)
189 ctl_fatal("%s", error
);
194 to_lower_and_underscores(unsigned c
)
196 return c
== '-' ? '_' : tolower(c
);
199 /* Returns a score representing how well 's' matches 'name'. Higher return
200 * values indicate a better match. The order of the arguments is important:
201 * 'name' is the name of an entity such as a table or a column, and 's' is user
204 score_partial_match(const char *name
, const char *s
)
208 if (!strcmp(name
, s
)) {
211 for (score
= 0; ; score
++, name
++, s
++) {
212 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
214 } else if (*name
== '\0') {
218 return *s
== '\0' ? score
: 0;
221 static struct ovsdb_symbol
*
222 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
224 struct ovsdb_symbol
*symbol
;
227 ctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
231 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
234 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
235 if (symbol
->created
) {
236 ctl_fatal("row id \"%s\" may only be specified on one --id option",
239 symbol
->created
= true;
244 record_id_equals(const union ovsdb_atom
*name
, enum ovsdb_atomic_type type
,
245 const char *record_id
)
247 if (type
== OVSDB_TYPE_STRING
) {
248 if (!strcmp(name
->string
, record_id
)) {
253 size_t len
= strlen(record_id
);
255 && uuid_from_string(&uuid
, name
->string
)
256 && !strncmp(name
->string
, record_id
, len
)) {
262 ovs_assert(type
== OVSDB_TYPE_INTEGER
);
263 return name
->integer
== strtoll(record_id
, NULL
, 10);
267 static const struct ovsdb_idl_row
*
268 get_row_by_id(struct ctl_context
*ctx
,
269 const struct ovsdb_idl_table_class
*table
,
270 const struct ctl_row_id
*id
, const char *record_id
)
273 if (!id
->name_column
) {
277 const struct ovsdb_idl_row
*referrer
= NULL
;
279 /* Figure out the 'key' and 'value' types for the column that we're going
280 * to look at. One of these ('name_type') is the type of the name we're
281 * going to compare against 'record_id'. */
282 enum ovsdb_atomic_type key
, value
, name_type
;
284 name_type
= key
= id
->name_column
->type
.key
.type
;
285 value
= OVSDB_TYPE_VOID
;
287 key
= OVSDB_TYPE_STRING
;
288 name_type
= value
= id
->name_column
->type
.value
.type
;
291 /* We only support integer and string names (so far). */
292 if (name_type
== OVSDB_TYPE_INTEGER
) {
293 if (!record_id
[0] || record_id
[strspn(record_id
, "0123456789")]) {
297 ovs_assert(name_type
== OVSDB_TYPE_STRING
);
300 const struct ovsdb_idl_class
*class = ovsdb_idl_get_class(ctx
->idl
);
301 const struct ovsdb_idl_table_class
*id_table
302 = ovsdb_idl_table_class_from_column(class, id
->name_column
);
303 for (const struct ovsdb_idl_row
*row
= ovsdb_idl_first_row(ctx
->idl
,
306 row
= ovsdb_idl_next_row(row
)) {
307 /* Pick out the name column's data. */
308 const struct ovsdb_datum
*datum
= ovsdb_idl_get(
309 row
, id
->name_column
, key
, value
);
311 /* Extract the name from the column. */
312 const union ovsdb_atom
*name
;
314 name
= datum
->n
== 1 ? &datum
->keys
[0] : NULL
;
316 const union ovsdb_atom key_atom
317 = { .string
= CONST_CAST(char *, id
->key
) };
318 unsigned int i
= ovsdb_datum_find_key(datum
, &key_atom
,
320 name
= i
== UINT_MAX
? NULL
: &datum
->values
[i
];
326 /* If the name equals 'record_id', take it. */
327 if (record_id_equals(name
, name_type
, record_id
)) {
329 ctl_fatal("multiple rows in %s match \"%s\"",
330 id_table
->name
, record_id
);
339 const struct ovsdb_idl_row
*final
= referrer
;
340 if (id
->uuid_column
) {
341 const struct ovsdb_datum
*uuid
;
343 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
344 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
345 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
347 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
,
348 &uuid
->keys
[0].uuid
);
356 const struct ovsdb_idl_row
*
357 ctl_get_row(struct ctl_context
*ctx
,
358 const struct ovsdb_idl_table_class
*table
, const char *record_id
,
361 const struct ovsdb_idl_row
*row
= NULL
;
364 if (uuid_from_string(&uuid
, record_id
)) {
365 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
, &uuid
);
368 if (!strcmp(record_id
, ".")) {
369 row
= ovsdb_idl_first_row(ctx
->idl
, table
);
370 if (row
&& ovsdb_idl_next_row(row
)) {
376 const struct ctl_table_class
*ctl_class
377 = &ctl_classes
[table
- idl_classes
];
378 for (int i
= 0; i
< ARRAY_SIZE(ctl_class
->row_ids
); i
++) {
379 row
= get_row_by_id(ctx
, table
, &ctl_class
->row_ids
[i
],
386 if (!row
&& uuid_is_partial_string(record_id
) >= 4) {
387 for (const struct ovsdb_idl_row
*r
= ovsdb_idl_first_row(ctx
->idl
,
390 r
= ovsdb_idl_next_row(r
)) {
391 if (uuid_is_partial_match(&r
->uuid
, record_id
)) {
395 ctl_fatal("%s contains 2 or more rows whose UUIDs begin "
396 "with %s: at least "UUID_FMT
" and "UUID_FMT
,
397 table
->name
, record_id
,
398 UUID_ARGS(&row
->uuid
),
399 UUID_ARGS(&r
->uuid
));
404 if (must_exist
&& !row
) {
405 ctl_fatal("no row \"%s\" in table %s", record_id
, table
->name
);
411 get_column(const struct ovsdb_idl_table_class
*table
, const char *column_name
,
412 const struct ovsdb_idl_column
**columnp
)
414 const struct ovsdb_idl_column
*best_match
= NULL
;
415 unsigned int best_score
= 0;
418 for (i
= 0; i
< table
->n_columns
; i
++) {
419 const struct ovsdb_idl_column
*column
= &table
->columns
[i
];
420 unsigned int score
= score_partial_match(column
->name
, column_name
);
421 if (score
> best_score
) {
424 } else if (score
== best_score
) {
429 *columnp
= best_match
;
432 } else if (best_score
) {
433 return xasprintf("%s contains more than one column whose name "
434 "matches \"%s\"", table
->name
, column_name
);
436 return xasprintf("%s does not contain a column whose name matches "
437 "\"%s\"", table
->name
, column_name
);
442 pre_get_column(struct ctl_context
*ctx
,
443 const struct ovsdb_idl_table_class
*table
,
444 const char *column_name
,
445 const struct ovsdb_idl_column
**columnp
)
447 die_if_error(get_column(table
, column_name
, columnp
));
448 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
451 static const struct ovsdb_idl_table_class
*
452 pre_get_table(struct ctl_context
*ctx
, const char *table_name
)
454 const struct ovsdb_idl_table_class
*table
= get_table(table_name
);
455 ovsdb_idl_add_table(ctx
->idl
, table
);
457 const struct ctl_table_class
*ctl
= &ctl_classes
[table
- idl_classes
];
458 for (int i
= 0; i
< ARRAY_SIZE(ctl
->row_ids
); i
++) {
459 const struct ctl_row_id
*id
= &ctl
->row_ids
[i
];
460 if (id
->name_column
) {
461 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
463 if (id
->uuid_column
) {
464 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
472 missing_operator_error(const char *arg
, const char **allowed_operators
,
478 ds_put_format(&s
, "%s: argument does not end in ", arg
);
479 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
480 if (n_allowed
== 2) {
481 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
482 } else if (n_allowed
> 2) {
485 for (i
= 1; i
< n_allowed
- 1; i
++) {
486 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
488 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
490 ds_put_format(&s
, " followed by a value.");
492 return ds_steal_cstr(&s
);
495 /* Breaks 'arg' apart into a number of fields in the following order:
497 * - The name of a column in 'table', stored into '*columnp'. The column
498 * name may be abbreviated.
500 * - Optionally ':' followed by a key string. The key is stored as a
501 * malloc()'d string into '*keyp', or NULL if no key is present in
504 * - If 'valuep' is nonnull, an operator followed by a value string. The
505 * allowed operators are the 'n_allowed' string in 'allowed_operators',
506 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
507 * index of the operator within 'allowed_operators' is stored into
508 * '*operatorp'. The value is stored as a malloc()'d string into
509 * '*valuep', or NULL if no value is present in 'arg'.
511 * On success, returns NULL. On failure, returned a malloc()'d string error
512 * message and stores NULL into all of the nonnull output arguments. */
513 static char * OVS_WARN_UNUSED_RESULT
514 parse_column_key_value(const char *arg
,
515 const struct ovsdb_idl_table_class
*table
,
516 const struct ovsdb_idl_column
**columnp
, char **keyp
,
518 const char **allowed_operators
, size_t n_allowed
,
525 ovs_assert(!(operatorp
&& !valuep
));
531 /* Parse column name. */
532 error
= ovsdb_token_parse(&p
, &column_name
);
536 if (column_name
[0] == '\0') {
538 error
= xasprintf("%s: missing column name", arg
);
541 error
= get_column(table
, column_name
, columnp
);
547 /* Parse key string. */
550 error
= ovsdb_token_parse(&p
, keyp
);
556 /* Parse value string. */
562 if (!allowed_operators
) {
563 static const char *equals
= "=";
564 allowed_operators
= &equals
;
570 for (i
= 0; i
< n_allowed
; i
++) {
571 const char *op
= allowed_operators
[i
];
572 size_t op_len
= strlen(op
);
574 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
580 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
587 *valuep
= xstrdup(p
+ best_len
);
590 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
611 static const struct ovsdb_idl_column
*
612 pre_parse_column_key_value(struct ctl_context
*ctx
,
614 const struct ovsdb_idl_table_class
*table
)
616 const struct ovsdb_idl_column
*column
;
621 die_if_error(ovsdb_token_parse(&p
, &column_name
));
622 if (column_name
[0] == '\0') {
623 ctl_fatal("%s: missing column name", arg
);
626 pre_get_column(ctx
, table
, column_name
, &column
);
633 check_mutable(const struct ovsdb_idl_row
*row
,
634 const struct ovsdb_idl_column
*column
)
636 if (!ovsdb_idl_is_mutable(row
, column
)) {
637 ctl_fatal("cannot modify read-only column %s in table %s",
638 column
->name
, row
->table
->class_
->name
);
643 RELOP(RELOP_EQ, "=") \
644 RELOP(RELOP_NE, "!=") \
645 RELOP(RELOP_LT, "<") \
646 RELOP(RELOP_GT, ">") \
647 RELOP(RELOP_LE, "<=") \
648 RELOP(RELOP_GE, ">=") \
649 RELOP(RELOP_SET_EQ, "{=}") \
650 RELOP(RELOP_SET_NE, "{!=}") \
651 RELOP(RELOP_SET_LT, "{<}") \
652 RELOP(RELOP_SET_GT, "{>}") \
653 RELOP(RELOP_SET_LE, "{<=}") \
654 RELOP(RELOP_SET_GE, "{>=}")
657 #define RELOP(ENUM, STRING) ENUM,
663 is_set_operator(enum relop op
)
665 return (op
== RELOP_SET_EQ
|| op
== RELOP_SET_NE
||
666 op
== RELOP_SET_LT
|| op
== RELOP_SET_GT
||
667 op
== RELOP_SET_LE
|| op
== RELOP_SET_GE
);
671 evaluate_relop(const struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
672 const struct ovsdb_type
*type
, enum relop op
)
677 return ovsdb_datum_compare_3way(a
, b
, type
) == 0;
680 return ovsdb_datum_compare_3way(a
, b
, type
) != 0;
682 return ovsdb_datum_compare_3way(a
, b
, type
) < 0;
684 return ovsdb_datum_compare_3way(a
, b
, type
) > 0;
686 return ovsdb_datum_compare_3way(a
, b
, type
) <= 0;
688 return ovsdb_datum_compare_3way(a
, b
, type
) >= 0;
691 return b
->n
> a
->n
&& ovsdb_datum_includes_all(a
, b
, type
);
693 return a
->n
> b
->n
&& ovsdb_datum_includes_all(b
, a
, type
);
695 return ovsdb_datum_includes_all(a
, b
, type
);
697 return ovsdb_datum_includes_all(b
, a
, type
);
705 is_condition_satisfied(const struct ovsdb_idl_table_class
*table
,
706 const struct ovsdb_idl_row
*row
, const char *arg
,
707 struct ovsdb_symbol_table
*symtab
)
709 static const char *operators
[] = {
710 #define RELOP(ENUM, STRING) STRING,
715 const struct ovsdb_idl_column
*column
;
716 const struct ovsdb_datum
*have_datum
;
717 char *key_string
, *value_string
;
718 struct ovsdb_type type
;
723 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
724 &operator, operators
, ARRAY_SIZE(operators
),
728 ctl_fatal("%s: missing value", arg
);
732 type
.n_max
= UINT_MAX
;
734 have_datum
= ovsdb_idl_read(row
, column
);
736 union ovsdb_atom want_key
;
737 struct ovsdb_datum b
;
740 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
741 ctl_fatal("cannot specify key to check for non-map column %s",
745 die_if_error(ovsdb_atom_from_string(&want_key
, NULL
, &column
->type
.key
,
746 key_string
, symtab
));
748 type
.key
= type
.value
;
749 type
.value
.type
= OVSDB_TYPE_VOID
;
750 die_if_error(ovsdb_datum_from_string(&b
, &type
, value_string
, symtab
));
752 idx
= ovsdb_datum_find_key(have_datum
,
753 &want_key
, column
->type
.key
.type
);
754 if (idx
== UINT_MAX
&& !is_set_operator(operator)) {
757 struct ovsdb_datum a
;
759 if (idx
!= UINT_MAX
) {
761 a
.keys
= &have_datum
->values
[idx
];
769 retval
= evaluate_relop(&a
, &b
, &type
, operator);
772 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
773 ovsdb_datum_destroy(&b
, &type
);
775 struct ovsdb_datum want_datum
;
777 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
778 value_string
, symtab
));
779 retval
= evaluate_relop(have_datum
, &want_datum
, &type
, operator);
780 ovsdb_datum_destroy(&want_datum
, &column
->type
);
790 invalidate_cache(struct ctl_context
*ctx
)
792 if (ctx
->invalidate_cache
) {
793 (ctx
->invalidate_cache
)(ctx
);
798 pre_cmd_get(struct ctl_context
*ctx
)
800 const char *id
= shash_find_data(&ctx
->options
, "--id");
801 const char *table_name
= ctx
->argv
[1];
802 const struct ovsdb_idl_table_class
*table
;
805 /* Using "get" without --id or a column name could possibly make sense.
806 * Maybe, for example, a *ctl command run wants to assert that a row
807 * exists. But it is unlikely that an interactive user would want to do
808 * that, so issue a warning if we're running on a terminal. */
809 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
810 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
811 "possibly erroneous");
814 table
= pre_get_table(ctx
, table_name
);
815 for (i
= 3; i
< ctx
->argc
; i
++) {
816 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
817 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
821 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
826 cmd_get(struct ctl_context
*ctx
)
828 const char *id
= shash_find_data(&ctx
->options
, "--id");
829 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
830 const char *table_name
= ctx
->argv
[1];
831 const char *record_id
= ctx
->argv
[2];
832 const struct ovsdb_idl_table_class
*table
;
833 const struct ovsdb_idl_row
*row
;
834 struct ds
*out
= &ctx
->output
;
837 if (id
&& !must_exist
) {
838 ctl_fatal("--if-exists and --id may not be specified together");
841 table
= get_table(table_name
);
842 row
= ctl_get_row(ctx
, table
, record_id
, must_exist
);
848 struct ovsdb_symbol
*symbol
;
851 symbol
= create_symbol(ctx
->symtab
, id
, &new);
853 ctl_fatal("row id \"%s\" specified on \"get\" command was used "
854 "before it was defined", id
);
856 symbol
->uuid
= row
->uuid
;
858 /* This symbol refers to a row that already exists, so disable warnings
859 * about it being unreferenced. */
860 symbol
->strong_ref
= true;
862 for (i
= 3; i
< ctx
->argc
; i
++) {
863 const struct ovsdb_idl_column
*column
;
864 const struct ovsdb_datum
*datum
;
867 /* Special case for obtaining the UUID of a row. We can't just do this
868 * through parse_column_key_value() below since it returns a "struct
869 * ovsdb_idl_column" and the UUID column doesn't have one. */
870 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
871 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
872 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
876 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
877 &column
, &key_string
,
878 NULL
, NULL
, 0, NULL
));
880 ovsdb_idl_txn_verify(row
, column
);
881 datum
= ovsdb_idl_read(row
, column
);
883 union ovsdb_atom key
;
886 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
887 ctl_fatal("cannot specify key to get for non-map column %s",
891 die_if_error(ovsdb_atom_from_string(&key
, NULL
,
893 key_string
, ctx
->symtab
));
895 idx
= ovsdb_datum_find_key(datum
, &key
,
896 column
->type
.key
.type
);
897 if (idx
== UINT_MAX
) {
899 ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
900 key_string
, table
->name
, record_id
,
904 ovsdb_atom_to_string(&datum
->values
[idx
],
905 column
->type
.value
.type
, out
);
907 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
909 ovsdb_datum_to_string(datum
, &column
->type
, out
);
911 ds_put_char(out
, '\n');
918 parse_column_names(const char *column_names
,
919 const struct ovsdb_idl_table_class
*table
,
920 const struct ovsdb_idl_column
***columnsp
,
923 const struct ovsdb_idl_column
**columns
;
929 n_columns
= table
->n_columns
+ 1;
930 columns
= xmalloc(n_columns
* sizeof *columns
);
932 for (i
= 0; i
< table
->n_columns
; i
++) {
933 columns
[i
+ 1] = &table
->columns
[i
];
936 char *s
= xstrdup(column_names
);
937 size_t allocated_columns
;
938 char *save_ptr
= NULL
;
942 allocated_columns
= n_columns
= 0;
943 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
944 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
945 const struct ovsdb_idl_column
*column
;
947 if (!strcasecmp(column_name
, "_uuid")) {
950 die_if_error(get_column(table
, column_name
, &column
));
952 if (n_columns
>= allocated_columns
) {
953 columns
= x2nrealloc(columns
, &allocated_columns
,
956 columns
[n_columns
++] = column
;
961 ctl_fatal("must specify at least one column name");
965 *n_columnsp
= n_columns
;
969 pre_list_columns(struct ctl_context
*ctx
,
970 const struct ovsdb_idl_table_class
*table
,
971 const char *column_names
)
973 const struct ovsdb_idl_column
**columns
;
977 parse_column_names(column_names
, table
, &columns
, &n_columns
);
978 for (i
= 0; i
< n_columns
; i
++) {
980 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
987 pre_cmd_list(struct ctl_context
*ctx
)
989 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
990 const char *table_name
= ctx
->argv
[1];
991 const struct ovsdb_idl_table_class
*table
;
993 table
= pre_get_table(ctx
, table_name
);
994 pre_list_columns(ctx
, table
, column_names
);
997 static struct table
*
998 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
1003 out
= xmalloc(sizeof *out
);
1006 for (i
= 0; i
< n_columns
; i
++) {
1007 const struct ovsdb_idl_column
*column
= columns
[i
];
1008 const char *column_name
= column
? column
->name
: "_uuid";
1010 table_add_column(out
, "%s", column_name
);
1017 list_record(const struct ovsdb_idl_row
*row
,
1018 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
1028 for (i
= 0; i
< n_columns
; i
++) {
1029 const struct ovsdb_idl_column
*column
= columns
[i
];
1030 struct cell
*cell
= table_add_cell(out
);
1033 struct ovsdb_datum datum
;
1034 union ovsdb_atom atom
;
1036 atom
.uuid
= row
->uuid
;
1039 datum
.values
= NULL
;
1042 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
1043 cell
->type
= &ovsdb_type_uuid
;
1045 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
1047 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
1048 cell
->type
= &column
->type
;
1054 cmd_list(struct ctl_context
*ctx
)
1056 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1057 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1058 const struct ovsdb_idl_column
**columns
;
1059 const char *table_name
= ctx
->argv
[1];
1060 const struct ovsdb_idl_table_class
*table
;
1065 table
= get_table(table_name
);
1066 parse_column_names(column_names
, table
, &columns
, &n_columns
);
1067 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1068 if (ctx
->argc
> 2) {
1069 for (i
= 2; i
< ctx
->argc
; i
++) {
1070 list_record(ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
),
1071 columns
, n_columns
, out
);
1074 const struct ovsdb_idl_row
*row
;
1076 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
!= NULL
;
1077 row
= ovsdb_idl_next_row(row
)) {
1078 list_record(row
, columns
, n_columns
, out
);
1084 /* Finds and returns the "struct ovsdb_idl_table_class *" with 'table_name' by
1085 * searching the tables in these schema. */
1086 static const struct ovsdb_idl_table_class
*
1087 get_table(const char *table_name
)
1089 const struct ovsdb_idl_table_class
*best_match
= NULL
;
1090 unsigned int best_score
= 0;
1092 for (const struct ovsdb_idl_table_class
*table
= idl_classes
;
1093 table
< &idl_classes
[n_classes
]; table
++) {
1094 unsigned int score
= score_partial_match(table
->name
, table_name
);
1095 if (score
> best_score
) {
1098 } else if (score
== best_score
) {
1104 } else if (best_score
) {
1105 ctl_fatal("multiple table names match \"%s\"", table_name
);
1107 ctl_fatal("unknown table \"%s\"", table_name
);
1113 pre_cmd_find(struct ctl_context
*ctx
)
1115 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1116 const char *table_name
= ctx
->argv
[1];
1117 const struct ovsdb_idl_table_class
*table
;
1120 table
= pre_get_table(ctx
, table_name
);
1121 pre_list_columns(ctx
, table
, column_names
);
1122 for (i
= 2; i
< ctx
->argc
; i
++) {
1123 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1128 cmd_find(struct ctl_context
*ctx
)
1130 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1131 const struct ovsdb_idl_column
**columns
;
1132 const char *table_name
= ctx
->argv
[1];
1133 const struct ovsdb_idl_table_class
*table
;
1134 const struct ovsdb_idl_row
*row
;
1138 table
= get_table(table_name
);
1139 parse_column_names(column_names
, table
, &columns
, &n_columns
);
1140 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1141 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
;
1142 row
= ovsdb_idl_next_row(row
)) {
1145 for (i
= 2; i
< ctx
->argc
; i
++) {
1146 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
],
1151 list_record(row
, columns
, n_columns
, out
);
1158 /* Sets the column of 'row' in 'table'. */
1160 set_column(const struct ovsdb_idl_table_class
*table
,
1161 const struct ovsdb_idl_row
*row
, const char *arg
,
1162 struct ovsdb_symbol_table
*symtab
)
1164 const struct ovsdb_idl_column
*column
;
1165 char *key_string
, *value_string
;
1168 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
1169 NULL
, NULL
, 0, &value_string
);
1170 die_if_error(error
);
1171 if (!value_string
) {
1172 ctl_fatal("%s: missing value", arg
);
1174 check_mutable(row
, column
);
1177 union ovsdb_atom key
, value
;
1178 struct ovsdb_datum datum
;
1180 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
1181 ctl_fatal("cannot specify key to set for non-map column %s",
1185 die_if_error(ovsdb_atom_from_string(&key
, NULL
, &column
->type
.key
,
1186 key_string
, symtab
));
1187 die_if_error(ovsdb_atom_from_string(&value
, NULL
, &column
->type
.value
,
1188 value_string
, symtab
));
1190 ovsdb_datum_init_empty(&datum
);
1191 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
, NULL
);
1193 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1194 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
1196 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
1197 &column
->type
, false);
1198 ovsdb_idl_txn_verify(row
, column
);
1199 ovsdb_idl_txn_write(row
, column
, &datum
);
1201 struct ovsdb_datum datum
;
1203 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
1204 value_string
, symtab
));
1205 ovsdb_idl_txn_write(row
, column
, &datum
);
1213 pre_cmd_set(struct ctl_context
*ctx
)
1215 const char *table_name
= ctx
->argv
[1];
1216 const struct ovsdb_idl_table_class
*table
;
1219 table
= pre_get_table(ctx
, table_name
);
1220 for (i
= 3; i
< ctx
->argc
; i
++) {
1221 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1226 cmd_set(struct ctl_context
*ctx
)
1228 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1229 const char *table_name
= ctx
->argv
[1];
1230 const char *record_id
= ctx
->argv
[2];
1231 const struct ovsdb_idl_table_class
*table
;
1232 const struct ovsdb_idl_row
*row
;
1235 table
= get_table(table_name
);
1236 row
= ctl_get_row(ctx
, table
, record_id
, must_exist
);
1241 for (i
= 3; i
< ctx
->argc
; i
++) {
1242 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
1245 invalidate_cache(ctx
);
1249 pre_cmd_add(struct ctl_context
*ctx
)
1251 const char *table_name
= ctx
->argv
[1];
1252 const char *column_name
= ctx
->argv
[3];
1253 const struct ovsdb_idl_table_class
*table
;
1254 const struct ovsdb_idl_column
*column
;
1256 table
= pre_get_table(ctx
, table_name
);
1257 pre_get_column(ctx
, table
, column_name
, &column
);
1261 cmd_add(struct ctl_context
*ctx
)
1263 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1264 const char *table_name
= ctx
->argv
[1];
1265 const char *record_id
= ctx
->argv
[2];
1266 const char *column_name
= ctx
->argv
[3];
1267 const struct ovsdb_idl_table_class
*table
;
1268 const struct ovsdb_idl_column
*column
;
1269 const struct ovsdb_idl_row
*row
;
1270 const struct ovsdb_type
*type
;
1271 struct ovsdb_datum old
;
1274 table
= get_table(table_name
);
1275 die_if_error(get_column(table
, column_name
, &column
));
1276 row
= ctl_get_row(ctx
, table
, record_id
, must_exist
);
1280 check_mutable(row
, column
);
1282 type
= &column
->type
;
1283 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1284 for (i
= 4; i
< ctx
->argc
; i
++) {
1285 struct ovsdb_type add_type
;
1286 struct ovsdb_datum add
;
1290 add_type
.n_max
= UINT_MAX
;
1291 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
1293 ovsdb_datum_union(&old
, &add
, type
, false);
1294 ovsdb_datum_destroy(&add
, type
);
1296 if (old
.n
> type
->n_max
) {
1297 ctl_fatal("\"add\" operation would put %u %s in column %s of "
1298 "table %s but the maximum number is %u",
1300 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1301 column
->name
, table
->name
, type
->n_max
);
1303 ovsdb_idl_txn_verify(row
, column
);
1304 ovsdb_idl_txn_write(row
, column
, &old
);
1306 invalidate_cache(ctx
);
1310 pre_cmd_remove(struct ctl_context
*ctx
)
1312 const char *table_name
= ctx
->argv
[1];
1313 const char *column_name
= ctx
->argv
[3];
1314 const struct ovsdb_idl_table_class
*table
;
1315 const struct ovsdb_idl_column
*column
;
1317 table
= pre_get_table(ctx
, table_name
);
1318 pre_get_column(ctx
, table
, column_name
, &column
);
1322 cmd_remove(struct ctl_context
*ctx
)
1324 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1325 const char *table_name
= ctx
->argv
[1];
1326 const char *record_id
= ctx
->argv
[2];
1327 const char *column_name
= ctx
->argv
[3];
1328 const struct ovsdb_idl_table_class
*table
;
1329 const struct ovsdb_idl_column
*column
;
1330 const struct ovsdb_idl_row
*row
;
1331 const struct ovsdb_type
*type
;
1332 struct ovsdb_datum old
;
1335 table
= get_table(table_name
);
1336 die_if_error(get_column(table
, column_name
, &column
));
1337 row
= ctl_get_row(ctx
, table
, record_id
, must_exist
);
1341 check_mutable(row
, column
);
1343 type
= &column
->type
;
1344 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1345 for (i
= 4; i
< ctx
->argc
; i
++) {
1346 struct ovsdb_type rm_type
;
1347 struct ovsdb_datum rm
;
1352 rm_type
.n_max
= UINT_MAX
;
1353 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
1354 ctx
->argv
[i
], ctx
->symtab
);
1357 if (ovsdb_type_is_map(&rm_type
)) {
1358 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
1360 die_if_error(ovsdb_datum_from_string(
1361 &rm
, &rm_type
, ctx
->argv
[i
], ctx
->symtab
));
1363 ctl_fatal("%s", error
);
1366 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
1367 ovsdb_datum_destroy(&rm
, &rm_type
);
1369 if (old
.n
< type
->n_min
) {
1370 ctl_fatal("\"remove\" operation would put %u %s in column %s of "
1371 "table %s but the minimum number is %u",
1373 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1374 column
->name
, table
->name
, type
->n_min
);
1376 ovsdb_idl_txn_verify(row
, column
);
1377 ovsdb_idl_txn_write(row
, column
, &old
);
1379 invalidate_cache(ctx
);
1383 pre_cmd_clear(struct ctl_context
*ctx
)
1385 const char *table_name
= ctx
->argv
[1];
1386 const struct ovsdb_idl_table_class
*table
;
1389 table
= pre_get_table(ctx
, table_name
);
1390 for (i
= 3; i
< ctx
->argc
; i
++) {
1391 const struct ovsdb_idl_column
*column
;
1393 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
1398 cmd_clear(struct ctl_context
*ctx
)
1400 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1401 const char *table_name
= ctx
->argv
[1];
1402 const char *record_id
= ctx
->argv
[2];
1403 const struct ovsdb_idl_table_class
*table
;
1404 const struct ovsdb_idl_row
*row
;
1407 table
= get_table(table_name
);
1408 row
= ctl_get_row(ctx
, table
, record_id
, must_exist
);
1413 for (i
= 3; i
< ctx
->argc
; i
++) {
1414 const struct ovsdb_idl_column
*column
;
1415 const struct ovsdb_type
*type
;
1416 struct ovsdb_datum datum
;
1418 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
1419 check_mutable(row
, column
);
1421 type
= &column
->type
;
1422 if (type
->n_min
> 0) {
1423 ctl_fatal("\"clear\" operation cannot be applied to column %s "
1424 "of table %s, which is not allowed to be empty",
1425 column
->name
, table
->name
);
1428 ovsdb_datum_init_empty(&datum
);
1429 ovsdb_idl_txn_write(row
, column
, &datum
);
1432 invalidate_cache(ctx
);
1436 pre_create(struct ctl_context
*ctx
)
1438 const char *id
= shash_find_data(&ctx
->options
, "--id");
1439 const char *table_name
= ctx
->argv
[1];
1440 const struct ovsdb_idl_table_class
*table
;
1442 table
= get_table(table_name
);
1443 if (!id
&& !table
->is_root
) {
1444 VLOG_WARN("applying \"create\" command to table %s without --id "
1445 "option will have no effect", table
->name
);
1450 cmd_create(struct ctl_context
*ctx
)
1452 const char *id
= shash_find_data(&ctx
->options
, "--id");
1453 const char *table_name
= ctx
->argv
[1];
1454 const struct ovsdb_idl_table_class
*table
= get_table(table_name
);
1455 const struct ovsdb_idl_row
*row
;
1456 const struct uuid
*uuid
;
1460 struct ovsdb_symbol
*symbol
= create_symbol(ctx
->symtab
, id
, NULL
);
1461 if (table
->is_root
) {
1462 /* This table is in the root set, meaning that rows created in it
1463 * won't disappear even if they are unreferenced, so disable
1464 * warnings about that by pretending that there is a reference. */
1465 symbol
->strong_ref
= true;
1467 uuid
= &symbol
->uuid
;
1472 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
, uuid
);
1473 for (i
= 2; i
< ctx
->argc
; i
++) {
1474 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
1476 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1479 /* This function may be used as the 'postprocess' function for commands that
1480 * insert new rows into the database. It expects that the command's 'run'
1481 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
1482 * sole output. It replaces that output by the row's permanent UUID assigned
1483 * by the database server and appends a new-line.
1485 * Currently we use this only for "create", because the higher-level commands
1486 * are supposed to be independent of the actual structure of the vswitch
1489 post_create(struct ctl_context
*ctx
)
1491 const struct uuid
*real
;
1494 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
1497 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
1499 ds_clear(&ctx
->output
);
1500 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
1502 ds_put_char(&ctx
->output
, '\n');
1506 pre_cmd_destroy(struct ctl_context
*ctx
)
1508 const char *table_name
= ctx
->argv
[1];
1510 pre_get_table(ctx
, table_name
);
1514 cmd_destroy(struct ctl_context
*ctx
)
1516 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1517 bool delete_all
= shash_find(&ctx
->options
, "--all");
1518 const char *table_name
= ctx
->argv
[1];
1519 const struct ovsdb_idl_table_class
*table
;
1522 table
= get_table(table_name
);
1524 if (delete_all
&& ctx
->argc
> 2) {
1525 ctl_fatal("--all and records argument should not be specified together");
1528 if (delete_all
&& !must_exist
) {
1529 ctl_fatal("--all and --if-exists should not be specified together");
1533 const struct ovsdb_idl_row
*row
;
1534 const struct ovsdb_idl_row
*next_row
;
1536 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
);
1538 next_row
= ovsdb_idl_next_row(row
);
1539 ovsdb_idl_txn_delete(row
);
1543 for (i
= 2; i
< ctx
->argc
; i
++) {
1544 const struct ovsdb_idl_row
*row
;
1546 row
= ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
);
1548 ovsdb_idl_txn_delete(row
);
1552 invalidate_cache(ctx
);
1556 pre_cmd_wait_until(struct ctl_context
*ctx
)
1558 const char *table_name
= ctx
->argv
[1];
1559 const struct ovsdb_idl_table_class
*table
;
1562 table
= pre_get_table(ctx
, table_name
);
1564 for (i
= 3; i
< ctx
->argc
; i
++) {
1565 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1570 cmd_wait_until(struct ctl_context
*ctx
)
1572 const char *table_name
= ctx
->argv
[1];
1573 const char *record_id
= ctx
->argv
[2];
1574 const struct ovsdb_idl_table_class
*table
;
1575 const struct ovsdb_idl_row
*row
;
1578 table
= get_table(table_name
);
1580 row
= ctl_get_row(ctx
, table
, record_id
, false);
1582 ctx
->try_again
= true;
1586 for (i
= 3; i
< ctx
->argc
; i
++) {
1587 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
1588 ctx
->try_again
= true;
1594 /* Parses one command. */
1596 parse_command(int argc
, char *argv
[], struct shash
*local_options
,
1597 struct ctl_command
*command
)
1599 const struct ctl_command_syntax
*p
;
1600 struct shash_node
*node
;
1604 shash_init(&command
->options
);
1605 shash_swap(local_options
, &command
->options
);
1606 for (i
= 0; i
< argc
; i
++) {
1607 const char *option
= argv
[i
];
1611 if (option
[0] != '-') {
1615 equals
= strchr(option
, '=');
1617 key
= xmemdup0(option
, equals
- option
);
1618 value
= xstrdup(equals
+ 1);
1620 key
= xstrdup(option
);
1624 if (shash_find(&command
->options
, key
)) {
1625 ctl_fatal("'%s' option specified multiple times", argv
[i
]);
1627 shash_add_nocopy(&command
->options
, key
, value
);
1630 ctl_fatal("missing command name (use --help for help)");
1633 p
= shash_find_data(&all_commands
, argv
[i
]);
1635 ctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
1638 SHASH_FOR_EACH (node
, &command
->options
) {
1639 const char *s
= strstr(p
->options
, node
->name
);
1640 int end
= s
? s
[strlen(node
->name
)] : EOF
;
1642 if (!strchr("=,? ", end
)) {
1643 ctl_fatal("'%s' command has no '%s' option",
1644 argv
[i
], node
->name
);
1646 if (end
!= '?' && (end
== '=') != (node
->data
!= NULL
)) {
1648 ctl_fatal("missing argument to '%s' option on '%s' "
1649 "command", node
->name
, argv
[i
]);
1651 ctl_fatal("'%s' option on '%s' does not accept an "
1652 "argument", node
->name
, argv
[i
]);
1657 n_arg
= argc
- i
- 1;
1658 if (n_arg
< p
->min_args
) {
1659 ctl_fatal("'%s' command requires at least %d arguments",
1660 p
->name
, p
->min_args
);
1661 } else if (n_arg
> p
->max_args
) {
1664 for (j
= i
+ 1; j
< argc
; j
++) {
1665 if (argv
[j
][0] == '-') {
1666 ctl_fatal("'%s' command takes at most %d arguments "
1667 "(note that options must precede command "
1668 "names and follow a \"--\" argument)",
1669 p
->name
, p
->max_args
);
1673 ctl_fatal("'%s' command takes at most %d arguments",
1674 p
->name
, p
->max_args
);
1677 command
->syntax
= p
;
1678 command
->argc
= n_arg
+ 1;
1679 command
->argv
= &argv
[i
];
1683 pre_cmd_show(struct ctl_context
*ctx
)
1685 const struct cmd_show_table
*show
;
1687 for (show
= cmd_show_tables
; show
->table
; show
++) {
1690 ovsdb_idl_add_table(ctx
->idl
, show
->table
);
1691 if (show
->name_column
) {
1692 ovsdb_idl_add_column(ctx
->idl
, show
->name_column
);
1694 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1695 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1697 ovsdb_idl_add_column(ctx
->idl
, column
);
1700 if (show
->wref_table
.table
) {
1701 ovsdb_idl_add_table(ctx
->idl
, show
->wref_table
.table
);
1703 if (show
->wref_table
.name_column
) {
1704 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.name_column
);
1706 if (show
->wref_table
.wref_column
) {
1707 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.wref_column
);
1712 static const struct cmd_show_table
*
1713 cmd_show_find_table_by_row(const struct ovsdb_idl_row
*row
)
1715 const struct cmd_show_table
*show
;
1717 for (show
= cmd_show_tables
; show
->table
; show
++) {
1718 if (show
->table
== row
->table
->class_
) {
1725 static const struct cmd_show_table
*
1726 cmd_show_find_table_by_name(const char *name
)
1728 const struct cmd_show_table
*show
;
1730 for (show
= cmd_show_tables
; show
->table
; show
++) {
1731 if (!strcmp(show
->table
->name
, name
)) {
1738 /* Prints table entries that weak reference the 'cur_row'. */
1740 cmd_show_weak_ref(struct ctl_context
*ctx
, const struct cmd_show_table
*show
,
1741 const struct ovsdb_idl_row
*cur_row
, int level
)
1743 const struct ovsdb_idl_row
*row_wref
;
1744 const struct ovsdb_idl_table_class
*table
= show
->wref_table
.table
;
1745 const struct ovsdb_idl_column
*name_column
1746 = show
->wref_table
.name_column
;
1747 const struct ovsdb_idl_column
*wref_column
1748 = show
->wref_table
.wref_column
;
1750 if (!table
|| !name_column
|| !wref_column
) {
1754 for (row_wref
= ovsdb_idl_first_row(ctx
->idl
, table
); row_wref
;
1755 row_wref
= ovsdb_idl_next_row(row_wref
)) {
1756 const struct ovsdb_datum
*wref_datum
1757 = ovsdb_idl_read(row_wref
, wref_column
);
1758 /* If weak reference refers to the 'cur_row', prints it. */
1760 && uuid_equals(&cur_row
->uuid
, &wref_datum
->keys
[0].uuid
)) {
1761 const struct ovsdb_datum
*name_datum
1762 = ovsdb_idl_read(row_wref
, name_column
);
1763 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1764 ds_put_format(&ctx
->output
, "%s ", table
->name
);
1765 ovsdb_datum_to_string(name_datum
, &name_column
->type
, &ctx
->output
);
1766 ds_put_char(&ctx
->output
, '\n');
1771 /* 'shown' records the tables that has been displayed by the current
1772 * command to avoid duplicated prints.
1775 cmd_show_row(struct ctl_context
*ctx
, const struct ovsdb_idl_row
*row
,
1776 int level
, struct sset
*shown
)
1778 const struct cmd_show_table
*show
= cmd_show_find_table_by_row(row
);
1781 ds_put_char_multiple(&ctx
->output
, ' ', level
* 4);
1782 if (show
&& show
->name_column
) {
1783 const struct ovsdb_datum
*datum
;
1785 ds_put_format(&ctx
->output
, "%s ", show
->table
->name
);
1786 datum
= ovsdb_idl_read(row
, show
->name_column
);
1787 ovsdb_datum_to_string(datum
, &show
->name_column
->type
, &ctx
->output
);
1789 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1791 ds_put_char(&ctx
->output
, '\n');
1793 if (!show
|| sset_find(shown
, show
->table
->name
)) {
1797 sset_add(shown
, show
->table
->name
);
1798 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
1799 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
1800 const struct ovsdb_datum
*datum
;
1806 datum
= ovsdb_idl_read(row
, column
);
1807 if (column
->type
.key
.type
== OVSDB_TYPE_UUID
&&
1808 column
->type
.key
.u
.uuid
.refTableName
) {
1809 const struct cmd_show_table
*ref_show
;
1812 ref_show
= cmd_show_find_table_by_name(
1813 column
->type
.key
.u
.uuid
.refTableName
);
1815 for (j
= 0; j
< datum
->n
; j
++) {
1816 const struct ovsdb_idl_row
*ref_row
;
1818 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1820 &datum
->keys
[j
].uuid
);
1822 cmd_show_row(ctx
, ref_row
, level
+ 1, shown
);
1827 } else if (ovsdb_type_is_map(&column
->type
) &&
1828 column
->type
.value
.type
== OVSDB_TYPE_UUID
&&
1829 column
->type
.value
.u
.uuid
.refTableName
) {
1830 const struct cmd_show_table
*ref_show
;
1833 /* Prints the key to ref'ed table name map if the ref'ed table
1834 * is also defined in 'cmd_show_tables'. */
1835 ref_show
= cmd_show_find_table_by_name(
1836 column
->type
.value
.u
.uuid
.refTableName
);
1837 if (ref_show
&& ref_show
->name_column
) {
1838 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1839 ds_put_format(&ctx
->output
, "%s:\n", column
->name
);
1840 for (j
= 0; j
< datum
->n
; j
++) {
1841 const struct ovsdb_idl_row
*ref_row
;
1843 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
1845 &datum
->values
[j
].uuid
);
1847 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 2) * 4);
1848 ovsdb_atom_to_string(&datum
->keys
[j
], column
->type
.key
.type
,
1850 ds_put_char(&ctx
->output
, '=');
1852 const struct ovsdb_datum
*ref_datum
;
1854 ref_datum
= ovsdb_idl_read(ref_row
,
1855 ref_show
->name_column
);
1856 ovsdb_datum_to_string(ref_datum
,
1857 &ref_show
->name_column
->type
,
1860 ds_put_cstr(&ctx
->output
, "\"<null>\"");
1862 ds_put_char(&ctx
->output
, '\n');
1868 if (!ovsdb_datum_is_default(datum
, &column
->type
)) {
1869 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
1870 ds_put_format(&ctx
->output
, "%s: ", column
->name
);
1871 ovsdb_datum_to_string(datum
, &column
->type
, &ctx
->output
);
1872 ds_put_char(&ctx
->output
, '\n');
1875 cmd_show_weak_ref(ctx
, show
, row
, level
);
1876 sset_find_and_delete_assert(shown
, show
->table
->name
);
1880 cmd_show(struct ctl_context
*ctx
)
1882 const struct ovsdb_idl_row
*row
;
1883 struct sset shown
= SSET_INITIALIZER(&shown
);
1885 for (row
= ovsdb_idl_first_row(ctx
->idl
, cmd_show_tables
[0].table
);
1886 row
; row
= ovsdb_idl_next_row(row
)) {
1887 cmd_show_row(ctx
, row
, 0, &shown
);
1890 ovs_assert(sset_is_empty(&shown
));
1891 sset_destroy(&shown
);
1895 /* Given pointer to dynamic array 'options_p', array's current size
1896 * 'allocated_options_p' and number of added options 'n_options_p',
1897 * adds all command options to the array. Enlarges the array if
1900 ctl_add_cmd_options(struct option
**options_p
, size_t *n_options_p
,
1901 size_t *allocated_options_p
, int opt_val
)
1904 const struct shash_node
*node
;
1905 size_t n_existing_options
= *n_options_p
;
1907 SHASH_FOR_EACH (node
, &all_commands
) {
1908 const struct ctl_command_syntax
*p
= node
->data
;
1910 if (p
->options
[0]) {
1911 char *save_ptr
= NULL
;
1915 s
= xstrdup(p
->options
);
1916 for (name
= strtok_r(s
, ",", &save_ptr
); name
!= NULL
;
1917 name
= strtok_r(NULL
, ",", &save_ptr
)) {
1918 ovs_assert(name
[0] == '-' && name
[1] == '-' && name
[2]);
1921 size_t n
= strcspn(name
, "=?");
1922 int has_arg
= (name
[n
] == '\0' ? no_argument
1923 : name
[n
] == '=' ? required_argument
1924 : optional_argument
);
1927 o
= find_option(name
, *options_p
, *n_options_p
);
1929 ovs_assert(o
- *options_p
>= n_existing_options
);
1930 ovs_assert(o
->has_arg
== has_arg
);
1932 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
1933 o
->name
= xstrdup(name
);
1934 o
->has_arg
= has_arg
;
1943 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
1944 memset(o
, 0, sizeof *o
);
1947 /* Parses command-line input for commands. */
1948 struct ctl_command
*
1949 ctl_parse_commands(int argc
, char *argv
[], struct shash
*local_options
,
1950 size_t *n_commandsp
)
1952 struct ctl_command
*commands
;
1953 size_t n_commands
, allocated_commands
;
1957 n_commands
= allocated_commands
= 0;
1959 for (start
= i
= 0; i
<= argc
; i
++) {
1960 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
1962 if (n_commands
>= allocated_commands
) {
1963 struct ctl_command
*c
;
1965 commands
= x2nrealloc(commands
, &allocated_commands
,
1967 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
1968 shash_moved(&c
->options
);
1971 parse_command(i
- start
, &argv
[start
], local_options
,
1972 &commands
[n_commands
++]);
1973 } else if (!shash_is_empty(local_options
)) {
1974 ctl_fatal("missing command name (use --help for help)");
1980 ctl_fatal("missing command name (use --help for help)");
1982 *n_commandsp
= n_commands
;
1986 /* Prints all registered commands. */
1988 ctl_print_commands(void)
1990 const struct shash_node
*node
;
1992 SHASH_FOR_EACH (node
, &all_commands
) {
1993 const struct ctl_command_syntax
*p
= node
->data
;
1994 char *options
= xstrdup(p
->options
);
1995 char *options_begin
= options
;
1998 for (item
= strsep(&options
, ","); item
!= NULL
;
1999 item
= strsep(&options
, ",")) {
2000 if (item
[0] != '\0') {
2001 printf("[%s] ", item
);
2004 printf(",%s,", p
->name
);
2005 print_command_arguments(p
);
2008 free(options_begin
);
2014 /* Given array of options 'options', prints them. */
2016 ctl_print_options(const struct option
*options
)
2018 for (; options
->name
; options
++) {
2019 const struct option
*o
= options
;
2021 printf("--%s%s\n", o
->name
, o
->has_arg
? "=ARG" : "");
2022 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
2023 printf("-%c%s\n", o
->val
, o
->has_arg
? " ARG" : "");
2030 /* Returns the default local database path. */
2032 ctl_default_db(void)
2036 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
2041 /* Returns true if it looks like this set of arguments might modify the
2042 * database, otherwise false. (Not very smart, so it's prone to false
2045 ctl_might_write_to_db(char **argv
)
2047 for (; *argv
; argv
++) {
2048 const struct ctl_command_syntax
*p
= shash_find_data(&all_commands
,
2050 if (p
&& p
->mode
== RW
) {
2058 ctl_fatal(const char *format
, ...)
2063 va_start(args
, format
);
2064 message
= xvasprintf(format
, args
);
2067 vlog_set_levels(&this_module
, VLF_CONSOLE
, VLL_OFF
);
2068 VLOG_ERR("%s", message
);
2069 ovs_error(0, "%s", message
);
2070 ctl_exit(EXIT_FAILURE
);
2073 /* Frees the current transaction and the underlying IDL and then calls
2076 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2077 * for a clean memory leak report from valgrind in the normal case. That makes
2078 * it easier to notice real memory leaks. */
2080 ctl_exit(int status
)
2082 if (ctl_exit_func
) {
2083 ctl_exit_func(status
);
2088 /* Comman database commands to be registered. */
2089 static const struct ctl_command_syntax db_ctl_commands
[] = {
2090 {"comment", 0, INT_MAX
, "[ARG]...", NULL
, NULL
, NULL
, "", RO
},
2091 {"get", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get
, cmd_get
,
2092 NULL
, "--if-exists,--id=", RO
},
2093 {"list", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_list
, cmd_list
, NULL
,
2094 "--if-exists,--columns=", RO
},
2095 {"find", 1, INT_MAX
, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find
,
2096 cmd_find
, NULL
, "--columns=", RO
},
2097 {"set", 3, INT_MAX
, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set
,
2098 cmd_set
, NULL
, "--if-exists", RW
},
2099 {"add", 4, INT_MAX
, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add
,
2100 cmd_add
, NULL
, "--if-exists", RW
},
2101 {"remove", 4, INT_MAX
, "TABLE RECORD COLUMN KEY|VALUE|KEY=VALUE...",
2102 pre_cmd_remove
, cmd_remove
, NULL
, "--if-exists", RW
},
2103 {"clear", 3, INT_MAX
, "TABLE RECORD COLUMN...", pre_cmd_clear
, cmd_clear
,
2104 NULL
, "--if-exists", RW
},
2105 {"create", 2, INT_MAX
, "TABLE COLUMN[:KEY]=VALUE...", pre_create
,
2106 cmd_create
, post_create
, "--id=", RW
},
2107 {"destroy", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_destroy
, cmd_destroy
,
2108 NULL
, "--if-exists,--all", RW
},
2109 {"wait-until", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]=VALUE]...",
2110 pre_cmd_wait_until
, cmd_wait_until
, NULL
, "", RO
},
2111 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2115 ctl_register_command(const struct ctl_command_syntax
*command
)
2117 shash_add_assert(&all_commands
, command
->name
, command
);
2120 /* Registers commands represented by 'struct ctl_command_syntax's to
2121 * 'all_commands'. The last element of 'commands' must be an all-NULL
2124 ctl_register_commands(const struct ctl_command_syntax
*commands
)
2126 const struct ctl_command_syntax
*p
;
2128 for (p
= commands
; p
->name
; p
++) {
2129 ctl_register_command(p
);
2133 /* Registers the 'db_ctl_commands' to 'all_commands'. */
2135 ctl_init__(const struct ovsdb_idl_table_class
*idl_classes_
,
2136 const struct ctl_table_class
*ctl_classes_
,
2138 const struct cmd_show_table cmd_show_tables_
[],
2139 void (*ctl_exit_func_
)(int status
))
2141 idl_classes
= idl_classes_
;
2142 ctl_classes
= ctl_classes_
;
2143 n_classes
= n_classes_
;
2144 ctl_exit_func
= ctl_exit_func_
;
2145 ctl_register_commands(db_ctl_commands
);
2147 cmd_show_tables
= cmd_show_tables_
;
2148 if (cmd_show_tables
) {
2149 static const struct ctl_command_syntax show
=
2150 {"show", 0, 0, "", pre_cmd_show
, cmd_show
, NULL
, "", RO
};
2151 ctl_register_command(&show
);
2155 /* Returns the text for the database commands usage. */
2157 ctl_get_db_cmd_usage(void)
2159 return "Database commands:\n\
2160 list TBL [REC] list RECord (or all records) in TBL\n\
2161 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
2162 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
2163 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
2164 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
2165 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
2166 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
2167 create TBL COL[:KEY]=VALUE create and initialize new record\n\
2168 destroy TBL REC delete RECord from TBL\n\
2169 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
2170 Potentially unsafe database commands require --force option.\n";
2173 /* Initializes 'ctx' from 'command'. */
2175 ctl_context_init_command(struct ctl_context
*ctx
,
2176 struct ctl_command
*command
)
2178 ctx
->argc
= command
->argc
;
2179 ctx
->argv
= command
->argv
;
2180 ctx
->options
= command
->options
;
2182 ds_swap(&ctx
->output
, &command
->output
);
2183 ctx
->table
= command
->table
;
2184 ctx
->try_again
= false;
2187 /* Initializes the entire 'ctx'. */
2189 ctl_context_init(struct ctl_context
*ctx
, struct ctl_command
*command
,
2190 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2191 struct ovsdb_symbol_table
*symtab
,
2192 void (*invalidate_cache
)(struct ctl_context
*))
2195 ctl_context_init_command(ctx
, command
);
2199 ctx
->symtab
= symtab
;
2200 ctx
->invalidate_cache
= invalidate_cache
;
2203 /* Completes processing of 'command' within 'ctx'. */
2205 ctl_context_done_command(struct ctl_context
*ctx
,
2206 struct ctl_command
*command
)
2208 ds_swap(&ctx
->output
, &command
->output
);
2209 command
->table
= ctx
->table
;
2212 /* Finishes up with 'ctx'.
2214 * If command is nonnull, first calls ctl_context_done_command() to complete
2215 * processing that command within 'ctx'. */
2217 ctl_context_done(struct ctl_context
*ctx
,
2218 struct ctl_command
*command
)
2221 ctl_context_done_command(ctx
, command
);
2223 invalidate_cache(ctx
);
2226 void ctl_set_column(const char *table_name
,
2227 const struct ovsdb_idl_row
*row
, const char *arg
,
2228 struct ovsdb_symbol_table
*symtab
)
2230 set_column(get_table(table_name
), row
, arg
, symtab
);