2 * Copyright (c) 2009, 2010, 2011 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"
35 #include "lib/table.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-error.h"
41 #include "stream-ssl.h"
47 VLOG_DEFINE_THIS_MODULE(ovsdb_client);
49 /* Format for table output. */
50 static struct table_style table_style = TABLE_STYLE_DEFAULT;
52 static const struct command all_commands[];
54 static void usage(void) NO_RETURN;
55 static void parse_options(int argc, char *argv[]);
58 main(int argc, char *argv[])
60 proctitle_init(argc, argv);
61 set_program_name(argv[0]);
62 parse_options(argc, argv);
63 signal(SIGPIPE, SIG_IGN);
64 run_command(argc - optind, argv + optind, all_commands);
69 parse_options(int argc, char *argv[])
72 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
76 static struct option long_options[] = {
77 {"verbose", optional_argument, 0, 'v'},
78 {"help", no_argument, 0, 'h'},
79 {"version", no_argument, 0, 'V'},
82 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
84 STREAM_SSL_LONG_OPTIONS
88 char *short_options = long_options_to_short_options(long_options);
93 c = getopt_long(argc, argv, short_options, long_options, NULL);
103 OVS_PRINT_VERSION(0, 0);
107 vlog_set_verbosity(optarg);
110 DAEMON_OPTION_HANDLERS
112 TABLE_OPTION_HANDLERS(&table_style)
115 STREAM_SSL_OPTION_HANDLERS
117 case OPT_BOOTSTRAP_CA_CERT:
118 stream_ssl_set_ca_cert_file(optarg, true);
126 /* getopt_long() already set the value for us. */
139 printf("%s: Open vSwitch database JSON-RPC client\n"
140 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
141 "\nValid commands are:\n"
142 "\n list-dbs SERVER\n"
143 " list databases available on SERVER\n"
144 "\n get-schema SERVER DATABASE\n"
145 " retrieve schema for DATABASE from SERVER\n"
146 "\n get-schema-version SERVER DATABASE\n"
147 " retrieve schema for DATABASE from SERVER and report only its\n"
148 " version number on stdout\n"
149 "\n list-tables SERVER DATABASE\n"
150 " list tables for DATABASE on SERVER\n"
151 "\n list-columns SERVER DATABASE [TABLE]\n"
152 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
153 "\n transact SERVER TRANSACTION\n"
154 " run TRANSACTION (a JSON array of operations) on SERVER\n"
155 " and print the results as JSON on stdout\n"
156 "\n monitor SERVER DATABASE TABLE [COLUMN,...]...\n"
157 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
158 " COLUMNs may include !initial, !insert, !delete, !modify\n"
159 " to avoid seeing the specified kinds of changes.\n"
160 "\n dump SERVER DATABASE\n"
161 " dump contents of DATABASE on SERVER to stdout\n",
162 program_name, program_name);
163 stream_usage("SERVER", true, true, true);
164 printf("\nOutput formatting options:\n"
165 " -f, --format=FORMAT set output formatting to FORMAT\n"
166 " (\"table\", \"html\", \"csv\", "
168 " --no-headings omit table heading row\n"
169 " --pretty pretty-print JSON in output");
172 printf("\nOther options:\n"
173 " -h, --help display this help message\n"
174 " -V, --version display version information\n");
179 parse_json(const char *s)
181 struct json *json = json_from_string(s);
182 if (json->type == JSON_STRING) {
183 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
188 static struct jsonrpc *
189 open_jsonrpc(const char *server)
191 struct stream *stream;
194 error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
195 if (error == EAFNOSUPPORT) {
196 struct pstream *pstream;
198 error = jsonrpc_pstream_open(server, &pstream);
200 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
203 VLOG_INFO("%s: waiting for connection...", server);
204 error = pstream_accept_block(pstream, &stream);
206 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
209 pstream_close(pstream);
211 ovs_fatal(error, "failed to connect to \"%s\"", server);
214 return jsonrpc_open(stream);
218 print_json(struct json *json)
220 char *string = json_to_string(json, table_style.json_flags);
221 fputs(string, stdout);
226 print_and_free_json(struct json *json)
233 check_ovsdb_error(struct ovsdb_error *error)
236 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
240 static struct ovsdb_schema *
241 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
243 struct jsonrpc_msg *request, *reply;
244 struct ovsdb_schema *schema;
247 request = jsonrpc_create_request("get_schema",
249 json_string_create(database)),
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, const char *database)
264 struct ovsdb_schema *schema;
267 rpc = open_jsonrpc(server);
268 schema = fetch_schema_from_rpc(rpc, database);
276 do_list_dbs(int argc OVS_UNUSED, char *argv[])
278 struct jsonrpc_msg *request, *reply;
283 rpc = open_jsonrpc(argv[1]);
284 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
286 error = jsonrpc_transact_block(rpc, request, &reply);
288 ovs_fatal(error, "transaction failed");
291 if (reply->result->type != JSON_ARRAY) {
292 ovs_fatal(0, "list_dbs response is not array");
295 for (i = 0; i < reply->result->u.array.n; i++) {
296 const struct json *name = reply->result->u.array.elems[i];
298 if (name->type != JSON_STRING) {
299 ovs_fatal(0, "list_dbs response %zu is not string", i);
301 puts(name->u.string);
303 jsonrpc_msg_destroy(reply);
307 do_get_schema(int argc OVS_UNUSED, char *argv[])
309 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
310 print_and_free_json(ovsdb_schema_to_json(schema));
311 ovsdb_schema_destroy(schema);
315 do_get_schema_version(int argc OVS_UNUSED, char *argv[])
317 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
318 puts(schema->version);
319 ovsdb_schema_destroy(schema);
323 do_list_tables(int argc OVS_UNUSED, char *argv[])
325 struct ovsdb_schema *schema;
326 struct shash_node *node;
329 schema = fetch_schema(argv[1], argv[2]);
331 table_add_column(&t, "Table");
332 SHASH_FOR_EACH (node, &schema->tables) {
333 struct ovsdb_table_schema *ts = node->data;
336 table_add_cell(&t)->text = xstrdup(ts->name);
338 ovsdb_schema_destroy(schema);
339 table_print(&t, &table_style);
343 do_list_columns(int argc OVS_UNUSED, char *argv[])
345 const char *table_name = argv[3];
346 struct ovsdb_schema *schema;
347 struct shash_node *table_node;
350 schema = fetch_schema(argv[1], argv[2]);
353 table_add_column(&t, "Table");
355 table_add_column(&t, "Column");
356 table_add_column(&t, "Type");
357 SHASH_FOR_EACH (table_node, &schema->tables) {
358 struct ovsdb_table_schema *ts = table_node->data;
360 if (!table_name || !strcmp(table_name, ts->name)) {
361 struct shash_node *column_node;
363 SHASH_FOR_EACH (column_node, &ts->columns) {
364 const struct ovsdb_column *column = column_node->data;
368 table_add_cell(&t)->text = xstrdup(ts->name);
370 table_add_cell(&t)->text = xstrdup(column->name);
371 table_add_cell(&t)->json = ovsdb_type_to_json(&column->type);
375 ovsdb_schema_destroy(schema);
376 table_print(&t, &table_style);
380 do_transact(int argc OVS_UNUSED, char *argv[])
382 struct jsonrpc_msg *request, *reply;
383 struct json *transaction;
387 transaction = parse_json(argv[2]);
389 rpc = open_jsonrpc(argv[1]);
390 request = jsonrpc_create_request("transact", transaction, NULL);
391 error = jsonrpc_transact_block(rpc, request, &reply);
393 ovs_fatal(error, "transaction failed");
396 ovs_fatal(error, "transaction returned error: %s",
397 json_to_string(reply->error, table_style.json_flags));
399 print_json(reply->result);
401 jsonrpc_msg_destroy(reply);
406 monitor_print_row(struct json *row, const char *type, const char *uuid,
407 const struct ovsdb_column_set *columns, struct table *t)
412 ovs_error(0, "missing %s row", type);
414 } else if (row->type != JSON_OBJECT) {
415 ovs_error(0, "<row> is not object");
420 table_add_cell(t)->text = xstrdup(uuid);
421 table_add_cell(t)->text = xstrdup(type);
422 for (i = 0; i < columns->n_columns; i++) {
423 const struct ovsdb_column *column = columns->columns[i];
424 struct json *value = shash_find_data(json_object(row), column->name);
425 struct cell *cell = table_add_cell(t);
427 cell->json = json_clone(value);
428 cell->type = &column->type;
434 monitor_print(struct json *table_updates,
435 const struct ovsdb_table_schema *table,
436 const struct ovsdb_column_set *columns, bool initial)
438 struct json *table_update;
439 struct shash_node *node;
445 if (table_updates->type != JSON_OBJECT) {
446 ovs_error(0, "<table-updates> is not object");
449 table_update = shash_find_data(json_object(table_updates), table->name);
453 if (table_update->type != JSON_OBJECT) {
454 ovs_error(0, "<table-update> is not object");
458 table_add_column(&t, "row");
459 table_add_column(&t, "action");
460 for (i = 0; i < columns->n_columns; i++) {
461 table_add_column(&t, "%s", columns->columns[i]->name);
463 SHASH_FOR_EACH (node, json_object(table_update)) {
464 struct json *row_update = node->data;
465 struct json *old, *new;
467 if (row_update->type != JSON_OBJECT) {
468 ovs_error(0, "<row-update> is not object");
471 old = shash_find_data(json_object(row_update), "old");
472 new = shash_find_data(json_object(row_update), "new");
474 monitor_print_row(new, "initial", node->name, columns, &t);
476 monitor_print_row(new, "insert", node->name, columns, &t);
478 monitor_print_row(old, "delete", node->name, columns, &t);
480 monitor_print_row(old, "old", node->name, columns, &t);
481 monitor_print_row(new, "new", "", columns, &t);
484 table_print(&t, &table_style);
489 add_column(const char *server, const struct ovsdb_column *column,
490 struct ovsdb_column_set *columns, struct json *columns_json)
492 if (ovsdb_column_set_contains(columns, column->index)) {
493 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
494 server, column->name);
496 ovsdb_column_set_add(columns, column);
497 json_array_add(columns_json, json_string_create(column->name));
501 parse_monitor_columns(char *arg, const char *server, const char *database,
502 const struct ovsdb_table_schema *table,
503 struct ovsdb_column_set *columns)
505 bool initial, insert, delete, modify;
506 struct json *mr, *columns_json;
507 char *save_ptr = NULL;
510 mr = json_object_create();
511 columns_json = json_array_create_empty();
512 json_object_put(mr, "columns", columns_json);
514 initial = insert = delete = modify = true;
515 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
516 token = strtok_r(NULL, ",", &save_ptr)) {
517 if (!strcmp(token, "!initial")) {
519 } else if (!strcmp(token, "!insert")) {
521 } else if (!strcmp(token, "!delete")) {
523 } else if (!strcmp(token, "!modify")) {
526 const struct ovsdb_column *column;
528 column = ovsdb_table_schema_get_column(table, token);
530 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
531 "column named \"%s\"",
532 server, table->name, database, token);
534 add_column(server, column, columns, columns_json);
538 if (columns_json->u.array.n == 0) {
539 const struct shash_node **nodes;
542 n = shash_count(&table->columns);
543 nodes = shash_sort(&table->columns);
544 for (i = 0; i < n; i++) {
545 const struct ovsdb_column *column = nodes[i]->data;
546 if (column->index != OVSDB_COL_UUID
547 && column->index != OVSDB_COL_VERSION) {
548 add_column(server, column, columns, columns_json);
553 add_column(server, ovsdb_table_schema_get_column(table,"_version"),
554 columns, columns_json);
557 if (!initial || !insert || !delete || !modify) {
558 struct json *select = json_object_create();
559 json_object_put(select, "initial", json_boolean_create(initial));
560 json_object_put(select, "insert", json_boolean_create(insert));
561 json_object_put(select, "delete", json_boolean_create(delete));
562 json_object_put(select, "modify", json_boolean_create(modify));
563 json_object_put(mr, "select", select);
570 do_monitor(int argc, char *argv[])
572 const char *server = argv[1];
573 const char *database = argv[2];
574 const char *table_name = argv[3];
575 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
576 struct ovsdb_table_schema *table;
577 struct ovsdb_schema *schema;
578 struct jsonrpc_msg *request;
580 struct json *monitor, *monitor_request_array,
581 *monitor_requests, *request_id;
583 rpc = open_jsonrpc(server);
585 schema = fetch_schema_from_rpc(rpc, database);
586 table = shash_find_data(&schema->tables, table_name);
588 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
589 server, database, table_name);
592 monitor_request_array = json_array_create_empty();
596 for (i = 4; i < argc; i++) {
598 monitor_request_array,
599 parse_monitor_columns(argv[i], server, database, table,
603 /* Allocate a writable empty string since parse_monitor_columns() is
604 * going to strtok() it and that's risky with literal "". */
607 monitor_request_array,
608 parse_monitor_columns(empty, server, database, table, &columns));
611 monitor_requests = json_object_create();
612 json_object_put(monitor_requests, table_name, monitor_request_array);
614 monitor = json_array_create_3(json_string_create(database),
615 json_null_create(), monitor_requests);
616 request = jsonrpc_create_request("monitor", monitor, NULL);
617 request_id = json_clone(request->id);
618 jsonrpc_send(rpc, request);
620 struct jsonrpc_msg *msg;
623 error = jsonrpc_recv_block(rpc, &msg);
625 ovsdb_schema_destroy(schema);
626 ovs_fatal(error, "%s: receive failed", server);
629 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
630 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
632 } else if (msg->type == JSONRPC_REPLY
633 && json_equal(msg->id, request_id)) {
634 monitor_print(msg->result, table, &columns, true);
637 /* daemonize() closes the standard file descriptors. We output
638 * to stdout, so we need to save and restore STDOUT_FILENO. */
639 int fd = dup(STDOUT_FILENO);
641 dup2(fd, STDOUT_FILENO);
644 } else if (msg->type == JSONRPC_NOTIFY
645 && !strcmp(msg->method, "update")) {
646 struct json *params = msg->params;
647 if (params->type == JSON_ARRAY
648 && params->u.array.n == 2
649 && params->u.array.elems[0]->type == JSON_NULL) {
650 monitor_print(params->u.array.elems[1],
651 table, &columns, false);
655 jsonrpc_msg_destroy(msg);
659 struct dump_table_aux {
660 struct ovsdb_datum **data;
661 const struct ovsdb_column **columns;
666 compare_data(size_t a_y, size_t b_y, size_t x,
667 const struct dump_table_aux *aux)
669 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
671 &aux->columns[x]->type);
675 compare_rows(size_t a_y, size_t b_y, void *aux_)
677 struct dump_table_aux *aux = aux_;
680 /* Skip UUID columns on the first pass, since their values tend to be
681 * random and make our results less reproducible. */
682 for (x = 0; x < aux->n_columns; x++) {
683 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
684 int cmp = compare_data(a_y, b_y, x, aux);
691 /* Use UUID columns as tie-breakers. */
692 for (x = 0; x < aux->n_columns; x++) {
693 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
694 int cmp = compare_data(a_y, b_y, x, aux);
705 swap_rows(size_t a_y, size_t b_y, void *aux_)
707 struct dump_table_aux *aux = aux_;
708 struct ovsdb_datum *tmp = aux->data[a_y];
709 aux->data[a_y] = aux->data[b_y];
710 aux->data[b_y] = tmp;
714 compare_columns(const void *a_, const void *b_)
716 const struct ovsdb_column *const *ap = a_;
717 const struct ovsdb_column *const *bp = b_;
718 const struct ovsdb_column *a = *ap;
719 const struct ovsdb_column *b = *bp;
721 return strcmp(a->name, b->name);
725 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
727 const struct ovsdb_column **columns;
730 struct ovsdb_datum **data;
732 struct dump_table_aux aux;
733 struct shash_node *node;
737 /* Sort columns by name, for reproducibility. */
738 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
740 SHASH_FOR_EACH (node, &ts->columns) {
741 struct ovsdb_column *column = node->data;
742 if (strcmp(column->name, "_version")) {
743 columns[n_columns++] = column;
746 qsort(columns, n_columns, sizeof *columns, compare_columns);
748 /* Extract data from table. */
749 data = xmalloc(rows->n * sizeof *data);
750 for (y = 0; y < rows->n; y++) {
753 if (rows->elems[y]->type != JSON_OBJECT) {
754 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
755 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
757 row = json_object(rows->elems[y]);
759 data[y] = xmalloc(n_columns * sizeof **data);
760 for (x = 0; x < n_columns; x++) {
761 const struct json *json = shash_find_data(row, columns[x]->name);
763 ovs_fatal(0, "row %zu in table %s response lacks %s column",
764 y, ts->name, columns[x]->name);
767 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
773 /* Sort rows by column values, for reproducibility. */
775 aux.columns = columns;
776 aux.n_columns = n_columns;
777 sort(rows->n, compare_rows, swap_rows, &aux);
779 /* Add column headings. */
781 table_set_caption(&t, xasprintf("%s table", ts->name));
782 for (x = 0; x < n_columns; x++) {
783 table_add_column(&t, "%s", columns[x]->name);
787 for (y = 0; y < rows->n; y++) {
789 for (x = 0; x < n_columns; x++) {
790 struct cell *cell = table_add_cell(&t);
791 cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
792 cell->type = &columns[x]->type;
795 table_print(&t, &table_style);
800 do_dump(int argc OVS_UNUSED, char *argv[])
802 const char *server = argv[1];
803 const char *database = argv[2];
805 struct jsonrpc_msg *request, *reply;
806 struct ovsdb_schema *schema;
807 struct json *transaction;
811 const struct shash_node **tables;
816 rpc = open_jsonrpc(server);
818 schema = fetch_schema_from_rpc(rpc, database);
819 tables = shash_sort(&schema->tables);
820 n_tables = shash_count(&schema->tables);
822 /* Construct transaction to retrieve entire database. */
823 transaction = json_array_create_1(json_string_create(database));
824 for (i = 0; i < n_tables; i++) {
825 const struct ovsdb_table_schema *ts = tables[i]->data;
826 struct json *op, *columns;
827 struct shash_node *node;
829 columns = json_array_create_empty();
830 SHASH_FOR_EACH (node, &ts->columns) {
831 const struct ovsdb_column *column = node->data;
833 if (strcmp(column->name, "_version")) {
834 json_array_add(columns, json_string_create(column->name));
838 op = json_object_create();
839 json_object_put_string(op, "op", "select");
840 json_object_put_string(op, "table", tables[i]->name);
841 json_object_put(op, "where", json_array_create_empty());
842 json_object_put(op, "columns", columns);
843 json_array_add(transaction, op);
846 /* Send request, get reply. */
847 request = jsonrpc_create_request("transact", transaction, NULL);
848 error = jsonrpc_transact_block(rpc, request, &reply);
850 ovs_fatal(error, "transaction failed");
853 /* Print database contents. */
854 if (reply->result->type != JSON_ARRAY
855 || reply->result->u.array.n != n_tables) {
856 ovs_fatal(0, "reply is not array of %zu elements: %s",
857 n_tables, json_to_string(reply->result, 0));
859 for (i = 0; i < n_tables; i++) {
860 const struct ovsdb_table_schema *ts = tables[i]->data;
861 const struct json *op_result = reply->result->u.array.elems[i];
864 if (op_result->type != JSON_OBJECT
865 || !(rows = shash_find_data(json_object(op_result), "rows"))
866 || rows->type != JSON_ARRAY) {
867 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
869 ts->name, json_to_string(op_result, 0));
872 dump_table(ts, &rows->u.array);
877 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
882 static const struct command all_commands[] = {
883 { "list-dbs", 1, 1, do_list_dbs },
884 { "get-schema", 2, 2, do_get_schema },
885 { "get-schema-version", 2, 2, do_get_schema_version },
886 { "list-tables", 2, 2, do_list_tables },
887 { "list-columns", 2, 3, do_list_columns },
888 { "transact", 2, 2, do_transact },
889 { "monitor", 3, INT_MAX, do_monitor },
890 { "dump", 2, 2, do_dump },
891 { "help", 0, INT_MAX, do_help },
892 { NULL, 0, 0, NULL },