2 * Copyright (c) 2009 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.
27 #include "command-line.h"
30 #include "dynamic-string.h"
34 #include "ovsdb-error.h"
41 #define THIS_MODULE VLM_ovsdb_client
43 /* --format: Output formatting. */
45 FMT_TABLE, /* Textual table. */
46 FMT_HTML, /* HTML table. */
47 FMT_CSV /* Comma-separated lines. */
50 /* --wide: For --format=table, the maximum output width. */
51 static int output_width;
53 /* --no-headings: Whether table output should include headings. */
54 static int output_headings = true;
56 static const struct command all_commands[];
58 static void usage(void) NO_RETURN;
59 static void parse_options(int argc, char *argv[]);
62 main(int argc, char *argv[])
64 set_program_name(argv[0]);
67 parse_options(argc, argv);
68 signal(SIGPIPE, SIG_IGN);
69 run_command(argc - optind, argv + optind, all_commands);
74 parse_options(int argc, char *argv[])
76 static struct option long_options[] = {
77 {"wide", no_argument, &output_width, INT_MAX},
78 {"format", required_argument, 0, 'f'},
79 {"no-headings", no_argument, &output_headings, 0},
80 {"verbose", optional_argument, 0, 'v'},
81 {"help", no_argument, 0, 'h'},
82 {"version", no_argument, 0, 'V'},
85 char *short_options = long_options_to_short_options(long_options);
87 output_width = isatty(fileno(stdout)) ? 79 : INT_MAX;
91 c = getopt_long(argc, argv, short_options, long_options, NULL);
98 if (!strcmp(optarg, "table")) {
99 output_format = FMT_TABLE;
100 } else if (!strcmp(optarg, "html")) {
101 output_format = FMT_HTML;
102 } else if (!strcmp(optarg, "csv")) {
103 output_format = FMT_CSV;
105 ovs_fatal(0, "unknown output format \"%s\"", optarg);
110 output_width = INT_MAX;
117 OVS_PRINT_VERSION(0, 0);
121 vlog_set_verbosity(optarg);
128 /* getopt_long() already set the value for us. */
141 printf("%s: Open vSwitch database JSON-RPC client\n"
142 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
143 "\nValid commands are:\n"
144 "\n get-schema SERVER\n"
145 " retrieve schema from SERVER\n"
146 "\n list-tables SERVER\n"
147 " list SERVER's tables\n"
148 "\n list-columns SERVER [TABLE]\n"
149 " list columns in TABLE (or all tables) on SERVER\n"
150 "\n transact SERVER TRANSACTION\n"
151 " run TRANSACTION (a JSON array of operations) on SERVER\n"
152 " and print the results as JSON on stdout\n"
153 "\n monitor SERVER TABLE [COLUMN,...] [SELECT,...]\n"
154 " monitor contents of (COLUMNs in) TABLE on SERVER\n"
155 " Valid SELECTs are: initial, insert, delete, modify\n",
156 program_name, program_name);
157 stream_usage("SERVER", true, true);
158 printf("\nOutput formatting options:\n"
159 " -f, --format=FORMAT set output formatting to FORMAT\n"
160 " (\"table\", \"html\", or \"csv\"\n"
161 " --wide don't limit TTY lines to 79 bytes\n"
162 " --no-headings omit table heading row\n");
164 printf("\nOther options:\n"
165 " -h, --help display this help message\n"
166 " -V, --version display version information\n");
171 parse_json(const char *s)
173 struct json *json = json_from_string(s);
174 if (json->type == JSON_STRING) {
175 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
180 static struct jsonrpc *
181 open_jsonrpc(const char *server)
183 struct stream *stream;
186 error = stream_open_block(server, &stream);
187 if (error == EAFNOSUPPORT) {
188 struct pstream *pstream;
190 error = pstream_open(server, &pstream);
192 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
195 VLOG_INFO("%s: waiting for connection...", server);
196 error = pstream_accept_block(pstream, &stream);
198 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
201 pstream_close(pstream);
203 ovs_fatal(error, "failed to connect to \"%s\"", server);
206 return jsonrpc_open(stream);
210 print_json(struct json *json)
212 char *string = json_to_string(json, JSSF_SORT);
213 fputs(string, stdout);
218 print_and_free_json(struct json *json)
225 check_ovsdb_error(struct ovsdb_error *error)
228 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
232 static struct ovsdb_schema *
233 fetch_schema_from_rpc(struct jsonrpc *rpc)
235 struct jsonrpc_msg *request, *reply;
236 struct ovsdb_schema *schema;
239 request = jsonrpc_create_request("get_schema", json_array_create_empty());
240 error = jsonrpc_transact_block(rpc, request, &reply);
242 ovs_fatal(error, "transaction failed");
244 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
245 jsonrpc_msg_destroy(reply);
250 static struct ovsdb_schema *
251 fetch_schema(const char *server)
253 struct ovsdb_schema *schema;
256 rpc = open_jsonrpc(server);
257 schema = fetch_schema_from_rpc(rpc);
270 struct column *columns;
271 size_t n_columns, allocated_columns;
272 size_t n_rows, allocated_rows;
273 size_t current_column;
277 table_init(struct table *table)
279 memset(table, 0, sizeof *table);
283 table_destroy(struct table *table)
287 for (i = 0; i < table->n_columns; i++) {
288 free(table->columns[i].heading);
290 free(table->columns);
292 for (i = 0; i < table->n_columns * table->n_rows; i++) {
293 free(table->cells[i]);
299 table_add_column(struct table *table, const char *heading, ...)
303 table_add_column(struct table *table, const char *heading, ...)
305 struct column *column;
308 assert(!table->n_rows);
309 if (table->n_columns >= table->allocated_columns) {
310 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
311 sizeof *table->columns);
313 column = &table->columns[table->n_columns++];
315 va_start(args, heading);
316 column->heading = xvasprintf(heading, args);
317 column->width = strlen(column->heading);
322 table_cell__(const struct table *table, size_t row, size_t column)
324 return &table->cells[column + row * table->n_columns];
328 table_add_row(struct table *table)
332 if (table->n_rows >= table->allocated_rows) {
333 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
334 table->n_columns * sizeof *table->cells);
338 table->current_column = 0;
339 for (x = 0; x < table->n_columns; x++) {
340 *table_cell__(table, y, x) = NULL;
345 table_add_cell_nocopy(struct table *table, char *s)
350 assert(table->n_rows > 0);
351 assert(table->current_column < table->n_columns);
353 x = table->current_column++;
354 y = table->n_rows - 1;
355 *table_cell__(table, y, x) = s;
358 if (length > table->columns[x].width) {
359 table->columns[x].width = length;
364 table_add_cell(struct table *table, const char *format, ...)
368 va_start(args, format);
369 table_add_cell_nocopy(table, xvasprintf(format, args));
374 table_print_table_line__(struct ds *line, size_t max_width)
376 ds_truncate(line, max_width);
382 table_print_table__(const struct table *table)
384 struct ds line = DS_EMPTY_INITIALIZER;
387 if (output_headings) {
388 for (x = 0; x < table->n_columns; x++) {
389 const struct column *column = &table->columns[x];
391 ds_put_char(&line, ' ');
393 ds_put_format(&line, "%-*s", column->width, column->heading);
395 table_print_table_line__(&line, output_width);
397 for (x = 0; x < table->n_columns; x++) {
398 const struct column *column = &table->columns[x];
402 ds_put_char(&line, ' ');
404 for (i = 0; i < column->width; i++) {
405 ds_put_char(&line, '-');
408 table_print_table_line__(&line, output_width);
411 for (y = 0; y < table->n_rows; y++) {
412 for (x = 0; x < table->n_columns; x++) {
413 const char *cell = *table_cell__(table, y, x);
415 ds_put_char(&line, ' ');
417 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
419 table_print_table_line__(&line, output_width);
426 table_print_html_cell__(const char *element, const char *content)
430 printf(" <%s>", element);
431 for (p = content; *p != '\0'; p++) {
434 fputs("&", stdout);
437 fputs("<", stdout);
440 fputs(">", stdout);
447 printf("</%s>\n", element);
451 table_print_html__(const struct table *table)
455 fputs("<table>\n", stdout);
457 if (output_headings) {
458 fputs(" <tr>\n", stdout);
459 for (x = 0; x < table->n_columns; x++) {
460 const struct column *column = &table->columns[x];
461 table_print_html_cell__("th", column->heading);
463 fputs(" </tr>\n", stdout);
466 for (y = 0; y < table->n_rows; y++) {
467 fputs(" <tr>\n", stdout);
468 for (x = 0; x < table->n_columns; x++) {
469 table_print_html_cell__("td", *table_cell__(table, y, x));
471 fputs(" </tr>\n", stdout);
474 fputs("</table>\n", stdout);
478 table_print_csv_cell__(const char *content)
482 if (!strpbrk(content, "\n\",")) {
483 fputs(content, stdout);
486 for (p = content; *p != '\0'; p++) {
489 fputs("\"\"", stdout);
501 table_print_csv__(const struct table *table)
505 if (output_headings) {
506 for (x = 0; x < table->n_columns; x++) {
507 const struct column *column = &table->columns[x];
511 table_print_csv_cell__(column->heading);
516 for (y = 0; y < table->n_rows; y++) {
517 for (x = 0; x < table->n_columns; x++) {
521 table_print_csv_cell__(*table_cell__(table, y, x));
528 table_print(const struct table *table)
530 switch (output_format) {
532 table_print_table__(table);
536 table_print_html__(table);
540 table_print_csv__(table);
546 do_get_schema(int argc UNUSED, char *argv[])
548 struct ovsdb_schema *schema = fetch_schema(argv[1]);
549 print_and_free_json(ovsdb_schema_to_json(schema));
550 ovsdb_schema_destroy(schema);
554 do_list_tables(int argc UNUSED, char *argv[])
556 struct ovsdb_schema *schema;
557 struct shash_node *node;
560 schema = fetch_schema(argv[1]);
562 table_add_column(&t, "Table");
563 table_add_column(&t, "Comment");
564 SHASH_FOR_EACH (node, &schema->tables) {
565 struct ovsdb_table_schema *ts = node->data;
568 table_add_cell(&t, ts->name);
570 table_add_cell(&t, ts->comment);
573 ovsdb_schema_destroy(schema);
578 do_list_columns(int argc UNUSED, char *argv[])
580 const char *table_name = argv[2];
581 struct ovsdb_schema *schema;
582 struct shash_node *table_node;
585 schema = fetch_schema(argv[1]);
588 table_add_column(&t, "Table");
590 table_add_column(&t, "Column");
591 table_add_column(&t, "Type");
592 table_add_column(&t, "Comment");
593 SHASH_FOR_EACH (table_node, &schema->tables) {
594 struct ovsdb_table_schema *ts = table_node->data;
596 if (!table_name || !strcmp(table_name, ts->name)) {
597 struct shash_node *column_node;
599 SHASH_FOR_EACH (column_node, &ts->columns) {
600 struct ovsdb_column *column = column_node->data;
601 struct json *type = ovsdb_type_to_json(&column->type);
605 table_add_cell(&t, ts->name);
607 table_add_cell(&t, column->name);
608 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
609 if (column->comment) {
610 table_add_cell(&t, column->comment);
617 ovsdb_schema_destroy(schema);
622 do_transact(int argc UNUSED, char *argv[])
624 struct jsonrpc_msg *request, *reply;
625 struct json *transaction;
629 transaction = parse_json(argv[2]);
631 rpc = open_jsonrpc(argv[1]);
632 request = jsonrpc_create_request("transact", transaction);
633 error = jsonrpc_transact_block(rpc, request, &reply);
635 ovs_fatal(error, "transaction failed");
638 ovs_fatal(error, "transaction returned error: %s",
639 json_to_string(reply->error, JSSF_SORT));
641 print_json(reply->result);
643 jsonrpc_msg_destroy(reply);
648 monitor_print_row(struct json *row, const char *type, const char *uuid,
649 const struct ovsdb_column_set *columns, struct table *t)
654 ovs_error(0, "missing %s row", type);
656 } else if (row->type != JSON_OBJECT) {
657 ovs_error(0, "<row> is not object");
662 table_add_cell(t, uuid);
663 table_add_cell(t, type);
664 for (i = 0; i < columns->n_columns; i++) {
665 const struct ovsdb_column *column = columns->columns[i];
666 struct json *value = shash_find_data(json_object(row), column->name);
668 table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
670 table_add_cell(t, "");
676 monitor_print(struct json *table_updates,
677 const struct ovsdb_table_schema *table,
678 const struct ovsdb_column_set *columns, bool initial)
680 struct json *table_update;
681 struct shash_node *node;
687 if (table_updates->type != JSON_OBJECT) {
688 ovs_error(0, "<table-updates> is not object");
691 table_update = shash_find_data(json_object(table_updates), table->name);
695 if (table_update->type != JSON_OBJECT) {
696 ovs_error(0, "<table-update> is not object");
700 table_add_column(&t, "row");
701 table_add_column(&t, "action");
702 for (i = 0; i < columns->n_columns; i++) {
703 table_add_column(&t, "%s", columns->columns[i]->name);
705 SHASH_FOR_EACH (node, json_object(table_update)) {
706 struct json *row_update = node->data;
707 struct json *old, *new;
709 if (row_update->type != JSON_OBJECT) {
710 ovs_error(0, "<row-update> is not object");
713 old = shash_find_data(json_object(row_update), "old");
714 new = shash_find_data(json_object(row_update), "new");
716 monitor_print_row(new, "initial", node->name, columns, &t);
718 monitor_print_row(new, "insert", node->name, columns, &t);
720 monitor_print_row(old, "delete", node->name, columns, &t);
722 monitor_print_row(old, "old", node->name, columns, &t);
723 monitor_print_row(new, "new", "", columns, &t);
731 do_monitor(int argc, char *argv[])
733 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
734 struct ovsdb_table_schema *table;
735 struct ovsdb_schema *schema;
736 struct jsonrpc_msg *request;
738 struct json *select, *monitor, *monitor_request, *monitor_requests,
741 rpc = open_jsonrpc(argv[1]);
743 schema = fetch_schema_from_rpc(rpc);
744 table = shash_find_data(&schema->tables, argv[2]);
746 ovs_fatal(0, "%s: no table named \"%s\"", argv[1], argv[2]);
749 if (argc >= 4 && *argv[3] != '\0') {
750 char *save_ptr = NULL;
753 for (token = strtok_r(argv[3], ",", &save_ptr); token != NULL;
754 token = strtok_r(NULL, ",", &save_ptr)) {
755 const struct ovsdb_column *column;
756 column = ovsdb_table_schema_get_column(table, token);
758 ovs_fatal(0, "%s: table \"%s\" does not have a "
759 "column named \"%s\"", argv[1], argv[2], token);
761 ovsdb_column_set_add(&columns, column);
764 struct shash_node *node;
766 SHASH_FOR_EACH (node, &table->columns) {
767 const struct ovsdb_column *column = node->data;
768 if (column->index != OVSDB_COL_UUID) {
769 ovsdb_column_set_add(&columns, column);
774 if (argc >= 5 && *argv[4] != '\0') {
775 char *save_ptr = NULL;
778 select = json_object_create();
779 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
780 token = strtok_r(NULL, ",", &save_ptr)) {
781 json_object_put(select, token, json_boolean_create(true));
787 monitor_request = json_object_create();
788 json_object_put(monitor_request,
789 "columns", ovsdb_column_set_to_json(&columns));
791 json_object_put(monitor_request, "select", select);
794 monitor_requests = json_object_create();
795 json_object_put(monitor_requests, argv[2], monitor_request);
797 monitor = json_array_create_2(json_null_create(), monitor_requests);
798 request = jsonrpc_create_request("monitor", monitor);
799 request_id = json_clone(request->id);
800 jsonrpc_send(rpc, request);
802 struct jsonrpc_msg *msg;
805 error = jsonrpc_recv_block(rpc, &msg);
807 ovs_fatal(error, "%s: receive failed", argv[1]);
810 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
811 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
813 } else if (msg->type == JSONRPC_REPLY
814 && json_equal(msg->id, request_id)) {
815 monitor_print(msg->result, table, &columns, true);
816 } else if (msg->type == JSONRPC_NOTIFY
817 && !strcmp(msg->method, "update")) {
818 struct json *params = msg->params;
819 if (params->type == JSON_ARRAY
820 && params->u.array.n == 2
821 && params->u.array.elems[0]->type == JSON_NULL) {
822 monitor_print(params->u.array.elems[1],
823 table, &columns, false);
830 do_help(int argc UNUSED, char *argv[] UNUSED)
835 static const struct command all_commands[] = {
836 { "get-schema", 1, 1, do_get_schema },
837 { "list-tables", 1, 1, do_list_tables },
838 { "list-columns", 1, 2, do_list_columns },
839 { "transact", 2, 2, do_transact },
840 { "monitor", 2, 4, do_monitor },
841 { "help", 0, INT_MAX, do_help },
842 { NULL, 0, 0, NULL },