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(),
241 error = jsonrpc_transact_block(rpc, request, &reply);
243 ovs_fatal(error, "transaction failed");
245 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
246 jsonrpc_msg_destroy(reply);
251 static struct ovsdb_schema *
252 fetch_schema(const char *server)
254 struct ovsdb_schema *schema;
257 rpc = open_jsonrpc(server);
258 schema = fetch_schema_from_rpc(rpc);
271 struct column *columns;
272 size_t n_columns, allocated_columns;
273 size_t n_rows, allocated_rows;
274 size_t current_column;
278 table_init(struct table *table)
280 memset(table, 0, sizeof *table);
284 table_destroy(struct table *table)
288 for (i = 0; i < table->n_columns; i++) {
289 free(table->columns[i].heading);
291 free(table->columns);
293 for (i = 0; i < table->n_columns * table->n_rows; i++) {
294 free(table->cells[i]);
300 table_add_column(struct table *table, const char *heading, ...)
304 table_add_column(struct table *table, const char *heading, ...)
306 struct column *column;
309 assert(!table->n_rows);
310 if (table->n_columns >= table->allocated_columns) {
311 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
312 sizeof *table->columns);
314 column = &table->columns[table->n_columns++];
316 va_start(args, heading);
317 column->heading = xvasprintf(heading, args);
318 column->width = strlen(column->heading);
323 table_cell__(const struct table *table, size_t row, size_t column)
325 return &table->cells[column + row * table->n_columns];
329 table_add_row(struct table *table)
333 if (table->n_rows >= table->allocated_rows) {
334 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
335 table->n_columns * sizeof *table->cells);
339 table->current_column = 0;
340 for (x = 0; x < table->n_columns; x++) {
341 *table_cell__(table, y, x) = NULL;
346 table_add_cell_nocopy(struct table *table, char *s)
351 assert(table->n_rows > 0);
352 assert(table->current_column < table->n_columns);
354 x = table->current_column++;
355 y = table->n_rows - 1;
356 *table_cell__(table, y, x) = s;
359 if (length > table->columns[x].width) {
360 table->columns[x].width = length;
365 table_add_cell(struct table *table, const char *format, ...)
369 va_start(args, format);
370 table_add_cell_nocopy(table, xvasprintf(format, args));
375 table_print_table_line__(struct ds *line, size_t max_width)
377 ds_truncate(line, max_width);
383 table_print_table__(const struct table *table)
385 struct ds line = DS_EMPTY_INITIALIZER;
388 if (output_headings) {
389 for (x = 0; x < table->n_columns; x++) {
390 const struct column *column = &table->columns[x];
392 ds_put_char(&line, ' ');
394 ds_put_format(&line, "%-*s", column->width, column->heading);
396 table_print_table_line__(&line, output_width);
398 for (x = 0; x < table->n_columns; x++) {
399 const struct column *column = &table->columns[x];
403 ds_put_char(&line, ' ');
405 for (i = 0; i < column->width; i++) {
406 ds_put_char(&line, '-');
409 table_print_table_line__(&line, output_width);
412 for (y = 0; y < table->n_rows; y++) {
413 for (x = 0; x < table->n_columns; x++) {
414 const char *cell = *table_cell__(table, y, x);
416 ds_put_char(&line, ' ');
418 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
420 table_print_table_line__(&line, output_width);
427 table_print_html_cell__(const char *element, const char *content)
431 printf(" <%s>", element);
432 for (p = content; *p != '\0'; p++) {
435 fputs("&", stdout);
438 fputs("<", stdout);
441 fputs(">", stdout);
448 printf("</%s>\n", element);
452 table_print_html__(const struct table *table)
456 fputs("<table>\n", stdout);
458 if (output_headings) {
459 fputs(" <tr>\n", stdout);
460 for (x = 0; x < table->n_columns; x++) {
461 const struct column *column = &table->columns[x];
462 table_print_html_cell__("th", column->heading);
464 fputs(" </tr>\n", stdout);
467 for (y = 0; y < table->n_rows; y++) {
468 fputs(" <tr>\n", stdout);
469 for (x = 0; x < table->n_columns; x++) {
470 table_print_html_cell__("td", *table_cell__(table, y, x));
472 fputs(" </tr>\n", stdout);
475 fputs("</table>\n", stdout);
479 table_print_csv_cell__(const char *content)
483 if (!strpbrk(content, "\n\",")) {
484 fputs(content, stdout);
487 for (p = content; *p != '\0'; p++) {
490 fputs("\"\"", stdout);
502 table_print_csv__(const struct table *table)
506 if (output_headings) {
507 for (x = 0; x < table->n_columns; x++) {
508 const struct column *column = &table->columns[x];
512 table_print_csv_cell__(column->heading);
517 for (y = 0; y < table->n_rows; y++) {
518 for (x = 0; x < table->n_columns; x++) {
522 table_print_csv_cell__(*table_cell__(table, y, x));
529 table_print(const struct table *table)
531 switch (output_format) {
533 table_print_table__(table);
537 table_print_html__(table);
541 table_print_csv__(table);
547 do_get_schema(int argc UNUSED, char *argv[])
549 struct ovsdb_schema *schema = fetch_schema(argv[1]);
550 print_and_free_json(ovsdb_schema_to_json(schema));
551 ovsdb_schema_destroy(schema);
555 do_list_tables(int argc UNUSED, char *argv[])
557 struct ovsdb_schema *schema;
558 struct shash_node *node;
561 schema = fetch_schema(argv[1]);
563 table_add_column(&t, "Table");
564 table_add_column(&t, "Comment");
565 SHASH_FOR_EACH (node, &schema->tables) {
566 struct ovsdb_table_schema *ts = node->data;
569 table_add_cell(&t, ts->name);
571 table_add_cell(&t, ts->comment);
574 ovsdb_schema_destroy(schema);
579 do_list_columns(int argc UNUSED, char *argv[])
581 const char *table_name = argv[2];
582 struct ovsdb_schema *schema;
583 struct shash_node *table_node;
586 schema = fetch_schema(argv[1]);
589 table_add_column(&t, "Table");
591 table_add_column(&t, "Column");
592 table_add_column(&t, "Type");
593 table_add_column(&t, "Comment");
594 SHASH_FOR_EACH (table_node, &schema->tables) {
595 struct ovsdb_table_schema *ts = table_node->data;
597 if (!table_name || !strcmp(table_name, ts->name)) {
598 struct shash_node *column_node;
600 SHASH_FOR_EACH (column_node, &ts->columns) {
601 struct ovsdb_column *column = column_node->data;
602 struct json *type = ovsdb_type_to_json(&column->type);
606 table_add_cell(&t, ts->name);
608 table_add_cell(&t, column->name);
609 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
610 if (column->comment) {
611 table_add_cell(&t, column->comment);
618 ovsdb_schema_destroy(schema);
623 do_transact(int argc UNUSED, char *argv[])
625 struct jsonrpc_msg *request, *reply;
626 struct json *transaction;
630 transaction = parse_json(argv[2]);
632 rpc = open_jsonrpc(argv[1]);
633 request = jsonrpc_create_request("transact", transaction, NULL);
634 error = jsonrpc_transact_block(rpc, request, &reply);
636 ovs_fatal(error, "transaction failed");
639 ovs_fatal(error, "transaction returned error: %s",
640 json_to_string(reply->error, JSSF_SORT));
642 print_json(reply->result);
644 jsonrpc_msg_destroy(reply);
649 monitor_print_row(struct json *row, const char *type, const char *uuid,
650 const struct ovsdb_column_set *columns, struct table *t)
655 ovs_error(0, "missing %s row", type);
657 } else if (row->type != JSON_OBJECT) {
658 ovs_error(0, "<row> is not object");
663 table_add_cell(t, uuid);
664 table_add_cell(t, type);
665 for (i = 0; i < columns->n_columns; i++) {
666 const struct ovsdb_column *column = columns->columns[i];
667 struct json *value = shash_find_data(json_object(row), column->name);
669 table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
671 table_add_cell(t, "");
677 monitor_print(struct json *table_updates,
678 const struct ovsdb_table_schema *table,
679 const struct ovsdb_column_set *columns, bool initial)
681 struct json *table_update;
682 struct shash_node *node;
688 if (table_updates->type != JSON_OBJECT) {
689 ovs_error(0, "<table-updates> is not object");
692 table_update = shash_find_data(json_object(table_updates), table->name);
696 if (table_update->type != JSON_OBJECT) {
697 ovs_error(0, "<table-update> is not object");
701 table_add_column(&t, "row");
702 table_add_column(&t, "action");
703 for (i = 0; i < columns->n_columns; i++) {
704 table_add_column(&t, "%s", columns->columns[i]->name);
706 SHASH_FOR_EACH (node, json_object(table_update)) {
707 struct json *row_update = node->data;
708 struct json *old, *new;
710 if (row_update->type != JSON_OBJECT) {
711 ovs_error(0, "<row-update> is not object");
714 old = shash_find_data(json_object(row_update), "old");
715 new = shash_find_data(json_object(row_update), "new");
717 monitor_print_row(new, "initial", node->name, columns, &t);
719 monitor_print_row(new, "insert", node->name, columns, &t);
721 monitor_print_row(old, "delete", node->name, columns, &t);
723 monitor_print_row(old, "old", node->name, columns, &t);
724 monitor_print_row(new, "new", "", columns, &t);
732 do_monitor(int argc, char *argv[])
734 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
735 struct ovsdb_table_schema *table;
736 struct ovsdb_schema *schema;
737 struct jsonrpc_msg *request;
739 struct json *select, *monitor, *monitor_request, *monitor_requests,
742 rpc = open_jsonrpc(argv[1]);
744 schema = fetch_schema_from_rpc(rpc);
745 table = shash_find_data(&schema->tables, argv[2]);
747 ovs_fatal(0, "%s: no table named \"%s\"", argv[1], argv[2]);
750 if (argc >= 4 && *argv[3] != '\0') {
751 char *save_ptr = NULL;
754 for (token = strtok_r(argv[3], ",", &save_ptr); token != NULL;
755 token = strtok_r(NULL, ",", &save_ptr)) {
756 const struct ovsdb_column *column;
757 column = ovsdb_table_schema_get_column(table, token);
759 ovs_fatal(0, "%s: table \"%s\" does not have a "
760 "column named \"%s\"", argv[1], argv[2], token);
762 ovsdb_column_set_add(&columns, column);
765 struct shash_node *node;
767 SHASH_FOR_EACH (node, &table->columns) {
768 const struct ovsdb_column *column = node->data;
769 if (column->index != OVSDB_COL_UUID) {
770 ovsdb_column_set_add(&columns, column);
775 if (argc >= 5 && *argv[4] != '\0') {
776 char *save_ptr = NULL;
779 select = json_object_create();
780 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
781 token = strtok_r(NULL, ",", &save_ptr)) {
782 json_object_put(select, token, json_boolean_create(true));
788 monitor_request = json_object_create();
789 json_object_put(monitor_request,
790 "columns", ovsdb_column_set_to_json(&columns));
792 json_object_put(monitor_request, "select", select);
795 monitor_requests = json_object_create();
796 json_object_put(monitor_requests, argv[2], monitor_request);
798 monitor = json_array_create_2(json_null_create(), monitor_requests);
799 request = jsonrpc_create_request("monitor", monitor, NULL);
800 request_id = json_clone(request->id);
801 jsonrpc_send(rpc, request);
803 struct jsonrpc_msg *msg;
806 error = jsonrpc_recv_block(rpc, &msg);
808 ovs_fatal(error, "%s: receive failed", argv[1]);
811 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
812 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
814 } else if (msg->type == JSONRPC_REPLY
815 && json_equal(msg->id, request_id)) {
816 monitor_print(msg->result, table, &columns, true);
817 } else if (msg->type == JSONRPC_NOTIFY
818 && !strcmp(msg->method, "update")) {
819 struct json *params = msg->params;
820 if (params->type == JSON_ARRAY
821 && params->u.array.n == 2
822 && params->u.array.elems[0]->type == JSON_NULL) {
823 monitor_print(params->u.array.elems[1],
824 table, &columns, false);
831 do_help(int argc UNUSED, char *argv[] UNUSED)
836 static const struct command all_commands[] = {
837 { "get-schema", 1, 1, do_get_schema },
838 { "list-tables", 1, 1, do_list_tables },
839 { "list-columns", 1, 2, do_list_columns },
840 { "transact", 2, 2, do_transact },
841 { "monitor", 2, 4, do_monitor },
842 { "help", 0, INT_MAX, do_help },
843 { NULL, 0, 0, NULL },