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]);
77 parse_options(argc, argv);
78 signal(SIGPIPE, SIG_IGN);
79 run_command(argc - optind, argv + optind, all_commands);
84 parse_options(int argc, char *argv[])
87 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
89 static struct option long_options[] = {
90 {"format", required_argument, 0, 'f'},
91 {"data", required_argument, 0, 'd'},
92 {"no-headings", no_argument, &output_headings, 0},
93 {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
94 {"verbose", optional_argument, 0, 'v'},
95 {"help", no_argument, 0, 'h'},
96 {"version", no_argument, 0, 'V'},
99 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
100 STREAM_SSL_LONG_OPTIONS
104 char *short_options = long_options_to_short_options(long_options);
109 c = getopt_long(argc, argv, short_options, long_options, NULL);
116 if (!strcmp(optarg, "table")) {
117 output_format = FMT_TABLE;
118 } else if (!strcmp(optarg, "html")) {
119 output_format = FMT_HTML;
120 } else if (!strcmp(optarg, "csv")) {
121 output_format = FMT_CSV;
123 ovs_fatal(0, "unknown output format \"%s\"", optarg);
128 if (!strcmp(optarg, "string")) {
129 data_format = DF_STRING;
130 } else if (!strcmp(optarg, "json")) {
131 data_format = DF_JSON;
133 ovs_fatal(0, "unknown data format \"%s\"", optarg);
141 OVS_PRINT_VERSION(0, 0);
145 vlog_set_verbosity(optarg);
148 DAEMON_OPTION_HANDLERS
151 STREAM_SSL_OPTION_HANDLERS
153 case OPT_BOOTSTRAP_CA_CERT:
154 stream_ssl_set_ca_cert_file(optarg, true);
162 /* getopt_long() already set the value for us. */
175 printf("%s: Open vSwitch database JSON-RPC client\n"
176 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
177 "\nValid commands are:\n"
178 "\n list-dbs SERVER\n"
179 " list databases available on SERVER\n"
180 "\n get-schema SERVER DATABASE\n"
181 " retrieve schema for DATABASE from SERVER\n"
182 "\n list-tables SERVER DATABASE\n"
183 " list tables for DATABASE on SERVER\n"
184 "\n list-columns SERVER DATABASE [TABLE]\n"
185 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
186 "\n transact SERVER TRANSACTION\n"
187 " run TRANSACTION (a JSON array of operations) on SERVER\n"
188 " and print the results as JSON on stdout\n"
189 "\n monitor SERVER DATABASE TABLE [COLUMN,...]...\n"
190 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
191 " COLUMNs may include !initial, !insert, !delete, !modify\n"
192 " to avoid seeing the specified kinds of changes.\n"
193 "\n dump SERVER DATABASE\n"
194 " dump contents of DATABASE on SERVER to stdout\n",
195 program_name, program_name);
196 stream_usage("SERVER", true, true, true);
197 printf("\nOutput formatting options:\n"
198 " -f, --format=FORMAT set output formatting to FORMAT\n"
199 " (\"table\", \"html\", or \"csv\"\n"
200 " --no-headings omit table heading row\n"
201 " --pretty pretty-print JSON in output");
204 printf("\nOther options:\n"
205 " -h, --help display this help message\n"
206 " -V, --version display version information\n");
211 parse_json(const char *s)
213 struct json *json = json_from_string(s);
214 if (json->type == JSON_STRING) {
215 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
220 static struct jsonrpc *
221 open_jsonrpc(const char *server)
223 struct stream *stream;
226 error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
227 if (error == EAFNOSUPPORT) {
228 struct pstream *pstream;
230 error = jsonrpc_pstream_open(server, &pstream);
232 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
235 VLOG_INFO("%s: waiting for connection...", server);
236 error = pstream_accept_block(pstream, &stream);
238 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
241 pstream_close(pstream);
243 ovs_fatal(error, "failed to connect to \"%s\"", server);
246 return jsonrpc_open(stream);
250 print_json(struct json *json)
252 char *string = json_to_string(json, json_flags);
253 fputs(string, stdout);
258 print_and_free_json(struct json *json)
265 check_ovsdb_error(struct ovsdb_error *error)
268 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
272 static struct ovsdb_schema *
273 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
275 struct jsonrpc_msg *request, *reply;
276 struct ovsdb_schema *schema;
279 request = jsonrpc_create_request("get_schema",
281 json_string_create(database)),
283 error = jsonrpc_transact_block(rpc, request, &reply);
285 ovs_fatal(error, "transaction failed");
287 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
288 jsonrpc_msg_destroy(reply);
293 static struct ovsdb_schema *
294 fetch_schema(const char *server, const char *database)
296 struct ovsdb_schema *schema;
299 rpc = open_jsonrpc(server);
300 schema = fetch_schema_from_rpc(rpc, database);
313 struct column *columns;
314 size_t n_columns, allocated_columns;
315 size_t n_rows, allocated_rows;
316 size_t current_column;
321 table_init(struct table *table)
323 memset(table, 0, sizeof *table);
327 table_destroy(struct table *table)
331 for (i = 0; i < table->n_columns; i++) {
332 free(table->columns[i].heading);
334 free(table->columns);
336 for (i = 0; i < table->n_columns * table->n_rows; i++) {
337 free(table->cells[i]);
341 free(table->caption);
345 table_set_caption(struct table *table, char *caption)
347 free(table->caption);
348 table->caption = caption;
352 table_add_column(struct table *table, const char *heading, ...)
356 table_add_column(struct table *table, const char *heading, ...)
358 struct column *column;
361 assert(!table->n_rows);
362 if (table->n_columns >= table->allocated_columns) {
363 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
364 sizeof *table->columns);
366 column = &table->columns[table->n_columns++];
368 va_start(args, heading);
369 column->heading = xvasprintf(heading, args);
370 column->width = strlen(column->heading);
375 table_cell__(const struct table *table, size_t row, size_t column)
377 return &table->cells[column + row * table->n_columns];
381 table_add_row(struct table *table)
385 if (table->n_rows >= table->allocated_rows) {
386 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
387 table->n_columns * sizeof *table->cells);
391 table->current_column = 0;
392 for (x = 0; x < table->n_columns; x++) {
393 *table_cell__(table, y, x) = NULL;
398 table_add_cell_nocopy(struct table *table, char *s)
403 assert(table->n_rows > 0);
404 assert(table->current_column < table->n_columns);
406 x = table->current_column++;
407 y = table->n_rows - 1;
408 *table_cell__(table, y, x) = s;
411 if (length > table->columns[x].width) {
412 table->columns[x].width = length;
417 table_add_cell(struct table *table, const char *format, ...)
421 va_start(args, format);
422 table_add_cell_nocopy(table, xvasprintf(format, args));
427 table_print_table_line__(struct ds *line)
434 table_print_table__(const struct table *table)
437 struct ds line = DS_EMPTY_INITIALIZER;
444 if (output_headings) {
445 for (x = 0; x < table->n_columns; x++) {
446 const struct column *column = &table->columns[x];
448 ds_put_char(&line, ' ');
450 ds_put_format(&line, "%-*s", column->width, column->heading);
452 table_print_table_line__(&line);
454 for (x = 0; x < table->n_columns; x++) {
455 const struct column *column = &table->columns[x];
459 ds_put_char(&line, ' ');
461 for (i = 0; i < column->width; i++) {
462 ds_put_char(&line, '-');
465 table_print_table_line__(&line);
468 for (y = 0; y < table->n_rows; y++) {
469 for (x = 0; x < table->n_columns; x++) {
470 const char *cell = *table_cell__(table, y, x);
472 ds_put_char(&line, ' ');
474 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
476 table_print_table_line__(&line);
483 table_escape_html_text__(const char *s, size_t n)
487 for (i = 0; i < n; i++) {
492 fputs("&", stdout);
495 fputs("<", stdout);
498 fputs(">", stdout);
501 fputs(""", stdout);
511 table_print_html_cell__(const char *element, const char *content)
515 printf(" <%s>", element);
516 for (p = content; *p; ) {
519 if (uuid_from_string_prefix(&uuid, p)) {
520 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
523 table_escape_html_text__(p, 1);
527 printf("</%s>\n", element);
531 table_print_html__(const struct table *table)
535 fputs("<table border=1>\n", stdout);
537 if (table->caption) {
538 table_print_html_cell__("caption", table->caption);
541 if (output_headings) {
542 fputs(" <tr>\n", stdout);
543 for (x = 0; x < table->n_columns; x++) {
544 const struct column *column = &table->columns[x];
545 table_print_html_cell__("th", column->heading);
547 fputs(" </tr>\n", stdout);
550 for (y = 0; y < table->n_rows; y++) {
551 fputs(" <tr>\n", stdout);
552 for (x = 0; x < table->n_columns; x++) {
553 const char *content = *table_cell__(table, y, x);
555 if (!strcmp(table->columns[x].heading, "_uuid")) {
556 fputs(" <td><a name=\"", stdout);
557 table_escape_html_text__(content, strlen(content));
558 fputs("\">", stdout);
559 table_escape_html_text__(content, 8);
560 fputs("</a></td>\n", stdout);
562 table_print_html_cell__("td", content);
565 fputs(" </tr>\n", stdout);
568 fputs("</table>\n", stdout);
572 table_print_csv_cell__(const char *content)
576 if (!strpbrk(content, "\n\",")) {
577 fputs(content, stdout);
580 for (p = content; *p != '\0'; p++) {
583 fputs("\"\"", stdout);
595 table_print_csv__(const struct table *table)
604 if (table->caption) {
605 puts(table->caption);
608 if (output_headings) {
609 for (x = 0; x < table->n_columns; x++) {
610 const struct column *column = &table->columns[x];
614 table_print_csv_cell__(column->heading);
619 for (y = 0; y < table->n_rows; y++) {
620 for (x = 0; x < table->n_columns; x++) {
624 table_print_csv_cell__(*table_cell__(table, y, x));
631 table_print(const struct table *table)
633 switch (output_format) {
635 table_print_table__(table);
639 table_print_html__(table);
643 table_print_csv__(table);
649 do_list_dbs(int argc OVS_UNUSED, char *argv[])
651 struct jsonrpc_msg *request, *reply;
656 rpc = open_jsonrpc(argv[1]);
657 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
659 error = jsonrpc_transact_block(rpc, request, &reply);
661 ovs_fatal(error, "transaction failed");
664 if (reply->result->type != JSON_ARRAY) {
665 ovs_fatal(0, "list_dbs response is not array");
668 for (i = 0; i < reply->result->u.array.n; i++) {
669 const struct json *name = reply->result->u.array.elems[i];
671 if (name->type != JSON_STRING) {
672 ovs_fatal(0, "list_dbs response %zu is not string", i);
674 puts(name->u.string);
676 jsonrpc_msg_destroy(reply);
680 do_get_schema(int argc OVS_UNUSED, char *argv[])
682 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
683 print_and_free_json(ovsdb_schema_to_json(schema));
684 ovsdb_schema_destroy(schema);
688 do_list_tables(int argc OVS_UNUSED, char *argv[])
690 struct ovsdb_schema *schema;
691 struct shash_node *node;
694 schema = fetch_schema(argv[1], argv[2]);
696 table_add_column(&t, "Table");
697 SHASH_FOR_EACH (node, &schema->tables) {
698 struct ovsdb_table_schema *ts = node->data;
701 table_add_cell(&t, ts->name);
703 ovsdb_schema_destroy(schema);
708 do_list_columns(int argc OVS_UNUSED, char *argv[])
710 const char *table_name = argv[3];
711 struct ovsdb_schema *schema;
712 struct shash_node *table_node;
715 schema = fetch_schema(argv[1], argv[2]);
718 table_add_column(&t, "Table");
720 table_add_column(&t, "Column");
721 table_add_column(&t, "Type");
722 SHASH_FOR_EACH (table_node, &schema->tables) {
723 struct ovsdb_table_schema *ts = table_node->data;
725 if (!table_name || !strcmp(table_name, ts->name)) {
726 struct shash_node *column_node;
728 SHASH_FOR_EACH (column_node, &ts->columns) {
729 const struct ovsdb_column *column = column_node->data;
730 struct json *type = ovsdb_type_to_json(&column->type);
734 table_add_cell(&t, ts->name);
736 table_add_cell(&t, column->name);
737 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
743 ovsdb_schema_destroy(schema);
748 do_transact(int argc OVS_UNUSED, char *argv[])
750 struct jsonrpc_msg *request, *reply;
751 struct json *transaction;
755 transaction = parse_json(argv[2]);
757 rpc = open_jsonrpc(argv[1]);
758 request = jsonrpc_create_request("transact", transaction, NULL);
759 error = jsonrpc_transact_block(rpc, request, &reply);
761 ovs_fatal(error, "transaction failed");
764 ovs_fatal(error, "transaction returned error: %s",
765 json_to_string(reply->error, json_flags));
767 print_json(reply->result);
769 jsonrpc_msg_destroy(reply);
774 format_json(const struct json *json, const struct ovsdb_type *type)
776 if (data_format == DF_JSON) {
777 return json_to_string(json, JSSF_SORT);
778 } else if (data_format == DF_STRING) {
779 struct ovsdb_datum datum;
780 struct ovsdb_error *error;
783 error = ovsdb_datum_from_json(&datum, type, json, NULL);
785 return json_to_string(json, JSSF_SORT);
789 ovsdb_datum_to_string(&datum, type, &s);
790 ovsdb_datum_destroy(&datum, type);
791 return ds_steal_cstr(&s);
798 monitor_print_row(struct json *row, const char *type, const char *uuid,
799 const struct ovsdb_column_set *columns, struct table *t)
804 ovs_error(0, "missing %s row", type);
806 } else if (row->type != JSON_OBJECT) {
807 ovs_error(0, "<row> is not object");
812 table_add_cell(t, uuid);
813 table_add_cell(t, type);
814 for (i = 0; i < columns->n_columns; i++) {
815 const struct ovsdb_column *column = columns->columns[i];
816 struct json *value = shash_find_data(json_object(row), column->name);
818 table_add_cell_nocopy(t, format_json(value, &column->type));
820 table_add_cell(t, "");
826 monitor_print(struct json *table_updates,
827 const struct ovsdb_table_schema *table,
828 const struct ovsdb_column_set *columns, bool initial)
830 struct json *table_update;
831 struct shash_node *node;
837 if (table_updates->type != JSON_OBJECT) {
838 ovs_error(0, "<table-updates> is not object");
841 table_update = shash_find_data(json_object(table_updates), table->name);
845 if (table_update->type != JSON_OBJECT) {
846 ovs_error(0, "<table-update> is not object");
850 table_add_column(&t, "row");
851 table_add_column(&t, "action");
852 for (i = 0; i < columns->n_columns; i++) {
853 table_add_column(&t, "%s", columns->columns[i]->name);
855 SHASH_FOR_EACH (node, json_object(table_update)) {
856 struct json *row_update = node->data;
857 struct json *old, *new;
859 if (row_update->type != JSON_OBJECT) {
860 ovs_error(0, "<row-update> is not object");
863 old = shash_find_data(json_object(row_update), "old");
864 new = shash_find_data(json_object(row_update), "new");
866 monitor_print_row(new, "initial", node->name, columns, &t);
868 monitor_print_row(new, "insert", node->name, columns, &t);
870 monitor_print_row(old, "delete", node->name, columns, &t);
872 monitor_print_row(old, "old", node->name, columns, &t);
873 monitor_print_row(new, "new", "", columns, &t);
881 add_column(const char *server, const struct ovsdb_column *column,
882 struct ovsdb_column_set *columns, struct json *columns_json)
884 if (ovsdb_column_set_contains(columns, column->index)) {
885 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
886 server, column->name);
888 ovsdb_column_set_add(columns, column);
889 json_array_add(columns_json, json_string_create(column->name));
893 parse_monitor_columns(char *arg, const char *server, const char *database,
894 const struct ovsdb_table_schema *table,
895 struct ovsdb_column_set *columns)
897 bool initial, insert, delete, modify;
898 struct json *mr, *columns_json;
899 char *save_ptr = NULL;
902 mr = json_object_create();
903 columns_json = json_array_create_empty();
904 json_object_put(mr, "columns", columns_json);
906 initial = insert = delete = modify = true;
907 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
908 token = strtok_r(NULL, ",", &save_ptr)) {
909 if (!strcmp(token, "!initial")) {
911 } else if (!strcmp(token, "!insert")) {
913 } else if (!strcmp(token, "!delete")) {
915 } else if (!strcmp(token, "!modify")) {
918 const struct ovsdb_column *column;
920 column = ovsdb_table_schema_get_column(table, token);
922 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
923 "column named \"%s\"",
924 server, table->name, database, token);
926 add_column(server, column, columns, columns_json);
930 if (columns_json->u.array.n == 0) {
931 const struct shash_node **nodes;
934 n = shash_count(&table->columns);
935 nodes = shash_sort(&table->columns);
936 for (i = 0; i < n; i++) {
937 const struct ovsdb_column *column = nodes[i]->data;
938 if (column->index != OVSDB_COL_UUID
939 && column->index != OVSDB_COL_VERSION) {
940 add_column(server, column, columns, columns_json);
945 add_column(server, ovsdb_table_schema_get_column(table,"_version"),
946 columns, columns_json);
949 if (!initial || !insert || !delete || !modify) {
950 struct json *select = json_object_create();
951 json_object_put(select, "initial", json_boolean_create(initial));
952 json_object_put(select, "insert", json_boolean_create(insert));
953 json_object_put(select, "delete", json_boolean_create(delete));
954 json_object_put(select, "modify", json_boolean_create(modify));
955 json_object_put(mr, "select", select);
962 do_monitor(int argc, char *argv[])
964 const char *server = argv[1];
965 const char *database = argv[2];
966 const char *table_name = argv[3];
967 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
968 struct ovsdb_table_schema *table;
969 struct ovsdb_schema *schema;
970 struct jsonrpc_msg *request;
972 struct json *monitor, *monitor_request_array,
973 *monitor_requests, *request_id;
975 rpc = open_jsonrpc(server);
977 schema = fetch_schema_from_rpc(rpc, database);
978 table = shash_find_data(&schema->tables, table_name);
980 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
981 server, database, table_name);
984 monitor_request_array = json_array_create_empty();
988 for (i = 4; i < argc; i++) {
990 monitor_request_array,
991 parse_monitor_columns(argv[i], server, database, table,
995 /* Allocate a writable empty string since parse_monitor_columns() is
996 * going to strtok() it and that's risky with literal "". */
999 monitor_request_array,
1000 parse_monitor_columns(empty, server, database, table, &columns));
1003 monitor_requests = json_object_create();
1004 json_object_put(monitor_requests, table_name, monitor_request_array);
1006 monitor = json_array_create_3(json_string_create(database),
1007 json_null_create(), monitor_requests);
1008 request = jsonrpc_create_request("monitor", monitor, NULL);
1009 request_id = json_clone(request->id);
1010 jsonrpc_send(rpc, request);
1012 struct jsonrpc_msg *msg;
1015 error = jsonrpc_recv_block(rpc, &msg);
1017 ovsdb_schema_destroy(schema);
1018 ovs_fatal(error, "%s: receive failed", server);
1021 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
1022 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
1024 } else if (msg->type == JSONRPC_REPLY
1025 && json_equal(msg->id, request_id)) {
1026 monitor_print(msg->result, table, &columns, true);
1029 /* daemonize() closes the standard file descriptors. We output
1030 * to stdout, so we need to save and restore STDOUT_FILENO. */
1031 int fd = dup(STDOUT_FILENO);
1033 dup2(fd, STDOUT_FILENO);
1036 } else if (msg->type == JSONRPC_NOTIFY
1037 && !strcmp(msg->method, "update")) {
1038 struct json *params = msg->params;
1039 if (params->type == JSON_ARRAY
1040 && params->u.array.n == 2
1041 && params->u.array.elems[0]->type == JSON_NULL) {
1042 monitor_print(params->u.array.elems[1],
1043 table, &columns, false);
1047 jsonrpc_msg_destroy(msg);
1051 struct dump_table_aux {
1052 struct ovsdb_datum **data;
1053 const struct ovsdb_column **columns;
1058 compare_data(size_t a_y, size_t b_y, size_t x,
1059 const struct dump_table_aux *aux)
1061 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
1063 &aux->columns[x]->type);
1067 compare_rows(size_t a_y, size_t b_y, void *aux_)
1069 struct dump_table_aux *aux = aux_;
1072 /* Skip UUID columns on the first pass, since their values tend to be
1073 * random and make our results less reproducible. */
1074 for (x = 0; x < aux->n_columns; x++) {
1075 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
1076 int cmp = compare_data(a_y, b_y, x, aux);
1083 /* Use UUID columns as tie-breakers. */
1084 for (x = 0; x < aux->n_columns; x++) {
1085 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
1086 int cmp = compare_data(a_y, b_y, x, aux);
1097 swap_rows(size_t a_y, size_t b_y, void *aux_)
1099 struct dump_table_aux *aux = aux_;
1100 struct ovsdb_datum *tmp = aux->data[a_y];
1101 aux->data[a_y] = aux->data[b_y];
1102 aux->data[b_y] = tmp;
1106 format_data(const struct ovsdb_datum *datum, const struct ovsdb_type *type)
1108 if (data_format == DF_JSON) {
1109 struct json *json = ovsdb_datum_to_json(datum, type);
1110 char *s = json_to_string(json, JSSF_SORT);
1113 } else if (data_format == DF_STRING) {
1117 ovsdb_datum_to_string(datum, type, &s);
1118 return ds_steal_cstr(&s);
1125 compare_columns(const void *a_, const void *b_)
1127 const struct ovsdb_column *const *ap = a_;
1128 const struct ovsdb_column *const *bp = b_;
1129 const struct ovsdb_column *a = *ap;
1130 const struct ovsdb_column *b = *bp;
1132 return strcmp(a->name, b->name);
1136 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
1138 const struct ovsdb_column **columns;
1141 struct ovsdb_datum **data;
1143 struct dump_table_aux aux;
1144 struct shash_node *node;
1148 /* Sort columns by name, for reproducibility. */
1149 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
1151 SHASH_FOR_EACH (node, &ts->columns) {
1152 struct ovsdb_column *column = node->data;
1153 if (strcmp(column->name, "_version")) {
1154 columns[n_columns++] = column;
1157 qsort(columns, n_columns, sizeof *columns, compare_columns);
1159 /* Extract data from table. */
1160 data = xmalloc(rows->n * sizeof *data);
1161 for (y = 0; y < rows->n; y++) {
1164 if (rows->elems[y]->type != JSON_OBJECT) {
1165 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
1166 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
1168 row = json_object(rows->elems[y]);
1170 data[y] = xmalloc(n_columns * sizeof **data);
1171 for (x = 0; x < n_columns; x++) {
1172 const struct json *json = shash_find_data(row, columns[x]->name);
1174 ovs_fatal(0, "row %zu in table %s response lacks %s column",
1175 y, ts->name, columns[x]->name);
1178 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
1184 /* Sort rows by column values, for reproducibility. */
1186 aux.columns = columns;
1187 aux.n_columns = n_columns;
1188 sort(rows->n, compare_rows, swap_rows, &aux);
1190 /* Add column headings. */
1192 table_set_caption(&t, xasprintf("%s table", ts->name));
1193 for (x = 0; x < n_columns; x++) {
1194 table_add_column(&t, "%s", columns[x]->name);
1198 for (y = 0; y < rows->n; y++) {
1200 for (x = 0; x < n_columns; x++) {
1201 table_add_cell_nocopy(&t, format_data(&data[y][x],
1202 &columns[x]->type));
1210 do_dump(int argc OVS_UNUSED, char *argv[])
1212 const char *server = argv[1];
1213 const char *database = argv[2];
1215 struct jsonrpc_msg *request, *reply;
1216 struct ovsdb_schema *schema;
1217 struct json *transaction;
1218 struct jsonrpc *rpc;
1221 const struct shash_node **tables;
1226 rpc = open_jsonrpc(server);
1228 schema = fetch_schema_from_rpc(rpc, database);
1229 tables = shash_sort(&schema->tables);
1230 n_tables = shash_count(&schema->tables);
1232 /* Construct transaction to retrieve entire database. */
1233 transaction = json_array_create_1(json_string_create(database));
1234 for (i = 0; i < n_tables; i++) {
1235 const struct ovsdb_table_schema *ts = tables[i]->data;
1236 struct json *op, *columns;
1237 struct shash_node *node;
1239 columns = json_array_create_empty();
1240 SHASH_FOR_EACH (node, &ts->columns) {
1241 const struct ovsdb_column *column = node->data;
1243 if (strcmp(column->name, "_version")) {
1244 json_array_add(columns, json_string_create(column->name));
1248 op = json_object_create();
1249 json_object_put_string(op, "op", "select");
1250 json_object_put_string(op, "table", tables[i]->name);
1251 json_object_put(op, "where", json_array_create_empty());
1252 json_object_put(op, "columns", columns);
1253 json_array_add(transaction, op);
1256 /* Send request, get reply. */
1257 request = jsonrpc_create_request("transact", transaction, NULL);
1258 error = jsonrpc_transact_block(rpc, request, &reply);
1260 ovs_fatal(error, "transaction failed");
1263 /* Print database contents. */
1264 if (reply->result->type != JSON_ARRAY
1265 || reply->result->u.array.n != n_tables) {
1266 ovs_fatal(0, "reply is not array of %zu elements: %s",
1267 n_tables, json_to_string(reply->result, 0));
1269 for (i = 0; i < n_tables; i++) {
1270 const struct ovsdb_table_schema *ts = tables[i]->data;
1271 const struct json *op_result = reply->result->u.array.elems[i];
1274 if (op_result->type != JSON_OBJECT
1275 || !(rows = shash_find_data(json_object(op_result), "rows"))
1276 || rows->type != JSON_ARRAY) {
1277 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1279 ts->name, json_to_string(op_result, 0));
1282 dump_table(ts, &rows->u.array);
1287 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1292 static const struct command all_commands[] = {
1293 { "list-dbs", 1, 1, do_list_dbs },
1294 { "get-schema", 2, 2, do_get_schema },
1295 { "list-tables", 2, 2, do_list_tables },
1296 { "list-columns", 2, 3, do_list_columns },
1297 { "transact", 2, 2, do_transact },
1298 { "monitor", 3, INT_MAX, do_monitor },
1299 { "dump", 2, 2, do_dump },
1300 { "help", 0, INT_MAX, do_help },
1301 { NULL, 0, 0, NULL },