Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / ovsdb / ovsdb-client.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "command-line.h"
29 #include "column.h"
30 #include "compiler.h"
31 #include "daemon.h"
32 #include "dynamic-string.h"
33 #include "json.h"
34 #include "jsonrpc.h"
35 #include "lib/table.h"
36 #include "ovsdb.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-error.h"
39 #include "sort.h"
40 #include "stream.h"
41 #include "stream-ssl.h"
42 #include "table.h"
43 #include "timeval.h"
44 #include "util.h"
45 #include "vlog.h"
46
47 VLOG_DEFINE_THIS_MODULE(ovsdb_client);
48
49 /* Format for table output. */
50 static struct table_style table_style = TABLE_STYLE_DEFAULT;
51
52 static const struct command all_commands[];
53
54 static void usage(void) NO_RETURN;
55 static void parse_options(int argc, char *argv[]);
56
57 int
58 main(int argc, char *argv[])
59 {
60     proctitle_init(argc, argv);
61     set_program_name(argv[0]);
62     parse_options(argc, argv);
63     signal(SIGPIPE, SIG_IGN);
64     run_command(argc - optind, argv + optind, all_commands);
65     return 0;
66 }
67
68 static void
69 parse_options(int argc, char *argv[])
70 {
71     enum {
72         OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
73         DAEMON_OPTION_ENUMS,
74         TABLE_OPTION_ENUMS
75     };
76     static struct option long_options[] = {
77         {"verbose", optional_argument, 0, 'v'},
78         {"help", no_argument, 0, 'h'},
79         {"version", no_argument, 0, 'V'},
80         DAEMON_LONG_OPTIONS,
81 #ifdef HAVE_OPENSSL
82         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
83         TABLE_LONG_OPTIONS,
84         STREAM_SSL_LONG_OPTIONS
85 #endif
86         {0, 0, 0, 0},
87     };
88     char *short_options = long_options_to_short_options(long_options);
89
90     for (;;) {
91         int c;
92
93         c = getopt_long(argc, argv, short_options, long_options, NULL);
94         if (c == -1) {
95             break;
96         }
97
98         switch (c) {
99         case 'h':
100             usage();
101
102         case 'V':
103             OVS_PRINT_VERSION(0, 0);
104             exit(EXIT_SUCCESS);
105
106         case 'v':
107             vlog_set_verbosity(optarg);
108             break;
109
110         DAEMON_OPTION_HANDLERS
111
112         TABLE_OPTION_HANDLERS(&table_style)
113
114 #ifdef HAVE_OPENSSL
115         STREAM_SSL_OPTION_HANDLERS
116
117         case OPT_BOOTSTRAP_CA_CERT:
118             stream_ssl_set_ca_cert_file(optarg, true);
119             break;
120 #endif
121
122         case '?':
123             exit(EXIT_FAILURE);
124
125         case 0:
126             /* getopt_long() already set the value for us. */
127             break;
128
129         default:
130             abort();
131         }
132     }
133     free(short_options);
134 }
135
136 static void
137 usage(void)
138 {
139     printf("%s: Open vSwitch database JSON-RPC client\n"
140            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
141            "\nValid commands are:\n"
142            "\n  list-dbs SERVER\n"
143            "    list databases available on SERVER\n"
144            "\n  get-schema SERVER DATABASE\n"
145            "    retrieve schema for DATABASE from SERVER\n"
146            "\n  get-schema-version SERVER DATABASE\n"
147            "    retrieve schema for DATABASE from SERVER and report only its\n"
148            "    version number on stdout\n"
149            "\n  list-tables SERVER DATABASE\n"
150            "    list tables for DATABASE on SERVER\n"
151            "\n  list-columns SERVER DATABASE [TABLE]\n"
152            "    list columns in TABLE (or all tables) in DATABASE on SERVER\n"
153            "\n  transact SERVER TRANSACTION\n"
154            "    run TRANSACTION (a JSON array of operations) on SERVER\n"
155            "    and print the results as JSON on stdout\n"
156            "\n  monitor SERVER DATABASE TABLE [COLUMN,...]...\n"
157            "    monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
158            "    COLUMNs may include !initial, !insert, !delete, !modify\n"
159            "    to avoid seeing the specified kinds of changes.\n"
160            "\n  dump SERVER DATABASE\n"
161            "    dump contents of DATABASE on SERVER to stdout\n",
162            program_name, program_name);
163     stream_usage("SERVER", true, true, true);
164     printf("\nOutput formatting options:\n"
165            "  -f, --format=FORMAT         set output formatting to FORMAT\n"
166            "                              (\"table\", \"html\", \"csv\", "
167            "or \"json\")\n"
168            "  --no-headings               omit table heading row\n"
169            "  --pretty                    pretty-print JSON in output");
170     daemon_usage();
171     vlog_usage();
172     printf("\nOther options:\n"
173            "  -h, --help                  display this help message\n"
174            "  -V, --version               display version information\n");
175     exit(EXIT_SUCCESS);
176 }
177 \f
178 static struct json *
179 parse_json(const char *s)
180 {
181     struct json *json = json_from_string(s);
182     if (json->type == JSON_STRING) {
183         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
184     }
185     return json;
186 }
187
188 static struct jsonrpc *
189 open_jsonrpc(const char *server)
190 {
191     struct stream *stream;
192     int error;
193
194     error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
195     if (error == EAFNOSUPPORT) {
196         struct pstream *pstream;
197
198         error = jsonrpc_pstream_open(server, &pstream);
199         if (error) {
200             ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
201         }
202
203         VLOG_INFO("%s: waiting for connection...", server);
204         error = pstream_accept_block(pstream, &stream);
205         if (error) {
206             ovs_fatal(error, "failed to accept connection on \"%s\"", server);
207         }
208
209         pstream_close(pstream);
210     } else if (error) {
211         ovs_fatal(error, "failed to connect to \"%s\"", server);
212     }
213
214     return jsonrpc_open(stream);
215 }
216
217 static void
218 print_json(struct json *json)
219 {
220     char *string = json_to_string(json, table_style.json_flags);
221     fputs(string, stdout);
222     free(string);
223 }
224
225 static void
226 print_and_free_json(struct json *json)
227 {
228     print_json(json);
229     json_destroy(json);
230 }
231
232 static void
233 check_ovsdb_error(struct ovsdb_error *error)
234 {
235     if (error) {
236         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
237     }
238 }
239
240 static struct ovsdb_schema *
241 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
242 {
243     struct jsonrpc_msg *request, *reply;
244     struct ovsdb_schema *schema;
245     int error;
246
247     request = jsonrpc_create_request("get_schema",
248                                      json_array_create_1(
249                                          json_string_create(database)),
250                                      NULL);
251     error = jsonrpc_transact_block(rpc, request, &reply);
252     if (error) {
253         ovs_fatal(error, "transaction failed");
254     }
255     check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
256     jsonrpc_msg_destroy(reply);
257
258     return schema;
259 }
260
261 static struct ovsdb_schema *
262 fetch_schema(const char *server, const char *database)
263 {
264     struct ovsdb_schema *schema;
265     struct jsonrpc *rpc;
266
267     rpc = open_jsonrpc(server);
268     schema = fetch_schema_from_rpc(rpc, database);
269     jsonrpc_close(rpc);
270
271     return schema;
272 }
273 \f
274 \f
275 static void
276 do_list_dbs(int argc OVS_UNUSED, char *argv[])
277 {
278     struct jsonrpc_msg *request, *reply;
279     struct jsonrpc *rpc;
280     int error;
281     size_t i;
282
283     rpc = open_jsonrpc(argv[1]);
284     request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
285                                      NULL);
286     error = jsonrpc_transact_block(rpc, request, &reply);
287     if (error) {
288         ovs_fatal(error, "transaction failed");
289     }
290
291     if (reply->result->type != JSON_ARRAY) {
292         ovs_fatal(0, "list_dbs response is not array");
293     }
294
295     for (i = 0; i < reply->result->u.array.n; i++) {
296         const struct json *name = reply->result->u.array.elems[i];
297
298         if (name->type != JSON_STRING) {
299             ovs_fatal(0, "list_dbs response %zu is not string", i);
300         }
301         puts(name->u.string);
302     }
303     jsonrpc_msg_destroy(reply);
304 }
305
306 static void
307 do_get_schema(int argc OVS_UNUSED, char *argv[])
308 {
309     struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
310     print_and_free_json(ovsdb_schema_to_json(schema));
311     ovsdb_schema_destroy(schema);
312 }
313
314 static void
315 do_get_schema_version(int argc OVS_UNUSED, char *argv[])
316 {
317     struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
318     puts(schema->version);
319     ovsdb_schema_destroy(schema);
320 }
321
322 static void
323 do_list_tables(int argc OVS_UNUSED, char *argv[])
324 {
325     struct ovsdb_schema *schema;
326     struct shash_node *node;
327     struct table t;
328
329     schema = fetch_schema(argv[1], argv[2]);
330     table_init(&t);
331     table_add_column(&t, "Table");
332     SHASH_FOR_EACH (node, &schema->tables) {
333         struct ovsdb_table_schema *ts = node->data;
334
335         table_add_row(&t);
336         table_add_cell(&t)->text = xstrdup(ts->name);
337     }
338     ovsdb_schema_destroy(schema);
339     table_print(&t, &table_style);
340 }
341
342 static void
343 do_list_columns(int argc OVS_UNUSED, char *argv[])
344 {
345     const char *table_name = argv[3];
346     struct ovsdb_schema *schema;
347     struct shash_node *table_node;
348     struct table t;
349
350     schema = fetch_schema(argv[1], argv[2]);
351     table_init(&t);
352     if (!table_name) {
353         table_add_column(&t, "Table");
354     }
355     table_add_column(&t, "Column");
356     table_add_column(&t, "Type");
357     SHASH_FOR_EACH (table_node, &schema->tables) {
358         struct ovsdb_table_schema *ts = table_node->data;
359
360         if (!table_name || !strcmp(table_name, ts->name)) {
361             struct shash_node *column_node;
362
363             SHASH_FOR_EACH (column_node, &ts->columns) {
364                 const struct ovsdb_column *column = column_node->data;
365
366                 table_add_row(&t);
367                 if (!table_name) {
368                     table_add_cell(&t)->text = xstrdup(ts->name);
369                 }
370                 table_add_cell(&t)->text = xstrdup(column->name);
371                 table_add_cell(&t)->json = ovsdb_type_to_json(&column->type);
372             }
373         }
374     }
375     ovsdb_schema_destroy(schema);
376     table_print(&t, &table_style);
377 }
378
379 static void
380 do_transact(int argc OVS_UNUSED, char *argv[])
381 {
382     struct jsonrpc_msg *request, *reply;
383     struct json *transaction;
384     struct jsonrpc *rpc;
385     int error;
386
387     transaction = parse_json(argv[2]);
388
389     rpc = open_jsonrpc(argv[1]);
390     request = jsonrpc_create_request("transact", transaction, NULL);
391     error = jsonrpc_transact_block(rpc, request, &reply);
392     if (error) {
393         ovs_fatal(error, "transaction failed");
394     }
395     if (reply->error) {
396         ovs_fatal(error, "transaction returned error: %s",
397                   json_to_string(reply->error, table_style.json_flags));
398     }
399     print_json(reply->result);
400     putchar('\n');
401     jsonrpc_msg_destroy(reply);
402     jsonrpc_close(rpc);
403 }
404
405 static void
406 monitor_print_row(struct json *row, const char *type, const char *uuid,
407                   const struct ovsdb_column_set *columns, struct table *t)
408 {
409     size_t i;
410
411     if (!row) {
412         ovs_error(0, "missing %s row", type);
413         return;
414     } else if (row->type != JSON_OBJECT) {
415         ovs_error(0, "<row> is not object");
416         return;
417     }
418
419     table_add_row(t);
420     table_add_cell(t)->text = xstrdup(uuid);
421     table_add_cell(t)->text = xstrdup(type);
422     for (i = 0; i < columns->n_columns; i++) {
423         const struct ovsdb_column *column = columns->columns[i];
424         struct json *value = shash_find_data(json_object(row), column->name);
425         struct cell *cell = table_add_cell(t);
426         if (value) {
427             cell->json = json_clone(value);
428             cell->type = &column->type;
429         }
430     }
431 }
432
433 static void
434 monitor_print(struct json *table_updates,
435               const struct ovsdb_table_schema *table,
436               const struct ovsdb_column_set *columns, bool initial)
437 {
438     struct json *table_update;
439     struct shash_node *node;
440     struct table t;
441     size_t i;
442
443     table_init(&t);
444
445     if (table_updates->type != JSON_OBJECT) {
446         ovs_error(0, "<table-updates> is not object");
447         return;
448     }
449     table_update = shash_find_data(json_object(table_updates), table->name);
450     if (!table_update) {
451         return;
452     }
453     if (table_update->type != JSON_OBJECT) {
454         ovs_error(0, "<table-update> is not object");
455         return;
456     }
457
458     table_add_column(&t, "row");
459     table_add_column(&t, "action");
460     for (i = 0; i < columns->n_columns; i++) {
461         table_add_column(&t, "%s", columns->columns[i]->name);
462     }
463     SHASH_FOR_EACH (node, json_object(table_update)) {
464         struct json *row_update = node->data;
465         struct json *old, *new;
466
467         if (row_update->type != JSON_OBJECT) {
468             ovs_error(0, "<row-update> is not object");
469             continue;
470         }
471         old = shash_find_data(json_object(row_update), "old");
472         new = shash_find_data(json_object(row_update), "new");
473         if (initial) {
474             monitor_print_row(new, "initial", node->name, columns, &t);
475         } else if (!old) {
476             monitor_print_row(new, "insert", node->name, columns, &t);
477         } else if (!new) {
478             monitor_print_row(old, "delete", node->name, columns, &t);
479         } else {
480             monitor_print_row(old, "old", node->name, columns, &t);
481             monitor_print_row(new, "new", "", columns, &t);
482         }
483     }
484     table_print(&t, &table_style);
485     table_destroy(&t);
486 }
487
488 static void
489 add_column(const char *server, const struct ovsdb_column *column,
490            struct ovsdb_column_set *columns, struct json *columns_json)
491 {
492     if (ovsdb_column_set_contains(columns, column->index)) {
493         ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
494                   server, column->name);
495     }
496     ovsdb_column_set_add(columns, column);
497     json_array_add(columns_json, json_string_create(column->name));
498 }
499
500 static struct json *
501 parse_monitor_columns(char *arg, const char *server, const char *database,
502                       const struct ovsdb_table_schema *table,
503                       struct ovsdb_column_set *columns)
504 {
505     bool initial, insert, delete, modify;
506     struct json *mr, *columns_json;
507     char *save_ptr = NULL;
508     char *token;
509
510     mr = json_object_create();
511     columns_json = json_array_create_empty();
512     json_object_put(mr, "columns", columns_json);
513
514     initial = insert = delete = modify = true;
515     for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
516          token = strtok_r(NULL, ",", &save_ptr)) {
517         if (!strcmp(token, "!initial")) {
518             initial = false;
519         } else if (!strcmp(token, "!insert")) {
520             insert = false;
521         } else if (!strcmp(token, "!delete")) {
522             delete = false;
523         } else if (!strcmp(token, "!modify")) {
524             modify = false;
525         } else {
526             const struct ovsdb_column *column;
527
528             column = ovsdb_table_schema_get_column(table, token);
529             if (!column) {
530                 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
531                           "column named \"%s\"",
532                           server, table->name, database, token);
533             }
534             add_column(server, column, columns, columns_json);
535         }
536     }
537
538     if (columns_json->u.array.n == 0) {
539         const struct shash_node **nodes;
540         size_t i, n;
541
542         n = shash_count(&table->columns);
543         nodes = shash_sort(&table->columns);
544         for (i = 0; i < n; i++) {
545             const struct ovsdb_column *column = nodes[i]->data;
546             if (column->index != OVSDB_COL_UUID
547                 && column->index != OVSDB_COL_VERSION) {
548                 add_column(server, column, columns, columns_json);
549             }
550         }
551         free(nodes);
552
553         add_column(server, ovsdb_table_schema_get_column(table,"_version"),
554                    columns, columns_json);
555     }
556
557     if (!initial || !insert || !delete || !modify) {
558         struct json *select = json_object_create();
559         json_object_put(select, "initial", json_boolean_create(initial));
560         json_object_put(select, "insert", json_boolean_create(insert));
561         json_object_put(select, "delete", json_boolean_create(delete));
562         json_object_put(select, "modify", json_boolean_create(modify));
563         json_object_put(mr, "select", select);
564     }
565
566     return mr;
567 }
568
569 static void
570 do_monitor(int argc, char *argv[])
571 {
572     const char *server = argv[1];
573     const char *database = argv[2];
574     const char *table_name = argv[3];
575     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
576     struct ovsdb_table_schema *table;
577     struct ovsdb_schema *schema;
578     struct jsonrpc_msg *request;
579     struct jsonrpc *rpc;
580     struct json *monitor, *monitor_request_array,
581         *monitor_requests, *request_id;
582
583     rpc = open_jsonrpc(server);
584
585     schema = fetch_schema_from_rpc(rpc, database);
586     table = shash_find_data(&schema->tables, table_name);
587     if (!table) {
588         ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
589                   server, database, table_name);
590     }
591
592     monitor_request_array = json_array_create_empty();
593     if (argc > 4) {
594         int i;
595
596         for (i = 4; i < argc; i++) {
597             json_array_add(
598                 monitor_request_array,
599                 parse_monitor_columns(argv[i], server, database, table,
600                                       &columns));
601         }
602     } else {
603         /* Allocate a writable empty string since parse_monitor_columns() is
604          * going to strtok() it and that's risky with literal "". */
605         char empty[] = "";
606         json_array_add(
607             monitor_request_array,
608             parse_monitor_columns(empty, server, database, table, &columns));
609     }
610
611     monitor_requests = json_object_create();
612     json_object_put(monitor_requests, table_name, monitor_request_array);
613
614     monitor = json_array_create_3(json_string_create(database),
615                                   json_null_create(), monitor_requests);
616     request = jsonrpc_create_request("monitor", monitor, NULL);
617     request_id = json_clone(request->id);
618     jsonrpc_send(rpc, request);
619     for (;;) {
620         struct jsonrpc_msg *msg;
621         int error;
622
623         error = jsonrpc_recv_block(rpc, &msg);
624         if (error) {
625             ovsdb_schema_destroy(schema);
626             ovs_fatal(error, "%s: receive failed", server);
627         }
628
629         if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
630             jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
631                                                    msg->id));
632         } else if (msg->type == JSONRPC_REPLY
633                    && json_equal(msg->id, request_id)) {
634             monitor_print(msg->result, table, &columns, true);
635             fflush(stdout);
636             if (get_detach()) {
637                 /* daemonize() closes the standard file descriptors.  We output
638                  * to stdout, so we need to save and restore STDOUT_FILENO. */
639                 int fd = dup(STDOUT_FILENO);
640                 daemonize();
641                 dup2(fd, STDOUT_FILENO);
642                 close(fd);
643             }
644         } else if (msg->type == JSONRPC_NOTIFY
645                    && !strcmp(msg->method, "update")) {
646             struct json *params = msg->params;
647             if (params->type == JSON_ARRAY
648                 && params->u.array.n == 2
649                 && params->u.array.elems[0]->type == JSON_NULL) {
650                 monitor_print(params->u.array.elems[1],
651                               table, &columns, false);
652                 fflush(stdout);
653             }
654         }
655         jsonrpc_msg_destroy(msg);
656     }
657 }
658
659 struct dump_table_aux {
660     struct ovsdb_datum **data;
661     const struct ovsdb_column **columns;
662     size_t n_columns;
663 };
664
665 static int
666 compare_data(size_t a_y, size_t b_y, size_t x,
667              const struct dump_table_aux *aux)
668 {
669     return ovsdb_datum_compare_3way(&aux->data[a_y][x],
670                                     &aux->data[b_y][x],
671                                     &aux->columns[x]->type);
672 }
673
674 static int
675 compare_rows(size_t a_y, size_t b_y, void *aux_)
676 {
677     struct dump_table_aux *aux = aux_;
678     size_t x;
679
680     /* Skip UUID columns on the first pass, since their values tend to be
681      * random and make our results less reproducible. */
682     for (x = 0; x < aux->n_columns; x++) {
683         if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
684             int cmp = compare_data(a_y, b_y, x, aux);
685             if (cmp) {
686                 return cmp;
687             }
688         }
689     }
690
691     /* Use UUID columns as tie-breakers. */
692     for (x = 0; x < aux->n_columns; x++) {
693         if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
694             int cmp = compare_data(a_y, b_y, x, aux);
695             if (cmp) {
696                 return cmp;
697             }
698         }
699     }
700
701     return 0;
702 }
703
704 static void
705 swap_rows(size_t a_y, size_t b_y, void *aux_)
706 {
707     struct dump_table_aux *aux = aux_;
708     struct ovsdb_datum *tmp = aux->data[a_y];
709     aux->data[a_y] = aux->data[b_y];
710     aux->data[b_y] = tmp;
711 }
712
713 static int
714 compare_columns(const void *a_, const void *b_)
715 {
716     const struct ovsdb_column *const *ap = a_;
717     const struct ovsdb_column *const *bp = b_;
718     const struct ovsdb_column *a = *ap;
719     const struct ovsdb_column *b = *bp;
720
721     return strcmp(a->name, b->name);
722 }
723
724 static void
725 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
726 {
727     const struct ovsdb_column **columns;
728     size_t n_columns;
729
730     struct ovsdb_datum **data;
731
732     struct dump_table_aux aux;
733     struct shash_node *node;
734     struct table t;
735     size_t x, y;
736
737     /* Sort columns by name, for reproducibility. */
738     columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
739     n_columns = 0;
740     SHASH_FOR_EACH (node, &ts->columns) {
741         struct ovsdb_column *column = node->data;
742         if (strcmp(column->name, "_version")) {
743             columns[n_columns++] = column;
744         }
745     }
746     qsort(columns, n_columns, sizeof *columns, compare_columns);
747
748     /* Extract data from table. */
749     data = xmalloc(rows->n * sizeof *data);
750     for (y = 0; y < rows->n; y++) {
751         struct shash *row;
752
753         if (rows->elems[y]->type != JSON_OBJECT) {
754             ovs_fatal(0,  "row %zu in table %s response is not a JSON object: "
755                       "%s", y, ts->name, json_to_string(rows->elems[y], 0));
756         }
757         row = json_object(rows->elems[y]);
758
759         data[y] = xmalloc(n_columns * sizeof **data);
760         for (x = 0; x < n_columns; x++) {
761             const struct json *json = shash_find_data(row, columns[x]->name);
762             if (!json) {
763                 ovs_fatal(0, "row %zu in table %s response lacks %s column",
764                           y, ts->name, columns[x]->name);
765             }
766
767             check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
768                                                     &columns[x]->type,
769                                                     json, NULL));
770         }
771     }
772
773     /* Sort rows by column values, for reproducibility. */
774     aux.data = data;
775     aux.columns = columns;
776     aux.n_columns = n_columns;
777     sort(rows->n, compare_rows, swap_rows, &aux);
778
779     /* Add column headings. */
780     table_init(&t);
781     table_set_caption(&t, xasprintf("%s table", ts->name));
782     for (x = 0; x < n_columns; x++) {
783         table_add_column(&t, "%s", columns[x]->name);
784     }
785
786     /* Print rows. */
787     for (y = 0; y < rows->n; y++) {
788         table_add_row(&t);
789         for (x = 0; x < n_columns; x++) {
790             struct cell *cell = table_add_cell(&t);
791             cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
792             cell->type = &columns[x]->type;
793         }
794     }
795     table_print(&t, &table_style);
796     table_destroy(&t);
797 }
798
799 static void
800 do_dump(int argc OVS_UNUSED, char *argv[])
801 {
802     const char *server = argv[1];
803     const char *database = argv[2];
804
805     struct jsonrpc_msg *request, *reply;
806     struct ovsdb_schema *schema;
807     struct json *transaction;
808     struct jsonrpc *rpc;
809     int error;
810
811     const struct shash_node **tables;
812     size_t n_tables;
813
814     size_t i;
815
816     rpc = open_jsonrpc(server);
817
818     schema = fetch_schema_from_rpc(rpc, database);
819     tables = shash_sort(&schema->tables);
820     n_tables = shash_count(&schema->tables);
821
822     /* Construct transaction to retrieve entire database. */
823     transaction = json_array_create_1(json_string_create(database));
824     for (i = 0; i < n_tables; i++) {
825         const struct ovsdb_table_schema *ts = tables[i]->data;
826         struct json *op, *columns;
827         struct shash_node *node;
828
829         columns = json_array_create_empty();
830         SHASH_FOR_EACH (node, &ts->columns) {
831             const struct ovsdb_column *column = node->data;
832
833             if (strcmp(column->name, "_version")) {
834                 json_array_add(columns, json_string_create(column->name));
835             }
836         }
837
838         op = json_object_create();
839         json_object_put_string(op, "op", "select");
840         json_object_put_string(op, "table", tables[i]->name);
841         json_object_put(op, "where", json_array_create_empty());
842         json_object_put(op, "columns", columns);
843         json_array_add(transaction, op);
844     }
845
846     /* Send request, get reply. */
847     request = jsonrpc_create_request("transact", transaction, NULL);
848     error = jsonrpc_transact_block(rpc, request, &reply);
849     if (error) {
850         ovs_fatal(error, "transaction failed");
851     }
852
853     /* Print database contents. */
854     if (reply->result->type != JSON_ARRAY
855         || reply->result->u.array.n != n_tables) {
856         ovs_fatal(0, "reply is not array of %zu elements: %s",
857                   n_tables, json_to_string(reply->result, 0));
858     }
859     for (i = 0; i < n_tables; i++) {
860         const struct ovsdb_table_schema *ts = tables[i]->data;
861         const struct json *op_result = reply->result->u.array.elems[i];
862         struct json *rows;
863
864         if (op_result->type != JSON_OBJECT
865             || !(rows = shash_find_data(json_object(op_result), "rows"))
866             || rows->type != JSON_ARRAY) {
867             ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
868                       "member array: %s",
869                       ts->name, json_to_string(op_result, 0));
870         }
871
872         dump_table(ts, &rows->u.array);
873     }
874 }
875
876 static void
877 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
878 {
879     usage();
880 }
881
882 static const struct command all_commands[] = {
883     { "list-dbs", 1, 1, do_list_dbs },
884     { "get-schema", 2, 2, do_get_schema },
885     { "get-schema-version", 2, 2, do_get_schema_version },
886     { "list-tables", 2, 2, do_list_tables },
887     { "list-columns", 2, 3, do_list_columns },
888     { "transact", 2, 2, do_transact },
889     { "monitor", 3, INT_MAX, do_monitor },
890     { "dump", 2, 2, do_dump },
891     { "help", 0, INT_MAX, do_help },
892     { NULL, 0, 0, NULL },
893 };