2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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"
33 #include "dynamic-string.h"
34 #include "fatal-signal.h"
37 #include "lib/table.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-error.h"
41 #include "poll-loop.h"
45 #include "stream-ssl.h"
51 #include "openvswitch/vlog.h"
53 VLOG_DEFINE_THIS_MODULE(ovsdb_client);
56 NEED_NONE, /* No JSON-RPC connection or database name needed. */
57 NEED_RPC, /* JSON-RPC connection needed. */
58 NEED_DATABASE /* JSON-RPC connection and database name needed. */
61 struct ovsdb_client_command {
63 enum args_needed need;
66 void (*handler)(struct jsonrpc *rpc, const char *database,
67 int argc, char *argv[]);
70 /* --timestamp: Print a timestamp before each update on "monitor" command? */
71 static bool timestamp;
73 /* Format for table output. */
74 static struct table_style table_style = TABLE_STYLE_DEFAULT;
76 static const struct ovsdb_client_command *get_all_commands(void);
78 OVS_NO_RETURN static void usage(void);
79 static void parse_options(int argc, char *argv[]);
80 static struct jsonrpc *open_jsonrpc(const char *server);
81 static void fetch_dbs(struct jsonrpc *, struct svec *dbs);
84 main(int argc, char *argv[])
86 const struct ovsdb_client_command *command;
90 ovs_cmdl_proctitle_init(argc, argv);
91 set_program_name(argv[0]);
92 parse_options(argc, argv);
93 fatal_ignore_sigpipe();
95 daemon_become_new_user(false);
97 ovs_fatal(0, "missing command name; use --help for help");
100 for (command = get_all_commands(); ; command++) {
101 if (!command->name) {
102 VLOG_FATAL("unknown command '%s'; use --help for help",
104 } else if (!strcmp(command->name, argv[optind])) {
110 if (command->need != NEED_NONE) {
111 if (argc - optind > command->min_args
112 && (isalpha((unsigned char) argv[optind][0])
113 && strchr(argv[optind], ':'))) {
114 rpc = open_jsonrpc(argv[optind++]);
116 char *sock = xasprintf("unix:%s/db.sock", ovs_rundir());
117 rpc = open_jsonrpc(sock);
124 if (command->need == NEED_DATABASE) {
128 fetch_dbs(rpc, &dbs);
129 if (argc - optind > command->min_args
130 && svec_contains(&dbs, argv[optind])) {
131 database = argv[optind++];
132 } else if (dbs.n == 1) {
133 database = xstrdup(dbs.names[0]);
134 } else if (svec_contains(&dbs, "Open_vSwitch")) {
135 database = "Open_vSwitch";
137 ovs_fatal(0, "no default database for `%s' command, please "
138 "specify a database name", command->name);
145 if (argc - optind < command->min_args ||
146 argc - optind > command->max_args) {
147 VLOG_FATAL("invalid syntax for '%s' (use --help for help)",
151 command->handler(rpc, database, argc - optind, argv + optind);
155 if (ferror(stdout)) {
156 VLOG_FATAL("write to stdout failed");
158 if (ferror(stderr)) {
159 VLOG_FATAL("write to stderr failed");
166 parse_options(int argc, char *argv[])
169 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
175 static const struct option long_options[] = {
176 {"help", no_argument, NULL, 'h'},
177 {"version", no_argument, NULL, 'V'},
178 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
182 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
183 STREAM_SSL_LONG_OPTIONS,
188 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
193 c = getopt_long(argc, argv, short_options, long_options, NULL);
203 ovs_print_version(0, 0);
207 DAEMON_OPTION_HANDLERS
208 TABLE_OPTION_HANDLERS(&table_style)
209 STREAM_SSL_OPTION_HANDLERS
211 case OPT_BOOTSTRAP_CA_CERT:
212 stream_ssl_set_ca_cert_file(optarg, true);
223 /* getopt_long() already set the value for us. */
236 printf("%s: Open vSwitch database JSON-RPC client\n"
237 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
238 "\nValid commands are:\n"
239 "\n list-dbs [SERVER]\n"
240 " list databases available on SERVER\n"
241 "\n get-schema [SERVER] [DATABASE]\n"
242 " retrieve schema for DATABASE from SERVER\n"
243 "\n get-schema-version [SERVER] [DATABASE]\n"
244 " retrieve schema for DATABASE from SERVER and report only its\n"
245 " version number on stdout\n"
246 "\n list-tables [SERVER] [DATABASE]\n"
247 " list tables for DATABASE on SERVER\n"
248 "\n list-columns [SERVER] [DATABASE] [TABLE]\n"
249 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
250 "\n transact [SERVER] TRANSACTION\n"
251 " run TRANSACTION (a JSON array of operations) on SERVER\n"
252 " and print the results as JSON on stdout\n"
253 "\n monitor [SERVER] [DATABASE] TABLE [COLUMN,...]...\n"
254 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
255 " COLUMNs may include !initial, !insert, !delete, !modify\n"
256 " to avoid seeing the specified kinds of changes.\n"
257 "\n monitor [SERVER] [DATABASE] ALL\n"
258 " monitor all changes to all columns in all tables\n"
259 " in DATBASE on SERVER.\n"
260 "\n monitor2 [SERVER] [DATABASE] ALL\n"
261 " same usage as monitor, but uses \"monitor2\" method over"
263 "\n dump [SERVER] [DATABASE]\n"
264 " dump contents of DATABASE on SERVER to stdout\n"
265 "\nThe default SERVER is unix:%s/db.sock.\n"
266 "The default DATABASE is Open_vSwitch.\n",
267 program_name, program_name, ovs_rundir());
268 stream_usage("SERVER", true, true, true);
269 printf("\nOutput formatting options:\n"
270 " -f, --format=FORMAT set output formatting to FORMAT\n"
271 " (\"table\", \"html\", \"csv\", "
273 " --no-headings omit table heading row\n"
274 " --pretty pretty-print JSON in output\n"
275 " --timestamp timestamp \"monitor\" output");
278 printf("\nOther options:\n"
279 " -h, --help display this help message\n"
280 " -V, --version display version information\n");
285 check_txn(int error, struct jsonrpc_msg **reply_)
287 struct jsonrpc_msg *reply = *reply_;
290 ovs_fatal(error, "transaction failed");
294 ovs_fatal(error, "transaction returned error: %s",
295 json_to_string(reply->error, table_style.json_flags));
300 parse_json(const char *s)
302 struct json *json = json_from_string(s);
303 if (json->type == JSON_STRING) {
304 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
309 static struct jsonrpc *
310 open_jsonrpc(const char *server)
312 struct stream *stream;
315 error = stream_open_block(jsonrpc_stream_open(server, &stream,
316 DSCP_DEFAULT), &stream);
317 if (error == EAFNOSUPPORT) {
318 struct pstream *pstream;
320 error = jsonrpc_pstream_open(server, &pstream, DSCP_DEFAULT);
322 ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
325 VLOG_INFO("%s: waiting for connection...", server);
326 error = pstream_accept_block(pstream, &stream);
328 ovs_fatal(error, "failed to accept connection on \"%s\"", server);
331 pstream_close(pstream);
333 ovs_fatal(error, "failed to connect to \"%s\"", server);
336 return jsonrpc_open(stream);
340 print_json(struct json *json)
342 char *string = json_to_string(json, table_style.json_flags);
343 fputs(string, stdout);
348 print_and_free_json(struct json *json)
355 check_ovsdb_error(struct ovsdb_error *error)
358 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
362 static struct ovsdb_schema *
363 fetch_schema(struct jsonrpc *rpc, const char *database)
365 struct jsonrpc_msg *request, *reply;
366 struct ovsdb_schema *schema;
368 request = jsonrpc_create_request("get_schema",
370 json_string_create(database)),
372 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
373 check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
374 jsonrpc_msg_destroy(reply);
380 fetch_dbs(struct jsonrpc *rpc, struct svec *dbs)
382 struct jsonrpc_msg *request, *reply;
385 request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
388 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
389 if (reply->result->type != JSON_ARRAY) {
390 ovs_fatal(0, "list_dbs response is not array");
393 for (i = 0; i < reply->result->u.array.n; i++) {
394 const struct json *name = reply->result->u.array.elems[i];
396 if (name->type != JSON_STRING) {
397 ovs_fatal(0, "list_dbs response %"PRIuSIZE" is not string", i);
399 svec_add(dbs, name->u.string);
401 jsonrpc_msg_destroy(reply);
406 do_list_dbs(struct jsonrpc *rpc, const char *database OVS_UNUSED,
407 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
414 fetch_dbs(rpc, &dbs);
415 SVEC_FOR_EACH (i, db_name, &dbs) {
422 do_get_schema(struct jsonrpc *rpc, const char *database,
423 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
425 struct ovsdb_schema *schema = fetch_schema(rpc, database);
426 print_and_free_json(ovsdb_schema_to_json(schema));
427 ovsdb_schema_destroy(schema);
431 do_get_schema_version(struct jsonrpc *rpc, const char *database,
432 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
434 struct ovsdb_schema *schema = fetch_schema(rpc, database);
435 puts(schema->version);
436 ovsdb_schema_destroy(schema);
440 do_list_tables(struct jsonrpc *rpc, const char *database,
441 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
443 struct ovsdb_schema *schema;
444 struct shash_node *node;
447 schema = fetch_schema(rpc, database);
449 table_add_column(&t, "Table");
450 SHASH_FOR_EACH (node, &schema->tables) {
451 struct ovsdb_table_schema *ts = node->data;
454 table_add_cell(&t)->text = xstrdup(ts->name);
456 ovsdb_schema_destroy(schema);
457 table_print(&t, &table_style);
461 do_list_columns(struct jsonrpc *rpc, const char *database,
462 int argc OVS_UNUSED, char *argv[])
464 const char *table_name = argv[0];
465 struct ovsdb_schema *schema;
466 struct shash_node *table_node;
469 schema = fetch_schema(rpc, database);
472 table_add_column(&t, "Table");
474 table_add_column(&t, "Column");
475 table_add_column(&t, "Type");
476 SHASH_FOR_EACH (table_node, &schema->tables) {
477 struct ovsdb_table_schema *ts = table_node->data;
479 if (!table_name || !strcmp(table_name, ts->name)) {
480 struct shash_node *column_node;
482 SHASH_FOR_EACH (column_node, &ts->columns) {
483 const struct ovsdb_column *column = column_node->data;
487 table_add_cell(&t)->text = xstrdup(ts->name);
489 table_add_cell(&t)->text = xstrdup(column->name);
490 table_add_cell(&t)->json = ovsdb_type_to_json(&column->type);
494 ovsdb_schema_destroy(schema);
495 table_print(&t, &table_style);
499 do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
500 int argc OVS_UNUSED, char *argv[])
502 struct jsonrpc_msg *request, *reply;
503 struct json *transaction;
505 transaction = parse_json(argv[0]);
507 request = jsonrpc_create_request("transact", transaction, NULL);
508 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
509 print_json(reply->result);
511 jsonrpc_msg_destroy(reply);
514 /* "monitor" command. */
516 struct monitored_table {
517 struct ovsdb_table_schema *table;
518 struct ovsdb_column_set columns;
522 monitor_print_row(struct json *row, const char *type, const char *uuid,
523 const struct ovsdb_column_set *columns, struct table *t)
528 ovs_error(0, "missing %s row", type);
530 } else if (row->type != JSON_OBJECT) {
531 ovs_error(0, "<row> is not object");
536 table_add_cell(t)->text = xstrdup(uuid);
537 table_add_cell(t)->text = xstrdup(type);
538 for (i = 0; i < columns->n_columns; i++) {
539 const struct ovsdb_column *column = columns->columns[i];
540 struct json *value = shash_find_data(json_object(row), column->name);
541 struct cell *cell = table_add_cell(t);
543 cell->json = json_clone(value);
544 cell->type = &column->type;
550 monitor_print_table(struct json *table_update,
551 const struct monitored_table *mt, char *caption,
554 const struct ovsdb_table_schema *table = mt->table;
555 const struct ovsdb_column_set *columns = &mt->columns;
556 struct shash_node *node;
560 if (table_update->type != JSON_OBJECT) {
561 ovs_error(0, "<table-update> for table %s is not object", table->name);
566 table_set_timestamp(&t, timestamp);
567 table_set_caption(&t, caption);
569 table_add_column(&t, "row");
570 table_add_column(&t, "action");
571 for (i = 0; i < columns->n_columns; i++) {
572 table_add_column(&t, "%s", columns->columns[i]->name);
574 SHASH_FOR_EACH (node, json_object(table_update)) {
575 struct json *row_update = node->data;
576 struct json *old, *new;
578 if (row_update->type != JSON_OBJECT) {
579 ovs_error(0, "<row-update> is not object");
582 old = shash_find_data(json_object(row_update), "old");
583 new = shash_find_data(json_object(row_update), "new");
585 monitor_print_row(new, "initial", node->name, columns, &t);
587 monitor_print_row(new, "insert", node->name, columns, &t);
589 monitor_print_row(old, "delete", node->name, columns, &t);
591 monitor_print_row(old, "old", node->name, columns, &t);
592 monitor_print_row(new, "new", "", columns, &t);
595 table_print(&t, &table_style);
600 monitor_print(struct json *table_updates,
601 const struct monitored_table *mts, size_t n_mts,
606 if (table_updates->type != JSON_OBJECT) {
607 ovs_error(0, "<table-updates> is not object");
611 for (i = 0; i < n_mts; i++) {
612 const struct monitored_table *mt = &mts[i];
613 struct json *table_update = shash_find_data(json_object(table_updates),
616 monitor_print_table(table_update, mt,
617 n_mts > 1 ? xstrdup(mt->table->name) : NULL,
624 monitor2_print_row(struct json *row, const char *type, const char *uuid,
625 const struct ovsdb_column_set *columns, struct table *t)
627 if (!strcmp(type, "delete")) {
628 if (row->type != JSON_NULL) {
629 ovs_error(0, "delete method does not expect <row>");
634 table_add_cell(t)->text = xstrdup(uuid);
635 table_add_cell(t)->text = xstrdup(type);
637 if (!row || row->type != JSON_OBJECT) {
638 ovs_error(0, "<row> is not object");
641 monitor_print_row(row, type, uuid, columns, t);
646 monitor2_print_table(struct json *table_update2,
647 const struct monitored_table *mt, char *caption)
649 const struct ovsdb_table_schema *table = mt->table;
650 const struct ovsdb_column_set *columns = &mt->columns;
651 struct shash_node *node;
655 if (table_update2->type != JSON_OBJECT) {
656 ovs_error(0, "<table-update> for table %s is not object", table->name);
661 table_set_timestamp(&t, timestamp);
662 table_set_caption(&t, caption);
664 table_add_column(&t, "row");
665 table_add_column(&t, "action");
666 for (i = 0; i < columns->n_columns; i++) {
667 table_add_column(&t, "%s", columns->columns[i]->name);
669 SHASH_FOR_EACH (node, json_object(table_update2)) {
670 struct json *row_update2 = node->data;
671 const char *operation;
673 const char *ops[] = {"delete", "initial", "modify", "insert"};
675 if (row_update2->type != JSON_OBJECT) {
676 ovs_error(0, "<row-update2> is not object");
680 /* row_update2 contains one of objects indexed by ops[] */
681 for (int i = 0; i < ARRAY_SIZE(ops); i++) {
683 row = shash_find_data(json_object(row_update2), operation);
686 monitor2_print_row(row, operation, node->name, columns, &t);
691 table_print(&t, &table_style);
696 monitor2_print(struct json *table_updates2,
697 const struct monitored_table *mts, size_t n_mts)
701 if (table_updates2->type != JSON_OBJECT) {
702 ovs_error(0, "<table-updates2> is not object");
706 for (i = 0; i < n_mts; i++) {
707 const struct monitored_table *mt = &mts[i];
708 struct json *table_update = shash_find_data(
709 json_object(table_updates2),
712 monitor2_print_table(table_update, mt,
713 n_mts > 1 ? xstrdup(mt->table->name) : NULL);
719 add_column(const char *server, const struct ovsdb_column *column,
720 struct ovsdb_column_set *columns, struct json *columns_json)
722 if (ovsdb_column_set_contains(columns, column->index)) {
723 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
724 server, column->name);
726 ovsdb_column_set_add(columns, column);
727 json_array_add(columns_json, json_string_create(column->name));
731 parse_monitor_columns(char *arg, const char *server, const char *database,
732 const struct ovsdb_table_schema *table,
733 struct ovsdb_column_set *columns)
735 bool initial, insert, delete, modify;
736 struct json *mr, *columns_json;
737 char *save_ptr = NULL;
740 mr = json_object_create();
741 columns_json = json_array_create_empty();
742 json_object_put(mr, "columns", columns_json);
744 initial = insert = delete = modify = true;
745 for (token = strtok_r(arg, ",", &save_ptr); token != NULL;
746 token = strtok_r(NULL, ",", &save_ptr)) {
747 if (!strcmp(token, "!initial")) {
749 } else if (!strcmp(token, "!insert")) {
751 } else if (!strcmp(token, "!delete")) {
753 } else if (!strcmp(token, "!modify")) {
756 const struct ovsdb_column *column;
758 column = ovsdb_table_schema_get_column(table, token);
760 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
761 "column named \"%s\"",
762 server, table->name, database, token);
764 add_column(server, column, columns, columns_json);
768 if (columns_json->u.array.n == 0) {
769 const struct shash_node **nodes;
772 n = shash_count(&table->columns);
773 nodes = shash_sort(&table->columns);
774 for (i = 0; i < n; i++) {
775 const struct ovsdb_column *column = nodes[i]->data;
776 if (column->index != OVSDB_COL_UUID
777 && column->index != OVSDB_COL_VERSION) {
778 add_column(server, column, columns, columns_json);
783 add_column(server, ovsdb_table_schema_get_column(table, "_version"),
784 columns, columns_json);
787 if (!initial || !insert || !delete || !modify) {
788 struct json *select = json_object_create();
789 json_object_put(select, "initial", json_boolean_create(initial));
790 json_object_put(select, "insert", json_boolean_create(insert));
791 json_object_put(select, "delete", json_boolean_create(delete));
792 json_object_put(select, "modify", json_boolean_create(modify));
793 json_object_put(mr, "select", select);
800 ovsdb_client_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
801 const char *argv[] OVS_UNUSED, void *exiting_)
803 bool *exiting = exiting_;
805 unixctl_command_reply(conn, NULL);
809 ovsdb_client_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
810 const char *argv[] OVS_UNUSED, void *blocked_)
812 bool *blocked = blocked_;
816 unixctl_command_reply(conn, NULL);
818 unixctl_command_reply(conn, "already blocking");
823 ovsdb_client_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
824 const char *argv[] OVS_UNUSED, void *blocked_)
826 bool *blocked = blocked_;
830 unixctl_command_reply(conn, NULL);
832 unixctl_command_reply(conn, "already unblocked");
837 add_monitored_table(int argc, char *argv[],
838 const char *server, const char *database,
839 struct ovsdb_table_schema *table,
840 struct json *monitor_requests,
841 struct monitored_table **mts,
842 size_t *n_mts, size_t *allocated_mts)
844 struct json *monitor_request_array;
845 struct monitored_table *mt;
847 if (*n_mts >= *allocated_mts) {
848 *mts = x2nrealloc(*mts, allocated_mts, sizeof **mts);
850 mt = &(*mts)[(*n_mts)++];
852 ovsdb_column_set_init(&mt->columns);
854 monitor_request_array = json_array_create_empty();
858 for (i = 1; i < argc; i++) {
860 monitor_request_array,
861 parse_monitor_columns(argv[i], server, database, table,
865 /* Allocate a writable empty string since parse_monitor_columns()
866 * is going to strtok() it and that's risky with literal "". */
869 monitor_request_array,
870 parse_monitor_columns(empty, server, database,
871 table, &mt->columns));
874 json_object_put(monitor_requests, table->name, monitor_request_array);
878 do_monitor__(struct jsonrpc *rpc, const char *database,
879 enum ovsdb_monitor_version version,
880 int argc, char *argv[])
882 const char *server = jsonrpc_get_name(rpc);
883 const char *table_name = argv[0];
884 struct unixctl_server *unixctl;
885 struct ovsdb_schema *schema;
886 struct jsonrpc_msg *request;
887 struct json *monitor, *monitor_requests, *request_id;
888 bool exiting = false;
889 bool blocked = false;
891 struct monitored_table *mts;
892 size_t n_mts, allocated_mts;
894 ovs_assert(version < OVSDB_MONITOR_VERSION_MAX);
896 daemon_save_fd(STDOUT_FILENO);
897 daemonize_start(false);
901 error = unixctl_server_create(NULL, &unixctl);
903 ovs_fatal(error, "failed to create unixctl server");
906 unixctl_command_register("exit", "", 0, 0,
907 ovsdb_client_exit, &exiting);
908 unixctl_command_register("ovsdb-client/block", "", 0, 0,
909 ovsdb_client_block, &blocked);
910 unixctl_command_register("ovsdb-client/unblock", "", 0, 0,
911 ovsdb_client_unblock, &blocked);
916 schema = fetch_schema(rpc, database);
918 monitor_requests = json_object_create();
921 n_mts = allocated_mts = 0;
922 if (strcmp(table_name, "ALL")) {
923 struct ovsdb_table_schema *table;
925 table = shash_find_data(&schema->tables, table_name);
927 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
928 server, database, table_name);
931 add_monitored_table(argc, argv, server, database, table,
932 monitor_requests, &mts, &n_mts, &allocated_mts);
934 size_t n = shash_count(&schema->tables);
935 const struct shash_node **nodes = shash_sort(&schema->tables);
938 for (i = 0; i < n; i++) {
939 struct ovsdb_table_schema *table = nodes[i]->data;
941 add_monitored_table(argc, argv, server, database, table,
943 &mts, &n_mts, &allocated_mts);
948 monitor = json_array_create_3(json_string_create(database),
949 json_null_create(), monitor_requests);
950 const char *method = version == OVSDB_MONITOR_V2 ? "monitor2"
953 request = jsonrpc_create_request(method, monitor, NULL);
954 request_id = json_clone(request->id);
955 jsonrpc_send(rpc, request);
958 unixctl_server_run(unixctl);
960 struct jsonrpc_msg *msg;
963 error = jsonrpc_recv(rpc, &msg);
964 if (error == EAGAIN) {
967 ovs_fatal(error, "%s: receive failed", server);
970 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
971 jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
973 } else if (msg->type == JSONRPC_REPLY
974 && json_equal(msg->id, request_id)) {
976 case OVSDB_MONITOR_V1:
977 monitor_print(msg->result, mts, n_mts, true);
979 case OVSDB_MONITOR_V2:
980 monitor2_print(msg->result, mts, n_mts);
982 case OVSDB_MONITOR_VERSION_MAX:
987 daemonize_complete();
988 } else if (msg->type == JSONRPC_NOTIFY
989 && !strcmp(msg->method, "update")) {
990 struct json *params = msg->params;
991 if (params->type == JSON_ARRAY
992 && params->u.array.n == 2
993 && params->u.array.elems[0]->type == JSON_NULL) {
994 monitor_print(params->u.array.elems[1], mts, n_mts, false);
997 } else if (msg->type == JSONRPC_NOTIFY
998 && version == OVSDB_MONITOR_V2
999 && !strcmp(msg->method, "update2")) {
1000 struct json *params = msg->params;
1001 if (params->type == JSON_ARRAY
1002 && params->u.array.n == 2
1003 && params->u.array.elems[0]->type == JSON_NULL) {
1004 monitor2_print(params->u.array.elems[1], mts, n_mts);
1008 jsonrpc_msg_destroy(msg);
1018 jsonrpc_recv_wait(rpc);
1020 unixctl_server_wait(unixctl);
1026 do_monitor(struct jsonrpc *rpc, const char *database,
1027 int argc, char *argv[])
1029 do_monitor__(rpc, database, OVSDB_MONITOR_V1, argc, argv);
1033 do_monitor2(struct jsonrpc *rpc, const char *database,
1034 int argc, char *argv[])
1036 do_monitor__(rpc, database, OVSDB_MONITOR_V2, argc, argv);
1039 struct dump_table_aux {
1040 struct ovsdb_datum **data;
1041 const struct ovsdb_column **columns;
1046 compare_data(size_t a_y, size_t b_y, size_t x,
1047 const struct dump_table_aux *aux)
1049 return ovsdb_datum_compare_3way(&aux->data[a_y][x],
1051 &aux->columns[x]->type);
1055 compare_rows(size_t a_y, size_t b_y, void *aux_)
1057 struct dump_table_aux *aux = aux_;
1060 /* Skip UUID columns on the first pass, since their values tend to be
1061 * random and make our results less reproducible. */
1062 for (x = 0; x < aux->n_columns; x++) {
1063 if (aux->columns[x]->type.key.type != OVSDB_TYPE_UUID) {
1064 int cmp = compare_data(a_y, b_y, x, aux);
1071 /* Use UUID columns as tie-breakers. */
1072 for (x = 0; x < aux->n_columns; x++) {
1073 if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
1074 int cmp = compare_data(a_y, b_y, x, aux);
1085 swap_rows(size_t a_y, size_t b_y, void *aux_)
1087 struct dump_table_aux *aux = aux_;
1088 struct ovsdb_datum *tmp = aux->data[a_y];
1089 aux->data[a_y] = aux->data[b_y];
1090 aux->data[b_y] = tmp;
1094 compare_columns(const void *a_, const void *b_)
1096 const struct ovsdb_column *const *ap = a_;
1097 const struct ovsdb_column *const *bp = b_;
1098 const struct ovsdb_column *a = *ap;
1099 const struct ovsdb_column *b = *bp;
1101 return strcmp(a->name, b->name);
1105 dump_table(const char *table_name, const struct shash *cols,
1106 struct json_array *rows)
1108 const struct ovsdb_column **columns;
1111 struct ovsdb_datum **data;
1113 struct dump_table_aux aux;
1114 struct shash_node *node;
1118 /* Sort columns by name, for reproducibility. */
1119 columns = xmalloc(shash_count(cols) * sizeof *columns);
1121 SHASH_FOR_EACH (node, cols) {
1122 struct ovsdb_column *column = node->data;
1123 if (strcmp(column->name, "_version")) {
1124 columns[n_columns++] = column;
1127 qsort(columns, n_columns, sizeof *columns, compare_columns);
1129 /* Extract data from table. */
1130 data = xmalloc(rows->n * sizeof *data);
1131 for (y = 0; y < rows->n; y++) {
1134 if (rows->elems[y]->type != JSON_OBJECT) {
1135 ovs_fatal(0, "row %"PRIuSIZE" in table %s response is not a JSON object: "
1136 "%s", y, table_name, json_to_string(rows->elems[y], 0));
1138 row = json_object(rows->elems[y]);
1140 data[y] = xmalloc(n_columns * sizeof **data);
1141 for (x = 0; x < n_columns; x++) {
1142 const struct json *json = shash_find_data(row, columns[x]->name);
1144 ovs_fatal(0, "row %"PRIuSIZE" in table %s response lacks %s column",
1145 y, table_name, columns[x]->name);
1148 check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
1154 /* Sort rows by column values, for reproducibility. */
1156 aux.columns = columns;
1157 aux.n_columns = n_columns;
1158 sort(rows->n, compare_rows, swap_rows, &aux);
1160 /* Add column headings. */
1162 table_set_caption(&t, xasprintf("%s table", table_name));
1163 for (x = 0; x < n_columns; x++) {
1164 table_add_column(&t, "%s", columns[x]->name);
1168 for (y = 0; y < rows->n; y++) {
1170 for (x = 0; x < n_columns; x++) {
1171 struct cell *cell = table_add_cell(&t);
1172 cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
1173 cell->type = &columns[x]->type;
1174 ovsdb_datum_destroy(&data[y][x], &columns[x]->type);
1178 table_print(&t, &table_style);
1186 do_dump(struct jsonrpc *rpc, const char *database,
1187 int argc, char *argv[])
1189 struct jsonrpc_msg *request, *reply;
1190 struct ovsdb_schema *schema;
1191 struct json *transaction;
1193 const struct shash_node *node, **tables;
1195 struct ovsdb_table_schema *tschema;
1196 const struct shash *columns;
1197 struct shash custom_columns;
1201 shash_init(&custom_columns);
1202 schema = fetch_schema(rpc, database);
1204 node = shash_find(&schema->tables, argv[0]);
1206 ovs_fatal(0, "No table \"%s\" found.", argv[0]);
1208 tables = xmemdup(&node, sizeof(&node));
1210 tschema = tables[0]->data;
1211 for (i = 1; i < argc; i++) {
1212 node = shash_find(&tschema->columns, argv[i]);
1214 ovs_fatal(0, "Table \"%s\" has no column %s.", argv[0], argv[1]);
1216 shash_add(&custom_columns, argv[1], node->data);
1219 tables = shash_sort(&schema->tables);
1220 n_tables = shash_count(&schema->tables);
1223 /* Construct transaction to retrieve entire database. */
1224 transaction = json_array_create_1(json_string_create(database));
1225 for (i = 0; i < n_tables; i++) {
1226 const struct ovsdb_table_schema *ts = tables[i]->data;
1227 struct json *op, *jcolumns;
1230 columns = &custom_columns;
1232 columns = &ts->columns;
1234 jcolumns = json_array_create_empty();
1235 SHASH_FOR_EACH (node, columns) {
1236 const struct ovsdb_column *column = node->data;
1238 if (strcmp(column->name, "_version")) {
1239 json_array_add(jcolumns, json_string_create(column->name));
1243 op = json_object_create();
1244 json_object_put_string(op, "op", "select");
1245 json_object_put_string(op, "table", tables[i]->name);
1246 json_object_put(op, "where", json_array_create_empty());
1247 json_object_put(op, "columns", jcolumns);
1248 json_array_add(transaction, op);
1251 /* Send request, get reply. */
1252 request = jsonrpc_create_request("transact", transaction, NULL);
1253 check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
1255 /* Print database contents. */
1256 if (reply->result->type != JSON_ARRAY
1257 || reply->result->u.array.n != n_tables) {
1258 ovs_fatal(0, "reply is not array of %"PRIuSIZE" elements: %s",
1259 n_tables, json_to_string(reply->result, 0));
1261 for (i = 0; i < n_tables; i++) {
1262 const struct ovsdb_table_schema *ts = tables[i]->data;
1263 const struct json *op_result = reply->result->u.array.elems[i];
1266 if (op_result->type != JSON_OBJECT
1267 || !(rows = shash_find_data(json_object(op_result), "rows"))
1268 || rows->type != JSON_ARRAY) {
1269 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1271 ts->name, json_to_string(op_result, 0));
1275 dump_table(tables[i]->name, &custom_columns, &rows->u.array);
1277 dump_table(tables[i]->name, &ts->columns, &rows->u.array);
1281 jsonrpc_msg_destroy(reply);
1282 shash_destroy(&custom_columns);
1284 ovsdb_schema_destroy(schema);
1288 do_help(struct jsonrpc *rpc OVS_UNUSED, const char *database OVS_UNUSED,
1289 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1294 /* All command handlers (except for "help") are expected to take an optional
1295 * server socket name (e.g. "unix:...") as their first argument. The socket
1296 * name argument must be included in max_args (but left out of min_args). The
1297 * command name and socket name are not included in the arguments passed to the
1298 * handler: the argv[0] passed to the handler is the first argument after the
1299 * optional server socket name. The connection to the server is available as
1300 * global variable 'rpc'. */
1301 static const struct ovsdb_client_command all_commands[] = {
1302 { "list-dbs", NEED_RPC, 0, 0, do_list_dbs },
1303 { "get-schema", NEED_DATABASE, 0, 0, do_get_schema },
1304 { "get-schema-version", NEED_DATABASE, 0, 0, do_get_schema_version },
1305 { "list-tables", NEED_DATABASE, 0, 0, do_list_tables },
1306 { "list-columns", NEED_DATABASE, 0, 1, do_list_columns },
1307 { "transact", NEED_RPC, 1, 1, do_transact },
1308 { "monitor", NEED_DATABASE, 1, INT_MAX, do_monitor },
1309 { "monitor2", NEED_DATABASE, 1, INT_MAX, do_monitor2 },
1310 { "dump", NEED_DATABASE, 0, INT_MAX, do_dump },
1311 { "help", NEED_NONE, 0, INT_MAX, do_help },
1313 { NULL, 0, 0, 0, NULL },
1316 static const struct ovsdb_client_command *get_all_commands(void)
1318 return all_commands;