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 to_lower_and_underscores(unsigned c
)
192 return c
== '-' ? '_' : tolower(c
);
195 /* Returns a score representing how well 's' matches 'name'. Higher return
196 * values indicate a better match. The order of the arguments is important:
197 * 'name' is the name of an entity such as a table or a column, and 's' is user
200 score_partial_match(const char *name
, const char *s
)
204 if (!strcmp(name
, s
)) {
207 for (score
= 0; ; score
++, name
++, s
++) {
208 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
210 } else if (*name
== '\0') {
214 return *s
== '\0' ? score
: 0;
217 /* Returns NULL and sets 'symbolp' and 'newp' if symbol was created
218 * successfully. Otherwise returns a malloc()'ed error message on failure. */
219 static char * OVS_WARN_UNUSED_RESULT
220 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
,
221 struct ovsdb_symbol
**symbolp
, bool *newp
)
223 struct ovsdb_symbol
*symbol
;
228 return xasprintf("row id \"%s\" does not begin with \"@\"", id
);
232 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
235 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
236 if (symbol
->created
) {
237 return xasprintf("row id \"%s\" may only be specified on one --id "
240 symbol
->created
= true;
246 record_id_equals(const union ovsdb_atom
*name
, enum ovsdb_atomic_type type
,
247 const char *record_id
)
249 if (type
== OVSDB_TYPE_STRING
) {
250 if (!strcmp(name
->string
, record_id
)) {
255 size_t len
= strlen(record_id
);
257 && uuid_from_string(&uuid
, name
->string
)
258 && !strncmp(name
->string
, record_id
, len
)) {
264 ovs_assert(type
== OVSDB_TYPE_INTEGER
);
265 return name
->integer
== strtoll(record_id
, NULL
, 10);
269 static const struct ovsdb_idl_row
*
270 get_row_by_id(struct ctl_context
*ctx
,
271 const struct ovsdb_idl_table_class
*table
,
272 const struct ctl_row_id
*id
, const char *record_id
,
273 bool *multiple_matches
)
275 ovs_assert(multiple_matches
);
276 *multiple_matches
= false;
278 if (!id
->name_column
) {
282 const struct ovsdb_idl_row
*referrer
= NULL
;
284 /* Figure out the 'key' and 'value' types for the column that we're going
285 * to look at. One of these ('name_type') is the type of the name we're
286 * going to compare against 'record_id'. */
287 enum ovsdb_atomic_type key
, value
, name_type
;
289 name_type
= key
= id
->name_column
->type
.key
.type
;
290 value
= OVSDB_TYPE_VOID
;
292 key
= OVSDB_TYPE_STRING
;
293 name_type
= value
= id
->name_column
->type
.value
.type
;
296 /* We only support integer and string names (so far). */
297 if (name_type
== OVSDB_TYPE_INTEGER
) {
298 if (!record_id
[0] || record_id
[strspn(record_id
, "0123456789")]) {
302 ovs_assert(name_type
== OVSDB_TYPE_STRING
);
305 const struct ovsdb_idl_class
*class = ovsdb_idl_get_class(ctx
->idl
);
306 const struct ovsdb_idl_table_class
*id_table
307 = ovsdb_idl_table_class_from_column(class, id
->name_column
);
308 for (const struct ovsdb_idl_row
*row
= ovsdb_idl_first_row(ctx
->idl
,
311 row
= ovsdb_idl_next_row(row
)) {
312 /* Pick out the name column's data. */
313 const struct ovsdb_datum
*datum
= ovsdb_idl_get(
314 row
, id
->name_column
, key
, value
);
316 /* Extract the name from the column. */
317 const union ovsdb_atom
*name
;
319 name
= datum
->n
== 1 ? &datum
->keys
[0] : NULL
;
321 const union ovsdb_atom key_atom
322 = { .string
= CONST_CAST(char *, id
->key
) };
323 unsigned int i
= ovsdb_datum_find_key(datum
, &key_atom
,
325 name
= i
== UINT_MAX
? NULL
: &datum
->values
[i
];
331 /* If the name equals 'record_id', take it. */
332 if (record_id_equals(name
, name_type
, record_id
)) {
334 *multiple_matches
= true;
344 const struct ovsdb_idl_row
*final
= referrer
;
345 if (id
->uuid_column
) {
346 const struct ovsdb_datum
*uuid
;
348 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
349 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
350 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
352 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
,
353 &uuid
->keys
[0].uuid
);
361 char * OVS_WARN_UNUSED_RESULT
362 ctl_get_row(struct ctl_context
*ctx
,
363 const struct ovsdb_idl_table_class
*table
, const char *record_id
,
364 bool must_exist
, const struct ovsdb_idl_row
**rowp
)
366 const struct ovsdb_idl_row
*row
= NULL
;
371 if (uuid_from_string(&uuid
, record_id
)) {
372 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
, &uuid
);
375 if (!strcmp(record_id
, ".")) {
376 row
= ovsdb_idl_first_row(ctx
->idl
, table
);
377 if (row
&& ovsdb_idl_next_row(row
)) {
383 const struct ctl_table_class
*ctl_class
384 = &ctl_classes
[table
- idl_classes
];
385 for (int i
= 0; i
< ARRAY_SIZE(ctl_class
->row_ids
); i
++) {
386 const struct ctl_row_id
*id
= &ctl_class
->row_ids
[i
];
387 bool multiple_matches
;
389 row
= get_row_by_id(ctx
, table
, id
, record_id
, &multiple_matches
);
390 if (multiple_matches
) {
391 const struct ovsdb_idl_class
*class =
392 ovsdb_idl_get_class(ctx
->idl
);
393 const struct ovsdb_idl_table_class
*table_class
=
394 ovsdb_idl_table_class_from_column(class, id
->name_column
);
395 return xasprintf("multiple rows in %s match \"%s\"",
396 table_class
->name
, record_id
);
403 if (!row
&& uuid_is_partial_string(record_id
) >= 4) {
404 for (const struct ovsdb_idl_row
*r
= ovsdb_idl_first_row(ctx
->idl
,
407 r
= ovsdb_idl_next_row(r
)) {
408 if (uuid_is_partial_match(&r
->uuid
, record_id
)) {
412 return xasprintf("%s contains 2 or more rows whose UUIDs "
413 "begin with %s: at least "UUID_FMT
" "
414 "and "UUID_FMT
, table
->name
, record_id
,
415 UUID_ARGS(&row
->uuid
),
416 UUID_ARGS(&r
->uuid
));
421 if (must_exist
&& !row
) {
422 return xasprintf("no row \"%s\" in table %s", record_id
, table
->name
);
430 get_column(const struct ovsdb_idl_table_class
*table
, const char *column_name
,
431 const struct ovsdb_idl_column
**columnp
)
433 struct sset best_matches
= SSET_INITIALIZER(&best_matches
);
434 const struct ovsdb_idl_column
*best_match
= NULL
;
435 unsigned int best_score
= 0;
437 for (size_t i
= 0; i
< table
->n_columns
; i
++) {
438 const struct ovsdb_idl_column
*column
= &table
->columns
[i
];
439 unsigned int score
= score_partial_match(column
->name
, column_name
);
440 if (score
&& score
>= best_score
) {
441 if (score
> best_score
) {
442 sset_clear(&best_matches
);
444 sset_add(&best_matches
, column
->name
);
453 error
= xasprintf("%s does not contain a column whose name matches "
454 "\"%s\"", table
->name
, column_name
);
455 } else if (sset_count(&best_matches
) == 1) {
456 *columnp
= best_match
;
458 char *matches
= sset_join(&best_matches
, ", ", "");
459 error
= xasprintf("%s contains more than one column "
460 "whose name matches \"%s\": %s",
461 table
->name
, column_name
, matches
);
464 sset_destroy(&best_matches
);
468 static char * OVS_WARN_UNUSED_RESULT
469 pre_get_column(struct ctl_context
*ctx
,
470 const struct ovsdb_idl_table_class
*table
,
471 const char *column_name
,
472 const struct ovsdb_idl_column
**columnp
)
474 char *error
= get_column(table
, column_name
, columnp
);
478 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
482 static char * OVS_WARN_UNUSED_RESULT
483 pre_get_table(struct ctl_context
*ctx
, const char *table_name
,
484 const struct ovsdb_idl_table_class
**tablep
)
486 const struct ovsdb_idl_table_class
*table
;
487 char *error
= get_table(table_name
, &table
);
491 ovsdb_idl_add_table(ctx
->idl
, table
);
493 const struct ctl_table_class
*ctl
= &ctl_classes
[table
- idl_classes
];
494 for (int i
= 0; i
< ARRAY_SIZE(ctl
->row_ids
); i
++) {
495 const struct ctl_row_id
*id
= &ctl
->row_ids
[i
];
496 if (id
->name_column
) {
497 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
499 if (id
->uuid_column
) {
500 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
511 missing_operator_error(const char *arg
, const char **allowed_operators
,
517 ds_put_format(&s
, "%s: argument does not end in ", arg
);
518 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
519 if (n_allowed
== 2) {
520 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
521 } else if (n_allowed
> 2) {
524 for (i
= 1; i
< n_allowed
- 1; i
++) {
525 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
527 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
529 ds_put_format(&s
, " followed by a value.");
531 return ds_steal_cstr(&s
);
534 /* Breaks 'arg' apart into a number of fields in the following order:
536 * - The name of a column in 'table', stored into '*columnp'. The column
537 * name may be abbreviated.
539 * - Optionally ':' followed by a key string. The key is stored as a
540 * malloc()'d string into '*keyp', or NULL if no key is present in
543 * - If 'valuep' is nonnull, an operator followed by a value string. The
544 * allowed operators are the 'n_allowed' string in 'allowed_operators',
545 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
546 * index of the operator within 'allowed_operators' is stored into
547 * '*operatorp'. The value is stored as a malloc()'d string into
548 * '*valuep', or NULL if no value is present in 'arg'.
550 * On success, returns NULL. On failure, returned a malloc()'d string error
551 * message and stores NULL into all of the nonnull output arguments. */
552 static char * OVS_WARN_UNUSED_RESULT
553 parse_column_key_value(const char *arg
,
554 const struct ovsdb_idl_table_class
*table
,
555 const struct ovsdb_idl_column
**columnp
, char **keyp
,
557 const char **allowed_operators
, size_t n_allowed
,
564 ovs_assert(!(operatorp
&& !valuep
));
570 /* Parse column name. */
571 error
= ovsdb_token_parse(&p
, &column_name
);
575 if (column_name
[0] == '\0') {
577 error
= xasprintf("%s: missing column name", arg
);
580 error
= get_column(table
, column_name
, columnp
);
586 /* Parse key string. */
589 error
= ovsdb_token_parse(&p
, keyp
);
595 /* Parse value string. */
601 if (!allowed_operators
) {
602 static const char *equals
= "=";
603 allowed_operators
= &equals
;
609 for (i
= 0; i
< n_allowed
; i
++) {
610 const char *op
= allowed_operators
[i
];
611 size_t op_len
= strlen(op
);
613 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
619 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
626 *valuep
= xstrdup(p
+ best_len
);
629 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
650 static char * OVS_WARN_UNUSED_RESULT
651 pre_parse_column_key_value(struct ctl_context
*ctx
, const char *arg
,
652 const struct ovsdb_idl_table_class
*table
)
654 const struct ovsdb_idl_column
*column
;
656 char *column_name
= NULL
;
660 error
= ovsdb_token_parse(&p
, &column_name
);
664 if (column_name
[0] == '\0') {
665 error
= xasprintf("%s: missing column name", arg
);
669 error
= pre_get_column(ctx
, table
, column_name
, &column
);
676 /* Checks if the 'column' is mutable. Returns NULL if it is mutable, or a
677 * malloc()'ed error message otherwise. */
678 static char * OVS_WARN_UNUSED_RESULT
679 check_mutable(const struct ovsdb_idl_row
*row
,
680 const struct ovsdb_idl_column
*column
)
682 if (!ovsdb_idl_is_mutable(row
, column
)) {
683 return xasprintf("cannot modify read-only column %s in table %s",
684 column
->name
, row
->table
->class_
->name
);
690 RELOP(RELOP_EQ, "=") \
691 RELOP(RELOP_NE, "!=") \
692 RELOP(RELOP_LT, "<") \
693 RELOP(RELOP_GT, ">") \
694 RELOP(RELOP_LE, "<=") \
695 RELOP(RELOP_GE, ">=") \
696 RELOP(RELOP_SET_EQ, "{=}") \
697 RELOP(RELOP_SET_NE, "{!=}") \
698 RELOP(RELOP_SET_LT, "{<}") \
699 RELOP(RELOP_SET_GT, "{>}") \
700 RELOP(RELOP_SET_LE, "{<=}") \
701 RELOP(RELOP_SET_GE, "{>=}")
704 #define RELOP(ENUM, STRING) ENUM,
710 is_set_operator(enum relop op
)
712 return (op
== RELOP_SET_EQ
|| op
== RELOP_SET_NE
||
713 op
== RELOP_SET_LT
|| op
== RELOP_SET_GT
||
714 op
== RELOP_SET_LE
|| op
== RELOP_SET_GE
);
718 evaluate_relop(const struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
719 const struct ovsdb_type
*type
, enum relop op
)
724 return ovsdb_datum_compare_3way(a
, b
, type
) == 0;
727 return ovsdb_datum_compare_3way(a
, b
, type
) != 0;
729 return ovsdb_datum_compare_3way(a
, b
, type
) < 0;
731 return ovsdb_datum_compare_3way(a
, b
, type
) > 0;
733 return ovsdb_datum_compare_3way(a
, b
, type
) <= 0;
735 return ovsdb_datum_compare_3way(a
, b
, type
) >= 0;
738 return b
->n
> a
->n
&& ovsdb_datum_includes_all(a
, b
, type
);
740 return a
->n
> b
->n
&& ovsdb_datum_includes_all(b
, a
, type
);
742 return ovsdb_datum_includes_all(a
, b
, type
);
744 return ovsdb_datum_includes_all(b
, a
, type
);
751 /* Checks if given row satisfies the specified condition. Returns the result of
752 * evaluating the condition in 'satisfied' flag and NULL as a return value on
753 * success. On failure returns a malloc()'ed error message and 'satisfied'
754 * value is not modified. */
755 static char * OVS_WARN_UNUSED_RESULT
756 check_condition(const struct ovsdb_idl_table_class
*table
,
757 const struct ovsdb_idl_row
*row
, const char *arg
,
758 struct ovsdb_symbol_table
*symtab
, bool *satisfied
)
760 static const char *operators
[] = {
761 #define RELOP(ENUM, STRING) STRING,
766 const struct ovsdb_idl_column
*column
;
767 const struct ovsdb_datum
*have_datum
;
768 char *key_string
= NULL
;
769 char *value_string
= NULL
;
770 struct ovsdb_type type
;
775 ovs_assert(satisfied
);
777 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
778 &operator, operators
, ARRAY_SIZE(operators
),
784 error
= xasprintf("%s: missing value", arg
);
789 type
.n_max
= UINT_MAX
;
791 have_datum
= ovsdb_idl_read(row
, column
);
793 union ovsdb_atom want_key
;
794 struct ovsdb_datum b
;
797 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
798 error
= xasprintf("cannot specify key to check for non-map column "
803 error
= ovsdb_atom_from_string(&want_key
, NULL
, &column
->type
.key
,
809 type
.key
= type
.value
;
810 type
.value
.type
= OVSDB_TYPE_VOID
;
811 error
= ovsdb_datum_from_string(&b
, &type
, value_string
, symtab
);
816 idx
= ovsdb_datum_find_key(have_datum
,
817 &want_key
, column
->type
.key
.type
);
818 if (idx
== UINT_MAX
&& !is_set_operator(operator)) {
821 struct ovsdb_datum a
;
823 if (idx
!= UINT_MAX
) {
825 a
.keys
= &have_datum
->values
[idx
];
833 retval
= evaluate_relop(&a
, &b
, &type
, operator);
836 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
837 ovsdb_datum_destroy(&b
, &type
);
839 struct ovsdb_datum want_datum
;
841 error
= ovsdb_datum_from_string(&want_datum
, &column
->type
,
842 value_string
, symtab
);
846 retval
= evaluate_relop(have_datum
, &want_datum
, &type
, operator);
847 ovsdb_datum_destroy(&want_datum
, &column
->type
);
859 invalidate_cache(struct ctl_context
*ctx
)
861 if (ctx
->invalidate_cache_cb
) {
862 (ctx
->invalidate_cache_cb
)(ctx
);
867 pre_cmd_get(struct ctl_context
*ctx
)
869 const char *id
= shash_find_data(&ctx
->options
, "--id");
870 const char *table_name
= ctx
->argv
[1];
871 const struct ovsdb_idl_table_class
*table
;
874 /* Using "get" without --id or a column name could possibly make sense.
875 * Maybe, for example, a *ctl command run wants to assert that a row
876 * exists. But it is unlikely that an interactive user would want to do
877 * that, so issue a warning if we're running on a terminal. */
878 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
879 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
880 "possibly erroneous");
883 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
887 for (i
= 3; i
< ctx
->argc
; i
++) {
888 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
889 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
893 ctx
->error
= pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
901 cmd_get(struct ctl_context
*ctx
)
903 const char *id
= shash_find_data(&ctx
->options
, "--id");
904 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
905 const char *table_name
= ctx
->argv
[1];
906 const char *record_id
= ctx
->argv
[2];
907 const struct ovsdb_idl_table_class
*table
;
908 const struct ovsdb_idl_row
*row
;
909 struct ds
*out
= &ctx
->output
;
912 if (id
&& !must_exist
) {
913 ctl_error(ctx
, "--if-exists and --id may not be specified together");
917 ctx
->error
= get_table(table_name
, &table
);
921 ctx
->error
= ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
);
930 struct ovsdb_symbol
*symbol
= NULL
;
933 ctx
->error
= create_symbol(ctx
->symtab
, id
, &symbol
, &new);
938 ctl_error(ctx
, "row id \"%s\" specified on \"get\" command was "
939 "used before it was defined", id
);
942 symbol
->uuid
= row
->uuid
;
944 /* This symbol refers to a row that already exists, so disable warnings
945 * about it being unreferenced. */
946 symbol
->strong_ref
= true;
948 for (i
= 3; i
< ctx
->argc
; i
++) {
949 const struct ovsdb_idl_column
*column
;
950 const struct ovsdb_datum
*datum
;
953 /* Special case for obtaining the UUID of a row. We can't just do this
954 * through parse_column_key_value() below since it returns a "struct
955 * ovsdb_idl_column" and the UUID column doesn't have one. */
956 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
957 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
958 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
962 ctx
->error
= parse_column_key_value(ctx
->argv
[i
], table
, &column
,
963 &key_string
, NULL
, NULL
, 0, NULL
);
968 ovsdb_idl_txn_verify(row
, column
);
969 datum
= ovsdb_idl_read(row
, column
);
971 union ovsdb_atom key
;
974 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
976 "cannot specify key to get for non-map column %s",
982 ctx
->error
= ovsdb_atom_from_string(&key
, NULL
, &column
->type
.key
,
983 key_string
, ctx
->symtab
);
989 idx
= ovsdb_datum_find_key(datum
, &key
,
990 column
->type
.key
.type
);
991 if (idx
== UINT_MAX
) {
994 ctx
, "no key \"%s\" in %s record \"%s\" column %s",
995 key_string
, table
->name
, record_id
, column
->name
);
997 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1001 ovsdb_atom_to_string(&datum
->values
[idx
],
1002 column
->type
.value
.type
, out
);
1004 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1006 ovsdb_datum_to_string(datum
, &column
->type
, out
);
1008 ds_put_char(out
, '\n');
1014 /* Returns NULL on success or malloc()'ed error message on failure. */
1015 static char * OVS_WARN_UNUSED_RESULT
1016 parse_column_names(const char *column_names
,
1017 const struct ovsdb_idl_table_class
*table
,
1018 const struct ovsdb_idl_column
***columnsp
,
1021 const struct ovsdb_idl_column
**columns
;
1024 if (!column_names
) {
1027 n_columns
= table
->n_columns
+ 1;
1028 columns
= xmalloc(n_columns
* sizeof *columns
);
1030 for (i
= 0; i
< table
->n_columns
; i
++) {
1031 columns
[i
+ 1] = &table
->columns
[i
];
1034 char *s
= xstrdup(column_names
);
1035 size_t allocated_columns
;
1036 char *save_ptr
= NULL
;
1040 allocated_columns
= n_columns
= 0;
1041 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
1042 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
1043 const struct ovsdb_idl_column
*column
;
1045 if (!strcasecmp(column_name
, "_uuid")) {
1048 char *error
= get_column(table
, column_name
, &column
);
1055 if (n_columns
>= allocated_columns
) {
1056 columns
= x2nrealloc(columns
, &allocated_columns
,
1059 columns
[n_columns
++] = column
;
1064 return xstrdup("must specify at least one column name");
1067 *columnsp
= columns
;
1068 *n_columnsp
= n_columns
;
1072 static char * OVS_WARN_UNUSED_RESULT
1073 pre_list_columns(struct ctl_context
*ctx
,
1074 const struct ovsdb_idl_table_class
*table
,
1075 const char *column_names
)
1077 const struct ovsdb_idl_column
**columns
;
1082 error
= parse_column_names(column_names
, table
, &columns
, &n_columns
);
1086 for (i
= 0; i
< n_columns
; i
++) {
1088 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
1096 pre_cmd_list(struct ctl_context
*ctx
)
1098 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1099 const char *table_name
= ctx
->argv
[1];
1100 const struct ovsdb_idl_table_class
*table
;
1102 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1106 ctx
->error
= pre_list_columns(ctx
, table
, column_names
);
1112 static struct table
*
1113 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
1118 out
= xmalloc(sizeof *out
);
1121 for (i
= 0; i
< n_columns
; i
++) {
1122 const struct ovsdb_idl_column
*column
= columns
[i
];
1123 const char *column_name
= column
? column
->name
: "_uuid";
1125 table_add_column(out
, "%s", column_name
);
1132 list_record(const struct ovsdb_idl_row
*row
,
1133 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
1143 for (i
= 0; i
< n_columns
; i
++) {
1144 const struct ovsdb_idl_column
*column
= columns
[i
];
1145 struct cell
*cell
= table_add_cell(out
);
1148 struct ovsdb_datum datum
;
1149 union ovsdb_atom atom
;
1151 atom
.uuid
= row
->uuid
;
1154 datum
.values
= NULL
;
1157 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
1158 cell
->type
= &ovsdb_type_uuid
;
1160 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
1162 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
1163 cell
->type
= &column
->type
;
1169 cmd_list(struct ctl_context
*ctx
)
1171 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1172 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1173 const struct ovsdb_idl_column
**columns
;
1174 const char *table_name
= ctx
->argv
[1];
1175 const struct ovsdb_idl_table_class
*table
;
1180 ctx
->error
= get_table(table_name
, &table
);
1184 ctx
->error
= parse_column_names(column_names
, table
, &columns
, &n_columns
);
1188 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1189 if (ctx
->argc
> 2) {
1190 for (i
= 2; i
< ctx
->argc
; i
++) {
1191 const struct ovsdb_idl_row
*row
;
1193 ctx
->error
= ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
,
1199 list_record(row
, columns
, n_columns
, out
);
1202 const struct ovsdb_idl_row
*row
;
1204 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
!= NULL
;
1205 row
= ovsdb_idl_next_row(row
)) {
1206 list_record(row
, columns
, n_columns
, out
);
1212 /* Finds the "struct ovsdb_idl_table_class *" with 'table_name' by searching
1213 * the tables in these schema. Returns NULL and sets 'tablep' on success, or a
1214 * malloc()'ed error message on failure. */
1215 static char * OVS_WARN_UNUSED_RESULT
1216 get_table(const char *table_name
, const struct ovsdb_idl_table_class
**tablep
)
1218 struct sset best_matches
= SSET_INITIALIZER(&best_matches
);
1219 const struct ovsdb_idl_table_class
*best_match
= NULL
;
1220 unsigned int best_score
= 0;
1222 for (const struct ovsdb_idl_table_class
*table
= idl_classes
;
1223 table
< &idl_classes
[n_classes
]; table
++) {
1224 unsigned int score
= score_partial_match(table
->name
, table_name
);
1225 if (score
&& score
>= best_score
) {
1226 if (score
> best_score
) {
1227 sset_clear(&best_matches
);
1229 sset_add(&best_matches
, table
->name
);
1237 error
= xasprintf("unknown table \"%s\"", table_name
);
1238 } else if (sset_count(&best_matches
) == 1) {
1239 *tablep
= best_match
;
1241 char *matches
= sset_join(&best_matches
, ", ", "");
1242 error
= xasprintf("\"%s\" matches multiple table names: %s",
1243 table_name
, matches
);
1246 sset_destroy(&best_matches
);
1251 pre_cmd_find(struct ctl_context
*ctx
)
1253 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1254 const char *table_name
= ctx
->argv
[1];
1255 const struct ovsdb_idl_table_class
*table
;
1258 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1262 ctx
->error
= pre_list_columns(ctx
, table
, column_names
);
1266 for (i
= 2; i
< ctx
->argc
; i
++) {
1267 ctx
->error
= pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1275 cmd_find(struct ctl_context
*ctx
)
1277 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
1278 const struct ovsdb_idl_column
**columns
;
1279 const char *table_name
= ctx
->argv
[1];
1280 const struct ovsdb_idl_table_class
*table
;
1281 const struct ovsdb_idl_row
*row
;
1285 ctx
->error
= get_table(table_name
, &table
);
1289 ctx
->error
= parse_column_names(column_names
, table
, &columns
, &n_columns
);
1293 out
= ctx
->table
= list_make_table(columns
, n_columns
);
1294 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
); row
;
1295 row
= ovsdb_idl_next_row(row
)) {
1298 for (i
= 2; i
< ctx
->argc
; i
++) {
1299 bool satisfied
= false;
1301 ctx
->error
= check_condition(table
, row
, ctx
->argv
[i
],
1302 ctx
->symtab
, &satisfied
);
1311 list_record(row
, columns
, n_columns
, out
);
1318 /* Sets the column of 'row' in 'table'. Returns NULL on success or a
1319 * malloc()'ed error message on failure. */
1320 static char * OVS_WARN_UNUSED_RESULT
1321 set_column(const struct ovsdb_idl_table_class
*table
,
1322 const struct ovsdb_idl_row
*row
, const char *arg
,
1323 struct ovsdb_symbol_table
*symtab
)
1325 const struct ovsdb_idl_column
*column
;
1326 char *key_string
= NULL
;
1327 char *value_string
= NULL
;
1330 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
1331 NULL
, NULL
, 0, &value_string
);
1335 if (!value_string
) {
1336 error
= xasprintf("%s: missing value", arg
);
1339 error
= check_mutable(row
, column
);
1345 union ovsdb_atom key
, value
;
1346 struct ovsdb_datum datum
;
1348 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
1349 error
= xasprintf("cannot specify key to set for non-map column "
1350 "%s", column
->name
);
1354 error
= ovsdb_atom_from_string(&key
, NULL
, &column
->type
.key
,
1355 key_string
, symtab
);
1359 error
= ovsdb_atom_from_string(&value
, NULL
, &column
->type
.value
,
1360 value_string
, symtab
);
1365 ovsdb_datum_init_empty(&datum
);
1366 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
, NULL
);
1368 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
1369 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
1371 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
1372 &column
->type
, false);
1373 ovsdb_idl_txn_verify(row
, column
);
1374 ovsdb_idl_txn_write(row
, column
, &datum
);
1376 struct ovsdb_datum datum
;
1378 error
= ovsdb_datum_from_string(&datum
, &column
->type
,
1379 value_string
, symtab
);
1383 ovsdb_idl_txn_write(row
, column
, &datum
);
1394 pre_cmd_set(struct ctl_context
*ctx
)
1396 const char *table_name
= ctx
->argv
[1];
1397 const struct ovsdb_idl_table_class
*table
;
1400 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1404 for (i
= 3; i
< ctx
->argc
; i
++) {
1405 ctx
->error
= pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1413 cmd_set(struct ctl_context
*ctx
)
1415 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1416 const char *table_name
= ctx
->argv
[1];
1417 const char *record_id
= ctx
->argv
[2];
1418 const struct ovsdb_idl_table_class
*table
;
1419 const struct ovsdb_idl_row
*row
;
1422 ctx
->error
= get_table(table_name
, &table
);
1426 ctx
->error
= ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
);
1434 for (i
= 3; i
< ctx
->argc
; i
++) {
1435 ctx
->error
= set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
1441 invalidate_cache(ctx
);
1445 pre_cmd_add(struct ctl_context
*ctx
)
1447 const char *table_name
= ctx
->argv
[1];
1448 const char *column_name
= ctx
->argv
[3];
1449 const struct ovsdb_idl_table_class
*table
;
1450 const struct ovsdb_idl_column
*column
;
1452 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1456 ctx
->error
= pre_get_column(ctx
, table
, column_name
, &column
);
1463 cmd_add(struct ctl_context
*ctx
)
1465 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1466 const char *table_name
= ctx
->argv
[1];
1467 const char *record_id
= ctx
->argv
[2];
1468 const char *column_name
= ctx
->argv
[3];
1469 const struct ovsdb_idl_table_class
*table
;
1470 const struct ovsdb_idl_column
*column
;
1471 const struct ovsdb_idl_row
*row
;
1472 const struct ovsdb_type
*type
;
1473 struct ovsdb_datum old
;
1476 ctx
->error
= get_table(table_name
, &table
);
1480 ctx
->error
= get_column(table
, column_name
, &column
);
1484 ctx
->error
= ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
);
1491 ctx
->error
= check_mutable(row
, column
);
1496 type
= &column
->type
;
1497 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1498 for (i
= 4; i
< ctx
->argc
; i
++) {
1499 struct ovsdb_type add_type
;
1500 struct ovsdb_datum add
;
1504 add_type
.n_max
= UINT_MAX
;
1505 ctx
->error
= ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
1508 ovsdb_datum_destroy(&old
, &column
->type
);
1511 ovsdb_datum_union(&old
, &add
, type
, false);
1512 ovsdb_datum_destroy(&add
, type
);
1514 if (old
.n
> type
->n_max
) {
1515 ctl_error(ctx
, "\"add\" operation would put %u %s in column %s of "
1516 "table %s but the maximum number is %u",
1518 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1519 column
->name
, table
->name
, type
->n_max
);
1520 ovsdb_datum_destroy(&old
, &column
->type
);
1523 ovsdb_idl_txn_verify(row
, column
);
1524 ovsdb_idl_txn_write(row
, column
, &old
);
1526 invalidate_cache(ctx
);
1530 pre_cmd_remove(struct ctl_context
*ctx
)
1532 const char *table_name
= ctx
->argv
[1];
1533 const char *column_name
= ctx
->argv
[3];
1534 const struct ovsdb_idl_table_class
*table
;
1535 const struct ovsdb_idl_column
*column
;
1537 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1541 ctx
->error
= pre_get_column(ctx
, table
, column_name
, &column
);
1548 cmd_remove(struct ctl_context
*ctx
)
1550 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1551 const char *table_name
= ctx
->argv
[1];
1552 const char *record_id
= ctx
->argv
[2];
1553 const char *column_name
= ctx
->argv
[3];
1554 const struct ovsdb_idl_table_class
*table
;
1555 const struct ovsdb_idl_column
*column
;
1556 const struct ovsdb_idl_row
*row
;
1557 const struct ovsdb_type
*type
;
1558 struct ovsdb_datum old
;
1561 ctx
->error
= get_table(table_name
, &table
);
1565 ctx
->error
= get_column(table
, column_name
, &column
);
1569 ctx
->error
= ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
);
1576 ctx
->error
= check_mutable(row
, column
);
1581 type
= &column
->type
;
1582 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
1583 for (i
= 4; i
< ctx
->argc
; i
++) {
1584 struct ovsdb_type rm_type
;
1585 struct ovsdb_datum rm
;
1590 rm_type
.n_max
= UINT_MAX
;
1591 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
1592 ctx
->argv
[i
], ctx
->symtab
);
1595 if (ovsdb_type_is_map(&rm_type
)) {
1596 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
1598 ctx
->error
= ovsdb_datum_from_string(&rm
, &rm_type
,
1602 ovsdb_datum_destroy(&old
, &column
->type
);
1607 ovsdb_datum_destroy(&old
, &column
->type
);
1611 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
1612 ovsdb_datum_destroy(&rm
, &rm_type
);
1614 if (old
.n
< type
->n_min
) {
1615 ctl_error(ctx
, "\"remove\" operation would put %u %s in column %s of "
1616 "table %s but the minimum number is %u", old
.n
,
1617 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
1618 column
->name
, table
->name
, type
->n_min
);
1619 ovsdb_datum_destroy(&old
, &column
->type
);
1622 ovsdb_idl_txn_verify(row
, column
);
1623 ovsdb_idl_txn_write(row
, column
, &old
);
1625 invalidate_cache(ctx
);
1629 pre_cmd_clear(struct ctl_context
*ctx
)
1631 const char *table_name
= ctx
->argv
[1];
1632 const struct ovsdb_idl_table_class
*table
;
1635 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1639 for (i
= 3; i
< ctx
->argc
; i
++) {
1640 const struct ovsdb_idl_column
*column
;
1642 ctx
->error
= pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
1650 cmd_clear(struct ctl_context
*ctx
)
1652 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1653 const char *table_name
= ctx
->argv
[1];
1654 const char *record_id
= ctx
->argv
[2];
1655 const struct ovsdb_idl_table_class
*table
;
1656 const struct ovsdb_idl_row
*row
;
1659 ctx
->error
= get_table(table_name
, &table
);
1663 ctx
->error
= ctl_get_row(ctx
, table
, record_id
, must_exist
, &row
);
1671 for (i
= 3; i
< ctx
->argc
; i
++) {
1672 const struct ovsdb_idl_column
*column
;
1673 const struct ovsdb_type
*type
;
1674 struct ovsdb_datum datum
;
1676 ctx
->error
= get_column(table
, ctx
->argv
[i
], &column
);
1680 ctx
->error
= check_mutable(row
, column
);
1685 type
= &column
->type
;
1686 if (type
->n_min
> 0) {
1687 ctl_error(ctx
, "\"clear\" operation cannot be applied to column "
1688 "%s of table %s, which is not allowed to be empty",
1689 column
->name
, table
->name
);
1693 ovsdb_datum_init_empty(&datum
);
1694 ovsdb_idl_txn_write(row
, column
, &datum
);
1697 invalidate_cache(ctx
);
1701 pre_create(struct ctl_context
*ctx
)
1703 const char *id
= shash_find_data(&ctx
->options
, "--id");
1704 const char *table_name
= ctx
->argv
[1];
1705 const struct ovsdb_idl_table_class
*table
;
1707 ctx
->error
= get_table(table_name
, &table
);
1711 if (!id
&& !table
->is_root
) {
1712 VLOG_WARN("applying \"create\" command to table %s without --id "
1713 "option will have no effect", table
->name
);
1718 cmd_create(struct ctl_context
*ctx
)
1720 const char *id
= shash_find_data(&ctx
->options
, "--id");
1721 const char *table_name
= ctx
->argv
[1];
1722 const struct ovsdb_idl_table_class
*table
;
1723 const struct ovsdb_idl_row
*row
;
1724 const struct uuid
*uuid
= NULL
;
1727 ctx
->error
= get_table(table_name
, &table
);
1732 struct ovsdb_symbol
*symbol
= NULL
;
1734 ctx
->error
= create_symbol(ctx
->symtab
, id
, &symbol
, NULL
);
1738 if (table
->is_root
) {
1739 /* This table is in the root set, meaning that rows created in it
1740 * won't disappear even if they are unreferenced, so disable
1741 * warnings about that by pretending that there is a reference. */
1742 symbol
->strong_ref
= true;
1744 uuid
= &symbol
->uuid
;
1747 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
, uuid
);
1748 for (i
= 2; i
< ctx
->argc
; i
++) {
1749 ctx
->error
= set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
1754 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
1757 /* This function may be used as the 'postprocess' function for commands that
1758 * insert new rows into the database. It expects that the command's 'run'
1759 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
1760 * sole output. It replaces that output by the row's permanent UUID assigned
1761 * by the database server and appends a new-line.
1763 * Currently we use this only for "create", because the higher-level commands
1764 * are supposed to be independent of the actual structure of the vswitch
1767 post_create(struct ctl_context
*ctx
)
1769 const struct uuid
*real
;
1772 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
1775 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
1777 ds_clear(&ctx
->output
);
1778 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
1780 ds_put_char(&ctx
->output
, '\n');
1784 pre_cmd_destroy(struct ctl_context
*ctx
)
1786 const char *table_name
= ctx
->argv
[1];
1788 ctx
->error
= pre_get_table(ctx
, table_name
, NULL
);
1795 cmd_destroy(struct ctl_context
*ctx
)
1797 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1798 bool delete_all
= shash_find(&ctx
->options
, "--all");
1799 const char *table_name
= ctx
->argv
[1];
1800 const struct ovsdb_idl_table_class
*table
;
1803 ctx
->error
= get_table(table_name
, &table
);
1808 if (delete_all
&& ctx
->argc
> 2) {
1809 ctl_error(ctx
, "--all and records argument should not be specified "
1814 if (delete_all
&& !must_exist
) {
1815 ctl_error(ctx
, "--all and --if-exists should not be specified "
1821 const struct ovsdb_idl_row
*row
;
1822 const struct ovsdb_idl_row
*next_row
;
1824 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
);
1826 next_row
= ovsdb_idl_next_row(row
);
1827 ovsdb_idl_txn_delete(row
);
1831 for (i
= 2; i
< ctx
->argc
; i
++) {
1832 const struct ovsdb_idl_row
*row
;
1834 ctx
->error
= ctl_get_row(ctx
, table
, ctx
->argv
[i
], must_exist
,
1840 ovsdb_idl_txn_delete(row
);
1844 invalidate_cache(ctx
);
1848 pre_cmd_wait_until(struct ctl_context
*ctx
)
1850 const char *table_name
= ctx
->argv
[1];
1851 const struct ovsdb_idl_table_class
*table
;
1854 ctx
->error
= pre_get_table(ctx
, table_name
, &table
);
1859 for (i
= 3; i
< ctx
->argc
; i
++) {
1860 ctx
->error
= pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
1868 cmd_wait_until(struct ctl_context
*ctx
)
1870 const char *table_name
= ctx
->argv
[1];
1871 const char *record_id
= ctx
->argv
[2];
1872 const struct ovsdb_idl_table_class
*table
;
1873 const struct ovsdb_idl_row
*row
;
1876 ctx
->error
= get_table(table_name
, &table
);
1880 ctx
->error
= ctl_get_row(ctx
, table
, record_id
, false, &row
);
1885 ctx
->try_again
= true;
1889 for (i
= 3; i
< ctx
->argc
; i
++) {
1892 ctx
->error
= check_condition(table
, row
, ctx
->argv
[i
], ctx
->symtab
,
1898 ctx
->try_again
= true;
1904 /* Parses one command. */
1905 static char * OVS_WARN_UNUSED_RESULT
1906 parse_command(int argc
, char *argv
[], struct shash
*local_options
,
1907 struct ctl_command
*command
)
1909 const struct ctl_command_syntax
*p
;
1910 struct shash_node
*node
;
1915 shash_init(&command
->options
);
1916 shash_swap(local_options
, &command
->options
);
1917 for (i
= 0; i
< argc
; i
++) {
1918 const char *option
= argv
[i
];
1922 if (option
[0] != '-') {
1926 equals
= strchr(option
, '=');
1928 key
= xmemdup0(option
, equals
- option
);
1929 value
= xstrdup(equals
+ 1);
1931 key
= xstrdup(option
);
1935 if (shash_find(&command
->options
, key
)) {
1938 error
= xasprintf("'%s' option specified multiple times", argv
[i
]);
1941 shash_add_nocopy(&command
->options
, key
, value
);
1944 error
= xstrdup("missing command name (use --help for help)");
1948 p
= shash_find_data(&all_commands
, argv
[i
]);
1950 error
= xasprintf("unknown command '%s'; use --help for help",
1955 SHASH_FOR_EACH (node
, &command
->options
) {
1956 const char *s
= strstr(p
->options
, node
->name
);
1957 int end
= s
? s
[strlen(node
->name
)] : EOF
;
1959 if (!strchr("=,? ", end
)) {
1960 error
= xasprintf("'%s' command has no '%s' option",
1961 argv
[i
], node
->name
);
1964 if (end
!= '?' && (end
== '=') != (node
->data
!= NULL
)) {
1966 error
= xasprintf("missing argument to '%s' option on '%s' "
1967 "command", node
->name
, argv
[i
]);
1970 error
= xasprintf("'%s' option on '%s' does not accept an "
1971 "argument", node
->name
, argv
[i
]);
1977 n_arg
= argc
- i
- 1;
1978 if (n_arg
< p
->min_args
) {
1979 error
= xasprintf("'%s' command requires at least %d arguments",
1980 p
->name
, p
->min_args
);
1982 } else if (n_arg
> p
->max_args
) {
1985 for (j
= i
+ 1; j
< argc
; j
++) {
1986 if (argv
[j
][0] == '-') {
1987 error
= xasprintf("'%s' command takes at most %d arguments "
1988 "(note that options must precede command "
1989 "names and follow a \"--\" argument)",
1990 p
->name
, p
->max_args
);
1995 error
= xasprintf("'%s' command takes at most %d arguments",
1996 p
->name
, p
->max_args
);
2000 command
->syntax
= p
;
2001 command
->argc
= n_arg
+ 1;
2002 command
->argv
= &argv
[i
];
2006 shash_destroy_free_data(&command
->options
);
2011 pre_cmd_show(struct ctl_context
*ctx
)
2013 const struct cmd_show_table
*show
;
2015 for (show
= cmd_show_tables
; show
->table
; show
++) {
2018 ovsdb_idl_add_table(ctx
->idl
, show
->table
);
2019 if (show
->name_column
) {
2020 ovsdb_idl_add_column(ctx
->idl
, show
->name_column
);
2022 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
2023 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
2025 ovsdb_idl_add_column(ctx
->idl
, column
);
2028 if (show
->wref_table
.table
) {
2029 ovsdb_idl_add_table(ctx
->idl
, show
->wref_table
.table
);
2031 if (show
->wref_table
.name_column
) {
2032 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.name_column
);
2034 if (show
->wref_table
.wref_column
) {
2035 ovsdb_idl_add_column(ctx
->idl
, show
->wref_table
.wref_column
);
2040 static const struct cmd_show_table
*
2041 cmd_show_find_table_by_row(const struct ovsdb_idl_row
*row
)
2043 const struct cmd_show_table
*show
;
2045 for (show
= cmd_show_tables
; show
->table
; show
++) {
2046 if (show
->table
== row
->table
->class_
) {
2053 static const struct cmd_show_table
*
2054 cmd_show_find_table_by_name(const char *name
)
2056 const struct cmd_show_table
*show
;
2058 for (show
= cmd_show_tables
; show
->table
; show
++) {
2059 if (!strcmp(show
->table
->name
, name
)) {
2066 /* Prints table entries that weak reference the 'cur_row'. */
2068 cmd_show_weak_ref(struct ctl_context
*ctx
, const struct cmd_show_table
*show
,
2069 const struct ovsdb_idl_row
*cur_row
, int level
)
2071 const struct ovsdb_idl_row
*row_wref
;
2072 const struct ovsdb_idl_table_class
*table
= show
->wref_table
.table
;
2073 const struct ovsdb_idl_column
*name_column
2074 = show
->wref_table
.name_column
;
2075 const struct ovsdb_idl_column
*wref_column
2076 = show
->wref_table
.wref_column
;
2078 if (!table
|| !name_column
|| !wref_column
) {
2082 for (row_wref
= ovsdb_idl_first_row(ctx
->idl
, table
); row_wref
;
2083 row_wref
= ovsdb_idl_next_row(row_wref
)) {
2084 const struct ovsdb_datum
*wref_datum
2085 = ovsdb_idl_read(row_wref
, wref_column
);
2086 /* If weak reference refers to the 'cur_row', prints it. */
2088 && uuid_equals(&cur_row
->uuid
, &wref_datum
->keys
[0].uuid
)) {
2089 const struct ovsdb_datum
*name_datum
2090 = ovsdb_idl_read(row_wref
, name_column
);
2091 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
2092 ds_put_format(&ctx
->output
, "%s ", table
->name
);
2093 ovsdb_datum_to_string(name_datum
, &name_column
->type
, &ctx
->output
);
2094 ds_put_char(&ctx
->output
, '\n');
2099 /* 'shown' records the tables that has been displayed by the current
2100 * command to avoid duplicated prints.
2103 cmd_show_row(struct ctl_context
*ctx
, const struct ovsdb_idl_row
*row
,
2104 int level
, struct sset
*shown
)
2106 const struct cmd_show_table
*show
= cmd_show_find_table_by_row(row
);
2109 ds_put_char_multiple(&ctx
->output
, ' ', level
* 4);
2110 if (show
&& show
->name_column
) {
2111 const struct ovsdb_datum
*datum
;
2113 ds_put_format(&ctx
->output
, "%s ", show
->table
->name
);
2114 datum
= ovsdb_idl_read(row
, show
->name_column
);
2115 ovsdb_datum_to_string(datum
, &show
->name_column
->type
, &ctx
->output
);
2117 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
2119 ds_put_char(&ctx
->output
, '\n');
2121 if (!show
|| sset_find(shown
, show
->table
->name
)) {
2125 sset_add(shown
, show
->table
->name
);
2126 for (i
= 0; i
< ARRAY_SIZE(show
->columns
); i
++) {
2127 const struct ovsdb_idl_column
*column
= show
->columns
[i
];
2128 const struct ovsdb_datum
*datum
;
2134 datum
= ovsdb_idl_read(row
, column
);
2135 if (column
->type
.key
.type
== OVSDB_TYPE_UUID
&&
2136 column
->type
.key
.uuid
.refTableName
) {
2137 const struct cmd_show_table
*ref_show
;
2140 ref_show
= cmd_show_find_table_by_name(
2141 column
->type
.key
.uuid
.refTableName
);
2143 for (j
= 0; j
< datum
->n
; j
++) {
2144 const struct ovsdb_idl_row
*ref_row
;
2146 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
2148 &datum
->keys
[j
].uuid
);
2150 cmd_show_row(ctx
, ref_row
, level
+ 1, shown
);
2155 } else if (ovsdb_type_is_map(&column
->type
) &&
2156 column
->type
.value
.type
== OVSDB_TYPE_UUID
&&
2157 column
->type
.value
.uuid
.refTableName
) {
2158 const struct cmd_show_table
*ref_show
;
2161 /* Prints the key to ref'ed table name map if the ref'ed table
2162 * is also defined in 'cmd_show_tables'. */
2163 ref_show
= cmd_show_find_table_by_name(
2164 column
->type
.value
.uuid
.refTableName
);
2165 if (ref_show
&& ref_show
->name_column
) {
2166 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
2167 ds_put_format(&ctx
->output
, "%s:\n", column
->name
);
2168 for (j
= 0; j
< datum
->n
; j
++) {
2169 const struct ovsdb_idl_row
*ref_row
;
2171 ref_row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
,
2173 &datum
->values
[j
].uuid
);
2175 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 2) * 4);
2176 ovsdb_atom_to_string(&datum
->keys
[j
], column
->type
.key
.type
,
2178 ds_put_char(&ctx
->output
, '=');
2180 const struct ovsdb_datum
*ref_datum
;
2182 ref_datum
= ovsdb_idl_read(ref_row
,
2183 ref_show
->name_column
);
2184 ovsdb_datum_to_string(ref_datum
,
2185 &ref_show
->name_column
->type
,
2188 ds_put_cstr(&ctx
->output
, "\"<null>\"");
2190 ds_put_char(&ctx
->output
, '\n');
2196 if (!ovsdb_datum_is_default(datum
, &column
->type
)) {
2197 ds_put_char_multiple(&ctx
->output
, ' ', (level
+ 1) * 4);
2198 ds_put_format(&ctx
->output
, "%s: ", column
->name
);
2199 ovsdb_datum_to_string(datum
, &column
->type
, &ctx
->output
);
2200 ds_put_char(&ctx
->output
, '\n');
2203 cmd_show_weak_ref(ctx
, show
, row
, level
);
2204 sset_find_and_delete_assert(shown
, show
->table
->name
);
2208 cmd_show(struct ctl_context
*ctx
)
2210 const struct ovsdb_idl_row
*row
;
2211 struct sset shown
= SSET_INITIALIZER(&shown
);
2213 for (row
= ovsdb_idl_first_row(ctx
->idl
, cmd_show_tables
[0].table
);
2214 row
; row
= ovsdb_idl_next_row(row
)) {
2215 cmd_show_row(ctx
, row
, 0, &shown
);
2218 ovs_assert(sset_is_empty(&shown
));
2219 sset_destroy(&shown
);
2223 /* Given pointer to dynamic array 'options_p', array's current size
2224 * 'allocated_options_p' and number of added options 'n_options_p',
2225 * adds all command options to the array. Enlarges the array if
2228 ctl_add_cmd_options(struct option
**options_p
, size_t *n_options_p
,
2229 size_t *allocated_options_p
, int opt_val
)
2232 const struct shash_node
*node
;
2233 size_t n_existing_options
= *n_options_p
;
2235 SHASH_FOR_EACH (node
, &all_commands
) {
2236 const struct ctl_command_syntax
*p
= node
->data
;
2238 if (p
->options
[0]) {
2239 char *save_ptr
= NULL
;
2243 s
= xstrdup(p
->options
);
2244 for (name
= strtok_r(s
, ",", &save_ptr
); name
!= NULL
;
2245 name
= strtok_r(NULL
, ",", &save_ptr
)) {
2246 ovs_assert(name
[0] == '-' && name
[1] == '-' && name
[2]);
2249 size_t n
= strcspn(name
, "=?");
2250 int has_arg
= (name
[n
] == '\0' ? no_argument
2251 : name
[n
] == '=' ? required_argument
2252 : optional_argument
);
2255 o
= find_option(name
, *options_p
, *n_options_p
);
2257 ovs_assert(o
- *options_p
>= n_existing_options
);
2258 ovs_assert(o
->has_arg
== has_arg
);
2260 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
2261 o
->name
= xstrdup(name
);
2262 o
->has_arg
= has_arg
;
2271 o
= add_option(options_p
, n_options_p
, allocated_options_p
);
2272 memset(o
, 0, sizeof *o
);
2275 /* Parses command-line input for commands. */
2277 ctl_parse_commands(int argc
, char *argv
[], struct shash
*local_options
,
2278 struct ctl_command
**commandsp
, size_t *n_commandsp
)
2280 struct ctl_command
*commands
;
2281 size_t n_commands
, allocated_commands
;
2286 n_commands
= allocated_commands
= 0;
2291 for (start
= i
= 0; i
<= argc
; i
++) {
2292 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
2294 if (n_commands
>= allocated_commands
) {
2295 struct ctl_command
*c
;
2297 commands
= x2nrealloc(commands
, &allocated_commands
,
2299 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2300 shash_moved(&c
->options
);
2303 error
= parse_command(i
- start
, &argv
[start
], local_options
,
2304 &commands
[n_commands
]);
2306 struct ctl_command
*c
;
2308 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
2309 shash_destroy_free_data(&c
->options
);
2317 } else if (!shash_is_empty(local_options
)) {
2318 return xstrdup("missing command name (use --help for help)");
2324 return xstrdup("missing command name (use --help for help)");
2326 *commandsp
= commands
;
2327 *n_commandsp
= n_commands
;
2331 /* Prints all registered commands. */
2333 ctl_print_commands(void)
2335 const struct shash_node
*node
;
2337 SHASH_FOR_EACH (node
, &all_commands
) {
2338 const struct ctl_command_syntax
*p
= node
->data
;
2339 char *options
= xstrdup(p
->options
);
2340 char *options_begin
= options
;
2343 for (item
= strsep(&options
, ","); item
!= NULL
;
2344 item
= strsep(&options
, ",")) {
2345 if (item
[0] != '\0') {
2346 printf("[%s] ", item
);
2349 printf(",%s,", p
->name
);
2350 print_command_arguments(p
);
2353 free(options_begin
);
2359 /* Given array of options 'options', prints them. */
2361 ctl_print_options(const struct option
*options
)
2363 for (; options
->name
; options
++) {
2364 const struct option
*o
= options
;
2366 printf("--%s%s\n", o
->name
, o
->has_arg
? "=ARG" : "");
2367 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
2368 printf("-%c%s\n", o
->val
, o
->has_arg
? " ARG" : "");
2375 /* Returns the default local database path. */
2377 ctl_default_db(void)
2381 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
2386 /* Returns true if it looks like this set of arguments might modify the
2387 * database, otherwise false. (Not very smart, so it's prone to false
2390 ctl_might_write_to_db(const struct ctl_command
*commands
, size_t n
)
2392 for (size_t i
= 0; i
< n
; i
++) {
2393 if (commands
[i
].syntax
->mode
== RW
) {
2400 /* Report an error while running in the command context. Caller should return
2401 * to its caller immediately after reporting the error. */
2403 ctl_error(struct ctl_context
*ctx
, const char *format
, ...)
2410 VLOG_ERR("Discarding unhandled error: %s", ctx
->error
);
2414 va_start(args
, format
);
2415 ctx
->error
= xvasprintf(format
, args
);
2420 ctl_fatal(const char *format
, ...)
2425 va_start(args
, format
);
2426 message
= xvasprintf(format
, args
);
2429 vlog_set_levels(&this_module
, VLF_CONSOLE
, VLL_OFF
);
2430 VLOG_ERR("%s", message
);
2431 ovs_error(0, "%s", message
);
2432 ctl_exit(EXIT_FAILURE
);
2435 /* Frees the current transaction and the underlying IDL and then calls
2438 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2439 * for a clean memory leak report from valgrind in the normal case. That makes
2440 * it easier to notice real memory leaks. */
2442 ctl_exit(int status
)
2444 if (ctl_exit_func
) {
2445 ctl_exit_func(status
);
2450 /* Comman database commands to be registered. */
2451 static const struct ctl_command_syntax db_ctl_commands
[] = {
2452 {"comment", 0, INT_MAX
, "[ARG]...", NULL
, NULL
, NULL
, "", RO
},
2453 {"get", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get
, cmd_get
,
2454 NULL
, "--if-exists,--id=", RO
},
2455 {"list", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_list
, cmd_list
, NULL
,
2456 "--if-exists,--columns=", RO
},
2457 {"find", 1, INT_MAX
, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find
,
2458 cmd_find
, NULL
, "--columns=", RO
},
2459 {"set", 3, INT_MAX
, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set
,
2460 cmd_set
, NULL
, "--if-exists", RW
},
2461 {"add", 4, INT_MAX
, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add
,
2462 cmd_add
, NULL
, "--if-exists", RW
},
2463 {"remove", 4, INT_MAX
, "TABLE RECORD COLUMN KEY|VALUE|KEY=VALUE...",
2464 pre_cmd_remove
, cmd_remove
, NULL
, "--if-exists", RW
},
2465 {"clear", 3, INT_MAX
, "TABLE RECORD COLUMN...", pre_cmd_clear
, cmd_clear
,
2466 NULL
, "--if-exists", RW
},
2467 {"create", 2, INT_MAX
, "TABLE COLUMN[:KEY]=VALUE...", pre_create
,
2468 cmd_create
, post_create
, "--id=", RW
},
2469 {"destroy", 1, INT_MAX
, "TABLE [RECORD]...", pre_cmd_destroy
, cmd_destroy
,
2470 NULL
, "--if-exists,--all", RW
},
2471 {"wait-until", 2, INT_MAX
, "TABLE RECORD [COLUMN[:KEY]=VALUE]...",
2472 pre_cmd_wait_until
, cmd_wait_until
, NULL
, "", RO
},
2473 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, RO
},
2477 ctl_register_command(const struct ctl_command_syntax
*command
)
2479 shash_add_assert(&all_commands
, command
->name
, command
);
2482 /* Registers commands represented by 'struct ctl_command_syntax's to
2483 * 'all_commands'. The last element of 'commands' must be an all-NULL
2486 ctl_register_commands(const struct ctl_command_syntax
*commands
)
2488 const struct ctl_command_syntax
*p
;
2490 for (p
= commands
; p
->name
; p
++) {
2491 ctl_register_command(p
);
2495 /* Registers the 'db_ctl_commands' to 'all_commands'. */
2497 ctl_init__(const struct ovsdb_idl_class
*idl_class_
,
2498 const struct ctl_table_class
*ctl_classes_
,
2499 const struct cmd_show_table cmd_show_tables_
[],
2500 void (*ctl_exit_func_
)(int status
))
2502 idl_class
= idl_class_
;
2503 idl_classes
= idl_class_
->tables
;
2504 ctl_classes
= ctl_classes_
;
2505 n_classes
= idl_class
->n_tables
;
2506 ctl_exit_func
= ctl_exit_func_
;
2507 ctl_register_commands(db_ctl_commands
);
2509 cmd_show_tables
= cmd_show_tables_
;
2510 if (cmd_show_tables
) {
2511 static const struct ctl_command_syntax show
=
2512 {"show", 0, 0, "", pre_cmd_show
, cmd_show
, NULL
, "", RO
};
2513 ctl_register_command(&show
);
2517 /* Returns the text for the database commands usage. */
2519 ctl_get_db_cmd_usage(void)
2521 return "Database commands:\n\
2522 list TBL [REC] list RECord (or all records) in TBL\n\
2523 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
2524 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
2525 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
2526 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
2527 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
2528 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
2529 create TBL COL[:KEY]=VALUE create and initialize new record\n\
2530 destroy TBL REC delete RECord from TBL\n\
2531 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
2532 Potentially unsafe database commands require --force option.\n";
2536 ctl_list_db_tables_usage(void)
2538 static struct ds s
= DS_EMPTY_INITIALIZER
;
2543 ds_put_cstr(&s
, "Database commands may reference a row in each table in the following ways:\n");
2544 for (int i
= 0; i
< n_classes
; i
++) {
2545 struct svec options
= SVEC_EMPTY_INITIALIZER
;
2547 svec_add(&options
, "by UUID");
2548 if (idl_classes
[i
].is_singleton
) {
2549 svec_add(&options
, "as \".\"");
2552 for (int j
= 0; j
< ARRAY_SIZE(ctl_classes
[i
].row_ids
); j
++) {
2553 const struct ctl_row_id
*id
= &ctl_classes
[i
].row_ids
[j
];
2554 if (!id
->name_column
) {
2558 struct ds o
= DS_EMPTY_INITIALIZER
;
2559 if (id
->uuid_column
) {
2560 ds_put_format(&o
, "via \"%s\"", id
->uuid_column
->name
);
2561 const struct ovsdb_idl_table_class
*referrer
2562 = ovsdb_idl_table_class_from_column(idl_class
,
2564 if (referrer
!= &idl_classes
[i
]) {
2565 ds_put_format(&o
, " of %s", referrer
->name
);
2568 ds_put_format(&o
, " with matching \"%s:%s\"",
2569 id
->name_column
->name
, id
->key
);
2571 ds_put_format(&o
, " with matching \"%s\"", id
->name_column
->name
);
2573 } else if (id
->key
) {
2574 ds_put_format(&o
, "by \"%s:%s\"", id
->name_column
->name
, id
->key
);
2576 ds_put_format(&o
, "by \"%s\"", id
->name_column
->name
);
2578 svec_add_nocopy(&options
, ds_steal_cstr(&o
));
2581 ds_put_format(&s
, " %s:", idl_classes
[i
].name
);
2582 for (int j
= 0; j
< options
.n
; j
++) {
2583 ds_put_format(&s
, "\n %s", options
.names
[j
]);
2585 ds_put_char(&s
, '\n');
2586 svec_destroy(&options
);
2592 /* Initializes 'ctx' from 'command'. */
2594 ctl_context_init_command(struct ctl_context
*ctx
,
2595 struct ctl_command
*command
)
2597 ctx
->argc
= command
->argc
;
2598 ctx
->argv
= command
->argv
;
2599 ctx
->options
= command
->options
;
2601 ds_swap(&ctx
->output
, &command
->output
);
2602 ctx
->table
= command
->table
;
2603 ctx
->try_again
= false;
2607 /* Initializes the entire 'ctx'. */
2609 ctl_context_init(struct ctl_context
*ctx
, struct ctl_command
*command
,
2610 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
2611 struct ovsdb_symbol_table
*symtab
,
2612 void (*invalidate_cache_cb
)(struct ctl_context
*))
2615 ctl_context_init_command(ctx
, command
);
2619 ctx
->symtab
= symtab
;
2620 ctx
->invalidate_cache_cb
= invalidate_cache_cb
;
2623 /* Completes processing of 'command' within 'ctx'. */
2625 ctl_context_done_command(struct ctl_context
*ctx
,
2626 struct ctl_command
*command
)
2628 ds_swap(&ctx
->output
, &command
->output
);
2629 command
->table
= ctx
->table
;
2634 /* Finishes up with 'ctx'.
2636 * If command is nonnull, first calls ctl_context_done_command() to complete
2637 * processing that command within 'ctx'. */
2639 ctl_context_done(struct ctl_context
*ctx
,
2640 struct ctl_command
*command
)
2643 ctl_context_done_command(ctx
, command
);
2645 invalidate_cache(ctx
);
2648 char * OVS_WARN_UNUSED_RESULT
2649 ctl_set_column(const char *table_name
, const struct ovsdb_idl_row
*row
,
2650 const char *arg
, struct ovsdb_symbol_table
*symtab
)
2652 const struct ovsdb_idl_table_class
*table
;
2655 error
= get_table(table_name
, &table
);
2659 error
= set_column(table
, row
, arg
, symtab
);