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, NULL, 'v'},
78 {"help", no_argument, NULL, 'h'},
79 {"version", no_argument, NULL, 'V'},
82 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
83 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)
114 STREAM_SSL_OPTION_HANDLERS
116 case OPT_BOOTSTRAP_CA_CERT:
117 stream_ssl_set_ca_cert_file(optarg, true);
124 /* getopt_long() already set the value for us. */
137 printf("%s: Open vSwitch database JSON-RPC client\n"
138 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
139 "\nValid commands are:\n"
140 "\n list-dbs SERVER\n"
141 " list databases available on SERVER\n"
142 "\n get-schema SERVER DATABASE\n"
143 " retrieve schema for DATABASE from SERVER\n"
144 "\n get-schema-version SERVER DATABASE\n"
145 " retrieve schema for DATABASE from SERVER and report only its\n"
146 " version number on stdout\n"
147 "\n list-tables SERVER DATABASE\n"
148 " list tables for DATABASE on SERVER\n"
149 "\n list-columns SERVER DATABASE [TABLE]\n"
150 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
151 "\n transact SERVER TRANSACTION\n"
152 " run TRANSACTION (a JSON array of operations) on SERVER\n"
153 " and print the results as JSON on stdout\n"
154 "\n monitor SERVER DATABASE TABLE [COLUMN,...]...\n"
155 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
156 " COLUMNs may include !initial, !insert, !delete, !modify\n"
157 " to avoid seeing the specified kinds of changes.\n"
158 "\n dump SERVER DATABASE\n"
159 " dump contents of DATABASE on SERVER to stdout\n",
160 program_name, program_name);
161 stream_usage("SERVER", true, true, true);
162 printf("\nOutput formatting options:\n"
163 " -f, --format=FORMAT set output formatting to FORMAT\n"
164 " (\"table\", \"html\", \"csv\", "
166 " --no-headings omit table heading row\n"
167 " --pretty pretty-print JSON in output");
170 printf("\nOther options:\n"
171 " -h, --help display this help message\n"
172 " -V, --version display version information\n");
177 parse_json(const char *s)
179 struct json *json = json_from_string(s);
180 if (json->type == JSON_STRING) {
181 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
186 static struct jsonrpc *
187 open_jsonrpc(const char *server)
189 struct stream *stream;
192 error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
193 if (error == EAFNOSUPPORT) {
194 struct pstream *pstream;
196 error = jsonrpc_pstream_open(server, &pstream);
198 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
201 VLOG_INFO("%s: waiting for connection...", server);
202 error = pstream_accept_block(pstream, &stream);
204 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
207 pstream_close(pstream);
209 ovs_fatal(error, "failed to connect to \"%s\"", server);
212 return jsonrpc_open(stream);
216 print_json(struct json *json)
218 char *string = json_to_string(json, table_style.json_flags);
219 fputs(string, stdout);
224 print_and_free_json(struct json *json)
231 check_ovsdb_error(struct ovsdb_error *error)
234 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
238 static struct ovsdb_schema *
239 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
241 struct jsonrpc_msg *request, *reply;
242 struct ovsdb_schema *schema;
245 request = jsonrpc_create_request("get_schema",
247 json_string_create(database)),
249 error = jsonrpc_transact_block(rpc, request, &reply);
251 ovs_fatal(error, "transaction failed");
253 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
254 jsonrpc_msg_destroy(reply);
259 static struct ovsdb_schema *
260 fetch_schema(const char *server, const char *database)
262 struct ovsdb_schema *schema;
265 rpc = open_jsonrpc(server);
266 schema = fetch_schema_from_rpc(rpc, database);
274 do_list_dbs(int argc OVS_UNUSED, char *argv[])
276 struct jsonrpc_msg *request, *reply;
281 rpc = open_jsonrpc(argv[1]);
282 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
284 error = jsonrpc_transact_block(rpc, request, &reply);
286 ovs_fatal(error, "transaction failed");
289 if (reply->result->type != JSON_ARRAY) {
290 ovs_fatal(0, "list_dbs response is not array");
293 for (i = 0; i < reply->result->u.array.n; i++) {
294 const struct json *name = reply->result->u.array.elems[i];
296 if (name->type != JSON_STRING) {
297 ovs_fatal(0, "list_dbs response %zu is not string", i);
299 puts(name->u.string);
301 jsonrpc_msg_destroy(reply);
305 do_get_schema(int argc OVS_UNUSED, char *argv[])
307 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
308 print_and_free_json(ovsdb_schema_to_json(schema));
309 ovsdb_schema_destroy(schema);
313 do_get_schema_version(int argc OVS_UNUSED, char *argv[])
315 struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
316 puts(schema->version);
317 ovsdb_schema_destroy(schema);
321 do_list_tables(int argc OVS_UNUSED, char *argv[])
323 struct ovsdb_schema *schema;
324 struct shash_node *node;
327 schema = fetch_schema(argv[1], argv[2]);
329 table_add_column(&t, "Table");
330 SHASH_FOR_EACH (node, &schema->tables) {
331 struct ovsdb_table_schema *ts = node->data;
334 table_add_cell(&t)->text = xstrdup(ts->name);
336 ovsdb_schema_destroy(schema);
337 table_print(&t, &table_style);
341 do_list_columns(int argc OVS_UNUSED, char *argv[])
343 const char *table_name = argv[3];
344 struct ovsdb_schema *schema;
345 struct shash_node *table_node;
348 schema = fetch_schema(argv[1], argv[2]);
351 table_add_column(&t, "Table");
353 table_add_column(&t, "Column");
354 table_add_column(&t, "Type");
355 SHASH_FOR_EACH (table_node, &schema->tables) {
356 struct ovsdb_table_schema *ts = table_node->data;
358 if (!table_name || !strcmp(table_name, ts->name)) {
359 struct shash_node *column_node;
361 SHASH_FOR_EACH (column_node, &ts->columns) {
362 const struct ovsdb_column *column = column_node->data;
366 table_add_cell(&t)->text = xstrdup(ts->name);
368 table_add_cell(&t)->text = xstrdup(column->name);
369 table_add_cell(&t)->json = ovsdb_type_to_json(&column->type);
373 ovsdb_schema_destroy(schema);
374 table_print(&t, &table_style);
378 do_transact(int argc OVS_UNUSED, char *argv[])
380 struct jsonrpc_msg *request, *reply;
381 struct json *transaction;
385 transaction = parse_json(argv[2]);
387 rpc = open_jsonrpc(argv[1]);
388 request = jsonrpc_create_request("transact", transaction, NULL);
389 error = jsonrpc_transact_block(rpc, request, &reply);
391 ovs_fatal(error, "transaction failed");
394 ovs_fatal(error, "transaction returned error: %s",
395 json_to_string(reply->error, table_style.json_flags));
397 print_json(reply->result);
399 jsonrpc_msg_destroy(reply);
404 monitor_print_row(struct json *row, const char *type, const char *uuid,
405 const struct ovsdb_column_set *columns, struct table *t)
410 ovs_error(0, "missing %s row", type);
412 } else if (row->type != JSON_OBJECT) {
413 ovs_error(0, "<row> is not object");
418 table_add_cell(t)->text = xstrdup(uuid);
419 table_add_cell(t)->text = xstrdup(type);
420 for (i = 0; i < columns->n_columns; i++) {
421 const struct ovsdb_column *column = columns->columns[i];
422 struct json *value = shash_find_data(json_object(row), column->name);
423 struct cell *cell = table_add_cell(t);
425 cell->json = json_clone(value);
426 cell->type = &column->type;
432 monitor_print(struct json *table_updates,
433 const struct ovsdb_table_schema *table,
434 const struct ovsdb_column_set *columns, bool initial)
436 struct json *table_update;
437 struct shash_node *node;
443 if (table_updates->type != JSON_OBJECT) {
444 ovs_error(0, "<table-updates> is not object");
447 table_update = shash_find_data(json_object(table_updates), table->name);
451 if (table_update->type != JSON_OBJECT) {
452 ovs_error(0, "<table-update> is not object");
456 table_add_column(&t, "row");
457 table_add_column(&t, "action");
458 for (i = 0; i < columns->n_columns; i++) {
459 table_add_column(&t, "%s", columns->columns[i]->name);
461 SHASH_FOR_EACH (node, json_object(table_update)) {
462 struct json *row_update = node->data;
463 struct json *old, *new;
465 if (row_update->type != JSON_OBJECT) {
466 ovs_error(0, "<row-update> is not object");
469 old = shash_find_data(json_object(row_update), "old");
470 new = shash_find_data(json_object(row_update), "new");
472 monitor_print_row(new, "initial", node->name, columns, &t);
474 monitor_print_row(new, "insert", node->name, columns, &t);
476 monitor_print_row(old, "delete", node->name, columns, &t);
478 monitor_print_row(old, "old", node->name, columns, &t);
479 monitor_print_row(new, "new", "", columns, &t);
482 table_print(&t, &table_style);
487 add_column(const char *server, const struct ovsdb_column *column,
488 struct ovsdb_column_set *columns, struct json *columns_json)
490 if (ovsdb_column_set_contains(columns, column->index)) {
491 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
492 server, column->name);
494 ovsdb_column_set_add(columns, column);
495 json_array_add(columns_json, json_string_create(column->name));
499 parse_monitor_columns(char *arg, const char *server, const char *database,
500 const struct ovsdb_table_schema *table,
501 struct ovsdb_column_set *columns)
503 bool initial, insert, delete, modify;
504 struct json *mr, *columns_json;
505 char *save_ptr = NULL;
508 mr = json_object_create();
509 columns_json = json_array_create_empty();
510 json_object_put(mr, "columns", columns_json);
512 initial = insert = delete = modify = true;
513 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
514 token = strtok_r(NULL, ",", &save_ptr)) {
515 if (!strcmp(token, "!initial")) {
517 } else if (!strcmp(token, "!insert")) {
519 } else if (!strcmp(token, "!delete")) {
521 } else if (!strcmp(token, "!modify")) {
524 const struct ovsdb_column *column;
526 column = ovsdb_table_schema_get_column(table, token);
528 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
529 "column named \"%s\"",
530 server, table->name, database, token);
532 add_column(server, column, columns, columns_json);
536 if (columns_json->u.array.n == 0) {
537 const struct shash_node **nodes;
540 n = shash_count(&table->columns);
541 nodes = shash_sort(&table->columns);
542 for (i = 0; i < n; i++) {
543 const struct ovsdb_column *column = nodes[i]->data;
544 if (column->index != OVSDB_COL_UUID
545 && column->index != OVSDB_COL_VERSION) {
546 add_column(server, column, columns, columns_json);
551 add_column(server, ovsdb_table_schema_get_column(table,"_version"),
552 columns, columns_json);
555 if (!initial || !insert || !delete || !modify) {
556 struct json *select = json_object_create();
557 json_object_put(select, "initial", json_boolean_create(initial));
558 json_object_put(select, "insert", json_boolean_create(insert));
559 json_object_put(select, "delete", json_boolean_create(delete));
560 json_object_put(select, "modify", json_boolean_create(modify));
561 json_object_put(mr, "select", select);
568 do_monitor(int argc, char *argv[])
570 const char *server = argv[1];
571 const char *database = argv[2];
572 const char *table_name = argv[3];
573 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
574 struct ovsdb_table_schema *table;
575 struct ovsdb_schema *schema;
576 struct jsonrpc_msg *request;
578 struct json *monitor, *monitor_request_array,
579 *monitor_requests, *request_id;
581 rpc = open_jsonrpc(server);
583 schema = fetch_schema_from_rpc(rpc, database);
584 table = shash_find_data(&schema->tables, table_name);
586 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
587 server, database, table_name);
590 monitor_request_array = json_array_create_empty();
594 for (i = 4; i < argc; i++) {
596 monitor_request_array,
597 parse_monitor_columns(argv[i], server, database, table,
601 /* Allocate a writable empty string since parse_monitor_columns() is
602 * going to strtok() it and that's risky with literal "". */
605 monitor_request_array,
606 parse_monitor_columns(empty, server, database, table, &columns));
609 monitor_requests = json_object_create();
610 json_object_put(monitor_requests, table_name, monitor_request_array);
612 monitor = json_array_create_3(json_string_create(database),
613 json_null_create(), monitor_requests);
614 request = jsonrpc_create_request("monitor", monitor, NULL);
615 request_id = json_clone(request->id);
616 jsonrpc_send(rpc, request);
618 struct jsonrpc_msg *msg;
621 error = jsonrpc_recv_block(rpc, &msg);
623 ovsdb_schema_destroy(schema);
624 ovs_fatal(error, "%s: receive failed", server);
627 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
628 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
630 } else if (msg->type == JSONRPC_REPLY
631 && json_equal(msg->id, request_id)) {
632 monitor_print(msg->result, table, &columns, true);
635 /* daemonize() closes the standard file descriptors. We output
636 * to stdout, so we need to save and restore STDOUT_FILENO. */
637 int fd = dup(STDOUT_FILENO);
639 dup2(fd, STDOUT_FILENO);
642 } else if (msg->type == JSONRPC_NOTIFY
643 && !strcmp(msg->method, "update")) {
644 struct json *params = msg->params;
645 if (params->type == JSON_ARRAY
646 && params->u.array.n == 2
647 && params->u.array.elems[0]->type == JSON_NULL) {
648 monitor_print(params->u.array.elems[1],
649 table, &columns, false);
653 jsonrpc_msg_destroy(msg);
657 struct dump_table_aux {
658 struct ovsdb_datum **data;
659 const struct ovsdb_column **columns;
664 compare_data(size_t a_y, size_t b_y, size_t x,
665 const struct dump_table_aux *aux)
667 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
669 &aux->columns[x]->type);
673 compare_rows(size_t a_y, size_t b_y, void *aux_)
675 struct dump_table_aux *aux = aux_;
678 /* Skip UUID columns on the first pass, since their values tend to be
679 * random and make our results less reproducible. */
680 for (x = 0; x < aux->n_columns; x++) {
681 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
682 int cmp = compare_data(a_y, b_y, x, aux);
689 /* Use UUID columns as tie-breakers. */
690 for (x = 0; x < aux->n_columns; x++) {
691 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
692 int cmp = compare_data(a_y, b_y, x, aux);
703 swap_rows(size_t a_y, size_t b_y, void *aux_)
705 struct dump_table_aux *aux = aux_;
706 struct ovsdb_datum *tmp = aux->data[a_y];
707 aux->data[a_y] = aux->data[b_y];
708 aux->data[b_y] = tmp;
712 compare_columns(const void *a_, const void *b_)
714 const struct ovsdb_column *const *ap = a_;
715 const struct ovsdb_column *const *bp = b_;
716 const struct ovsdb_column *a = *ap;
717 const struct ovsdb_column *b = *bp;
719 return strcmp(a->name, b->name);
723 dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
725 const struct ovsdb_column **columns;
728 struct ovsdb_datum **data;
730 struct dump_table_aux aux;
731 struct shash_node *node;
735 /* Sort columns by name, for reproducibility. */
736 columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
738 SHASH_FOR_EACH (node, &ts->columns) {
739 struct ovsdb_column *column = node->data;
740 if (strcmp(column->name, "_version")) {
741 columns[n_columns++] = column;
744 qsort(columns, n_columns, sizeof *columns, compare_columns);
746 /* Extract data from table. */
747 data = xmalloc(rows->n * sizeof *data);
748 for (y = 0; y < rows->n; y++) {
751 if (rows->elems[y]->type != JSON_OBJECT) {
752 ovs_fatal(0, "row %zu in table %s response is not a JSON object: "
753 "%s", y, ts->name, json_to_string(rows->elems[y], 0));
755 row = json_object(rows->elems[y]);
757 data[y] = xmalloc(n_columns * sizeof **data);
758 for (x = 0; x < n_columns; x++) {
759 const struct json *json = shash_find_data(row, columns[x]->name);
761 ovs_fatal(0, "row %zu in table %s response lacks %s column",
762 y, ts->name, columns[x]->name);
765 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
771 /* Sort rows by column values, for reproducibility. */
773 aux.columns = columns;
774 aux.n_columns = n_columns;
775 sort(rows->n, compare_rows, swap_rows, &aux);
777 /* Add column headings. */
779 table_set_caption(&t, xasprintf("%s table", ts->name));
780 for (x = 0; x < n_columns; x++) {
781 table_add_column(&t, "%s", columns[x]->name);
785 for (y = 0; y < rows->n; y++) {
787 for (x = 0; x < n_columns; x++) {
788 struct cell *cell = table_add_cell(&t);
789 cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
790 cell->type = &columns[x]->type;
793 table_print(&t, &table_style);
798 do_dump(int argc OVS_UNUSED, char *argv[])
800 const char *server = argv[1];
801 const char *database = argv[2];
803 struct jsonrpc_msg *request, *reply;
804 struct ovsdb_schema *schema;
805 struct json *transaction;
809 const struct shash_node **tables;
814 rpc = open_jsonrpc(server);
816 schema = fetch_schema_from_rpc(rpc, database);
817 tables = shash_sort(&schema->tables);
818 n_tables = shash_count(&schema->tables);
820 /* Construct transaction to retrieve entire database. */
821 transaction = json_array_create_1(json_string_create(database));
822 for (i = 0; i < n_tables; i++) {
823 const struct ovsdb_table_schema *ts = tables[i]->data;
824 struct json *op, *columns;
825 struct shash_node *node;
827 columns = json_array_create_empty();
828 SHASH_FOR_EACH (node, &ts->columns) {
829 const struct ovsdb_column *column = node->data;
831 if (strcmp(column->name, "_version")) {
832 json_array_add(columns, json_string_create(column->name));
836 op = json_object_create();
837 json_object_put_string(op, "op", "select");
838 json_object_put_string(op, "table", tables[i]->name);
839 json_object_put(op, "where", json_array_create_empty());
840 json_object_put(op, "columns", columns);
841 json_array_add(transaction, op);
844 /* Send request, get reply. */
845 request = jsonrpc_create_request("transact", transaction, NULL);
846 error = jsonrpc_transact_block(rpc, request, &reply);
848 ovs_fatal(error, "transaction failed");
851 /* Print database contents. */
852 if (reply->result->type != JSON_ARRAY
853 || reply->result->u.array.n != n_tables) {
854 ovs_fatal(0, "reply is not array of %zu elements: %s",
855 n_tables, json_to_string(reply->result, 0));
857 for (i = 0; i < n_tables; i++) {
858 const struct ovsdb_table_schema *ts = tables[i]->data;
859 const struct json *op_result = reply->result->u.array.elems[i];
862 if (op_result->type != JSON_OBJECT
863 || !(rows = shash_find_data(json_object(op_result), "rows"))
864 || rows->type != JSON_ARRAY) {
865 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
867 ts->name, json_to_string(op_result, 0));
870 dump_table(ts, &rows->u.array);
875 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
880 static const struct command all_commands[] = {
881 { "list-dbs", 1, 1, do_list_dbs },
882 { "get-schema", 2, 2, do_get_schema },
883 { "get-schema-version", 2, 2, do_get_schema_version },
884 { "list-tables", 2, 2, do_list_tables },
885 { "list-columns", 2, 3, do_list_columns },
886 { "transact", 2, 2, do_transact },
887 { "monitor", 3, INT_MAX, do_monitor },
888 { "dump", 2, 2, do_dump },
889 { "help", 0, INT_MAX, do_help },
890 { NULL, 0, 0, NULL },