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"
50 #include "openvswitch/vlog.h"
52 VLOG_DEFINE_THIS_MODULE(ovsdb_client
);
55 NEED_NONE
, /* No JSON-RPC connection or database name needed. */
56 NEED_RPC
, /* JSON-RPC connection needed. */
57 NEED_DATABASE
/* JSON-RPC connection and database name needed. */
60 struct ovsdb_client_command
{
62 enum args_needed need
;
65 void (*handler
)(struct jsonrpc
*rpc
, const char *database
,
66 int argc
, char *argv
[]);
69 /* --timestamp: Print a timestamp before each update on "monitor" command? */
70 static bool timestamp
;
72 /* Format for table output. */
73 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
75 static const struct ovsdb_client_command
*get_all_commands(void);
77 OVS_NO_RETURN
static void usage(void);
78 static void parse_options(int argc
, char *argv
[]);
79 static struct jsonrpc
*open_jsonrpc(const char *server
);
80 static void fetch_dbs(struct jsonrpc
*, struct svec
*dbs
);
83 main(int argc
, char *argv
[])
85 const struct ovsdb_client_command
*command
;
89 ovs_cmdl_proctitle_init(argc
, argv
);
90 set_program_name(argv
[0]);
91 parse_options(argc
, argv
);
92 fatal_ignore_sigpipe();
94 daemon_become_new_user(false);
96 ovs_fatal(0, "missing command name; use --help for help");
99 for (command
= get_all_commands(); ; command
++) {
100 if (!command
->name
) {
101 VLOG_FATAL("unknown command '%s'; use --help for help",
103 } else if (!strcmp(command
->name
, argv
[optind
])) {
109 if (command
->need
!= NEED_NONE
) {
110 if (argc
- optind
> command
->min_args
111 && (isalpha((unsigned char) argv
[optind
][0])
112 && strchr(argv
[optind
], ':'))) {
113 rpc
= open_jsonrpc(argv
[optind
++]);
115 char *sock
= xasprintf("unix:%s/db.sock", ovs_rundir());
116 rpc
= open_jsonrpc(sock
);
123 if (command
->need
== NEED_DATABASE
) {
127 fetch_dbs(rpc
, &dbs
);
128 if (argc
- optind
> command
->min_args
129 && svec_contains(&dbs
, argv
[optind
])) {
130 database
= argv
[optind
++];
131 } else if (dbs
.n
== 1) {
132 database
= xstrdup(dbs
.names
[0]);
133 } else if (svec_contains(&dbs
, "Open_vSwitch")) {
134 database
= "Open_vSwitch";
136 ovs_fatal(0, "no default database for `%s' command, please "
137 "specify a database name", command
->name
);
144 if (argc
- optind
< command
->min_args
||
145 argc
- optind
> command
->max_args
) {
146 VLOG_FATAL("invalid syntax for '%s' (use --help for help)",
150 command
->handler(rpc
, database
, argc
- optind
, argv
+ optind
);
154 if (ferror(stdout
)) {
155 VLOG_FATAL("write to stdout failed");
157 if (ferror(stderr
)) {
158 VLOG_FATAL("write to stderr failed");
165 parse_options(int argc
, char *argv
[])
168 OPT_BOOTSTRAP_CA_CERT
= UCHAR_MAX
+ 1,
174 static const struct option long_options
[] = {
175 {"help", no_argument
, NULL
, 'h'},
176 {"version", no_argument
, NULL
, 'V'},
177 {"timestamp", no_argument
, NULL
, OPT_TIMESTAMP
},
181 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
182 STREAM_SSL_LONG_OPTIONS
,
187 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
192 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
202 ovs_print_version(0, 0);
206 DAEMON_OPTION_HANDLERS
207 TABLE_OPTION_HANDLERS(&table_style
)
208 STREAM_SSL_OPTION_HANDLERS
210 case OPT_BOOTSTRAP_CA_CERT
:
211 stream_ssl_set_ca_cert_file(optarg
, true);
222 /* getopt_long() already set the value for us. */
235 printf("%s: Open vSwitch database JSON-RPC client\n"
236 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
237 "\nValid commands are:\n"
238 "\n list-dbs [SERVER]\n"
239 " list databases available on SERVER\n"
240 "\n get-schema [SERVER] [DATABASE]\n"
241 " retrieve schema for DATABASE from SERVER\n"
242 "\n get-schema-version [SERVER] [DATABASE]\n"
243 " retrieve schema for DATABASE from SERVER and report only its\n"
244 " version number on stdout\n"
245 "\n list-tables [SERVER] [DATABASE]\n"
246 " list tables for DATABASE on SERVER\n"
247 "\n list-columns [SERVER] [DATABASE] [TABLE]\n"
248 " list columns in TABLE (or all tables) in DATABASE on SERVER\n"
249 "\n transact [SERVER] TRANSACTION\n"
250 " run TRANSACTION (a JSON array of operations) on SERVER\n"
251 " and print the results as JSON on stdout\n"
252 "\n monitor [SERVER] [DATABASE] TABLE [COLUMN,...]...\n"
253 " monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
254 " COLUMNs may include !initial, !insert, !delete, !modify\n"
255 " to avoid seeing the specified kinds of changes.\n"
256 "\n monitor [SERVER] [DATABASE] ALL\n"
257 " monitor all changes to all columns in all tables\n"
258 " in DATBASE on SERVER.\n"
259 "\n dump [SERVER] [DATABASE] [TABLE [COLUMN]...]\n"
260 " dump contents of DATABASE on SERVER to stdout\n"
261 "\nThe default SERVER is unix:%s/db.sock.\n"
262 "The default DATABASE is Open_vSwitch.\n",
263 program_name
, program_name
, ovs_rundir());
264 stream_usage("SERVER", true, true, true);
265 printf("\nOutput formatting options:\n"
266 " -f, --format=FORMAT set output formatting to FORMAT\n"
267 " (\"table\", \"html\", \"csv\", "
269 " --no-headings omit table heading row\n"
270 " --pretty pretty-print JSON in output\n"
271 " --timestamp timestamp \"monitor\" output");
274 printf("\nOther options:\n"
275 " -h, --help display this help message\n"
276 " -V, --version display version information\n");
281 check_txn(int error
, struct jsonrpc_msg
**reply_
)
283 struct jsonrpc_msg
*reply
= *reply_
;
286 ovs_fatal(error
, "transaction failed");
290 ovs_fatal(error
, "transaction returned error: %s",
291 json_to_string(reply
->error
, table_style
.json_flags
));
296 parse_json(const char *s
)
298 struct json
*json
= json_from_string(s
);
299 if (json
->type
== JSON_STRING
) {
300 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
305 static struct jsonrpc
*
306 open_jsonrpc(const char *server
)
308 struct stream
*stream
;
311 error
= stream_open_block(jsonrpc_stream_open(server
, &stream
,
312 DSCP_DEFAULT
), &stream
);
313 if (error
== EAFNOSUPPORT
) {
314 struct pstream
*pstream
;
316 error
= jsonrpc_pstream_open(server
, &pstream
, DSCP_DEFAULT
);
318 ovs_fatal(error
, "failed to connect or listen to \"%s\"", server
);
321 VLOG_INFO("%s: waiting for connection...", server
);
322 error
= pstream_accept_block(pstream
, &stream
);
324 ovs_fatal(error
, "failed to accept connection on \"%s\"", server
);
327 pstream_close(pstream
);
329 ovs_fatal(error
, "failed to connect to \"%s\"", server
);
332 return jsonrpc_open(stream
);
336 print_json(struct json
*json
)
338 char *string
= json_to_string(json
, table_style
.json_flags
);
339 fputs(string
, stdout
);
344 print_and_free_json(struct json
*json
)
351 check_ovsdb_error(struct ovsdb_error
*error
)
354 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
358 static struct ovsdb_schema
*
359 fetch_schema(struct jsonrpc
*rpc
, const char *database
)
361 struct jsonrpc_msg
*request
, *reply
;
362 struct ovsdb_schema
*schema
;
364 request
= jsonrpc_create_request("get_schema",
366 json_string_create(database
)),
368 check_txn(jsonrpc_transact_block(rpc
, request
, &reply
), &reply
);
369 check_ovsdb_error(ovsdb_schema_from_json(reply
->result
, &schema
));
370 jsonrpc_msg_destroy(reply
);
376 fetch_dbs(struct jsonrpc
*rpc
, struct svec
*dbs
)
378 struct jsonrpc_msg
*request
, *reply
;
381 request
= jsonrpc_create_request("list_dbs", json_array_create_empty(),
384 check_txn(jsonrpc_transact_block(rpc
, request
, &reply
), &reply
);
385 if (reply
->result
->type
!= JSON_ARRAY
) {
386 ovs_fatal(0, "list_dbs response is not array");
389 for (i
= 0; i
< reply
->result
->u
.array
.n
; i
++) {
390 const struct json
*name
= reply
->result
->u
.array
.elems
[i
];
392 if (name
->type
!= JSON_STRING
) {
393 ovs_fatal(0, "list_dbs response %"PRIuSIZE
" is not string", i
);
395 svec_add(dbs
, name
->u
.string
);
397 jsonrpc_msg_destroy(reply
);
402 do_list_dbs(struct jsonrpc
*rpc
, const char *database OVS_UNUSED
,
403 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
410 fetch_dbs(rpc
, &dbs
);
411 SVEC_FOR_EACH (i
, db_name
, &dbs
) {
418 do_get_schema(struct jsonrpc
*rpc
, const char *database
,
419 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
421 struct ovsdb_schema
*schema
= fetch_schema(rpc
, database
);
422 print_and_free_json(ovsdb_schema_to_json(schema
));
423 ovsdb_schema_destroy(schema
);
427 do_get_schema_version(struct jsonrpc
*rpc
, const char *database
,
428 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
430 struct ovsdb_schema
*schema
= fetch_schema(rpc
, database
);
431 puts(schema
->version
);
432 ovsdb_schema_destroy(schema
);
436 do_list_tables(struct jsonrpc
*rpc
, const char *database
,
437 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
439 struct ovsdb_schema
*schema
;
440 struct shash_node
*node
;
443 schema
= fetch_schema(rpc
, database
);
445 table_add_column(&t
, "Table");
446 SHASH_FOR_EACH (node
, &schema
->tables
) {
447 struct ovsdb_table_schema
*ts
= node
->data
;
450 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
452 ovsdb_schema_destroy(schema
);
453 table_print(&t
, &table_style
);
457 do_list_columns(struct jsonrpc
*rpc
, const char *database
,
458 int argc OVS_UNUSED
, char *argv
[])
460 const char *table_name
= argv
[0];
461 struct ovsdb_schema
*schema
;
462 struct shash_node
*table_node
;
465 schema
= fetch_schema(rpc
, database
);
468 table_add_column(&t
, "Table");
470 table_add_column(&t
, "Column");
471 table_add_column(&t
, "Type");
472 SHASH_FOR_EACH (table_node
, &schema
->tables
) {
473 struct ovsdb_table_schema
*ts
= table_node
->data
;
475 if (!table_name
|| !strcmp(table_name
, ts
->name
)) {
476 struct shash_node
*column_node
;
478 SHASH_FOR_EACH (column_node
, &ts
->columns
) {
479 const struct ovsdb_column
*column
= column_node
->data
;
483 table_add_cell(&t
)->text
= xstrdup(ts
->name
);
485 table_add_cell(&t
)->text
= xstrdup(column
->name
);
486 table_add_cell(&t
)->json
= ovsdb_type_to_json(&column
->type
);
490 ovsdb_schema_destroy(schema
);
491 table_print(&t
, &table_style
);
495 do_transact(struct jsonrpc
*rpc
, const char *database OVS_UNUSED
,
496 int argc OVS_UNUSED
, char *argv
[])
498 struct jsonrpc_msg
*request
, *reply
;
499 struct json
*transaction
;
501 transaction
= parse_json(argv
[0]);
503 request
= jsonrpc_create_request("transact", transaction
, NULL
);
504 check_txn(jsonrpc_transact_block(rpc
, request
, &reply
), &reply
);
505 print_json(reply
->result
);
507 jsonrpc_msg_destroy(reply
);
510 /* "monitor" command. */
512 struct monitored_table
{
513 struct ovsdb_table_schema
*table
;
514 struct ovsdb_column_set columns
;
518 monitor_print_row(struct json
*row
, const char *type
, const char *uuid
,
519 const struct ovsdb_column_set
*columns
, struct table
*t
)
524 ovs_error(0, "missing %s row", type
);
526 } else if (row
->type
!= JSON_OBJECT
) {
527 ovs_error(0, "<row> is not object");
532 table_add_cell(t
)->text
= xstrdup(uuid
);
533 table_add_cell(t
)->text
= xstrdup(type
);
534 for (i
= 0; i
< columns
->n_columns
; i
++) {
535 const struct ovsdb_column
*column
= columns
->columns
[i
];
536 struct json
*value
= shash_find_data(json_object(row
), column
->name
);
537 struct cell
*cell
= table_add_cell(t
);
539 cell
->json
= json_clone(value
);
540 cell
->type
= &column
->type
;
546 monitor_print_table(struct json
*table_update
,
547 const struct monitored_table
*mt
, char *caption
,
550 const struct ovsdb_table_schema
*table
= mt
->table
;
551 const struct ovsdb_column_set
*columns
= &mt
->columns
;
552 struct shash_node
*node
;
556 if (table_update
->type
!= JSON_OBJECT
) {
557 ovs_error(0, "<table-update> for table %s is not object", table
->name
);
562 table_set_timestamp(&t
, timestamp
);
563 table_set_caption(&t
, caption
);
565 table_add_column(&t
, "row");
566 table_add_column(&t
, "action");
567 for (i
= 0; i
< columns
->n_columns
; i
++) {
568 table_add_column(&t
, "%s", columns
->columns
[i
]->name
);
570 SHASH_FOR_EACH (node
, json_object(table_update
)) {
571 struct json
*row_update
= node
->data
;
572 struct json
*old
, *new;
574 if (row_update
->type
!= JSON_OBJECT
) {
575 ovs_error(0, "<row-update> is not object");
578 old
= shash_find_data(json_object(row_update
), "old");
579 new = shash_find_data(json_object(row_update
), "new");
581 monitor_print_row(new, "initial", node
->name
, columns
, &t
);
583 monitor_print_row(new, "insert", node
->name
, columns
, &t
);
585 monitor_print_row(old
, "delete", node
->name
, columns
, &t
);
587 monitor_print_row(old
, "old", node
->name
, columns
, &t
);
588 monitor_print_row(new, "new", "", columns
, &t
);
591 table_print(&t
, &table_style
);
596 monitor_print(struct json
*table_updates
,
597 const struct monitored_table
*mts
, size_t n_mts
,
602 if (table_updates
->type
!= JSON_OBJECT
) {
603 ovs_error(0, "<table-updates> is not object");
607 for (i
= 0; i
< n_mts
; i
++) {
608 const struct monitored_table
*mt
= &mts
[i
];
609 struct json
*table_update
= shash_find_data(json_object(table_updates
),
612 monitor_print_table(table_update
, mt
,
613 n_mts
> 1 ? xstrdup(mt
->table
->name
) : NULL
,
620 add_column(const char *server
, const struct ovsdb_column
*column
,
621 struct ovsdb_column_set
*columns
, struct json
*columns_json
)
623 if (ovsdb_column_set_contains(columns
, column
->index
)) {
624 ovs_fatal(0, "%s: column \"%s\" mentioned multiple times",
625 server
, column
->name
);
627 ovsdb_column_set_add(columns
, column
);
628 json_array_add(columns_json
, json_string_create(column
->name
));
632 parse_monitor_columns(char *arg
, const char *server
, const char *database
,
633 const struct ovsdb_table_schema
*table
,
634 struct ovsdb_column_set
*columns
)
636 bool initial
, insert
, delete, modify
;
637 struct json
*mr
, *columns_json
;
638 char *save_ptr
= NULL
;
641 mr
= json_object_create();
642 columns_json
= json_array_create_empty();
643 json_object_put(mr
, "columns", columns_json
);
645 initial
= insert
= delete = modify
= true;
646 for (token
= strtok_r(arg
, ",", &save_ptr
); token
!= NULL
;
647 token
= strtok_r(NULL
, ",", &save_ptr
)) {
648 if (!strcmp(token
, "!initial")) {
650 } else if (!strcmp(token
, "!insert")) {
652 } else if (!strcmp(token
, "!delete")) {
654 } else if (!strcmp(token
, "!modify")) {
657 const struct ovsdb_column
*column
;
659 column
= ovsdb_table_schema_get_column(table
, token
);
661 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
662 "column named \"%s\"",
663 server
, table
->name
, database
, token
);
665 add_column(server
, column
, columns
, columns_json
);
669 if (columns_json
->u
.array
.n
== 0) {
670 const struct shash_node
**nodes
;
673 n
= shash_count(&table
->columns
);
674 nodes
= shash_sort(&table
->columns
);
675 for (i
= 0; i
< n
; i
++) {
676 const struct ovsdb_column
*column
= nodes
[i
]->data
;
677 if (column
->index
!= OVSDB_COL_UUID
678 && column
->index
!= OVSDB_COL_VERSION
) {
679 add_column(server
, column
, columns
, columns_json
);
684 add_column(server
, ovsdb_table_schema_get_column(table
, "_version"),
685 columns
, columns_json
);
688 if (!initial
|| !insert
|| !delete || !modify
) {
689 struct json
*select
= json_object_create();
690 json_object_put(select
, "initial", json_boolean_create(initial
));
691 json_object_put(select
, "insert", json_boolean_create(insert
));
692 json_object_put(select
, "delete", json_boolean_create(delete));
693 json_object_put(select
, "modify", json_boolean_create(modify
));
694 json_object_put(mr
, "select", select
);
701 ovsdb_client_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
702 const char *argv
[] OVS_UNUSED
, void *exiting_
)
704 bool *exiting
= exiting_
;
706 unixctl_command_reply(conn
, NULL
);
710 ovsdb_client_block(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
711 const char *argv
[] OVS_UNUSED
, void *blocked_
)
713 bool *blocked
= blocked_
;
717 unixctl_command_reply(conn
, NULL
);
719 unixctl_command_reply(conn
, "already blocking");
724 ovsdb_client_unblock(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
725 const char *argv
[] OVS_UNUSED
, void *blocked_
)
727 bool *blocked
= blocked_
;
731 unixctl_command_reply(conn
, NULL
);
733 unixctl_command_reply(conn
, "already unblocked");
738 add_monitored_table(int argc
, char *argv
[],
739 const char *server
, const char *database
,
740 struct ovsdb_table_schema
*table
,
741 struct json
*monitor_requests
,
742 struct monitored_table
**mts
,
743 size_t *n_mts
, size_t *allocated_mts
)
745 struct json
*monitor_request_array
;
746 struct monitored_table
*mt
;
748 if (*n_mts
>= *allocated_mts
) {
749 *mts
= x2nrealloc(*mts
, allocated_mts
, sizeof **mts
);
751 mt
= &(*mts
)[(*n_mts
)++];
753 ovsdb_column_set_init(&mt
->columns
);
755 monitor_request_array
= json_array_create_empty();
759 for (i
= 1; i
< argc
; i
++) {
761 monitor_request_array
,
762 parse_monitor_columns(argv
[i
], server
, database
, table
,
766 /* Allocate a writable empty string since parse_monitor_columns()
767 * is going to strtok() it and that's risky with literal "". */
770 monitor_request_array
,
771 parse_monitor_columns(empty
, server
, database
,
772 table
, &mt
->columns
));
775 json_object_put(monitor_requests
, table
->name
, monitor_request_array
);
779 do_monitor(struct jsonrpc
*rpc
, const char *database
,
780 int argc
, char *argv
[])
782 const char *server
= jsonrpc_get_name(rpc
);
783 const char *table_name
= argv
[0];
784 struct unixctl_server
*unixctl
;
785 struct ovsdb_schema
*schema
;
786 struct jsonrpc_msg
*request
;
787 struct json
*monitor
, *monitor_requests
, *request_id
;
788 bool exiting
= false;
789 bool blocked
= false;
791 struct monitored_table
*mts
;
792 size_t n_mts
, allocated_mts
;
794 daemon_save_fd(STDOUT_FILENO
);
795 daemonize_start(false);
799 error
= unixctl_server_create(NULL
, &unixctl
);
801 ovs_fatal(error
, "failed to create unixctl server");
804 unixctl_command_register("exit", "", 0, 0,
805 ovsdb_client_exit
, &exiting
);
806 unixctl_command_register("ovsdb-client/block", "", 0, 0,
807 ovsdb_client_block
, &blocked
);
808 unixctl_command_register("ovsdb-client/unblock", "", 0, 0,
809 ovsdb_client_unblock
, &blocked
);
814 schema
= fetch_schema(rpc
, database
);
816 monitor_requests
= json_object_create();
819 n_mts
= allocated_mts
= 0;
820 if (strcmp(table_name
, "ALL")) {
821 struct ovsdb_table_schema
*table
;
823 table
= shash_find_data(&schema
->tables
, table_name
);
825 ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
826 server
, database
, table_name
);
829 add_monitored_table(argc
, argv
, server
, database
, table
,
830 monitor_requests
, &mts
, &n_mts
, &allocated_mts
);
832 size_t n
= shash_count(&schema
->tables
);
833 const struct shash_node
**nodes
= shash_sort(&schema
->tables
);
836 for (i
= 0; i
< n
; i
++) {
837 struct ovsdb_table_schema
*table
= nodes
[i
]->data
;
839 add_monitored_table(argc
, argv
, server
, database
, table
,
841 &mts
, &n_mts
, &allocated_mts
);
846 monitor
= json_array_create_3(json_string_create(database
),
847 json_null_create(), monitor_requests
);
848 request
= jsonrpc_create_request("monitor", monitor
, NULL
);
849 request_id
= json_clone(request
->id
);
850 jsonrpc_send(rpc
, request
);
853 unixctl_server_run(unixctl
);
855 struct jsonrpc_msg
*msg
;
858 error
= jsonrpc_recv(rpc
, &msg
);
859 if (error
== EAGAIN
) {
862 ovs_fatal(error
, "%s: receive failed", server
);
865 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
866 jsonrpc_send(rpc
, jsonrpc_create_reply(json_clone(msg
->params
),
868 } else if (msg
->type
== JSONRPC_REPLY
869 && json_equal(msg
->id
, request_id
)) {
870 monitor_print(msg
->result
, mts
, n_mts
, true);
872 daemonize_complete();
873 } else if (msg
->type
== JSONRPC_NOTIFY
874 && !strcmp(msg
->method
, "update")) {
875 struct json
*params
= msg
->params
;
876 if (params
->type
== JSON_ARRAY
877 && params
->u
.array
.n
== 2
878 && params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
879 monitor_print(params
->u
.array
.elems
[1], mts
, n_mts
, false);
883 jsonrpc_msg_destroy(msg
);
893 jsonrpc_recv_wait(rpc
);
895 unixctl_server_wait(unixctl
);
900 struct dump_table_aux
{
901 struct ovsdb_datum
**data
;
902 const struct ovsdb_column
**columns
;
907 compare_data(size_t a_y
, size_t b_y
, size_t x
,
908 const struct dump_table_aux
*aux
)
910 return ovsdb_datum_compare_3way(&aux
->data
[a_y
][x
],
912 &aux
->columns
[x
]->type
);
916 compare_rows(size_t a_y
, size_t b_y
, void *aux_
)
918 struct dump_table_aux
*aux
= aux_
;
921 /* Skip UUID columns on the first pass, since their values tend to be
922 * random and make our results less reproducible. */
923 for (x
= 0; x
< aux
->n_columns
; x
++) {
924 if (aux
->columns
[x
]->type
.key
.type
!= OVSDB_TYPE_UUID
) {
925 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
932 /* Use UUID columns as tie-breakers. */
933 for (x
= 0; x
< aux
->n_columns
; x
++) {
934 if (aux
->columns
[x
]->type
.key
.type
== OVSDB_TYPE_UUID
) {
935 int cmp
= compare_data(a_y
, b_y
, x
, aux
);
946 swap_rows(size_t a_y
, size_t b_y
, void *aux_
)
948 struct dump_table_aux
*aux
= aux_
;
949 struct ovsdb_datum
*tmp
= aux
->data
[a_y
];
950 aux
->data
[a_y
] = aux
->data
[b_y
];
951 aux
->data
[b_y
] = tmp
;
955 compare_columns(const void *a_
, const void *b_
)
957 const struct ovsdb_column
*const *ap
= a_
;
958 const struct ovsdb_column
*const *bp
= b_
;
959 const struct ovsdb_column
*a
= *ap
;
960 const struct ovsdb_column
*b
= *bp
;
962 return strcmp(a
->name
, b
->name
);
966 dump_table(const char *table_name
, const struct shash
*cols
,
967 struct json_array
*rows
)
969 const struct ovsdb_column
**columns
;
972 struct ovsdb_datum
**data
;
974 struct dump_table_aux aux
;
975 struct shash_node
*node
;
979 /* Sort columns by name, for reproducibility. */
980 columns
= xmalloc(shash_count(cols
) * sizeof *columns
);
982 SHASH_FOR_EACH (node
, cols
) {
983 struct ovsdb_column
*column
= node
->data
;
984 if (strcmp(column
->name
, "_version")) {
985 columns
[n_columns
++] = column
;
988 qsort(columns
, n_columns
, sizeof *columns
, compare_columns
);
990 /* Extract data from table. */
991 data
= xmalloc(rows
->n
* sizeof *data
);
992 for (y
= 0; y
< rows
->n
; y
++) {
995 if (rows
->elems
[y
]->type
!= JSON_OBJECT
) {
996 ovs_fatal(0, "row %"PRIuSIZE
" in table %s response is not a JSON object: "
997 "%s", y
, table_name
, json_to_string(rows
->elems
[y
], 0));
999 row
= json_object(rows
->elems
[y
]);
1001 data
[y
] = xmalloc(n_columns
* sizeof **data
);
1002 for (x
= 0; x
< n_columns
; x
++) {
1003 const struct json
*json
= shash_find_data(row
, columns
[x
]->name
);
1005 ovs_fatal(0, "row %"PRIuSIZE
" in table %s response lacks %s column",
1006 y
, table_name
, columns
[x
]->name
);
1009 check_ovsdb_error(ovsdb_datum_from_json(&data
[y
][x
],
1015 /* Sort rows by column values, for reproducibility. */
1017 aux
.columns
= columns
;
1018 aux
.n_columns
= n_columns
;
1019 sort(rows
->n
, compare_rows
, swap_rows
, &aux
);
1021 /* Add column headings. */
1023 table_set_caption(&t
, xasprintf("%s table", table_name
));
1024 for (x
= 0; x
< n_columns
; x
++) {
1025 table_add_column(&t
, "%s", columns
[x
]->name
);
1029 for (y
= 0; y
< rows
->n
; y
++) {
1031 for (x
= 0; x
< n_columns
; x
++) {
1032 struct cell
*cell
= table_add_cell(&t
);
1033 cell
->json
= ovsdb_datum_to_json(&data
[y
][x
], &columns
[x
]->type
);
1034 cell
->type
= &columns
[x
]->type
;
1035 ovsdb_datum_destroy(&data
[y
][x
], &columns
[x
]->type
);
1039 table_print(&t
, &table_style
);
1047 do_dump(struct jsonrpc
*rpc
, const char *database
,
1048 int argc
, char *argv
[])
1050 struct jsonrpc_msg
*request
, *reply
;
1051 struct ovsdb_schema
*schema
;
1052 struct json
*transaction
;
1054 const struct shash_node
*node
, **tables
;
1056 struct ovsdb_table_schema
*tschema
;
1057 const struct shash
*columns
;
1058 struct shash custom_columns
;
1062 shash_init(&custom_columns
);
1063 schema
= fetch_schema(rpc
, database
);
1065 node
= shash_find(&schema
->tables
, argv
[0]);
1067 ovs_fatal(0, "No table \"%s\" found.", argv
[0]);
1069 tables
= xmemdup(&node
, sizeof(&node
));
1071 tschema
= tables
[0]->data
;
1072 for (i
= 1; i
< argc
; i
++) {
1073 node
= shash_find(&tschema
->columns
, argv
[i
]);
1075 ovs_fatal(0, "Table \"%s\" has no column %s.", argv
[0], argv
[1]);
1077 shash_add(&custom_columns
, argv
[1], node
->data
);
1080 tables
= shash_sort(&schema
->tables
);
1081 n_tables
= shash_count(&schema
->tables
);
1084 /* Construct transaction to retrieve entire database. */
1085 transaction
= json_array_create_1(json_string_create(database
));
1086 for (i
= 0; i
< n_tables
; i
++) {
1087 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
1088 struct json
*op
, *jcolumns
;
1091 columns
= &custom_columns
;
1093 columns
= &ts
->columns
;
1095 jcolumns
= json_array_create_empty();
1096 SHASH_FOR_EACH (node
, columns
) {
1097 const struct ovsdb_column
*column
= node
->data
;
1099 if (strcmp(column
->name
, "_version")) {
1100 json_array_add(jcolumns
, json_string_create(column
->name
));
1104 op
= json_object_create();
1105 json_object_put_string(op
, "op", "select");
1106 json_object_put_string(op
, "table", tables
[i
]->name
);
1107 json_object_put(op
, "where", json_array_create_empty());
1108 json_object_put(op
, "columns", jcolumns
);
1109 json_array_add(transaction
, op
);
1112 /* Send request, get reply. */
1113 request
= jsonrpc_create_request("transact", transaction
, NULL
);
1114 check_txn(jsonrpc_transact_block(rpc
, request
, &reply
), &reply
);
1116 /* Print database contents. */
1117 if (reply
->result
->type
!= JSON_ARRAY
1118 || reply
->result
->u
.array
.n
!= n_tables
) {
1119 ovs_fatal(0, "reply is not array of %"PRIuSIZE
" elements: %s",
1120 n_tables
, json_to_string(reply
->result
, 0));
1122 for (i
= 0; i
< n_tables
; i
++) {
1123 const struct ovsdb_table_schema
*ts
= tables
[i
]->data
;
1124 const struct json
*op_result
= reply
->result
->u
.array
.elems
[i
];
1127 if (op_result
->type
!= JSON_OBJECT
1128 || !(rows
= shash_find_data(json_object(op_result
), "rows"))
1129 || rows
->type
!= JSON_ARRAY
) {
1130 ovs_fatal(0, "%s table reply is not an object with a \"rows\" "
1132 ts
->name
, json_to_string(op_result
, 0));
1136 dump_table(tables
[i
]->name
, &custom_columns
, &rows
->u
.array
);
1138 dump_table(tables
[i
]->name
, &ts
->columns
, &rows
->u
.array
);
1142 jsonrpc_msg_destroy(reply
);
1143 shash_destroy(&custom_columns
);
1145 ovsdb_schema_destroy(schema
);
1149 do_help(struct jsonrpc
*rpc OVS_UNUSED
, const char *database OVS_UNUSED
,
1150 int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1155 /* All command handlers (except for "help") are expected to take an optional
1156 * server socket name (e.g. "unix:...") as their first argument. The socket
1157 * name argument must be included in max_args (but left out of min_args). The
1158 * command name and socket name are not included in the arguments passed to the
1159 * handler: the argv[0] passed to the handler is the first argument after the
1160 * optional server socket name. The connection to the server is available as
1161 * global variable 'rpc'. */
1162 static const struct ovsdb_client_command all_commands
[] = {
1163 { "list-dbs", NEED_RPC
, 0, 0, do_list_dbs
},
1164 { "get-schema", NEED_DATABASE
, 0, 0, do_get_schema
},
1165 { "get-schema-version", NEED_DATABASE
, 0, 0, do_get_schema_version
},
1166 { "list-tables", NEED_DATABASE
, 0, 0, do_list_tables
},
1167 { "list-columns", NEED_DATABASE
, 0, 1, do_list_columns
},
1168 { "transact", NEED_RPC
, 1, 1, do_transact
},
1169 { "monitor", NEED_DATABASE
, 1, INT_MAX
, do_monitor
},
1170 { "dump", NEED_DATABASE
, 0, INT_MAX
, do_dump
},
1172 { "help", NEED_NONE
, 0, INT_MAX
, do_help
},
1174 { NULL
, 0, 0, 0, NULL
},
1177 static const struct ovsdb_client_command
*get_all_commands(void)
1179 return all_commands
;