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-error.h"
38 #include "stream-ssl.h"
44 #define THIS_MODULE VLM_ovsdb_client
46 /* --format: Output formatting. */
48 FMT_TABLE
, /* Textual table. */
49 FMT_HTML
, /* HTML table. */
50 FMT_CSV
/* Comma-separated lines. */
53 /* --wide: For --format=table, the maximum output width. */
54 static int output_width
;
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 static const struct command all_commands
[];
64 static void usage(void) NO_RETURN
;
65 static void parse_options(int argc
, char *argv
[]);
68 main(int argc
, char *argv
[])
70 proctitle_init(argc
, argv
);
71 set_program_name(argv
[0]);
74 parse_options(argc
, argv
);
75 signal(SIGPIPE
, SIG_IGN
);
76 run_command(argc
- optind
, argv
+ optind
, all_commands
);
81 parse_options(int argc
, char *argv
[])
84 OPT_BOOTSTRAP_CA_CERT
= UCHAR_MAX
+ 1
86 static struct option long_options
[] = {
87 {"wide", no_argument
, &output_width
, INT_MAX
},
88 {"format", required_argument
, 0, 'f'},
89 {"no-headings", no_argument
, &output_headings
, 0},
90 {"pretty", no_argument
, &json_flags
, JSSF_PRETTY
| JSSF_SORT
},
91 {"verbose", optional_argument
, 0, 'v'},
92 {"help", no_argument
, 0, 'h'},
93 {"version", no_argument
, 0, 'V'},
96 {"bootstrap-ca-cert", required_argument
, 0, OPT_BOOTSTRAP_CA_CERT
},
97 STREAM_SSL_LONG_OPTIONS
101 char *short_options
= long_options_to_short_options(long_options
);
103 output_width
= isatty(fileno(stdout
)) ? 79 : INT_MAX
;
107 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
114 if (!strcmp(optarg
, "table")) {
115 output_format
= FMT_TABLE
;
116 } else if (!strcmp(optarg
, "html")) {
117 output_format
= FMT_HTML
;
118 } else if (!strcmp(optarg
, "csv")) {
119 output_format
= FMT_CSV
;
121 ovs_fatal(0, "unknown output format \"%s\"", optarg
);
126 output_width
= INT_MAX
;
133 OVS_PRINT_VERSION(0, 0);
137 vlog_set_verbosity(optarg
);
140 DAEMON_OPTION_HANDLERS
143 STREAM_SSL_OPTION_HANDLERS
145 case OPT_BOOTSTRAP_CA_CERT
:
146 stream_ssl_set_ca_cert_file(optarg
, true);
154 /* getopt_long() already set the value for us. */
167 printf("%s: Open vSwitch database JSON-RPC client\n"
168 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
169 "\nValid commands are:\n"
170 "\n list-dbs SERVER\n"
171 " list databases available on SERVER\n"
172 "\n get-schema SERVER DATABASE\n"
173 " retrieve schema for DATABASE from SERVER\n"
174 "\n list-tables SERVER DATABSE\n"
175 " list tables for DATABSAE on SERVER\n"
176 "\n list-columns SERVER DATABASE [TABLE]\n"
177 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
178 "\n transact SERVER TRANSACTION\n"
179 " run TRANSACTION (a JSON array of operations) on SERVER\n"
180 " and print the results as JSON on stdout\n"
181 "\n monitor SERVER DATABASE TABLE [COLUMN,...] [SELECT,...]\n"
182 " monitor contents of (COLUMNs in) TABLE in DATABASE on SERVER\n"
183 " Valid SELECTs are: initial, insert, delete, modify\n",
184 program_name
, program_name
);
185 stream_usage("SERVER", true, true, true);
186 printf("\nOutput formatting options:\n"
187 " -f, --format=FORMAT set output formatting to FORMAT\n"
188 " (\"table\", \"html\", or \"csv\"\n"
189 " --wide don't limit TTY lines to 79 bytes\n"
190 " --no-headings omit table heading row\n"
191 " --pretty pretty-print JSON in output");
194 printf("\nOther options:\n"
195 " -h, --help display this help message\n"
196 " -V, --version display version information\n");
201 parse_json(const char *s
)
203 struct json
*json
= json_from_string(s
);
204 if (json
->type
== JSON_STRING
) {
205 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
210 static struct jsonrpc
*
211 open_jsonrpc(const char *server
)
213 struct stream
*stream
;
216 error
= stream_open_block(server
, &stream
);
217 if (error
== EAFNOSUPPORT
) {
218 struct pstream
*pstream
;
220 error
= pstream_open(server
, &pstream
);
222 ovs_fatal(error
, "failed to connect or listen to \"%s\"", server
);
225 VLOG_INFO("%s: waiting for connection...", server
);
226 error
= pstream_accept_block(pstream
, &stream
);
228 ovs_fatal(error
, "failed to accept connection on \"%s\"", server
);
231 pstream_close(pstream
);
233 ovs_fatal(error
, "failed to connect to \"%s\"", server
);
236 return jsonrpc_open(stream
);
240 print_json(struct json
*json
)
242 char *string
= json_to_string(json
, json_flags
);
243 fputs(string
, stdout
);
248 print_and_free_json(struct json
*json
)
255 check_ovsdb_error(struct ovsdb_error
*error
)
258 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
262 static struct ovsdb_schema
*
263 fetch_schema_from_rpc(struct jsonrpc
*rpc
, const char *database
)
265 struct jsonrpc_msg
*request
, *reply
;
266 struct ovsdb_schema
*schema
;
269 request
= jsonrpc_create_request("get_schema",
271 json_string_create(database
)),
273 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
275 ovs_fatal(error
, "transaction failed");
277 check_ovsdb_error(ovsdb_schema_from_json(reply
->result
, &schema
));
278 jsonrpc_msg_destroy(reply
);
283 static struct ovsdb_schema
*
284 fetch_schema(const char *server
, const char *database
)
286 struct ovsdb_schema
*schema
;
289 rpc
= open_jsonrpc(server
);
290 schema
= fetch_schema_from_rpc(rpc
, database
);
303 struct column
*columns
;
304 size_t n_columns
, allocated_columns
;
305 size_t n_rows
, allocated_rows
;
306 size_t current_column
;
310 table_init(struct table
*table
)
312 memset(table
, 0, sizeof *table
);
316 table_destroy(struct table
*table
)
320 for (i
= 0; i
< table
->n_columns
; i
++) {
321 free(table
->columns
[i
].heading
);
323 free(table
->columns
);
325 for (i
= 0; i
< table
->n_columns
* table
->n_rows
; i
++) {
326 free(table
->cells
[i
]);
332 table_add_column(struct table
*table
, const char *heading
, ...)
336 table_add_column(struct table
*table
, const char *heading
, ...)
338 struct column
*column
;
341 assert(!table
->n_rows
);
342 if (table
->n_columns
>= table
->allocated_columns
) {
343 table
->columns
= x2nrealloc(table
->columns
, &table
->allocated_columns
,
344 sizeof *table
->columns
);
346 column
= &table
->columns
[table
->n_columns
++];
348 va_start(args
, heading
);
349 column
->heading
= xvasprintf(heading
, args
);
350 column
->width
= strlen(column
->heading
);
355 table_cell__(const struct table
*table
, size_t row
, size_t column
)
357 return &table
->cells
[column
+ row
* table
->n_columns
];
361 table_add_row(struct table
*table
)
365 if (table
->n_rows
>= table
->allocated_rows
) {
366 table
->cells
= x2nrealloc(table
->cells
, &table
->allocated_rows
,
367 table
->n_columns
* sizeof *table
->cells
);
371 table
->current_column
= 0;
372 for (x
= 0; x
< table
->n_columns
; x
++) {
373 *table_cell__(table
, y
, x
) = NULL
;
378 table_add_cell_nocopy(struct table
*table
, char *s
)
383 assert(table
->n_rows
> 0);
384 assert(table
->current_column
< table
->n_columns
);
386 x
= table
->current_column
++;
387 y
= table
->n_rows
- 1;
388 *table_cell__(table
, y
, x
) = s
;
391 if (length
> table
->columns
[x
].width
) {
392 table
->columns
[x
].width
= length
;
397 table_add_cell(struct table
*table
, const char *format
, ...)
401 va_start(args
, format
);
402 table_add_cell_nocopy(table
, xvasprintf(format
, args
));
407 table_print_table_line__(struct ds
*line
, size_t max_width
)
409 ds_truncate(line
, max_width
);
415 table_print_table__(const struct table
*table
)
417 struct ds line
= DS_EMPTY_INITIALIZER
;
420 if (output_headings
) {
421 for (x
= 0; x
< table
->n_columns
; x
++) {
422 const struct column
*column
= &table
->columns
[x
];
424 ds_put_char(&line
, ' ');
426 ds_put_format(&line
, "%-*s", column
->width
, column
->heading
);
428 table_print_table_line__(&line
, output_width
);
430 for (x
= 0; x
< table
->n_columns
; x
++) {
431 const struct column
*column
= &table
->columns
[x
];
435 ds_put_char(&line
, ' ');
437 for (i
= 0; i
< column
->width
; i
++) {
438 ds_put_char(&line
, '-');
441 table_print_table_line__(&line
, output_width
);
444 for (y
= 0; y
< table
->n_rows
; y
++) {
445 for (x
= 0; x
< table
->n_columns
; x
++) {
446 const char *cell
= *table_cell__(table
, y
, x
);
448 ds_put_char(&line
, ' ');
450 ds_put_format(&line
, "%-*s", table
->columns
[x
].width
, cell
);
452 table_print_table_line__(&line
, output_width
);
459 table_print_html_cell__(const char *element
, const char *content
)
463 printf(" <%s>", element
);
464 for (p
= content
; *p
!= '\0'; p
++) {
467 fputs("&", stdout
);
470 fputs("<", stdout
);
473 fputs(">", stdout
);
480 printf("</%s>\n", element
);
484 table_print_html__(const struct table
*table
)
488 fputs("<table>\n", stdout
);
490 if (output_headings
) {
491 fputs(" <tr>\n", stdout
);
492 for (x
= 0; x
< table
->n_columns
; x
++) {
493 const struct column
*column
= &table
->columns
[x
];
494 table_print_html_cell__("th", column
->heading
);
496 fputs(" </tr>\n", stdout
);
499 for (y
= 0; y
< table
->n_rows
; y
++) {
500 fputs(" <tr>\n", stdout
);
501 for (x
= 0; x
< table
->n_columns
; x
++) {
502 table_print_html_cell__("td", *table_cell__(table
, y
, x
));
504 fputs(" </tr>\n", stdout
);
507 fputs("</table>\n", stdout
);
511 table_print_csv_cell__(const char *content
)
515 if (!strpbrk(content
, "\n\",")) {
516 fputs(content
, stdout
);
519 for (p
= content
; *p
!= '\0'; p
++) {
522 fputs("\"\"", stdout
);
534 table_print_csv__(const struct table
*table
)
538 if (output_headings
) {
539 for (x
= 0; x
< table
->n_columns
; x
++) {
540 const struct column
*column
= &table
->columns
[x
];
544 table_print_csv_cell__(column
->heading
);
549 for (y
= 0; y
< table
->n_rows
; y
++) {
550 for (x
= 0; x
< table
->n_columns
; x
++) {
554 table_print_csv_cell__(*table_cell__(table
, y
, x
));
561 table_print(const struct table
*table
)
563 switch (output_format
) {
565 table_print_table__(table
);
569 table_print_html__(table
);
573 table_print_csv__(table
);
579 do_list_dbs(int argc OVS_UNUSED
, char *argv
[])
581 struct jsonrpc_msg
*request
, *reply
;
586 rpc
= open_jsonrpc(argv
[1]);
587 request
= jsonrpc_create_request("list_dbs", json_array_create_empty(),
589 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
591 ovs_fatal(error
, "transaction failed");
594 if (reply
->result
->type
!= JSON_ARRAY
) {
595 ovs_fatal(0, "list_dbs response is not array");
598 for (i
= 0; i
< reply
->result
->u
.array
.n
; i
++) {
599 const struct json
*name
= reply
->result
->u
.array
.elems
[i
];
601 if (name
->type
!= JSON_STRING
) {
602 ovs_fatal(0, "list_dbs response %zu is not string", i
);
604 puts(name
->u
.string
);
606 jsonrpc_msg_destroy(reply
);
610 do_get_schema(int argc OVS_UNUSED
, char *argv
[])
612 struct ovsdb_schema
*schema
= fetch_schema(argv
[1], argv
[2]);
613 print_and_free_json(ovsdb_schema_to_json(schema
));
614 ovsdb_schema_destroy(schema
);
618 do_list_tables(int argc OVS_UNUSED
, char *argv
[])
620 struct ovsdb_schema
*schema
;
621 struct shash_node
*node
;
624 schema
= fetch_schema(argv
[1], argv
[2]);
626 table_add_column(&t
, "Table");
627 table_add_column(&t
, "Comment");
628 SHASH_FOR_EACH (node
, &schema
->tables
) {
629 struct ovsdb_table_schema
*ts
= node
->data
;
632 table_add_cell(&t
, ts
->name
);
634 table_add_cell(&t
, ts
->comment
);
637 ovsdb_schema_destroy(schema
);
642 do_list_columns(int argc OVS_UNUSED
, char *argv
[])
644 const char *table_name
= argv
[3];
645 struct ovsdb_schema
*schema
;
646 struct shash_node
*table_node
;
649 schema
= fetch_schema(argv
[1], argv
[2]);
652 table_add_column(&t
, "Table");
654 table_add_column(&t
, "Column");
655 table_add_column(&t
, "Type");
656 table_add_column(&t
, "Comment");
657 SHASH_FOR_EACH (table_node
, &schema
->tables
) {
658 struct ovsdb_table_schema
*ts
= table_node
->data
;
660 if (!table_name
|| !strcmp(table_name
, ts
->name
)) {
661 struct shash_node
*column_node
;
663 SHASH_FOR_EACH (column_node
, &ts
->columns
) {
664 const struct ovsdb_column
*column
= column_node
->data
;
665 struct json
*type
= ovsdb_type_to_json(&column
->type
);
669 table_add_cell(&t
, ts
->name
);
671 table_add_cell(&t
, column
->name
);
672 table_add_cell_nocopy(&t
, json_to_string(type
, JSSF_SORT
));
673 if (column
->comment
) {
674 table_add_cell(&t
, column
->comment
);
681 ovsdb_schema_destroy(schema
);
686 do_transact(int argc OVS_UNUSED
, char *argv
[])
688 struct jsonrpc_msg
*request
, *reply
;
689 struct json
*transaction
;
693 transaction
= parse_json(argv
[2]);
695 rpc
= open_jsonrpc(argv
[1]);
696 request
= jsonrpc_create_request("transact", transaction
, NULL
);
697 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
699 ovs_fatal(error
, "transaction failed");
702 ovs_fatal(error
, "transaction returned error: %s",
703 json_to_string(reply
->error
, json_flags
));
705 print_json(reply
->result
);
707 jsonrpc_msg_destroy(reply
);
712 monitor_print_row(struct json
*row
, const char *type
, const char *uuid
,
713 const struct ovsdb_column_set
*columns
, struct table
*t
)
718 ovs_error(0, "missing %s row", type
);
720 } else if (row
->type
!= JSON_OBJECT
) {
721 ovs_error(0, "<row> is not object");
726 table_add_cell(t
, uuid
);
727 table_add_cell(t
, type
);
728 for (i
= 0; i
< columns
->n_columns
; i
++) {
729 const struct ovsdb_column
*column
= columns
->columns
[i
];
730 struct json
*value
= shash_find_data(json_object(row
), column
->name
);
732 table_add_cell_nocopy(t
, json_to_string(value
, JSSF_SORT
));
734 table_add_cell(t
, "");
740 monitor_print(struct json
*table_updates
,
741 const struct ovsdb_table_schema
*table
,
742 const struct ovsdb_column_set
*columns
, bool initial
)
744 struct json
*table_update
;
745 struct shash_node
*node
;
751 if (table_updates
->type
!= JSON_OBJECT
) {
752 ovs_error(0, "<table-updates> is not object");
755 table_update
= shash_find_data(json_object(table_updates
), table
->name
);
759 if (table_update
->type
!= JSON_OBJECT
) {
760 ovs_error(0, "<table-update> is not object");
764 table_add_column(&t
, "row");
765 table_add_column(&t
, "action");
766 for (i
= 0; i
< columns
->n_columns
; i
++) {
767 table_add_column(&t
, "%s", columns
->columns
[i
]->name
);
769 SHASH_FOR_EACH (node
, json_object(table_update
)) {
770 struct json
*row_update
= node
->data
;
771 struct json
*old
, *new;
773 if (row_update
->type
!= JSON_OBJECT
) {
774 ovs_error(0, "<row-update> is not object");
777 old
= shash_find_data(json_object(row_update
), "old");
778 new = shash_find_data(json_object(row_update
), "new");
780 monitor_print_row(new, "initial", node
->name
, columns
, &t
);
782 monitor_print_row(new, "insert", node
->name
, columns
, &t
);
784 monitor_print_row(old
, "delete", node
->name
, columns
, &t
);
786 monitor_print_row(old
, "old", node
->name
, columns
, &t
);
787 monitor_print_row(new, "new", "", columns
, &t
);
795 do_monitor(int argc
, char *argv
[])
797 const char *server
= argv
[1];
798 const char *database
= argv
[2];
799 const char *table_name
= argv
[3];
800 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
801 struct ovsdb_table_schema
*table
;
802 struct ovsdb_schema
*schema
;
803 struct jsonrpc_msg
*request
;
805 struct json
*select
, *monitor
, *monitor_request
, *monitor_requests
,
808 rpc
= open_jsonrpc(server
);
810 schema
= fetch_schema_from_rpc(rpc
, database
);
811 table
= shash_find_data(&schema
->tables
, table_name
);
813 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
814 server
, database
, table_name
);
817 if (argc
>= 5 && *argv
[4] != '\0') {
818 char *save_ptr
= NULL
;
821 for (token
= strtok_r(argv
[4], ",", &save_ptr
); token
!= NULL
;
822 token
= strtok_r(NULL
, ",", &save_ptr
)) {
823 const struct ovsdb_column
*column
;
824 column
= ovsdb_table_schema_get_column(table
, token
);
826 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
827 "column named \"%s\"",
828 server
, table_name
, database
, token
);
830 ovsdb_column_set_add(&columns
, column
);
833 struct shash_node
*node
;
835 SHASH_FOR_EACH (node
, &table
->columns
) {
836 const struct ovsdb_column
*column
= node
->data
;
837 if (column
->index
!= OVSDB_COL_UUID
) {
838 ovsdb_column_set_add(&columns
, column
);
843 if (argc
>= 6 && *argv
[5] != '\0') {
844 char *save_ptr
= NULL
;
847 select
= json_object_create();
848 for (token
= strtok_r(argv
[5], ",", &save_ptr
); token
!= NULL
;
849 token
= strtok_r(NULL
, ",", &save_ptr
)) {
850 json_object_put(select
, token
, json_boolean_create(true));
856 monitor_request
= json_object_create();
857 json_object_put(monitor_request
,
858 "columns", ovsdb_column_set_to_json(&columns
));
860 json_object_put(monitor_request
, "select", select
);
863 monitor_requests
= json_object_create();
864 json_object_put(monitor_requests
, table_name
, monitor_request
);
866 monitor
= json_array_create_3(json_string_create(database
),
867 json_null_create(), monitor_requests
);
868 request
= jsonrpc_create_request("monitor", monitor
, NULL
);
869 request_id
= json_clone(request
->id
);
870 jsonrpc_send(rpc
, request
);
872 struct jsonrpc_msg
*msg
;
875 error
= jsonrpc_recv_block(rpc
, &msg
);
877 ovsdb_schema_destroy(schema
);
878 ovs_fatal(error
, "%s: receive failed", server
);
881 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
882 jsonrpc_send(rpc
, jsonrpc_create_reply(json_clone(msg
->params
),
884 } else if (msg
->type
== JSONRPC_REPLY
885 && json_equal(msg
->id
, request_id
)) {
886 monitor_print(msg
->result
, table
, &columns
, true);
889 /* daemonize() closes the standard file descriptors. We output
890 * to stdout, so we need to save and restore STDOUT_FILENO. */
891 int fd
= dup(STDOUT_FILENO
);
893 dup2(fd
, STDOUT_FILENO
);
896 } else if (msg
->type
== JSONRPC_NOTIFY
897 && !strcmp(msg
->method
, "update")) {
898 struct json
*params
= msg
->params
;
899 if (params
->type
== JSON_ARRAY
900 && params
->u
.array
.n
== 2
901 && params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
902 monitor_print(params
->u
.array
.elems
[1],
903 table
, &columns
, false);
907 jsonrpc_msg_destroy(msg
);
912 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
917 static const struct command all_commands
[] = {
918 { "list-dbs", 1, 1, do_list_dbs
},
919 { "get-schema", 2, 2, do_get_schema
},
920 { "list-tables", 2, 2, do_list_tables
},
921 { "list-columns", 2, 3, do_list_columns
},
922 { "transact", 2, 2, do_transact
},
923 { "monitor", 3, 5, do_monitor
},
924 { "help", 0, INT_MAX
, do_help
},
925 { NULL
, 0, 0, NULL
},