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"
31 #include "dynamic-string.h"
35 #include "ovsdb-error.h"
42 #define THIS_MODULE VLM_ovsdb_client
44 /* --format: Output formatting. */
46 FMT_TABLE, /* Textual table. */
47 FMT_HTML, /* HTML table. */
48 FMT_CSV /* Comma-separated lines. */
51 /* --wide: For --format=table, the maximum output width. */
52 static int output_width;
54 /* --no-headings: Whether table output should include headings. */
55 static int output_headings = true;
57 static const struct command all_commands[];
59 static void usage(void) NO_RETURN;
60 static void parse_options(int argc, char *argv[]);
63 main(int argc, char *argv[])
65 set_program_name(argv[0]);
68 parse_options(argc, argv);
69 signal(SIGPIPE, SIG_IGN);
70 run_command(argc - optind, argv + optind, all_commands);
75 parse_options(int argc, char *argv[])
77 static struct option long_options[] = {
78 {"wide", no_argument, &output_width, INT_MAX},
79 {"format", required_argument, 0, 'f'},
80 {"no-headings", no_argument, &output_headings, 0},
81 {"verbose", optional_argument, 0, 'v'},
82 {"help", no_argument, 0, 'h'},
83 {"version", no_argument, 0, 'V'},
87 char *short_options = long_options_to_short_options(long_options);
89 output_width = isatty(fileno(stdout)) ? 79 : INT_MAX;
93 c = getopt_long(argc, argv, short_options, long_options, NULL);
100 if (!strcmp(optarg, "table")) {
101 output_format = FMT_TABLE;
102 } else if (!strcmp(optarg, "html")) {
103 output_format = FMT_HTML;
104 } else if (!strcmp(optarg, "csv")) {
105 output_format = FMT_CSV;
107 ovs_fatal(0, "unknown output format \"%s\"", optarg);
112 output_width = INT_MAX;
119 OVS_PRINT_VERSION(0, 0);
123 vlog_set_verbosity(optarg);
126 DAEMON_OPTION_HANDLERS
132 /* getopt_long() already set the value for us. */
145 printf("%s: Open vSwitch database JSON-RPC client\n"
146 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
147 "\nValid commands are:\n"
148 "\n get-schema SERVER\n"
149 " retrieve schema from SERVER\n"
150 "\n list-tables SERVER\n"
151 " list SERVER's tables\n"
152 "\n list-columns SERVER [TABLE]\n"
153 " list columns in TABLE (or all tables) on SERVER\n"
154 "\n transact SERVER TRANSACTION\n"
155 " run TRANSACTION (a JSON array of operations) on SERVER\n"
156 " and print the results as JSON on stdout\n"
157 "\n monitor SERVER TABLE [COLUMN,...] [SELECT,...]\n"
158 " monitor contents of (COLUMNs in) TABLE on SERVER\n"
159 " Valid SELECTs are: initial, insert, delete, modify\n",
160 program_name, program_name);
161 stream_usage("SERVER", true, true);
162 printf("\nOutput formatting options:\n"
163 " -f, --format=FORMAT set output formatting to FORMAT\n"
164 " (\"table\", \"html\", or \"csv\"\n"
165 " --wide don't limit TTY lines to 79 bytes\n"
166 " --no-headings omit table heading row\n");
169 printf("\nOther options:\n"
170 " -h, --help display this help message\n"
171 " -V, --version display version information\n");
176 parse_json(const char *s)
178 struct json *json = json_from_string(s);
179 if (json->type == JSON_STRING) {
180 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
185 static struct jsonrpc *
186 open_jsonrpc(const char *server)
188 struct stream *stream;
191 error = stream_open_block(server, &stream);
192 if (error == EAFNOSUPPORT) {
193 struct pstream *pstream;
195 error = pstream_open(server, &pstream);
197 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
200 VLOG_INFO("%s: waiting for connection...", server);
201 error = pstream_accept_block(pstream, &stream);
203 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
206 pstream_close(pstream);
208 ovs_fatal(error, "failed to connect to \"%s\"", server);
211 return jsonrpc_open(stream);
215 print_json(struct json *json)
217 char *string = json_to_string(json, JSSF_SORT);
218 fputs(string, stdout);
223 print_and_free_json(struct json *json)
230 check_ovsdb_error(struct ovsdb_error *error)
233 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
237 static struct ovsdb_schema *
238 fetch_schema_from_rpc(struct jsonrpc *rpc)
240 struct jsonrpc_msg *request, *reply;
241 struct ovsdb_schema *schema;
244 request = jsonrpc_create_request("get_schema", json_array_create_empty(),
246 error = jsonrpc_transact_block(rpc, request, &reply);
248 ovs_fatal(error, "transaction failed");
250 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
251 jsonrpc_msg_destroy(reply);
256 static struct ovsdb_schema *
257 fetch_schema(const char *server)
259 struct ovsdb_schema *schema;
262 rpc = open_jsonrpc(server);
263 schema = fetch_schema_from_rpc(rpc);
276 struct column *columns;
277 size_t n_columns, allocated_columns;
278 size_t n_rows, allocated_rows;
279 size_t current_column;
283 table_init(struct table *table)
285 memset(table, 0, sizeof *table);
289 table_destroy(struct table *table)
293 for (i = 0; i < table->n_columns; i++) {
294 free(table->columns[i].heading);
296 free(table->columns);
298 for (i = 0; i < table->n_columns * table->n_rows; i++) {
299 free(table->cells[i]);
305 table_add_column(struct table *table, const char *heading, ...)
309 table_add_column(struct table *table, const char *heading, ...)
311 struct column *column;
314 assert(!table->n_rows);
315 if (table->n_columns >= table->allocated_columns) {
316 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
317 sizeof *table->columns);
319 column = &table->columns[table->n_columns++];
321 va_start(args, heading);
322 column->heading = xvasprintf(heading, args);
323 column->width = strlen(column->heading);
328 table_cell__(const struct table *table, size_t row, size_t column)
330 return &table->cells[column + row * table->n_columns];
334 table_add_row(struct table *table)
338 if (table->n_rows >= table->allocated_rows) {
339 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
340 table->n_columns * sizeof *table->cells);
344 table->current_column = 0;
345 for (x = 0; x < table->n_columns; x++) {
346 *table_cell__(table, y, x) = NULL;
351 table_add_cell_nocopy(struct table *table, char *s)
356 assert(table->n_rows > 0);
357 assert(table->current_column < table->n_columns);
359 x = table->current_column++;
360 y = table->n_rows - 1;
361 *table_cell__(table, y, x) = s;
364 if (length > table->columns[x].width) {
365 table->columns[x].width = length;
370 table_add_cell(struct table *table, const char *format, ...)
374 va_start(args, format);
375 table_add_cell_nocopy(table, xvasprintf(format, args));
380 table_print_table_line__(struct ds *line, size_t max_width)
382 ds_truncate(line, max_width);
388 table_print_table__(const struct table *table)
390 struct ds line = DS_EMPTY_INITIALIZER;
393 if (output_headings) {
394 for (x = 0; x < table->n_columns; x++) {
395 const struct column *column = &table->columns[x];
397 ds_put_char(&line, ' ');
399 ds_put_format(&line, "%-*s", column->width, column->heading);
401 table_print_table_line__(&line, output_width);
403 for (x = 0; x < table->n_columns; x++) {
404 const struct column *column = &table->columns[x];
408 ds_put_char(&line, ' ');
410 for (i = 0; i < column->width; i++) {
411 ds_put_char(&line, '-');
414 table_print_table_line__(&line, output_width);
417 for (y = 0; y < table->n_rows; y++) {
418 for (x = 0; x < table->n_columns; x++) {
419 const char *cell = *table_cell__(table, y, x);
421 ds_put_char(&line, ' ');
423 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
425 table_print_table_line__(&line, output_width);
432 table_print_html_cell__(const char *element, const char *content)
436 printf(" <%s>", element);
437 for (p = content; *p != '\0'; p++) {
440 fputs("&", stdout);
443 fputs("<", stdout);
446 fputs(">", stdout);
453 printf("</%s>\n", element);
457 table_print_html__(const struct table *table)
461 fputs("<table>\n", stdout);
463 if (output_headings) {
464 fputs(" <tr>\n", stdout);
465 for (x = 0; x < table->n_columns; x++) {
466 const struct column *column = &table->columns[x];
467 table_print_html_cell__("th", column->heading);
469 fputs(" </tr>\n", stdout);
472 for (y = 0; y < table->n_rows; y++) {
473 fputs(" <tr>\n", stdout);
474 for (x = 0; x < table->n_columns; x++) {
475 table_print_html_cell__("td", *table_cell__(table, y, x));
477 fputs(" </tr>\n", stdout);
480 fputs("</table>\n", stdout);
484 table_print_csv_cell__(const char *content)
488 if (!strpbrk(content, "\n\",")) {
489 fputs(content, stdout);
492 for (p = content; *p != '\0'; p++) {
495 fputs("\"\"", stdout);
507 table_print_csv__(const struct table *table)
511 if (output_headings) {
512 for (x = 0; x < table->n_columns; x++) {
513 const struct column *column = &table->columns[x];
517 table_print_csv_cell__(column->heading);
522 for (y = 0; y < table->n_rows; y++) {
523 for (x = 0; x < table->n_columns; x++) {
527 table_print_csv_cell__(*table_cell__(table, y, x));
534 table_print(const struct table *table)
536 switch (output_format) {
538 table_print_table__(table);
542 table_print_html__(table);
546 table_print_csv__(table);
552 do_get_schema(int argc UNUSED, char *argv[])
554 struct ovsdb_schema *schema = fetch_schema(argv[1]);
555 print_and_free_json(ovsdb_schema_to_json(schema));
556 ovsdb_schema_destroy(schema);
560 do_list_tables(int argc UNUSED, char *argv[])
562 struct ovsdb_schema *schema;
563 struct shash_node *node;
566 schema = fetch_schema(argv[1]);
568 table_add_column(&t, "Table");
569 table_add_column(&t, "Comment");
570 SHASH_FOR_EACH (node, &schema->tables) {
571 struct ovsdb_table_schema *ts = node->data;
574 table_add_cell(&t, ts->name);
576 table_add_cell(&t, ts->comment);
579 ovsdb_schema_destroy(schema);
584 do_list_columns(int argc UNUSED, char *argv[])
586 const char *table_name = argv[2];
587 struct ovsdb_schema *schema;
588 struct shash_node *table_node;
591 schema = fetch_schema(argv[1]);
594 table_add_column(&t, "Table");
596 table_add_column(&t, "Column");
597 table_add_column(&t, "Type");
598 table_add_column(&t, "Comment");
599 SHASH_FOR_EACH (table_node, &schema->tables) {
600 struct ovsdb_table_schema *ts = table_node->data;
602 if (!table_name || !strcmp(table_name, ts->name)) {
603 struct shash_node *column_node;
605 SHASH_FOR_EACH (column_node, &ts->columns) {
606 struct ovsdb_column *column = column_node->data;
607 struct json *type = ovsdb_type_to_json(&column->type);
611 table_add_cell(&t, ts->name);
613 table_add_cell(&t, column->name);
614 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
615 if (column->comment) {
616 table_add_cell(&t, column->comment);
623 ovsdb_schema_destroy(schema);
628 do_transact(int argc UNUSED, char *argv[])
630 struct jsonrpc_msg *request, *reply;
631 struct json *transaction;
635 transaction = parse_json(argv[2]);
637 rpc = open_jsonrpc(argv[1]);
638 request = jsonrpc_create_request("transact", transaction, NULL);
639 error = jsonrpc_transact_block(rpc, request, &reply);
641 ovs_fatal(error, "transaction failed");
644 ovs_fatal(error, "transaction returned error: %s",
645 json_to_string(reply->error, JSSF_SORT));
647 print_json(reply->result);
649 jsonrpc_msg_destroy(reply);
654 monitor_print_row(struct json *row, const char *type, const char *uuid,
655 const struct ovsdb_column_set *columns, struct table *t)
660 ovs_error(0, "missing %s row", type);
662 } else if (row->type != JSON_OBJECT) {
663 ovs_error(0, "<row> is not object");
668 table_add_cell(t, uuid);
669 table_add_cell(t, type);
670 for (i = 0; i < columns->n_columns; i++) {
671 const struct ovsdb_column *column = columns->columns[i];
672 struct json *value = shash_find_data(json_object(row), column->name);
674 table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
676 table_add_cell(t, "");
682 monitor_print(struct json *table_updates,
683 const struct ovsdb_table_schema *table,
684 const struct ovsdb_column_set *columns, bool initial)
686 struct json *table_update;
687 struct shash_node *node;
693 if (table_updates->type != JSON_OBJECT) {
694 ovs_error(0, "<table-updates> is not object");
697 table_update = shash_find_data(json_object(table_updates), table->name);
701 if (table_update->type != JSON_OBJECT) {
702 ovs_error(0, "<table-update> is not object");
706 table_add_column(&t, "row");
707 table_add_column(&t, "action");
708 for (i = 0; i < columns->n_columns; i++) {
709 table_add_column(&t, "%s", columns->columns[i]->name);
711 SHASH_FOR_EACH (node, json_object(table_update)) {
712 struct json *row_update = node->data;
713 struct json *old, *new;
715 if (row_update->type != JSON_OBJECT) {
716 ovs_error(0, "<row-update> is not object");
719 old = shash_find_data(json_object(row_update), "old");
720 new = shash_find_data(json_object(row_update), "new");
722 monitor_print_row(new, "initial", node->name, columns, &t);
724 monitor_print_row(new, "insert", node->name, columns, &t);
726 monitor_print_row(old, "delete", node->name, columns, &t);
728 monitor_print_row(old, "old", node->name, columns, &t);
729 monitor_print_row(new, "new", "", columns, &t);
737 do_monitor(int argc, char *argv[])
739 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
740 struct ovsdb_table_schema *table;
741 struct ovsdb_schema *schema;
742 struct jsonrpc_msg *request;
744 struct json *select, *monitor, *monitor_request, *monitor_requests,
747 rpc = open_jsonrpc(argv[1]);
749 schema = fetch_schema_from_rpc(rpc);
750 table = shash_find_data(&schema->tables, argv[2]);
752 ovs_fatal(0, "%s: no table named \"%s\"", argv[1], argv[2]);
755 if (argc >= 4 && *argv[3] != '\0') {
756 char *save_ptr = NULL;
759 for (token = strtok_r(argv[3], ",", &save_ptr); token != NULL;
760 token = strtok_r(NULL, ",", &save_ptr)) {
761 const struct ovsdb_column *column;
762 column = ovsdb_table_schema_get_column(table, token);
764 ovs_fatal(0, "%s: table \"%s\" does not have a "
765 "column named \"%s\"", argv[1], argv[2], token);
767 ovsdb_column_set_add(&columns, column);
770 struct shash_node *node;
772 SHASH_FOR_EACH (node, &table->columns) {
773 const struct ovsdb_column *column = node->data;
774 if (column->index != OVSDB_COL_UUID) {
775 ovsdb_column_set_add(&columns, column);
780 if (argc >= 5 && *argv[4] != '\0') {
781 char *save_ptr = NULL;
784 select = json_object_create();
785 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
786 token = strtok_r(NULL, ",", &save_ptr)) {
787 json_object_put(select, token, json_boolean_create(true));
793 monitor_request = json_object_create();
794 json_object_put(monitor_request,
795 "columns", ovsdb_column_set_to_json(&columns));
797 json_object_put(monitor_request, "select", select);
800 monitor_requests = json_object_create();
801 json_object_put(monitor_requests, argv[2], monitor_request);
803 monitor = json_array_create_2(json_null_create(), monitor_requests);
804 request = jsonrpc_create_request("monitor", monitor, NULL);
805 request_id = json_clone(request->id);
806 jsonrpc_send(rpc, request);
808 struct jsonrpc_msg *msg;
811 error = jsonrpc_recv_block(rpc, &msg);
813 ovs_fatal(error, "%s: receive failed", argv[1]);
816 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
817 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
819 } else if (msg->type == JSONRPC_REPLY
820 && json_equal(msg->id, request_id)) {
821 monitor_print(msg->result, table, &columns, true);
824 } else if (msg->type == JSONRPC_NOTIFY
825 && !strcmp(msg->method, "update")) {
826 struct json *params = msg->params;
827 if (params->type == JSON_ARRAY
828 && params->u.array.n == 2
829 && params->u.array.elems[0]->type == JSON_NULL) {
830 monitor_print(params->u.array.elems[1],
831 table, &columns, false);
839 do_help(int argc UNUSED, char *argv[] UNUSED)
844 static const struct command all_commands[] = {
845 { "get-schema", 1, 1, do_get_schema },
846 { "list-tables", 1, 1, do_list_tables },
847 { "list-columns", 1, 2, do_list_columns },
848 { "transact", 2, 2, do_transact },
849 { "monitor", 2, 4, do_monitor },
850 { "help", 0, INT_MAX, do_help },
851 { NULL, 0, 0, NULL },