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,...]...\n"
192 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
193 " COLUMNs may include !initial, !insert, !delete, !modify\n"
194 " to avoid seeing the specified kinds of changes.\n"
195 "\n dump SERVER DATABASE\n"
196 " dump contents of DATABASE on SERVER to stdout\n",
197 program_name, program_name);
198 stream_usage("SERVER", true, true, true);
199 printf("\nOutput formatting options:\n"
200 " -f, --format=FORMAT set output formatting to FORMAT\n"
201 " (\"table\", \"html\", or \"csv\"\n"
202 " --no-headings omit table heading row\n"
203 " --pretty pretty-print JSON in output");
206 printf("\nOther options:\n"
207 " -h, --help display this help message\n"
208 " -V, --version display version information\n");
213 parse_json(const char *s)
215 struct json *json = json_from_string(s);
216 if (json->type == JSON_STRING) {
217 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
222 static struct jsonrpc *
223 open_jsonrpc(const char *server)
225 struct stream *stream;
228 error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
229 if (error == EAFNOSUPPORT) {
230 struct pstream *pstream;
232 error = jsonrpc_pstream_open(server, &pstream);
234 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
237 VLOG_INFO("%s: waiting for connection...", server);
238 error = pstream_accept_block(pstream, &stream);
240 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
243 pstream_close(pstream);
245 ovs_fatal(error, "failed to connect to \"%s\"", server);
248 return jsonrpc_open(stream);
252 print_json(struct json *json)
254 char *string = json_to_string(json, json_flags);
255 fputs(string, stdout);
260 print_and_free_json(struct json *json)
267 check_ovsdb_error(struct ovsdb_error *error)
270 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
274 static struct ovsdb_schema *
275 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
277 struct jsonrpc_msg *request, *reply;
278 struct ovsdb_schema *schema;
281 request = jsonrpc_create_request("get_schema",
283 json_string_create(database)),
285 error = jsonrpc_transact_block(rpc, request, &reply);
287 ovs_fatal(error, "transaction failed");
289 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
290 jsonrpc_msg_destroy(reply);
295 static struct ovsdb_schema *
296 fetch_schema(const char *server, const char *database)
298 struct ovsdb_schema *schema;
301 rpc = open_jsonrpc(server);
302 schema = fetch_schema_from_rpc(rpc, database);
315 struct column *columns;
316 size_t n_columns, allocated_columns;
317 size_t n_rows, allocated_rows;
318 size_t current_column;
323 table_init(struct table *table)
325 memset(table, 0, sizeof *table);
329 table_destroy(struct table *table)
333 for (i = 0; i < table->n_columns; i++) {
334 free(table->columns[i].heading);
336 free(table->columns);
338 for (i = 0; i < table->n_columns * table->n_rows; i++) {
339 free(table->cells[i]);
343 free(table->caption);
347 table_set_caption(struct table *table, char *caption)
349 free(table->caption);
350 table->caption = caption;
354 table_add_column(struct table *table, const char *heading, ...)
358 table_add_column(struct table *table, const char *heading, ...)
360 struct column *column;
363 assert(!table->n_rows);
364 if (table->n_columns >= table->allocated_columns) {
365 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
366 sizeof *table->columns);
368 column = &table->columns[table->n_columns++];
370 va_start(args, heading);
371 column->heading = xvasprintf(heading, args);
372 column->width = strlen(column->heading);
377 table_cell__(const struct table *table, size_t row, size_t column)
379 return &table->cells[column + row * table->n_columns];
383 table_add_row(struct table *table)
387 if (table->n_rows >= table->allocated_rows) {
388 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
389 table->n_columns * sizeof *table->cells);
393 table->current_column = 0;
394 for (x = 0; x < table->n_columns; x++) {
395 *table_cell__(table, y, x) = NULL;
400 table_add_cell_nocopy(struct table *table, char *s)
405 assert(table->n_rows > 0);
406 assert(table->current_column < table->n_columns);
408 x = table->current_column++;
409 y = table->n_rows - 1;
410 *table_cell__(table, y, x) = s;
413 if (length > table->columns[x].width) {
414 table->columns[x].width = length;
419 table_add_cell(struct table *table, const char *format, ...)
423 va_start(args, format);
424 table_add_cell_nocopy(table, xvasprintf(format, args));
429 table_print_table_line__(struct ds *line)
436 table_print_table__(const struct table *table)
439 struct ds line = DS_EMPTY_INITIALIZER;
446 if (output_headings) {
447 for (x = 0; x < table->n_columns; x++) {
448 const struct column *column = &table->columns[x];
450 ds_put_char(&line, ' ');
452 ds_put_format(&line, "%-*s", column->width, column->heading);
454 table_print_table_line__(&line);
456 for (x = 0; x < table->n_columns; x++) {
457 const struct column *column = &table->columns[x];
461 ds_put_char(&line, ' ');
463 for (i = 0; i < column->width; i++) {
464 ds_put_char(&line, '-');
467 table_print_table_line__(&line);
470 for (y = 0; y < table->n_rows; y++) {
471 for (x = 0; x < table->n_columns; x++) {
472 const char *cell = *table_cell__(table, y, x);
474 ds_put_char(&line, ' ');
476 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
478 table_print_table_line__(&line);
485 table_escape_html_text__(const char *s, size_t n)
489 for (i = 0; i < n; i++) {
494 fputs("&", stdout);
497 fputs("<", stdout);
500 fputs(">", stdout);
503 fputs(""", stdout);
513 table_print_html_cell__(const char *element, const char *content)
517 printf(" <%s>", element);
518 for (p = content; *p; ) {
521 if (uuid_from_string_prefix(&uuid, p)) {
522 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
525 table_escape_html_text__(p, 1);
529 printf("</%s>\n", element);
533 table_print_html__(const struct table *table)
537 fputs("<table border=1>\n", stdout);
539 if (table->caption) {
540 table_print_html_cell__("caption", table->caption);
543 if (output_headings) {
544 fputs(" <tr>\n", stdout);
545 for (x = 0; x < table->n_columns; x++) {
546 const struct column *column = &table->columns[x];
547 table_print_html_cell__("th", column->heading);
549 fputs(" </tr>\n", stdout);
552 for (y = 0; y < table->n_rows; y++) {
553 fputs(" <tr>\n", stdout);
554 for (x = 0; x < table->n_columns; x++) {
555 const char *content = *table_cell__(table, y, x);
557 if (!strcmp(table->columns[x].heading, "_uuid")) {
558 fputs(" <td><a name=\"", stdout);
559 table_escape_html_text__(content, strlen(content));
560 fputs("\">", stdout);
561 table_escape_html_text__(content, 8);
562 fputs("</a></td>\n", stdout);
564 table_print_html_cell__("td", content);
567 fputs(" </tr>\n", stdout);
570 fputs("</table>\n", stdout);
574 table_print_csv_cell__(const char *content)
578 if (!strpbrk(content, "\n\",")) {
579 fputs(content, stdout);
582 for (p = content; *p != '\0'; p++) {
585 fputs("\"\"", stdout);
597 table_print_csv__(const struct table *table)
606 if (table->caption) {
607 puts(table->caption);
610 if (output_headings) {
611 for (x = 0; x < table->n_columns; x++) {
612 const struct column *column = &table->columns[x];
616 table_print_csv_cell__(column->heading);
621 for (y = 0; y < table->n_rows; y++) {
622 for (x = 0; x < table->n_columns; x++) {
626 table_print_csv_cell__(*table_cell__(table, y, x));
633 table_print(const struct table *table)
635 switch (output_format) {
637 table_print_table__(table);
641 table_print_html__(table);
645 table_print_csv__(table);
651 do_list_dbs(int argc OVS_UNUSED, char *argv[])
653 struct jsonrpc_msg *request, *reply;
658 rpc = open_jsonrpc(argv[1]);
659 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
661 error = jsonrpc_transact_block(rpc, request, &reply);
663 ovs_fatal(error, "transaction failed");
666 if (reply->result->type != JSON_ARRAY) {
667 ovs_fatal(0, "list_dbs response is not array");
670 for (i = 0; i < reply->result->u.array.n; i++) {
671 const struct json *name = reply->result->u.array.elems[i];
673 if (name->type != JSON_STRING) {
674 ovs_fatal(0, "list_dbs response %zu is not string", i);
676 puts(name->u.string);
678 jsonrpc_msg_destroy(reply);
682 do_get_schema(int argc OVS_UNUSED, char *argv[])
684 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
685 print_and_free_json(ovsdb_schema_to_json(schema));
686 ovsdb_schema_destroy(schema);
690 do_list_tables(int argc OVS_UNUSED, char *argv[])
692 struct ovsdb_schema *schema;
693 struct shash_node *node;
696 schema = fetch_schema(argv[1], argv[2]);
698 table_add_column(&t, "Table");
699 SHASH_FOR_EACH (node, &schema->tables) {
700 struct ovsdb_table_schema *ts = node->data;
703 table_add_cell(&t, ts->name);
705 ovsdb_schema_destroy(schema);
710 do_list_columns(int argc OVS_UNUSED, char *argv[])
712 const char *table_name = argv[3];
713 struct ovsdb_schema *schema;
714 struct shash_node *table_node;
717 schema = fetch_schema(argv[1], argv[2]);
720 table_add_column(&t, "Table");
722 table_add_column(&t, "Column");
723 table_add_column(&t, "Type");
724 SHASH_FOR_EACH (table_node, &schema->tables) {
725 struct ovsdb_table_schema *ts = table_node->data;
727 if (!table_name || !strcmp(table_name, ts->name)) {
728 struct shash_node *column_node;
730 SHASH_FOR_EACH (column_node, &ts->columns) {
731 const struct ovsdb_column *column = column_node->data;
732 struct json *type = ovsdb_type_to_json(&column->type);
736 table_add_cell(&t, ts->name);
738 table_add_cell(&t, column->name);
739 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
745 ovsdb_schema_destroy(schema);
750 do_transact(int argc OVS_UNUSED, char *argv[])
752 struct jsonrpc_msg *request, *reply;
753 struct json *transaction;
757 transaction = parse_json(argv[2]);
759 rpc = open_jsonrpc(argv[1]);
760 request = jsonrpc_create_request("transact", transaction, NULL);
761 error = jsonrpc_transact_block(rpc, request, &reply);
763 ovs_fatal(error, "transaction failed");
766 ovs_fatal(error, "transaction returned error: %s",
767 json_to_string(reply->error, json_flags));
769 print_json(reply->result);
771 jsonrpc_msg_destroy(reply);
776 format_json(const struct json *json, const struct ovsdb_type *type)
778 if (data_format == DF_JSON) {
779 return json_to_string(json, JSSF_SORT);
780 } else if (data_format == DF_STRING) {
781 struct ovsdb_datum datum;
782 struct ovsdb_error *error;
785 error = ovsdb_datum_from_json(&datum, type, json, NULL);
787 return json_to_string(json, JSSF_SORT);
791 ovsdb_datum_to_string(&datum, type, &s);
792 ovsdb_datum_destroy(&datum, type);
793 return ds_steal_cstr(&s);
800 monitor_print_row(struct json *row, const char *type, const char *uuid,
801 const struct ovsdb_column_set *columns, struct table *t)
806 ovs_error(0, "missing %s row", type);
808 } else if (row->type != JSON_OBJECT) {
809 ovs_error(0, "<row> is not object");
814 table_add_cell(t, uuid);
815 table_add_cell(t, type);
816 for (i = 0; i < columns->n_columns; i++) {
817 const struct ovsdb_column *column = columns->columns[i];
818 struct json *value = shash_find_data(json_object(row), column->name);
820 table_add_cell_nocopy(t, format_json(value, &column->type));
822 table_add_cell(t, "");
828 monitor_print(struct json *table_updates,
829 const struct ovsdb_table_schema *table,
830 const struct ovsdb_column_set *columns, bool initial)
832 struct json *table_update;
833 struct shash_node *node;
839 if (table_updates->type != JSON_OBJECT) {
840 ovs_error(0, "<table-updates> is not object");
843 table_update = shash_find_data(json_object(table_updates), table->name);
847 if (table_update->type != JSON_OBJECT) {
848 ovs_error(0, "<table-update> is not object");
852 table_add_column(&t, "row");
853 table_add_column(&t, "action");
854 for (i = 0; i < columns->n_columns; i++) {
855 table_add_column(&t, "%s", columns->columns[i]->name);
857 SHASH_FOR_EACH (node, json_object(table_update)) {
858 struct json *row_update = node->data;
859 struct json *old, *new;
861 if (row_update->type != JSON_OBJECT) {
862 ovs_error(0, "<row-update> is not object");
865 old = shash_find_data(json_object(row_update), "old");
866 new = shash_find_data(json_object(row_update), "new");
868 monitor_print_row(new, "initial", node->name, columns, &t);
870 monitor_print_row(new, "insert", node->name, columns, &t);
872 monitor_print_row(old, "delete", node->name, columns, &t);
874 monitor_print_row(old, "old", node->name, columns, &t);
875 monitor_print_row(new, "new", "", columns, &t);
883 add_column(const char *server, const struct ovsdb_column *column,
884 struct ovsdb_column_set *columns, struct json *columns_json)
886 if (ovsdb_column_set_contains(columns, column->index)) {
887 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
888 server, column->name);
890 ovsdb_column_set_add(columns, column);
891 json_array_add(columns_json, json_string_create(column->name));
895 parse_monitor_columns(char *arg, const char *server, const char *database,
896 const struct ovsdb_table_schema *table,
897 struct ovsdb_column_set *columns)
899 bool initial, insert, delete, modify;
900 struct json *mr, *columns_json;
901 char *save_ptr = NULL;
904 mr = json_object_create();
905 columns_json = json_array_create_empty();
906 json_object_put(mr, "columns", columns_json);
908 initial = insert = delete = modify = true;
909 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
910 token = strtok_r(NULL, ",", &save_ptr)) {
911 if (!strcmp(token, "!initial")) {
913 } else if (!strcmp(token, "!insert")) {
915 } else if (!strcmp(token, "!delete")) {
917 } else if (!strcmp(token, "!modify")) {
920 const struct ovsdb_column *column;
922 column = ovsdb_table_schema_get_column(table, token);
924 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
925 "column named \"%s\"",
926 server, table->name, database, token);
928 add_column(server, column, columns, columns_json);
932 if (columns_json->u.array.n == 0) {
933 const struct shash_node **nodes;
936 n = shash_count(&table->columns);
937 nodes = shash_sort(&table->columns);
938 for (i = 0; i < n; i++) {
939 const struct ovsdb_column *column = nodes[i]->data;
940 if (column->index != OVSDB_COL_UUID
941 && column->index != OVSDB_COL_VERSION) {
942 add_column(server, column, columns, columns_json);
947 add_column(server, ovsdb_table_schema_get_column(table,"_version"),
948 columns, columns_json);
951 if (!initial || !insert || !delete || !modify) {
952 struct json *select = json_object_create();
953 json_object_put(select, "initial", json_boolean_create(initial));
954 json_object_put(select, "insert", json_boolean_create(insert));
955 json_object_put(select, "delete", json_boolean_create(delete));
956 json_object_put(select, "modify", json_boolean_create(modify));
957 json_object_put(mr, "select", select);
964 do_monitor(int argc, char *argv[])
966 const char *server = argv[1];
967 const char *database = argv[2];
968 const char *table_name = argv[3];
969 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
970 struct ovsdb_table_schema *table;
971 struct ovsdb_schema *schema;
972 struct jsonrpc_msg *request;
974 struct json *monitor, *monitor_request_array,
975 *monitor_requests, *request_id;
977 rpc = open_jsonrpc(server);
979 schema = fetch_schema_from_rpc(rpc, database);
980 table = shash_find_data(&schema->tables, table_name);
982 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
983 server, database, table_name);
986 monitor_request_array = json_array_create_empty();
990 for (i = 4; i < argc; i++) {
992 monitor_request_array,
993 parse_monitor_columns(argv[i], server, database, table,
997 /* Allocate a writable empty string since parse_monitor_columns() is
998 * going to strtok() it and that's risky with literal "". */
1001 monitor_request_array,
1002 parse_monitor_columns(empty, server, database, table, &columns));
1005 monitor_requests = json_object_create();
1006 json_object_put(monitor_requests, table_name, monitor_request_array);
1008 monitor = json_array_create_3(json_string_create(database),
1009 json_null_create(), monitor_requests);
1010 request = jsonrpc_create_request("monitor", monitor, NULL);
1011 request_id = json_clone(request->id);
1012 jsonrpc_send(rpc, request);
1014 struct jsonrpc_msg *msg;
1017 error = jsonrpc_recv_block(rpc, &msg);
1019 ovsdb_schema_destroy(schema);
1020 ovs_fatal(error, "%s: receive failed", server);
1023 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
1024 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
1026 } else if (msg->type == JSONRPC_REPLY
1027 && json_equal(msg->id, request_id)) {
1028 monitor_print(msg->result, table, &columns, true);
1031 /* daemonize() closes the standard file descriptors. We output
1032 * to stdout, so we need to save and restore STDOUT_FILENO. */
1033 int fd = dup(STDOUT_FILENO);
1035 dup2(fd, STDOUT_FILENO);
1038 } else if (msg->type == JSONRPC_NOTIFY
1039 && !strcmp(msg->method, "update")) {
1040 struct json *params = msg->params;
1041 if (params->type == JSON_ARRAY
1042 && params->u.array.n == 2
1043 && params->u.array.elems[0]->type == JSON_NULL) {
1044 monitor_print(params->u.array.elems[1],
1045 table, &columns, false);
1049 jsonrpc_msg_destroy(msg);
1053 struct dump_table_aux {
1054 struct ovsdb_datum **data;
1055 const struct ovsdb_column **columns;
1060 compare_data(size_t a_y, size_t b_y, size_t x,
1061 const struct dump_table_aux *aux)
1063 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
1065 &aux->columns[x]->type);
1069 compare_rows(size_t a_y, size_t b_y, void *aux_)
1071 struct dump_table_aux *aux = aux_;
1074 /* Skip UUID columns on the first pass, since their values tend to be
1075 * random and make our results less reproducible. */
1076 for (x = 0; x < aux->n_columns; x++) {
1077 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
1078 int cmp = compare_data(a_y, b_y, x, aux);
1085 /* Use UUID columns as tie-breakers. */
1086 for (x = 0; x < aux->n_columns; x++) {
1087 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
1088 int cmp = compare_data(a_y, b_y, x, aux);
1099 swap_rows(size_t a_y, size_t b_y, void *aux_)
1101 struct dump_table_aux *aux = aux_;
1102 struct ovsdb_datum *tmp = aux->data[a_y];
1103 aux->data[a_y] = aux->data[b_y];
1104 aux->data[b_y] = tmp;
1108 format_data(const struct ovsdb_datum *datum, const struct ovsdb_type *type)
1110 if (data_format == DF_JSON) {
1111 struct json *json = ovsdb_datum_to_json(datum, type);
1112 char *s = json_to_string(json, JSSF_SORT);
1115 } else if (data_format == DF_STRING) {
1119 ovsdb_datum_to_string(datum, type, &s);
1120 return ds_steal_cstr(&s);
1127 compare_columns(const void *a_, const void *b_)
1129 const struct ovsdb_column *const *ap = a_;
1130 const struct ovsdb_column *const *bp = b_;
1131 const struct ovsdb_column *a = *ap;
1132 const struct ovsdb_column *b = *bp;
1134 return strcmp(a->name, b->name);
1138 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
1140 const struct ovsdb_column **columns;
1143 struct ovsdb_datum **data;
1145 struct dump_table_aux aux;
1146 struct shash_node *node;
1150 /* Sort columns by name, for reproducibility. */
1151 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
1153 SHASH_FOR_EACH (node, &ts->columns) {
1154 struct ovsdb_column *column = node->data;
1155 if (strcmp(column->name, "_version")) {
1156 columns[n_columns++] = column;
1159 qsort(columns, n_columns, sizeof *columns, compare_columns);
1161 /* Extract data from table. */
1162 data = xmalloc(rows->n * sizeof *data);
1163 for (y = 0; y < rows->n; y++) {
1166 if (rows->elems[y]->type != JSON_OBJECT) {
1167 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
1168 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
1170 row = json_object(rows->elems[y]);
1172 data[y] = xmalloc(n_columns * sizeof **data);
1173 for (x = 0; x < n_columns; x++) {
1174 const struct json *json = shash_find_data(row, columns[x]->name);
1176 ovs_fatal(0, "row %zu in table %s response lacks %s column",
1177 y, ts->name, columns[x]->name);
1180 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
1186 /* Sort rows by column values, for reproducibility. */
1188 aux.columns = columns;
1189 aux.n_columns = n_columns;
1190 sort(rows->n, compare_rows, swap_rows, &aux);
1192 /* Add column headings. */
1194 table_set_caption(&t, xasprintf("%s table", ts->name));
1195 for (x = 0; x < n_columns; x++) {
1196 table_add_column(&t, "%s", columns[x]->name);
1200 for (y = 0; y < rows->n; y++) {
1202 for (x = 0; x < n_columns; x++) {
1203 table_add_cell_nocopy(&t, format_data(&data[y][x],
1204 &columns[x]->type));
1212 do_dump(int argc OVS_UNUSED, char *argv[])
1214 const char *server = argv[1];
1215 const char *database = argv[2];
1217 struct jsonrpc_msg *request, *reply;
1218 struct ovsdb_schema *schema;
1219 struct json *transaction;
1220 struct jsonrpc *rpc;
1223 const struct shash_node **tables;
1228 rpc = open_jsonrpc(server);
1230 schema = fetch_schema_from_rpc(rpc, database);
1231 tables = shash_sort(&schema->tables);
1232 n_tables = shash_count(&schema->tables);
1234 /* Construct transaction to retrieve entire database. */
1235 transaction = json_array_create_1(json_string_create(database));
1236 for (i = 0; i < n_tables; i++) {
1237 const struct ovsdb_table_schema *ts = tables[i]->data;
1238 struct json *op, *columns;
1239 struct shash_node *node;
1241 columns = json_array_create_empty();
1242 SHASH_FOR_EACH (node, &ts->columns) {
1243 const struct ovsdb_column *column = node->data;
1245 if (strcmp(column->name, "_version")) {
1246 json_array_add(columns, json_string_create(column->name));
1250 op = json_object_create();
1251 json_object_put_string(op, "op", "select");
1252 json_object_put_string(op, "table", tables[i]->name);
1253 json_object_put(op, "where", json_array_create_empty());
1254 json_object_put(op, "columns", columns);
1255 json_array_add(transaction, op);
1258 /* Send request, get reply. */
1259 request = jsonrpc_create_request("transact", transaction, NULL);
1260 error = jsonrpc_transact_block(rpc, request, &reply);
1262 ovs_fatal(error, "transaction failed");
1265 /* Print database contents. */
1266 if (reply->result->type != JSON_ARRAY
1267 || reply->result->u.array.n != n_tables) {
1268 ovs_fatal(0, "reply is not array of %zu elements: %s",
1269 n_tables, json_to_string(reply->result, 0));
1271 for (i = 0; i < n_tables; i++) {
1272 const struct ovsdb_table_schema *ts = tables[i]->data;
1273 const struct json *op_result = reply->result->u.array.elems[i];
1276 if (op_result->type != JSON_OBJECT
1277 || !(rows = shash_find_data(json_object(op_result), "rows"))
1278 || rows->type != JSON_ARRAY) {
1279 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1281 ts->name, json_to_string(op_result, 0));
1284 dump_table(ts, &rows->u.array);
1289 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1294 static const struct command all_commands[] = {
1295 { "list-dbs", 1, 1, do_list_dbs },
1296 { "get-schema", 2, 2, do_get_schema },
1297 { "list-tables", 2, 2, do_list_tables },
1298 { "list-columns", 2, 3, do_list_columns },
1299 { "transact", 2, 2, do_transact },
1300 { "monitor", 3, INT_MAX, do_monitor },
1301 { "dump", 2, 2, do_dump },
1302 { "help", 0, INT_MAX, do_help },
1303 { NULL, 0, 0, NULL },