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 get-schema SERVER\n"
171 " retrieve schema from SERVER\n"
172 "\n list-tables SERVER\n"
173 " list SERVER's tables\n"
174 "\n list-columns SERVER [TABLE]\n"
175 " list columns in TABLE (or all tables) on SERVER\n"
176 "\n transact SERVER TRANSACTION\n"
177 " run TRANSACTION (a JSON array of operations) on SERVER\n"
178 " and print the results as JSON on stdout\n"
179 "\n monitor SERVER TABLE [COLUMN,...] [SELECT,...]\n"
180 " monitor contents of (COLUMNs in) TABLE on SERVER\n"
181 " Valid SELECTs are: initial, insert, delete, modify\n",
182 program_name
, program_name
);
183 stream_usage("SERVER", true, true, true);
184 printf("\nOutput formatting options:\n"
185 " -f, --format=FORMAT set output formatting to FORMAT\n"
186 " (\"table\", \"html\", or \"csv\"\n"
187 " --wide don't limit TTY lines to 79 bytes\n"
188 " --no-headings omit table heading row\n"
189 " --pretty pretty-print JSON in output");
192 printf("\nOther options:\n"
193 " -h, --help display this help message\n"
194 " -V, --version display version information\n");
199 parse_json(const char *s
)
201 struct json
*json
= json_from_string(s
);
202 if (json
->type
== JSON_STRING
) {
203 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
208 static struct jsonrpc
*
209 open_jsonrpc(const char *server
)
211 struct stream
*stream
;
214 error
= stream_open_block(server
, &stream
);
215 if (error
== EAFNOSUPPORT
) {
216 struct pstream
*pstream
;
218 error
= pstream_open(server
, &pstream
);
220 ovs_fatal(error
, "failed to connect or listen to \"%s\"", server
);
223 VLOG_INFO("%s: waiting for connection...", server
);
224 error
= pstream_accept_block(pstream
, &stream
);
226 ovs_fatal(error
, "failed to accept connection on \"%s\"", server
);
229 pstream_close(pstream
);
231 ovs_fatal(error
, "failed to connect to \"%s\"", server
);
234 return jsonrpc_open(stream
);
238 print_json(struct json
*json
)
240 char *string
= json_to_string(json
, json_flags
);
241 fputs(string
, stdout
);
246 print_and_free_json(struct json
*json
)
253 check_ovsdb_error(struct ovsdb_error
*error
)
256 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
260 static struct ovsdb_schema
*
261 fetch_schema_from_rpc(struct jsonrpc
*rpc
)
263 struct jsonrpc_msg
*request
, *reply
;
264 struct ovsdb_schema
*schema
;
267 request
= jsonrpc_create_request("get_schema", json_array_create_empty(),
269 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
271 ovs_fatal(error
, "transaction failed");
273 check_ovsdb_error(ovsdb_schema_from_json(reply
->result
, &schema
));
274 jsonrpc_msg_destroy(reply
);
279 static struct ovsdb_schema
*
280 fetch_schema(const char *server
)
282 struct ovsdb_schema
*schema
;
285 rpc
= open_jsonrpc(server
);
286 schema
= fetch_schema_from_rpc(rpc
);
299 struct column
*columns
;
300 size_t n_columns
, allocated_columns
;
301 size_t n_rows
, allocated_rows
;
302 size_t current_column
;
306 table_init(struct table
*table
)
308 memset(table
, 0, sizeof *table
);
312 table_destroy(struct table
*table
)
316 for (i
= 0; i
< table
->n_columns
; i
++) {
317 free(table
->columns
[i
].heading
);
319 free(table
->columns
);
321 for (i
= 0; i
< table
->n_columns
* table
->n_rows
; i
++) {
322 free(table
->cells
[i
]);
328 table_add_column(struct table
*table
, const char *heading
, ...)
332 table_add_column(struct table
*table
, const char *heading
, ...)
334 struct column
*column
;
337 assert(!table
->n_rows
);
338 if (table
->n_columns
>= table
->allocated_columns
) {
339 table
->columns
= x2nrealloc(table
->columns
, &table
->allocated_columns
,
340 sizeof *table
->columns
);
342 column
= &table
->columns
[table
->n_columns
++];
344 va_start(args
, heading
);
345 column
->heading
= xvasprintf(heading
, args
);
346 column
->width
= strlen(column
->heading
);
351 table_cell__(const struct table
*table
, size_t row
, size_t column
)
353 return &table
->cells
[column
+ row
* table
->n_columns
];
357 table_add_row(struct table
*table
)
361 if (table
->n_rows
>= table
->allocated_rows
) {
362 table
->cells
= x2nrealloc(table
->cells
, &table
->allocated_rows
,
363 table
->n_columns
* sizeof *table
->cells
);
367 table
->current_column
= 0;
368 for (x
= 0; x
< table
->n_columns
; x
++) {
369 *table_cell__(table
, y
, x
) = NULL
;
374 table_add_cell_nocopy(struct table
*table
, char *s
)
379 assert(table
->n_rows
> 0);
380 assert(table
->current_column
< table
->n_columns
);
382 x
= table
->current_column
++;
383 y
= table
->n_rows
- 1;
384 *table_cell__(table
, y
, x
) = s
;
387 if (length
> table
->columns
[x
].width
) {
388 table
->columns
[x
].width
= length
;
393 table_add_cell(struct table
*table
, const char *format
, ...)
397 va_start(args
, format
);
398 table_add_cell_nocopy(table
, xvasprintf(format
, args
));
403 table_print_table_line__(struct ds
*line
, size_t max_width
)
405 ds_truncate(line
, max_width
);
411 table_print_table__(const struct table
*table
)
413 struct ds line
= DS_EMPTY_INITIALIZER
;
416 if (output_headings
) {
417 for (x
= 0; x
< table
->n_columns
; x
++) {
418 const struct column
*column
= &table
->columns
[x
];
420 ds_put_char(&line
, ' ');
422 ds_put_format(&line
, "%-*s", column
->width
, column
->heading
);
424 table_print_table_line__(&line
, output_width
);
426 for (x
= 0; x
< table
->n_columns
; x
++) {
427 const struct column
*column
= &table
->columns
[x
];
431 ds_put_char(&line
, ' ');
433 for (i
= 0; i
< column
->width
; i
++) {
434 ds_put_char(&line
, '-');
437 table_print_table_line__(&line
, output_width
);
440 for (y
= 0; y
< table
->n_rows
; y
++) {
441 for (x
= 0; x
< table
->n_columns
; x
++) {
442 const char *cell
= *table_cell__(table
, y
, x
);
444 ds_put_char(&line
, ' ');
446 ds_put_format(&line
, "%-*s", table
->columns
[x
].width
, cell
);
448 table_print_table_line__(&line
, output_width
);
455 table_print_html_cell__(const char *element
, const char *content
)
459 printf(" <%s>", element
);
460 for (p
= content
; *p
!= '\0'; p
++) {
463 fputs("&", stdout
);
466 fputs("<", stdout
);
469 fputs(">", stdout
);
476 printf("</%s>\n", element
);
480 table_print_html__(const struct table
*table
)
484 fputs("<table>\n", stdout
);
486 if (output_headings
) {
487 fputs(" <tr>\n", stdout
);
488 for (x
= 0; x
< table
->n_columns
; x
++) {
489 const struct column
*column
= &table
->columns
[x
];
490 table_print_html_cell__("th", column
->heading
);
492 fputs(" </tr>\n", stdout
);
495 for (y
= 0; y
< table
->n_rows
; y
++) {
496 fputs(" <tr>\n", stdout
);
497 for (x
= 0; x
< table
->n_columns
; x
++) {
498 table_print_html_cell__("td", *table_cell__(table
, y
, x
));
500 fputs(" </tr>\n", stdout
);
503 fputs("</table>\n", stdout
);
507 table_print_csv_cell__(const char *content
)
511 if (!strpbrk(content
, "\n\",")) {
512 fputs(content
, stdout
);
515 for (p
= content
; *p
!= '\0'; p
++) {
518 fputs("\"\"", stdout
);
530 table_print_csv__(const struct table
*table
)
534 if (output_headings
) {
535 for (x
= 0; x
< table
->n_columns
; x
++) {
536 const struct column
*column
= &table
->columns
[x
];
540 table_print_csv_cell__(column
->heading
);
545 for (y
= 0; y
< table
->n_rows
; y
++) {
546 for (x
= 0; x
< table
->n_columns
; x
++) {
550 table_print_csv_cell__(*table_cell__(table
, y
, x
));
557 table_print(const struct table
*table
)
559 switch (output_format
) {
561 table_print_table__(table
);
565 table_print_html__(table
);
569 table_print_csv__(table
);
575 do_get_schema(int argc UNUSED
, char *argv
[])
577 struct ovsdb_schema
*schema
= fetch_schema(argv
[1]);
578 print_and_free_json(ovsdb_schema_to_json(schema
));
579 ovsdb_schema_destroy(schema
);
583 do_list_tables(int argc UNUSED
, char *argv
[])
585 struct ovsdb_schema
*schema
;
586 struct shash_node
*node
;
589 schema
= fetch_schema(argv
[1]);
591 table_add_column(&t
, "Table");
592 table_add_column(&t
, "Comment");
593 SHASH_FOR_EACH (node
, &schema
->tables
) {
594 struct ovsdb_table_schema
*ts
= node
->data
;
597 table_add_cell(&t
, ts
->name
);
599 table_add_cell(&t
, ts
->comment
);
602 ovsdb_schema_destroy(schema
);
607 do_list_columns(int argc UNUSED
, char *argv
[])
609 const char *table_name
= argv
[2];
610 struct ovsdb_schema
*schema
;
611 struct shash_node
*table_node
;
614 schema
= fetch_schema(argv
[1]);
617 table_add_column(&t
, "Table");
619 table_add_column(&t
, "Column");
620 table_add_column(&t
, "Type");
621 table_add_column(&t
, "Comment");
622 SHASH_FOR_EACH (table_node
, &schema
->tables
) {
623 struct ovsdb_table_schema
*ts
= table_node
->data
;
625 if (!table_name
|| !strcmp(table_name
, ts
->name
)) {
626 struct shash_node
*column_node
;
628 SHASH_FOR_EACH (column_node
, &ts
->columns
) {
629 struct ovsdb_column
*column
= column_node
->data
;
630 struct json
*type
= ovsdb_type_to_json(&column
->type
);
634 table_add_cell(&t
, ts
->name
);
636 table_add_cell(&t
, column
->name
);
637 table_add_cell_nocopy(&t
, json_to_string(type
, JSSF_SORT
));
638 if (column
->comment
) {
639 table_add_cell(&t
, column
->comment
);
646 ovsdb_schema_destroy(schema
);
651 do_transact(int argc UNUSED
, char *argv
[])
653 struct jsonrpc_msg
*request
, *reply
;
654 struct json
*transaction
;
658 transaction
= parse_json(argv
[2]);
660 rpc
= open_jsonrpc(argv
[1]);
661 request
= jsonrpc_create_request("transact", transaction
, NULL
);
662 error
= jsonrpc_transact_block(rpc
, request
, &reply
);
664 ovs_fatal(error
, "transaction failed");
667 ovs_fatal(error
, "transaction returned error: %s",
668 json_to_string(reply
->error
, json_flags
));
670 print_json(reply
->result
);
672 jsonrpc_msg_destroy(reply
);
677 monitor_print_row(struct json
*row
, const char *type
, const char *uuid
,
678 const struct ovsdb_column_set
*columns
, struct table
*t
)
683 ovs_error(0, "missing %s row", type
);
685 } else if (row
->type
!= JSON_OBJECT
) {
686 ovs_error(0, "<row> is not object");
691 table_add_cell(t
, uuid
);
692 table_add_cell(t
, type
);
693 for (i
= 0; i
< columns
->n_columns
; i
++) {
694 const struct ovsdb_column
*column
= columns
->columns
[i
];
695 struct json
*value
= shash_find_data(json_object(row
), column
->name
);
697 table_add_cell_nocopy(t
, json_to_string(value
, JSSF_SORT
));
699 table_add_cell(t
, "");
705 monitor_print(struct json
*table_updates
,
706 const struct ovsdb_table_schema
*table
,
707 const struct ovsdb_column_set
*columns
, bool initial
)
709 struct json
*table_update
;
710 struct shash_node
*node
;
716 if (table_updates
->type
!= JSON_OBJECT
) {
717 ovs_error(0, "<table-updates> is not object");
720 table_update
= shash_find_data(json_object(table_updates
), table
->name
);
724 if (table_update
->type
!= JSON_OBJECT
) {
725 ovs_error(0, "<table-update> is not object");
729 table_add_column(&t
, "row");
730 table_add_column(&t
, "action");
731 for (i
= 0; i
< columns
->n_columns
; i
++) {
732 table_add_column(&t
, "%s", columns
->columns
[i
]->name
);
734 SHASH_FOR_EACH (node
, json_object(table_update
)) {
735 struct json
*row_update
= node
->data
;
736 struct json
*old
, *new;
738 if (row_update
->type
!= JSON_OBJECT
) {
739 ovs_error(0, "<row-update> is not object");
742 old
= shash_find_data(json_object(row_update
), "old");
743 new = shash_find_data(json_object(row_update
), "new");
745 monitor_print_row(new, "initial", node
->name
, columns
, &t
);
747 monitor_print_row(new, "insert", node
->name
, columns
, &t
);
749 monitor_print_row(old
, "delete", node
->name
, columns
, &t
);
751 monitor_print_row(old
, "old", node
->name
, columns
, &t
);
752 monitor_print_row(new, "new", "", columns
, &t
);
760 do_monitor(int argc
, char *argv
[])
762 struct ovsdb_column_set columns
= OVSDB_COLUMN_SET_INITIALIZER
;
763 struct ovsdb_table_schema
*table
;
764 struct ovsdb_schema
*schema
;
765 struct jsonrpc_msg
*request
;
767 struct json
*select
, *monitor
, *monitor_request
, *monitor_requests
,
770 rpc
= open_jsonrpc(argv
[1]);
772 schema
= fetch_schema_from_rpc(rpc
);
773 table
= shash_find_data(&schema
->tables
, argv
[2]);
775 ovs_fatal(0, "%s: no table named \"%s\"", argv
[1], argv
[2]);
778 if (argc
>= 4 && *argv
[3] != '\0') {
779 char *save_ptr
= NULL
;
782 for (token
= strtok_r(argv
[3], ",", &save_ptr
); token
!= NULL
;
783 token
= strtok_r(NULL
, ",", &save_ptr
)) {
784 const struct ovsdb_column
*column
;
785 column
= ovsdb_table_schema_get_column(table
, token
);
787 ovs_fatal(0, "%s: table \"%s\" does not have a "
788 "column named \"%s\"", argv
[1], argv
[2], token
);
790 ovsdb_column_set_add(&columns
, column
);
793 struct shash_node
*node
;
795 SHASH_FOR_EACH (node
, &table
->columns
) {
796 const struct ovsdb_column
*column
= node
->data
;
797 if (column
->index
!= OVSDB_COL_UUID
) {
798 ovsdb_column_set_add(&columns
, column
);
803 if (argc
>= 5 && *argv
[4] != '\0') {
804 char *save_ptr
= NULL
;
807 select
= json_object_create();
808 for (token
= strtok_r(argv
[4], ",", &save_ptr
); token
!= NULL
;
809 token
= strtok_r(NULL
, ",", &save_ptr
)) {
810 json_object_put(select
, token
, json_boolean_create(true));
816 monitor_request
= json_object_create();
817 json_object_put(monitor_request
,
818 "columns", ovsdb_column_set_to_json(&columns
));
820 json_object_put(monitor_request
, "select", select
);
823 monitor_requests
= json_object_create();
824 json_object_put(monitor_requests
, argv
[2], monitor_request
);
826 monitor
= json_array_create_2(json_null_create(), monitor_requests
);
827 request
= jsonrpc_create_request("monitor", monitor
, NULL
);
828 request_id
= json_clone(request
->id
);
829 jsonrpc_send(rpc
, request
);
831 struct jsonrpc_msg
*msg
;
834 error
= jsonrpc_recv_block(rpc
, &msg
);
836 ovsdb_schema_destroy(schema
);
837 ovs_fatal(error
, "%s: receive failed", argv
[1]);
840 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
841 jsonrpc_send(rpc
, jsonrpc_create_reply(json_clone(msg
->params
),
843 } else if (msg
->type
== JSONRPC_REPLY
844 && json_equal(msg
->id
, request_id
)) {
845 monitor_print(msg
->result
, table
, &columns
, true);
848 /* daemonize() closes the standard file descriptors. We output
849 * to stdout, so we need to save and restore STDOUT_FILENO. */
850 int fd
= dup(STDOUT_FILENO
);
852 dup2(fd
, STDOUT_FILENO
);
855 } else if (msg
->type
== JSONRPC_NOTIFY
856 && !strcmp(msg
->method
, "update")) {
857 struct json
*params
= msg
->params
;
858 if (params
->type
== JSON_ARRAY
859 && params
->u
.array
.n
== 2
860 && params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
861 monitor_print(params
->u
.array
.elems
[1],
862 table
, &columns
, false);
866 jsonrpc_msg_destroy(msg
);
871 do_help(int argc UNUSED
, char *argv
[] UNUSED
)
876 static const struct command all_commands
[] = {
877 { "get-schema", 1, 1, do_get_schema
},
878 { "list-tables", 1, 1, do_list_tables
},
879 { "list-columns", 1, 2, do_list_columns
},
880 { "transact", 2, 2, do_transact
},
881 { "monitor", 2, 4, do_monitor
},
882 { "help", 0, INT_MAX
, do_help
},
883 { NULL
, 0, 0, NULL
},