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 #define THIS_MODULE VLM_ovsdb_client
48 /* --format: Output formatting. */
50 FMT_TABLE, /* Textual table. */
51 FMT_HTML, /* HTML table. */
52 FMT_CSV /* Comma-separated lines. */
55 /* --no-headings: Whether table output should include headings. */
56 static int output_headings = true;
58 /* --pretty: Flags to pass to json_to_string(). */
59 static int json_flags = JSSF_SORT;
61 /* --data: Format of data in output tables. */
63 DF_STRING, /* String format. */
67 static const struct command all_commands[];
69 static void usage(void) NO_RETURN;
70 static void parse_options(int argc, char *argv[]);
73 main(int argc, char *argv[])
75 proctitle_init(argc, argv);
76 set_program_name(argv[0]);
79 parse_options(argc, argv);
80 signal(SIGPIPE, SIG_IGN);
81 run_command(argc - optind, argv + optind, all_commands);
86 parse_options(int argc, char *argv[])
89 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
91 static struct option long_options[] = {
92 {"format", required_argument, 0, 'f'},
93 {"data", required_argument, 0, 'd'},
94 {"no-headings", no_argument, &output_headings, 0},
95 {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
96 {"verbose", optional_argument, 0, 'v'},
97 {"help", no_argument, 0, 'h'},
98 {"version", no_argument, 0, 'V'},
101 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
102 STREAM_SSL_LONG_OPTIONS
106 char *short_options = long_options_to_short_options(long_options);
111 c = getopt_long(argc, argv, short_options, long_options, NULL);
118 if (!strcmp(optarg, "table")) {
119 output_format = FMT_TABLE;
120 } else if (!strcmp(optarg, "html")) {
121 output_format = FMT_HTML;
122 } else if (!strcmp(optarg, "csv")) {
123 output_format = FMT_CSV;
125 ovs_fatal(0, "unknown output format \"%s\"", optarg);
130 if (!strcmp(optarg, "string")) {
131 data_format = DF_STRING;
132 } else if (!strcmp(optarg, "json")) {
133 data_format = DF_JSON;
135 ovs_fatal(0, "unknown data format \"%s\"", optarg);
143 OVS_PRINT_VERSION(0, 0);
147 vlog_set_verbosity(optarg);
150 DAEMON_OPTION_HANDLERS
153 STREAM_SSL_OPTION_HANDLERS
155 case OPT_BOOTSTRAP_CA_CERT:
156 stream_ssl_set_ca_cert_file(optarg, true);
164 /* getopt_long() already set the value for us. */
177 printf("%s: Open vSwitch database JSON-RPC client\n"
178 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
179 "\nValid commands are:\n"
180 "\n list-dbs SERVER\n"
181 " list databases available on SERVER\n"
182 "\n get-schema SERVER DATABASE\n"
183 " retrieve schema for DATABASE from SERVER\n"
184 "\n list-tables SERVER DATABASE\n"
185 " list tables for DATABASE on SERVER\n"
186 "\n list-columns SERVER DATABASE [TABLE]\n"
187 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
188 "\n transact SERVER TRANSACTION\n"
189 " run TRANSACTION (a JSON array of operations) on SERVER\n"
190 " and print the results as JSON on stdout\n"
191 "\n monitor SERVER DATABASE TABLE [COLUMN,...] [SELECT,...]\n"
192 " monitor contents of (COLUMNs in) TABLE in DATABASE on SERVER\n"
193 " Valid SELECTs are: initial, insert, delete, modify\n"
194 "\n dump SERVER DATABASE\n"
195 " dump contents of DATABASE on SERVER to stdout\n",
196 program_name, program_name);
197 stream_usage("SERVER", true, true, true);
198 printf("\nOutput formatting options:\n"
199 " -f, --format=FORMAT set output formatting to FORMAT\n"
200 " (\"table\", \"html\", or \"csv\"\n"
201 " --no-headings omit table heading row\n"
202 " --pretty pretty-print JSON in output");
205 printf("\nOther options:\n"
206 " -h, --help display this help message\n"
207 " -V, --version display version information\n");
212 parse_json(const char *s)
214 struct json *json = json_from_string(s);
215 if (json->type == JSON_STRING) {
216 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
221 static struct jsonrpc *
222 open_jsonrpc(const char *server)
224 struct stream *stream;
227 error = stream_open_block(server, &stream);
228 if (error == EAFNOSUPPORT) {
229 struct pstream *pstream;
231 error = pstream_open(server, &pstream);
233 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
236 VLOG_INFO("%s: waiting for connection...", server);
237 error = pstream_accept_block(pstream, &stream);
239 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
242 pstream_close(pstream);
244 ovs_fatal(error, "failed to connect to \"%s\"", server);
247 return jsonrpc_open(stream);
251 print_json(struct json *json)
253 char *string = json_to_string(json, json_flags);
254 fputs(string, stdout);
259 print_and_free_json(struct json *json)
266 check_ovsdb_error(struct ovsdb_error *error)
269 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
273 static struct ovsdb_schema *
274 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
276 struct jsonrpc_msg *request, *reply;
277 struct ovsdb_schema *schema;
280 request = jsonrpc_create_request("get_schema",
282 json_string_create(database)),
284 error = jsonrpc_transact_block(rpc, request, &reply);
286 ovs_fatal(error, "transaction failed");
288 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
289 jsonrpc_msg_destroy(reply);
294 static struct ovsdb_schema *
295 fetch_schema(const char *server, const char *database)
297 struct ovsdb_schema *schema;
300 rpc = open_jsonrpc(server);
301 schema = fetch_schema_from_rpc(rpc, database);
314 struct column *columns;
315 size_t n_columns, allocated_columns;
316 size_t n_rows, allocated_rows;
317 size_t current_column;
322 table_init(struct table *table)
324 memset(table, 0, sizeof *table);
328 table_destroy(struct table *table)
332 for (i = 0; i < table->n_columns; i++) {
333 free(table->columns[i].heading);
335 free(table->columns);
337 for (i = 0; i < table->n_columns * table->n_rows; i++) {
338 free(table->cells[i]);
342 free(table->caption);
346 table_set_caption(struct table *table, char *caption)
348 free(table->caption);
349 table->caption = caption;
353 table_add_column(struct table *table, const char *heading, ...)
357 table_add_column(struct table *table, const char *heading, ...)
359 struct column *column;
362 assert(!table->n_rows);
363 if (table->n_columns >= table->allocated_columns) {
364 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
365 sizeof *table->columns);
367 column = &table->columns[table->n_columns++];
369 va_start(args, heading);
370 column->heading = xvasprintf(heading, args);
371 column->width = strlen(column->heading);
376 table_cell__(const struct table *table, size_t row, size_t column)
378 return &table->cells[column + row * table->n_columns];
382 table_add_row(struct table *table)
386 if (table->n_rows >= table->allocated_rows) {
387 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
388 table->n_columns * sizeof *table->cells);
392 table->current_column = 0;
393 for (x = 0; x < table->n_columns; x++) {
394 *table_cell__(table, y, x) = NULL;
399 table_add_cell_nocopy(struct table *table, char *s)
404 assert(table->n_rows > 0);
405 assert(table->current_column < table->n_columns);
407 x = table->current_column++;
408 y = table->n_rows - 1;
409 *table_cell__(table, y, x) = s;
412 if (length > table->columns[x].width) {
413 table->columns[x].width = length;
418 table_add_cell(struct table *table, const char *format, ...)
422 va_start(args, format);
423 table_add_cell_nocopy(table, xvasprintf(format, args));
428 table_print_table_line__(struct ds *line)
435 table_print_table__(const struct table *table)
438 struct ds line = DS_EMPTY_INITIALIZER;
445 if (output_headings) {
446 for (x = 0; x < table->n_columns; x++) {
447 const struct column *column = &table->columns[x];
449 ds_put_char(&line, ' ');
451 ds_put_format(&line, "%-*s", column->width, column->heading);
453 table_print_table_line__(&line);
455 for (x = 0; x < table->n_columns; x++) {
456 const struct column *column = &table->columns[x];
460 ds_put_char(&line, ' ');
462 for (i = 0; i < column->width; i++) {
463 ds_put_char(&line, '-');
466 table_print_table_line__(&line);
469 for (y = 0; y < table->n_rows; y++) {
470 for (x = 0; x < table->n_columns; x++) {
471 const char *cell = *table_cell__(table, y, x);
473 ds_put_char(&line, ' ');
475 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
477 table_print_table_line__(&line);
484 table_escape_html_text__(const char *s, size_t n)
488 for (i = 0; i < n; i++) {
493 fputs("&", stdout);
496 fputs("<", stdout);
499 fputs(">", stdout);
502 fputs(""", stdout);
512 table_print_html_cell__(const char *element, const char *content)
516 printf(" <%s>", element);
517 for (p = content; *p; ) {
520 if (uuid_from_string_prefix(&uuid, p)) {
521 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
524 table_escape_html_text__(p, 1);
528 printf("</%s>\n", element);
532 table_print_html__(const struct table *table)
536 fputs("<table border=1>\n", stdout);
538 if (table->caption) {
539 table_print_html_cell__("caption", table->caption);
542 if (output_headings) {
543 fputs(" <tr>\n", stdout);
544 for (x = 0; x < table->n_columns; x++) {
545 const struct column *column = &table->columns[x];
546 table_print_html_cell__("th", column->heading);
548 fputs(" </tr>\n", stdout);
551 for (y = 0; y < table->n_rows; y++) {
552 fputs(" <tr>\n", stdout);
553 for (x = 0; x < table->n_columns; x++) {
554 const char *content = *table_cell__(table, y, x);
556 if (!strcmp(table->columns[x].heading, "_uuid")) {
557 fputs(" <td><a name=\"", stdout);
558 table_escape_html_text__(content, strlen(content));
559 fputs("\">", stdout);
560 table_escape_html_text__(content, 8);
561 fputs("</a></td>\n", stdout);
563 table_print_html_cell__("td", content);
566 fputs(" </tr>\n", stdout);
569 fputs("</table>\n", stdout);
573 table_print_csv_cell__(const char *content)
577 if (!strpbrk(content, "\n\",")) {
578 fputs(content, stdout);
581 for (p = content; *p != '\0'; p++) {
584 fputs("\"\"", stdout);
596 table_print_csv__(const struct table *table)
605 if (table->caption) {
606 puts(table->caption);
609 if (output_headings) {
610 for (x = 0; x < table->n_columns; x++) {
611 const struct column *column = &table->columns[x];
615 table_print_csv_cell__(column->heading);
620 for (y = 0; y < table->n_rows; y++) {
621 for (x = 0; x < table->n_columns; x++) {
625 table_print_csv_cell__(*table_cell__(table, y, x));
632 table_print(const struct table *table)
634 switch (output_format) {
636 table_print_table__(table);
640 table_print_html__(table);
644 table_print_csv__(table);
650 do_list_dbs(int argc OVS_UNUSED, char *argv[])
652 struct jsonrpc_msg *request, *reply;
657 rpc = open_jsonrpc(argv[1]);
658 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
660 error = jsonrpc_transact_block(rpc, request, &reply);
662 ovs_fatal(error, "transaction failed");
665 if (reply->result->type != JSON_ARRAY) {
666 ovs_fatal(0, "list_dbs response is not array");
669 for (i = 0; i < reply->result->u.array.n; i++) {
670 const struct json *name = reply->result->u.array.elems[i];
672 if (name->type != JSON_STRING) {
673 ovs_fatal(0, "list_dbs response %zu is not string", i);
675 puts(name->u.string);
677 jsonrpc_msg_destroy(reply);
681 do_get_schema(int argc OVS_UNUSED, char *argv[])
683 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
684 print_and_free_json(ovsdb_schema_to_json(schema));
685 ovsdb_schema_destroy(schema);
689 do_list_tables(int argc OVS_UNUSED, char *argv[])
691 struct ovsdb_schema *schema;
692 struct shash_node *node;
695 schema = fetch_schema(argv[1], argv[2]);
697 table_add_column(&t, "Table");
698 table_add_column(&t, "Comment");
699 SHASH_FOR_EACH (node, &schema->tables) {
700 struct ovsdb_table_schema *ts = node->data;
703 table_add_cell(&t, ts->name);
705 table_add_cell(&t, ts->comment);
708 ovsdb_schema_destroy(schema);
713 do_list_columns(int argc OVS_UNUSED, char *argv[])
715 const char *table_name = argv[3];
716 struct ovsdb_schema *schema;
717 struct shash_node *table_node;
720 schema = fetch_schema(argv[1], argv[2]);
723 table_add_column(&t, "Table");
725 table_add_column(&t, "Column");
726 table_add_column(&t, "Type");
727 table_add_column(&t, "Comment");
728 SHASH_FOR_EACH (table_node, &schema->tables) {
729 struct ovsdb_table_schema *ts = table_node->data;
731 if (!table_name || !strcmp(table_name, ts->name)) {
732 struct shash_node *column_node;
734 SHASH_FOR_EACH (column_node, &ts->columns) {
735 const struct ovsdb_column *column = column_node->data;
736 struct json *type = ovsdb_type_to_json(&column->type);
740 table_add_cell(&t, ts->name);
742 table_add_cell(&t, column->name);
743 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
744 if (column->comment) {
745 table_add_cell(&t, column->comment);
752 ovsdb_schema_destroy(schema);
757 do_transact(int argc OVS_UNUSED, char *argv[])
759 struct jsonrpc_msg *request, *reply;
760 struct json *transaction;
764 transaction = parse_json(argv[2]);
766 rpc = open_jsonrpc(argv[1]);
767 request = jsonrpc_create_request("transact", transaction, NULL);
768 error = jsonrpc_transact_block(rpc, request, &reply);
770 ovs_fatal(error, "transaction failed");
773 ovs_fatal(error, "transaction returned error: %s",
774 json_to_string(reply->error, json_flags));
776 print_json(reply->result);
778 jsonrpc_msg_destroy(reply);
783 format_json(const struct json *json, const struct ovsdb_type *type)
785 if (data_format == DF_JSON) {
786 return json_to_string(json, JSSF_SORT);
787 } else if (data_format == DF_STRING) {
788 struct ovsdb_datum datum;
789 struct ovsdb_error *error;
792 error = ovsdb_datum_from_json(&datum, type, json, NULL);
794 return json_to_string(json, JSSF_SORT);
798 ovsdb_datum_to_string(&datum, type, &s);
799 ovsdb_datum_destroy(&datum, type);
800 return ds_steal_cstr(&s);
807 monitor_print_row(struct json *row, const char *type, const char *uuid,
808 const struct ovsdb_column_set *columns, struct table *t)
813 ovs_error(0, "missing %s row", type);
815 } else if (row->type != JSON_OBJECT) {
816 ovs_error(0, "<row> is not object");
821 table_add_cell(t, uuid);
822 table_add_cell(t, type);
823 for (i = 0; i < columns->n_columns; i++) {
824 const struct ovsdb_column *column = columns->columns[i];
825 struct json *value = shash_find_data(json_object(row), column->name);
827 table_add_cell_nocopy(t, format_json(value, &column->type));
829 table_add_cell(t, "");
835 monitor_print(struct json *table_updates,
836 const struct ovsdb_table_schema *table,
837 const struct ovsdb_column_set *columns, bool initial)
839 struct json *table_update;
840 struct shash_node *node;
846 if (table_updates->type != JSON_OBJECT) {
847 ovs_error(0, "<table-updates> is not object");
850 table_update = shash_find_data(json_object(table_updates), table->name);
854 if (table_update->type != JSON_OBJECT) {
855 ovs_error(0, "<table-update> is not object");
859 table_add_column(&t, "row");
860 table_add_column(&t, "action");
861 for (i = 0; i < columns->n_columns; i++) {
862 table_add_column(&t, "%s", columns->columns[i]->name);
864 SHASH_FOR_EACH (node, json_object(table_update)) {
865 struct json *row_update = node->data;
866 struct json *old, *new;
868 if (row_update->type != JSON_OBJECT) {
869 ovs_error(0, "<row-update> is not object");
872 old = shash_find_data(json_object(row_update), "old");
873 new = shash_find_data(json_object(row_update), "new");
875 monitor_print_row(new, "initial", node->name, columns, &t);
877 monitor_print_row(new, "insert", node->name, columns, &t);
879 monitor_print_row(old, "delete", node->name, columns, &t);
881 monitor_print_row(old, "old", node->name, columns, &t);
882 monitor_print_row(new, "new", "", columns, &t);
890 do_monitor(int argc, char *argv[])
892 const char *server = argv[1];
893 const char *database = argv[2];
894 const char *table_name = argv[3];
895 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
896 struct ovsdb_table_schema *table;
897 struct ovsdb_schema *schema;
898 struct jsonrpc_msg *request;
900 struct json *select, *monitor, *monitor_request, *monitor_requests,
903 rpc = open_jsonrpc(server);
905 schema = fetch_schema_from_rpc(rpc, database);
906 table = shash_find_data(&schema->tables, table_name);
908 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
909 server, database, table_name);
912 if (argc >= 5 && *argv[4] != '\0') {
913 char *save_ptr = NULL;
916 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
917 token = strtok_r(NULL, ",", &save_ptr)) {
918 const struct ovsdb_column *column;
919 column = ovsdb_table_schema_get_column(table, token);
921 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
922 "column named \"%s\"",
923 server, table_name, database, token);
925 ovsdb_column_set_add(&columns, column);
928 struct shash_node *node;
930 SHASH_FOR_EACH (node, &table->columns) {
931 const struct ovsdb_column *column = node->data;
932 if (column->index != OVSDB_COL_UUID) {
933 ovsdb_column_set_add(&columns, column);
938 if (argc >= 6 && *argv[5] != '\0') {
939 char *save_ptr = NULL;
942 select = json_object_create();
943 for (token = strtok_r(argv[5], ",", &save_ptr); token != NULL;
944 token = strtok_r(NULL, ",", &save_ptr)) {
945 json_object_put(select, token, json_boolean_create(true));
951 monitor_request = json_object_create();
952 json_object_put(monitor_request,
953 "columns", ovsdb_column_set_to_json(&columns));
955 json_object_put(monitor_request, "select", select);
958 monitor_requests = json_object_create();
959 json_object_put(monitor_requests, table_name, monitor_request);
961 monitor = json_array_create_3(json_string_create(database),
962 json_null_create(), monitor_requests);
963 request = jsonrpc_create_request("monitor", monitor, NULL);
964 request_id = json_clone(request->id);
965 jsonrpc_send(rpc, request);
967 struct jsonrpc_msg *msg;
970 error = jsonrpc_recv_block(rpc, &msg);
972 ovsdb_schema_destroy(schema);
973 ovs_fatal(error, "%s: receive failed", server);
976 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
977 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
979 } else if (msg->type == JSONRPC_REPLY
980 && json_equal(msg->id, request_id)) {
981 monitor_print(msg->result, table, &columns, true);
984 /* daemonize() closes the standard file descriptors. We output
985 * to stdout, so we need to save and restore STDOUT_FILENO. */
986 int fd = dup(STDOUT_FILENO);
988 dup2(fd, STDOUT_FILENO);
991 } else if (msg->type == JSONRPC_NOTIFY
992 && !strcmp(msg->method, "update")) {
993 struct json *params = msg->params;
994 if (params->type == JSON_ARRAY
995 && params->u.array.n == 2
996 && params->u.array.elems[0]->type == JSON_NULL) {
997 monitor_print(params->u.array.elems[1],
998 table, &columns, false);
1002 jsonrpc_msg_destroy(msg);
1006 struct dump_table_aux {
1007 struct ovsdb_datum **data;
1008 const struct ovsdb_column **columns;
1013 compare_data(size_t a_y, size_t b_y, size_t x,
1014 const struct dump_table_aux *aux)
1016 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
1018 &aux->columns[x]->type);
1022 compare_rows(size_t a_y, size_t b_y, void *aux_)
1024 struct dump_table_aux *aux = aux_;
1027 /* Skip UUID columns on the first pass, since their values tend to be
1028 * random and make our results less reproducible. */
1029 for (x = 0; x < aux->n_columns; x++) {
1030 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
1031 int cmp = compare_data(a_y, b_y, x, aux);
1038 /* Use UUID columns as tie-breakers. */
1039 for (x = 0; x < aux->n_columns; x++) {
1040 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
1041 int cmp = compare_data(a_y, b_y, x, aux);
1052 swap_rows(size_t a_y, size_t b_y, void *aux_)
1054 struct dump_table_aux *aux = aux_;
1055 struct ovsdb_datum *tmp = aux->data[a_y];
1056 aux->data[a_y] = aux->data[b_y];
1057 aux->data[b_y] = tmp;
1061 format_data(const struct ovsdb_datum *datum, const struct ovsdb_type *type)
1063 if (data_format == DF_JSON) {
1064 struct json *json = ovsdb_datum_to_json(datum, type);
1065 char *s = json_to_string(json, JSSF_SORT);
1068 } else if (data_format == DF_STRING) {
1072 ovsdb_datum_to_string(datum, type, &s);
1073 return ds_steal_cstr(&s);
1080 compare_columns(const void *a_, const void *b_)
1082 const struct ovsdb_column *const *ap = a_;
1083 const struct ovsdb_column *const *bp = b_;
1084 const struct ovsdb_column *a = *ap;
1085 const struct ovsdb_column *b = *bp;
1087 return strcmp(a->name, b->name);
1091 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
1093 const struct ovsdb_column **columns;
1096 struct ovsdb_datum **data;
1098 struct dump_table_aux aux;
1099 struct shash_node *node;
1103 /* Sort columns by name, for reproducibility. */
1104 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
1106 SHASH_FOR_EACH (node, &ts->columns) {
1107 struct ovsdb_column *column = node->data;
1108 if (strcmp(column->name, "_version")) {
1109 columns[n_columns++] = column;
1112 qsort(columns, n_columns, sizeof *columns, compare_columns);
1114 /* Extract data from table. */
1115 data = xmalloc(rows->n * sizeof *data);
1116 for (y = 0; y < rows->n; y++) {
1119 if (rows->elems[y]->type != JSON_OBJECT) {
1120 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
1121 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
1123 row = json_object(rows->elems[y]);
1125 data[y] = xmalloc(n_columns * sizeof **data);
1126 for (x = 0; x < n_columns; x++) {
1127 const struct json *json = shash_find_data(row, columns[x]->name);
1129 ovs_fatal(0, "row %zu in table %s response lacks %s column",
1130 y, ts->name, columns[x]->name);
1133 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
1139 /* Sort rows by column values, for reproducibility. */
1141 aux.columns = columns;
1142 aux.n_columns = n_columns;
1143 sort(rows->n, compare_rows, swap_rows, &aux);
1145 /* Add column headings. */
1147 table_set_caption(&t, xasprintf("%s table", ts->name));
1148 for (x = 0; x < n_columns; x++) {
1149 table_add_column(&t, "%s", columns[x]->name);
1153 for (y = 0; y < rows->n; y++) {
1155 for (x = 0; x < n_columns; x++) {
1156 table_add_cell_nocopy(&t, format_data(&data[y][x],
1157 &columns[x]->type));
1165 do_dump(int argc OVS_UNUSED, char *argv[])
1167 const char *server = argv[1];
1168 const char *database = argv[2];
1170 struct jsonrpc_msg *request, *reply;
1171 struct ovsdb_schema *schema;
1172 struct json *transaction;
1173 struct jsonrpc *rpc;
1176 const struct shash_node **tables;
1181 rpc = open_jsonrpc(server);
1183 schema = fetch_schema_from_rpc(rpc, database);
1184 tables = shash_sort(&schema->tables);
1185 n_tables = shash_count(&schema->tables);
1187 /* Construct transaction to retrieve entire database. */
1188 transaction = json_array_create_1(json_string_create(database));
1189 for (i = 0; i < n_tables; i++) {
1190 const struct ovsdb_table_schema *ts = tables[i]->data;
1191 struct json *op, *columns;
1192 struct shash_node *node;
1194 columns = json_array_create_empty();
1195 SHASH_FOR_EACH (node, &ts->columns) {
1196 const struct ovsdb_column *column = node->data;
1198 if (strcmp(column->name, "_version")) {
1199 json_array_add(columns, json_string_create(column->name));
1203 op = json_object_create();
1204 json_object_put_string(op, "op", "select");
1205 json_object_put_string(op, "table", tables[i]->name);
1206 json_object_put(op, "where", json_array_create_empty());
1207 json_object_put(op, "columns", columns);
1208 json_array_add(transaction, op);
1211 /* Send request, get reply. */
1212 request = jsonrpc_create_request("transact", transaction, NULL);
1213 error = jsonrpc_transact_block(rpc, request, &reply);
1215 ovs_fatal(error, "transaction failed");
1218 /* Print database contents. */
1219 if (reply->result->type != JSON_ARRAY
1220 || reply->result->u.array.n != n_tables) {
1221 ovs_fatal(0, "reply is not array of %zu elements: %s",
1222 n_tables, json_to_string(reply->result, 0));
1224 for (i = 0; i < n_tables; i++) {
1225 const struct ovsdb_table_schema *ts = tables[i]->data;
1226 const struct json *op_result = reply->result->u.array.elems[i];
1229 if (op_result->type != JSON_OBJECT
1230 || !(rows = shash_find_data(json_object(op_result), "rows"))
1231 || rows->type != JSON_ARRAY) {
1232 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1234 ts->name, json_to_string(op_result, 0));
1237 dump_table(ts, &rows->u.array);
1242 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1247 static const struct command all_commands[] = {
1248 { "list-dbs", 1, 1, do_list_dbs },
1249 { "get-schema", 2, 2, do_get_schema },
1250 { "list-tables", 2, 2, do_list_tables },
1251 { "list-columns", 2, 3, do_list_columns },
1252 { "transact", 2, 2, do_transact },
1253 { "monitor", 3, 5, do_monitor },
1254 { "dump", 2, 2, do_dump },
1255 { "help", 0, INT_MAX, do_help },
1256 { NULL, 0, 0, NULL },