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 set_program_name(argv[0]);
73 parse_options(argc, argv);
74 signal(SIGPIPE, SIG_IGN);
75 run_command(argc - optind, argv + optind, all_commands);
80 parse_options(int argc, char *argv[])
83 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
85 static struct option long_options[] = {
86 {"wide", no_argument, &output_width, INT_MAX},
87 {"format", required_argument, 0, 'f'},
88 {"no-headings", no_argument, &output_headings, 0},
89 {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
90 {"verbose", optional_argument, 0, 'v'},
91 {"help", no_argument, 0, 'h'},
92 {"version", no_argument, 0, 'V'},
95 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
96 STREAM_SSL_LONG_OPTIONS
100 char *short_options = long_options_to_short_options(long_options);
102 output_width = isatty(fileno(stdout)) ? 79 : INT_MAX;
106 c = getopt_long(argc, argv, short_options, long_options, NULL);
113 if (!strcmp(optarg, "table")) {
114 output_format = FMT_TABLE;
115 } else if (!strcmp(optarg, "html")) {
116 output_format = FMT_HTML;
117 } else if (!strcmp(optarg, "csv")) {
118 output_format = FMT_CSV;
120 ovs_fatal(0, "unknown output format \"%s\"", optarg);
125 output_width = INT_MAX;
132 OVS_PRINT_VERSION(0, 0);
136 vlog_set_verbosity(optarg);
139 DAEMON_OPTION_HANDLERS
142 STREAM_SSL_OPTION_HANDLERS
144 case OPT_BOOTSTRAP_CA_CERT:
145 stream_ssl_set_ca_cert_file(optarg, true);
153 /* getopt_long() already set the value for us. */
166 printf("%s: Open vSwitch database JSON-RPC client\n"
167 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
168 "\nValid commands are:\n"
169 "\n get-schema SERVER\n"
170 " retrieve schema from SERVER\n"
171 "\n list-tables SERVER\n"
172 " list SERVER's tables\n"
173 "\n list-columns SERVER [TABLE]\n"
174 " list columns in TABLE (or all tables) on SERVER\n"
175 "\n transact SERVER TRANSACTION\n"
176 " run TRANSACTION (a JSON array of operations) on SERVER\n"
177 " and print the results as JSON on stdout\n"
178 "\n monitor SERVER TABLE [COLUMN,...] [SELECT,...]\n"
179 " monitor contents of (COLUMNs in) TABLE on SERVER\n"
180 " Valid SELECTs are: initial, insert, delete, modify\n",
181 program_name, program_name);
182 stream_usage("SERVER", true, true, true);
183 printf("\nOutput formatting options:\n"
184 " -f, --format=FORMAT set output formatting to FORMAT\n"
185 " (\"table\", \"html\", or \"csv\"\n"
186 " --wide don't limit TTY lines to 79 bytes\n"
187 " --no-headings omit table heading row\n"
188 " --pretty pretty-print JSON in output");
191 printf("\nOther options:\n"
192 " -h, --help display this help message\n"
193 " -V, --version display version information\n");
198 parse_json(const char *s)
200 struct json *json = json_from_string(s);
201 if (json->type == JSON_STRING) {
202 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
207 static struct jsonrpc *
208 open_jsonrpc(const char *server)
210 struct stream *stream;
213 error = stream_open_block(server, &stream);
214 if (error == EAFNOSUPPORT) {
215 struct pstream *pstream;
217 error = pstream_open(server, &pstream);
219 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
222 VLOG_INFO("%s: waiting for connection...", server);
223 error = pstream_accept_block(pstream, &stream);
225 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
228 pstream_close(pstream);
230 ovs_fatal(error, "failed to connect to \"%s\"", server);
233 return jsonrpc_open(stream);
237 print_json(struct json *json)
239 char *string = json_to_string(json, json_flags);
240 fputs(string, stdout);
245 print_and_free_json(struct json *json)
252 check_ovsdb_error(struct ovsdb_error *error)
255 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
259 static struct ovsdb_schema *
260 fetch_schema_from_rpc(struct jsonrpc *rpc)
262 struct jsonrpc_msg *request, *reply;
263 struct ovsdb_schema *schema;
266 request = jsonrpc_create_request("get_schema", json_array_create_empty(),
268 error = jsonrpc_transact_block(rpc, request, &reply);
270 ovs_fatal(error, "transaction failed");
272 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
273 jsonrpc_msg_destroy(reply);
278 static struct ovsdb_schema *
279 fetch_schema(const char *server)
281 struct ovsdb_schema *schema;
284 rpc = open_jsonrpc(server);
285 schema = fetch_schema_from_rpc(rpc);
298 struct column *columns;
299 size_t n_columns, allocated_columns;
300 size_t n_rows, allocated_rows;
301 size_t current_column;
305 table_init(struct table *table)
307 memset(table, 0, sizeof *table);
311 table_destroy(struct table *table)
315 for (i = 0; i < table->n_columns; i++) {
316 free(table->columns[i].heading);
318 free(table->columns);
320 for (i = 0; i < table->n_columns * table->n_rows; i++) {
321 free(table->cells[i]);
327 table_add_column(struct table *table, const char *heading, ...)
331 table_add_column(struct table *table, const char *heading, ...)
333 struct column *column;
336 assert(!table->n_rows);
337 if (table->n_columns >= table->allocated_columns) {
338 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
339 sizeof *table->columns);
341 column = &table->columns[table->n_columns++];
343 va_start(args, heading);
344 column->heading = xvasprintf(heading, args);
345 column->width = strlen(column->heading);
350 table_cell__(const struct table *table, size_t row, size_t column)
352 return &table->cells[column + row * table->n_columns];
356 table_add_row(struct table *table)
360 if (table->n_rows >= table->allocated_rows) {
361 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
362 table->n_columns * sizeof *table->cells);
366 table->current_column = 0;
367 for (x = 0; x < table->n_columns; x++) {
368 *table_cell__(table, y, x) = NULL;
373 table_add_cell_nocopy(struct table *table, char *s)
378 assert(table->n_rows > 0);
379 assert(table->current_column < table->n_columns);
381 x = table->current_column++;
382 y = table->n_rows - 1;
383 *table_cell__(table, y, x) = s;
386 if (length > table->columns[x].width) {
387 table->columns[x].width = length;
392 table_add_cell(struct table *table, const char *format, ...)
396 va_start(args, format);
397 table_add_cell_nocopy(table, xvasprintf(format, args));
402 table_print_table_line__(struct ds *line, size_t max_width)
404 ds_truncate(line, max_width);
410 table_print_table__(const struct table *table)
412 struct ds line = DS_EMPTY_INITIALIZER;
415 if (output_headings) {
416 for (x = 0; x < table->n_columns; x++) {
417 const struct column *column = &table->columns[x];
419 ds_put_char(&line, ' ');
421 ds_put_format(&line, "%-*s", column->width, column->heading);
423 table_print_table_line__(&line, output_width);
425 for (x = 0; x < table->n_columns; x++) {
426 const struct column *column = &table->columns[x];
430 ds_put_char(&line, ' ');
432 for (i = 0; i < column->width; i++) {
433 ds_put_char(&line, '-');
436 table_print_table_line__(&line, output_width);
439 for (y = 0; y < table->n_rows; y++) {
440 for (x = 0; x < table->n_columns; x++) {
441 const char *cell = *table_cell__(table, y, x);
443 ds_put_char(&line, ' ');
445 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
447 table_print_table_line__(&line, output_width);
454 table_print_html_cell__(const char *element, const char *content)
458 printf(" <%s>", element);
459 for (p = content; *p != '\0'; p++) {
462 fputs("&", stdout);
465 fputs("<", stdout);
468 fputs(">", stdout);
475 printf("</%s>\n", element);
479 table_print_html__(const struct table *table)
483 fputs("<table>\n", stdout);
485 if (output_headings) {
486 fputs(" <tr>\n", stdout);
487 for (x = 0; x < table->n_columns; x++) {
488 const struct column *column = &table->columns[x];
489 table_print_html_cell__("th", column->heading);
491 fputs(" </tr>\n", stdout);
494 for (y = 0; y < table->n_rows; y++) {
495 fputs(" <tr>\n", stdout);
496 for (x = 0; x < table->n_columns; x++) {
497 table_print_html_cell__("td", *table_cell__(table, y, x));
499 fputs(" </tr>\n", stdout);
502 fputs("</table>\n", stdout);
506 table_print_csv_cell__(const char *content)
510 if (!strpbrk(content, "\n\",")) {
511 fputs(content, stdout);
514 for (p = content; *p != '\0'; p++) {
517 fputs("\"\"", stdout);
529 table_print_csv__(const struct table *table)
533 if (output_headings) {
534 for (x = 0; x < table->n_columns; x++) {
535 const struct column *column = &table->columns[x];
539 table_print_csv_cell__(column->heading);
544 for (y = 0; y < table->n_rows; y++) {
545 for (x = 0; x < table->n_columns; x++) {
549 table_print_csv_cell__(*table_cell__(table, y, x));
556 table_print(const struct table *table)
558 switch (output_format) {
560 table_print_table__(table);
564 table_print_html__(table);
568 table_print_csv__(table);
574 do_get_schema(int argc UNUSED, char *argv[])
576 struct ovsdb_schema *schema = fetch_schema(argv[1]);
577 print_and_free_json(ovsdb_schema_to_json(schema));
578 ovsdb_schema_destroy(schema);
582 do_list_tables(int argc UNUSED, char *argv[])
584 struct ovsdb_schema *schema;
585 struct shash_node *node;
588 schema = fetch_schema(argv[1]);
590 table_add_column(&t, "Table");
591 table_add_column(&t, "Comment");
592 SHASH_FOR_EACH (node, &schema->tables) {
593 struct ovsdb_table_schema *ts = node->data;
596 table_add_cell(&t, ts->name);
598 table_add_cell(&t, ts->comment);
601 ovsdb_schema_destroy(schema);
606 do_list_columns(int argc UNUSED, char *argv[])
608 const char *table_name = argv[2];
609 struct ovsdb_schema *schema;
610 struct shash_node *table_node;
613 schema = fetch_schema(argv[1]);
616 table_add_column(&t, "Table");
618 table_add_column(&t, "Column");
619 table_add_column(&t, "Type");
620 table_add_column(&t, "Comment");
621 SHASH_FOR_EACH (table_node, &schema->tables) {
622 struct ovsdb_table_schema *ts = table_node->data;
624 if (!table_name || !strcmp(table_name, ts->name)) {
625 struct shash_node *column_node;
627 SHASH_FOR_EACH (column_node, &ts->columns) {
628 struct ovsdb_column *column = column_node->data;
629 struct json *type = ovsdb_type_to_json(&column->type);
633 table_add_cell(&t, ts->name);
635 table_add_cell(&t, column->name);
636 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
637 if (column->comment) {
638 table_add_cell(&t, column->comment);
645 ovsdb_schema_destroy(schema);
650 do_transact(int argc UNUSED, char *argv[])
652 struct jsonrpc_msg *request, *reply;
653 struct json *transaction;
657 transaction = parse_json(argv[2]);
659 rpc = open_jsonrpc(argv[1]);
660 request = jsonrpc_create_request("transact", transaction, NULL);
661 error = jsonrpc_transact_block(rpc, request, &reply);
663 ovs_fatal(error, "transaction failed");
666 ovs_fatal(error, "transaction returned error: %s",
667 json_to_string(reply->error, json_flags));
669 print_json(reply->result);
671 jsonrpc_msg_destroy(reply);
676 monitor_print_row(struct json *row, const char *type, const char *uuid,
677 const struct ovsdb_column_set *columns, struct table *t)
682 ovs_error(0, "missing %s row", type);
684 } else if (row->type != JSON_OBJECT) {
685 ovs_error(0, "<row> is not object");
690 table_add_cell(t, uuid);
691 table_add_cell(t, type);
692 for (i = 0; i < columns->n_columns; i++) {
693 const struct ovsdb_column *column = columns->columns[i];
694 struct json *value = shash_find_data(json_object(row), column->name);
696 table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
698 table_add_cell(t, "");
704 monitor_print(struct json *table_updates,
705 const struct ovsdb_table_schema *table,
706 const struct ovsdb_column_set *columns, bool initial)
708 struct json *table_update;
709 struct shash_node *node;
715 if (table_updates->type != JSON_OBJECT) {
716 ovs_error(0, "<table-updates> is not object");
719 table_update = shash_find_data(json_object(table_updates), table->name);
723 if (table_update->type != JSON_OBJECT) {
724 ovs_error(0, "<table-update> is not object");
728 table_add_column(&t, "row");
729 table_add_column(&t, "action");
730 for (i = 0; i < columns->n_columns; i++) {
731 table_add_column(&t, "%s", columns->columns[i]->name);
733 SHASH_FOR_EACH (node, json_object(table_update)) {
734 struct json *row_update = node->data;
735 struct json *old, *new;
737 if (row_update->type != JSON_OBJECT) {
738 ovs_error(0, "<row-update> is not object");
741 old = shash_find_data(json_object(row_update), "old");
742 new = shash_find_data(json_object(row_update), "new");
744 monitor_print_row(new, "initial", node->name, columns, &t);
746 monitor_print_row(new, "insert", node->name, columns, &t);
748 monitor_print_row(old, "delete", node->name, columns, &t);
750 monitor_print_row(old, "old", node->name, columns, &t);
751 monitor_print_row(new, "new", "", columns, &t);
759 do_monitor(int argc, char *argv[])
761 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
762 struct ovsdb_table_schema *table;
763 struct ovsdb_schema *schema;
764 struct jsonrpc_msg *request;
766 struct json *select, *monitor, *monitor_request, *monitor_requests,
769 rpc = open_jsonrpc(argv[1]);
771 schema = fetch_schema_from_rpc(rpc);
772 table = shash_find_data(&schema->tables, argv[2]);
774 ovs_fatal(0, "%s: no table named \"%s\"", argv[1], argv[2]);
777 if (argc >= 4 && *argv[3] != '\0') {
778 char *save_ptr = NULL;
781 for (token = strtok_r(argv[3], ",", &save_ptr); token != NULL;
782 token = strtok_r(NULL, ",", &save_ptr)) {
783 const struct ovsdb_column *column;
784 column = ovsdb_table_schema_get_column(table, token);
786 ovs_fatal(0, "%s: table \"%s\" does not have a "
787 "column named \"%s\"", argv[1], argv[2], token);
789 ovsdb_column_set_add(&columns, column);
792 struct shash_node *node;
794 SHASH_FOR_EACH (node, &table->columns) {
795 const struct ovsdb_column *column = node->data;
796 if (column->index != OVSDB_COL_UUID) {
797 ovsdb_column_set_add(&columns, column);
802 if (argc >= 5 && *argv[4] != '\0') {
803 char *save_ptr = NULL;
806 select = json_object_create();
807 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
808 token = strtok_r(NULL, ",", &save_ptr)) {
809 json_object_put(select, token, json_boolean_create(true));
815 monitor_request = json_object_create();
816 json_object_put(monitor_request,
817 "columns", ovsdb_column_set_to_json(&columns));
819 json_object_put(monitor_request, "select", select);
822 monitor_requests = json_object_create();
823 json_object_put(monitor_requests, argv[2], monitor_request);
825 monitor = json_array_create_2(json_null_create(), monitor_requests);
826 request = jsonrpc_create_request("monitor", monitor, NULL);
827 request_id = json_clone(request->id);
828 jsonrpc_send(rpc, request);
830 struct jsonrpc_msg *msg;
833 error = jsonrpc_recv_block(rpc, &msg);
835 ovs_fatal(error, "%s: receive failed", argv[1]);
838 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
839 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
841 } else if (msg->type == JSONRPC_REPLY
842 && json_equal(msg->id, request_id)) {
843 monitor_print(msg->result, table, &columns, true);
846 /* daemonize() closes the standard file descriptors. We output
847 * to stdout, so we need to save and restore STDOUT_FILENO. */
848 int fd = dup(STDOUT_FILENO);
850 dup2(fd, STDOUT_FILENO);
853 } else if (msg->type == JSONRPC_NOTIFY
854 && !strcmp(msg->method, "update")) {
855 struct json *params = msg->params;
856 if (params->type == JSON_ARRAY
857 && params->u.array.n == 2
858 && params->u.array.elems[0]->type == JSON_NULL) {
859 monitor_print(params->u.array.elems[1],
860 table, &columns, false);
868 do_help(int argc UNUSED, char *argv[] UNUSED)
873 static const struct command all_commands[] = {
874 { "get-schema", 1, 1, do_get_schema },
875 { "list-tables", 1, 1, do_list_tables },
876 { "list-columns", 1, 2, do_list_columns },
877 { "transact", 2, 2, do_transact },
878 { "monitor", 2, 4, do_monitor },
879 { "help", 0, INT_MAX, do_help },
880 { NULL, 0, 0, NULL },