10884b4268f7a7e99b0d255f3dfcaef4002d3e60
[cascardo/ovs.git] / lib / db-ctl-base.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <getopt.h>
21 #include <unistd.h>
22
23 #include "db-ctl-base.h"
24
25 #include "command-line.h"
26 #include "compiler.h"
27 #include "dirs.h"
28 #include "dynamic-string.h"
29 #include "fatal-signal.h"
30 #include "hash.h"
31 #include "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 "shash.h"
37 #include "string.h"
38 #include "table.h"
39 #include "util.h"
40
41 VLOG_DEFINE_THIS_MODULE(db_ctl_base);
42
43 /* The IDL we're using and the current transaction, if any.
44  * This is for use by ctl_exit() only, to allow it to clean up.
45  * Other code should use its context arguments. */
46 struct ovsdb_idl *the_idl;
47 struct ovsdb_idl_txn *the_idl_txn;
48
49 static struct shash all_commands = SHASH_INITIALIZER(&all_commands);
50
51 \f
52 static struct option *
53 find_option(const char *name, struct option *options, size_t n_options)
54 {
55     size_t i;
56
57     for (i = 0; i < n_options; i++) {
58         if (!strcmp(options[i].name, name)) {
59             return &options[i];
60         }
61     }
62     return NULL;
63 }
64
65 static struct option *
66 add_option(struct option **optionsp, size_t *n_optionsp,
67            size_t *allocated_optionsp)
68 {
69     if (*n_optionsp >= *allocated_optionsp) {
70         *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
71                                sizeof **optionsp);
72     }
73     return &(*optionsp)[(*n_optionsp)++];
74 }
75
76 /* Converts the command arguments into format that can be parsed by
77  * bash completion script.
78  *
79  * Therein, arguments will be attached with following prefixes:
80  *
81  *    !argument :: The argument is required
82  *    ?argument :: The argument is optional
83  *    *argument :: The argument may appear any number (0 or more) times
84  *    +argument :: The argument may appear one or more times
85  *
86  */
87 static void
88 print_command_arguments(const struct ctl_command_syntax *command)
89 {
90     /*
91      * The argument string is parsed in reverse.  We use a stack 'oew_stack' to
92      * keep track of nested optionals.  Whenever a ']' is encountered, we push
93      * a bit to 'oew_stack'.  The bit is set to 1 if the ']' is not nested.
94      * Subsequently, we pop an entry everytime '[' is met.
95      *
96      * We use 'whole_word_is_optional' value to decide whether or not a ! or +
97      * should be added on encountering a space: if the optional surrounds the
98      * whole word then it shouldn't be, but if it is only a part of the word
99      * (i.e. [key=]value), it should be.
100      */
101     uint32_t oew_stack = 0;
102
103     const char *arguments = command->arguments;
104     int length = strlen(arguments);
105     if (!length) {
106         return;
107     }
108
109     /* Output buffer, written backward from end. */
110     char *output = xmalloc(2 * length);
111     char *outp = output + 2 * length;
112     *--outp = '\0';
113
114     bool in_repeated = false;
115     bool whole_word_is_optional = false;
116
117     for (const char *inp = arguments + length; inp > arguments; ) {
118         switch (*--inp) {
119         case ']':
120             oew_stack <<= 1;
121             if (inp[1] == '\0' || inp[1] == ' ' || inp[1] == '.') {
122                 oew_stack |= 1;
123             }
124             break;
125         case '[':
126             /* Checks if the whole word is optional, and sets the
127              * 'whole_word_is_optional' accordingly. */
128             if ((inp == arguments || inp[-1] == ' ') && oew_stack & 1) {
129                 *--outp = in_repeated ? '*' : '?';
130                 whole_word_is_optional = true;
131             } else {
132                 *--outp = '?';
133                 whole_word_is_optional = false;
134             }
135             oew_stack >>= 1;
136             break;
137         case ' ':
138             if (!whole_word_is_optional) {
139                 *--outp = in_repeated ? '+' : '!';
140             }
141             *--outp = ' ';
142             in_repeated = false;
143             whole_word_is_optional = false;
144             break;
145         case '.':
146             in_repeated = true;
147             break;
148         default:
149             *--outp = *inp;
150             break;
151         }
152     }
153     if (arguments[0] != '[' && outp != output + 2 * length - 1) {
154         *--outp = in_repeated ? '+' : '!';
155     }
156     printf("%s", outp);
157     free(output);
158 }
159
160 static void
161 die_if_error(char *error)
162 {
163     if (error) {
164         ctl_fatal("%s", error);
165     }
166 }
167
168 static int
169 to_lower_and_underscores(unsigned c)
170 {
171     return c == '-' ? '_' : tolower(c);
172 }
173
174 static unsigned int
175 score_partial_match(const char *name, const char *s)
176 {
177     int score;
178
179     if (!strcmp(name, s)) {
180         return UINT_MAX;
181     }
182     for (score = 0; ; score++, name++, s++) {
183         if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
184             break;
185         } else if (*name == '\0') {
186             return UINT_MAX - 1;
187         }
188     }
189     return *s == '\0' ? score : 0;
190 }
191
192 static struct ovsdb_symbol *
193 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
194 {
195     struct ovsdb_symbol *symbol;
196
197     if (id[0] != '@') {
198         ctl_fatal("row id \"%s\" does not begin with \"@\"", id);
199     }
200
201     if (newp) {
202         *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
203     }
204
205     symbol = ovsdb_symbol_table_insert(symtab, id);
206     if (symbol->created) {
207         ctl_fatal("row id \"%s\" may only be specified on one --id option",
208                   id);
209     }
210     symbol->created = true;
211     return symbol;
212 }
213
214 static const struct ovsdb_idl_row *
215 get_row_by_id(struct ctl_context *ctx, const struct ctl_table_class *table,
216               const struct ctl_row_id *id, const char *record_id)
217 {
218     const struct ovsdb_idl_row *referrer, *final;
219
220     if (!id->table) {
221         return NULL;
222     }
223
224     if (!id->name_column) {
225         if (strcmp(record_id, ".")) {
226             return NULL;
227         }
228         referrer = ovsdb_idl_first_row(ctx->idl, id->table);
229         if (!referrer || ovsdb_idl_next_row(referrer)) {
230             return NULL;
231         }
232     } else {
233         const struct ovsdb_idl_row *row;
234
235         referrer = NULL;
236         for (row = ovsdb_idl_first_row(ctx->idl, id->table);
237              row != NULL;
238              row = ovsdb_idl_next_row(row))
239             {
240                 const struct ovsdb_datum *name;
241
242                 name = ovsdb_idl_get(row, id->name_column,
243                                      OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
244                 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
245                     if (referrer) {
246                         ctl_fatal("multiple rows in %s match \"%s\"",
247                                   table->class->name, record_id);
248                     }
249                     referrer = row;
250                 }
251             }
252     }
253     if (!referrer) {
254         return NULL;
255     }
256
257     final = NULL;
258     if (id->uuid_column) {
259         const struct ovsdb_datum *uuid;
260
261         ovsdb_idl_txn_verify(referrer, id->uuid_column);
262         uuid = ovsdb_idl_get(referrer, id->uuid_column,
263                              OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
264         if (uuid->n == 1) {
265             final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
266                                                &uuid->keys[0].uuid);
267         }
268     } else {
269         final = referrer;
270     }
271
272     return final;
273 }
274
275 static const struct ovsdb_idl_row *
276 get_row(struct ctl_context *ctx,
277         const struct ctl_table_class *table, const char *record_id,
278         bool must_exist)
279 {
280     const struct ovsdb_idl_row *row;
281     struct uuid uuid;
282
283     row = NULL;
284     if (uuid_from_string(&uuid, record_id)) {
285         row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
286     }
287     if (!row) {
288         int i;
289
290         for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
291             row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
292             if (row) {
293                 break;
294             }
295         }
296     }
297     if (must_exist && !row) {
298         ctl_fatal("no row \"%s\" in table %s",
299                   record_id, table->class->name);
300     }
301     return row;
302 }
303
304 static char *
305 get_column(const struct ctl_table_class *table, const char *column_name,
306            const struct ovsdb_idl_column **columnp)
307 {
308     const struct ovsdb_idl_column *best_match = NULL;
309     unsigned int best_score = 0;
310     size_t i;
311
312     for (i = 0; i < table->class->n_columns; i++) {
313         const struct ovsdb_idl_column *column = &table->class->columns[i];
314         unsigned int score = score_partial_match(column->name, column_name);
315         if (score > best_score) {
316             best_match = column;
317             best_score = score;
318         } else if (score == best_score) {
319             best_match = NULL;
320         }
321     }
322
323     *columnp = best_match;
324     if (best_match) {
325         return NULL;
326     } else if (best_score) {
327         return xasprintf("%s contains more than one column whose name "
328                          "matches \"%s\"", table->class->name, column_name);
329     } else {
330         return xasprintf("%s does not contain a column whose name matches "
331                          "\"%s\"", table->class->name, column_name);
332     }
333 }
334
335 static void
336 pre_get_column(struct ctl_context *ctx,
337                const struct ctl_table_class *table, const char *column_name,
338                const struct ovsdb_idl_column **columnp)
339 {
340     die_if_error(get_column(table, column_name, columnp));
341     ovsdb_idl_add_column(ctx->idl, *columnp);
342 }
343
344 static const struct ctl_table_class *
345 pre_get_table(struct ctl_context *ctx, const char *table_name)
346 {
347     const struct ctl_table_class *table_class;
348     int i;
349
350     table_class = get_table(table_name);
351     ovsdb_idl_add_table(ctx->idl, table_class->class);
352
353     for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
354         const struct ctl_row_id *id = &table_class->row_ids[i];
355         if (id->table) {
356             ovsdb_idl_add_table(ctx->idl, id->table);
357         }
358         if (id->name_column) {
359             ovsdb_idl_add_column(ctx->idl, id->name_column);
360         }
361         if (id->uuid_column) {
362             ovsdb_idl_add_column(ctx->idl, id->uuid_column);
363         }
364     }
365
366     return table_class;
367 }
368
369 static char *
370 missing_operator_error(const char *arg, const char **allowed_operators,
371                        size_t n_allowed)
372 {
373     struct ds s;
374
375     ds_init(&s);
376     ds_put_format(&s, "%s: argument does not end in ", arg);
377     ds_put_format(&s, "\"%s\"", allowed_operators[0]);
378     if (n_allowed == 2) {
379         ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
380     } else if (n_allowed > 2) {
381         size_t i;
382
383         for (i = 1; i < n_allowed - 1; i++) {
384             ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
385         }
386         ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
387     }
388     ds_put_format(&s, " followed by a value.");
389
390     return ds_steal_cstr(&s);
391 }
392
393 /* Breaks 'arg' apart into a number of fields in the following order:
394  *
395  *      - The name of a column in 'table', stored into '*columnp'.  The column
396  *        name may be abbreviated.
397  *
398  *      - Optionally ':' followed by a key string.  The key is stored as a
399  *        malloc()'d string into '*keyp', or NULL if no key is present in
400  *        'arg'.
401  *
402  *      - If 'valuep' is nonnull, an operator followed by a value string.  The
403  *        allowed operators are the 'n_allowed' string in 'allowed_operators',
404  *        or just "=" if 'n_allowed' is 0.  If 'operatorp' is nonnull, then the
405  *        index of the operator within 'allowed_operators' is stored into
406  *        '*operatorp'.  The value is stored as a malloc()'d string into
407  *        '*valuep', or NULL if no value is present in 'arg'.
408  *
409  * On success, returns NULL.  On failure, returned a malloc()'d string error
410  * message and stores NULL into all of the nonnull output arguments. */
411 static char * OVS_WARN_UNUSED_RESULT
412 parse_column_key_value(const char *arg,
413                        const struct ctl_table_class *table,
414                        const struct ovsdb_idl_column **columnp, char **keyp,
415                        int *operatorp,
416                        const char **allowed_operators, size_t n_allowed,
417                        char **valuep)
418 {
419     const char *p = arg;
420     char *column_name;
421     char *error;
422
423     ovs_assert(!(operatorp && !valuep));
424     *keyp = NULL;
425     if (valuep) {
426         *valuep = NULL;
427     }
428
429     /* Parse column name. */
430     error = ovsdb_token_parse(&p, &column_name);
431     if (error) {
432         goto error;
433     }
434     if (column_name[0] == '\0') {
435         free(column_name);
436         error = xasprintf("%s: missing column name", arg);
437         goto error;
438     }
439     error = get_column(table, column_name, columnp);
440     free(column_name);
441     if (error) {
442         goto error;
443     }
444
445     /* Parse key string. */
446     if (*p == ':') {
447         p++;
448         error = ovsdb_token_parse(&p, keyp);
449         if (error) {
450             goto error;
451         }
452     }
453
454     /* Parse value string. */
455     if (valuep) {
456         size_t best_len;
457         size_t i;
458         int best;
459
460         if (!allowed_operators) {
461             static const char *equals = "=";
462             allowed_operators = &equals;
463             n_allowed = 1;
464         }
465
466         best = -1;
467         best_len = 0;
468         for (i = 0; i < n_allowed; i++) {
469             const char *op = allowed_operators[i];
470             size_t op_len = strlen(op);
471
472             if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
473                 best_len = op_len;
474                 best = i;
475             }
476         }
477         if (best < 0) {
478             error = missing_operator_error(arg, allowed_operators, n_allowed);
479             goto error;
480         }
481
482         if (operatorp) {
483             *operatorp = best;
484         }
485         *valuep = xstrdup(p + best_len);
486     } else {
487         if (*p != '\0') {
488             error = xasprintf("%s: trailing garbage \"%s\" in argument",
489                               arg, p);
490             goto error;
491         }
492     }
493     return NULL;
494
495  error:
496     *columnp = NULL;
497     free(*keyp);
498     *keyp = NULL;
499     if (valuep) {
500         free(*valuep);
501         *valuep = NULL;
502         if (operatorp) {
503             *operatorp = -1;
504         }
505     }
506     return error;
507 }
508
509 static const struct ovsdb_idl_column *
510 pre_parse_column_key_value(struct ctl_context *ctx,
511                            const char *arg,
512                            const struct ctl_table_class *table)
513 {
514     const struct ovsdb_idl_column *column;
515     const char *p;
516     char *column_name;
517
518     p = arg;
519     die_if_error(ovsdb_token_parse(&p, &column_name));
520     if (column_name[0] == '\0') {
521         ctl_fatal("%s: missing column name", arg);
522     }
523
524     pre_get_column(ctx, table, column_name, &column);
525     free(column_name);
526
527     return column;
528 }
529
530 static void
531 check_mutable(const struct ovsdb_idl_row *row,
532               const struct ovsdb_idl_column *column)
533 {
534     if (!ovsdb_idl_is_mutable(row, column)) {
535         ctl_fatal("cannot modify read-only column %s in table %s",
536                   column->name, row->table->class->name);
537     }
538 }
539
540 #define RELOPS                                  \
541     RELOP(RELOP_EQ,     "=")                    \
542     RELOP(RELOP_NE,     "!=")                   \
543     RELOP(RELOP_LT,     "<")                    \
544     RELOP(RELOP_GT,     ">")                    \
545     RELOP(RELOP_LE,     "<=")                   \
546     RELOP(RELOP_GE,     ">=")                   \
547     RELOP(RELOP_SET_EQ, "{=}")                  \
548     RELOP(RELOP_SET_NE, "{!=}")                 \
549     RELOP(RELOP_SET_LT, "{<}")                  \
550     RELOP(RELOP_SET_GT, "{>}")                  \
551     RELOP(RELOP_SET_LE, "{<=}")                 \
552     RELOP(RELOP_SET_GE, "{>=}")
553
554 enum relop {
555 #define RELOP(ENUM, STRING) ENUM,
556     RELOPS
557 #undef RELOP
558 };
559
560 static bool
561 is_set_operator(enum relop op)
562 {
563     return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
564             op == RELOP_SET_LT || op == RELOP_SET_GT ||
565             op == RELOP_SET_LE || op == RELOP_SET_GE);
566 }
567
568 static bool
569 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
570                const struct ovsdb_type *type, enum relop op)
571 {
572     switch (op) {
573     case RELOP_EQ:
574     case RELOP_SET_EQ:
575         return ovsdb_datum_compare_3way(a, b, type) == 0;
576     case RELOP_NE:
577     case RELOP_SET_NE:
578         return ovsdb_datum_compare_3way(a, b, type) != 0;
579     case RELOP_LT:
580         return ovsdb_datum_compare_3way(a, b, type) < 0;
581     case RELOP_GT:
582         return ovsdb_datum_compare_3way(a, b, type) > 0;
583     case RELOP_LE:
584         return ovsdb_datum_compare_3way(a, b, type) <= 0;
585     case RELOP_GE:
586         return ovsdb_datum_compare_3way(a, b, type) >= 0;
587
588     case RELOP_SET_LT:
589         return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
590     case RELOP_SET_GT:
591         return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
592     case RELOP_SET_LE:
593         return ovsdb_datum_includes_all(a, b, type);
594     case RELOP_SET_GE:
595         return ovsdb_datum_includes_all(b, a, type);
596
597     default:
598         OVS_NOT_REACHED();
599     }
600 }
601
602 static bool
603 is_condition_satisfied(const struct ctl_table_class *table,
604                        const struct ovsdb_idl_row *row, const char *arg,
605                        struct ovsdb_symbol_table *symtab)
606 {
607     static const char *operators[] = {
608 #define RELOP(ENUM, STRING) STRING,
609         RELOPS
610 #undef RELOP
611     };
612
613     const struct ovsdb_idl_column *column;
614     const struct ovsdb_datum *have_datum;
615     char *key_string, *value_string;
616     struct ovsdb_type type;
617     int operator;
618     bool retval;
619     char *error;
620
621     error = parse_column_key_value(arg, table, &column, &key_string,
622                                    &operator, operators, ARRAY_SIZE(operators),
623                                    &value_string);
624     die_if_error(error);
625     if (!value_string) {
626         ctl_fatal("%s: missing value", arg);
627     }
628
629     type = column->type;
630     type.n_max = UINT_MAX;
631
632     have_datum = ovsdb_idl_read(row, column);
633     if (key_string) {
634         union ovsdb_atom want_key;
635         struct ovsdb_datum b;
636         unsigned int idx;
637
638         if (column->type.value.type == OVSDB_TYPE_VOID) {
639             ctl_fatal("cannot specify key to check for non-map column %s",
640                       column->name);
641         }
642
643         die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
644                                             key_string, symtab));
645
646         type.key = type.value;
647         type.value.type = OVSDB_TYPE_VOID;
648         die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
649
650         idx = ovsdb_datum_find_key(have_datum,
651                                    &want_key, column->type.key.type);
652         if (idx == UINT_MAX && !is_set_operator(operator)) {
653             retval = false;
654         } else {
655             struct ovsdb_datum a;
656
657             if (idx != UINT_MAX) {
658                 a.n = 1;
659                 a.keys = &have_datum->values[idx];
660                 a.values = NULL;
661             } else {
662                 a.n = 0;
663                 a.keys = NULL;
664                 a.values = NULL;
665             }
666
667             retval = evaluate_relop(&a, &b, &type, operator);
668         }
669
670         ovsdb_atom_destroy(&want_key, column->type.key.type);
671         ovsdb_datum_destroy(&b, &type);
672     } else {
673         struct ovsdb_datum want_datum;
674
675         die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
676                                              value_string, symtab));
677         retval = evaluate_relop(have_datum, &want_datum, &type, operator);
678         ovsdb_datum_destroy(&want_datum, &column->type);
679     }
680
681     free(key_string);
682     free(value_string);
683
684     return retval;
685 }
686
687 static void
688 invalidate_cache(struct ctl_context *ctx)
689 {
690     if (ctx->invalidate_cache) {
691         (ctx->invalidate_cache)(ctx);
692     }
693 }
694 \f
695 static void
696 pre_cmd_get(struct ctl_context *ctx)
697 {
698     const char *id = shash_find_data(&ctx->options, "--id");
699     const char *table_name = ctx->argv[1];
700     const struct ctl_table_class *table;
701     int i;
702
703     /* Using "get" without --id or a column name could possibly make sense.
704      * Maybe, for example, a *ctl command run wants to assert that a row
705      * exists.  But it is unlikely that an interactive user would want to do
706      * that, so issue a warning if we're running on a terminal. */
707     if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
708         VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
709                   "possibly erroneous");
710     }
711
712     table = pre_get_table(ctx, table_name);
713     for (i = 3; i < ctx->argc; i++) {
714         if (!strcasecmp(ctx->argv[i], "_uuid")
715             || !strcasecmp(ctx->argv[i], "-uuid")) {
716             continue;
717         }
718
719         pre_parse_column_key_value(ctx, ctx->argv[i], table);
720     }
721 }
722
723 static void
724 cmd_get(struct ctl_context *ctx)
725 {
726     const char *id = shash_find_data(&ctx->options, "--id");
727     bool must_exist = !shash_find(&ctx->options, "--if-exists");
728     const char *table_name = ctx->argv[1];
729     const char *record_id = ctx->argv[2];
730     const struct ctl_table_class *table;
731     const struct ovsdb_idl_row *row;
732     struct ds *out = &ctx->output;
733     int i;
734
735     if (id && !must_exist) {
736         ctl_fatal("--if-exists and --id may not be specified together");
737     }
738
739     table = get_table(table_name);
740     row = get_row(ctx, table, record_id, must_exist);
741     if (!row) {
742         return;
743     }
744
745     if (id) {
746         struct ovsdb_symbol *symbol;
747         bool new;
748
749         symbol = create_symbol(ctx->symtab, id, &new);
750         if (!new) {
751             ctl_fatal("row id \"%s\" specified on \"get\" command was used "
752                       "before it was defined", id);
753         }
754         symbol->uuid = row->uuid;
755
756         /* This symbol refers to a row that already exists, so disable warnings
757          * about it being unreferenced. */
758         symbol->strong_ref = true;
759     }
760     for (i = 3; i < ctx->argc; i++) {
761         const struct ovsdb_idl_column *column;
762         const struct ovsdb_datum *datum;
763         char *key_string;
764
765         /* Special case for obtaining the UUID of a row.  We can't just do this
766          * through parse_column_key_value() below since it returns a "struct
767          * ovsdb_idl_column" and the UUID column doesn't have one. */
768         if (!strcasecmp(ctx->argv[i], "_uuid")
769             || !strcasecmp(ctx->argv[i], "-uuid")) {
770             ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
771             continue;
772         }
773
774         die_if_error(parse_column_key_value(ctx->argv[i], table,
775                                             &column, &key_string,
776                                             NULL, NULL, 0, NULL));
777
778         ovsdb_idl_txn_verify(row, column);
779         datum = ovsdb_idl_read(row, column);
780         if (key_string) {
781             union ovsdb_atom key;
782             unsigned int idx;
783
784             if (column->type.value.type == OVSDB_TYPE_VOID) {
785                 ctl_fatal("cannot specify key to get for non-map column %s",
786                           column->name);
787             }
788
789             die_if_error(ovsdb_atom_from_string(&key,
790                                                 &column->type.key,
791                                                 key_string, ctx->symtab));
792
793             idx = ovsdb_datum_find_key(datum, &key,
794                                        column->type.key.type);
795             if (idx == UINT_MAX) {
796                 if (must_exist) {
797                     ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
798                               key_string, table->class->name, record_id,
799                               column->name);
800                 }
801             } else {
802                 ovsdb_atom_to_string(&datum->values[idx],
803                                      column->type.value.type, out);
804             }
805             ovsdb_atom_destroy(&key, column->type.key.type);
806         } else {
807             ovsdb_datum_to_string(datum, &column->type, out);
808         }
809         ds_put_char(out, '\n');
810
811         free(key_string);
812     }
813 }
814
815 static void
816 parse_column_names(const char *column_names,
817                    const struct ctl_table_class *table,
818                    const struct ovsdb_idl_column ***columnsp,
819                    size_t *n_columnsp)
820 {
821     const struct ovsdb_idl_column **columns;
822     size_t n_columns;
823
824     if (!column_names) {
825         size_t i;
826
827         n_columns = table->class->n_columns + 1;
828         columns = xmalloc(n_columns * sizeof *columns);
829         columns[0] = NULL;
830         for (i = 0; i < table->class->n_columns; i++) {
831             columns[i + 1] = &table->class->columns[i];
832         }
833     } else {
834         char *s = xstrdup(column_names);
835         size_t allocated_columns;
836         char *save_ptr = NULL;
837         char *column_name;
838
839         columns = NULL;
840         allocated_columns = n_columns = 0;
841         for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
842              column_name = strtok_r(NULL, ", ", &save_ptr)) {
843             const struct ovsdb_idl_column *column;
844
845             if (!strcasecmp(column_name, "_uuid")) {
846                 column = NULL;
847             } else {
848                 die_if_error(get_column(table, column_name, &column));
849             }
850             if (n_columns >= allocated_columns) {
851                 columns = x2nrealloc(columns, &allocated_columns,
852                                      sizeof *columns);
853             }
854             columns[n_columns++] = column;
855         }
856         free(s);
857
858         if (!n_columns) {
859             ctl_fatal("must specify at least one column name");
860         }
861     }
862     *columnsp = columns;
863     *n_columnsp = n_columns;
864 }
865
866 static void
867 pre_list_columns(struct ctl_context *ctx,
868                  const struct ctl_table_class *table,
869                  const char *column_names)
870 {
871     const struct ovsdb_idl_column **columns;
872     size_t n_columns;
873     size_t i;
874
875     parse_column_names(column_names, table, &columns, &n_columns);
876     for (i = 0; i < n_columns; i++) {
877         if (columns[i]) {
878             ovsdb_idl_add_column(ctx->idl, columns[i]);
879         }
880     }
881     free(columns);
882 }
883
884 static void
885 pre_cmd_list(struct ctl_context *ctx)
886 {
887     const char *column_names = shash_find_data(&ctx->options, "--columns");
888     const char *table_name = ctx->argv[1];
889     const struct ctl_table_class *table;
890
891     table = pre_get_table(ctx, table_name);
892     pre_list_columns(ctx, table, column_names);
893 }
894
895 static struct table *
896 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
897 {
898     struct table *out;
899     size_t i;
900
901     out = xmalloc(sizeof *out);
902     table_init(out);
903
904     for (i = 0; i < n_columns; i++) {
905         const struct ovsdb_idl_column *column = columns[i];
906         const char *column_name = column ? column->name : "_uuid";
907
908         table_add_column(out, "%s", column_name);
909     }
910
911     return out;
912 }
913
914 static void
915 list_record(const struct ovsdb_idl_row *row,
916             const struct ovsdb_idl_column **columns, size_t n_columns,
917             struct table *out)
918 {
919     size_t i;
920
921     if (!row) {
922         return;
923     }
924
925     table_add_row(out);
926     for (i = 0; i < n_columns; i++) {
927         const struct ovsdb_idl_column *column = columns[i];
928         struct cell *cell = table_add_cell(out);
929
930         if (!column) {
931             struct ovsdb_datum datum;
932             union ovsdb_atom atom;
933
934             atom.uuid = row->uuid;
935
936             datum.keys = &atom;
937             datum.values = NULL;
938             datum.n = 1;
939
940             cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
941             cell->type = &ovsdb_type_uuid;
942         } else {
943             const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
944
945             cell->json = ovsdb_datum_to_json(datum, &column->type);
946             cell->type = &column->type;
947         }
948     }
949 }
950
951 static void
952 cmd_list(struct ctl_context *ctx)
953 {
954     const char *column_names = shash_find_data(&ctx->options, "--columns");
955     bool must_exist = !shash_find(&ctx->options, "--if-exists");
956     const struct ovsdb_idl_column **columns;
957     const char *table_name = ctx->argv[1];
958     const struct ctl_table_class *table;
959     struct table *out;
960     size_t n_columns;
961     int i;
962
963     table = get_table(table_name);
964     parse_column_names(column_names, table, &columns, &n_columns);
965     out = ctx->table = list_make_table(columns, n_columns);
966     if (ctx->argc > 2) {
967         for (i = 2; i < ctx->argc; i++) {
968             list_record(get_row(ctx, table, ctx->argv[i], must_exist),
969                         columns, n_columns, out);
970         }
971     } else {
972         const struct ovsdb_idl_row *row;
973
974         for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
975              row = ovsdb_idl_next_row(row)) {
976             list_record(row, columns, n_columns, out);
977         }
978     }
979     free(columns);
980 }
981
982 static void
983 pre_cmd_find(struct ctl_context *ctx)
984 {
985     const char *column_names = shash_find_data(&ctx->options, "--columns");
986     const char *table_name = ctx->argv[1];
987     const struct ctl_table_class *table;
988     int i;
989
990     table = pre_get_table(ctx, table_name);
991     pre_list_columns(ctx, table, column_names);
992     for (i = 2; i < ctx->argc; i++) {
993         pre_parse_column_key_value(ctx, ctx->argv[i], table);
994     }
995 }
996
997 static void
998 cmd_find(struct ctl_context *ctx)
999 {
1000     const char *column_names = shash_find_data(&ctx->options, "--columns");
1001     const struct ovsdb_idl_column **columns;
1002     const char *table_name = ctx->argv[1];
1003     const struct ctl_table_class *table;
1004     const struct ovsdb_idl_row *row;
1005     struct table *out;
1006     size_t n_columns;
1007
1008     table = get_table(table_name);
1009     parse_column_names(column_names, table, &columns, &n_columns);
1010     out = ctx->table = list_make_table(columns, n_columns);
1011     for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
1012          row = ovsdb_idl_next_row(row)) {
1013         int i;
1014
1015         for (i = 2; i < ctx->argc; i++) {
1016             if (!is_condition_satisfied(table, row, ctx->argv[i],
1017                                         ctx->symtab)) {
1018                 goto next_row;
1019             }
1020         }
1021         list_record(row, columns, n_columns, out);
1022
1023     next_row: ;
1024     }
1025     free(columns);
1026 }
1027
1028 static void
1029 pre_cmd_set(struct ctl_context *ctx)
1030 {
1031     const char *table_name = ctx->argv[1];
1032     const struct ctl_table_class *table;
1033     int i;
1034
1035     table = pre_get_table(ctx, table_name);
1036     for (i = 3; i < ctx->argc; i++) {
1037         pre_parse_column_key_value(ctx, ctx->argv[i], table);
1038     }
1039 }
1040
1041 static void
1042 cmd_set(struct ctl_context *ctx)
1043 {
1044     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1045     const char *table_name = ctx->argv[1];
1046     const char *record_id = ctx->argv[2];
1047     const struct ctl_table_class*table;
1048     const struct ovsdb_idl_row *row;
1049     int i;
1050
1051     table = get_table(table_name);
1052     row = get_row(ctx, table, record_id, must_exist);
1053     if (!row) {
1054         return;
1055     }
1056
1057     for (i = 3; i < ctx->argc; i++) {
1058         set_column(table, row, ctx->argv[i], ctx->symtab);
1059     }
1060
1061     invalidate_cache(ctx);
1062 }
1063
1064 static void
1065 pre_cmd_add(struct ctl_context *ctx)
1066 {
1067     const char *table_name = ctx->argv[1];
1068     const char *column_name = ctx->argv[3];
1069     const struct ctl_table_class *table;
1070     const struct ovsdb_idl_column *column;
1071
1072     table = pre_get_table(ctx, table_name);
1073     pre_get_column(ctx, table, column_name, &column);
1074 }
1075
1076 static void
1077 cmd_add(struct ctl_context *ctx)
1078 {
1079     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1080     const char *table_name = ctx->argv[1];
1081     const char *record_id = ctx->argv[2];
1082     const char *column_name = ctx->argv[3];
1083     const struct ctl_table_class *table;
1084     const struct ovsdb_idl_column *column;
1085     const struct ovsdb_idl_row *row;
1086     const struct ovsdb_type *type;
1087     struct ovsdb_datum old;
1088     int i;
1089
1090     table = get_table(table_name);
1091     die_if_error(get_column(table, column_name, &column));
1092     row = get_row(ctx, table, record_id, must_exist);
1093     if (!row) {
1094         return;
1095     }
1096     check_mutable(row, column);
1097
1098     type = &column->type;
1099     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
1100     for (i = 4; i < ctx->argc; i++) {
1101         struct ovsdb_type add_type;
1102         struct ovsdb_datum add;
1103
1104         add_type = *type;
1105         add_type.n_min = 1;
1106         add_type.n_max = UINT_MAX;
1107         die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
1108                                              ctx->symtab));
1109         ovsdb_datum_union(&old, &add, type, false);
1110         ovsdb_datum_destroy(&add, type);
1111     }
1112     if (old.n > type->n_max) {
1113         ctl_fatal("\"add\" operation would put %u %s in column %s of "
1114                   "table %s but the maximum number is %u",
1115                   old.n,
1116                   type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
1117                   column->name, table->class->name, type->n_max);
1118     }
1119     ovsdb_idl_txn_verify(row, column);
1120     ovsdb_idl_txn_write(row, column, &old);
1121
1122     invalidate_cache(ctx);
1123 }
1124
1125 static void
1126 pre_cmd_remove(struct ctl_context *ctx)
1127 {
1128     const char *table_name = ctx->argv[1];
1129     const char *column_name = ctx->argv[3];
1130     const struct ctl_table_class *table;
1131     const struct ovsdb_idl_column *column;
1132
1133     table = pre_get_table(ctx, table_name);
1134     pre_get_column(ctx, table, column_name, &column);
1135 }
1136
1137 static void
1138 cmd_remove(struct ctl_context *ctx)
1139 {
1140     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1141     const char *table_name = ctx->argv[1];
1142     const char *record_id = ctx->argv[2];
1143     const char *column_name = ctx->argv[3];
1144     const struct ctl_table_class *table;
1145     const struct ovsdb_idl_column *column;
1146     const struct ovsdb_idl_row *row;
1147     const struct ovsdb_type *type;
1148     struct ovsdb_datum old;
1149     int i;
1150
1151     table = get_table(table_name);
1152     die_if_error(get_column(table, column_name, &column));
1153     row = get_row(ctx, table, record_id, must_exist);
1154     if (!row) {
1155         return;
1156     }
1157     check_mutable(row, column);
1158
1159     type = &column->type;
1160     ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
1161     for (i = 4; i < ctx->argc; i++) {
1162         struct ovsdb_type rm_type;
1163         struct ovsdb_datum rm;
1164         char *error;
1165
1166         rm_type = *type;
1167         rm_type.n_min = 1;
1168         rm_type.n_max = UINT_MAX;
1169         error = ovsdb_datum_from_string(&rm, &rm_type,
1170                                         ctx->argv[i], ctx->symtab);
1171
1172         if (error) {
1173             if (ovsdb_type_is_map(&rm_type)) {
1174                 rm_type.value.type = OVSDB_TYPE_VOID;
1175                 free(error);
1176                 die_if_error(ovsdb_datum_from_string(
1177                                                      &rm, &rm_type, ctx->argv[i], ctx->symtab));
1178             } else {
1179                 ctl_fatal("%s", error);
1180             }
1181         }
1182         ovsdb_datum_subtract(&old, type, &rm, &rm_type);
1183         ovsdb_datum_destroy(&rm, &rm_type);
1184     }
1185     if (old.n < type->n_min) {
1186         ctl_fatal("\"remove\" operation would put %u %s in column %s of "
1187                   "table %s but the minimum number is %u",
1188                   old.n,
1189                   type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
1190                   column->name, table->class->name, type->n_min);
1191     }
1192     ovsdb_idl_txn_verify(row, column);
1193     ovsdb_idl_txn_write(row, column, &old);
1194
1195     invalidate_cache(ctx);
1196 }
1197
1198 static void
1199 pre_cmd_clear(struct ctl_context *ctx)
1200 {
1201     const char *table_name = ctx->argv[1];
1202     const struct ctl_table_class *table;
1203     int i;
1204
1205     table = pre_get_table(ctx, table_name);
1206     for (i = 3; i < ctx->argc; i++) {
1207         const struct ovsdb_idl_column *column;
1208
1209         pre_get_column(ctx, table, ctx->argv[i], &column);
1210     }
1211 }
1212
1213 static void
1214 cmd_clear(struct ctl_context *ctx)
1215 {
1216     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1217     const char *table_name = ctx->argv[1];
1218     const char *record_id = ctx->argv[2];
1219     const struct ctl_table_class *table;
1220     const struct ovsdb_idl_row *row;
1221     int i;
1222
1223     table = get_table(table_name);
1224     row = get_row(ctx, table, record_id, must_exist);
1225     if (!row) {
1226         return;
1227     }
1228
1229     for (i = 3; i < ctx->argc; i++) {
1230         const struct ovsdb_idl_column *column;
1231         const struct ovsdb_type *type;
1232         struct ovsdb_datum datum;
1233
1234         die_if_error(get_column(table, ctx->argv[i], &column));
1235         check_mutable(row, column);
1236
1237         type = &column->type;
1238         if (type->n_min > 0) {
1239             ctl_fatal("\"clear\" operation cannot be applied to column %s "
1240                       "of table %s, which is not allowed to be empty",
1241                       column->name, table->class->name);
1242         }
1243
1244         ovsdb_datum_init_empty(&datum);
1245         ovsdb_idl_txn_write(row, column, &datum);
1246     }
1247
1248     invalidate_cache(ctx);
1249 }
1250
1251 static void
1252 pre_create(struct ctl_context *ctx)
1253 {
1254     const char *id = shash_find_data(&ctx->options, "--id");
1255     const char *table_name = ctx->argv[1];
1256     const struct ctl_table_class *table;
1257
1258     table = get_table(table_name);
1259     if (!id && !table->class->is_root) {
1260         VLOG_WARN("applying \"create\" command to table %s without --id "
1261                   "option will have no effect", table->class->name);
1262     }
1263 }
1264
1265 static void
1266 cmd_create(struct ctl_context *ctx)
1267 {
1268     const char *id = shash_find_data(&ctx->options, "--id");
1269     const char *table_name = ctx->argv[1];
1270     const struct ctl_table_class *table = get_table(table_name);
1271     const struct ovsdb_idl_row *row;
1272     const struct uuid *uuid;
1273     int i;
1274
1275     if (id) {
1276         struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
1277         if (table->class->is_root) {
1278             /* This table is in the root set, meaning that rows created in it
1279              * won't disappear even if they are unreferenced, so disable
1280              * warnings about that by pretending that there is a reference. */
1281             symbol->strong_ref = true;
1282         }
1283         uuid = &symbol->uuid;
1284     } else {
1285         uuid = NULL;
1286     }
1287
1288     row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
1289     for (i = 2; i < ctx->argc; i++) {
1290         set_column(table, row, ctx->argv[i], ctx->symtab);
1291     }
1292     ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1293 }
1294
1295 /* This function may be used as the 'postprocess' function for commands that
1296  * insert new rows into the database.  It expects that the command's 'run'
1297  * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
1298  * sole output.  It replaces that output by the row's permanent UUID assigned
1299  * by the database server and appends a new-line.
1300  *
1301  * Currently we use this only for "create", because the higher-level commands
1302  * are supposed to be independent of the actual structure of the vswitch
1303  * configuration. */
1304 static void
1305 post_create(struct ctl_context *ctx)
1306 {
1307     const struct uuid *real;
1308     struct uuid dummy;
1309
1310     if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
1311         OVS_NOT_REACHED();
1312     }
1313     real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
1314     if (real) {
1315         ds_clear(&ctx->output);
1316         ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
1317     }
1318     ds_put_char(&ctx->output, '\n');
1319 }
1320
1321 static void
1322 pre_cmd_destroy(struct ctl_context *ctx)
1323 {
1324     const char *table_name = ctx->argv[1];
1325
1326     pre_get_table(ctx, table_name);
1327 }
1328
1329 static void
1330 cmd_destroy(struct ctl_context *ctx)
1331 {
1332     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1333     bool delete_all = shash_find(&ctx->options, "--all");
1334     const char *table_name = ctx->argv[1];
1335     const struct ctl_table_class *table;
1336     int i;
1337
1338     table = get_table(table_name);
1339
1340     if (delete_all && ctx->argc > 2) {
1341         ctl_fatal("--all and records argument should not be specified together");
1342     }
1343
1344     if (delete_all && !must_exist) {
1345         ctl_fatal("--all and --if-exists should not be specified together");
1346     }
1347
1348     if (delete_all) {
1349         const struct ovsdb_idl_row *row;
1350         const struct ovsdb_idl_row *next_row;
1351
1352         for (row = ovsdb_idl_first_row(ctx->idl, table->class);
1353              row;) {
1354             next_row = ovsdb_idl_next_row(row);
1355             ovsdb_idl_txn_delete(row);
1356             row = next_row;
1357         }
1358     } else {
1359         for (i = 2; i < ctx->argc; i++) {
1360             const struct ovsdb_idl_row *row;
1361
1362             row = get_row(ctx, table, ctx->argv[i], must_exist);
1363             if (row) {
1364                 ovsdb_idl_txn_delete(row);
1365             }
1366         }
1367     }
1368     invalidate_cache(ctx);
1369 }
1370
1371 static void
1372 pre_cmd_wait_until(struct ctl_context *ctx)
1373 {
1374     const char *table_name = ctx->argv[1];
1375     const struct ctl_table_class *table;
1376     int i;
1377
1378     table = pre_get_table(ctx, table_name);
1379
1380     for (i = 3; i < ctx->argc; i++) {
1381         pre_parse_column_key_value(ctx, ctx->argv[i], table);
1382     }
1383 }
1384
1385 static void
1386 cmd_wait_until(struct ctl_context *ctx)
1387 {
1388     const char *table_name = ctx->argv[1];
1389     const char *record_id = ctx->argv[2];
1390     const struct ctl_table_class *table;
1391     const struct ovsdb_idl_row *row;
1392     int i;
1393
1394     table = get_table(table_name);
1395
1396     row = get_row(ctx, table, record_id, false);
1397     if (!row) {
1398         ctx->try_again = true;
1399         return;
1400     }
1401
1402     for (i = 3; i < ctx->argc; i++) {
1403         if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
1404             ctx->try_again = true;
1405             return;
1406         }
1407     }
1408 }
1409
1410 /* Parses one command. */
1411 static void
1412 parse_command(int argc, char *argv[], struct shash *local_options,
1413               struct ctl_command *command)
1414 {
1415     const struct ctl_command_syntax *p;
1416     struct shash_node *node;
1417     int n_arg;
1418     int i;
1419
1420     shash_init(&command->options);
1421     shash_swap(local_options, &command->options);
1422     for (i = 0; i < argc; i++) {
1423         const char *option = argv[i];
1424         const char *equals;
1425         char *key, *value;
1426
1427         if (option[0] != '-') {
1428             break;
1429         }
1430
1431         equals = strchr(option, '=');
1432         if (equals) {
1433             key = xmemdup0(option, equals - option);
1434             value = xstrdup(equals + 1);
1435         } else {
1436             key = xstrdup(option);
1437             value = NULL;
1438         }
1439
1440         if (shash_find(&command->options, key)) {
1441             ctl_fatal("'%s' option specified multiple times", argv[i]);
1442         }
1443         shash_add_nocopy(&command->options, key, value);
1444     }
1445     if (i == argc) {
1446         ctl_fatal("missing command name (use --help for help)");
1447     }
1448
1449     p = shash_find_data(&all_commands, argv[i]);
1450     if (!p) {
1451         ctl_fatal("unknown command '%s'; use --help for help", argv[i]);
1452     }
1453
1454     SHASH_FOR_EACH (node, &command->options) {
1455         const char *s = strstr(p->options, node->name);
1456         int end = s ? s[strlen(node->name)] : EOF;
1457
1458         if (end != '=' && end != ',' && end != ' ' && end != '\0') {
1459             ctl_fatal("'%s' command has no '%s' option",
1460                       argv[i], node->name);
1461         }
1462         if ((end == '=') != (node->data != NULL)) {
1463             if (end == '=') {
1464                 ctl_fatal("missing argument to '%s' option on '%s' "
1465                           "command", node->name, argv[i]);
1466             } else {
1467                 ctl_fatal("'%s' option on '%s' does not accept an "
1468                           "argument", node->name, argv[i]);
1469             }
1470         }
1471     }
1472
1473     n_arg = argc - i - 1;
1474     if (n_arg < p->min_args) {
1475         ctl_fatal("'%s' command requires at least %d arguments",
1476                   p->name, p->min_args);
1477     } else if (n_arg > p->max_args) {
1478         int j;
1479
1480         for (j = i + 1; j < argc; j++) {
1481             if (argv[j][0] == '-') {
1482                 ctl_fatal("'%s' command takes at most %d arguments "
1483                           "(note that options must precede command "
1484                           "names and follow a \"--\" argument)",
1485                           p->name, p->max_args);
1486             }
1487         }
1488
1489         ctl_fatal("'%s' command takes at most %d arguments",
1490                   p->name, p->max_args);
1491     }
1492
1493     command->syntax = p;
1494     command->argc = n_arg + 1;
1495     command->argv = &argv[i];
1496 }
1497
1498 static void
1499 pre_cmd_show(struct ctl_context *ctx)
1500 {
1501     struct cmd_show_table *show;
1502
1503     for (show = cmd_show_tables; show->table; show++) {
1504         size_t i;
1505
1506         ovsdb_idl_add_table(ctx->idl, show->table);
1507         if (show->name_column) {
1508             ovsdb_idl_add_column(ctx->idl, show->name_column);
1509         }
1510         for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1511             const struct ovsdb_idl_column *column = show->columns[i];
1512             if (column) {
1513                 ovsdb_idl_add_column(ctx->idl, column);
1514             }
1515         }
1516     }
1517 }
1518
1519 static struct cmd_show_table *
1520 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1521 {
1522     struct cmd_show_table *show;
1523
1524     for (show = cmd_show_tables; show->table; show++) {
1525         if (show->table == row->table->class) {
1526             return show;
1527         }
1528     }
1529     return NULL;
1530 }
1531
1532 static struct cmd_show_table *
1533 cmd_show_find_table_by_name(const char *name)
1534 {
1535     struct cmd_show_table *show;
1536
1537     for (show = cmd_show_tables; show->table; show++) {
1538         if (!strcmp(show->table->name, name)) {
1539             return show;
1540         }
1541     }
1542     return NULL;
1543 }
1544
1545 static void
1546 cmd_show_row(struct ctl_context *ctx, const struct ovsdb_idl_row *row,
1547              int level)
1548 {
1549     struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1550     size_t i;
1551
1552     ds_put_char_multiple(&ctx->output, ' ', level * 4);
1553     if (show && show->name_column) {
1554         const struct ovsdb_datum *datum;
1555
1556         ds_put_format(&ctx->output, "%s ", show->table->name);
1557         datum = ovsdb_idl_read(row, show->name_column);
1558         ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1559     } else {
1560         ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1561     }
1562     ds_put_char(&ctx->output, '\n');
1563
1564     if (!show || show->recurse) {
1565         return;
1566     }
1567
1568     show->recurse = true;
1569     for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1570         const struct ovsdb_idl_column *column = show->columns[i];
1571         const struct ovsdb_datum *datum;
1572
1573         if (!column) {
1574             break;
1575         }
1576
1577         datum = ovsdb_idl_read(row, column);
1578         if (column->type.key.type == OVSDB_TYPE_UUID &&
1579             column->type.key.u.uuid.refTableName) {
1580             struct cmd_show_table *ref_show;
1581             size_t j;
1582
1583             ref_show = cmd_show_find_table_by_name(
1584                 column->type.key.u.uuid.refTableName);
1585             if (ref_show) {
1586                 for (j = 0; j < datum->n; j++) {
1587                     const struct ovsdb_idl_row *ref_row;
1588
1589                     ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1590                                                          ref_show->table,
1591                                                          &datum->keys[j].uuid);
1592                     if (ref_row) {
1593                         cmd_show_row(ctx, ref_row, level + 1);
1594                     }
1595                 }
1596                 continue;
1597             }
1598         } else if (ovsdb_type_is_map(&column->type) &&
1599                    column->type.value.type == OVSDB_TYPE_UUID &&
1600                    column->type.value.u.uuid.refTableName) {
1601             struct cmd_show_table *ref_show;
1602             size_t j;
1603
1604             /* Prints the key to ref'ed table name map if the ref'ed table
1605              * is also defined in 'cmd_show_tables'.  */
1606             ref_show = cmd_show_find_table_by_name(
1607                 column->type.value.u.uuid.refTableName);
1608             if (ref_show && ref_show->name_column) {
1609                 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1610                 ds_put_format(&ctx->output, "%s:\n", column->name);
1611                 for (j = 0; j < datum->n; j++) {
1612                     const struct ovsdb_idl_row *ref_row;
1613
1614                     ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1615                                                          ref_show->table,
1616                                                          &datum->values[j].uuid);
1617
1618                     ds_put_char_multiple(&ctx->output, ' ', (level + 2) * 4);
1619                     ovsdb_atom_to_string(&datum->keys[j], column->type.key.type,
1620                                          &ctx->output);
1621                     ds_put_char(&ctx->output, '=');
1622                     if (ref_row) {
1623                         const struct ovsdb_datum *ref_datum;
1624
1625                         ref_datum = ovsdb_idl_read(ref_row,
1626                                                    ref_show->name_column);
1627                         ovsdb_datum_to_string(ref_datum,
1628                                               &ref_show->name_column->type,
1629                                               &ctx->output);
1630                     } else {
1631                         ds_put_cstr(&ctx->output, "\"<null>\"");
1632                     }
1633                     ds_put_char(&ctx->output, '\n');
1634                 }
1635                 continue;
1636             }
1637         }
1638
1639         if (!ovsdb_datum_is_default(datum, &column->type)) {
1640             ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1641             ds_put_format(&ctx->output, "%s: ", column->name);
1642             ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1643             ds_put_char(&ctx->output, '\n');
1644         }
1645     }
1646     show->recurse = false;
1647 }
1648
1649 static void
1650 cmd_show(struct ctl_context *ctx)
1651 {
1652     const struct ovsdb_idl_row *row;
1653
1654     for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1655          row; row = ovsdb_idl_next_row(row)) {
1656         cmd_show_row(ctx, row, 0);
1657     }
1658 }
1659
1660 \f
1661 /* Given pointer to dynamic array 'options_p',  array's current size
1662  * 'allocated_options_p' and number of added options 'n_options_p',
1663  * adds all command options to the array.  Enlarges the array if
1664  * necessary. */
1665 void
1666 ctl_add_cmd_options(struct option **options_p, size_t *n_options_p,
1667                     size_t *allocated_options_p, int opt_val)
1668 {
1669     struct option *o;
1670     const struct shash_node *node;
1671     size_t n_existing_options = *n_options_p;
1672
1673     SHASH_FOR_EACH (node, ctl_get_all_commands()) {
1674         const struct ctl_command_syntax *p = node->data;
1675
1676         if (p->options[0]) {
1677             char *save_ptr = NULL;
1678             char *name;
1679             char *s;
1680
1681             s = xstrdup(p->options);
1682             for (name = strtok_r(s, ",", &save_ptr); name != NULL;
1683                  name = strtok_r(NULL, ",", &save_ptr)) {
1684                 char *equals;
1685                 int has_arg;
1686
1687                 ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
1688                 name += 2;
1689
1690                 equals = strchr(name, '=');
1691                 if (equals) {
1692                     has_arg = required_argument;
1693                     *equals = '\0';
1694                 } else {
1695                     has_arg = no_argument;
1696                 }
1697
1698                 o = find_option(name, *options_p, *n_options_p);
1699                 if (o) {
1700                     ovs_assert(o - *options_p >= n_existing_options);
1701                     ovs_assert(o->has_arg == has_arg);
1702                 } else {
1703                     o = add_option(options_p, n_options_p, allocated_options_p);
1704                     o->name = xstrdup(name);
1705                     o->has_arg = has_arg;
1706                     o->flag = NULL;
1707                     o->val = opt_val;
1708                 }
1709             }
1710
1711             free(s);
1712         }
1713     }
1714     o = add_option(options_p, n_options_p, allocated_options_p);
1715     memset(o, 0, sizeof *o);
1716 }
1717
1718 /* Parses command-line input for commands. */
1719 struct ctl_command *
1720 ctl_parse_commands(int argc, char *argv[], struct shash *local_options,
1721                    size_t *n_commandsp)
1722 {
1723     struct ctl_command *commands;
1724     size_t n_commands, allocated_commands;
1725     int i, start;
1726
1727     commands = NULL;
1728     n_commands = allocated_commands = 0;
1729
1730     for (start = i = 0; i <= argc; i++) {
1731         if (i == argc || !strcmp(argv[i], "--")) {
1732             if (i > start) {
1733                 if (n_commands >= allocated_commands) {
1734                     struct ctl_command *c;
1735
1736                     commands = x2nrealloc(commands, &allocated_commands,
1737                                           sizeof *commands);
1738                     for (c = commands; c < &commands[n_commands]; c++) {
1739                         shash_moved(&c->options);
1740                     }
1741                 }
1742                 parse_command(i - start, &argv[start], local_options,
1743                               &commands[n_commands++]);
1744             } else if (!shash_is_empty(local_options)) {
1745                 ctl_fatal("missing command name (use --help for help)");
1746             }
1747             start = i + 1;
1748         }
1749     }
1750     if (!n_commands) {
1751         ctl_fatal("missing command name (use --help for help)");
1752     }
1753     *n_commandsp = n_commands;
1754     return commands;
1755 }
1756
1757 /* Prints all registered commands. */
1758 void
1759 ctl_print_commands(void)
1760 {
1761     const struct shash_node *node;
1762
1763     SHASH_FOR_EACH (node, ctl_get_all_commands()) {
1764         const struct ctl_command_syntax *p = node->data;
1765         char *options = xstrdup(p->options);
1766         char *options_begin = options;
1767         char *item;
1768
1769         for (item = strsep(&options, ","); item != NULL;
1770              item = strsep(&options, ",")) {
1771             if (item[0] != '\0') {
1772                 printf("[%s] ", item);
1773             }
1774         }
1775         printf(",%s,", p->name);
1776         print_command_arguments(p);
1777         printf("\n");
1778
1779         free(options_begin);
1780     }
1781
1782     exit(EXIT_SUCCESS);
1783 }
1784
1785 /* Given array of options 'options', prints them. */
1786 void
1787 ctl_print_options(const struct option *options)
1788 {
1789     for (; options->name; options++) {
1790         const struct option *o = options;
1791
1792         printf("--%s%s\n", o->name, o->has_arg ? "=ARG" : "");
1793         if (o->flag == NULL && o->val > 0 && o->val <= UCHAR_MAX) {
1794             printf("-%c%s\n", o->val, o->has_arg ? " ARG" : "");
1795         }
1796     }
1797
1798     exit(EXIT_SUCCESS);
1799 }
1800
1801 /* Returns the default local database path. */
1802 char *
1803 ctl_default_db(void)
1804 {
1805     static char *def;
1806     if (!def) {
1807         def = xasprintf("unix:%s/db.sock", ovs_rundir());
1808     }
1809     return def;
1810 }
1811
1812 /* Returns true if it looks like this set of arguments might modify the
1813  * database, otherwise false.  (Not very smart, so it's prone to false
1814  * positives.) */
1815 bool
1816 ctl_might_write_to_db(char **argv)
1817 {
1818     for (; *argv; argv++) {
1819         const struct ctl_command_syntax *p = shash_find_data(&all_commands, *argv);
1820         if (p && p->mode == RW) {
1821             return true;
1822         }
1823     }
1824     return false;
1825 }
1826
1827 void
1828 ctl_fatal(const char *format, ...)
1829 {
1830     char *message;
1831     va_list args;
1832
1833     va_start(args, format);
1834     message = xvasprintf(format, args);
1835     va_end(args);
1836
1837     vlog_set_levels(&VLM_db_ctl_base, VLF_CONSOLE, VLL_OFF);
1838     VLOG_ERR("%s", message);
1839     ovs_error(0, "%s", message);
1840     ctl_exit(EXIT_FAILURE);
1841 }
1842
1843 /* Frees the current transaction and the underlying IDL and then calls
1844  * exit(status).
1845  *
1846  * Freeing the transaction and the IDL is not strictly necessary, but it makes
1847  * for a clean memory leak report from valgrind in the normal case.  That makes
1848  * it easier to notice real memory leaks. */
1849 void
1850 ctl_exit(int status)
1851 {
1852     if (the_idl_txn) {
1853         ovsdb_idl_txn_abort(the_idl_txn);
1854         ovsdb_idl_txn_destroy(the_idl_txn);
1855     }
1856     ovsdb_idl_destroy(the_idl);
1857     exit(status);
1858 }
1859
1860 /* Command for showing overview of database contents. */
1861 static const struct ctl_command_syntax db_ctl_show_command[] = {
1862     {"show", 0, 0, "", pre_cmd_show, cmd_show, NULL, "", RO},
1863     {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
1864 };
1865
1866 /* Comman database commands to be registered. */
1867 static const struct ctl_command_syntax db_ctl_commands[] = {
1868     {"comment", 0, INT_MAX, "[ARG]...", NULL, NULL, NULL, "", RO},
1869     {"get", 2, INT_MAX, "TABLE RECORD [COLUMN[:KEY]]...",pre_cmd_get, cmd_get,
1870      NULL, "--if-exists,--id=", RO},
1871     {"list", 1, INT_MAX, "TABLE [RECORD]...", pre_cmd_list, cmd_list, NULL,
1872      "--if-exists,--columns=", RO},
1873     {"find", 1, INT_MAX, "TABLE [COLUMN[:KEY]=VALUE]...", pre_cmd_find,
1874      cmd_find, NULL, "--columns=", RO},
1875     {"set", 3, INT_MAX, "TABLE RECORD COLUMN[:KEY]=VALUE...", pre_cmd_set,
1876      cmd_set, NULL, "--if-exists", RW},
1877     {"add", 4, INT_MAX, "TABLE RECORD COLUMN [KEY=]VALUE...", pre_cmd_add,
1878      cmd_add, NULL, "--if-exists", RW},
1879     {"remove", 4, INT_MAX, "TABLE RECORD COLUMN KEY|VALUE|KEY=VALUE...",
1880      pre_cmd_remove, cmd_remove, NULL, "--if-exists", RW},
1881     {"clear", 3, INT_MAX, "TABLE RECORD COLUMN...", pre_cmd_clear, cmd_clear,
1882      NULL, "--if-exists", RW},
1883     {"create", 2, INT_MAX, "TABLE COLUMN[:KEY]=VALUE...", pre_create,
1884      cmd_create, post_create, "--id=", RW},
1885     {"destroy", 1, INT_MAX, "TABLE [RECORD]...", pre_cmd_destroy, cmd_destroy,
1886      NULL, "--if-exists,--all", RW},
1887     {"wait-until", 2, INT_MAX, "TABLE RECORD [COLUMN[:KEY]=VALUE]...",
1888      pre_cmd_wait_until, cmd_wait_until, NULL, "", RO},
1889     {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
1890 };
1891
1892 /* Registers commands represented by 'struct ctl_command_syntax's to
1893  * 'all_commands'.  The last element of 'commands' must be an all-NULL
1894  * element. */
1895 void
1896 ctl_register_commands(const struct ctl_command_syntax *commands)
1897 {
1898     const struct ctl_command_syntax *p;
1899
1900     for (p = commands; p->name; p++) {
1901         shash_add_assert(&all_commands, p->name, p);
1902     }
1903 }
1904
1905 /* Registers the 'db_ctl_commands' to 'all_commands'. */
1906 void
1907 ctl_init(void)
1908 {
1909     ctl_register_commands(db_ctl_commands);
1910     ctl_register_commands(db_ctl_show_command);
1911 }
1912
1913 /* Returns 'all_commands'. */
1914 const struct shash *
1915 ctl_get_all_commands(void)
1916 {
1917     return &all_commands;
1918 }
1919
1920 /* Returns the text for the database commands usage.  */
1921 const char *
1922 ctl_get_db_cmd_usage(void)
1923 {
1924     return "Database commands:\n\
1925   list TBL [REC]              list RECord (or all records) in TBL\n\
1926   find TBL CONDITION...       list records satisfying CONDITION in TBL\n\
1927   get TBL REC COL[:KEY]       print values of COLumns in RECord in TBL\n\
1928   set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
1929   add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
1930   remove TBL REC COL [KEY=]VALUE  remove (KEY=)VALUE from COLumn\n\
1931   clear TBL REC COL           clear values from COLumn in RECord in TBL\n\
1932   create TBL COL[:KEY]=VALUE  create and initialize new record\n\
1933   destroy TBL REC             delete RECord from TBL\n\
1934   wait-until TBL REC [COL[:KEY]=VALUE]  wait until condition is true\n\
1935 Potentially unsafe database commands require --force option.\n";
1936 }
1937
1938 /* Initializes 'ctx' from 'command'. */
1939 void
1940 ctl_context_init_command(struct ctl_context *ctx,
1941                          struct ctl_command *command)
1942 {
1943     ctx->argc = command->argc;
1944     ctx->argv = command->argv;
1945     ctx->options = command->options;
1946
1947     ds_swap(&ctx->output, &command->output);
1948     ctx->table = command->table;
1949     ctx->try_again = false;
1950 }
1951
1952 /* Initializes the entire 'ctx'. */
1953 void
1954 ctl_context_init(struct ctl_context *ctx, struct ctl_command *command,
1955                  struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
1956                  struct ovsdb_symbol_table *symtab,
1957                  void (*invalidate_cache)(struct ctl_context *))
1958 {
1959     if (command) {
1960         ctl_context_init_command(ctx, command);
1961     }
1962     ctx->idl = idl;
1963     ctx->txn = txn;
1964     ctx->symtab = symtab;
1965     ctx->invalidate_cache = invalidate_cache;
1966 }
1967
1968 /* Completes processing of 'command' within 'ctx'. */
1969 void
1970 ctl_context_done_command(struct ctl_context *ctx,
1971                          struct ctl_command *command)
1972 {
1973     ds_swap(&ctx->output, &command->output);
1974     command->table = ctx->table;
1975 }
1976
1977 /* Finishes up with 'ctx'.
1978  *
1979  * If command is nonnull, first calls ctl_context_done_command() to complete
1980  * processing that command within 'ctx'. */
1981 void
1982 ctl_context_done(struct ctl_context *ctx,
1983                  struct ctl_command *command)
1984 {
1985     if (command) {
1986         ctl_context_done_command(ctx, command);
1987     }
1988     invalidate_cache(ctx);
1989 }
1990
1991 /* Finds and returns the "struct ctl_table_class *" with 'table_name' by
1992  * searching the 'tables'. */
1993 const struct ctl_table_class *
1994 get_table(const char *table_name)
1995 {
1996     const struct ctl_table_class *table;
1997     const struct ctl_table_class *best_match = NULL;
1998     unsigned int best_score = 0;
1999
2000     for (table = tables; table->class; table++) {
2001         unsigned int score = score_partial_match(table->class->name,
2002                                                  table_name);
2003         if (score > best_score) {
2004             best_match = table;
2005             best_score = score;
2006         } else if (score == best_score) {
2007             best_match = NULL;
2008         }
2009     }
2010     if (best_match) {
2011         return best_match;
2012     } else if (best_score) {
2013         ctl_fatal("multiple table names match \"%s\"", table_name);
2014     } else {
2015         ctl_fatal("unknown table \"%s\"", table_name);
2016     }
2017     return NULL;
2018 }
2019
2020 /* Sets the column of 'row' in 'table'. */
2021 void
2022 set_column(const struct ctl_table_class *table,
2023            const struct ovsdb_idl_row *row, const char *arg,
2024            struct ovsdb_symbol_table *symtab)
2025 {
2026     const struct ovsdb_idl_column *column;
2027     char *key_string, *value_string;
2028     char *error;
2029
2030     error = parse_column_key_value(arg, table, &column, &key_string,
2031                                    NULL, NULL, 0, &value_string);
2032     die_if_error(error);
2033     if (!value_string) {
2034         ctl_fatal("%s: missing value", arg);
2035     }
2036     check_mutable(row, column);
2037
2038     if (key_string) {
2039         union ovsdb_atom key, value;
2040         struct ovsdb_datum datum;
2041
2042         if (column->type.value.type == OVSDB_TYPE_VOID) {
2043             ctl_fatal("cannot specify key to set for non-map column %s",
2044                       column->name);
2045         }
2046
2047         die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2048                                             key_string, symtab));
2049         die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2050                                             value_string, symtab));
2051
2052         ovsdb_datum_init_empty(&datum);
2053         ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2054
2055         ovsdb_atom_destroy(&key, column->type.key.type);
2056         ovsdb_atom_destroy(&value, column->type.value.type);
2057
2058         ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2059                           &column->type, false);
2060         ovsdb_idl_txn_verify(row, column);
2061         ovsdb_idl_txn_write(row, column, &datum);
2062     } else {
2063         struct ovsdb_datum datum;
2064
2065         die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2066                                              value_string, symtab));
2067         ovsdb_idl_txn_write(row, column, &datum);
2068     }
2069
2070     free(key_string);
2071     free(value_string);
2072 }