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 /* --no-headings: Whether table output should include headings. */
54 static int output_headings = true;
56 /* --pretty: Flags to pass to json_to_string(). */
57 static int json_flags = JSSF_SORT;
59 static const struct command all_commands[];
61 static void usage(void) NO_RETURN;
62 static void parse_options(int argc, char *argv[]);
65 main(int argc, char *argv[])
67 proctitle_init(argc, argv);
68 set_program_name(argv[0]);
71 parse_options(argc, argv);
72 signal(SIGPIPE, SIG_IGN);
73 run_command(argc - optind, argv + optind, all_commands);
78 parse_options(int argc, char *argv[])
81 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
83 static struct option long_options[] = {
84 {"format", required_argument, 0, 'f'},
85 {"no-headings", no_argument, &output_headings, 0},
86 {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
87 {"verbose", optional_argument, 0, 'v'},
88 {"help", no_argument, 0, 'h'},
89 {"version", no_argument, 0, 'V'},
92 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
93 STREAM_SSL_LONG_OPTIONS
97 char *short_options = long_options_to_short_options(long_options);
102 c = getopt_long(argc, argv, short_options, long_options, NULL);
109 if (!strcmp(optarg, "table")) {
110 output_format = FMT_TABLE;
111 } else if (!strcmp(optarg, "html")) {
112 output_format = FMT_HTML;
113 } else if (!strcmp(optarg, "csv")) {
114 output_format = FMT_CSV;
116 ovs_fatal(0, "unknown output format \"%s\"", optarg);
124 OVS_PRINT_VERSION(0, 0);
128 vlog_set_verbosity(optarg);
131 DAEMON_OPTION_HANDLERS
134 STREAM_SSL_OPTION_HANDLERS
136 case OPT_BOOTSTRAP_CA_CERT:
137 stream_ssl_set_ca_cert_file(optarg, true);
145 /* getopt_long() already set the value for us. */
158 printf("%s: Open vSwitch database JSON-RPC client\n"
159 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
160 "\nValid commands are:\n"
161 "\n list-dbs SERVER\n"
162 " list databases available on SERVER\n"
163 "\n get-schema SERVER DATABASE\n"
164 " retrieve schema for DATABASE from SERVER\n"
165 "\n list-tables SERVER DATABSE\n"
166 " list tables for DATABSAE on SERVER\n"
167 "\n list-columns SERVER DATABASE [TABLE]\n"
168 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
169 "\n transact SERVER TRANSACTION\n"
170 " run TRANSACTION (a JSON array of operations) on SERVER\n"
171 " and print the results as JSON on stdout\n"
172 "\n monitor SERVER DATABASE TABLE [COLUMN,...] [SELECT,...]\n"
173 " monitor contents of (COLUMNs in) TABLE in DATABASE on SERVER\n"
174 " Valid SELECTs are: initial, insert, delete, modify\n",
175 program_name, program_name);
176 stream_usage("SERVER", true, true, true);
177 printf("\nOutput formatting options:\n"
178 " -f, --format=FORMAT set output formatting to FORMAT\n"
179 " (\"table\", \"html\", or \"csv\"\n"
180 " --no-headings omit table heading row\n"
181 " --pretty pretty-print JSON in output");
184 printf("\nOther options:\n"
185 " -h, --help display this help message\n"
186 " -V, --version display version information\n");
191 parse_json(const char *s)
193 struct json *json = json_from_string(s);
194 if (json->type == JSON_STRING) {
195 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
200 static struct jsonrpc *
201 open_jsonrpc(const char *server)
203 struct stream *stream;
206 error = stream_open_block(server, &stream);
207 if (error == EAFNOSUPPORT) {
208 struct pstream *pstream;
210 error = pstream_open(server, &pstream);
212 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
215 VLOG_INFO("%s: waiting for connection...", server);
216 error = pstream_accept_block(pstream, &stream);
218 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
221 pstream_close(pstream);
223 ovs_fatal(error, "failed to connect to \"%s\"", server);
226 return jsonrpc_open(stream);
230 print_json(struct json *json)
232 char *string = json_to_string(json, json_flags);
233 fputs(string, stdout);
238 print_and_free_json(struct json *json)
245 check_ovsdb_error(struct ovsdb_error *error)
248 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
252 static struct ovsdb_schema *
253 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
255 struct jsonrpc_msg *request, *reply;
256 struct ovsdb_schema *schema;
259 request = jsonrpc_create_request("get_schema",
261 json_string_create(database)),
263 error = jsonrpc_transact_block(rpc, request, &reply);
265 ovs_fatal(error, "transaction failed");
267 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
268 jsonrpc_msg_destroy(reply);
273 static struct ovsdb_schema *
274 fetch_schema(const char *server, const char *database)
276 struct ovsdb_schema *schema;
279 rpc = open_jsonrpc(server);
280 schema = fetch_schema_from_rpc(rpc, database);
293 struct column *columns;
294 size_t n_columns, allocated_columns;
295 size_t n_rows, allocated_rows;
296 size_t current_column;
300 table_init(struct table *table)
302 memset(table, 0, sizeof *table);
306 table_destroy(struct table *table)
310 for (i = 0; i < table->n_columns; i++) {
311 free(table->columns[i].heading);
313 free(table->columns);
315 for (i = 0; i < table->n_columns * table->n_rows; i++) {
316 free(table->cells[i]);
322 table_add_column(struct table *table, const char *heading, ...)
326 table_add_column(struct table *table, const char *heading, ...)
328 struct column *column;
331 assert(!table->n_rows);
332 if (table->n_columns >= table->allocated_columns) {
333 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
334 sizeof *table->columns);
336 column = &table->columns[table->n_columns++];
338 va_start(args, heading);
339 column->heading = xvasprintf(heading, args);
340 column->width = strlen(column->heading);
345 table_cell__(const struct table *table, size_t row, size_t column)
347 return &table->cells[column + row * table->n_columns];
351 table_add_row(struct table *table)
355 if (table->n_rows >= table->allocated_rows) {
356 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
357 table->n_columns * sizeof *table->cells);
361 table->current_column = 0;
362 for (x = 0; x < table->n_columns; x++) {
363 *table_cell__(table, y, x) = NULL;
368 table_add_cell_nocopy(struct table *table, char *s)
373 assert(table->n_rows > 0);
374 assert(table->current_column < table->n_columns);
376 x = table->current_column++;
377 y = table->n_rows - 1;
378 *table_cell__(table, y, x) = s;
381 if (length > table->columns[x].width) {
382 table->columns[x].width = length;
387 table_add_cell(struct table *table, const char *format, ...)
391 va_start(args, format);
392 table_add_cell_nocopy(table, xvasprintf(format, args));
397 table_print_table_line__(struct ds *line)
404 table_print_table__(const struct table *table)
406 struct ds line = DS_EMPTY_INITIALIZER;
409 if (output_headings) {
410 for (x = 0; x < table->n_columns; x++) {
411 const struct column *column = &table->columns[x];
413 ds_put_char(&line, ' ');
415 ds_put_format(&line, "%-*s", column->width, column->heading);
417 table_print_table_line__(&line);
419 for (x = 0; x < table->n_columns; x++) {
420 const struct column *column = &table->columns[x];
424 ds_put_char(&line, ' ');
426 for (i = 0; i < column->width; i++) {
427 ds_put_char(&line, '-');
430 table_print_table_line__(&line);
433 for (y = 0; y < table->n_rows; y++) {
434 for (x = 0; x < table->n_columns; x++) {
435 const char *cell = *table_cell__(table, y, x);
437 ds_put_char(&line, ' ');
439 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
441 table_print_table_line__(&line);
448 table_print_html_cell__(const char *element, const char *content)
452 printf(" <%s>", element);
453 for (p = content; *p != '\0'; p++) {
456 fputs("&", stdout);
459 fputs("<", stdout);
462 fputs(">", stdout);
469 printf("</%s>\n", element);
473 table_print_html__(const struct table *table)
477 fputs("<table>\n", stdout);
479 if (output_headings) {
480 fputs(" <tr>\n", stdout);
481 for (x = 0; x < table->n_columns; x++) {
482 const struct column *column = &table->columns[x];
483 table_print_html_cell__("th", column->heading);
485 fputs(" </tr>\n", stdout);
488 for (y = 0; y < table->n_rows; y++) {
489 fputs(" <tr>\n", stdout);
490 for (x = 0; x < table->n_columns; x++) {
491 table_print_html_cell__("td", *table_cell__(table, y, x));
493 fputs(" </tr>\n", stdout);
496 fputs("</table>\n", stdout);
500 table_print_csv_cell__(const char *content)
504 if (!strpbrk(content, "\n\",")) {
505 fputs(content, stdout);
508 for (p = content; *p != '\0'; p++) {
511 fputs("\"\"", stdout);
523 table_print_csv__(const struct table *table)
527 if (output_headings) {
528 for (x = 0; x < table->n_columns; x++) {
529 const struct column *column = &table->columns[x];
533 table_print_csv_cell__(column->heading);
538 for (y = 0; y < table->n_rows; y++) {
539 for (x = 0; x < table->n_columns; x++) {
543 table_print_csv_cell__(*table_cell__(table, y, x));
550 table_print(const struct table *table)
552 switch (output_format) {
554 table_print_table__(table);
558 table_print_html__(table);
562 table_print_csv__(table);
568 do_list_dbs(int argc OVS_UNUSED, char *argv[])
570 struct jsonrpc_msg *request, *reply;
575 rpc = open_jsonrpc(argv[1]);
576 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
578 error = jsonrpc_transact_block(rpc, request, &reply);
580 ovs_fatal(error, "transaction failed");
583 if (reply->result->type != JSON_ARRAY) {
584 ovs_fatal(0, "list_dbs response is not array");
587 for (i = 0; i < reply->result->u.array.n; i++) {
588 const struct json *name = reply->result->u.array.elems[i];
590 if (name->type != JSON_STRING) {
591 ovs_fatal(0, "list_dbs response %zu is not string", i);
593 puts(name->u.string);
595 jsonrpc_msg_destroy(reply);
599 do_get_schema(int argc OVS_UNUSED, char *argv[])
601 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
602 print_and_free_json(ovsdb_schema_to_json(schema));
603 ovsdb_schema_destroy(schema);
607 do_list_tables(int argc OVS_UNUSED, char *argv[])
609 struct ovsdb_schema *schema;
610 struct shash_node *node;
613 schema = fetch_schema(argv[1], argv[2]);
615 table_add_column(&t, "Table");
616 table_add_column(&t, "Comment");
617 SHASH_FOR_EACH (node, &schema->tables) {
618 struct ovsdb_table_schema *ts = node->data;
621 table_add_cell(&t, ts->name);
623 table_add_cell(&t, ts->comment);
626 ovsdb_schema_destroy(schema);
631 do_list_columns(int argc OVS_UNUSED, char *argv[])
633 const char *table_name = argv[3];
634 struct ovsdb_schema *schema;
635 struct shash_node *table_node;
638 schema = fetch_schema(argv[1], argv[2]);
641 table_add_column(&t, "Table");
643 table_add_column(&t, "Column");
644 table_add_column(&t, "Type");
645 table_add_column(&t, "Comment");
646 SHASH_FOR_EACH (table_node, &schema->tables) {
647 struct ovsdb_table_schema *ts = table_node->data;
649 if (!table_name || !strcmp(table_name, ts->name)) {
650 struct shash_node *column_node;
652 SHASH_FOR_EACH (column_node, &ts->columns) {
653 const struct ovsdb_column *column = column_node->data;
654 struct json *type = ovsdb_type_to_json(&column->type);
658 table_add_cell(&t, ts->name);
660 table_add_cell(&t, column->name);
661 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
662 if (column->comment) {
663 table_add_cell(&t, column->comment);
670 ovsdb_schema_destroy(schema);
675 do_transact(int argc OVS_UNUSED, char *argv[])
677 struct jsonrpc_msg *request, *reply;
678 struct json *transaction;
682 transaction = parse_json(argv[2]);
684 rpc = open_jsonrpc(argv[1]);
685 request = jsonrpc_create_request("transact", transaction, NULL);
686 error = jsonrpc_transact_block(rpc, request, &reply);
688 ovs_fatal(error, "transaction failed");
691 ovs_fatal(error, "transaction returned error: %s",
692 json_to_string(reply->error, json_flags));
694 print_json(reply->result);
696 jsonrpc_msg_destroy(reply);
701 monitor_print_row(struct json *row, const char *type, const char *uuid,
702 const struct ovsdb_column_set *columns, struct table *t)
707 ovs_error(0, "missing %s row", type);
709 } else if (row->type != JSON_OBJECT) {
710 ovs_error(0, "<row> is not object");
715 table_add_cell(t, uuid);
716 table_add_cell(t, type);
717 for (i = 0; i < columns->n_columns; i++) {
718 const struct ovsdb_column *column = columns->columns[i];
719 struct json *value = shash_find_data(json_object(row), column->name);
721 table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
723 table_add_cell(t, "");
729 monitor_print(struct json *table_updates,
730 const struct ovsdb_table_schema *table,
731 const struct ovsdb_column_set *columns, bool initial)
733 struct json *table_update;
734 struct shash_node *node;
740 if (table_updates->type != JSON_OBJECT) {
741 ovs_error(0, "<table-updates> is not object");
744 table_update = shash_find_data(json_object(table_updates), table->name);
748 if (table_update->type != JSON_OBJECT) {
749 ovs_error(0, "<table-update> is not object");
753 table_add_column(&t, "row");
754 table_add_column(&t, "action");
755 for (i = 0; i < columns->n_columns; i++) {
756 table_add_column(&t, "%s", columns->columns[i]->name);
758 SHASH_FOR_EACH (node, json_object(table_update)) {
759 struct json *row_update = node->data;
760 struct json *old, *new;
762 if (row_update->type != JSON_OBJECT) {
763 ovs_error(0, "<row-update> is not object");
766 old = shash_find_data(json_object(row_update), "old");
767 new = shash_find_data(json_object(row_update), "new");
769 monitor_print_row(new, "initial", node->name, columns, &t);
771 monitor_print_row(new, "insert", node->name, columns, &t);
773 monitor_print_row(old, "delete", node->name, columns, &t);
775 monitor_print_row(old, "old", node->name, columns, &t);
776 monitor_print_row(new, "new", "", columns, &t);
784 do_monitor(int argc, char *argv[])
786 const char *server = argv[1];
787 const char *database = argv[2];
788 const char *table_name = argv[3];
789 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
790 struct ovsdb_table_schema *table;
791 struct ovsdb_schema *schema;
792 struct jsonrpc_msg *request;
794 struct json *select, *monitor, *monitor_request, *monitor_requests,
797 rpc = open_jsonrpc(server);
799 schema = fetch_schema_from_rpc(rpc, database);
800 table = shash_find_data(&schema->tables, table_name);
802 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
803 server, database, table_name);
806 if (argc >= 5 && *argv[4] != '\0') {
807 char *save_ptr = NULL;
810 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
811 token = strtok_r(NULL, ",", &save_ptr)) {
812 const struct ovsdb_column *column;
813 column = ovsdb_table_schema_get_column(table, token);
815 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
816 "column named \"%s\"",
817 server, table_name, database, token);
819 ovsdb_column_set_add(&columns, column);
822 struct shash_node *node;
824 SHASH_FOR_EACH (node, &table->columns) {
825 const struct ovsdb_column *column = node->data;
826 if (column->index != OVSDB_COL_UUID) {
827 ovsdb_column_set_add(&columns, column);
832 if (argc >= 6 && *argv[5] != '\0') {
833 char *save_ptr = NULL;
836 select = json_object_create();
837 for (token = strtok_r(argv[5], ",", &save_ptr); token != NULL;
838 token = strtok_r(NULL, ",", &save_ptr)) {
839 json_object_put(select, token, json_boolean_create(true));
845 monitor_request = json_object_create();
846 json_object_put(monitor_request,
847 "columns", ovsdb_column_set_to_json(&columns));
849 json_object_put(monitor_request, "select", select);
852 monitor_requests = json_object_create();
853 json_object_put(monitor_requests, table_name, monitor_request);
855 monitor = json_array_create_3(json_string_create(database),
856 json_null_create(), monitor_requests);
857 request = jsonrpc_create_request("monitor", monitor, NULL);
858 request_id = json_clone(request->id);
859 jsonrpc_send(rpc, request);
861 struct jsonrpc_msg *msg;
864 error = jsonrpc_recv_block(rpc, &msg);
866 ovsdb_schema_destroy(schema);
867 ovs_fatal(error, "%s: receive failed", server);
870 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
871 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
873 } else if (msg->type == JSONRPC_REPLY
874 && json_equal(msg->id, request_id)) {
875 monitor_print(msg->result, table, &columns, true);
878 /* daemonize() closes the standard file descriptors. We output
879 * to stdout, so we need to save and restore STDOUT_FILENO. */
880 int fd = dup(STDOUT_FILENO);
882 dup2(fd, STDOUT_FILENO);
885 } else if (msg->type == JSONRPC_NOTIFY
886 && !strcmp(msg->method, "update")) {
887 struct json *params = msg->params;
888 if (params->type == JSON_ARRAY
889 && params->u.array.n == 2
890 && params->u.array.elems[0]->type == JSON_NULL) {
891 monitor_print(params->u.array.elems[1],
892 table, &columns, false);
896 jsonrpc_msg_destroy(msg);
901 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
906 static const struct command all_commands[] = {
907 { "list-dbs", 1, 1, do_list_dbs },
908 { "get-schema", 2, 2, do_get_schema },
909 { "list-tables", 2, 2, do_list_tables },
910 { "list-columns", 2, 3, do_list_columns },
911 { "transact", 2, 2, do_transact },
912 { "monitor", 3, 5, do_monitor },
913 { "help", 0, INT_MAX, do_help },
914 { NULL, 0, 0, NULL },