2 * Copyright (c) 2009, 2010 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.
28 #include "command-line.h"
32 #include "dynamic-string.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-error.h"
40 #include "stream-ssl.h"
46 VLOG_DEFINE_THIS_MODULE(ovsdb_client);
48 /* --format: Output formatting. */
50 FMT_TABLE, /* Textual table. */
51 FMT_HTML, /* HTML table. */
52 FMT_CSV, /* Comma-separated lines. */
56 /* --no-headings: Whether table output should include headings. */
57 static int output_headings = true;
59 /* --pretty: Flags to pass to json_to_string(). */
60 static int json_flags = JSSF_SORT;
62 /* --data: Format of data in output tables. */
64 DF_STRING, /* String format. */
68 static const struct command all_commands[];
70 static void usage(void) NO_RETURN;
71 static void parse_options(int argc, char *argv[]);
74 main(int argc, char *argv[])
76 proctitle_init(argc, argv);
77 set_program_name(argv[0]);
78 parse_options(argc, argv);
79 signal(SIGPIPE, SIG_IGN);
80 run_command(argc - optind, argv + optind, all_commands);
85 parse_options(int argc, char *argv[])
88 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
90 static struct option long_options[] = {
91 {"format", required_argument, 0, 'f'},
92 {"data", required_argument, 0, 'd'},
93 {"no-headings", no_argument, &output_headings, 0},
94 {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
95 {"verbose", optional_argument, 0, 'v'},
96 {"help", no_argument, 0, 'h'},
97 {"version", no_argument, 0, 'V'},
100 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
101 STREAM_SSL_LONG_OPTIONS
105 char *short_options = long_options_to_short_options(long_options);
110 c = getopt_long(argc, argv, short_options, long_options, NULL);
117 if (!strcmp(optarg, "table")) {
118 output_format = FMT_TABLE;
119 } else if (!strcmp(optarg, "html")) {
120 output_format = FMT_HTML;
121 } else if (!strcmp(optarg, "csv")) {
122 output_format = FMT_CSV;
123 } else if (!strcmp(optarg, "json")) {
124 output_format = FMT_JSON;
126 ovs_fatal(0, "unknown output format \"%s\"", optarg);
131 if (!strcmp(optarg, "string")) {
132 data_format = DF_STRING;
133 } else if (!strcmp(optarg, "json")) {
134 data_format = DF_JSON;
136 ovs_fatal(0, "unknown data format \"%s\"", optarg);
144 OVS_PRINT_VERSION(0, 0);
148 vlog_set_verbosity(optarg);
151 DAEMON_OPTION_HANDLERS
154 STREAM_SSL_OPTION_HANDLERS
156 case OPT_BOOTSTRAP_CA_CERT:
157 stream_ssl_set_ca_cert_file(optarg, true);
165 /* getopt_long() already set the value for us. */
178 printf("%s: Open vSwitch database JSON-RPC client\n"
179 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
180 "\nValid commands are:\n"
181 "\n list-dbs SERVER\n"
182 " list databases available on SERVER\n"
183 "\n get-schema SERVER DATABASE\n"
184 " retrieve schema for DATABASE from SERVER\n"
185 "\n list-tables SERVER DATABASE\n"
186 " list tables for DATABASE on SERVER\n"
187 "\n list-columns SERVER DATABASE [TABLE]\n"
188 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
189 "\n transact SERVER TRANSACTION\n"
190 " run TRANSACTION (a JSON array of operations) on SERVER\n"
191 " and print the results as JSON on stdout\n"
192 "\n monitor SERVER DATABASE TABLE [COLUMN,...]...\n"
193 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
194 " COLUMNs may include !initial, !insert, !delete, !modify\n"
195 " to avoid seeing the specified kinds of changes.\n"
196 "\n dump SERVER DATABASE\n"
197 " dump contents of DATABASE on SERVER to stdout\n",
198 program_name, program_name);
199 stream_usage("SERVER", true, true, true);
200 printf("\nOutput formatting options:\n"
201 " -f, --format=FORMAT set output formatting to FORMAT\n"
202 " (\"table\", \"html\", \"csv\", "
204 " --no-headings omit table heading row\n"
205 " --pretty pretty-print JSON in output");
208 printf("\nOther options:\n"
209 " -h, --help display this help message\n"
210 " -V, --version display version information\n");
215 parse_json(const char *s)
217 struct json *json = json_from_string(s);
218 if (json->type == JSON_STRING) {
219 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
224 static struct jsonrpc *
225 open_jsonrpc(const char *server)
227 struct stream *stream;
230 error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
231 if (error == EAFNOSUPPORT) {
232 struct pstream *pstream;
234 error = jsonrpc_pstream_open(server, &pstream);
236 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
239 VLOG_INFO("%s: waiting for connection...", server);
240 error = pstream_accept_block(pstream, &stream);
242 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
245 pstream_close(pstream);
247 ovs_fatal(error, "failed to connect to \"%s\"", server);
250 return jsonrpc_open(stream);
254 print_json(struct json *json)
256 char *string = json_to_string(json, json_flags);
257 fputs(string, stdout);
262 print_and_free_json(struct json *json)
269 check_ovsdb_error(struct ovsdb_error *error)
272 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
276 static struct ovsdb_schema *
277 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
279 struct jsonrpc_msg *request, *reply;
280 struct ovsdb_schema *schema;
283 request = jsonrpc_create_request("get_schema",
285 json_string_create(database)),
287 error = jsonrpc_transact_block(rpc, request, &reply);
289 ovs_fatal(error, "transaction failed");
291 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
292 jsonrpc_msg_destroy(reply);
297 static struct ovsdb_schema *
298 fetch_schema(const char *server, const char *database)
300 struct ovsdb_schema *schema;
303 rpc = open_jsonrpc(server);
304 schema = fetch_schema_from_rpc(rpc, database);
320 const struct ovsdb_type *type;
324 cell_to_text(const struct cell *cell_)
326 struct cell *cell = (struct cell *) cell_;
329 if (data_format == DF_JSON || !cell->type) {
330 cell->text = json_to_string(cell->json, JSSF_SORT);
331 } else if (data_format == DF_STRING) {
332 struct ovsdb_datum datum;
333 struct ovsdb_error *error;
336 error = ovsdb_datum_from_json(&datum, cell->type, cell->json,
340 ovsdb_datum_to_string(&datum, cell->type, &s);
341 ovsdb_datum_destroy(&datum, cell->type);
342 cell->text = ds_steal_cstr(&s);
344 cell->text = json_to_string(cell->json, JSSF_SORT);
350 cell->text = xstrdup("");
358 cell_destroy(struct cell *cell)
361 json_destroy(cell->json);
366 struct column *columns;
367 size_t n_columns, allocated_columns;
368 size_t n_rows, allocated_rows;
369 size_t current_column;
374 table_init(struct table *table)
376 memset(table, 0, sizeof *table);
380 table_destroy(struct table *table)
384 for (i = 0; i < table->n_columns; i++) {
385 free(table->columns[i].heading);
387 free(table->columns);
389 for (i = 0; i < table->n_columns * table->n_rows; i++) {
390 cell_destroy(&table->cells[i]);
394 free(table->caption);
398 table_set_caption(struct table *table, char *caption)
400 free(table->caption);
401 table->caption = caption;
405 table_add_column(struct table *table, const char *heading, ...)
409 table_add_column(struct table *table, const char *heading, ...)
411 struct column *column;
414 assert(!table->n_rows);
415 if (table->n_columns >= table->allocated_columns) {
416 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
417 sizeof *table->columns);
419 column = &table->columns[table->n_columns++];
421 va_start(args, heading);
422 column->heading = xvasprintf(heading, args);
427 table_cell__(const struct table *table, size_t row, size_t column)
429 return &table->cells[column + row * table->n_columns];
433 table_add_row(struct table *table)
437 if (table->n_rows >= table->allocated_rows) {
438 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
439 table->n_columns * sizeof *table->cells);
443 table->current_column = 0;
444 for (x = 0; x < table->n_columns; x++) {
445 struct cell *cell = table_cell__(table, y, x);
446 memset(cell, 0, sizeof *cell);
451 table_add_cell(struct table *table)
455 assert(table->n_rows > 0);
456 assert(table->current_column < table->n_columns);
458 x = table->current_column++;
459 y = table->n_rows - 1;
461 return table_cell__(table, y, x);
465 table_print_table_line__(struct ds *line)
472 table_print_table__(const struct table *table)
475 struct ds line = DS_EMPTY_INITIALIZER;
483 if (table->caption) {
484 puts(table->caption);
487 widths = xmalloc(table->n_columns * sizeof *widths);
488 for (x = 0; x < table->n_columns; x++) {
489 const struct column *column = &table->columns[x];
491 widths[x] = strlen(column->heading);
492 for (y = 0; y < table->n_rows; y++) {
493 const char *text = cell_to_text(table_cell__(table, y, x));
494 size_t length = strlen(text);
496 if (length > widths[x])
501 if (output_headings) {
502 for (x = 0; x < table->n_columns; x++) {
503 const struct column *column = &table->columns[x];
505 ds_put_char(&line, ' ');
507 ds_put_format(&line, "%-*s", widths[x], column->heading);
509 table_print_table_line__(&line);
511 for (x = 0; x < table->n_columns; x++) {
513 ds_put_char(&line, ' ');
515 ds_put_char_multiple(&line, '-', widths[x]);
517 table_print_table_line__(&line);
520 for (y = 0; y < table->n_rows; y++) {
521 for (x = 0; x < table->n_columns; x++) {
522 const char *text = cell_to_text(table_cell__(table, y, x));
524 ds_put_char(&line, ' ');
526 ds_put_format(&line, "%-*s", widths[x], text);
528 table_print_table_line__(&line);
536 table_escape_html_text__(const char *s, size_t n)
540 for (i = 0; i < n; i++) {
545 fputs("&", stdout);
548 fputs("<", stdout);
551 fputs(">", stdout);
554 fputs(""", stdout);
564 table_print_html_cell__(const char *element, const char *content)
568 printf(" <%s>", element);
569 for (p = content; *p; ) {
572 if (uuid_from_string_prefix(&uuid, p)) {
573 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
576 table_escape_html_text__(p, 1);
580 printf("</%s>\n", element);
584 table_print_html__(const struct table *table)
588 fputs("<table border=1>\n", stdout);
590 if (table->caption) {
591 table_print_html_cell__("caption", table->caption);
594 if (output_headings) {
595 fputs(" <tr>\n", stdout);
596 for (x = 0; x < table->n_columns; x++) {
597 const struct column *column = &table->columns[x];
598 table_print_html_cell__("th", column->heading);
600 fputs(" </tr>\n", stdout);
603 for (y = 0; y < table->n_rows; y++) {
604 fputs(" <tr>\n", stdout);
605 for (x = 0; x < table->n_columns; x++) {
606 const char *content = cell_to_text(table_cell__(table, y, x));
608 if (!strcmp(table->columns[x].heading, "_uuid")) {
609 fputs(" <td><a name=\"", stdout);
610 table_escape_html_text__(content, strlen(content));
611 fputs("\">", stdout);
612 table_escape_html_text__(content, 8);
613 fputs("</a></td>\n", stdout);
615 table_print_html_cell__("td", content);
618 fputs(" </tr>\n", stdout);
621 fputs("</table>\n", stdout);
625 table_print_csv_cell__(const char *content)
629 if (!strpbrk(content, "\n\",")) {
630 fputs(content, stdout);
633 for (p = content; *p != '\0'; p++) {
636 fputs("\"\"", stdout);
648 table_print_csv__(const struct table *table)
657 if (table->caption) {
658 puts(table->caption);
661 if (output_headings) {
662 for (x = 0; x < table->n_columns; x++) {
663 const struct column *column = &table->columns[x];
667 table_print_csv_cell__(column->heading);
672 for (y = 0; y < table->n_rows; y++) {
673 for (x = 0; x < table->n_columns; x++) {
677 table_print_csv_cell__(cell_to_text(table_cell__(table, y, x)));
684 table_print_json__(const struct table *table)
686 struct json *json, *headings, *data;
690 json = json_object_create();
691 if (table->caption) {
692 json_object_put_string(json, "caption", table->caption);
695 headings = json_array_create_empty();
696 for (x = 0; x < table->n_columns; x++) {
697 const struct column *column = &table->columns[x];
698 json_array_add(headings, json_string_create(column->heading));
700 json_object_put(json, "headings", headings);
702 data = json_array_create_empty();
703 for (y = 0; y < table->n_rows; y++) {
704 struct json *row = json_array_create_empty();
705 for (x = 0; x < table->n_columns; x++) {
706 const struct cell *cell = table_cell__(table, y, x);
708 json_array_add(row, json_string_create(cell->text));
710 json_array_add(row, json_clone(cell->json));
713 json_array_add(data, row);
715 json_object_put(json, "data", data);
717 s = json_to_string(json, json_flags);
724 table_print(const struct table *table)
726 switch (output_format) {
728 table_print_table__(table);
732 table_print_html__(table);
736 table_print_csv__(table);
740 table_print_json__(table);
746 do_list_dbs(int argc OVS_UNUSED, char *argv[])
748 struct jsonrpc_msg *request, *reply;
753 rpc = open_jsonrpc(argv[1]);
754 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
756 error = jsonrpc_transact_block(rpc, request, &reply);
758 ovs_fatal(error, "transaction failed");
761 if (reply->result->type != JSON_ARRAY) {
762 ovs_fatal(0, "list_dbs response is not array");
765 for (i = 0; i < reply->result->u.array.n; i++) {
766 const struct json *name = reply->result->u.array.elems[i];
768 if (name->type != JSON_STRING) {
769 ovs_fatal(0, "list_dbs response %zu is not string", i);
771 puts(name->u.string);
773 jsonrpc_msg_destroy(reply);
777 do_get_schema(int argc OVS_UNUSED, char *argv[])
779 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
780 print_and_free_json(ovsdb_schema_to_json(schema));
781 ovsdb_schema_destroy(schema);
785 do_list_tables(int argc OVS_UNUSED, char *argv[])
787 struct ovsdb_schema *schema;
788 struct shash_node *node;
791 schema = fetch_schema(argv[1], argv[2]);
793 table_add_column(&t, "Table");
794 SHASH_FOR_EACH (node, &schema->tables) {
795 struct ovsdb_table_schema *ts = node->data;
798 table_add_cell(&t)->text = xstrdup(ts->name);
800 ovsdb_schema_destroy(schema);
805 do_list_columns(int argc OVS_UNUSED, char *argv[])
807 const char *table_name = argv[3];
808 struct ovsdb_schema *schema;
809 struct shash_node *table_node;
812 schema = fetch_schema(argv[1], argv[2]);
815 table_add_column(&t, "Table");
817 table_add_column(&t, "Column");
818 table_add_column(&t, "Type");
819 SHASH_FOR_EACH (table_node, &schema->tables) {
820 struct ovsdb_table_schema *ts = table_node->data;
822 if (!table_name || !strcmp(table_name, ts->name)) {
823 struct shash_node *column_node;
825 SHASH_FOR_EACH (column_node, &ts->columns) {
826 const struct ovsdb_column *column = column_node->data;
830 table_add_cell(&t)->text = xstrdup(ts->name);
832 table_add_cell(&t)->text = xstrdup(column->name);
833 table_add_cell(&t)->json = ovsdb_type_to_json(&column->type);
837 ovsdb_schema_destroy(schema);
842 do_transact(int argc OVS_UNUSED, char *argv[])
844 struct jsonrpc_msg *request, *reply;
845 struct json *transaction;
849 transaction = parse_json(argv[2]);
851 rpc = open_jsonrpc(argv[1]);
852 request = jsonrpc_create_request("transact", transaction, NULL);
853 error = jsonrpc_transact_block(rpc, request, &reply);
855 ovs_fatal(error, "transaction failed");
858 ovs_fatal(error, "transaction returned error: %s",
859 json_to_string(reply->error, json_flags));
861 print_json(reply->result);
863 jsonrpc_msg_destroy(reply);
868 monitor_print_row(struct json *row, const char *type, const char *uuid,
869 const struct ovsdb_column_set *columns, struct table *t)
874 ovs_error(0, "missing %s row", type);
876 } else if (row->type != JSON_OBJECT) {
877 ovs_error(0, "<row> is not object");
882 table_add_cell(t)->text = xstrdup(uuid);
883 table_add_cell(t)->text = xstrdup(type);
884 for (i = 0; i < columns->n_columns; i++) {
885 const struct ovsdb_column *column = columns->columns[i];
886 struct json *value = shash_find_data(json_object(row), column->name);
887 struct cell *cell = table_add_cell(t);
889 cell->json = json_clone(value);
890 cell->type = &column->type;
896 monitor_print(struct json *table_updates,
897 const struct ovsdb_table_schema *table,
898 const struct ovsdb_column_set *columns, bool initial)
900 struct json *table_update;
901 struct shash_node *node;
907 if (table_updates->type != JSON_OBJECT) {
908 ovs_error(0, "<table-updates> is not object");
911 table_update = shash_find_data(json_object(table_updates), table->name);
915 if (table_update->type != JSON_OBJECT) {
916 ovs_error(0, "<table-update> is not object");
920 table_add_column(&t, "row");
921 table_add_column(&t, "action");
922 for (i = 0; i < columns->n_columns; i++) {
923 table_add_column(&t, "%s", columns->columns[i]->name);
925 SHASH_FOR_EACH (node, json_object(table_update)) {
926 struct json *row_update = node->data;
927 struct json *old, *new;
929 if (row_update->type != JSON_OBJECT) {
930 ovs_error(0, "<row-update> is not object");
933 old = shash_find_data(json_object(row_update), "old");
934 new = shash_find_data(json_object(row_update), "new");
936 monitor_print_row(new, "initial", node->name, columns, &t);
938 monitor_print_row(new, "insert", node->name, columns, &t);
940 monitor_print_row(old, "delete", node->name, columns, &t);
942 monitor_print_row(old, "old", node->name, columns, &t);
943 monitor_print_row(new, "new", "", columns, &t);
951 add_column(const char *server, const struct ovsdb_column *column,
952 struct ovsdb_column_set *columns, struct json *columns_json)
954 if (ovsdb_column_set_contains(columns, column->index)) {
955 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
956 server, column->name);
958 ovsdb_column_set_add(columns, column);
959 json_array_add(columns_json, json_string_create(column->name));
963 parse_monitor_columns(char *arg, const char *server, const char *database,
964 const struct ovsdb_table_schema *table,
965 struct ovsdb_column_set *columns)
967 bool initial, insert, delete, modify;
968 struct json *mr, *columns_json;
969 char *save_ptr = NULL;
972 mr = json_object_create();
973 columns_json = json_array_create_empty();
974 json_object_put(mr, "columns", columns_json);
976 initial = insert = delete = modify = true;
977 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
978 token = strtok_r(NULL, ",", &save_ptr)) {
979 if (!strcmp(token, "!initial")) {
981 } else if (!strcmp(token, "!insert")) {
983 } else if (!strcmp(token, "!delete")) {
985 } else if (!strcmp(token, "!modify")) {
988 const struct ovsdb_column *column;
990 column = ovsdb_table_schema_get_column(table, token);
992 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
993 "column named \"%s\"",
994 server, table->name, database, token);
996 add_column(server, column, columns, columns_json);
1000 if (columns_json->u.array.n == 0) {
1001 const struct shash_node **nodes;
1004 n = shash_count(&table->columns);
1005 nodes = shash_sort(&table->columns);
1006 for (i = 0; i < n; i++) {
1007 const struct ovsdb_column *column = nodes[i]->data;
1008 if (column->index != OVSDB_COL_UUID
1009 && column->index != OVSDB_COL_VERSION) {
1010 add_column(server, column, columns, columns_json);
1015 add_column(server, ovsdb_table_schema_get_column(table,"_version"),
1016 columns, columns_json);
1019 if (!initial || !insert || !delete || !modify) {
1020 struct json *select = json_object_create();
1021 json_object_put(select, "initial", json_boolean_create(initial));
1022 json_object_put(select, "insert", json_boolean_create(insert));
1023 json_object_put(select, "delete", json_boolean_create(delete));
1024 json_object_put(select, "modify", json_boolean_create(modify));
1025 json_object_put(mr, "select", select);
1032 do_monitor(int argc, char *argv[])
1034 const char *server = argv[1];
1035 const char *database = argv[2];
1036 const char *table_name = argv[3];
1037 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
1038 struct ovsdb_table_schema *table;
1039 struct ovsdb_schema *schema;
1040 struct jsonrpc_msg *request;
1041 struct jsonrpc *rpc;
1042 struct json *monitor, *monitor_request_array,
1043 *monitor_requests, *request_id;
1045 rpc = open_jsonrpc(server);
1047 schema = fetch_schema_from_rpc(rpc, database);
1048 table = shash_find_data(&schema->tables, table_name);
1050 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
1051 server, database, table_name);
1054 monitor_request_array = json_array_create_empty();
1058 for (i = 4; i < argc; i++) {
1060 monitor_request_array,
1061 parse_monitor_columns(argv[i], server, database, table,
1065 /* Allocate a writable empty string since parse_monitor_columns() is
1066 * going to strtok() it and that's risky with literal "". */
1069 monitor_request_array,
1070 parse_monitor_columns(empty, server, database, table, &columns));
1073 monitor_requests = json_object_create();
1074 json_object_put(monitor_requests, table_name, monitor_request_array);
1076 monitor = json_array_create_3(json_string_create(database),
1077 json_null_create(), monitor_requests);
1078 request = jsonrpc_create_request("monitor", monitor, NULL);
1079 request_id = json_clone(request->id);
1080 jsonrpc_send(rpc, request);
1082 struct jsonrpc_msg *msg;
1085 error = jsonrpc_recv_block(rpc, &msg);
1087 ovsdb_schema_destroy(schema);
1088 ovs_fatal(error, "%s: receive failed", server);
1091 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
1092 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
1094 } else if (msg->type == JSONRPC_REPLY
1095 && json_equal(msg->id, request_id)) {
1096 monitor_print(msg->result, table, &columns, true);
1099 /* daemonize() closes the standard file descriptors. We output
1100 * to stdout, so we need to save and restore STDOUT_FILENO. */
1101 int fd = dup(STDOUT_FILENO);
1103 dup2(fd, STDOUT_FILENO);
1106 } else if (msg->type == JSONRPC_NOTIFY
1107 && !strcmp(msg->method, "update")) {
1108 struct json *params = msg->params;
1109 if (params->type == JSON_ARRAY
1110 && params->u.array.n == 2
1111 && params->u.array.elems[0]->type == JSON_NULL) {
1112 monitor_print(params->u.array.elems[1],
1113 table, &columns, false);
1117 jsonrpc_msg_destroy(msg);
1121 struct dump_table_aux {
1122 struct ovsdb_datum **data;
1123 const struct ovsdb_column **columns;
1128 compare_data(size_t a_y, size_t b_y, size_t x,
1129 const struct dump_table_aux *aux)
1131 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
1133 &aux->columns[x]->type);
1137 compare_rows(size_t a_y, size_t b_y, void *aux_)
1139 struct dump_table_aux *aux = aux_;
1142 /* Skip UUID columns on the first pass, since their values tend to be
1143 * random and make our results less reproducible. */
1144 for (x = 0; x < aux->n_columns; x++) {
1145 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
1146 int cmp = compare_data(a_y, b_y, x, aux);
1153 /* Use UUID columns as tie-breakers. */
1154 for (x = 0; x < aux->n_columns; x++) {
1155 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
1156 int cmp = compare_data(a_y, b_y, x, aux);
1167 swap_rows(size_t a_y, size_t b_y, void *aux_)
1169 struct dump_table_aux *aux = aux_;
1170 struct ovsdb_datum *tmp = aux->data[a_y];
1171 aux->data[a_y] = aux->data[b_y];
1172 aux->data[b_y] = tmp;
1176 compare_columns(const void *a_, const void *b_)
1178 const struct ovsdb_column *const *ap = a_;
1179 const struct ovsdb_column *const *bp = b_;
1180 const struct ovsdb_column *a = *ap;
1181 const struct ovsdb_column *b = *bp;
1183 return strcmp(a->name, b->name);
1187 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
1189 const struct ovsdb_column **columns;
1192 struct ovsdb_datum **data;
1194 struct dump_table_aux aux;
1195 struct shash_node *node;
1199 /* Sort columns by name, for reproducibility. */
1200 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
1202 SHASH_FOR_EACH (node, &ts->columns) {
1203 struct ovsdb_column *column = node->data;
1204 if (strcmp(column->name, "_version")) {
1205 columns[n_columns++] = column;
1208 qsort(columns, n_columns, sizeof *columns, compare_columns);
1210 /* Extract data from table. */
1211 data = xmalloc(rows->n * sizeof *data);
1212 for (y = 0; y < rows->n; y++) {
1215 if (rows->elems[y]->type != JSON_OBJECT) {
1216 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
1217 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
1219 row = json_object(rows->elems[y]);
1221 data[y] = xmalloc(n_columns * sizeof **data);
1222 for (x = 0; x < n_columns; x++) {
1223 const struct json *json = shash_find_data(row, columns[x]->name);
1225 ovs_fatal(0, "row %zu in table %s response lacks %s column",
1226 y, ts->name, columns[x]->name);
1229 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
1235 /* Sort rows by column values, for reproducibility. */
1237 aux.columns = columns;
1238 aux.n_columns = n_columns;
1239 sort(rows->n, compare_rows, swap_rows, &aux);
1241 /* Add column headings. */
1243 table_set_caption(&t, xasprintf("%s table", ts->name));
1244 for (x = 0; x < n_columns; x++) {
1245 table_add_column(&t, "%s", columns[x]->name);
1249 for (y = 0; y < rows->n; y++) {
1251 for (x = 0; x < n_columns; x++) {
1252 struct cell *cell = table_add_cell(&t);
1253 cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
1254 cell->type = &columns[x]->type;
1262 do_dump(int argc OVS_UNUSED, char *argv[])
1264 const char *server = argv[1];
1265 const char *database = argv[2];
1267 struct jsonrpc_msg *request, *reply;
1268 struct ovsdb_schema *schema;
1269 struct json *transaction;
1270 struct jsonrpc *rpc;
1273 const struct shash_node **tables;
1278 rpc = open_jsonrpc(server);
1280 schema = fetch_schema_from_rpc(rpc, database);
1281 tables = shash_sort(&schema->tables);
1282 n_tables = shash_count(&schema->tables);
1284 /* Construct transaction to retrieve entire database. */
1285 transaction = json_array_create_1(json_string_create(database));
1286 for (i = 0; i < n_tables; i++) {
1287 const struct ovsdb_table_schema *ts = tables[i]->data;
1288 struct json *op, *columns;
1289 struct shash_node *node;
1291 columns = json_array_create_empty();
1292 SHASH_FOR_EACH (node, &ts->columns) {
1293 const struct ovsdb_column *column = node->data;
1295 if (strcmp(column->name, "_version")) {
1296 json_array_add(columns, json_string_create(column->name));
1300 op = json_object_create();
1301 json_object_put_string(op, "op", "select");
1302 json_object_put_string(op, "table", tables[i]->name);
1303 json_object_put(op, "where", json_array_create_empty());
1304 json_object_put(op, "columns", columns);
1305 json_array_add(transaction, op);
1308 /* Send request, get reply. */
1309 request = jsonrpc_create_request("transact", transaction, NULL);
1310 error = jsonrpc_transact_block(rpc, request, &reply);
1312 ovs_fatal(error, "transaction failed");
1315 /* Print database contents. */
1316 if (reply->result->type != JSON_ARRAY
1317 || reply->result->u.array.n != n_tables) {
1318 ovs_fatal(0, "reply is not array of %zu elements: %s",
1319 n_tables, json_to_string(reply->result, 0));
1321 for (i = 0; i < n_tables; i++) {
1322 const struct ovsdb_table_schema *ts = tables[i]->data;
1323 const struct json *op_result = reply->result->u.array.elems[i];
1326 if (op_result->type != JSON_OBJECT
1327 || !(rows = shash_find_data(json_object(op_result), "rows"))
1328 || rows->type != JSON_ARRAY) {
1329 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1331 ts->name, json_to_string(op_result, 0));
1334 dump_table(ts, &rows->u.array);
1339 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1344 static const struct command all_commands[] = {
1345 { "list-dbs", 1, 1, do_list_dbs },
1346 { "get-schema", 2, 2, do_get_schema },
1347 { "list-tables", 2, 2, do_list_tables },
1348 { "list-columns", 2, 3, do_list_columns },
1349 { "transact", 2, 2, do_transact },
1350 { "monitor", 3, INT_MAX, do_monitor },
1351 { "dump", 2, 2, do_dump },
1352 { "help", 0, INT_MAX, do_help },
1353 { NULL, 0, 0, NULL },