2 * Copyright (c) 2009 Nicira Networks.
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.
26 #include "command-line.h"
29 #include "ovsdb-data.h"
30 #include "ovsdb-error.h"
31 #include "ovsdb-idl.h"
32 #include "ovsdb-types.h"
33 #include "ovsdb/column.h"
34 #include "ovsdb/condition.h"
35 #include "ovsdb/file.h"
36 #include "ovsdb/log.h"
37 #include "ovsdb/ovsdb.h"
38 #include "ovsdb/query.h"
39 #include "ovsdb/row.h"
40 #include "ovsdb/table.h"
41 #include "ovsdb/transaction.h"
42 #include "ovsdb/trigger.h"
43 #include "poll-loop.h"
46 #include "tests/idltest.h"
51 static struct command all_commands[];
53 static void usage(void) NO_RETURN;
54 static void parse_options(int argc, char *argv[]);
57 main(int argc, char *argv[])
59 set_program_name(argv[0]);
62 parse_options(argc, argv);
63 run_command(argc - optind, argv + optind, all_commands);
68 parse_options(int argc, char *argv[])
70 static struct option long_options[] = {
71 {"timeout", required_argument, 0, 't'},
72 {"verbose", optional_argument, 0, 'v'},
73 {"help", no_argument, 0, 'h'},
76 char *short_options = long_options_to_short_options(long_options);
79 unsigned long int timeout;
82 c = getopt_long(argc, argv, short_options, long_options, NULL);
89 timeout = strtoul(optarg, NULL, 10);
91 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
102 vlog_set_verbosity(optarg);
118 printf("%s: Open vSwitch database test utility\n"
119 "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
120 " log-io FILE FLAGS COMMAND...\n"
121 " open FILE with FLAGS, run COMMANDs\n"
122 " parse-atomic-type TYPE\n"
123 " parse TYPE as OVSDB atomic type, and re-serialize\n"
125 " parse JSON as OVSDB type, and re-serialize\n"
126 " parse-atoms TYPE ATOM...\n"
127 " parse ATOMs as atoms of given TYPE, and re-serialize\n"
128 " sort-atoms TYPE ATOM...\n"
129 " print ATOMs in sorted order, and re-serialize\n"
130 " parse-data TYPE DATUM...\n"
131 " parse DATUMs as data of given TYPE, and re-serialize\n"
132 " parse-column NAME OBJECT\n"
133 " parse column NAME with info OBJECT, and re-serialize\n"
134 " parse-table NAME OBJECT\n"
135 " parse table NAME with info OBJECT\n"
136 " parse-row TABLE ROW..., and re-serialize\n"
137 " parse each ROW of defined TABLE\n"
138 " compare-row TABLE ROW...\n"
139 " mutually compare all of the ROWs, print those that are equal\n"
140 " parse-conditions TABLE CONDITION...\n"
141 " parse each CONDITION on TABLE, and re-serialize\n"
142 " evaluate-conditions TABLE [CONDITION,...] [ROW,...]\n"
143 " test CONDITIONS on TABLE against each ROW, print results\n"
144 " query TABLE [ROW,...] [CONDITION,...]\n"
145 " add each ROW to TABLE, then query and print the rows that\n"
146 " satisfy each CONDITION.\n"
147 " query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
148 " add each ROW to TABLE, then query and print the rows that\n"
149 " satisfy each CONDITION and have distinct COLUMNS.\n"
150 " transact COMMAND\n"
151 " execute each specified transactional COMMAND:\n"
158 " execute SCHEMA TRANSACTION...\n"
159 " executes each TRANSACTION on an initially empty database\n"
160 " the specified SCHEMA\n"
161 " trigger SCHEMA TRANSACTION...\n"
162 " executes each TRANSACTION on an initially empty database\n"
163 " the specified SCHEMA. A TRANSACTION of the form\n"
164 " [\"advance\", NUMBER] advances NUMBER milliseconds in\n"
165 " simulated time, for causing triggers to time out.\n"
166 " idl SERVER [TRANSACTION...]\n"
167 " connect to SERVER and dump the contents of the database\n"
168 " as seen initially by the IDL implementation and after\n"
169 " executing each TRANSACTION. (Each TRANSACTION must modify\n"
170 " the database or this command will hang.)\n",
171 program_name, program_name);
173 printf("\nOther options:\n"
174 " -t, --timeout=SECS give up after SECS seconds\n"
175 " -h, --help display this help message\n");
179 /* Command helper functions. */
182 parse_json(const char *s)
184 struct json *json = json_from_string(s);
185 if (json->type == JSON_STRING) {
186 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
192 unbox_json(struct json *json)
194 if (json->type == JSON_ARRAY && json->u.array.n == 1) {
195 struct json *inner = json->u.array.elems[0];
196 json->u.array.elems[0] = NULL;
205 print_and_free_json(struct json *json)
207 char *string = json_to_string(json, JSSF_SORT);
214 check_ovsdb_error(struct ovsdb_error *error)
217 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
221 /* Command implementations. */
224 do_log_io(int argc, char *argv[])
226 const char *name = argv[1];
227 char *mode = argv[2];
229 struct ovsdb_error *error;
230 struct ovsdb_log *log;
231 char *save_ptr = NULL;
236 for (flags = 0, token = strtok_r(mode, " |", &save_ptr); token != NULL;
237 token = strtok_r(NULL, " |", &save_ptr))
239 if (!strcmp(token, "O_RDONLY")) {
241 } else if (!strcmp(token, "O_RDWR")) {
243 } else if (!strcmp(token, "O_TRUNC")) {
245 } else if (!strcmp(token, "O_CREAT")) {
247 } else if (!strcmp(token, "O_EXCL")) {
249 } else if (!strcmp(token, "O_TRUNC")) {
254 check_ovsdb_error(ovsdb_log_open(name, flags, &log));
255 printf("%s: open successful\n", name);
257 for (i = 3; i < argc; i++) {
258 const char *command = argv[i];
259 if (!strcmp(command, "read")) {
262 error = ovsdb_log_read(log, &json);
264 printf("%s: read: ", name);
266 print_and_free_json(json);
268 printf("end of log\n");
272 } else if (!strncmp(command, "write:", 6)) {
273 struct json *json = parse_json(command + 6);
274 error = ovsdb_log_write(log, json);
276 } else if (!strcmp(command, "commit")) {
277 error = ovsdb_log_commit(log);
279 ovs_fatal(0, "unknown log-io command \"%s\"", command);
282 char *s = ovsdb_error_to_string(error);
283 printf("%s: %s failed: %s\n", name, command, s);
286 printf("%s: %s successful\n", name, command);
290 ovsdb_log_close(log);
294 do_parse_atomic_type(int argc UNUSED, char *argv[])
296 enum ovsdb_atomic_type type;
299 json = unbox_json(parse_json(argv[1]));
300 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
302 print_and_free_json(ovsdb_atomic_type_to_json(type));
306 do_parse_type(int argc UNUSED, char *argv[])
308 struct ovsdb_type type;
311 json = unbox_json(parse_json(argv[1]));
312 check_ovsdb_error(ovsdb_type_from_json(&type, json));
314 print_and_free_json(ovsdb_type_to_json(&type));
318 do_parse_atoms(int argc, char *argv[])
320 enum ovsdb_atomic_type type;
324 json = unbox_json(parse_json(argv[1]));
325 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
328 for (i = 2; i < argc; i++) {
329 union ovsdb_atom atom;
331 json = unbox_json(parse_json(argv[i]));
332 check_ovsdb_error(ovsdb_atom_from_json(&atom, type, json, NULL));
335 print_and_free_json(ovsdb_atom_to_json(&atom, type));
337 ovsdb_atom_destroy(&atom, type);
342 do_parse_data(int argc, char *argv[])
344 struct ovsdb_type type;
348 json = unbox_json(parse_json(argv[1]));
349 check_ovsdb_error(ovsdb_type_from_json(&type, json));
352 for (i = 2; i < argc; i++) {
353 struct ovsdb_datum datum;
355 json = unbox_json(parse_json(argv[i]));
356 check_ovsdb_error(ovsdb_datum_from_json(&datum, &type, json, NULL));
359 print_and_free_json(ovsdb_datum_to_json(&datum, &type));
361 ovsdb_datum_destroy(&datum, &type);
365 static enum ovsdb_atomic_type compare_atoms_atomic_type;
368 compare_atoms(const void *a_, const void *b_)
370 const union ovsdb_atom *a = a_;
371 const union ovsdb_atom *b = b_;
373 return ovsdb_atom_compare_3way(a, b, compare_atoms_atomic_type);
377 do_sort_atoms(int argc UNUSED, char *argv[])
379 enum ovsdb_atomic_type type;
380 union ovsdb_atom *atoms;
381 struct json *json, **json_atoms;
385 json = unbox_json(parse_json(argv[1]));
386 check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json));
389 json = unbox_json(parse_json(argv[2]));
390 if (json->type != JSON_ARRAY) {
391 ovs_fatal(0, "second argument must be array");
394 /* Convert JSON atoms to internal representation. */
395 n_atoms = json->u.array.n;
396 atoms = xmalloc(n_atoms * sizeof *atoms);
397 for (i = 0; i < n_atoms; i++) {
398 check_ovsdb_error(ovsdb_atom_from_json(&atoms[i], type,
399 json->u.array.elems[i], NULL));
404 compare_atoms_atomic_type = type;
405 qsort(atoms, n_atoms, sizeof *atoms, compare_atoms);
407 /* Convert internal representation back to JSON. */
408 json_atoms = xmalloc(n_atoms * sizeof *json_atoms);
409 for (i = 0; i < n_atoms; i++) {
410 json_atoms[i] = ovsdb_atom_to_json(&atoms[i], type);
411 ovsdb_atom_destroy(&atoms[i], type);
413 print_and_free_json(json_array_create(json_atoms, n_atoms));
417 do_parse_column(int argc UNUSED, char *argv[])
419 struct ovsdb_column *column;
422 json = parse_json(argv[2]);
423 check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column));
425 print_and_free_json(ovsdb_column_to_json(column));
426 ovsdb_column_destroy(column);
430 do_parse_table(int argc UNUSED, char *argv[])
432 struct ovsdb_table_schema *ts;
435 json = parse_json(argv[2]);
436 check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts));
438 print_and_free_json(ovsdb_table_schema_to_json(ts));
439 ovsdb_table_schema_destroy(ts);
443 do_parse_rows(int argc, char *argv[])
445 struct ovsdb_column_set all_columns;
446 struct ovsdb_table_schema *ts;
447 struct ovsdb_table *table;
451 json = unbox_json(parse_json(argv[1]));
452 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
455 table = ovsdb_table_create(ts);
456 ovsdb_column_set_init(&all_columns);
457 ovsdb_column_set_add_all(&all_columns, table);
459 for (i = 2; i < argc; i++) {
460 struct ovsdb_column_set columns;
461 struct ovsdb_row *row;
463 ovsdb_column_set_init(&columns);
464 row = ovsdb_row_create(table);
466 json = unbox_json(parse_json(argv[i]));
467 check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns));
470 print_and_free_json(ovsdb_row_to_json(row, &all_columns));
472 if (columns.n_columns) {
478 for (j = 0; j < columns.n_columns; j++) {
479 svec_add(&names, columns.columns[j]->name);
482 s = svec_join(&names, ", ", "");
485 svec_destroy(&names);
490 ovsdb_column_set_destroy(&columns);
491 ovsdb_row_destroy(row);
494 ovsdb_column_set_destroy(&all_columns);
495 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
499 do_compare_rows(int argc, char *argv[])
501 struct ovsdb_column_set all_columns;
502 struct ovsdb_table_schema *ts;
503 struct ovsdb_table *table;
504 struct ovsdb_row **rows;
510 json = unbox_json(parse_json(argv[1]));
511 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
514 table = ovsdb_table_create(ts);
515 ovsdb_column_set_init(&all_columns);
516 ovsdb_column_set_add_all(&all_columns, table);
519 rows = xmalloc(sizeof *rows * n_rows);
520 names = xmalloc(sizeof *names * n_rows);
521 for (i = 0; i < n_rows; i++) {
522 rows[i] = ovsdb_row_create(table);
524 json = parse_json(argv[i + 2]);
525 if (json->type != JSON_ARRAY || json->u.array.n != 2
526 || json->u.array.elems[0]->type != JSON_STRING) {
527 ovs_fatal(0, "\"%s\" does not have expected form "
528 "[\"name\", {data}]", argv[i]);
530 names[i] = xstrdup(json->u.array.elems[0]->u.string);
531 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1],
535 for (i = 0; i < n_rows; i++) {
536 uint32_t i_hash = ovsdb_row_hash_columns(rows[i], &all_columns, 0);
537 for (j = i + 1; j < n_rows; j++) {
538 uint32_t j_hash = ovsdb_row_hash_columns(rows[j], &all_columns, 0);
539 if (ovsdb_row_equal_columns(rows[i], rows[j], &all_columns)) {
540 printf("%s == %s\n", names[i], names[j]);
541 if (i_hash != j_hash) {
542 printf("but hash(%s) != hash(%s)\n", names[i], names[j]);
545 } else if (i_hash == j_hash) {
546 printf("hash(%s) == hash(%s)\n", names[i], names[j]);
553 ovsdb_column_set_destroy(&all_columns);
554 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
558 do_parse_conditions(int argc, char *argv[])
560 struct ovsdb_table_schema *ts;
565 json = unbox_json(parse_json(argv[1]));
566 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
569 for (i = 2; i < argc; i++) {
570 struct ovsdb_condition cnd;
571 struct ovsdb_error *error;
573 json = parse_json(argv[i]);
574 error = ovsdb_condition_from_json(ts, json, NULL, &cnd);
576 print_and_free_json(ovsdb_condition_to_json(&cnd));
578 char *s = ovsdb_error_to_string(error);
579 ovs_error(0, "%s", s);
581 ovsdb_error_destroy(error);
586 ovsdb_condition_destroy(&cnd);
588 ovsdb_table_schema_destroy(ts);
594 do_evaluate_conditions(int argc UNUSED, char *argv[])
596 struct ovsdb_table_schema *ts;
597 struct ovsdb_table *table;
598 struct ovsdb_condition *conditions;
600 struct ovsdb_row **rows;
605 /* Parse table schema, create table. */
606 json = unbox_json(parse_json(argv[1]));
607 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
610 table = ovsdb_table_create(ts);
612 /* Parse conditions. */
613 json = parse_json(argv[2]);
614 if (json->type != JSON_ARRAY) {
615 ovs_fatal(0, "CONDITION argument is not JSON array");
617 n_conditions = json->u.array.n;
618 conditions = xmalloc(n_conditions * sizeof *conditions);
619 for (i = 0; i < n_conditions; i++) {
620 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
621 NULL, &conditions[i]));
626 json = parse_json(argv[3]);
627 if (json->type != JSON_ARRAY) {
628 ovs_fatal(0, "ROW argument is not JSON array");
630 n_rows = json->u.array.n;
631 rows = xmalloc(n_rows * sizeof *rows);
632 for (i = 0; i < n_rows; i++) {
633 rows[i] = ovsdb_row_create(table);
634 check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
639 for (i = 0; i < n_conditions; i++) {
640 printf("condition %2d:", i);
641 for (j = 0; j < n_rows; j++) {
642 bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
646 putchar(result ? 'T' : '-');
651 for (i = 0; i < n_conditions; i++) {
652 ovsdb_condition_destroy(&conditions[i]);
654 for (i = 0; i < n_rows; i++) {
655 ovsdb_row_destroy(rows[i]);
657 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
660 struct do_query_cbdata {
661 struct uuid *row_uuids;
667 do_query_cb(const struct ovsdb_row *row, void *cbdata_)
669 struct do_query_cbdata *cbdata = cbdata_;
672 for (i = 0; i < cbdata->n_rows; i++) {
673 if (uuid_equals(ovsdb_row_get_uuid(row), &cbdata->row_uuids[i])) {
682 do_query(int argc UNUSED, char *argv[])
684 struct do_query_cbdata cbdata;
685 struct ovsdb_table_schema *ts;
686 struct ovsdb_table *table;
691 /* Parse table schema, create table. */
692 json = unbox_json(parse_json(argv[1]));
693 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
696 table = ovsdb_table_create(ts);
698 /* Parse rows, add to table. */
699 json = parse_json(argv[2]);
700 if (json->type != JSON_ARRAY) {
701 ovs_fatal(0, "ROW argument is not JSON array");
703 cbdata.n_rows = json->u.array.n;
704 cbdata.row_uuids = xmalloc(cbdata.n_rows * sizeof *cbdata.row_uuids);
705 cbdata.counts = xmalloc(cbdata.n_rows * sizeof *cbdata.counts);
706 for (i = 0; i < cbdata.n_rows; i++) {
707 struct ovsdb_row *row = ovsdb_row_create(table);
708 uuid_generate(ovsdb_row_get_uuid_rw(row));
709 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
711 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
712 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
713 UUID_ARGS(ovsdb_row_get_uuid(row)));
715 cbdata.row_uuids[i] = *ovsdb_row_get_uuid(row);
716 ovsdb_table_put_row(table, row);
720 /* Parse conditions and execute queries. */
721 json = parse_json(argv[3]);
722 if (json->type != JSON_ARRAY) {
723 ovs_fatal(0, "CONDITION argument is not JSON array");
725 for (i = 0; i < json->u.array.n; i++) {
726 struct ovsdb_condition cnd;
729 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
732 memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
733 ovsdb_query(table, &cnd, do_query_cb, &cbdata);
735 printf("query %2d:", i);
736 for (j = 0; j < cbdata.n_rows; j++) {
740 if (cbdata.counts[j]) {
741 printf("%d", cbdata.counts[j]);
742 if (cbdata.counts[j] > 1) {
752 ovsdb_condition_destroy(&cnd);
756 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
761 struct do_query_distinct_class {
762 struct ovsdb_row *example;
766 struct do_query_distinct_row {
768 struct do_query_distinct_class *class;
772 do_query_distinct(int argc UNUSED, char *argv[])
774 struct ovsdb_column_set columns;
775 struct ovsdb_table_schema *ts;
776 struct ovsdb_table *table;
777 struct do_query_distinct_row *rows;
779 struct do_query_distinct_class *classes;
785 /* Parse table schema, create table. */
786 json = unbox_json(parse_json(argv[1]));
787 check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
790 table = ovsdb_table_create(ts);
792 /* Parse column set. */
793 json = parse_json(argv[4]);
794 ovsdb_column_set_from_json(json, table, &columns);
797 /* Parse rows, add to table. */
798 json = parse_json(argv[2]);
799 if (json->type != JSON_ARRAY) {
800 ovs_fatal(0, "ROW argument is not JSON array");
802 n_rows = json->u.array.n;
803 rows = xmalloc(n_rows * sizeof *rows);
804 classes = xmalloc(n_rows * sizeof *classes);
806 for (i = 0; i < n_rows; i++) {
807 struct ovsdb_row *row;
811 row = ovsdb_row_create(table);
812 uuid_generate(ovsdb_row_get_uuid_rw(row));
813 check_ovsdb_error(ovsdb_row_from_json(row, json->u.array.elems[i],
816 /* Initialize row and find equivalence class. */
817 rows[i].uuid = *ovsdb_row_get_uuid(row);
818 rows[i].class = NULL;
819 for (j = 0; j < n_classes; j++) {
820 if (ovsdb_row_equal_columns(row, classes[j].example, &columns)) {
821 rows[i].class = &classes[j];
825 if (!rows[i].class) {
826 rows[i].class = &classes[n_classes];
827 classes[n_classes].example = ovsdb_row_clone(row);
831 /* Add row to table. */
832 if (ovsdb_table_get_row(table, ovsdb_row_get_uuid(row))) {
833 ovs_fatal(0, "duplicate UUID "UUID_FMT" in table",
834 UUID_ARGS(ovsdb_row_get_uuid(row)));
836 ovsdb_table_put_row(table, row);
841 /* Parse conditions and execute queries. */
842 json = parse_json(argv[3]);
843 if (json->type != JSON_ARRAY) {
844 ovs_fatal(0, "CONDITION argument is not JSON array");
846 for (i = 0; i < json->u.array.n; i++) {
847 struct ovsdb_row_set results;
848 struct ovsdb_condition cnd;
850 check_ovsdb_error(ovsdb_condition_from_json(ts, json->u.array.elems[i],
853 for (j = 0; j < n_classes; j++) {
854 classes[j].count = 0;
856 ovsdb_row_set_init(&results);
857 ovsdb_query_distinct(table, &cnd, &columns, &results);
858 for (j = 0; j < results.n_rows; j++) {
859 for (k = 0; k < n_rows; k++) {
860 if (uuid_equals(ovsdb_row_get_uuid(results.rows[j]),
862 rows[k].class->count++;
866 ovsdb_row_set_destroy(&results);
868 printf("query %2d:", i);
869 for (j = 0; j < n_rows; j++) {
870 int count = rows[j].class->count;
879 } else if (count == 1) {
880 putchar("abcdefghijklmnopqrstuvwxyz"[rows[j].class - classes]);
887 ovsdb_condition_destroy(&cnd);
891 ovsdb_table_destroy(table); /* Also destroys 'ts'. */
897 do_execute(int argc UNUSED, char *argv[])
899 struct ovsdb_schema *schema;
904 /* Create database. */
905 json = parse_json(argv[1]);
906 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
908 db = ovsdb_create(schema);
910 for (i = 2; i < argc; i++) {
911 struct json *params, *result;
914 params = parse_json(argv[i]);
915 result = ovsdb_execute(db, params, 0, NULL);
916 s = json_to_string(result, JSSF_SORT);
918 json_destroy(params);
919 json_destroy(result);
925 struct test_trigger {
926 struct ovsdb_trigger trigger;
931 do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
936 result = ovsdb_trigger_steal_result(&t->trigger);
937 s = json_to_string(result, JSSF_SORT);
938 printf("t=%lld: trigger %d (%s): %s\n", now, t->number, title, s);
939 json_destroy(result);
940 ovsdb_trigger_destroy(&t->trigger);
945 do_trigger(int argc UNUSED, char *argv[])
947 struct ovsdb_schema *schema;
948 struct list completions;
955 /* Create database. */
956 json = parse_json(argv[1]);
957 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
959 db = ovsdb_create(schema);
961 list_init(&completions);
964 for (i = 2; i < argc; i++) {
965 struct json *params = parse_json(argv[i]);
966 if (params->type == JSON_ARRAY
967 && json_array(params)->n == 2
968 && json_array(params)->elems[0]->type == JSON_STRING
969 && !strcmp(json_string(json_array(params)->elems[0]), "advance")
970 && json_array(params)->elems[1]->type == JSON_INTEGER) {
971 now += json_integer(json_array(params)->elems[1]);
972 json_destroy(params);
974 struct test_trigger *t = xmalloc(sizeof *t);
975 ovsdb_trigger_init(db, &t->trigger, params, &completions, now);
976 t->number = number++;
977 if (ovsdb_trigger_is_complete(&t->trigger)) {
978 do_trigger_dump(t, now, "immediate");
980 printf("t=%lld: new trigger %d\n", now, t->number);
984 ovsdb_trigger_run(db, now);
985 while (!list_is_empty(&completions)) {
986 do_trigger_dump(CONTAINER_OF(list_pop_front(&completions),
987 struct test_trigger, trigger.node),
991 ovsdb_trigger_wait(db, now);
992 poll_immediate_wake();
1000 do_help(int argc UNUSED, char *argv[] UNUSED)
1005 /* "transact" command. */
1007 static struct ovsdb *do_transact_db;
1008 static struct ovsdb_txn *do_transact_txn;
1009 static struct ovsdb_table *do_transact_table;
1012 do_transact_commit(int argc UNUSED, char *argv[] UNUSED)
1014 ovsdb_txn_commit(do_transact_txn, false);
1015 do_transact_txn = NULL;
1019 do_transact_abort(int argc UNUSED, char *argv[] UNUSED)
1021 ovsdb_txn_abort(do_transact_txn);
1022 do_transact_txn = NULL;
1026 uuid_from_integer(int integer, struct uuid *uuid)
1029 uuid->parts[3] = integer;
1032 static const struct ovsdb_row *
1033 do_transact_find_row(const char *uuid_string)
1035 const struct ovsdb_row *row;
1038 uuid_from_integer(atoi(uuid_string), &uuid);
1039 row = ovsdb_table_get_row(do_transact_table, &uuid);
1041 ovs_fatal(0, "table does not contain row with UUID "UUID_FMT,
1048 do_transact_set_integer(struct ovsdb_row *row, const char *column_name,
1051 if (integer != -1) {
1052 const struct ovsdb_column *column;
1054 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1056 row->fields[column->index].keys[0].integer = integer;
1061 do_transact_get_integer(const struct ovsdb_row *row, const char *column_name)
1063 const struct ovsdb_column *column;
1065 column = ovsdb_table_schema_get_column(do_transact_table->schema,
1067 return row->fields[column->index].keys[0].integer;
1071 do_transact_set_i_j(struct ovsdb_row *row,
1072 const char *i_string, const char *j_string)
1074 do_transact_set_integer(row, "i", atoi(i_string));
1075 do_transact_set_integer(row, "j", atoi(j_string));
1079 do_transact_insert(int argc UNUSED, char *argv[] UNUSED)
1081 struct ovsdb_row *row;
1084 row = ovsdb_row_create(do_transact_table);
1087 uuid = ovsdb_row_get_uuid_rw(row);
1088 uuid_from_integer(atoi(argv[1]), uuid);
1089 if (ovsdb_table_get_row(do_transact_table, uuid)) {
1090 ovs_fatal(0, "table already contains row with UUID "UUID_FMT,
1094 do_transact_set_i_j(row, argv[2], argv[3]);
1097 ovsdb_txn_row_insert(do_transact_txn, row);
1101 do_transact_delete(int argc UNUSED, char *argv[] UNUSED)
1103 const struct ovsdb_row *row = do_transact_find_row(argv[1]);
1104 ovsdb_txn_row_delete(do_transact_txn, row);
1108 do_transact_modify(int argc UNUSED, char *argv[] UNUSED)
1110 const struct ovsdb_row *row_ro;
1111 struct ovsdb_row *row_rw;
1113 row_ro = do_transact_find_row(argv[1]);
1114 row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro);
1115 do_transact_set_i_j(row_rw, argv[2], argv[3]);
1119 compare_rows_by_uuid(const void *a_, const void *b_)
1121 struct ovsdb_row *const *ap = a_;
1122 struct ovsdb_row *const *bp = b_;
1124 return uuid_compare_3way(ovsdb_row_get_uuid(*ap), ovsdb_row_get_uuid(*bp));
1128 do_transact_print(int argc UNUSED, char *argv[] UNUSED)
1130 const struct ovsdb_row **rows;
1131 const struct ovsdb_row *row;
1135 n_rows = hmap_count(&do_transact_table->rows);
1136 rows = xmalloc(n_rows * sizeof *rows);
1138 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1139 &do_transact_table->rows) {
1142 assert(i == n_rows);
1144 qsort(rows, n_rows, sizeof *rows, compare_rows_by_uuid);
1146 for (i = 0; i < n_rows; i++) {
1147 printf("\n%"PRId32": i=%d, j=%d",
1148 ovsdb_row_get_uuid(rows[i])->parts[3],
1149 do_transact_get_integer(rows[i], "i"),
1150 do_transact_get_integer(rows[i], "j"));
1157 do_transact(int argc, char *argv[])
1159 static const struct command do_transact_commands[] = {
1160 { "commit", 0, 0, do_transact_commit },
1161 { "abort", 0, 0, do_transact_abort },
1162 { "insert", 2, 3, do_transact_insert },
1163 { "delete", 1, 1, do_transact_delete },
1164 { "modify", 2, 3, do_transact_modify },
1165 { "print", 0, 0, do_transact_print },
1166 { NULL, 0, 0, NULL },
1169 struct ovsdb_schema *schema;
1174 json = parse_json("{\"name\": \"testdb\", "
1178 " {\"i\": {\"type\": \"integer\"}, "
1179 " \"j\": {\"type\": \"integer\"}}}}}");
1180 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
1182 do_transact_db = ovsdb_create(schema);
1183 do_transact_table = ovsdb_get_table(do_transact_db, "mytable");
1184 assert(do_transact_table != NULL);
1186 for (i = 1; i < argc; i++) {
1187 struct json *command;
1192 command = parse_json(argv[i]);
1193 if (command->type != JSON_ARRAY) {
1194 ovs_fatal(0, "transaction %d must be JSON array "
1195 "with at least 1 element", i);
1198 n_args = command->u.array.n;
1199 args = xmalloc((n_args + 1) * sizeof *args);
1200 for (j = 0; j < n_args; j++) {
1201 struct json *s = command->u.array.elems[j];
1202 if (s->type != JSON_STRING) {
1203 ovs_fatal(0, "transaction %d argument %d must be JSON string",
1206 args[j] = xstrdup(json_string(s));
1208 args[n_args] = NULL;
1210 if (!do_transact_txn) {
1211 do_transact_txn = ovsdb_txn_create(do_transact_db);
1214 for (j = 0; j < n_args; j++) {
1218 fputs(args[j], stdout);
1221 run_command(n_args, args, do_transact_commands);
1224 for (j = 0; j < n_args; j++) {
1228 json_destroy(command);
1230 ovsdb_txn_abort(do_transact_txn);
1231 ovsdb_destroy(do_transact_db); /* Also destroys 'schema'. */
1235 compare_link1(const void *a_, const void *b_)
1237 const struct idltest_link1 *const *ap = a_;
1238 const struct idltest_link1 *const *bp = b_;
1239 const struct idltest_link1 *a = *ap;
1240 const struct idltest_link1 *b = *bp;
1242 return a->i < b->i ? -1 : a->i > b->i;
1246 print_idl(struct ovsdb_idl *idl, int step)
1248 const struct idltest_simple *s;
1249 const struct idltest_link1 *l1;
1250 const struct idltest_link2 *l2;
1253 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1256 printf("%03d: i=%"PRId64" r=%g b=%s s=%s u="UUID_FMT" ia=[",
1257 step, s->i, s->r, s->b ? "true" : "false",
1258 s->s, UUID_ARGS(&s->u));
1259 for (i = 0; i < s->n_ia; i++) {
1260 printf("%s%"PRId64, i ? " " : "", s->ia[i]);
1263 for (i = 0; i < s->n_ra; i++) {
1264 printf("%s%g", i ? " " : "", s->ra[i]);
1267 for (i = 0; i < s->n_ba; i++) {
1268 printf("%s%s", i ? " " : "", s->ba[i] ? "true" : "false");
1271 for (i = 0; i < s->n_sa; i++) {
1272 printf("%s%s", i ? " " : "", s->sa[i]);
1275 for (i = 0; i < s->n_ua; i++) {
1276 printf("%s"UUID_FMT, i ? " " : "", UUID_ARGS(&s->ua[i]));
1278 printf("] uuid="UUID_FMT"\n", UUID_ARGS(&s->header_.uuid));
1281 IDLTEST_LINK1_FOR_EACH (l1, idl) {
1282 struct idltest_link1 **links;
1285 printf("%03d: i=%"PRId64" k=", step, l1->i);
1287 printf("%"PRId64, l1->k->i);
1290 links = xmemdup(l1->ka, l1->n_ka * sizeof *l1->ka);
1291 qsort(links, l1->n_ka, sizeof *links, compare_link1);
1292 for (i = 0; i < l1->n_ka; i++) {
1293 printf("%s%"PRId64, i ? " " : "", links[i]->i);
1298 printf("%"PRId64, l1->l2->i);
1300 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l1->header_.uuid));
1303 IDLTEST_LINK2_FOR_EACH (l2, idl) {
1304 printf("%03d: i=%"PRId64" l1=", step, l2->i);
1306 printf("%"PRId64, l2->l1->i);
1308 printf(" uuid="UUID_FMT"\n", UUID_ARGS(&l2->header_.uuid));
1312 printf("%03d: empty\n", step);
1317 print_updated_idl(struct ovsdb_idl *idl, struct jsonrpc *rpc,
1318 int step, unsigned int seqno)
1321 unsigned int new_seqno;
1327 new_seqno = ovsdb_idl_get_seqno(idl);
1328 if (new_seqno != seqno) {
1329 print_idl(idl, step);
1336 ovsdb_idl_wait(idl);
1342 parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
1347 if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
1348 char *name = xasprintf("#%d#", *n);
1349 fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
1350 ovsdb_symbol_table_put(symtab, name, &uuid, false);
1353 } else if (json->type == JSON_ARRAY) {
1356 for (i = 0; i < json->u.array.n; i++) {
1357 parse_uuids(json->u.array.elems[i], symtab, n);
1359 } else if (json->type == JSON_OBJECT) {
1360 const struct shash_node *node;
1362 SHASH_FOR_EACH (node, json_object(json)) {
1363 parse_uuids(node->data, symtab, n);
1369 substitute_uuids(struct json *json, const struct ovsdb_symbol_table *symtab)
1371 if (json->type == JSON_STRING) {
1372 const struct ovsdb_symbol *symbol;
1374 symbol = ovsdb_symbol_table_get(symtab, json->u.string);
1376 free(json->u.string);
1377 json->u.string = xasprintf(UUID_FMT, UUID_ARGS(&symbol->uuid));
1379 } else if (json->type == JSON_ARRAY) {
1382 for (i = 0; i < json->u.array.n; i++) {
1383 substitute_uuids(json->u.array.elems[i], symtab);
1385 } else if (json->type == JSON_OBJECT) {
1386 const struct shash_node *node;
1388 SHASH_FOR_EACH (node, json_object(json)) {
1389 substitute_uuids(node->data, symtab);
1394 static const struct idltest_simple *
1395 idltest_find_simple(struct ovsdb_idl *idl, int i)
1397 const struct idltest_simple *s;
1399 IDLTEST_SIMPLE_FOR_EACH (s, idl) {
1408 idl_set(struct ovsdb_idl *idl, char *commands, int step)
1410 char *cmd, *save_ptr1 = NULL;
1411 struct ovsdb_idl_txn *txn;
1412 enum ovsdb_idl_txn_status status;
1414 txn = ovsdb_idl_txn_create(idl);
1415 for (cmd = strtok_r(commands, ",", &save_ptr1); cmd;
1416 cmd = strtok_r(NULL, ",", &save_ptr1)) {
1417 char *save_ptr2 = NULL;
1418 char *name, *arg1, *arg2, *arg3;
1420 name = strtok_r(cmd, " ", &save_ptr2);
1421 arg1 = strtok_r(NULL, " ", &save_ptr2);
1422 arg2 = strtok_r(NULL, " ", &save_ptr2);
1423 arg3 = strtok_r(NULL, " ", &save_ptr2);
1425 if (!strcmp(name, "set")) {
1426 const struct idltest_simple *s;
1429 ovs_fatal(0, "\"set\" command requires 3 arguments");
1432 s = idltest_find_simple(idl, atoi(arg1));
1434 ovs_fatal(0, "\"set\" command asks for nonexistent "
1435 "i=%d", atoi(arg1));
1438 if (!strcmp(arg2, "b")) {
1439 idltest_simple_set_b(s, atoi(arg3));
1440 } else if (!strcmp(arg2, "s")) {
1441 idltest_simple_set_s(s, arg3);
1442 } else if (!strcmp(arg2, "u")) {
1444 uuid_from_string(&uuid, arg3);
1445 idltest_simple_set_u(s, uuid);
1446 } else if (!strcmp(arg2, "r")) {
1447 idltest_simple_set_r(s, atof(arg3));
1449 ovs_fatal(0, "\"set\" command asks for unknown column %s",
1452 } else if (!strcmp(name, "insert")) {
1453 struct idltest_simple *s;
1455 if (!arg1 || arg2) {
1456 ovs_fatal(0, "\"set\" command requires 1 argument");
1459 s = idltest_simple_insert(txn);
1460 idltest_simple_set_i(s, atoi(arg1));
1461 } else if (!strcmp(name, "delete")) {
1462 const struct idltest_simple *s;
1464 if (!arg1 || arg2) {
1465 ovs_fatal(0, "\"set\" command requires 1 argument");
1468 s = idltest_find_simple(idl, atoi(arg1));
1470 ovs_fatal(0, "\"set\" command asks for nonexistent "
1471 "i=%d", atoi(arg1));
1473 idltest_simple_delete(s);
1475 ovs_fatal(0, "unknown command %s", name);
1479 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1481 ovsdb_idl_wait(idl);
1482 ovsdb_idl_txn_wait(txn);
1485 printf("%03d: commit, status=%s\n",
1486 step, ovsdb_idl_txn_status_to_string(status));
1487 ovsdb_idl_txn_destroy(txn);
1491 do_idl(int argc, char *argv[])
1493 struct jsonrpc *rpc;
1494 struct ovsdb_idl *idl;
1495 unsigned int seqno = 0;
1496 struct ovsdb_symbol_table *symtab;
1502 idl = ovsdb_idl_create(argv[1], &idltest_idl_class);
1504 struct stream *stream;
1506 error = stream_open_block(argv[1], &stream);
1508 ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
1510 rpc = jsonrpc_open(stream);
1515 symtab = ovsdb_symbol_table_create();
1516 for (i = 2; i < argc; i++) {
1517 struct jsonrpc_msg *request, *reply;
1520 seqno = print_updated_idl(idl, rpc, step++, seqno);
1522 if (!strcmp(argv[i], "reconnect")) {
1523 printf("%03d: reconnect\n", step++);
1524 ovsdb_idl_force_reconnect(idl);
1525 } else if (argv[i][0] != '[') {
1526 idl_set(idl, argv[i], step++);
1528 struct json *json = parse_json(argv[i]);
1529 substitute_uuids(json, symtab);
1530 request = jsonrpc_create_request("transact", json, NULL);
1531 error = jsonrpc_transact_block(rpc, request, &reply);
1533 ovs_fatal(error, "jsonrpc transaction failed");
1535 printf("%03d: ", step++);
1536 if (reply->result) {
1537 parse_uuids(reply->result, symtab, &n_uuids);
1539 json_destroy(reply->id);
1541 print_and_free_json(jsonrpc_msg_to_json(reply));
1544 ovsdb_symbol_table_destroy(symtab);
1549 print_updated_idl(idl, NULL, step++, seqno);
1550 ovsdb_idl_destroy(idl);
1551 printf("%03d: done\n", step);
1554 static struct command all_commands[] = {
1555 { "log-io", 2, INT_MAX, do_log_io },
1556 { "parse-atomic-type", 1, 1, do_parse_atomic_type },
1557 { "parse-type", 1, 1, do_parse_type },
1558 { "parse-atoms", 2, INT_MAX, do_parse_atoms },
1559 { "parse-data", 2, INT_MAX, do_parse_data },
1560 { "sort-atoms", 2, 2, do_sort_atoms },
1561 { "parse-column", 2, 2, do_parse_column },
1562 { "parse-table", 2, 2, do_parse_table },
1563 { "parse-rows", 2, INT_MAX, do_parse_rows },
1564 { "compare-rows", 2, INT_MAX, do_compare_rows },
1565 { "parse-conditions", 2, INT_MAX, do_parse_conditions },
1566 { "evaluate-conditions", 3, 3, do_evaluate_conditions },
1567 { "query", 3, 3, do_query },
1568 { "query-distinct", 4, 4, do_query_distinct },
1569 { "transact", 1, INT_MAX, do_transact },
1570 { "execute", 2, INT_MAX, do_execute },
1571 { "trigger", 2, INT_MAX, do_trigger },
1572 { "idl", 1, INT_MAX, do_idl },
1573 { "help", 0, INT_MAX, do_help },
1574 { NULL, 0, 0, NULL },