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. */
56 /* --no-headings: Whether table output should include headings. */
57 static int output_headings
= true;
59 /* --pretty: Flags to pass to json_to_string(). */
60 static int json_flags
= JSSF_SORT
;
62 /* --data: Format of data in output tables. */
64 DF_STRING
, /* String format. */
68 static const struct command all_commands
[];
70 static void usage(void) NO_RETURN
;
71 static void parse_options(int argc
, char *argv
[]);
74 main(int argc
, char *argv
[])
76 proctitle_init(argc
, argv
);
77 set_program_name(argv
[0]);
78 parse_options(argc
, argv
);
79 signal(SIGPIPE
, SIG_IGN
);
80 run_command(argc
- optind
, argv
+ optind
, all_commands
);
85 parse_options(int argc
, char *argv
[])
88 OPT_BOOTSTRAP_CA_CERT
= UCHAR_MAX
+ 1
90 static struct option long_options
[] = {
91 {"format", required_argument
, 0, 'f'},
92 {"data", required_argument
, 0, 'd'},
93 {"no-headings", no_argument
, &output_headings
, 0},
94 {"pretty", no_argument
, &json_flags
, JSSF_PRETTY
| JSSF_SORT
},
95 {"verbose", optional_argument
, 0, 'v'},
96 {"help", no_argument
, 0, 'h'},
97 {"version", no_argument
, 0, 'V'},
100 {"bootstrap-ca-cert", required_argument
, 0, OPT_BOOTSTRAP_CA_CERT
},
101 STREAM_SSL_LONG_OPTIONS
105 char *short_options
= long_options_to_short_options(long_options
);
110 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
117 if (!strcmp(optarg
, "table")) {
118 output_format
= FMT_TABLE
;
119 } else if (!strcmp(optarg
, "html")) {
120 output_format
= FMT_HTML
;
121 } else if (!strcmp(optarg
, "csv")) {
122 output_format
= FMT_CSV
;
123 } else if (!strcmp(optarg
, "json")) {
124 output_format
= FMT_JSON
;
126 ovs_fatal(0, "unknown output format \"%s\"", optarg
);
131 if (!strcmp(optarg
, "string")) {
132 data_format
= DF_STRING
;
133 } else if (!strcmp(optarg
, "json")) {
134 data_format
= DF_JSON
;
136 ovs_fatal(0, "unknown data format \"%s\"", optarg
);
144 OVS_PRINT_VERSION(0, 0);
148 vlog_set_verbosity(optarg
);
151 DAEMON_OPTION_HANDLERS
154 STREAM_SSL_OPTION_HANDLERS
156 case OPT_BOOTSTRAP_CA_CERT
:
157 stream_ssl_set_ca_cert_file(optarg
, true);
165 /* getopt_long() already set the value for us. */
178 printf("%s: Open vSwitch database JSON-RPC client\n"
179 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
180 "\nValid commands are:\n"
181 "\n list-dbs SERVER\n"
182 " list databases available on SERVER\n"
183 "\n get-schema SERVER DATABASE\n"
184 " retrieve schema for DATABASE from SERVER\n"
185 "\n get-schema-version SERVER DATABASE\n"
186 " retrieve schema for DATABASE from SERVER and report only its\n"
187 " version number on stdout\n"
188 "\n list-tables SERVER DATABASE\n"
189 " list tables for DATABASE on SERVER\n"
190 "\n list-columns SERVER DATABASE [TABLE]\n"
191 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
192 "\n transact SERVER TRANSACTION\n"
193 " run TRANSACTION (a JSON array of operations) on SERVER\n"
194 " and print the results as JSON on stdout\n"
195 "\n monitor SERVER DATABASE TABLE [COLUMN,...]...\n"
196 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
197 " COLUMNs may include !initial, !insert, !delete, !modify\n"
198 " to avoid seeing the specified kinds of changes.\n"
199 "\n dump SERVER DATABASE\n"
200 " dump contents of DATABASE on SERVER to stdout\n",
201 program_name
, program_name
);
202 stream_usage("SERVER", true, true, true);
203 printf("\nOutput formatting options:\n"
204 " -f, --format=FORMAT set output formatting to FORMAT\n"
205 " (\"table\", \"html\", \"csv\", "
207 " --no-headings omit table heading row\n"
208 " --pretty pretty-print JSON in output");
211 printf("\nOther options:\n"
212 " -h, --help display this help message\n"
213 " -V, --version display version information\n");
218 parse_json(const char *s
)
220 struct json
*json
= json_from_string(s
);
221 if (json
->type
== JSON_STRING
) {
222 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
227 static struct jsonrpc
*
228 open_jsonrpc(const char *server
)
230 struct stream
*stream
;
233 error
= stream_open_block(jsonrpc_stream_open(server
, &stream
), &stream
);
234 if (error
== EAFNOSUPPORT
) {
235 struct pstream
*pstream
;
237 error
= jsonrpc_pstream_open(server
, &pstream
);
239 ovs_fatal(error
, "failed to connect or listen to \"%s\"", server
);
242 VLOG_INFO("%s: waiting for connection...", server
);
243 error
= pstream_accept_block(pstream
, &stream
);
245 ovs_fatal(error
, "failed to accept connection on \"%s\"", server
);
248 pstream_close(pstream
);
250 ovs_fatal(error
, "failed to connect to \"%s\"", server
);
253 return jsonrpc_open(stream
);
257 print_json(struct json
*json
)
259 char *string
= json_to_string(json
, json_flags
);
260 fputs(string
, stdout
);
265 print_and_free_json(struct json
*json
)
272 check_ovsdb_error(struct ovsdb_error
*error
)
275 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
279 static struct ovsdb_schema
*
280 fetch_schema_from_rpc(struct jsonrpc
*rpc
, const char *database
)
282 struct jsonrpc_msg
*request
, *reply
;
283 struct ovsdb_schema
*schema
;
286 request
= jsonrpc_create_request("get_schema",
288 json_string_create(database
)),
290 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
292 ovs_fatal(error
, "transaction failed");
294 check_ovsdb_error(ovsdb_schema_from_json(reply
->result
, &schema
));
295 jsonrpc_msg_destroy(reply
);
300 static struct ovsdb_schema
*
301 fetch_schema(const char *server
, const char *database
)
303 struct ovsdb_schema
*schema
;
306 rpc
= open_jsonrpc(server
);
307 schema
= fetch_schema_from_rpc(rpc
, database
);
323 const struct ovsdb_type
*type
;
327 cell_to_text(const struct cell
*cell_
)
329 struct cell
*cell
= (struct cell
*) cell_
;
332 if (data_format
== DF_JSON
|| !cell
->type
) {
333 cell
->text
= json_to_string(cell
->json
, JSSF_SORT
);
334 } else if (data_format
== DF_STRING
) {
335 struct ovsdb_datum datum
;
336 struct ovsdb_error
*error
;
339 error
= ovsdb_datum_from_json(&datum
, cell
->type
, cell
->json
,
343 ovsdb_datum_to_string(&datum
, cell
->type
, &s
);
344 ovsdb_datum_destroy(&datum
, cell
->type
);
345 cell
->text
= ds_steal_cstr(&s
);
347 cell
->text
= json_to_string(cell
->json
, JSSF_SORT
);
353 cell
->text
= xstrdup("");
361 cell_destroy(struct cell
*cell
)
364 json_destroy(cell
->json
);
369 struct column
*columns
;
370 size_t n_columns
, allocated_columns
;
371 size_t n_rows
, allocated_rows
;
372 size_t current_column
;
377 table_init(struct table
*table
)
379 memset(table
, 0, sizeof *table
);
383 table_destroy(struct table
*table
)
387 for (i
= 0; i
< table
->n_columns
; i
++) {
388 free(table
->columns
[i
].heading
);
390 free(table
->columns
);
392 for (i
= 0; i
< table
->n_columns
* table
->n_rows
; i
++) {
393 cell_destroy(&table
->cells
[i
]);
397 free(table
->caption
);
401 table_set_caption(struct table
*table
, char *caption
)
403 free(table
->caption
);
404 table
->caption
= caption
;
408 table_add_column(struct table
*table
, const char *heading
, ...)
412 table_add_column(struct table
*table
, const char *heading
, ...)
414 struct column
*column
;
417 assert(!table
->n_rows
);
418 if (table
->n_columns
>= table
->allocated_columns
) {
419 table
->columns
= x2nrealloc(table
->columns
, &table
->allocated_columns
,
420 sizeof *table
->columns
);
422 column
= &table
->columns
[table
->n_columns
++];
424 va_start(args
, heading
);
425 column
->heading
= xvasprintf(heading
, args
);
430 table_cell__(const struct table
*table
, size_t row
, size_t column
)
432 return &table
->cells
[column
+ row
* table
->n_columns
];
436 table_add_row(struct table
*table
)
440 if (table
->n_rows
>= table
->allocated_rows
) {
441 table
->cells
= x2nrealloc(table
->cells
, &table
->allocated_rows
,
442 table
->n_columns
* sizeof *table
->cells
);
446 table
->current_column
= 0;
447 for (x
= 0; x
< table
->n_columns
; x
++) {
448 struct cell
*cell
= table_cell__(table
, y
, x
);
449 memset(cell
, 0, sizeof *cell
);
454 table_add_cell(struct table
*table
)
458 assert(table
->n_rows
> 0);
459 assert(table
->current_column
< table
->n_columns
);
461 x
= table
->current_column
++;
462 y
= table
->n_rows
- 1;
464 return table_cell__(table
, y
, x
);
468 table_print_table_line__(struct ds
*line
)
475 table_print_table__(const struct table
*table
)
478 struct ds line
= DS_EMPTY_INITIALIZER
;
486 if (table
->caption
) {
487 puts(table
->caption
);
490 widths
= xmalloc(table
->n_columns
* sizeof *widths
);
491 for (x
= 0; x
< table
->n_columns
; x
++) {
492 const struct column
*column
= &table
->columns
[x
];
494 widths
[x
] = strlen(column
->heading
);
495 for (y
= 0; y
< table
->n_rows
; y
++) {
496 const char *text
= cell_to_text(table_cell__(table
, y
, x
));
497 size_t length
= strlen(text
);
499 if (length
> widths
[x
])
504 if (output_headings
) {
505 for (x
= 0; x
< table
->n_columns
; x
++) {
506 const struct column
*column
= &table
->columns
[x
];
508 ds_put_char(&line
, ' ');
510 ds_put_format(&line
, "%-*s", widths
[x
], column
->heading
);
512 table_print_table_line__(&line
);
514 for (x
= 0; x
< table
->n_columns
; x
++) {
516 ds_put_char(&line
, ' ');
518 ds_put_char_multiple(&line
, '-', widths
[x
]);
520 table_print_table_line__(&line
);
523 for (y
= 0; y
< table
->n_rows
; y
++) {
524 for (x
= 0; x
< table
->n_columns
; x
++) {
525 const char *text
= cell_to_text(table_cell__(table
, y
, x
));
527 ds_put_char(&line
, ' ');
529 ds_put_format(&line
, "%-*s", widths
[x
], text
);
531 table_print_table_line__(&line
);
539 table_escape_html_text__(const char *s
, size_t n
)
543 for (i
= 0; i
< n
; i
++) {
548 fputs("&", stdout
);
551 fputs("<", stdout
);
554 fputs(">", stdout
);
557 fputs(""", stdout
);
567 table_print_html_cell__(const char *element
, const char *content
)
571 printf(" <%s>", element
);
572 for (p
= content
; *p
; ) {
575 if (uuid_from_string_prefix(&uuid
, p
)) {
576 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN
, p
, 8, p
);
579 table_escape_html_text__(p
, 1);
583 printf("</%s>\n", element
);
587 table_print_html__(const struct table
*table
)
591 fputs("<table border=1>\n", stdout
);
593 if (table
->caption
) {
594 table_print_html_cell__("caption", table
->caption
);
597 if (output_headings
) {
598 fputs(" <tr>\n", stdout
);
599 for (x
= 0; x
< table
->n_columns
; x
++) {
600 const struct column
*column
= &table
->columns
[x
];
601 table_print_html_cell__("th", column
->heading
);
603 fputs(" </tr>\n", stdout
);
606 for (y
= 0; y
< table
->n_rows
; y
++) {
607 fputs(" <tr>\n", stdout
);
608 for (x
= 0; x
< table
->n_columns
; x
++) {
609 const char *content
= cell_to_text(table_cell__(table
, y
, x
));
611 if (!strcmp(table
->columns
[x
].heading
, "_uuid")) {
612 fputs(" <td><a name=\"", stdout
);
613 table_escape_html_text__(content
, strlen(content
));
614 fputs("\">", stdout
);
615 table_escape_html_text__(content
, 8);
616 fputs("</a></td>\n", stdout
);
618 table_print_html_cell__("td", content
);
621 fputs(" </tr>\n", stdout
);
624 fputs("</table>\n", stdout
);
628 table_print_csv_cell__(const char *content
)
632 if (!strpbrk(content
, "\n\",")) {
633 fputs(content
, stdout
);
636 for (p
= content
; *p
!= '\0'; p
++) {
639 fputs("\"\"", stdout
);
651 table_print_csv__(const struct table
*table
)
660 if (table
->caption
) {
661 puts(table
->caption
);
664 if (output_headings
) {
665 for (x
= 0; x
< table
->n_columns
; x
++) {
666 const struct column
*column
= &table
->columns
[x
];
670 table_print_csv_cell__(column
->heading
);
675 for (y
= 0; y
< table
->n_rows
; y
++) {
676 for (x
= 0; x
< table
->n_columns
; x
++) {
680 table_print_csv_cell__(cell_to_text(table_cell__(table
, y
, x
)));
687 table_print_json__(const struct table
*table
)
689 struct json
*json
, *headings
, *data
;
693 json
= json_object_create();
694 if (table
->caption
) {
695 json_object_put_string(json
, "caption", table
->caption
);
698 headings
= json_array_create_empty();
699 for (x
= 0; x
< table
->n_columns
; x
++) {
700 const struct column
*column
= &table
->columns
[x
];
701 json_array_add(headings
, json_string_create(column
->heading
));
703 json_object_put(json
, "headings", headings
);
705 data
= json_array_create_empty();
706 for (y
= 0; y
< table
->n_rows
; y
++) {
707 struct json
*row
= json_array_create_empty();
708 for (x
= 0; x
< table
->n_columns
; x
++) {
709 const struct cell
*cell
= table_cell__(table
, y
, x
);
711 json_array_add(row
, json_string_create(cell
->text
));
713 json_array_add(row
, json_clone(cell
->json
));
716 json_array_add(data
, row
);
718 json_object_put(json
, "data", data
);
720 s
= json_to_string(json
, json_flags
);
727 table_print(const struct table
*table
)
729 switch (output_format
) {
731 table_print_table__(table
);
735 table_print_html__(table
);
739 table_print_csv__(table
);
743 table_print_json__(table
);
749 do_list_dbs(int argc OVS_UNUSED
, char *argv
[])
751 struct jsonrpc_msg
*request
, *reply
;
756 rpc
= open_jsonrpc(argv
[1]);
757 request
= jsonrpc_create_request("list_dbs", json_array_create_empty(),
759 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
761 ovs_fatal(error
, "transaction failed");
764 if (reply
->result
->type
!= JSON_ARRAY
) {
765 ovs_fatal(0, "list_dbs response is not array");
768 for (i
= 0; i
< reply
->result
->u
.array
.n
; i
++) {
769 const struct json
*name
= reply
->result
->u
.array
.elems
[i
];
771 if (name
->type
!= JSON_STRING
) {
772 ovs_fatal(0, "list_dbs response %zu is not string", i
);
774 puts(name
->u
.string
);
776 jsonrpc_msg_destroy(reply
);
780 do_get_schema(int argc OVS_UNUSED
, char *argv
[])
782 struct ovsdb_schema
*schema
= fetch_schema(argv
[1], argv
[2]);
783 print_and_free_json(ovsdb_schema_to_json(schema
));
784 ovsdb_schema_destroy(schema
);
788 do_get_schema_version(int argc OVS_UNUSED
, char *argv
[])
790 struct ovsdb_schema
*schema
= fetch_schema(argv
[1], argv
[2]);
791 puts(schema
->version
);
792 ovsdb_schema_destroy(schema
);
796 do_list_tables(int argc OVS_UNUSED
, char *argv
[])
798 struct ovsdb_schema
*schema
;
799 struct shash_node
*node
;
802 schema
= fetch_schema(argv
[1], argv
[2]);
804 table_add_column(&t
, "Table");
805 SHASH_FOR_EACH (node
, &schema
->tables
) {
806 struct ovsdb_table_schema
*ts
= node
->data
;
809 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
811 ovsdb_schema_destroy(schema
);
816 do_list_columns(int argc OVS_UNUSED
, char *argv
[])
818 const char *table_name
= argv
[3];
819 struct ovsdb_schema
*schema
;
820 struct shash_node
*table_node
;
823 schema
= fetch_schema(argv
[1], argv
[2]);
826 table_add_column(&t
, "Table");
828 table_add_column(&t
, "Column");
829 table_add_column(&t
, "Type");
830 SHASH_FOR_EACH (table_node
, &schema
->tables
) {
831 struct ovsdb_table_schema
*ts
= table_node
->data
;
833 if (!table_name
|| !strcmp(table_name
, ts
->name
)) {
834 struct shash_node
*column_node
;
836 SHASH_FOR_EACH (column_node
, &ts
->columns
) {
837 const struct ovsdb_column
*column
= column_node
->data
;
841 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
843 table_add_cell(&t
)->text
= xstrdup(column
->name
);
844 table_add_cell(&t
)->json
= ovsdb_type_to_json(&column
->type
);
848 ovsdb_schema_destroy(schema
);
853 do_transact(int argc OVS_UNUSED
, char *argv
[])
855 struct jsonrpc_msg
*request
, *reply
;
856 struct json
*transaction
;
860 transaction
= parse_json(argv
[2]);
862 rpc
= open_jsonrpc(argv
[1]);
863 request
= jsonrpc_create_request("transact", transaction
, NULL
);
864 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
866 ovs_fatal(error
, "transaction failed");
869 ovs_fatal(error
, "transaction returned error: %s",
870 json_to_string(reply
->error
, json_flags
));
872 print_json(reply
->result
);
874 jsonrpc_msg_destroy(reply
);
879 monitor_print_row(struct json
*row
, const char *type
, const char *uuid
,
880 const struct ovsdb_column_set
*columns
, struct table
*t
)
885 ovs_error(0, "missing %s row", type
);
887 } else if (row
->type
!= JSON_OBJECT
) {
888 ovs_error(0, "<row> is not object");
893 table_add_cell(t
)->text
= xstrdup(uuid
);
894 table_add_cell(t
)->text
= xstrdup(type
);
895 for (i
= 0; i
< columns
->n_columns
; i
++) {
896 const struct ovsdb_column
*column
= columns
->columns
[i
];
897 struct json
*value
= shash_find_data(json_object(row
), column
->name
);
898 struct cell
*cell
= table_add_cell(t
);
900 cell
->json
= json_clone(value
);
901 cell
->type
= &column
->type
;
907 monitor_print(struct json
*table_updates
,
908 const struct ovsdb_table_schema
*table
,
909 const struct ovsdb_column_set
*columns
, bool initial
)
911 struct json
*table_update
;
912 struct shash_node
*node
;
918 if (table_updates
->type
!= JSON_OBJECT
) {
919 ovs_error(0, "<table-updates> is not object");
922 table_update
= shash_find_data(json_object(table_updates
), table
->name
);
926 if (table_update
->type
!= JSON_OBJECT
) {
927 ovs_error(0, "<table-update> is not object");
931 table_add_column(&t
, "row");
932 table_add_column(&t
, "action");
933 for (i
= 0; i
< columns
->n_columns
; i
++) {
934 table_add_column(&t
, "%s", columns
->columns
[i
]->name
);
936 SHASH_FOR_EACH (node
, json_object(table_update
)) {
937 struct json
*row_update
= node
->data
;
938 struct json
*old
, *new;
940 if (row_update
->type
!= JSON_OBJECT
) {
941 ovs_error(0, "<row-update> is not object");
944 old
= shash_find_data(json_object(row_update
), "old");
945 new = shash_find_data(json_object(row_update
), "new");
947 monitor_print_row(new, "initial", node
->name
, columns
, &t
);
949 monitor_print_row(new, "insert", node
->name
, columns
, &t
);
951 monitor_print_row(old
, "delete", node
->name
, columns
, &t
);
953 monitor_print_row(old
, "old", node
->name
, columns
, &t
);
954 monitor_print_row(new, "new", "", columns
, &t
);
962 add_column(const char *server
, const struct ovsdb_column
*column
,
963 struct ovsdb_column_set
*columns
, struct json
*columns_json
)
965 if (ovsdb_column_set_contains(columns
, column
->index
)) {
966 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
967 server
, column
->name
);
969 ovsdb_column_set_add(columns
, column
);
970 json_array_add(columns_json
, json_string_create(column
->name
));
974 parse_monitor_columns(char *arg
, const char *server
, const char *database
,
975 const struct ovsdb_table_schema
*table
,
976 struct ovsdb_column_set
*columns
)
978 bool initial
, insert
, delete, modify
;
979 struct json
*mr
, *columns_json
;
980 char *save_ptr
= NULL
;
983 mr
= json_object_create();
984 columns_json
= json_array_create_empty();
985 json_object_put(mr
, "columns", columns_json
);
987 initial
= insert
= delete = modify
= true;
988 for (token
= strtok_r(arg
, ",", &save_ptr
); token
!= NULL
;
989 token
= strtok_r(NULL
, ",", &save_ptr
)) {
990 if (!strcmp(token
, "!initial")) {
992 } else if (!strcmp(token
, "!insert")) {
994 } else if (!strcmp(token
, "!delete")) {
996 } else if (!strcmp(token
, "!modify")) {
999 const struct ovsdb_column
*column
;
1001 column
= ovsdb_table_schema_get_column(table
, token
);
1003 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
1004 "column named \"%s\"",
1005 server
, table
->name
, database
, token
);
1007 add_column(server
, column
, columns
, columns_json
);
1011 if (columns_json
->u
.array
.n
== 0) {
1012 const struct shash_node
**nodes
;
1015 n
= shash_count(&table
->columns
);
1016 nodes
= shash_sort(&table
->columns
);
1017 for (i
= 0; i
< n
; i
++) {
1018 const struct ovsdb_column
*column
= nodes
[i
]->data
;
1019 if (column
->index
!= OVSDB_COL_UUID
1020 && column
->index
!= OVSDB_COL_VERSION
) {
1021 add_column(server
, column
, columns
, columns_json
);
1026 add_column(server
, ovsdb_table_schema_get_column(table
,"_version"),
1027 columns
, columns_json
);
1030 if (!initial
|| !insert
|| !delete || !modify
) {
1031 struct json
*select
= json_object_create();
1032 json_object_put(select
, "initial", json_boolean_create(initial
));
1033 json_object_put(select
, "insert", json_boolean_create(insert
));
1034 json_object_put(select
, "delete", json_boolean_create(delete));
1035 json_object_put(select
, "modify", json_boolean_create(modify
));
1036 json_object_put(mr
, "select", select
);
1043 do_monitor(int argc
, char *argv
[])
1045 const char *server
= argv
[1];
1046 const char *database
= argv
[2];
1047 const char *table_name
= argv
[3];
1048 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
1049 struct ovsdb_table_schema
*table
;
1050 struct ovsdb_schema
*schema
;
1051 struct jsonrpc_msg
*request
;
1052 struct jsonrpc
*rpc
;
1053 struct json
*monitor
, *monitor_request_array
,
1054 *monitor_requests
, *request_id
;
1056 rpc
= open_jsonrpc(server
);
1058 schema
= fetch_schema_from_rpc(rpc
, database
);
1059 table
= shash_find_data(&schema
->tables
, table_name
);
1061 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
1062 server
, database
, table_name
);
1065 monitor_request_array
= json_array_create_empty();
1069 for (i
= 4; i
< argc
; i
++) {
1071 monitor_request_array
,
1072 parse_monitor_columns(argv
[i
], server
, database
, table
,
1076 /* Allocate a writable empty string since parse_monitor_columns() is
1077 * going to strtok() it and that's risky with literal "". */
1080 monitor_request_array
,
1081 parse_monitor_columns(empty
, server
, database
, table
, &columns
));
1084 monitor_requests
= json_object_create();
1085 json_object_put(monitor_requests
, table_name
, monitor_request_array
);
1087 monitor
= json_array_create_3(json_string_create(database
),
1088 json_null_create(), monitor_requests
);
1089 request
= jsonrpc_create_request("monitor", monitor
, NULL
);
1090 request_id
= json_clone(request
->id
);
1091 jsonrpc_send(rpc
, request
);
1093 struct jsonrpc_msg
*msg
;
1096 error
= jsonrpc_recv_block(rpc
, &msg
);
1098 ovsdb_schema_destroy(schema
);
1099 ovs_fatal(error
, "%s: receive failed", server
);
1102 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
1103 jsonrpc_send(rpc
, jsonrpc_create_reply(json_clone(msg
->params
),
1105 } else if (msg
->type
== JSONRPC_REPLY
1106 && json_equal(msg
->id
, request_id
)) {
1107 monitor_print(msg
->result
, table
, &columns
, true);
1110 /* daemonize() closes the standard file descriptors. We output
1111 * to stdout, so we need to save and restore STDOUT_FILENO. */
1112 int fd
= dup(STDOUT_FILENO
);
1114 dup2(fd
, STDOUT_FILENO
);
1117 } else if (msg
->type
== JSONRPC_NOTIFY
1118 && !strcmp(msg
->method
, "update")) {
1119 struct json
*params
= msg
->params
;
1120 if (params
->type
== JSON_ARRAY
1121 && params
->u
.array
.n
== 2
1122 && params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
1123 monitor_print(params
->u
.array
.elems
[1],
1124 table
, &columns
, false);
1128 jsonrpc_msg_destroy(msg
);
1132 struct dump_table_aux
{
1133 struct ovsdb_datum
**data
;
1134 const struct ovsdb_column
**columns
;
1139 compare_data(size_t a_y
, size_t b_y
, size_t x
,
1140 const struct dump_table_aux
*aux
)
1142 return ovsdb_datum_compare_3way(&aux
->data
[a_y
][x
],
1144 &aux
->columns
[x
]->type
);
1148 compare_rows(size_t a_y
, size_t b_y
, void *aux_
)
1150 struct dump_table_aux
*aux
= aux_
;
1153 /* Skip UUID columns on the first pass, since their values tend to be
1154 * random and make our results less reproducible. */
1155 for (x
= 0; x
< aux
->n_columns
; x
++) {
1156 if (aux
->columns
[x
]->type
.key
.type
!= OVSDB_TYPE_UUID
) {
1157 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
1164 /* Use UUID columns as tie-breakers. */
1165 for (x
= 0; x
< aux
->n_columns
; x
++) {
1166 if (aux
->columns
[x
]->type
.key
.type
== OVSDB_TYPE_UUID
) {
1167 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
1178 swap_rows(size_t a_y
, size_t b_y
, void *aux_
)
1180 struct dump_table_aux
*aux
= aux_
;
1181 struct ovsdb_datum
*tmp
= aux
->data
[a_y
];
1182 aux
->data
[a_y
] = aux
->data
[b_y
];
1183 aux
->data
[b_y
] = tmp
;
1187 compare_columns(const void *a_
, const void *b_
)
1189 const struct ovsdb_column
*const *ap
= a_
;
1190 const struct ovsdb_column
*const *bp
= b_
;
1191 const struct ovsdb_column
*a
= *ap
;
1192 const struct ovsdb_column
*b
= *bp
;
1194 return strcmp(a
->name
, b
->name
);
1198 dump_table(const struct ovsdb_table_schema
*ts
, struct json_array
*rows
)
1200 const struct ovsdb_column
**columns
;
1203 struct ovsdb_datum
**data
;
1205 struct dump_table_aux aux
;
1206 struct shash_node
*node
;
1210 /* Sort columns by name, for reproducibility. */
1211 columns
= xmalloc(shash_count(&ts
->columns
) * sizeof *columns
);
1213 SHASH_FOR_EACH (node
, &ts
->columns
) {
1214 struct ovsdb_column
*column
= node
->data
;
1215 if (strcmp(column
->name
, "_version")) {
1216 columns
[n_columns
++] = column
;
1219 qsort(columns
, n_columns
, sizeof *columns
, compare_columns
);
1221 /* Extract data from table. */
1222 data
= xmalloc(rows
->n
* sizeof *data
);
1223 for (y
= 0; y
< rows
->n
; y
++) {
1226 if (rows
->elems
[y
]->type
!= JSON_OBJECT
) {
1227 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
1228 "%s", y
, ts
->name
, json_to_string(rows
->elems
[y
], 0));
1230 row
= json_object(rows
->elems
[y
]);
1232 data
[y
] = xmalloc(n_columns
* sizeof **data
);
1233 for (x
= 0; x
< n_columns
; x
++) {
1234 const struct json
*json
= shash_find_data(row
, columns
[x
]->name
);
1236 ovs_fatal(0, "row %zu in table %s response lacks %s column",
1237 y
, ts
->name
, columns
[x
]->name
);
1240 check_ovsdb_error(ovsdb_datum_from_json(&data
[y
][x
],
1246 /* Sort rows by column values, for reproducibility. */
1248 aux
.columns
= columns
;
1249 aux
.n_columns
= n_columns
;
1250 sort(rows
->n
, compare_rows
, swap_rows
, &aux
);
1252 /* Add column headings. */
1254 table_set_caption(&t
, xasprintf("%s table", ts
->name
));
1255 for (x
= 0; x
< n_columns
; x
++) {
1256 table_add_column(&t
, "%s", columns
[x
]->name
);
1260 for (y
= 0; y
< rows
->n
; y
++) {
1262 for (x
= 0; x
< n_columns
; x
++) {
1263 struct cell
*cell
= table_add_cell(&t
);
1264 cell
->json
= ovsdb_datum_to_json(&data
[y
][x
], &columns
[x
]->type
);
1265 cell
->type
= &columns
[x
]->type
;
1273 do_dump(int argc OVS_UNUSED
, char *argv
[])
1275 const char *server
= argv
[1];
1276 const char *database
= argv
[2];
1278 struct jsonrpc_msg
*request
, *reply
;
1279 struct ovsdb_schema
*schema
;
1280 struct json
*transaction
;
1281 struct jsonrpc
*rpc
;
1284 const struct shash_node
**tables
;
1289 rpc
= open_jsonrpc(server
);
1291 schema
= fetch_schema_from_rpc(rpc
, database
);
1292 tables
= shash_sort(&schema
->tables
);
1293 n_tables
= shash_count(&schema
->tables
);
1295 /* Construct transaction to retrieve entire database. */
1296 transaction
= json_array_create_1(json_string_create(database
));
1297 for (i
= 0; i
< n_tables
; i
++) {
1298 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
1299 struct json
*op
, *columns
;
1300 struct shash_node
*node
;
1302 columns
= json_array_create_empty();
1303 SHASH_FOR_EACH (node
, &ts
->columns
) {
1304 const struct ovsdb_column
*column
= node
->data
;
1306 if (strcmp(column
->name
, "_version")) {
1307 json_array_add(columns
, json_string_create(column
->name
));
1311 op
= json_object_create();
1312 json_object_put_string(op
, "op", "select");
1313 json_object_put_string(op
, "table", tables
[i
]->name
);
1314 json_object_put(op
, "where", json_array_create_empty());
1315 json_object_put(op
, "columns", columns
);
1316 json_array_add(transaction
, op
);
1319 /* Send request, get reply. */
1320 request
= jsonrpc_create_request("transact", transaction
, NULL
);
1321 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
1323 ovs_fatal(error
, "transaction failed");
1326 /* Print database contents. */
1327 if (reply
->result
->type
!= JSON_ARRAY
1328 || reply
->result
->u
.array
.n
!= n_tables
) {
1329 ovs_fatal(0, "reply is not array of %zu elements: %s",
1330 n_tables
, json_to_string(reply
->result
, 0));
1332 for (i
= 0; i
< n_tables
; i
++) {
1333 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
1334 const struct json
*op_result
= reply
->result
->u
.array
.elems
[i
];
1337 if (op_result
->type
!= JSON_OBJECT
1338 || !(rows
= shash_find_data(json_object(op_result
), "rows"))
1339 || rows
->type
!= JSON_ARRAY
) {
1340 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1342 ts
->name
, json_to_string(op_result
, 0));
1345 dump_table(ts
, &rows
->u
.array
);
1350 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1355 static const struct command all_commands
[] = {
1356 { "list-dbs", 1, 1, do_list_dbs
},
1357 { "get-schema", 2, 2, do_get_schema
},
1358 { "get-schema-version", 2, 2, do_get_schema_version
},
1359 { "list-tables", 2, 2, do_list_tables
},
1360 { "list-columns", 2, 3, do_list_columns
},
1361 { "transact", 2, 2, do_transact
},
1362 { "monitor", 3, INT_MAX
, do_monitor
},
1363 { "dump", 2, 2, do_dump
},
1364 { "help", 0, INT_MAX
, do_help
},
1365 { NULL
, 0, 0, NULL
},