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"
39 #include "stream-ssl.h"
45 #define THIS_MODULE VLM_ovsdb_client
47 /* --format: Output formatting. */
49 FMT_TABLE, /* Textual table. */
50 FMT_HTML, /* HTML table. */
51 FMT_CSV /* Comma-separated lines. */
54 /* --no-headings: Whether table output should include headings. */
55 static int output_headings = true;
57 /* --pretty: Flags to pass to json_to_string(). */
58 static int json_flags = JSSF_SORT;
60 /* --data: Format of data in output tables. */
62 DF_STRING, /* String format. */
66 static const struct command all_commands[];
68 static void usage(void) NO_RETURN;
69 static void parse_options(int argc, char *argv[]);
72 main(int argc, char *argv[])
74 proctitle_init(argc, argv);
75 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;
124 ovs_fatal(0, "unknown output format \"%s\"", optarg);
129 if (!strcmp(optarg, "string")) {
130 data_format = DF_STRING;
131 } else if (!strcmp(optarg, "json")) {
132 data_format = DF_JSON;
134 ovs_fatal(0, "unknown data format \"%s\"", optarg);
142 OVS_PRINT_VERSION(0, 0);
146 vlog_set_verbosity(optarg);
149 DAEMON_OPTION_HANDLERS
152 STREAM_SSL_OPTION_HANDLERS
154 case OPT_BOOTSTRAP_CA_CERT:
155 stream_ssl_set_ca_cert_file(optarg, true);
163 /* getopt_long() already set the value for us. */
176 printf("%s: Open vSwitch database JSON-RPC client\n"
177 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
178 "\nValid commands are:\n"
179 "\n list-dbs SERVER\n"
180 " list databases available on SERVER\n"
181 "\n get-schema SERVER DATABASE\n"
182 " retrieve schema for DATABASE from SERVER\n"
183 "\n list-tables SERVER DATABSE\n"
184 " list tables for DATABSAE on SERVER\n"
185 "\n list-columns SERVER DATABASE [TABLE]\n"
186 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
187 "\n transact SERVER TRANSACTION\n"
188 " run TRANSACTION (a JSON array of operations) on SERVER\n"
189 " and print the results as JSON on stdout\n"
190 "\n monitor SERVER DATABASE TABLE [COLUMN,...] [SELECT,...]\n"
191 " monitor contents of (COLUMNs in) TABLE in DATABASE on SERVER\n"
192 " Valid SELECTs are: initial, insert, delete, modify\n",
193 program_name, program_name);
194 stream_usage("SERVER", true, true, true);
195 printf("\nOutput formatting options:\n"
196 " -f, --format=FORMAT set output formatting to FORMAT\n"
197 " (\"table\", \"html\", or \"csv\"\n"
198 " --no-headings omit table heading row\n"
199 " --pretty pretty-print JSON in output");
202 printf("\nOther options:\n"
203 " -h, --help display this help message\n"
204 " -V, --version display version information\n");
209 parse_json(const char *s)
211 struct json *json = json_from_string(s);
212 if (json->type == JSON_STRING) {
213 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
218 static struct jsonrpc *
219 open_jsonrpc(const char *server)
221 struct stream *stream;
224 error = stream_open_block(server, &stream);
225 if (error == EAFNOSUPPORT) {
226 struct pstream *pstream;
228 error = pstream_open(server, &pstream);
230 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
233 VLOG_INFO("%s: waiting for connection...", server);
234 error = pstream_accept_block(pstream, &stream);
236 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
239 pstream_close(pstream);
241 ovs_fatal(error, "failed to connect to \"%s\"", server);
244 return jsonrpc_open(stream);
248 print_json(struct json *json)
250 char *string = json_to_string(json, json_flags);
251 fputs(string, stdout);
256 print_and_free_json(struct json *json)
263 check_ovsdb_error(struct ovsdb_error *error)
266 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
270 static struct ovsdb_schema *
271 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
273 struct jsonrpc_msg *request, *reply;
274 struct ovsdb_schema *schema;
277 request = jsonrpc_create_request("get_schema",
279 json_string_create(database)),
281 error = jsonrpc_transact_block(rpc, request, &reply);
283 ovs_fatal(error, "transaction failed");
285 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
286 jsonrpc_msg_destroy(reply);
291 static struct ovsdb_schema *
292 fetch_schema(const char *server, const char *database)
294 struct ovsdb_schema *schema;
297 rpc = open_jsonrpc(server);
298 schema = fetch_schema_from_rpc(rpc, database);
311 struct column *columns;
312 size_t n_columns, allocated_columns;
313 size_t n_rows, allocated_rows;
314 size_t current_column;
318 table_init(struct table *table)
320 memset(table, 0, sizeof *table);
324 table_destroy(struct table *table)
328 for (i = 0; i < table->n_columns; i++) {
329 free(table->columns[i].heading);
331 free(table->columns);
333 for (i = 0; i < table->n_columns * table->n_rows; i++) {
334 free(table->cells[i]);
340 table_add_column(struct table *table, const char *heading, ...)
344 table_add_column(struct table *table, const char *heading, ...)
346 struct column *column;
349 assert(!table->n_rows);
350 if (table->n_columns >= table->allocated_columns) {
351 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
352 sizeof *table->columns);
354 column = &table->columns[table->n_columns++];
356 va_start(args, heading);
357 column->heading = xvasprintf(heading, args);
358 column->width = strlen(column->heading);
363 table_cell__(const struct table *table, size_t row, size_t column)
365 return &table->cells[column + row * table->n_columns];
369 table_add_row(struct table *table)
373 if (table->n_rows >= table->allocated_rows) {
374 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
375 table->n_columns * sizeof *table->cells);
379 table->current_column = 0;
380 for (x = 0; x < table->n_columns; x++) {
381 *table_cell__(table, y, x) = NULL;
386 table_add_cell_nocopy(struct table *table, char *s)
391 assert(table->n_rows > 0);
392 assert(table->current_column < table->n_columns);
394 x = table->current_column++;
395 y = table->n_rows - 1;
396 *table_cell__(table, y, x) = s;
399 if (length > table->columns[x].width) {
400 table->columns[x].width = length;
405 table_add_cell(struct table *table, const char *format, ...)
409 va_start(args, format);
410 table_add_cell_nocopy(table, xvasprintf(format, args));
415 table_print_table_line__(struct ds *line)
422 table_print_table__(const struct table *table)
424 struct ds line = DS_EMPTY_INITIALIZER;
427 if (output_headings) {
428 for (x = 0; x < table->n_columns; x++) {
429 const struct column *column = &table->columns[x];
431 ds_put_char(&line, ' ');
433 ds_put_format(&line, "%-*s", column->width, column->heading);
435 table_print_table_line__(&line);
437 for (x = 0; x < table->n_columns; x++) {
438 const struct column *column = &table->columns[x];
442 ds_put_char(&line, ' ');
444 for (i = 0; i < column->width; i++) {
445 ds_put_char(&line, '-');
448 table_print_table_line__(&line);
451 for (y = 0; y < table->n_rows; y++) {
452 for (x = 0; x < table->n_columns; x++) {
453 const char *cell = *table_cell__(table, y, x);
455 ds_put_char(&line, ' ');
457 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
459 table_print_table_line__(&line);
466 table_escape_html_text__(const char *s, size_t n)
470 for (i = 0; i < n; i++) {
475 fputs("&", stdout);
478 fputs("<", stdout);
481 fputs(">", stdout);
484 fputs(""", stdout);
494 table_print_html_cell__(const char *element, const char *content)
498 printf(" <%s>", element);
499 for (p = content; *p; ) {
502 if (uuid_from_string_prefix(&uuid, p)) {
503 printf("<a href=\"#%.*s\">%.*s</a>", UUID_LEN, p, 8, p);
506 table_escape_html_text__(p, 1);
510 printf("</%s>\n", element);
514 table_print_html__(const struct table *table)
518 fputs("<table border=1>\n", stdout);
520 if (output_headings) {
521 fputs(" <tr>\n", stdout);
522 for (x = 0; x < table->n_columns; x++) {
523 const struct column *column = &table->columns[x];
524 table_print_html_cell__("th", column->heading);
526 fputs(" </tr>\n", stdout);
529 for (y = 0; y < table->n_rows; y++) {
530 fputs(" <tr>\n", stdout);
531 for (x = 0; x < table->n_columns; x++) {
532 const char *content = *table_cell__(table, y, x);
534 if (!strcmp(table->columns[x].heading, "_uuid")) {
535 fputs(" <td><a name=\"", stdout);
536 table_escape_html_text__(content, strlen(content));
537 fputs("\">", stdout);
538 table_escape_html_text__(content, 8);
539 fputs("</a></td>\n", stdout);
541 table_print_html_cell__("td", content);
544 fputs(" </tr>\n", stdout);
547 fputs("</table>\n", stdout);
551 table_print_csv_cell__(const char *content)
555 if (!strpbrk(content, "\n\",")) {
556 fputs(content, stdout);
559 for (p = content; *p != '\0'; p++) {
562 fputs("\"\"", stdout);
574 table_print_csv__(const struct table *table)
578 if (output_headings) {
579 for (x = 0; x < table->n_columns; x++) {
580 const struct column *column = &table->columns[x];
584 table_print_csv_cell__(column->heading);
589 for (y = 0; y < table->n_rows; y++) {
590 for (x = 0; x < table->n_columns; x++) {
594 table_print_csv_cell__(*table_cell__(table, y, x));
601 table_print(const struct table *table)
603 switch (output_format) {
605 table_print_table__(table);
609 table_print_html__(table);
613 table_print_csv__(table);
619 do_list_dbs(int argc OVS_UNUSED, char *argv[])
621 struct jsonrpc_msg *request, *reply;
626 rpc = open_jsonrpc(argv[1]);
627 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
629 error = jsonrpc_transact_block(rpc, request, &reply);
631 ovs_fatal(error, "transaction failed");
634 if (reply->result->type != JSON_ARRAY) {
635 ovs_fatal(0, "list_dbs response is not array");
638 for (i = 0; i < reply->result->u.array.n; i++) {
639 const struct json *name = reply->result->u.array.elems[i];
641 if (name->type != JSON_STRING) {
642 ovs_fatal(0, "list_dbs response %zu is not string", i);
644 puts(name->u.string);
646 jsonrpc_msg_destroy(reply);
650 do_get_schema(int argc OVS_UNUSED, char *argv[])
652 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
653 print_and_free_json(ovsdb_schema_to_json(schema));
654 ovsdb_schema_destroy(schema);
658 do_list_tables(int argc OVS_UNUSED, char *argv[])
660 struct ovsdb_schema *schema;
661 struct shash_node *node;
664 schema = fetch_schema(argv[1], argv[2]);
666 table_add_column(&t, "Table");
667 table_add_column(&t, "Comment");
668 SHASH_FOR_EACH (node, &schema->tables) {
669 struct ovsdb_table_schema *ts = node->data;
672 table_add_cell(&t, ts->name);
674 table_add_cell(&t, ts->comment);
677 ovsdb_schema_destroy(schema);
682 do_list_columns(int argc OVS_UNUSED, char *argv[])
684 const char *table_name = argv[3];
685 struct ovsdb_schema *schema;
686 struct shash_node *table_node;
689 schema = fetch_schema(argv[1], argv[2]);
692 table_add_column(&t, "Table");
694 table_add_column(&t, "Column");
695 table_add_column(&t, "Type");
696 table_add_column(&t, "Comment");
697 SHASH_FOR_EACH (table_node, &schema->tables) {
698 struct ovsdb_table_schema *ts = table_node->data;
700 if (!table_name || !strcmp(table_name, ts->name)) {
701 struct shash_node *column_node;
703 SHASH_FOR_EACH (column_node, &ts->columns) {
704 const struct ovsdb_column *column = column_node->data;
705 struct json *type = ovsdb_type_to_json(&column->type);
709 table_add_cell(&t, ts->name);
711 table_add_cell(&t, column->name);
712 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
713 if (column->comment) {
714 table_add_cell(&t, column->comment);
721 ovsdb_schema_destroy(schema);
726 do_transact(int argc OVS_UNUSED, char *argv[])
728 struct jsonrpc_msg *request, *reply;
729 struct json *transaction;
733 transaction = parse_json(argv[2]);
735 rpc = open_jsonrpc(argv[1]);
736 request = jsonrpc_create_request("transact", transaction, NULL);
737 error = jsonrpc_transact_block(rpc, request, &reply);
739 ovs_fatal(error, "transaction failed");
742 ovs_fatal(error, "transaction returned error: %s",
743 json_to_string(reply->error, json_flags));
745 print_json(reply->result);
747 jsonrpc_msg_destroy(reply);
752 format_data(const struct json *json, const struct ovsdb_type *type)
754 if (data_format == DF_JSON) {
755 return json_to_string(json, JSSF_SORT);
756 } else if (data_format == DF_STRING) {
757 struct ovsdb_datum datum;
758 struct ovsdb_error *error;
761 error = ovsdb_datum_from_json(&datum, type, json, NULL);
763 return json_to_string(json, JSSF_SORT);
767 ovsdb_datum_to_string(&datum, type, &s);
768 ovsdb_datum_destroy(&datum, type);
769 return ds_steal_cstr(&s);
776 monitor_print_row(struct json *row, const char *type, const char *uuid,
777 const struct ovsdb_column_set *columns, struct table *t)
782 ovs_error(0, "missing %s row", type);
784 } else if (row->type != JSON_OBJECT) {
785 ovs_error(0, "<row> is not object");
790 table_add_cell(t, uuid);
791 table_add_cell(t, type);
792 for (i = 0; i < columns->n_columns; i++) {
793 const struct ovsdb_column *column = columns->columns[i];
794 struct json *value = shash_find_data(json_object(row), column->name);
796 table_add_cell_nocopy(t, format_data(value, &column->type));
798 table_add_cell(t, "");
804 monitor_print(struct json *table_updates,
805 const struct ovsdb_table_schema *table,
806 const struct ovsdb_column_set *columns, bool initial)
808 struct json *table_update;
809 struct shash_node *node;
815 if (table_updates->type != JSON_OBJECT) {
816 ovs_error(0, "<table-updates> is not object");
819 table_update = shash_find_data(json_object(table_updates), table->name);
823 if (table_update->type != JSON_OBJECT) {
824 ovs_error(0, "<table-update> is not object");
828 table_add_column(&t, "row");
829 table_add_column(&t, "action");
830 for (i = 0; i < columns->n_columns; i++) {
831 table_add_column(&t, "%s", columns->columns[i]->name);
833 SHASH_FOR_EACH (node, json_object(table_update)) {
834 struct json *row_update = node->data;
835 struct json *old, *new;
837 if (row_update->type != JSON_OBJECT) {
838 ovs_error(0, "<row-update> is not object");
841 old = shash_find_data(json_object(row_update), "old");
842 new = shash_find_data(json_object(row_update), "new");
844 monitor_print_row(new, "initial", node->name, columns, &t);
846 monitor_print_row(new, "insert", node->name, columns, &t);
848 monitor_print_row(old, "delete", node->name, columns, &t);
850 monitor_print_row(old, "old", node->name, columns, &t);
851 monitor_print_row(new, "new", "", columns, &t);
859 do_monitor(int argc, char *argv[])
861 const char *server = argv[1];
862 const char *database = argv[2];
863 const char *table_name = argv[3];
864 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
865 struct ovsdb_table_schema *table;
866 struct ovsdb_schema *schema;
867 struct jsonrpc_msg *request;
869 struct json *select, *monitor, *monitor_request, *monitor_requests,
872 rpc = open_jsonrpc(server);
874 schema = fetch_schema_from_rpc(rpc, database);
875 table = shash_find_data(&schema->tables, table_name);
877 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
878 server, database, table_name);
881 if (argc >= 5 && *argv[4] != '\0') {
882 char *save_ptr = NULL;
885 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
886 token = strtok_r(NULL, ",", &save_ptr)) {
887 const struct ovsdb_column *column;
888 column = ovsdb_table_schema_get_column(table, token);
890 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
891 "column named \"%s\"",
892 server, table_name, database, token);
894 ovsdb_column_set_add(&columns, column);
897 struct shash_node *node;
899 SHASH_FOR_EACH (node, &table->columns) {
900 const struct ovsdb_column *column = node->data;
901 if (column->index != OVSDB_COL_UUID) {
902 ovsdb_column_set_add(&columns, column);
907 if (argc >= 6 && *argv[5] != '\0') {
908 char *save_ptr = NULL;
911 select = json_object_create();
912 for (token = strtok_r(argv[5], ",", &save_ptr); token != NULL;
913 token = strtok_r(NULL, ",", &save_ptr)) {
914 json_object_put(select, token, json_boolean_create(true));
920 monitor_request = json_object_create();
921 json_object_put(monitor_request,
922 "columns", ovsdb_column_set_to_json(&columns));
924 json_object_put(monitor_request, "select", select);
927 monitor_requests = json_object_create();
928 json_object_put(monitor_requests, table_name, monitor_request);
930 monitor = json_array_create_3(json_string_create(database),
931 json_null_create(), monitor_requests);
932 request = jsonrpc_create_request("monitor", monitor, NULL);
933 request_id = json_clone(request->id);
934 jsonrpc_send(rpc, request);
936 struct jsonrpc_msg *msg;
939 error = jsonrpc_recv_block(rpc, &msg);
941 ovsdb_schema_destroy(schema);
942 ovs_fatal(error, "%s: receive failed", server);
945 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
946 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
948 } else if (msg->type == JSONRPC_REPLY
949 && json_equal(msg->id, request_id)) {
950 monitor_print(msg->result, table, &columns, true);
953 /* daemonize() closes the standard file descriptors. We output
954 * to stdout, so we need to save and restore STDOUT_FILENO. */
955 int fd = dup(STDOUT_FILENO);
957 dup2(fd, STDOUT_FILENO);
960 } else if (msg->type == JSONRPC_NOTIFY
961 && !strcmp(msg->method, "update")) {
962 struct json *params = msg->params;
963 if (params->type == JSON_ARRAY
964 && params->u.array.n == 2
965 && params->u.array.elems[0]->type == JSON_NULL) {
966 monitor_print(params->u.array.elems[1],
967 table, &columns, false);
971 jsonrpc_msg_destroy(msg);
976 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
981 static const struct command all_commands[] = {
982 { "list-dbs", 1, 1, do_list_dbs },
983 { "get-schema", 2, 2, do_get_schema },
984 { "list-tables", 2, 2, do_list_tables },
985 { "list-columns", 2, 3, do_list_columns },
986 { "transact", 2, 2, do_transact },
987 { "monitor", 3, 5, do_monitor },
988 { "help", 0, INT_MAX, do_help },
989 { NULL, 0, 0, NULL },