2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
28 #include "command-line.h"
32 #include "dynamic-string.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-error.h"
40 #include "stream-ssl.h"
46 VLOG_DEFINE_THIS_MODULE(ovsdb_client
);
48 /* --format: Output formatting. */
50 FMT_TABLE
, /* Textual table. */
51 FMT_HTML
, /* HTML table. */
52 FMT_CSV
/* Comma-separated lines. */
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
);
316 const struct ovsdb_type
*type
;
320 cell_to_text(const struct cell
*cell_
)
322 struct cell
*cell
= (struct cell
*) cell_
;
325 if (data_format
== DF_JSON
|| !cell
->type
) {
326 cell
->text
= json_to_string(cell
->json
, JSSF_SORT
);
327 } else if (data_format
== DF_STRING
) {
328 struct ovsdb_datum datum
;
329 struct ovsdb_error
*error
;
332 error
= ovsdb_datum_from_json(&datum
, cell
->type
, cell
->json
,
336 ovsdb_datum_to_string(&datum
, cell
->type
, &s
);
337 ovsdb_datum_destroy(&datum
, cell
->type
);
338 cell
->text
= ds_steal_cstr(&s
);
340 cell
->text
= json_to_string(cell
->json
, JSSF_SORT
);
346 cell
->text
= xstrdup("");
354 cell_destroy(struct cell
*cell
)
357 json_destroy(cell
->json
);
362 struct column
*columns
;
363 size_t n_columns
, allocated_columns
;
364 size_t n_rows
, allocated_rows
;
365 size_t current_column
;
370 table_init(struct table
*table
)
372 memset(table
, 0, sizeof *table
);
376 table_destroy(struct table
*table
)
380 for (i
= 0; i
< table
->n_columns
; i
++) {
381 free(table
->columns
[i
].heading
);
383 free(table
->columns
);
385 for (i
= 0; i
< table
->n_columns
* table
->n_rows
; i
++) {
386 cell_destroy(&table
->cells
[i
]);
390 free(table
->caption
);
394 table_set_caption(struct table
*table
, char *caption
)
396 free(table
->caption
);
397 table
->caption
= caption
;
401 table_add_column(struct table
*table
, const char *heading
, ...)
405 table_add_column(struct table
*table
, const char *heading
, ...)
407 struct column
*column
;
410 assert(!table
->n_rows
);
411 if (table
->n_columns
>= table
->allocated_columns
) {
412 table
->columns
= x2nrealloc(table
->columns
, &table
->allocated_columns
,
413 sizeof *table
->columns
);
415 column
= &table
->columns
[table
->n_columns
++];
417 va_start(args
, heading
);
418 column
->heading
= xvasprintf(heading
, args
);
423 table_cell__(const struct table
*table
, size_t row
, size_t column
)
425 return &table
->cells
[column
+ row
* table
->n_columns
];
429 table_add_row(struct table
*table
)
433 if (table
->n_rows
>= table
->allocated_rows
) {
434 table
->cells
= x2nrealloc(table
->cells
, &table
->allocated_rows
,
435 table
->n_columns
* sizeof *table
->cells
);
439 table
->current_column
= 0;
440 for (x
= 0; x
< table
->n_columns
; x
++) {
441 struct cell
*cell
= table_cell__(table
, y
, x
);
442 memset(cell
, 0, sizeof *cell
);
447 table_add_cell(struct table
*table
)
451 assert(table
->n_rows
> 0);
452 assert(table
->current_column
< table
->n_columns
);
454 x
= table
->current_column
++;
455 y
= table
->n_rows
- 1;
457 return table_cell__(table
, y
, x
);
461 table_print_table_line__(struct ds
*line
)
468 table_print_table__(const struct table
*table
)
471 struct ds line
= DS_EMPTY_INITIALIZER
;
479 if (table
->caption
) {
480 puts(table
->caption
);
483 widths
= xmalloc(table
->n_columns
* sizeof *widths
);
484 for (x
= 0; x
< table
->n_columns
; x
++) {
485 const struct column
*column
= &table
->columns
[x
];
487 widths
[x
] = strlen(column
->heading
);
488 for (y
= 0; y
< table
->n_rows
; y
++) {
489 const char *text
= cell_to_text(table_cell__(table
, y
, x
));
490 size_t length
= strlen(text
);
492 if (length
> widths
[x
])
497 if (output_headings
) {
498 for (x
= 0; x
< table
->n_columns
; x
++) {
499 const struct column
*column
= &table
->columns
[x
];
501 ds_put_char(&line
, ' ');
503 ds_put_format(&line
, "%-*s", widths
[x
], column
->heading
);
505 table_print_table_line__(&line
);
507 for (x
= 0; x
< table
->n_columns
; x
++) {
509 ds_put_char(&line
, ' ');
511 ds_put_char_multiple(&line
, '-', widths
[x
]);
513 table_print_table_line__(&line
);
516 for (y
= 0; y
< table
->n_rows
; y
++) {
517 for (x
= 0; x
< table
->n_columns
; x
++) {
518 const char *text
= cell_to_text(table_cell__(table
, y
, x
));
520 ds_put_char(&line
, ' ');
522 ds_put_format(&line
, "%-*s", widths
[x
], text
);
524 table_print_table_line__(&line
);
532 table_escape_html_text__(const char *s
, size_t n
)
536 for (i
= 0; i
< n
; i
++) {
541 fputs("&", stdout
);
544 fputs("<", stdout
);
547 fputs(">", stdout
);
550 fputs(""", stdout
);
560 table_print_html_cell__(const char *element
, const char *content
)
564 printf(" <%s>", element
);
565 for (p
= content
; *p
; ) {
568 if (uuid_from_string_prefix(&uuid
, p
)) {
569 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN
, p
, 8, p
);
572 table_escape_html_text__(p
, 1);
576 printf("</%s>\n", element
);
580 table_print_html__(const struct table
*table
)
584 fputs("<table border=1>\n", stdout
);
586 if (table
->caption
) {
587 table_print_html_cell__("caption", table
->caption
);
590 if (output_headings
) {
591 fputs(" <tr>\n", stdout
);
592 for (x
= 0; x
< table
->n_columns
; x
++) {
593 const struct column
*column
= &table
->columns
[x
];
594 table_print_html_cell__("th", column
->heading
);
596 fputs(" </tr>\n", stdout
);
599 for (y
= 0; y
< table
->n_rows
; y
++) {
600 fputs(" <tr>\n", stdout
);
601 for (x
= 0; x
< table
->n_columns
; x
++) {
602 const char *content
= cell_to_text(table_cell__(table
, y
, x
));
604 if (!strcmp(table
->columns
[x
].heading
, "_uuid")) {
605 fputs(" <td><a name=\"", stdout
);
606 table_escape_html_text__(content
, strlen(content
));
607 fputs("\">", stdout
);
608 table_escape_html_text__(content
, 8);
609 fputs("</a></td>\n", stdout
);
611 table_print_html_cell__("td", content
);
614 fputs(" </tr>\n", stdout
);
617 fputs("</table>\n", stdout
);
621 table_print_csv_cell__(const char *content
)
625 if (!strpbrk(content
, "\n\",")) {
626 fputs(content
, stdout
);
629 for (p
= content
; *p
!= '\0'; p
++) {
632 fputs("\"\"", stdout
);
644 table_print_csv__(const struct table
*table
)
653 if (table
->caption
) {
654 puts(table
->caption
);
657 if (output_headings
) {
658 for (x
= 0; x
< table
->n_columns
; x
++) {
659 const struct column
*column
= &table
->columns
[x
];
663 table_print_csv_cell__(column
->heading
);
668 for (y
= 0; y
< table
->n_rows
; y
++) {
669 for (x
= 0; x
< table
->n_columns
; x
++) {
673 table_print_csv_cell__(cell_to_text(table_cell__(table
, y
, x
)));
680 table_print(const struct table
*table
)
682 switch (output_format
) {
684 table_print_table__(table
);
688 table_print_html__(table
);
692 table_print_csv__(table
);
698 do_list_dbs(int argc OVS_UNUSED
, char *argv
[])
700 struct jsonrpc_msg
*request
, *reply
;
705 rpc
= open_jsonrpc(argv
[1]);
706 request
= jsonrpc_create_request("list_dbs", json_array_create_empty(),
708 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
710 ovs_fatal(error
, "transaction failed");
713 if (reply
->result
->type
!= JSON_ARRAY
) {
714 ovs_fatal(0, "list_dbs response is not array");
717 for (i
= 0; i
< reply
->result
->u
.array
.n
; i
++) {
718 const struct json
*name
= reply
->result
->u
.array
.elems
[i
];
720 if (name
->type
!= JSON_STRING
) {
721 ovs_fatal(0, "list_dbs response %zu is not string", i
);
723 puts(name
->u
.string
);
725 jsonrpc_msg_destroy(reply
);
729 do_get_schema(int argc OVS_UNUSED
, char *argv
[])
731 struct ovsdb_schema
*schema
= fetch_schema(argv
[1], argv
[2]);
732 print_and_free_json(ovsdb_schema_to_json(schema
));
733 ovsdb_schema_destroy(schema
);
737 do_list_tables(int argc OVS_UNUSED
, char *argv
[])
739 struct ovsdb_schema
*schema
;
740 struct shash_node
*node
;
743 schema
= fetch_schema(argv
[1], argv
[2]);
745 table_add_column(&t
, "Table");
746 SHASH_FOR_EACH (node
, &schema
->tables
) {
747 struct ovsdb_table_schema
*ts
= node
->data
;
750 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
752 ovsdb_schema_destroy(schema
);
757 do_list_columns(int argc OVS_UNUSED
, char *argv
[])
759 const char *table_name
= argv
[3];
760 struct ovsdb_schema
*schema
;
761 struct shash_node
*table_node
;
764 schema
= fetch_schema(argv
[1], argv
[2]);
767 table_add_column(&t
, "Table");
769 table_add_column(&t
, "Column");
770 table_add_column(&t
, "Type");
771 SHASH_FOR_EACH (table_node
, &schema
->tables
) {
772 struct ovsdb_table_schema
*ts
= table_node
->data
;
774 if (!table_name
|| !strcmp(table_name
, ts
->name
)) {
775 struct shash_node
*column_node
;
777 SHASH_FOR_EACH (column_node
, &ts
->columns
) {
778 const struct ovsdb_column
*column
= column_node
->data
;
782 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
784 table_add_cell(&t
)->text
= xstrdup(column
->name
);
785 table_add_cell(&t
)->json
= ovsdb_type_to_json(&column
->type
);
789 ovsdb_schema_destroy(schema
);
794 do_transact(int argc OVS_UNUSED
, char *argv
[])
796 struct jsonrpc_msg
*request
, *reply
;
797 struct json
*transaction
;
801 transaction
= parse_json(argv
[2]);
803 rpc
= open_jsonrpc(argv
[1]);
804 request
= jsonrpc_create_request("transact", transaction
, NULL
);
805 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
807 ovs_fatal(error
, "transaction failed");
810 ovs_fatal(error
, "transaction returned error: %s",
811 json_to_string(reply
->error
, json_flags
));
813 print_json(reply
->result
);
815 jsonrpc_msg_destroy(reply
);
820 monitor_print_row(struct json
*row
, const char *type
, const char *uuid
,
821 const struct ovsdb_column_set
*columns
, struct table
*t
)
826 ovs_error(0, "missing %s row", type
);
828 } else if (row
->type
!= JSON_OBJECT
) {
829 ovs_error(0, "<row> is not object");
834 table_add_cell(t
)->text
= xstrdup(uuid
);
835 table_add_cell(t
)->text
= xstrdup(type
);
836 for (i
= 0; i
< columns
->n_columns
; i
++) {
837 const struct ovsdb_column
*column
= columns
->columns
[i
];
838 struct json
*value
= shash_find_data(json_object(row
), column
->name
);
839 struct cell
*cell
= table_add_cell(t
);
841 cell
->json
= json_clone(value
);
842 cell
->type
= &column
->type
;
848 monitor_print(struct json
*table_updates
,
849 const struct ovsdb_table_schema
*table
,
850 const struct ovsdb_column_set
*columns
, bool initial
)
852 struct json
*table_update
;
853 struct shash_node
*node
;
859 if (table_updates
->type
!= JSON_OBJECT
) {
860 ovs_error(0, "<table-updates> is not object");
863 table_update
= shash_find_data(json_object(table_updates
), table
->name
);
867 if (table_update
->type
!= JSON_OBJECT
) {
868 ovs_error(0, "<table-update> is not object");
872 table_add_column(&t
, "row");
873 table_add_column(&t
, "action");
874 for (i
= 0; i
< columns
->n_columns
; i
++) {
875 table_add_column(&t
, "%s", columns
->columns
[i
]->name
);
877 SHASH_FOR_EACH (node
, json_object(table_update
)) {
878 struct json
*row_update
= node
->data
;
879 struct json
*old
, *new;
881 if (row_update
->type
!= JSON_OBJECT
) {
882 ovs_error(0, "<row-update> is not object");
885 old
= shash_find_data(json_object(row_update
), "old");
886 new = shash_find_data(json_object(row_update
), "new");
888 monitor_print_row(new, "initial", node
->name
, columns
, &t
);
890 monitor_print_row(new, "insert", node
->name
, columns
, &t
);
892 monitor_print_row(old
, "delete", node
->name
, columns
, &t
);
894 monitor_print_row(old
, "old", node
->name
, columns
, &t
);
895 monitor_print_row(new, "new", "", columns
, &t
);
903 add_column(const char *server
, const struct ovsdb_column
*column
,
904 struct ovsdb_column_set
*columns
, struct json
*columns_json
)
906 if (ovsdb_column_set_contains(columns
, column
->index
)) {
907 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
908 server
, column
->name
);
910 ovsdb_column_set_add(columns
, column
);
911 json_array_add(columns_json
, json_string_create(column
->name
));
915 parse_monitor_columns(char *arg
, const char *server
, const char *database
,
916 const struct ovsdb_table_schema
*table
,
917 struct ovsdb_column_set
*columns
)
919 bool initial
, insert
, delete, modify
;
920 struct json
*mr
, *columns_json
;
921 char *save_ptr
= NULL
;
924 mr
= json_object_create();
925 columns_json
= json_array_create_empty();
926 json_object_put(mr
, "columns", columns_json
);
928 initial
= insert
= delete = modify
= true;
929 for (token
= strtok_r(arg
, ",", &save_ptr
); token
!= NULL
;
930 token
= strtok_r(NULL
, ",", &save_ptr
)) {
931 if (!strcmp(token
, "!initial")) {
933 } else if (!strcmp(token
, "!insert")) {
935 } else if (!strcmp(token
, "!delete")) {
937 } else if (!strcmp(token
, "!modify")) {
940 const struct ovsdb_column
*column
;
942 column
= ovsdb_table_schema_get_column(table
, token
);
944 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
945 "column named \"%s\"",
946 server
, table
->name
, database
, token
);
948 add_column(server
, column
, columns
, columns_json
);
952 if (columns_json
->u
.array
.n
== 0) {
953 const struct shash_node
**nodes
;
956 n
= shash_count(&table
->columns
);
957 nodes
= shash_sort(&table
->columns
);
958 for (i
= 0; i
< n
; i
++) {
959 const struct ovsdb_column
*column
= nodes
[i
]->data
;
960 if (column
->index
!= OVSDB_COL_UUID
961 && column
->index
!= OVSDB_COL_VERSION
) {
962 add_column(server
, column
, columns
, columns_json
);
967 add_column(server
, ovsdb_table_schema_get_column(table
,"_version"),
968 columns
, columns_json
);
971 if (!initial
|| !insert
|| !delete || !modify
) {
972 struct json
*select
= json_object_create();
973 json_object_put(select
, "initial", json_boolean_create(initial
));
974 json_object_put(select
, "insert", json_boolean_create(insert
));
975 json_object_put(select
, "delete", json_boolean_create(delete));
976 json_object_put(select
, "modify", json_boolean_create(modify
));
977 json_object_put(mr
, "select", select
);
984 do_monitor(int argc
, char *argv
[])
986 const char *server
= argv
[1];
987 const char *database
= argv
[2];
988 const char *table_name
= argv
[3];
989 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
990 struct ovsdb_table_schema
*table
;
991 struct ovsdb_schema
*schema
;
992 struct jsonrpc_msg
*request
;
994 struct json
*monitor
, *monitor_request_array
,
995 *monitor_requests
, *request_id
;
997 rpc
= open_jsonrpc(server
);
999 schema
= fetch_schema_from_rpc(rpc
, database
);
1000 table
= shash_find_data(&schema
->tables
, table_name
);
1002 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
1003 server
, database
, table_name
);
1006 monitor_request_array
= json_array_create_empty();
1010 for (i
= 4; i
< argc
; i
++) {
1012 monitor_request_array
,
1013 parse_monitor_columns(argv
[i
], server
, database
, table
,
1017 /* Allocate a writable empty string since parse_monitor_columns() is
1018 * going to strtok() it and that's risky with literal "". */
1021 monitor_request_array
,
1022 parse_monitor_columns(empty
, server
, database
, table
, &columns
));
1025 monitor_requests
= json_object_create();
1026 json_object_put(monitor_requests
, table_name
, monitor_request_array
);
1028 monitor
= json_array_create_3(json_string_create(database
),
1029 json_null_create(), monitor_requests
);
1030 request
= jsonrpc_create_request("monitor", monitor
, NULL
);
1031 request_id
= json_clone(request
->id
);
1032 jsonrpc_send(rpc
, request
);
1034 struct jsonrpc_msg
*msg
;
1037 error
= jsonrpc_recv_block(rpc
, &msg
);
1039 ovsdb_schema_destroy(schema
);
1040 ovs_fatal(error
, "%s: receive failed", server
);
1043 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
1044 jsonrpc_send(rpc
, jsonrpc_create_reply(json_clone(msg
->params
),
1046 } else if (msg
->type
== JSONRPC_REPLY
1047 && json_equal(msg
->id
, request_id
)) {
1048 monitor_print(msg
->result
, table
, &columns
, true);
1051 /* daemonize() closes the standard file descriptors. We output
1052 * to stdout, so we need to save and restore STDOUT_FILENO. */
1053 int fd
= dup(STDOUT_FILENO
);
1055 dup2(fd
, STDOUT_FILENO
);
1058 } else if (msg
->type
== JSONRPC_NOTIFY
1059 && !strcmp(msg
->method
, "update")) {
1060 struct json
*params
= msg
->params
;
1061 if (params
->type
== JSON_ARRAY
1062 && params
->u
.array
.n
== 2
1063 && params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
1064 monitor_print(params
->u
.array
.elems
[1],
1065 table
, &columns
, false);
1069 jsonrpc_msg_destroy(msg
);
1073 struct dump_table_aux
{
1074 struct ovsdb_datum
**data
;
1075 const struct ovsdb_column
**columns
;
1080 compare_data(size_t a_y
, size_t b_y
, size_t x
,
1081 const struct dump_table_aux
*aux
)
1083 return ovsdb_datum_compare_3way(&aux
->data
[a_y
][x
],
1085 &aux
->columns
[x
]->type
);
1089 compare_rows(size_t a_y
, size_t b_y
, void *aux_
)
1091 struct dump_table_aux
*aux
= aux_
;
1094 /* Skip UUID columns on the first pass, since their values tend to be
1095 * random and make our results less reproducible. */
1096 for (x
= 0; x
< aux
->n_columns
; x
++) {
1097 if (aux
->columns
[x
]->type
.key
.type
!= OVSDB_TYPE_UUID
) {
1098 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
1105 /* Use UUID columns as tie-breakers. */
1106 for (x
= 0; x
< aux
->n_columns
; x
++) {
1107 if (aux
->columns
[x
]->type
.key
.type
== OVSDB_TYPE_UUID
) {
1108 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
1119 swap_rows(size_t a_y
, size_t b_y
, void *aux_
)
1121 struct dump_table_aux
*aux
= aux_
;
1122 struct ovsdb_datum
*tmp
= aux
->data
[a_y
];
1123 aux
->data
[a_y
] = aux
->data
[b_y
];
1124 aux
->data
[b_y
] = tmp
;
1128 compare_columns(const void *a_
, const void *b_
)
1130 const struct ovsdb_column
*const *ap
= a_
;
1131 const struct ovsdb_column
*const *bp
= b_
;
1132 const struct ovsdb_column
*a
= *ap
;
1133 const struct ovsdb_column
*b
= *bp
;
1135 return strcmp(a
->name
, b
->name
);
1139 dump_table(const struct ovsdb_table_schema
*ts
, struct json_array
*rows
)
1141 const struct ovsdb_column
**columns
;
1144 struct ovsdb_datum
**data
;
1146 struct dump_table_aux aux
;
1147 struct shash_node
*node
;
1151 /* Sort columns by name, for reproducibility. */
1152 columns
= xmalloc(shash_count(&ts
->columns
) * sizeof *columns
);
1154 SHASH_FOR_EACH (node
, &ts
->columns
) {
1155 struct ovsdb_column
*column
= node
->data
;
1156 if (strcmp(column
->name
, "_version")) {
1157 columns
[n_columns
++] = column
;
1160 qsort(columns
, n_columns
, sizeof *columns
, compare_columns
);
1162 /* Extract data from table. */
1163 data
= xmalloc(rows
->n
* sizeof *data
);
1164 for (y
= 0; y
< rows
->n
; y
++) {
1167 if (rows
->elems
[y
]->type
!= JSON_OBJECT
) {
1168 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
1169 "%s", y
, ts
->name
, json_to_string(rows
->elems
[y
], 0));
1171 row
= json_object(rows
->elems
[y
]);
1173 data
[y
] = xmalloc(n_columns
* sizeof **data
);
1174 for (x
= 0; x
< n_columns
; x
++) {
1175 const struct json
*json
= shash_find_data(row
, columns
[x
]->name
);
1177 ovs_fatal(0, "row %zu in table %s response lacks %s column",
1178 y
, ts
->name
, columns
[x
]->name
);
1181 check_ovsdb_error(ovsdb_datum_from_json(&data
[y
][x
],
1187 /* Sort rows by column values, for reproducibility. */
1189 aux
.columns
= columns
;
1190 aux
.n_columns
= n_columns
;
1191 sort(rows
->n
, compare_rows
, swap_rows
, &aux
);
1193 /* Add column headings. */
1195 table_set_caption(&t
, xasprintf("%s table", ts
->name
));
1196 for (x
= 0; x
< n_columns
; x
++) {
1197 table_add_column(&t
, "%s", columns
[x
]->name
);
1201 for (y
= 0; y
< rows
->n
; y
++) {
1203 for (x
= 0; x
< n_columns
; x
++) {
1204 struct cell
*cell
= table_add_cell(&t
);
1205 cell
->json
= ovsdb_datum_to_json(&data
[y
][x
], &columns
[x
]->type
);
1206 cell
->type
= &columns
[x
]->type
;
1214 do_dump(int argc OVS_UNUSED
, char *argv
[])
1216 const char *server
= argv
[1];
1217 const char *database
= argv
[2];
1219 struct jsonrpc_msg
*request
, *reply
;
1220 struct ovsdb_schema
*schema
;
1221 struct json
*transaction
;
1222 struct jsonrpc
*rpc
;
1225 const struct shash_node
**tables
;
1230 rpc
= open_jsonrpc(server
);
1232 schema
= fetch_schema_from_rpc(rpc
, database
);
1233 tables
= shash_sort(&schema
->tables
);
1234 n_tables
= shash_count(&schema
->tables
);
1236 /* Construct transaction to retrieve entire database. */
1237 transaction
= json_array_create_1(json_string_create(database
));
1238 for (i
= 0; i
< n_tables
; i
++) {
1239 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
1240 struct json
*op
, *columns
;
1241 struct shash_node
*node
;
1243 columns
= json_array_create_empty();
1244 SHASH_FOR_EACH (node
, &ts
->columns
) {
1245 const struct ovsdb_column
*column
= node
->data
;
1247 if (strcmp(column
->name
, "_version")) {
1248 json_array_add(columns
, json_string_create(column
->name
));
1252 op
= json_object_create();
1253 json_object_put_string(op
, "op", "select");
1254 json_object_put_string(op
, "table", tables
[i
]->name
);
1255 json_object_put(op
, "where", json_array_create_empty());
1256 json_object_put(op
, "columns", columns
);
1257 json_array_add(transaction
, op
);
1260 /* Send request, get reply. */
1261 request
= jsonrpc_create_request("transact", transaction
, NULL
);
1262 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
1264 ovs_fatal(error
, "transaction failed");
1267 /* Print database contents. */
1268 if (reply
->result
->type
!= JSON_ARRAY
1269 || reply
->result
->u
.array
.n
!= n_tables
) {
1270 ovs_fatal(0, "reply is not array of %zu elements: %s",
1271 n_tables
, json_to_string(reply
->result
, 0));
1273 for (i
= 0; i
< n_tables
; i
++) {
1274 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
1275 const struct json
*op_result
= reply
->result
->u
.array
.elems
[i
];
1278 if (op_result
->type
!= JSON_OBJECT
1279 || !(rows
= shash_find_data(json_object(op_result
), "rows"))
1280 || rows
->type
!= JSON_ARRAY
) {
1281 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1283 ts
->name
, json_to_string(op_result
, 0));
1286 dump_table(ts
, &rows
->u
.array
);
1291 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1296 static const struct command all_commands
[] = {
1297 { "list-dbs", 1, 1, do_list_dbs
},
1298 { "get-schema", 2, 2, do_get_schema
},
1299 { "list-tables", 2, 2, do_list_tables
},
1300 { "list-columns", 2, 3, do_list_columns
},
1301 { "transact", 2, 2, do_transact
},
1302 { "monitor", 3, INT_MAX
, do_monitor
},
1303 { "dump", 2, 2, do_dump
},
1304 { "help", 0, INT_MAX
, do_help
},
1305 { NULL
, 0, 0, NULL
},