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 /* --pretty: Flags to pass to json_to_string(). */
58 static int json_flags = JSSF_SORT;
60 static const struct command all_commands[];
62 static void usage(void) NO_RETURN;
63 static void parse_options(int argc, char *argv[]);
66 main(int argc, char *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[])
80 static struct option long_options[] = {
81 {"wide", no_argument, &output_width, INT_MAX},
82 {"format", required_argument, 0, 'f'},
83 {"no-headings", no_argument, &output_headings, 0},
84 {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
85 {"verbose", optional_argument, 0, 'v'},
86 {"help", no_argument, 0, 'h'},
87 {"version", no_argument, 0, 'V'},
91 char *short_options = long_options_to_short_options(long_options);
93 output_width = isatty(fileno(stdout)) ? 79 : INT_MAX;
97 c = getopt_long(argc, argv, short_options, long_options, NULL);
104 if (!strcmp(optarg, "table")) {
105 output_format = FMT_TABLE;
106 } else if (!strcmp(optarg, "html")) {
107 output_format = FMT_HTML;
108 } else if (!strcmp(optarg, "csv")) {
109 output_format = FMT_CSV;
111 ovs_fatal(0, "unknown output format \"%s\"", optarg);
116 output_width = INT_MAX;
123 OVS_PRINT_VERSION(0, 0);
127 vlog_set_verbosity(optarg);
130 DAEMON_OPTION_HANDLERS
136 /* getopt_long() already set the value for us. */
149 printf("%s: Open vSwitch database JSON-RPC client\n"
150 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
151 "\nValid commands are:\n"
152 "\n get-schema SERVER\n"
153 " retrieve schema from SERVER\n"
154 "\n list-tables SERVER\n"
155 " list SERVER's tables\n"
156 "\n list-columns SERVER [TABLE]\n"
157 " list columns in TABLE (or all tables) on SERVER\n"
158 "\n transact SERVER TRANSACTION\n"
159 " run TRANSACTION (a JSON array of operations) on SERVER\n"
160 " and print the results as JSON on stdout\n"
161 "\n monitor SERVER TABLE [COLUMN,...] [SELECT,...]\n"
162 " monitor contents of (COLUMNs in) TABLE on SERVER\n"
163 " Valid SELECTs are: initial, insert, delete, modify\n",
164 program_name, program_name);
165 stream_usage("SERVER", true, true);
166 printf("\nOutput formatting options:\n"
167 " -f, --format=FORMAT set output formatting to FORMAT\n"
168 " (\"table\", \"html\", or \"csv\"\n"
169 " --wide don't limit TTY lines to 79 bytes\n"
170 " --no-headings omit table heading row\n"
171 " --pretty pretty-print JSON in output");
174 printf("\nOther options:\n"
175 " -h, --help display this help message\n"
176 " -V, --version display version information\n");
181 parse_json(const char *s)
183 struct json *json = json_from_string(s);
184 if (json->type == JSON_STRING) {
185 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
190 static struct jsonrpc *
191 open_jsonrpc(const char *server)
193 struct stream *stream;
196 error = stream_open_block(server, &stream);
197 if (error == EAFNOSUPPORT) {
198 struct pstream *pstream;
200 error = pstream_open(server, &pstream);
202 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
205 VLOG_INFO("%s: waiting for connection...", server);
206 error = pstream_accept_block(pstream, &stream);
208 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
211 pstream_close(pstream);
213 ovs_fatal(error, "failed to connect to \"%s\"", server);
216 return jsonrpc_open(stream);
220 print_json(struct json *json)
222 char *string = json_to_string(json, json_flags);
223 fputs(string, stdout);
228 print_and_free_json(struct json *json)
235 check_ovsdb_error(struct ovsdb_error *error)
238 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
242 static struct ovsdb_schema *
243 fetch_schema_from_rpc(struct jsonrpc *rpc)
245 struct jsonrpc_msg *request, *reply;
246 struct ovsdb_schema *schema;
249 request = jsonrpc_create_request("get_schema", json_array_create_empty(),
251 error = jsonrpc_transact_block(rpc, request, &reply);
253 ovs_fatal(error, "transaction failed");
255 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
256 jsonrpc_msg_destroy(reply);
261 static struct ovsdb_schema *
262 fetch_schema(const char *server)
264 struct ovsdb_schema *schema;
267 rpc = open_jsonrpc(server);
268 schema = fetch_schema_from_rpc(rpc);
281 struct column *columns;
282 size_t n_columns, allocated_columns;
283 size_t n_rows, allocated_rows;
284 size_t current_column;
288 table_init(struct table *table)
290 memset(table, 0, sizeof *table);
294 table_destroy(struct table *table)
298 for (i = 0; i < table->n_columns; i++) {
299 free(table->columns[i].heading);
301 free(table->columns);
303 for (i = 0; i < table->n_columns * table->n_rows; i++) {
304 free(table->cells[i]);
310 table_add_column(struct table *table, const char *heading, ...)
314 table_add_column(struct table *table, const char *heading, ...)
316 struct column *column;
319 assert(!table->n_rows);
320 if (table->n_columns >= table->allocated_columns) {
321 table->columns = x2nrealloc(table->columns, &table->allocated_columns,
322 sizeof *table->columns);
324 column = &table->columns[table->n_columns++];
326 va_start(args, heading);
327 column->heading = xvasprintf(heading, args);
328 column->width = strlen(column->heading);
333 table_cell__(const struct table *table, size_t row, size_t column)
335 return &table->cells[column + row * table->n_columns];
339 table_add_row(struct table *table)
343 if (table->n_rows >= table->allocated_rows) {
344 table->cells = x2nrealloc(table->cells, &table->allocated_rows,
345 table->n_columns * sizeof *table->cells);
349 table->current_column = 0;
350 for (x = 0; x < table->n_columns; x++) {
351 *table_cell__(table, y, x) = NULL;
356 table_add_cell_nocopy(struct table *table, char *s)
361 assert(table->n_rows > 0);
362 assert(table->current_column < table->n_columns);
364 x = table->current_column++;
365 y = table->n_rows - 1;
366 *table_cell__(table, y, x) = s;
369 if (length > table->columns[x].width) {
370 table->columns[x].width = length;
375 table_add_cell(struct table *table, const char *format, ...)
379 va_start(args, format);
380 table_add_cell_nocopy(table, xvasprintf(format, args));
385 table_print_table_line__(struct ds *line, size_t max_width)
387 ds_truncate(line, max_width);
393 table_print_table__(const struct table *table)
395 struct ds line = DS_EMPTY_INITIALIZER;
398 if (output_headings) {
399 for (x = 0; x < table->n_columns; x++) {
400 const struct column *column = &table->columns[x];
402 ds_put_char(&line, ' ');
404 ds_put_format(&line, "%-*s", column->width, column->heading);
406 table_print_table_line__(&line, output_width);
408 for (x = 0; x < table->n_columns; x++) {
409 const struct column *column = &table->columns[x];
413 ds_put_char(&line, ' ');
415 for (i = 0; i < column->width; i++) {
416 ds_put_char(&line, '-');
419 table_print_table_line__(&line, output_width);
422 for (y = 0; y < table->n_rows; y++) {
423 for (x = 0; x < table->n_columns; x++) {
424 const char *cell = *table_cell__(table, y, x);
426 ds_put_char(&line, ' ');
428 ds_put_format(&line, "%-*s", table->columns[x].width, cell);
430 table_print_table_line__(&line, output_width);
437 table_print_html_cell__(const char *element, const char *content)
441 printf(" <%s>", element);
442 for (p = content; *p != '\0'; p++) {
445 fputs("&", stdout);
448 fputs("<", stdout);
451 fputs(">", stdout);
458 printf("</%s>\n", element);
462 table_print_html__(const struct table *table)
466 fputs("<table>\n", stdout);
468 if (output_headings) {
469 fputs(" <tr>\n", stdout);
470 for (x = 0; x < table->n_columns; x++) {
471 const struct column *column = &table->columns[x];
472 table_print_html_cell__("th", column->heading);
474 fputs(" </tr>\n", stdout);
477 for (y = 0; y < table->n_rows; y++) {
478 fputs(" <tr>\n", stdout);
479 for (x = 0; x < table->n_columns; x++) {
480 table_print_html_cell__("td", *table_cell__(table, y, x));
482 fputs(" </tr>\n", stdout);
485 fputs("</table>\n", stdout);
489 table_print_csv_cell__(const char *content)
493 if (!strpbrk(content, "\n\",")) {
494 fputs(content, stdout);
497 for (p = content; *p != '\0'; p++) {
500 fputs("\"\"", stdout);
512 table_print_csv__(const struct table *table)
516 if (output_headings) {
517 for (x = 0; x < table->n_columns; x++) {
518 const struct column *column = &table->columns[x];
522 table_print_csv_cell__(column->heading);
527 for (y = 0; y < table->n_rows; y++) {
528 for (x = 0; x < table->n_columns; x++) {
532 table_print_csv_cell__(*table_cell__(table, y, x));
539 table_print(const struct table *table)
541 switch (output_format) {
543 table_print_table__(table);
547 table_print_html__(table);
551 table_print_csv__(table);
557 do_get_schema(int argc UNUSED, char *argv[])
559 struct ovsdb_schema *schema = fetch_schema(argv[1]);
560 print_and_free_json(ovsdb_schema_to_json(schema));
561 ovsdb_schema_destroy(schema);
565 do_list_tables(int argc UNUSED, char *argv[])
567 struct ovsdb_schema *schema;
568 struct shash_node *node;
571 schema = fetch_schema(argv[1]);
573 table_add_column(&t, "Table");
574 table_add_column(&t, "Comment");
575 SHASH_FOR_EACH (node, &schema->tables) {
576 struct ovsdb_table_schema *ts = node->data;
579 table_add_cell(&t, ts->name);
581 table_add_cell(&t, ts->comment);
584 ovsdb_schema_destroy(schema);
589 do_list_columns(int argc UNUSED, char *argv[])
591 const char *table_name = argv[2];
592 struct ovsdb_schema *schema;
593 struct shash_node *table_node;
596 schema = fetch_schema(argv[1]);
599 table_add_column(&t, "Table");
601 table_add_column(&t, "Column");
602 table_add_column(&t, "Type");
603 table_add_column(&t, "Comment");
604 SHASH_FOR_EACH (table_node, &schema->tables) {
605 struct ovsdb_table_schema *ts = table_node->data;
607 if (!table_name || !strcmp(table_name, ts->name)) {
608 struct shash_node *column_node;
610 SHASH_FOR_EACH (column_node, &ts->columns) {
611 struct ovsdb_column *column = column_node->data;
612 struct json *type = ovsdb_type_to_json(&column->type);
616 table_add_cell(&t, ts->name);
618 table_add_cell(&t, column->name);
619 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
620 if (column->comment) {
621 table_add_cell(&t, column->comment);
628 ovsdb_schema_destroy(schema);
633 do_transact(int argc UNUSED, char *argv[])
635 struct jsonrpc_msg *request, *reply;
636 struct json *transaction;
640 transaction = parse_json(argv[2]);
642 rpc = open_jsonrpc(argv[1]);
643 request = jsonrpc_create_request("transact", transaction, NULL);
644 error = jsonrpc_transact_block(rpc, request, &reply);
646 ovs_fatal(error, "transaction failed");
649 ovs_fatal(error, "transaction returned error: %s",
650 json_to_string(reply->error, json_flags));
652 print_json(reply->result);
654 jsonrpc_msg_destroy(reply);
659 monitor_print_row(struct json *row, const char *type, const char *uuid,
660 const struct ovsdb_column_set *columns, struct table *t)
665 ovs_error(0, "missing %s row", type);
667 } else if (row->type != JSON_OBJECT) {
668 ovs_error(0, "<row> is not object");
673 table_add_cell(t, uuid);
674 table_add_cell(t, type);
675 for (i = 0; i < columns->n_columns; i++) {
676 const struct ovsdb_column *column = columns->columns[i];
677 struct json *value = shash_find_data(json_object(row), column->name);
679 table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
681 table_add_cell(t, "");
687 monitor_print(struct json *table_updates,
688 const struct ovsdb_table_schema *table,
689 const struct ovsdb_column_set *columns, bool initial)
691 struct json *table_update;
692 struct shash_node *node;
698 if (table_updates->type != JSON_OBJECT) {
699 ovs_error(0, "<table-updates> is not object");
702 table_update = shash_find_data(json_object(table_updates), table->name);
706 if (table_update->type != JSON_OBJECT) {
707 ovs_error(0, "<table-update> is not object");
711 table_add_column(&t, "row");
712 table_add_column(&t, "action");
713 for (i = 0; i < columns->n_columns; i++) {
714 table_add_column(&t, "%s", columns->columns[i]->name);
716 SHASH_FOR_EACH (node, json_object(table_update)) {
717 struct json *row_update = node->data;
718 struct json *old, *new;
720 if (row_update->type != JSON_OBJECT) {
721 ovs_error(0, "<row-update> is not object");
724 old = shash_find_data(json_object(row_update), "old");
725 new = shash_find_data(json_object(row_update), "new");
727 monitor_print_row(new, "initial", node->name, columns, &t);
729 monitor_print_row(new, "insert", node->name, columns, &t);
731 monitor_print_row(old, "delete", node->name, columns, &t);
733 monitor_print_row(old, "old", node->name, columns, &t);
734 monitor_print_row(new, "new", "", columns, &t);
742 do_monitor(int argc, char *argv[])
744 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
745 struct ovsdb_table_schema *table;
746 struct ovsdb_schema *schema;
747 struct jsonrpc_msg *request;
749 struct json *select, *monitor, *monitor_request, *monitor_requests,
752 rpc = open_jsonrpc(argv[1]);
754 schema = fetch_schema_from_rpc(rpc);
755 table = shash_find_data(&schema->tables, argv[2]);
757 ovs_fatal(0, "%s: no table named \"%s\"", argv[1], argv[2]);
760 if (argc >= 4 && *argv[3] != '\0') {
761 char *save_ptr = NULL;
764 for (token = strtok_r(argv[3], ",", &save_ptr); token != NULL;
765 token = strtok_r(NULL, ",", &save_ptr)) {
766 const struct ovsdb_column *column;
767 column = ovsdb_table_schema_get_column(table, token);
769 ovs_fatal(0, "%s: table \"%s\" does not have a "
770 "column named \"%s\"", argv[1], argv[2], token);
772 ovsdb_column_set_add(&columns, column);
775 struct shash_node *node;
777 SHASH_FOR_EACH (node, &table->columns) {
778 const struct ovsdb_column *column = node->data;
779 if (column->index != OVSDB_COL_UUID) {
780 ovsdb_column_set_add(&columns, column);
785 if (argc >= 5 && *argv[4] != '\0') {
786 char *save_ptr = NULL;
789 select = json_object_create();
790 for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
791 token = strtok_r(NULL, ",", &save_ptr)) {
792 json_object_put(select, token, json_boolean_create(true));
798 monitor_request = json_object_create();
799 json_object_put(monitor_request,
800 "columns", ovsdb_column_set_to_json(&columns));
802 json_object_put(monitor_request, "select", select);
805 monitor_requests = json_object_create();
806 json_object_put(monitor_requests, argv[2], monitor_request);
808 monitor = json_array_create_2(json_null_create(), monitor_requests);
809 request = jsonrpc_create_request("monitor", monitor, NULL);
810 request_id = json_clone(request->id);
811 jsonrpc_send(rpc, request);
813 struct jsonrpc_msg *msg;
816 error = jsonrpc_recv_block(rpc, &msg);
818 ovs_fatal(error, "%s: receive failed", argv[1]);
821 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
822 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
824 } else if (msg->type == JSONRPC_REPLY
825 && json_equal(msg->id, request_id)) {
826 monitor_print(msg->result, table, &columns, true);
829 } else if (msg->type == JSONRPC_NOTIFY
830 && !strcmp(msg->method, "update")) {
831 struct json *params = msg->params;
832 if (params->type == JSON_ARRAY
833 && params->u.array.n == 2
834 && params->u.array.elems[0]->type == JSON_NULL) {
835 monitor_print(params->u.array.elems[1],
836 table, &columns, false);
844 do_help(int argc UNUSED, char *argv[] UNUSED)
849 static const struct command all_commands[] = {
850 { "get-schema", 1, 1, do_get_schema },
851 { "list-tables", 1, 1, do_list_tables },
852 { "list-columns", 1, 2, do_list_columns },
853 { "transact", 2, 2, do_transact },
854 { "monitor", 2, 4, do_monitor },
855 { "help", 0, INT_MAX, do_help },
856 { NULL, 0, 0, NULL },